/mod/glossary/exportentry.php

https://github.com/markn86/moodle · PHP · 127 lines · 99 code · 25 blank · 3 comment · 17 complexity · 0c42dadf0c9baabed48e99e1404b24a0 MD5 · raw file

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