Commit 29596525 authored by Dan Poltawski's avatar Dan Poltawski
Browse files

Merge branch 'MDL-49504-master' of git://github.com/jleyva/moodle

Conflicts:
	lib/db/services.php
	version.php
parents 75ece778 29ab6351
......@@ -893,6 +893,15 @@ $functions = array(
'capabilities'=> 'moodle/notes:manage',
),
'core_notes_view_notes' => array(
'classname' => 'core_notes_external',
'methodname' => 'view_notes',
'classpath' => 'notes/externallib.php',
'description' => 'Simulates the web interface view of notes/index.php: trigger events.',
'type' => 'write',
'capabilities' => 'moodle/notes:view',
),
// === grading related functions ===
'core_grading_get_definitions' => array(
......@@ -1095,6 +1104,7 @@ $services = array(
'core_completion_get_course_completion_status',
'core_user_view_user_list',
'core_message_mark_message_read',
'core_notes_view_notes',
),
'enabled' => 0,
'restrictedusers' => 0,
......
......@@ -628,6 +628,102 @@ class core_notes_external extends external_api {
), 'notes'
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 2.9
*/
public static function view_notes_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_INT, 'course id, 0 for notes at system level'),
'userid' => new external_value(PARAM_INT, 'user id, 0 means view all the user notes', VALUE_DEFAULT, 0)
)
);
}
/**
* Simulates the web interface view of notes/index.php: trigger events
*
* @param int $courseid id of the course
* @param int $userid id of the user
* @return array of warnings and status result
* @since Moodle 2.9
* @throws moodle_exception
*/
public static function view_notes($courseid, $userid = 0) {
global $CFG;
require_once($CFG->dirroot . "/notes/lib.php");
if (empty($CFG->enablenotes)) {
throw new moodle_exception('notesdisabled', 'notes');
}
$warnings = array();
$arrayparams = array(
'courseid' => $courseid,
'userid' => $userid
);
$params = self::validate_parameters(self::view_notes_parameters(), $arrayparams);
if (empty($params['courseid'])) {
$params['courseid'] = SITEID;
}
$course = get_course($params['courseid']);
if ($course->id == SITEID) {
$context = context_system::instance();
} else {
$context = context_course::instance($course->id);
}
// First of all, validate the context before do further permission checks.
self::validate_context($context);
require_capability('moodle/notes:view', $context);
if (!empty($params['userid'])) {
$user = core_user::get_user($params['userid'], 'id, deleted', MUST_EXIST);
if ($user->deleted) {
throw new moodle_exception('userdeleted');
}
if (isguestuser($user)) {
throw new moodle_exception('invaliduserid');
}
if ($course->id != SITEID and !is_enrolled($context, $user, '', true)) {
throw new moodle_exception('notenrolledprofile');
}
}
note_view($context, $params['userid']);
$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 2.9
*/
public static function view_notes_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings()
)
);
}
}
/**
......
......@@ -87,11 +87,7 @@ require_capability('moodle/notes:view', $coursecontext);
$systemcontext = context_system::instance();
// Trigger event.
$event = \core\event\notes_viewed::create(array(
'relateduserid' => $userid,
'context' => $coursecontext
));
$event->trigger();
note_view($coursecontext, $userid);
$strnotes = get_string('notes', 'notes');
if ($userid) {
......
......@@ -16,6 +16,9 @@
/**
* Library of functions and constants for notes
*
* @package core_notes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
......@@ -347,3 +350,20 @@ function note_delete_all($courseid) {
function note_page_type_list($pagetype, $parentcontext, $currentcontext) {
return array('notes-*' => get_string('page-notes-x', 'notes'));
}
/**
* Trigger notes viewed event
*
* @param stdClass $context context object
* @param int $userid user id (the user we are viewing the notes)
* @since Moodle 2.9
*/
function note_view($context, $userid) {
$event = \core\event\notes_viewed::create(array(
'relateduserid' => $userid,
'context' => $context
));
$event->trigger();
}
......@@ -380,4 +380,71 @@ class core_notes_externallib_testcase extends externallib_advanced_testcase {
$this->assertCount(1, $result['personalnotes']);
}
/**
* Test view_notes
*/
public function test_view_notes() {
global $DB, $CFG;
$this->resetAfterTest(true);
$CFG->enablenotes = true;
// Take role definitions.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
// Create students and teachers.
$student = $this->getDataGenerator()->create_user();
$teacher = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
// Enroll students and teachers to course.
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
// Generate notes.
$gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
$this->setUser($teacher);
// NoteA1: on student (Course) by Teacher.
$params = array('courseid' => $course->id, 'userid' => $student->id, 'publishstate' => NOTES_STATE_PUBLIC,
'usermodified' => $teacher->id);
$notea1 = $gen->create_instance($params);
$sink = $this->redirectEvents();
$result = core_notes_external::view_notes($course->id, $student->id);
$result = external_api::clean_returnvalue(core_notes_external::view_notes_returns(), $result);
$result = core_notes_external::view_notes($course->id);
$result = external_api::clean_returnvalue(core_notes_external::view_notes_returns(), $result);
$events = $sink->get_events();
$this->assertCount(2, $events);
$this->assertInstanceOf('\core\event\notes_viewed', $events[0]);
$this->assertEquals($coursecontext, $events[0]->get_context());
$this->assertEquals($student->id, $events[0]->relateduserid);
$this->assertInstanceOf('\core\event\notes_viewed', $events[1]);
$this->assertEquals($coursecontext, $events[1]->get_context());
$this->assertEquals(0, $events[1]->relateduserid);
try {
core_notes_external::view_notes(0);
$this->fail('Exception expected due to invalid permissions at system level.');
} catch (moodle_exception $e) {
$this->assertEquals('nopermissions', $e->errorcode);
}
try {
core_notes_external::view_notes($course->id, $student->id + 100);
$this->fail('Exception expected due to invalid user id.');
} catch (moodle_exception $e) {
$this->assertEquals('invaliduser', $e->errorcode);
}
}
}
......@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2015040700.01; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2015040700.02; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment