Skip to content

Commit

Permalink
MDL-45734 Badges: Add additional events
Browse files Browse the repository at this point in the history
  • Loading branch information
sbourget committed Jul 30, 2016
1 parent d1a3ea6 commit a0e1387
Show file tree
Hide file tree
Showing 16 changed files with 1,376 additions and 5 deletions.
24 changes: 22 additions & 2 deletions badges/criteria/award_criteria.php
Expand Up @@ -332,7 +332,7 @@ public function get_params($cid) {
*
*/
public function delete() {
global $DB;
global $DB, $PAGE;

// Remove any records if it has already been met.
$DB->delete_records('badge_criteria_met', array('critid' => $this->id));
Expand All @@ -342,6 +342,13 @@ public function delete() {

// Finally remove criterion itself.
$DB->delete_records('badge_criteria', array('id' => $this->id));

// Trigger event, badge criteria deleted.
$eventparams = array('objectid' => $this->id,
'context' => $PAGE->context,
'other' => array('badgeid' => $this->badgeid));
$event = \core\event\badge_criteria_deleted::create($eventparams);
$event->trigger();
}

/**
Expand All @@ -350,7 +357,7 @@ public function delete() {
* @param array $params Values from the form or any other array.
*/
public function save($params = array()) {
global $DB;
global $DB, $PAGE;

// Figure out criteria description.
// If it is coming from the form editor, it is an array(text, format).
Expand Down Expand Up @@ -386,6 +393,13 @@ public function save($params = array()) {
$fordb->id = $cid;
$DB->update_record('badge_criteria', $fordb, true);

// Trigger event: badge_criteria_updated.
$eventparams = array('objectid' => $this->id,
'context' => $PAGE->context,
'other' => array('badgeid' => $this->badgeid));
$event = \core\event\badge_criteria_updated::create($eventparams);
$event->trigger();

$existing = $DB->get_fieldset_select('badge_criteria_param', 'name', 'critid = ?', array($cid));
$todelete = array_diff($existing, $requiredkeys);

Expand Down Expand Up @@ -429,6 +443,12 @@ public function save($params = array()) {
$DB->insert_record('badge_criteria_param', $newp, false, true);
}
}
// Trigger event: badge_criteria_created.
$eventparams = array('objectid' => $this->id,
'context' => $PAGE->context,
'other' => array('badgeid' => $this->badgeid));
$event = \core\event\badge_criteria_created::create($eventparams);
$event->trigger();
}
$t->allow_commit();
}
Expand Down
5 changes: 5 additions & 0 deletions badges/newbadge.php
Expand Up @@ -98,6 +98,11 @@

$newid = $DB->insert_record('badge', $fordb, true);

// Trigger event, badge created.
$eventparams = array('objectid' => $newid, 'context' => $PAGE->context);
$event = \core\event\badge_created::create($eventparams);
$event->trigger();

$newbadge = new badge($newid);
badges_process_badge_image($newbadge, $form->save_temp_file('image'));
// If a user can configure badge criteria, they will be redirected to the criteria page.
Expand Down
257 changes: 256 additions & 1 deletion badges/tests/events_test.php
Expand Up @@ -55,4 +55,259 @@ public function test_badge_awarded() {

$sink->close();
}
}

/**
* Test the badge created event.
*
* There is no external API for creating a badge, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_badge_created() {

$badge = new badge($this->badgeid);
// Trigger an event: badge created.
$eventparams = array(
'userid' => $badge->usercreated,
'objectid' => $badge->id,
'context' => $badge->get_context(),
);

$event = \core\event\badge_created::create($eventparams);
// Trigger and capture 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\badge_created', $event);
$this->assertEquals($badge->usercreated, $event->userid);
$this->assertEquals($badge->id, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();

}

/**
* Test the badge archived event.
*
*/
public function test_badge_archived() {
$badge = new badge($this->badgeid);
$sink = $this->redirectEvents();

// Trigger and capture the event.
$badge->delete(true);
$events = $sink->get_events();
$this->assertCount(2, $events);
$event = $events[1];

// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_archived', $event);
$this->assertEquals($badge->id, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();

}


/**
* Test the badge updated event.
*
*/
public function test_badge_updated() {
$badge = new badge($this->badgeid);
$sink = $this->redirectEvents();

// Trigger and capture the event.
$badge->save();
$events = $sink->get_events();
$event = reset($events);
$this->assertCount(1, $events);

// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_updated', $event);
$this->assertEquals($badge->id, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();

}
/**
* Test the badge deleted event.
*/
public function test_badge_deleted() {
$badge = new badge($this->badgeid);
$sink = $this->redirectEvents();

// Trigger and capture the event.
$badge->delete(false);
$events = $sink->get_events();
$event = reset($events);
$this->assertCount(1, $events);

// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_deleted', $event);
$this->assertEquals($badge->id, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();

}

/**
* Test the badge duplicated event.
*
*/
public function test_badge_duplicated() {
$badge = new badge($this->badgeid);
$sink = $this->redirectEvents();

// Trigger and capture the event.
$newid = $badge->make_clone();
$events = $sink->get_events();
$event = reset($events);
$this->assertCount(1, $events);

// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_duplicated', $event);
$this->assertEquals($newid, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();

}

/**
* Test the badge disabled event.
*
*/
public function test_badge_disabled() {
$badge = new badge($this->badgeid);
$sink = $this->redirectEvents();

// Trigger and capture the event.
$badge->set_status(BADGE_STATUS_INACTIVE);
$events = $sink->get_events();
$event = reset($events);
$this->assertCount(2, $events);
$event = $events[1];

// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_disabled', $event);
$this->assertEquals($badge->id, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();

}

/**
* Test the badge enabled event.
*
*/
public function test_badge_enabled() {
$badge = new badge($this->badgeid);
$sink = $this->redirectEvents();

// Trigger and capture the event.
$badge->set_status(BADGE_STATUS_ACTIVE);
$events = $sink->get_events();
$event = reset($events);
$this->assertCount(2, $events);
$event = $events[1];

// Check that the event data is valid.
$this->assertInstanceOf('\core\event\badge_enabled', $event);
$this->assertEquals($badge->id, $event->objectid);
$this->assertDebuggingNotCalled();
$sink->close();

}

/**
* Test the badge criteria created event.
*
* There is no external API for this, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_badge_criteria_created() {

$badge = new badge($this->badgeid);

// Trigger and capture the event.
$sink = $this->redirectEvents();
$criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
$criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
$criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
$params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
$criteriaprofile->save($params);
$events = $sink->get_events();
$event = reset($events);

// Check that the event data is valid.
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\badge_criteria_created', $event);
$this->assertEquals($criteriaprofile->id, $event->objectid);
$this->assertEquals($criteriaprofile->badgeid, $event->other['badgeid']);
$this->assertDebuggingNotCalled();
$sink->close();

}

/**
* Test the badge criteria updated event.
*
* There is no external API for this, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_badge_criteria_updated() {

$criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
$criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
$criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
$params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
$criteriaprofile->save($params);
$badge = new badge($this->badgeid);

// Trigger and capture the event.
$sink = $this->redirectEvents();
$criteria = $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE];
$params2 = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'id' => $criteria->id);
$criteria->save((array)$params2);
$events = $sink->get_events();
$event = reset($events);

// Check that the event data is valid.
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\badge_criteria_updated', $event);
$this->assertEquals($criteria->id, $event->objectid);
$this->assertEquals($this->badgeid, $event->other['badgeid']);
$this->assertDebuggingNotCalled();
$sink->close();

}

/**
* Test the badge criteria deleted event.
*
* There is no external API for this, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_badge_criteria_deleted() {

$criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
$criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
$badge = new badge($this->badgeid);

// Trigger and capture the event.
$sink = $this->redirectEvents();
$badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
$events = $sink->get_events();
$event = reset($events);

// Check that the event data is valid.
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\badge_criteria_deleted', $event);
$this->assertEquals($criteriaoverall->badgeid, $event->other['badgeid']);
$this->assertDebuggingNotCalled();
$sink->close();

}
}
10 changes: 10 additions & 0 deletions lang/en/badges.php
Expand Up @@ -248,7 +248,17 @@
$string['error:requesterror'] = 'The connection request failed (error code {$a}).';
$string['error:save'] = 'Cannot save the badge.';
$string['error:userdeleted'] = '{$a->user} (This user no longer exists in {$a->site})';
$string['eventbadgearchived'] = 'Badge archived';
$string['eventbadgeawarded'] = 'Badge awarded';
$string['eventbadgecreated'] = 'Badge created';
$string['eventbadgecriteriacreated'] = 'Badge criteria created';
$string['eventbadgecriteriadeleted'] = 'Badge criteria deleted';
$string['eventbadgecriteriaupdated'] = 'Badge criteria updated';
$string['eventbadgedeleted'] = 'Badge deleted';
$string['eventbadgedisabled'] = 'Badge disabled';
$string['eventbadgeduplicated'] = 'Badge duplicated';
$string['eventbadgeenabled'] = 'Badge enabled';
$string['eventbadgeupdated'] = 'Badge updated';
$string['evidence'] = 'Evidence';
$string['existingrecipients'] = 'Existing badge recipients';
$string['expired'] = 'Expired';
Expand Down

0 comments on commit a0e1387

Please sign in to comment.