diff --git a/Moodle/move_pages_to_topics.php b/Moodle/move_pages_to_topics.php new file mode 100644 index 0000000..14adc75 --- /dev/null +++ b/Moodle/move_pages_to_topics.php @@ -0,0 +1,151 @@ +dirroot . '/lib/clilib.php'); +require_once($CFG->dirroot . '/lib/moodlelib.php'); +require_once($CFG->dirroot . '/course/lib.php'); + +// Đặt các tham số CLI +list($options, $unrecognised) = cli_get_params([ + 'courseid' => 0, + 'help' => false, +], [ + 'c' => 'courseid', + 'h' => 'help', +]); + +// Hiển thị trợ giúp nếu cần +if ($options['help'] || !is_numeric($options['courseid']) || $options['courseid'] <= 0) { + $help = "Di chuyển các trang trong Moodle.\n\n" . + "Sử dụng: php move_pages_to_topics.php [OPTIONS]\n" . + " --courseid, -c COURSEID ID của khóa học (bắt buộc).\n" . + " --help, -h Hiển thị trợ giúp này.\n"; + echo $help; + exit(0); +} + +$courseid = (int)$options['courseid']; + +// Lấy thông tin khóa học và các section +$course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST); +$sections = $DB->get_records('course_sections', ['course' => $courseid], 'section ASC'); + +cli_heading("Bắt đầu di chuyển các trang cho khóa học ID: $courseid"); + +$count_moved = 0; +$count_total = 0; + +// Lấy ID của module 'page' +$page_module_id = $DB->get_field('modules', 'id', ['name' => 'page']); + +if (!$page_module_id) { + cli_write("Không tìm thấy module 'page'. Kiểm tra cài đặt Moodle."); + exit(1); +} + +// Lấy tất cả các module 'page' trong toàn bộ khóa học +$all_pages_in_course = $DB->get_records('course_modules', ['course' => $courseid, 'module' => $page_module_id]); + +if (empty($all_pages_in_course)) { + cli_write("Không tìm thấy trang nào trong khóa học này. Kết thúc."); + exit(0); +} + +// Lặp qua tất cả các trang trong khóa học +foreach ($all_pages_in_course as $cm) { + $count_total++; + + $page = $DB->get_record('page', ['id' => $cm->instance]); + if (!$page) { + continue; + } + $page_name = $page->name; + + cli_write("Đang xử lý trang: " . $page_name); + + $page_name_lowercase = strtolower($page_name); + + // Lấy một từ khóa duy nhất từ tên trang + $main_keyword = ''; + preg_match_all('/\b([a-z]{5,})\b/', $page_name_lowercase, $matches); + if (!empty($matches[1])) { + // Lấy từ đầu tiên có ý nghĩa + $main_keyword = $matches[1][0]; + } + + if (empty($main_keyword)) { + cli_write(" -> Không tìm thấy từ khóa có ý nghĩa trong tên trang. Bỏ qua trang này."); + continue; + } + + $best_match_section = null; + $found_match = false; + + // Lặp qua tất cả các section để tìm section phù hợp + foreach ($sections as $section) { + if ($section->id === $cm->section) { + continue; + } + + $section_name = ''; + if ($section->name) { + $section_name = $section->name; + } else { + $section_name = get_string('topic', 'moodle') . ' ' . $section->section; + } + $section_name_lowercase = strtolower($section_name); + + // Kiểm tra xem từ khóa chính của trang có trong tên section không + if (str_contains($section_name_lowercase, $main_keyword)) { + $best_match_section = $section; + $found_match = true; + break; + } + } + + if ($found_match) { + // Kiểm tra xem trang đã ở đúng chỗ chưa + if ($cm->section === $best_match_section->id) { + cli_write(" -> Trang đã ở đúng section: " . $best_match_section->name . ". Bỏ qua."); + continue; + } + + cli_write(" -> Tìm thấy sự trùng khớp với section: " . $best_match_section->name); + + $old_section = $DB->get_record('course_sections', ['id' => $cm->section]); + if (!$old_section) { + cli_write(" -> Lỗi: Không tìm thấy section cũ. Bỏ qua trang này."); + continue; + } + + $modid_to_move = $cm->id; + + $old_sequence = explode(',', $old_section->sequence); + $new_sequence = explode(',', $best_match_section->sequence); + + if (($key = array_search($modid_to_move, $old_sequence)) !== false) { + unset($old_sequence[$key]); + } + $old_section->sequence = implode(',', array_filter($old_sequence)); + + $new_sequence[] = $modid_to_move; + $best_match_section->sequence = implode(',', array_filter($new_sequence)); + + $DB->set_field('course_modules', 'section', $best_match_section->id, ['id' => $modid_to_move]); + $DB->update_record('course_sections', $old_section); + $DB->update_record('course_sections', $best_match_section); + + rebuild_course_cache($courseid, true); + + cli_write(" -> Đã di chuyển thành công!"); + $count_moved++; + } else { + cli_write(" -> Không tìm thấy sự trùng khớp phù hợp. Bỏ qua trang này."); + } +} + +cli_write("Hoàn thành! Đã di chuyển $count_moved trên tổng số $count_total trang."); \ No newline at end of file