Update code
This commit is contained in:
67
Moodle/PHP/createfile.php
Normal file
67
Moodle/PHP/createfile.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
// Khai báo CLI
|
||||
define('CLI_SCRIPT', true);
|
||||
|
||||
// Nạp config Moodle
|
||||
require('/home/online.huph.edu.vn/public_html/online/config.php');
|
||||
require_once($CFG->dirroot.'/course/lib.php');
|
||||
require_once($CFG->dirroot.'/mod/resource/lib.php');
|
||||
|
||||
global $DB, $CFG;
|
||||
|
||||
// Ép user thành admin để tránh lỗi guest
|
||||
$admin = get_admin();
|
||||
\core\session\manager::set_user($admin);
|
||||
|
||||
// ==== Thông tin cần thay đổi ====
|
||||
$courseid = 1645; // ID khóa học
|
||||
$sectionnum = 1; // số thứ tự section/topic
|
||||
$filepath = '/nas1videos/English Learning/English with Lucy - Beautiful British English B2 Programme (Upper Intermediate) 2025-6/02. PDF/B2 Week 1 Weekly Langua Prompts.pdf';
|
||||
|
||||
$filename = basename($filepath); // Tên activity
|
||||
|
||||
// Lấy course
|
||||
$course = $DB->get_record('course', ['id'=>$courseid], '*', MUST_EXIST);
|
||||
|
||||
// Lấy section thực theo số thứ tự
|
||||
$section = $DB->get_record('course_sections', ['course'=>$courseid,'section'=>$sectionnum], '*', MUST_EXIST);
|
||||
$sectionid = $section->id;
|
||||
|
||||
// Module ID cho resource
|
||||
$moduleid = $DB->get_field('modules','id',['name'=>'resource'], MUST_EXIST);
|
||||
|
||||
// ===== Tạo bản ghi resource =====
|
||||
$resource = new stdClass();
|
||||
$resource->course = $courseid;
|
||||
$resource->name = $filename;
|
||||
$resource->intro = '';
|
||||
$resource->introformat = FORMAT_HTML;
|
||||
$resource->display = 0; // hiển thị link trực tiếp
|
||||
$resource->externalurl = $filepath; // link đến file repository
|
||||
$resource->section = $sectionid;
|
||||
$resource->timecreated = time();
|
||||
$resource->timemodified = time();
|
||||
$resource->visible = 1;
|
||||
|
||||
// Thêm vào bảng resource
|
||||
$resource->id = $DB->insert_record('resource', $resource);
|
||||
|
||||
// ===== Tạo course module =====
|
||||
$cm = new stdClass();
|
||||
$cm->course = $courseid;
|
||||
$cm->module = $moduleid;
|
||||
$cm->instance = $resource->id;
|
||||
$cm->section = $sectionid;
|
||||
$cm->visible = 1;
|
||||
$cm->added = time();
|
||||
$cm->score = 0;
|
||||
$cm->indent = 0;
|
||||
$cm->completion = 0;
|
||||
|
||||
// Thêm vào bảng course_modules
|
||||
$cm->id = $DB->insert_record('course_modules', $cm);
|
||||
|
||||
// ===== Cập nhật course_sections =====
|
||||
course_add_cm_to_section($course, $cm->id, $sectionnum);
|
||||
|
||||
echo "✅ File activity '{$filename}' đã được tạo trong course {$courseid}, section {$sectionnum}, cmid={$cm->id}\n";
|
Reference in New Issue
Block a user