Commit 3b120e46 authored by moodler's avatar moodler
Browse files

MDL-10107 - First check-in of Messaging 2.0 code from Luis Rodrigues, GSOC student.

This new messaging system replaces all the various email_to_user() calls.
They are now replaced by events triggers, and the messages are then
processed centrally according to user preferences and sent to one or more
processors (email, popup, jabber etc...)

This code is not finished yet, a lot of work still has to be done on the
interface.  However, the basic structure is there and should be working.
Luis and I will be reviewing and polishing this heavily in the next few weeks.
parent 09f295fd
......@@ -6,6 +6,8 @@
* php -d error_log=/path/to/log thisfile.php will override the default error log for php cli, which is stderr, so if you want this script to just print stuff out, use php thisfile.php instead.
*/
require_once($CFG->libdir.'/eventslib.php');
$fd = fopen('php://stdin','r');
if (!$fd) {
......@@ -59,7 +61,22 @@ function notify_user($user,$subject,$a) {
return false;
}
$body = get_string('virusfoundlater','moodle',$a);
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = get_admin();
$eventdata->userto = $user;
$eventdata->subject = $subject;
$eventdata->fullmessage = $body;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($user,get_admin(),$subject,$body);
*/
}
......@@ -69,7 +86,20 @@ function notify_admins($user,$subject,$a) {
$body = get_string('virusfoundlateradmin','moodle',$a);
foreach ($admins as $admin) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $admin;
$eventdata->userto = $admin;
$eventdata->subject = $subject;
$eventdata->fullmessage = $body;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($admin,$admin,$subject,$body);
*/
}
}
......@@ -81,7 +111,20 @@ function notify_admins_unknown($file,$a) {
$subject = get_string('virusfoundsubject','moodle',format_string($site->fullname));
$body = get_string('virusfoundlateradminnolog','moodle',$a);
foreach ($admins as $admin) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $admin;
$eventdata->userto = $admin;
$eventdata->subject = $subject;
$eventdata->fullmessage = $body;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($admin,$admin,$subject,$body);
*/
}
}
......
<?php //$Id$
//This file contains all the code needed to execute scheduled backups
require_once($CFG->libdir.'/eventslib.php');
//This function is executed via moodle cron
//It prepares all the info and execute backups as necessary
......@@ -195,7 +197,20 @@ function schedule_backup_cron() {
$subject = $prefix.get_string("scheduledbackupstatus");
//Send the message
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $admin;
$eventdata->userto = $admin;
$eventdata->subject = $subject;
$eventdata->fullmessage = $message;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($admin,$admin,$subject,$message);
*/
}
......
......@@ -5,6 +5,7 @@
require_once($CFG->libdir.'/pagelib.php');
require_once($CFG->libdir.'/blocklib.php');
require_once('lib.php');
require_once($CFG->libdir.'/eventslib.php');
require_login();
......@@ -52,7 +53,21 @@
$a->name = $course->fullname;
$a->url = $CFG->wwwroot.'/course/view.php?id='.$courseid;
$a->teacher = $course->teacher;
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $USER;
$eventdata->userto = $user;
$eventdata->subject = get_string('courseapprovedsubject');
$eventdata->fullmessage = get_string('courseapprovedemail','moodle',$a);
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($user,$USER,get_string('courseapprovedsubject'),get_string('courseapprovedemail','moodle',$a));
*/
redirect($CFG->wwwroot.'/course/edit.php?id='.$courseid);
exit;
}
......@@ -79,7 +94,20 @@
}
else {
$user = $DB->get_record("user", array("id"=>$reject->requester));
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $USER;
$eventdata->userto = $user;
$eventdata->subject = get_string('courserejectsubject');
$eventdata->fullmessage = get_string('courserejectemail','moodle',$rejectnotice);
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($user,$USER,get_string('courserejectsubject'),get_string('courserejectemail','moodle',$rejectnotice));
*/
$DB->delete_records("course_request", array("id"=>$reject->id));
notice(get_string('courserejected'),'pending.php');
}
......
......@@ -4,6 +4,7 @@ require_once($CFG->dirroot.'/enrol/enrol.class.php');
require_once($CFG->dirroot.'/enrol/authorize/const.php');
require_once($CFG->dirroot.'/enrol/authorize/localfuncs.php');
require_once($CFG->dirroot.'/enrol/authorize/authorizenet.class.php');
require_once($CFG->libdir.'/eventslib.php');
/**
* Authorize.net Payment Gateway plugin
......@@ -171,7 +172,7 @@ class enrolment_plugin_authorize
$order->currency = $curcost['currency'];
$order->id = $DB->insert_record("enrol_authorize", $order);
if (!$order->id) {
email_to_admin("Error while trying to insert new data", $order);
message_to_admin("Error while trying to insert new data", $order);
return "Insert record error. Admin has been notified!";
}
......@@ -242,7 +243,20 @@ class enrolment_plugin_authorize
$context = get_context_instance(CONTEXT_COURSE, $course->id);
if (($paymentmanagers = get_users_by_capability($context, 'enrol/authorize:managepayments'))) {
foreach ($paymentmanagers as $paymentmanager) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $USER;
$eventdata->userto = $paymentmanager;
$eventdata->subject = $emailsubject;
$eventdata->fullmessage = $emailmessage;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($paymentmanager, $USER, $emailsubject, $emailmessage);
*/
}
}
redirect($CFG->wwwroot, get_string("reviewnotify", "enrol_authorize"), '30');
......@@ -265,12 +279,26 @@ class enrolment_plugin_authorize
$a = new stdClass;
$a->course = "$course->fullname";
$a->user = fullname($USER);
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $USER;
$eventdata->userto = $paymentmanager;
$eventdata->subject = get_string("enrolmentnew", '', format_string($course->shortname));
$eventdata->fullmessage = get_string('enrolmentnewuser', '', $a);
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user(
$paymentmanager,
$USER,
get_string("enrolmentnew", '', format_string($course->shortname)),
get_string('enrolmentnewuser', '', $a)
);
*/
}
if (!empty($CFG->enrol_mailadmins)) {
$a = new stdClass;
......@@ -278,18 +306,31 @@ class enrolment_plugin_authorize
$a->user = fullname($USER);
$admins = get_admins();
foreach ($admins as $admin) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $USER;
$eventdata->userto = $paymentmanager;
$eventdata->subject = get_string("enrolmentnew", '', format_string($course->shortname));
$eventdata->fullmessage = get_string('enrolmentnewuser', '', $a);
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user(
$admin,
$USER,
get_string("enrolmentnew", '', format_string($course->shortname)),
get_string('enrolmentnewuser', '', $a)
);
*/
}
}
}
else
{
email_to_admin("Error while trying to enrol " . fullname($USER) . " in '$course->fullname'", $order);
message_to_admin("Error while trying to enrol " . fullname($USER) . " in '$course->fullname'", $order);
}
if ($SESSION->wantsurl) {
......@@ -308,7 +349,7 @@ class enrolment_plugin_authorize
}
else
{
email_to_admin($message, $order);
message_to_admin($message, $order);
return $message;
}
}
......@@ -348,7 +389,7 @@ class enrolment_plugin_authorize
$order->currency = $curcost['currency'];
$order->id = $DB->insert_record("enrol_authorize", $order);
if (!$order->id) {
email_to_admin("Error while trying to insert new data", $order);
message_to_admin("Error while trying to insert new data", $order);
return "Insert record error. Admin has been notified!";
}
......@@ -385,7 +426,7 @@ class enrolment_plugin_authorize
return NULL;
}
else {
email_to_admin($message, $order);
message_to_admin($message, $order);
return $message;
}
}
......@@ -695,10 +736,35 @@ class enrolment_plugin_authorize
$adminuser = get_admin();
if (!empty($faults)) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $adminuser;
$eventdata->userto = $adminuser;
$eventdata->subject = "AUTHORIZE.NET CRON FAULTS";
$eventdata->fullmessage = $faults;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($adminuser, $adminuser, "AUTHORIZE.NET CRON FAULTS", $faults);
*/
}
if (!empty($CFG->enrol_mailadmins)) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $adminuser;
$eventdata->userto = $adminuser;
$eventdata->subject = "AUTHORIZE.NET CRON LOG";
$eventdata->fullmessage = $this->log;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($adminuser, $adminuser, "AUTHORIZE.NET CRON LOG", $this->log);
*/
}
// Send emails to students about which courses have enrolled.
......@@ -757,7 +823,20 @@ class enrolment_plugin_authorize
$a->url = $CFG->wwwroot.'/enrol/authorize/uploadcsv.php';
$message = get_string('pendingecheckemail', 'enrol_authorize', $a);
foreach($csvusers as $csvuser) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $adminuser;
$eventdata->userto = $csvuser;
$eventdata->subject = $subject;
$eventdata->fullmessage = $message;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
@email_to_user($csvuser, $adminuser, $subject, $message);
*/
}
mtrace(" users who have 'enrol/authorize:uploadcsv' were mailed");
}
......@@ -791,7 +870,21 @@ class enrolment_plugin_authorize
$a->enrolurl = "$CFG->wwwroot/$CFG->admin/enrol_config.php?enrol=authorize";
$a->url = $CFG->wwwroot.'/enrol/authorize/index.php?status='.AN_STATUS_AUTH;
$message = get_string('pendingordersemail', 'enrol_authorize', $a);
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $adminuser;
$eventdata->userto = $adminuser;
$eventdata->subject = $subject;
$eventdata->fullmessage = $message;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($adminuser, $adminuser, $subject, $message);
*/
// Email to payment managers
if (empty($CFG->an_emailexpiredteacher)) {
......@@ -830,7 +923,20 @@ class enrolment_plugin_authorize
$a->url = $CFG->wwwroot.'/enrol/authorize/index.php?course='.$lastcourse.'&amp;status='.AN_STATUS_AUTH;
$message = get_string('pendingordersemailteacher', 'enrol_authorize', $a);
foreach ($paymentmanagers as $paymentmanager) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $adminuser;
$eventdata->userto = $paymentmanager;
$eventdata->subject = $subject;
$eventdata->fullmessage = $message;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($paymentmanager, $adminuser, $subject, $message);
*/
}
}
}
......
<?php // $Id$
require_once($CFG->libdir.'/eventslib.php');
function get_course_cost($course)
{
global $CFG;
......@@ -111,7 +113,7 @@ function get_list_of_bank_account_types($getall = false)
}
}
function email_to_admin($subject, $data)
function message_to_admin($subject, $data)
{
global $SITE;
......@@ -120,7 +122,19 @@ function email_to_admin($subject, $data)
$message = "$SITE->fullname: Transaction failed.\n\n$subject\n\n";
$message .= print_r($data, true);
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $admin;
$eventdata->userto = $admin;
$eventdata->subject = "$SITE->fullname: Authorize.net ERROR";
$eventdata->fullmessage = $emailmessage;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($admin, $admin, "$SITE->fullname: Authorize.net ERROR", $message);
*/
}
function send_welcome_messages($orderdata)
......@@ -177,7 +191,21 @@ function send_welcome_messages($orderdata)
$a->profileurl = "$CFG->wwwroot/user/view.php?id=$lastuserid";
$a->paymenturl = "$CFG->wwwroot/enrol/authorize/index.php?user=$lastuserid";
$emailmessage = get_string('welcometocoursesemail', 'enrol_authorize', $a);
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $sender;
$eventdata->userto = $user;
$eventdata->subject = get_string("enrolmentnew", '', $SITE->shortname);
$eventdata->fullmessage = $emailmessage;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
@email_to_user($user, $sender, get_string("enrolmentnew", '', $SITE->shortname), $emailmessage);
*/
}
}
while ($ei);
......
......@@ -5,6 +5,7 @@
require_once($CFG->libdir.'/uploadlib.php');
require_once($CFG->dirroot.'/enrol/authorize/const.php');
require_once($CFG->dirroot.'/enrol/authorize/localfuncs.php');
require_once($CFG->libdir.'/eventslib.php');
/// Require capabilites
require_login();
......@@ -224,7 +225,21 @@ function authorize_process_csv($filename)
/// Send email to admin
if (!empty($ignoredlines)) {
$admin = get_admin();
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $admin;
$eventdata->userto = $admin;
$eventdata->subject = "$SITE->fullname: Authorize.net CSV ERROR LOG";
$eventdata->fullmessage = $ignoredlines;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($admin, $admin, "$SITE->fullname: Authorize.net CSV ERROR LOG", $ignoredlines);
*/
}
/// Send welcome messages to users
......
......@@ -5,6 +5,7 @@
// $CFG->enrol_emailteachers: send email to teachers when they are enrolled in a course
// $CFG->enrol_emailadmins: email the log from the cron job to the admin
require_once($CFG->libdir.'/eventslib.php');
class enrolment_plugin_flatfile {
......@@ -216,8 +217,22 @@ function get_access_icons($course) {
if (!empty($CFG->enrol_mailstudents)) {
$a->coursename = "$course->fullname";
$a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id";
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $teacher;
$eventdata->userto = $user;
$eventdata->subject = get_string("enrolmentnew", '', $course->shortname);
$eventdata->fullmessage = get_string('welcometocoursetext', '', $a);
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($user, $teacher, get_string("enrolmentnew", '', $course->shortname),
get_string('welcometocoursetext', '', $a));
*/
}
if (!empty($CFG->enrol_mailteachers) && $teachers) {
......@@ -227,8 +242,22 @@ function get_access_icons($course) {
if (!$u->hidden || has_capability('moodle/role:viewhiddenassigns', $context)) {
$a->course = "$course->fullname";
$a->user = fullname($user);
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $user;
$eventdata->userto = $teacher;
$eventdata->subject = get_string("enrolmentnew", '', $course->shortname);
$eventdata->fullmessage = get_string('enrolmentnewuser', '', $a);
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($teacher, $user, get_string("enrolmentnew", '', $course->shortname),
get_string('enrolmentnewuser', '', $a));
*/
}
}
}
......@@ -246,12 +275,38 @@ function get_access_icons($course) {
} // end of if(file_open)
if(! @unlink($filename)) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = get_admin();
$eventdata->userto = get_admin();
$eventdata->subject = get_string("filelockedmailsubject", "enrol_flatfile");
$eventdata->fullmessage = get_string("filelockedmail", "enrol_flatfile", $filename);
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user(get_admin(), get_admin(), get_string("filelockedmailsubject", "enrol_flatfile"), get_string("filelockedmail", "enrol_flatfile", $filename));
*/
$this->log .= "Error unlinking file $filename\n";
}
if (!empty($CFG->enrol_mailadmins)) {
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = get_admin();
$eventdata->userto = get_admin();
$eventdata->subject = "Flatfile Enrolment Log";
$eventdata->fullmessage = $this->log;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user(get_admin(), get_admin(), "Flatfile Enrolment Log", $this->log);
*/
}
} // end of if(file_exists)
......
......@@ -6,6 +6,7 @@
*/
require_once("$CFG->libdir/blocklib.php");
require_once($CFG->dirroot.'/group/lib.php');
require_once($CFG->libdir.'/eventslib.php');
// The following flags are set in the configuration
// $CFG->enrol_imsfilelocation: where is the file we are looking for?
......@@ -380,8 +381,21 @@ function cron() {
}else{
$msg .= "Logging is currently not active.";
}
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = get_admin();
$eventdata->userto = get_admin();
$eventdata->subject = "Moodle IMS Enterprise enrolment notification";
$eventdata->fullmessage = $msg;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user(get_admin(), get_admin(), "Moodle IMS Enterprise enrolment notification", $msg);
*/
$this->log_line('Notification email sent to administrator.');
}
......
......@@ -23,6 +23,7 @@
///////////////////////////////////////////////////////////////////////////
require_once($CFG->dirroot.'/group/lib.php');
require_once($CFG->libdir.'/eventslib.php');
/**
* enrolment_plugin_manual is the default enrolment plugin
......@@ -313,8 +314,20 @@ function cron() {
$strexpirynotifystudentsemail = get_string('expirynotifystudentsemail', '', $a);
$strexpirynotify = get_string('expirynotify');
$eventdata = new object();
$eventdata->modulename = 'moodle';
$eventdata->userfrom = $teacher;
$eventdata->userto = $user;
$eventdata->subject = format_string($SITE->fullname) .' '. $strexpirynotify;
$eventdata->fullmessage = $strexpirynotifystudentsemail;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = '';
$eventdata->smallmessage = '';
events_trigger('message_send', $eventdata);
/*
email_to_user($user, $teacher, format_string($SITE->fullname) .' '. $strexpirynotify,
$strexpirynotifystudentsemail);
*/
}
}
}
......@@ -331,7 +344,20 @@ function cron() {
$strexpirynotifyemail = get_string('expirynotifyemail', '', $a);
$strexpirynotify = get_string('expirynotify');
$eventdata = new object();
$eventdata->modulename = 'moodle';