Skip to content

Commit

Permalink
Merge branch 'MDL-39923_master' of https://github.com/totara/openbadges
Browse files Browse the repository at this point in the history
Conflicts:
	version.php
  • Loading branch information
Damyon Wiese committed Nov 11, 2013
2 parents f845946 + be2b37c commit d5c0f04
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 34 deletions.
19 changes: 7 additions & 12 deletions badges/cron.php
Expand Up @@ -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))) {
Expand All @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions badges/edit_form.php
Expand Up @@ -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'),
Expand Down
5 changes: 5 additions & 0 deletions badges/tests/badgeslib_test.php
Expand Up @@ -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();

Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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));

Expand All @@ -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));

Expand All @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion lang/en/badges.php
Expand Up @@ -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.';
Expand Down
2 changes: 2 additions & 0 deletions lang/en/moodle.php
Expand Up @@ -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';
Expand Down
54 changes: 35 additions & 19 deletions lib/badgeslib.php
Expand Up @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -969,10 +978,17 @@ function badges_bake($hash, $badgeid, $userid = 0, $pathhash = false) {
}
}
} else {
debugging('Error baking badge image!');
debugging('Error baking badge image!', DEBUG_DEVELOPER);
return;
}
}

// 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;
}
Expand Down
17 changes: 16 additions & 1 deletion lib/db/messages.php
Expand Up @@ -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,
)
)
);
2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2013110600.02; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2013111100.00; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit d5c0f04

Please sign in to comment.