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 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  /**
  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          $mform->addElement('filemanager', 'introattachments',
  62                              get_string('introattachments', 'assign'),
  63                              null, array('subdirs' => 0, 'maxbytes' => $COURSE->maxbytes) );
  64          $mform->addHelpButton('introattachments', 'introattachments', 'assign');
  65  
  66          $ctx = null;
  67          if ($this->current && $this->current->coursemodule) {
  68              $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
  69              $ctx = context_module::instance($cm->id);
  70          }
  71          $assignment = new assign($ctx, null, null);
  72          if ($this->current && $this->current->course) {
  73              if (!$ctx) {
  74                  $ctx = context_course::instance($this->current->course);
  75              }
  76              $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
  77              $assignment->set_course($course);
  78          }
  79  
  80          $config = get_config('assign');
  81  
  82          $mform->addElement('header', 'availability', get_string('availability', 'assign'));
  83          $mform->setExpanded('availability', true);
  84  
  85          $name = get_string('allowsubmissionsfromdate', 'assign');
  86          $options = array('optional'=>true);
  87          $mform->addElement('date_time_selector', 'allowsubmissionsfromdate', $name, $options);
  88          $mform->addHelpButton('allowsubmissionsfromdate', 'allowsubmissionsfromdate', 'assign');
  89  
  90          $name = get_string('duedate', 'assign');
  91          $mform->addElement('date_time_selector', 'duedate', $name, array('optional'=>true));
  92          $mform->addHelpButton('duedate', 'duedate', 'assign');
  93  
  94          $name = get_string('cutoffdate', 'assign');
  95          $mform->addElement('date_time_selector', 'cutoffdate', $name, array('optional'=>true));
  96          $mform->addHelpButton('cutoffdate', 'cutoffdate', 'assign');
  97  
  98          $name = get_string('gradingduedate', 'assign');
  99          $mform->addElement('date_time_selector', 'gradingduedate', $name, array('optional' => true));
 100          $mform->addHelpButton('gradingduedate', 'gradingduedate', 'assign');
 101  
 102          $name = get_string('alwaysshowdescription', 'assign');
 103          $mform->addElement('checkbox', 'alwaysshowdescription', $name);
 104          $mform->addHelpButton('alwaysshowdescription', 'alwaysshowdescription', 'assign');
 105          $mform->disabledIf('alwaysshowdescription', 'allowsubmissionsfromdate[enabled]', 'notchecked');
 106  
 107          $assignment->add_all_plugin_settings($mform);
 108  
 109          $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'assign'));
 110  
 111          $name = get_string('submissiondrafts', 'assign');
 112          $mform->addElement('selectyesno', 'submissiondrafts', $name);
 113          $mform->addHelpButton('submissiondrafts', 'submissiondrafts', 'assign');
 114  
 115          $name = get_string('requiresubmissionstatement', 'assign');
 116          $mform->addElement('selectyesno', 'requiresubmissionstatement', $name);
 117          $mform->addHelpButton('requiresubmissionstatement',
 118                                'requiresubmissionstatement',
 119                                'assign');
 120          $mform->setType('requiresubmissionstatement', PARAM_BOOL);
 121  
 122          $options = array(
 123              ASSIGN_ATTEMPT_REOPEN_METHOD_NONE => get_string('attemptreopenmethod_none', 'mod_assign'),
 124              ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL => get_string('attemptreopenmethod_manual', 'mod_assign'),
 125              ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS => get_string('attemptreopenmethod_untilpass', 'mod_assign')
 126          );
 127          $mform->addElement('select', 'attemptreopenmethod', get_string('attemptreopenmethod', 'mod_assign'), $options);
 128          $mform->addHelpButton('attemptreopenmethod', 'attemptreopenmethod', 'mod_assign');
 129  
 130          $options = array(ASSIGN_UNLIMITED_ATTEMPTS => get_string('unlimitedattempts', 'mod_assign'));
 131          $options += array_combine(range(1, 30), range(1, 30));
 132          $mform->addElement('select', 'maxattempts', get_string('maxattempts', 'mod_assign'), $options);
 133          $mform->addHelpButton('maxattempts', 'maxattempts', 'assign');
 134          $mform->hideIf('maxattempts', 'attemptreopenmethod', 'eq', ASSIGN_ATTEMPT_REOPEN_METHOD_NONE);
 135  
 136          $mform->addElement('header', 'groupsubmissionsettings', get_string('groupsubmissionsettings', 'assign'));
 137  
 138          $name = get_string('teamsubmission', 'assign');
 139          $mform->addElement('selectyesno', 'teamsubmission', $name);
 140          $mform->addHelpButton('teamsubmission', 'teamsubmission', 'assign');
 141          if ($assignment->has_submissions_or_grades()) {
 142              $mform->freeze('teamsubmission');
 143          }
 144  
 145          $name = get_string('preventsubmissionnotingroup', 'assign');
 146          $mform->addElement('selectyesno', 'preventsubmissionnotingroup', $name);
 147          $mform->addHelpButton('preventsubmissionnotingroup',
 148              'preventsubmissionnotingroup',
 149              'assign');
 150          $mform->setType('preventsubmissionnotingroup', PARAM_BOOL);
 151          $mform->hideIf('preventsubmissionnotingroup', 'teamsubmission', 'eq', 0);
 152  
 153          $name = get_string('requireallteammemberssubmit', 'assign');
 154          $mform->addElement('selectyesno', 'requireallteammemberssubmit', $name);
 155          $mform->addHelpButton('requireallteammemberssubmit', 'requireallteammemberssubmit', 'assign');
 156          $mform->hideIf('requireallteammemberssubmit', 'teamsubmission', 'eq', 0);
 157          $mform->disabledIf('requireallteammemberssubmit', 'submissiondrafts', 'eq', 0);
 158  
 159          $groupings = groups_get_all_groupings($assignment->get_course()->id);
 160          $options = array();
 161          $options[0] = get_string('none');
 162          foreach ($groupings as $grouping) {
 163              $options[$grouping->id] = $grouping->name;
 164          }
 165  
 166          $name = get_string('teamsubmissiongroupingid', 'assign');
 167          $mform->addElement('select', 'teamsubmissiongroupingid', $name, $options);
 168          $mform->addHelpButton('teamsubmissiongroupingid', 'teamsubmissiongroupingid', 'assign');
 169          $mform->hideIf('teamsubmissiongroupingid', 'teamsubmission', 'eq', 0);
 170          if ($assignment->has_submissions_or_grades()) {
 171              $mform->freeze('teamsubmissiongroupingid');
 172          }
 173  
 174          $mform->addElement('header', 'notifications', get_string('notifications', 'assign'));
 175  
 176          $name = get_string('sendnotifications', 'assign');
 177          $mform->addElement('selectyesno', 'sendnotifications', $name);
 178          $mform->addHelpButton('sendnotifications', 'sendnotifications', 'assign');
 179  
 180          $name = get_string('sendlatenotifications', 'assign');
 181          $mform->addElement('selectyesno', 'sendlatenotifications', $name);
 182          $mform->addHelpButton('sendlatenotifications', 'sendlatenotifications', 'assign');
 183          $mform->disabledIf('sendlatenotifications', 'sendnotifications', 'eq', 1);
 184  
 185          $name = get_string('sendstudentnotificationsdefault', 'assign');
 186          $mform->addElement('selectyesno', 'sendstudentnotifications', $name);
 187          $mform->addHelpButton('sendstudentnotifications', 'sendstudentnotificationsdefault', 'assign');
 188  
 189          // Plagiarism enabling form. To be removed (deprecated) with MDL-67526.
 190          if (!empty($CFG->enableplagiarism)) {
 191              require_once($CFG->libdir . '/plagiarismlib.php');
 192              plagiarism_get_form_elements_module($mform, $ctx->get_course_context(), 'mod_assign');
 193          }
 194  
 195          $this->standard_grading_coursemodule_elements();
 196          $name = get_string('blindmarking', 'assign');
 197          $mform->addElement('selectyesno', 'blindmarking', $name);
 198          $mform->addHelpButton('blindmarking', 'blindmarking', 'assign');
 199          if ($assignment->has_submissions_or_grades() ) {
 200              $mform->freeze('blindmarking');
 201          }
 202  
 203          $name = get_string('hidegrader', 'assign');
 204          $mform->addElement('selectyesno', 'hidegrader', $name);
 205          $mform->addHelpButton('hidegrader', 'hidegrader', 'assign');
 206  
 207          $name = get_string('markingworkflow', 'assign');
 208          $mform->addElement('selectyesno', 'markingworkflow', $name);
 209          $mform->addHelpButton('markingworkflow', 'markingworkflow', 'assign');
 210  
 211          $name = get_string('markingallocation', 'assign');
 212          $mform->addElement('selectyesno', 'markingallocation', $name);
 213          $mform->addHelpButton('markingallocation', 'markingallocation', 'assign');
 214          $mform->hideIf('markingallocation', 'markingworkflow', 'eq', 0);
 215  
 216          $this->standard_coursemodule_elements();
 217          $this->apply_admin_defaults();
 218  
 219          $this->add_action_buttons();
 220      }
 221  
 222      /**
 223       * Perform minimal validation on the settings form
 224       * @param array $data
 225       * @param array $files
 226       */
 227      public function validation($data, $files) {
 228          $errors = parent::validation($data, $files);
 229  
 230          if (!empty($data['allowsubmissionsfromdate']) && !empty($data['duedate'])) {
 231              if ($data['duedate'] < $data['allowsubmissionsfromdate']) {
 232                  $errors['duedate'] = get_string('duedatevalidation', 'assign');
 233              }
 234          }
 235          if (!empty($data['cutoffdate']) && !empty($data['duedate'])) {
 236              if ($data['cutoffdate'] < $data['duedate'] ) {
 237                  $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'assign');
 238              }
 239          }
 240          if (!empty($data['allowsubmissionsfromdate']) && !empty($data['cutoffdate'])) {
 241              if ($data['cutoffdate'] < $data['allowsubmissionsfromdate']) {
 242                  $errors['cutoffdate'] = get_string('cutoffdatefromdatevalidation', 'assign');
 243              }
 244          }
 245          if ($data['gradingduedate']) {
 246              if ($data['allowsubmissionsfromdate'] && $data['allowsubmissionsfromdate'] > $data['gradingduedate']) {
 247                  $errors['gradingduedate'] = get_string('gradingduefromdatevalidation', 'assign');
 248              }
 249              if ($data['duedate'] && $data['duedate'] > $data['gradingduedate']) {
 250                  $errors['gradingduedate'] = get_string('gradingdueduedatevalidation', 'assign');
 251              }
 252          }
 253          if ($data['blindmarking'] && $data['attemptreopenmethod'] == ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS) {
 254              $errors['attemptreopenmethod'] = get_string('reopenuntilpassincompatiblewithblindmarking', 'assign');
 255          }
 256  
 257          return $errors;
 258      }
 259  
 260      /**
 261       * Any data processing needed before the form is displayed
 262       * (needed to set up draft areas for editor and filemanager elements)
 263       * @param array $defaultvalues
 264       */
 265      public function data_preprocessing(&$defaultvalues) {
 266          global $DB;
 267  
 268          $ctx = null;
 269          if ($this->current && $this->current->coursemodule) {
 270              $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
 271              $ctx = context_module::instance($cm->id);
 272          }
 273          $assignment = new assign($ctx, null, null);
 274          if ($this->current && $this->current->course) {
 275              if (!$ctx) {
 276                  $ctx = context_course::instance($this->current->course);
 277              }
 278              $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
 279              $assignment->set_course($course);
 280          }
 281  
 282          $draftitemid = file_get_submitted_draft_itemid('introattachments');
 283          file_prepare_draft_area($draftitemid, $ctx->id, 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA,
 284                                  0, array('subdirs' => 0));
 285          $defaultvalues['introattachments'] = $draftitemid;
 286  
 287          $assignment->plugin_data_preprocessing($defaultvalues);
 288      }
 289  
 290      /**
 291       * Add any custom completion rules to the form.
 292       *
 293       * @return array Contains the names of the added form elements
 294       */
 295      public function add_completion_rules() {
 296          $mform =& $this->_form;
 297  
 298          $mform->addElement('advcheckbox', 'completionsubmit', '', get_string('completionsubmit', 'assign'));
 299          // Enable this completion rule by default.
 300          $mform->setDefault('completionsubmit', 1);
 301          return array('completionsubmit');
 302      }
 303  
 304      /**
 305       * Determines if completion is enabled for this module.
 306       *
 307       * @param array $data
 308       * @return bool
 309       */
 310      public function completion_rule_enabled($data) {
 311          return !empty($data['completionsubmit']);
 312      }
 313  
 314  }