2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * External grade report overview API
20 * @package gradereport_overview
21 * @copyright 2016 Juan Leyva <juan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die;
27 require_once($CFG->libdir . '/externallib.php');
28 require_once($CFG->libdir . '/gradelib.php');
29 require_once($CFG->dirroot . '/grade/lib.php');
30 require_once($CFG->dirroot . '/grade/report/overview/lib.php');
33 * External grade overview report API implementation
35 * @package gradereport_overview
36 * @copyright 2016 Juan Leyva <juan@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class gradereport_overview_external extends external_api {
43 * Describes the parameters for get_course_grades.
45 * @return external_function_parameters
48 public static function get_course_grades_parameters() {
49 return new external_function_parameters (
51 'userid' => new external_value(PARAM_INT, 'Get grades for this user (optional, default current)', VALUE_DEFAULT, 0)
57 * Get the given user courses final grades
59 * @param int $userid get grades for this user (optional, default current)
61 * @return array the grades tables
64 public static function get_course_grades($userid = 0) {
69 // Validate the parameter.
70 $params = self::validate_parameters(self::get_course_grades_parameters(),
76 $userid = $params['userid'];
81 $systemcontext = context_system::instance();
82 self::validate_context($systemcontext);
84 if ($USER->id != $userid) {
85 // We must check if the current user can view other users grades.
86 $user = core_user::get_user($userid, '*', MUST_EXIST);
87 core_user::require_active_user($user);
88 require_capability('moodle/grade:viewall', $systemcontext);
91 // We need the site course, and course context.
92 $course = get_course(SITEID);
93 $context = context_course::instance($course->id);
95 // Force a regrade if required.
96 grade_regrade_final_grades_if_required($course);
97 // Get the course final grades now.
98 $gpr = new grade_plugin_return(array('type' => 'report', 'plugin' => 'overview', 'courseid' => $course->id,
99 'userid' => $userid));
100 $report = new grade_report_overview($userid, $gpr, $context);
101 $coursesgrades = $report->setup_courses_data(true);
104 foreach ($coursesgrades as $coursegrade) {
106 'courseid' => $coursegrade['course']->id,
107 'grade' => grade_format_gradevalue($coursegrade['finalgrade'], $coursegrade['courseitem'], true),
108 'rawgrade' => $coursegrade['finalgrade'],
110 if (isset($coursegrade['rank'])) {
111 $gradeinfo['rank'] = $coursegrade['rank'];
113 $grades[] = $gradeinfo;
117 $result['grades'] = $grades;
118 $result['warnings'] = $warnings;
123 * Describes the get_course_grades return value.
125 * @return external_single_structure
128 public static function get_course_grades_returns() {
129 return new external_single_structure(
131 'grades' => new external_multiple_structure(
132 new external_single_structure(
134 'courseid' => new external_value(PARAM_INT, 'Course id'),
135 'grade' => new external_value(PARAM_RAW, 'Grade formatted'),
136 'rawgrade' => new external_value(PARAM_RAW, 'Raw grade, not formatted'),
137 'rank' => new external_value(PARAM_INT, 'Your rank in the course', VALUE_OPTIONAL),
141 'warnings' => new external_warnings()
147 * Returns description of method parameters
149 * @return external_function_parameters
152 public static function view_grade_report_parameters() {
153 return new external_function_parameters(
155 'courseid' => new external_value(PARAM_INT, 'id of the course'),
156 'userid' => new external_value(PARAM_INT, 'id of the user, 0 means current user', VALUE_DEFAULT, 0)
162 * Trigger the user report events, do the same that the web interface view of the report
164 * @param int $courseid id of course
165 * @param int $userid id of the user the report belongs to
166 * @return array of warnings and status result
168 * @throws moodle_exception
170 public static function view_grade_report($courseid, $userid = 0) {
173 $params = self::validate_parameters(self::view_grade_report_parameters(),
175 'courseid' => $courseid,
181 $course = get_course($params['courseid']);
183 $context = context_course::instance($course->id);
184 self::validate_context($context);
186 $userid = $params['userid'];
187 if (empty($userid)) {
190 $user = core_user::get_user($userid, '*', MUST_EXIST);
191 core_user::require_active_user($user);
193 $systemcontext = context_system::instance();
194 $personalcontext = context_user::instance($userid);
196 $access = grade_report_overview::check_access($systemcontext, $context, $personalcontext, $course, $userid);
199 throw new moodle_exception('nopermissiontoviewgrades', 'error');
202 grade_report_overview::viewed($context, $course->id, $userid);
205 $result['status'] = true;
206 $result['warnings'] = $warnings;
211 * Returns description of method result value
213 * @return external_description
216 public static function view_grade_report_returns() {
217 return new external_single_structure(
219 'status' => new external_value(PARAM_BOOL, 'status: true if success'),
220 'warnings' => new external_warnings()