Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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      // Check if the user can update the entry (trigger exception if he can't).
  42      mod_glossary_can_update_entry($entry, $glossary, $context, $cm, false);
  43      // Prepare extra data.
  44      $entry = mod_glossary_prepare_entry_for_edition($entry);
  45  
  46  } else { // new entry
  47      require_capability('mod/glossary:write', $context);
  48      // note: guest user does not have any write capability
  49      $entry = new stdClass();
  50      $entry->id = null;
  51  }
  52  
  53  list($definitionoptions, $attachmentoptions) = glossary_get_editor_and_attachment_options($course, $context, $entry);
  54  
  55  $entry = file_prepare_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id);
  56  $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment', $entry->id);
  57  
  58  $entry->cmid = $cm->id;
  59  
  60  // create form and set initial data
  61  $mform = new mod_glossary_entry_form(null, array('current'=>$entry, 'cm'=>$cm, 'glossary'=>$glossary,
  62                                                   'definitionoptions'=>$definitionoptions, 'attachmentoptions'=>$attachmentoptions));
  63  
  64  if ($mform->is_cancelled()){
  65      if ($id){
  66          redirect("view.php?id=$cm->id&mode=entry&hook=$id");
  67      } else {
  68          redirect("view.php?id=$cm->id");
  69      }
  70  
  71  } else if ($data = $mform->get_data()) {
  72      $entry = glossary_edit_entry($data, $course, $cm, $glossary, $context);
  73      if (core_tag_tag::is_enabled('mod_glossary', 'glossary_entries') && isset($data->tags)) {
  74          core_tag_tag::set_item_tags('mod_glossary', 'glossary_entries', $data->id, $context, $data->tags);
  75      }
  76      redirect("view.php?id=$cm->id&mode=entry&hook=$entry->id");
  77  }
  78  
  79  if (!empty($id)) {
  80      $PAGE->navbar->add(get_string('edit'));
  81  }
  82  
  83  $PAGE->set_title($glossary->name);
  84  $PAGE->set_heading($course->fullname);
  85  $PAGE->set_secondary_active_tab('modulepage');
  86  $PAGE->activityheader->set_attrs([
  87      'hidecompletion' => true,
  88      'description' => ''
  89  ]);
  90  echo $OUTPUT->header();
  91  if (!$id) {
  92      echo $OUTPUT->heading(get_string('addsingleentry', 'mod_glossary'));
  93  } else {
  94      echo $OUTPUT->heading(get_string('editentry', 'mod_glossary'));
  95  }
  96  
  97  $data = new StdClass();
  98  $data->tags = core_tag_tag::get_item_tags_array('mod_glossary', 'glossary_entries', $id);
  99  $mform->set_data($data);
 100  
 101  $mform->display();
 102  
 103  echo $OUTPUT->footer();
 104