Commite Code

This commit is contained in:
2025-08-20 09:47:42 +07:00
parent 066cecfad9
commit 7e99b228f5
47 changed files with 5279 additions and 0 deletions

5
Moodle/PHP/agets.sh Normal file
View File

@@ -0,0 +1,5 @@
aria2c -x 16 --content-disposition-default-utf8=true --check-certificate=false -i list.txt \
--continue=true \
--check-integrity=true \
--max-concurrent-downloads=5 \
--log=aria2.log --log-level=notice

62
Moodle/PHP/batch_run.sh Normal file
View File

@@ -0,0 +1,62 @@
#!/usr/bin/env bash
# Kiểm tra có đối số không
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: $(basename "$0") <arg1> <arg2>"
exit 1
fi
siteid=$1
courseid=$2
# Lưu thư mục hiện tại (nơi chạy script, không phải nơi đặt script)
BASEDIR="$(pwd)"
for dir in */ ; do
# Bỏ dấu '/' cuối
dirname="${dir%/}"
# Cắt phần số trước dấu '.' và trim khoảng trắng
index=$(echo "$dirname" | cut -d'.' -f1 | tr -d '[:space:]')
# Bỏ số 0 đầu nếu có
index_nozero=$(echo "$index" | sed 's/^0*//')
# Nếu chuỗi rỗng (trường hợp '0')
if [ -z "$index_nozero" ]; then
index_nozero=0
fi
# Kiểm tra là số hợp lệ
if [[ "$index_nozero" =~ ^[0-9]+$ ]]; then
# Tính i-1
i_minus_1=$((index_nozero - 1))
echo "===> Đang xử lý thư mục: $dirname (index: $index_nozero, i-1: $i_minus_1)"
# Chuyển vào thư mục
cd "$dirname" || { echo "Không thể vào thư mục $dirname"; exit 1; }
# Gọi online.sh với courseid từ dòng lệnh và i-1
case "$siteid" in
1)
online.sh "$courseid" "$i_minus_1"
;;
2)
elearning.sh "$courseid" "$i_minus_1"
;;
3)
english.sh "$courseid" "$i_minus_1"
;;
*)
echo "Không hỗ trợ siteid=$siteid"
exit 1
;;
esac
# Quay lại thư mục gốc
cd "$BASEDIR"
else
echo "Bỏ qua $dirname (không bắt đầu bằng số)"
fi
done

View File

@@ -0,0 +1,7 @@
#!/bin/bash
# Lấy thư mục hiện tại nơi người dùng chạy lệnh
CURRENT_DIR="$(pwd)"
# Gọi PHP script, truyền tham số đầu vào và thư mục hiện tại
/usr/local/lsws/lsphp82/bin/php /usr/bin/create_moodle_course.php "$@" "$CURRENT_DIR"

View File

@@ -0,0 +1,131 @@
#!/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";
}

114
Moodle/PHP/createcourse.php Normal file
View File

@@ -0,0 +1,114 @@
#!/usr/bin/env php
<?php
// Bắt buộc để script CLI hoạt động với Moodle
define('CLI_SCRIPT', true);
define('DIRROOT', '/home/online.huph.edu.vn/public_html/online');
// Đườ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";
}

View File

@@ -0,0 +1,7 @@
#!/bin/bash
# Lấy thư mục hiện tại nơi người dùng chạy lệnh
CURRENT_DIR="$(pwd)"
# Gọi PHP script, truyền tham số đầu vào và thư mục hiện tại
php /usr/bin/createcourse.php "$@" "$CURRENT_DIR"

View File

@@ -0,0 +1,42 @@
#!/usr/bin/env php
<?php
// Bắt buộc để script CLI hoạt động với Moodle
define('CLI_SCRIPT', true);
define('DIRROOT', '/home/online.huph.edu.vn/public_html/online');
require_once(DIRROOT . '/config.php');
require_once($CFG->libdir . '/clilib.php');
require_once($CFG->dirroot . '/course/lib.php');
// 🛠 Nhận tham số dòng lệnh
global $argv;
if ($argc < 2) {
echo "❗ Cách dùng: delete_course <courseid>\n";
echo " Ví dụ : delete_course 123\n";
exit(1);
}
$courseid = intval($argv[1]);
if (!$course = $DB->get_record('course', ['id' => $courseid])) {
echo "⚠️ Không tìm thấy khóa học với ID: $courseid\n";
exit(1);
}
if ($course->id == SITEID) {
echo "❌ Không thể xóa site course (ID = SITEID).\n";
exit(1);
}
echo "⚠️ Bạn sắp xóa khóa học: [{$course->id}] {$course->fullname}\n";
// ✅ Thực hiện xóa
try {
delete_course($course, false); // false = không tái sắp xếp category
echo "✅ Đã xóa khóa học thành công.\n";
} catch (Exception $e) {
echo "❌ Lỗi khi xóa khóa học: " . $e->getMessage() . "\n";
exit(1);
}

47
Moodle/PHP/ebatch_run.sh Normal file
View File

@@ -0,0 +1,47 @@
#!/usr/bin/env bash
# Kiểm tra có đối số không
if [ -z "$1" ]; then
echo "Usage: $(basename "$0") <courseid>"
exit 1
fi
courseid="$1"
# Lưu thư mục hiện tại (nơi chạy script, không phải nơi đặt script)
BASEDIR="$(pwd)"
for dir in */ ; do
# Bỏ dấu '/' cuối
dirname="${dir%/}"
# Cắt phần số trước dấu '.' và trim khoảng trắng
index=$(echo "$dirname" | cut -d'.' -f1 | tr -d '[:space:]')
# Bỏ số 0 đầu nếu có
index_nozero=$(echo "$index" | sed 's/^0*//')
# Nếu chuỗi rỗng (trường hợp '0')
if [ -z "$index_nozero" ]; then
index_nozero=0
fi
# Kiểm tra là số hợp lệ
if [[ "$index_nozero" =~ ^[0-9]+$ ]]; then
# Tính i-1
i_minus_1=$((index_nozero - 1))
echo "===> Đang xử lý thư mục: $dirname (index: $index_nozero, i-1: $i_minus_1)"
# Chuyển vào thư mục
cd "$dirname" || { echo "Không thể vào thư mục $dirname"; exit 1; }
# Gọi online.sh với courseid từ dòng lệnh và i-1
elearning.sh "$courseid" "$i_minus_1"
# Quay lại thư mục gốc
cd "$BASEDIR"
else
echo "Bỏ qua $dirname (không bắt đầu bằng số)"
fi
done

View File

@@ -0,0 +1,7 @@
#!/bin/bash
# Lấy thư mục hiện tại nơi người dùng chạy lệnh
CURRENT_DIR="$(pwd)"
# Gọi PHP script, truyền tham số đầu vào và thư mục hiện tại
php /usr/bin/ecreate_moodle_course.php "$@" "$CURRENT_DIR"

View File

@@ -0,0 +1,113 @@
#!/usr/bin/env php
<?php
// Bắt buộc để script CLI hoạt động với Moodle
define('CLI_SCRIPT', true);
define('DIRROOT', '/home/elearning.huph.edu.vn/public_html/elearning');
// Đườ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[1]);
$format = $argv[2];
$cwd = $argv[3]; // 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";
}

View File

@@ -0,0 +1,42 @@
#!/usr/bin/env php
<?php
// Bắt buộc để script CLI hoạt động với Moodle
define('CLI_SCRIPT', true);
define('DIRROOT', '/home/elearning.huph.edu.vn/public_html/elearning');
require_once(DIRROOT . '/config.php');
require_once($CFG->libdir . '/clilib.php');
require_once($CFG->dirroot . '/course/lib.php');
// 🛠 Nhận tham số dòng lệnh
global $argv;
if ($argc < 2) {
echo "❗ Cách dùng: delete_course <courseid>\n";
echo " Ví dụ : delete_course 123\n";
exit(1);
}
$courseid = intval($argv[1]);
if (!$course = $DB->get_record('course', ['id' => $courseid])) {
echo "⚠️ Không tìm thấy khóa học với ID: $courseid\n";
exit(1);
}
if ($course->id == SITEID) {
echo "❌ Không thể xóa site course (ID = SITEID).\n";
exit(1);
}
echo "⚠️ Bạn sắp xóa khóa học: [{$course->id}] {$course->fullname}\n";
// ✅ Thực hiện xóa
try {
delete_course($course, false); // false = không tái sắp xếp category
echo "✅ Đã xóa khóa học thành công.\n";
} catch (Exception $e) {
echo "❌ Lỗi khi xóa khóa học: " . $e->getMessage() . "\n";
exit(1);
}

20
Moodle/PHP/elearning.sh Normal file
View File

@@ -0,0 +1,20 @@
#!/bin/bash
# Kiểm tra số lượng tham số đầu vào
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <courseid> <topicid>"
exit 1
fi
# Gán tham số đầu vào vào biến
COURSEID=$1
TOPICID=$2
# Lấy đường dẫn thư mục hiện tại
CURRENT_DIR=$(pwd)
# Chạy lệnh PHP với các tham số, sử dụng mdl.media từ thư mục hiện tại
/usr/local/lsws/lsphp82/bin/php /home/elearning.huph.edu.vn/public_html/elearning/page.php "$CURRENT_DIR/mdl.media" "$COURSEID" "$TOPICID"
# Hiển thị thông báo hoàn tất
echo "Command executed with mdl.media from $CURRENT_DIR, courseid=$COURSEID, and topicid=$TOPICID"

47
Moodle/PHP/enbatch_run.sh Normal file
View File

@@ -0,0 +1,47 @@
#!/usr/bin/env bash
# Kiểm tra có đối số không
if [ -z "$1" ]; then
echo "Usage: $(basename "$0") <courseid>"
exit 1
fi
courseid="$1"
# Lưu thư mục hiện tại (nơi chạy script, không phải nơi đặt script)
BASEDIR="$(pwd)"
for dir in */ ; do
# Bỏ dấu '/' cuối
dirname="${dir%/}"
# Cắt phần số trước dấu '.' và trim khoảng trắng
index=$(echo "$dirname" | cut -d'.' -f1 | tr -d '[:space:]')
# Bỏ số 0 đầu nếu có
index_nozero=$(echo "$index" | sed 's/^0*//')
# Nếu chuỗi rỗng (trường hợp '0')
if [ -z "$index_nozero" ]; then
index_nozero=0
fi
# Kiểm tra là số hợp lệ
if [[ "$index_nozero" =~ ^[0-9]+$ ]]; then
# Tính i-1
i_minus_1=$((index_nozero - 1))
echo "===> Đang xử lý thư mục: $dirname (index: $index_nozero, i-1: $i_minus_1)"
# Chuyển vào thư mục
cd "$dirname" || { echo "Không thể vào thư mục $dirname"; exit 1; }
# Gọi online.sh với courseid từ dòng lệnh và i-1
english.sh "$courseid" "$i_minus_1"
# Quay lại thư mục gốc
cd "$BASEDIR"
else
echo "Bỏ qua $dirname (không bắt đầu bằng số)"
fi
done

View File

@@ -0,0 +1,7 @@
#!/bin/bash
# Lấy thư mục hiện tại nơi người dùng chạy lệnh
CURRENT_DIR="$(pwd)"
# Gọi PHP script, truyền tham số đầu vào và thư mục hiện tại
php /usr/bin/encreate_moodle_course.php "$@" "$CURRENT_DIR"

View File

@@ -0,0 +1,113 @@
#!/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 <categoryid> <format>\n";
echo " Ví dụ : create_course 3 topics\n";
exit(1);
}
$categoryid = intval($argv[1]);
$format = $argv[2];
$cwd = $argv[3]; // 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";
}

View File

@@ -0,0 +1,114 @@
#!/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";
}

View File

@@ -0,0 +1,7 @@
#!/bin/bash
# Lấy thư mục hiện tại nơi người dùng chạy lệnh
CURRENT_DIR="$(pwd)"
# Gọi PHP script, truyền tham số đầu vào và thư mục hiện tại
php /usr/bin/encreatecourse.php "$@" "$CURRENT_DIR"

20
Moodle/PHP/english.sh Normal file
View File

@@ -0,0 +1,20 @@
#!/bin/bash
# Kiểm tra số lượng tham số đầu vào
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <courseid> <topicid>"
exit 1
fi
# Gán tham số đầu vào vào biến
COURSEID=$1
TOPICID=$2
# Lấy đường dẫn thư mục hiện tại
CURRENT_DIR=$(pwd)
# Chạy lệnh PHP với các tham số, sử dụng mdl.media từ thư mục hiện tại
/usr/local/lsws/lsphp82/bin/php /home/english.huph.edu.vn/public_html/english/page.php "$CURRENT_DIR/mdl.media" "$COURSEID" "$TOPICID"
# Hiển thị thông báo hoàn tất
echo "Command executed with mdl.media from $CURRENT_DIR, courseid=$COURSEID, and topicid=$TOPICID"

26
Moodle/PHP/genmedia.sh Normal file
View File

@@ -0,0 +1,26 @@
#!/bin/bash
# Thư mục gốc của khóa học
COURSE_FOLDER="$(pwd)"
# Tìm tất cả các thư mục con
mapfile -t DIRS < <(find "$COURSE_FOLDER" -type d)
for dir in "${DIRS[@]}"; do
relative_path="${dir#*/vod/}"
OUTPUT_JSON="$dir/mdl.media"
# Xóa tệp mdl.media cũ nếu tồn tại
rm -f "$OUTPUT_JSON"
# Tìm và sắp xếp các file .mp3 và .mp4 theo thứ tự tự nhiên
mapfile -d '' -t FILES < <(find "$dir" -maxdepth 1 -type f \( -name '*.mp3' -o -name '*.mp4' \) -print0 | sort -z -V)
for file in "${FILES[@]}"; do
filename=$(basename "$file")
vid="$relative_path/$filename"
echo "[stream=$vid]" >> "$OUTPUT_JSON"
done
echo "File mdl.media created successfully in $dir."
done

View File

@@ -0,0 +1,32 @@
#!/bin/bash
# Thư mục gốc của khóa học
COURSE_FOLDER="$(pwd)"
# Hàm xử lý tạo mdl.media cho mỗi thư mục
process_dir() {
local dir="$1"
local relative_path="${dir#*/vod/}"
# Đường dẫn đến tệp mdl.media trong thư mục
OUTPUT_JSON="$dir/mdl.media"
# Xóa tệp mdl.media cũ nếu tồn tại
rm -f "$OUTPUT_JSON"
# Tìm và sắp xếp các file .mp4 theo thứ tự tự nhiên (hỗ trợ cả số có hoặc không có leading zeros)
find "$dir" -maxdepth 1 -type f -name '*.mp4' -print0 | sort -z -V | while IFS= read -r -d '' file; do
if [ -f "$file" ]; then
filename=$(basename "$file")
vid="$relative_path/$filename"
echo "[stream=$vid]" >> "$OUTPUT_JSON"
fi
done
echo "File mdl.media created successfully in $dir."
}
# Duyệt qua thư mục gốc và tất cả các thư mục con, gọi hàm xử lý
find "$COURSE_FOLDER" -type d | while IFS= read -r dir; do
process_dir "$dir"
done

32
Moodle/PHP/genmp3.sh Normal file
View File

@@ -0,0 +1,32 @@
#!/bin/bash
# Thư mục gốc của khóa học
COURSE_FOLDER="$(pwd)"
# Hàm xử lý tạo mdl.media cho mỗi thư mục
process_dir() {
local dir="$1"
local relative_path="${dir#*/vod/}"
# Đường dẫn đến tệp mdl.media trong thư mục
OUTPUT_JSON="$dir/mdl.media"
# Xóa tệp mdl.media cũ nếu tồn tại
rm -f "$OUTPUT_JSON"
# Tìm và sắp xếp các file .mp3 theo thứ tự tự nhiên (hỗ trợ cả số có hoặc không có leading zeros)
find "$dir" -maxdepth 1 -type f -name '*.mp3' -print0 | sort -z -V | while IFS= read -r -d '' file; do
if [ -f "$file" ]; then
filename=$(basename "$file")
vid="$relative_path/$filename"
echo "[stream=$vid]" >> "$OUTPUT_JSON"
fi
done
echo "File mdl.media created successfully in $dir."
}
# Duyệt qua thư mục gốc và tất cả các thư mục con, gọi hàm xử lý
find "$COURSE_FOLDER" -type d | while IFS= read -r dir; do
process_dir "$dir"
done

2
Moodle/PHP/gets.sh Normal file
View File

@@ -0,0 +1,2 @@
wget --no-check-certificate --content-disposition --max-redirect=10 --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36" --referer="https://en.git.ir/" -i list.txt

135
Moodle/PHP/gettext.sh Normal file
View File

@@ -0,0 +1,135 @@
#! /bin/sh
#
# Copyright (C) 2003, 2005-2007, 2011, 2018-2020 Free Software Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Find a way to echo strings without interpreting backslash.
if test "X`(echo '\t') 2>/dev/null`" = 'X\t'; then
echo='echo'
else
if test "X`(printf '%s\n' '\t') 2>/dev/null`" = 'X\t'; then
echo='printf %s\n'
else
echo_func () {
cat <<EOT
$*
EOT
}
echo='echo_func'
fi
fi
# This script is primarily a shell function library. In order for
# ". gettext.sh" to find it, we install it in $PREFIX/bin (that is usually
# contained in $PATH), rather than in some other location such as
# $PREFIX/share/sh-scripts or $PREFIX/share/gettext. In order to not violate
# the Filesystem Hierarchy Standard when doing so, this script is executable.
# Therefore it needs to support the standard --help and --version.
if test -z "${ZSH_VERSION+set}"; then
# zsh is not POSIX compliant: By default, while ". gettext.sh" is executed,
# it sets $0 to "gettext.sh", defeating the purpose of this test. But
# fortunately we know that when running under zsh, this script is always
# being sourced, not executed, because hardly anyone is crazy enough to
# install zsh as /bin/sh.
case "$0" in
gettext.sh | */gettext.sh | *\\gettext.sh)
progname=$0
package=gettext-runtime
version=0.21
# func_usage
# outputs to stdout the --help usage message.
func_usage ()
{
echo "GNU gettext shell script function library version $version"
echo "Usage: . gettext.sh"
}
# func_version
# outputs to stdout the --version message.
func_version ()
{
echo "$progname (GNU $package) $version"
echo "Copyright (C) 2003-2020 Free Software Foundation, Inc.
License GPLv2+: GNU GPL version 2 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law."
echo "Written by" "Bruno Haible"
}
if test $# = 1; then
case "$1" in
--help | --hel | --he | --h )
func_usage; exit 0 ;;
--version | --versio | --versi | --vers | --ver | --ve | --v )
func_version; exit 0 ;;
esac
fi
func_usage 1>&2
exit 1
;;
esac
fi
# eval_gettext MSGID
# looks up the translation of MSGID and substitutes shell variables in the
# result.
eval_gettext () {
gettext "$1" | (export PATH `envsubst --variables "$1"`; envsubst "$1")
}
# eval_ngettext MSGID MSGID-PLURAL COUNT
# looks up the translation of MSGID / MSGID-PLURAL for COUNT and substitutes
# shell variables in the result.
eval_ngettext () {
ngettext "$1" "$2" "$3" | (export PATH `envsubst --variables "$1 $2"`; envsubst "$1 $2")
}
# eval_pgettext MSGCTXT MSGID
# looks up the translation of MSGID in the context MSGCTXT and substitutes
# shell variables in the result.
eval_pgettext () {
gettext --context="$1" "$2" | (export PATH `envsubst --variables "$2"`; envsubst "$2")
}
# eval_npgettext MSGCTXT MSGID MSGID-PLURAL COUNT
# looks up the translation of MSGID / MSGID-PLURAL for COUNT in the context
# MSGCTXT and substitutes shell variables in the result.
eval_npgettext () {
ngettext --context="$1" "$2" "$3" "$4" | (export PATH `envsubst --variables "$2 $3"`; envsubst "$2 $3")
}
# Note: This use of envsubst is much safer than using the shell built-in 'eval'
# would be.
# 1) The security problem with Chinese translations that happen to use a
# character such as \xe0\x60 is avoided.
# 2) The security problem with malevolent translators who put in command lists
# like "$(...)" or "`...`" is avoided.
# 3) The translations can only refer to shell variables that are already
# mentioned in MSGID or MSGID-PLURAL.
#
# Note: "export PATH" above is a dummy; this is for the case when
# `envsubst --variables ...` returns nothing.
#
# Note: In eval_ngettext above, "$1 $2" means a string whose variables set is
# the union of the variables set of "$1" and "$2".
#
# Note: The minimal use of backquote above ensures that trailing newlines are
# not dropped, not from the gettext invocation and not from the value of any
# shell variable.
#
# Note: Field splitting on the `envsubst --variables ...` result is desired,
# since envsubst outputs the variables, separated by newlines. Pathname
# wildcard expansion or tilde expansion has no effect here, since the words
# output by "envsubst --variables ..." consist solely of alphanumeric
# characters and underscore.

20
Moodle/PHP/hsphnline.sh Normal file
View File

@@ -0,0 +1,20 @@
#!/bin/bash
# Kiểm tra số lượng tham số đầu vào
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <courseid> <topicid>"
exit 1
fi
# Gán tham số đầu vào vào biến
COURSEID=$1
TOPICID=$2
# Lấy đường dẫn thư mục hiện tại
CURRENT_DIR=$(pwd)
# Chạy lệnh PHP với các tham số, sử dụng mdl.media từ thư mục hiện tại
php /home/online.linkvn.vn/public_html/online/objpage.php "$CURRENT_DIR/mdl.media" "$COURSEID" "$TOPICID"
# Hiển thị thông báo hoàn tất
echo "Command executed with mdl.media from $CURRENT_DIR, courseid=$COURSEID, and topicid=$TOPICID"

20
Moodle/PHP/hsphonline.sh Normal file
View File

@@ -0,0 +1,20 @@
#!/bin/bash
# Kiểm tra số lượng tham số đầu vào
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <courseid> <topicid>"
exit 1
fi
# Gán tham số đầu vào vào biến
COURSEID=$1
TOPICID=$2
# Lấy đường dẫn thư mục hiện tại
CURRENT_DIR=$(pwd)
# Chạy lệnh PHP với các tham số, sử dụng mdl.media từ thư mục hiện tại
php /home/online.linkvn.vn/public_html/online/page.php "$CURRENT_DIR/mdl.media" "$COURSEID" "$TOPICID"
# Hiển thị thông báo hoàn tất
echo "Command executed with mdl.media from $CURRENT_DIR, courseid=$COURSEID, and topicid=$TOPICID"

View File

@@ -0,0 +1,151 @@
<?php
// Thêm các thư viện cần thiết của Moodle
define('CLI_SCRIPT', true);
require_once(__DIR__ . '/config.php');
global $CFG, $DB;
require_once($CFG->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.");

20
Moodle/PHP/objonline.sh Normal file
View File

@@ -0,0 +1,20 @@
#!/bin/bash
# Kiểm tra số lượng tham số đầu vào
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <courseid> <topicid>"
exit 1
fi
# Gán tham số đầu vào vào biến
COURSEID=$1
TOPICID=$2
# Lấy đường dẫn thư mục hiện tại
CURRENT_DIR=$(pwd)
# Chạy lệnh PHP với các tham số, sử dụng mdl.media từ thư mục hiện tại
php /home/online.linkvn.vn/public_html/online/objpage.php "$CURRENT_DIR/mdl.media" "$COURSEID" "$TOPICID"
# Hiển thị thông báo hoàn tất
echo "Command executed with mdl.media from $CURRENT_DIR, courseid=$COURSEID, and topicid=$TOPICID"

21
Moodle/PHP/online.sh Normal file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
# Kiểm tra số lượng tham số đầu vào
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <courseid> <topicid>"
exit 1
fi
# Gán tham số đầu vào vào biến
COURSEID=$1
TOPICID=$2
# Lấy đường dẫn thư mục hiện tại
CURRENT_DIR=$(pwd)
# Chạy lệnh PHP với các tham số, sử dụng mdl.media từ thư mục hiện tại
/usr/local/lsws/lsphp82/bin/php /home/online.huph.edu.vn/public_html/online/page.php "$CURRENT_DIR/mdl.media" "$COURSEID" "$TOPICID"
# Hiển thị thông báo hoàn tất
echo "Command executed with mdl.media from $CURRENT_DIR, courseid=$COURSEID, and topicid=$TOPICID"

22
Moodle/PHP/removeblank.sh Normal file
View File

@@ -0,0 +1,22 @@
#!/bin/bash
BASE_DIR="."
find "$BASE_DIR" -depth | while IFS= read -r path; do
current_name=$(basename "$path")
parent_dir=$(dirname "$path")
# Loại bỏ ký tự đặc biệt: # ! ' [ ] @ bằng tr + tr -d
new_name=$(echo "$current_name" | tr -d "#!'\[\]@")
# Loại bỏ khoảng trắng thừa (chỉ giữ 1 dấu cách, loại bỏ cuối)
new_name=$(echo "$new_name" | tr -s ' ' | sed 's/ *$//')
# Nếu tên thay đổi thì đổi tên
if [[ "$current_name" != "$new_name" ]]; then
mv -- "$path" "$parent_dir/$new_name"
echo "Đã đổi tên: '$path' → '$parent_dir/$new_name'"
fi
done
echo "✅ Hoàn thành."

View File

@@ -0,0 +1,14 @@
#!/bin/bash
# Lấy thư mục hiện hành nơi người dùng đang gọi lệnh
TARGET_DIR="$(pwd)"
# Đổi tên các thư mục con cấp 1 theo mẫu: 1 - ABC → 1. ABC
find "$TARGET_DIR" -mindepth 1 -maxdepth 1 -type d -regextype posix-extended -regex '.*/[0-9]+ - .+' | while read dir; do
base=$(basename "$dir")
newname=$(echo "$base" | sed -E 's/^([0-9]+) - (.+)$/\1. \2/')
if [ "$base" != "$newname" ]; then
echo "🔁 Đổi tên: $base$newname"
mv "$TARGET_DIR/$base" "$TARGET_DIR/$newname"
fi
done

File diff suppressed because it is too large Load Diff