Skip to content

Commit

Permalink
MDL-80250 course_format: Create new section_viewed event
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaia Anabitarte committed Jan 31, 2024
1 parent f30110b commit 522a7ec
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 2 deletions.
3 changes: 3 additions & 0 deletions course/format/tests/behat/section_page.feature
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ Feature: Single section course page
And I should not see "Activity sample 1.3" in the "region-main" "region"
And I should not see "Activity sample 2.1" in the "region-main" "region"
And I should not see "Activity sample 2.1" in the "region-main" "region"
# The section viewed has been trigered.
And I navigate to "Reports > Live logs" in current page administration
And I should see "Section viewed"
19 changes: 19 additions & 0 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5079,3 +5079,22 @@ function course_update_communication_instance_data(stdClass $data): void {
$data->id = $data->instanceid; // For correct use in update_course.
update_course($data);
}

/**
* Trigger course section viewed event.
*
* @param context_course $context course context object
* @param int $sectionid section number
* @since Moodle 4.4.
*/
function course_section_view(context_course $context, int $sectionid) {

$eventdata = [
'objectid' => $sectionid,
'context' => $context,
];
$event = \core\event\section_viewed::create($eventdata);
$event->trigger();

user_accesstime_log($context->instanceid);
}
4 changes: 2 additions & 2 deletions course/section.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@

echo $renderer->container_end();

// Trigger course viewed event.
course_view($context, $section->section);
// Trigger section viewed event.
course_section_view($context, $sectionid);

// Load the view JS module if completion tracking is enabled for this course.
$completion = new completion_info($course);
Expand Down
32 changes: 32 additions & 0 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use context_module;
use context_system;
use context_coursecat;
use core\event\section_viewed;
use core_completion_external;
use core_external;
use core_tag_index_builder;
Expand Down Expand Up @@ -7415,4 +7416,35 @@ public function test_course_update_communication_instance_data(): void {
$course = get_course($course->id);
$this->assertEquals($course->fullname, $data->fullname);
}

/**
* Test course_section_view() function
*
* @covers ::course_section_view
*/
public function test_course_section_view(): void {

$this->resetAfterTest();

// Course without sections.
$course = $this->getDataGenerator()->create_course(['numsections' => 5], ['createsections' => true]);
$coursecontext = context_course::instance($course->id);
$format = course_get_format($course->id);
$sections = $format->get_sections();
$section = reset($sections);

// Redirect events to the sink, so we can recover them later.
$sink = $this->redirectEvents();

course_section_view($coursecontext, $section->id);

$events = $sink->get_events();
$event = reset($events);

// Check the event details are correct.
$this->assertInstanceOf('\core\event\section_viewed', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals('course_sections', $event->objecttable);
$this->assertEquals($section->id, $event->objectid);
}
}
1 change: 1 addition & 0 deletions lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@
$string['eventrecentactivityviewed'] = 'Recent activity viewed';
$string['eventsearchindexed'] = 'Search data indexed';
$string['eventsearchresultsviewed'] = 'Search results viewed';
$string['eventsectionviewed'] = 'Section viewed';
$string['eventunknownlogged'] = 'Unknown event';
$string['eventurlblocked'] = 'The URL was blocked';
$string['eventusercreated'] = 'User created';
Expand Down
96 changes: 96 additions & 0 deletions lib/classes/event/section_viewed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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 core\event;

/**
* Section viewed event class.
*
* @package core
* @copyright 2023 Amaia Anabitarte <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class section_viewed extends base {

/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['objecttable'] = 'course_sections';
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
}

/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' viewed the section with id '$this->objectid'.";
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventsectionviewed', 'core');
}

/**
* Get URL related to the action.
*
* @return \moodle_url|null
*/
public function get_url() {
global $CFG, $DB;
require_once($CFG->dirroot . '/course/lib.php');
try {
$section = $DB->get_record($this->objecttable, ['id' => $this->objectid], '*', MUST_EXIST);
return course_get_url($this->courseid, $section, ['navigation' => true]);
} catch (\Exception $e) {
return null;
}
}

/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();

if ($this->contextlevel != CONTEXT_COURSE) {
throw new \coding_exception('Context level must be CONTEXT_COURSE.');
}
}

/**
* Used for mapping events on restore
*
* @return bool
*/
public static function get_other_mapping() {
// No mapping required.
return false;
}
}

0 comments on commit 522a7ec

Please sign in to comment.