| 1 | 
           efrain | 
           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 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           19 | 
           global $CFG;
  | 
        
        
            | 
            | 
           20 | 
           require_once($CFG->libdir . '/gradelib.php');
  | 
        
        
            | 
            | 
           21 | 
              | 
        
        
           | 1 | 
           efrain | 
           22 | 
           /**
  | 
        
        
            | 
            | 
           23 | 
            * assign module data generator class
  | 
        
        
            | 
            | 
           24 | 
            *
  | 
        
        
            | 
            | 
           25 | 
            * @package mod_assign
  | 
        
        
            | 
            | 
           26 | 
            * @category test
  | 
        
        
            | 
            | 
           27 | 
            * @copyright 2012 Paul Charsley
  | 
        
        
            | 
            | 
           28 | 
            * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           29 | 
            */
  | 
        
        
            | 
            | 
           30 | 
           class mod_assign_generator extends testing_module_generator {
  | 
        
        
            | 
            | 
           31 | 
              | 
        
        
            | 
            | 
           32 | 
               /**
  | 
        
        
            | 
            | 
           33 | 
                * Create a new instance of the assignment activity.
  | 
        
        
            | 
            | 
           34 | 
                *
  | 
        
        
            | 
            | 
           35 | 
                * @param array|stdClass|null $record
  | 
        
        
            | 
            | 
           36 | 
                * @param array|null $options
  | 
        
        
            | 
            | 
           37 | 
                * @return stdClass
  | 
        
        
            | 
            | 
           38 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           39 | 
               public function create_instance($record = null, ?array $options = null) {
  | 
        
        
           | 1 | 
           efrain | 
           40 | 
                   $record = (object)(array)$record;
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           42 | 
                   $defaultsettings = [
  | 
        
        
           | 1 | 
           efrain | 
           43 | 
                       'alwaysshowdescription'             => 1,
  | 
        
        
            | 
            | 
           44 | 
                       'submissiondrafts'                  => 1,
  | 
        
        
            | 
            | 
           45 | 
                       'requiresubmissionstatement'        => 0,
  | 
        
        
            | 
            | 
           46 | 
                       'sendnotifications'                 => 0,
  | 
        
        
            | 
            | 
           47 | 
                       'sendstudentnotifications'          => 1,
  | 
        
        
            | 
            | 
           48 | 
                       'sendlatenotifications'             => 0,
  | 
        
        
            | 
            | 
           49 | 
                       'duedate'                           => 0,
  | 
        
        
            | 
            | 
           50 | 
                       'allowsubmissionsfromdate'          => 0,
  | 
        
        
            | 
            | 
           51 | 
                       'grade'                             => 100,
  | 
        
        
            | 
            | 
           52 | 
                       'cutoffdate'                        => 0,
  | 
        
        
            | 
            | 
           53 | 
                       'gradingduedate'                    => 0,
  | 
        
        
            | 
            | 
           54 | 
                       'teamsubmission'                    => 0,
  | 
        
        
            | 
            | 
           55 | 
                       'requireallteammemberssubmit'       => 0,
  | 
        
        
            | 
            | 
           56 | 
                       'teamsubmissiongroupingid'          => 0,
  | 
        
        
            | 
            | 
           57 | 
                       'blindmarking'                      => 0,
  | 
        
        
           | 1441 | 
           ariadna | 
           58 | 
                       'attemptreopenmethod'               => 'untilpass',
  | 
        
        
            | 
            | 
           59 | 
                       'maxattempts'                       => 1,
  | 
        
        
           | 1 | 
           efrain | 
           60 | 
                       'markingworkflow'                   => 0,
  | 
        
        
            | 
            | 
           61 | 
                       'markingallocation'                 => 0,
  | 
        
        
            | 
            | 
           62 | 
                       'markinganonymous'                  => 0,
  | 
        
        
            | 
            | 
           63 | 
                       'activityformat'                    => 0,
  | 
        
        
            | 
            | 
           64 | 
                       'timelimit'                         => 0,
  | 
        
        
            | 
            | 
           65 | 
                       'submissionattachments'             => 0,
  | 
        
        
           | 1441 | 
           ariadna | 
           66 | 
                   ];
  | 
        
        
           | 1 | 
           efrain | 
           67 | 
              | 
        
        
            | 
            | 
           68 | 
                   if (property_exists($record, 'teamsubmissiongroupingid')) {
  | 
        
        
            | 
            | 
           69 | 
                       $record->teamsubmissiongroupingid = $this->get_grouping_id($record->teamsubmissiongroupingid);
  | 
        
        
            | 
            | 
           70 | 
                   }
  | 
        
        
            | 
            | 
           71 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           72 | 
                   if (property_exists($record, 'gradetype')) {
  | 
        
        
            | 
            | 
           73 | 
                       if ((int)$record->gradetype === GRADE_TYPE_SCALE && property_exists($record, 'gradescale')) {
  | 
        
        
            | 
            | 
           74 | 
                           // Get the scale id and apply it.
  | 
        
        
            | 
            | 
           75 | 
                           $defaultsettings['grade[modgrade_type]'] = GRADE_TYPE_SCALE;
  | 
        
        
            | 
            | 
           76 | 
                           $defaultsettings['grade[modgrade_scale]'] = $record->gradescale;
  | 
        
        
            | 
            | 
           77 | 
                           $defaultsettings['grade'] = -$record->gradescale;
  | 
        
        
            | 
            | 
           78 | 
                       } else if ((int)$record->gradetype === GRADE_TYPE_NONE) {
  | 
        
        
            | 
            | 
           79 | 
                           $defaultsettings['grade[modgrade_type]'] = GRADE_TYPE_NONE;
  | 
        
        
            | 
            | 
           80 | 
                           $defaultsettings['grade'] = 0;
  | 
        
        
            | 
            | 
           81 | 
                       }
  | 
        
        
            | 
            | 
           82 | 
                   }
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
           | 1 | 
           efrain | 
           84 | 
                   foreach ($defaultsettings as $name => $value) {
  | 
        
        
            | 
            | 
           85 | 
                       if (!isset($record->{$name})) {
  | 
        
        
            | 
            | 
           86 | 
                           $record->{$name} = $value;
  | 
        
        
            | 
            | 
           87 | 
                       }
  | 
        
        
            | 
            | 
           88 | 
                   }
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
                   return parent::create_instance($record, (array)$options);
  | 
        
        
            | 
            | 
           91 | 
               }
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
               /**
  | 
        
        
            | 
            | 
           94 | 
                * Create an assignment submission.
  | 
        
        
            | 
            | 
           95 | 
                *
  | 
        
        
           | 1441 | 
           ariadna | 
           96 | 
                * @param array $data with keys userid, cmid and
  | 
        
        
            | 
            | 
           97 | 
                *      then data for each assignsubmission plugin used.
  | 
        
        
            | 
            | 
           98 | 
                *      For backwards compatibility, you can pass cmid as 'assignid' but that generates a warning.
  | 
        
        
           | 1 | 
           efrain | 
           99 | 
                */
  | 
        
        
            | 
            | 
           100 | 
               public function create_submission(array $data): void {
  | 
        
        
            | 
            | 
           101 | 
                   global $USER;
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           103 | 
                   if (array_key_exists('assignid', $data)) {
  | 
        
        
            | 
            | 
           104 | 
                       debugging(
  | 
        
        
            | 
            | 
           105 | 
                           'The cmid passed to create_submission should have array key cmid, not assignid.',
  | 
        
        
            | 
            | 
           106 | 
                           DEBUG_DEVELOPER,
  | 
        
        
            | 
            | 
           107 | 
                       );
  | 
        
        
            | 
            | 
           108 | 
                       $data['cmid'] = $data['assignid'];
  | 
        
        
            | 
            | 
           109 | 
                       unset($data['assignid']);
  | 
        
        
            | 
            | 
           110 | 
                   }
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
           | 1 | 
           efrain | 
           112 | 
                   $currentuser = $USER;
  | 
        
        
            | 
            | 
           113 | 
                   $user = \core_user::get_user($data['userid']);
  | 
        
        
            | 
            | 
           114 | 
                   $this->set_user($user);
  | 
        
        
            | 
            | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
                   $submission = (object) [
  | 
        
        
            | 
            | 
           117 | 
                       'userid' => $user->id,
  | 
        
        
            | 
            | 
           118 | 
                   ];
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           120 | 
                   [$course, $cm] = get_course_and_cm_from_cmid($data['cmid'], 'assign');
  | 
        
        
           | 1 | 
           efrain | 
           121 | 
                   $context = context_module::instance($cm->id);
  | 
        
        
            | 
            | 
           122 | 
                   $assign = new assign($context, $cm, $course);
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
                   foreach ($assign->get_submission_plugins() as $plugin) {
  | 
        
        
            | 
            | 
           125 | 
                       $pluginname = $plugin->get_type();
  | 
        
        
            | 
            | 
           126 | 
                       if (array_key_exists($pluginname, $data)) {
  | 
        
        
            | 
            | 
           127 | 
                           $plugingenerator = $this->datagenerator->get_plugin_generator("assignsubmission_{$pluginname}");
  | 
        
        
            | 
            | 
           128 | 
                           $plugingenerator->add_submission_data($submission, $assign, $data);
  | 
        
        
            | 
            | 
           129 | 
                       }
  | 
        
        
            | 
            | 
           130 | 
                   }
  | 
        
        
            | 
            | 
           131 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           132 | 
                   if (isset($data['status'])) {
  | 
        
        
            | 
            | 
           133 | 
                       $submission->status = $data['status'];
  | 
        
        
            | 
            | 
           134 | 
                   }
  | 
        
        
           | 1 | 
           efrain | 
           135 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           136 | 
                   $assign->save_submission($submission, $notices);
  | 
        
        
            | 
            | 
           137 | 
              | 
        
        
           | 1 | 
           efrain | 
           138 | 
                   $this->set_user($currentuser);
  | 
        
        
            | 
            | 
           139 | 
               }
  | 
        
        
            | 
            | 
           140 | 
              | 
        
        
            | 
            | 
           141 | 
               /**
  | 
        
        
           | 1441 | 
           ariadna | 
           142 | 
                * Create an assignment extension.
  | 
        
        
            | 
            | 
           143 | 
                *
  | 
        
        
            | 
            | 
           144 | 
                * @param array $data must have keys cmid, userid, extensionduedate.
  | 
        
        
            | 
            | 
           145 | 
                */
  | 
        
        
            | 
            | 
           146 | 
               public function create_extension(array $data): void {
  | 
        
        
            | 
            | 
           147 | 
                   $user = \core_user::get_user($data['userid'], '*', MUST_EXIST);
  | 
        
        
            | 
            | 
           148 | 
              | 
        
        
            | 
            | 
           149 | 
                   [$course, $cm] = get_course_and_cm_from_cmid($data['cmid'], 'assign');
  | 
        
        
            | 
            | 
           150 | 
                   $context = context_module::instance($cm->id);
  | 
        
        
            | 
            | 
           151 | 
                   $assign = new assign($context, $cm, $course);
  | 
        
        
            | 
            | 
           152 | 
              | 
        
        
            | 
            | 
           153 | 
                   if (!$assign->save_user_extension($user->id, $data['extensionduedate'] ?: null)) {
  | 
        
        
            | 
            | 
           154 | 
                       throw new \core\exception\coding_exception('The requested extension could not be created.');
  | 
        
        
            | 
            | 
           155 | 
                   }
  | 
        
        
            | 
            | 
           156 | 
               }
  | 
        
        
            | 
            | 
           157 | 
              | 
        
        
            | 
            | 
           158 | 
               /**
  | 
        
        
           | 1 | 
           efrain | 
           159 | 
                * Gets the grouping id from it's idnumber.
  | 
        
        
            | 
            | 
           160 | 
                *
  | 
        
        
            | 
            | 
           161 | 
                * @throws Exception
  | 
        
        
            | 
            | 
           162 | 
                * @param string $idnumber
  | 
        
        
            | 
            | 
           163 | 
                * @return int
  | 
        
        
            | 
            | 
           164 | 
                */
  | 
        
        
            | 
            | 
           165 | 
               protected function get_grouping_id(string $idnumber): int {
  | 
        
        
            | 
            | 
           166 | 
                   global $DB;
  | 
        
        
            | 
            | 
           167 | 
              | 
        
        
            | 
            | 
           168 | 
                   // Do not fetch grouping ID for empty grouping idnumber.
  | 
        
        
            | 
            | 
           169 | 
                   if (empty($idnumber)) {
  | 
        
        
           | 1441 | 
           ariadna | 
           170 | 
                       throw new \core\exception\coding_exception('idnumber cannot be empty');
  | 
        
        
           | 1 | 
           efrain | 
           171 | 
                   }
  | 
        
        
            | 
            | 
           172 | 
              | 
        
        
            | 
            | 
           173 | 
                   if (!$id = $DB->get_field('groupings', 'id', ['idnumber' => $idnumber])) {
  | 
        
        
            | 
            | 
           174 | 
                       if (is_numeric($idnumber)) {
  | 
        
        
            | 
            | 
           175 | 
                           return $idnumber;
  | 
        
        
            | 
            | 
           176 | 
                       }
  | 
        
        
            | 
            | 
           177 | 
                       throw new Exception('The specified grouping with idnumber "' . $idnumber . '" does not exist');
  | 
        
        
            | 
            | 
           178 | 
                   }
  | 
        
        
            | 
            | 
           179 | 
              | 
        
        
            | 
            | 
           180 | 
                   return $id;
  | 
        
        
            | 
            | 
           181 | 
               }
  | 
        
        
            | 
            | 
           182 | 
              | 
        
        
            | 
            | 
           183 | 
               /**
  | 
        
        
            | 
            | 
           184 | 
                * Create an assign override (either user or group).
  | 
        
        
            | 
            | 
           185 | 
                *
  | 
        
        
            | 
            | 
           186 | 
                * @param array $data must specify assignid, and one of userid or groupid.
  | 
        
        
            | 
            | 
           187 | 
                * @throws coding_exception
  | 
        
        
            | 
            | 
           188 | 
                */
  | 
        
        
            | 
            | 
           189 | 
               public function create_override(array $data): void {
  | 
        
        
            | 
            | 
           190 | 
                   global $DB;
  | 
        
        
            | 
            | 
           191 | 
              | 
        
        
            | 
            | 
           192 | 
                   if (!isset($data['assignid'])) {
  | 
        
        
            | 
            | 
           193 | 
                       throw new coding_exception('Must specify assignid when creating an assign override.');
  | 
        
        
            | 
            | 
           194 | 
                   }
  | 
        
        
            | 
            | 
           195 | 
              | 
        
        
            | 
            | 
           196 | 
                   if (!isset($data['userid']) && !isset($data['groupid'])) {
  | 
        
        
            | 
            | 
           197 | 
                       throw new coding_exception('Must specify one of userid or groupid when creating an assign override.');
  | 
        
        
            | 
            | 
           198 | 
                   }
  | 
        
        
            | 
            | 
           199 | 
              | 
        
        
            | 
            | 
           200 | 
                   if (isset($data['userid']) && isset($data['groupid'])) {
  | 
        
        
            | 
            | 
           201 | 
                       throw new coding_exception('Cannot specify both userid and groupid when creating an assign override.');
  | 
        
        
            | 
            | 
           202 | 
                   }
  | 
        
        
            | 
            | 
           203 | 
              | 
        
        
            | 
            | 
           204 | 
                   $DB->insert_record('assign_overrides', (object) $data);
  | 
        
        
            | 
            | 
           205 | 
               }
  | 
        
        
            | 
            | 
           206 | 
           }
  |