/** * Form definition. */ public function definition() { $mform =& $this->_form; $element = $this->_customdata['element']; // Add the field for the name of the element, this is required for all elements. $mform->addElement('text', 'name', get_string('elementname', 'customcert')); $mform->setType('name', PARAM_TEXT); $mform->setDefault('name', get_string('pluginname', 'customcertelement_' . $element->element)); $mform->addRule('name', get_string('required'), 'required', null, 'client'); $mform->addHelpButton('name', 'elementname', 'customcert'); $this->element = customcert_get_element_instance($element); $this->element->render_form_elements($mform); $this->add_action_buttons(true); }
$style = 'height: ' . $page->height . 'mm; line-height: normal;'; if ($page->margin) { $style .= 'width: ' . ($page->width - $page->margin) . 'mm;'; $style .= 'background-image: url(' . new moodle_url('/mod/customcert/pix/dash') . ');'; $style .= 'background-repeat: repeat-y;'; $style .= 'background-position-x: ' . ($page->width - $page->margin) . 'mm;'; $style .= 'padding-right: ' . $page->margin . 'mm;'; } else { $style .= 'width: ' . $page->width . 'mm;'; } // Create the div that represents the PDF. $html .= html_writer::start_tag('div', array('id' => 'pdf', 'style' => $style)); if ($elements) { foreach ($elements as $element) { // Get an instance of the element class. if ($e = customcert_get_element_instance($element)) { switch ($element->refpoint) { case CUSTOMCERT_REF_POINT_TOPRIGHT: $class = 'element refpoint-right'; break; case CUSTOMCERT_REF_POINT_TOPCENTER: $class = 'element refpoint-center'; break; case CUSTOMCERT_REF_POINT_TOPLEFT: default: $class = 'element refpoint-left'; } $html .= html_writer::tag('div', $e->render_html(), array('class' => $class, 'id' => 'element-' . $element->id)); } } }
/** * Given an ID of an instance of this module, * this function will permanently delete the instance * and any data that depends on it. * * @param int $id * @return bool true if successful */ function customcert_delete_instance($id) { global $CFG, $DB; // Ensure the customcert exists. if (!$DB->get_record('customcert', array('id' => $id))) { return false; } // Get the course module as it is used when deleting files. if (!($cm = get_coursemodule_from_instance('customcert', $id))) { return false; } // Delete the customcert instance. if (!$DB->delete_records('customcert', array('id' => $id))) { return false; } // Delete the elements. $sql = "SELECT e.*\n FROM {customcert_elements} e\n INNER JOIN {customcert_pages} p\n ON e.pageid = p.id\n WHERE p.customcertid = :customcertid"; if ($elements = $DB->get_records_sql($sql, array('customcertid' => $id))) { require_once $CFG->dirroot . '/mod/customcert/locallib.php'; foreach ($elements as $element) { // Get an instance of the element class. if ($e = customcert_get_element_instance($element)) { $e->delete_element(); } else { // The plugin files are missing, so just remove the entry from the DB. $DB->delete_records('customcert_elements', array('id' => $element->id)); } } } // Delete the pages. if (!$DB->delete_records('customcert_pages', array('customcertid' => $id))) { return false; } // Delete the customcert issues. if (!$DB->delete_records('customcert_issues', array('customcertid' => $id))) { return false; } // Delete any files associated with the customcert. $context = context_module::instance($cm->id); $fs = get_file_storage(); $fs->delete_area_files($context->id); return true; }
/** * This function is called after all the activities in the backup have been restored. This allows us to get * the new course module ids, as they may have been restored after the customcert module, meaning no id * was available at the time. */ public function after_restore() { global $CFG, $DB; require_once $CFG->dirroot . '/mod/customcert/locallib.php'; // Get the customcert elements. $sql = "SELECT e.*\n FROM {customcert_elements} e\n INNER JOIN {customcert_pages} p\n ON e.pageid = p.id\n INNER JOIN {customcert} c\n ON p.customcertid = c.id\n WHERE c.id = :customcertid"; if ($elements = $DB->get_records_sql($sql, array('customcertid' => $this->get_activityid()))) { // Go through the elements for the certificate. foreach ($elements as $e) { // Get an instance of the element class. if ($e = customcert_get_element_instance($e)) { $e->after_restore($this); } } } }
/** * Generate the PDF for the specified customcert and user. * * @param stdClass $customcert * @param bool $preview true if it is a preview, false otherwise */ function customcert_generate_pdf($customcert, $preview = false) { global $CFG, $DB; require_once $CFG->libdir . '/pdflib.php'; // Get the pages for the customcert, there should always be at least one page for each customcert. if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $customcert->id), 'pagenumber ASC')) { // Create the pdf object. $pdf = new pdf(); if (!empty($customcert->protection)) { $protection = explode(', ', $customcert->protection); $pdf->SetProtection($protection); } $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); $pdf->SetTitle($customcert->name); $pdf->SetAutoPageBreak(true, 0); // Remove full-stop at the end, if it exists, to avoid "..pdf" being created and being filtered by clean_filename. $filename = rtrim($customcert->name, '.'); $filename = clean_filename($filename . '.pdf'); // Loop through the pages and display their content. foreach ($pages as $page) { // Add the page to the PDF. if ($page->width > $page->height) { $orientation = 'L'; } else { $orientation = 'P'; } $pdf->AddPage($orientation, array($page->width, $page->height)); $pdf->SetMargins(0, 0, $page->margin); // Get the elements for the page. if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id), 'sequence ASC')) { // Loop through and display. foreach ($elements as $element) { // Get an instance of the element class. if ($e = customcert_get_element_instance($element)) { $e->render($pdf, $preview); } } } } $pdf->Output($filename, 'D'); } }