1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of the customcert module for Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* This file contains the customcert element background image's core interaction API.
|
|
|
19 |
*
|
|
|
20 |
* @package customcertelement_bgimage
|
|
|
21 |
* @copyright 2016 Mark Nelson <markn@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace customcertelement_bgimage;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* The customcert element background image's core interaction API.
|
|
|
29 |
*
|
|
|
30 |
* @package customcertelement_bgimage
|
|
|
31 |
* @copyright 2016 Mark Nelson <markn@moodle.com>
|
|
|
32 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
*/
|
|
|
34 |
class element extends \customcertelement_image\element {
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* This function renders the form elements when adding a customcert element.
|
|
|
38 |
*
|
|
|
39 |
* @param \MoodleQuickForm $mform the edit_form instance
|
|
|
40 |
*/
|
|
|
41 |
public function render_form_elements($mform) {
|
|
|
42 |
$mform->addElement('select', 'fileid', get_string('image', 'customcertelement_image'), self::get_images());
|
|
|
43 |
$mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '',
|
|
|
44 |
$this->filemanageroptions);
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Performs validation on the element values.
|
|
|
49 |
*
|
|
|
50 |
* @param array $data the submitted data
|
|
|
51 |
* @param array $files the submitted files
|
|
|
52 |
* @return array the validation errors
|
|
|
53 |
*/
|
|
|
54 |
public function validate_form_elements($data, $files) {
|
|
|
55 |
// Array to return the errors.
|
|
|
56 |
return [];
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Handles rendering the element on the pdf.
|
|
|
61 |
*
|
|
|
62 |
* @param \pdf $pdf the pdf object
|
|
|
63 |
* @param bool $preview true if it is a preview, false otherwise
|
|
|
64 |
* @param \stdClass $user the user we are rendering this for
|
|
|
65 |
*/
|
|
|
66 |
public function render($pdf, $preview, $user) {
|
|
|
67 |
// If there is no element data, we have nothing to display.
|
|
|
68 |
if (empty($this->get_data())) {
|
|
|
69 |
return;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$imageinfo = json_decode($this->get_data());
|
|
|
73 |
|
|
|
74 |
// If there is no file, we have nothing to display.
|
|
|
75 |
if (empty($imageinfo->filename)) {
|
|
|
76 |
return;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
if ($file = $this->get_file()) {
|
|
|
80 |
$location = make_request_directory() . '/target';
|
|
|
81 |
$file->copy_content_to($location);
|
|
|
82 |
|
|
|
83 |
// Set the image to the size of the PDF page.
|
|
|
84 |
$mimetype = $file->get_mimetype();
|
|
|
85 |
if ($mimetype == 'image/svg+xml') {
|
|
|
86 |
$pdf->ImageSVG($location, 0, 0, $pdf->getPageWidth(), $pdf->getPageHeight());
|
|
|
87 |
} else {
|
|
|
88 |
$pdf->Image($location, 0, 0, $pdf->getPageWidth(), $pdf->getPageHeight());
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Render the element in html.
|
|
|
95 |
*
|
|
|
96 |
* This function is used to render the element when we are using the
|
|
|
97 |
* drag and drop interface to position it.
|
|
|
98 |
*
|
|
|
99 |
* @return string the html
|
|
|
100 |
*/
|
|
|
101 |
public function render_html() {
|
|
|
102 |
global $DB;
|
|
|
103 |
|
|
|
104 |
// If there is no element data, we have nothing to display.
|
|
|
105 |
if (empty($this->get_data())) {
|
|
|
106 |
return '';
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
$imageinfo = json_decode($this->get_data());
|
|
|
110 |
|
|
|
111 |
// If there is no file, we have nothing to display.
|
|
|
112 |
if (empty($imageinfo->filename)) {
|
|
|
113 |
return '';
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if ($file = $this->get_file()) {
|
|
|
117 |
$url = \moodle_url::make_pluginfile_url($file->get_contextid(), 'mod_customcert', 'image', $file->get_itemid(),
|
|
|
118 |
$file->get_filepath(), $file->get_filename());
|
|
|
119 |
// Get the page we are rendering this on.
|
|
|
120 |
$page = $DB->get_record('customcert_pages', ['id' => $this->get_pageid()], '*', MUST_EXIST);
|
|
|
121 |
|
|
|
122 |
// Set the image to the size of the page.
|
|
|
123 |
$style = 'width: ' . $page->width . 'mm; height: ' . $page->height . 'mm';
|
|
|
124 |
return \html_writer::tag('img', '', ['src' => $url, 'style' => $style]);
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
|