forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externallib.php
528 lines (482 loc) · 21.1 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<?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 notes API
*
* @package core_notes
* @category external
* @copyright 2011 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("$CFG->libdir/externallib.php");
/**
* Notes external functions
*
* @package core_notes
* @category external
* @copyright 2011 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.2
*/
class core_notes_external extends external_api {
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 2.2
*/
public static function create_notes_parameters() {
return new external_function_parameters(
array(
'notes' => new external_multiple_structure(
new external_single_structure(
array(
'userid' => new external_value(PARAM_INT, 'id of the user the note is about'),
'publishstate' => new external_value(PARAM_ALPHA, '\'personal\', \'course\' or \'site\''),
'courseid' => new external_value(PARAM_INT, 'course id of the note (in Moodle a note can only be created into a course, even for site and personal notes)'),
'text' => new external_value(PARAM_RAW, 'the text of the message - text or HTML'),
'format' => new external_format_value('text', VALUE_DEFAULT),
'clientnoteid' => new external_value(PARAM_ALPHANUMEXT, 'your own client id for the note. If this id is provided, the fail message id will be returned to you', VALUE_OPTIONAL),
)
)
)
)
);
}
/**
* Create notes about some users
* Note: code should be matching the /notes/edit.php checks
* and the /user/addnote.php checks. (they are similar cheks)
*
* @param array $notes An array of notes to create.
* @return array (success infos and fail infos)
* @since Moodle 2.2
*/
public static function create_notes($notes = array()) {
global $CFG, $DB;
require_once($CFG->dirroot . "/notes/lib.php");
$params = self::validate_parameters(self::create_notes_parameters(), array('notes' => $notes));
// Check if note system is enabled.
if (!$CFG->enablenotes) {
throw new moodle_exception('notesdisabled', 'notes');
}
// Retrieve all courses.
$courseids = array();
foreach ($params['notes'] as $note) {
$courseids[] = $note['courseid'];
}
$courses = $DB->get_records_list("course", "id", $courseids);
// Retrieve all users of the notes.
$userids = array();
foreach ($params['notes'] as $note) {
$userids[] = $note['userid'];
}
list($sqluserids, $sqlparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'userid_');
$users = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
$resultnotes = array();
foreach ($params['notes'] as $note) {
$success = true;
$resultnote = array(); // The infos about the success of the operation.
// Check the course exists.
if (empty($courses[$note['courseid']])) {
$success = false;
$errormessage = get_string('invalidcourseid', 'error');
} else {
// Ensure the current user is allowed to run this function.
$context = context_course::instance($note['courseid']);
self::validate_context($context);
require_capability('moodle/notes:manage', $context);
}
// Check the user exists.
if (empty($users[$note['userid']])) {
$success = false;
$errormessage = get_string('invaliduserid', 'notes', $note['userid']);
}
// Build the resultnote.
if (isset($note['clientnoteid'])) {
$resultnote['clientnoteid'] = $note['clientnoteid'];
}
if ($success) {
// Now we can create the note.
$dbnote = new stdClass;
$dbnote->courseid = $note['courseid'];
$dbnote->userid = $note['userid'];
// Need to support 'html' and 'text' format values for backward compatibility.
switch (strtolower($note['format'])) {
case 'html':
$textformat = FORMAT_HTML;
break;
case 'text':
$textformat = FORMAT_PLAIN;
default:
$textformat = external_validate_format($note['format']);
break;
}
$dbnote->content = $note['text'];
$dbnote->format = $textformat;
// Get the state ('personal', 'course', 'site').
switch ($note['publishstate']) {
case 'personal':
$dbnote->publishstate = NOTES_STATE_DRAFT;
break;
case 'course':
$dbnote->publishstate = NOTES_STATE_PUBLIC;
break;
case 'site':
$dbnote->publishstate = NOTES_STATE_SITE;
$dbnote->courseid = SITEID;
break;
default:
break;
}
// TODO MDL-31119 performance improvement - if possible create a bulk functions for saving multiple notes at once
if (note_save($dbnote)) { // Note_save attribut an id in case of success.
$success = $dbnote->id;
}
$resultnote['noteid'] = $success;
} else {
// WARNINGS: for backward compatibility we return this errormessage.
// We should have thrown exceptions as these errors prevent results to be returned.
// See http://docs.moodle.org/dev/Errors_handling_in_web_services#When_to_send_a_warning_on_the_server_side .
$resultnote['noteid'] = -1;
$resultnote['errormessage'] = $errormessage;
}
$resultnotes[] = $resultnote;
}
return $resultnotes;
}
/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 2.2
*/
public static function create_notes_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'clientnoteid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the note', VALUE_OPTIONAL),
'noteid' => new external_value(PARAM_INT, 'ID of the created note when successful, -1 when failed'),
'errormessage' => new external_value(PARAM_TEXT, 'error message - if failed', VALUE_OPTIONAL)
)
)
);
}
/**
* Returns description of delete_notes parameters
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function delete_notes_parameters() {
return new external_function_parameters(
array(
"notes"=> new external_multiple_structure(
new external_value(PARAM_INT, 'ID of the note to be deleted'), 'Array of Note Ids to be deleted.'
)
)
);
}
/**
* Delete notes about users.
* Note: code should be matching the /notes/delete.php checks.
*
* @param array $notes An array of ids for the notes to delete.
* @return null
* @since Moodle 2.5
*/
public static function delete_notes($notes = array()) {
global $CFG;
require_once($CFG->dirroot . "/notes/lib.php");
$params = self::validate_parameters(self::delete_notes_parameters(), array('notes' => $notes));
// Check if note system is enabled.
if (!$CFG->enablenotes) {
throw new moodle_exception('notesdisabled', 'notes');
}
$warnings = array();
foreach ($params['notes'] as $noteid) {
$note = note_load($noteid);
if (isset($note->id)) {
// Ensure the current user is allowed to run this function.
$context = context_course::instance($note->courseid);
self::validate_context($context);
require_capability('moodle/notes:manage', $context);
if (!note_delete($note)) {
$warnings[] = array(array('item' => 'note',
'itemid' => $noteid,
'warningcode' => 'savedfailed',
'message' => 'Note could not be modified'));
}
} else {
$warnings[] = array('item'=>'note', 'itemid'=>$noteid, 'warningcode'=>'badid', 'message'=>'Note does not exist');
}
}
return $warnings;
}
/**
* Returns description of delete_notes result value.
*
* @return external_description
* @since Moodle 2.5
*/
public static function delete_notes_returns() {
return new external_warnings('item is always \'note\'',
'When errorcode is savedfailed the note could not be modified.' .
'When errorcode is badparam, an incorrect parameter was provided.' .
'When errorcode is badid, the note does not exist',
'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)');
}
/**
* Returns description of get_notes parameters.
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function get_notes_parameters() {
return new external_function_parameters(
array(
"notes"=> new external_multiple_structure(
new external_value(PARAM_INT, 'ID of the note to be retrieved'), 'Array of Note Ids to be retrieved.'
)
)
);
}
/**
* Get notes about users.
*
* @param array $notes An array of ids for the notes to retrieve.
* @return null
* @since Moodle 2.5
*/
public static function get_notes($notes) {
global $CFG;
require_once($CFG->dirroot . "/notes/lib.php");
$params = self::validate_parameters(self::get_notes_parameters(), array('notes' => $notes));
// Check if note system is enabled.
if (!$CFG->enablenotes) {
throw new moodle_exception('notesdisabled', 'notes');
}
$resultnotes = array();
foreach ($params['notes'] as $noteid) {
$resultnote = array();
$note = note_load($noteid);
if (isset($note->id)) {
// Ensure the current user is allowed to run this function.
$context = context_course::instance($note->courseid);
self::validate_context($context);
require_capability('moodle/notes:view', $context);
list($gotnote['text'], $gotnote['format']) = external_format_text($note->content,
$note->format,
$context->id,
'notes',
'',
'');
$gotnote['noteid'] = $note->id;
$gotnote['userid'] = $note->userid;
$gotnote['publishstate'] = $note->publishstate;
$gotnote['courseid'] = $note->courseid;
$resultnotes["notes"][] = $gotnote;
} else {
$resultnotes["warnings"][] = array('item' => 'note',
'itemid' => $noteid,
'warningcode' => 'badid',
'message' => 'Note does not exist');
}
}
return $resultnotes;
}
/**
* Returns description of get_notes result value.
*
* @return external_description
* @since Moodle 2.5
*/
public static function get_notes_returns() {
return new external_single_structure(
array(
'notes' => new external_multiple_structure(
new external_single_structure(
array(
'noteid' => new external_value(PARAM_INT, 'id of the note', VALUE_OPTIONAL),
'userid' => new external_value(PARAM_INT, 'id of the user the note is about', VALUE_OPTIONAL),
'publishstate' => new external_value(PARAM_ALPHA, '\'personal\', \'course\' or \'site\'', VALUE_OPTIONAL),
'courseid' => new external_value(PARAM_INT, 'course id of the note', VALUE_OPTIONAL),
'text' => new external_value(PARAM_RAW, 'the text of the message - text or HTML', VALUE_OPTIONAL),
'format' => new external_format_value('text', VALUE_OPTIONAL),
), 'note'
)
),
'warnings' => new external_warnings('item is always \'note\'',
'When errorcode is savedfailed the note could not be modified.' .
'When errorcode is badparam, an incorrect parameter was provided.' .
'When errorcode is badid, the note does not exist',
'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)')
)
);
}
/**
* Returns description of update_notes parameters.
*
* @return external_function_parameters
* @since Moodle 2.5
*/
public static function update_notes_parameters() {
return new external_function_parameters(
array(
'notes' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'id of the note'),
'publishstate' => new external_value(PARAM_ALPHA, '\'personal\', \'course\' or \'site\''),
'text' => new external_value(PARAM_RAW, 'the text of the message - text or HTML'),
'format' => new external_format_value('text', VALUE_DEFAULT),
)
), "Array of Notes", VALUE_DEFAULT, array()
)
)
);
}
/**
* Update notes about users.
*
* @param array $notes An array of ids for the notes to update.
* @return array fail infos.
* @since Moodle 2.2
*/
public static function update_notes($notes = array()) {
global $CFG, $DB;
require_once($CFG->dirroot . "/notes/lib.php");
$params = self::validate_parameters(self::update_notes_parameters(), array('notes' => $notes));
// Check if note system is enabled.
if (!$CFG->enablenotes) {
throw new moodle_exception('notesdisabled', 'notes');
}
$warnings = array();
foreach ($params['notes'] as $note) {
$notedetails = note_load($note['id']);
if (isset($notedetails->id)) {
// Ensure the current user is allowed to run this function.
$context = context_course::instance($notedetails->courseid);
self::validate_context($context);
require_capability('moodle/notes:manage', $context);
$dbnote = new stdClass;
$dbnote->id = $note['id'];
$dbnote->content = $note['text'];
$dbnote->format = external_validate_format($note['format']);
// Get the state ('personal', 'course', 'site').
switch ($note['publishstate']) {
case 'personal':
$dbnote->publishstate = NOTES_STATE_DRAFT;
break;
case 'course':
$dbnote->publishstate = NOTES_STATE_PUBLIC;
break;
case 'site':
$dbnote->publishstate = NOTES_STATE_SITE;
$dbnote->courseid = SITEID;
break;
default:
$warnings[] = array('item' => 'note',
'itemid' => $note["id"],
'warningcode' => 'badparam',
'message' => 'Provided publishstate incorrect');
break;
}
if (!note_save($dbnote)) {
$warnings[] = array('item' => 'note',
'itemid' => $note["id"],
'warningcode' => 'savedfailed',
'message' => 'Note could not be modified');
}
} else {
$warnings[] = array('item' => 'note',
'itemid' => $note["id"],
'warningcode' => 'badid',
'message' => 'Note does not exist');
}
}
return $warnings;
}
/**
* Returns description of update_notes result value.
*
* @return external_description
* @since Moodle 2.5
*/
public static function update_notes_returns() {
return new external_warnings('item is always \'note\'',
'When errorcode is savedfailed the note could not be modified.' .
'When errorcode is badparam, an incorrect parameter was provided.' .
'When errorcode is badid, the note does not exist',
'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)');
}
}
/**
* Deprecated notes external functions
*
* @package core_notes
* @copyright 2011 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.1
* @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
* @see core_notes_external
*/
class moodle_notes_external extends external_api {
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 2.1
* @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
* @see core_notes_external::create_notes_parameters()
*/
public static function create_notes_parameters() {
return core_notes_external::create_notes_parameters();
}
/**
* Create notes about some users
* Note: code should be matching the /notes/edit.php checks
* and the /user/addnote.php checks. (they are similar cheks)
*
* @param array $notes An array of notes to create.
* @return array (success infos and fail infos)
* @since Moodle 2.1
* @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
* @see core_notes_external::create_notes()
*/
public static function create_notes($notes = array()) {
return core_notes_external::create_notes($notes);
}
/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 2.1
* @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
* @see core_notes_external::create_notes_returns()
*/
public static function create_notes_returns() {
return core_notes_external::create_notes_returns();
}
/**
* Marking the method as deprecated.
*
* @return bool
*/
public static function create_notes_is_deprecated() {
return true;
}
}