PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/glossary/edit_form.php

https://bitbucket.org/moodle/moodle
PHP | 145 lines | 108 code | 29 blank | 8 comment | 17 complexity | 2efb314c171c455c0e3e8d0cd1eb0996 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.'/lib/formslib.php');
  6. class mod_glossary_entry_form extends moodleform {
  7. function definition() {
  8. global $CFG, $DB;
  9. $mform = $this->_form;
  10. $currententry = $this->_customdata['current'];
  11. $glossary = $this->_customdata['glossary'];
  12. $cm = $this->_customdata['cm'];
  13. $definitionoptions = $this->_customdata['definitionoptions'];
  14. $attachmentoptions = $this->_customdata['attachmentoptions'];
  15. $context = context_module::instance($cm->id);
  16. // Prepare format_string/text options
  17. $fmtoptions = array(
  18. 'context' => $context);
  19. //-------------------------------------------------------------------------------
  20. $mform->addElement('header', 'general', get_string('general', 'form'));
  21. $mform->addElement('text', 'concept', get_string('concept', 'glossary'));
  22. $mform->setType('concept', PARAM_TEXT);
  23. $mform->addRule('concept', null, 'required', null, 'client');
  24. $mform->addElement('editor', 'definition_editor', get_string('definition', 'glossary'), null, $definitionoptions);
  25. $mform->setType('definition_editor', PARAM_RAW);
  26. $mform->addRule('definition_editor', get_string('required'), 'required', null, 'client');
  27. if ($categories = $DB->get_records_menu('glossary_categories', array('glossaryid'=>$glossary->id), 'name ASC', 'id, name')){
  28. foreach ($categories as $id => $name) {
  29. $categories[$id] = format_string($name, true, $fmtoptions);
  30. }
  31. $categories = array(0 => get_string('notcategorised', 'glossary')) + $categories;
  32. $categoriesEl = $mform->addElement('select', 'categories', get_string('categories', 'glossary'), $categories);
  33. $categoriesEl->setMultiple(true);
  34. $categoriesEl->setSize(5);
  35. }
  36. $mform->addElement('textarea', 'aliases', get_string('aliases', 'glossary'), 'rows="2" cols="40"');
  37. $mform->setType('aliases', PARAM_TEXT);
  38. $mform->addHelpButton('aliases', 'aliases', 'glossary');
  39. $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'glossary'), null, $attachmentoptions);
  40. $mform->addHelpButton('attachment_filemanager', 'attachment', 'glossary');
  41. if (!$glossary->usedynalink) {
  42. $mform->addElement('hidden', 'usedynalink', $CFG->glossary_linkentries);
  43. $mform->setType('usedynalink', PARAM_INT);
  44. $mform->addElement('hidden', 'casesensitive', $CFG->glossary_casesensitive);
  45. $mform->setType('casesensitive', PARAM_INT);
  46. $mform->addElement('hidden', 'fullmatch', $CFG->glossary_fullmatch);
  47. $mform->setType('fullmatch', PARAM_INT);
  48. } else {
  49. //-------------------------------------------------------------------------------
  50. $mform->addElement('header', 'linkinghdr', get_string('linking', 'glossary'));
  51. $mform->addElement('checkbox', 'usedynalink', get_string('entryusedynalink', 'glossary'));
  52. $mform->addHelpButton('usedynalink', 'entryusedynalink', 'glossary');
  53. $mform->setDefault('usedynalink', $CFG->glossary_linkentries);
  54. $mform->addElement('checkbox', 'casesensitive', get_string('casesensitive', 'glossary'));
  55. $mform->addHelpButton('casesensitive', 'casesensitive', 'glossary');
  56. $mform->hideIf('casesensitive', 'usedynalink');
  57. $mform->setDefault('casesensitive', $CFG->glossary_casesensitive);
  58. $mform->addElement('checkbox', 'fullmatch', get_string('fullmatch', 'glossary'));
  59. $mform->addHelpButton('fullmatch', 'fullmatch', 'glossary');
  60. $mform->hideIf('fullmatch', 'usedynalink');
  61. $mform->setDefault('fullmatch', $CFG->glossary_fullmatch);
  62. }
  63. if (core_tag_tag::is_enabled('mod_glossary', 'glossary_entries')) {
  64. $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
  65. $mform->addElement('tags', 'tags', get_string('tags'),
  66. array('itemtype' => 'glossary_entries', 'component' => 'mod_glossary'));
  67. }
  68. $mform->addElement('hidden', 'id');
  69. $mform->setType('id', PARAM_INT);
  70. $mform->addElement('hidden', 'cmid');
  71. $mform->setType('cmid', PARAM_INT);
  72. //-------------------------------------------------------------------------------
  73. $this->add_action_buttons();
  74. //-------------------------------------------------------------------------------
  75. $this->set_data($currententry);
  76. }
  77. function validation($data, $files) {
  78. global $CFG, $USER, $DB;
  79. $errors = parent::validation($data, $files);
  80. $glossary = $this->_customdata['glossary'];
  81. $cm = $this->_customdata['cm'];
  82. $context = context_module::instance($cm->id);
  83. $id = (int)$data['id'];
  84. $data['concept'] = trim($data['concept']);
  85. if ($id) {
  86. //We are updating an entry, so we compare current session user with
  87. //existing entry user to avoid some potential problems if secureforms=off
  88. //Perhaps too much security? Anyway thanks to skodak (Bug 1823)
  89. $old = $DB->get_record('glossary_entries', array('id'=>$id));
  90. $ineditperiod = ((time() - $old->timecreated < $CFG->maxeditingtime) || $glossary->editalways);
  91. if ((!$ineditperiod || $USER->id != $old->userid) and !has_capability('mod/glossary:manageentries', $context)) {
  92. if ($USER->id != $old->userid) {
  93. $errors['concept'] = get_string('errcannoteditothers', 'glossary');
  94. } elseif (!$ineditperiod) {
  95. $errors['concept'] = get_string('erredittimeexpired', 'glossary');
  96. }
  97. }
  98. if (!$glossary->allowduplicatedentries) {
  99. if ($DB->record_exists_select('glossary_entries',
  100. 'glossaryid = :glossaryid AND LOWER(concept) = :concept AND id != :id', array(
  101. 'glossaryid' => $glossary->id,
  102. 'concept' => core_text::strtolower($data['concept']),
  103. 'id' => $id))) {
  104. $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
  105. }
  106. }
  107. } else {
  108. if (!$glossary->allowduplicatedentries) {
  109. if (glossary_concept_exists($glossary, $data['concept'])) {
  110. $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
  111. }
  112. }
  113. }
  114. return $errors;
  115. }
  116. }