diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..48b8bf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor/ diff --git a/README.md b/README.md index e4b870d..5dcf2f7 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ This activity allows the dynamic generation of PDF certificates with complete cu There are two installation methods that are available. +First, make sure that wkhtmltopdf is installed on your server and available on $PATH. + Follow one of these, then log into your Moodle site as an administrator and visit the notifications page to complete the install. ### Git @@ -36,4 +38,4 @@ Visit the [wiki page](https://docs.moodle.org/en/Custom_certificate_module "Wiki ## License -Licensed under the [GNU GPL License](http://www.gnu.org/copyleft/gpl.html). \ No newline at end of file +Licensed under the [GNU GPL License](http://www.gnu.org/copyleft/gpl.html). diff --git a/classes/.template.php.swp b/classes/.template.php.swp new file mode 100644 index 0000000..1b4914e Binary files /dev/null and b/classes/.template.php.swp differ diff --git a/classes/template.php b/classes/template.php index 595fc70..72a4720 100644 --- a/classes/template.php +++ b/classes/template.php @@ -24,7 +24,12 @@ namespace mod_customcert; -defined('MOODLE_INTERNAL') || die(); +error_reporting(E_ALL); +require_once(__DIR__ . "/../vendor/autoload.php"); + +use mikehaertl\wkhtmlto\Pdf; + +#defined('MOODLE_INTERNAL') || die(); /** * Class represents a customcert template. @@ -257,7 +262,7 @@ class template { * @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; + global $CFG, $DB, $USER, $SITE; if (empty($userid)) { $user = $USER; @@ -315,7 +320,65 @@ class template { $pdf->AddPage($orientation, array($page->width, $page->height)); if ($this->html) { - $pdf->WriteHTML($this->html); + $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(); + } else if ($preview) { + } + $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_customcert\certificate::generate_code(); + } else { + $issue = $DB->get_record('customcert_issues', array('userid' => $user->id, 'customcertid' => $customcert->id), + '*', IGNORE_MULTIPLE); + $code = $issue->code; + } + + $html = str_replace("__CERTNUM__", $code, $html); + + if ($preview) { + $courseid = $SITE->id; + } else { + $courseid = $customcert->course; + } + + $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); + + $pdf->addPage($html); + $pdf->send(); + die($pdf->getError()); } else { $pdf->SetMargins($page->leftmargin, 0, $page->rightmargin); // Get the elements for the page. @@ -331,11 +394,13 @@ class template { } } - if ($return) { + if ($return && !$this->html) { return $pdf->Output('', 'S'); } - $pdf->Output($filename, $deliveryoption); + if (!$this->html) { + $pdf->Output($filename, $deliveryoption); + } } } @@ -347,8 +412,9 @@ class template { public function copy_to_template($copytotemplateid) { global $DB; - $copytotemplate = $DB->get_records('customcert_templates', array('id' => $copytotemplateid)); + $copytotemplate = $DB->get_record('customcert_templates', array('id' => $copytotemplateid)); $copytotemplate->html = $this->html; + $DB->update_record('customcert_templates', $copytotemplate); // Get the pages for the template, there should always be at least one page for each template. if ($templatepages = $DB->get_records('customcert_pages', array('templateid' => $this->id))) { diff --git a/composer.json b/composer.json index f072256..d609b46 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,8 @@ "name": "mdjnelson/moodle-mod_customcert", "type": "moodle-mod", "require": { - "composer/installers": "~1.0" + "composer/installers": "~1.0", + "mikehaertl/phpwkhtmltopdf": "^2.5" }, "extra": { "installer-name": "customcert" diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..5d6fad3 --- /dev/null +++ b/composer.lock @@ -0,0 +1,311 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "749e39445f7c33f767b710400da4e9c6", + "packages": [ + { + "name": "composer/installers", + "version": "v1.12.0", + "source": { + "type": "git", + "url": "https://github.com/composer/installers.git", + "reference": "d20a64ed3c94748397ff5973488761b22f6d3f19" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/composer/installers/zipball/d20a64ed3c94748397ff5973488761b22f6d3f19", + "reference": "d20a64ed3c94748397ff5973488761b22f6d3f19", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^1.0 || ^2.0" + }, + "replace": { + "roundcube/plugin-installer": "*", + "shama/baton": "*" + }, + "require-dev": { + "composer/composer": "1.6.* || ^2.0", + "composer/semver": "^1 || ^3", + "phpstan/phpstan": "^0.12.55", + "phpstan/phpstan-phpunit": "^0.12.16", + "symfony/phpunit-bridge": "^4.2 || ^5", + "symfony/process": "^2.3" + }, + "type": "composer-plugin", + "extra": { + "class": "Composer\\Installers\\Plugin", + "branch-alias": { + "dev-main": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Composer\\Installers\\": "src/Composer/Installers" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Kyle Robinson Young", + "email": "kyle@dontkry.com", + "homepage": "https://github.com/shama" + } + ], + "description": "A multi-framework Composer library installer", + "homepage": "https://composer.github.io/installers/", + "keywords": [ + "Craft", + "Dolibarr", + "Eliasis", + "Hurad", + "ImageCMS", + "Kanboard", + "Lan Management System", + "MODX Evo", + "MantisBT", + "Mautic", + "Maya", + "OXID", + "Plentymarkets", + "Porto", + "RadPHP", + "SMF", + "Starbug", + "Thelia", + "Whmcs", + "WolfCMS", + "agl", + "aimeos", + "annotatecms", + "attogram", + "bitrix", + "cakephp", + "chef", + "cockpit", + "codeigniter", + "concrete5", + "croogo", + "dokuwiki", + "drupal", + "eZ Platform", + "elgg", + "expressionengine", + "fuelphp", + "grav", + "installer", + "itop", + "joomla", + "known", + "kohana", + "laravel", + "lavalite", + "lithium", + "magento", + "majima", + "mako", + "mediawiki", + "miaoxing", + "modulework", + "modx", + "moodle", + "osclass", + "pantheon", + "phpbb", + "piwik", + "ppi", + "processwire", + "puppet", + "pxcms", + "reindex", + "roundcube", + "shopware", + "silverstripe", + "sydes", + "sylius", + "symfony", + "tastyigniter", + "typo3", + "wordpress", + "yawik", + "zend", + "zikula" + ], + "support": { + "issues": "https://github.com/composer/installers/issues", + "source": "https://github.com/composer/installers/tree/v1.12.0" + }, + "funding": [ + { + "url": "https://packagist.com", + "type": "custom" + }, + { + "url": "https://github.com/composer", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/composer/composer", + "type": "tidelift" + } + ], + "time": "2021-09-13T08:19:44+00:00" + }, + { + "name": "mikehaertl/php-shellcommand", + "version": "1.6.4", + "source": { + "type": "git", + "url": "https://github.com/mikehaertl/php-shellcommand.git", + "reference": "3488d7803df1e8f1a343d3d0ca452d527ad8d5e5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mikehaertl/php-shellcommand/zipball/3488d7803df1e8f1a343d3d0ca452d527ad8d5e5", + "reference": "3488d7803df1e8f1a343d3d0ca452d527ad8d5e5", + "shasum": "" + }, + "require": { + "php": ">= 5.3.0" + }, + "require-dev": { + "phpunit/phpunit": ">4.0 <=9.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "mikehaertl\\shellcommand\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Härtl", + "email": "haertl.mike@gmail.com" + } + ], + "description": "An object oriented interface to shell commands", + "keywords": [ + "shell" + ], + "support": { + "issues": "https://github.com/mikehaertl/php-shellcommand/issues", + "source": "https://github.com/mikehaertl/php-shellcommand/tree/1.6.4" + }, + "time": "2021-03-17T06:54:33+00:00" + }, + { + "name": "mikehaertl/php-tmpfile", + "version": "1.2.1", + "source": { + "type": "git", + "url": "https://github.com/mikehaertl/php-tmpfile.git", + "reference": "70a5b70b17bc0d9666388e6a551ecc93d0b40a10" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mikehaertl/php-tmpfile/zipball/70a5b70b17bc0d9666388e6a551ecc93d0b40a10", + "reference": "70a5b70b17bc0d9666388e6a551ecc93d0b40a10", + "shasum": "" + }, + "require-dev": { + "php": ">=5.3.0", + "phpunit/phpunit": ">4.0 <=9.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "mikehaertl\\tmp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Härtl", + "email": "haertl.mike@gmail.com" + } + ], + "description": "A convenience class for temporary files", + "keywords": [ + "files" + ], + "support": { + "issues": "https://github.com/mikehaertl/php-tmpfile/issues", + "source": "https://github.com/mikehaertl/php-tmpfile/tree/1.2.1" + }, + "time": "2021-03-01T18:26:25+00:00" + }, + { + "name": "mikehaertl/phpwkhtmltopdf", + "version": "2.5.0", + "source": { + "type": "git", + "url": "https://github.com/mikehaertl/phpwkhtmltopdf.git", + "reference": "17ee71341591415d942774eda2c98d8ba7ea9e90" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mikehaertl/phpwkhtmltopdf/zipball/17ee71341591415d942774eda2c98d8ba7ea9e90", + "reference": "17ee71341591415d942774eda2c98d8ba7ea9e90", + "shasum": "" + }, + "require": { + "mikehaertl/php-shellcommand": "^1.5.0", + "mikehaertl/php-tmpfile": "^1.2.1", + "php": ">=5.0.0" + }, + "require-dev": { + "phpunit/phpunit": ">4.0 <9.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "mikehaertl\\wkhtmlto\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Haertl", + "email": "haertl.mike@gmail.com" + } + ], + "description": "A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface", + "homepage": "http://mikehaertl.github.com/phpwkhtmltopdf/", + "keywords": [ + "pdf", + "wkhtmltoimage", + "wkhtmltopdf" + ], + "support": { + "issues": "https://github.com/mikehaertl/phpwkhtmltopdf/issues", + "source": "https://github.com/mikehaertl/phpwkhtmltopdf/tree/2.5.0" + }, + "time": "2021-03-01T19:41:06+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.0.0" +}