Skip to content

Commit

Permalink
Merge pull request #195 from jay-oswald/optimise_2022072200_script
Browse files Browse the repository at this point in the history
Optimising 2022072200 upgrade step
  • Loading branch information
gjb2048 authored Feb 13, 2024
2 parents 4cb0d77 + b6adb2d commit 6e61184
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 70 deletions.
89 changes: 89 additions & 0 deletions classes/task/upgrade_single_course.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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/>.

namespace format_grid\task;

/**
* Class upgrade_single_course
*
* @package format_grid
* @copyright 2023 Jay Oswad <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class upgrade_single_course extends \core\task\adhoc_task {

public function get_name() {
return "Course Format Grid Image Migration - 3.9 to 4.1";
}

public function execute() {
$params = $this->get_custom_data();
$courseid = (int)$params->course_id;
$this->upgrade_course($courseid);
}

private function upgrade_course($currentcourseid) {
global $DB;
$newimagecoursearray = $DB->get_records_sql(
'SELECT sectionid, courseid, image, displayedimagestate FROM {format_grid_image} WHERE courseid = ?',
[$currentcourseid]
);
$fs = get_file_storage();
$coursecontext = \context_course::instance($currentcourseid);
$files = $fs->get_area_files($coursecontext->id, 'course', 'section');
foreach ($files as $file) {
if (!$file->is_directory()) {
if ($file->get_filepath() == '/gridimage/') {
$file->delete();
} else {
$filename = $file->get_filename();
$filesectionid = $file->get_itemid();
// Ensure we know about this section.
if (array_key_exists($filesectionid, $newimagecoursearray)) {
$gridimage = $newimagecoursearray[$filesectionid];
// Ensure the correct file.
if (($gridimage) && ($gridimage->image == $filename)) {
$filerecord = new \stdClass();
$filerecord->contextid = $coursecontext->id;
$filerecord->component = 'format_grid';
$filerecord->filearea = 'sectionimage';
$filerecord->itemid = $filesectionid;
$filerecord->filepath = '/';
$filerecord->filename = $filename;
$thefile = false;
// Check to see if the file is already there.
$thefile = $fs->get_file(
$filerecord->contextid,
$filerecord->component,
$filerecord->filearea,
$filerecord->itemid,
$filerecord->filepath,
$filerecord->filename);
if ($thefile === false) {
$thefile = $fs->create_file_from_storedfile($filerecord, $file);
}
if ($thefile !== false) {
$DB->set_field('format_grid_image', 'contenthash',
$thefile->get_contenthash(), ['sectionid' => $filesectionid]);
// Don't delete the section file in case used in the summary.
}
}
}
}
}
}
}
}
85 changes: 15 additions & 70 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,76 +72,21 @@ function xmldb_format_grid_upgrade($oldversion = 0) {
// Upgrade from old images.
$oldimages = $DB->get_records('format_grid_icon');
if (!empty($oldimages)) {
$newimages = [];
foreach ($oldimages as $oldimage) {
if (!empty($oldimage->image)) {
try {
context_course::instance($oldimage->courseid);
} catch (\Exception $ex) {
// Course does not exist for this image, skip.
continue;
}

$newimagecontainer = new \stdClass();
$newimagecontainer->sectionid = $oldimage->sectionid;
$newimagecontainer->courseid = $oldimage->courseid;
$newimagecontainer->image = $oldimage->image;
$newimagecontainer->displayedimagestate = 0;
// Contenthash later!
$DB->insert_record('format_grid_image', $newimagecontainer, true);
if (!array_key_exists($newimagecontainer->courseid, $newimages)) {
$newimages[$newimagecontainer->courseid] = [];
}
$newimages[$newimagecontainer->courseid][$newimagecontainer->sectionid] = $newimagecontainer;
}
}

$fs = get_file_storage();
foreach ($newimages as $currentcourseid => $newimagecoursearray) {
$coursecontext = context_course::instance($currentcourseid);
$files = $fs->get_area_files($coursecontext->id, 'course', 'section');
foreach ($files as $file) {
if (!$file->is_directory()) {
if ($file->get_filepath() == '/gridimage/') {
$file->delete();
} else {
$filename = $file->get_filename();
$filesectionid = $file->get_itemid();
// Ensure we know about this section.
if (array_key_exists($filesectionid, $newimagecoursearray)) {
$gridimage = $newimagecoursearray[$filesectionid];
// Ensure the correct file.
if (($gridimage) && ($gridimage->image == $filename)) {
$filerecord = new stdClass();
$filerecord->contextid = $coursecontext->id;
$filerecord->component = 'format_grid';
$filerecord->filearea = 'sectionimage';
$filerecord->itemid = $filesectionid;
$filerecord->filepath = '/';
$filerecord->filename = $filename;
$thefile = false;
if ($somethingbroke) {
// Check to see if the file is already there.
$thefile = $fs->get_file(
$filerecord->contextid,
$filerecord->component,
$filerecord->filearea,
$filerecord->itemid,
$filerecord->filepath,
$filerecord->filename);
}
if ($thefile === false) {
$thefile = $fs->create_file_from_storedfile($filerecord, $file);
}
if ($thefile !== false) {
$DB->set_field('format_grid_image', 'contenthash',
$thefile->get_contenthash(), ['sectionid' => $filesectionid]);
// Don't delete the section file in case used in the summary.
}
}
}
}
}
if ($dbman->table_exists($oldtable)) {
// Move images.
$DB->execute('
INSERT INTO {format_grid_image} (sectionid, courseid, image, displayedimagestate)
SELECT sectionid, courseid, image, 0
FROM {format_grid_icon}
WHERE courseid IN ( SELECT id FROM {course} )
');
$courses = $DB->get_records_sql('SELECT DISTINCT courseid FROM {format_grid_image} ORDER BY courseid DESC');
foreach ($courses as $course) {
$task = new \format_grid\task\upgrade_single_course();
$task->set_custom_data([
'course_id' => $course->courseid,
]);
\core\task\manager::queue_adhoc_task($task, true);
}
}
}
Expand Down

0 comments on commit 6e61184

Please sign in to comment.