Add ability to set the width and height even if no image is selected

This commit is contained in:
Mark Nelson 2017-08-27 16:14:58 +08:00
parent 43a4b8b8e7
commit 1e35923b43
3 changed files with 56 additions and 17 deletions

View file

@ -71,6 +71,13 @@ class element extends \customcertelement_image\element {
return; 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()) { if ($file = $this->get_file()) {
$location = make_request_directory() . '/target'; $location = make_request_directory() . '/target';
$file->copy_content_to($location); $file->copy_content_to($location);
@ -101,6 +108,13 @@ class element extends \customcertelement_image\element {
return ''; 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()) { if ($file = $this->get_file()) {
$url = \moodle_url::make_pluginfile_url($file->get_contextid(), 'mod_customcert', 'image', $file->get_itemid(), $url = \moodle_url::make_pluginfile_url($file->get_contextid(), 'mod_customcert', 'image', $file->get_itemid(),
$file->get_filepath(), $file->get_filename()); $file->get_filepath(), $file->get_filename());

View file

@ -143,22 +143,23 @@ class element extends \mod_customcert\element {
* @return string the json encoded array * @return string the json encoded array
*/ */
public function save_unique_data($data) { public function save_unique_data($data) {
if (empty($data->fileid)) { $arrtostore = [
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(),
'width' => !empty($data->width) ? (int) $data->width : 0, 'width' => !empty($data->width) ? (int) $data->width : 0,
'height' => !empty($data->height) ? (int) $data->height : 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); return json_encode($arrtostore);
} }
@ -178,6 +179,11 @@ class element extends \mod_customcert\element {
$imageinfo = json_decode($this->get_data()); $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()) { if ($file = $this->get_file()) {
$location = make_request_directory() . '/target'; $location = make_request_directory() . '/target';
$file->copy_content_to($location); $file->copy_content_to($location);
@ -207,6 +213,11 @@ class element extends \mod_customcert\element {
$imageinfo = json_decode($this->get_data()); $imageinfo = json_decode($this->get_data());
// If there is no file, we have nothing to display.
if (empty($imageinfo->filename)) {
return '';
}
// Get the image. // Get the image.
$fs = get_file_storage(); $fs = get_file_storage();
if ($file = $fs->get_file($imageinfo->contextid, 'mod_customcert', $imageinfo->filearea, $imageinfo->itemid, 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. // Set the image, width and height for this element.
if (!empty($this->get_data())) { if (!empty($this->get_data())) {
$imageinfo = json_decode($this->get_data()); $imageinfo = json_decode($this->get_data());
if ($file = $this->get_file()) { if (!empty($imageinfo->filename)) {
$element = $mform->getElement('fileid'); if ($file = $this->get_file()) {
$element->setValue($file->get_id()); $element = $mform->getElement('fileid');
$element->setValue($file->get_id());
}
}
if (isset($imageinfo->width) && $mform->elementExists('width')) {
$element = $mform->getElement('width'); $element = $mform->getElement('width');
$element->setValue($imageinfo->width); $element->setValue($imageinfo->width);
}
if (isset($imageinfo->height) && $mform->elementExists('height')) {
$element = $mform->getElement('height'); $element = $mform->getElement('height');
$element->setValue($imageinfo->height); $element->setValue($imageinfo->height);
} }

View file

@ -163,8 +163,16 @@ Feature: Being able to manage elements in a certificate template
And I press "Save changes" And I press "Save changes"
# Image. # Image.
And I add the element "Image" to page "1" of the "Custom certificate 1" certificate template 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 press "Save changes"
And I should see "Image" in the "elementstable" "table" 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. # Student name.
And I add the element "Student name" to page "1" of the "Custom certificate 1" certificate template 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: And I set the following fields to these values: