Merge branch 'MDL-81073' of https://github.com/paulholden/moodle
[moodle.git] / mod / glossary / exportentry.php
blob0b345dab432337c7a64936c7c3b6a4ebe6d57b63
1 <?php
3 require_once('../../config.php');
4 require_once('lib.php');
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);
11 $url = new moodle_url('/mod/glossary/exportentry.php', array('id'=>$id,'prevmode'=>$prevmode));
12 if ($confirm !== 0) {
13 $url->param('confirm', $confirm);
15 if ($hook !== 'ALL') {
16 $url->param('hook', $hook);
18 $PAGE->set_url($url);
20 if (!$entry = $DB->get_record('glossary_entries', array('id'=>$id))) {
21 throw new \moodle_exception('invalidentry');
24 if ($entry->sourceglossaryid) {
25 //already exported
26 if (!$cm = get_coursemodule_from_id('glossary', $entry->sourceglossaryid)) {
27 throw new \moodle_exception('invalidcoursemodule');
29 redirect('view.php?id='.$cm->id.'&amp;mode=entry&amp;hook='.$entry->id);
32 if (!$cm = get_coursemodule_from_instance('glossary', $entry->glossaryid)) {
33 throw new \moodle_exception('invalidcoursemodule');
36 if (!$glossary = $DB->get_record('glossary', array('id'=>$cm->instance))) {
37 throw new \moodle_exception('invalidid', 'glossary');
40 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
41 throw new \moodle_exception('coursemisconf');
44 require_course_login($course->id, true, $cm);
45 $context = context_module::instance($cm->id);
46 require_capability('mod/glossary:export', $context);
48 $returnurl = "view.php?id=$cm->id&amp;mode=$prevmode&amp;hook=".urlencode($hook);
50 if (!$mainglossary = $DB->get_record('glossary', array('course'=>$cm->course, 'mainglossary'=>1))) {
51 //main glossary not present
52 redirect($returnurl);
55 if (!$maincm = get_coursemodule_from_instance('glossary', $mainglossary->id)) {
56 throw new \moodle_exception('invalidcoursemodule');
59 $context = context_module::instance($cm->id);
60 $maincontext = context_module::instance($maincm->id);
62 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
63 throw new \moodle_exception('coursemisconf');
67 $strglossaries = get_string('modulenameplural', 'glossary');
68 $entryalreadyexist = get_string('entryalreadyexist','glossary');
69 $entryexported = get_string('entryexported','glossary');
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;
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);
98 echo $OUTPUT->confirm($areyousure, new moodle_url($linkyes, $optionsyes), new moodle_url($linkno, $optionsno));
99 echo '</div>';
100 echo $OUTPUT->footer();
101 die;
103 } else {
104 $entry->glossaryid = $mainglossary->id;
105 $entry->sourceglossaryid = $glossary->id;
107 $DB->update_record('glossary_entries', $entry);
109 // move attachments too
110 $fs = get_file_storage();
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);
118 $fs->delete_area_files($context->id, 'mod_glossary', 'attachment', $entry->id);
119 $entry->attachment = '1';
120 } else {
121 $entry->attachment = '0';
123 $DB->update_record('glossary_entries', $entry);
125 redirect ($returnurl);