mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 00:12:56 +02:00
MDL-80250 course_format: Create new section_viewed event
This commit is contained in:
parent
f30110b5eb
commit
522a7ec010
@ -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"
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -181,8 +181,8 @@ foreach ($jsfiles as $jsfile) {
|
||||
|
||||
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);
|
||||
|
@ -30,6 +30,7 @@ use context_course;
|
||||
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;
|
||||
@ -7415,4 +7416,35 @@ class courselib_test extends advanced_testcase {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
@ -851,6 +851,7 @@ $string['eventname'] = 'Event name';
|
||||
$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';
|
||||
|
96
lib/classes/event/section_viewed.php
Normal file
96
lib/classes/event/section_viewed.php
Normal file
@ -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 <amaia@moodle.com>
|
||||
* @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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user