MDL-20353 adding missing data types of hidden form fields
[moodle.git] / mod / glossary / edit_form.php
1 <?php // $Id$
2 require_once ($CFG->dirroot.'/lib/formslib.php');
4 class mod_glossary_entry_form extends moodleform {
6     function definition() {
7         global $CFG, $DB;
9         $mform = $this->_form;
11         $currententry      = $this->_customdata['current'];
12         $glossary          = $this->_customdata['glossary'];
13         $cm                = $this->_customdata['cm'];
14         $definitionoptions = $this->_customdata['definitionoptions'];
15         $attachmentoptions = $this->_customdata['attachmentoptions'];
17 //-------------------------------------------------------------------------------
18         $mform->addElement('header', 'general', get_string('general', 'form'));
20         $mform->addElement('text', 'concept', get_string('concept', 'glossary'));
21         $mform->setType('concept', PARAM_TEXT);
22         $mform->addRule('concept', null, 'required', null, 'client');
24         $mform->addElement('editor', 'definition_editor', get_string('definition', 'glossary'), null, $definitionoptions);
25         $mform->setType('definition_editor', PARAM_RAW);
26         $mform->addRule('definition_editor', get_string('required'), 'required', null, 'client');
28         if ($categories = $DB->get_records_menu('glossary_categories', array('glossaryid'=>$glossary->id), 'name ASC', 'id, name')){
29             $categories = array(0 => get_string('notcategorised', 'glossary')) + $categories;
30             $categoriesEl = $mform->addElement('select', 'categories', get_string('categories', 'glossary'), $categories);
31             $categoriesEl->setMultiple(true);
32             $categoriesEl->setSize(5);
33         }
35         $mform->addElement('textarea', 'aliases', get_string('aliases', 'glossary'), 'rows="2" cols="40"');
36         $mform->setType('aliases', PARAM_TEXT);
37         $mform->setHelpButton('aliases', array('aliases2', strip_tags(get_string('aliases', 'glossary')), 'glossary'));
39         $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'glossary'), null, $attachmentoptions);
40         $mform->setHelpButton('attachment_filemanager', array('attachment2', get_string('attachment', 'glossary'), 'glossary'));
42         if (!$glossary->usedynalink) {
43             $mform->addElement('hidden', 'usedynalink',   $CFG->glossary_linkentries);
44             $mform->setType('usedynalink', PARAM_INT);
45             $mform->addElement('hidden', 'casesensitive', $CFG->glossary_casesensitive);
46             $mform->setType('casesensitive', PARAM_INT);
47             $mform->addElement('hidden', 'fullmatch',     $CFG->glossary_fullmatch);
48             $mform->setType('fullmatch', PARAM_INT);
50         } else {
51 //-------------------------------------------------------------------------------
52             $mform->addElement('header', 'linkinghdr', get_string('linking', 'glossary'));
54             $mform->addElement('checkbox', 'usedynalink', get_string('entryusedynalink', 'glossary'));
55             $mform->setHelpButton('usedynalink', array('usedynalinkentry', strip_tags(get_string('usedynalink', 'glossary')), 'glossary'));
56             $mform->setDefault('usedynalink', $CFG->glossary_linkentries);
58             $mform->addElement('checkbox', 'casesensitive', get_string('casesensitive', 'glossary'));
59             $mform->setHelpButton('casesensitive', array('casesensitive', strip_tags(get_string('casesensitive', 'glossary')), 'glossary'));
60             $mform->disabledIf('casesensitive', 'usedynalink');
61             $mform->setDefault('casesensitive', $CFG->glossary_casesensitive);
63             $mform->addElement('checkbox', 'fullmatch', get_string('fullmatch', 'glossary'));
64             $mform->setHelpButton('fullmatch', array('fullmatch', strip_tags(get_string('fullmatch', 'glossary')), 'glossary'));
65             $mform->disabledIf('fullmatch', 'usedynalink');
66             $mform->setDefault('fullmatch', $CFG->glossary_fullmatch);
67         }
69         $mform->addElement('hidden', 'id');
70         $mform->setType('id', PARAM_INT);
71         $mform->addElement('hidden', 'cmid');
72         $mform->setType('cmid', PARAM_INT);
74 //-------------------------------------------------------------------------------
75         $this->add_action_buttons();
77 //-------------------------------------------------------------------------------
78         $this->set_data($currententry);
79     }
81     function validation($data, $files) {
82         global $CFG, $USER, $DB;
83         $errors = parent::validation($data, $files);
85         $glossary = $this->_customdata['glossary'];
86         $cm       = $this->_customdata['cm'];
87         $context  = get_context_instance(CONTEXT_MODULE, $cm->id);
89         $id = (int)$data['id'];
90         $data['concept'] = trim($data['concept']);
92         if ($id) {
93             //We are updating an entry, so we compare current session user with
94             //existing entry user to avoid some potential problems if secureforms=off
95             //Perhaps too much security? Anyway thanks to skodak (Bug 1823)
96             $old = $DB->get_record('glossary_entries', array('id'=>$id));
97             $ineditperiod = ((time() - $old->timecreated <  $CFG->maxeditingtime) || $glossary->editalways);
98             if ((!$ineditperiod || $USER->id != $old->userid) and !has_capability('mod/glossary:manageentries', $context)) {
99                 if ($USER->id != $old->userid) {
100                     $errors['concept'] = get_string('errcannoteditothers', 'glossary');
101                 } elseif (!$ineditperiod) {
102                     $errors['concept'] = get_string('erredittimeexpired', 'glossary');
103                 }
104             }
105             if (!$glossary->allowduplicatedentries) {
106                 if ($dupentries = $DB->get_records('glossary_entries', array('LOWER(concept)'=>moodle_strtolower($data['concept'])))) {
107                     foreach ($dupentries as $curentry) {
108                         if ($glossary->id == $curentry->glossaryid) {
109                            if ($curentry->id != $id) {
110                                $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
111                                break;
112                            }
113                         }
114                     }
115                 }
116             }
118         } else {
119             if (!$glossary->allowduplicatedentries) {
120                 if ($dupentries = $DB->get_record('glossary_entries', array('LOWER(concept)'=>moodle_strtolower($data['concept']), 'glossaryid'=>$glossary->id))) {
121                     $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
122                 }
123             }
124         }
126         return $errors;
127     }
129 ?>