Skip to content

Commit

Permalink
badge updated (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
milt authored Nov 25, 2024
1 parent e58ba23 commit 29b32a3
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/transformer/events/core/badge_updated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Transformer fn for badge updated event.
*
* @package logstore_xapi
* @copyright Milt Reder <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace src\transformer\events\core;

use src\transformer\utils as utils;
use src\transformer\utils\get_activity as activity;

/**
* Transformer fn for badge updated event.
*
* @param array $config The transformer config settings.
* @param \stdClass $event The event to be transformed.
* @return array
*/

function badge_updated(array $config, \stdClass $event) {
$repo = $config['repo'];
$user = $repo->read_record_by_id('user', $event->userid);
$badge = $repo->read_record_by_id('badge', $event->objectid);
$course = (isset($event->courseid) && $event->courseid != 0)
? $repo->read_record_by_id('course', $event->courseid)
: null;
$lang = $badge->language ??
(
!is_null($course)
? utils\get_course_lang($course)
: $config['source_lang']
);

$statement = [
'actor' => utils\get_user($config,$user),
'verb' => [
'id' => 'https://w3id.org/xapi/acrossx/verbs/edited',
'display' => [
'en' => 'Edited'
]
],
'object' => utils\badge_object($config, $lang, $badge),
'context' => [
'language' => $lang,
'contextActivities' => [
'category' => [
activity\site($config)
],
],
'extensions' => utils\extensions\base($config, $event, $course)
]];

if ($course){
$statement = utils\add_parent($config,$statement,$course);
}

return [$statement];
}
1 change: 1 addition & 0 deletions src/transformer/get_event_function_map.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function get_event_function_map() {
$availableevents = [
'\core\event\course_category_created' => 'core\course_category_created',
'\core\event\badge_awarded' => 'core\badge_awarded',
'\core\event\badge_updated' => 'core\badge_updated',
'\core\event\badge_revoked' => 'core\badge_revoked',
'\core\event\badge_viewed' => 'core\badge_viewed',
'\core\event\message_sent' => 'core\message_sent',
Expand Down
70 changes: 70 additions & 0 deletions tests/core/badge_updated/badge_updated_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace logstore_xapi\core\badge_updated;

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

global $CFG;

require_once($CFG->dirroot . '/admin/tool/log/store/xapi/tests/xapi_test_case.php');

/**
* Unit test for badge updated event.
*
* @package logstore_xapi
* @copyright Milt Reder <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class badge_updated_test extends \logstore_xapi\xapi_test_case {

/**
* Retrieve the directory of the unit test.
*
* @return string
*/
protected function get_test_dir() {
return __DIR__;
}

/**
* Retrieve the plugin type being tested.
*
* @return string
*/
protected function get_plugin_type() {
return "core";
}

/**
* Retrieve the plugin name being tested.
*
* @return string
*/
protected function get_plugin_name() {
return "badge";
}

/**
* Appease auto-detecting of test cases. xapi_test_case has default test cases.
*
* @covers ::badge_updated
* @return void
*/
public function test_init() {

}
}
19 changes: 19 additions & 0 deletions tests/core/badge_updated/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"badge": [
{
"id": 1,
"name": "test_badgename",
"description": "test badge description",
"type": 2,
"courseid": 1,
"version": "1.0",
"message": "you got the test badge!"
}
],
"badge_manual_award": [
{
"id": 2,
"issuerid": 2
}
]
}
10 changes: 10 additions & 0 deletions tests/core/badge_updated/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": 1,
"eventname": "\\core\\event\\badge_updated",
"timecreated": 1433946701,
"relateduserid": 1,
"courseid": 1,
"userid": 1,
"objectid": 1,
"objecttable": "badge"
}
68 changes: 68 additions & 0 deletions tests/core/badge_updated/statements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
[
{
"actor": {
"name": "test_fullname",
"account": {
"homePage": "http://www.example.org",
"name": "1"
}
},
"verb": {
"id": "https://w3id.org/xapi/acrossx/verbs/edited",
"display": {
"en": "Edited"
}
},
"object": {
"id": "http://www.example.org/badges/overview.php?id=1",
"definition": {
"name": {
"en": "test_badgename"
},
"description": {
"en": "test badge description"
},
"type": "https://xapi.edlm/profiles/edlm-lms/concepts/activity-types/badge",
"extensions": {
"https://xapi.edlm/profiles/edlm-lms/v1/concepts/activity-extensions/badge-type": "Course",
"https://xapi.edlm/profiles/edlm-lms/v1/concepts/activity-extensions/badge-version": "1.0"
}
}
},
"context": {
"language": "en",
"contextActivities": {
"category": [
{
"id": "http://www.example.org",
"definition": {
"type": "http://id.tincanapi.com/activitytype/lms",
"name": {
"en": "test_name"
}
}
}
],
"parent": [
{
"id": "http://www.example.org/course/view.php?id=1",
"definition": {
"type": "https://w3id.org/xapi/cmi5/activitytype/course",
"name": {
"en": "test_name"
}
}
}
]
},
"extensions": {
"http://lrs.learninglocker.net/define/extensions/info": {
"http://moodle.org": "1.0.0",
"https://github.com/xAPI-vle/moodle-logstore_xapi": "0.0.0-development",
"event_name": "\\core\\event\\badge_updated",
"event_function": "\\src\\transformer\\events\\core\\badge_updated"
}
}
}
}
]

0 comments on commit 29b32a3

Please sign in to comment.