/** * Parse general placeholders, mostly certificate data * * @param ilObjCourse $course * @return array */ protected function parseGeneralPlaceholders(ilObjCourse $course) { $utc = ilCertificateConfig::get('time_format_utc'); $cert_valid_from = strtotime($this->certificate->getValidFrom()); $cert_valid_to = strtotime($this->certificate->getValidTo()); if ($utc) { // fix for timezone issue: when converting a mysql date into a timestamp and then into another timezone, its possible the date changes (because the start date is the first second of the day). // We now add 12*60*60 seconds to be in the middle of the day $cert_valid_to += srCertificate::TIME_ZONE_CORRECTION; $cert_valid_from += srCertificate::TIME_ZONE_CORRECTION; } $placeholder = array('DATE' => $this->formatDate('DATE'), 'DATETIME' => $this->formatDateTime('DATETIME'), 'TIMESTAMP' => $utc ? strtotime(gmdate('Y-m-d H:i:s')) : time(), 'CERT_FILE_NAME' => $this->certificate->getFilename(), 'CERT_FILE_VERSION' => $this->certificate->getFileVersion(), 'CERT_VALID_FROM' => $this->certificate->getValidFrom() == '' ? $this->pl->txt('unlimited') : $this->formatDate('CERT_VALID_FROM', $cert_valid_from), 'CERT_VALID_TO' => $this->certificate->getValidTo() == '' ? $this->pl->txt('unlimited') : $this->formatDate('CERT_VALID_TO', $cert_valid_to), 'CERT_ID' => $this->certificate->getId(), 'CERT_TEMPLATE_PATH' => $this->certificate->getDefinition()->getType()->getCertificateTemplatesPath(), 'CERT_TYPE_TITLE' => $this->certificate->getDefinition()->getType()->getTitle(), 'CERT_TYPE_DESCRIPTION' => $this->certificate->getDefinition()->getType()->getDescription(), 'COURSE_TITLE' => $course->getTitle()); return $placeholder; }
/** * Generate the report for given certificate * * @param srCertificate $cert * @throws ilException * @return bool */ public function generate(srCertificate $cert) { if (!$this->isAvailable()) { throw new ilException("Generating certificates with TemplateTypeJasper is only available if the JasperReport service is installed"); } require_once self::JASPER_CLASS; $template = $cert->getDefinition()->getType()->getCertificateTemplatesPath(true); // A template is required, so quit early if it does not exist for some reason if (!is_file($template)) { return false; } $placeholders = $cert->getPlaceholders(); try { $defined_placeholders = $this->parseDefinedPlaceholders($template); } catch (Exception $e) { // XML is not valid return false; } // Only send defined placeholders to jasper, otherwise the template file is not considered as valid $placeholders = array_intersect_key($placeholders, $defined_placeholders); $placeholders = $this->nl2br($placeholders); $report = new JasperReport($template, $cert->getFilename(false)); if ($locale = $this->pl->config('jasper_locale')) { $report->setLocale($this->pl->config('jasper_locale')); } if ($java = $this->pl->config('jasper_path_java')) { $report->setPathJava($java); } $report->setDataSource(JasperReport::DATASOURCE_EMPTY); $report->setParameters($placeholders); try { $report->generateOutput(); $report_file = $report->getOutputFile(); // Move pdf to correct certificate location $cert_path = $cert->getCertificatePath(); if (!file_exists($cert_path)) { ilUtil::makeDirParents($cert_path); } $from = $report_file . '.pdf'; $to = $cert->getFilePath(); return ilUtil::moveUploadedFile($from, '', $to, false, 'rename'); } catch (JasperReportException $e) { $this->log->write("srCertificateTemplyteTypeJasper::generate() Report file of certificate with ID {$cert->getId()} was not created by Jasper: " . implode(', ', $e->getErrors())); return false; } }