Fixed broken logic (#298)

This commit is contained in:
Mark Nelson 2020-03-16 17:04:08 +01:00
parent 48c83d34df
commit b480cf3c68

View file

@ -410,37 +410,44 @@ class element extends \mod_customcert\element {
$imagedata = json_decode($data->data); $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 we are in the site context we don't have to do anything, the image is already there.
if ($COURSE->id == $SITE->id) { if ($COURSE->id == $SITE->id) {
return true; return true;
} }
$context = \context_course::instance($COURSE->id); $coursecontext = \context_course::instance($COURSE->id);
$systemcontext = \context_system::instance();
// Copy files to new location.
$fs = get_file_storage(); $fs = get_file_storage();
$systemfiles = $fs->get_area_files(\context_system::instance()->id, 'mod_customcert', $imagedata->filearea,
$imagedata->itemid); // If the course file doesn't exist, copy the system file to the course context.
$fieldupdates = [ if (!$coursefile = $fs->get_file(
'contextid' => $context->id $coursecontext->id,
];
foreach ($systemfiles as $systemfile) {
// We might have loaded this template already in this course.
if (!$storedfile = $fs->get_file(
$context->id,
'mod_customcert', 'mod_customcert',
$systemfile->get_filearea(), $imagedata->filearea,
$systemfile->get_itemid(), $imagedata->itemid,
$systemfile->get_filepath(), $imagedata->filepath,
$systemfile->get_filename()) $imagedata->filename
) { )) {
$storedfile = $fs->create_file_from_storedfile($fieldupdates, $systemfile); $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);
} }
// Set the image to the copied file in the course. // Set the image to the copied file in the course.
$imagedata->fileid = $storedfile->get_id(); $imagedata->fileid = $coursefile->get_id();
$DB->set_field('customcert_elements', 'data', $this->save_unique_data($imagedata), ['id' => $this->get_id()]); $DB->set_field('customcert_elements', 'data', $this->save_unique_data($imagedata), ['id' => $this->get_id()]);
}
return true; return true;
} }