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.

Differences Between: [Versions 400 and 402] [Versions 401 and 402]

   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_assign\external;
  18  
  19  use core_external\external_function_parameters;
  20  use core_external\external_single_structure;
  21  use core_external\external_value;
  22  use core_external\external_warnings;
  23  
  24  /**
  25   * External function to notify Moodle that an assignment submission is starting.
  26   *
  27   * @package    mod_assign
  28   * @author     Andrew Madden <andrewmadden@catalyst-au.net>
  29   * @copyright  2021 Catalyst IT
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class start_submission extends external_api {
  33  
  34      /**
  35       * Describes the parameters for submission_start.
  36       *
  37       * @return external_function_parameters
  38       * @since Moodle 4.0
  39       */
  40      public static function execute_parameters(): external_function_parameters {
  41          return new external_function_parameters ([
  42                  'assignid' => new external_value(PARAM_INT, 'Assignment instance id'),
  43              ]
  44          );
  45      }
  46  
  47      /**
  48       * Call to start an assignment submission.
  49       *
  50       * @param int $assignid Assignment ID.
  51       * @return array
  52       * @since Moodle 4.0
  53       */
  54      public static function execute(int $assignid): array {
  55          global $DB, $USER;
  56  
  57          $result = $warnings = [];
  58          $submission = null;
  59  
  60          [
  61              'assignid' => $assignid,
  62          ] = self::validate_parameters(self::execute_parameters(), [
  63              'assignid' => $assignid,
  64          ]);
  65  
  66          list($assignment, $course, $cm, $context) = self::validate_assign($assignid);
  67  
  68          $assignment->update_effective_access($USER->id);
  69          $latestsubmission = external_api::get_user_or_group_submission($assignment, $USER->id);
  70          if (!$assignment->submissions_open($USER->id)) {
  71              $warnings[] = self::generate_warning($assignid,
  72                  'submissionnotopen',
  73                  get_string('submissionnotopen', 'assign'));
  74          }
  75  
  76          if (!$assignment->is_time_limit_enabled()) {
  77              $warnings[] = self::generate_warning($assignid,
  78                  'timelimitnotenabled',
  79                  get_string('timelimitnotenabled', 'assign'));
  80          } else if ($assignment->is_attempt_in_progress()) {
  81              $warnings[] = self::generate_warning($assignid,
  82                  'opensubmissionexists',
  83                  get_string('opensubmissionexists', 'assign'));
  84          }
  85  
  86          if (empty($warnings)) {
  87              // If there is an open submission with no start time, use latest submission, otherwise create a new submission.
  88              if (!empty($latestsubmission)
  89                      && $latestsubmission->status !== ASSIGN_SUBMISSION_STATUS_SUBMITTED
  90                      && empty($latestsubmission->timestarted)) {
  91                  $submission = $latestsubmission;
  92              } else {
  93                  $submission = external_api::get_user_or_group_submission($assignment, $USER->id, 0, true);
  94              }
  95  
  96              // Set the start time of the submission.
  97              $submission->timestarted = time();
  98              $DB->update_record('assign_submission', $submission);
  99          }
 100  
 101          $result['submissionid'] = $submission ? $submission->id : 0;
 102          $result['warnings'] = $warnings;
 103          return $result;
 104      }
 105  
 106      /**
 107       * Describes the submission_start return value.
 108       *
 109       * @return external_single_structure
 110       * @since Moodle 4.0
 111       */
 112      public static function execute_returns(): external_single_structure {
 113          return new external_single_structure([
 114              'submissionid' => new external_value(PARAM_INT, 'New submission ID.'),
 115              'warnings' => new external_warnings(),
 116          ]);
 117      }
 118  }