Merge branch 'MDL-48372-27' of git://github.com/ankitagarwal/moodle into MOODLE_27_STABLE
[moodle.git] / mod / glossary / mod_form.php
blob9576132c770df9466031564f4087b7f7fc5b6e69
1 <?php
2 if (!defined('MOODLE_INTERNAL')) {
3 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
6 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
8 class mod_glossary_mod_form extends moodleform_mod {
10 function definition() {
11 global $CFG, $COURSE, $DB;
13 $mform =& $this->_form;
15 //-------------------------------------------------------------------------------
16 $mform->addElement('header', 'general', get_string('general', 'form'));
18 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
19 if (!empty($CFG->formatstringstriptags)) {
20 $mform->setType('name', PARAM_TEXT);
21 } else {
22 $mform->setType('name', PARAM_CLEANHTML);
24 $mform->addRule('name', null, 'required', null, 'client');
25 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
27 $this->add_intro_editor(true);
29 if (has_capability('mod/glossary:manageentries', context_system::instance())) {
30 $mform->addElement('checkbox', 'globalglossary', get_string('isglobal', 'glossary'));
31 $mform->addHelpButton('globalglossary', 'isglobal', 'glossary');
33 }else{
34 $mform->addElement('hidden', 'globalglossary');
35 $mform->setType('globalglossary', PARAM_INT);
38 $options = array(1=>get_string('mainglossary', 'glossary'), 0=>get_string('secondaryglossary', 'glossary'));
39 $mform->addElement('select', 'mainglossary', get_string('glossarytype', 'glossary'), $options);
40 $mform->addHelpButton('mainglossary', 'glossarytype', 'glossary');
41 $mform->setDefault('mainglossary', 0);
43 // ----------------------------------------------------------------------
44 $mform->addElement('header', 'entrieshdr', get_string('entries', 'glossary'));
46 $mform->addElement('selectyesno', 'defaultapproval', get_string('defaultapproval', 'glossary'));
47 $mform->setDefault('defaultapproval', $CFG->glossary_defaultapproval);
48 $mform->addHelpButton('defaultapproval', 'defaultapproval', 'glossary');
50 $mform->addElement('selectyesno', 'editalways', get_string('editalways', 'glossary'));
51 $mform->setDefault('editalways', 0);
52 $mform->addHelpButton('editalways', 'editalways', 'glossary');
54 $mform->addElement('selectyesno', 'allowduplicatedentries', get_string('allowduplicatedentries', 'glossary'));
55 $mform->setDefault('allowduplicatedentries', $CFG->glossary_dupentries);
56 $mform->addHelpButton('allowduplicatedentries', 'allowduplicatedentries', 'glossary');
58 $mform->addElement('selectyesno', 'allowcomments', get_string('allowcomments', 'glossary'));
59 $mform->setDefault('allowcomments', $CFG->glossary_allowcomments);
60 $mform->addHelpButton('allowcomments', 'allowcomments', 'glossary');
62 $mform->addElement('selectyesno', 'usedynalink', get_string('usedynalink', 'glossary'));
63 $mform->setDefault('usedynalink', $CFG->glossary_linkbydefault);
64 $mform->addHelpButton('usedynalink', 'usedynalink', 'glossary');
66 // ----------------------------------------------------------------------
67 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
69 // Get and update available formats.
70 $recformats = glossary_get_available_formats();
71 $formats = array();
72 foreach ($recformats as $format) {
73 $formats[$format->name] = get_string('displayformat'.$format->name, 'glossary');
75 asort($formats);
76 $mform->addElement('select', 'displayformat', get_string('displayformat', 'glossary'), $formats);
77 $mform->setDefault('displayformat', 'dictionary');
78 $mform->addHelpButton('displayformat', 'displayformat', 'glossary');
80 $displayformats['default'] = get_string('displayformatdefault', 'glossary');
81 $displayformats = array_merge($displayformats, $formats);
82 $mform->addElement('select', 'approvaldisplayformat', get_string('approvaldisplayformat', 'glossary'), $displayformats);
83 $mform->setDefault('approvaldisplayformat', 'default');
84 $mform->addHelpButton('approvaldisplayformat', 'approvaldisplayformat', 'glossary');
86 $mform->addElement('text', 'entbypage', get_string('entbypage', 'glossary'));
87 $mform->setDefault('entbypage', $this->get_default_entbypage());
88 $mform->addRule('entbypage', null, 'numeric', null, 'client');
89 $mform->setType('entbypage', PARAM_INT);
91 $mform->addElement('selectyesno', 'showalphabet', get_string('showalphabet', 'glossary'));
92 $mform->setDefault('showalphabet', 1);
93 $mform->addHelpButton('showalphabet', 'showalphabet', 'glossary');
95 $mform->addElement('selectyesno', 'showall', get_string('showall', 'glossary'));
96 $mform->setDefault('showall', 1);
97 $mform->addHelpButton('showall', 'showall', 'glossary');
99 $mform->addElement('selectyesno', 'showspecial', get_string('showspecial', 'glossary'));
100 $mform->setDefault('showspecial', 1);
101 $mform->addHelpButton('showspecial', 'showspecial', 'glossary');
103 $mform->addElement('selectyesno', 'allowprintview', get_string('allowprintview', 'glossary'));
104 $mform->setDefault('allowprintview', 1);
105 $mform->addHelpButton('allowprintview', 'allowprintview', 'glossary');
107 if ($CFG->enablerssfeeds && isset($CFG->glossary_enablerssfeeds) && $CFG->glossary_enablerssfeeds) {
108 //-------------------------------------------------------------------------------
109 $mform->addElement('header', 'rssheader', get_string('rss'));
110 $choices = array();
111 $choices[0] = get_string('none');
112 $choices[1] = get_string('withauthor', 'glossary');
113 $choices[2] = get_string('withoutauthor', 'glossary');
114 $mform->addElement('select', 'rsstype', get_string('rsstype'), $choices);
115 $mform->addHelpButton('rsstype', 'rsstype', 'glossary');
117 $choices = array();
118 $choices[0] = '0';
119 $choices[1] = '1';
120 $choices[2] = '2';
121 $choices[3] = '3';
122 $choices[4] = '4';
123 $choices[5] = '5';
124 $choices[10] = '10';
125 $choices[15] = '15';
126 $choices[20] = '20';
127 $choices[25] = '25';
128 $choices[30] = '30';
129 $choices[40] = '40';
130 $choices[50] = '50';
131 $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
132 $mform->addHelpButton('rssarticles', 'rssarticles', 'glossary');
133 $mform->disabledIf('rssarticles', 'rsstype', 'eq', 0);
136 //-------------------------------------------------------------------------------
138 $this->standard_grading_coursemodule_elements();
140 $this->standard_coursemodule_elements();
142 //-------------------------------------------------------------------------------
143 // buttons
144 $this->add_action_buttons();
147 function definition_after_data() {
148 global $COURSE, $DB;
150 parent::definition_after_data();
151 $mform =& $this->_form;
152 $mainglossaryel =& $mform->getElement('mainglossary');
153 $mainglossary = $DB->get_record('glossary', array('mainglossary'=>1, 'course'=>$COURSE->id));
154 if ($mainglossary && ($mainglossary->id != $mform->getElementValue('instance'))){
155 //secondary glossary, a main one already exists in this course.
156 $mainglossaryel->setValue(0);
157 $mainglossaryel->freeze();
158 $mainglossaryel->setPersistantFreeze(true);
159 } else {
160 $mainglossaryel->unfreeze();
161 $mainglossaryel->setPersistantFreeze(false);
166 function data_preprocessing(&$default_values){
167 parent::data_preprocessing($default_values);
169 // Fallsback on the default setting if 'Entries shown per page' has been left blank.
170 // This prevents the field from being required and expand its section which should not
171 // be the case if there is a default value defined.
172 if (empty($default_values['entbypage']) || $default_values['entbypage'] < 0) {
173 $default_values['entbypage'] = $this->get_default_entbypage();
176 // Set up the completion checkboxes which aren't part of standard data.
177 // We also make the default value (if you turn on the checkbox) for those
178 // numbers to be 1, this will not apply unless checkbox is ticked.
179 $default_values['completionentriesenabled']=
180 !empty($default_values['completionentries']) ? 1 : 0;
181 if (empty($default_values['completionentries'])) {
182 $default_values['completionentries']=1;
186 function add_completion_rules() {
187 $mform =& $this->_form;
189 $group=array();
190 $group[] =& $mform->createElement('checkbox', 'completionentriesenabled', '', get_string('completionentries','glossary'));
191 $group[] =& $mform->createElement('text', 'completionentries', '', array('size'=>3));
192 $mform->setType('completionentries', PARAM_INT);
193 $mform->addGroup($group, 'completionentriesgroup', get_string('completionentriesgroup','glossary'), array(' '), false);
194 $mform->disabledIf('completionentries','completionentriesenabled','notchecked');
196 return array('completionentriesgroup');
199 function completion_rule_enabled($data) {
200 return (!empty($data['completionentriesenabled']) && $data['completionentries']!=0);
203 function get_data() {
204 $data = parent::get_data();
205 if (!$data) {
206 return false;
208 if (!empty($data->completionunlocked)) {
209 // Turn off completion settings if the checkboxes aren't ticked
210 $autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
211 if (empty($data->completionentriesenabled) || !$autocompletion) {
212 $data->completionentries = 0;
215 return $data;
219 * Returns the default value for 'Entries shown per page'.
221 * @return int default for number of entries per page.
223 protected function get_default_entbypage() {
224 global $CFG;
225 return !empty($CFG->glossary_entbypage) ? $CFG->glossary_entbypage : 10;