MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / glossary / edit_form.php
blob17c3892f3be2d0015d84b29ec4dba7e388d75987
1 <?php // $Id$
2 require_once ($CFG->dirroot.'/lib/formslib.php');
4 class mod_glossary_entry_form extends moodleform {
6 function definition() {
8 global $CFG, $COURSE;
9 $mform =& $this->_form;
11 $glossary =& $this->_customdata['glossary'];
12 $mode =& $this->_customdata['mode'];
13 $cm =& $this->_customdata['cm'];
14 $hook =& $this->_customdata['hook'];
15 $e =& $this->_customdata['e'];
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('htmleditor', 'definition', get_string('definition', 'glossary'), array('rows'=>20));
25 $mform->setType('definition', PARAM_RAW);
26 $mform->addRule('definition', null, 'required', null, 'client');
27 $mform->setHelpButton('definition', array('writing', 'richtext'), false, 'editorhelpbutton');
29 $mform->addElement('format');
31 $categories = array();
32 if ($categories = get_records_menu('glossary_categories', 'glossaryid', $glossary->id, 'name ASC', 'id, name')){
33 $categories = array(0 => get_string('notcategorised', 'glossary')) + $categories;
34 } else {
35 $categories = array(0 => get_string('notcategorised', 'glossary'));
38 $categoriesEl =& $mform->addElement('select', 'categories', get_string('categories', 'glossary'), $categories);
39 $categoriesEl->setMultiple(true);
40 $categoriesEl->setSize(5);
42 $mform->addElement('textarea', 'aliases', get_string('aliases', 'glossary'), 'rows="2" cols="40"');
43 $mform->setType('aliases', PARAM_TEXT);
44 $mform->setHelpButton('aliases', array('aliases2', strip_tags(get_string('aliases', 'glossary')), 'glossary'));
46 $this->set_upload_manager(new upload_manager('attachment', true, false, $COURSE, false, 0, true, true, false));
47 $mform->addElement('file', 'attachment', get_string('attachment', 'forum'));
48 $mform->setHelpButton('attachment', array('attachment', get_string('attachment', 'glossary'), 'glossary'));
50 if (isset($CFG->glossary_linkentries)) {
51 $usedynalink = $CFG->glossary_linkentries;
52 } else {
53 $usedynalink = 0;
55 if (isset($CFG->glossary_casesensitive)) {
56 $casesensitive = $CFG->glossary_casesensitive;
57 } else {
58 $casesensitive = 0;
60 if (isset($CFG->glossary_fullmatch)) {
61 $fullmatch = $CFG->glossary_fullmatch;
62 } else {
63 $fullmatch = 0;
65 if ( !$glossary->usedynalink ) {
66 $mform->addElement('hidden', 'usedynalink', $usedynalink);
67 $mform->addElement('hidden', 'casesensitive', $casesensitive);
68 $mform->addElement('hidden', 'fullmatch', $fullmatch);
69 } else {
70 //-------------------------------------------------------------------------------
71 $mform->addElement('header', 'linkinghdr', get_string('linking', 'glossary'));
73 $mform->addElement('checkbox', 'usedynalink', get_string('entryusedynalink', 'glossary'));
74 $mform->setHelpButton('usedynalink', array('usedynalinkentry', strip_tags(get_string('usedynalink', 'glossary')), 'glossary'));
75 $mform->setDefault('usedynalink', $usedynalink);
77 $mform->addElement('checkbox', 'casesensitive', get_string('casesensitive', 'glossary'));
78 $mform->setHelpButton('casesensitive', array('casesensitive', strip_tags(get_string('casesensitive', 'glossary')), 'glossary'));
79 $mform->disabledIf('casesensitive', 'usedynalink');
80 $mform->setDefault('casesensitive', $casesensitive);
82 $mform->addElement('checkbox', 'fullmatch', get_string('fullmatch', 'glossary'));
83 $mform->setHelpButton('fullmatch', array('fullmatch', strip_tags(get_string('fullmatch', 'glossary')), 'glossary'));
84 $mform->disabledIf('fullmatch', 'usedynalink');
85 $mform->setDefault('fullmatch', $fullmatch);
88 $mform->addElement('hidden', 'e', $e);
89 $mform->addElement('hidden', 'id', $cm->id);
90 $mform->addElement('hidden', 'mode', $mode);
91 $mform->addElement('hidden', 'hook', $hook);
93 //-------------------------------------------------------------------------------
94 $this->add_action_buttons();
97 function validation($data){
98 global $CFG, $USER;
99 $errors = array();
100 $e = $this->_customdata['e'];
101 $glossary = $this->_customdata['glossary'];
102 $context = $this->_customdata['context'];
103 $data['concept'] = trim($data['concept']);
104 if ($e) {
105 //We are updating an entry, so we compare current session user with
106 //existing entry user to avoid some potential problems if secureforms=off
107 //Perhaps too much security? Anyway thanks to skodak (Bug 1823)
108 $old = get_record('glossary_entries', 'id', $e);
109 $ineditperiod = ((time() - $old->timecreated < $CFG->maxeditingtime) || $glossary->editalways);
110 if ( (!$ineditperiod || $USER->id != $old->userid) and !has_capability('mod/glossary:manageentries', $context)) {
111 if ( $USER->id != $old->userid ) {
112 $errors['concept'] = get_string('errcannoteditothers', 'glossary');
113 } elseif (!$ineditperiod) {
114 $errors['concept'] = get_string('erredittimeexpired', 'glossary');
117 if ( !$glossary->allowduplicatedentries ) {
118 if ($dupentries = get_records('glossary_entries', 'lower(concept)', moodle_strtolower($data['concept']))) {
119 foreach ($dupentries as $curentry) {
120 if ( $glossary->id == $curentry->glossaryid ) {
121 if ( $curentry->id != $e ) {
122 $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
123 break;
130 } else {
131 if ( !$glossary->allowduplicatedentries ) {
132 if ($dupentries = get_record('glossary_entries', 'lower(concept)', moodle_strtolower($data['concept']), 'glossaryid', $glossary->id)) {
133 $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
137 return $errors;