MDL-23781 migrated old non-standard moddata lesson files and fixed media files migration

This commit is contained in:
Petr Skoda
2010-08-12 16:05:41 +00:00
parent 0d6521541f
commit 8d1a39637a
6 changed files with 164 additions and 64 deletions
+1 -1
View File
@@ -30,7 +30,7 @@
<FIELD NAME="maxtime" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timed" NEXT="retake"/>
<FIELD NAME="retake" TYPE="int" LENGTH="3" NOTNULL="true" UNSIGNED="true" DEFAULT="1" SEQUENCE="false" PREVIOUS="maxtime" NEXT="activitylink"/>
<FIELD NAME="activitylink" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="retake" NEXT="mediafile"/>
<FIELD NAME="mediafile" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="activitylink" NEXT="mediaheight"/>
<FIELD NAME="mediafile" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="activitylink" NEXT="mediaheight" COMMENT="Local file path or full external URL"/>
<FIELD NAME="mediaheight" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="100" SEQUENCE="false" PREVIOUS="mediafile" NEXT="mediawidth"/>
<FIELD NAME="mediawidth" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="650" SEQUENCE="false" PREVIOUS="mediaheight" NEXT="mediaclose"/>
<FIELD NAME="mediaclose" TYPE="int" LENGTH="3" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="mediawidth" NEXT="slideshow"/>
+65 -56
View File
@@ -89,62 +89,6 @@ function xmldb_lesson_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2009111600, 'lesson');
}
if ($oldversion < 2009120400) {
$sqlfrom = "FROM {lesson} l
JOIN {modules} m ON m.name = 'lesson'
JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = l.id)
WHERE l.mediafile <> :empty";
$params = array('empty'=>$DB->sql_empty());
$count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom", $params);
if ($count > 0) {
$rs = $DB->get_recordset_sql("SELECT l.id, l.mediafile, l.course, cm.id AS cmid $sqlfrom ORDER BY l.course, l.id", $params);
$pbar = new progress_bar('migratelessonfiles', 500, true);
$fs = get_file_storage();
$i = 0;
foreach ($rs as $lesson) {
$i++;
upgrade_set_timeout(60); // set up timeout, may also abort execution
$pbar->update($i, $count, "Migrating lesson mediafiles - $i/$count.");
$filepath = $CFG->dataroot.'/'.$lesson->course.'/'.$CFG->moddata.'/lesson/'.$lesson->mediafile;
if (!is_readable($filepath)) {
//file missing??
echo $OUTPUT->notification("File not readable, skipping: ".$filepath);
$DB->set_field('lesson', 'mediafile', '', array('id'=>$lesson->id));
continue;
}
$filename = clean_param($lesson->mediafile, PARAM_FILE);
if ($filename === '') {
echo $OUTPUT->notification("Unsupported lesson filename, skipping: ".$filepath);
$DB->set_field('lesson', 'mediafile', '', array('id'=>$lesson->id));
continue;
}
$context = get_context_instance(CONTEXT_MODULE, $lesson->cmid);
if (!$fs->file_exists($context->id, 'mod_lesson', 'mediafile', 0, '/', $filename)) {
$file_record = array('contextid'=>$context->id, 'component'=>'mod_lesson', 'filearea'=>'mediafile', 'itemid'=>0, 'filepath'=>'/', 'filename'=>$filename);
if ($fs->create_file_from_pathname($file_record, $filepath)) {
if ($DB->set_field('lesson', 'mediafile', $filename, array('id'=>$lesson->id))) {
unlink($filepath);
}
}
}
// remove dir if empty
@rmdir("$CFG->dataroot/$post->course/$CFG->moddata/lesson");
}
$rs->close();
}
upgrade_mod_savepoint(true, 2009120400, 'lesson');
}
if ($oldversion < 2009120800) {
/**
* Drop the lesson_default table, as of Moodle 2.0 it is no longer used
@@ -229,6 +173,71 @@ function xmldb_lesson_upgrade($oldversion) {
}
if ($oldversion < 2010081200) {
require_once("$CFG->dirroot/mod/lesson/db/upgradelib.php");
$sqlfrom = "FROM {lesson} l
JOIN {modules} m ON m.name = 'lesson'
JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = l.id)";
$count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
if ($count > 0) {
$rs = $DB->get_recordset_sql("SELECT l.id, l.mediafile, l.course, cm.id AS cmid $sqlfrom ORDER BY l.course, l.id");
$pbar = new progress_bar('migratelessonfiles', 500, true);
$fs = get_file_storage();
$i = 0;
foreach ($rs as $lesson) {
$i++;
upgrade_set_timeout(120); // set up timeout, may also abort execution
$pbar->update($i, $count, "Migrating lesson files - $i/$count.");
// fix images incorrectly placed in moddata subfolders,
// this was a really bad decision done by the ppt import developer
if (file_exists("$CFG->dataroot/$lesson->course/moddata/lesson/")) {
lesson_20_migrate_moddata_mixture($lesson->course, '/moddata/lesson/');
@rmdir("$CFG->dataroot/$lesson->course/moddata/lesson/"); // remove dir if empty
@rmdir("$CFG->dataroot/$lesson->course/moddata/"); // remove dir if empty
}
// migrate media file only if local course file selected - this is not nice at all,
// it should better be a real block, not a lesson specific hack
if (strpos($lesson->mediafile, '://') !== false) {
// some external URL
} else if ($lesson->mediafile) {
$context = get_context_instance(CONTEXT_MODULE, $lesson->cmid);
$coursecontext = get_context_instance(CONTEXT_COURSE, $lesson->course);
$filepathname = clean_param('/'.$lesson->mediafile, PARAM_PATH);
$fullpath = "/$context->id/mod_lesson/mediafile/0$filepathname";
if ($fs->get_file_by_hash(sha1($fullpath)) and !$file->is_directory()) {
// already converted, just update filename
$DB->set_field('lesson', 'mediafile', $filepathname, array('id'=>$lesson->id));
} else {
// let's copy file from current course legacy files if possible
$fullpath = "/$coursecontext->id/course/legacy/0$filepathname";
if ($file = $fs->get_file_by_hash(sha1($fullpath)) and !$file->is_directory()) {
$file_record = array('contextid'=>$context->id, 'component'=>'mod_lesson', 'filearea'=>'mediafile', 'itemid'=>0, 'filepath'=>$file->get_filepath(), 'filename'=>$file->get_filename(), 'sortorder'=>1);
$fs->create_file_from_storedfile($file_record, $file);
$DB->set_field('lesson', 'mediafile', $filepathname, array('id'=>$lesson->id));
} else {
// bad luck, no such file exists
$DB->set_field('lesson', 'mediafile', '', array('id'=>$lesson->id));
}
}
}
}
$rs->close();
}
upgrade_mod_savepoint(true, 2010081200, 'lesson');
}
return true;
}
+86
View File
@@ -0,0 +1,86 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Resource module upgrade related helper functions
*
* @package mod
* @subpackage lesson
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Migrate lesson images incorrectly placed into moddata folder,
* the images were stored in random directories ignoring all coding rules.
*
* @return void
*/
function lesson_20_migrate_moddata_mixture($courseid, $path) {
global $CFG, $DB;
$fullpathname = "$CFG->dataroot/$courseid".$path;
if (!file_exists($fullpathname)) {
return;
}
$context = get_context_instance(CONTEXT_COURSE, $courseid);
$items = new DirectoryIterator($fullpathname);
$fs = get_file_storage();
foreach ($items as $item) {
if ($item->isDot()) {
// skip
continue;
}
if ($item->isFile()) {
if (!$item->isReadable()) {
echo $OUTPUT->notification(" File not readable, skipping: ".$courseid.$pathname.$item->getFilename());
continue;
}
$filepath = clean_param($path, PARAM_PATH);
$filename = clean_param($item->getFilename(), PARAM_FILE);
if ($filename === '') {
//unsupported chars, sorry
continue;
}
if (!$fs->file_exists($context->id, 'course', 'legacy', '0', $filepath, $filename)) {
$file_record = array('contextid'=>$context->id, 'component'=>'course', 'filearea'=>'legacy', 'itemid'=>0, 'filepath'=>$filepath, 'filename'=>$filename,
'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
$fs->create_file_from_pathname($file_record, $fullpathname.$item->getFilename());
@unlink($fullpathname.$item->getFilename());
}
} else {
//migrate recursively all subdirectories
lesson_20_migrate_moddata_mixture($courseid, $path.$item->getFilename().'/');
}
}
unset($item); //release file handles
unset($items); //release file handles
// delete dir if empty
@rmdir($fullpathname);
}
+3 -3
View File
@@ -20,7 +20,7 @@
*
* @package mod
* @subpackage lesson
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
**/
@@ -55,7 +55,7 @@ function lesson_add_instance($data, $mform) {
if ($filename = $mform->get_new_filename('mediafilepicker')) {
if ($file = $mform->save_stored_file('mediafilepicker', $context->id, 'mod_lesson', 'mediafile', 0, '/', $filename)) {
$DB->set_field('lesson', 'mediafile', $file->get_filename(), array('id'=>$lesson->id));
$DB->set_field('lesson', 'mediafile', '/'.$file->get_filename(), array('id'=>$lesson->id));
}
}
@@ -89,7 +89,7 @@ function lesson_update_instance($data, $mform) {
$context = get_context_instance(CONTEXT_MODULE, $cmid);
if ($filename = $mform->get_new_filename('mediafilepicker')) {
if ($file = $mform->save_stored_file('mediafilepicker', $context->id, 'mod_lesson', 'mediafile', 0, '/', $filename, true)) {
$DB->set_field('lesson', 'mediafile', $file->get_filename(), array('id'=>$data->id));
$DB->set_field('lesson', 'mediafile', '/'.$file->get_filename(), array('id'=>$data->id));
} else {
$DB->set_field('lesson', 'mediafile', '', array('id'=>$data->id));
}
+8 -3
View File
@@ -442,7 +442,7 @@ function lesson_add_pretend_blocks($page, $cm, $lesson, $timer = null) {
**/
function lesson_mediafile_block_contents($cmid, $lesson) {
global $OUTPUT;
if (empty($lesson->mediafile) && empty($lesson->mediafileid)) {
if (empty($lesson->mediafile)) {
return null;
}
@@ -590,10 +590,15 @@ function lesson_get_media_html($lesson, $context) {
require_once("$CFG->libdir/resourcelib.php");
// get the media file link
$url = moodle_url::make_pluginfile_url($context->id, 'mod_lesson', 'mediafile', $lesson->timemodified, '/', $lesson->mediafile);
if (strpos($lesson->mediafile, '://') !== false) {
$url = new moodle_url($lesson->mediafile);
} else {
// the timemodified is used to prevent caching problems, instead of '/' we should better read from files table and use sortorder
$url = moodle_url::make_pluginfile_url($context->id, 'mod_lesson', 'mediafile', $lesson->timemodified, '/', ltrim($lesson->mediafile, '/'));
}
$title = $lesson->mediafile;
$clicktoopen = html_writer::link(new moodle_url($url), get_string('download'));
$clicktoopen = html_writer::link($url, get_string('download'));
$mimetype = resourcelib_guess_url_mimetype($url);
+1 -1
View File
@@ -27,7 +27,7 @@
defined('MOODLE_INTERNAL') || die();
$module->version = 2010080300; // The current module version (Date: YYYYMMDDXX)
$module->version = 2010081200; // The current module version (Date: YYYYMMDDXX)
$module->requires = 2010080300; // Requires this Moodle version
$module->cron = 0; // Period for cron to check this module (secs)