Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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          );
  60  
  61          if (property_exists($record, 'teamsubmissiongroupingid')) {
  62              $record->teamsubmissiongroupingid = $this->get_grouping_id($record->teamsubmissiongroupingid);
  63          }
  64  
  65          foreach ($defaultsettings as $name => $value) {
  66              if (!isset($record->{$name})) {
  67                  $record->{$name} = $value;
  68              }
  69          }
  70  
  71          return parent::create_instance($record, (array)$options);
  72      }
  73  
  74      /**
  75       * Create an assignment submission.
  76       *
  77       * @param array $data
  78       */
  79      public function create_submission(array $data): void {
  80          global $USER;
  81  
  82          $currentuser = $USER;
  83          $user = \core_user::get_user($data['userid']);
  84          $this->set_user($user);
  85  
  86          $submission = (object) [
  87              'userid' => $user->id,
  88          ];
  89  
  90          [$course, $cm] = get_course_and_cm_from_cmid($data['assignid'], 'assign');
  91          $context = context_module::instance($cm->id);
  92          $assign = new assign($context, $cm, $course);
  93  
  94          foreach ($assign->get_submission_plugins() as $plugin) {
  95              $pluginname = $plugin->get_type();
  96              if (array_key_exists($pluginname, $data)) {
  97                  $plugingenerator = $this->datagenerator->get_plugin_generator("assignsubmission_{$pluginname}");
  98                  $plugingenerator->add_submission_data($submission, $assign, $data);
  99              }
 100          }
 101  
 102          $assign->save_submission((object) $submission, $notices);
 103  
 104          $this->set_user($currentuser);
 105      }
 106  
 107      /**
 108       * Gets the grouping id from it's idnumber.
 109       *
 110       * @throws Exception
 111       * @param string $idnumber
 112       * @return int
 113       */
 114      protected function get_grouping_id(string $idnumber): int {
 115          global $DB;
 116  
 117          // Do not fetch grouping ID for empty grouping idnumber.
 118          if (empty($idnumber)) {
 119              return null;
 120          }
 121  
 122          if (!$id = $DB->get_field('groupings', 'id', ['idnumber' => $idnumber])) {
 123              if (is_numeric($idnumber)) {
 124                  return $idnumber;
 125              }
 126              throw new Exception('The specified grouping with idnumber "' . $idnumber . '" does not exist');
 127          }
 128  
 129          return $id;
 130      }
 131  }