Fixed exception loading template image that has no image selected (#369)

This commit is contained in:
Mark Nelson 2020-09-20 14:54:43 +02:00
parent e7e238aaf7
commit 2e9bc44bf3

View file

@ -420,35 +420,38 @@ class element extends \mod_customcert\element {
$fs = get_file_storage(); $fs = get_file_storage();
// If the course file doesn't exist, copy the system file to the course context. // Check that a file has been selected.
if (!$coursefile = $fs->get_file( if (isset($imagedata->filearea)) {
$coursecontext->id, // If the course file doesn't exist, copy the system file to the course context.
'mod_customcert', if (!$coursefile = $fs->get_file(
$imagedata->filearea, $coursecontext->id,
$imagedata->itemid,
$imagedata->filepath,
$imagedata->filename
)) {
$systemfile = $fs->get_file(
$systemcontext->id,
'mod_customcert', 'mod_customcert',
$imagedata->filearea, $imagedata->filearea,
$imagedata->itemid, $imagedata->itemid,
$imagedata->filepath, $imagedata->filepath,
$imagedata->filename $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. // We want to update the context of the file if it doesn't exist in the course context.
$fieldupdates = [ $fieldupdates = [
'contextid' => $coursecontext->id 'contextid' => $coursecontext->id
]; ];
$coursefile = $fs->create_file_from_storedfile($fieldupdates, $systemfile); $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; return true;
} }
} }