Skip to content

Commit

Permalink
Merge branch 'master' into mod_feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
milt committed Nov 4, 2024
2 parents 8826e65 + 70446a5 commit cdb2b25
Show file tree
Hide file tree
Showing 11 changed files with 475 additions and 11 deletions.
74 changes: 74 additions & 0 deletions src/transformer/events/mod_choice/answer_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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 fn for answer created 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_choice;

use src\transformer\utils as utils;

/**
* Transformer fn for answer created event.
*
* @param array $config The transformer config settings.
* @param \stdClass $event The event to be transformed.
* @return array
*/

function answer_created(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);
$answer = $repo->read_record_by_id('choice_answers', $event->objectid);
$option = $repo->read_record_by_id('choice_options', $answer->optionid);
$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' => utils\get_activity\course_module(
$config, $course, $event->contextinstanceid
),
'result' => [
'response' => $option->text,
],
'context' => [
'language' => $lang,
'extensions' => utils\extensions\base($config, $event, null),
'contextActivities' => [
'parent' => utils\context_activities\get_parent(
$config,
$event->contextinstanceid
),
'category' => [
utils\get_activity\site($config),
],
],
]
]];
}
1 change: 1 addition & 0 deletions src/transformer/get_event_function_map.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function get_event_function_map() {
'\mod_book\event\chapter_created' => 'mod_book\chapter_created',
'\mod_chat\event\course_module_viewed' => 'all\course_module_viewed',
'\mod_choice\event\course_module_viewed' => 'all\course_module_viewed',
'\mod_choice\event\answer_created' => 'mod_choice\answer_created',
'\mod_data\event\course_module_viewed' => 'all\course_module_viewed',
'\mod_facetoface\event\cancel_booking' => 'mod_facetoface\cancel_booking',
'\mod_facetoface\event\course_module_viewed' => 'all\course_module_viewed',
Expand Down
26 changes: 18 additions & 8 deletions src/transformer/utils/get_activity/course_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,26 @@ function course_module(array $config, \stdClass $course, int $cmid) {
utils\is_enabled_config($config, 'send_jisc_data')
);

// TODO: Some objects (like mod_choice CMI interactions) will need more
// dispatch and add those here
// default definition
$def = [
'type' => $activitytype,
'name' => [
$courselang => $instancename,
],
];

// process special cases

// Choice
if ($module->name === 'choice') {
$def = utils\get_activity\definition\choice\get_choice_definition(
$config, $instance, $courselang
);
}

$object = [
'id' => $coursemoduleurl,
'definition' => [
'type' => $activitytype,
'name' => [
$courselang => $instancename,
],
],
'definition' => $def,
];

if (utils\is_enabled_config($config, 'send_course_and_module_idnumber')) {
Expand Down
81 changes: 81 additions & 0 deletions src/transformer/utils/get_activity/definition/choice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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 utilities for creating Choice xAPI Activity object definitions.
*
* @package logstore_xapi
* @copyright Milt Reder <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace src\transformer\utils\get_activity\definition\choice;

use src\transformer\utils as utils;

/**
* Transformer util for creating choice definitions
*
* @param array $config The transformer config settings.
* @param \stdClass $choice The choice object.
* @param string $lang The language.
*/
function get_choice_definition(
array $config,
\stdClass $choice,
string $lang
) {
$repo = $config['repo'];
$options = $repo->read_records(
'choice_options', ['choiceid' => $choice->id], 'id ASC'
);

return [
'type' => 'http://adlnet.gov/expapi/activities/cmi.interaction',
'name' => [
$lang => $choice->name,
],
'description' => [
$lang => utils\get_string_html_removed($choice->intro),
],
'interactionType' => 'choice',
'correctResponsesPattern' => [
implode(
'[,]',
array_map(
function($option) {
return $option->text;
},
$options
)
),
],
// use array values because this sometimes comes out associative
'choices' => array_values(
array_map(
function($option) use ($lang) {
return [
'id' => utils\slugify($option->text),
'description' => [
$lang => $option->text,
],
];
},
$options
)
)
];
}
48 changes: 48 additions & 0 deletions src/transformer/utils/slugify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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/>.

/**
* Utility to make human-readable but id-safe strings.
*
* @package logstore_xapi
* @copyright Milt Reder <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace src\transformer\utils;

/**
* Transformer utility that converts a given string into a URL-friendly "slug".
*
* @param string $string The input string to be converted into a slug.
* @return string The URL-friendly slug created from the input string.
*/

function slugify($string) {
// Convert the string to lowercase
$string = strtolower($string);

// Replace spaces and consecutive whitespace with a single dash
$string = preg_replace('/\s+/', '-', $string);

// Remove any non-alphanumeric characters except dashes
$string = preg_replace('/[^a-z0-9-]/', '', $string);

// Trim any trailing or leading dashes
$string = trim($string, '-');

return $string;
}
70 changes: 70 additions & 0 deletions tests/mod_choice/answer_created/answer_created_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_choice\answer_created;

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

global $CFG;

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

/**
* Unit test for answer_created event
*
* @package logstore_xapi
* @copyright Milt Reder <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class answer_created_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_choice";
}

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

}
}
34 changes: 34 additions & 0 deletions tests/mod_choice/answer_created/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"modules": [
{
"id": 1,
"name": "choice"
}
],
"choice": [
{
"id": 1,
"name": "To be or not to be?",
"intro": "<p>That is the question.</p>"
}
],
"choice_options": [
{
"id": 1,
"choiceid": 1,
"text": "To be"
},
{
"id": 2,
"choiceid": 1,
"text": "Not to be"
}
],
"choice_answers": [
{
"id": 1,
"choiceid": 1,
"optionid": 1
}
]
}
10 changes: 10 additions & 0 deletions tests/mod_choice/answer_created/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"courseid": 1,
"contextinstanceid": 1,
"eventname": "\\mod_choice\\event\\answer_created",
"id": 1,
"objectid": 1,
"objecttable": "choice_answers",
"timecreated": 1433946701,
"userid": 1
}
Loading

0 comments on commit cdb2b25

Please sign in to comment.