1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Forum repository class to encapsulate all of the AJAX requests that subscribe or unsubscribe
18 * can be sent for forum.
20 * @module mod_forum/repository
22 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define(['core/ajax'], function(Ajax) {
27 * Set the subscription state for a discussion in a forum.
29 * @param {number} forumId ID of the forum the discussion belongs to
30 * @param {number} discussionId ID of the discussion with the subscription state
31 * @param {boolean} targetState Set the subscribed state. True == subscribed; false == unsubscribed.
32 * @return {object} jQuery promise
34 var setDiscussionSubscriptionState = function(forumId, discussionId, targetState) {
36 methodname: 'mod_forum_set_subscription_state',
39 discussionid: discussionId,
40 targetstate: targetState
43 return Ajax.call([request])[0];
46 var addDiscussionPost = function(postid, subject, message, messageformat, isprivatereply, topreferredformat) {
48 methodname: 'mod_forum_add_discussion_post',
52 messageformat: messageformat,
56 value: isprivatereply,
58 name: "topreferredformat",
59 value: topreferredformat,
63 return Ajax.call([request])[0];
67 * Set the favourite state for a discussion in a forum.
69 * @param {number} forumId ID of the forum the discussion belongs to
70 * @param {number} discussionId ID of the discussion with the subscription state
71 * @param {null|date} targetState Set the favourite state. True == favourited; false == unfavourited.
72 * @return {object} jQuery promise
74 var setFavouriteDiscussionState = function(forumId, discussionId, targetState) {
76 methodname: 'mod_forum_toggle_favourite_state',
78 discussionid: discussionId,
79 targetstate: targetState
82 return Ajax.call([request])[0];
85 var setDiscussionLockState = function(forumId, discussionId, targetState) {
87 methodname: 'mod_forum_set_lock_state',
90 discussionid: discussionId,
91 targetstate: targetState}
93 return Ajax.call([request])[0];
97 * Set the pinned state for the discussion provided.
99 * @param {number} forumid
100 * @param {number} discussionid
101 * @param {boolean} targetstate
102 * @return {*|Promise}
104 var setPinDiscussionState = function(forumid, discussionid, targetstate) {
106 methodname: 'mod_forum_set_pin_state',
108 discussionid: discussionid,
109 targetstate: targetstate
112 return Ajax.call([request])[0];
116 * Get the discussions for the user and cmid provided.
118 * @param {number} userid
119 * @param {number} cmid
120 * @param {string} sortby
121 * @param {string} sortdirection
122 * @return {*|Promise}
124 var getDiscussionByUserID = function(userid, cmid, sortby = 'modified', sortdirection = 'DESC') {
126 methodname: 'mod_forum_get_discussion_posts_by_userid',
131 sortdirection: sortdirection,
134 return Ajax.call([request])[0];
138 * Get the posts for the discussion ID provided.
140 * @param {number} discussionId
141 * @param {String} sortby
142 * @param {String} sortdirection
143 * @return {*|Promise}
145 var getDiscussionPosts = function(discussionId, sortby = 'created', sortdirection = 'ASC') {
147 methodname: 'mod_forum_get_discussion_posts',
149 discussionid: discussionId,
151 sortdirection: sortdirection,
154 return Ajax.call([request])[0];
158 setDiscussionSubscriptionState: setDiscussionSubscriptionState,
159 addDiscussionPost: addDiscussionPost,
160 setDiscussionLockState: setDiscussionLockState,
161 setFavouriteDiscussionState: setFavouriteDiscussionState,
162 setPinDiscussionState: setPinDiscussionState,
163 getDiscussionByUserID: getDiscussionByUserID,
164 getDiscussionPosts: getDiscussionPosts,