2016-02-16 09:03:34 +00:00
|
|
|
<?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.
|
2016-02-16 09:03:34 +00:00
|
|
|
*
|
2021-11-24 07:29:43 +00:00
|
|
|
* @package mod_htmlcert
|
|
|
|
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
|
2016-02-16 09:03:34 +00:00
|
|
|
* @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");
|
|
|
|
|
|
|
|
use mikehaertl\wkhtmlto\Pdf;
|
2016-02-16 09:03:34 +00:00
|
|
|
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
|
|
|
/**
|
2021-11-24 07:29:43 +00:00
|
|
|
* Class represents a htmlcert template.
|
2016-02-16 09:03:34 +00:00
|
|
|
*
|
2021-11-24 07:29:43 +00:00
|
|
|
* @package mod_htmlcert
|
|
|
|
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
|
2016-02-16 09:03:34 +00:00
|
|
|
* @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;
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2016-02-16 09:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2021-11-24 12:30:19 +00:00
|
|
|
$savedata->html = $data->html || $this->html;
|
2017-02-16 12:12:19 +00:00
|
|
|
$savedata->timemodified = time();
|
2016-02-16 09:03:34 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
$DB->update_record('htmlcert_templates', $savedata);
|
2016-02-16 09:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2016-02-16 09:03:34 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
if (!$DB->delete_records('htmlcert_templates', array('id' => $this->id))) {
|
2016-02-16 09:03:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the PDF for the template.
|
|
|
|
*
|
|
|
|
* @param bool $preview true if it is a preview, false otherwise
|
2016-08-23 08:22:58 +00:00
|
|
|
* @param int $userid the id of the user whose certificate we want to view
|
2017-02-02 09:17:41 +00:00
|
|
|
* @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.
|
2016-02-16 09:03:34 +00:00
|
|
|
*/
|
2021-04-05 08:22:33 +00:00
|
|
|
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;
|
2016-08-23 08:22:58 +00:00
|
|
|
|
|
|
|
if (empty($userid)) {
|
|
|
|
$user = $USER;
|
|
|
|
} else {
|
|
|
|
$user = \core_user::get_user($userid);
|
|
|
|
}
|
2016-02-16 09:03:34 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
$htmlcert = $DB->get_record('htmlcert', ['templateid' => $this->id]);
|
2016-02-16 09:03:34 +00:00
|
|
|
|
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"
|
|
|
|
));
|
2016-02-16 09:03:34 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
$html = $this->html;
|
2021-04-05 08:22:33 +00:00
|
|
|
|
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-04-05 08:22:33 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
$content = "";
|
|
|
|
foreach ($files as $filefound) {
|
|
|
|
if (!$filefound->is_directory()) {
|
|
|
|
$file = $filefound;
|
|
|
|
break;
|
2016-02-16 09:03:34 +00:00
|
|
|
}
|
2021-11-24 07:29:43 +00:00
|
|
|
}
|
2016-02-16 09:03:34 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
if ($file) {
|
|
|
|
$location = make_request_directory() . '/target';
|
|
|
|
$content = $file->get_content();
|
|
|
|
}
|
2021-06-29 21:10:52 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
$html = str_replace("__PROFILEPIC__", 'data: ' . mime_content_type($file) . ';base64,' . $content, $html);
|
2020-09-20 13:39:53 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
$html = str_replace("__NAME__", $user->firstname . " " . $user->lastname, $html);
|
2020-09-20 13:39:53 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2020-09-20 13:39:53 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
$html = str_replace("__CERTNUM__", $code, $html);
|
2020-09-20 13:39:53 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
$courseid = $htmlcert->course || $SITE->id;
|
|
|
|
|
|
|
|
$course = get_course($courseid);
|
|
|
|
$coursename = $course->fullname;
|
|
|
|
|
|
|
|
$html = str_replace("__COURSE__", $coursename, $html);
|
|
|
|
|
|
|
|
$date = $issue->timecreated;
|
2020-09-20 13:39:53 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
$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();
|
2016-02-16 09:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 11:50:42 +00:00
|
|
|
/**
|
2017-09-04 10:33:37 +00:00
|
|
|
* Handles copying this template into another.
|
2017-09-03 11:50:42 +00:00
|
|
|
*
|
2017-09-04 10:33:37 +00:00
|
|
|
* @param int $copytotemplateid The template id to copy to
|
2017-09-03 11:50:42 +00:00
|
|
|
*/
|
2017-09-04 10:33:37 +00:00
|
|
|
public function copy_to_template($copytotemplateid) {
|
2017-09-03 11:50:42 +00:00
|
|
|
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);
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
2016-02-16 09:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2016-02-16 09:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2016-02-16 09:03:34 +00:00
|
|
|
*/
|
|
|
|
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);
|
2016-02-16 09:03:34 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
return new \mod_htmlcert\template($template);
|
2016-02-16 09:03:34 +00:00
|
|
|
}
|
|
|
|
}
|