我获得了moodle上传到moodle课程的命令行脚本,编码如下。
为课程添加多个资源(到课程的第一部分)。资源将命名为“文件A”,“文件B”等。对于每个资源,我们将上传多个文件,并根据文件名决定哪个文件位于哪里,例如。以“a”开头的文件将转到名为“Files A”的资源。
我一步一步做了什么:
获取文件资源上下文ID
获取文件存储(单件)
创建文件信息数据
上传文件
现在,在获取资源上下文ID时,我们必须确保选择正确的资源名称(例如“文件A”)并且该资源确实存在。因此,我们需要一个能够为我们做到这一点的功能,并创建所有需要的资源。
然后我们需要确保新创建的资源可见,可用,创建上下文,资源放在正确的课程部分等等。我们还可以检查模块是否已启用以及许多其他内容,但这对于此示例来说已经足够了。
在整个操作之后,我们将清理课程缓存,以便用户可以看到新的课程元素。
<?php
define('CLI_SCRIPT', true);
// this is config.php within Moodle installation
require_once('config.php');
$courseid = $argv[1];
$filename = $argv[2];
$course_name = $DB->get_record('course', array('id' => $courseid), 'id, shortname')
or die("Course not found");
$course_destination['path'] = "/files/"; // any path that starts and ends with "/"
$course_destination['filename'] = $filename;
$course_destination['resource_name'] = 'Files ' . get_first_letter(clean_string($filename));
if (file_exists($filename)) {
upload_for_course($uploadcourseid, $path, $course_destination);
} else {
echo "Not uploading, file does not exist\n";
}
function upload_for_course($courseid, $source_filename, $destination) {
global $DB, $CFG;
if (file_exists($source_filename) && is_readable($source_filename)) {
require_once($CFG->dirroot.'/lib/filestorage/file_storage.php');
$sql = "SELECT m.id moduleid, cm.id cmid, cm.course courseid, cm.module moduleid
FROM {course_modules} cm, {modules} m
WHERE m.id = cm.module AND cm.course=$courseid AND m.name='resource'";
$resource = get_resource($courseid, $destination['resource_name']);
$fs = get_file_storage();
// Prepare file record object
$fileinfo = array(
'contextid' => $resource->id, // ID of context
'component' => 'mod_resource', // usually = table name
'filearea' => 'content', // usually = table name
'itemid' => 0, // usually = ID of row in table
'filepath' => $destination['path'], // any path beginning and ending in /
'filename' => $destination['filename']); // any filename
$fs->create_file_from_pathname($fileinfo, $source_filename);
} else {
die("Error: Not exist or not readable: " . $source_filename . "\n");
}
}
function clean_string($str) {
setlocale(LC_ALL, 'en_US');
$chars = "\\/`!@#$%^&*()_-=+|}{]['\";:?><,. \t\n";
$str = strtolower( trim( strip_tags($str), $chars) );
$str = iconv('UTF-8', 'ASCII//IGNORE', $str);
return $str;
}
function get_first_letter($str) {
$str = clean_string($str);
// return first letter
return strtoupper(substr($str, 0, 1));
}
function get_resource($courseid, $resource_name) {
global $DB, $CFG;
$sql = "SELECT cm.id as cmid FROM {course_modules} cm, {resource} res
WHERE res.name = '" . $resource_name . "'
AND cm.course = " . $courseid . "
AND cm.instance = res.id";
if (! $coursemodule = $DB->get_record_sql($sql)) {
$letters = range('A', 'Z');
require_once($CFG->dirroot.'/course/lib.php');
echo "\tCreate new resouces\n";
foreach($letters as $key => $letter) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
// get module id
$module = $DB->get_record('modules', array('name' => 'resource'), '*', MUST_EXIST);
// get course section
$cw = get_course_section(0, $course->id);
$sectionid = $DB->get_record('course_sections', array('course' => $this->courseid, 'section' => $cw->id), '*', MUST_EXIST);
$resource_data = new stdClass();
$resource_data->course = $course->id;
$resource_data->name = 'Files ' . strtoupper($letter);
$resource_data->intro = '<p>'.'Files ' . strtoupper($letter).'</p>';
$resource_data->introformat = 1;
$resource_data->tobemigrated = 0;
$resource_data->legacyfiles = 0;
$resource_data->display = 0;
$resource_data->displayoptions = 'a:2:{s:12:"printheading";i:0;s:10:"printintro";i:1;}';
$resource_data->revision = 1;
$resource_data->completion = 1;
$resource_data->showavailability = 1;
$resource_data->timemodified = time();
$resource_id = $DB->insert_record('resource', $resource_data);
// add course module
$cm = new stdClass();
$cm->course = $courseid;
$cm->module = $module->id; // should be retrieved from mdl_modules
$cm->instance = $resource_id; // from mdl_resource
$cm->section = $sectionid->id; // from mdl_course_sections
$cm->visible = 1;
$cm->visibleold = 1;
$cm->showavailability = 1;
$cm->added = time();
$cmid = $DB->insert_record('course_modules', $cm);
// add module to course section so it'll be visible
if ($DB->record_exists('course_sections', array('course' => $courseid, 'section' => 1))) {
$sectionid = $DB->get_record('course_sections', array('course' => $courseid, 'section' => 1));
// if sequence is not empty, add another course_module id
if (!empty($sectionid->sequence)) {
$sequence = $sectionid->sequence . ',' . $cmid;
} else {
// if sequence is empty, add course_module id
$sequence = $cmid;
}
$course_section = new stdClass();
$course_section->id = $sectionid->id;
$course_section->course = $courseid;
$course_section->section = 1;
$course_section->sequence = $sequence;
$csid = $DB->update_record('course_sections', $course_section);
} else {
$sequence = $cmid;
$course_section = new stdClass();
$course_section->course = $courseid;
$course_section->section = 1;
$course_section->sequence = $sequence;
$csid = $DB->insert_record('course_sections', $course_section);
}
// force clear module cache
$modulecache = new stdClass();
$modulecache->id = $courseid;
$modulecache->sectioncache = 'NULL';
$DB->update_record('course', $modulecache);
} // foreach
// get context again, this time with all resources present
$context = get_resource($courseid, $resource_name);
return $context;
} else {
$context = get_context_instance(CONTEXT_MODULE, $coursemodule->cmid);
return $context;
}
} // get_resource
}
输入就像
/> php filename.php courseid "upload/file/path"
example:
/> php script.php 4 "E:\file\untitled-1.pdf"
但结果是错误
Error: Not exists or Not readable
请帮助解决此问题。 可以修复吗
提前致谢。
Vinoth
答案 0 :(得分:0)
我对您的代码进行了一些更改,因为我尝试过的moodle版本告诉我某些功能已被弃用。
我已经测试了下面的代码,可以使用文件或带资源的资源创建一个文件夹。
首先是资源我做了这个:
function get_resource($courseid, $resource_name) {
global $DB, $CFG;
$sql = "SELECT cm.id as cmid FROM {course_modules} cm, {resource} res
WHERE res.name = '" . $resource_name . "'
AND cm.course = " . $courseid . "
AND cm.instance = res.id";
if (! $coursemodule = $DB->get_record_sql($sql)) {
require_once($CFG->dirroot.'/course/lib.php');
echo "\tCreate new resouce\n";
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
// get module id
$module = $DB->get_record('modules', array('name' => 'resource'), '*', MUST_EXIST);
// get course section
course_create_sections_if_missing($course->id, 0);
$modinfo = get_fast_modinfo($course->id);
$cw = $modinfo->get_section_info(0);
echo "section id: " . $cw->id;
$sectionid = $DB->get_record('course_sections', array('course' => $course->id, 'name' => 'Recursos'), '*', MUST_EXIST);
$resource_data = new stdClass();
$resource_data->course = $course->id;
$resource_data->name = $resource_name;
$resource_data->intro = '<p>'.$resource_name.'</p>';
$resource_data->introformat = 1;
$resource_data->tobemigrated = 0;
$resource_data->legacyfiles = 0;
$resource_data->display = 0;
$resource_data->displayoptions = 'a:2:{s:12:"printheading";i:0;s:10:"printintro";i:1;}';
$resource_data->revision = 1;
$resource_data->completion = 1;
$resource_data->showavailability = 1;
$resource_data->timemodified = time();
$resource_id = $DB->insert_record('resource', $resource_data);
// add course module
$cm = new stdClass();
$cm->course = $courseid;
$cm->module = $module->id; // should be retrieved from mdl_modules
$cm->instance = $resource_id; // from mdl_resource
$cm->section = $sectionid->id; // from mdl_course_sections
$cm->visible = 1;
$cm->visibleold = 1;
$cm->showavailability = 1;
$cm->added = time();
$cmid = $DB->insert_record('course_modules', $cm);
// add module to course section so it'll be visible
if ($DB->record_exists('course_sections', array('course' => $courseid, 'section' => 1))) {
$sectionid = $DB->get_record('course_sections', array('course' => $courseid, 'section' => 1));
// if sequence is not empty, add another course_module id
if (!empty($sectionid->sequence)) {
$sequence = $sectionid->sequence . ',' . $cmid;
} else {
// if sequence is empty, add course_module id
$sequence = $cmid;
}
$course_section = new stdClass();
$course_section->id = $sectionid->id;
$course_section->course = $courseid;
$course_section->section = 1;
$course_section->sequence = $sequence;
$csid = $DB->update_record('course_sections', $course_section);
} else {
$sequence = $cmid;
$course_section = new stdClass();
$course_section->course = $courseid;
$course_section->section = 1;
$course_section->sequence = $sequence;
$csid = $DB->insert_record('course_sections', $course_section);
}
rebuild_course_cache($courseid, true);
// get context again, this time with all resources present
$context = get_resource($courseid, $resource_name);
return $context;
} else {
$context = context_module::instance($coursemodule->cmid);
return $context;
}
} // get_resource
对于文件夹我做了这个:
function get_folder($courseid, $resource_name) {
global $DB, $CFG;
$sql = "SELECT cm.id as cmid FROM {course_modules} cm, {folder} res
WHERE res.name = '" . $resource_name . "'
AND cm.course = " . $courseid . "
AND cm.instance = res.id";
if (! $coursemodule = $DB->get_record_sql($sql)) {
require_once($CFG->dirroot.'/course/lib.php');
echo "\tCreate new folder\n";
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
// get module id
$module = $DB->get_record('modules', array('name' => 'folder'), '*', MUST_EXIST);
// get course section
course_create_sections_if_missing($course->id, 0);
$modinfo = get_fast_modinfo($course->id);
$cw = $modinfo->get_section_info(0);
echo "section id: " . $cw->id;
$sectionid = $DB->get_record('course_sections', array('course' => $course->id, 'name' => 'Recursos'), '*', MUST_EXIST);
$folder_data = new stdClass();
$folder_data->course = $course->id;
$folder_data->name = $resource_name;
$folder_data->intro = '<p>'.$resource_name.'</p>';
$folder_data->introformat = 1;
$folder_data->revision = 1;
$folder_data->timemodified = time();
$folder_data->display = 0;
$folder_data->showexpanded = 1;
$folder_data->showdownloadfolder = 1;
$folder_id = $DB->insert_record('folder', $folder_data);
echo "folder id: " . $folder_id;
// add course module
$cm = new stdClass();
$cm->course = $courseid;
$cm->module = $module->id; // should be retrieved from mdl_modules
$cm->instance = $folder_id; // from mdl_resource
$cm->section = $sectionid->id; // from mdl_course_sections
$cm->visible = 1;
$cm->visibleold = 1;
$cm->showavailability = 1;
$cm->added = time();
$cmid = $DB->insert_record('course_modules', $cm);
// add module to course section so it'll be visible
if ($DB->record_exists('course_sections', array('course' => $courseid, 'section' => 1))) {
$sectionid = $DB->get_record('course_sections', array('course' => $courseid, 'section' => 1));
// if sequence is not empty, add another course_module id
if (!empty($sectionid->sequence)) {
$sequence = $sectionid->sequence . ',' . $cmid;
} else {
// if sequence is empty, add course_module id
$sequence = $cmid;
}
$course_section = new stdClass();
$course_section->id = $sectionid->id;
$course_section->course = $courseid;
$course_section->section = 1;
$course_section->sequence = $sequence;
$csid = $DB->update_record('course_sections', $course_section);
} else {
$sequence = $cmid;
$course_section = new stdClass();
$course_section->course = $courseid;
$course_section->section = 1;
$course_section->sequence = $sequence;
$csid = $DB->insert_record('course_sections', $course_section);
}
rebuild_course_cache($courseid, true);
// get context again, this time with all resources present
$context = get_folder($courseid, $resource_name);
return $context;
} else {
$context = context_module::instance($coursemodule->cmid);
return $context;
}
} // get_folder
我如何称呼这个功能:
$resource = get_folder($course, 'Folder Script'); // for folders
//$resource = get_resource($course, 'Resource Script'); // for resources
$fs = get_file_storage();
$filename = 'Text1.txt';
// Prepare file record object
$fileinfo = array(
'contextid' => $resource->id, // ID of context
'component' => 'mod_folder', // usually = table name
'filearea' => 'content', // usually = table name
'itemid' => 0, // usually = ID of row in table
'filepath' => '/', // any path beginning and ending in /
'filename' => $filename); // any filename
/*$fileinfo = array(
'contextid' => $resource->id, // ID of context
'component' => 'mod_resource', // usually = table name
'filearea' => 'content', // usually = table name
'itemid' => 0, // usually = ID of row in table
'filepath' => '/', // any path beginning and ending in /
'filename' => $filename); // any filename
*/
$path_upload = '/path/Text1.txt';
$existingfile = $fs->file_exists($resource->id, 'mod_resource', 'content', 0, '/', $filename);
if ($existingfile) {
//the file exists
} else {
$fs->create_file_from_pathname($fileinfo, $path_upload);
//file uploaded
}
请注意,这是一个快速测试。
我希望这能为你服务。