Skip to content

Commit

Permalink
MDL-49500 grades: External function gradereport_user_view_grade_report
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Apr 2, 2015
1 parent ef0dd22 commit 5727212
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions grade/report/user/externallib.php
Expand Up @@ -253,4 +253,99 @@ public static function get_grades_table_returns() {
)
);
}

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

0 comments on commit 5727212

Please sign in to comment.