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 403]

   1  <?php
   2  if (!defined('MOODLE_INTERNAL')) {
   3      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
   4  }
   5  
   6  require_once ($CFG->dirroot.'/course/moodleform_mod.php');
   7  
   8  class mod_data_mod_form extends moodleform_mod {
   9  
  10      function definition() {
  11          global $CFG, $DB, $OUTPUT;
  12  
  13          $mform =& $this->_form;
  14  
  15          //-------------------------------------------------------------------------------
  16          $mform->addElement('header', 'general', get_string('general', 'form'));
  17  
  18          $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
  19          if (!empty($CFG->formatstringstriptags)) {
  20              $mform->setType('name', PARAM_TEXT);
  21          } else {
  22              $mform->setType('name', PARAM_CLEANHTML);
  23          }
  24          $mform->addRule('name', null, 'required', null, 'client');
  25          $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  26  
  27          $this->standard_intro_elements(get_string('intro', 'data'));
  28  
  29          // ----------------------------------------------------------------------
  30          $mform->addElement('header', 'entrieshdr', get_string('entries', 'data'));
  31  
  32          $mform->addElement('selectyesno', 'approval', get_string('requireapproval', 'data'));
  33          $mform->addHelpButton('approval', 'requireapproval', 'data');
  34  
  35          $mform->addElement('selectyesno', 'manageapproved', get_string('manageapproved', 'data'));
  36          $mform->addHelpButton('manageapproved', 'manageapproved', 'data');
  37          $mform->setDefault('manageapproved', 1);
  38          $mform->hideIf('manageapproved', 'approval', 'eq', 0);
  39  
  40          $mform->addElement('selectyesno', 'comments', get_string('allowcomments', 'data'));
  41          if (empty($CFG->usecomments)) {
  42              $mform->hardFreeze('comments');
  43              $mform->setConstant('comments', 0);
  44          }
  45  
  46          $countoptions = array(0=>get_string('none'))+
  47                          (array_combine(range(1, DATA_MAX_ENTRIES), // Keys.
  48                                          range(1, DATA_MAX_ENTRIES))); // Values.
  49          /*only show fields if there are legacy values from
  50           *before completionentries was added*/
  51          if (!empty($this->current->requiredentries)) {
  52              $group = array();
  53              $group[] = $mform->createElement('select', 'requiredentries',
  54                      get_string('requiredentries', 'data'), $countoptions);
  55              $mform->addGroup($group, 'requiredentriesgroup', get_string('requiredentries', 'data'), array(''), false);
  56              $mform->addHelpButton('requiredentriesgroup', 'requiredentries', 'data');
  57              $mform->addElement('html', $OUTPUT->notification( get_string('requiredentrieswarning', 'data')));
  58          }
  59  
  60          $mform->addElement('select', 'requiredentriestoview', get_string('requiredentriestoview', 'data'), $countoptions);
  61          $mform->addHelpButton('requiredentriestoview', 'requiredentriestoview', 'data');
  62  
  63          $mform->addElement('select', 'maxentries', get_string('maxentries', 'data'), $countoptions);
  64          $mform->addHelpButton('maxentries', 'maxentries', 'data');
  65  
  66          // ----------------------------------------------------------------------
  67          $mform->addElement('header', 'availibilityhdr', get_string('availability'));
  68  
  69          $mform->addElement('date_time_selector', 'timeavailablefrom', get_string('availablefromdate', 'data'),
  70                             array('optional' => true));
  71  
  72          $mform->addElement('date_time_selector', 'timeavailableto', get_string('availabletodate', 'data'),
  73                             array('optional' => true));
  74  
  75          $mform->addElement('date_time_selector', 'timeviewfrom', get_string('viewfromdate', 'data'),
  76                             array('optional' => true));
  77  
  78          $mform->addElement('date_time_selector', 'timeviewto', get_string('viewtodate', 'data'),
  79                             array('optional' => true));
  80  
  81          // ----------------------------------------------------------------------
  82          if ($CFG->enablerssfeeds && $CFG->data_enablerssfeeds) {
  83              $mform->addElement('header', 'rsshdr', get_string('rss'));
  84              $mform->addElement('select', 'rssarticles', get_string('numberrssarticles', 'data') , $countoptions);
  85          }
  86  
  87          $this->standard_grading_coursemodule_elements();
  88  
  89          $this->standard_coursemodule_elements();
  90  
  91  //-------------------------------------------------------------------------------
  92          // buttons
  93          $this->add_action_buttons();
  94      }
  95  
  96      /**
  97       * Enforce validation rules here
  98       *
  99       * @param array $data array of ("fieldname"=>value) of submitted data
 100       * @param array $files array of uploaded files "element_name"=>tmp_file_path
 101       * @return array
 102       **/
 103      public function validation($data, $files) {
 104          $errors = parent::validation($data, $files);
 105  
 106          // Check open and close times are consistent.
 107          if ($data['timeavailablefrom'] && $data['timeavailableto'] &&
 108                  $data['timeavailableto'] < $data['timeavailablefrom']) {
 109              $errors['timeavailableto'] = get_string('availabletodatevalidation', 'data');
 110          }
 111          if ($data['timeviewfrom'] && $data['timeviewto'] &&
 112                  $data['timeviewto'] < $data['timeviewfrom']) {
 113              $errors['timeviewto'] = get_string('viewtodatevalidation', 'data');
 114          }
 115  
 116          return $errors;
 117      }
 118  
 119      /**
 120       * Display module-specific activity completion rules.
 121       * Part of the API defined by moodleform_mod
 122       * @return array Array of string IDs of added items, empty array if none
 123       */
 124      public function add_completion_rules() {
 125          $mform = & $this->_form;
 126          $group = array();
 127          $group[] = $mform->createElement('checkbox', 'completionentriesenabled', '',
 128                  get_string('completionentriescount', 'data'));
 129          $group[] = $mform->createElement('text', 'completionentries',
 130                  get_string('completionentriescount', 'data'), array('size' => '1'));
 131  
 132          $mform->addGroup($group, 'completionentriesgroup', get_string('completionentries', 'data'),
 133                  array(' '), false);
 134          $mform->disabledIf('completionentries', 'completionentriesenabled', 'notchecked');
 135          $mform->setDefault('completionentries', 1);
 136          $mform->setType('completionentries', PARAM_INT);
 137          /* This ensures the elements are disabled unless completion rules are enabled */
 138          return array('completionentriesgroup');
 139      }
 140  
 141      /**
 142       * Called during validation. Indicates if a module-specific completion rule is selected.
 143       *
 144       * @param array $data
 145       * @return bool True if one or more rules is enabled, false if none are.
 146       */
 147      public function completion_rule_enabled($data) {
 148          return (!empty($data['completionentriesenabled']) && $data['completionentries'] != 0);
 149      }
 150  
 151        /**
 152         * Set up the completion checkbox which is not part of standard data.
 153         *
 154         * @param array $defaultvalues
 155         *
 156         */
 157      public function data_preprocessing(&$defaultvalues) {
 158          parent::data_preprocessing($defaultvalues);
 159          $defaultvalues['completionentriesenabled'] = !empty($defaultvalues['completionentries']) ? 1 : 0;
 160          if (empty($defaultvalues['completionentries'])) {
 161              $defaultvalues['completionentries'] = 1;
 162          }
 163      }
 164  
 165      /**
 166       * Allows modules to modify the data returned by form get_data().
 167       * This method is also called in the bulk activity completion form.
 168       *
 169       * Only available on moodleform_mod.
 170       *
 171       * @param stdClass $data the form data to be modified.
 172       */
 173      public function data_postprocessing($data) {
 174          parent::data_postprocessing($data);
 175          if (!empty($data->completionunlocked)) {
 176              $autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
 177              if (empty($data->completionentriesenabled) || !$autocompletion) {
 178                  $data->completionentries = 0;
 179              }
 180          }
 181      }
 182  
 183  }