From bcf17dbde8dcdcb538279e163b17a2578e6cc007 Mon Sep 17 00:00:00 2001 From: Ryan Smith <3045513+ryansmith94@users.noreply.github.com> Date: Fri, 3 Aug 2018 09:54:19 +0100 Subject: [PATCH] feat: Adds statement for numeric feedback questions. (#215) --- .../mod_feedback/item_answered/handler.php | 2 + .../mod_feedback/item_answered/numeric.php | 74 ++++++++ src/transformer/handler.php | 3 +- .../response_submitted/numeric/data.json | 44 +++++ .../response_submitted/numeric/event.json | 9 + .../numeric/statements.json | 159 ++++++++++++++++++ .../response_submitted/numeric/test.php | 24 +++ 7 files changed, 314 insertions(+), 1 deletion(-) create mode 100644 src/transformer/events/mod_feedback/item_answered/numeric.php create mode 100644 tests/mod_feedback/response_submitted/numeric/data.json create mode 100644 tests/mod_feedback/response_submitted/numeric/event.json create mode 100644 tests/mod_feedback/response_submitted/numeric/statements.json create mode 100644 tests/mod_feedback/response_submitted/numeric/test.php diff --git a/src/transformer/events/mod_feedback/item_answered/handler.php b/src/transformer/events/mod_feedback/item_answered/handler.php index a1f099122..02c650051 100644 --- a/src/transformer/events/mod_feedback/item_answered/handler.php +++ b/src/transformer/events/mod_feedback/item_answered/handler.php @@ -25,6 +25,8 @@ function handler(array $config, \stdClass $event, \stdClass $feedbackvalue) { $feedbackitem = $repo->read_record_by_id('feedback_item', $feedbackvalue->item); switch ($feedbackitem->typ) { + case 'numeric': + return numeric($config, $event, $feedbackvalue, $feedbackitem); case 'textarea': return textarea($config, $event, $feedbackvalue, $feedbackitem); case 'textfield': diff --git a/src/transformer/events/mod_feedback/item_answered/numeric.php b/src/transformer/events/mod_feedback/item_answered/numeric.php new file mode 100644 index 000000000..7534b7951 --- /dev/null +++ b/src/transformer/events/mod_feedback/item_answered/numeric.php @@ -0,0 +1,74 @@ +. + +namespace src\transformer\events\mod_feedback\item_answered; + +defined('MOODLE_INTERNAL') || die(); + +use src\transformer\utils as utils; + +function numeric(array $config, \stdClass $event, \stdClass $feedbackvalue, \stdClass $feedbackitem) { + $repo = $config['repo']; + $user = $repo->read_record_by_id('user', $event->userid); + $course = $repo->read_record_by_id('course', $event->courseid); + $feedback = $repo->read_record_by_id('feedback', $feedbackitem->feedback); + $lang = utils\get_course_lang($course); + + return [[ + 'actor' => utils\get_user($config, $user), + 'verb' => [ + 'id' => 'http://adlnet.gov/expapi/verbs/answered', + 'display' => [ + $lang => 'answered' + ], + ], + 'object' => [ + 'id' => $config['app_url'].'/mod/feedback/edit_item.php?id='.$feedbackitem->id, + 'definition' => [ + 'type' => 'http://adlnet.gov/expapi/activities/cmi.interaction', + 'name' => [ + $lang => $feedbackitem->name, + ], + 'interactionType' => 'numeric', + ], + ], + 'timestamp' => utils\get_event_timestamp($event), + 'result' => [ + 'response' => $feedbackvalue->value, + 'completion' => $feedbackvalue->value !== '', + 'extensions' => [ + 'http://learninglocker.net/xapi/cmi/numeric/response' => floatval($feedbackvalue->value), + ], + ], + 'context' => [ + 'platform' => $config['source_name'], + 'language' => $lang, + 'extensions' => [ + utils\INFO_EXTENSION => utils\get_info($config, $event), + ], + 'contextActivities' => [ + 'grouping' => [ + utils\get_activity\site($config), + utils\get_activity\course($config, $course), + utils\get_activity\course_feedback($config, $event->contextinstanceid, $feedback, $lang), + ], + 'category' => [ + utils\get_activity\source($config), + ] + ], + ] + ]]; +} \ No newline at end of file diff --git a/src/transformer/handler.php b/src/transformer/handler.php index c6a6e2167..828a08ba5 100644 --- a/src/transformer/handler.php +++ b/src/transformer/handler.php @@ -36,7 +36,8 @@ function handler(array $config, array $events) { return $transformedevent; } catch (\Exception $e) { $logerror = $config['log_error']; - $logerror("Caught exception for event id #" . $eventobj->id . ": " . $e->getMessage(), "\n"); + $logerror("Caught exception for event id #" . $eventobj->id . ": " . $e->getMessage()); + $logerror($e->getTraceAsString()); return null; } }, $events)); diff --git a/tests/mod_feedback/response_submitted/numeric/data.json b/tests/mod_feedback/response_submitted/numeric/data.json new file mode 100644 index 000000000..6e3fb1299 --- /dev/null +++ b/tests/mod_feedback/response_submitted/numeric/data.json @@ -0,0 +1,44 @@ +{ + "user": [ + { + "id": 1, + "firstname": "test_fullname", + "email": "test@test.com" + } + ], + "course": [ + { + "id": 1, + "fullname": "test_name", + "lang": "en" + } + ], + "feedback_completed": [ + { + "id": 1, + "feedback": 1 + } + ], + "feedback": [ + { + "id": 1, + "name": "test_feedback_name" + } + ], + "feedback_item": [ + { + "id": 1, + "feedback": 1, + "name": "test_feedback_item", + "typ": "numeric" + } + ], + "feedback_value": [ + { + "id": 1, + "item": 1, + "completed": 1, + "value": "3" + } + ] +} \ No newline at end of file diff --git a/tests/mod_feedback/response_submitted/numeric/event.json b/tests/mod_feedback/response_submitted/numeric/event.json new file mode 100644 index 000000000..5cd948b5e --- /dev/null +++ b/tests/mod_feedback/response_submitted/numeric/event.json @@ -0,0 +1,9 @@ +{ + "id": 1, + "userid": 1, + "courseid": 1, + "timecreated": 1433946701, + "objectid": 1, + "contextinstanceid": 1, + "eventname": "\\mod_feedback\\event\\response_submitted" +} \ No newline at end of file diff --git a/tests/mod_feedback/response_submitted/numeric/statements.json b/tests/mod_feedback/response_submitted/numeric/statements.json new file mode 100644 index 000000000..647392788 --- /dev/null +++ b/tests/mod_feedback/response_submitted/numeric/statements.json @@ -0,0 +1,159 @@ +[ + { + "actor": { + "name": "test_fullname", + "account": { + "homePage": "http:\/\/www.example.org", + "name": "1" + } + }, + "verb": { + "id": "http:\/\/id.tincanapi.com\/verb\/submitted", + "display": { + "en": "submitted" + } + }, + "object": { + "id": "http:\/\/www.example.org\/mod\/feedback\/view.php?id=1", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/survey", + "name": { + "en": "test_feedback_name" + } + } + }, + "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_feedback\\event\\response_submitted", + "event_function": "\\src\\transformer\\events\\mod_feedback\\response_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" + } + } + } + ], + "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\/mod\/feedback\/edit_item.php?id=1", + "definition": { + "type": "http:\/\/adlnet.gov\/expapi\/activities\/cmi.interaction", + "name": { + "en": "test_feedback_item" + }, + "interactionType": "numeric" + } + }, + "timestamp": "2015-06-10T15:31:41+01:00", + "result": { + "response": "3", + "completion": true, + "extensions": { + "http:\/\/learninglocker.net\/xapi\/cmi\/numeric\/response": 3 + } + }, + "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_feedback\\event\\response_submitted", + "event_function": "\\src\\transformer\\events\\mod_feedback\\response_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\/feedback\/view.php?id=1", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/survey", + "name": { + "en": "test_feedback_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_feedback/response_submitted/numeric/test.php b/tests/mod_feedback/response_submitted/numeric/test.php new file mode 100644 index 000000000..60aae33e1 --- /dev/null +++ b/tests/mod_feedback/response_submitted/numeric/test.php @@ -0,0 +1,24 @@ +. + +namespace tests\mod_feedback\response_submitted\numeric; +defined('MOODLE_INTERNAL') || die(); + +class test extends \tests\xapi_test_case { + protected function get_test_dir() { + return __DIR__; + } +} \ No newline at end of file