. /** * Class represents a htmlcert template. * * @package mod_htmlcert * @copyright 2016 Mark Nelson , 2021-2022 Kumi Systems e.U. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace mod_htmlcert; require_once(__DIR__ . "/../vendor/autoload.php"); require_once($CFG->dirroot.'/user/profile/lib.php'); use mikehaertl\wkhtmlto\Pdf; defined('MOODLE_INTERNAL') || die(); /** * Class represents a htmlcert template. * * @package mod_htmlcert * @copyright 2016 Mark Nelson , 2021-2022 Kumi Systems e.U. * @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 ?: $this->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, $SITE; if (empty($userid)) { $user = $USER; } else { $user = \core_user::get_user($userid); } profile_load_data($user); $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->username, $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); } }