Files
Documentation/Moodle/PHP/create_moodle_course.php
2025-08-20 09:47:42 +07:00

132 lines
3.9 KiB
PHP

#!/usr/bin/env php
<?php
// Bắt buộc để script CLI hoạt động với Moodle
define('CLI_SCRIPT', true);
$site = intval($argv[1]); // site ID
$categoryid = intval($argv[2]);
$format = $argv[3];
// ✅ Xác định DIRROOT theo site
switch ($site) {
case 1:
define('DIRROOT', '/home/online.huph.edu.vn/public_html/online');
break;
case 2:
define('DIRROOT', '/home/elearning.huph.edu.vn/public_html/elearning');
break;
case 3:
define('DIRROOT', '/home/english.huph.edu.vn/public_html/english');
break;
default:
echo "❌ Site ID không hợp lệ: $site\n";
exit(1);
}
// Đườ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 <categoryid> <format>\n";
echo " Ví dụ : create_course 3 topics\n";
exit(1);
}
$categoryid = intval($argv[2]);
$format = $argv[3];
$cwd = $argv[4]; // Thư mục làm việc thực tế
$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";
// ✅ Lấy danh sách thư mục con để đếm số topic
$topics = array_filter(glob($cwd . '/*'), 'is_dir');
$numsections = count($topics);
echo "🧩 Số topic được tạo: $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 tên cho từng topic/section
foreach ($topics as $dir) {
$dirname = basename($dir);
$index = 0;
if (preg_match('/^(\d+)/', $dirname, $matches)) {
$index = intval($matches[1]) - 1;
}
$topicname = preg_replace('/^\d+\.\s*/', '', $dirname);
$section = $DB->get_record('course_sections', [
'course' => $newcourse->id,
'section' => $index
]);
if ($section) {
$section->name = $topicname;
$DB->update_record('course_sections', $section);
echo "🔁 Đổi tên topic [$index] thành: $topicname\n";
} else {
$section = course_create_section($newcourse->id, $index);
$section->name = $topicname;
$section->summary = '';
$section->summaryformat = FORMAT_HTML;
$DB->update_record('course_sections', $section);
echo "🆕 Tạo topic mới [$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";
}