moodle-mod_htmlcert/classes/template.php

287 lines
7.8 KiB
PHP
Raw Permalink Normal View History

<?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/>.
/**
2021-11-24 07:29:43 +00:00
* Class represents a htmlcert template.
*
2021-11-24 07:29:43 +00:00
* @package mod_htmlcert
2022-09-07 13:59:29 +00:00
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2021-2022 Kumi Systems e.U. <office@kumi.systems>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
2021-11-24 07:29:43 +00:00
namespace mod_htmlcert;
require_once(__DIR__ . "/../vendor/autoload.php");
2021-11-24 15:29:16 +00:00
require_once($CFG->dirroot.'/user/profile/lib.php');
2021-11-24 07:29:43 +00:00
use mikehaertl\wkhtmlto\Pdf;
defined('MOODLE_INTERNAL') || die();
/**
2021-11-24 07:29:43 +00:00
* Class represents a htmlcert template.
*
2021-11-24 07:29:43 +00:00
* @package mod_htmlcert
2022-09-07 13:59:29 +00:00
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2021-2022 Kumi Systems e.U. <office@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;
2021-11-24 07:29:43 +00:00
protected $html;
/**
* The constructor.
*
* @param \stdClass $template
*/
public function __construct($template) {
$this->id = $template->id;
$this->name = $template->name;
$this->contextid = $template->contextid;
2021-11-24 07:29:43 +00:00
$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 ?: $this->html;
2017-02-16 12:12:19 +00:00
$savedata->timemodified = time();
2021-11-24 07:29:43 +00:00
$DB->update_record('htmlcert_templates', $savedata);
}
/**
* Handles deleting the template.
*
* @return bool return true if the deletion was successful, false otherwise
*/
public function delete() {
2016-08-24 04:36:26 +00:00
global $DB;
2021-11-24 07:29:43 +00:00
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) {
2021-11-24 08:36:12 +00:00
global $CFG, $DB, $USER, $SITE;
if (empty($userid)) {
$user = $USER;
} else {
$user = \core_user::get_user($userid);
}
2022-09-07 13:59:29 +00:00
profile_load_data($user);
2021-11-24 15:29:16 +00:00
2021-11-24 07:29:43 +00:00
$htmlcert = $DB->get_record('htmlcert', ['templateid' => $this->id]);
2021-11-24 07:29:43 +00:00
$pdf = new Pdf(array(
"disable-smart-shrinking",
"margin-bottom" => "0",
"margin-right" => "0",
"margin-left" => "0",
"margin-top" => "0"
));
2021-11-24 07:29:43 +00:00
$html = $this->html;
2021-11-24 07:29:43 +00:00
$context = \context_user::instance($user->id);
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'user', 'icon', 0);
$file = null;
2021-11-24 07:29:43 +00:00
$content = "";
foreach ($files as $filefound) {
if (!$filefound->is_directory()) {
$file = $filefound;
break;
}
2021-11-24 07:29:43 +00:00
}
2021-11-24 07:29:43 +00:00
if ($file) {
$location = make_request_directory() . '/target';
$content = $file->get_content();
}
2021-11-24 07:29:43 +00:00
$html = str_replace("__PROFILEPIC__", 'data: ' . mime_content_type($file) . ';base64,' . $content, $html);
2021-11-24 07:29:43 +00:00
$html = str_replace("__NAME__", $user->firstname . " " . $user->lastname, $html);
2021-11-24 07:29:43 +00:00
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;
}
2021-11-24 07:29:43 +00:00
$html = str_replace("__CERTNUM__", $code, $html);
2022-09-07 13:59:29 +00:00
$courseid = $htmlcert->course ?: $SITE->id;
2021-11-24 07:29:43 +00:00
$course = get_course($courseid);
$coursename = $course->fullname;
$html = str_replace("__COURSE__", $coursename, $html);
$date = $issue->timecreated;
2021-11-24 07:29:43 +00:00
$html = str_replace("__DATE__", userdate($date, '%B %d, %Y'), $html);
2022-09-07 13:59:29 +00:00
$html = str_replace("__PIN__", $user->username, $html);
2021-11-24 07:29:43 +00:00
$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;
2021-11-24 07:29:43 +00:00
$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;
}
2021-11-24 07:29:43 +00:00
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) {
2021-11-24 07:29:43 +00:00
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() {
2021-11-24 07:29:43 +00:00
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
2021-11-24 07:29:43 +00:00
* @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;
2021-11-24 07:29:43 +00:00
$template->id = $DB->insert_record('htmlcert_templates', $template);
2021-11-24 07:29:43 +00:00
return new \mod_htmlcert\template($template);
}
}