Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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  namespace mod_quiz\external;
  18  
  19  use core_external\external_api;
  20  use core_external\external_description;
  21  use core_external\external_function_parameters;
  22  use core_external\external_value;
  23  use mod_quiz\quiz_attempt;
  24  use moodle_exception;
  25  
  26  /**
  27   * Web service method for re-opening a quiz attempt.
  28   *
  29   * The use must have the 'mod/quiz:reopenattempts' capability and the attempt
  30   * must (at least for now) be in the 'Never submitted' state (quiz_attempt::ABANDONED).
  31   *
  32   * @package    mod_quiz
  33   * @copyright  2023 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class reopen_attempt extends external_api {
  37  
  38      /**
  39       * Declare the method parameters.
  40       *
  41       * @return external_function_parameters
  42       */
  43      public static function execute_parameters(): external_function_parameters {
  44          return new external_function_parameters([
  45              'attemptid' => new external_value(PARAM_INT, 'The id of the attempt to reopen'),
  46          ]);
  47      }
  48  
  49      /**
  50       * Re-opening a submitted attempt method implementation.
  51       *
  52       * @param int $attemptid the id of the attempt to reopen.
  53       */
  54      public static function execute(int $attemptid): void {
  55          ['attemptid' => $attemptid] = self::validate_parameters(
  56                  self::execute_parameters(), ['attemptid' => $attemptid]);
  57  
  58          // Check the request is valid.
  59          $attemptobj = quiz_attempt::create($attemptid);
  60          require_capability('mod/quiz:reopenattempts', $attemptobj->get_context());
  61          self::validate_context($attemptobj->get_context());
  62          if ($attemptobj->get_state() != quiz_attempt::ABANDONED) {
  63              throw new moodle_exception('reopenattemptwrongstate', 'quiz', '',
  64                      ['attemptid' => $attemptid, 'state' => quiz_attempt_state_name($attemptobj->get_state())]);
  65          }
  66  
  67          // Re-open the attempt.
  68          $attemptobj->process_reopen_abandoned(time());
  69      }
  70  
  71      /**
  72       * Define the webservice response.
  73       *
  74       * @return external_description|null always null.
  75       */
  76      public static function execute_returns(): ?external_description {
  77          return null;
  78      }
  79  }