calendar/amd/src/repository.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * A javascript module to handle calendar ajax actions.
  17. *
  18. * @module core_calendar/repository
  19. * @copyright 2017 Simey Lameze <lameze@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import Ajax from 'core/ajax';
  23. /**
  24. * Delete a calendar event.
  25. *
  26. * @method deleteEvent
  27. * @param {number} eventId The event id.
  28. * @param {boolean} deleteSeries Whether to delete all events in the series
  29. * @return {promise} Resolved with requested calendar event
  30. */
  31. export const deleteEvent = (eventId, deleteSeries = false) => {
  32. const request = {
  33. methodname: 'core_calendar_delete_calendar_events',
  34. args: {
  35. events: [{
  36. eventid: eventId,
  37. repeat: deleteSeries,
  38. }]
  39. }
  40. };
  41. return Ajax.call([request])[0];
  42. };
  43. /**
  44. * Get a calendar event by id.
  45. *
  46. * @method getEventById
  47. * @param {number} eventId The event id.
  48. * @return {promise} Resolved with requested calendar event
  49. */
  50. export const getEventById = (eventId) => {
  51. const request = {
  52. methodname: 'core_calendar_get_calendar_event_by_id',
  53. args: {
  54. eventid: eventId
  55. }
  56. };
  57. return Ajax.call([request])[0];
  58. };
  59. /**
  60. * Submit the form data for the event form.
  61. *
  62. * @method submitCreateUpdateForm
  63. * @param {string} formData The URL encoded values from the form
  64. * @return {promise} Resolved with the new or edited event
  65. */
  66. export const submitCreateUpdateForm = (formData) => {
  67. const request = {
  68. methodname: 'core_calendar_submit_create_update_form',
  69. args: {
  70. formdata: formData
  71. }
  72. };
  73. return Ajax.call([request])[0];
  74. };
  75. /**
  76. * Get calendar data for the month view.
  77. *
  78. * @method getCalendarMonthData
  79. * @param {number} year Year
  80. * @param {number} month Month
  81. * @param {number} courseId The course id.
  82. * @param {number} categoryId The category id.
  83. * @param {boolean} includeNavigation Whether to include navigation.
  84. * @param {boolean} mini Whether the month is in mini view.
  85. * @param {number} day Day (optional)
  86. * @param {string} view The calendar view mode.
  87. * @return {promise} Resolved with the month view data.
  88. */
  89. export const getCalendarMonthData = (year, month, courseId, categoryId, includeNavigation, mini, day = 1, view = 'month') => {
  90. const request = {
  91. methodname: 'core_calendar_get_calendar_monthly_view',
  92. args: {
  93. year,
  94. month,
  95. courseid: courseId,
  96. categoryid: categoryId,
  97. includenavigation: includeNavigation,
  98. mini,
  99. day,
  100. view,
  101. }
  102. };
  103. return Ajax.call([request])[0];
  104. };
  105. /**
  106. * Get calendar data for the day view.
  107. *
  108. * @method getCalendarDayData
  109. * @param {number} year Year
  110. * @param {number} month Month
  111. * @param {number} day Day
  112. * @param {number} courseId The course id.
  113. * @param {number} categoryId The id of the category whose events are shown
  114. * @return {promise} Resolved with the day view data.
  115. */
  116. export const getCalendarDayData = (year, month, day, courseId, categoryId) => {
  117. const request = {
  118. methodname: 'core_calendar_get_calendar_day_view',
  119. args: {
  120. year,
  121. month,
  122. day,
  123. courseid: courseId,
  124. categoryid: categoryId,
  125. }
  126. };
  127. return Ajax.call([request])[0];
  128. };
  129. /**
  130. * Change the start day for the given event id. The day timestamp
  131. * only has to be any time during the target day because only the
  132. * date information is extracted, the time of the day is ignored.
  133. *
  134. * @param {int} eventId The id of the event to update
  135. * @param {int} dayTimestamp A timestamp for some time during the target day
  136. * @return {promise}
  137. */
  138. export const updateEventStartDay = (eventId, dayTimestamp) => {
  139. const request = {
  140. methodname: 'core_calendar_update_event_start_day',
  141. args: {
  142. eventid: eventId,
  143. daytimestamp: dayTimestamp
  144. }
  145. };
  146. return Ajax.call([request])[0];
  147. };
  148. /**
  149. * Get calendar upcoming data.
  150. *
  151. * @method getCalendarUpcomingData
  152. * @param {number} courseId The course id.
  153. * @param {number} categoryId The category id.
  154. * @return {promise} Resolved with the month view data.
  155. */
  156. export const getCalendarUpcomingData = (courseId, categoryId) => {
  157. const request = {
  158. methodname: 'core_calendar_get_calendar_upcoming_view',
  159. args: {
  160. courseid: courseId,
  161. categoryid: categoryId,
  162. }
  163. };
  164. return Ajax.call([request])[0];
  165. };
  166. /**
  167. * Get the groups by course id.
  168. *
  169. * @param {Number} courseId The course id to fetch the groups from.
  170. * @return {promise} Resolved with the course groups.
  171. */
  172. export const getCourseGroupsData = (courseId) => {
  173. const request = {
  174. methodname: 'core_group_get_course_groups',
  175. args: {
  176. courseid: courseId
  177. }
  178. };
  179. return Ajax.call([request])[0];
  180. };
  181. /**
  182. * Delete calendar subscription by id.
  183. *
  184. * @param {Number} subscriptionId The subscription id
  185. * @return {promise}
  186. */
  187. export const deleteSubscription = (subscriptionId) => {
  188. const request = {
  189. methodname: 'core_calendar_delete_subscription',
  190. args: {
  191. subscriptionid: subscriptionId
  192. }
  193. };
  194. return Ajax.call([request])[0];
  195. };