Merge branch 'MDL-86860-main' of https://github.com/sarjona/moodle

This commit is contained in:
Amaia Anabitarte
2025-11-24 10:26:41 +01:00
12 changed files with 189 additions and 20 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
@@ -0,0 +1,9 @@
issueNumber: MDL-86860
notes:
core_course:
- message: >-
The `course_set_marker` function has been deprecated and should no
longer be used.
Please consider using the equivalent methods, `set_marker` or `remove_all_markers`,
in `core_courseformat\local\sectionactions` instead.
type: deprecated
+1 -1
View File
@@ -1826,7 +1826,7 @@ abstract class base {
// Remove the marker if it points to this section.
if ($section->section == $course->marker) {
course_set_marker($course->id, 0);
\core_courseformat\formatactions::section($course->id)->remove_all_markers();
}
$lastsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
@@ -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,
);
}
}
@@ -388,7 +388,8 @@ final class sectionactions_test extends \advanced_testcase {
);
// Remove marked section.
course_set_marker($course->id, 1);
$sectioninfo = get_fast_modinfo($course->id)->get_section_info(1);
\core_courseformat\formatactions::section($course->id)->set_marker($sectioninfo, true);
$this->assertTrue(course_get_format($course)->is_section_current(1));
$this->assertTrue($sectionactions->delete(
get_fast_modinfo($course)->get_section_info(1),
@@ -911,4 +912,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);
}
}
@@ -68,7 +68,8 @@ class stateactions extends stateactions_base {
}
// Mark the new one.
course_set_marker($course->id, $section->section);
$sectioninfo = get_fast_modinfo($course->id)->get_section_info($section->section);
\core_courseformat\formatactions::section($course->id)->set_marker($sectioninfo, true);
$updates->add_section_put($section->id);
if ($previousmarker) {
$section = $modinfo->get_section_info($previousmarker);
@@ -103,7 +104,7 @@ class stateactions extends stateactions_base {
// Get the previous marked section and unmark it.
$modinfo = get_fast_modinfo($course);
$previousmarker = $DB->get_field("course", "marker", ['id' => $course->id]);
course_set_marker($course->id, 0);
\core_courseformat\formatactions::section($course->id)->remove_all_markers();
$section = $modinfo->get_section_info($previousmarker, MUST_EXIST);
$updates->add_section_put($section->id);
+6 -1
View File
@@ -44,7 +44,12 @@ $context = context_course::instance($course->id);
if (($marker >= 0) && has_capability('moodle/course:setcurrentsection', $context) && confirm_sesskey()) {
$course->marker = $marker;
course_set_marker($course->id, $marker);
if ($marker == 0) {
\core_courseformat\formatactions::section($course->id)->remove_all_markers();
} else {
$sectioninfo = get_fast_modinfo($course->id)->get_section_info($marker);
\core_courseformat\formatactions::section($course->id)->set_marker($sectioninfo, true);
}
}
// Make sure section 0 is created.
+6 -1
View File
@@ -388,7 +388,12 @@ class format_topics extends core_courseformat\base {
if ($section->section && ($action === 'setmarker' || $action === 'removemarker')) {
// Format 'topics' allows to set and remove markers in addition to common section actions.
require_capability('moodle/course:setcurrentsection', context_course::instance($this->courseid));
course_set_marker($this->courseid, ($action === 'setmarker') ? $section->section : 0);
if ($action === 'setmarker') {
$sectioninfo = get_fast_modinfo($this->courseid)->get_section_info($section->section);
\core_courseformat\formatactions::section($this->courseid)->set_marker($sectioninfo, true);
} else {
\core_courseformat\formatactions::section($this->courseid)->remove_all_markers();
}
return null;
}
@@ -133,7 +133,8 @@ final class stateactions_test extends \advanced_testcase {
);
// Highlight section 1.
course_set_marker($course->id, 1);
$sectioninfo = get_fast_modinfo($course->id)->get_section_info(1);
\core_courseformat\formatactions::section($course->id)->set_marker($sectioninfo, true);
$this->enrol_user($course, $rolename);
+25 -10
View File
@@ -421,16 +421,28 @@ function get_module_types_names($plural = false, $resetcache = false) {
*
* @param int $courseid course id
* @param int $marker highlight section with this number, 0 means remove higlightin
* @return void
* @deprecated since Moodle 5.2.
* @todo MDL-87238 Final deprecation in Moodle 6.0.
*/
#[\core\attribute\deprecated(
replacement: 'core_courseformat\local\sectionactions::set_marker',
since: '5.2',
mdl: 'MDL-86860',
reason: 'Course activity editing global functions have been moved to format actions',
)]
function course_set_marker($courseid, $marker) {
global $DB, $COURSE;
$DB->set_field("course", "marker", $marker, array('id' => $courseid));
if ($COURSE && $COURSE->id == $courseid) {
$COURSE->marker = $marker;
\core\deprecation::emit_deprecation(__FUNCTION__);
if ($marker === 0) {
formatactions::section($courseid)->remove_all_markers();
return;
}
core_courseformat\base::reset_course_cache($courseid);
course_modinfo::clear_instance_cache($courseid);
$sectioninfo = get_fast_modinfo($courseid)->get_section_info($marker);
if (!$sectioninfo) {
return;
}
formatactions::section($courseid)->set_marker($sectioninfo, true);
}
/**
@@ -960,11 +972,14 @@ function move_section_to($course, $section, $destination, $ignorenumsections = f
// If we move the highlighted section itself, then just highlight the destination.
// Adjust the higlighted section location if we move something over it either direction.
if ($section == $course->marker) {
course_set_marker($course->id, $destination);
$sectioninfo = get_fast_modinfo($course->id)->get_section_info($destination);
formatactions::section($course->id)->set_marker($sectioninfo, true);
} else if ($section > $course->marker && $course->marker >= $destination) {
course_set_marker($course->id, $course->marker+1);
$sectioninfo = get_fast_modinfo($course->id)->get_section_info($course->marker + 1);
formatactions::section($course->id)->set_marker($sectioninfo, true);
} else if ($section < $course->marker && $course->marker <= $destination) {
course_set_marker($course->id, $course->marker-1);
$sectioninfo = get_fast_modinfo($course->id)->get_section_info($course->marker - 1);
formatactions::section($course->id)->set_marker($sectioninfo, true);
}
$transaction->allow_commit();
+5 -2
View File
@@ -1046,7 +1046,9 @@ final class courselib_test extends advanced_testcase {
$course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
// Set course marker to the section we are going to move..
course_set_marker($course->id, 2);
$sectioninfo = get_fast_modinfo($course->id)->get_section_info(2);
\core_courseformat\formatactions::section($course->id)->set_marker($sectioninfo, true);
// Verify that the course marker is set correctly.
$course = $DB->get_record('course', array('id' => $course->id));
$this->assertEquals(2, $course->marker);
@@ -1266,7 +1268,8 @@ final class courselib_test extends advanced_testcase {
3 => array($assign5->cmid)), get_fast_modinfo($course)->sections);
// Remove marked section.
course_set_marker($course->id, 1);
$sectioninfo = get_fast_modinfo($course->id)->get_section_info(1);
\core_courseformat\formatactions::section($course->id)->set_marker($sectioninfo, true);
$this->assertTrue(course_get_format($course)->is_section_current(1));
$this->assertTrue(course_delete_section($course, 1, true));
$this->assertFalse(course_get_format($course)->is_section_current(1));
+7 -1
View File
@@ -228,7 +228,13 @@ if ($PAGE->user_allowed_editing()) {
'The marker param in course view is deprecated. Please use course/format/update.php instead.',
DEBUG_DEVELOPER
);
course_set_marker($course->id, $marker);
if ($marker == 0) {
\core_courseformat\formatactions::section($course->id)->remove_all_markers();
} else {
$sectioninfo = get_fast_modinfo($course->id)->get_section_info($marker);
\core_courseformat\formatactions::section($course->id)->set_marker($sectioninfo, true);
}
if ($sectionid) {
redirect(course_get_url($course, $section, ['navigation' => true]));
} else {