From 9857381dc335dcff02986acecfb5f8c6dde646f6 Mon Sep 17 00:00:00 2001 From: Costantino Cito Date: Fri, 20 Mar 2015 12:56:32 +0100 Subject: [PATCH] MDL-49330 core_notes: New external function core_notes_get_course_notes --- lib/db/services.php | 11 ++- notes/externallib.php | 168 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+), 1 deletion(-) diff --git a/lib/db/services.php b/lib/db/services.php index 47eb28c3cb858..c23624c38872e 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -312,6 +312,14 @@ 'capabilities' => 'moodle/course:managegroups', ), + 'core_notes_get_course_notes' => array( + 'classname' => 'core_notes_external', + 'methodname' => 'get_course_notes', + 'description' => 'Returns all notes in specified course (or site) for the specified user.', + 'type' => 'read', + 'capabilities' => 'moodle/notes:view', + ), + // === file related functions === 'moodle_file_get_files' => array( @@ -1023,7 +1031,8 @@ 'core_user_remove_user_device', 'core_course_get_courses', 'core_completion_update_activity_completion_status_manually', - 'mod_data_get_databases_by_courses' + 'mod_data_get_databases_by_courses', + 'core_notes_get_course_notes', ), 'enabled' => 0, 'restrictedusers' => 0, diff --git a/notes/externallib.php b/notes/externallib.php index d06d98270e7c2..957a07de5b560 100644 --- a/notes/externallib.php +++ b/notes/externallib.php @@ -463,6 +463,174 @@ public static function update_notes_returns() { '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 method parameters + * + * @return external_function_parameters + * @since Moodle 2.9 + */ + public static function get_course_notes_parameters() { + return new external_function_parameters( + array( + 'courseid' => new external_value(PARAM_INT, 'course id, 0 for SITE'), + 'userid' => new external_value(PARAM_INT, 'user id', VALUE_OPTIONAL), + ) + ); + } + + /** + * Create a notes list + * + * @param int $courseid ID of the Course + * @param stdClass $context context object + * @param int $userid ID of the User + * @param int $state + * @param int $author + * @return array of notes + * @since Moodle 2.9 + */ + protected static function create_note_list($courseid, $context, $userid, $state, $author = 0) { + $results = array(); + $notes = note_list($courseid, $userid, $state, $author); + foreach ($notes as $key => $note) { + $note = (array)$note; + list($note['content'], $note['format']) = external_format_text($note['content'], + $note['format'], + $context->id, + '', + '', + 0); + $results[$key] = $note; + } + return $results; + } + + /** + * Get a list of course notes + * + * @param int $courseid ID of the Course + * @param int $userid ID of the User + * @return array of site, course and personal notes and warnings + * @since Moodle 2.9 + * @throws moodle_exception + */ + public static function get_course_notes($courseid, $userid = 0) { + global $CFG, $USER; + + if (empty($CFG->enablenotes)) { + throw new moodle_exception('notesdisabled', 'notes'); + } + + $warnings = array(); + $arrayparams = array( + 'courseid' => $courseid, + 'userid' => $userid, + ); + $params = self::validate_parameters(self::get_course_notes_parameters(), $arrayparams); + + if (empty($params['courseid'])) { + $params['courseid'] = SITEID; + } + $user = null; + if (!empty($params['userid'])) { + $user = core_user::get_user($params['userid'], 'id', MUST_EXIST); + } + + $course = get_course($params['courseid']); + + if ($course->id == SITEID) { + $context = context_system::instance(); + } else { + $context = context_course::instance($course->id); + } + self::validate_context($context); + + $sitenotes = array(); + $coursenotes = array(); + $personalnotes = array(); + + if ($course->id != SITEID) { + + require_capability('moodle/notes:view', $context); + $sitenotes = self::create_note_list($course->id, $context, $params['userid'], NOTES_STATE_SITE); + $coursenotes = self::create_note_list($course->id, $context, $params['userid'], NOTES_STATE_PUBLIC); + $personalnotes = self::create_note_list($course->id, $context, $params['userid'], NOTES_STATE_DRAFT, + $USER->id); + } else { + if (has_capability('moodle/notes:view', $context)) { + $sitenotes = self::create_note_list(0, $context, $params['userid'], NOTES_STATE_SITE); + } + // It returns notes only for a specific user! + if (!empty($user)) { + $usercourses = enrol_get_users_courses($user->id, true); + foreach ($usercourses as $c) { + // All notes at course level, only if we have capability on every course. + if (has_capability('moodle/notes:view', context_course::instance($c->id))) { + $coursenotes += self::create_note_list($c->id, $context, $params['userid'], NOTES_STATE_PUBLIC); + } + } + } + } + + $results = array( + 'sitenotes' => $sitenotes, + 'coursenotes' => $coursenotes, + 'personalnotes' => $personalnotes, + 'warnings' => $warnings + ); + return $results; + + } + + /** + * Returns array of note structure + * + * @return external_description + * @since Moodle 2.9 + */ + protected static function get_note_structure() { + return array( + 'id' => new external_value(PARAM_INT, 'id of this note'), + 'courseid' => new external_value(PARAM_INT, 'id of the course'), + 'userid' => new external_value(PARAM_INT, 'user id'), + 'content' => new external_value(PARAM_RAW, 'the content text formated'), + 'format' => new external_format_value('content'), + 'created' => new external_value(PARAM_INT, 'time created (timestamp)'), + 'lastmodified' => new external_value(PARAM_INT, 'time of last modification (timestamp)'), + 'usermodified' => new external_value(PARAM_INT, 'user id of the creator of this note'), + 'publishstate' => new external_value(PARAM_ALPHA, "state of the note (i.e. draft, public, site) ") + ); + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 2.9 + */ + public static function get_course_notes_returns() { + return new external_single_structure( + array( + 'sitenotes' => new external_multiple_structure( + new external_single_structure( + self::get_note_structure() , '' + ), 'site notes', VALUE_OPTIONAL + ), + 'coursenotes' => new external_multiple_structure( + new external_single_structure( + self::get_note_structure() , '' + ), 'couse notes', VALUE_OPTIONAL + ), + 'personalnotes' => new external_multiple_structure( + new external_single_structure( + self::get_note_structure() , '' + ), 'personal notes', VALUE_OPTIONAL + ), + 'warnings' => new external_warnings() + ), 'notes' + ); + } } /**