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  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  
  23  require_once("$CFG->libdir/externallib.php");
  24  require_once("$CFG->dirroot/mod/assign/locallib.php");
  25  
  26  /**
  27   * Extend the base external_api class with mod_assign utility methods.
  28   *
  29   * @package    mod_assign
  30   * @author     Andrew Madden <andrewmadden@catalyst-au.net>
  31   * @copyright  2021 Catalyst IT
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class external_api extends \external_api {
  35  
  36      /**
  37       * Generate a warning in a standard structure for a known failure.
  38       *
  39       * @param int $assignmentid - The assignment
  40       * @param string $warningcode - The key for the warning message
  41       * @param string $detail - A description of the error
  42       * @return array - Warning structure containing item, itemid, warningcode, message
  43       */
  44      protected static function generate_warning(int $assignmentid, string $warningcode, string $detail): array {
  45          $warningmessages = [
  46              'couldnotlock' => 'Could not lock the submission for this user.',
  47              'couldnotunlock' => 'Could not unlock the submission for this user.',
  48              'couldnotsubmitforgrading' => 'Could not submit assignment for grading.',
  49              'couldnotrevealidentities' => 'Could not reveal identities.',
  50              'couldnotgrantextensions' => 'Could not grant submission date extensions.',
  51              'couldnotrevert' => 'Could not revert submission to draft.',
  52              'invalidparameters' => 'Invalid parameters.',
  53              'couldnotsavesubmission' => 'Could not save submission.',
  54              'couldnotsavegrade' => 'Could not save grade.',
  55              'couldnotstartsubmission' => 'Could not start submission with time limit.',
  56              'submissionnotopen' => 'This assignment is not open for submissions',
  57              'timelimitnotenabled' => 'Time limit is not enabled for assignment.',
  58              'opensubmissionexists' => 'Open assignment submission already exists.',
  59          ];
  60  
  61          $message = $warningmessages[$warningcode];
  62          if (empty($message)) {
  63              $message = 'Unknown warning type.';
  64          }
  65  
  66          return [
  67              'item' => s($detail),
  68              'itemid' => $assignmentid,
  69              'warningcode' => $warningcode,
  70              'message' => $message,
  71          ];
  72      }
  73  
  74      /**
  75       * Utility function for validating an assign.
  76       *
  77       * @param int $assignid assign instance id
  78       * @return array array containing the assign, course, context and course module objects
  79       * @since  Moodle 3.2
  80       */
  81      protected static function validate_assign(int $assignid): array {
  82          global $DB;
  83  
  84          // Request and permission validation.
  85          $assign = $DB->get_record('assign', ['id' => $assignid], 'id', MUST_EXIST);
  86          list($course, $cm) = get_course_and_cm_from_instance($assign, 'assign');
  87  
  88          $context = \context_module::instance($cm->id);
  89          // Please, note that is not required to check mod/assign:view because is done by validate_context->require_login.
  90          self::validate_context($context);
  91          $assign = new \assign($context, $cm, $course);
  92  
  93          return [$assign, $course, $cm, $context];
  94      }
  95  
  96      /**
  97       * Get a submission from an assignment for a user. Encapsulates checking whether it's a solo or team submission.
  98       *
  99       * @param \assign $assignment Assignment object.
 100       * @param int|null $userid User id.
 101       * @param int $groupid Group id.
 102       * @param bool $create Whether a new submission should be created.
 103       * @param int $attemptnumber Attempt number. Use -1 for last attempt.
 104       * @return bool|\stdClass
 105       */
 106      protected static function get_user_or_group_submission(\assign $assignment, int $userid = null,
 107              int $groupid = 0, bool $create = false, int $attemptnumber = -1) {
 108          if ($assignment->get_instance($userid)->teamsubmission) {
 109              $submission = $assignment->get_group_submission($userid, $groupid, $create, $attemptnumber);
 110          } else {
 111              $submission = $assignment->get_user_submission($userid, $create, $attemptnumber);
 112          }
 113          return $submission;
 114      }
 115  }