From 58ce3162215fa6c4bc226cc32f2dc45586654270 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 1 May 2017 18:50:42 +0800 Subject: [PATCH] #108 Upgrade existing image elements --- element/bgimage/db/upgrade.php | 66 ++++++++++++++++++++++++++++++++++ element/bgimage/version.php | 2 +- element/image/db/upgrade.php | 66 ++++++++++++++++++++++++++++++++++ element/image/version.php | 2 +- 4 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 element/bgimage/db/upgrade.php create mode 100644 element/image/db/upgrade.php diff --git a/element/bgimage/db/upgrade.php b/element/bgimage/db/upgrade.php new file mode 100644 index 0000000..900c24e --- /dev/null +++ b/element/bgimage/db/upgrade.php @@ -0,0 +1,66 @@ +. + +/** + * Customcert background image element upgrade code. + * + * @package customcertelement_bgimage + * @copyright 2017 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +/** + * Customcert background image element upgrade code. + * + * @param int $oldversion the version we are upgrading from + * @return bool always true + */ +function xmldb_customcertelement_bgimage_upgrade($oldversion) { + global $DB; + + if ($oldversion < 2016120501) { + // Go through each 'image' element and update the file stored information. + if ($images = $DB->get_records_select('customcert_elements', $DB->sql_compare_text('element') . ' = \'bgimage\'')) { + // Create a file storage instance we are going to use to create pathname hashes. + $fs = get_file_storage(); + // Go through and update the details. + foreach ($images as $image) { + // Get the current data we have stored for this element. + $elementinfo = json_decode($image->data); + if ($file = $fs->get_file_by_hash($elementinfo->pathnamehash)) { + $arrtostore = array( + 'contextid' => $file->get_contextid(), + 'filearea' => $file->get_filearea(), + 'itemid' => $file->get_itemid(), + 'filepath' => $file->get_filepath(), + 'filename' => $file->get_filename(), + 'width' => (int) $elementinfo->width, + 'height' => (int) $elementinfo->height + ); + $arrtostore = json_encode($arrtostore); + $DB->set_field('customcert_elements', 'data', $arrtostore, array('id' => $image->id)); + } + } + } + + // Savepoint reached. + upgrade_plugin_savepoint(true, 2016120501, 'customcertelement', 'bgimage'); + } + + return true; +} diff --git a/element/bgimage/version.php b/element/bgimage/version.php index 447b4a7..82a6ac3 100644 --- a/element/bgimage/version.php +++ b/element/bgimage/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); -$plugin->version = 2016120500; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2016120501; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2016120500; // Requires this Moodle version (3.2). $plugin->component = 'customcertelement_bgimage'; diff --git a/element/image/db/upgrade.php b/element/image/db/upgrade.php new file mode 100644 index 0000000..ec5db8d --- /dev/null +++ b/element/image/db/upgrade.php @@ -0,0 +1,66 @@ +. + +/** + * Customcert image element upgrade code. + * + * @package customcertelement_image + * @copyright 2017 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +/** + * Customcert image element upgrade code. + * + * @param int $oldversion the version we are upgrading from + * @return bool always true + */ +function xmldb_customcertelement_image_upgrade($oldversion) { + global $DB; + + if ($oldversion < 2016120501) { + // Go through each 'image' element and update the file stored information. + if ($images = $DB->get_records_select('customcert_elements', $DB->sql_compare_text('element') . ' = \'image\'')) { + // Create a file storage instance we are going to use to create pathname hashes. + $fs = get_file_storage(); + // Go through and update the details. + foreach ($images as $image) { + // Get the current data we have stored for this element. + $elementinfo = json_decode($image->data); + if ($file = $fs->get_file_by_hash($elementinfo->pathnamehash)) { + $arrtostore = array( + 'contextid' => $file->get_contextid(), + 'filearea' => $file->get_filearea(), + 'itemid' => $file->get_itemid(), + 'filepath' => $file->get_filepath(), + 'filename' => $file->get_filename(), + 'width' => (int) $elementinfo->width, + 'height' => (int) $elementinfo->height + ); + $arrtostore = json_encode($arrtostore); + $DB->set_field('customcert_elements', 'data', $arrtostore, array('id' => $image->id)); + } + } + } + + // Savepoint reached. + upgrade_plugin_savepoint(true, 2016120501, 'customcertelement', 'image'); + } + + return true; +} diff --git a/element/image/version.php b/element/image/version.php index 2fc0c01..bb44655 100644 --- a/element/image/version.php +++ b/element/image/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); -$plugin->version = 2016120500; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2016120501; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2016120500; // Requires this Moodle version (3.2). $plugin->component = 'customcertelement_image';