MDL-49500 grades: External function gradereport_user_view_grade_report

This commit is contained in:
Juan Leyva 2015-03-30 10:33:54 +02:00
parent ef0dd22446
commit 57272121d8

View File

@ -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()
)
);
}
}