From 8446a2e10ac7f662438b47d1f4b6e2bad4c6a1d4 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 5 Apr 2021 16:22:33 +0800 Subject: [PATCH] Add ability to choose how to deliver the certificate (#401) --- classes/certificate.php | 10 ++++++++++ classes/template.php | 20 +++++++++++++------- db/install.xml | 1 + db/upgrade.php | 12 ++++++++++++ lang/en/customcert.php | 3 +++ mod_form.php | 27 +++++++++++++-------------- version.php | 2 +- 7 files changed, 53 insertions(+), 22 deletions(-) diff --git a/classes/certificate.php b/classes/certificate.php index 0dd842e..39168dd 100644 --- a/classes/certificate.php +++ b/classes/certificate.php @@ -37,6 +37,16 @@ defined('MOODLE_INTERNAL') || die(); */ class certificate { + /** + * Send the file inline to the browser. + */ + const DELIVERY_OPTION_INLINE = 'I'; + + /** + * Send to the browser and force a file download + */ + const DELIVERY_OPTION_DOWNLOAD = 'D'; + /** * @var string the print protection variable */ diff --git a/classes/template.php b/classes/template.php index 6bf8ac0..10be74c 100644 --- a/classes/template.php +++ b/classes/template.php @@ -253,7 +253,7 @@ class template { * @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($preview = false, $userid = null, $return = false) { + public function generate_pdf(bool $preview = false, int $userid = null, bool $return = false) { global $CFG, $DB, $USER; if (empty($userid)) { @@ -269,12 +269,18 @@ class template { // Create the pdf object. $pdf = new \pdf(); + $customcert = $DB->get_record('customcert', ['templateid' => $this->id]); + // If the template belongs to a certificate then we need to check what permissions we set for it. - if ($protection = $DB->get_field('customcert', 'protection', array('templateid' => $this->id))) { - if (!empty($protection)) { - $protection = explode(', ', $protection); - $pdf->SetProtection($protection); - } + $protection = $customcert->protection; + if (!empty($protection)) { + $protection = explode(', ', $protection); + $pdf->SetProtection($protection); + } + + $deliveryoption = $customcert->deliveryoption; + if (empty($deliveryoption)) { + $deliveryoption = certificate::DELIVERY_OPTION_INLINE; } $pdf->setPrintHeader(false); @@ -320,7 +326,7 @@ class template { return $pdf->Output('', 'S'); } - $pdf->Output($filename, 'I'); + $pdf->Output($filename, $deliveryoption); } } diff --git a/db/install.xml b/db/install.xml index f6ac796..22fe3ec 100644 --- a/db/install.xml +++ b/db/install.xml @@ -14,6 +14,7 @@ + diff --git a/db/upgrade.php b/db/upgrade.php index 57d2b4f..fb22193 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -166,5 +166,17 @@ function xmldb_customcert_upgrade($oldversion) { upgrade_mod_savepoint(true, 2019111803, 'customcert'); } + if ($oldversion < 2020110901) { + $table = new xmldb_table('customcert'); + $field = new xmldb_field('deliveryoption', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'verifyany'); + + // Conditionally launch add field. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + upgrade_mod_savepoint(true, 2020110901, 'customcert'); + } + return true; } diff --git a/lang/en/customcert.php b/lang/en/customcert.php index 8e2fc43..7ed7b9a 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -57,6 +57,9 @@ $string['deleteissueconfirm'] = 'Are you sure you want to delete this certificat $string['deleteissuedcertificates'] = 'Delete issued certificates'; $string['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?'; $string['deletetemplateconfirm'] = 'Are you sure you want to delete this certificate template?'; +$string['deliveryoptiondownload'] = 'Send to the browser and force a file download'; +$string['deliveryoptioninline'] = 'Send the file inline to the browser'; +$string['deliveryoptions'] = 'Delivery options'; $string['description'] = 'Description'; $string['duplicate'] = 'Duplicate'; $string['duplicateconfirm'] = 'Duplicate confirmation'; diff --git a/mod_form.php b/mod_form.php index fb023a4..e82c4a5 100644 --- a/mod_form.php +++ b/mod_form.php @@ -22,6 +22,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use mod_customcert\certificate; + defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); require_once($CFG->dirroot.'/course/moodleform_mod.php'); @@ -55,14 +57,20 @@ class mod_customcert_mod_form extends moodleform_mod { $this->standard_intro_elements(get_string('description', 'customcert')); - $optionsheader = $mform->createElement('header', 'options', get_string('options', 'customcert')); + $mform->addElement('header', 'options', get_string('options', 'customcert')); + + $deliveryoptions = [ + certificate::DELIVERY_OPTION_INLINE => get_string('deliveryoptioninline', 'customcert'), + certificate::DELIVERY_OPTION_DOWNLOAD => get_string('deliveryoptiondownload', 'customcert') + ]; + $mform->addElement('select', 'deliveryoption', get_string('deliveryoptions', 'customcert'), $deliveryoptions); + $mform->setDefault('deliveryoption', certificate::DELIVERY_OPTION_INLINE); if (has_capability('mod/customcert:manageemailstudents', $this->get_context())) { $mform->addElement('selectyesno', 'emailstudents', get_string('emailstudents', 'customcert')); $mform->setDefault('emailstudents', get_config('customcert', 'emailstudents')); $mform->addHelpButton('emailstudents', 'emailstudents', 'customcert'); $mform->setType('emailstudents', PARAM_INT); - $firstoption = 'emailstudents'; } if (has_capability('mod/customcert:manageemailteachers', $this->get_context())) { @@ -70,7 +78,6 @@ class mod_customcert_mod_form extends moodleform_mod { $mform->setDefault('emailteachers', get_config('customcert', 'emailteachers')); $mform->addHelpButton('emailteachers', 'emailteachers', 'customcert'); $mform->setType('emailteachers', PARAM_INT); - $firstoption = empty($firstoption) ? 'emailteachers' : $firstoption; } if (has_capability('mod/customcert:manageemailothers', $this->get_context())) { @@ -78,7 +85,6 @@ class mod_customcert_mod_form extends moodleform_mod { $mform->addHelpButton('emailothers', 'emailothers', 'customcert'); $mform->setDefault('emailothers', get_config('customcert', 'emailothers')); $mform->setType('emailothers', PARAM_TEXT); - $firstoption = empty($firstoption) ? 'emailothers' : $firstoption; } if (has_capability('mod/customcert:manageverifyany', $this->get_context())) { @@ -86,7 +92,6 @@ class mod_customcert_mod_form extends moodleform_mod { $mform->addHelpButton('verifyany', 'verifycertificateanyone', 'customcert'); $mform->setDefault('verifyany', get_config('customcert', 'verifyany')); $mform->setType('verifyany', PARAM_INT); - $firstoption = empty($firstoption) ? 'verifyany' : $firstoption; } if (has_capability('mod/customcert:managerequiredtime', $this->get_context())) { @@ -94,7 +99,6 @@ class mod_customcert_mod_form extends moodleform_mod { $mform->addHelpButton('requiredtime', 'coursetimereq', 'customcert'); $mform->setDefault('requiredtime', get_config('customcert', 'requiredtime')); $mform->setType('requiredtime', PARAM_INT); - $firstoption = empty($firstoption) ? 'requiredtime' : $firstoption; } if (has_capability('mod/customcert:manageprotection', $this->get_context())) { @@ -106,11 +110,6 @@ class mod_customcert_mod_form extends moodleform_mod { $mform->setType('protection_print', PARAM_BOOL); $mform->setType('protection_modify', PARAM_BOOL); $mform->setType('protection_copy', PARAM_BOOL); - $firstoption = empty($firstoption) ? 'protection_print' : $firstoption; - } - - if (!empty($firstoption)) { - $mform->insertElementBefore($optionsheader, $firstoption); } $this->standard_coursemodule_elements(); @@ -227,13 +226,13 @@ class mod_customcert_mod_form extends moodleform_mod { $protection = explode(', ', $protection); - if (in_array(\mod_customcert\certificate::PROTECTION_PRINT, $protection)) { + if (in_array(certificate::PROTECTION_PRINT, $protection)) { $data->protection_print = 1; } - if (in_array(\mod_customcert\certificate::PROTECTION_MODIFY, $protection)) { + if (in_array(certificate::PROTECTION_MODIFY, $protection)) { $data->protection_modify = 1; } - if (in_array(\mod_customcert\certificate::PROTECTION_COPY, $protection)) { + if (in_array(certificate::PROTECTION_COPY, $protection)) { $data->protection_copy = 1; } diff --git a/version.php b/version.php index 5459d2f..da19e75 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); -$plugin->version = 2020110900; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2020110901; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2020110900; // Requires this Moodle version (3.10). $plugin->cron = 0; // Period for cron to check this module (secs). $plugin->component = 'mod_customcert';