Skip to content

Commit 2ca21a0

Browse files
garemokoryasmi
authored andcommitted
fix: Errors from null values. (#473 - Thanks @garemoko)
1 parent fab6b07 commit 2ca21a0

File tree

14 files changed

+497
-9
lines changed

14 files changed

+497
-9
lines changed

src/transformer/events/mod_assign/assignment_graded.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ function assignment_graded(array $config, \stdClass $event) {
2929
$assignment = $repo->read_record_by_id('assign', $grade->assignment);
3030
$lang = utils\get_course_lang($course);
3131

32-
$gradecomment = $repo->read_record('assignfeedback_comments', [
33-
'assignment' => $grade->assignment,
34-
'grade' => $grade->id
35-
])->commenttext;
32+
$gradecomment = null;
33+
try {
34+
$gradecomment = $repo->read_record('assignfeedback_comments', [
35+
'assignment' => $grade->assignment,
36+
'grade' => $grade->id
37+
])->commenttext;
38+
} catch (\Exception $e) {
39+
$gradecomment = null;
40+
}
41+
3642
$gradeitems = $repo->read_record('grade_items', [
3743
'itemmodule' => 'assign',
3844
'iteminstance' => $grade->assignment
@@ -63,8 +69,7 @@ function assignment_graded(array $config, \stdClass $event) {
6369
'raw' => $scoreraw
6470
],
6571
'completion' => true,
66-
'success' => $success,
67-
'response' => $gradecomment
72+
'success' => $success
6873
],
6974
'timestamp' => utils\get_event_timestamp($event),
7075
'context' => [
@@ -86,6 +91,10 @@ function assignment_graded(array $config, \stdClass $event) {
8691
]
8792
];
8893

94+
if (!is_null($gradecomment)) {
95+
$statement['result']['response'] = $gradecomment;
96+
}
97+
8998
// Only include min score if raw score is valid for that min.
9099
if ($scoreraw >= $scoremin) {
91100
$statement['result']['score']['min'] = $scoremin;

src/transformer/events/mod_quiz/question_answered/essay.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ function essay(array $config, \stdClass $event, \stdClass $questionattempt, \std
2929
$coursemodule = $repo->read_record_by_id('course_modules', $event->contextinstanceid);
3030
$lang = utils\get_course_lang($course);
3131

32+
$responsesummary = is_null($questionattempt->responsesummary) ? '' : $questionattempt->responsesummary;
33+
3234
return [[
3335
'actor' => utils\get_user($config, $user),
3436
'verb' => [
@@ -49,8 +51,8 @@ function essay(array $config, \stdClass $event, \stdClass $questionattempt, \std
4951
],
5052
'timestamp' => utils\get_event_timestamp($event),
5153
'result' => [
52-
'response' => $questionattempt->responsesummary,
53-
'completion' => $questionattempt->responsesummary !== '',
54+
'response' => $responsesummary,
55+
'completion' => $responsesummary !== '',
5456
],
5557
'context' => [
5658
'platform' => $config['source_name'],

tests/mod_assign/assignment_graded/existing_assignment_graded/test.php renamed to tests/mod_assign/assignment_graded/existing_assignment_graded_comment/test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

17-
namespace tests\mod_assign\assignment_graded\existing_assignment_graded;
17+
namespace tests\mod_assign\assignment_graded\existing_assignment_graded_comment;
1818
defined('MOODLE_INTERNAL') || die();
1919

2020
class test extends \tests\xapi_test_case {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"user": [
3+
{
4+
"id": 1,
5+
"firstname": "test_fullname",
6+
"email": "[email protected]"
7+
},
8+
{
9+
"id": 2,
10+
"firstname": "test2_fullname",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"course": [
15+
{
16+
"id": 1,
17+
"fullname": "test_name",
18+
"lang": "en"
19+
}
20+
],
21+
"course_modules": [
22+
{
23+
"id": 1,
24+
"course": 1,
25+
"module": 1,
26+
"instance": 1
27+
}
28+
],
29+
"assign": [
30+
{
31+
"id": 1,
32+
"name": "test_name"
33+
}
34+
],
35+
"grade": [
36+
{
37+
"id": 1,
38+
"userid": 2,
39+
"assignment": 1,
40+
"grade": 1
41+
}
42+
],
43+
"assignfeedback_comments": [
44+
],
45+
"grade_items": [
46+
{
47+
"id": 1,
48+
"itemmodule": "assign",
49+
"iteminstance": 1,
50+
"grademin": 0,
51+
"grademax": 2,
52+
"gradepass": 1
53+
}
54+
]
55+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"id": 1,
3+
"userid": 1,
4+
"courseid": 1,
5+
"timecreated": 1433946701,
6+
"objecttable": "grade",
7+
"objectid": 1,
8+
"contextinstanceid": 1,
9+
"eventname": "\\mod_assign\\event\\submission_graded"
10+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
[
2+
{
3+
"actor": {
4+
"name": "test2_fullname",
5+
"account": {
6+
"homePage": "http:\/\/www.example.org",
7+
"name": "2"
8+
}
9+
},
10+
"verb": {
11+
"id": "http:\/\/adlnet.gov\/expapi\/verbs\/scored",
12+
"display": {
13+
"en": "attained grade for"
14+
}
15+
},
16+
"object": {
17+
"id": "http:\/\/www.example.org\/mod\/assign\/view.php?id=1",
18+
"definition": {
19+
"type": "http:\/\/adlnet.gov\/expapi\/activities\/assessment",
20+
"name": {
21+
"en": "test_name"
22+
}
23+
}
24+
},
25+
"result": {
26+
"score": {
27+
"raw": 1,
28+
"min": 0,
29+
"max": 2,
30+
"scaled": 0.5
31+
},
32+
"completion": true,
33+
"success": true
34+
},
35+
"timestamp": "2015-06-10T15:31:41+01:00",
36+
"context": {
37+
"instructor": {
38+
"name": "test_fullname",
39+
"account": {
40+
"homePage": "http:\/\/www.example.org",
41+
"name": "1"
42+
}
43+
},
44+
"platform": "Moodle",
45+
"language": "en",
46+
"extensions": {
47+
"http:\/\/lrs.learninglocker.net\/define\/extensions\/info": {
48+
"http:\/\/moodle.org": "1.0.0",
49+
"https:\/\/github.com\/xAPI-vle\/moodle-logstore_xapi": "0.0.0-development",
50+
"event_name": "\\mod_assign\\event\\submission_graded",
51+
"event_function": "\\src\\transformer\\events\\mod_assign\\assignment_graded"
52+
}
53+
},
54+
"contextActivities": {
55+
"grouping": [
56+
{
57+
"id": "http:\/\/www.example.org",
58+
"definition": {
59+
"type": "http:\/\/id.tincanapi.com\/activitytype\/lms",
60+
"name": {
61+
"en": "test_name"
62+
}
63+
}
64+
},
65+
{
66+
"id": "http:\/\/www.example.org\/course\/view.php?id=1",
67+
"definition": {
68+
"type": "http:\/\/id.tincanapi.com\/activitytype\/lms\/course",
69+
"name": {
70+
"en": "test_name"
71+
}
72+
}
73+
}
74+
],
75+
"category": [
76+
{
77+
"id": "http:\/\/moodle.org",
78+
"definition": {
79+
"type": "http:\/\/id.tincanapi.com\/activitytype\/source",
80+
"name": {
81+
"en": "Moodle"
82+
}
83+
}
84+
}
85+
]
86+
}
87+
}
88+
}
89+
]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace tests\mod_assign\assignment_graded\existing_assignment_graded_nocomment;
18+
defined('MOODLE_INTERNAL') || die();
19+
20+
class test extends \tests\xapi_test_case {
21+
protected function get_test_dir() {
22+
return __DIR__;
23+
}
24+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"user": [
3+
{
4+
"id": 1,
5+
"firstname": "test_fullname",
6+
"email": "[email protected]"
7+
}
8+
],
9+
"course": [
10+
{
11+
"id": 1,
12+
"fullname": "test_name",
13+
"lang": "en"
14+
}
15+
],
16+
"course_modules": [
17+
{
18+
"id": 1,
19+
"course": 1,
20+
"module": 1,
21+
"instance": 1
22+
}
23+
],
24+
"modules": [
25+
{
26+
"id": 1,
27+
"name": "quiz"
28+
}
29+
],
30+
"quiz_attempts": [
31+
{
32+
"id": 1,
33+
"quiz": 1,
34+
"uniqueid": 1,
35+
"sumgrades": 50,
36+
"state": "finished",
37+
"timefinish": 1,
38+
"timestart": 0
39+
}
40+
],
41+
"question_usages": [
42+
{
43+
"id": 1
44+
45+
}
46+
],
47+
"quiz": [
48+
{
49+
"id": 1,
50+
"name": "test_quiz_name"
51+
}
52+
],
53+
"grade_items": [
54+
{
55+
"id": 1,
56+
"iteminstance": 1,
57+
"itemmodule": "quiz",
58+
"grademin": 0,
59+
"grademax": 100,
60+
"gradepass": 50
61+
}
62+
],
63+
"question_attempts": [
64+
{
65+
"id": 1,
66+
"questionusageid": 1,
67+
"questionid": 1,
68+
"responsesummary": null
69+
}
70+
],
71+
"question": [
72+
{
73+
"id": 1,
74+
"qtype": "essay",
75+
"questiontext": "test_question"
76+
}
77+
]
78+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"id": 1,
3+
"relateduserid": 1,
4+
"courseid": 1,
5+
"timecreated": 1433946701,
6+
"objecttable": "attempt",
7+
"objectid": 1,
8+
"contextinstanceid": 1,
9+
"eventname": "\\mod_quiz\\event\\attempt_submitted"
10+
}

0 commit comments

Comments
 (0)