mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
MDL-54854 grade: New WS gradereport_overview_view_grade_report
This commit is contained in:
parent
7cd19312f5
commit
78310c30d0
@ -142,4 +142,83 @@ class gradereport_overview_external extends external_api {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
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 3.2
|
||||
* @throws moodle_exception
|
||||
*/
|
||||
public static function view_grade_report($courseid, $userid = 0) {
|
||||
global $USER;
|
||||
|
||||
$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);
|
||||
core_user::require_active_user($user);
|
||||
}
|
||||
$systemcontext = context_system::instance();
|
||||
$personalcontext = context_user::instance($userid);
|
||||
|
||||
$access = grade_report_overview::check_access($systemcontext, $context, $personalcontext, $course, $userid);
|
||||
|
||||
if (!$access) {
|
||||
throw new moodle_exception('nopermissiontoviewgrades', 'error');
|
||||
}
|
||||
|
||||
grade_report_overview::viewed($context, $course->id, $userid);
|
||||
|
||||
$result = array();
|
||||
$result['status'] = true;
|
||||
$result['warnings'] = $warnings;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
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()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -32,5 +32,13 @@ $functions = array(
|
||||
'description' => 'Get the given user courses final grades',
|
||||
'type' => 'read',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'gradereport_overview_view_grade_report' => array(
|
||||
'classname' => 'gradereport_overview_external',
|
||||
'methodname' => 'view_grade_report',
|
||||
'description' => 'Trigger the report view event',
|
||||
'type' => 'write',
|
||||
'capabilities' => 'gradereport/overview:view',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
)
|
||||
);
|
||||
|
@ -71,27 +71,7 @@ if ($userid == $USER->id) {
|
||||
$PAGE->navigation->extend_for_user($user);
|
||||
}
|
||||
|
||||
$access = false;
|
||||
if (has_capability('moodle/grade:viewall', $systemcontext)) {
|
||||
// Ok - can view all course grades.
|
||||
$access = true;
|
||||
|
||||
} else if (has_capability('moodle/grade:viewall', $context)) {
|
||||
// Ok - can view any grades in context.
|
||||
$access = true;
|
||||
|
||||
} else if ($userid == $USER->id and ((has_capability('moodle/grade:view', $context) and $course->showgrades)
|
||||
|| $courseid == SITEID)) {
|
||||
// Ok - can view own course grades.
|
||||
$access = true;
|
||||
|
||||
} else if (has_capability('moodle/grade:viewall', $personalcontext) and $course->showgrades) {
|
||||
// Ok - can view grades of this user - parent most probably.
|
||||
$access = true;
|
||||
} else if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) and $course->showgrades) {
|
||||
// Ok - can view grades of this user - parent most probably.
|
||||
$access = true;
|
||||
}
|
||||
$access = grade_report_overview::check_access($systemcontext, $context, $personalcontext, $course, $userid);
|
||||
|
||||
if (!$access) {
|
||||
// no access to grades!
|
||||
@ -214,15 +194,6 @@ if (has_capability('moodle/grade:viewall', $context) && $courseid != SITEID) {
|
||||
}
|
||||
}
|
||||
|
||||
$event = \gradereport_overview\event\grade_report_viewed::create(
|
||||
array(
|
||||
'context' => $context,
|
||||
'courseid' => $courseid,
|
||||
'relateduserid' => $userid,
|
||||
)
|
||||
);
|
||||
$event->trigger();
|
||||
grade_report_overview::viewed($context, $courseid, $userid);
|
||||
|
||||
echo $OUTPUT->footer();
|
||||
|
||||
|
||||
|
@ -359,6 +359,63 @@ class grade_report_overview extends grade_report {
|
||||
public static function supports_mygrades() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the user can access the report.
|
||||
*
|
||||
* @param stdClass $systemcontext system context
|
||||
* @param stdClass $context course context
|
||||
* @param stdClass $personalcontext personal context
|
||||
* @param stdClass $course course object
|
||||
* @param int $userid userid
|
||||
* @return bool true if the user can access the report
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
public static function check_access($systemcontext, $context, $personalcontext, $course, $userid) {
|
||||
global $USER;
|
||||
|
||||
$access = false;
|
||||
if (has_capability('moodle/grade:viewall', $systemcontext)) {
|
||||
// Ok - can view all course grades.
|
||||
$access = true;
|
||||
|
||||
} else if (has_capability('moodle/grade:viewall', $context)) {
|
||||
// Ok - can view any grades in context.
|
||||
$access = true;
|
||||
|
||||
} else if ($userid == $USER->id and ((has_capability('moodle/grade:view', $context) and $course->showgrades)
|
||||
|| $course->id == SITEID)) {
|
||||
// Ok - can view own course grades.
|
||||
$access = true;
|
||||
|
||||
} else if (has_capability('moodle/grade:viewall', $personalcontext) and $course->showgrades) {
|
||||
// Ok - can view grades of this user - parent most probably.
|
||||
$access = true;
|
||||
} else if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) and $course->showgrades) {
|
||||
// Ok - can view grades of this user - parent most probably.
|
||||
$access = true;
|
||||
}
|
||||
return $access;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger the grade_report_viewed event
|
||||
*
|
||||
* @param stdClass $context course context
|
||||
* @param int $courseid course id
|
||||
* @param int $userid user id
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
public static function viewed($context, $courseid, $userid) {
|
||||
$event = \gradereport_overview\event\grade_report_viewed::create(
|
||||
array(
|
||||
'context' => $context,
|
||||
'courseid' => $courseid,
|
||||
'relateduserid' => $userid,
|
||||
)
|
||||
);
|
||||
$event->trigger();
|
||||
}
|
||||
}
|
||||
|
||||
function grade_report_overview_settings_definition(&$mform) {
|
||||
|
@ -184,4 +184,48 @@ class gradereport_overview_externallib_testcase extends externallib_advanced_tes
|
||||
$this->expectException('required_capability_exception');
|
||||
$studentgrade = gradereport_overview_external::get_course_grades($this->student1->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test view_grade_report function
|
||||
*/
|
||||
public function test_view_grade_report() {
|
||||
global $USER;
|
||||
|
||||
// Redirect events to the sink, so we can recover them later.
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
$this->setUser($this->student1);
|
||||
$result = gradereport_overview_external::view_grade_report($this->course1->id);
|
||||
$result = external_api::clean_returnvalue(gradereport_overview_external::view_grade_report_returns(), $result);
|
||||
$events = $sink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Check the event details are correct.
|
||||
$this->assertInstanceOf('\gradereport_overview\event\grade_report_viewed', $event);
|
||||
$this->assertEquals(context_course::instance($this->course1->id), $event->get_context());
|
||||
$this->assertEquals($USER->id, $event->get_data()['relateduserid']);
|
||||
|
||||
$this->setUser($this->teacher);
|
||||
$result = gradereport_overview_external::view_grade_report($this->course1->id, $this->student1->id);
|
||||
$result = external_api::clean_returnvalue(gradereport_overview_external::view_grade_report_returns(), $result);
|
||||
$events = $sink->get_events();
|
||||
$event = reset($events);
|
||||
$sink->close();
|
||||
|
||||
// Check the event details are correct.
|
||||
$this->assertInstanceOf('\gradereport_overview\event\grade_report_viewed', $event);
|
||||
$this->assertEquals(context_course::instance($this->course1->id), $event->get_context());
|
||||
$this->assertEquals($this->student1->id, $event->get_data()['relateduserid']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test view_grade_report_permissions function
|
||||
*/
|
||||
public function test_view_grade_report_permissions() {
|
||||
$this->setUser($this->student2);
|
||||
|
||||
$this->expectException('moodle_exception');
|
||||
$studentgrade = gradereport_overview_external::view_grade_report($this->course1->id, $this->student1->id);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user