Files
Documentation/Moodle/PHP/renametopics.php
2025-08-20 23:34:45 +07:00

53 lines
1.3 KiB
PHP

<?php
// renametopics.php
// PHP CLI script to rename topics of a Moodle course based on list.txt
define('CLI_SCRIPT', true);
require(__DIR__ . '/config.php');
require_once($CFG->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";