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/>.
17 use core_external\external_api
;
18 use core_external\external_format_value
;
19 use core_external\external_function_parameters
;
20 use core_external\external_multiple_structure
;
21 use core_external\external_single_structure
;
22 use core_external\external_value
;
23 use core_external\external_warnings
;
24 use core_external\util
;
31 * @copyright 2011 Jerome Mouneyrac
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 defined('MOODLE_INTERNAL') ||
die();
37 require_once($CFG->dirroot
. "/notes/lib.php");
40 * Notes external functions
44 * @copyright 2011 Jerome Mouneyrac
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48 class core_notes_external
extends external_api
{
51 * Returns description of method parameters
53 * @return external_function_parameters
56 public static function create_notes_parameters() {
57 return new external_function_parameters(
59 'notes' => new external_multiple_structure(
60 new external_single_structure(
62 'userid' => new external_value(PARAM_INT
, 'id of the user the note is about'),
63 'publishstate' => new external_value(PARAM_ALPHA
, '\'personal\', \'course\' or \'site\''),
64 '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)'),
65 'text' => new external_value(PARAM_RAW
, 'the text of the message - text or HTML'),
66 'format' => new external_format_value('text', VALUE_DEFAULT
),
67 '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
),
76 * Create notes about some users
77 * Note: code should be matching the /notes/edit.php checks
78 * and the /user/addnote.php checks. (they are similar cheks)
80 * @param array $notes An array of notes to create.
81 * @return array (success infos and fail infos)
84 public static function create_notes($notes = array()) {
87 $params = self
::validate_parameters(self
::create_notes_parameters(), array('notes' => $notes));
89 // Check if note system is enabled.
90 if (!$CFG->enablenotes
) {
91 throw new moodle_exception('notesdisabled', 'notes');
94 // Retrieve all courses.
96 foreach ($params['notes'] as $note) {
97 $courseids[] = $note['courseid'];
99 $courses = $DB->get_records_list("course", "id", $courseids);
101 // Retrieve all users of the notes.
103 foreach ($params['notes'] as $note) {
104 $userids[] = $note['userid'];
106 list($sqluserids, $sqlparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED
, 'userid_');
107 $users = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
109 $resultnotes = array();
110 foreach ($params['notes'] as $note) {
113 $resultnote = array(); // The infos about the success of the operation.
115 // Check the course exists.
116 if (empty($courses[$note['courseid']])) {
118 $errormessage = get_string('invalidcourseid', 'error');
120 // Ensure the current user is allowed to run this function.
121 $context = context_course
::instance($note['courseid']);
122 self
::validate_context($context);
123 require_capability('moodle/notes:manage', $context);
126 // Check the user exists.
127 if (empty($users[$note['userid']])) {
129 $errormessage = get_string('invaliduserid', 'notes', $note['userid']);
132 // Build the resultnote.
133 if (isset($note['clientnoteid'])) {
134 $resultnote['clientnoteid'] = $note['clientnoteid'];
138 // Now we can create the note.
139 $dbnote = new stdClass
;
140 $dbnote->courseid
= $note['courseid'];
141 $dbnote->userid
= $note['userid'];
142 // Need to support 'html' and 'text' format values for backward compatibility.
143 switch (strtolower($note['format'])) {
145 $textformat = FORMAT_HTML
;
148 $textformat = FORMAT_PLAIN
;
150 $textformat = util
::validate_format($note['format']);
153 $dbnote->content
= $note['text'];
154 $dbnote->format
= $textformat;
156 // Get the state ('personal', 'course', 'site').
157 switch ($note['publishstate']) {
159 $dbnote->publishstate
= NOTES_STATE_DRAFT
;
162 $dbnote->publishstate
= NOTES_STATE_PUBLIC
;
165 $dbnote->publishstate
= NOTES_STATE_SITE
;
166 $dbnote->courseid
= SITEID
;
172 // TODO MDL-31119 performance improvement - if possible create a bulk functions for saving multiple notes at once
173 if (note_save($dbnote)) { // Note_save attribut an id in case of success.
174 $success = $dbnote->id
;
177 $resultnote['noteid'] = $success;
179 // WARNINGS: for backward compatibility we return this errormessage.
180 // We should have thrown exceptions as these errors prevent results to be returned.
181 // See http://docs.moodle.org/dev/Errors_handling_in_web_services#When_to_send_a_warning_on_the_server_side .
182 $resultnote['noteid'] = -1;
183 $resultnote['errormessage'] = $errormessage;
186 $resultnotes[] = $resultnote;
193 * Returns description of method result value
195 * @return \core_external\external_description
198 public static function create_notes_returns() {
199 return new external_multiple_structure(
200 new external_single_structure(
202 'clientnoteid' => new external_value(PARAM_ALPHANUMEXT
, 'your own id for the note', VALUE_OPTIONAL
),
203 'noteid' => new external_value(PARAM_INT
, 'ID of the created note when successful, -1 when failed'),
204 'errormessage' => new external_value(PARAM_TEXT
, 'error message - if failed', VALUE_OPTIONAL
)
211 * Returns description of delete_notes parameters
213 * @return external_function_parameters
216 public static function delete_notes_parameters() {
217 return new external_function_parameters(
219 "notes"=> new external_multiple_structure(
220 new external_value(PARAM_INT
, 'ID of the note to be deleted'), 'Array of Note Ids to be deleted.'
227 * Delete notes about users.
228 * Note: code should be matching the /notes/delete.php checks.
230 * @param array $notes An array of ids for the notes to delete.
234 public static function delete_notes($notes = array()) {
237 $params = self
::validate_parameters(self
::delete_notes_parameters(), array('notes' => $notes));
239 // Check if note system is enabled.
240 if (!$CFG->enablenotes
) {
241 throw new moodle_exception('notesdisabled', 'notes');
244 foreach ($params['notes'] as $noteid) {
245 $note = note_load($noteid);
246 if (isset($note->id
)) {
247 // Ensure the current user is allowed to run this function.
248 $context = context_course
::instance($note->courseid
);
249 self
::validate_context($context);
250 require_capability('moodle/notes:manage', $context);
253 $warnings[] = array('item'=>'note', 'itemid'=>$noteid, 'warningcode'=>'badid', 'message'=>'Note does not exist');
260 * Returns description of delete_notes result value.
262 * @return \core_external\external_description
265 public static function delete_notes_returns() {
266 return new external_warnings('item is always \'note\'',
267 'When errorcode is savedfailed the note could not be modified.' .
268 'When errorcode is badparam, an incorrect parameter was provided.' .
269 'When errorcode is badid, the note does not exist',
270 'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)');
275 * Returns description of get_notes parameters.
277 * @return external_function_parameters
280 public static function get_notes_parameters() {
281 return new external_function_parameters(
283 "notes"=> new external_multiple_structure(
284 new external_value(PARAM_INT
, 'ID of the note to be retrieved'), 'Array of Note Ids to be retrieved.'
291 * Get notes about users.
293 * @param array $notes An array of ids for the notes to retrieve.
297 public static function get_notes($notes) {
300 $params = self
::validate_parameters(self
::get_notes_parameters(), array('notes' => $notes));
301 // Check if note system is enabled.
302 if (!$CFG->enablenotes
) {
303 throw new moodle_exception('notesdisabled', 'notes');
305 $resultnotes = array();
306 foreach ($params['notes'] as $noteid) {
307 $resultnote = array();
309 $note = note_load($noteid);
310 if (isset($note->id
)) {
311 // Ensure the current user is allowed to run this function.
312 $context = context_course
::instance($note->courseid
);
313 self
::validate_context($context);
314 require_capability('moodle/notes:view', $context);
315 list($gotnote['text'], $gotnote['format']) = util
::format_text($note->content
,
321 $gotnote['noteid'] = $note->id
;
322 $gotnote['userid'] = $note->userid
;
323 $gotnote['publishstate'] = $note->publishstate
;
324 $gotnote['courseid'] = $note->courseid
;
325 $resultnotes["notes"][] = $gotnote;
327 $resultnotes["warnings"][] = array('item' => 'note',
329 'warningcode' => 'badid',
330 'message' => 'Note does not exist');
337 * Returns description of get_notes result value.
339 * @return \core_external\external_description
342 public static function get_notes_returns() {
343 return new external_single_structure(
345 'notes' => new external_multiple_structure(
346 new external_single_structure(
348 'noteid' => new external_value(PARAM_INT
, 'id of the note', VALUE_OPTIONAL
),
349 'userid' => new external_value(PARAM_INT
, 'id of the user the note is about', VALUE_OPTIONAL
),
350 'publishstate' => new external_value(PARAM_ALPHA
, '\'personal\', \'course\' or \'site\'', VALUE_OPTIONAL
),
351 'courseid' => new external_value(PARAM_INT
, 'course id of the note', VALUE_OPTIONAL
),
352 'text' => new external_value(PARAM_RAW
, 'the text of the message - text or HTML', VALUE_OPTIONAL
),
353 'format' => new external_format_value('text', VALUE_OPTIONAL
),
357 'warnings' => new external_warnings('item is always \'note\'',
358 'When errorcode is savedfailed the note could not be modified.' .
359 'When errorcode is badparam, an incorrect parameter was provided.' .
360 'When errorcode is badid, the note does not exist',
361 'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)')
367 * Returns description of update_notes parameters.
369 * @return external_function_parameters
372 public static function update_notes_parameters() {
373 return new external_function_parameters(
375 'notes' => new external_multiple_structure(
376 new external_single_structure(
378 'id' => new external_value(PARAM_INT
, 'id of the note'),
379 'publishstate' => new external_value(PARAM_ALPHA
, '\'personal\', \'course\' or \'site\''),
380 'text' => new external_value(PARAM_RAW
, 'the text of the message - text or HTML'),
381 'format' => new external_format_value('text', VALUE_DEFAULT
),
383 ), "Array of Notes", VALUE_DEFAULT
, array()
390 * Update notes about users.
392 * @param array $notes An array of ids for the notes to update.
393 * @return array fail infos.
396 public static function update_notes($notes = array()) {
399 $params = self
::validate_parameters(self
::update_notes_parameters(), array('notes' => $notes));
401 // Check if note system is enabled.
402 if (!$CFG->enablenotes
) {
403 throw new moodle_exception('notesdisabled', 'notes');
407 foreach ($params['notes'] as $note) {
408 $notedetails = note_load($note['id']);
409 if (isset($notedetails->id
)) {
410 // Ensure the current user is allowed to run this function.
411 $context = context_course
::instance($notedetails->courseid
);
412 self
::validate_context($context);
413 require_capability('moodle/notes:manage', $context);
415 $dbnote = new stdClass
;
416 $dbnote->id
= $note['id'];
417 $dbnote->content
= $note['text'];
418 $dbnote->format
= util
::validate_format($note['format']);
419 // Get the state ('personal', 'course', 'site').
420 switch ($note['publishstate']) {
422 $dbnote->publishstate
= NOTES_STATE_DRAFT
;
425 $dbnote->publishstate
= NOTES_STATE_PUBLIC
;
428 $dbnote->publishstate
= NOTES_STATE_SITE
;
429 $dbnote->courseid
= SITEID
;
432 $warnings[] = array('item' => 'note',
433 'itemid' => $note["id"],
434 'warningcode' => 'badparam',
435 'message' => 'Provided publishstate incorrect');
438 if (!note_save($dbnote)) {
439 $warnings[] = array('item' => 'note',
440 'itemid' => $note["id"],
441 'warningcode' => 'savedfailed',
442 'message' => 'Note could not be modified');
445 $warnings[] = array('item' => 'note',
446 'itemid' => $note["id"],
447 'warningcode' => 'badid',
448 'message' => 'Note does not exist');
455 * Returns description of update_notes result value.
457 * @return \core_external\external_description
460 public static function update_notes_returns() {
461 return new external_warnings('item is always \'note\'',
462 'When errorcode is savedfailed the note could not be modified.' .
463 'When errorcode is badparam, an incorrect parameter was provided.' .
464 'When errorcode is badid, the note does not exist',
465 'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)');
469 * Returns description of method parameters
471 * @return external_function_parameters
474 public static function get_course_notes_parameters() {
475 return new external_function_parameters(
477 'courseid' => new external_value(PARAM_INT
, 'course id, 0 for SITE'),
478 'userid' => new external_value(PARAM_INT
, 'user id', VALUE_DEFAULT
, 0),
484 * Create a notes list
486 * @param int $courseid ID of the Course
487 * @param stdClass $context context object
488 * @param int $userid ID of the User
491 * @return array of notes
494 protected static function create_note_list($courseid, $context, $userid, $state, $author = 0) {
496 $notes = note_list($courseid, $userid, $state, $author);
497 foreach ($notes as $key => $note) {
498 $note = (array)$note;
499 [$note['content'], $note['format']] = util
::format_text(
507 $results[$key] = $note;
513 * Get a list of course notes
515 * @param int $courseid ID of the Course
516 * @param int $userid ID of the User
517 * @return array of site, course and personal notes and warnings
519 * @throws moodle_exception
521 public static function get_course_notes($courseid, $userid = 0) {
524 if (empty($CFG->enablenotes
)) {
525 throw new moodle_exception('notesdisabled', 'notes');
529 $arrayparams = array(
530 'courseid' => $courseid,
533 $params = self
::validate_parameters(self
::get_course_notes_parameters(), $arrayparams);
535 if (empty($params['courseid'])) {
536 $params['courseid'] = SITEID
;
539 if (!empty($params['userid'])) {
540 $user = core_user
::get_user($params['userid'], '*', MUST_EXIST
);
541 core_user
::require_active_user($user);
544 $course = get_course($params['courseid']);
546 $systemcontext = context_system
::instance();
547 $canmanagesystemnotes = has_capability('moodle/notes:manage', $systemcontext);
549 if ($course->id
== SITEID
) {
550 $context = $systemcontext;
551 $canmanagecoursenotes = $canmanagesystemnotes;
553 $context = context_course
::instance($course->id
);
554 $canmanagecoursenotes = has_capability('moodle/notes:manage', $context);
556 self
::validate_context($context);
558 $sitenotes = array();
559 $coursenotes = array();
560 $personalnotes = array();
562 if ($course->id
!= SITEID
) {
564 require_capability('moodle/notes:view', $context);
565 $sitenotes = self
::create_note_list(0, $systemcontext, $params['userid'], NOTES_STATE_SITE
);
566 $coursenotes = self
::create_note_list($course->id
, $context, $params['userid'], NOTES_STATE_PUBLIC
);
567 $personalnotes = self
::create_note_list($course->id
, $context, $params['userid'], NOTES_STATE_DRAFT
,
570 if (has_capability('moodle/notes:view', $context)) {
571 $sitenotes = self
::create_note_list(0, $context, $params['userid'], NOTES_STATE_SITE
);
573 // It returns notes only for a specific user!
575 $usercourses = enrol_get_users_courses($user->id
, true);
576 foreach ($usercourses as $c) {
577 // All notes at course level, only if we have capability on every course.
578 if (has_capability('moodle/notes:view', context_course
::instance($c->id
))) {
579 $coursenotes +
= self
::create_note_list($c->id
, $context, $params['userid'], NOTES_STATE_PUBLIC
);
586 'sitenotes' => $sitenotes,
587 'coursenotes' => $coursenotes,
588 'personalnotes' => $personalnotes,
589 'canmanagesystemnotes' => $canmanagesystemnotes,
590 'canmanagecoursenotes' => $canmanagecoursenotes,
591 'warnings' => $warnings
598 * Returns array of note structure
600 * @return \core_external\external_description
603 protected static function get_note_structure() {
605 'id' => new external_value(PARAM_INT
, 'id of this note'),
606 'courseid' => new external_value(PARAM_INT
, 'id of the course'),
607 'userid' => new external_value(PARAM_INT
, 'user id'),
608 'content' => new external_value(PARAM_RAW
, 'the content text formated'),
609 'format' => new external_format_value('content'),
610 'created' => new external_value(PARAM_INT
, 'time created (timestamp)'),
611 'lastmodified' => new external_value(PARAM_INT
, 'time of last modification (timestamp)'),
612 'usermodified' => new external_value(PARAM_INT
, 'user id of the creator of this note'),
613 'publishstate' => new external_value(PARAM_ALPHA
, "state of the note (i.e. draft, public, site) ")
618 * Returns description of method result value
620 * @return \core_external\external_description
623 public static function get_course_notes_returns() {
624 return new external_single_structure(
626 'sitenotes' => new external_multiple_structure(
627 new external_single_structure(self
::get_note_structure() , ''), 'site notes', VALUE_OPTIONAL
629 'coursenotes' => new external_multiple_structure(
630 new external_single_structure(self
::get_note_structure() , ''), 'couse notes', VALUE_OPTIONAL
632 'personalnotes' => new external_multiple_structure(
633 new external_single_structure(self
::get_note_structure() , ''), 'personal notes', VALUE_OPTIONAL
635 'canmanagesystemnotes' => new external_value(PARAM_BOOL
, 'Whether the user can manage notes at system level.',
637 'canmanagecoursenotes' => new external_value(PARAM_BOOL
, 'Whether the user can manage notes at the given course.',
639 'warnings' => new external_warnings()
645 * Returns description of method parameters
647 * @return external_function_parameters
650 public static function view_notes_parameters() {
651 return new external_function_parameters(
653 'courseid' => new external_value(PARAM_INT
, 'course id, 0 for notes at system level'),
654 'userid' => new external_value(PARAM_INT
, 'user id, 0 means view all the user notes', VALUE_DEFAULT
, 0)
660 * Simulates the web interface view of notes/index.php: trigger events
662 * @param int $courseid id of the course
663 * @param int $userid id of the user
664 * @return array of warnings and status result
666 * @throws moodle_exception
668 public static function view_notes($courseid, $userid = 0) {
670 require_once($CFG->dirroot
. "/notes/lib.php");
672 if (empty($CFG->enablenotes
)) {
673 throw new moodle_exception('notesdisabled', 'notes');
677 $arrayparams = array(
678 'courseid' => $courseid,
681 $params = self
::validate_parameters(self
::view_notes_parameters(), $arrayparams);
683 if (empty($params['courseid'])) {
684 $params['courseid'] = SITEID
;
687 $course = get_course($params['courseid']);
689 if ($course->id
== SITEID
) {
690 $context = context_system
::instance();
692 $context = context_course
::instance($course->id
);
695 // First of all, validate the context before do further permission checks.
696 self
::validate_context($context);
697 require_capability('moodle/notes:view', $context);
699 if (!empty($params['userid'])) {
700 $user = core_user
::get_user($params['userid'], '*', MUST_EXIST
);
701 core_user
::require_active_user($user);
703 if ($course->id
!= SITEID
and !can_access_course($course, $user, '', true)) {
704 throw new moodle_exception('notenrolledprofile');
708 note_view($context, $params['userid']);
711 $result['status'] = true;
712 $result['warnings'] = $warnings;
718 * Returns description of method result value
720 * @return \core_external\external_description
723 public static function view_notes_returns() {
724 return new external_single_structure(
726 'status' => new external_value(PARAM_BOOL
, 'status: true if success'),
727 'warnings' => new external_warnings()