From 7246c2d14285a43e8945ab7eb2388eaba2e66f1e Mon Sep 17 00:00:00 2001 From: patrickslee Date: Sun, 2 Oct 2005 21:37:54 +0000 Subject: [PATCH] New feature: Notify teachers/students of expirying enrolments --- course/edit.html | 36 +++++++++++++++++ enrol/enrol.class.php | 54 ++++++++++++++++++++++++++ lang/en/help/expirynotify.html | 1 + lang/en/help/expirynotifystudents.html | 1 + lang/en/help/expirythreshold.html | 1 + lang/en/moodle.php | 22 +++++++++++ lib/db/mysql.php | 10 +++++ lib/db/mysql.sql | 3 ++ lib/db/postgres7.php | 10 +++++ lib/db/postgres7.sql | 5 ++- version.php | 2 +- 11 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 lang/en/help/expirynotify.html create mode 100644 lang/en/help/expirynotifystudents.html create mode 100644 lang/en/help/expirythreshold.html diff --git a/course/edit.html b/course/edit.html index 6154fa5d6b1..4c5be51f67a 100644 --- a/course/edit.html +++ b/course/edit.html @@ -18,6 +18,15 @@ if (!isset($form->enrolperiod)) { $form->enrolperiod = 0; } + if (!isset($form->expirynotify)) { + $form->expirynotify = 1; + } + if (!isset($form->notifystudents)) { + $form->notifystudents = 0; + } + if (!isset($form->expirythreshold)) { + $form->expirythreshold = 10 * 86400; + } if (!isset($form->metacourse)) { $form->metacourse = 0; } @@ -98,6 +107,33 @@ helpbutton("enrolperiod", get_string("enrolperiod")); ?> + + : + + expirynotify", ""); + helpbutton("expirynotify", get_string("expirynotify")); + + echo '    '; + print_string('expirynotifystudents'); + echo ': '; + choose_from_menu ($choices, "notifystudents", "$form->notifystudents", ""); + helpbutton("expirynotifystudents", get_string("expirynotifystudents")); + + echo '    '; + print_string('expirythreshold'); + echo ': '; + for ($i=1; $i<=30; $i++) { + $seconds = $i * 86400; + $thresholdmenu[$seconds] = get_string('numdays', '', $i); + } + choose_from_menu ($thresholdmenu, "expirythreshold", "$form->expirythreshold", ""); + helpbutton("expirythreshold", get_string("expirythreshold")); + ?> + : diff --git a/enrol/enrol.class.php b/enrol/enrol.class.php index b5cd8f59f01..eec81d45480 100644 --- a/enrol/enrol.class.php +++ b/enrol/enrol.class.php @@ -350,6 +350,60 @@ function cron() { remove_teacher($teacher->userid, $teacher->course); } } + + // Notify teachers/students about students who's enrolment are going to expire + global $CFG; + if ($CFG->lastexpirynotify < date('Ymd') && ($courses = get_records_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0'))) { + $site = get_site(); + $admin = get_admin(); + $strexpirynotify = get_string('expirynotify'); + foreach ($courses as $course) { + $a = new stdClass(); + $a->course = $course->shortname .' '. $course->fullname; + $a->threshold = $course->expirythreshold / 86400; + $a->extendurl = $CFG->wwwroot . '/user/index.php?id=' . $course->id; + $a->current = array(); + $a->past = array(); + $a->current = $a->past = array(); + $expiry = time() + $course->expirythreshold; + $sql = "SELECT * FROM {$CFG->prefix}user u INNER JOIN {$CFG->prefix}user_students s ON u.id=s.userid WHERE s.course = $course->id AND s.timeend > 0 AND s.timeend <= $expiry"; + if ($students = get_records_sql($sql)) { + $teacher = get_teacher($course->id); + $strexpirynotifystudentsemail = get_string('expirynotifystudentsemail', '', $a); + foreach ($students as $student) { + if ($student->timeend < ($expiry - 86400)) { + $a->past[] = fullname($student) . " <$student->email>"; + } else { + $a->current[] = fullname($student) . " <$student->email>"; + if ($course->notifystudents) { + // Send this guy notice + email_to_user($student, $teacher, $site->fullname .' '. $strexpirynotify, $strexpirynotifystudentsemail); + } + } + } + } + $a->current = implode("\n", $a->current); + $a->past = implode("\n", $a->past); + $strexpirynotifyemail = get_string('expirynotifyemail', '', $a); + if ($a->current || $a->past) { + $sql = "SELECT u.* FROM {$CFG->prefix}user u INNER JOIN {$CFG->prefix}user_teachers t ON u.id=t.userid WHERE t.course = $course->id"; + if ($teachers = get_records_sql($sql)) { + foreach ($teachers as $teacher) { + email_to_user($teacher, $admin, $a->course .' '. $strexpirynotify, $strexpirynotifyemail); + } + } + } + if ($lastexpirynotify = get_record('config', 'name', 'lastexpirynotify')) { + $lastexpirynotify->value = date('Ymd'); + update_record('config', $lastexpirynotify); + } else { + $lastexpirynotify = new stdClass(); + $lastexpirynotify->name = 'lastexpirynotify'; + $lastexpirynotify->value = date('Ymd'); + insert_record('config', $lastexpirynotify); + } + } + } } diff --git a/lang/en/help/expirynotify.html b/lang/en/help/expirynotify.html new file mode 100644 index 00000000000..57d079cb73a --- /dev/null +++ b/lang/en/help/expirynotify.html @@ -0,0 +1 @@ +

Enrolment expiry notification

diff --git a/lang/en/help/expirynotifystudents.html b/lang/en/help/expirynotifystudents.html new file mode 100644 index 00000000000..53e175e0ca4 --- /dev/null +++ b/lang/en/help/expirynotifystudents.html @@ -0,0 +1 @@ +

Notify students as well

diff --git a/lang/en/help/expirythreshold.html b/lang/en/help/expirythreshold.html new file mode 100644 index 00000000000..bd0a85cb817 --- /dev/null +++ b/lang/en/help/expirythreshold.html @@ -0,0 +1 @@ +

Enrolment expiry notification threshold

diff --git a/lang/en/moodle.php b/lang/en/moodle.php index f5bc7a8d5fa..77b7d23c54f 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -463,6 +463,28 @@ $string['existingcreators'] = 'Existing course creators'; $string['existingstudents'] = 'Enrolled students'; $string['existingteachers'] = 'Existing teachers'; $string['explanation'] = 'Explanation'; +$string['expirynotify'] = 'Enrolment expiry notification'; +$string['expirynotifyemail'] = 'The following students in this course are expiring after exact $a->threshold days: + +$a->current + + +The following students in this course are expiring in less than $a->threshold days: + +$a->past + + +You may go to the following page to extend their enrolment period: +$a->extendurl +'; +$string['expirynotifystudents'] = 'Notify students'; +$string['expirynotifystudentsemail'] = 'Dear student: + +This is a notification that your enrolment for course $a->course will be expired in $a->threshold days. + +Please contact your tutor for any further enqiries. +'; +$string['expirythreshold'] = 'Threshold'; $string['extendenrol'] = 'Extend enrolment'; $string['extendperiod'] = 'Extended period'; $string['failedloginattempts'] = '$a->attempts failed logins since your last login'; diff --git a/lib/db/mysql.php b/lib/db/mysql.php index ece04abbbf9..062502165b9 100644 --- a/lib/db/mysql.php +++ b/lib/db/mysql.php @@ -1633,6 +1633,16 @@ function main_upgrade($oldversion=0) { } + if ($oldversion < 2005100300) { + table_column('course','','expirynotify','tinyint','1'); + table_column('course','','expirythreshold','int','10'); + table_column('course','','notifystudents','tinyint','1'); + $new = new stdClass(); + $new->name = 'lastexpirynotify'; + $new->value = 0; + insert_record('config', $new); + } + return $result; } diff --git a/lib/db/mysql.sql b/lib/db/mysql.sql index 0bce36dbb93..99c5323cb7c 100644 --- a/lib/db/mysql.sql +++ b/lib/db/mysql.sql @@ -79,6 +79,9 @@ CREATE TABLE `prefix_course` ( `metacourse` int(1) unsigned NOT NULL default '0', `requested` int(1) unsigned NOT NULL default '0', `restrictmodules` int(1) unsigned NOT NULL default '0', + `expirynotify` tinyint(1) unsigned NOT NULL default '0', + `expirythreshold` int(10) unsigned NOT NULL default '0', + `notifystudents` tinyint(1) unsigned NOT NULL default '0', PRIMARY KEY (`id`), KEY `category` (`category`), KEY `idnumber` (`idnumber`), diff --git a/lib/db/postgres7.php b/lib/db/postgres7.php index 12f4957a232..f67df7063b8 100644 --- a/lib/db/postgres7.php +++ b/lib/db/postgres7.php @@ -1375,6 +1375,16 @@ function main_upgrade($oldversion=0) { modify_database("","CREATE INDEX prefix_stats_user_monthly_timeend_idx ON prefix_stats_user_monthly (timeend);"); } + if ($oldversion < 2005100300) { + table_column('course','','expirynotify','integer','1'); + table_column('course','','expirythreshold','integer'); + table_column('course','','notifystudents','integer','1'); + $new = new stdClass(); + $new->name = 'lastexpirynotify'; + $new->value = 0; + insert_record('config', $new); + } + return $result; } diff --git a/lib/db/postgres7.sql b/lib/db/postgres7.sql index c80aab191b4..8e78ebc8846 100644 --- a/lib/db/postgres7.sql +++ b/lib/db/postgres7.sql @@ -49,7 +49,10 @@ CREATE TABLE prefix_course ( timemodified integer NOT NULL default '0', metacourse integer NOT NULL default '0', requested integer NOT NULL default '0', - restrictmodules integer NOT NULL default '0' + restrictmodules integer NOT NULL default '0', + expirynotify integer NOT NULL default '0', + expirythreshold integer NOT NULL default '0', + notifystudents integer NOT NULL default '0' ); CREATE UNIQUE INDEX prefix_course_category_sortorder_uk ON prefix_course (category,sortorder); diff --git a/version.php b/version.php index 1de900ccdaa..3c610d46333 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2005090100; // YYYYMMDD = date + $version = 2005100300; // YYYYMMDD = date // XY = increments within a single day $release = '1.6 development'; // Human-friendly version name