MDL-28048 glossary - add multilang support to categories
[moodle.git] / mod / glossary / edit_form.php
CommitLineData
1adbd2c3 1<?php
bfebaf64
MD
2if (!defined('MOODLE_INTERNAL')) {
3 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
4}
5
a23f0aaf 6require_once ($CFG->dirroot.'/lib/formslib.php');
7
f07b9627 8class mod_glossary_entry_form extends moodleform {
a23f0aaf 9
2ee60b49 10 function definition() {
49bcd737 11 global $CFG, $DB;
a23f0aaf 12
85db95e7 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'];
a23f0aaf 20
bce3e3dc
EL
21 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
22 // Prepare format_string/text options
23 $fmtoptions = array(
24 'context' => $context);
25
a23f0aaf 26//-------------------------------------------------------------------------------
27 $mform->addElement('header', 'general', get_string('general', 'form'));
28
29 $mform->addElement('text', 'concept', get_string('concept', 'glossary'));
2ee60b49 30 $mform->setType('concept', PARAM_TEXT);
31 $mform->addRule('concept', null, 'required', null, 'client');
a23f0aaf 32
34d39b79 33 $mform->addElement('editor', 'definition_editor', get_string('definition', 'glossary'), null, $definitionoptions);
85db95e7 34 $mform->setType('definition_editor', PARAM_RAW);
35 $mform->addRule('definition_editor', get_string('required'), 'required', null, 'client');
a23f0aaf 36
f33e1ed4 37 if ($categories = $DB->get_records_menu('glossary_categories', array('glossaryid'=>$glossary->id), 'name ASC', 'id, name')){
bce3e3dc
EL
38 foreach ($categories as $id => $name) {
39 $categories[$id] = format_string($name, true, $fmtoptions);
40 }
a23f0aaf 41 $categories = array(0 => get_string('notcategorised', 'glossary')) + $categories;
49bcd737 42 $categoriesEl = $mform->addElement('select', 'categories', get_string('categories', 'glossary'), $categories);
43 $categoriesEl->setMultiple(true);
44 $categoriesEl->setSize(5);
a23f0aaf 45 }
46
2ee60b49 47 $mform->addElement('textarea', 'aliases', get_string('aliases', 'glossary'), 'rows="2" cols="40"');
48 $mform->setType('aliases', PARAM_TEXT);
94c8a386 49 $mform->addHelpButton('aliases', 'aliases', 'glossary');
a23f0aaf 50
9d54b8cd 51 $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'glossary'), null, $attachmentoptions);
94c8a386 52 $mform->addHelpButton('attachment_filemanager', 'attachment', 'glossary');
a23f0aaf 53
49bcd737 54 if (!$glossary->usedynalink) {
55 $mform->addElement('hidden', 'usedynalink', $CFG->glossary_linkentries);
d18e0fe6 56 $mform->setType('usedynalink', PARAM_INT);
49bcd737 57 $mform->addElement('hidden', 'casesensitive', $CFG->glossary_casesensitive);
d18e0fe6 58 $mform->setType('casesensitive', PARAM_INT);
49bcd737 59 $mform->addElement('hidden', 'fullmatch', $CFG->glossary_fullmatch);
d18e0fe6 60 $mform->setType('fullmatch', PARAM_INT);
49bcd737 61
a23f0aaf 62 } else {
63//-------------------------------------------------------------------------------
64 $mform->addElement('header', 'linkinghdr', get_string('linking', 'glossary'));
65
66 $mform->addElement('checkbox', 'usedynalink', get_string('entryusedynalink', 'glossary'));
94c8a386 67 $mform->addHelpButton('usedynalink', 'entryusedynalink', 'glossary');
49bcd737 68 $mform->setDefault('usedynalink', $CFG->glossary_linkentries);
a23f0aaf 69
70 $mform->addElement('checkbox', 'casesensitive', get_string('casesensitive', 'glossary'));
94c8a386 71 $mform->addHelpButton('casesensitive', 'casesensitive', 'glossary');
a23f0aaf 72 $mform->disabledIf('casesensitive', 'usedynalink');
49bcd737 73 $mform->setDefault('casesensitive', $CFG->glossary_casesensitive);
a23f0aaf 74
75 $mform->addElement('checkbox', 'fullmatch', get_string('fullmatch', 'glossary'));
94c8a386 76 $mform->addHelpButton('fullmatch', 'fullmatch', 'glossary');
a23f0aaf 77 $mform->disabledIf('fullmatch', 'usedynalink');
49bcd737 78 $mform->setDefault('fullmatch', $CFG->glossary_fullmatch);
a23f0aaf 79 }
80
49bcd737 81 $mform->addElement('hidden', 'id');
d18e0fe6 82 $mform->setType('id', PARAM_INT);
49bcd737 83 $mform->addElement('hidden', 'cmid');
d18e0fe6 84 $mform->setType('cmid', PARAM_INT);
a23f0aaf 85
86//-------------------------------------------------------------------------------
87 $this->add_action_buttons();
85db95e7 88
89//-------------------------------------------------------------------------------
90 $this->set_data($currententry);
2ee60b49 91 }
92
a78890d5 93 function validation($data, $files) {
32648682 94 global $CFG, $USER, $DB;
a78890d5 95 $errors = parent::validation($data, $files);
49bcd737 96
2ee60b49 97 $glossary = $this->_customdata['glossary'];
49bcd737 98 $cm = $this->_customdata['cm'];
99 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
100
101 $id = (int)$data['id'];
2ee60b49 102 $data['concept'] = trim($data['concept']);
49bcd737 103
104 if ($id) {
a23f0aaf 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)
49bcd737 108 $old = $DB->get_record('glossary_entries', array('id'=>$id));
a23f0aaf 109 $ineditperiod = ((time() - $old->timecreated < $CFG->maxeditingtime) || $glossary->editalways);
49bcd737 110 if ((!$ineditperiod || $USER->id != $old->userid) and !has_capability('mod/glossary:manageentries', $context)) {
111 if ($USER->id != $old->userid) {
a23f0aaf 112 $errors['concept'] = get_string('errcannoteditothers', 'glossary');
113 } elseif (!$ineditperiod) {
114 $errors['concept'] = get_string('erredittimeexpired', 'glossary');
115 }
116 }
49bcd737 117 if (!$glossary->allowduplicatedentries) {
3e7d3866
EL
118 if ($DB->record_exists_select('glossary_entries',
119 'glossaryid = :glossaryid AND LOWER(concept) = :concept AND id != :id', array(
120 'glossaryid' => $glossary->id,
121 'concept' => moodle_strtolower($data['concept']),
122 'id' => $id))) {
123 $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
a23f0aaf 124 }
125 }
126
127 } else {
49bcd737 128 if (!$glossary->allowduplicatedentries) {
3e7d3866
EL
129 if ($DB->record_exists_select('glossary_entries',
130 'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
131 'glossaryid' => $glossary->id,
132 'concept' => moodle_strtolower($data['concept'])))) {
a23f0aaf 133 $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
134 }
135 }
136 }
49bcd737 137
a23f0aaf 138 return $errors;
2ee60b49 139 }
a23f0aaf 140}
1adbd2c3 141