external.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 question API
  18. *
  19. * @package core_question
  20. * @category external
  21. * @copyright 2016 Pau Ferrer <pau@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. require_once("$CFG->libdir/externallib.php");
  25. require_once($CFG->dirroot . '/question/engine/lib.php');
  26. /**
  27. * Question external functions
  28. *
  29. * @package core_question
  30. * @category external
  31. * @copyright 2016 Pau Ferrer <pau@moodle.com>
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. * @since Moodle 3.1
  34. */
  35. class core_question_external extends external_api {
  36. /**
  37. * Returns description of method parameters
  38. *
  39. * @return external_function_parameters
  40. * @since Moodle 3.1
  41. */
  42. public static function update_flag_parameters() {
  43. return new external_function_parameters(
  44. array(
  45. 'qubaid' => new external_value(PARAM_INT, 'the question usage id.'),
  46. 'questionid' => new external_value(PARAM_INT, 'the question id'),
  47. 'qaid' => new external_value(PARAM_INT, 'the question_attempt id'),
  48. 'slot' => new external_value(PARAM_INT, 'the slot number within the usage'),
  49. 'checksum' => new external_value(PARAM_ALPHANUM, 'computed checksum with the last three arguments and
  50. the users username'),
  51. 'newstate' => new external_value(PARAM_BOOL, 'the new state of the flag. true = flagged')
  52. )
  53. );
  54. }
  55. /**
  56. * Update the flag state of a question attempt.
  57. *
  58. * @param int $qubaid the question usage id.
  59. * @param int $questionid the question id.
  60. * @param int $qaid the question_attempt id.
  61. * @param int $slot the slot number within the usage.
  62. * @param string $checksum checksum, as computed by {@link get_toggle_checksum()}
  63. * corresponding to the last three arguments and the users username.
  64. * @param bool $newstate the new state of the flag. true = flagged.
  65. * @return array (success infos and fail infos)
  66. * @since Moodle 3.1
  67. */
  68. public static function update_flag($qubaid, $questionid, $qaid, $slot, $checksum, $newstate) {
  69. global $CFG, $DB;
  70. $params = self::validate_parameters(self::update_flag_parameters(),
  71. array(
  72. 'qubaid' => $qubaid,
  73. 'questionid' => $questionid,
  74. 'qaid' => $qaid,
  75. 'slot' => $slot,
  76. 'checksum' => $checksum,
  77. 'newstate' => $newstate
  78. )
  79. );
  80. $warnings = array();
  81. self::validate_context(context_system::instance());
  82. // The checksum will be checked to provide security flagging other users questions.
  83. question_flags::update_flag($params['qubaid'], $params['questionid'], $params['qaid'], $params['slot'], $params['checksum'],
  84. $params['newstate']);
  85. $result = array();
  86. $result['status'] = true;
  87. $result['warnings'] = $warnings;
  88. return $result;
  89. }
  90. /**
  91. * Returns description of method result value
  92. *
  93. * @return external_description
  94. * @since Moodle 3.1
  95. */
  96. public static function update_flag_returns() {
  97. return new external_single_structure(
  98. array(
  99. 'status' => new external_value(PARAM_BOOL, 'status: true if success'),
  100. 'warnings' => new external_warnings()
  101. )
  102. );
  103. }
  104. }