mod/forum/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. * Forum repository class to encapsulate all of the AJAX requests that subscribe or unsubscribe
  17. * can be sent for forum.
  18. *
  19. * @module mod_forum/repository
  20. * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. define(['core/ajax'], function(Ajax) {
  24. /**
  25. * Set the subscription state for a discussion in a forum.
  26. *
  27. * @param {number} forumId ID of the forum the discussion belongs to
  28. * @param {number} discussionId ID of the discussion with the subscription state
  29. * @param {boolean} targetState Set the subscribed state. True == subscribed; false == unsubscribed.
  30. * @return {object} jQuery promise
  31. */
  32. var setDiscussionSubscriptionState = function(forumId, discussionId, targetState) {
  33. var request = {
  34. methodname: 'mod_forum_set_subscription_state',
  35. args: {
  36. forumid: forumId,
  37. discussionid: discussionId,
  38. targetstate: targetState
  39. }
  40. };
  41. return Ajax.call([request])[0];
  42. };
  43. var addDiscussionPost = function(postid, subject, message, messageformat, isprivatereply, topreferredformat) {
  44. var request = {
  45. methodname: 'mod_forum_add_discussion_post',
  46. args: {
  47. postid: postid,
  48. message: message,
  49. messageformat: messageformat,
  50. subject: subject,
  51. options: [{
  52. name: "private",
  53. value: isprivatereply,
  54. }, {
  55. name: "topreferredformat",
  56. value: topreferredformat,
  57. }]
  58. }
  59. };
  60. return Ajax.call([request])[0];
  61. };
  62. /**
  63. * Set the favourite state for a discussion in a forum.
  64. *
  65. * @param {number} forumId ID of the forum the discussion belongs to
  66. * @param {number} discussionId ID of the discussion with the subscription state
  67. * @param {null|date} targetState Set the favourite state. True == favourited; false == unfavourited.
  68. * @return {object} jQuery promise
  69. */
  70. var setFavouriteDiscussionState = function(forumId, discussionId, targetState) {
  71. var request = {
  72. methodname: 'mod_forum_toggle_favourite_state',
  73. args: {
  74. discussionid: discussionId,
  75. targetstate: targetState
  76. }
  77. };
  78. return Ajax.call([request])[0];
  79. };
  80. var setDiscussionLockState = function(forumId, discussionId, targetState) {
  81. var request = {
  82. methodname: 'mod_forum_set_lock_state',
  83. args: {
  84. forumid: forumId,
  85. discussionid: discussionId,
  86. targetstate: targetState}
  87. };
  88. return Ajax.call([request])[0];
  89. };
  90. /**
  91. * Set the pinned state for the discussion provided.
  92. *
  93. * @param {number} forumid
  94. * @param {number} discussionid
  95. * @param {boolean} targetstate
  96. * @return {*|Promise}
  97. */
  98. var setPinDiscussionState = function(forumid, discussionid, targetstate) {
  99. var request = {
  100. methodname: 'mod_forum_set_pin_state',
  101. args: {
  102. discussionid: discussionid,
  103. targetstate: targetstate
  104. }
  105. };
  106. return Ajax.call([request])[0];
  107. };
  108. /**
  109. * Get the discussions for the user and cmid provided.
  110. *
  111. * @param {number} userid
  112. * @param {number} cmid
  113. * @param {string} sortby
  114. * @param {string} sortdirection
  115. * @return {*|Promise}
  116. */
  117. var getDiscussionByUserID = function(userid, cmid, sortby = 'modified', sortdirection = 'DESC') {
  118. var request = {
  119. methodname: 'mod_forum_get_discussion_posts_by_userid',
  120. args: {
  121. userid: userid,
  122. cmid: cmid,
  123. sortby: sortby,
  124. sortdirection: sortdirection,
  125. },
  126. };
  127. return Ajax.call([request])[0];
  128. };
  129. /**
  130. * Get the posts for the discussion ID provided.
  131. *
  132. * @param {number} discussionId
  133. * @param {String} sortby
  134. * @param {String} sortdirection
  135. * @return {*|Promise}
  136. */
  137. var getDiscussionPosts = function(discussionId, sortby = 'created', sortdirection = 'ASC') {
  138. var request = {
  139. methodname: 'mod_forum_get_discussion_posts',
  140. args: {
  141. discussionid: discussionId,
  142. sortby: sortby,
  143. sortdirection: sortdirection,
  144. },
  145. };
  146. return Ajax.call([request])[0];
  147. };
  148. return {
  149. setDiscussionSubscriptionState: setDiscussionSubscriptionState,
  150. addDiscussionPost: addDiscussionPost,
  151. setDiscussionLockState: setDiscussionLockState,
  152. setFavouriteDiscussionState: setFavouriteDiscussionState,
  153. setPinDiscussionState: setPinDiscussionState,
  154. getDiscussionByUserID: getDiscussionByUserID,
  155. getDiscussionPosts: getDiscussionPosts,
  156. };
  157. });