Merge branch 'install_26_STABLE' of https://git.in.moodle.com/amosbot/moodle-install...
[moodle.git] / mod / glossary / deleteentry.php
blob5532617254463c12ba5d887421e23a55a614a1a3
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 if ($entry->sourceglossaryid) {
67 if (!$newcm = get_coursemodule_from_instance('glossary', $entry->sourceglossaryid)) {
68 print_error('invalidcoursemodule');
70 $newcontext = context_module::instance($newcm->id);
72 $entry->glossaryid = $entry->sourceglossaryid;
73 $entry->sourceglossaryid = 0;
74 $DB->update_record('glossary_entries', $entry);
76 // move attachments too
77 $fs = get_file_storage();
79 if ($oldfiles = $fs->get_area_files($context->id, 'mod_glossary', 'attachment', $entry->id)) {
80 foreach ($oldfiles as $oldfile) {
81 $file_record = new stdClass();
82 $file_record->contextid = $newcontext->id;
83 $fs->create_file_from_storedfile($file_record, $oldfile);
85 $fs->delete_area_files($context->id, 'mod_glossary', 'attachment', $entry->id);
86 $entry->attachment = '1';
87 } else {
88 $entry->attachment = '0';
90 $DB->update_record('glossary_entries', $entry);
92 } else {
93 $fs = get_file_storage();
94 $fs->delete_area_files($context->id, 'mod_glossary', 'attachment', $entry->id);
95 $DB->delete_records("comments", array('itemid'=>$entry->id, 'commentarea'=>'glossary_entry', 'contextid'=>$context->id));
96 $DB->delete_records("glossary_alias", array("entryid"=>$entry->id));
97 $DB->delete_records("glossary_entries", array("id"=>$entry->id));
99 // Update completion state
100 $completion = new completion_info($course);
101 if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $glossary->completionentries) {
102 $completion->update_state($cm, COMPLETION_INCOMPLETE, $entry->userid);
105 //delete glossary entry ratings
106 require_once($CFG->dirroot.'/rating/lib.php');
107 $delopt = new stdClass;
108 $delopt->contextid = $context->id;
109 $delopt->component = 'mod_glossary';
110 $delopt->ratingarea = 'entry';
111 $delopt->itemid = $entry->id;
112 $rm = new rating_manager();
113 $rm->delete_ratings($delopt);
116 add_to_log($course->id, "glossary", "delete entry", "view.php?id=$cm->id&amp;mode=$prevmode&amp;hook=$hook", $entry->id,$cm->id);
118 // Delete cached RSS feeds.
119 if (!empty($CFG->enablerssfeeds)) {
120 require_once($CFG->dirroot.'/mod/glossary/rsslib.php');
121 glossary_rss_delete_file($glossary);
124 redirect("view.php?id=$cm->id&amp;mode=$prevmode&amp;hook=$hook");
126 } else { // the operation has not been confirmed yet so ask the user to do so
127 $PAGE->navbar->add(get_string('delete'));
128 $PAGE->set_title($glossary->name);
129 $PAGE->set_heading($course->fullname);
130 echo $OUTPUT->header();
131 $areyousure = "<b>".format_string($entry->concept)."</b><p>$strareyousuredelete</p>";
132 $linkyes = 'deleteentry.php';
133 $linkno = 'view.php';
134 $optionsyes = array('id'=>$cm->id, 'entry'=>$entry->id, 'confirm'=>1, 'sesskey'=>sesskey(), 'prevmode'=>$prevmode, 'hook'=>$hook);
135 $optionsno = array('id'=>$cm->id, 'mode'=>$prevmode, 'hook'=>$hook);
137 echo $OUTPUT->confirm($areyousure, new moodle_url($linkyes, $optionsyes), new moodle_url($linkno, $optionsno));
139 echo $OUTPUT->footer();