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 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_glossary_mod_form extends moodleform_mod {
   9  
  10      function definition() {
  11          global $CFG, $COURSE, $DB;
  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();
  28  
  29          if (has_capability('mod/glossary:manageentries', context_system::instance())) {
  30              $mform->addElement('checkbox', 'globalglossary', get_string('isglobal', 'glossary'));
  31              $mform->addHelpButton('globalglossary', 'isglobal', 'glossary');
  32  
  33          }else{
  34              $mform->addElement('hidden', 'globalglossary');
  35              $mform->setType('globalglossary', PARAM_INT);
  36          }
  37  
  38          $options = array(1=>get_string('mainglossary', 'glossary'), 0=>get_string('secondaryglossary', 'glossary'));
  39          $mform->addElement('select', 'mainglossary', get_string('glossarytype', 'glossary'), $options);
  40          $mform->addHelpButton('mainglossary', 'glossarytype', 'glossary');
  41          $mform->setDefault('mainglossary', 0);
  42  
  43          // ----------------------------------------------------------------------
  44          $mform->addElement('header', 'entrieshdr', get_string('entries', 'glossary'));
  45  
  46          $mform->addElement('selectyesno', 'defaultapproval', get_string('defaultapproval', 'glossary'));
  47          $mform->setDefault('defaultapproval', $CFG->glossary_defaultapproval);
  48          $mform->addHelpButton('defaultapproval', 'defaultapproval', 'glossary');
  49  
  50          $mform->addElement('selectyesno', 'editalways', get_string('editalways', 'glossary'));
  51          $mform->setDefault('editalways', 0);
  52          $mform->addHelpButton('editalways', 'editalways', 'glossary');
  53  
  54          $mform->addElement('selectyesno', 'allowduplicatedentries', get_string('allowduplicatedentries', 'glossary'));
  55          $mform->setDefault('allowduplicatedentries', $CFG->glossary_dupentries);
  56          $mform->addHelpButton('allowduplicatedentries', 'allowduplicatedentries', 'glossary');
  57  
  58          $mform->addElement('selectyesno', 'allowcomments', get_string('allowcomments', 'glossary'));
  59          $mform->setDefault('allowcomments', $CFG->glossary_allowcomments);
  60          $mform->addHelpButton('allowcomments', 'allowcomments', 'glossary');
  61  
  62          $mform->addElement('selectyesno', 'usedynalink', get_string('usedynalink', 'glossary'));
  63          $mform->setDefault('usedynalink', $CFG->glossary_linkbydefault);
  64          $mform->addHelpButton('usedynalink', 'usedynalink', 'glossary');
  65  
  66          // ----------------------------------------------------------------------
  67          $mform->addElement('header', 'appearancehdr', get_string('appearance'));
  68  
  69          // Get and update available formats.
  70          $recformats = glossary_get_available_formats();
  71          $formats = array();
  72          foreach ($recformats as $format) {
  73             $formats[$format->name] = get_string('displayformat'.$format->name, 'glossary');
  74          }
  75          asort($formats);
  76          $mform->addElement('select', 'displayformat', get_string('displayformat', 'glossary'), $formats);
  77          $mform->setDefault('displayformat', 'dictionary');
  78          $mform->addHelpButton('displayformat', 'displayformat', 'glossary');
  79  
  80          $displayformats['default'] = get_string('displayformatdefault', 'glossary');
  81          $displayformats = array_merge($displayformats, $formats);
  82          $mform->addElement('select', 'approvaldisplayformat', get_string('approvaldisplayformat', 'glossary'), $displayformats);
  83          $mform->setDefault('approvaldisplayformat', 'default');
  84          $mform->addHelpButton('approvaldisplayformat', 'approvaldisplayformat', 'glossary');
  85  
  86          $mform->addElement('text', 'entbypage', get_string('entbypage', 'glossary'));
  87          $mform->setDefault('entbypage', $this->get_default_entbypage());
  88          $mform->addRule('entbypage', null, 'numeric', null, 'client');
  89          $mform->setType('entbypage', PARAM_INT);
  90  
  91          $mform->addElement('selectyesno', 'showalphabet', get_string('showalphabet', 'glossary'));
  92          $mform->setDefault('showalphabet', 1);
  93          $mform->addHelpButton('showalphabet', 'showalphabet', 'glossary');
  94  
  95          $mform->addElement('selectyesno', 'showall', get_string('showall', 'glossary'));
  96          $mform->setDefault('showall', 1);
  97          $mform->addHelpButton('showall', 'showall', 'glossary');
  98  
  99          $mform->addElement('selectyesno', 'showspecial', get_string('showspecial', 'glossary'));
 100          $mform->setDefault('showspecial', 1);
 101          $mform->addHelpButton('showspecial', 'showspecial', 'glossary');
 102  
 103          $mform->addElement('selectyesno', 'allowprintview', get_string('allowprintview', 'glossary'));
 104          $mform->setDefault('allowprintview', 1);
 105          $mform->addHelpButton('allowprintview', 'allowprintview', 'glossary');
 106  
 107          if ($CFG->enablerssfeeds && isset($CFG->glossary_enablerssfeeds) && $CFG->glossary_enablerssfeeds) {
 108  //-------------------------------------------------------------------------------
 109              $mform->addElement('header', 'rssheader', get_string('rss'));
 110              $choices = array();
 111              $choices[0] = get_string('none');
 112              $choices[1] = get_string('withauthor', 'glossary');
 113              $choices[2] = get_string('withoutauthor', 'glossary');
 114              $mform->addElement('select', 'rsstype', get_string('rsstype', 'glossary'), $choices);
 115              $mform->addHelpButton('rsstype', 'rsstype', 'glossary');
 116  
 117              $choices = array();
 118              $choices[0] = '0';
 119              $choices[1] = '1';
 120              $choices[2] = '2';
 121              $choices[3] = '3';
 122              $choices[4] = '4';
 123              $choices[5] = '5';
 124              $choices[10] = '10';
 125              $choices[15] = '15';
 126              $choices[20] = '20';
 127              $choices[25] = '25';
 128              $choices[30] = '30';
 129              $choices[40] = '40';
 130              $choices[50] = '50';
 131              $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
 132              $mform->addHelpButton('rssarticles', 'rssarticles', 'glossary');
 133              $mform->hideIf('rssarticles', 'rsstype', 'eq', 0);
 134          }
 135  
 136  //-------------------------------------------------------------------------------
 137  
 138          $this->standard_grading_coursemodule_elements();
 139  
 140          $this->standard_coursemodule_elements();
 141  
 142  //-------------------------------------------------------------------------------
 143          // buttons
 144          $this->add_action_buttons();
 145      }
 146  
 147      function definition_after_data() {
 148          global $COURSE, $DB;
 149  
 150          parent::definition_after_data();
 151          $mform    =& $this->_form;
 152          $mainglossaryel =& $mform->getElement('mainglossary');
 153          $mainglossary = $DB->get_record('glossary', array('mainglossary'=>1, 'course'=>$COURSE->id));
 154          if ($mainglossary && ($mainglossary->id != $mform->getElementValue('instance'))){
 155              //secondary glossary, a main one already exists in this course.
 156              $mainglossaryel->setValue(0);
 157              $mainglossaryel->freeze();
 158              $mainglossaryel->setPersistantFreeze(true);
 159          } else {
 160              $mainglossaryel->unfreeze();
 161              $mainglossaryel->setPersistantFreeze(false);
 162  
 163          }
 164      }
 165  
 166      function data_preprocessing(&$default_values){
 167          parent::data_preprocessing($default_values);
 168  
 169          // Fallsback on the default setting if 'Entries shown per page' has been left blank.
 170          // This prevents the field from being required and expand its section which should not
 171          // be the case if there is a default value defined.
 172          if (empty($default_values['entbypage']) || $default_values['entbypage'] < 0) {
 173              $default_values['entbypage'] = $this->get_default_entbypage();
 174          }
 175  
 176          // Set up the completion checkboxes which aren't part of standard data.
 177          // Tick by default if Add mode or if completion entries settings is set to 1 or more.
 178          if (empty($this->_instance) || !empty($default_values['completionentries'])) {
 179              $default_values['completionentriesenabled'] = 1;
 180          } else {
 181              $default_values['completionentriesenabled'] = 0;
 182          }
 183          if (empty($default_values['completionentries'])) {
 184              $default_values['completionentries']=1;
 185          }
 186      }
 187  
 188      function add_completion_rules() {
 189          $mform =& $this->_form;
 190  
 191          $group=array();
 192          $group[] =& $mform->createElement('checkbox', 'completionentriesenabled', '', get_string('completionentries','glossary'));
 193          $group[] =& $mform->createElement('text', 'completionentries', '', array('size'=>3));
 194          $mform->setType('completionentries', PARAM_INT);
 195          $mform->addGroup($group, 'completionentriesgroup', get_string('completionentriesgroup','glossary'), array(' '), false);
 196          $mform->disabledIf('completionentries','completionentriesenabled','notchecked');
 197  
 198          return array('completionentriesgroup');
 199      }
 200  
 201      function completion_rule_enabled($data) {
 202          return (!empty($data['completionentriesenabled']) && $data['completionentries']!=0);
 203      }
 204  
 205      /**
 206       * Allows module to modify the data returned by form get_data().
 207       * This method is also called in the bulk activity completion form.
 208       *
 209       * Only available on moodleform_mod.
 210       *
 211       * @param stdClass $data the form data to be modified.
 212       */
 213      public function data_postprocessing($data) {
 214          parent::data_postprocessing($data);
 215          if (!empty($data->completionunlocked)) {
 216              // Turn off completion settings if the checkboxes aren't ticked
 217              $autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
 218              if (empty($data->completionentriesenabled) || !$autocompletion) {
 219                  $data->completionentries = 0;
 220              }
 221          }
 222      }
 223  
 224      /**
 225       * Returns the default value for 'Entries shown per page'.
 226       *
 227       * @return int default for number of entries per page.
 228       */
 229      protected function get_default_entbypage() {
 230          global $CFG;
 231          return !empty($CFG->glossary_entbypage) ? $CFG->glossary_entbypage : 10;
 232      }
 233  
 234  }
 235