From 2e9bc44bf384991e85474b1bfc62e462fa0b8bb6 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Sun, 20 Sep 2020 14:54:43 +0200 Subject: [PATCH] Fixed exception loading template image that has no image selected (#369) --- element/image/classes/element.php | 45 ++++++++++++++++--------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/element/image/classes/element.php b/element/image/classes/element.php index e76d541..5323273 100644 --- a/element/image/classes/element.php +++ b/element/image/classes/element.php @@ -420,35 +420,38 @@ class element extends \mod_customcert\element { $fs = get_file_storage(); - // If the course file doesn't exist, copy the system file to the course context. - if (!$coursefile = $fs->get_file( - $coursecontext->id, - 'mod_customcert', - $imagedata->filearea, - $imagedata->itemid, - $imagedata->filepath, - $imagedata->filename - )) { - $systemfile = $fs->get_file( - $systemcontext->id, + // Check that a file has been selected. + if (isset($imagedata->filearea)) { + // If the course file doesn't exist, copy the system file to the course context. + if (!$coursefile = $fs->get_file( + $coursecontext->id, 'mod_customcert', $imagedata->filearea, $imagedata->itemid, $imagedata->filepath, $imagedata->filename - ); + )) { + $systemfile = $fs->get_file( + $systemcontext->id, + 'mod_customcert', + $imagedata->filearea, + $imagedata->itemid, + $imagedata->filepath, + $imagedata->filename + ); - // We want to update the context of the file if it doesn't exist in the course context. - $fieldupdates = [ - 'contextid' => $coursecontext->id - ]; - $coursefile = $fs->create_file_from_storedfile($fieldupdates, $systemfile); + // We want to update the context of the file if it doesn't exist in the course context. + $fieldupdates = [ + 'contextid' => $coursecontext->id + ]; + $coursefile = $fs->create_file_from_storedfile($fieldupdates, $systemfile); + } + + // Set the image to the copied file in the course. + $imagedata->fileid = $coursefile->get_id(); + $DB->set_field('customcert_elements', 'data', $this->save_unique_data($imagedata), ['id' => $this->get_id()]); } - // Set the image to the copied file in the course. - $imagedata->fileid = $coursefile->get_id(); - $DB->set_field('customcert_elements', 'data', $this->save_unique_data($imagedata), ['id' => $this->get_id()]); - return true; } }