Implement data privacy provider.
[moodle-mod_attendance.git] / warnings.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17 /**
18 * Allows default warnings to be modified.
19 *
20 * @package mod_attendance
21 * @copyright 2017 Dan Marsden http://danmarsden.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25 require_once(__DIR__.'/../../config.php');
26 require_once($CFG->libdir.'/adminlib.php');
27 require_once($CFG->libdir.'/formslib.php');
28 require_once($CFG->dirroot.'/mod/attendance/lib.php');
29 require_once($CFG->dirroot.'/mod/attendance/locallib.php');
30
31 $action = optional_param('action', '', PARAM_ALPHA);
32 $notid = optional_param('notid', 0, PARAM_INT);
33 $id = optional_param('id', 0, PARAM_INT);
34
35 $url = new moodle_url('/mod/attendance/warnings.php');
36
37 // This page is used for configuring default set and for configuring attendance level set.
38 if (empty($id)) {
39 // This is the default status set - show appropriate admin stuff and check admin permissions.
40 admin_externalpage_setup('managemodules');
41
42 $output = $PAGE->get_renderer('mod_attendance');
43 echo $OUTPUT->header();
44 echo $OUTPUT->heading(get_string('defaultwarnings', 'mod_attendance'));
45 $tabmenu = attendance_print_settings_tabs('defaultwarnings');
46 echo $tabmenu;
47
48 } else {
49 // This is an attendance level config.
50 $cm = get_coursemodule_from_id('attendance', $id, 0, false, MUST_EXIST);
51 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
52 $att = $DB->get_record('attendance', array('id' => $cm->instance), '*', MUST_EXIST);
53
54 require_login($course, false, $cm);
55 $context = context_module::instance($cm->id);
56 require_capability('mod/attendance:changepreferences', $context);
57
58 $att = new mod_attendance_structure($att, $cm, $course, $PAGE->context);
59
60 $PAGE->set_url($url);
61 $PAGE->set_title($course->shortname. ": ".$att->name);
62 $PAGE->set_heading($course->fullname);
63 $PAGE->navbar->add($att->name);
64
65 $output = $PAGE->get_renderer('mod_attendance');
66 $tabs = new attendance_tabs($att, attendance_tabs::TAB_WARNINGS);
67 echo $output->header();
68 echo $output->heading(get_string('attendanceforthecourse', 'attendance').' :: ' .format_string($course->fullname));
69 echo $output->render($tabs);
70
71 }
72
73 $mform = new mod_attendance_add_warning_form($url, array('notid' => $notid, 'id' => $id));
74
75 if ($data = $mform->get_data()) {
76 if (empty($data->notid)) {
77 // Insert new record.
78 $notify = new stdClass();
79 if (empty($id)) {
80 $notify->idnumber = 0;
81 } else {
82 $notify->idnumber = $att->id;
83 }
84
85 $notify->warningpercent = $data->warningpercent;
86 $notify->warnafter = $data->warnafter;
87 $notify->maxwarn = $data->maxwarn;
88 $notify->emailuser = empty($data->emailuser) ? 0 : $data->emailuser;
89 $notify->emailsubject = $data->emailsubject;
90 $notify->emailcontent = $data->emailcontent['text'];
91 $notify->emailcontentformat = $data->emailcontent['format'];
92 $notify->thirdpartyemails = '';
93 if (!empty($data->thirdpartyemails)) {
94 $notify->thirdpartyemails = implode(',', $data->thirdpartyemails);
95 }
96 $existingrecord = $DB->record_exists('attendance_warning', array('idnumber' => $notify->idnumber,
97 'warningpercent' => $notify->warningpercent,
98 'warnafter' => $notify->warnafter));
99 if (empty($existingrecord)) {
100 $DB->insert_record('attendance_warning', $notify);
101 echo $OUTPUT->notification(get_string('warningupdated', 'mod_attendance'), 'success');
102 } else {
103 echo $OUTPUT->notification(get_string('warningfailed', 'mod_attendance'), 'warning');
104 }
105
106 } else {
107 $notify = $DB->get_record('attendance_warning', array('id' => $data->notid));
108 if (!empty($id) && $data->idnumber != $att->id) {
109 // Someone is trying to update a record for a different attendance.
110 print_error('invalidcoursemodule');
111 } else {
112 $notify = new stdClass();
113 $notify->id = $data->notid;
114 $notify->idnumber = $data->idnumber;
115 $notify->warningpercent = $data->warningpercent;
116 $notify->warnafter = $data->warnafter;
117 $notify->maxwarn = $data->maxwarn;
118 $notify->emailuser = empty($data->emailuser) ? 0 : $data->emailuser;
119 $notify->emailsubject = $data->emailsubject;
120 $notify->emailcontentformat = $data->emailcontent['format'];
121 $notify->emailcontent = $data->emailcontent['text'];
122 $notify->thirdpartyemails = '';
123 if (!empty($data->thirdpartyemails)) {
124 $notify->thirdpartyemails = implode(',', $data->thirdpartyemails);
125 }
126 $existingrecord = $DB->get_record('attendance_warning', array('idnumber' => $notify->idnumber,
127 'warningpercent' => $notify->warningpercent, 'warnafter' => $notify->warnafter));
128 if (empty($existingrecord) || $existingrecord->id == $notify->id) {
129 $DB->update_record('attendance_warning', $notify);
130 echo $OUTPUT->notification(get_string('warningupdated', 'mod_attendance'), 'success');
131 } else {
132 echo $OUTPUT->notification(get_string('warningfailed', 'mod_attendance'), 'error');
133 }
134 }
135 }
136 }
137 if ($action == 'delete' && !empty($notid)) {
138 if (!optional_param('confirm', false, PARAM_BOOL)) {
139 $cancelurl = $url;
140 $url->params(array('action' => 'delete', 'notid' => $notid, 'sesskey' => sesskey(), 'confirm' => true, 'id' => $id));
141 echo $OUTPUT->confirm(get_string('deletewarningconfirm', 'mod_attendance'), $url, $cancelurl);
142 echo $OUTPUT->footer();
143 exit;
144 } else {
145 require_sesskey();
146 $params = array('id' => $notid);
147 if (!empty($att)) {
148 // Add id/level to array.
149 $params['idnumber'] = $att->id;
150 }
151 $DB->delete_records('attendance_warning', $params);
152 echo $OUTPUT->notification(get_string('warningdeleted', 'mod_attendance'), 'success');
153 }
154 }
155 if ($action == 'update' && !empty($notid)) {
156 $existing = $DB->get_record('attendance_warning', array('id' => $notid));
157 $content = $existing->emailcontent;
158 $existing->emailcontent = array();
159 $existing->emailcontent['text'] = $content;
160 $existing->emailcontent['format'] = $existing->emailcontentformat;
161 $existing->notid = $existing->id;
162 $existing->id = $id;
163 $mform->set_data($existing);
164 $mform->display();
165 } else if ($action == 'add' && confirm_sesskey()) {
166 $mform->display();
167 } else {
168 if (empty($id)) {
169 $warningdesc = get_string('warningdesc', 'mod_attendance');
170 $idnumber = 0;
171 } else {
172 $warningdesc = get_string('warningdesc_course', 'mod_attendance');
173 $idnumber = $att->id;
174 }
175 echo $OUTPUT->box($warningdesc, 'generalbox attendancedesc', 'notice');
176 $existingnotifications = $DB->get_records('attendance_warning',
177 array('idnumber' => $idnumber),
178 'warningpercent');
179
180 if (!empty($existingnotifications)) {
181 $table = new html_table();
182 $table->head = array(get_string('warningthreshold', 'mod_attendance'),
183 get_string('numsessions', 'mod_attendance'),
184 get_string('emailsubject', 'mod_attendance'),
185 '');
186 foreach ($existingnotifications as $notification) {
187 $url->params(array('action' => 'delete', 'notid' => $notification->id, 'id' => $id));
188 $actionbuttons = $OUTPUT->action_icon($url, new pix_icon('t/delete',
189 get_string('delete', 'attendance')), null, null);
190 $url->params(array('action' => 'update', 'notid' => $notification->id, 'id' => $id));
191 $actionbuttons .= $OUTPUT->action_icon($url, new pix_icon('t/edit',
192 get_string('update', 'attendance')), null, null);
193 $table->data[] = array($notification->warningpercent, $notification->warnafter,
194 $notification->emailsubject, $actionbuttons);
195 }
196 echo html_writer::table($table);
197 }
198 $addurl = new moodle_url('/mod/attendance/warnings.php', array('action' => 'add', 'id' => $id));
199 echo $OUTPUT->single_button($addurl, get_string('addwarning', 'mod_attendance'));
200
201 }
202
203 echo $OUTPUT->footer();