283 lines
7.7 KiB
PHP
283 lines
7.7 KiB
PHP
<?php
|
|
// This file is part of 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
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
/**
|
|
* Class represents a htmlcert template.
|
|
*
|
|
* @package mod_htmlcert
|
|
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
|
|
namespace mod_htmlcert;
|
|
|
|
require_once(__DIR__ . "/../vendor/autoload.php");
|
|
|
|
use mikehaertl\wkhtmlto\Pdf;
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
/**
|
|
* Class represents a htmlcert template.
|
|
*
|
|
* @package mod_htmlcert
|
|
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
class template {
|
|
|
|
/**
|
|
* @var int $id The id of the template.
|
|
*/
|
|
protected $id;
|
|
|
|
/**
|
|
* @var string $name The name of this template
|
|
*/
|
|
protected $name;
|
|
|
|
/**
|
|
* @var int $contextid The context id of this template
|
|
*/
|
|
protected $contextid;
|
|
|
|
protected $html;
|
|
|
|
/**
|
|
* The constructor.
|
|
*
|
|
* @param \stdClass $template
|
|
*/
|
|
public function __construct($template) {
|
|
$this->id = $template->id;
|
|
$this->name = $template->name;
|
|
$this->contextid = $template->contextid;
|
|
$this->html = $template->html;
|
|
}
|
|
|
|
/**
|
|
* Handles saving data.
|
|
*
|
|
* @param \stdClass $data the template data
|
|
*/
|
|
public function save($data) {
|
|
global $DB;
|
|
|
|
$savedata = new \stdClass();
|
|
$savedata->id = $this->id;
|
|
$savedata->name = $data->name;
|
|
$savedata->html = $data->html;
|
|
$savedata->timemodified = time();
|
|
|
|
$DB->update_record('htmlcert_templates', $savedata);
|
|
}
|
|
|
|
/**
|
|
* Handles deleting the template.
|
|
*
|
|
* @return bool return true if the deletion was successful, false otherwise
|
|
*/
|
|
public function delete() {
|
|
global $DB;
|
|
|
|
if (!$DB->delete_records('htmlcert_templates', array('id' => $this->id))) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Generate the PDF for the template.
|
|
*
|
|
* @param bool $preview true if it is a preview, false otherwise
|
|
* @param int $userid the id of the user whose certificate we want to view
|
|
* @param bool $return Do we want to return the contents of the PDF?
|
|
* @return string|void Can return the PDF in string format if specified.
|
|
*/
|
|
public function generate_pdf(bool $preview = false, int $userid = null, bool $return = false) {
|
|
global $CFG, $DB, $USER;
|
|
|
|
if (empty($userid)) {
|
|
$user = $USER;
|
|
} else {
|
|
$user = \core_user::get_user($userid);
|
|
}
|
|
|
|
$htmlcert = $DB->get_record('htmlcert', ['templateid' => $this->id]);
|
|
|
|
$pdf = new Pdf(array(
|
|
"disable-smart-shrinking",
|
|
"margin-bottom" => "0",
|
|
"margin-right" => "0",
|
|
"margin-left" => "0",
|
|
"margin-top" => "0"
|
|
));
|
|
|
|
$html = $this->html;
|
|
|
|
$context = \context_user::instance($user->id);
|
|
$fs = get_file_storage();
|
|
$files = $fs->get_area_files($context->id, 'user', 'icon', 0);
|
|
|
|
$file = null;
|
|
|
|
$content = "";
|
|
foreach ($files as $filefound) {
|
|
if (!$filefound->is_directory()) {
|
|
$file = $filefound;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($file) {
|
|
$location = make_request_directory() . '/target';
|
|
$content = $file->get_content();
|
|
}
|
|
|
|
$html = str_replace("__PROFILEPIC__", 'data: ' . mime_content_type($file) . ';base64,' . $content, $html);
|
|
|
|
$html = str_replace("__NAME__", $user->firstname . " " . $user->lastname, $html);
|
|
|
|
if ($preview) {
|
|
$code = \mod_htmlcert\certificate::generate_code();
|
|
} else {
|
|
$issue = $DB->get_record('htmlcert_issues', array('userid' => $user->id, 'htmlcertid' => $htmlcert->id),
|
|
'*', IGNORE_MULTIPLE);
|
|
$code = $issue->code;
|
|
}
|
|
|
|
$html = str_replace("__CERTNUM__", $code, $html);
|
|
|
|
$courseid = $htmlcert->course || $SITE->id;
|
|
|
|
$course = get_course($courseid);
|
|
$coursename = $course->fullname;
|
|
|
|
$html = str_replace("__COURSE__", $coursename, $html);
|
|
|
|
$date = $issue->timecreated;
|
|
|
|
$html = str_replace("__DATE__", userdate($date, '%B %d, %Y'), $html);
|
|
|
|
$html = str_replace("__PIN__", $user->profile["pin"], $html);
|
|
|
|
$pdf->addPage($html);
|
|
$pdf->send();
|
|
die($pdf->getError());
|
|
|
|
if ($return) {
|
|
return $pdf->toString();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handles copying this template into another.
|
|
*
|
|
* @param int $copytotemplateid The template id to copy to
|
|
*/
|
|
public function copy_to_template($copytotemplateid) {
|
|
global $DB;
|
|
|
|
$copytotemplate = $DB->get_record('htmlcert_templates', array('id' => $copytotemplateid));
|
|
$copytotemplate->html = $this->html;
|
|
$DB->update_record('htmlcert_templates', $copytotemplate);
|
|
|
|
}
|
|
|
|
/**
|
|
* Returns the id of the template.
|
|
*
|
|
* @return int the id of the template
|
|
*/
|
|
public function get_id() {
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
* Returns the name of the template.
|
|
*
|
|
* @return string the name of the template
|
|
*/
|
|
public function get_name() {
|
|
return $this->name;
|
|
}
|
|
|
|
public function get_html() {
|
|
return $this->html;
|
|
}
|
|
|
|
/**
|
|
* Returns the context id.
|
|
*
|
|
* @return int the context id
|
|
*/
|
|
public function get_contextid() {
|
|
return $this->contextid;
|
|
}
|
|
|
|
/**
|
|
* Returns the context id.
|
|
*
|
|
* @return \context the context
|
|
*/
|
|
public function get_context() {
|
|
return \context::instance_by_id($this->contextid);
|
|
}
|
|
|
|
/**
|
|
* Returns the context id.
|
|
*
|
|
* @return \context_module|null the context module, null if there is none
|
|
*/
|
|
public function get_cm() {
|
|
$context = $this->get_context();
|
|
if ($context->contextlevel === CONTEXT_MODULE) {
|
|
return get_coursemodule_from_id('htmlcert', $context->instanceid, 0, false, MUST_EXIST);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Ensures the user has the proper capabilities to manage this template.
|
|
*
|
|
* @throws \required_capability_exception if the user does not have the necessary capabilities (ie. Fred)
|
|
*/
|
|
public function require_manage() {
|
|
require_capability('mod/htmlcert:manage', $this->get_context());
|
|
}
|
|
|
|
/**
|
|
* Creates a template.
|
|
*
|
|
* @param string $templatename the name of the template
|
|
* @param int $contextid the context id
|
|
* @return \mod_htmlcert\template the template object
|
|
*/
|
|
public static function create($templatename, $contextid) {
|
|
global $DB;
|
|
|
|
$template = new \stdClass();
|
|
$template->name = $templatename;
|
|
$template->contextid = $contextid;
|
|
$template->timecreated = time();
|
|
$template->timemodified = $template->timecreated;
|
|
$template->id = $DB->insert_record('htmlcert_templates', $template);
|
|
|
|
return new \mod_htmlcert\template($template);
|
|
}
|
|
}
|