forked from PukunuiAustralia/moodle-courseformat_grid
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from jay-oswald/optimise_2022072200_script
Optimising 2022072200 upgrade step
- Loading branch information
Showing
2 changed files
with
104 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters