PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/externallib.php

https://bitbucket.org/moodle/moodle
PHP | 468 lines | 333 code | 29 blank | 106 comment | 31 complexity | 3538f037866182cfdd30ce32c3e076d1 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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 grading API
  18. *
  19. * @package core_grading
  20. * @since Moodle 2.5
  21. * @copyright 2013 Paul Charsley
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die;
  25. require_once("$CFG->libdir/externallib.php");
  26. require_once("$CFG->dirroot/grade/grading/lib.php");
  27. /**
  28. * core grading functions
  29. */
  30. class core_grading_external extends external_api {
  31. /**
  32. * Describes the parameters for get_definitions
  33. * @return external_function_parameters
  34. * @since Moodle 2.5
  35. */
  36. public static function get_definitions_parameters() {
  37. return new external_function_parameters(
  38. array(
  39. 'cmids' => new external_multiple_structure(
  40. new external_value(PARAM_INT, 'course module id'), '1 or more course module ids'),
  41. 'areaname' => new external_value(PARAM_AREA, 'area name'),
  42. 'activeonly' => new external_value(PARAM_BOOL, 'Only the active method', VALUE_DEFAULT, 0)
  43. )
  44. );
  45. }
  46. /**
  47. * Returns the definitions for the requested course module ids
  48. * @param array of ints $cmids
  49. * @param string $areaname
  50. * @param boolean $activeonly default is false, if true, only the active method is returned
  51. * @return array of areas with definitions for each requested course module id
  52. * @since Moodle 2.5
  53. */
  54. public static function get_definitions($cmids, $areaname, $activeonly = false) {
  55. global $DB, $CFG;
  56. require_once("$CFG->dirroot/grade/grading/form/lib.php");
  57. $params = self::validate_parameters(self::get_definitions_parameters(),
  58. array('cmids' => $cmids,
  59. 'areaname' => $areaname,
  60. 'activeonly' => $activeonly));
  61. $warnings = array();
  62. $areas = array();
  63. foreach ($params['cmids'] as $cmid) {
  64. $context = context_module::instance($cmid);
  65. try {
  66. self::validate_context($context);
  67. } catch (Exception $e) {
  68. $warnings[] = array(
  69. 'item' => 'module',
  70. 'itemid' => $cmid,
  71. 'message' => 'No access rights in module context',
  72. 'warningcode' => '1'
  73. );
  74. continue;
  75. }
  76. // Check if the user has managegradingforms capability.
  77. $isgradingmethodmanager = false;
  78. if (has_capability('moodle/grade:managegradingforms', $context)) {
  79. $isgradingmethodmanager = true;
  80. }
  81. $module = get_coursemodule_from_id('', $cmid, 0, false, MUST_EXIST);
  82. $componentname = "mod_".$module->modname;
  83. // Get the grading manager.
  84. $gradingmanager = get_grading_manager($context, $componentname, $params['areaname']);
  85. // Get the controller for each grading method.
  86. $methods = array();
  87. if ($params['activeonly'] == true) {
  88. $methods[] = $gradingmanager->get_active_method();
  89. } else {
  90. $methods = array_keys($gradingmanager->get_available_methods(false));
  91. }
  92. $area = array();
  93. $area['cmid'] = $cmid;
  94. $area['contextid'] = $context->id;
  95. $area['component'] = $componentname;
  96. $area['activemethod'] = $gradingmanager->get_active_method();
  97. $area['definitions'] = array();
  98. foreach ($methods as $method) {
  99. $controller = $gradingmanager->get_controller($method);
  100. $def = $controller->get_definition(true);
  101. if ($def == false) {
  102. continue;
  103. }
  104. if ($isgradingmethodmanager == false) {
  105. $isviewable = true;
  106. if ($def->status != gradingform_controller::DEFINITION_STATUS_READY) {
  107. $warnings[] = array(
  108. 'item' => 'module',
  109. 'itemid' => $cmid,
  110. 'message' => 'Capability moodle/grade:managegradingforms required to view draft definitions',
  111. 'warningcode' => '1'
  112. );
  113. $isviewable = false;
  114. }
  115. if (!empty($def->options)) {
  116. $options = json_decode($def->options);
  117. if (isset($options->alwaysshowdefinition) &&
  118. $options->alwaysshowdefinition == 0) {
  119. $warnings[] = array(
  120. 'item' => 'module',
  121. 'itemid' => $cmid,
  122. 'message' => 'Capability moodle/grade:managegradingforms required to preview definition',
  123. 'warningcode' => '1'
  124. );
  125. $isviewable = false;
  126. }
  127. }
  128. if ($isviewable == false) {
  129. continue;
  130. }
  131. }
  132. $definition = array();
  133. $definition['id'] = $def->id;
  134. $definition['method'] = $method;
  135. $definition['name'] = $def->name;
  136. $definition['description'] = $def->description;
  137. $definition['descriptionformat'] = $def->descriptionformat;
  138. $definition['status'] = $def->status;
  139. $definition['copiedfromid'] = $def->copiedfromid;
  140. $definition['timecreated'] = $def->timecreated;
  141. $definition['usercreated'] = $def->usercreated;
  142. $definition['timemodified'] = $def->timemodified;
  143. $definition['usermodified'] = $def->usermodified;
  144. $definition['timecopied'] = $def->timecopied;
  145. // Format the description text field.
  146. $formattedtext = external_format_text($definition['description'],
  147. $definition['descriptionformat'],
  148. $context->id,
  149. $componentname,
  150. 'description',
  151. $def->id);
  152. $definition['description'] = $formattedtext[0];
  153. $definition['descriptionformat'] = $formattedtext[1];
  154. $details = $controller->get_external_definition_details();
  155. $items = array();
  156. foreach ($details as $key => $value) {
  157. $items[$key] = self::format_text($def->{$key}, $context->id, $componentname, $def->id);
  158. }
  159. $definition[$method] = $items;
  160. $area['definitions'][] = $definition;
  161. }
  162. $areas[] = $area;
  163. }
  164. $result = array(
  165. 'areas' => $areas,
  166. 'warnings' => $warnings
  167. );
  168. return $result;
  169. }
  170. /**
  171. * Recursively processes all elements in an array and runs external_format_text()on
  172. * all elements which have a text field and associated format field with a key name
  173. * that ends with the text 'format'. The modified array is returned.
  174. * @param array $items the array to be processed
  175. * @param int $contextid
  176. * @param string $componentname
  177. * @param int $itemid
  178. * @see external_format_text in lib/externallib.php
  179. * @return array the input array with all fields formatted
  180. */
  181. private static function format_text($items, $contextid, $componentname, $itemid) {
  182. $formatkeys = array();
  183. foreach ($items as $key => $value) {
  184. if (!is_array($value) && substr_compare($key, 'format', -6, 6) === 0) {
  185. $formatkeys[] = $key;
  186. }
  187. }
  188. foreach ($formatkeys as $formatkey) {
  189. $descriptionkey = substr($formatkey, 0, -6);
  190. $formattedtext = external_format_text($items[$descriptionkey],
  191. $items[$formatkey],
  192. $contextid,
  193. $componentname,
  194. 'description',
  195. $itemid);
  196. $items[$descriptionkey] = $formattedtext[0];
  197. $items[$formatkey] = $formattedtext[1];
  198. }
  199. foreach ($items as $key => $value) {
  200. if (is_array($value)) {
  201. $items[$key] = self::format_text($value, $contextid, $componentname, $itemid);
  202. }
  203. }
  204. return $items;
  205. }
  206. /**
  207. * Creates a grading area
  208. * @return external_single_structure
  209. * @since Moodle 2.5
  210. */
  211. private static function grading_area() {
  212. return new external_single_structure(
  213. array (
  214. 'cmid' => new external_value(PARAM_INT, 'course module id'),
  215. 'contextid' => new external_value(PARAM_INT, 'context id'),
  216. 'component' => new external_value(PARAM_TEXT, 'component name'),
  217. 'activemethod' => new external_value(PARAM_TEXT, 'active method', VALUE_OPTIONAL),
  218. 'definitions' => new external_multiple_structure(self::definition(), 'definitions')
  219. )
  220. );
  221. }
  222. /**
  223. * creates a grading form definition
  224. * @return external_single_structure
  225. * @since Moodle 2.5
  226. */
  227. private static function definition() {
  228. global $CFG;
  229. $definition = array();
  230. $definition['id'] = new external_value(PARAM_INT, 'definition id');
  231. $definition['method'] = new external_value(PARAM_TEXT, 'method');
  232. $definition['name'] = new external_value(PARAM_TEXT, 'name');
  233. $definition['description'] = new external_value(PARAM_RAW, 'description');
  234. $definition['descriptionformat'] = new external_format_value('description');
  235. $definition['status'] = new external_value(PARAM_INT, 'status');
  236. $definition['copiedfromid'] = new external_value(PARAM_INT, 'copied from id', VALUE_OPTIONAL);
  237. $definition['timecreated'] = new external_value(PARAM_INT, 'creation time');
  238. $definition['usercreated'] = new external_value(PARAM_INT, 'user who created definition');
  239. $definition['timemodified'] = new external_value(PARAM_INT, 'last modified time');
  240. $definition['usermodified'] = new external_value(PARAM_INT, 'user who modified definition');
  241. $definition['timecopied'] = new external_value(PARAM_INT, 'time copied', VALUE_OPTIONAL);
  242. foreach (self::get_grading_methods() as $method) {
  243. require_once($CFG->dirroot.'/grade/grading/form/'.$method.'/lib.php');
  244. $details = call_user_func('gradingform_'.$method.'_controller::get_external_definition_details');
  245. if ($details != null) {
  246. $items = array();
  247. foreach ($details as $key => $value) {
  248. $details[$key]->required = VALUE_OPTIONAL;
  249. $items[$key] = $value;
  250. }
  251. $definition[$method] = new external_single_structure($items, 'items', VALUE_OPTIONAL);
  252. }
  253. }
  254. return new external_single_structure($definition);
  255. }
  256. /**
  257. * Describes the get_definitions return value
  258. * @return external_single_structure
  259. * @since Moodle 2.5
  260. */
  261. public static function get_definitions_returns() {
  262. return new external_single_structure(
  263. array(
  264. 'areas' => new external_multiple_structure(self::grading_area(), 'list of grading areas'),
  265. 'warnings' => new external_warnings()
  266. )
  267. );
  268. }
  269. /**
  270. * @return array of available grading methods
  271. * @since Moodle 2.5
  272. */
  273. private static function get_grading_methods() {
  274. $methods = array_keys(grading_manager::available_methods(false));
  275. return $methods;
  276. }
  277. /**
  278. * Describes the parameters for get_gradingform_instances
  279. *
  280. * @return external_function_parameters
  281. * @since Moodle 2.6
  282. */
  283. public static function get_gradingform_instances_parameters() {
  284. return new external_function_parameters(
  285. array(
  286. 'definitionid' => new external_value(PARAM_INT, 'definition id'),
  287. 'since' => new external_value(PARAM_INT, 'submitted since', VALUE_DEFAULT, 0)
  288. )
  289. );
  290. }
  291. /**
  292. * Returns the instances and fillings for the requested definition id
  293. *
  294. * @param int $definitionid
  295. * @param int $since only return instances with timemodified >= since
  296. * @return array of grading instances with fillings for the definition id
  297. * @since Moodle 2.6
  298. */
  299. public static function get_gradingform_instances($definitionid, $since = 0) {
  300. global $DB, $CFG;
  301. require_once("$CFG->dirroot/grade/grading/form/lib.php");
  302. $params = self::validate_parameters(self::get_gradingform_instances_parameters(),
  303. array('definitionid' => $definitionid,
  304. 'since' => $since));
  305. $instances = array();
  306. $warnings = array();
  307. $definition = $DB->get_record('grading_definitions',
  308. array('id' => $params['definitionid']),
  309. 'areaid,method', MUST_EXIST);
  310. $area = $DB->get_record('grading_areas',
  311. array('id' => $definition->areaid),
  312. 'contextid,component', MUST_EXIST);
  313. $context = context::instance_by_id($area->contextid);
  314. require_capability('moodle/grade:managegradingforms', $context);
  315. $gradingmanager = get_grading_manager($definition->areaid);
  316. $controller = $gradingmanager->get_controller($definition->method);
  317. $activeinstances = $controller->get_all_active_instances ($params['since']);
  318. $details = $controller->get_external_instance_filling_details();
  319. if ($details == null) {
  320. $warnings[] = array(
  321. 'item' => 'definition',
  322. 'itemid' => $params['definitionid'],
  323. 'message' => 'Fillings unavailable because get_external_instance_filling_details is not defined',
  324. 'warningcode' => '1'
  325. );
  326. }
  327. $getfilling = null;
  328. if (method_exists('gradingform_'.$definition->method.'_instance', 'get_'.$definition->method.'_filling')) {
  329. $getfilling = 'get_'.$definition->method.'_filling';
  330. } else {
  331. $warnings[] = array(
  332. 'item' => 'definition',
  333. 'itemid' => $params['definitionid'],
  334. 'message' => 'Fillings unavailable because get_'.$definition->method.'_filling is not defined',
  335. 'warningcode' => '1'
  336. );
  337. }
  338. foreach ($activeinstances as $activeinstance) {
  339. $instance = array();
  340. $instance['id'] = $activeinstance->get_id();
  341. $instance['raterid'] = $activeinstance->get_data('raterid');
  342. $instance['itemid'] = $activeinstance->get_data('itemid');
  343. $instance['rawgrade'] = $activeinstance->get_data('rawgrade');
  344. $instance['status'] = $activeinstance->get_data('status');
  345. $instance['feedback'] = $activeinstance->get_data('feedback');
  346. $instance['feedbackformat'] = $activeinstance->get_data('feedbackformat');
  347. // Format the feedback text field.
  348. $formattedtext = external_format_text($activeinstance->get_data('feedback'),
  349. $activeinstance->get_data('feedbackformat'),
  350. $context->id,
  351. $area->component,
  352. 'feedback',
  353. $params['definitionid']);
  354. $instance['feedback'] = $formattedtext[0];
  355. $instance['feedbackformat'] = $formattedtext[1];
  356. $instance['timemodified'] = $activeinstance->get_data('timemodified');
  357. if ($details != null && $getfilling != null) {
  358. $fillingdata = $activeinstance->$getfilling();
  359. $filling = array();
  360. foreach ($details as $key => $value) {
  361. $filling[$key] = self::format_text($fillingdata[$key],
  362. $context->id,
  363. $area->component,
  364. $params['definitionid']);
  365. }
  366. $instance[$definition->method] = $filling;
  367. }
  368. $instances[] = $instance;
  369. }
  370. $result = array(
  371. 'instances' => $instances,
  372. 'warnings' => $warnings
  373. );
  374. return $result;
  375. }
  376. /**
  377. * Creates a grading instance
  378. *
  379. * @return external_single_structure
  380. * @since Moodle 2.6
  381. */
  382. private static function grading_instance() {
  383. global $CFG;
  384. $instance = array();
  385. $instance['id'] = new external_value(PARAM_INT, 'instance id');
  386. $instance['raterid'] = new external_value(PARAM_INT, 'rater id');
  387. $instance['itemid'] = new external_value(PARAM_INT, 'item id');
  388. $instance['rawgrade'] = new external_value(PARAM_TEXT, 'raw grade', VALUE_OPTIONAL);
  389. $instance['status'] = new external_value(PARAM_INT, 'status');
  390. $instance['feedback'] = new external_value(PARAM_RAW, 'feedback', VALUE_OPTIONAL);
  391. $instance['feedbackformat'] = new external_format_value('feedback', VALUE_OPTIONAL);
  392. $instance['timemodified'] = new external_value(PARAM_INT, 'modified time');
  393. foreach (self::get_grading_methods() as $method) {
  394. require_once($CFG->dirroot.'/grade/grading/form/'.$method.'/lib.php');
  395. $details = call_user_func('gradingform_'.$method.'_controller::get_external_instance_filling_details');
  396. if ($details != null) {
  397. $items = array();
  398. foreach ($details as $key => $value) {
  399. $details[$key]->required = VALUE_OPTIONAL;
  400. $items[$key] = $value;
  401. }
  402. $instance[$method] = new external_single_structure($items, 'items', VALUE_OPTIONAL);
  403. }
  404. }
  405. return new external_single_structure($instance);
  406. }
  407. /**
  408. * Describes the get_gradingform_instances return value
  409. *
  410. * @return external_single_structure
  411. * @since Moodle 2.6
  412. */
  413. public static function get_gradingform_instances_returns() {
  414. return new external_single_structure(
  415. array(
  416. 'instances' => new external_multiple_structure(self::grading_instance(), 'list of grading instances'),
  417. 'warnings' => new external_warnings()
  418. )
  419. );
  420. }
  421. }
  422. /**
  423. * core grading functions. Renamed to core_grading_external
  424. *
  425. * @since Moodle 2.5
  426. * @deprecated since 2.6 See MDL-30085. Please do not use this class any more.
  427. * @see core_grading_external
  428. */
  429. class core_grade_external extends external_api {
  430. public static function get_definitions_parameters() {
  431. return core_grading_external::get_definitions_parameters();
  432. }
  433. public static function get_definitions($cmids, $areaname, $activeonly = false) {
  434. return core_grading_external::get_definitions($cmids, $areaname, $activeonly = false);
  435. }
  436. public static function get_definitions_returns() {
  437. return core_grading_external::get_definitions_returns();
  438. }
  439. }