Merge branch 'MDL-47162' of git://github.com/merrill-oakland/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 31 Oct 2016 21:45:37 +0000 (22:45 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 31 Oct 2016 21:45:37 +0000 (22:45 +0100)
37 files changed:
admin/tool/messageinbound/classes/manager.php
admin/tool/monitor/classes/notification_task.php
backup/util/helper/backup_cron_helper.class.php
badges/cron.php
competency/lib.php
course/lib.php
enrol/flatfile/lib.php
enrol/imsenterprise/lib.php
enrol/paypal/classes/util.php
enrol/paypal/ipn.php
error/index.php
lib/badgeslib.php
lib/classes/antivirus/scanner.php
lib/classes/event/message_sent.php
lib/classes/message/manager.php
lib/classes/message/message.php
lib/classes/update/checker.php
lib/enrollib.php
lib/messagelib.php
lib/phpunit/tests/advanced_test.php
lib/portfoliolib.php
lib/tests/message_test.php
lib/tests/messagelib_test.php
lib/upgrade.txt
message/lib.php
message/tests/events_test.php
message/tests/externallib_test.php
message/tests/search_test_received.php
message/tests/search_test_sent.php
mod/assign/locallib.php
mod/assign/tests/locallib_test.php
mod/feedback/lib.php
mod/feedback/show_nonrespondents.php
mod/forum/lib.php
mod/forum/tests/mail_test.php
mod/lesson/essay.php
mod/quiz/locallib.php

index a352f7c..0e26c58 100644 (file)
@@ -920,7 +920,7 @@ class manager {
         $addressmanager->set_handler('\tool_messageinbound\message\inbound\invalid_recipient_handler');
         $addressmanager->set_data($record->id);
 
-        $eventdata = new \stdClass();
+        $eventdata = new \core\message\message();
         $eventdata->component           = 'tool_messageinbound';
         $eventdata->name                = 'invalidrecipienthandler';
 
@@ -930,6 +930,7 @@ class manager {
         $userfrom->customheaders[] = 'In-Reply-To: ' . $messageid;
 
         // The message will be sent from the intended user.
+        $eventdata->courseid            = SITEID;
         $eventdata->userfrom            = \core_user::get_support_user();
         $eventdata->userto              = $USER;
         $eventdata->subject             = $this->get_reply_subject($this->currentmessagedata->envelope->subject);
@@ -970,7 +971,8 @@ class manager {
         $messagedata->subject = $this->currentmessagedata->envelope->subject;
         $messagedata->error = $error;
 
-        $eventdata = new \stdClass();
+        $eventdata = new \core\message\message();
+        $eventdata->courseid            = SITEID;
         $eventdata->component           = 'tool_messageinbound';
         $eventdata->name                = 'messageprocessingerror';
         $eventdata->userfrom            = $userfrom;
@@ -1029,7 +1031,8 @@ class manager {
         $messagedata = new \stdClass();
         $messagedata->subject = $this->currentmessagedata->envelope->subject;
 
-        $eventdata = new \stdClass();
+        $eventdata = new \core\message\message();
+        $eventdata->courseid            = SITEID;
         $eventdata->component           = 'tool_messageinbound';
         $eventdata->name                = 'messageprocessingsuccess';
         $eventdata->userfrom            = $userfrom;
index 6e36841..a65ef04 100644 (file)
@@ -83,7 +83,8 @@ class notification_task extends \core\task\adhoc_task {
         $template = $subscription->template;
         $template = $this->replace_placeholders($template, $subscription, $eventobj, $context);
         $htmlmessage = format_text($template, $subscription->templateformat, array('context' => $context));
-        $msgdata = new \stdClass();
+        $msgdata = new \core\message\message();
+        $msgdata->courseid          = empty($subscription->courseid) ? SITEID : $subscription->courseid;
         $msgdata->component         = 'tool_monitor'; // Your component name.
         $msgdata->name              = 'notification'; // This is the message name from messages.php.
         $msgdata->userfrom          = \core_user::get_noreply_user();
index 8b6eeb8..fc62cfc 100644 (file)
@@ -264,7 +264,8 @@ abstract class backup_cron_automated_helper {
             $subject = $prefix.get_string('automatedbackupstatus', 'backup');
 
             //Send the message
-            $eventdata = new stdClass();
+            $eventdata = new \core\message\message();
+            $eventdata->courseid          = SITEID;
             $eventdata->modulename        = 'moodle';
             $eventdata->userfrom          = $admin;
             $eventdata->userto            = $admin;
index a0bee0f..fc51a8d 100644 (file)
@@ -140,7 +140,8 @@ function badge_assemble_notification(stdClass $badge) {
         }
 
         // Create a message object.
-        $eventdata = new stdClass();
+        $eventdata = new \core\message\message();
+        $eventdata->courseid          = SITEID;
         $eventdata->component         = 'moodle';
         $eventdata->name              = 'badgecreatornotice';
         $eventdata->userfrom          = $userfrom;
index e900774..7472db1 100644 (file)
@@ -116,6 +116,7 @@ function core_competency_comment_add($comment, $params) {
         }
 
         $message = new \core\message\message();
+        $message->courseid = SITEID;
         $message->component = 'moodle';
         $message->name = 'competencyusercompcomment';
         $message->notification = 1;
@@ -184,6 +185,7 @@ function core_competency_comment_add($comment, $params) {
         }
 
         $message = new \core\message\message();
+        $message->courseid = SITEID;
         $message->component = 'moodle';
         $message->name = 'competencyplancomment';
         $message->notification = 1;
index b884fc3..5536326 100644 (file)
@@ -2636,7 +2636,7 @@ class course_request {
         $a = new stdClass();
         $a->name = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
         $a->url = $CFG->wwwroot.'/course/view.php?id=' . $course->id;
-        $this->notify($user, $USER, 'courserequestapproved', get_string('courseapprovedsubject'), get_string('courseapprovedemail2', 'moodle', $a));
+        $this->notify($user, $USER, 'courserequestapproved', get_string('courseapprovedsubject'), get_string('courseapprovedemail2', 'moodle', $a), $course->id);
 
         return $course->id;
     }
@@ -2672,9 +2672,11 @@ class course_request {
      * @param string $name
      * @param string $subject
      * @param string $message
+     * @param int|null $courseid
      */
-    protected function notify($touser, $fromuser, $name='courserequested', $subject, $message) {
-        $eventdata = new stdClass();
+    protected function notify($touser, $fromuser, $name='courserequested', $subject, $message, $courseid = null) {
+        $eventdata = new \core\message\message();
+        $eventdata->courseid          = empty($courseid) ? SITEID : $courseid;
         $eventdata->component         = 'moodle';
         $eventdata->name              = $name;
         $eventdata->userfrom          = $fromuser;
index 57438fb..4bff2a9 100644 (file)
@@ -186,7 +186,8 @@ class enrol_flatfile_plugin extends enrol_plugin {
 
         if ($processed and $mailadmins) {
             if ($log = $buffer->get_buffer()) {
-                $eventdata = new stdClass();
+                $eventdata = new \core\message\message();
+                $eventdata->courseid          = SITEID;
                 $eventdata->modulename        = 'moodle';
                 $eventdata->component         = 'enrol_flatfile';
                 $eventdata->name              = 'flatfile_enrolment';
@@ -340,7 +341,8 @@ class enrol_flatfile_plugin extends enrol_plugin {
         }
 
         if (!unlink($filelocation)) {
-            $eventdata = new stdClass();
+            $eventdata = new \core\message\message();
+            $eventdata->courseid          = SITEID;
             $eventdata->modulename        = 'moodle';
             $eventdata->component         = 'enrol_flatfile';
             $eventdata->name              = 'flatfile_enrolment';
@@ -463,7 +465,8 @@ class enrol_flatfile_plugin extends enrol_plugin {
                 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id";
                 $subject = get_string('enrolmentnew', 'enrol', format_string($course->shortname, true, array('context' => $context)));
 
-                $eventdata = new stdClass();
+                $eventdata = new \core\message\message();
+                $eventdata->courseid          = $course->id;
                 $eventdata->modulename        = 'moodle';
                 $eventdata->component         = 'enrol_flatfile';
                 $eventdata->name              = 'flatfile_enrolment';
@@ -494,7 +497,8 @@ class enrol_flatfile_plugin extends enrol_plugin {
                 $a->user = fullname($user);
                 $subject = get_string('enrolmentnew', 'enrol', format_string($course->shortname, true, array('context' => $context)));
 
-                $eventdata = new stdClass();
+                $eventdata = new \core\message\message();
+                $eventdata->courseid          = $course->id;
                 $eventdata->modulename        = 'moodle';
                 $eventdata->component         = 'enrol_flatfile';
                 $eventdata->name              = 'flatfile_enrolment';
index ede2700..573606f 100644 (file)
@@ -222,7 +222,8 @@ class enrol_imsenterprise_plugin extends enrol_plugin {
                 $msg .= "Logging is currently not active.";
             }
 
-            $eventdata = new stdClass();
+            $eventdata = new \core\message\message();
+            $eventdata->courseid          = SITEID;
             $eventdata->modulename        = 'moodle';
             $eventdata->component         = 'enrol_imsenterprise';
             $eventdata->name              = 'imsenterprise_enrolment';
index a07ae24..6f91abd 100644 (file)
@@ -51,7 +51,8 @@ final class util {
             $message .= "$key => $value\n";
         }
 
-        $eventdata = new \stdClass();
+        $eventdata = new \core\message\message();
+        $eventdata->courseid          = empty($data->courseid) ? SITEID : $data->courseid;
         $eventdata->modulename        = 'moodle';
         $eventdata->component         = 'enrol_paypal';
         $eventdata->name              = 'paypal_enrolment';
index 809cdb7..ccbcd0b 100644 (file)
@@ -85,7 +85,7 @@ if (! $course = $DB->get_record("course", array("id"=>$data->courseid))) {
 }
 
 if (! $context = context_course::instance($course->id, IGNORE_MISSING)) {
-    message_paypal_error_to_admin("Not a valid context id", $data);
+    \enrol_paypal\util::message_paypal_error_to_admin("Not a valid context id", $data);
     die;
 }
 
@@ -152,7 +152,8 @@ if (strlen($result) > 0) {
         // Email user to let them know. Email admin.
 
         if ($data->payment_status == "Pending" and $data->pending_reason != "echeck") {
-            $eventdata = new stdClass();
+            $eventdata = new \core\message\message();
+            $eventdata->courseid          = empty($data->courseid) ? SITEID : $data->courseid;
             $eventdata->modulename        = 'moodle';
             $eventdata->component         = 'enrol_paypal';
             $eventdata->name              = 'paypal_enrolment';
@@ -259,7 +260,8 @@ if (strlen($result) > 0) {
             $a->coursename = format_string($course->fullname, true, array('context' => $coursecontext));
             $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id";
 
-            $eventdata = new stdClass();
+            $eventdata = new \core\message\message();
+            $eventdata->courseid          = $course->id;
             $eventdata->modulename        = 'moodle';
             $eventdata->component         = 'enrol_paypal';
             $eventdata->name              = 'paypal_enrolment';
@@ -278,7 +280,8 @@ if (strlen($result) > 0) {
             $a->course = format_string($course->fullname, true, array('context' => $coursecontext));
             $a->user = fullname($user);
 
-            $eventdata = new stdClass();
+            $eventdata = new \core\message\message();
+            $eventdata->courseid          = $course->id;
             $eventdata->modulename        = 'moodle';
             $eventdata->component         = 'enrol_paypal';
             $eventdata->name              = 'paypal_enrolment';
@@ -297,7 +300,8 @@ if (strlen($result) > 0) {
             $a->user = fullname($user);
             $admins = get_admins();
             foreach ($admins as $admin) {
-                $eventdata = new stdClass();
+                $eventdata = new \core\message\message();
+                $eventdata->courseid          = $course->id;
                 $eventdata->modulename        = 'moodle';
                 $eventdata->component         = 'enrol_paypal';
                 $eventdata->name              = 'paypal_enrolment';
@@ -319,4 +323,3 @@ if (strlen($result) > 0) {
 }
 
 exit;
-
index 721f141..2e3280d 100644 (file)
@@ -11,7 +11,8 @@
         }
 
         // Send the message and redirect.
-        $eventdata = new stdClass();
+        $eventdata = new \core\message\message();
+        $eventdata->courseid         = SITEID;
         $eventdata->component        = 'moodle';
         $eventdata->name             = 'errors';
         $eventdata->userfrom          = $USER;
index d77d739..9724bf7 100644 (file)
@@ -722,7 +722,8 @@ function badges_notify_badge_award(badge $badge, $userid, $issued, $filepathhash
     $plaintext = html_to_text($message);
 
     // Notify recipient.
-    $eventdata = new stdClass();
+    $eventdata = new \core\message\message();
+    $eventdata->courseid          = $badge->courseid;
     $eventdata->component         = 'moodle';
     $eventdata->name              = 'badgerecipientnotice';
     $eventdata->userfrom          = $userfrom;
@@ -758,7 +759,8 @@ function badges_notify_badge_award(badge $badge, $userid, $issued, $filepathhash
         $creatormessage = get_string('creatorbody', 'badges', $a);
         $creatorsubject = get_string('creatorsubject', 'badges', $badge->name);
 
-        $eventdata = new stdClass();
+        $eventdata = new \core\message\message();
+        $eventdata->courseid          = $badge->courseid;
         $eventdata->component         = 'moodle';
         $eventdata->name              = 'badgecreatornotice';
         $eventdata->userfrom          = $userfrom;
index 5f4186c..a93e56d 100644 (file)
@@ -122,7 +122,8 @@ abstract class scanner {
         $subject = get_string('emailsubject', 'antivirus', format_string($site->fullname));
         $admins = get_admins();
         foreach ($admins as $admin) {
-            $eventdata = new \stdClass();
+            $eventdata = new \core\message\message();
+            $eventdata->courseid          = SITEID;
             $eventdata->component         = 'moodle';
             $eventdata->name              = 'errors';
             $eventdata->userfrom          = get_admin();
index 0d58ddb..e330919 100644 (file)
@@ -33,6 +33,7 @@ defined('MOODLE_INTERNAL') || die();
  *      Extra information about event.
  *
  *      - int messageid: the id of the message.
+ *      - int courseid: the id of the related course.
  * }
  *
  * @package    core
@@ -43,12 +44,14 @@ defined('MOODLE_INTERNAL') || die();
 class message_sent extends base {
     /**
      * Create event using ids.
+     * @todo MDL-55449 Make $courseid mandatory in Moodle 3.6
      * @param int $userfromid
      * @param int $usertoid
      * @param int $messageid
+     * @param int|null $courseid course id the event is related with. Use SITEID if no relation exists.
      * @return message_sent
      */
-    public static function create_from_ids($userfromid, $usertoid, $messageid) {
+    public static function create_from_ids($userfromid, $usertoid, $messageid, $courseid = null) {
         // We may be sending a message from the 'noreply' address, which means we are not actually sending a
         // message from a valid user. In this case, we will set the userid to 0.
         // Check if the userid is valid.
@@ -56,6 +59,15 @@ class message_sent extends base {
             $userfromid = 0;
         }
 
+        // TODO: MDL-55449 Make $courseid mandatory in Moodle 3.6.
+        if (is_null($courseid)) {
+            // Arrived here with not defined $courseid to associate the event with.
+            // Let's default to SITEID and perform debugging so devs are aware. MDL-47162.
+            $courseid = SITEID;
+            debugging('message_sent::create_from_ids() needs a $courseid to be passed, nothing was detected. Please, change ' .
+                    'the call to include it, using SITEID if the message is unrelated to any real course.', DEBUG_DEVELOPER);
+        }
+
         $event = self::create(array(
             'userid' => $userfromid,
             'context' => \context_system::instance(),
@@ -64,7 +76,8 @@ class message_sent extends base {
                 // In earlier versions it can either be the id in the 'message_read' or 'message' table.
                 // Now it is always the id from 'message' table. Please note that the record is still moved
                 // to the 'message_read' table later when message marked as read.
-                'messageid' => $messageid
+                'messageid' => $messageid,
+                'courseid' => $courseid
             )
         ));
 
@@ -143,6 +156,10 @@ class message_sent extends base {
         if (!isset($this->other['messageid'])) {
             throw new \coding_exception('The \'messageid\' value must be set in other.');
         }
+
+        if (!isset($this->other['courseid'])) {
+            throw new \coding_exception('The \'courseid\' value must be set in other.');
+        }
     }
 
     public static function get_objectid_mapping() {
@@ -155,6 +172,7 @@ class message_sent extends base {
         $othermapped = array();
         // The messages table could vary for older events - so cannot be mapped.
         $othermapped['messageid'] = array('db' => base::NOT_MAPPED, 'restore' => base::NOT_MAPPED);
+        $othermapped['courseid'] = array('db' => base::NOT_MAPPED, 'restore' => base::NOT_MAPPED);
         return $othermapped;
     }
 }
index 234cc93..21e046a 100644 (file)
@@ -50,7 +50,8 @@ class manager {
      *
      * NOTE: to be used from message_send() only.
      *
-     * @param \stdClass|\core\message\message $eventdata fully prepared event data for processors
+     * @todo MDL-55449 Drop support for stdClass in Moodle 3.6
+     * @param \core\message\message $eventdata fully prepared event data for processors
      * @param \stdClass $savemessage the message saved in 'message' table
      * @param array $processorlist list of processors for target user
      * @return int $messageid the id from 'message' or 'message_read' table (false is not returned)
@@ -58,16 +59,31 @@ class manager {
     public static function send_message($eventdata, \stdClass $savemessage, array $processorlist) {
         global $CFG;
 
+        // TODO MDL-55449 Drop support for stdClass in Moodle 3.6.
         if (!($eventdata instanceof \stdClass) && !($eventdata instanceof message)) {
             // Not a valid object.
             throw new \coding_exception('Message should be of type stdClass or \core\message\message');
         }
 
+        // TODO MDL-55449 Drop support for stdClass in Moodle 3.6.
+        if ($eventdata instanceof \stdClass) {
+            if (!isset($eventdata->courseid)) {
+                $eventdata->courseid = null;
+            }
+
+            debugging('eventdata as \stdClass is deprecated. Please use \core\message\message instead.', DEBUG_DEVELOPER);
+        }
+
         require_once($CFG->dirroot.'/message/lib.php'); // This is most probably already included from messagelib.php file.
 
         if (empty($processorlist)) {
             // Trigger event for sending a message - we need to do this before marking as read!
-            \core\event\message_sent::create_from_ids($eventdata->userfrom->id, $eventdata->userto->id, $savemessage->id)->trigger();
+            \core\event\message_sent::create_from_ids(
+                $eventdata->userfrom->id,
+                $eventdata->userto->id,
+                $savemessage->id,
+                $eventdata->courseid
+                )->trigger();
 
             if ($savemessage->notification or empty($CFG->messaging)) {
                 // If they have deselected all processors and its a notification mark it read. The user doesn't want to be bothered.
@@ -132,7 +148,12 @@ class manager {
         }
 
         // Trigger event for sending a message - must be done before marking as read.
-        \core\event\message_sent::create_from_ids($eventdata->userfrom->id, $eventdata->userto->id, $savemessage->id)->trigger();
+        \core\event\message_sent::create_from_ids(
+            $eventdata->userfrom->id,
+            $eventdata->userto->id,
+            $savemessage->id,
+            $eventdata->courseid
+            )->trigger();
 
         if (empty($CFG->messaging)) {
             // If messaging is disabled and they previously had forum notifications handled by the popup processor
index f75cce9..107335d 100644 (file)
@@ -56,6 +56,12 @@ defined('MOODLE_INTERNAL') || die();
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 class message {
+    /** @var int Course id. */
+    private $courseid;
+
+    /** @var string Module name. */
+    private $modulename;
+
     /** @var string Component name. */
     private $component;
 
@@ -104,10 +110,31 @@ class message {
     /** @var  string Name of the attachment. Note:- not all processors support this.*/
     private $attachname;
 
+    /** @var  int The time the message was created.*/
+    private $timecreated;
+
     /** @var array a list of properties that is allowed for each message. */
-    private $properties = array('component', 'name', 'userfrom', 'userto', 'subject', 'fullmessage', 'fullmessageformat',
-                                'fullmessagehtml', 'smallmessage', 'notification', 'contexturl', 'contexturlname', 'savedmessageid',
-                                'replyto', 'attachment', 'attachname');
+    private $properties = array(
+        'courseid',
+        'modulename',
+        'component',
+        'name',
+        'userfrom',
+        'userto',
+        'subject',
+        'fullmessage',
+        'fullmessageformat',
+        'fullmessagehtml',
+        'smallmessage',
+        'notification',
+        'contexturl',
+        'contexturlname',
+        'replyto',
+        'savedmessageid',
+        'attachment',
+        'attachname',
+        'timecreated'
+        );
 
     /** @var array property to store any additional message processor specific content */
     private $additionalcontent = array();
index b5c701a..ba19c15 100644 (file)
@@ -798,7 +798,8 @@ class checker {
             array('style' => 'font-size:smaller; color:#333;')));
 
         foreach ($admins as $admin) {
-            $message = new \stdClass();
+            $message = new \core\message\message();
+            $message->courseid          = SITEID;
             $message->component         = 'moodle';
             $message->name              = 'availableupdate';
             $message->userfrom          = get_admin();
index 6ef9c71..4849b07 100644 (file)
@@ -2655,7 +2655,8 @@ abstract class enrol_plugin {
         $subject = get_string('expirymessageenrolledsubject', 'enrol_'.$name, $a);
         $body = get_string('expirymessageenrolledbody', 'enrol_'.$name, $a);
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = $ue->courseid;
         $message->notification      = 1;
         $message->component         = 'enrol_'.$name;
         $message->name              = 'expiry_notification';
@@ -2716,7 +2717,8 @@ abstract class enrol_plugin {
         $subject = get_string('expirymessageenrollersubject', 'enrol_'.$name, $a);
         $body = get_string('expirymessageenrollerbody', 'enrol_'.$name, $a);
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = $course->id;
         $message->notification      = 1;
         $message->component         = 'enrol_'.$name;
         $message->name              = 'expiry_notification';
index 3c3a529..34e7c38 100644 (file)
@@ -50,13 +50,23 @@ require_once(__DIR__ . '/../message/lib.php');
  * Note: processor failure is is not reported as false return value,
  *       earlier versions did not do it consistently either.
  *
+ * @todo MDL-55449 Drop support for stdClass in Moodle 3.6
  * @category message
- * @param stdClass|\core\message\message $eventdata information about the message (component, userfrom, userto, ...)
+ * @param \core\message\message $eventdata information about the message (component, userfrom, userto, ...)
  * @return mixed the integer ID of the new message or false if there was a problem with submitted data
  */
 function message_send($eventdata) {
     global $CFG, $DB;
 
+    // TODO MDL-55449 Drop support for stdClass in Moodle 3.6.
+    if ($eventdata instanceof \stdClass) {
+        if (!isset($eventdata->courseid)) {
+            $eventdata->courseid = null;
+        }
+
+        debugging('eventdata as \stdClass is deprecated. Please use core\message\message instead.', DEBUG_DEVELOPER);
+    }
+
     //new message ID to return
     $messageid = false;
 
@@ -119,6 +129,7 @@ function message_send($eventdata) {
 
     // Create the message object
     $savemessage = new stdClass();
+    $savemessage->courseid          = $eventdata->courseid;
     $savemessage->useridfrom        = $eventdata->userfrom->id;
     $savemessage->useridto          = $eventdata->userto->id;
     $savemessage->subject           = $eventdata->subject;
index 344a5bd..e3dc965 100644 (file)
@@ -424,7 +424,8 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
         $user2 = $this->getDataGenerator()->create_user();
 
         // Any core message will do here.
-        $message1 = new stdClass();
+        $message1 = new \core\message\message();
+        $message1->courseid          = 1;
         $message1->component         = 'moodle';
         $message1->name              = 'instantmessage';
         $message1->userfrom          = $user1;
@@ -436,7 +437,8 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
         $message1->smallmessage      = 'small message';
         $message1->notification      = 0;
 
-        $message2 = new stdClass();
+        $message2 = new \core\message\message();
+        $message2->courseid          = 1;
         $message2->component         = 'moodle';
         $message2->name              = 'instantmessage';
         $message2->userfrom          = $user2;
@@ -501,7 +503,8 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
 
         $sink = $this->redirectMessages();
 
-        $message3 = new stdClass();
+        $message3 = new \core\message\message();
+        $message3->courseid          = 1;
         $message3->component         = 'xxxx_yyyyy';
         $message3->name              = 'instantmessage';
         $message3->userfrom          = $user2;
@@ -547,7 +550,8 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
         $this->assertTrue(phpunit_util::is_redirecting_messages());
         $this->assertEquals(1, $sink->count());
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = get_admin();
index 8033546..edadee7 100644 (file)
@@ -1109,7 +1109,8 @@ function portfolio_insane_notify_admins($insane, $instances=false) {
     $smallbody = get_string('insanebodysmall', 'portfolio', $a);
 
     foreach ($admins as $admin) {
-        $eventdata = new stdClass();
+        $eventdata = new \core\message\message();
+        $eventdata->courseid = SITEID;
         $eventdata->modulename = 'portfolio';
         $eventdata->component = 'portfolio';
         $eventdata->name = 'notices';
index 7377f89..6cd05d0 100644 (file)
@@ -48,6 +48,7 @@ class core_message_testcase extends advanced_testcase {
         $user = $this->getDataGenerator()->create_user();
 
         $message = new \core\message\message();
+        $message->courseid = SITEID;
         $message->component = 'moodle';
         $message->name = 'instantmessage';
         $message->userfrom = $USER;
@@ -82,6 +83,7 @@ class core_message_testcase extends advanced_testcase {
 
         $stdclass = $message->get_eventobject_for_processor('test');
 
+        $this->assertSame($message->courseid, $stdclass->courseid);
         $this->assertSame($message->component, $stdclass->component);
         $this->assertSame($message->name, $stdclass->name);
         $this->assertSame($message->userfrom, $stdclass->userfrom);
@@ -143,6 +145,7 @@ class core_message_testcase extends advanced_testcase {
 
         // Extra content for all types of messages.
         $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = $user1;
@@ -171,9 +174,19 @@ class core_message_testcase extends advanced_testcase {
         $this->assertRegExp('/test message body test/', $email->body);
         $sink->clear();
 
+        // Test that event fired includes the courseid.
+        $eventsink = $this->redirectEvents();
+        $messageid = message_send($message);
+        $events = $eventsink->get_events();
+        $event = reset($events);
+        $this->assertEquals($message->courseid, $event->other['courseid']);
+        $eventsink->clear();
+        $sink->clear();
+
         // Extra content for small message only. Shouldn't show up in emails as we sent fullmessage and fullmessagehtml only in
         // the emails.
         $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = $user1;
@@ -199,6 +212,14 @@ class core_message_testcase extends advanced_testcase {
         $this->assertNotEmpty($email->header);
         $this->assertNotEmpty($email->body);
         $this->assertNotRegExp('/test message body test/', $email->body);
+
+        // Test that event fired includes the courseid.
+        $eventsink = $this->redirectEvents();
+        $messageid = message_send($message);
+        $events = $eventsink->get_events();
+        $event = reset($events);
+        $this->assertEquals($message->courseid, $event->other['courseid']);
+        $eventsink->close();
         $sink->close();
     }
 }
index ecc3dc5..c79d3cf 100644 (file)
@@ -37,7 +37,8 @@ class core_messagelib_testcase extends advanced_testcase {
         $preferences = get_message_output_default_preferences();
         $this->assertTrue($preferences->$disableprovidersetting == 1);
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = get_admin();
@@ -189,7 +190,8 @@ class core_messagelib_testcase extends advanced_testcase {
         $user2 = $this->getDataGenerator()->create_user();
 
         // Test basic message redirection.
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid = 1;
         $message->component = 'moodle';
         $message->name = 'instantmessage';
         $message->userfrom = $user1;
@@ -225,7 +227,8 @@ class core_messagelib_testcase extends advanced_testcase {
         $this->assertFalse($DB->record_exists('message', array()));
         $DB->delete_records('message_read', array());
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid = 1;
         $message->component = 'moodle';
         $message->name = 'instantmessage';
         $message->userfrom = $user1->id;
@@ -263,7 +266,8 @@ class core_messagelib_testcase extends advanced_testcase {
 
         // Test phpunit problem detection.
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid = 1;
         $message->component = 'xxxxx';
         $message->name = 'instantmessage';
         $message->userfrom = $user1;
@@ -298,7 +302,8 @@ class core_messagelib_testcase extends advanced_testcase {
 
         // Invalid users.
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid = 1;
         $message->component = 'moodle';
         $message->name = 'instantmessage';
         $message->userfrom = $user1;
@@ -314,7 +319,8 @@ class core_messagelib_testcase extends advanced_testcase {
         $this->assertFalse($messageid);
         $this->assertDebuggingCalled('Attempt to send msg to unknown user');
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid = 1;
         $message->component = 'moodle';
         $message->name = 'instantmessage';
         $message->userfrom = -1;
@@ -330,7 +336,8 @@ class core_messagelib_testcase extends advanced_testcase {
         $this->assertFalse($messageid);
         $this->assertDebuggingCalled('Attempt to send msg from unknown user');
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid = 1;
         $message->component = 'moodle';
         $message->name = 'instantmessage';
         $message->userfrom = $user1;
@@ -349,7 +356,8 @@ class core_messagelib_testcase extends advanced_testcase {
         // Some debugging hints for devs.
 
         unset($user2->emailstop);
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid = 1;
         $message->component = 'moodle';
         $message->name = 'instantmessage';
         $message->userfrom = $user1;
@@ -393,7 +401,8 @@ class core_messagelib_testcase extends advanced_testcase {
 
         set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'none', $user2);
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = $user1;
@@ -420,7 +429,8 @@ class core_messagelib_testcase extends advanced_testcase {
 
         $CFG->messaging = 0;
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = $user1;
@@ -447,7 +457,8 @@ class core_messagelib_testcase extends advanced_testcase {
 
         $CFG->messaging = 1;
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = $user1;
@@ -474,7 +485,8 @@ class core_messagelib_testcase extends advanced_testcase {
 
         set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = $user1;
@@ -504,7 +516,8 @@ class core_messagelib_testcase extends advanced_testcase {
 
         set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = $user1;
@@ -537,7 +550,8 @@ class core_messagelib_testcase extends advanced_testcase {
 
         set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email,popup', $user2);
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = $user1;
@@ -570,7 +584,8 @@ class core_messagelib_testcase extends advanced_testcase {
 
         set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'popup', $user2);
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = $user1;
@@ -604,7 +619,8 @@ class core_messagelib_testcase extends advanced_testcase {
 
         set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'none', $user2);
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = $user1;
@@ -635,7 +651,8 @@ class core_messagelib_testcase extends advanced_testcase {
 
         set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = $user1;
@@ -716,7 +733,8 @@ class core_messagelib_testcase extends advanced_testcase {
         $user1 = $this->getDataGenerator()->create_user();
         $user2 = $this->getDataGenerator()->create_user();
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = $user1;
@@ -778,7 +796,8 @@ class core_messagelib_testcase extends advanced_testcase {
         $user1 = $this->getDataGenerator()->create_user();
         $user2 = $this->getDataGenerator()->create_user();
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = $user1;
@@ -835,7 +854,8 @@ class core_messagelib_testcase extends advanced_testcase {
         );
         $file = $fs->create_file_from_string($filerecord, 'Test content');
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = 1;
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
         $message->userfrom          = get_admin();
index 1f659ac..f557986 100644 (file)
@@ -119,6 +119,12 @@ information provided here is intended especially for developers.
 * Webservice function mod_assign_get_submissions returns a new field 'gradingstatus' from each submission.
 * The return signature for the antivirus::scan_file() function has changed.
   The calling function will now handle removal of infected files from Moodle based on the new integer return value.
+* The first parameter $eventdata of both message_send() and \core\message\manager::send_message() should
+  be \core\message\message. Use of stdClass is deprecated.
+* The message_sent event now expects other[courseid] to be always set, exception otherwise. For BC with contrib code,
+  message_sent::create_from_ids() will show a debugging notice if the \core\message\message being sent is missing
+  the courseid property, defaulting to SITEID automatically. In Moodle 3.6 (MDL-55449) courseid will be fully mandatory
+  for all messages sent.
 
 === 3.1 ===
 
index cda6995..641a14e 100644 (file)
@@ -830,7 +830,8 @@ function message_format_contexturl($message) {
 function message_post_message($userfrom, $userto, $message, $format) {
     global $SITE, $CFG, $USER;
 
-    $eventdata = new stdClass();
+    $eventdata = new \core\message\message();
+    $eventdata->courseid         = 1;
     $eventdata->component        = 'moodle';
     $eventdata->name             = 'instantmessage';
     $eventdata->userfrom         = $userfrom;
index 1e3bc9e..4006738 100644 (file)
@@ -201,7 +201,8 @@ class core_message_events_testcase extends advanced_testcase {
             'context'  => context_system::instance(),
             'relateduserid' => 2,
             'other' => array(
-                'messageid' => 3
+                'messageid' => 3,
+                'courseid' => 4
             )
         ));
 
@@ -218,8 +219,66 @@ class core_message_events_testcase extends advanced_testcase {
         $this->assertEventLegacyLogData($expected, $event);
         $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
         $this->assertEquals($url, $event->get_url());
+        $this->assertEquals(3, $event->other['messageid']);
+        $this->assertEquals(4, $event->other['courseid']);
     }
 
+    public function test_mesage_sent_without_other_courseid() {
+
+        // Creating a message_sent event without other[courseid] leads to exception.
+        $this->expectException('coding_exception');
+        $this->expectExceptionMessage('The \'courseid\' value must be set in other');
+
+        $event = \core\event\message_sent::create(array(
+            'userid' => 1,
+            'context'  => context_system::instance(),
+            'relateduserid' => 2,
+            'other' => array(
+                'messageid' => 3,
+            )
+        ));
+    }
+
+    public function test_mesage_sent_via_create_from_ids() {
+        // Containing courseid.
+        $event = \core\event\message_sent::create_from_ids(1, 2, 3, 4);
+
+        // Trigger and capturing the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\message_sent', $event);
+        $this->assertEquals(context_system::instance(), $event->get_context());
+        $expected = array(SITEID, 'message', 'write', 'index.php?user=1&id=2&history=1#m3', 1);
+        $this->assertEventLegacyLogData($expected, $event);
+        $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
+        $this->assertEquals($url, $event->get_url());
+        $this->assertEquals(3, $event->other['messageid']);
+        $this->assertEquals(4, $event->other['courseid']);
+    }
+
+    public function test_mesage_sent_via_create_from_ids_without_other_courseid() {
+
+        // Creating a message_sent event without courseid leads to debugging + SITEID.
+        // TODO: MDL-55449 Ensure this leads to exception instead of debugging in Moodle 3.6.
+        $event = \core\event\message_sent::create_from_ids(1, 2, 3);
+
+        // Trigger and capturing the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        $this->assertDebuggingCalled();
+        $this->assertEquals(SITEID, $event->other['courseid']);
+    }
+
+
+
+
     /**
      * Test the message viewed event.
      */
index 2d0678a..e4d740c 100644 (file)
@@ -511,7 +511,8 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         // We are creating fake notifications but based on real ones.
 
         // This one omits notification = 1.
-        $eventdata = new stdClass();
+        $eventdata = new \core\message\message();
+        $eventdata->courseid          = $course->id;
         $eventdata->modulename        = 'moodle';
         $eventdata->component         = 'enrol_paypal';
         $eventdata->name              = 'paypal_enrolment';
@@ -524,7 +525,8 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $eventdata->smallmessage      = '';
         message_send($eventdata);
 
-        $message = new stdClass();
+        $message = new \core\message\message();
+        $message->courseid          = $course->id;
         $message->notification      = 1;
         $message->component         = 'enrol_manual';
         $message->name              = 'expiry_notification';
@@ -541,7 +543,8 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
 
         $userfrom = core_user::get_noreply_user();
         $userfrom->maildisplay = true;
-        $eventdata = new stdClass();
+        $eventdata = new \core\message\message();
+        $eventdata->courseid          = $course->id;
         $eventdata->component         = 'moodle';
         $eventdata->name              = 'badgecreatornotice';
         $eventdata->userfrom          = $userfrom;
@@ -554,7 +557,8 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $eventdata->smallmessage      = $eventdata->subject;
         message_send($eventdata);
 
-        $eventdata = new stdClass();
+        $eventdata = new \core\message\message();
+        $eventdata->courseid         = $course->id;
         $eventdata->name             = 'submission';
         $eventdata->component        = 'mod_feedback';
         $eventdata->userfrom         = $user1;
index 7b41803..191850e 100644 (file)
@@ -72,7 +72,8 @@ class message_received_search_testcase extends advanced_testcase {
         $this->preventResetByRollback();
         $sink = $this->redirectMessages();
 
-        $message = new StdClass();
+        $message = new \core\message\message();
+        $message->courseid = SITEID;
         $message->userfrom = $user1;
         $message->userto = $user2;
         $message->subject = "Test Subject";
@@ -129,7 +130,8 @@ class message_received_search_testcase extends advanced_testcase {
         $this->preventResetByRollback();
         $sink = $this->redirectMessages();
 
-        $message = new StdClass();
+        $message = new \core\message\message();
+        $message->courseid = SITEID;
         $message->userfrom = $user1;
         $message->userto = $user2;
         $message->subject = "Test Subject";
@@ -175,7 +177,8 @@ class message_received_search_testcase extends advanced_testcase {
         $this->preventResetByRollback();
         $sink = $this->redirectMessages();
 
-        $message = new StdClass();
+        $message = new \core\message\message();
+        $message->courseid = SITEID;
         $message->userfrom = $user1;
         $message->userto = $user2;
         $message->subject = "Test Subject";
index 9938ae1..7e9c887 100644 (file)
@@ -72,7 +72,8 @@ class message_sent_search_testcase extends advanced_testcase {
         $this->preventResetByRollback();
         $sink = $this->redirectMessages();
 
-        $message = new StdClass();
+        $message = new \core\message\message();
+        $message->courseid = SITEID;
         $message->userfrom = $user1;
         $message->userto = $user2;
         $message->subject = "Test Subject";
@@ -129,7 +130,8 @@ class message_sent_search_testcase extends advanced_testcase {
         $this->preventResetByRollback();
         $sink = $this->redirectMessages();
 
-        $message = new StdClass();
+        $message = new \core\message\message();
+        $message->courseid = SITEID;
         $message->userfrom = $user1;
         $message->userto = $user2;
         $message->subject = "Test Subject";
@@ -175,7 +177,8 @@ class message_sent_search_testcase extends advanced_testcase {
         $this->preventResetByRollback();
         $sink = $this->redirectMessages();
 
-        $message = new StdClass();
+        $message = new \core\message\message();
+        $message->courseid = SITEID;
         $message->userfrom = $user1;
         $message->userto = $user2;
         $message->subject = "Test Subject";
index d8e49ab..5f07def 100644 (file)
@@ -5475,7 +5475,8 @@ class assign {
                                                                $assignmentname);
         }
 
-        $eventdata = new stdClass();
+        $eventdata = new \core\message\message();
+        $eventdata->courseid         = $course->id;
         $eventdata->modulename       = 'assign';
         $eventdata->userfrom         = $userfrom;
         $eventdata->userto           = $userto;
index 6b189d9..f799e9f 100644 (file)
@@ -1246,6 +1246,40 @@ class mod_assign_locallib_testcase extends mod_assign_base_testcase {
         $this->assertEquals($assign->get_instance()->name, $messages[0]->contexturlname);
     }
 
+    public function test_cron_message_includes_courseid() {
+        // First run cron so there are no messages waiting to be sent (from other tests).
+        cron_setup_user();
+        assign::cron();
+
+        // Now create an assignment.
+        $this->setUser($this->editingteachers[0]);
+        $assign = $this->create_instance(array('sendstudentnotifications' => 1));
+
+        // Simulate adding a grade.
+        $this->setUser($this->teachers[0]);
+        $data = new stdClass();
+        $data->grade = '50.0';
+        $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
+
+        $this->preventResetByRollback();
+        $sink = $this->redirectEvents();
+        $this->expectOutputRegex('/Done processing 1 assignment submissions/');
+
+        assign::cron();
+
+        $events = $sink->get_events();
+        // Two messages are sent, one to student and one to teacher. This generates
+        // four events:
+        // core\event\message_sent
+        // core\event\message_viewed
+        // core\event\message_sent
+        // core\event\message_viewed.
+        $event = reset($events);
+        $this->assertInstanceOf('\core\event\message_sent', $event);
+        $this->assertEquals($assign->get_course()->id, $event->other['courseid']);
+        $sink->close();
+    }
+
     public function test_is_graded() {
         $this->setUser($this->editingteachers[0]);
         $assign = $this->create_instance();
index 45edfba..cee0cc6 100644 (file)
@@ -3027,7 +3027,8 @@ function feedback_send_email($cm, $feedback, $course, $user) {
             }
 
             if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
-                $eventdata = new stdClass();
+                $eventdata = new \core\message\message();
+                $eventdata->courseid         = $course->id;
                 $eventdata->name             = 'submission';
                 $eventdata->component        = 'mod_feedback';
                 $eventdata->userfrom         = $user;
@@ -3037,9 +3038,13 @@ function feedback_send_email($cm, $feedback, $course, $user) {
                 $eventdata->fullmessageformat = FORMAT_PLAIN;
                 $eventdata->fullmessagehtml  = $posthtml;
                 $eventdata->smallmessage     = '';
+                $eventdata->courseid         = $course->id;
+                $eventdata->contexturl       = $info->url;
+                $eventdata->contexturlname   = $info->feedback;
                 message_send($eventdata);
             } else {
-                $eventdata = new stdClass();
+                $eventdata = new \core\message\message();
+                $eventdata->courseid         = $course->id;
                 $eventdata->name             = 'submission';
                 $eventdata->component        = 'mod_feedback';
                 $eventdata->userfrom         = $teacher;
@@ -3049,6 +3054,9 @@ function feedback_send_email($cm, $feedback, $course, $user) {
                 $eventdata->fullmessageformat = FORMAT_PLAIN;
                 $eventdata->fullmessagehtml  = $posthtml;
                 $eventdata->smallmessage     = '';
+                $eventdata->courseid         = $course->id;
+                $eventdata->contexturl       = $info->url;
+                $eventdata->contexturlname   = $info->feedback;
                 message_send($eventdata);
             }
         }
@@ -3097,7 +3105,8 @@ function feedback_send_email_anonym($cm, $feedback, $course) {
                 $posthtml = '';
             }
 
-            $eventdata = new stdClass();
+            $eventdata = new \core\message\message();
+            $eventdata->courseid         = $course->id;
             $eventdata->name             = 'submission';
             $eventdata->component        = 'mod_feedback';
             $eventdata->userfrom         = $teacher;
@@ -3107,6 +3116,9 @@ function feedback_send_email_anonym($cm, $feedback, $course) {
             $eventdata->fullmessageformat = FORMAT_PLAIN;
             $eventdata->fullmessagehtml  = $posthtml;
             $eventdata->smallmessage     = '';
+            $eventdata->courseid         = $course->id;
+            $eventdata->contexturl       = $info->url;
+            $eventdata->contexturlname   = $info->feedback;
             message_send($eventdata);
         }
     }
index 7984486..50f89fd 100644 (file)
@@ -97,7 +97,8 @@ if ($action == 'sendmessage' AND has_capability('moodle/course:bulkmessaging', $
     if (is_array($messageuser)) {
         foreach ($messageuser as $userid) {
             $senduser = $DB->get_record('user', array('id'=>$userid));
-            $eventdata = new stdClass();
+            $eventdata = new \core\message\message();
+            $eventdata->courseid         = $course->id;
             $eventdata->name             = 'message';
             $eventdata->component        = 'mod_feedback';
             $eventdata->userfrom         = $USER;
@@ -107,6 +108,9 @@ if ($action == 'sendmessage' AND has_capability('moodle/course:bulkmessaging', $
             $eventdata->fullmessageformat = FORMAT_PLAIN;
             $eventdata->fullmessagehtml  = $htmlmessage;
             $eventdata->smallmessage     = '';
+            $eventdata->courseid         = $course->id;
+            $eventdata->contexturl       = $link3;
+            $eventdata->contexturlname   = $feedback->name;
             $good = $good && message_send($eventdata);
         }
         if (!empty($good)) {
index ab4a4ae..8e7cd89 100644 (file)
@@ -834,6 +834,7 @@ function forum_cron() {
                 mtrace('Sending ', '');
 
                 $eventdata = new \core\message\message();
+                $eventdata->courseid            = $course->id;
                 $eventdata->component           = 'mod_forum';
                 $eventdata->name                = 'posts';
                 $eventdata->userfrom            = $userfrom;
@@ -1185,6 +1186,7 @@ function forum_cron() {
                 }
 
                 $eventdata = new \core\message\message();
+                $eventdata->courseid            = SITEID;
                 $eventdata->component           = 'mod_forum';
                 $eventdata->name                = 'digests';
                 $eventdata->userfrom            = core_user::get_noreply_user();
index 4c88100..d77a9df 100644 (file)
@@ -252,6 +252,43 @@ class mod_forum_mail_testcase extends advanced_testcase {
         return $messages;
     }
 
+    public function test_cron_message_includes_courseid() {
+        $this->resetAfterTest(true);
+
+        // Create a course, with a forum.
+        $course = $this->getDataGenerator()->create_course();
+
+        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_FORCESUBSCRIBE);
+        $forum = $this->getDataGenerator()->create_module('forum', $options);
+
+        // Create two users enrolled in the course as students.
+        list($author, $recipient) = $this->helper_create_users($course, 2);
+
+        // Post a discussion to the forum.
+        list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
+
+        // Run cron and check that \core\event\message_sent contains the course id.
+        // Close the message sink so that message_send is run.
+        $this->helper->messagesink->close();
+
+        // Catch just the cron events. For each message sent two events are fired:
+        // core\event\message_sent
+        // core\event\message_viewed.
+        $this->helper->eventsink = $this->redirectEvents();
+        $this->expectOutputRegex('/Processing user/');
+
+        forum_cron();
+
+        // Get the events and close the sink so that remaining events can be triggered.
+        $events = $this->helper->eventsink->get_events();
+        $this->helper->eventsink->close();
+
+        // Reset the message sink for other tests.
+        $this->helper->messagesink = $this->redirectMessages();
+        $event = reset($events);
+        $this->assertEquals($course->id, $event->other['courseid']);
+    }
+
     public function test_forced_subscription() {
         $this->resetAfterTest(true);
 
index ee2fb4c..e741343 100644 (file)
@@ -263,7 +263,11 @@ switch ($mode) {
                 // Subject
                 $subject = get_string('essayemailsubject', 'lesson');
 
-                $eventdata = new stdClass();
+                // Context url.
+                $contexturl = new moodle_url('/grade/report/user/index.php', array('id' => $course->id));
+
+                $eventdata = new \core\message\message();
+                $eventdata->courseid         = $course->id;
                 $eventdata->modulename       = 'lesson';
                 $eventdata->userfrom         = $USER;
                 $eventdata->userto           = $users[$attempt->userid];
@@ -272,6 +276,7 @@ switch ($mode) {
                 $eventdata->fullmessageformat = FORMAT_PLAIN;
                 $eventdata->fullmessagehtml  = $message;
                 $eventdata->smallmessage     = '';
+                $eventdata->contexturl       = $contexturl;
 
                 // Required for messaging framework
                 $eventdata->component = 'mod_lesson';
index d6f4033..7584894 100644 (file)
@@ -1487,7 +1487,8 @@ function quiz_send_confirmation($recipient, $a) {
     $a->userusername = $recipient->username;
 
     // Prepare the message.
-    $eventdata = new stdClass();
+    $eventdata = new \core\message\message();
+    $eventdata->courseid          = $a->courseid;
     $eventdata->component         = 'mod_quiz';
     $eventdata->name              = 'confirmation';
     $eventdata->notification      = 1;
@@ -1523,7 +1524,8 @@ function quiz_send_notification($recipient, $submitter, $a) {
     $a->userusername = $recipient->username;
 
     // Prepare the message.
-    $eventdata = new stdClass();
+    $eventdata = new \core\message\message();
+    $eventdata->courseid          = $a->courseid;
     $eventdata->component         = 'mod_quiz';
     $eventdata->name              = 'submission';
     $eventdata->notification      = 1;
@@ -1596,6 +1598,7 @@ function quiz_send_notification_messages($course, $quiz, $attempt, $context, $cm
 
     $a = new stdClass();
     // Course info.
+    $a->courseid        = $course->id;
     $a->coursename      = $course->fullname;
     $a->courseshortname = $course->shortname;
     // Quiz info.
@@ -1670,6 +1673,7 @@ function quiz_send_overdue_message($attemptobj) {
 
     $a = new stdClass();
     // Course info.
+    $a->courseid           = $attemptobj->get_course()->id;
     $a->coursename         = format_string($attemptobj->get_course()->fullname);
     $a->courseshortname    = format_string($attemptobj->get_course()->shortname);
     // Quiz info.
@@ -1687,7 +1691,8 @@ function quiz_send_overdue_message($attemptobj) {
     $a->studentusername    = $submitter->username;
 
     // Prepare the message.
-    $eventdata = new stdClass();
+    $eventdata = new \core\message\message();
+    $eventdata->courseid          = $a->courseid;
     $eventdata->component         = 'mod_quiz';
     $eventdata->name              = 'attempt_overdue';
     $eventdata->notification      = 1;