Skip to content

Commit

Permalink
Mod Survey (#58)
Browse files Browse the repository at this point in the history
* make survey intro into description

* fix tests for desc

* report viewed

* response submitted
  • Loading branch information
milt authored Nov 20, 2024
1 parent b713df1 commit 44cc2c1
Show file tree
Hide file tree
Showing 14 changed files with 518 additions and 1 deletion.
79 changes: 79 additions & 0 deletions src/transformer/events/mod_survey/report_viewed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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/>.

/**
* Transformer for survey report viewed event.
*
* @package logstore_xapi
* @copyright Milt Reder <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace src\transformer\events\mod_survey;

use src\transformer\utils as utils;

/**
* Transformer for survey report viewed event.
*
* @param array $config The transformer config settings.
* @param \stdClass $event The event to be transformed.
* @return array
*/
function 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);
$survey = $repo->read_record_by_id('survey', $event->objectid);
$lang = utils\get_course_lang($course);
$action = unserialize($event->other)['action'];

return[[
'actor' => utils\get_user($config, $user),
'verb' => [
'id' => 'http://id.tincanapi.com/verb/viewed',
'display' => [
'en' => 'Viewed'
],
],
'object' => [
'id' => $config['app_url']
. '/mod/survey/report.php?id=' . $event->contextinstanceid
. '&action=' . $action,
'objectType' => 'Activity',
'definition' => [
'type' => 'https://xapi.edlm/profiles/edlm-lms/concepts/activity-types/report',
'name' => [
$lang => $survey->name . ' Report: ' . ucfirst($action),
]
]
],
'context' => [
'language' => $lang,
'extensions' => utils\extensions\base($config, $event, $course),
'contextActivities' => [
'parent' => utils\context_activities\get_parent(
$config,
$event->contextinstanceid,
true
),
'category' => [
utils\get_activity\site($config),
],
],
]
]];
}
67 changes: 67 additions & 0 deletions src/transformer/events/mod_survey/response_submitted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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/>.

/**
* Transformer for survey response submitted event.
*
* @package logstore_xapi
* @copyright Milt Reder <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace src\transformer\events\mod_survey;

use src\transformer\utils as utils;

/**
* Transformer for survey response submitted event.
*
* @param array $config The transformer config settings.
* @param \stdClass $event The event to be transformed.
* @return array
*/
function response_submitted(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);
$lang = utils\get_course_lang($course);

return[[
'actor' => utils\get_user($config, $user),
'verb' => [
'id' => 'http://adlnet.gov/expapi/verbs/completed',
'display' => [
'en' => 'Completed'
],
],
'object' => utils\get_activity\course_module(
$config, $course, $event->contextinstanceid
),
'context' => [
'language' => $lang,
'extensions' => utils\extensions\base($config, $event, $course),
'contextActivities' => [
'parent' => utils\context_activities\get_parent(
$config,
$event->contextinstanceid
),
'category' => [
utils\get_activity\site($config),
],
],
]
]];
}
2 changes: 2 additions & 0 deletions src/transformer/get_event_function_map.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ function get_event_function_map() {
'\mod_scorm\event\scoreraw_submitted' => 'mod_scorm\scoreraw_submitted',
'\mod_scorm\event\status_submitted' => 'mod_scorm\status_submitted',
'\mod_survey\event\course_module_viewed' => 'all\course_module_viewed',
'\mod_survey\event\report_viewed' => 'mod_survey\report_viewed',
'\mod_survey\event\response_submitted' => 'mod_survey\response_submitted',
'\mod_url\event\course_module_viewed' => 'all\course_module_viewed',
'\mod_wiki\event\course_module_viewed' => 'all\course_module_viewed',
'\mod_workshop\event\course_module_viewed' => 'all\course_module_viewed',
Expand Down
13 changes: 13 additions & 0 deletions src/transformer/utils/get_activity/course_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ function course_module(array $config, \stdClass $course, int $cmid) {
);
}

// Survey
if ($module->name === 'survey') {
$def = [
'type' => $activitytype,
'name' => [
$courselang => $instancename,
],
'description' => [
$courselang => utils\get_string_html_removed($instance->intro),
],
];
}

$object = [
'id' => $coursemoduleurl,
'definition' => $def,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"survey": [
{
"id": 1,
"name": "test_name"
"name": "test_name",
"intro": "<p>test_intro</p>"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"type": "http://id.tincanapi.com/activitytype/survey",
"name": {
"en": "test_name"
},
"description": {
"en": "test_intro"
}
}
},
Expand Down
15 changes: 15 additions & 0 deletions tests/mod_survey/report_viewed/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"modules": [
{
"id": 1,
"name": "survey"
}
],
"survey": [
{
"id": 1,
"name": "test_name",
"intro": "<p>test_intro</p>"
}
]
}
12 changes: 12 additions & 0 deletions tests/mod_survey/report_viewed/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": 1,
"userid": 1,
"courseid": 1,
"timecreated": 1433946701,
"contextinstanceid": 1,
"eventname": "\\mod_survey\\event\\report_viewed",
"objectid": 1,
"contextinstanceid": 1,
"objecttable": "survey",
"other": "a:2:{s:6:\"action\";s:7:\"summary\";s:7:\"groupid\";i:0;}"
}
70 changes: 70 additions & 0 deletions tests/mod_survey/report_viewed/report_viewed_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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 logstore_xapi\mod_survey;

defined('MOODLE_INTERNAL') || die();

global $CFG;

require_once($CFG->dirroot . '/admin/tool/log/store/xapi/tests/xapi_test_case.php');

/**
* Unit test for report viewed event
*
* @package logstore_xapi
* @copyright Milt Reder <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_viewed_test extends \logstore_xapi\xapi_test_case {

/**
* Retrieve the directory of the unit test.
*
* @return string
*/
protected function get_test_dir() {
return __DIR__;
}

/**
* Retrieve the plugin type being tested.
*
* @return string
*/
protected function get_plugin_type() {
return "core";
}

/**
* Retrieve the plugin name being tested.
*
* @return string
*/
protected function get_plugin_name() {
return "mod_survey";
}

/**
* Appease auto-detecting of test cases. xapi_test_case has default test cases.
*
* @covers ::report_viewed
* @return void
*/
public function test_init() {

}
}
84 changes: 84 additions & 0 deletions tests/mod_survey/report_viewed/statements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[
{
"actor": {
"name": "test_fullname",
"account": {
"homePage": "http://www.example.org",
"name": "1"
}
},
"verb": {
"id": "http://id.tincanapi.com/verb/viewed",
"display": {
"en": "Viewed"
}
},
"object": {
"id": "http://www.example.org/mod/survey/report.php?id=1&action=summary",
"objectType": "Activity",
"definition": {
"type": "https://xapi.edlm/profiles/edlm-lms/concepts/activity-types/report",
"name": {
"en": "test_name Report: Summary"
}
}
},
"context": {
"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_survey\\event\\report_viewed",
"event_function": "\\src\\transformer\\events\\mod_survey\\report_viewed"
}
},
"contextActivities": {
"parent": [
{
"id": "http://www.example.org/mod/survey/view.php?id=1",
"definition": {
"type": "http://id.tincanapi.com/activitytype/survey",
"name": {
"en": "test_name"
},
"description": {
"en": "test_intro"
}
}
},
{
"id": "http://www.example.org/course/section.php?id=1",
"objectType": "Activity",
"definition": {
"name": {
"en": "test_name Section 0"
},
"type": "http://id.tincanapi.com/activitytype/section"
}
},
{
"id": "http://www.example.org/course/view.php?id=1",
"definition": {
"type": "https://w3id.org/xapi/cmi5/activitytype/course",
"name": {
"en": "test_name"
}
}
}
],
"category": [
{
"id": "http://www.example.org",
"definition": {
"type": "http://id.tincanapi.com/activitytype/lms",
"name": {
"en": "test_name"
}
}
}
]
}
}
}
]
15 changes: 15 additions & 0 deletions tests/mod_survey/response_submitted/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"modules": [
{
"id": 1,
"name": "survey"
}
],
"survey": [
{
"id": 1,
"name": "test_name",
"intro": "<p>test_intro</p>"
}
]
}
Loading

0 comments on commit 44cc2c1

Please sign in to comment.