From 2ca21a0e60eb7decb88627cdaf69511c7a497bfe Mon Sep 17 00:00:00 2001 From: Andrew Downes Date: Wed, 10 Apr 2019 03:43:22 -0500 Subject: [PATCH] fix: Errors from null values. (#473 - Thanks @garemoko) --- .../events/mod_assign/assignment_graded.php | 21 +- .../mod_quiz/question_answered/essay.php | 6 +- .../data.json | 0 .../event.json | 0 .../statements.json | 0 .../test.php | 2 +- .../data.json | 55 ++++++ .../event.json | 10 + .../statements.json | 89 +++++++++ .../test.php | 24 +++ .../essay_null_response/data.json | 78 ++++++++ .../essay_null_response/event.json | 10 + .../essay_null_response/statements.json | 187 ++++++++++++++++++ .../essay_null_response/test.php | 24 +++ 14 files changed, 497 insertions(+), 9 deletions(-) rename tests/mod_assign/assignment_graded/{existing_assignment_graded => existing_assignment_graded_comment}/data.json (100%) rename tests/mod_assign/assignment_graded/{existing_assignment_graded => existing_assignment_graded_comment}/event.json (100%) rename tests/mod_assign/assignment_graded/{existing_assignment_graded => existing_assignment_graded_comment}/statements.json (100%) rename tests/mod_assign/assignment_graded/{existing_assignment_graded => existing_assignment_graded_comment}/test.php (98%) create mode 100644 tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/data.json create mode 100644 tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/event.json create mode 100644 tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/statements.json create mode 100644 tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/test.php create mode 100644 tests/mod_quiz/attempt_submitted/essay_null_response/data.json create mode 100644 tests/mod_quiz/attempt_submitted/essay_null_response/event.json create mode 100644 tests/mod_quiz/attempt_submitted/essay_null_response/statements.json create mode 100644 tests/mod_quiz/attempt_submitted/essay_null_response/test.php diff --git a/src/transformer/events/mod_assign/assignment_graded.php b/src/transformer/events/mod_assign/assignment_graded.php index c8e147dfd..ec5d17151 100644 --- a/src/transformer/events/mod_assign/assignment_graded.php +++ b/src/transformer/events/mod_assign/assignment_graded.php @@ -29,10 +29,16 @@ function assignment_graded(array $config, \stdClass $event) { $assignment = $repo->read_record_by_id('assign', $grade->assignment); $lang = utils\get_course_lang($course); - $gradecomment = $repo->read_record('assignfeedback_comments', [ - 'assignment' => $grade->assignment, - 'grade' => $grade->id - ])->commenttext; + $gradecomment = null; + try { + $gradecomment = $repo->read_record('assignfeedback_comments', [ + 'assignment' => $grade->assignment, + 'grade' => $grade->id + ])->commenttext; + } catch (\Exception $e) { + $gradecomment = null; + } + $gradeitems = $repo->read_record('grade_items', [ 'itemmodule' => 'assign', 'iteminstance' => $grade->assignment @@ -63,8 +69,7 @@ function assignment_graded(array $config, \stdClass $event) { 'raw' => $scoreraw ], 'completion' => true, - 'success' => $success, - 'response' => $gradecomment + 'success' => $success ], 'timestamp' => utils\get_event_timestamp($event), 'context' => [ @@ -86,6 +91,10 @@ function assignment_graded(array $config, \stdClass $event) { ] ]; + if (!is_null($gradecomment)) { + $statement['result']['response'] = $gradecomment; + } + // Only include min score if raw score is valid for that min. if ($scoreraw >= $scoremin) { $statement['result']['score']['min'] = $scoremin; diff --git a/src/transformer/events/mod_quiz/question_answered/essay.php b/src/transformer/events/mod_quiz/question_answered/essay.php index 42f581d11..707646751 100644 --- a/src/transformer/events/mod_quiz/question_answered/essay.php +++ b/src/transformer/events/mod_quiz/question_answered/essay.php @@ -29,6 +29,8 @@ function essay(array $config, \stdClass $event, \stdClass $questionattempt, \std $coursemodule = $repo->read_record_by_id('course_modules', $event->contextinstanceid); $lang = utils\get_course_lang($course); + $responsesummary = is_null($questionattempt->responsesummary) ? '' : $questionattempt->responsesummary; + return [[ 'actor' => utils\get_user($config, $user), 'verb' => [ @@ -49,8 +51,8 @@ function essay(array $config, \stdClass $event, \stdClass $questionattempt, \std ], 'timestamp' => utils\get_event_timestamp($event), 'result' => [ - 'response' => $questionattempt->responsesummary, - 'completion' => $questionattempt->responsesummary !== '', + 'response' => $responsesummary, + 'completion' => $responsesummary !== '', ], 'context' => [ 'platform' => $config['source_name'], diff --git a/tests/mod_assign/assignment_graded/existing_assignment_graded/data.json b/tests/mod_assign/assignment_graded/existing_assignment_graded_comment/data.json similarity index 100% rename from tests/mod_assign/assignment_graded/existing_assignment_graded/data.json rename to tests/mod_assign/assignment_graded/existing_assignment_graded_comment/data.json diff --git a/tests/mod_assign/assignment_graded/existing_assignment_graded/event.json b/tests/mod_assign/assignment_graded/existing_assignment_graded_comment/event.json similarity index 100% rename from tests/mod_assign/assignment_graded/existing_assignment_graded/event.json rename to tests/mod_assign/assignment_graded/existing_assignment_graded_comment/event.json diff --git a/tests/mod_assign/assignment_graded/existing_assignment_graded/statements.json b/tests/mod_assign/assignment_graded/existing_assignment_graded_comment/statements.json similarity index 100% rename from tests/mod_assign/assignment_graded/existing_assignment_graded/statements.json rename to tests/mod_assign/assignment_graded/existing_assignment_graded_comment/statements.json diff --git a/tests/mod_assign/assignment_graded/existing_assignment_graded/test.php b/tests/mod_assign/assignment_graded/existing_assignment_graded_comment/test.php similarity index 98% rename from tests/mod_assign/assignment_graded/existing_assignment_graded/test.php rename to tests/mod_assign/assignment_graded/existing_assignment_graded_comment/test.php index 734b0f925..9fee02ac9 100644 --- a/tests/mod_assign/assignment_graded/existing_assignment_graded/test.php +++ b/tests/mod_assign/assignment_graded/existing_assignment_graded_comment/test.php @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -namespace tests\mod_assign\assignment_graded\existing_assignment_graded; +namespace tests\mod_assign\assignment_graded\existing_assignment_graded_comment; defined('MOODLE_INTERNAL') || die(); class test extends \tests\xapi_test_case { diff --git a/tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/data.json b/tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/data.json new file mode 100644 index 000000000..d49ef92cd --- /dev/null +++ b/tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/data.json @@ -0,0 +1,55 @@ +{ + "user": [ + { + "id": 1, + "firstname": "test_fullname", + "email": "test@test.com" + }, + { + "id": 2, + "firstname": "test2_fullname", + "email": "test2@test.com" + } + ], + "course": [ + { + "id": 1, + "fullname": "test_name", + "lang": "en" + } + ], + "course_modules": [ + { + "id": 1, + "course": 1, + "module": 1, + "instance": 1 + } + ], + "assign": [ + { + "id": 1, + "name": "test_name" + } + ], + "grade": [ + { + "id": 1, + "userid": 2, + "assignment": 1, + "grade": 1 + } + ], + "assignfeedback_comments": [ + ], + "grade_items": [ + { + "id": 1, + "itemmodule": "assign", + "iteminstance": 1, + "grademin": 0, + "grademax": 2, + "gradepass": 1 + } + ] +} \ No newline at end of file diff --git a/tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/event.json b/tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/event.json new file mode 100644 index 000000000..bce74db3d --- /dev/null +++ b/tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/event.json @@ -0,0 +1,10 @@ +{ + "id": 1, + "userid": 1, + "courseid": 1, + "timecreated": 1433946701, + "objecttable": "grade", + "objectid": 1, + "contextinstanceid": 1, + "eventname": "\\mod_assign\\event\\submission_graded" +} \ No newline at end of file diff --git a/tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/statements.json b/tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/statements.json new file mode 100644 index 000000000..9fe4a73e8 --- /dev/null +++ b/tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/statements.json @@ -0,0 +1,89 @@ +[ + { + "actor": { + "name": "test2_fullname", + "account": { + "homePage": "http:\/\/www.example.org", + "name": "2" + } + }, + "verb": { + "id": "http:\/\/adlnet.gov\/expapi\/verbs\/scored", + "display": { + "en": "attained grade for" + } + }, + "object": { + "id": "http:\/\/www.example.org\/mod\/assign\/view.php?id=1", + "definition": { + "type": "http:\/\/adlnet.gov\/expapi\/activities\/assessment", + "name": { + "en": "test_name" + } + } + }, + "result": { + "score": { + "raw": 1, + "min": 0, + "max": 2, + "scaled": 0.5 + }, + "completion": true, + "success": true + }, + "timestamp": "2015-06-10T15:31:41+01:00", + "context": { + "instructor": { + "name": "test_fullname", + "account": { + "homePage": "http:\/\/www.example.org", + "name": "1" + } + }, + "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_assign\\event\\submission_graded", + "event_function": "\\src\\transformer\\events\\mod_assign\\assignment_graded" + } + }, + "contextActivities": { + "grouping": [ + { + "id": "http:\/\/www.example.org", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/lms", + "name": { + "en": "test_name" + } + } + }, + { + "id": "http:\/\/www.example.org\/course\/view.php?id=1", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/lms\/course", + "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_assign/assignment_graded/existing_assignment_graded_nocomment/test.php b/tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/test.php new file mode 100644 index 000000000..ef9c93de1 --- /dev/null +++ b/tests/mod_assign/assignment_graded/existing_assignment_graded_nocomment/test.php @@ -0,0 +1,24 @@ +. + +namespace tests\mod_assign\assignment_graded\existing_assignment_graded_nocomment; +defined('MOODLE_INTERNAL') || die(); + +class test extends \tests\xapi_test_case { + protected function get_test_dir() { + return __DIR__; + } +} \ No newline at end of file diff --git a/tests/mod_quiz/attempt_submitted/essay_null_response/data.json b/tests/mod_quiz/attempt_submitted/essay_null_response/data.json new file mode 100644 index 000000000..33929a378 --- /dev/null +++ b/tests/mod_quiz/attempt_submitted/essay_null_response/data.json @@ -0,0 +1,78 @@ +{ + "user": [ + { + "id": 1, + "firstname": "test_fullname", + "email": "test@test.com" + } + ], + "course": [ + { + "id": 1, + "fullname": "test_name", + "lang": "en" + } + ], + "course_modules": [ + { + "id": 1, + "course": 1, + "module": 1, + "instance": 1 + } + ], + "modules": [ + { + "id": 1, + "name": "quiz" + } + ], + "quiz_attempts": [ + { + "id": 1, + "quiz": 1, + "uniqueid": 1, + "sumgrades": 50, + "state": "finished", + "timefinish": 1, + "timestart": 0 + } + ], + "question_usages": [ + { + "id": 1 + + } + ], + "quiz": [ + { + "id": 1, + "name": "test_quiz_name" + } + ], + "grade_items": [ + { + "id": 1, + "iteminstance": 1, + "itemmodule": "quiz", + "grademin": 0, + "grademax": 100, + "gradepass": 50 + } + ], + "question_attempts": [ + { + "id": 1, + "questionusageid": 1, + "questionid": 1, + "responsesummary": null + } + ], + "question": [ + { + "id": 1, + "qtype": "essay", + "questiontext": "test_question" + } + ] +} \ No newline at end of file diff --git a/tests/mod_quiz/attempt_submitted/essay_null_response/event.json b/tests/mod_quiz/attempt_submitted/essay_null_response/event.json new file mode 100644 index 000000000..0f8370b23 --- /dev/null +++ b/tests/mod_quiz/attempt_submitted/essay_null_response/event.json @@ -0,0 +1,10 @@ +{ + "id": 1, + "relateduserid": 1, + "courseid": 1, + "timecreated": 1433946701, + "objecttable": "attempt", + "objectid": 1, + "contextinstanceid": 1, + "eventname": "\\mod_quiz\\event\\attempt_submitted" +} \ No newline at end of file diff --git a/tests/mod_quiz/attempt_submitted/essay_null_response/statements.json b/tests/mod_quiz/attempt_submitted/essay_null_response/statements.json new file mode 100644 index 000000000..0ca9eff3f --- /dev/null +++ b/tests/mod_quiz/attempt_submitted/essay_null_response/statements.json @@ -0,0 +1,187 @@ +[ + { + "actor": { + "name": "test_fullname", + "account": { + "homePage": "http:\/\/www.example.org", + "name": "1" + } + }, + "verb": { + "id": "http:\/\/adlnet.gov\/expapi\/verbs\/completed", + "display": { + "en": "completed" + } + }, + "object": { + "id": "http:\/\/www.example.org\/mod\/quiz\/view.php?id=1", + "definition": { + "type": "http:\/\/adlnet.gov\/expapi\/activities\/assessment", + "name": { + "en": "test_quiz_name" + } + } + }, + "timestamp": "2015-06-10T15:31:41+01:00", + "result": { + "score": { + "raw": 50, + "min": 0, + "max": 100, + "scaled": 0.5 + }, + "completion": true, + "success": true, + "duration": "PT1S" + }, + "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_quiz\\event\\attempt_submitted", + "event_function": "\\src\\transformer\\events\\mod_quiz\\attempt_submitted\\handler" + } + }, + "contextActivities": { + "other": [ + { + "id": "http:\/\/www.example.org\/mod\/quiz\/attempt.php?attempt=1&cmid=1", + "definition": { + "type": "http:\/\/adlnet.gov\/expapi\/activities\/attempt", + "name": { + "en": "Attempt" + } + } + } + ], + "grouping": [ + { + "id": "http:\/\/www.example.org", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/lms", + "name": { + "en": "test_name" + } + } + }, + { + "id": "http:\/\/www.example.org\/course\/view.php?id=1", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/lms\/course", + "name": { + "en": "test_name" + } + } + } + ], + "category": [ + { + "id": "http:\/\/moodle.org", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/source", + "name": { + "en": "Moodle" + } + } + } + ] + } + } + }, + { + "actor": { + "name": "test_fullname", + "account": { + "homePage": "http:\/\/www.example.org", + "name": "1" + } + }, + "verb": { + "id": "http:\/\/adlnet.gov\/expapi\/verbs\/answered", + "display": { + "en": "answered" + } + }, + "object": { + "id": "http:\/\/www.example.org\/question\/question.php?cmid=1&id=1", + "definition": { + "type": "http:\/\/adlnet.gov\/expapi\/activities\/cmi.interaction", + "name": { + "en": "test_question" + }, + "interactionType": "long-fill-in" + } + }, + "timestamp": "2015-06-10T15:31:41+01:00", + "result": { + "response": "", + "completion": false + }, + "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_quiz\\event\\attempt_submitted", + "event_function": "\\src\\transformer\\events\\mod_quiz\\attempt_submitted\\handler" + } + }, + "contextActivities": { + "grouping": [ + { + "id": "http:\/\/www.example.org", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/lms", + "name": { + "en": "test_name" + } + } + }, + { + "id": "http:\/\/www.example.org\/course\/view.php?id=1", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/lms\/course", + "name": { + "en": "test_name" + } + } + }, + { + "id": "http:\/\/www.example.org\/mod\/quiz\/view.php?id=1", + "definition": { + "type": "http:\/\/adlnet.gov\/expapi\/activities\/assessment", + "name": { + "en": "test_quiz_name" + } + } + }, + { + "id": "http:\/\/www.example.org\/mod\/quiz\/attempt.php?attempt=1&cmid=1", + "definition": { + "type": "http:\/\/adlnet.gov\/expapi\/activities\/attempt", + "name": { + "en": "Attempt" + } + } + } + ], + "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_quiz/attempt_submitted/essay_null_response/test.php b/tests/mod_quiz/attempt_submitted/essay_null_response/test.php new file mode 100644 index 000000000..fb57e2904 --- /dev/null +++ b/tests/mod_quiz/attempt_submitted/essay_null_response/test.php @@ -0,0 +1,24 @@ +. + +namespace tests\mod_quiz\attempt_submitted\essay_null_response; +defined('MOODLE_INTERNAL') || die(); + +class test extends \tests\xapi_test_case { + protected function get_test_dir() { + return __DIR__; + } +} \ No newline at end of file