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
|
|
|
|
2013-06-12 10:35:27 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
|
|
|
|
|
2013-06-21 09:35:14 +00:00
|
|
|
require_once($CFG->dirroot . '/mod/customcert/element/element.class.php');
|
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
|
|
|
|
*/
|
2013-06-21 09:35:14 +00:00
|
|
|
class customcert_element_image extends customcert_element_base {
|
2013-05-30 08:28:47 +00:00
|
|
|
|
2013-06-06 10:59:08 +00:00
|
|
|
/**
|
|
|
|
* This function renders the form elements when adding a customcert element.
|
|
|
|
*
|
2013-06-12 10:35:27 +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) {
|
2013-06-21 09:35:14 +00:00
|
|
|
$mform->addElement('select', 'image', 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
|
|
|
|
2013-06-21 09:35:14 +00:00
|
|
|
parent::render_form_element_position($mform);
|
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.
|
2013-06-21 09:35:14 +00:00
|
|
|
$errors += $this->validate_form_element_position($data);
|
2013-05-30 08:28:47 +00:00
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will handle how form data will be saved into the data column in the
|
|
|
|
* customcert_elements table.
|
|
|
|
*
|
2013-06-12 10:35:27 +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) {
|
|
|
|
// Array of data we will be storing in the database.
|
|
|
|
$arrtostore = array(
|
2013-06-06 10:59:08 +00:00
|
|
|
'pathnamehash' => $data->image,
|
|
|
|
'width' => $data->width,
|
|
|
|
'height' => $data->height
|
2013-05-30 08:28:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return json_encode($arrtostore);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles rendering the element on the pdf.
|
|
|
|
*
|
2013-06-12 10:35:27 +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
|
2013-05-30 08:28:47 +00:00
|
|
|
*/
|
2013-06-28 07:17:43 +00:00
|
|
|
public function render($pdf, $preview) {
|
2013-05-30 08:28:47 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
// If there is no element data, we have nothing to display.
|
|
|
|
if (empty($this->element->data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$imageinfo = json_decode($this->element->data);
|
|
|
|
|
|
|
|
// Get the image.
|
|
|
|
$fs = get_file_storage();
|
2013-06-06 10:59:08 +00:00
|
|
|
if ($file = $fs->get_file_by_hash($imageinfo->pathnamehash)) {
|
2013-05-30 08:28:47 +00:00
|
|
|
$contenthash = $file->get_contenthash();
|
|
|
|
$l1 = $contenthash[0] . $contenthash[1];
|
|
|
|
$l2 = $contenthash[2] . $contenthash[3];
|
|
|
|
$location = $CFG->dataroot . '/filedir' . '/' . $l1 . '/' . $l2 . '/' . $contenthash;
|
2013-06-06 10:59:08 +00:00
|
|
|
$pdf->Image($location, $this->element->posx, $this->element->posy, $imageinfo->width, $imageinfo->height);
|
2013-05-30 08:28:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:40:56 +00:00
|
|
|
/**
|
|
|
|
* Sets the data on the form when editing an element.
|
|
|
|
*
|
|
|
|
* @param mod_customcert_edit_element_form $mform the edit_form instance
|
|
|
|
*/
|
|
|
|
public function definition_after_data($mform) {
|
|
|
|
// Set the image, width and height for this element.
|
2013-07-23 09:01:27 +00:00
|
|
|
$imageinfo = json_decode($this->element->data);
|
|
|
|
$this->element->image = $imageinfo->pathnamehash;
|
|
|
|
$this->element->width = $imageinfo->width;
|
|
|
|
$this->element->height = $imageinfo->height;
|
2013-06-25 03:40:56 +00:00
|
|
|
|
|
|
|
parent::definition_after_data($mform);
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
// Create file storage object.
|
|
|
|
$fs = get_file_storage();
|
|
|
|
|
|
|
|
// The array used to store the images.
|
|
|
|
$arrfiles = array();
|
|
|
|
$arrfiles[0] = get_string('noimage', 'customcert');
|
|
|
|
if ($files = $fs->get_area_files(context_system::instance()->id, 'mod_customcert', 'image', false, 'filename', false)) {
|
|
|
|
foreach ($files as $hash => $file) {
|
|
|
|
$arrfiles[$hash] = $file->get_filename();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arrfiles;
|
|
|
|
}
|
|
|
|
}
|