diff --git a/src/transformer/events/mod_forum/user_report_viewed.php b/src/transformer/events/mod_forum/user_report_viewed.php index 474b612e4..fbbf45396 100644 --- a/src/transformer/events/mod_forum/user_report_viewed.php +++ b/src/transformer/events/mod_forum/user_report_viewed.php @@ -23,11 +23,19 @@ function user_report_viewed(array $config, \stdClass $event) { $repo = $config['repo']; $user = $repo->read_record_by_id('user', $event->userid); - $course = $repo->read_record_by_id('course', $event->courseid); - $discussion = $repo->read_record_by_id('user', $event->relateduserid); - $lang = utils\get_course_lang($course); + $relateduser = $repo->read_record_by_id('user', $event->relateduserid); - return[[ + if ($event->courseid == "0") { + $course = (object) [ + "id" => 0 + ]; + $lang = "en"; + } else { + $course = $repo->read_record_by_id('course', $event->courseid); + $lang = utils\get_course_lang($course); + } + + $statement = [ 'actor' => utils\get_user($config, $user), 'verb' => [ 'id' => 'http://id.tincanapi.com/verb/viewed', @@ -35,7 +43,7 @@ function user_report_viewed(array $config, \stdClass $event) { $lang => 'viewed' ], ], - 'object' => utils\get_activity\user_report($config, $user, $course), + 'object' => utils\get_activity\user_report($config, $relateduser, $course, $lang), 'timestamp' => utils\get_event_timestamp($event), 'context' => [ 'platform' => $config['source_name'], @@ -46,12 +54,17 @@ function user_report_viewed(array $config, \stdClass $event) { 'contextActivities' => [ 'grouping' => [ utils\get_activity\site($config), - utils\get_activity\course($config, $course), ], 'category' => [ utils\get_activity\source($config), ] ], ] - ]]; + ]; + + if ($event->courseid != "0") { + array_push($statement['context']['contextActivities']['grouping'], utils\get_activity\course($config, $course)); + } + + return[$statement]; } diff --git a/src/transformer/utils/get_activity/user_report.php b/src/transformer/utils/get_activity/user_report.php index d9fc55fe2..0f582f38d 100644 --- a/src/transformer/utils/get_activity/user_report.php +++ b/src/transformer/utils/get_activity/user_report.php @@ -19,11 +19,9 @@ use src\transformer\utils as utils; -function user_report(array $config, \stdClass $user, \stdClass $course) { - $courselang = utils\get_course_lang($course); +function user_report(array $config, \stdClass $user, \stdClass $course, $courselang) { - return [ - 'id' => $config['app_url'].'/mod/forum/user.php?id='.$user->id.'&course='.$course->id, + $activity = [ 'definition' => [ 'type' => 'http://id.tincanapi.com/activitytype/user-profile', 'name' => [ @@ -31,8 +29,16 @@ function user_report(array $config, \stdClass $user, \stdClass $course) { ], 'extensions' => [ 'https://moodle.org/xapi/extensions/user_id' => $user->id, - 'https://moodle.org/xapi/extensions/course_id' => $course->id, ], ], ]; + + if ($course->id == "0") { + $activity['id'] = $config['app_url'].'/mod/forum/user.php?id='.$user->id; + } else { + $activity['id'] = $config['app_url'].'/mod/forum/user.php?id='.$user->id.'&course='.$course->id; + $activity['definition']['extensions']['https://moodle.org/xapi/extensions/course_id'] = $course->id; + } + + return $activity; } diff --git a/tests/mod_forum/user_report_viewed/existing_report_viewed/statements.json b/tests/mod_forum/user_report_viewed/existing_report_viewed/statements.json index 53dd25207..6523c71f1 100644 --- a/tests/mod_forum/user_report_viewed/existing_report_viewed/statements.json +++ b/tests/mod_forum/user_report_viewed/existing_report_viewed/statements.json @@ -14,17 +14,17 @@ } }, "object": { - "id": "http:\/\/www.example.org\/mod\/forum\/user.php?id=1&course=1", "definition": { "type": "http:\/\/id.tincanapi.com\/activitytype\/user-profile", "name": { - "en": "forum posts of loggedin_user_firstname" + "en": "forum posts of viewed_user_firstname" }, "extensions": { - "https:\/\/moodle.org\/xapi\/extensions\/user_id": 1, + "https:\/\/moodle.org\/xapi\/extensions\/user_id": 2, "https:\/\/moodle.org\/xapi\/extensions\/course_id": 1 } - } + }, + "id": "http:\/\/www.example.org\/mod\/forum\/user.php?id=2&course=1" }, "timestamp": "2015-06-10T15:31:41+01:00", "context": { diff --git a/tests/mod_forum/user_report_viewed/existing_report_viewed_all_courses/data.json b/tests/mod_forum/user_report_viewed/existing_report_viewed_all_courses/data.json new file mode 100644 index 000000000..b999fcb0f --- /dev/null +++ b/tests/mod_forum/user_report_viewed/existing_report_viewed_all_courses/data.json @@ -0,0 +1,21 @@ +{ + "user": [ + { + "id": 1, + "firstname": "loggedin_user_firstname", + "email": "test1@test.com" + }, + { + "id": 2, + "firstname": "viewed_user_firstname", + "email": "test2@test.com" + } + ], + "course": [ + { + "id": 1, + "fullname": "test_name", + "lang": "en" + } + ] +} \ No newline at end of file diff --git a/tests/mod_forum/user_report_viewed/existing_report_viewed_all_courses/event.json b/tests/mod_forum/user_report_viewed/existing_report_viewed_all_courses/event.json new file mode 100644 index 000000000..79cbabda6 --- /dev/null +++ b/tests/mod_forum/user_report_viewed/existing_report_viewed_all_courses/event.json @@ -0,0 +1,10 @@ +{ + "id": 1, + "userid": 1, + "relateduserid": 2, + "courseid": 0, + "timecreated": 1433946701, + "objecttable": null, + "objectid": null, + "eventname": "\\mod_forum\\event\\user_report_viewed" +} \ No newline at end of file diff --git a/tests/mod_forum/user_report_viewed/existing_report_viewed_all_courses/statements.json b/tests/mod_forum/user_report_viewed/existing_report_viewed_all_courses/statements.json new file mode 100644 index 000000000..66ec7f9b3 --- /dev/null +++ b/tests/mod_forum/user_report_viewed/existing_report_viewed_all_courses/statements.json @@ -0,0 +1,66 @@ +[ + { + "actor": { + "name": "loggedin_user_firstname", + "account": { + "homePage": "http:\/\/www.example.org", + "name": "1" + } + }, + "verb": { + "id": "http:\/\/id.tincanapi.com\/verb\/viewed", + "display": { + "en": "viewed" + } + }, + "object": { + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/user-profile", + "name": { + "en": "forum posts of viewed_user_firstname" + }, + "extensions": { + "https:\/\/moodle.org\/xapi\/extensions\/user_id": 2 + } + }, + "id": "http:\/\/www.example.org\/mod\/forum\/user.php?id=2" + }, + "timestamp": "2015-06-10T15:31:41+01:00", + "context": { + "platform": "Moodle", + "language": "en", + "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": "\\mod_forum\\event\\user_report_viewed", + "event_function": "\\src\\transformer\\events\\mod_forum\\user_report_viewed" + } + }, + "contextActivities": { + "grouping": [ + { + "id": "http:\/\/www.example.org", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/lms", + "name": { + "en": "test_name" + } + } + } + ], + "category": [ + { + "id": "http:\/\/moodle.org", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/source", + "name": { + "en": "Moodle" + } + } + } + ] + } + } + } +] \ No newline at end of file diff --git a/tests/mod_forum/user_report_viewed/existing_report_viewed_all_courses/test.php b/tests/mod_forum/user_report_viewed/existing_report_viewed_all_courses/test.php new file mode 100644 index 000000000..7f2884303 --- /dev/null +++ b/tests/mod_forum/user_report_viewed/existing_report_viewed_all_courses/test.php @@ -0,0 +1,24 @@ +. + +namespace tests\mod_forum\user_report_viewed\existing_report_viewed_all_courses; +defined('MOODLE_INTERNAL') || die(); + +class test extends \tests\xapi_test_case { + protected function get_test_dir() { + return __DIR__; + } +} \ No newline at end of file