From 2a16dbe1c0b937fe9f0753c2366c1619d98cfb49 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 12 Mar 2020 15:41:10 +0100 Subject: [PATCH] Loading templates copies system images to the course (#298) Before it would just reference the system images. However, this meant backup/restore was broken as it did not include the images the template was using. --- CHANGES.md | 6 +++ element/image/classes/element.php | 50 ++++++++++++++++++- .../image/lang/en/customcertelement_image.php | 2 + 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ef62df4..c963884 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,12 @@ Note - All hash comments refer to the issue number. Eg. #169 refers to https://g - Added extra Behat steps for new elements (#309). +### Changed + +- When copying a site template the site images are also copied to the course context and then those copied images are used. + Before, the elements would simply point to the site images. However, this meant when performing a backup/restore the + images were not stored in the backup file (#298). + ### Fixed - Fixed the displaying of names of a custom user field (#326). diff --git a/element/image/classes/element.php b/element/image/classes/element.php index e2dde5a..bc8b5d1 100644 --- a/element/image/classes/element.php +++ b/element/image/classes/element.php @@ -382,14 +382,14 @@ class element extends \mod_customcert\element { // Loop through the files uploaded in the system context. if ($files = $fs->get_area_files(\context_system::instance()->id, 'mod_customcert', 'image', false, 'filename', false)) { foreach ($files as $hash => $file) { - $arrfiles[$file->get_id()] = $file->get_filename(); + $arrfiles[$file->get_id()] = get_string('systemimage', 'customcertelement_image', $file->get_filename()); } } // Loop through the files uploaded in the course context. if ($files = $fs->get_area_files(\context_course::instance($COURSE->id)->id, 'mod_customcert', 'image', false, 'filename', false)) { foreach ($files as $hash => $file) { - $arrfiles[$file->get_id()] = $file->get_filename(); + $arrfiles[$file->get_id()] = get_string('courseimage', 'customcertelement_image', $file->get_filename()); } } @@ -398,4 +398,50 @@ class element extends \mod_customcert\element { return $arrfiles; } + + /** + * This handles copying data from another element of the same type. + * + * @param \stdClass $data the form data + * @return bool returns true if the data was copied successfully, false otherwise + */ + public function copy_element($data) { + global $COURSE, $DB, $SITE; + + $imagedata = json_decode($data->data); + + // If we are in the site context we don't have to do anything, the images are already there. + if ($COURSE->id == $SITE->id) { + return true; + } + + $context = \context_course::instance($COURSE->id); + + // Copy files to new location. + $fs = get_file_storage(); + $systemfiles = $fs->get_area_files(\context_system::instance()->id, 'mod_customcert', $imagedata->filearea, + $imagedata->itemid); + $fieldupdates = [ + 'contextid' => $context->id + ]; + foreach ($systemfiles as $systemfile) { + // We might have loaded this template already in this course. + if (!$storedfile = $fs->get_file( + $context->id, + 'mod_customcert', + $systemfile->get_filearea(), + $systemfile->get_itemid(), + $systemfile->get_filepath(), + $systemfile->get_filename()) + ) { + $storedfile = $fs->create_file_from_storedfile($fieldupdates, $systemfile); + } + + // Set the image to the copied file in the course. + $imagedata->fileid = $storedfile->get_id(); + $DB->set_field('customcert_elements', 'data', $this->save_unique_data($imagedata), ['id' => $this->get_id()]); + } + + return true; + } } diff --git a/element/image/lang/en/customcertelement_image.php b/element/image/lang/en/customcertelement_image.php index ba3ab25..8842250 100644 --- a/element/image/lang/en/customcertelement_image.php +++ b/element/image/lang/en/customcertelement_image.php @@ -24,6 +24,7 @@ $string['alphachannel'] = 'Alpha channel'; $string['alphachannel_help'] = 'This value determines how transparent the image is. You can set the alpha channel from 0 (fully transparent) to 1 (fully opaque).'; +$string['courseimage'] = 'Course image: {$a}'; $string['height'] = 'Height'; $string['height_help'] = 'Height of the image in mm. If equal to zero, it is automatically calculated.'; $string['image'] = 'Image'; @@ -31,5 +32,6 @@ $string['invalidheight'] = 'The height has to be a valid number greater than or $string['invalidwidth'] = 'The width has to be a valid number greater than or equal to 0.'; $string['pluginname'] = 'Image'; $string['privacy:metadata'] = 'The Image plugin does not store any personal data.'; +$string['systemimage'] = 'Site image: {$a}'; $string['width'] = 'Width'; $string['width_help'] = 'Width of the image in mm. If equal to zero, it is automatically calculated.';