Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  
   3  require_once('../../config.php');
   4  require_once ('lib.php');
   5  
   6  $id       = required_param('id', PARAM_INT);          // Entry ID
   7  $confirm  = optional_param('confirm', 0, PARAM_BOOL); // export confirmation
   8  $prevmode = required_param('prevmode', PARAM_ALPHA);
   9  $hook     = optional_param('hook', '', PARAM_CLEAN);
  10  
  11  $url = new moodle_url('/mod/glossary/exportentry.php', array('id'=>$id,'prevmode'=>$prevmode));
  12  if ($confirm !== 0) {
  13      $url->param('confirm', $confirm);
  14  }
  15  if ($hook !== 'ALL') {
  16      $url->param('hook', $hook);
  17  }
  18  $PAGE->set_url($url);
  19  
  20  if (!$entry = $DB->get_record('glossary_entries', array('id'=>$id))) {
  21      throw new \moodle_exception('invalidentry');
  22  }
  23  
  24  if ($entry->sourceglossaryid) {
  25      //already exported
  26      if (!$cm = get_coursemodule_from_id('glossary', $entry->sourceglossaryid)) {
  27          throw new \moodle_exception('invalidcoursemodule');
  28      }
  29      redirect('view.php?id='.$cm->id.'&amp;mode=entry&amp;hook='.$entry->id);
  30  }
  31  
  32  if (!$cm = get_coursemodule_from_instance('glossary', $entry->glossaryid)) {
  33      throw new \moodle_exception('invalidcoursemodule');
  34  }
  35  
  36  if (!$glossary = $DB->get_record('glossary', array('id'=>$cm->instance))) {
  37      throw new \moodle_exception('invalidid', 'glossary');
  38  }
  39  
  40  if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
  41      throw new \moodle_exception('coursemisconf');
  42  }
  43  
  44  require_course_login($course->id, true, $cm);
  45  $context = context_module::instance($cm->id);
  46  require_capability('mod/glossary:export', $context);
  47  
  48  $returnurl = "view.php?id=$cm->id&amp;mode=$prevmode&amp;hook=".urlencode($hook);
  49  
  50  if (!$mainglossary = $DB->get_record('glossary', array('course'=>$cm->course, 'mainglossary'=>1))) {
  51      //main glossary not present
  52      redirect($returnurl);
  53  }
  54  
  55  if (!$maincm = get_coursemodule_from_instance('glossary', $mainglossary->id)) {
  56      throw new \moodle_exception('invalidcoursemodule');
  57  }
  58  
  59  $context     = context_module::instance($cm->id);
  60  $maincontext = context_module::instance($maincm->id);
  61  
  62  if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
  63      throw new \moodle_exception('coursemisconf');
  64  }
  65  
  66  
  67  $strglossaries     = get_string('modulenameplural', 'glossary');
  68  $entryalreadyexist = get_string('entryalreadyexist','glossary');
  69  $entryexported     = get_string('entryexported','glossary');
  70  
  71  if (!$mainglossary->allowduplicatedentries) {
  72      if ($DB->record_exists_select('glossary_entries',
  73              'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
  74                  'glossaryid' => $mainglossary->id,
  75                  'concept'    => core_text::strtolower($entry->concept)))) {
  76          $PAGE->set_title($glossary->name);
  77          $PAGE->set_heading($course->fullname);
  78          echo $OUTPUT->header();
  79          echo $OUTPUT->notification(get_string('errconceptalreadyexists', 'glossary'));
  80          echo $OUTPUT->continue_button($returnurl);
  81          echo $OUTPUT->box_end();
  82          echo $OUTPUT->footer();
  83          die;
  84      }
  85  }
  86  
  87  if (!data_submitted() or !$confirm or !confirm_sesskey()) {
  88      $PAGE->set_title($glossary->name);
  89      $PAGE->set_heading($course->fullname);
  90      echo $OUTPUT->header();
  91      echo '<div class="boxaligncenter">';
  92      $areyousure = '<h2>'.format_string($entry->concept).'</h2><p align="center">'.get_string('areyousureexport','glossary').'<br /><b>'.format_string($mainglossary->name).'</b>?';
  93      $linkyes    = 'exportentry.php';
  94      $linkno     = 'view.php';
  95      $optionsyes = array('id'=>$entry->id, 'confirm'=>1, 'sesskey'=>sesskey(), 'prevmode'=>$prevmode, 'hook'=>$hook);
  96      $optionsno  = array('id'=>$cm->id, 'mode'=>$prevmode, 'hook'=>$hook);
  97  
  98      echo $OUTPUT->confirm($areyousure, new moodle_url($linkyes, $optionsyes), new moodle_url($linkno, $optionsno));
  99      echo '</div>';
 100      echo $OUTPUT->footer();
 101      die;
 102  
 103  } else {
 104      $entry->glossaryid       = $mainglossary->id;
 105      $entry->sourceglossaryid = $glossary->id;
 106  
 107      $DB->update_record('glossary_entries', $entry);
 108  
 109      // move attachments too
 110      $fs = get_file_storage();
 111  
 112      if ($oldfiles = $fs->get_area_files($context->id, 'mod_glossary', 'attachment', $entry->id)) {
 113          foreach ($oldfiles as $oldfile) {
 114              $file_record = new stdClass();
 115              $file_record->contextid = $maincontext->id;
 116              $fs->create_file_from_storedfile($file_record, $oldfile);
 117          }
 118          $fs->delete_area_files($context->id, 'mod_glossary', 'attachment', $entry->id);
 119          $entry->attachment = '1';
 120      } else {
 121          $entry->attachment = '0';
 122      }
 123      $DB->update_record('glossary_entries', $entry);
 124  
 125      redirect ($returnurl);
 126  }
 127