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]

   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.'/lib/formslib.php');
   7  
   8  class mod_glossary_entry_form extends moodleform {
   9  
  10      function definition() {
  11          global $CFG, $DB;
  12  
  13          $mform = $this->_form;
  14  
  15          $currententry      = $this->_customdata['current'];
  16          $glossary          = $this->_customdata['glossary'];
  17          $cm                = $this->_customdata['cm'];
  18          $definitionoptions = $this->_customdata['definitionoptions'];
  19          $attachmentoptions = $this->_customdata['attachmentoptions'];
  20  
  21          $context  = context_module::instance($cm->id);
  22          // Prepare format_string/text options
  23          $fmtoptions = array(
  24              'context' => $context);
  25  
  26  //-------------------------------------------------------------------------------
  27          $mform->addElement('header', 'general', get_string('general', 'form'));
  28  
  29          $mform->addElement('text', 'concept', get_string('concept', 'glossary'));
  30          $mform->setType('concept', PARAM_TEXT);
  31          $mform->addRule('concept', null, 'required', null, 'client');
  32  
  33          $mform->addElement('editor', 'definition_editor', get_string('definition', 'glossary'), null, $definitionoptions);
  34          $mform->setType('definition_editor', PARAM_RAW);
  35          $mform->addRule('definition_editor', get_string('required'), 'required', null, 'client');
  36  
  37          if ($categories = $DB->get_records_menu('glossary_categories', array('glossaryid'=>$glossary->id), 'name ASC', 'id, name')){
  38              foreach ($categories as $id => $name) {
  39                  $categories[$id] = format_string($name, true, $fmtoptions);
  40              }
  41              $categories = array(0 => get_string('notcategorised', 'glossary')) + $categories;
  42              $categoriesEl = $mform->addElement('select', 'categories', get_string('categories', 'glossary'), $categories);
  43              $categoriesEl->setMultiple(true);
  44              $categoriesEl->setSize(5);
  45          }
  46  
  47          $mform->addElement('textarea', 'aliases', get_string('aliases', 'glossary'), 'rows="2" cols="40"');
  48          $mform->setType('aliases', PARAM_TEXT);
  49          $mform->addHelpButton('aliases', 'aliases', 'glossary');
  50  
  51          $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'glossary'), null, $attachmentoptions);
  52          $mform->addHelpButton('attachment_filemanager', 'attachment', 'glossary');
  53  
  54          if (!$glossary->usedynalink) {
  55              $mform->addElement('hidden', 'usedynalink',   $CFG->glossary_linkentries);
  56              $mform->setType('usedynalink', PARAM_INT);
  57              $mform->addElement('hidden', 'casesensitive', $CFG->glossary_casesensitive);
  58              $mform->setType('casesensitive', PARAM_INT);
  59              $mform->addElement('hidden', 'fullmatch',     $CFG->glossary_fullmatch);
  60              $mform->setType('fullmatch', PARAM_INT);
  61  
  62          } else {
  63  //-------------------------------------------------------------------------------
  64              $mform->addElement('header', 'linkinghdr', get_string('linking', 'glossary'));
  65  
  66              $mform->addElement('checkbox', 'usedynalink', get_string('entryusedynalink', 'glossary'));
  67              $mform->addHelpButton('usedynalink', 'entryusedynalink', 'glossary');
  68              $mform->setDefault('usedynalink', $CFG->glossary_linkentries);
  69  
  70              $mform->addElement('checkbox', 'casesensitive', get_string('casesensitive', 'glossary'));
  71              $mform->addHelpButton('casesensitive', 'casesensitive', 'glossary');
  72              $mform->hideIf('casesensitive', 'usedynalink');
  73              $mform->setDefault('casesensitive', $CFG->glossary_casesensitive);
  74  
  75              $mform->addElement('checkbox', 'fullmatch', get_string('fullmatch', 'glossary'));
  76              $mform->addHelpButton('fullmatch', 'fullmatch', 'glossary');
  77              $mform->hideIf('fullmatch', 'usedynalink');
  78              $mform->setDefault('fullmatch', $CFG->glossary_fullmatch);
  79          }
  80  
  81          if (core_tag_tag::is_enabled('mod_glossary', 'glossary_entries')) {
  82              $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
  83  
  84              $mform->addElement('tags', 'tags', get_string('tags'),
  85                  array('itemtype' => 'glossary_entries', 'component' => 'mod_glossary'));
  86          }
  87  
  88          $mform->addElement('hidden', 'id');
  89          $mform->setType('id', PARAM_INT);
  90          $mform->addElement('hidden', 'cmid');
  91          $mform->setType('cmid', PARAM_INT);
  92  
  93  //-------------------------------------------------------------------------------
  94          $this->add_action_buttons();
  95  
  96  //-------------------------------------------------------------------------------
  97          $this->set_data($currententry);
  98      }
  99  
 100      function validation($data, $files) {
 101          global $CFG, $USER, $DB;
 102          $errors = parent::validation($data, $files);
 103  
 104          $glossary = $this->_customdata['glossary'];
 105          $cm       = $this->_customdata['cm'];
 106          $context  = context_module::instance($cm->id);
 107  
 108          $id = (int)$data['id'];
 109          $data['concept'] = trim($data['concept']);
 110          $aliases = explode("\r\n", trim($data['aliases']));
 111          foreach ($aliases as $alias) {
 112              // Check if the alias is just a single character and that it doesn't match reserved symbols.
 113              if (strlen(trim($alias)) == 1 && preg_match('/[$-\/:-?{-~!"^_`\[\]]/', trim($alias))) {
 114                  $errors['aliases'] = get_string('errreservedkeywords', 'glossary');
 115              }
 116          }
 117  
 118          if ($id) {
 119              //We are updating an entry, so we compare current session user with
 120              //existing entry user to avoid some potential problems if secureforms=off
 121              //Perhaps too much security? Anyway thanks to skodak (Bug 1823)
 122              $old = $DB->get_record('glossary_entries', array('id'=>$id));
 123              $ineditperiod = ((time() - $old->timecreated <  $CFG->maxeditingtime) || $glossary->editalways);
 124              if ((!$ineditperiod || $USER->id != $old->userid) and !has_capability('mod/glossary:manageentries', $context)) {
 125                  if ($USER->id != $old->userid) {
 126                      $errors['concept'] = get_string('errcannoteditothers', 'glossary');
 127                  } elseif (!$ineditperiod) {
 128                      $errors['concept'] = get_string('erredittimeexpired', 'glossary');
 129                  }
 130              }
 131              if (!$glossary->allowduplicatedentries) {
 132                  if ($DB->record_exists_select('glossary_entries',
 133                          'glossaryid = :glossaryid AND LOWER(concept) = :concept AND id != :id', array(
 134                              'glossaryid' => $glossary->id,
 135                              'concept'    => core_text::strtolower($data['concept']),
 136                              'id'         => $id))) {
 137                      $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
 138                  }
 139              }
 140  
 141          } else {
 142              if (!$glossary->allowduplicatedentries) {
 143                  if (glossary_concept_exists($glossary, $data['concept'])) {
 144                      $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
 145                  }
 146              }
 147          }
 148  
 149          return $errors;
 150      }
 151  }
 152