dirroot . '/course/lib.php'); require_once($CFG->dirroot . '/mod/page/lib.php'); class PageActivityGenerator { protected $courseId; protected $topicId; protected $streamFile; public function __construct($streamFile, $courseId, $topicId) { $this->courseId = $courseId; $this->topicId = $topicId; $this->streamFile = $streamFile; } public function run() { if (!file_exists($this->streamFile)) { echo "Lỗi: Không tìm thấy file: {$this->streamFile}\n"; exit(1); } $lines = file($this->streamFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($lines as $line) { list($filename, $url) = $this->parse_stream_line($line); if (!$filename || !$url) { echo "⚠️ Bỏ qua dòng không hợp lệ: $line\n"; continue; } $pageName = $this->ensure_utf8($filename); $pageContent = $this->ensure_utf8('[stream=' . $url . ']'); $success = $this->create_page_activity($pageName, $pageContent); if ($success) { echo "✅ Đã tạo Page: $pageName\n"; } } } protected function ensure_utf8($string) { return mb_convert_encoding($string, 'UTF-8', mb_detect_encoding($string, 'UTF-8, ISO-8859-1, ISO-8859-15', true)); } protected function parse_stream_line($line) { if (preg_match('/\[stream=(.*?)\]/', $line, $matches)) { $url = trim($matches[1]); $filename = pathinfo($url, PATHINFO_FILENAME); $filename_clean = preg_replace('/[^\p{L}\p{N}\s_-]/u', '', $filename); // Giữ unicode và ký tự hợp lệ return [$filename_clean, $url]; } return ['', '']; } protected function create_page_activity($pageName, $pageContent) { global $DB; try { // Lấy thông tin course & topic $course = $DB->get_record('course', ['id' => $this->courseId], '*', MUST_EXIST); $section = $DB->get_record('course_sections', ['course' => $this->courseId, 'section' => $this->topicId], '*', MUST_EXIST); // Tạo bản ghi trong bảng page $page = (object)[ 'course' => $this->courseId, 'name' => $pageName, 'content' => $pageContent, 'contentformat' => FORMAT_HTML, 'intro' => 'Automatically generated page activity.', 'introformat' => FORMAT_HTML, 'timemodified' => time() ]; $pageId = $DB->insert_record('page', $page); if (!$pageId) { throw new moodle_exception("Không thể tạo bản ghi trong bảng page."); } // Tạo bản ghi trong bảng course_modules $module = $DB->get_record('modules', ['name' => 'page'], '*', MUST_EXIST); $cm = (object)[ 'course' => $this->courseId, 'module' => $module->id, 'instance' => $pageId, 'section' => $section->id, 'added' => time(), 'visible' => 1, 'visibleold' => 1, 'groupmode' => 0, 'groupingid' => 0, 'groupmembersonly' => 0 ]; $cmid = $DB->insert_record('course_modules', $cm); if (!$cmid) { throw new moodle_exception("Không thể tạo bản ghi trong bảng course_modules."); } // Cập nhật chuỗi hoạt động của section if (!empty($section->sequence)) { $section->sequence .= ',' . $cmid; } else { $section->sequence = $cmid; } $DB->update_record('course_sections', $section); // Làm mới cache khóa học rebuild_course_cache($this->courseId); return $pageId; } catch (Exception $e) { echo "❌ Lỗi: " . $e->getMessage() . "\n"; return false; } } } // CLI entry point if ($argc !== 4) { echo "Cách dùng: php script.php \n"; exit(1); } $streamFile = $argv[1]; $courseId = (int)$argv[2]; $topicId = (int)$argv[3]; $generator = new PageActivityGenerator($streamFile, $courseId, $topicId); $generator->run();