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] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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          $this->standard_grading_coursemodule_elements();
 209          $name = get_string('blindmarking', 'assign');
 210          $mform->addElement('selectyesno', 'blindmarking', $name);
 211          $mform->addHelpButton('blindmarking', 'blindmarking', 'assign');
 212          if ($assignment->has_submissions_or_grades() ) {
 213              $mform->freeze('blindmarking');
 214          }
 215  
 216          $name = get_string('hidegrader', 'assign');
 217          $mform->addElement('selectyesno', 'hidegrader', $name);
 218          $mform->addHelpButton('hidegrader', 'hidegrader', 'assign');
 219  
 220          $name = get_string('markingworkflow', 'assign');
 221          $mform->addElement('selectyesno', 'markingworkflow', $name);
 222          $mform->addHelpButton('markingworkflow', 'markingworkflow', 'assign');
 223  
 224          $name = get_string('markingallocation', 'assign');
 225          $mform->addElement('selectyesno', 'markingallocation', $name);
 226          $mform->addHelpButton('markingallocation', 'markingallocation', 'assign');
 227          $mform->hideIf('markingallocation', 'markingworkflow', 'eq', 0);
 228  
 229          $this->standard_coursemodule_elements();
 230          $this->apply_admin_defaults();
 231  
 232          $this->add_action_buttons();
 233      }
 234  
 235      /**
 236       * Perform minimal validation on the settings form
 237       * @param array $data
 238       * @param array $files
 239       */
 240      public function validation($data, $files) {
 241          $errors = parent::validation($data, $files);
 242  
 243          if (!empty($data['allowsubmissionsfromdate']) && !empty($data['duedate'])) {
 244              if ($data['duedate'] <= $data['allowsubmissionsfromdate']) {
 245                  $errors['duedate'] = get_string('duedateaftersubmissionvalidation', 'assign');
 246              }
 247          }
 248          if (!empty($data['cutoffdate']) && !empty($data['duedate'])) {
 249              if ($data['cutoffdate'] < $data['duedate'] ) {
 250                  $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'assign');
 251              }
 252          }
 253          if (!empty($data['allowsubmissionsfromdate']) && !empty($data['cutoffdate'])) {
 254              if ($data['cutoffdate'] < $data['allowsubmissionsfromdate']) {
 255                  $errors['cutoffdate'] = get_string('cutoffdatefromdatevalidation', 'assign');
 256              }
 257          }
 258          if ($data['gradingduedate']) {
 259              if ($data['allowsubmissionsfromdate'] && $data['allowsubmissionsfromdate'] > $data['gradingduedate']) {
 260                  $errors['gradingduedate'] = get_string('gradingduefromdatevalidation', 'assign');
 261              }
 262              if ($data['duedate'] && $data['duedate'] > $data['gradingduedate']) {
 263                  $errors['gradingduedate'] = get_string('gradingdueduedatevalidation', 'assign');
 264              }
 265          }
 266          if ($data['blindmarking'] && $data['attemptreopenmethod'] == ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS) {
 267              $errors['attemptreopenmethod'] = get_string('reopenuntilpassincompatiblewithblindmarking', 'assign');
 268          }
 269  
 270          return $errors;
 271      }
 272  
 273      /**
 274       * Any data processing needed before the form is displayed
 275       * (needed to set up draft areas for editor and filemanager elements)
 276       * @param array $defaultvalues
 277       */
 278      public function data_preprocessing(&$defaultvalues) {
 279          global $DB;
 280  
 281          $ctx = null;
 282          if ($this->current && $this->current->coursemodule) {
 283              $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
 284              $ctx = context_module::instance($cm->id);
 285          }
 286          $assignment = new assign($ctx, null, null);
 287          if ($this->current && $this->current->course) {
 288              if (!$ctx) {
 289                  $ctx = context_course::instance($this->current->course);
 290              }
 291              $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
 292              $assignment->set_course($course);
 293          }
 294  
 295          $draftitemid = file_get_submitted_draft_itemid('introattachments');
 296          file_prepare_draft_area($draftitemid, $ctx->id, 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA,
 297                                  0, array('subdirs' => 0));
 298          $defaultvalues['introattachments'] = $draftitemid;
 299  
 300          // Activity editor fields.
 301          $activitydraftitemid = file_get_submitted_draft_itemid('activityeditor');
 302          if (!empty($defaultvalues['activity'])) {
 303              $defaultvalues['activityeditor'] = array(
 304                  'text' => file_prepare_draft_area($activitydraftitemid, $ctx->id, 'mod_assign', ASSIGN_ACTIVITYATTACHMENT_FILEAREA,
 305                      0, array('subdirs' => 0), $defaultvalues['activity']),
 306                  'format' => $defaultvalues['activityformat'],
 307                  'itemid' => $activitydraftitemid
 308              );
 309          }
 310  
 311          $assignment->plugin_data_preprocessing($defaultvalues);
 312      }
 313  
 314      /**
 315       * Add any custom completion rules to the form.
 316       *
 317       * @return array Contains the names of the added form elements
 318       */
 319      public function add_completion_rules() {
 320          $mform =& $this->_form;
 321  
 322          $suffix = $this->get_suffix();
 323          $completionsubmitel = 'completionsubmit' . $suffix;
 324          $mform->addElement('advcheckbox', $completionsubmitel, '', get_string('completionsubmit', 'assign'));
 325          // Enable this completion rule by default.
 326          $mform->setDefault($completionsubmitel, 1);
 327  
 328          return [$completionsubmitel];
 329      }
 330  
 331      /**
 332       * Determines if completion is enabled for this module.
 333       *
 334       * @param array $data
 335       * @return bool
 336       */
 337      public function completion_rule_enabled($data) {
 338          $suffix = $this->get_suffix();
 339          return !empty($data['completionsubmit' . $suffix]);
 340      }
 341  
 342  }