Skip to content

Commit

Permalink
refactor parent
Browse files Browse the repository at this point in the history
  • Loading branch information
milt committed Oct 11, 2024
1 parent eca1513 commit 1d6b4f5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/transformer/events/core/course_module_created.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ function course_module_created(array $config, \stdClass $event) {
$user = $repo->read_record_by_id('user', $event->userid);
$course = $repo->read_record_by_id('course', $event->courseid);
$lang = utils\get_course_lang($course);
$coursemodule = $repo->read_record_by_id('course_modules', $event->contextinstanceid);

return [[
'actor' => utils\get_user($config, $user),
Expand All @@ -56,10 +55,10 @@ function course_module_created(array $config, \stdClass $event) {
'context' => [
'extensions' => utils\extensions\base($config, $event, null),
'contextActivities' => [
'parent' => [
utils\get_activity\course_section($config, $course, $coursemodule->section),
utils\get_activity\course($config, $course),
],
'parent' => utils\context_activities\get_parent(
$config,
$event->contextinstanceid
),
'category' => [
utils\get_activity\site($config),
],
Expand Down
54 changes: 54 additions & 0 deletions src/transformer/utils/context_activities/get_parent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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 utility for retrieving the contextActivities parent array.
*
* @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\context_activities;

use src\transformer\utils as utils;

/**
* Given a course module id, return the contextActivities parent array.
*
* @param array $config The transformer config.
* @param int $cmid The course module ID.
* @param ?bool $include_module Whether or not to include the course module in the array. Defaults to false.
* @return array
*/
function get_parent(array $config, int $cmid, ?bool $include_module = false) {
$repo = $config['repo'];
$coursemodule = $repo->read_record_by_id('course_modules', $cmid);
$course = $repo->read_record_by_id('course', $coursemodule->course);

$parent = [
utils\get_activity\course_section($config, $course, $coursemodule->section),
utils\get_activity\course($config, $course),
];

if ($include_module) {
$parent = array_merge([
utils\get_activity\course_module($config, $course, $cmid),
], $parent);
}

return $parent;
}

0 comments on commit 1d6b4f5

Please sign in to comment.