From f5d17c14eac1052ed2ab88f6e1ee54de4388982d Mon Sep 17 00:00:00 2001 From: Yuliya Bozhko Date: Tue, 24 Sep 2013 16:09:44 +1200 Subject: [PATCH 1/2] MDL-39923 badges: Send badge messages through internal messaging system --- badges/cron.php | 19 ++++++----------- badges/edit_form.php | 3 +++ lang/en/badges.php | 2 +- lang/en/moodle.php | 2 ++ lib/badgeslib.php | 51 ++++++++++++++++++++++++++++---------------- lib/db/messages.php | 17 ++++++++++++++- version.php | 2 +- 7 files changed, 63 insertions(+), 33 deletions(-) diff --git a/badges/cron.php b/badges/cron.php index a1dcfcc1f1515..a0bee0f93f8da 100644 --- a/badges/cron.php +++ b/badges/cron.php @@ -116,14 +116,9 @@ function badge_message_cron() { * @param object $badge A badge which is notified about. */ function badge_assemble_notification(stdClass $badge) { - global $CFG, $DB; - - $admin = get_admin(); - $userfrom = new stdClass(); - $userfrom->id = $admin->id; - $userfrom->email = !empty($CFG->badges_defaultissuercontact) ? $CFG->badges_defaultissuercontact : $admin->email; - $userfrom->firstname = !empty($CFG->badges_defaultissuername) ? $CFG->badges_defaultissuername : $admin->firstname; - $userfrom->lastname = !empty($CFG->badges_defaultissuername) ? '' : $admin->lastname; + global $DB; + + $userfrom = core_user::get_noreply_user(); $userfrom->maildisplay = true; if ($msgs = $DB->get_records_select('badge_issued', 'issuernotified IS NULL AND badgeid = ?', array($badge->id))) { @@ -147,15 +142,15 @@ function badge_assemble_notification(stdClass $badge) { // Create a message object. $eventdata = new stdClass(); $eventdata->component = 'moodle'; - $eventdata->name = 'instantmessage'; + $eventdata->name = 'badgecreatornotice'; $eventdata->userfrom = $userfrom; $eventdata->userto = $creator; $eventdata->notification = 1; $eventdata->subject = $creatorsubject; - $eventdata->fullmessage = $creatormessage; + $eventdata->fullmessage = format_text_email($creatormessage, FORMAT_HTML); $eventdata->fullmessageformat = FORMAT_PLAIN; - $eventdata->fullmessagehtml = format_text($creatormessage, FORMAT_HTML); - $eventdata->smallmessage = ''; + $eventdata->fullmessagehtml = $creatormessage; + $eventdata->smallmessage = $creatorsubject; message_send($eventdata); } diff --git a/badges/edit_form.php b/badges/edit_form.php index 7ebe2f4e8bb62..24e52d7ee058a 100644 --- a/badges/edit_form.php +++ b/badges/edit_form.php @@ -225,6 +225,9 @@ public function definition() { $mform->addElement('advcheckbox', 'attachment', get_string('attachment', 'badges'), '', null, array(0, 1)); $mform->addHelpButton('attachment', 'attachment', 'badges'); + if (empty($CFG->allowattachments)) { + $mform->freeze('attachment'); + } $options = array( BADGE_MESSAGE_NEVER => get_string('never'), diff --git a/lang/en/badges.php b/lang/en/badges.php index 3065685c2d760..2e3dfa2505890 100644 --- a/lang/en/badges.php +++ b/lang/en/badges.php @@ -56,7 +56,7 @@ $string['anymethodmanual'] = 'Any of the selected roles awards the badge'; $string['anymethodprofile'] = 'Any of the selected profile fields has been completed'; $string['attachment'] = 'Attach badge to message'; -$string['attachment_help'] = 'If checked, an issued badge will be attached to the recepient\'s email for download'; +$string['attachment_help'] = 'If checked, an issued badge file will be attached to the recepient\'s email for download. Email attachments must be enabled in site settings to use this option.'; $string['award'] = 'Award badge'; $string['awardedtoyou'] = 'Issued to me'; $string['awardoncron'] = 'Access to the badges was successfully enabled. Too many users can instantly earn this badge. To ensure site performance, this action will take some time to process.'; diff --git a/lang/en/moodle.php b/lang/en/moodle.php index a95d2e7a76f1b..8156424c0bef8 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -1087,6 +1087,8 @@ $string['messagedselectedusersfailed'] = 'Something went wrong while messaging selected users. Some may have received the email.'; $string['messageprovider:availableupdate'] = 'Available update notifications'; $string['messageprovider:backup'] = 'Backup notifications'; +$string['messageprovider:badgecreatornotice'] = 'Badge creator notifications'; +$string['messageprovider:badgerecipientnotice'] = 'Badge recipient notifications'; $string['messageprovider:courserequestapproved'] = 'Course creation request approval notification'; $string['messageprovider:courserequested'] = 'Course creation request notification'; $string['messageprovider:courserequestrejected'] = 'Course creation request rejection notification'; diff --git a/lib/badgeslib.php b/lib/badgeslib.php index 26d44478fb929..53db86bf298f4 100644 --- a/lib/badgeslib.php +++ b/lib/badgeslib.php @@ -632,27 +632,36 @@ function badges_notify_badge_award(badge $badge, $userid, $issued, $filepathhash $message = badge_message_from_template($badge->message, $params); $plaintext = format_text_email($message, FORMAT_HTML); - // TODO: $filepathhash may be moodle_url instance too... - - if ($badge->attachment && is_string($filepathhash)) { + // Notify recipient. + $eventdata = new stdClass(); + $eventdata->component = 'moodle'; + $eventdata->name = 'badgerecipientnotice'; + $eventdata->userfrom = $userfrom; + $eventdata->userto = $userto; + $eventdata->notification = 1; + $eventdata->subject = $badge->messagesubject; + $eventdata->fullmessage = $plaintext; + $eventdata->fullmessageformat = FORMAT_PLAIN; + $eventdata->fullmessagehtml = $message; + $eventdata->smallmessage = $plaintext; + + // Attach badge image if possible. + if (!empty($CFG->allowattachments) && $badge->attachment && is_string($filepathhash)) { $fs = get_file_storage(); $file = $fs->get_file_by_hash($filepathhash); - $attachment = $file->copy_content_to_temp(); - email_to_user($userto, - $userfrom, - $badge->messagesubject, - $plaintext, - $message, - str_replace($CFG->dataroot, '', $attachment), - str_replace(' ', '_', $badge->name) . ".png" - ); - @unlink($attachment); + $eventdata->attachment = $file; + $eventdata->attachname = str_replace(' ', '_', $badge->name) . ".png"; + + message_send($eventdata); } else { - email_to_user($userto, $userfrom, $badge->messagesubject, $plaintext, $message); + message_send($eventdata); } // Notify badge creator about the award if they receive notifications every time. if ($badge->notification == 1) { + $userfrom = core_user::get_noreply_user(); + $userfrom->maildisplay = true; + $creator = $DB->get_record('user', array('id' => $badge->usercreated), '*', MUST_EXIST); $a = new stdClass(); $a->user = fullname($userto); @@ -662,15 +671,15 @@ function badges_notify_badge_award(badge $badge, $userid, $issued, $filepathhash $eventdata = new stdClass(); $eventdata->component = 'moodle'; - $eventdata->name = 'instantmessage'; + $eventdata->name = 'badgecreatornotice'; $eventdata->userfrom = $userfrom; $eventdata->userto = $creator; $eventdata->notification = 1; $eventdata->subject = $creatorsubject; - $eventdata->fullmessage = $creatormessage; + $eventdata->fullmessage = format_text_email($creatormessage, FORMAT_HTML); $eventdata->fullmessageformat = FORMAT_PLAIN; - $eventdata->fullmessagehtml = format_text($creatormessage, FORMAT_HTML); - $eventdata->smallmessage = ''; + $eventdata->fullmessagehtml = $creatormessage; + $eventdata->smallmessage = $creatorsubject; message_send($eventdata); $DB->set_field('badge_issued', 'issuernotified', time(), array('badgeid' => $badge->id, 'userid' => $userid)); @@ -973,6 +982,12 @@ function badges_bake($hash, $badgeid, $userid = 0, $pathhash = false) { } } + // If file exists and we just need its path hash, return it. + if ($pathhash) { + $file = $fs->get_file($user_context->id, 'badges', 'userbadge', $badge->id, '/', $hash . '.png'); + return $file->get_pathnamehash(); + } + $fileurl = moodle_url::make_pluginfile_url($user_context->id, 'badges', 'userbadge', $badge->id, '/', $hash, true); return $fileurl; } diff --git a/lib/db/messages.php b/lib/db/messages.php index 88e99c58d2d3b..75afbb7370a0f 100644 --- a/lib/db/messages.php +++ b/lib/db/messages.php @@ -78,6 +78,21 @@ // Course request rejection notification 'courserequestrejected' => array ( 'capability' => 'moodle/course:request' - ) + ), + // Badge award notification to a badge recipient. + 'badgerecipientnotice' => array ( + 'defaults' => array( + 'popup' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF, + 'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDOFF, + ), + 'capability' => 'moodle/badges:earnbadge' + ), + + // Badge award notification to a badge creator (mostly cron-based). + 'badgecreatornotice' => array ( + 'defaults' => array( + 'email' => MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDOFF, + ) + ) ); diff --git a/version.php b/version.php index 7ccc3a517fee2..50f52bbd140b4 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2013110600.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2013110600.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. From be2b37cf4b434848ac481f068a4850aece794050 Mon Sep 17 00:00:00 2001 From: Yuliya Date: Thu, 7 Nov 2013 21:44:15 +1300 Subject: [PATCH 2/2] MDL-39923 badges: Fix unit tests --- badges/tests/badgeslib_test.php | 5 +++++ lib/badgeslib.php | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/badges/tests/badgeslib_test.php b/badges/tests/badgeslib_test.php index cc1a0d47075ac..b70d377f1b004 100644 --- a/badges/tests/badgeslib_test.php +++ b/badges/tests/badgeslib_test.php @@ -166,6 +166,7 @@ public function test_delete_badge_criteria() { } public function test_badge_awards() { + $this->preventResetByRollback(); // Messaging is not compatible with transactions. $badge = new badge($this->badgeid); $user1 = $this->getDataGenerator()->create_user(); @@ -225,6 +226,7 @@ public function test_badge_message_from_template($message, $params, $result) { * Test badges observer when course module completion event id fired. */ public function test_badges_observer_course_module_criteria_review() { + $this->preventResetByRollback(); // Messaging is not compatible with transactions. $badge = new badge($this->coursebadge); $this->assertFalse($badge->is_issued($this->user->id)); @@ -257,6 +259,7 @@ public function test_badges_observer_course_module_criteria_review() { * Test badges observer when course_completed event is fired. */ public function test_badges_observer_course_criteria_review() { + $this->preventResetByRollback(); // Messaging is not compatible with transactions. $badge = new badge($this->coursebadge); $this->assertFalse($badge->is_issued($this->user->id)); @@ -282,6 +285,7 @@ public function test_badges_observer_course_criteria_review() { * Test badges observer when user_updated event is fired. */ public function test_badges_observer_profile_criteria_review() { + $this->preventResetByRollback(); // Messaging is not compatible with transactions. $badge = new badge($this->coursebadge); $this->assertFalse($badge->is_issued($this->user->id)); @@ -304,6 +308,7 @@ public function test_badges_observer_profile_criteria_review() { * Test badges assertion generated when a badge is issued. */ public function test_badges_assertion() { + $this->preventResetByRollback(); // Messaging is not compatible with transactions. $badge = new badge($this->coursebadge); $this->assertFalse($badge->is_issued($this->user->id)); diff --git a/lib/badgeslib.php b/lib/badgeslib.php index 53db86bf298f4..8219cdb854866 100644 --- a/lib/badgeslib.php +++ b/lib/badgeslib.php @@ -978,7 +978,8 @@ function badges_bake($hash, $badgeid, $userid = 0, $pathhash = false) { } } } else { - debugging('Error baking badge image!'); + debugging('Error baking badge image!', DEBUG_DEVELOPER); + return; } }