PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/glossary/mod_form.php

https://bitbucket.org/moodle/moodle
PHP | 235 lines | 163 code | 44 blank | 28 comment | 22 complexity | 7629a833719b52a12c3082b6eb725a87 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. if (!defined('MOODLE_INTERNAL')) {
  3. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  4. }
  5. require_once ($CFG->dirroot.'/course/moodleform_mod.php');
  6. class mod_glossary_mod_form extends moodleform_mod {
  7. function definition() {
  8. global $CFG, $COURSE, $DB;
  9. $mform =& $this->_form;
  10. //-------------------------------------------------------------------------------
  11. $mform->addElement('header', 'general', get_string('general', 'form'));
  12. $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
  13. if (!empty($CFG->formatstringstriptags)) {
  14. $mform->setType('name', PARAM_TEXT);
  15. } else {
  16. $mform->setType('name', PARAM_CLEANHTML);
  17. }
  18. $mform->addRule('name', null, 'required', null, 'client');
  19. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  20. $this->standard_intro_elements();
  21. if (has_capability('mod/glossary:manageentries', context_system::instance())) {
  22. $mform->addElement('checkbox', 'globalglossary', get_string('isglobal', 'glossary'));
  23. $mform->addHelpButton('globalglossary', 'isglobal', 'glossary');
  24. }else{
  25. $mform->addElement('hidden', 'globalglossary');
  26. $mform->setType('globalglossary', PARAM_INT);
  27. }
  28. $options = array(1=>get_string('mainglossary', 'glossary'), 0=>get_string('secondaryglossary', 'glossary'));
  29. $mform->addElement('select', 'mainglossary', get_string('glossarytype', 'glossary'), $options);
  30. $mform->addHelpButton('mainglossary', 'glossarytype', 'glossary');
  31. $mform->setDefault('mainglossary', 0);
  32. // ----------------------------------------------------------------------
  33. $mform->addElement('header', 'entrieshdr', get_string('entries', 'glossary'));
  34. $mform->addElement('selectyesno', 'defaultapproval', get_string('defaultapproval', 'glossary'));
  35. $mform->setDefault('defaultapproval', $CFG->glossary_defaultapproval);
  36. $mform->addHelpButton('defaultapproval', 'defaultapproval', 'glossary');
  37. $mform->addElement('selectyesno', 'editalways', get_string('editalways', 'glossary'));
  38. $mform->setDefault('editalways', 0);
  39. $mform->addHelpButton('editalways', 'editalways', 'glossary');
  40. $mform->addElement('selectyesno', 'allowduplicatedentries', get_string('allowduplicatedentries', 'glossary'));
  41. $mform->setDefault('allowduplicatedentries', $CFG->glossary_dupentries);
  42. $mform->addHelpButton('allowduplicatedentries', 'allowduplicatedentries', 'glossary');
  43. $mform->addElement('selectyesno', 'allowcomments', get_string('allowcomments', 'glossary'));
  44. $mform->setDefault('allowcomments', $CFG->glossary_allowcomments);
  45. $mform->addHelpButton('allowcomments', 'allowcomments', 'glossary');
  46. $mform->addElement('selectyesno', 'usedynalink', get_string('usedynalink', 'glossary'));
  47. $mform->setDefault('usedynalink', $CFG->glossary_linkbydefault);
  48. $mform->addHelpButton('usedynalink', 'usedynalink', 'glossary');
  49. // ----------------------------------------------------------------------
  50. $mform->addElement('header', 'appearancehdr', get_string('appearance'));
  51. // Get and update available formats.
  52. $recformats = glossary_get_available_formats();
  53. $formats = array();
  54. foreach ($recformats as $format) {
  55. $formats[$format->name] = get_string('displayformat'.$format->name, 'glossary');
  56. }
  57. asort($formats);
  58. $mform->addElement('select', 'displayformat', get_string('displayformat', 'glossary'), $formats);
  59. $mform->setDefault('displayformat', 'dictionary');
  60. $mform->addHelpButton('displayformat', 'displayformat', 'glossary');
  61. $displayformats['default'] = get_string('displayformatdefault', 'glossary');
  62. $displayformats = array_merge($displayformats, $formats);
  63. $mform->addElement('select', 'approvaldisplayformat', get_string('approvaldisplayformat', 'glossary'), $displayformats);
  64. $mform->setDefault('approvaldisplayformat', 'default');
  65. $mform->addHelpButton('approvaldisplayformat', 'approvaldisplayformat', 'glossary');
  66. $mform->addElement('text', 'entbypage', get_string('entbypage', 'glossary'));
  67. $mform->setDefault('entbypage', $this->get_default_entbypage());
  68. $mform->addRule('entbypage', null, 'numeric', null, 'client');
  69. $mform->setType('entbypage', PARAM_INT);
  70. $mform->addElement('selectyesno', 'showalphabet', get_string('showalphabet', 'glossary'));
  71. $mform->setDefault('showalphabet', 1);
  72. $mform->addHelpButton('showalphabet', 'showalphabet', 'glossary');
  73. $mform->addElement('selectyesno', 'showall', get_string('showall', 'glossary'));
  74. $mform->setDefault('showall', 1);
  75. $mform->addHelpButton('showall', 'showall', 'glossary');
  76. $mform->addElement('selectyesno', 'showspecial', get_string('showspecial', 'glossary'));
  77. $mform->setDefault('showspecial', 1);
  78. $mform->addHelpButton('showspecial', 'showspecial', 'glossary');
  79. $mform->addElement('selectyesno', 'allowprintview', get_string('allowprintview', 'glossary'));
  80. $mform->setDefault('allowprintview', 1);
  81. $mform->addHelpButton('allowprintview', 'allowprintview', 'glossary');
  82. if ($CFG->enablerssfeeds && isset($CFG->glossary_enablerssfeeds) && $CFG->glossary_enablerssfeeds) {
  83. //-------------------------------------------------------------------------------
  84. $mform->addElement('header', 'rssheader', get_string('rss'));
  85. $choices = array();
  86. $choices[0] = get_string('none');
  87. $choices[1] = get_string('withauthor', 'glossary');
  88. $choices[2] = get_string('withoutauthor', 'glossary');
  89. $mform->addElement('select', 'rsstype', get_string('rsstype', 'glossary'), $choices);
  90. $mform->addHelpButton('rsstype', 'rsstype', 'glossary');
  91. $choices = array();
  92. $choices[0] = '0';
  93. $choices[1] = '1';
  94. $choices[2] = '2';
  95. $choices[3] = '3';
  96. $choices[4] = '4';
  97. $choices[5] = '5';
  98. $choices[10] = '10';
  99. $choices[15] = '15';
  100. $choices[20] = '20';
  101. $choices[25] = '25';
  102. $choices[30] = '30';
  103. $choices[40] = '40';
  104. $choices[50] = '50';
  105. $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
  106. $mform->addHelpButton('rssarticles', 'rssarticles', 'glossary');
  107. $mform->hideIf('rssarticles', 'rsstype', 'eq', 0);
  108. }
  109. //-------------------------------------------------------------------------------
  110. $this->standard_grading_coursemodule_elements();
  111. $this->standard_coursemodule_elements();
  112. //-------------------------------------------------------------------------------
  113. // buttons
  114. $this->add_action_buttons();
  115. }
  116. function definition_after_data() {
  117. global $COURSE, $DB;
  118. parent::definition_after_data();
  119. $mform =& $this->_form;
  120. $mainglossaryel =& $mform->getElement('mainglossary');
  121. $mainglossary = $DB->get_record('glossary', array('mainglossary'=>1, 'course'=>$COURSE->id));
  122. if ($mainglossary && ($mainglossary->id != $mform->getElementValue('instance'))){
  123. //secondary glossary, a main one already exists in this course.
  124. $mainglossaryel->setValue(0);
  125. $mainglossaryel->freeze();
  126. $mainglossaryel->setPersistantFreeze(true);
  127. } else {
  128. $mainglossaryel->unfreeze();
  129. $mainglossaryel->setPersistantFreeze(false);
  130. }
  131. }
  132. function data_preprocessing(&$default_values){
  133. parent::data_preprocessing($default_values);
  134. // Fallsback on the default setting if 'Entries shown per page' has been left blank.
  135. // This prevents the field from being required and expand its section which should not
  136. // be the case if there is a default value defined.
  137. if (empty($default_values['entbypage']) || $default_values['entbypage'] < 0) {
  138. $default_values['entbypage'] = $this->get_default_entbypage();
  139. }
  140. // Set up the completion checkboxes which aren't part of standard data.
  141. // Tick by default if Add mode or if completion entries settings is set to 1 or more.
  142. if (empty($this->_instance) || !empty($default_values['completionentries'])) {
  143. $default_values['completionentriesenabled'] = 1;
  144. } else {
  145. $default_values['completionentriesenabled'] = 0;
  146. }
  147. if (empty($default_values['completionentries'])) {
  148. $default_values['completionentries']=1;
  149. }
  150. }
  151. function add_completion_rules() {
  152. $mform =& $this->_form;
  153. $group=array();
  154. $group[] =& $mform->createElement('checkbox', 'completionentriesenabled', '', get_string('completionentries','glossary'));
  155. $group[] =& $mform->createElement('text', 'completionentries', '', array('size'=>3));
  156. $mform->setType('completionentries', PARAM_INT);
  157. $mform->addGroup($group, 'completionentriesgroup', get_string('completionentriesgroup','glossary'), array(' '), false);
  158. $mform->disabledIf('completionentries','completionentriesenabled','notchecked');
  159. return array('completionentriesgroup');
  160. }
  161. function completion_rule_enabled($data) {
  162. return (!empty($data['completionentriesenabled']) && $data['completionentries']!=0);
  163. }
  164. /**
  165. * Allows module to modify the data returned by form get_data().
  166. * This method is also called in the bulk activity completion form.
  167. *
  168. * Only available on moodleform_mod.
  169. *
  170. * @param stdClass $data the form data to be modified.
  171. */
  172. public function data_postprocessing($data) {
  173. parent::data_postprocessing($data);
  174. if (!empty($data->completionunlocked)) {
  175. // Turn off completion settings if the checkboxes aren't ticked
  176. $autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
  177. if (empty($data->completionentriesenabled) || !$autocompletion) {
  178. $data->completionentries = 0;
  179. }
  180. }
  181. }
  182. /**
  183. * Returns the default value for 'Entries shown per page'.
  184. *
  185. * @return int default for number of entries per page.
  186. */
  187. protected function get_default_entbypage() {
  188. global $CFG;
  189. return !empty($CFG->glossary_entbypage) ? $CFG->glossary_entbypage : 10;
  190. }
  191. }