2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
23 * @copyright 2011 Jerome Mouneyrac
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once("$CFG->libdir/externallib.php");
30 * Notes external functions
34 * @copyright 2011 Jerome Mouneyrac
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class core_notes_external
extends external_api
{
41 * Returns description of method parameters
43 * @return external_function_parameters
46 public static function create_notes_parameters() {
47 return new external_function_parameters(
49 'notes' => new external_multiple_structure(
50 new external_single_structure(
52 'userid' => new external_value(PARAM_INT
, 'id of the user the note is about'),
53 'publishstate' => new external_value(PARAM_ALPHA
, '\'personal\', \'course\' or \'site\''),
54 '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)'),
55 'text' => new external_value(PARAM_RAW
, 'the text of the message - text or HTML'),
56 'format' => new external_format_value('text', VALUE_DEFAULT
),
57 '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
),
66 * Create notes about some users
67 * Note: code should be matching the /notes/edit.php checks
68 * and the /user/addnote.php checks. (they are similar cheks)
70 * @param array $notes An array of notes to create.
71 * @return array (success infos and fail infos)
74 public static function create_notes($notes = array()) {
76 require_once($CFG->dirroot
. "/notes/lib.php");
78 $params = self
::validate_parameters(self
::create_notes_parameters(), array('notes' => $notes));
80 // Check if note system is enabled.
81 if (!$CFG->enablenotes
) {
82 throw new moodle_exception('notesdisabled', 'notes');
85 // Retrieve all courses.
87 foreach($params['notes'] as $note) {
88 $courseids[] = $note['courseid'];
90 $courses = $DB->get_records_list("course", "id", $courseids);
92 // Retrieve all users of the notes.
94 foreach($params['notes'] as $note) {
95 $userids[] = $note['userid'];
97 list($sqluserids, $sqlparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED
, 'userid_');
98 $users = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
100 $resultnotes = array();
101 foreach ($params['notes'] as $note) {
104 $resultnote = array(); // The infos about the success of the operation.
106 // Check the course exists.
107 if (empty($courses[$note['courseid']])) {
109 $errormessage = get_string('invalidcourseid', 'error');
111 // Ensure the current user is allowed to run this function.
112 $context = context_course
::instance($note['courseid']);
113 self
::validate_context($context);
114 require_capability('moodle/notes:manage', $context);
117 // Check the user exists.
118 if (empty($users[$note['userid']])) {
120 $errormessage = get_string('invaliduserid', 'notes', $note['userid']);
123 // Build the resultnote.
124 if (isset($note['clientnoteid'])) {
125 $resultnote['clientnoteid'] = $note['clientnoteid'];
129 // Now we can create the note.
130 $dbnote = new stdClass
;
131 $dbnote->courseid
= $note['courseid'];
132 $dbnote->userid
= $note['userid'];
133 // Need to support 'html' and 'text' format values for backward compatibility.
134 switch (strtolower($note['format'])) {
136 $textformat = FORMAT_HTML
;
139 $textformat = FORMAT_PLAIN
;
141 $textformat = external_validate_format($note['format']);
144 $dbnote->content
= $note['text'];
145 $dbnote->format
= $textformat;
147 // Get the state ('personal', 'course', 'site').
148 switch ($note['publishstate']) {
150 $dbnote->publishstate
= NOTES_STATE_DRAFT
;
153 $dbnote->publishstate
= NOTES_STATE_PUBLIC
;
156 $dbnote->publishstate
= NOTES_STATE_SITE
;
157 $dbnote->courseid
= SITEID
;
163 // TODO MDL-31119 performance improvement - if possible create a bulk functions for saving multiple notes at once
164 if (note_save($dbnote)) { // Note_save attribut an id in case of success.
165 $success = $dbnote->id
;
168 $resultnote['noteid'] = $success;
170 // WARNINGS: for backward compatibility we return this errormessage.
171 // We should have thrown exceptions as these errors prevent results to be returned.
172 // See http://docs.moodle.org/dev/Errors_handling_in_web_services#When_to_send_a_warning_on_the_server_side .
173 $resultnote['noteid'] = -1;
174 $resultnote['errormessage'] = $errormessage;
177 $resultnotes[] = $resultnote;
184 * Returns description of method result value
186 * @return external_description
189 public static function create_notes_returns() {
190 return new external_multiple_structure(
191 new external_single_structure(
193 'clientnoteid' => new external_value(PARAM_ALPHANUMEXT
, 'your own id for the note', VALUE_OPTIONAL
),
194 'noteid' => new external_value(PARAM_INT
, 'test this to know if it success: id of the created note when successed, -1 when failed'),
195 'errormessage' => new external_value(PARAM_TEXT
, 'error message - if failed', VALUE_OPTIONAL
)
202 * Returns description of delete_notes parameters
204 * @return external_function_parameters
207 public static function delete_notes_parameters() {
208 return new external_function_parameters(
210 "notes"=> new external_multiple_structure(
211 new external_value(PARAM_INT
, 'ID of the note to be deleted'), 'Array of Note Ids to be deleted.'
218 * Delete notes about users.
219 * Note: code should be matching the /notes/delete.php checks.
221 * @param array $notes An array of ids for the notes to delete.
225 public static function delete_notes($notes = array()) {
227 require_once($CFG->dirroot
. "/notes/lib.php");
229 $params = self
::validate_parameters(self
::delete_notes_parameters(), $notes);
231 // Check if note system is enabled.
232 if (!$CFG->enablenotes
) {
233 throw new moodle_exception('notesdisabled', 'notes');
236 foreach ($params['notes'] as $noteid) {
237 $note = note_load($noteid);
238 if (isset($note->id
)) {
239 // Ensure the current user is allowed to run this function.
240 $context = context_course
::instance($note->courseid
);
241 self
::validate_context($context);
242 require_capability('moodle/notes:manage', $context);
243 if (!note_delete($note)) {
244 $warnings[] = array(array('item'=>'note', 'itemid'=>$noteid, 'warningcode'=>'savedfailed', 'message'=>'Note could not be modified'));
247 $warnings[] = array('item'=>'note', 'itemid'=>$noteid, 'warningcode'=>'badid', 'message'=>'Note does not exist');
254 * Returns description of delete_notes result value.
256 * @return external_description
259 public static function delete_notes_returns() {
260 return new external_warnings('item is always \'note\'',
261 'When errorcode is savedfailed the note could not be modified.' .
262 'When errorcode is badparam, an incorrect parameter was provided.' .
263 'When errorcode is badid, the note does not exist',
264 'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)');
269 * Returns description of get_notes parameters.
271 * @return external_function_parameters
274 public static function get_notes_parameters() {
275 return new external_function_parameters(
277 "notes"=> new external_multiple_structure(
278 new external_value(PARAM_INT
, 'ID of the note to be retrieved'), 'Array of Note Ids to be retrieved.'
285 * Get notes about users.
287 * @param array $notes An array of ids for the notes to retrieve.
291 public static function get_notes($notes) {
293 require_once($CFG->dirroot
. "/notes/lib.php");
295 $params = self
::validate_parameters(self
::get_notes_parameters(), $notes);
296 // Check if note system is enabled.
297 if (!$CFG->enablenotes
) {
298 throw new moodle_exception('notesdisabled', 'notes');
300 $resultnotes = array();
301 foreach ($params['notes'] as $noteid) {
302 $resultnote = array();
304 $note = note_load($noteid);
305 if (isset($note->id
)) {
306 // Ensure the current user is allowed to run this function.
307 $context = context_course
::instance($note->courseid
);
308 self
::validate_context($context);
309 require_capability('moodle/notes:view', $context);
310 list($gotnote['text'], $gotnote['format']) = external_format_text($note->content
, $note->format
, $context->id
, 'notes', '', '');
311 $gotnote['noteid'] = $note->id
;
312 $gotnote['userid'] = $note->userid
;
313 $gotnote['publishstate'] = $note->publishstate
;
314 $gotnote['courseid'] = $note->courseid
;
315 $resultnotes["notes"][] = $gotnote;
317 $resultnotes["warnings"][] = array('item'=>'note', 'itemid'=>$noteid, 'warningcode'=>'badid', 'message'=>'Note does not exist');
324 * Returns description of get_notes result value.
326 * @return external_description
329 public static function get_notes_returns() {
330 return new external_single_structure(
332 'notes' => new external_multiple_structure(
333 new external_single_structure(
335 'noteid' => new external_value(PARAM_INT
, 'id of the note', VALUE_OPTIONAL
),
336 'userid' => new external_value(PARAM_INT
, 'id of the user the note is about', VALUE_OPTIONAL
),
337 'publishstate' => new external_value(PARAM_ALPHA
, '\'personal\', \'course\' or \'site\'', VALUE_OPTIONAL
),
338 'courseid' => new external_value(PARAM_INT
, 'course id of the note', VALUE_OPTIONAL
),
339 'text' => new external_value(PARAM_RAW
, 'the text of the message - text or HTML', VALUE_OPTIONAL
),
340 'format' => new external_format_value('text', VALUE_OPTIONAL
),
344 'warnings' => new external_warnings('item is always \'note\'',
345 'When errorcode is savedfailed the note could not be modified.' .
346 'When errorcode is badparam, an incorrect parameter was provided.' .
347 'When errorcode is badid, the note does not exist',
348 'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)')
354 * Returns description of update_notes parameters.
356 * @return external_function_parameters
359 public static function update_notes_parameters() {
360 return new external_function_parameters(
362 'notes' => new external_multiple_structure(
363 new external_single_structure(
365 'id' => new external_value(PARAM_INT
, 'id of the note'),
366 'publishstate' => new external_value(PARAM_ALPHA
, '\'personal\', \'course\' or \'site\''),
367 'text' => new external_value(PARAM_RAW
, 'the text of the message - text or HTML'),
368 'format' => new external_format_value('text', VALUE_DEFAULT
),
370 ), "Array of Notes", VALUE_DEFAULT
, array()
377 * Update notes about users.
379 * @param array $notes An array of ids for the notes to update.
380 * @return array fail infos.
383 public static function update_notes($notes = array()) {
385 require_once($CFG->dirroot
. "/notes/lib.php");
387 $params = self
::validate_parameters(self
::update_notes_parameters(), array('notes' => $notes));
389 // Check if note system is enabled.
390 if (!$CFG->enablenotes
) {
391 throw new moodle_exception('notesdisabled', 'notes');
395 foreach ($params['notes'] as $note) {
396 $notedetails = note_load($note['id']);
397 if (isset($notedetails->id
)) {
398 // Ensure the current user is allowed to run this function.
399 $context = context_course
::instance($notedetails->courseid
);
400 self
::validate_context($context);
401 require_capability('moodle/notes:manage', $context);
403 $dbnote = new stdClass
;
404 $dbnote->id
= $note['id'];
405 $dbnote->content
= $note['text'];
406 $dbnote->format
= external_validate_format($note['format']);
407 // Get the state ('personal', 'course', 'site').
408 switch ($note['publishstate']) {
410 $dbnote->publishstate
= NOTES_STATE_DRAFT
;
413 $dbnote->publishstate
= NOTES_STATE_PUBLIC
;
416 $dbnote->publishstate
= NOTES_STATE_SITE
;
417 $dbnote->courseid
= SITEID
;
420 $warnings[] = array('item'=>'note', 'itemid'=>$note["id"], 'warningcode'=>'badparam', 'message'=>'Provided publishstate incorrect');
423 if (!note_save($dbnote)) {
424 $warnings[] = array('item'=>'note', 'itemid'=>$note["id"], 'warningcode'=>'savedfailed', 'message'=>'Note could not be modified');
427 $warnings[] = array('item'=>'note', 'itemid'=>$note["id"], 'warningcode'=>'badid', 'message'=>'Note does not exist');
434 * Returns description of update_notes result value.
436 * @return external_description
439 public static function update_notes_returns() {
440 return new external_warnings('item is always \'note\'',
441 'When errorcode is savedfailed the note could not be modified.' .
442 'When errorcode is badparam, an incorrect parameter was provided.' .
443 'When errorcode is badid, the note does not exist',
444 'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)');
449 * Deprecated notes external functions
451 * @package core_notes
452 * @copyright 2011 Jerome Mouneyrac
453 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
455 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
456 * @see core_notes_external
458 class moodle_notes_external
extends external_api
{
461 * Returns description of method parameters
463 * @return external_function_parameters
465 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
466 * @see core_notes_external::create_notes_parameters()
468 public static function create_notes_parameters() {
469 return core_notes_external
::create_notes_parameters();
473 * Create notes about some users
474 * Note: code should be matching the /notes/edit.php checks
475 * and the /user/addnote.php checks. (they are similar cheks)
477 * @param array $notes An array of notes to create.
478 * @return array (success infos and fail infos)
480 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
481 * @see core_notes_external::create_notes()
483 public static function create_notes($notes = array()) {
484 return core_notes_external
::create_notes($notes);
488 * Returns description of method result value
490 * @return external_description
492 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
493 * @see core_notes_external::create_notes_returns()
495 public static function create_notes_returns() {
496 return core_notes_external
::create_notes_returns();