MDL-63806 glossary: Move delete code to API function
[moodle.git] / mod / glossary / deleteentry.php
CommitLineData
5fa0208e 1<?php
2
3require_once("../../config.php");
4require_once("lib.php");
5
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);
11
a6855934 12$url = new moodle_url('/mod/glossary/deleteentry.php', array('id'=>$id,'prevmode'=>$prevmode));
5fa0208e 13if ($confirm !== 0) {
14 $url->param('confirm', $confirm);
15}
16if ($entry !== 0) {
17 $url->param('entry', $entry);
18}
19if ($hook !== '') {
20 $url->param('hook', $hook);
21}
22$PAGE->set_url($url);
23
24$strglossary = get_string("modulename", "glossary");
25$strglossaries = get_string("modulenameplural", "glossary");
26$stredit = get_string("edit");
27$entrydeleted = get_string("entrydeleted","glossary");
28
29
30if (! $cm = get_coursemodule_from_id('glossary', $id)) {
31 print_error("invalidcoursemodule");
32}
33
34if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
35 print_error('coursemisconf');
36}
37
38if (! $entry = $DB->get_record("glossary_entries", array("id"=>$entry))) {
39 print_error('invalidentry');
40}
41
043b38d5
DW
42// Permission checks are based on the course module instance so make sure it is correct.
43if ($cm->instance != $entry->glossaryid) {
44 print_error('invalidentry');
45}
46
cdbea7ee 47require_login($course, false, $cm);
e0a91e11 48$context = context_module::instance($cm->id);
5fa0208e 49
50if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
51 print_error('invalidid', 'glossary');
52}
53
0ab3fd79
JL
54// Throws an exception if the user cannot delete the entry.
55mod_glossary_can_delete_entry($entry, $glossary, $context, false);
07842023 56
5fa0208e 57/// If data submitted, then process and store.
bdb2f23f 58
5fa0208e 59if ($confirm and confirm_sesskey()) { // the operation was confirmed.
37625e2a 60
0ab3fd79 61 mod_glossary_delete_entry($entry, $glossary, $cm, $context, $course, $hook, $prevmode);
5fa0208e 62 redirect("view.php?id=$cm->id&amp;mode=$prevmode&amp;hook=$hook");
bdb2f23f 63
5fa0208e 64} else { // the operation has not been confirmed yet so ask the user to do so
0ab3fd79 65 $strareyousuredelete = get_string("areyousuredelete", "glossary");
2e4fd166 66 $PAGE->navbar->add(get_string('delete'));
8c6ca696 67 $PAGE->set_title($glossary->name);
2e4fd166 68 $PAGE->set_heading($course->fullname);
5fa0208e 69 echo $OUTPUT->header();
70 $areyousure = "<b>".format_string($entry->concept)."</b><p>$strareyousuredelete</p>";
71 $linkyes = 'deleteentry.php';
72 $linkno = 'view.php';
73 $optionsyes = array('id'=>$cm->id, 'entry'=>$entry->id, 'confirm'=>1, 'sesskey'=>sesskey(), 'prevmode'=>$prevmode, 'hook'=>$hook);
74 $optionsno = array('id'=>$cm->id, 'mode'=>$prevmode, 'hook'=>$hook);
49bcd737 75
5fa0208e 76 echo $OUTPUT->confirm($areyousure, new moodle_url($linkyes, $optionsyes), new moodle_url($linkno, $optionsno));
49bcd737 77
5fa0208e 78 echo $OUTPUT->footer();
79}