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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [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  /**
  18   * This file contains the forms to create and edit an instance of this module
  19   *
  20   * @package   mod_assign
  21   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
  26  
  27  require_once($CFG->dirroot.'/course/moodleform_mod.php');
  28  require_once($CFG->dirroot . '/mod/assign/locallib.php');
  29  
  30  /**
  31   * Assignment settings form.
  32   *
  33   * @package   mod_assign
  34   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class mod_assign_mod_form extends moodleform_mod {
  38  
  39      /**
  40       * Called to define this moodle form
  41       *
  42       * @return void
  43       */
  44      public function definition() {
  45          global $CFG, $COURSE, $DB, $PAGE;
  46          $mform = $this->_form;
  47  
  48          $mform->addElement('header', 'general', get_string('general', 'form'));
  49  
  50          $mform->addElement('text', 'name', get_string('assignmentname', 'assign'), array('size'=>'64'));
  51          if (!empty($CFG->formatstringstriptags)) {
  52              $mform->setType('name', PARAM_TEXT);
  53          } else {
  54              $mform->setType('name', PARAM_CLEANHTML);
  55          }
  56          $mform->addRule('name', null, 'required', null, 'client');
  57          $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  58  
  59          $this->standard_intro_elements(get_string('description', 'assign'));
  60  
  61          // Activity.
  62          $mform->addElement('editor', 'activityeditor',
  63               get_string('activityeditor', 'assign'), array('rows' => 10), array('maxfiles' => EDITOR_UNLIMITED_FILES,
  64              'noclean' => true, 'context' => $this->context, 'subdirs' => true));
  65          $mform->addHelpButton('activityeditor', 'activityeditor', 'assign');
  66          $mform->setType('activityeditor', PARAM_RAW);
  67  
  68          $mform->addElement('filemanager', 'introattachments',
  69                              get_string('introattachments', 'assign'),
  70                              null, array('subdirs' => 0, 'maxbytes' => $COURSE->maxbytes) );
  71          $mform->addHelpButton('introattachments', 'introattachments', 'assign');
  72  
  73          $mform->addElement('advcheckbox', 'submissionattachments', get_string('submissionattachments', 'assign'));
  74          $mform->addHelpButton('submissionattachments', 'submissionattachments', 'assign');
  75  
  76          $ctx = null;
  77          if ($this->current && $this->current->coursemodule) {
  78              $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
  79              $ctx = context_module::instance($cm->id);
  80          }
  81          $assignment = new assign($ctx, null, null);
  82          if ($this->current && $this->current->course) {
  83              if (!$ctx) {
  84                  $ctx = context_course::instance($this->current->course);
  85              }
  86              $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
  87              $assignment->set_course($course);
  88          }
  89  
  90          $mform->addElement('header', 'availability', get_string('availability', 'assign'));
  91          $mform->setExpanded('availability', true);
  92  
  93          $name = get_string('allowsubmissionsfromdate', 'assign');
  94          $options = array('optional'=>true);
  95          $mform->addElement('date_time_selector', 'allowsubmissionsfromdate', $name, $options);
  96          $mform->addHelpButton('allowsubmissionsfromdate', 'allowsubmissionsfromdate', 'assign');
  97  
  98          $name = get_string('duedate', 'assign');
  99          $mform->addElement('date_time_selector', 'duedate', $name, array('optional'=>true));
 100          $mform->addHelpButton('duedate', 'duedate', 'assign');
 101  
 102          $name = get_string('cutoffdate', 'assign');
 103          $mform->addElement('date_time_selector', 'cutoffdate', $name, array('optional'=>true));
 104          $mform->addHelpButton('cutoffdate', 'cutoffdate', 'assign');
 105  
 106          $name = get_string('gradingduedate', 'assign');
 107          $mform->addElement('date_time_selector', 'gradingduedate', $name, array('optional' => true));
 108          $mform->addHelpButton('gradingduedate', 'gradingduedate', 'assign');
 109  
 110          $timelimitenabled = get_config('assign', 'enabletimelimit');
 111          // Time limit.
 112          if ($timelimitenabled) {
 113              $mform->addElement('duration', 'timelimit', get_string('timelimit', 'assign'),
 114                  array('optional' => true));
 115              $mform->addHelpButton('timelimit', 'timelimit', 'assign');
 116          }
 117  
 118          $name = get_string('alwaysshowdescription', 'assign');
 119          $mform->addElement('checkbox', 'alwaysshowdescription', $name);
 120          $mform->addHelpButton('alwaysshowdescription', 'alwaysshowdescription', 'assign');
 121          $mform->disabledIf('alwaysshowdescription', 'allowsubmissionsfromdate[enabled]', 'notchecked');
 122  
 123          $assignment->add_all_plugin_settings($mform);
 124  
 125          $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'assign'));
 126  
 127          $name = get_string('submissiondrafts', 'assign');
 128          $mform->addElement('selectyesno', 'submissiondrafts', $name);
 129          $mform->addHelpButton('submissiondrafts', 'submissiondrafts', 'assign');
 130          if ($assignment->has_submissions_or_grades()) {
 131              $mform->freeze('submissiondrafts');
 132          }
 133  
 134          $name = get_string('requiresubmissionstatement', 'assign');
 135          $mform->addElement('selectyesno', 'requiresubmissionstatement', $name);
 136          $mform->addHelpButton('requiresubmissionstatement',
 137                                'requiresubmissionstatement',
 138                                'assign');
 139          $mform->setType('requiresubmissionstatement', PARAM_BOOL);
 140  
 141          $options = array(
 142              ASSIGN_ATTEMPT_REOPEN_METHOD_NONE => get_string('attemptreopenmethod_none', 'mod_assign'),
 143              ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL => get_string('attemptreopenmethod_manual', 'mod_assign'),
 144              ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS => get_string('attemptreopenmethod_untilpass', 'mod_assign')
 145          );
 146          $mform->addElement('select', 'attemptreopenmethod', get_string('attemptreopenmethod', 'mod_assign'), $options);
 147          $mform->addHelpButton('attemptreopenmethod', 'attemptreopenmethod', 'mod_assign');
 148  
 149          $options = array(ASSIGN_UNLIMITED_ATTEMPTS => get_string('unlimitedattempts', 'mod_assign'));
 150          $options += array_combine(range(1, 30), range(1, 30));
 151          $mform->addElement('select', 'maxattempts', get_string('maxattempts', 'mod_assign'), $options);
 152          $mform->addHelpButton('maxattempts', 'maxattempts', 'assign');
 153          $mform->hideIf('maxattempts', 'attemptreopenmethod', 'eq', ASSIGN_ATTEMPT_REOPEN_METHOD_NONE);
 154  
 155          $mform->addElement('header', 'groupsubmissionsettings', get_string('groupsubmissionsettings', 'assign'));
 156  
 157          $name = get_string('teamsubmission', 'assign');
 158          $mform->addElement('selectyesno', 'teamsubmission', $name);
 159          $mform->addHelpButton('teamsubmission', 'teamsubmission', 'assign');
 160          if ($assignment->has_submissions_or_grades()) {
 161              $mform->freeze('teamsubmission');
 162          }
 163  
 164          $name = get_string('preventsubmissionnotingroup', 'assign');
 165          $mform->addElement('selectyesno', 'preventsubmissionnotingroup', $name);
 166          $mform->addHelpButton('preventsubmissionnotingroup',
 167              'preventsubmissionnotingroup',
 168              'assign');
 169          $mform->setType('preventsubmissionnotingroup', PARAM_BOOL);
 170          $mform->hideIf('preventsubmissionnotingroup', 'teamsubmission', 'eq', 0);
 171  
 172          $name = get_string('requireallteammemberssubmit', 'assign');
 173          $mform->addElement('selectyesno', 'requireallteammemberssubmit', $name);
 174          $mform->addHelpButton('requireallteammemberssubmit', 'requireallteammemberssubmit', 'assign');
 175          $mform->hideIf('requireallteammemberssubmit', 'teamsubmission', 'eq', 0);
 176          $mform->disabledIf('requireallteammemberssubmit', 'submissiondrafts', 'eq', 0);
 177  
 178          $groupings = groups_get_all_groupings($assignment->get_course()->id);
 179          $options = array();
 180          $options[0] = get_string('none');
 181          foreach ($groupings as $grouping) {
 182              $options[$grouping->id] = $grouping->name;
 183          }
 184  
 185          $name = get_string('teamsubmissiongroupingid', 'assign');
 186          $mform->addElement('select', 'teamsubmissiongroupingid', $name, $options);
 187          $mform->addHelpButton('teamsubmissiongroupingid', 'teamsubmissiongroupingid', 'assign');
 188          $mform->hideIf('teamsubmissiongroupingid', 'teamsubmission', 'eq', 0);
 189          if ($assignment->has_submissions_or_grades()) {
 190              $mform->freeze('teamsubmissiongroupingid');
 191          }
 192  
 193          $mform->addElement('header', 'notifications', get_string('notifications', 'assign'));
 194  
 195          $name = get_string('sendnotifications', 'assign');
 196          $mform->addElement('selectyesno', 'sendnotifications', $name);
 197          $mform->addHelpButton('sendnotifications', 'sendnotifications', 'assign');
 198  
 199          $name = get_string('sendlatenotifications', 'assign');
 200          $mform->addElement('selectyesno', 'sendlatenotifications', $name);
 201          $mform->addHelpButton('sendlatenotifications', 'sendlatenotifications', 'assign');
 202          $mform->disabledIf('sendlatenotifications', 'sendnotifications', 'eq', 1);
 203  
 204          $name = get_string('sendstudentnotificationsdefault', 'assign');
 205          $mform->addElement('selectyesno', 'sendstudentnotifications', $name);
 206          $mform->addHelpButton('sendstudentnotifications', 'sendstudentnotificationsdefault', 'assign');
 207  
 208          // Plagiarism enabling form. To be removed (deprecated) with MDL-67526.
 209          if (!empty($CFG->enableplagiarism)) {
 210              require_once($CFG->libdir . '/plagiarismlib.php');
 211              plagiarism_get_form_elements_module($mform, $ctx->get_course_context(), 'mod_assign');
 212          }
 213  
 214          $this->standard_grading_coursemodule_elements();
 215          $name = get_string('blindmarking', 'assign');
 216          $mform->addElement('selectyesno', 'blindmarking', $name);
 217          $mform->addHelpButton('blindmarking', 'blindmarking', 'assign');
 218          if ($assignment->has_submissions_or_grades() ) {
 219              $mform->freeze('blindmarking');
 220          }
 221  
 222          $name = get_string('hidegrader', 'assign');
 223          $mform->addElement('selectyesno', 'hidegrader', $name);
 224          $mform->addHelpButton('hidegrader', 'hidegrader', 'assign');
 225  
 226          $name = get_string('markingworkflow', 'assign');
 227          $mform->addElement('selectyesno', 'markingworkflow', $name);
 228          $mform->addHelpButton('markingworkflow', 'markingworkflow', 'assign');
 229  
 230          $name = get_string('markingallocation', 'assign');
 231          $mform->addElement('selectyesno', 'markingallocation', $name);
 232          $mform->addHelpButton('markingallocation', 'markingallocation', 'assign');
 233          $mform->hideIf('markingallocation', 'markingworkflow', 'eq', 0);
 234  
 235          $this->standard_coursemodule_elements();
 236          $this->apply_admin_defaults();
 237  
 238          $this->add_action_buttons();
 239      }
 240  
 241      /**
 242       * Perform minimal validation on the settings form
 243       * @param array $data
 244       * @param array $files
 245       */
 246      public function validation($data, $files) {
 247          $errors = parent::validation($data, $files);
 248  
 249          if (!empty($data['allowsubmissionsfromdate']) && !empty($data['duedate'])) {
 250              if ($data['duedate'] <= $data['allowsubmissionsfromdate']) {
 251                  $errors['duedate'] = get_string('duedateaftersubmissionvalidation', 'assign');
 252              }
 253          }
 254          if (!empty($data['cutoffdate']) && !empty($data['duedate'])) {
 255              if ($data['cutoffdate'] < $data['duedate'] ) {
 256                  $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'assign');
 257              }
 258          }
 259          if (!empty($data['allowsubmissionsfromdate']) && !empty($data['cutoffdate'])) {
 260              if ($data['cutoffdate'] < $data['allowsubmissionsfromdate']) {
 261                  $errors['cutoffdate'] = get_string('cutoffdatefromdatevalidation', 'assign');
 262              }
 263          }
 264          if ($data['gradingduedate']) {
 265              if ($data['allowsubmissionsfromdate'] && $data['allowsubmissionsfromdate'] > $data['gradingduedate']) {
 266                  $errors['gradingduedate'] = get_string('gradingduefromdatevalidation', 'assign');
 267              }
 268              if ($data['duedate'] && $data['duedate'] > $data['gradingduedate']) {
 269                  $errors['gradingduedate'] = get_string('gradingdueduedatevalidation', 'assign');
 270              }
 271          }
 272          if ($data['blindmarking'] && $data['attemptreopenmethod'] == ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS) {
 273              $errors['attemptreopenmethod'] = get_string('reopenuntilpassincompatiblewithblindmarking', 'assign');
 274          }
 275  
 276          return $errors;
 277      }
 278  
 279      /**
 280       * Any data processing needed before the form is displayed
 281       * (needed to set up draft areas for editor and filemanager elements)
 282       * @param array $defaultvalues
 283       */
 284      public function data_preprocessing(&$defaultvalues) {
 285          global $DB;
 286  
 287          $ctx = null;
 288          if ($this->current && $this->current->coursemodule) {
 289              $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
 290              $ctx = context_module::instance($cm->id);
 291          }
 292          $assignment = new assign($ctx, null, null);
 293          if ($this->current && $this->current->course) {
 294              if (!$ctx) {
 295                  $ctx = context_course::instance($this->current->course);
 296              }
 297              $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
 298              $assignment->set_course($course);
 299          }
 300  
 301          $draftitemid = file_get_submitted_draft_itemid('introattachments');
 302          file_prepare_draft_area($draftitemid, $ctx->id, 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA,
 303                                  0, array('subdirs' => 0));
 304          $defaultvalues['introattachments'] = $draftitemid;
 305  
 306          // Activity editor fields.
 307          $activitydraftitemid = file_get_submitted_draft_itemid('activityeditor');
 308          if (!empty($defaultvalues['activity'])) {
 309              $defaultvalues['activityeditor'] = array(
 310                  'text' => file_prepare_draft_area($activitydraftitemid, $ctx->id, 'mod_assign', ASSIGN_ACTIVITYATTACHMENT_FILEAREA,
 311                      0, array('subdirs' => 0), $defaultvalues['activity']),
 312                  'format' => $defaultvalues['activityformat'],
 313                  'itemid' => $activitydraftitemid
 314              );
 315          }
 316  
 317          $assignment->plugin_data_preprocessing($defaultvalues);
 318      }
 319  
 320      /**
 321       * Add any custom completion rules to the form.
 322       *
 323       * @return array Contains the names of the added form elements
 324       */
 325      public function add_completion_rules() {
 326          $mform =& $this->_form;
 327  
 328          $mform->addElement('advcheckbox', 'completionsubmit', '', get_string('completionsubmit', 'assign'));
 329          // Enable this completion rule by default.
 330          $mform->setDefault('completionsubmit', 1);
 331          return array('completionsubmit');
 332      }
 333  
 334      /**
 335       * Determines if completion is enabled for this module.
 336       *
 337       * @param array $data
 338       * @return bool
 339       */
 340      public function completion_rule_enabled($data) {
 341          return !empty($data['completionsubmit']);
 342      }
 343  
 344  }