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