forked from moodleou/moodle-mod_oublog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternallib.php
309 lines (294 loc) · 15.4 KB
/
externallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* External oublog API.
* Used for importing posts from another server.
*
* @package mod
* @subpackage oublog
* @copyright 2013 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("$CFG->libdir/externallib.php");
require_once("$CFG->dirroot/mod/oublog/locallib.php");
class mod_oublog_external extends external_api {
public static function get_user_blogs_parameters() {
return new external_function_parameters(array(
'username' => new external_value(PARAM_USERNAME, 'User username')
));
}
/**
* Return all blogs on the system for the user
* @param string $username
*/
public static function get_user_blogs($username) {
global $DB, $remoteuserid;
$username = self::validate_parameters(self::get_user_blogs_parameters(),
array('username' => $username));
$user = $DB->get_field('user', 'id', array('username' => $username['username']), IGNORE_MISSING);
if (!$user) {
return array();
}
$remoteuserid = $user;
$result = oublog_import_getblogs($user);
// Add remote property to each blog to identify that it came from web service.
foreach ($result as &$blog) {
$blog->remote = true;
}
return $result;
}
public static function get_user_blogs_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'cmid' => new external_value(PARAM_INT, 'blog course module id'),
'coursename' => new external_value(PARAM_TEXT, 'course name text'),
'numposts' => new external_value(PARAM_INT, 'number of posts in blog'),
'name' => new external_value(PARAM_TEXT, 'activity name text'),
'remote' => new external_value(PARAM_BOOL, 'identifies activity is remote'),
)
)
);
}
public static function get_blog_info_parameters() {
return new external_function_parameters(
array(
'cmid' => new external_value(PARAM_INT, 'Blog cm id'),
'username' => new external_value(PARAM_USERNAME, 'User username'),
)
);
}
public static function get_blog_info($cmid, $username) {
global $DB, $remoteuserid;
$params = self::validate_parameters(self::get_blog_info_parameters(),
array('cmid' => $cmid, 'username' => $username));
$user = $DB->get_field('user', 'id', array('username' => $params['username']), IGNORE_MISSING);
if (!$user) {
return array();
}
$remoteuserid = $user;
$result = oublog_import_getbloginfo($params['cmid'], $user);
return array(
'bcmid' => $result[0],
'boublogid' => $result[1],
'bcontextid' => $result[2],
'boublogname' => $result[3],
'bcoursename' => $result[4],
);
}
public static function get_blog_info_returns() {
return new external_single_structure(
array(
'bcmid' => new external_value(PARAM_INT, 'Blog cm id'),
'boublogid' => new external_value(PARAM_INT, 'Blog id'),
'bcontextid' => new external_value(PARAM_INT, 'Blog context id'),
'boublogname' => new external_value(PARAM_TEXT, 'Blog name'),
'bcoursename' => new external_value(PARAM_TEXT, 'Course short name'),
));
}
public static function get_blog_allposts_parameters() {
return new external_function_parameters(
array(
'blogid' => new external_value(PARAM_INT, 'Blog id'),
'sort' => new external_value(PARAM_TEXT, 'sort sql'),
'username' => new external_value(PARAM_USERNAME, 'User username'),
'page' => new external_value(PARAM_INT, 'results page', VALUE_DEFAULT, 0),
'tags' => new external_value(PARAM_SEQUENCE, 'tags to filter by', VALUE_DEFAULT, null),
)
);
}
/**
* Gets all user posts from blog, filtered by page and tags
* @param int $blogid
* @param string $sort
* @param string $username
* @param int $page
* @param string $tags comma separated sequence of selected tag ids to filter by
* @return array
*/
public static function get_blog_allposts($blogid, $sort, $username, $page = 0, $tags = null) {
global $DB;
$params = self::validate_parameters(self::get_blog_allposts_parameters(),
array('blogid' => $blogid, 'sort' => $sort, 'username' => $username,
'page' => $page, 'tags' => $tags));
$user = $DB->get_field('user', 'id', array('username' => $params['username']), IGNORE_MISSING);
if (!$user) {
return array();
}
$result = oublog_import_getallposts($params['blogid'], $params['sort'], $user,
$params['page'], $params['tags']);
if (!is_array($result[2])) {
$result[2] = array();
}
foreach ($result[0] as &$post) {
if (isset($post->tags)) {
$tagupdate = array();
// Update post tags into a format that Moodle WS can work with.
foreach ($post->tags as $id => $tag) {
$tagupdate[] = (object) array('id' => $id, 'tag' => $tag);
}
$post->tags = $tagupdate;
}
}
return array('posts' => $result[0], 'total' => $result[1], 'tagnames' => $result[2]);
}
public static function get_blog_allposts_returns() {
return new external_single_structure(array(
'posts' => new external_multiple_structure(new external_single_structure(array(
'id' => new external_value(PARAM_INT, 'post id'),
'title' => new external_value(PARAM_TEXT, 'title'),
'timeposted' => new external_value(PARAM_INT, 'created'),
'tags' => new external_multiple_structure(new external_single_structure(array(
'id' => new external_value(PARAM_INT, 'tag id'),
'tag' => new external_value(PARAM_TEXT, 'tag value'))
), 'tags', VALUE_OPTIONAL),
))),
'total' => new external_value(PARAM_INT, 'total user posts in blog'),
'tagnames' => new external_multiple_structure(
new external_single_structure(array(
'id' => new external_value(PARAM_INT, 'tag id'),
'tag' => new external_value(PARAM_TEXT, 'tag value'),
)), 'tags', VALUE_OPTIONAL)
));
}
public static function get_blog_posts_parameters() {
return new external_function_parameters(
array(
'blogid' => new external_value(PARAM_INT, 'Blog id'),
'bcontextid' => new external_value(PARAM_INT, 'Blog module context id'),
'selected' => new external_value(PARAM_SEQUENCE, 'post ids'),
'inccomments' => new external_value(PARAM_BOOL, 'include comments',
VALUE_DEFAULT, false),
'username' => new external_value(PARAM_USERNAME, 'User username'),
)
);
}
/**
* Get selected blog posts from blog
* @param int $blogid
* @param string $selected comma separated sequence of selected post ids to filter by
* @param bool $inccomments - blog uses comments or not
* @param string $username - used to ensure user posts only
* @return array of posts
*/
public static function get_blog_posts($blogid, $bcontextid, $selected, $inccomments = false, $username) {
global $DB;
$params = self::validate_parameters(self::get_blog_posts_parameters(),
array('blogid' => $blogid, 'bcontextid' => $bcontextid, 'selected' => $selected,
'inccomments' => $inccomments, 'username' => $username));
$user = $DB->get_field('user', 'id', array('username' => $username), IGNORE_MISSING);
if (!$user) {
return array();
}
$selected = explode(',', $params['selected']);
if ($selected[0] == 0) {
$return = oublog_import_getposts($params['blogid'], $params['bcontextid'],
$selected, $params['inccomments'], $user, true);
} else {
$return = oublog_import_getposts($params['blogid'], $params['bcontextid'],
$selected, $params['inccomments'], $user);
}
// Convert file objects into a custom known object to send.
foreach ($return as &$post) {
foreach ($post->images as &$file) {
$file = (object) array(
'contextid' => $file->get_contextid(),
'filearea' => $file->get_filearea(),
'filepath' => $file->get_filepath(),
'filename' => $file->get_filename(),
'itemid' => $file->get_itemid()
);
}
foreach ($post->attachments as &$file) {
$file = (object) array(
'contextid' => $file->get_contextid(),
'filearea' => $file->get_filearea(),
'filepath' => $file->get_filepath(),
'filename' => $file->get_filename(),
'itemid' => $file->get_itemid()
);
}
foreach ($post->comments as &$comment) {
foreach ($comment->images as &$file) {
$file = (object) array(
'contextid' => $file->get_contextid(),
'filearea' => $file->get_filearea(),
'filepath' => $file->get_filepath(),
'filename' => $file->get_filename(),
'itemid' => $file->get_itemid()
);
}
}
}
return $return;
}
public static function get_blog_posts_returns() {
return new external_multiple_structure(new external_single_structure(array(
'id' => new external_value(PARAM_INT, 'post id'),
'oubloginstancesid' => new external_value(PARAM_INT, 'instance id'),
'groupid' => new external_value(PARAM_INT, 'group id'),
'title' => new external_value(PARAM_TEXT, 'title'),
'message' => new external_value(PARAM_RAW, 'message'),
'timeposted' => new external_value(PARAM_INT, 'created'),
'allowcomments' => new external_value(PARAM_INT, 'comments allowed'),
'timeupdated' => new external_value(PARAM_INT, 'updated'),
'deletedby' => new external_value(PARAM_INT, 'deleted by'),
'timedeleted' => new external_value(PARAM_INT, 'deleted'),
'visibility' => new external_value(PARAM_INT, 'visibility'),
'lasteditedby' => new external_value(PARAM_INT, 'edited by'),
'tags' => new external_multiple_structure(new external_single_structure(array(
'id' => new external_value(PARAM_INT, 'tag id'),
'tag' => new external_value(PARAM_TEXT, 'tag value'),
'postid' => new external_value(PARAM_INT, 'tag post id'),
)), 'tags', VALUE_OPTIONAL),
'images' => new external_multiple_structure(new external_single_structure(array(
'contextid' => new external_value(PARAM_INT, 'context id'),
'filearea' => new external_value(PARAM_AREA, 'filearea'),
'filepath' => new external_value(PARAM_PATH, 'path'),
'filename' => new external_value(PARAM_FILE, 'filename'),
'itemid' => new external_value(PARAM_INT, 'item id'),
)), 'images', VALUE_OPTIONAL),
'attachments' => new external_multiple_structure(new external_single_structure(array(
'contextid' => new external_value(PARAM_INT, 'context id'),
'filearea' => new external_value(PARAM_AREA, 'filearea'),
'filepath' => new external_value(PARAM_PATH, 'filepath'),
'filename' => new external_value(PARAM_FILE, 'filename'),
'itemid' => new external_value(PARAM_INT, 'item id'),
)), 'attachments', VALUE_OPTIONAL),
'comments' => new external_multiple_structure(
new external_single_structure(array(
'id' => new external_value(PARAM_INT, 'id'),
'postid' => new external_value(PARAM_INT, 'post id'),
'userid' => new external_value(PARAM_INT, 'user id'),
'title' => new external_value(PARAM_TEXT, 'title'),
'message' => new external_value(PARAM_RAW, 'message'),
'timeposted' => new external_value(PARAM_INT, 'posted'),
'deletedby' => new external_value(PARAM_INT, 'deleted by'),
'timedeleted' => new external_value(PARAM_INT, 'deleted'),
'authorname' => new external_value(PARAM_INT, 'ex author'),
'authorip' => new external_value(PARAM_INT, 'ex author ip'),
'timeapproved' => new external_value(PARAM_INT, 'approved'),
'images' => new external_multiple_structure(new external_single_structure(array(
'contextid' => new external_value(PARAM_INT, 'context id'),
'filearea' => new external_value(PARAM_AREA, 'filearea'),
'filepath' => new external_value(PARAM_PATH, 'path'),
'filename' => new external_value(PARAM_FILE, 'filename'),
'itemid' => new external_value(PARAM_INT, 'item id'),
)), 'images', VALUE_OPTIONAL),
)), 'comments', VALUE_OPTIONAL)
)));
}
}