Updated the 19 build version to 20101023
[moodle.git] / mod / glossary / edit_form.php
blobf4e72f0a159d7ebcd23790984b7434597325899e
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->setType('usedynalink', PARAM_INT);
68 $mform->addElement('hidden', 'casesensitive', $casesensitive);
69 $mform->setType('casesensitive', PARAM_INT);
70 $mform->addElement('hidden', 'fullmatch', $fullmatch);
71 $mform->setType('fullmatch', PARAM_INT);
72 } else {
73 //-------------------------------------------------------------------------------
74 $mform->addElement('header', 'linkinghdr', get_string('linking', 'glossary'));
76 $mform->addElement('checkbox', 'usedynalink', get_string('entryusedynalink', 'glossary'));
77 $mform->setHelpButton('usedynalink', array('usedynalinkentry', strip_tags(get_string('usedynalink', 'glossary')), 'glossary'));
78 $mform->setDefault('usedynalink', $usedynalink);
80 $mform->addElement('checkbox', 'casesensitive', get_string('casesensitive', 'glossary'));
81 $mform->setHelpButton('casesensitive', array('casesensitive', strip_tags(get_string('casesensitive', 'glossary')), 'glossary'));
82 $mform->disabledIf('casesensitive', 'usedynalink');
83 $mform->setDefault('casesensitive', $casesensitive);
85 $mform->addElement('checkbox', 'fullmatch', get_string('fullmatch', 'glossary'));
86 $mform->setHelpButton('fullmatch', array('fullmatch', strip_tags(get_string('fullmatch', 'glossary')), 'glossary'));
87 $mform->disabledIf('fullmatch', 'usedynalink');
88 $mform->setDefault('fullmatch', $fullmatch);
91 $mform->addElement('hidden', 'e', $e);
92 $mform->setType('e', PARAM_INT);
93 $mform->addElement('hidden', 'id', $cm->id);
94 $mform->setType('id', PARAM_INT);
95 $mform->addElement('hidden', 'mode', $mode);
96 $mform->setType('mode', PARAM_ALPHA);
97 $mform->addElement('hidden', 'hook', $hook);
98 $mform->setType('hook', PARAM_ALPHANUM);
100 //-------------------------------------------------------------------------------
101 $this->add_action_buttons();
104 function validation($data, $files) {
105 global $CFG, $USER;
106 $errors = parent::validation($data, $files);
107 $e = $this->_customdata['e'];
108 $glossary = $this->_customdata['glossary'];
109 $context = $this->_customdata['context'];
110 $data['concept'] = trim($data['concept']);
111 if ($e) {
112 //We are updating an entry, so we compare current session user with
113 //existing entry user to avoid some potential problems if secureforms=off
114 //Perhaps too much security? Anyway thanks to skodak (Bug 1823)
115 $old = get_record('glossary_entries', 'id', $e);
116 $ineditperiod = ((time() - $old->timecreated < $CFG->maxeditingtime) || $glossary->editalways);
117 if ( (!$ineditperiod || $USER->id != $old->userid) and !has_capability('mod/glossary:manageentries', $context)) {
118 if ( $USER->id != $old->userid ) {
119 $errors['concept'] = get_string('errcannoteditothers', 'glossary');
120 } elseif (!$ineditperiod) {
121 $errors['concept'] = get_string('erredittimeexpired', 'glossary');
124 if ( !$glossary->allowduplicatedentries ) {
125 if ($dupentries = get_records('glossary_entries', 'lower(concept)', moodle_strtolower($data['concept']))) {
126 foreach ($dupentries as $curentry) {
127 if ( $glossary->id == $curentry->glossaryid ) {
128 if ( $curentry->id != $e ) {
129 $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
130 break;
137 } else {
138 if ( !$glossary->allowduplicatedentries ) {
139 if ($dupentries = get_record('glossary_entries', 'lower(concept)', moodle_strtolower($data['concept']), 'glossaryid', $glossary->id)) {
140 $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
144 return $errors;