2013-05-30 08:28:47 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of the customcert module for Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2013-07-22 05:06:18 +00:00
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
2013-05-30 08:28:47 +00:00
|
|
|
|
2017-02-16 12:12:19 +00:00
|
|
|
/**
|
|
|
|
* This file contains the customcert element image's core interaction API.
|
|
|
|
*
|
|
|
|
* @package customcertelement_image
|
|
|
|
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2015-12-11 06:53:45 +00:00
|
|
|
namespace customcertelement_image;
|
2013-06-12 10:35:27 +00:00
|
|
|
|
2015-12-11 06:53:45 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
2013-06-12 10:35:27 +00:00
|
|
|
|
2013-05-30 08:28:47 +00:00
|
|
|
/**
|
2013-06-12 10:35:27 +00:00
|
|
|
* The customcert element image's core interaction API.
|
2013-05-30 08:28:47 +00:00
|
|
|
*
|
2013-06-21 09:35:14 +00:00
|
|
|
* @package customcertelement_image
|
2013-07-22 05:06:18 +00:00
|
|
|
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
2013-05-30 08:28:47 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2015-12-11 06:53:45 +00:00
|
|
|
class element extends \mod_customcert\element {
|
2013-05-30 08:28:47 +00:00
|
|
|
|
2017-02-16 12:12:19 +00:00
|
|
|
/**
|
|
|
|
* @var array The file manager options.
|
|
|
|
*/
|
2016-02-16 09:03:34 +00:00
|
|
|
protected $filemanageroptions = array();
|
2015-12-10 07:04:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \stdClass $element the element data
|
2015-12-10 07:04:48 +00:00
|
|
|
*/
|
|
|
|
public function __construct($element) {
|
|
|
|
global $COURSE;
|
|
|
|
|
|
|
|
$this->filemanageroptions = array(
|
|
|
|
'maxbytes' => $COURSE->maxbytes,
|
|
|
|
'subdirs' => 1,
|
|
|
|
'accepted_types' => 'image'
|
|
|
|
);
|
|
|
|
|
|
|
|
parent::__construct($element);
|
|
|
|
}
|
|
|
|
|
2013-06-06 10:59:08 +00:00
|
|
|
/**
|
|
|
|
* This function renders the form elements when adding a customcert element.
|
|
|
|
*
|
2016-02-16 09:03:34 +00:00
|
|
|
* @param \mod_customcert\edit_element_form $mform the edit_form instance
|
2013-06-06 10:59:08 +00:00
|
|
|
*/
|
|
|
|
public function render_form_elements($mform) {
|
2017-05-01 10:59:19 +00:00
|
|
|
$mform->addElement('select', 'fileid', get_string('image', 'customcertelement_image'), self::get_images());
|
2013-05-30 08:28:47 +00:00
|
|
|
|
2013-06-21 09:35:14 +00:00
|
|
|
$mform->addElement('text', 'width', get_string('width', 'customcertelement_image'), array('size' => 10));
|
2013-06-06 10:59:08 +00:00
|
|
|
$mform->setType('width', PARAM_INT);
|
2013-07-23 09:05:37 +00:00
|
|
|
$mform->setDefault('width', 0);
|
2013-06-21 09:35:14 +00:00
|
|
|
$mform->addHelpButton('width', 'width', 'customcertelement_image');
|
2013-06-06 10:59:08 +00:00
|
|
|
|
2013-06-21 09:35:14 +00:00
|
|
|
$mform->addElement('text', 'height', get_string('height', 'customcertelement_image'), array('size' => 10));
|
2013-06-06 10:59:08 +00:00
|
|
|
$mform->setType('height', PARAM_INT);
|
2013-07-23 09:05:37 +00:00
|
|
|
$mform->setDefault('height', 0);
|
2013-06-21 09:35:14 +00:00
|
|
|
$mform->addHelpButton('height', 'height', 'customcertelement_image');
|
2013-05-30 08:28:47 +00:00
|
|
|
|
2017-01-31 04:45:00 +00:00
|
|
|
if (get_config('customcert', 'showposxy')) {
|
|
|
|
\mod_customcert\element_helper::render_form_element_position($mform);
|
|
|
|
}
|
|
|
|
|
2017-02-16 12:12:19 +00:00
|
|
|
$mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '',
|
|
|
|
$this->filemanageroptions);
|
2013-05-30 08:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs validation on the element values.
|
|
|
|
*
|
|
|
|
* @param array $data the submitted data
|
|
|
|
* @param array $files the submitted files
|
|
|
|
* @return array the validation errors
|
|
|
|
*/
|
|
|
|
public function validate_form_elements($data, $files) {
|
|
|
|
// Array to return the errors.
|
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
// Check if width is not set, or not numeric or less than 0.
|
2013-06-06 10:59:08 +00:00
|
|
|
if ((!isset($data['width'])) || (!is_numeric($data['width'])) || ($data['width'] < 0)) {
|
2013-06-21 09:35:14 +00:00
|
|
|
$errors['width'] = get_string('invalidwidth', 'customcertelement_image');
|
2013-05-30 08:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if height is not set, or not numeric or less than 0.
|
2013-06-06 10:59:08 +00:00
|
|
|
if ((!isset($data['height'])) || (!is_numeric($data['height'])) || ($data['height'] < 0)) {
|
2013-06-21 09:35:14 +00:00
|
|
|
$errors['height'] = get_string('invalidheight', 'customcertelement_image');
|
2013-05-30 08:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the position.
|
2017-01-31 04:45:00 +00:00
|
|
|
if (get_config('customcert', 'showposxy')) {
|
|
|
|
$errors += \mod_customcert\element_helper::validate_form_element_position($data);
|
|
|
|
}
|
2013-05-30 08:28:47 +00:00
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
2015-03-13 06:58:35 +00:00
|
|
|
/**
|
|
|
|
* Handles saving the form elements created by this element.
|
|
|
|
* Can be overridden if more functionality is needed.
|
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \stdClass $data the form data
|
2016-06-13 11:39:57 +00:00
|
|
|
* @return bool true of success, false otherwise.
|
2015-03-13 06:58:35 +00:00
|
|
|
*/
|
|
|
|
public function save_form_elements($data) {
|
2016-08-26 04:56:04 +00:00
|
|
|
global $COURSE, $SITE;
|
|
|
|
|
|
|
|
// Set the context.
|
|
|
|
if ($COURSE->id == $SITE->id) {
|
|
|
|
$context = \context_system::instance();
|
|
|
|
} else {
|
|
|
|
$context = \context_course::instance($COURSE->id);
|
|
|
|
}
|
2015-03-13 06:58:35 +00:00
|
|
|
|
|
|
|
// Handle file uploads.
|
2016-08-26 04:56:04 +00:00
|
|
|
\mod_customcert\certificate::upload_imagefiles($data->customcertimage, $context->id);
|
2015-03-13 06:58:35 +00:00
|
|
|
|
2016-06-13 11:39:57 +00:00
|
|
|
return parent::save_form_elements($data);
|
2015-03-13 06:58:35 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 08:28:47 +00:00
|
|
|
/**
|
|
|
|
* This will handle how form data will be saved into the data column in the
|
|
|
|
* customcert_elements table.
|
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \stdClass $data the form data
|
2013-05-30 08:28:47 +00:00
|
|
|
* @return string the json encoded array
|
|
|
|
*/
|
|
|
|
public function save_unique_data($data) {
|
2017-06-23 05:00:25 +00:00
|
|
|
if (empty($data->fileid)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-30 08:28:47 +00:00
|
|
|
// Array of data we will be storing in the database.
|
2017-05-01 10:59:19 +00:00
|
|
|
$fs = get_file_storage();
|
|
|
|
$file = $fs->get_file_by_id($data->fileid);
|
2013-05-30 08:28:47 +00:00
|
|
|
$arrtostore = array(
|
2017-05-01 10:59:19 +00:00
|
|
|
'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,
|
|
|
|
'height' => !empty($data->height) ? (int) $data->height : 0
|
2013-05-30 08:28:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return json_encode($arrtostore);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles rendering the element on the pdf.
|
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \pdf $pdf the pdf object
|
2013-06-28 07:17:43 +00:00
|
|
|
* @param bool $preview true if it is a preview, false otherwise
|
2016-08-23 08:22:58 +00:00
|
|
|
* @param \stdClass $user the user we are rendering this for
|
2013-05-30 08:28:47 +00:00
|
|
|
*/
|
2016-08-23 08:22:58 +00:00
|
|
|
public function render($pdf, $preview, $user) {
|
2013-05-30 08:28:47 +00:00
|
|
|
// If there is no element data, we have nothing to display.
|
|
|
|
if (empty($this->element->data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$imageinfo = json_decode($this->element->data);
|
|
|
|
|
2017-05-01 10:59:19 +00:00
|
|
|
if ($file = $this->get_file()) {
|
2017-03-08 06:30:34 +00:00
|
|
|
$location = make_request_directory() . '/target';
|
|
|
|
$file->copy_content_to($location);
|
2017-04-24 15:17:41 +00:00
|
|
|
|
|
|
|
$mimetype = $file->get_mimetype();
|
|
|
|
if ($mimetype == 'image/svg+xml') {
|
|
|
|
$pdf->ImageSVG($location, $this->element->posx, $this->element->posy, $imageinfo->width, $imageinfo->height);
|
|
|
|
} else {
|
|
|
|
$pdf->Image($location, $this->element->posx, $this->element->posy, $imageinfo->width, $imageinfo->height);
|
|
|
|
}
|
2013-05-30 08:28:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-05 05:38:35 +00:00
|
|
|
/**
|
|
|
|
* Render the element in html.
|
|
|
|
*
|
|
|
|
* This function is used to render the element when we are using the
|
|
|
|
* drag and drop interface to position it.
|
2016-02-16 09:03:34 +00:00
|
|
|
*
|
|
|
|
* @return string the html
|
2015-08-05 05:38:35 +00:00
|
|
|
*/
|
|
|
|
public function render_html() {
|
|
|
|
// If there is no element data, we have nothing to display.
|
|
|
|
if (empty($this->element->data)) {
|
2016-02-16 09:03:34 +00:00
|
|
|
return '';
|
2015-08-05 05:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$imageinfo = json_decode($this->element->data);
|
|
|
|
|
|
|
|
// Get the image.
|
|
|
|
$fs = get_file_storage();
|
2017-05-01 10:59:19 +00:00
|
|
|
if ($file = $fs->get_file($imageinfo->contextid, 'mod_customcert', $imageinfo->filearea, $imageinfo->itemid,
|
|
|
|
$imageinfo->filepath, $imageinfo->filename)) {
|
2015-12-11 06:53:45 +00:00
|
|
|
$url = \moodle_url::make_pluginfile_url($file->get_contextid(), 'mod_customcert', 'image', $file->get_itemid(),
|
2015-08-05 05:38:35 +00:00
|
|
|
$file->get_filepath(), $file->get_filename());
|
|
|
|
$fileimageinfo = $file->get_imageinfo();
|
|
|
|
$whratio = $fileimageinfo['width'] / $fileimageinfo['height'];
|
|
|
|
// The size of the images to use in the CSS style.
|
|
|
|
$style = '';
|
|
|
|
if ($imageinfo->width === 0 && $imageinfo->height === 0) {
|
|
|
|
$style .= 'width: ' . $fileimageinfo['width'] . 'px; ';
|
|
|
|
$style .= 'height: ' . $fileimageinfo['height'] . 'px';
|
|
|
|
} else if ($imageinfo->width === 0) { // Then the height must be set.
|
|
|
|
// We must get the width based on the height to keep the ratio.
|
|
|
|
$style .= 'width: ' . ($imageinfo->height * $whratio) . 'mm; ';
|
|
|
|
$style .= 'height: ' . $imageinfo->height . 'mm';
|
|
|
|
} else if ($imageinfo->height === 0) { // Then the width must be set.
|
|
|
|
$style .= 'width: ' . $imageinfo->width . 'mm; ';
|
|
|
|
// We must get the height based on the width to keep the ratio.
|
|
|
|
$style .= 'height: ' . ($imageinfo->width / $whratio) . 'mm';
|
|
|
|
} else { // Must both be set.
|
|
|
|
$style .= 'width: ' . $imageinfo->width . 'mm; ';
|
|
|
|
$style .= 'height: ' . $imageinfo->height . 'mm';
|
|
|
|
}
|
|
|
|
|
2015-12-11 06:53:45 +00:00
|
|
|
return \html_writer::tag('img', '', array('src' => $url, 'style' => $style));
|
2015-08-05 05:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:40:56 +00:00
|
|
|
/**
|
|
|
|
* Sets the data on the form when editing an element.
|
|
|
|
*
|
2016-02-16 09:03:34 +00:00
|
|
|
* @param \mod_customcert\edit_element_form $mform the edit_form instance
|
2013-06-25 03:40:56 +00:00
|
|
|
*/
|
|
|
|
public function definition_after_data($mform) {
|
2016-08-26 04:56:04 +00:00
|
|
|
global $COURSE, $SITE;
|
2015-03-13 06:58:35 +00:00
|
|
|
|
2013-06-25 03:40:56 +00:00
|
|
|
// Set the image, width and height for this element.
|
2015-12-10 07:26:49 +00:00
|
|
|
if (!empty($this->element->data)) {
|
|
|
|
$imageinfo = json_decode($this->element->data);
|
2017-05-01 10:59:19 +00:00
|
|
|
if ($file = $this->get_file()) {
|
|
|
|
$this->element->fileid = $file->get_id();
|
|
|
|
$this->element->width = $imageinfo->width;
|
|
|
|
$this->element->height = $imageinfo->height;
|
|
|
|
}
|
2015-12-10 07:26:49 +00:00
|
|
|
}
|
2013-06-25 03:40:56 +00:00
|
|
|
|
2016-08-26 04:56:04 +00:00
|
|
|
// Set the context.
|
|
|
|
if ($COURSE->id == $SITE->id) {
|
|
|
|
$context = \context_system::instance();
|
|
|
|
} else {
|
|
|
|
$context = \context_course::instance($COURSE->id);
|
|
|
|
}
|
|
|
|
|
2015-03-13 06:58:35 +00:00
|
|
|
// Editing existing instance - copy existing files into draft area.
|
|
|
|
$draftitemid = file_get_submitted_draft_itemid('customcertimage');
|
2016-08-26 04:56:04 +00:00
|
|
|
file_prepare_draft_area($draftitemid, $context->id, 'mod_customcert', 'image', 0, $this->filemanageroptions);
|
2015-03-13 06:58:35 +00:00
|
|
|
$element = $mform->getElement('customcertimage');
|
|
|
|
$element->setValue($draftitemid);
|
|
|
|
|
2013-06-25 03:40:56 +00:00
|
|
|
parent::definition_after_data($mform);
|
|
|
|
}
|
|
|
|
|
2017-04-27 13:54:10 +00:00
|
|
|
/**
|
|
|
|
* This function is responsible for handling the restoration process of the element.
|
|
|
|
*
|
|
|
|
* We will want to update the file's pathname hash.
|
|
|
|
*
|
|
|
|
* @param \restore_customcert_activity_task $restore
|
|
|
|
*/
|
|
|
|
public function after_restore($restore) {
|
|
|
|
global $DB;
|
|
|
|
|
2017-05-01 10:59:19 +00:00
|
|
|
// Get the current data we have stored for this element.
|
|
|
|
$elementinfo = json_decode($this->element->data);
|
|
|
|
|
|
|
|
// Update the context.
|
|
|
|
$elementinfo->contextid = \context_course::instance($restore->get_courseid())->id;
|
|
|
|
|
|
|
|
// Encode again before saving.
|
|
|
|
$elementinfo = json_encode($elementinfo);
|
|
|
|
|
|
|
|
// Perform the update.
|
|
|
|
$DB->set_field('customcert_elements', 'data', $elementinfo, array('id' => $this->element->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch stored file.
|
|
|
|
*
|
|
|
|
* @return \stored_file|bool stored_file instance if exists, false if not
|
|
|
|
*/
|
|
|
|
public function get_file() {
|
|
|
|
$imageinfo = json_decode($this->element->data);
|
|
|
|
|
|
|
|
$fs = get_file_storage();
|
|
|
|
|
|
|
|
return $fs->get_file($imageinfo->contextid, 'mod_customcert', $imageinfo->filearea, $imageinfo->itemid,
|
|
|
|
$imageinfo->filepath, $imageinfo->filename);
|
2017-04-27 13:54:10 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 08:28:47 +00:00
|
|
|
/**
|
|
|
|
* Return the list of possible images to use.
|
|
|
|
*
|
2013-06-12 10:35:27 +00:00
|
|
|
* @return array the list of images that can be used
|
2013-05-30 08:28:47 +00:00
|
|
|
*/
|
|
|
|
public static function get_images() {
|
2013-09-09 08:50:48 +00:00
|
|
|
global $COURSE;
|
|
|
|
|
2013-05-30 08:28:47 +00:00
|
|
|
// Create file storage object.
|
|
|
|
$fs = get_file_storage();
|
|
|
|
|
|
|
|
// The array used to store the images.
|
|
|
|
$arrfiles = array();
|
2013-09-09 08:50:48 +00:00
|
|
|
// Loop through the files uploaded in the system context.
|
2015-12-11 06:53:45 +00:00
|
|
|
if ($files = $fs->get_area_files(\context_system::instance()->id, 'mod_customcert', 'image', false, 'filename', false)) {
|
2013-05-30 08:28:47 +00:00
|
|
|
foreach ($files as $hash => $file) {
|
2017-05-01 10:59:19 +00:00
|
|
|
$arrfiles[$file->get_id()] = $file->get_filename();
|
2013-05-30 08:28:47 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-09 08:50:48 +00:00
|
|
|
// Loop through the files uploaded in the course context.
|
2017-02-16 12:12:19 +00:00
|
|
|
if ($files = $fs->get_area_files(\context_course::instance($COURSE->id)->id, 'mod_customcert', 'image', false,
|
|
|
|
'filename', false)) {
|
2013-09-09 08:50:48 +00:00
|
|
|
foreach ($files as $hash => $file) {
|
2017-05-01 10:59:19 +00:00
|
|
|
$arrfiles[$file->get_id()] = $file->get_filename();
|
2013-09-09 08:50:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 06:53:45 +00:00
|
|
|
\core_collator::asort($arrfiles);
|
2017-05-01 10:59:19 +00:00
|
|
|
$arrfiles = array('0' => get_string('noimage', 'customcert')) + $arrfiles;
|
2013-05-30 08:28:47 +00:00
|
|
|
|
|
|
|
return $arrfiles;
|
|
|
|
}
|
|
|
|
}
|