MDL-86860 courseformat: Add set_marker to sectionactions

Apart from set_marker, the method remove_all_markers has also been
added to sectionactions.
This commit is contained in:
Sara Arjona
2025-11-21 19:08:02 +01:00
parent a280753085
commit 6304e24dee
3 changed files with 124 additions and 1 deletions
@@ -0,0 +1,7 @@
issueNumber: MDL-86860
notes:
core_courseformat:
- message: >-
Added `set_marker` and `remove_all_markers` methods to the
`core_courseformat\sectionactions` class.
type: improved
@@ -467,4 +467,55 @@ class sectionactions extends baseactions {
}
return $fields;
}
/**
* Highlight a course section.
*
* @param section_info $sectioninfo the section info to set marker.
* @param bool $marked whether the section is highlighted.
*/
public function set_marker(section_info $sectioninfo, bool $marked): void {
if (!$marked) {
$this->remove_all_markers();
return;
}
if ($this->course->marker == $sectioninfo->section) {
// Nothing to do because it's already marked.
return;
}
$this->set_marker_internal($sectioninfo->section);
}
/**
* Removes any marker in the course.
*/
public function remove_all_markers(): void {
if ($this->course->marker !== 0) {
$this->set_marker_internal(0);
}
}
/**
* Set marker for the course.
*
* @param int $marker the section number to set as marker or 0 to remove any marker.
*/
private function set_marker_internal(int $marker): void {
global $DB, $COURSE;
$DB->set_field('course', 'marker', $marker, ['id' => $this->course->id]);
if ($COURSE && $COURSE->id == $this->course->id) {
$COURSE->marker = $marker;
}
// Make sure the cache is reset.
\course_modinfo::purge_course_section_cache_by_number($this->course->id, $marker);
rebuild_course_cache(
courseid: $this->course->id,
clearonly: true,
partialrebuild: true,
);
}
}
@@ -24,8 +24,8 @@ use stdClass;
* @package core_courseformat
* @copyright 2023 Ferran Recio <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_courseformat\local\sectionactions
*/
#[\PHPUnit\Framework\Attributes\CoversClass(sectionactions::class)]
final class sectionactions_test extends \advanced_testcase {
/**
* Setup to ensure that fixtures are loaded.
@@ -940,4 +940,69 @@ final class sectionactions_test extends \advanced_testcase {
$this->assertEquals($delegatedsection1->id, $section[4]->id);
$this->assertEquals($delegatedsection2->id, $section[5]->id);
}
/**
* Test set_marker method.
*/
public function test_set_marker(): void {
global $COURSE;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course([
'format' => 'topics',
'numsections' => 2,
]);
$COURSE = get_course($course->id);
$sectionactions = new sectionactions($course);
$this->assertFalse(course_get_format($course)->is_section_current(1));
$this->assertFalse(course_get_format($course)->is_section_current(2));
$this->assertEquals(0, $COURSE->marker);
// Highlight the section.
$sectioninfo1 = get_fast_modinfo($course)->get_section_info(1);
$sectionactions->set_marker($sectioninfo1, true);
$this->assertTrue(course_get_format($course)->is_section_current(1));
$this->assertFalse(course_get_format($course)->is_section_current(2));
$this->assertEquals(1, $COURSE->marker);
// Highlight another section.
$sectioninfo2 = get_fast_modinfo($course)->get_section_info(2);
$sectionactions->set_marker($sectioninfo2, true);
$this->assertFalse(course_get_format($course)->is_section_current(1));
$this->assertTrue(course_get_format($course)->is_section_current(2));
$this->assertEquals(2, $COURSE->marker);
// Unhighlight the section.
$sectionactions->set_marker($sectioninfo2, false);
$this->assertFalse(course_get_format($course)->is_section_current(1));
$this->assertFalse(course_get_format($course)->is_section_current(2));
$this->assertEquals(0, $COURSE->marker);
}
/**
* Test remove_all_markers method.
*/
public function test_remove_all_markers(): void {
global $COURSE;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course([
'format' => 'topics',
'numsections' => 1,
]);
$COURSE = get_course($course->id);
$sectionactions = new sectionactions($course);
// Highlight the section.
$sectioninfo1 = get_fast_modinfo($course)->get_section_info(1);
$sectionactions->set_marker($sectioninfo1, true);
$this->assertTrue(course_get_format($course)->is_section_current(1));
$this->assertEquals(1, $COURSE->marker);
// Unhighlight the section.
$sectionactions->remove_all_markers();
$this->assertFalse(course_get_format($course)->is_section_current(1));
$this->assertEquals(0, $COURSE->marker);
}
}