Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  
   3  require_once('../../config.php');
   4  require_once ('lib.php');
   5  require_once ('edit_form.php');
   6  
   7  $cmid = required_param('cmid', PARAM_INT);            // Course Module ID
   8  $id   = optional_param('id', 0, PARAM_INT);           // EntryID
   9  
  10  if (!$cm = get_coursemodule_from_id('glossary', $cmid)) {
  11      print_error('invalidcoursemodule');
  12  }
  13  
  14  if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
  15      print_error('coursemisconf');
  16  }
  17  
  18  require_login($course, false, $cm);
  19  
  20  $context = context_module::instance($cm->id);
  21  
  22  if (!$glossary = $DB->get_record('glossary', array('id'=>$cm->instance))) {
  23      print_error('invalidid', 'glossary');
  24  }
  25  
  26  $url = new moodle_url('/mod/glossary/edit.php', array('cmid'=>$cm->id));
  27  if (!empty($id)) {
  28      $url->param('id', $id);
  29  }
  30  $PAGE->set_url($url);
  31  
  32  if ($id) { // if entry is specified
  33      if (isguestuser()) {
  34          print_error('guestnoedit', 'glossary', "$CFG->wwwroot/mod/glossary/view.php?id=$cmid");
  35      }
  36  
  37      if (!$entry = $DB->get_record('glossary_entries', array('id'=>$id, 'glossaryid'=>$glossary->id))) {
  38          print_error('invalidentry');
  39      }
  40  
  41      $ineditperiod = ((time() - $entry->timecreated <  $CFG->maxeditingtime) || $glossary->editalways);
  42      if (!has_capability('mod/glossary:manageentries', $context) and !($entry->userid == $USER->id and ($ineditperiod and has_capability('mod/glossary:write', $context)))) {
  43          if ($USER->id != $entry->userid) {
  44              print_error('errcannoteditothers', 'glossary', "view.php?id=$cm->id&amp;mode=entry&amp;hook=$id");
  45          } elseif (!$ineditperiod) {
  46              print_error('erredittimeexpired', 'glossary', "view.php?id=$cm->id&amp;mode=entry&amp;hook=$id");
  47          }
  48      }
  49  
  50      //prepare extra data
  51      if ($aliases = $DB->get_records_menu("glossary_alias", array("entryid"=>$id), '', 'id, alias')) {
  52          $entry->aliases = implode("\n", $aliases) . "\n";
  53      }
  54      if ($categoriesarr = $DB->get_records_menu("glossary_entries_categories", array('entryid'=>$id), '', 'id, categoryid')) {
  55          // TODO: this fetches cats from both main and secondary glossary :-(
  56          $entry->categories = array_values($categoriesarr);
  57      }
  58  
  59  } else { // new entry
  60      require_capability('mod/glossary:write', $context);
  61      // note: guest user does not have any write capability
  62      $entry = new stdClass();
  63      $entry->id = null;
  64  }
  65  
  66  list($definitionoptions, $attachmentoptions) = glossary_get_editor_and_attachment_options($course, $context, $entry);
  67  
  68  $entry = file_prepare_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id);
  69  $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment', $entry->id);
  70  
  71  $entry->cmid = $cm->id;
  72  
  73  // create form and set initial data
  74  $mform = new mod_glossary_entry_form(null, array('current'=>$entry, 'cm'=>$cm, 'glossary'=>$glossary,
  75                                                   'definitionoptions'=>$definitionoptions, 'attachmentoptions'=>$attachmentoptions));
  76  
  77  if ($mform->is_cancelled()){
  78      if ($id){
  79          redirect("view.php?id=$cm->id&mode=entry&hook=$id");
  80      } else {
  81          redirect("view.php?id=$cm->id");
  82      }
  83  
  84  } else if ($data = $mform->get_data()) {
  85      $entry = glossary_edit_entry($data, $course, $cm, $glossary, $context);
  86      if (core_tag_tag::is_enabled('mod_glossary', 'glossary_entries') && isset($data->tags)) {
  87          core_tag_tag::set_item_tags('mod_glossary', 'glossary_entries', $data->id, $context, $data->tags);
  88      }
  89      redirect("view.php?id=$cm->id&mode=entry&hook=$entry->id");
  90  }
  91  
  92  if (!empty($id)) {
  93      $PAGE->navbar->add(get_string('edit'));
  94  }
  95  
  96  $PAGE->set_title($glossary->name);
  97  $PAGE->set_heading($course->fullname);
  98  echo $OUTPUT->header();
  99  echo $OUTPUT->heading(format_string($glossary->name), 2);
 100  if ($glossary->intro) {
 101      echo $OUTPUT->box(format_module_intro('glossary', $glossary, $cm->id), 'generalbox', 'intro');
 102  }
 103  
 104  $data = new StdClass();
 105  $data->tags = core_tag_tag::get_item_tags_array('mod_glossary', 'glossary_entries', $id);
 106  $mform->set_data($data);
 107  
 108  $mform->display();
 109  
 110  echo $OUTPUT->footer();
 111