Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

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