From be016b0144ce41e37bf3ed146bcf02780d148030 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 10 Sep 2018 15:04:15 +0800 Subject: [PATCH] MDL-63211 core: added notification when contact request is made --- lang/en/message.php | 2 ++ lang/en/moodle.php | 1 + lib/db/messages.php | 10 +++++++++- message/classes/api.php | 25 +++++++++++++++++++++++++ message/tests/privacy_provider_test.php | 18 ++++++++++++------ version.php | 2 +- 6 files changed, 50 insertions(+), 8 deletions(-) diff --git a/lang/en/message.php b/lang/en/message.php index d8bcf7fa751..41fb26ea3d1 100644 --- a/lang/en/message.php +++ b/lang/en/message.php @@ -71,6 +71,8 @@ $string['managemessageoutputs'] = 'Manage message outputs'; $string['messageoutputs'] = 'Message outputs'; $string['messagepreferences'] = 'Message preferences'; $string['message'] = 'Message'; +$string['messagecontactrequestsnotification'] = '{$a} wants to be added as a contact'; +$string['messagecontactrequestsnotificationsubject'] = '{$a} wants to be added as a contact'; $string['messagepreferences'] = 'Message preferences'; $string['messages'] = 'Messages'; $string['messagingdatahasnotbeenmigrated'] = 'Your messages are temporarily unavailable due to upgrades in the messaging infrastructure. Please wait for them to be migrated.'; diff --git a/lang/en/moodle.php b/lang/en/moodle.php index eb72e18b41f..1e5b7464050 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -1194,6 +1194,7 @@ $string['messageprovider:courserequested'] = 'Course creation request notificati $string['messageprovider:courserequestrejected'] = 'Course creation request rejection notification'; $string['messageprovider:errors'] = 'Important errors with the site'; $string['messageprovider:errors_help'] = 'These are important errors that an administrator should know about.'; +$string['messageprovider:messagecontactrequests'] = 'Message contact requests notification'; $string['messageprovider:notices'] = 'Notices about minor problems'; $string['messageprovider:notices_help'] = 'These are notices that an administrator might be interested in seeing.'; $string['messageprovider:insights'] = 'Insights generated by prediction models'; diff --git a/lib/db/messages.php b/lib/db/messages.php index 68ac95f77a0..7a088411c94 100644 --- a/lib/db/messages.php +++ b/lib/db/messages.php @@ -105,5 +105,13 @@ $messageproviders = array ( // User insights. 'insights' => array ( 'capability' => 'moodle/analytics:listinsights' - ) + ), + + // Message contact requests. + 'messagecontactrequests' => [ + 'defaults' => [ + 'popup' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF, + 'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDOFF, + ] + ], ); diff --git a/message/classes/api.php b/message/classes/api.php index d9e69abf01a..83171eff361 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -1324,6 +1324,31 @@ class api { $request->timecreated = time(); $DB->insert_record('message_contact_requests', $request); + + // Send a notification. + $userfrom = \core_user::get_user($userid); + $userfromfullname = fullname($userfrom); + $userto = \core_user::get_user($requesteduserid); + $url = new \moodle_url('/message/pendingcontactrequests.php'); + + $subject = get_string('messagecontactrequestsnotificationsubject', 'core_message', $userfromfullname); + $fullmessage = get_string('messagecontactrequestsnotification', 'core_message', $userfromfullname); + + $message = new \core\message\message(); + $message->courseid = SITEID; + $message->component = 'moodle'; + $message->name = 'messagecontactrequests'; + $message->notification = 1; + $message->userfrom = $userfrom; + $message->userto = $userto; + $message->subject = $subject; + $message->fullmessage = text_to_html($fullmessage); + $message->fullmessageformat = FORMAT_HTML; + $message->fullmessagehtml = $fullmessage; + $message->smallmessage = ''; + $message->contexturl = $url->out(false); + + message_send($message); } diff --git a/message/tests/privacy_provider_test.php b/message/tests/privacy_provider_test.php index 8201d92e4e2..3500c79c306 100644 --- a/message/tests/privacy_provider_test.php +++ b/message/tests/privacy_provider_test.php @@ -509,8 +509,8 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide // There should be two conversation members. $this->assertEquals(2, $DB->count_records('message_conversation_members')); - // There should be two notifications. - $this->assertEquals(2, $DB->count_records('notifications')); + // There should be two notifications + one for the contact request. + $this->assertEquals(3, $DB->count_records('notifications')); provider::delete_data_for_all_users_in_context($systemcontext); @@ -585,8 +585,8 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide // There should be two conversation members. $this->assertEquals(2, $DB->count_records('message_conversation_members')); - // There should be three notifications. - $this->assertEquals(3, $DB->count_records('notifications')); + // There should be three notifications + two for the contact requests. + $this->assertEquals(5, $DB->count_records('notifications')); $systemcontext = \context_system::instance(); $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_message', @@ -631,8 +631,14 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide $mcm = reset($mcms); $this->assertEquals($user2->id, $mcm->userid); - $this->assertCount(1, $notifications); - $notification = reset($notifications); + $this->assertCount(2, $notifications); + ksort($notifications); + + $notification = array_shift($notifications); + $this->assertEquals($user2->id, $notification->useridfrom); + $this->assertEquals($user4->id, $notification->useridto); + + $notification = array_shift($notifications); $this->assertEquals($user2->id, $notification->useridfrom); $this->assertEquals($user3->id, $notification->useridto); } diff --git a/version.php b/version.php index a659fed374e..39a68644a10 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2018092800.04; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2018092800.05; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.