public function test_certificate_print_user_files() { global $CFG, $USER; $this->setAdminUser(); $certificate = $this->generator->create_instance(array('course' => $this->course->id)); $coursemodule = get_coursemodule_from_instance('certificate', $certificate->id); $certificateissues = certificate_get_issue($this->course, $USER, $certificate, $coursemodule); $context = context_module::instance($coursemodule->id); require_once "{$CFG->dirroot}/mod/certificate/certificate_pdf_renderer.php"; $cpr = new certificate_pdf_renderer($certificate, $this->course, $coursemodule); $pdf = $cpr->get_pdf(); $pdfstring = $pdf->Output('', 'S'); $filename = "dummyfile.pdf"; certificate_save_pdf($pdfstring, $certificateissues->id, $filename, $context->id); $this->assertTrue(!!preg_match("/{$filename}/i", certificate_print_user_files($certificate, $USER->id, $context->id))); }
$button = new single_button($link, $linkname); if ($certificate->delivery != 1) { $button->add_action(new popup_action('click', $link, 'view' . $cm->id, array('height' => 600, 'width' => 800))); } echo html_writer::tag('div', $OUTPUT->render($button), array('style' => 'text-align:center')); echo $OUTPUT->footer($course); exit; } else { // Output to pdf // No debugging here, sorry. $CFG->debugdisplay = 0; @ini_set('display_errors', '0'); @ini_set('log_errors', '1'); $filename = certificate_get_certificate_filename($certificate, $cm, $course) . '.pdf'; require "{$CFG->dirroot}/mod/certificate/certificate_pdf_renderer.php"; $cpr = new certificate_pdf_renderer($certificate, $course, $cm); $pdf = $cpr->get_pdf(); // PDF contents are now in $file_contents as a string. $filecontents = $pdf->Output('', 'S'); if ($certificate->savecert == 1) { certificate_save_pdf($filecontents, $certrecord->id, $filename, $context->id); } if ($certificate->delivery == 0) { // Open in browser. send_file($filecontents, $filename, 0, 0, true, false, 'application/pdf'); } elseif ($certificate->delivery == 1) { // Force download. send_file($filecontents, $filename, 0, 0, true, true, 'application/pdf'); } elseif ($certificate->delivery == 2) { certificate_email_student($course, $certificate, $certrecord, $context, $filecontents, $filename); // Open in browser after sending email.
public function test_certificate_type_letter_non_embedded_printteacher() { $this->setAdminUser(); $certificate = $this->generator->create_instance(array('course' => $this->course->id, 'borderstyle' => 'Fancy2-brown.jpg', 'certificatetype' => "letter_non_embedded", 'printteacher' => 1)); $coursemodule = get_coursemodule_from_instance('certificate', $certificate->id); $certificaterenderer = new certificate_pdf_renderer($certificate, $this->course, $coursemodule); $this->assertNotEmpty($certificaterenderer->get_pdf()); }
public function test_certificate_reset_userdata_nullify_old_recipients() { global $DB; // This should create certificate db entry as well as course module entry. $certificate = $this->generator->create_instance(array('course' => $this->course->id)); $coursemodule = get_coursemodule_from_instance('certificate', $certificate->id); $context = context_module::instance($coursemodule->id); $userarray = array(); for ($i = 0; $i < 10; $i++) { $userarray[] = $this->getDataGenerator()->create_user(); $certificateissue = certificate_get_issue($this->course, end($userarray), $certificate, $coursemodule); // Save the files along the way. $filename = certificate_get_certificate_filename($certificate, $coursemodule, $this->course) . '.pdf'; $cpr = new certificate_pdf_renderer($certificate, $this->course, $coursemodule); $pdf = $cpr->get_pdf(); // PDF contents are now in $file_contents as a string. $filecontents = $pdf->Output('', 'S'); certificate_save_pdf($filecontents, $certificate->id, $filename, $context->id); } // Assert that only one file exist. @see certificate_save_pdf $fs = get_file_storage(); $component = 'mod_certificate'; $filearea = 'issue'; $files = $fs->get_area_files($context->id, $component, $filearea, $certificate->id); $certfiles = array(); foreach ($files as $key => $file) { $validfile = trim($file->get_filename()) != "."; if ($validfile) { $certfiles[] = $file; } } $this->assertEquals(1, sizeof($certfiles)); $data = new stdClass(); $data->courseid = $this->course->id; $data->reset_certificate = true; $data->timeshift = 0; $componentstr = get_string('modulenameplural', 'certificate'); $statusarray = certificate_reset_userdata($data); $expectedstatus = array('component' => $componentstr, 'item' => get_string('removecert', 'certificate'), 'error' => false); // Ensure that we have the expected return status. $this->assertContains($expectedstatus, $statusarray); // Ensure that all certificate recpients are no longer a recipient. $this->assertEquals(0, $DB->count_records('certificate_issues', array('certificateid' => $certificate->id))); // Ensure that all saved pdf files are deleted. $files = $fs->get_area_files($context->id, $component, $filearea, $certificate->id); $certfiles = array(); foreach ($files as $key => $file) { $validfile = trim($file->get_filename()) != "."; if ($validfile) { $certfiles[] = $file; } } $this->assertEquals(0, sizeof($certfiles)); }