forked from tlock/block_inactiveuseralert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.php
127 lines (107 loc) · 4.46 KB
/
alert.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once(dirname(__FILE__).'/../../config.php');
$courseid = required_param('course', PARAM_INT);
$type = optional_param('alerttype', 'login', PARAM_ALPHA);
$alertid = optional_param('id', 0, PARAM_INT);
$delete = optional_param('delete', false, PARAM_BOOL);
$confirm = optional_param('confirm', false, PARAM_BOOL);
$context = context_course::instance($courseid);
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
require_login($course);
require_capability('block/inactiveuseralert:addinstance', $context);
$PAGE->set_context($context);
$pagename = get_string('addalert', 'block_inactiveuseralert');
$title = format_string($course->fullname) . ": $pagename";
$pageurl = new moodle_url('/blocks/inactiveuseralert/alert.php', array('course' => $courseid));
$PAGE->navbar->add(get_string('pluginname', 'block_inactiveuseralert'));
$PAGE->navbar->add($pagename, $pageurl);
$PAGE->set_pagelayout('report');
$PAGE->set_url($pageurl);
$PAGE->set_title(get_string('course') . ': ' . $course->fullname);
$PAGE->set_heading($title);
$activities = [];
if ($type == 'activity') {
$modinfo = get_fast_modinfo($course);
$mods = $modinfo->get_cms();
foreach ($mods as $mod) {
if ($mod->completion) {
$activities[$mod->id] = $mod->name;
}
}
asort($activities);
if (empty($activities)) {
print_error('errornoactivitieswithcompletion', 'block_inactiveuseralert');
}
}
$returnurl = new moodle_url('/course/view.php', ['id' => $courseid]);
if ($biid = $DB->get_field('block_instances', 'id', ['blockname' => 'inactiveuseralert', 'parentcontextid' => $context->id])) {
$returnurl->param('bui_editid', $biid);
$returnurl->param('sesskey', sesskey());
}
$alert = new \block_inactiveuseralert\alert();
$alert->course = $courseid;
$alert->alerttype = $type;
$alert->template = get_config('block_inactiveuseralert', 'defaulttemplate'.$type);
if (!empty($alertid)) {
$alert = \block_inactiveuseralert\alert::instance($alertid);
} else if ($type == 'login') {
$alert = \block_inactiveuseralert\alert::fetch_login_alert($courseid);
}
// Ensure no one is trying to edit an alert that belongs to another course.
if ($courseid != $alert->course) {
print_error('errorcoursemismatch', 'block_inactiveuseralert');
}
if ($delete && !empty($alert->id)) {
if ($confirm) {
$alert->delete();
redirect($returnurl);
}
echo $OUTPUT->header();
$url = new moodle_url('/blocks/inactiveuseralert/alert.php', ['course' => $courseid, 'delete' => 1, 'id' => $alertid, 'confirm' => 1]);
if (!$DB->record_exists('course_modules', array('course' => $alert->course, 'id' => $alert->cmid))) {
echo $OUTPUT->confirm(get_string('deleteconfirminvalidalert', 'block_inactiveuseralert'), $url, $returnurl);
} else {
echo $OUTPUT->confirm(get_string('deleteconfirm', 'block_inactiveuseralert', $activities[$alert->cmid]), $url, $returnurl);
}
echo $OUTPUT->footer();
exit;
}
$data = $alert->data_for_form();
$data->course = $courseid;
$actoptions = $activities;
if (empty($alertid) && $type == 'activity') {
$actoptions = \block_inactiveuseralert\helper::get_cms_without_alert($modinfo, $courseid);
}
$mform = new \block_inactiveuseralert\form\alert(null,
array('type' => $type, 'course' => $course, 'activities' => $actoptions, 'alertid' => $alertid, 'alert' => $alert));
$mform->set_data($data);
if ($mform->is_cancelled()) {
redirect($returnurl);
} else if ($data = $mform->get_data()) {
foreach ($data as $key => $value) {
if (substr($key, 0, 5) == 'alert') {
$alert->set_alert_time(substr($key, 5), $value);
continue;
}
$alert->$key = $value;
}
$alert->save();
redirect($returnurl);
}
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();