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 f75a943352
commit 33d2f65e56
3 changed files with 56 additions and 17 deletions

View file

@ -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());

View file

@ -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);
}

View file

@ -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: