PageRenderTime 49ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/grade/report/user/externallib.php

https://gitlab.com/JrLucena/moodle
PHP | 349 lines | 216 code | 44 blank | 89 comment | 19 complexity | 5475fba30384e581b05b2a2db6bb4536 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  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.
  8. //
  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.
  13. //
  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/>.
  16. /**
  17. * External grade report user API
  18. *
  19. * @package gradereport_user
  20. * @copyright 2015 Juan Leyva <juan@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die;
  24. require_once("$CFG->libdir/externallib.php");
  25. /**
  26. * External grade report API implementation
  27. *
  28. * @package gradereport_user
  29. * @copyright 2015 Juan Leyva <juan@moodle.com>
  30. * @category external
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class gradereport_user_external extends external_api {
  34. /**
  35. * Describes the parameters for get_grades_table.
  36. *
  37. * @return external_external_function_parameters
  38. * @since Moodle 2.9
  39. */
  40. public static function get_grades_table_parameters() {
  41. return new external_function_parameters (
  42. array(
  43. 'courseid' => new external_value(PARAM_INT, 'Course Id', VALUE_REQUIRED),
  44. 'userid' => new external_value(PARAM_INT, 'Return grades only for this user (optional)', VALUE_DEFAULT, 0)
  45. )
  46. );
  47. }
  48. /**
  49. * Returns a list of grades tables for users in a course.
  50. *
  51. * @param int $courseid Course Id
  52. * @param int $userid Only this user (optional)
  53. *
  54. * @return array the grades tables
  55. * @since Moodle 2.9
  56. */
  57. public static function get_grades_table($courseid, $userid = 0) {
  58. global $CFG, $USER;
  59. $warnings = array();
  60. // Validate the parameter.
  61. $params = self::validate_parameters(self::get_grades_table_parameters(),
  62. array(
  63. 'courseid' => $courseid,
  64. 'userid' => $userid)
  65. );
  66. // Compact/extract functions are not recommended.
  67. $courseid = $params['courseid'];
  68. $userid = $params['userid'];
  69. // Function get_course internally throws an exception if the course doesn't exist.
  70. $course = get_course($courseid);
  71. $context = context_course::instance($courseid);
  72. self::validate_context($context);
  73. // Specific capabilities.
  74. require_capability('gradereport/user:view', $context);
  75. $user = null;
  76. if (empty($userid)) {
  77. require_capability('moodle/grade:viewall', $context);
  78. } else {
  79. $user = core_user::get_user($userid, '*', MUST_EXIST);
  80. core_user::require_active_user($user);
  81. }
  82. $access = false;
  83. if (has_capability('moodle/grade:viewall', $context)) {
  84. // Can view all course grades.
  85. $access = true;
  86. } else if ($userid == $USER->id and has_capability('moodle/grade:view', $context) and $course->showgrades) {
  87. // View own grades.
  88. $access = true;
  89. }
  90. if (!$access) {
  91. throw new moodle_exception('nopermissiontoviewgrades', 'error');
  92. }
  93. // Require files here to save some memory in case validation fails.
  94. require_once($CFG->dirroot . '/group/lib.php');
  95. require_once($CFG->libdir . '/gradelib.php');
  96. require_once($CFG->dirroot . '/grade/lib.php');
  97. require_once($CFG->dirroot . '/grade/report/user/lib.php');
  98. // Force regrade to update items marked as 'needupdate'.
  99. grade_regrade_final_grades($course->id);
  100. $gpr = new grade_plugin_return(
  101. array(
  102. 'type' => 'report',
  103. 'plugin' => 'user',
  104. 'courseid' => $courseid,
  105. 'userid' => $userid)
  106. );
  107. $tables = array();
  108. // Just one user.
  109. if ($user) {
  110. $report = new grade_report_user($courseid, $gpr, $context, $userid);
  111. $report->fill_table();
  112. $tables[] = array(
  113. 'courseid' => $courseid,
  114. 'userid' => $user->id,
  115. 'userfullname' => fullname($user),
  116. 'maxdepth' => $report->maxdepth,
  117. 'tabledata' => $report->tabledata
  118. );
  119. } else {
  120. $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
  121. $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
  122. $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
  123. $gui = new graded_users_iterator($course);
  124. $gui->require_active_enrolment($showonlyactiveenrol);
  125. $gui->init();
  126. while ($userdata = $gui->next_user()) {
  127. $currentuser = $userdata->user;
  128. $report = new grade_report_user($courseid, $gpr, $context, $currentuser->id);
  129. $report->fill_table();
  130. $tables[] = array(
  131. 'courseid' => $courseid,
  132. 'userid' => $currentuser->id,
  133. 'userfullname' => fullname($currentuser),
  134. 'maxdepth' => $report->maxdepth,
  135. 'tabledata' => $report->tabledata
  136. );
  137. }
  138. $gui->close();
  139. }
  140. $result = array();
  141. $result['tables'] = $tables;
  142. $result['warnings'] = $warnings;
  143. return $result;
  144. }
  145. /**
  146. * Creates a table column structure
  147. *
  148. * @return array
  149. * @since Moodle 2.9
  150. */
  151. private static function grades_table_column() {
  152. return array (
  153. 'class' => new external_value(PARAM_RAW, 'class'),
  154. 'content' => new external_value(PARAM_RAW, 'cell content'),
  155. 'headers' => new external_value(PARAM_RAW, 'headers')
  156. );
  157. }
  158. /**
  159. * Describes tget_grades_table return value.
  160. *
  161. * @return external_single_structure
  162. * @since Moodle 2.9
  163. */
  164. public static function get_grades_table_returns() {
  165. return new external_single_structure(
  166. array(
  167. 'tables' => new external_multiple_structure(
  168. new external_single_structure(
  169. array(
  170. 'courseid' => new external_value(PARAM_INT, 'course id'),
  171. 'userid' => new external_value(PARAM_INT, 'user id'),
  172. 'userfullname' => new external_value(PARAM_TEXT, 'user fullname'),
  173. 'maxdepth' => new external_value(PARAM_INT, 'table max depth (needed for printing it)'),
  174. 'tabledata' => new external_multiple_structure(
  175. new external_single_structure(
  176. array(
  177. 'itemname' => new external_single_structure(
  178. array (
  179. 'class' => new external_value(PARAM_RAW, 'class'),
  180. 'colspan' => new external_value(PARAM_INT, 'col span'),
  181. 'content' => new external_value(PARAM_RAW, 'cell content'),
  182. 'celltype' => new external_value(PARAM_RAW, 'cell type'),
  183. 'id' => new external_value(PARAM_ALPHANUMEXT, 'id')
  184. ), 'The item returned data', VALUE_OPTIONAL
  185. ),
  186. 'leader' => new external_single_structure(
  187. array (
  188. 'class' => new external_value(PARAM_RAW, 'class'),
  189. 'rowspan' => new external_value(PARAM_INT, 'row span')
  190. ), 'The item returned data', VALUE_OPTIONAL
  191. ),
  192. 'weight' => new external_single_structure(
  193. self::grades_table_column(), 'weight column', VALUE_OPTIONAL
  194. ),
  195. 'grade' => new external_single_structure(
  196. self::grades_table_column(), 'grade column', VALUE_OPTIONAL
  197. ),
  198. 'range' => new external_single_structure(
  199. self::grades_table_column(), 'range column', VALUE_OPTIONAL
  200. ),
  201. 'percentage' => new external_single_structure(
  202. self::grades_table_column(), 'percentage column', VALUE_OPTIONAL
  203. ),
  204. 'lettergrade' => new external_single_structure(
  205. self::grades_table_column(), 'lettergrade column', VALUE_OPTIONAL
  206. ),
  207. 'rank' => new external_single_structure(
  208. self::grades_table_column(), 'rank column', VALUE_OPTIONAL
  209. ),
  210. 'average' => new external_single_structure(
  211. self::grades_table_column(), 'average column', VALUE_OPTIONAL
  212. ),
  213. 'feedback' => new external_single_structure(
  214. self::grades_table_column(), 'feedback column', VALUE_OPTIONAL
  215. ),
  216. 'contributiontocoursetotal' => new external_single_structure(
  217. self::grades_table_column(), 'contributiontocoursetotal column', VALUE_OPTIONAL
  218. ),
  219. ), 'table'
  220. )
  221. )
  222. )
  223. )
  224. ),
  225. 'warnings' => new external_warnings()
  226. )
  227. );
  228. }
  229. /**
  230. * Returns description of method parameters
  231. *
  232. * @return external_function_parameters
  233. * @since Moodle 2.9
  234. */
  235. public static function view_grade_report_parameters() {
  236. return new external_function_parameters(
  237. array(
  238. 'courseid' => new external_value(PARAM_INT, 'id of the course'),
  239. 'userid' => new external_value(PARAM_INT, 'id of the user, 0 means current user', VALUE_DEFAULT, 0)
  240. )
  241. );
  242. }
  243. /**
  244. * Trigger the user report events, do the same that the web interface view of the report
  245. *
  246. * @param int $courseid id of course
  247. * @param int $userid id of the user the report belongs to
  248. * @return array of warnings and status result
  249. * @since Moodle 2.9
  250. * @throws moodle_exception
  251. */
  252. public static function view_grade_report($courseid, $userid = 0) {
  253. global $CFG, $USER;
  254. require_once($CFG->dirroot . "/grade/lib.php");
  255. require_once($CFG->dirroot . "/grade/report/user/lib.php");
  256. $params = self::validate_parameters(self::view_grade_report_parameters(),
  257. array(
  258. 'courseid' => $courseid,
  259. 'userid' => $userid
  260. ));
  261. $warnings = array();
  262. $course = get_course($params['courseid']);
  263. $context = context_course::instance($course->id);
  264. self::validate_context($context);
  265. $userid = $params['userid'];
  266. if (empty($userid)) {
  267. $userid = $USER->id;
  268. } else {
  269. $user = core_user::get_user($userid, '*', MUST_EXIST);
  270. core_user::require_active_user($user);
  271. }
  272. $access = false;
  273. if (has_capability('moodle/grade:viewall', $context)) {
  274. // Can view all course grades (any user).
  275. $access = true;
  276. } else if ($userid == $USER->id and has_capability('moodle/grade:view', $context) and $course->showgrades) {
  277. // View own grades.
  278. $access = true;
  279. }
  280. if (!$access) {
  281. throw new moodle_exception('nopermissiontoviewgrades', 'error');
  282. }
  283. // Create a report instance. We don't need the gpr second parameter.
  284. $report = new grade_report_user($course->id, null, $context, $userid);
  285. $report->viewed();
  286. $result = array();
  287. $result['status'] = true;
  288. $result['warnings'] = $warnings;
  289. return $result;
  290. }
  291. /**
  292. * Returns description of method result value
  293. *
  294. * @return external_description
  295. * @since Moodle 2.9
  296. */
  297. public static function view_grade_report_returns() {
  298. return new external_single_structure(
  299. array(
  300. 'status' => new external_value(PARAM_BOOL, 'status: true if success'),
  301. 'warnings' => new external_warnings()
  302. )
  303. );
  304. }
  305. }