Browse Source
New feature/reports to allow warning thresholds to be set and e-mail notifications.MOODLE_32_STABLE
Dan Marsden
8 years ago
18 changed files with 1218 additions and 9 deletions
@ -0,0 +1,141 @@ |
|||||
|
<?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/>. |
||||
|
|
||||
|
/** |
||||
|
* Attendance course summary report. |
||||
|
* |
||||
|
* @package mod_attendance |
||||
|
* @copyright 2017 onwards Dan Marsden http://danmarsden.com |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
|
||||
|
require_once('../../config.php'); |
||||
|
require_once($CFG->dirroot.'/mod/attendance/lib.php'); |
||||
|
require_once($CFG->dirroot.'/mod/attendance/locallib.php'); |
||||
|
require_once($CFG->libdir.'/tablelib.php'); |
||||
|
require_once($CFG->libdir.'/coursecatlib.php'); |
||||
|
|
||||
|
$category = optional_param('category', 0, PARAM_INT); |
||||
|
$attendancecm = optional_param('id', 0, PARAM_INT); |
||||
|
$download = optional_param('download', '', PARAM_ALPHA); |
||||
|
$sort = optional_param('tsort', '', PARAM_ALPHA); |
||||
|
|
||||
|
if (!empty($category)) { |
||||
|
$context = context_coursecat::instance($category); |
||||
|
$coursecat = coursecat::get($category); |
||||
|
$courses = $coursecat->get_courses(array('recursive' => true, 'idonly' => true)); |
||||
|
$PAGE->set_category_by_id($category); |
||||
|
require_login(); |
||||
|
} else if (!empty($attendancecm)) { |
||||
|
$cm = get_coursemodule_from_id('attendance', $attendancecm, 0, false, MUST_EXIST); |
||||
|
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); |
||||
|
$att = $DB->get_record('attendance', array('id' => $cm->instance), '*', MUST_EXIST); |
||||
|
$courses = array($course->id); |
||||
|
$context = context_module::instance($cm->id); |
||||
|
require_login($course, false, $cm); |
||||
|
} else { |
||||
|
$context = context_system::instance(); |
||||
|
$courses = array(); // Show all courses. |
||||
|
$PAGE->set_context($context); |
||||
|
require_login(); |
||||
|
} |
||||
|
// Check permissions. |
||||
|
require_capability('mod/attendance:viewreports', $context); |
||||
|
|
||||
|
$exportfilename = 'attendanceatrisk.csv'; |
||||
|
|
||||
|
$PAGE->set_url('/mod/attendance/atrisk.php', array('category' => $category)); |
||||
|
|
||||
|
$PAGE->set_heading($SITE->fullname); |
||||
|
|
||||
|
$table = new flexible_table('attendanceatrisk'); |
||||
|
$table->define_baseurl($PAGE->url); |
||||
|
|
||||
|
if (!$table->is_downloading($download, $exportfilename)) { |
||||
|
if (!empty($attendancecm)) { |
||||
|
$pageparams = new mod_attendance_sessions_page_params(); |
||||
|
$att = new mod_attendance_structure($att, $cm, $course, $context, $pageparams); |
||||
|
$output = $PAGE->get_renderer('mod_attendance'); |
||||
|
$tabs = new attendance_tabs($att, attendance_tabs::TAB_ATRISK); |
||||
|
echo $output->header(); |
||||
|
echo $output->heading(get_string('attendanceforthecourse', 'attendance').' :: ' .format_string($course->fullname)); |
||||
|
echo $output->render($tabs); |
||||
|
} else { |
||||
|
echo $OUTPUT->header(); |
||||
|
echo $OUTPUT->heading(get_string('atriskreport', 'mod_attendance')); |
||||
|
if (empty($category)) { |
||||
|
// Only show tabs if displaying via the admin page. |
||||
|
$tabmenu = attendance_print_settings_tabs('atrisk'); |
||||
|
echo $tabmenu; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
$table->define_columns(array('course', 'attendance', 'userid', 'numtakensessions', 'percent', 'timesent')); |
||||
|
$table->define_headers(array(get_string('course'), |
||||
|
get_string('pluginname', 'attendance'), |
||||
|
get_string('user'), |
||||
|
get_string('takensessions', 'attendance'), |
||||
|
get_string('averageattendance', 'attendance'), |
||||
|
get_string('triggered', 'attendance'))); |
||||
|
$table->sortable(true); |
||||
|
$table->no_sorting('course'); |
||||
|
$table->set_attribute('cellspacing', '0'); |
||||
|
$table->set_attribute('class', 'generaltable generalbox'); |
||||
|
$table->show_download_buttons_at(array(TABLE_P_BOTTOM)); |
||||
|
$table->setup(); |
||||
|
|
||||
|
// Work out direction of sort required. |
||||
|
$sortcolumns = $table->get_sort_columns(); |
||||
|
// Now do sorting if specified. |
||||
|
|
||||
|
$orderby = ' ORDER BY percent ASC'; |
||||
|
if (!empty($sort)) { |
||||
|
$direction = ' DESC'; |
||||
|
if (!empty($sortcolumns[$sort]) && $sortcolumns[$sort] == SORT_ASC) { |
||||
|
$direction = ' ASC'; |
||||
|
} |
||||
|
$orderby = " ORDER BY $sort $direction"; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
$records = attendance_get_users_to_notify($courses, $orderby); |
||||
|
foreach ($records as $record) { |
||||
|
if (!$table->is_downloading($download, $exportfilename)) { |
||||
|
$url = new moodle_url('/mod/attendance/index.php', array('id' => $record->courseid)); |
||||
|
$name = html_writer::link($url, $record->coursename); |
||||
|
} else { |
||||
|
$name = $record->coursename; |
||||
|
} |
||||
|
$url = new moodle_url('/mod/attendance/view.php', array('studentid' => $record->userid, |
||||
|
'id' => $record->cmid, 'view' => ATT_VIEW_ALL)); |
||||
|
$attendancename = html_writer::link($url, $record->aname); |
||||
|
|
||||
|
$username = html_writer::link($url, fullname($record)); |
||||
|
$percent = round($record->percent * 100)."%"; |
||||
|
$timesent = "-"; |
||||
|
if (!empty($record->timesent)) { |
||||
|
$timesent = userdate($record->timesent); |
||||
|
} |
||||
|
|
||||
|
$table->add_data(array($name, $attendancename, $username, $record->numtakensessions, $percent, $timesent)); |
||||
|
} |
||||
|
$table->finish_output(); |
||||
|
|
||||
|
if (!$table->is_downloading()) { |
||||
|
echo $OUTPUT->footer(); |
||||
|
} |
@ -0,0 +1,108 @@ |
|||||
|
<?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/>. |
||||
|
|
||||
|
/** |
||||
|
* Contains class mod_attendance_add_warning_form |
||||
|
* |
||||
|
* @package mod_attendance |
||||
|
* @copyright 2017 Dan Marsden |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
|
||||
|
defined('MOODLE_INTERNAL') || die(); |
||||
|
|
||||
|
/** |
||||
|
* Class mod_attendance_add_warning_form |
||||
|
* |
||||
|
* @package mod_attendance |
||||
|
* @copyright 2017 Dan Marsden |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
class mod_attendance_add_warning_form extends moodleform { |
||||
|
/** |
||||
|
* Form definition |
||||
|
*/ |
||||
|
public function definition() { |
||||
|
global $COURSE; |
||||
|
$mform = $this->_form; |
||||
|
|
||||
|
// Load global defaults. |
||||
|
$config = get_config('attendance'); |
||||
|
|
||||
|
$options = array(); |
||||
|
for ($i = 1; $i <= 100; $i++) { |
||||
|
$options[$i] = "$i%"; |
||||
|
} |
||||
|
$mform->addElement('select', 'warningpercent', get_string('warningpercent', 'mod_attendance'), $options); |
||||
|
$mform->addHelpButton('warningpercent', 'warningpercent', 'mod_attendance'); |
||||
|
$mform->setType('warningpercent', PARAM_INT); |
||||
|
$mform->setDefault('warningpercent', $config->warningpercent); |
||||
|
|
||||
|
$options = array(); |
||||
|
for ($i = 1; $i <= 50; $i++) { |
||||
|
$options[$i] = "$i"; |
||||
|
} |
||||
|
$mform->addElement('select', 'warnafter', get_string('warnafter', 'mod_attendance'), $options); |
||||
|
$mform->addHelpButton('warnafter', 'warnafter', 'mod_attendance'); |
||||
|
$mform->setType('warnafter', PARAM_INT); |
||||
|
$mform->setDefault('warnafter', $config->warnafter); |
||||
|
|
||||
|
$mform->addElement('checkbox', 'emailuser', get_string('emailuser', 'mod_attendance')); |
||||
|
$mform->addHelpButton('emailuser', 'emailuser', 'mod_attendance'); |
||||
|
$mform->setDefault('emailuser', $config->emailuser); |
||||
|
|
||||
|
$mform->addElement('text', 'emailsubject', get_string('emailsubject', 'mod_attendance'), array('size' => '64')); |
||||
|
$mform->setType('emailsubject', PARAM_TEXT); |
||||
|
$mform->addRule('emailsubject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client'); |
||||
|
$mform->addHelpButton('emailsubject', 'emailsubject', 'mod_attendance'); |
||||
|
$mform->setDefault('emailsubject', $config->emailsubject); |
||||
|
|
||||
|
$mform->addElement('editor', 'emailcontent', get_string('emailcontent', 'mod_attendance'), null, null); |
||||
|
$mform->setDefault('emailcontent', array('text' => format_text($config->emailcontent))); |
||||
|
$mform->setType('emailcontent', PARAM_RAW); |
||||
|
$mform->addHelpButton('emailcontent', 'emailcontent', 'mod_attendance'); |
||||
|
|
||||
|
$users = get_users_by_capability(context_course::instance($COURSE->id), 'mod/attendance:viewreports'); |
||||
|
$options = array(); |
||||
|
foreach ($users as $user) { |
||||
|
$options[$user->id] = fullname($user); |
||||
|
} |
||||
|
|
||||
|
$select = $mform->addElement('searchableselector', 'thirdpartyemails', |
||||
|
get_string('thirdpartyemails', 'mod_attendance'), $options); |
||||
|
$mform->setType('thirdpartyemails', PARAM_TEXT); |
||||
|
$mform->addHelpButton('thirdpartyemails', 'thirdpartyemails', 'mod_attendance'); |
||||
|
$select->setMultiple(true); |
||||
|
|
||||
|
// Need to set hidden elements when adding default options. |
||||
|
$mform->addElement('hidden', 'idnumber', 0); // Default options use 0 as the idnumber. |
||||
|
$mform->setType('idnumber', PARAM_INT); |
||||
|
|
||||
|
$mform->addElement('hidden', 'notid', 0); // The id of warning record. |
||||
|
$mform->setType('notid', PARAM_INT); |
||||
|
|
||||
|
$mform->addElement('hidden', 'id', $this->_customdata['id']); // The id of course module record if attendance level. |
||||
|
$mform->setType('id', PARAM_INT); |
||||
|
|
||||
|
if (!empty($this->_customdata['notid'])) { |
||||
|
$btnstring = get_string('update', 'attendance'); |
||||
|
} else { |
||||
|
$btnstring = get_string('add', 'attendance'); |
||||
|
} |
||||
|
$this->add_action_buttons(true, $btnstring); |
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,122 @@ |
|||||
|
<?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/>. |
||||
|
|
||||
|
/** |
||||
|
* Attendance task - Send warnings. |
||||
|
* |
||||
|
* @package mod_attendance |
||||
|
* @copyright 2017 onwards Dan Marsden http://danmarsden.com |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
|
||||
|
namespace mod_attendance\task; |
||||
|
defined('MOODLE_INTERNAL') || die(); |
||||
|
|
||||
|
require_once($CFG->dirroot . '/mod/attendance/locallib.php'); |
||||
|
/** |
||||
|
* Task class |
||||
|
* |
||||
|
* @package mod_attendance |
||||
|
* @copyright 2017 onwards Dan Marsden http://danmarsden.com |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
class notify extends \core\task\scheduled_task { |
||||
|
public function get_name() { |
||||
|
// Shown in admin screens. |
||||
|
return get_string('notifytask', 'mod_attendance'); |
||||
|
} |
||||
|
public function execute() { |
||||
|
global $DB; |
||||
|
if (empty(get_config('attendance', 'enablewarnings'))) { |
||||
|
return; // Warnings not enabled. |
||||
|
} |
||||
|
$now = time(); // Store current time to use in queries so they all match nicely. |
||||
|
$lastrun = get_config('mod_attendance', 'notifylastrun'); |
||||
|
if (empty($lastrun)) { |
||||
|
$lastrun = 0; |
||||
|
} |
||||
|
|
||||
|
$orderby = 'ORDER BY cm.id, atl.studentid, n.warningpercent ASC'; |
||||
|
$records = attendance_get_users_to_notify(array(), $orderby, $lastrun, true); |
||||
|
$sentnotifications = array(); |
||||
|
$thirdpartynotifications = array(); |
||||
|
$numsentusers = 0; |
||||
|
$numsentthird = 0; |
||||
|
foreach ($records as $record) { |
||||
|
if (empty($sentnotifications[$record->userid])) { |
||||
|
$sentnotifications[$record->userid] = array(); |
||||
|
} |
||||
|
|
||||
|
if (!empty($record->emailuser)) { |
||||
|
// Only send one warning to this user from each attendance in this run. - flag any higher percent notifications as sent. |
||||
|
if (empty($sentnotifications[$record->userid]) || !in_array($record->aid, $sentnotifications[$record->userid])) { |
||||
|
// Convert variables in emailcontent. |
||||
|
$record = attendance_template_variables($record); |
||||
|
$user = $DB->get_record('user', array('id' => $record->userid)); |
||||
|
$from = \core_user::get_noreply_user(); |
||||
|
|
||||
|
$emailcontent = format_text($record->emailcontent, $record->emailcontentformat); |
||||
|
|
||||
|
email_to_user($user, $from, $record->emailsubject, $emailcontent, $emailcontent); |
||||
|
|
||||
|
$sentnotifications[$record->userid][] = $record->aid; |
||||
|
$numsentusers++; |
||||
|
} |
||||
|
} |
||||
|
// Only send one warning to this user from each attendance in this run. - flag any higher percent notifications as sent. |
||||
|
if (!empty($record->thirdpartyemails)) { |
||||
|
$sendto = explode(',', $record->thirdpartyemails); |
||||
|
$record->percent = round($record->percent * 100)."%"; |
||||
|
foreach ($sendto as $senduser) { |
||||
|
if (empty($thirdpartynotifications[$senduser])) { |
||||
|
$thirdpartynotifications[$senduser] = array(); |
||||
|
} |
||||
|
if (!isset($thirdpartynotifications[$senduser][$record->aid.'_'.$record->userid])) { |
||||
|
$thirdpartynotifications[$senduser][$record->aid.'_'.$record->userid] = get_string('thirdpartyemailtext', 'attendance', $record); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
$notify = new \stdClass(); |
||||
|
$notify->userid = $record->userid; |
||||
|
$notify->notifyid = $record->notifyid; |
||||
|
$notify->timesent = $now; |
||||
|
$DB->insert_record('attendance_warning_done', $notify); |
||||
|
} |
||||
|
if (!empty($numsentusers)) { |
||||
|
mtrace($numsentusers ." user emails sent"); |
||||
|
} |
||||
|
if (!empty($thirdpartynotifications)) { |
||||
|
foreach ($thirdpartynotifications as $sendid => $notifications) { |
||||
|
$user = $DB->get_record('user', array('id' => $sendid)); |
||||
|
$from = \core_user::get_noreply_user(); |
||||
|
|
||||
|
$emailcontent = implode("\n", $notifications); |
||||
|
$emailcontent .= "\n\n".get_string('thirdpartyemailtextfooter', 'attendance'); |
||||
|
$emailcontent = format_text($emailcontent); |
||||
|
$emailsubject = get_string('thirdpartyemailsubject', 'attendance'); |
||||
|
|
||||
|
email_to_user($user, $from, $emailsubject, $emailcontent, $emailcontent); |
||||
|
$numsentthird++; |
||||
|
} |
||||
|
if (!empty($numsentthird)) { |
||||
|
mtrace($numsentthird ." thirdparty emails sent"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
set_config('notifylastrun', $now, 'mod_attendance'); |
||||
|
} |
||||
|
} |
@ -0,0 +1,199 @@ |
|||||
|
<?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/>. |
||||
|
|
||||
|
/** |
||||
|
* Allows default warnings to be modified. |
||||
|
* |
||||
|
* @package mod_attendance |
||||
|
* @copyright 2017 Dan Marsden http://danmarsden.com |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
|
||||
|
require_once(__DIR__.'/../../config.php'); |
||||
|
require_once($CFG->libdir.'/adminlib.php'); |
||||
|
require_once($CFG->libdir.'/formslib.php'); |
||||
|
require_once($CFG->dirroot.'/mod/attendance/lib.php'); |
||||
|
require_once($CFG->dirroot.'/mod/attendance/locallib.php'); |
||||
|
|
||||
|
$action = optional_param('action', '', PARAM_ALPHA); |
||||
|
$notid = optional_param('notid', 0, PARAM_INT); |
||||
|
$id = optional_param('id', 0, PARAM_INT); |
||||
|
|
||||
|
$url = new moodle_url('/mod/attendance/warnings.php'); |
||||
|
|
||||
|
// This page is used for configuring default set and for configuring attendance level set. |
||||
|
if (empty($id)) { |
||||
|
// This is the default status set - show appropriate admin stuff and check admin permissions. |
||||
|
admin_externalpage_setup('managemodules'); |
||||
|
|
||||
|
$output = $PAGE->get_renderer('mod_attendance'); |
||||
|
echo $OUTPUT->header(); |
||||
|
echo $OUTPUT->heading(get_string('defaultwarnings', 'mod_attendance')); |
||||
|
$tabmenu = attendance_print_settings_tabs('defaultwarnings'); |
||||
|
echo $tabmenu; |
||||
|
|
||||
|
} else { |
||||
|
// This is an attendance level config. |
||||
|
$cm = get_coursemodule_from_id('attendance', $id, 0, false, MUST_EXIST); |
||||
|
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); |
||||
|
$att = $DB->get_record('attendance', array('id' => $cm->instance), '*', MUST_EXIST); |
||||
|
|
||||
|
require_login($course, false, $cm); |
||||
|
$context = context_module::instance($cm->id); |
||||
|
require_capability('mod/attendance:changepreferences', $context); |
||||
|
|
||||
|
$att = new mod_attendance_structure($att, $cm, $course, $PAGE->context); |
||||
|
|
||||
|
$PAGE->set_url($url); |
||||
|
$PAGE->set_title($course->shortname. ": ".$att->name); |
||||
|
$PAGE->set_heading($course->fullname); |
||||
|
$PAGE->navbar->add($att->name); |
||||
|
|
||||
|
$output = $PAGE->get_renderer('mod_attendance'); |
||||
|
$tabs = new attendance_tabs($att, attendance_tabs::TAB_WARNINGS); |
||||
|
echo $output->header(); |
||||
|
echo $output->heading(get_string('attendanceforthecourse', 'attendance').' :: ' .format_string($course->fullname)); |
||||
|
echo $output->render($tabs); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
$mform = new mod_attendance_add_warning_form($url, array('notid' => $notid, 'id' => $id)); |
||||
|
|
||||
|
if ($data = $mform->get_data()) { |
||||
|
if (empty($data->notid)) { |
||||
|
// Insert new record. |
||||
|
$notify = new stdClass(); |
||||
|
if (empty($id)) { |
||||
|
$notify->idnumber = 0; |
||||
|
} else { |
||||
|
$notify->idnumber = $cm->id; |
||||
|
} |
||||
|
|
||||
|
$notify->warningpercent = $data->warningpercent; |
||||
|
$notify->warnafter = $data->warnafter; |
||||
|
$notify->emailuser = empty($data->emailuser) ? 0 : $data->emailuser; |
||||
|
$notify->emailsubject = $data->emailsubject; |
||||
|
$notify->emailcontent = $data->emailcontent['text']; |
||||
|
$notify->emailcontentformat = $data->emailcontent['format']; |
||||
|
$notify->thirdpartyemails = ''; |
||||
|
if (!empty($data->thirdpartyemails)) { |
||||
|
$notify->thirdpartyemails = implode(',', $data->thirdpartyemails); |
||||
|
} |
||||
|
$existingrecord = $DB->record_exists('attendance_warning', array('idnumber' => $notify->idnumber, |
||||
|
'warningpercent' => $notify->warningpercent)); |
||||
|
if (empty($existingrecord)) { |
||||
|
$DB->insert_record('attendance_warning', $notify); |
||||
|
echo $OUTPUT->notification(get_string('warningupdated', 'mod_attendance'), 'success'); |
||||
|
} else { |
||||
|
echo $OUTPUT->notification(get_string('warningfailed', 'mod_attendance'), 'warning'); |
||||
|
} |
||||
|
|
||||
|
} else { |
||||
|
$notify = $DB->get_record('attendance_warning', array('id' => $data->notid)); |
||||
|
if (!empty($id) && $data->idnumber != $id) { |
||||
|
// Someone is trying to update a record for a different attendance. |
||||
|
print_error('invalidcoursemodule'); |
||||
|
} else { |
||||
|
$notify = new stdClass(); |
||||
|
$notify->id = $data->notid; |
||||
|
$notify->idnumber = $data->idnumber; |
||||
|
$notify->warningpercent = $data->warningpercent; |
||||
|
$notify->warnafter = $data->warnafter; |
||||
|
$notify->emailuser = empty($data->emailuser) ? 0 : $data->emailuser; |
||||
|
$notify->emailsubject = $data->emailsubject; |
||||
|
$notify->emailcontentformat = $data->emailcontent['format']; |
||||
|
$notify->emailcontent = $data->emailcontent['text']; |
||||
|
$notify->thirdpartyemails = ''; |
||||
|
if (!empty($data->thirdpartyemails)) { |
||||
|
$notify->thirdpartyemails = implode(',', $data->thirdpartyemails); |
||||
|
} |
||||
|
$existingrecord = $DB->get_record('attendance_warning', array('idnumber' => $notify->idnumber, |
||||
|
'warningpercent' => $notify->warningpercent)); |
||||
|
if (empty($existingrecord) || $existingrecord->id == $notify->id) { |
||||
|
$DB->update_record('attendance_warning', $notify); |
||||
|
echo $OUTPUT->notification(get_string('warningupdated', 'mod_attendance'), 'success'); |
||||
|
} else { |
||||
|
echo $OUTPUT->notification(get_string('warningfailed', 'mod_attendance'), 'error'); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
if ($action == 'delete' && !empty($notid)) { |
||||
|
if (!optional_param('confirm', false, PARAM_BOOL)) { |
||||
|
$cancelurl = $url; |
||||
|
$url->params(array('action' => 'delete', 'notid' => $notid, 'sesskey' => sesskey(), 'confirm' => true, 'id' => $id)); |
||||
|
echo $OUTPUT->confirm(get_string('deletewarningconfirm', 'mod_attendance'), $url, $cancelurl); |
||||
|
echo $OUTPUT->footer(); |
||||
|
exit; |
||||
|
} else { |
||||
|
require_sesskey(); |
||||
|
$params = array('id' => $notid); |
||||
|
if (!empty($id)) { |
||||
|
// Add id/level to array. |
||||
|
$params['idnumber'] = $cm->id; |
||||
|
} |
||||
|
$DB->delete_records('attendance_warning', $params); |
||||
|
echo $OUTPUT->notification(get_string('warningdeleted', 'mod_attendance'), 'success'); |
||||
|
} |
||||
|
} |
||||
|
if ($action == 'update' && !empty($notid)) { |
||||
|
$existing = $DB->get_record('attendance_warning', array('id' => $notid)); |
||||
|
$content = $existing->emailcontent; |
||||
|
$existing->emailcontent = array(); |
||||
|
$existing->emailcontent['text'] = $content; |
||||
|
$existing->emailcontent['format'] = $existing->emailcontentformat; |
||||
|
$existing->notid = $existing->id; |
||||
|
$existing->id = $id; |
||||
|
$mform->set_data($existing); |
||||
|
$mform->display(); |
||||
|
} else if ($action == 'add' && confirm_sesskey()) { |
||||
|
$mform->display(); |
||||
|
} else { |
||||
|
if (empty($id)) { |
||||
|
echo $OUTPUT->box(get_string('warningdesc', 'mod_attendance'), 'generalbox', 'notice'); |
||||
|
|
||||
|
$existingnotifications = $DB->get_records('attendance_warning', |
||||
|
array('idnumber' => 0), |
||||
|
'warningpercent'); |
||||
|
} else { |
||||
|
$existingnotifications = $DB->get_records('attendance_warning', |
||||
|
array('idnumber' => $cm->id), |
||||
|
'warningpercent'); |
||||
|
} |
||||
|
if (!empty($existingnotifications)) { |
||||
|
$table = new html_table(); |
||||
|
$table->head = array(get_string('warningthreshold', 'mod_attendance'), |
||||
|
get_string('numsessions', 'mod_attendance'), |
||||
|
get_string('emailsubject', 'mod_attendance'), |
||||
|
''); |
||||
|
foreach ($existingnotifications as $notification) { |
||||
|
$url->params(array('action' => 'delete', 'notid' => $notification->id, 'id' => $id)); |
||||
|
$actionbuttons = $OUTPUT->action_icon($url, new pix_icon('t/delete', |
||||
|
get_string('delete', 'attendance')), null, null); |
||||
|
$url->params(array('action' => 'update', 'notid' => $notification->id, 'id' => $id)); |
||||
|
$actionbuttons .= $OUTPUT->action_icon($url, new pix_icon('t/edit', |
||||
|
get_string('update', 'attendance')), null, null); |
||||
|
$table->data[] = array($notification->warningpercent, $notification->warnafter, |
||||
|
$notification->emailsubject, $actionbuttons); |
||||
|
} |
||||
|
echo html_writer::table($table); |
||||
|
} |
||||
|
$addurl = new moodle_url('/mod/attendance/warnings.php', array('action' => 'add', 'id' => $id)); |
||||
|
echo $OUTPUT->single_button($addurl, get_string('addwarning', 'mod_attendance')); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
echo $OUTPUT->footer(); |
Loading…
Reference in new issue