Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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 Exception;
  24  use html_writer;
  25  use mod_quiz\quiz_attempt;
  26  use moodle_exception;
  27  
  28  /**
  29   * Web service to check a quiz attempt state, and return a confirmation message if it can be reopened now.
  30   *
  31   * The use must have the 'mod/quiz:reopenattempts' capability and the attempt
  32   * must (at least for now) be in the 'Never submitted' state (quiz_attempt::ABANDONED).
  33   *
  34   * @package    mod_quiz
  35   * @copyright  2023 The Open University
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class get_reopen_attempt_confirmation extends external_api {
  39  
  40      /**
  41       * Declare the method parameters.
  42       *
  43       * @return external_function_parameters
  44       */
  45      public static function execute_parameters(): external_function_parameters {
  46          return new external_function_parameters([
  47              'attemptid' => new external_value(PARAM_INT, 'The id of the attempt to reopen'),
  48          ]);
  49      }
  50  
  51      /**
  52       * Check a quiz attempt state, and return a confirmation message method implementation.
  53       *
  54       * @param int $attemptid the id of the attempt to reopen.
  55       * @return string a suitable confirmation message (HTML), if the attempt is suitable to be reopened.
  56       * @throws Exception an appropriate exception if the attempt cannot be reopened now.
  57       */
  58      public static function execute(int $attemptid): string {
  59          global $DB;
  60          ['attemptid' => $attemptid] = self::validate_parameters(
  61                  self::execute_parameters(), ['attemptid' => $attemptid]);
  62  
  63          // Check the request is valid.
  64          $attemptobj = quiz_attempt::create($attemptid);
  65          require_capability('mod/quiz:reopenattempts', $attemptobj->get_context());
  66          self::validate_context($attemptobj->get_context());
  67          if ($attemptobj->get_state() != quiz_attempt::ABANDONED) {
  68              throw new moodle_exception('reopenattemptwrongstate', 'quiz', '',
  69                      ['attemptid' => $attemptid, 'state' => quiz_attempt_state_name($attemptobj->get_state())]);
  70          }
  71  
  72          // Work out what the affect or re-opening will be.
  73          $timestamp = time();
  74          $timeclose = $attemptobj->get_access_manager(time())->get_end_time($attemptobj->get_attempt());
  75          if ($timeclose && $timestamp > $timeclose) {
  76              $expectedoutcome = get_string('reopenedattemptwillbesubmitted', 'quiz');
  77          } else if ($timeclose) {
  78              $expectedoutcome = get_string('reopenedattemptwillbeinprogressuntil', 'quiz', userdate($timeclose));
  79          } else {
  80              $expectedoutcome = get_string('reopenedattemptwillbeinprogress', 'quiz');
  81          }
  82  
  83          // Return the required message.
  84          $user = $DB->get_record('user', ['id' => $attemptobj->get_userid()], '*', MUST_EXIST);
  85          return html_writer::tag('p', get_string('reopenattemptareyousuremessage', 'quiz',
  86                  ['attemptnumber' => $attemptobj->get_attempt_number(), 'attemptuser' => s(fullname($user))])) .
  87                  html_writer::tag('p', $expectedoutcome);
  88      }
  89  
  90      /**
  91       * Define the webservice response.
  92       *
  93       * @return external_description
  94       */
  95      public static function execute_returns(): external_description {
  96          return new external_value(PARAM_RAW, 'Confirmation to show the user before the attempt is reopened.');
  97      }
  98  }