diff --git a/.upgradenotes/MDL-87280-2025121211100178.yml b/.upgradenotes/MDL-87280-2025121211100178.yml new file mode 100644 index 00000000000..2b4a417d750 --- /dev/null +++ b/.upgradenotes/MDL-87280-2025121211100178.yml @@ -0,0 +1,8 @@ +issueNumber: MDL-87280 +notes: + mod_subsection: + - message: >- + A new scheduled task, `remove_existing_descriptions`, has been added. + Once enabled, this task will remove the descriptions for all existing + subsection instances. + type: improved diff --git a/public/mod/subsection/classes/task/remove_existing_descriptions_task.php b/public/mod/subsection/classes/task/remove_existing_descriptions_task.php new file mode 100644 index 00000000000..c4515cc5b11 --- /dev/null +++ b/public/mod/subsection/classes/task/remove_existing_descriptions_task.php @@ -0,0 +1,46 @@ +. + +namespace mod_subsection\task; + +use core\task\scheduled_task; + +/** + * A scheduled task to remove existing descriptions from subsection instances. + * + * @package mod_subsection + * @copyright 2025 Sara Arjona + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class remove_existing_descriptions_task extends scheduled_task { + /** + * Return the task name. + * + * @return string The name of the task. + */ + public function get_name(): string { + return get_string('removeexistingdescriptions', 'mod_subsection'); + } + + /** + * Execute the task. + */ + public function execute(): void { + global $DB; + + $DB->set_field('course_sections', 'summary', '', ['component' => 'mod_subsection']); + } +} diff --git a/public/mod/subsection/db/tasks.php b/public/mod/subsection/db/tasks.php new file mode 100644 index 00000000000..7a78fbc02a5 --- /dev/null +++ b/public/mod/subsection/db/tasks.php @@ -0,0 +1,39 @@ +. + +/** + * Definition of Subsection scheduled tasks. + * + * @package mod_subsection + * @category task + * @copyright 2025 Sara Arjona + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$tasks = [ + [ + 'classname' => '\mod_subsection\task\remove_existing_descriptions_task', + 'blocking' => 0, + 'minute' => 'R', + 'hour' => '0', + 'day' => '*', + 'dayofweek' => '*', + 'month' => '*', + 'disabled' => true, + ], +]; diff --git a/public/mod/subsection/lang/en/subsection.php b/public/mod/subsection/lang/en/subsection.php index 3893fbe662a..4989639fcb5 100644 --- a/public/mod/subsection/lang/en/subsection.php +++ b/public/mod/subsection/lang/en/subsection.php @@ -29,6 +29,7 @@ $string['pluginadministration'] = 'Subsection administration'; $string['pluginname'] = 'Subsection'; $string['privacy:metadata'] = 'Subsection does not store any personal data'; $string['quickcreatename'] = 'New subsection'; +$string['removeexistingdescriptions'] = 'Remove existing descriptions from subsection instances'; $string['subsection:addinstance'] = 'Add subsection'; $string['subsection:view'] = 'View subsection'; $string['subsectionname'] = 'Name'; diff --git a/public/mod/subsection/tests/task/remove_existing_descriptions_task_test.php b/public/mod/subsection/tests/task/remove_existing_descriptions_task_test.php new file mode 100644 index 00000000000..f1ea59fc1bc --- /dev/null +++ b/public/mod/subsection/tests/task/remove_existing_descriptions_task_test.php @@ -0,0 +1,99 @@ +. + +namespace mod_subsection\task; + +/** + * Class containing unit tests for the remove existing descriptions task. + * + * @package mod_subsection + * @copyright 2025 Sara Arjona + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +#[\PHPUnit\Framework\Attributes\CoversClass(remove_existing_descriptions_task::class)] +final class remove_existing_descriptions_task_test extends \advanced_testcase { + /** + * Test remove_existing_descriptions task. + */ + public function test_remove_existing_descriptions(): void { + global $DB; + + $this->resetAfterTest(); + + $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 1]); + $this->getDataGenerator()->create_module('subsection', ['course' => $course->id, 'section' => 1]); + // Add description to course sections and the subsection. + $DB->set_field( + 'course_sections', + 'summary', + 'Section with description', + ['course' => $course->id], + ); + // Add another subsection without description. + $this->getDataGenerator()->create_module('subsection', ['course' => $course->id, 'section' => 1]); + + // Check only 2 sections and 1 subsection have description. + $this->assertEquals( + 3, + $DB->count_records_select( + 'course_sections', + 'course = :courseid AND summary != \'\'', + ['courseid' => $course->id], + ), + ); + $this->assertEquals( + 2, + $DB->count_records_select( + 'course_sections', + 'course = :courseid AND component = \'mod_subsection\'', + ['courseid' => $course->id], + ), + ); + $this->assertEquals( + 1, + $DB->count_records_select( + 'course_sections', + 'course = :courseid AND component = \'mod_subsection\' AND summary != \'\'', + ['courseid' => $course->id], + ), + ); + + // Run the task. + ob_start(); + $task = new remove_existing_descriptions_task(); + $task->execute(); + ob_end_clean(); + + // Check only 2 sections keep having description after running the task. + $this->assertEquals( + 2, + $DB->count_records_select( + 'course_sections', + 'course = :courseid AND summary != \'\'', + ['courseid' => $course->id], + ), + ); + // Check no subsection has description after running the task. + $this->assertEquals( + 0, + $DB->count_records_select( + 'course_sections', + 'course = :courseid AND component = \'mod_subsection\' AND summary != \'\'', + ['courseid' => $course->id], + ), + ); + } +} diff --git a/public/mod/subsection/version.php b/public/mod/subsection/version.php index 59403937cd8..e1592e53f6d 100644 --- a/public/mod/subsection/version.php +++ b/public/mod/subsection/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'mod_subsection'; -$plugin->version = 2025100600; +$plugin->version = 2025100601; $plugin->requires = 2025092600; $plugin->maturity = MATURITY_STABLE;