PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/glossary/edit_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 141 lines | 106 code | 27 blank | 8 comment | 16 complexity | f20e45856b8bb8b9ad5840fdb5e83db0 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-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->disabledIf('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->disabledIf('fullmatch', 'usedynalink');
  61. $mform->setDefault('fullmatch', $CFG->glossary_fullmatch);
  62. }
  63. $mform->addElement('hidden', 'id');
  64. $mform->setType('id', PARAM_INT);
  65. $mform->addElement('hidden', 'cmid');
  66. $mform->setType('cmid', PARAM_INT);
  67. //-------------------------------------------------------------------------------
  68. $this->add_action_buttons();
  69. //-------------------------------------------------------------------------------
  70. $this->set_data($currententry);
  71. }
  72. function validation($data, $files) {
  73. global $CFG, $USER, $DB;
  74. $errors = parent::validation($data, $files);
  75. $glossary = $this->_customdata['glossary'];
  76. $cm = $this->_customdata['cm'];
  77. $context = context_module::instance($cm->id);
  78. $id = (int)$data['id'];
  79. $data['concept'] = trim($data['concept']);
  80. if ($id) {
  81. //We are updating an entry, so we compare current session user with
  82. //existing entry user to avoid some potential problems if secureforms=off
  83. //Perhaps too much security? Anyway thanks to skodak (Bug 1823)
  84. $old = $DB->get_record('glossary_entries', array('id'=>$id));
  85. $ineditperiod = ((time() - $old->timecreated < $CFG->maxeditingtime) || $glossary->editalways);
  86. if ((!$ineditperiod || $USER->id != $old->userid) and !has_capability('mod/glossary:manageentries', $context)) {
  87. if ($USER->id != $old->userid) {
  88. $errors['concept'] = get_string('errcannoteditothers', 'glossary');
  89. } elseif (!$ineditperiod) {
  90. $errors['concept'] = get_string('erredittimeexpired', 'glossary');
  91. }
  92. }
  93. if (!$glossary->allowduplicatedentries) {
  94. if ($DB->record_exists_select('glossary_entries',
  95. 'glossaryid = :glossaryid AND LOWER(concept) = :concept AND id != :id', array(
  96. 'glossaryid' => $glossary->id,
  97. 'concept' => core_text::strtolower($data['concept']),
  98. 'id' => $id))) {
  99. $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
  100. }
  101. }
  102. } else {
  103. if (!$glossary->allowduplicatedentries) {
  104. if ($DB->record_exists_select('glossary_entries',
  105. 'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
  106. 'glossaryid' => $glossary->id,
  107. 'concept' => core_text::strtolower($data['concept'])))) {
  108. $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
  109. }
  110. }
  111. }
  112. return $errors;
  113. }
  114. }