115 lines
3.5 KiB
PHP
115 lines
3.5 KiB
PHP
#!/usr/bin/env php
|
|
<?php
|
|
|
|
// Bắt buộc để script CLI hoạt động với Moodle
|
|
define('CLI_SCRIPT', true);
|
|
define('DIRROOT', '/home/english.huph.edu.vn/public_html/english');
|
|
|
|
// Đường dẫn đến config.php
|
|
require_once(DIRROOT . '/config.php');
|
|
require_once($CFG->libdir . '/clilib.php');
|
|
require_once($CFG->dirroot . '/course/lib.php');
|
|
require_once($CFG->dirroot . '/course/externallib.php');
|
|
|
|
// 🛠 Nhận tham số dòng lệnh
|
|
global $argv;
|
|
|
|
if (count($argv) < 4) {
|
|
echo "❗Cách dùng: create_course_list <categoryid> <format>\n";
|
|
echo " Ví dụ : create_course_list 3 topics\n";
|
|
exit(1);
|
|
}
|
|
|
|
$categoryid = intval($argv[1]);
|
|
$format = $argv[2];
|
|
$cwd = $argv[3]; // Thư mục đang thao tác (nơi chứa list.txt)
|
|
|
|
// 🧾 Đọc tên khóa học từ thư mục
|
|
$coursefullname = basename($cwd);
|
|
$shortname = preg_replace('/\s+/', '', $coursefullname);
|
|
|
|
echo "📂 Thư mục hiện tại: $cwd\n";
|
|
echo "📘 Tên khóa học: $coursefullname\n";
|
|
echo "🔤 Shortname: $shortname\n";
|
|
echo "📁 Category ID: $categoryid\n";
|
|
echo "🧱 Course Format: $format\n";
|
|
|
|
// 📄 Đọc danh sách topic từ file list.txt
|
|
$listfile = $cwd . '/list.txt';
|
|
|
|
if (!file_exists($listfile)) {
|
|
echo "❌ Không tìm thấy file list.txt trong thư mục: $cwd\n";
|
|
exit(1);
|
|
}
|
|
|
|
$lines = file($listfile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
$numsections = count($lines);
|
|
echo "🧩 Số topic từ list.txt: $numsections\n";
|
|
|
|
// ✅ Kiểm tra shortname đã tồn tại chưa
|
|
if ($DB->record_exists('course', ['shortname' => $shortname])) {
|
|
echo "⚠️ Shortname '$shortname' đã tồn tại. Dừng lại để tránh trùng.\n";
|
|
exit(1);
|
|
}
|
|
|
|
// ✅ Tạo khóa học
|
|
$course = new stdClass();
|
|
$course->fullname = $coursefullname;
|
|
$course->shortname = $shortname;
|
|
$course->category = $categoryid;
|
|
$course->format = $format;
|
|
$course->numsections = $numsections;
|
|
$course->summary = $coursefullname;
|
|
$course->summaryformat = FORMAT_HTML;
|
|
$course->visible = 1;
|
|
|
|
$newcourse = create_course($course);
|
|
echo "✅ Đã tạo khóa học thành công với ID: {$newcourse->id}\n";
|
|
|
|
// ✅ Tạo và đặt tên cho từng topic từ danh sách
|
|
foreach ($lines as $i => $topicname) {
|
|
$index = $i; // Bắt đầu từ 0
|
|
|
|
$section = $DB->get_record('course_sections', [
|
|
'course' => $newcourse->id,
|
|
'section' => $index
|
|
]);
|
|
|
|
if ($section) {
|
|
$section->name = trim($topicname);
|
|
$DB->update_record('course_sections', $section);
|
|
echo "🔁 Đặt tên topic [$index] thành: $topicname\n";
|
|
} else {
|
|
$section = course_create_section($newcourse->id, $index);
|
|
$section->name = trim($topicname);
|
|
$section->summary = '';
|
|
$section->summaryformat = FORMAT_HTML;
|
|
$DB->update_record('course_sections', $section);
|
|
echo "🆕 Tạo topic [$index]: $topicname\n";
|
|
}
|
|
}
|
|
|
|
// ✅ Ghi course ID vào file .env
|
|
$envFile = $cwd . '/.env';
|
|
$courseIdLine = "COURSEID={$newcourse->id}";
|
|
|
|
if (file_exists($envFile)) {
|
|
$lines = file($envFile, FILE_IGNORE_NEW_LINES);
|
|
$found = false;
|
|
foreach ($lines as &$line) {
|
|
if (str_starts_with(trim($line), 'COURSEID=')) {
|
|
$line = $courseIdLine;
|
|
$found = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!$found) {
|
|
$lines[] = $courseIdLine;
|
|
}
|
|
file_put_contents($envFile, implode(PHP_EOL, $lines) . PHP_EOL);
|
|
echo "📄 Đã cập nhật COURSEID vào .env\n";
|
|
} else {
|
|
file_put_contents($envFile, $courseIdLine . PHP_EOL);
|
|
echo "📄 Đã tạo file .env với COURSEID={$newcourse->id}\n";
|
|
}
|