Update code
This commit is contained in:
123
Moodle/PHP/page.php
Normal file
123
Moodle/PHP/page.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
// Đặt cấu hình cho môi trường Moodle.
|
||||
define('CLI_SCRIPT', true); // Định nghĩa để script chạy trên command line.
|
||||
require_once(__DIR__ . '/config.php');
|
||||
require_once($CFG->dirroot . '/course/lib.php');
|
||||
require_once($CFG->dirroot . '/mod/page/lib.php');
|
||||
|
||||
/**
|
||||
* Đảm bảo chuỗi ở định dạng UTF-8.
|
||||
*
|
||||
* @param string $string Chuỗi cần kiểm tra.
|
||||
* @return string Chuỗi đã được đảm bảo là UTF-8.
|
||||
*/
|
||||
function ensure_utf8($string) {
|
||||
return mb_convert_encoding($string, 'UTF-8', 'auto');
|
||||
}
|
||||
|
||||
/**
|
||||
* Hàm tạo một Page Activity trong Moodle.
|
||||
*/
|
||||
function create_page_activity($courseid, $topicid, $page_name, $page_content) {
|
||||
global $DB;
|
||||
|
||||
try {
|
||||
$course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST);
|
||||
$section = $DB->get_record('course_sections', ['course' => $courseid, 'section' => $topicid], '*', MUST_EXIST);
|
||||
|
||||
// Đảm bảo tên và nội dung luôn là UTF-8
|
||||
$page_name = ensure_utf8($page_name);
|
||||
$page_content = ensure_utf8($page_content);
|
||||
|
||||
$page_data = (object)[
|
||||
'course' => $courseid,
|
||||
'name' => $page_name,
|
||||
'content' => $page_content,
|
||||
'contentformat' => FORMAT_HTML,
|
||||
'intro' => 'Automatically generated page activity.',
|
||||
'introformat' => FORMAT_HTML,
|
||||
'timemodified' => time()
|
||||
];
|
||||
|
||||
$pageid = $DB->insert_record('page', $page_data);
|
||||
if (!$pageid) {
|
||||
throw new moodle_exception('Không thể tạo bản ghi trong bảng page.');
|
||||
}
|
||||
|
||||
$module = $DB->get_record('modules', ['name' => 'page'], '*', MUST_EXIST);
|
||||
$module_instance = (object)[
|
||||
'course' => $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', $module_instance);
|
||||
if (!$cmid) {
|
||||
throw new moodle_exception('Không thể tạo bản ghi trong bảng course_modules.');
|
||||
}
|
||||
|
||||
$section->sequence = trim($section->sequence . ',' . $cmid, ',');
|
||||
$DB->update_record('course_sections', $section);
|
||||
|
||||
rebuild_course_cache($courseid);
|
||||
|
||||
return $pageid;
|
||||
} catch (Exception $e) {
|
||||
echo "Lỗi: " . $e->getMessage() . "\n";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Phân tích dòng stream và đảm bảo tên UTF-8.
|
||||
*/
|
||||
function parse_stream_line($stream_line) {
|
||||
if (strpos($stream_line, '[') === false || strpos($stream_line, ']') === false) {
|
||||
return ['', ''];
|
||||
}
|
||||
|
||||
preg_match('/\[(.*?)\]/', $stream_line, $matches);
|
||||
if (isset($matches[1])) {
|
||||
$file_name_with_extension = $matches[1];
|
||||
$file_name_without_extension = pathinfo($file_name_with_extension, PATHINFO_FILENAME);
|
||||
$file_name_clean = preg_replace('/[^\w\s-]/u', '', $file_name_without_extension); // Sử dụng flag `u` cho UTF-8
|
||||
$file_name_clean = preg_replace('/[-\s]+/', ' ', $file_name_clean);
|
||||
$file_name_clean = trim($file_name_clean);
|
||||
return [ensure_utf8($file_name_clean), ensure_utf8($stream_line)];
|
||||
}
|
||||
|
||||
return ['', ''];
|
||||
}
|
||||
|
||||
// Nhận tham số từ dòng lệnh
|
||||
if ($argc < 4) {
|
||||
echo "Usage: php script.php <filename> <courseid> <topicid>\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$filename = $argv[1];
|
||||
$courseid = intval($argv[2]);
|
||||
$topicid = intval($argv[3]);
|
||||
|
||||
if (!file_exists($filename)) {
|
||||
echo "Tệp không tồn tại: {$filename}\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$file_lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
foreach ($file_lines as $stream_line) {
|
||||
list($page_name, $page_content) = parse_stream_line($stream_line);
|
||||
$pageid = create_page_activity($courseid, $topicid, $page_name, $page_content);
|
||||
if ($pageid !== false) {
|
||||
echo "Tạo Page Activity thành công! Page ID: {$pageid}\n";
|
||||
} else {
|
||||
echo "Không thể tạo Page Activity.\n";
|
||||
}
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user