Skip to content

Commit

Permalink
MDL-49330 core_notes: New external function core_notes_get_course_notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Costantino Cito authored and jleyva committed Apr 1, 2015
1 parent 20d3883 commit 9857381
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
168 changes: 168 additions & 0 deletions notes/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}
}

/**
Expand Down

0 comments on commit 9857381

Please sign in to comment.