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.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 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  defined('MOODLE_INTERNAL') || die();
  18  
  19  /**
  20   * assign module data generator class
  21   *
  22   * @package mod_assign
  23   * @category test
  24   * @copyright 2012 Paul Charsley
  25   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class mod_assign_generator extends testing_module_generator {
  28  
  29      /**
  30       * Create a new instance of the assignment activity.
  31       *
  32       * @param array|stdClass|null $record
  33       * @param array|null $options
  34       * @return stdClass
  35       */
  36      public function create_instance($record = null, array $options = null) {
  37          $record = (object)(array)$record;
  38  
  39          $defaultsettings = array(
  40              'alwaysshowdescription'             => 1,
  41              'submissiondrafts'                  => 1,
  42              'requiresubmissionstatement'        => 0,
  43              'sendnotifications'                 => 0,
  44              'sendstudentnotifications'          => 1,
  45              'sendlatenotifications'             => 0,
  46              'duedate'                           => 0,
  47              'allowsubmissionsfromdate'          => 0,
  48              'grade'                             => 100,
  49              'cutoffdate'                        => 0,
  50              'gradingduedate'                    => 0,
  51              'teamsubmission'                    => 0,
  52              'requireallteammemberssubmit'       => 0,
  53              'teamsubmissiongroupingid'          => 0,
  54              'blindmarking'                      => 0,
  55              'attemptreopenmethod'               => 'none',
  56              'maxattempts'                       => -1,
  57              'markingworkflow'                   => 0,
  58              'markingallocation'                 => 0,
  59              'activityformat'                    => 0,
  60              'timelimit'                         => 0,
  61              'submissionattachments'             => 0,
  62          );
  63  
  64          if (property_exists($record, 'teamsubmissiongroupingid')) {
  65              $record->teamsubmissiongroupingid = $this->get_grouping_id($record->teamsubmissiongroupingid);
  66          }
  67  
  68          foreach ($defaultsettings as $name => $value) {
  69              if (!isset($record->{$name})) {
  70                  $record->{$name} = $value;
  71              }
  72          }
  73  
  74          return parent::create_instance($record, (array)$options);
  75      }
  76  
  77      /**
  78       * Create an assignment submission.
  79       *
  80       * @param array $data
  81       */
  82      public function create_submission(array $data): void {
  83          global $USER;
  84  
  85          $currentuser = $USER;
  86          $user = \core_user::get_user($data['userid']);
  87          $this->set_user($user);
  88  
  89          $submission = (object) [
  90              'userid' => $user->id,
  91          ];
  92  
  93          [$course, $cm] = get_course_and_cm_from_cmid($data['assignid'], 'assign');
  94          $context = context_module::instance($cm->id);
  95          $assign = new assign($context, $cm, $course);
  96  
  97          foreach ($assign->get_submission_plugins() as $plugin) {
  98              $pluginname = $plugin->get_type();
  99              if (array_key_exists($pluginname, $data)) {
 100                  $plugingenerator = $this->datagenerator->get_plugin_generator("assignsubmission_{$pluginname}");
 101                  $plugingenerator->add_submission_data($submission, $assign, $data);
 102              }
 103          }
 104  
 105          $assign->save_submission((object) $submission, $notices);
 106  
 107          $this->set_user($currentuser);
 108      }
 109  
 110      /**
 111       * Gets the grouping id from it's idnumber.
 112       *
 113       * @throws Exception
 114       * @param string $idnumber
 115       * @return int
 116       */
 117      protected function get_grouping_id(string $idnumber): int {
 118          global $DB;
 119  
 120          // Do not fetch grouping ID for empty grouping idnumber.
 121          if (empty($idnumber)) {
 122              return null;
 123          }
 124  
 125          if (!$id = $DB->get_field('groupings', 'id', ['idnumber' => $idnumber])) {
 126              if (is_numeric($idnumber)) {
 127                  return $idnumber;
 128              }
 129              throw new Exception('The specified grouping with idnumber "' . $idnumber . '" does not exist');
 130          }
 131  
 132          return $id;
 133      }
 134  
 135      /**
 136       * Create an assign override (either user or group).
 137       *
 138       * @param array $data must specify assignid, and one of userid or groupid.
 139       * @throws coding_exception
 140       */
 141      public function create_override(array $data): void {
 142          global $DB;
 143  
 144          if (!isset($data['assignid'])) {
 145              throw new coding_exception('Must specify assignid when creating an assign override.');
 146          }
 147  
 148          if (!isset($data['userid']) && !isset($data['groupid'])) {
 149              throw new coding_exception('Must specify one of userid or groupid when creating an assign override.');
 150          }
 151  
 152          if (isset($data['userid']) && isset($data['groupid'])) {
 153              throw new coding_exception('Cannot specify both userid and groupid when creating an assign override.');
 154          }
 155  
 156          $DB->insert_record('assign_overrides', (object) $data);
 157      }
 158  }