Merge branch 'MDL-62384-33' of git://github.com/andrewnicols/moodle into MOODLE_33_STABLE
[moodle.git] / mod / glossary / deleteentry.php
blob8cacbb373ebb4f3de5b3d65937c0b34f91e373a9
1 <?php
3 require_once("../../config.php");
4 require_once("lib.php");
6 $id = required_param('id', PARAM_INT); // course module ID
7 $confirm = optional_param('confirm', 0, PARAM_INT); // commit the operation?
8 $entry = optional_param('entry', 0, PARAM_INT); // entry id
9 $prevmode = required_param('prevmode', PARAM_ALPHA);
10 $hook = optional_param('hook', '', PARAM_CLEAN);
12 $url = new moodle_url('/mod/glossary/deleteentry.php', array('id'=>$id,'prevmode'=>$prevmode));
13 if ($confirm !== 0) {
14 $url->param('confirm', $confirm);
16 if ($entry !== 0) {
17 $url->param('entry', $entry);
19 if ($hook !== '') {
20 $url->param('hook', $hook);
22 $PAGE->set_url($url);
24 $strglossary = get_string("modulename", "glossary");
25 $strglossaries = get_string("modulenameplural", "glossary");
26 $stredit = get_string("edit");
27 $entrydeleted = get_string("entrydeleted","glossary");
30 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
31 print_error("invalidcoursemodule");
34 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
35 print_error('coursemisconf');
38 if (! $entry = $DB->get_record("glossary_entries", array("id"=>$entry))) {
39 print_error('invalidentry');
42 require_login($course, false, $cm);
43 $context = context_module::instance($cm->id);
44 $manageentries = has_capability('mod/glossary:manageentries', $context);
46 if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
47 print_error('invalidid', 'glossary');
51 $strareyousuredelete = get_string("areyousuredelete","glossary");
53 if (($entry->userid != $USER->id) and !$manageentries) { // guest id is never matched, no need for special check here
54 print_error('nopermissiontodelentry');
56 $ineditperiod = ((time() - $entry->timecreated < $CFG->maxeditingtime) || $glossary->editalways);
57 if (!$ineditperiod and !$manageentries) {
58 print_error('errdeltimeexpired', 'glossary');
61 /// If data submitted, then process and store.
63 if ($confirm and confirm_sesskey()) { // the operation was confirmed.
64 // if it is an imported entry, just delete the relation
66 $origentry = fullclone($entry);
67 if ($entry->sourceglossaryid) {
68 if (!$newcm = get_coursemodule_from_instance('glossary', $entry->sourceglossaryid)) {
69 print_error('invalidcoursemodule');
71 $newcontext = context_module::instance($newcm->id);
73 $entry->glossaryid = $entry->sourceglossaryid;
74 $entry->sourceglossaryid = 0;
75 $DB->update_record('glossary_entries', $entry);
77 // move attachments too
78 $fs = get_file_storage();
80 if ($oldfiles = $fs->get_area_files($context->id, 'mod_glossary', 'attachment', $entry->id)) {
81 foreach ($oldfiles as $oldfile) {
82 $file_record = new stdClass();
83 $file_record->contextid = $newcontext->id;
84 $fs->create_file_from_storedfile($file_record, $oldfile);
86 $fs->delete_area_files($context->id, 'mod_glossary', 'attachment', $entry->id);
87 $entry->attachment = '1';
88 } else {
89 $entry->attachment = '0';
91 $DB->update_record('glossary_entries', $entry);
93 } else {
94 $fs = get_file_storage();
95 $fs->delete_area_files($context->id, 'mod_glossary', 'attachment', $entry->id);
96 $DB->delete_records("comments", array('itemid'=>$entry->id, 'commentarea'=>'glossary_entry', 'contextid'=>$context->id));
97 $DB->delete_records("glossary_alias", array("entryid"=>$entry->id));
98 $DB->delete_records("glossary_entries", array("id"=>$entry->id));
100 // Update completion state
101 $completion = new completion_info($course);
102 if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $glossary->completionentries) {
103 $completion->update_state($cm, COMPLETION_INCOMPLETE, $entry->userid);
106 //delete glossary entry ratings
107 require_once($CFG->dirroot.'/rating/lib.php');
108 $delopt = new stdClass;
109 $delopt->contextid = $context->id;
110 $delopt->component = 'mod_glossary';
111 $delopt->ratingarea = 'entry';
112 $delopt->itemid = $entry->id;
113 $rm = new rating_manager();
114 $rm->delete_ratings($delopt);
117 // Delete cached RSS feeds.
118 if (!empty($CFG->enablerssfeeds)) {
119 require_once($CFG->dirroot.'/mod/glossary/rsslib.php');
120 glossary_rss_delete_file($glossary);
123 core_tag_tag::remove_all_item_tags('mod_glossary', 'glossary_entries', $origentry->id);
125 $event = \mod_glossary\event\entry_deleted::create(array(
126 'context' => $context,
127 'objectid' => $origentry->id,
128 'other' => array(
129 'mode' => $prevmode,
130 'hook' => $hook,
131 'concept' => $origentry->concept
134 $event->add_record_snapshot('glossary_entries', $origentry);
135 $event->trigger();
137 // Reset caches.
138 if ($entry->usedynalink and $entry->approved) {
139 \mod_glossary\local\concept_cache::reset_glossary($glossary);
142 redirect("view.php?id=$cm->id&amp;mode=$prevmode&amp;hook=$hook");
144 } else { // the operation has not been confirmed yet so ask the user to do so
145 $PAGE->navbar->add(get_string('delete'));
146 $PAGE->set_title($glossary->name);
147 $PAGE->set_heading($course->fullname);
148 echo $OUTPUT->header();
149 $areyousure = "<b>".format_string($entry->concept)."</b><p>$strareyousuredelete</p>";
150 $linkyes = 'deleteentry.php';
151 $linkno = 'view.php';
152 $optionsyes = array('id'=>$cm->id, 'entry'=>$entry->id, 'confirm'=>1, 'sesskey'=>sesskey(), 'prevmode'=>$prevmode, 'hook'=>$hook);
153 $optionsno = array('id'=>$cm->id, 'mode'=>$prevmode, 'hook'=>$hook);
155 echo $OUTPUT->confirm($areyousure, new moodle_url($linkyes, $optionsyes), new moodle_url($linkno, $optionsno));
157 echo $OUTPUT->footer();