|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 if (!defined('MOODLE_INTERNAL')) { 00003 die('Direct access to this script is forbidden.'); 00004 } 00005 00006 require_once ($CFG->dirroot.'/lib/formslib.php'); 00007 00008 class mod_glossary_entry_form extends moodleform { 00009 00010 function definition() { 00011 global $CFG, $DB; 00012 00013 $mform = $this->_form; 00014 00015 $currententry = $this->_customdata['current']; 00016 $glossary = $this->_customdata['glossary']; 00017 $cm = $this->_customdata['cm']; 00018 $definitionoptions = $this->_customdata['definitionoptions']; 00019 $attachmentoptions = $this->_customdata['attachmentoptions']; 00020 00021 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00022 // Prepare format_string/text options 00023 $fmtoptions = array( 00024 'context' => $context); 00025 00026 //------------------------------------------------------------------------------- 00027 $mform->addElement('header', 'general', get_string('general', 'form')); 00028 00029 $mform->addElement('text', 'concept', get_string('concept', 'glossary')); 00030 $mform->setType('concept', PARAM_TEXT); 00031 $mform->addRule('concept', null, 'required', null, 'client'); 00032 00033 $mform->addElement('editor', 'definition_editor', get_string('definition', 'glossary'), null, $definitionoptions); 00034 $mform->setType('definition_editor', PARAM_RAW); 00035 $mform->addRule('definition_editor', get_string('required'), 'required', null, 'client'); 00036 00037 if ($categories = $DB->get_records_menu('glossary_categories', array('glossaryid'=>$glossary->id), 'name ASC', 'id, name')){ 00038 foreach ($categories as $id => $name) { 00039 $categories[$id] = format_string($name, true, $fmtoptions); 00040 } 00041 $categories = array(0 => get_string('notcategorised', 'glossary')) + $categories; 00042 $categoriesEl = $mform->addElement('select', 'categories', get_string('categories', 'glossary'), $categories); 00043 $categoriesEl->setMultiple(true); 00044 $categoriesEl->setSize(5); 00045 } 00046 00047 $mform->addElement('textarea', 'aliases', get_string('aliases', 'glossary'), 'rows="2" cols="40"'); 00048 $mform->setType('aliases', PARAM_TEXT); 00049 $mform->addHelpButton('aliases', 'aliases', 'glossary'); 00050 00051 $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'glossary'), null, $attachmentoptions); 00052 $mform->addHelpButton('attachment_filemanager', 'attachment', 'glossary'); 00053 00054 if (!$glossary->usedynalink) { 00055 $mform->addElement('hidden', 'usedynalink', $CFG->glossary_linkentries); 00056 $mform->setType('usedynalink', PARAM_INT); 00057 $mform->addElement('hidden', 'casesensitive', $CFG->glossary_casesensitive); 00058 $mform->setType('casesensitive', PARAM_INT); 00059 $mform->addElement('hidden', 'fullmatch', $CFG->glossary_fullmatch); 00060 $mform->setType('fullmatch', PARAM_INT); 00061 00062 } else { 00063 //------------------------------------------------------------------------------- 00064 $mform->addElement('header', 'linkinghdr', get_string('linking', 'glossary')); 00065 00066 $mform->addElement('checkbox', 'usedynalink', get_string('entryusedynalink', 'glossary')); 00067 $mform->addHelpButton('usedynalink', 'entryusedynalink', 'glossary'); 00068 $mform->setDefault('usedynalink', $CFG->glossary_linkentries); 00069 00070 $mform->addElement('checkbox', 'casesensitive', get_string('casesensitive', 'glossary')); 00071 $mform->addHelpButton('casesensitive', 'casesensitive', 'glossary'); 00072 $mform->disabledIf('casesensitive', 'usedynalink'); 00073 $mform->setDefault('casesensitive', $CFG->glossary_casesensitive); 00074 00075 $mform->addElement('checkbox', 'fullmatch', get_string('fullmatch', 'glossary')); 00076 $mform->addHelpButton('fullmatch', 'fullmatch', 'glossary'); 00077 $mform->disabledIf('fullmatch', 'usedynalink'); 00078 $mform->setDefault('fullmatch', $CFG->glossary_fullmatch); 00079 } 00080 00081 $mform->addElement('hidden', 'id'); 00082 $mform->setType('id', PARAM_INT); 00083 $mform->addElement('hidden', 'cmid'); 00084 $mform->setType('cmid', PARAM_INT); 00085 00086 //------------------------------------------------------------------------------- 00087 $this->add_action_buttons(); 00088 00089 //------------------------------------------------------------------------------- 00090 $this->set_data($currententry); 00091 } 00092 00093 function validation($data, $files) { 00094 global $CFG, $USER, $DB; 00095 $errors = parent::validation($data, $files); 00096 00097 $glossary = $this->_customdata['glossary']; 00098 $cm = $this->_customdata['cm']; 00099 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00100 00101 $id = (int)$data['id']; 00102 $data['concept'] = trim($data['concept']); 00103 00104 if ($id) { 00105 //We are updating an entry, so we compare current session user with 00106 //existing entry user to avoid some potential problems if secureforms=off 00107 //Perhaps too much security? Anyway thanks to skodak (Bug 1823) 00108 $old = $DB->get_record('glossary_entries', array('id'=>$id)); 00109 $ineditperiod = ((time() - $old->timecreated < $CFG->maxeditingtime) || $glossary->editalways); 00110 if ((!$ineditperiod || $USER->id != $old->userid) and !has_capability('mod/glossary:manageentries', $context)) { 00111 if ($USER->id != $old->userid) { 00112 $errors['concept'] = get_string('errcannoteditothers', 'glossary'); 00113 } elseif (!$ineditperiod) { 00114 $errors['concept'] = get_string('erredittimeexpired', 'glossary'); 00115 } 00116 } 00117 if (!$glossary->allowduplicatedentries) { 00118 if ($DB->record_exists_select('glossary_entries', 00119 'glossaryid = :glossaryid AND LOWER(concept) = :concept AND id != :id', array( 00120 'glossaryid' => $glossary->id, 00121 'concept' => moodle_strtolower($data['concept']), 00122 'id' => $id))) { 00123 $errors['concept'] = get_string('errconceptalreadyexists', 'glossary'); 00124 } 00125 } 00126 00127 } else { 00128 if (!$glossary->allowduplicatedentries) { 00129 if ($DB->record_exists_select('glossary_entries', 00130 'glossaryid = :glossaryid AND LOWER(concept) = :concept', array( 00131 'glossaryid' => $glossary->id, 00132 'concept' => moodle_strtolower($data['concept'])))) { 00133 $errors['concept'] = get_string('errconceptalreadyexists', 'glossary'); 00134 } 00135 } 00136 } 00137 00138 return $errors; 00139 } 00140 } 00141