diff --git a/element/bgimage/classes/element.php b/element/bgimage/classes/element.php index 540fe13..cbd2641 100644 --- a/element/bgimage/classes/element.php +++ b/element/bgimage/classes/element.php @@ -71,6 +71,13 @@ class element extends \customcertelement_image\element { return; } + $imageinfo = json_decode($this->get_data()); + + // If there is no file, we have nothing to display. + if (empty($imageinfo->filename)) { + return; + } + if ($file = $this->get_file()) { $location = make_request_directory() . '/target'; $file->copy_content_to($location); @@ -101,6 +108,13 @@ class element extends \customcertelement_image\element { return ''; } + $imageinfo = json_decode($this->get_data()); + + // If there is no file, we have nothing to display. + if (empty($imageinfo->filename)) { + return ''; + } + if ($file = $this->get_file()) { $url = \moodle_url::make_pluginfile_url($file->get_contextid(), 'mod_customcert', 'image', $file->get_itemid(), $file->get_filepath(), $file->get_filename()); diff --git a/element/image/classes/element.php b/element/image/classes/element.php index aa6441b..7458beb 100644 --- a/element/image/classes/element.php +++ b/element/image/classes/element.php @@ -143,22 +143,23 @@ class element extends \mod_customcert\element { * @return string the json encoded array */ public function save_unique_data($data) { - if (empty($data->fileid)) { - return; - } - - // Array of data we will be storing in the database. - $fs = get_file_storage(); - $file = $fs->get_file_by_id($data->fileid); - $arrtostore = array( - 'contextid' => $file->get_contextid(), - 'filearea' => $file->get_filearea(), - 'itemid' => $file->get_itemid(), - 'filepath' => $file->get_filepath(), - 'filename' => $file->get_filename(), + $arrtostore = [ 'width' => !empty($data->width) ? (int) $data->width : 0, 'height' => !empty($data->height) ? (int) $data->height : 0 - ); + ]; + + if (!empty($data->fileid)) { + // Array of data we will be storing in the database. + $fs = get_file_storage(); + $file = $fs->get_file_by_id($data->fileid); + $arrtostore += [ + 'contextid' => $file->get_contextid(), + 'filearea' => $file->get_filearea(), + 'itemid' => $file->get_itemid(), + 'filepath' => $file->get_filepath(), + 'filename' => $file->get_filename(), + ]; + } return json_encode($arrtostore); } @@ -178,6 +179,11 @@ class element extends \mod_customcert\element { $imageinfo = json_decode($this->get_data()); + // If there is no file, we have nothing to display. + if (empty($imageinfo->filename)) { + return; + } + if ($file = $this->get_file()) { $location = make_request_directory() . '/target'; $file->copy_content_to($location); @@ -207,6 +213,11 @@ class element extends \mod_customcert\element { $imageinfo = json_decode($this->get_data()); + // If there is no file, we have nothing to display. + if (empty($imageinfo->filename)) { + return ''; + } + // Get the image. $fs = get_file_storage(); if ($file = $fs->get_file($imageinfo->contextid, 'mod_customcert', $imageinfo->filearea, $imageinfo->itemid, @@ -248,13 +259,19 @@ class element extends \mod_customcert\element { // Set the image, width and height for this element. if (!empty($this->get_data())) { $imageinfo = json_decode($this->get_data()); - if ($file = $this->get_file()) { - $element = $mform->getElement('fileid'); - $element->setValue($file->get_id()); + if (!empty($imageinfo->filename)) { + if ($file = $this->get_file()) { + $element = $mform->getElement('fileid'); + $element->setValue($file->get_id()); + } + } + if (isset($imageinfo->width) && $mform->elementExists('width')) { $element = $mform->getElement('width'); $element->setValue($imageinfo->width); + } + if (isset($imageinfo->height) && $mform->elementExists('height')) { $element = $mform->getElement('height'); $element->setValue($imageinfo->height); } diff --git a/tests/behat/managing_elements.feature b/tests/behat/managing_elements.feature index 21d6a16..56db814 100644 --- a/tests/behat/managing_elements.feature +++ b/tests/behat/managing_elements.feature @@ -163,8 +163,16 @@ Feature: Being able to manage elements in a certificate template And I press "Save changes" # Image. And I add the element "Image" to page "1" of the "Custom certificate 1" certificate template + And I set the following fields to these values: + | Width | 25 | + | Height | 15 | And I press "Save changes" And I should see "Image" in the "elementstable" "table" + And I click on ".edit-icon" "css_element" in the "Image" "table_row" + And the following fields match these values: + | Width | 25 | + | Height | 15 | + And I press "Save changes" # Student name. And I add the element "Student name" to page "1" of the "Custom certificate 1" certificate template And I set the following fields to these values: