diff --git a/Moodle/PHP/renametopics.php b/Moodle/PHP/renametopics.php new file mode 100644 index 0000000..2778eac --- /dev/null +++ b/Moodle/PHP/renametopics.php @@ -0,0 +1,52 @@ +dirroot . '/course/lib.php'); + +global $DB; + +$courseid = 1449; // ID of your course + +// Read list.txt +$listfile = __DIR__ . '/list.txt'; +if (!file_exists($listfile)) { + cli_error("File list.txt not found."); +} +$lines = file($listfile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); +$total = count($lines); + +if ($total === 0) { + cli_error("list.txt is empty."); +} + +echo "Renaming topics for course ID {$courseid}...\n"; + +// Fetch all course sections +$sections = $DB->get_records('course_sections', ['course' => $courseid]); + +foreach ($sections as $section) { + $sectionnum = $section->section; + + if ($sectionnum < $total) { + $newname = trim($lines[$sectionnum]); + + if ($newname !== '') { + // Prepare data to update + $data = ['name' => $newname]; + + // Call Moodle API with 3 arguments + course_update_section($courseid, $section, $data); + + echo "Section {$sectionnum} renamed to: {$newname}\n"; + } else { + echo "Section {$sectionnum}: Skipped (empty line).\n"; + } + } else { + echo "Section {$sectionnum}: No corresponding line in list.txt.\n"; + } +} + +echo "Done.\n";