2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Allows default warnings to be modified.
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
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');
31 $action = optional_param('action', '', PARAM_ALPHA
);
32 $notid = optional_param('notid', 0, PARAM_INT
);
33 $id = optional_param('id', 0, PARAM_INT
);
35 $url = new moodle_url('/mod/attendance/warnings.php');
37 // This page is used for configuring default set and for configuring attendance level set.
39 // This is the default status set - show appropriate admin stuff and check admin permissions.
40 admin_externalpage_setup('managemodules');
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');
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
);
54 require_login($course, false
, $cm);
55 $context = context_module
::instance($cm->id
);
56 require_capability('mod/attendance:changepreferences', $context);
58 $att = new mod_attendance_structure($att, $cm, $course, $PAGE->context
);
61 $PAGE->set_title($course->shortname
. ": ".$att->name
);
62 $PAGE->set_heading($course->fullname
);
63 $PAGE->navbar
->add($att->name
);
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);
73 $mform = new mod_attendance_add_warning_form($url, array('notid' => $notid, 'id' => $id));
75 if ($data = $mform->get_data()) {
76 if (empty($data->notid
)) {
78 $notify = new stdClass();
80 $notify->idnumber
= 0;
82 $notify->idnumber
= $att->id
;
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
);
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');
103 echo $OUTPUT->notification(get_string('warningfailed', 'mod_attendance'), 'warning');
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');
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
);
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');
132 echo $OUTPUT->notification(get_string('warningfailed', 'mod_attendance'), 'error');
137 if ($action == 'delete' && !empty($notid)) {
138 if (!optional_param('confirm', false
, PARAM_BOOL
)) {
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();
146 $params = array('id' => $notid);
148 // Add id/level to array.
149 $params['idnumber'] = $att->id
;
151 $DB->delete_records('attendance_warning', $params);
152 echo $OUTPUT->notification(get_string('warningdeleted', 'mod_attendance'), 'success');
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
;
163 $mform->set_data($existing);
165 } else if ($action == 'add' && confirm_sesskey()) {
169 $warningdesc = get_string('warningdesc', 'mod_attendance');
172 $warningdesc = get_string('warningdesc_course', 'mod_attendance');
173 $idnumber = $att->id
;
175 echo $OUTPUT->box($warningdesc, 'generalbox attendancedesc', 'notice');
176 $existingnotifications = $DB->get_records('attendance_warning',
177 array('idnumber' => $idnumber),
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'),
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);
196 echo html_writer
::table($table);
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'));
203 echo $OUTPUT->footer();