Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(user_report_viewed): Stops errors where course ID is 0. #527

Merged
merged 9 commits into from
May 3, 2019
Merged
27 changes: 20 additions & 7 deletions src/transformer/events/mod_forum/user_report_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,27 @@
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',
'display' => [
$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'],
Expand All @@ -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];
}
16 changes: 11 additions & 5 deletions src/transformer/utils/get_activity/user_report.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,26 @@

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' => [
$courselang => 'forum posts of '.utils\get_full_name($user),
Copy link
Member

@ryasmi ryasmi May 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're using the related user ID below for the activity ID, does that mean we should use the related user here? Same comment for the extension below too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, definitely. Well spotted!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which case i probably shouldn't have modified the user part of this file, but instead should have just changed the user that is passed in by user_report_viewed.php

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet. Yeah good idea that probably makes the most sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryansmith94 this is resolved.

],
'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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"user": [
{
"id": 1,
"firstname": "loggedin_user_firstname",
"email": "[email protected]"
},
{
"id": 2,
"firstname": "viewed_user_firstname",
"email": "[email protected]"
}
],
"course": [
{
"id": 1,
"fullname": "test_name",
"lang": "en"
}
]
}
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
]
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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 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__;
}
}