Commit 464ba8e5 authored by Dan Poltawski's avatar Dan Poltawski
Browse files

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

Conflicts:
	lib/db/services.php
	version.php
parents 522eff0d 45e091ac
......@@ -31,5 +31,13 @@ $functions = array(
'description' => 'Get the user/s report grades table for a course',
'type' => 'read',
'capabilities' => 'gradereport/user:view'
),
'gradereport_user_view_grade_report' => array(
'classname' => 'gradereport_user_external',
'methodname' => 'view_grade_report',
'classpath' => 'grade/report/user/externallib.php',
'description' => 'Trigger the report view event',
'type' => 'write',
'capabilities' => 'gradereport/user:view'
)
);
......@@ -253,4 +253,99 @@ class gradereport_user_external extends external_api {
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 2.9
*/
public static function view_grade_report_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_INT, 'id of the course'),
'userid' => new external_value(PARAM_INT, 'id of the user, 0 means current user', VALUE_DEFAULT, 0)
)
);
}
/**
* Trigger the user report events, do the same that the web interface view of the report
*
* @param int $courseid id of course
* @param int $userid id of the user the report belongs to
* @return array of warnings and status result
* @since Moodle 2.9
* @throws moodle_exception
*/
public static function view_grade_report($courseid, $userid = 0) {
global $CFG, $USER;
require_once($CFG->dirroot . "/grade/lib.php");
require_once($CFG->dirroot . "/grade/report/user/lib.php");
$params = self::validate_parameters(self::view_grade_report_parameters(),
array(
'courseid' => $courseid,
'userid' => $userid
));
$warnings = array();
$course = get_course($params['courseid']);
$context = context_course::instance($course->id);
self::validate_context($context);
$userid = $params['userid'];
if (empty($userid)) {
$userid = $USER->id;
} else {
$user = core_user::get_user($userid, '*', MUST_EXIST);
if ($user->deleted) {
throw new moodle_exception('userdeleted');
}
if (isguestuser($user)) {
// Can not view profile of guest - thre is nothing to see there.
throw new moodle_exception('invaliduserid');
}
}
$access = false;
if (has_capability('moodle/grade:viewall', $context)) {
// Can view all course grades (any user).
$access = true;
} else if ($userid == $USER->id and has_capability('moodle/grade:view', $context) and $course->showgrades) {
// View own grades.
$access = true;
}
if (!$access) {
throw new moodle_exception('nopermissiontoviewgrades', 'error');
}
// Create a report instance. We don't need the gpr second parameter.
$report = new grade_report_user($course->id, null, $context, $userid);
$report->viewed();
$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_grade_report_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings()
)
);
}
}
......@@ -164,13 +164,7 @@ if (has_capability('moodle/grade:viewall', $context)) { //Teachers will see all
}
}
$event = \gradereport_user\event\grade_report_viewed::create(
array(
'context' => $context,
'courseid' => $courseid,
'relateduserid' => $userid,
)
);
$event->trigger();
// Trigger report viewed event.
$report->viewed();
echo $OUTPUT->footer();
......@@ -1004,6 +1004,22 @@ class grade_report_user extends grade_report {
}
}
}
/**
* Trigger the grade_report_viewed event
*
* @since Moodle 2.9
*/
public function viewed() {
$event = \gradereport_user\event\grade_report_viewed::create(
array(
'context' => $this->context,
'courseid' => $this->courseid,
'relateduserid' => $this->user->id,
)
);
$event->trigger();
}
}
function grade_report_user_settings_definition(&$mform) {
......
......@@ -161,4 +161,51 @@ class gradereport_user_externallib_testcase extends externallib_advanced_testcas
}
/**
* Test view_grade_report function
*/
public function test_view_grade_report() {
global $USER;
$this->resetAfterTest(true);
$s1grade = 80;
$s2grade = 60;
list($course, $teacher, $student1, $student2) = $this->load_data($s1grade, $s2grade);
// Redirect events to the sink, so we can recover them later.
$sink = $this->redirectEvents();
$this->setUser($student1);
gradereport_user_external::view_grade_report($course->id);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Check the event details are correct.
$this->assertInstanceOf('\gradereport_user\event\grade_report_viewed', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($USER->id, $event->get_data()['relateduserid']);
$this->setUser($teacher);
gradereport_user_external::view_grade_report($course->id, $student1->id);
$events = $sink->get_events();
$event = reset($events);
$sink->close();
// Check the event details are correct.
$this->assertInstanceOf('\gradereport_user\event\grade_report_viewed', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($student1->id, $event->get_data()['relateduserid']);
$this->setUser($student2);
try {
$studentgrade = gradereport_user_external::view_grade_report($course->id, $student1->id);
$this->fail('Exception expected due to not permissions to view other user grades.');
} catch (moodle_exception $e) {
$this->assertEquals('nopermissiontoviewgrades', $e->errorcode);
}
}
}
......@@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2014111001; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2014111002; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2014110400; // Requires this Moodle version
$plugin->component = 'gradereport_user'; // Full name of the plugin (used for diagnostics)
......@@ -1116,6 +1116,7 @@ $services = array(
'core_notes_view_notes',
'mod_forum_view_forum_discussion',
'core_user_view_user_profile',
'gradereport_user_view_grade_report',
),
'enabled' => 0,
'restrictedusers' => 0,
......
......@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2015040200.04; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2015040200.05; // 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