/** * @param srCertificate $certificate */ protected function retryGeneration(srCertificate $certificate) { $certificate->setStatus(srCertificate::STATUS_NEW); $certificate->update(); ilUtil::sendSuccess($this->pl->txt('msg_retry_generation'), true); $this->ctrl->redirect($this, 'index'); }
/** * 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 TemplateTypeHtml is only available for ILIAS > 4.4"); } $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; } require_once './Services/PDFGeneration/classes/class.ilPDFGeneration.php'; // Get HTML markup by parsing the template and replace placeholders $markup = file_get_contents($template); $markup = srCertificatePlaceholdersParser::getInstance()->parse($markup, $cert->getPlaceholders()); try { $job = new ilPDFGenerationJob(); $job->setMarginLeft('20'); $job->setMarginBottom('20'); $job->setMarginRight('20'); $job->setMarginTop('20'); $job->setOutputMode('F'); // Save to disk $job->setFilename($cert->getFilePath()); $job->addPage($markup); ilPDFGeneration::doJob($job); return true; // Method above gives no feedback so assume true -.- } catch (Exception $e) { $this->log->write("srCertificateTemplyteTypeHtml::generate() : " . $e->getMessage()); return false; } }
/** * Update any certificates in the draft status to new, in order to process them via cronjob */ protected function updateStatusFromDraftToNew() { $certificates = srCertificate::where(array('user_id' => $this->user->getId(), 'status' => srCertificate::STATUS_DRAFT))->get(); foreach ($certificates as $certificate) { /** @var srCertificate $certificate */ $certificate->setStatus(srCertificate::STATUS_NEW); $certificate->save(); } }
/** * 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; } }
/** * Execute notification * * @return bool */ public function notify() { global $ilSetting; if (!$this->email) { return false; } $this->mailer->To($this->email); $from = $ilSetting->get('mail_external_sender_noreply'); if ($from) { $this->mailer->From($from); } $this->mailer->Subject($this->getSubject()); $this->mailer->Body($this->getBody()); if ($this->attach_certificate) { $this->mailer->Attach($this->certificate->getFilePath()); } $this->mailer->Send(); return true; }
/** * Return all Placeholders of Learning Progress data * * @param ilObjCourse $course * @param ilObjUser $user * @return array */ protected function parseLearningProgressPlaceholders(ilObjCourse $course, ilObjUser $user) { $passed_datetime = ilCourseParticipants::getDateTimeOfPassed($course->getId(), $user->getId()); $lp_fields = array('first_access', 'last_access', 'percentage', 'status', 'read_count', 'childs_spent_seconds'); $lp_data = ilTrQuery::getObjectsDataForUser($user->getId(), $course->getId(), $course->getRefId(), '', '', 0, 9999, null, $lp_fields); $lp_avg = $this->buildAvgPercentageOfCourseObjects($lp_data); $lp_crs = array(); $max_last_access = 0; foreach ($lp_data['set'] as $v) { if ($v['type'] == 'crs') { $lp_crs = $v; $lp_crs['first_access'] = strtotime($v['first_access']); // First access is not stored as UNIX timestamp... } if ($v['last_access'] > $max_last_access) { $max_last_access = $v['last_access']; } } $lp_crs['last_access'] = $max_last_access; // calculates spent time different for scorm modules if enabled in config /** @var $cert_def srCertificateDefinition */ $cert_definition = $this->certificate->getDefinition(); if ($cert_definition->getScormTiming()) { $spent_seconds = 0; require_once './Services/Object/classes/class.ilObjectLP.php'; $ilScormLP = ilObjectLP::getInstance($course->getId()); /** * @var $ilLPCollection ilLPCollection */ $ilLPCollection = $ilScormLP->getCollectionInstance(); if ($ilLPCollection instanceof ilLPCollection) { foreach ($ilLPCollection->getItems() as $item) { $spent_seconds += $this->getSpentSeconds(ilObject::_lookupObjectId($item), $user->getId()); } } $lp_crs['childs_spent_seconds'] = $spent_seconds; } $lp_spent_time = $this->buildLpSpentTime($lp_crs); return array('DATE_COMPLETED' => $this->formatDate('DATE_COMPLETED', strtotime($passed_datetime)), 'DATETIME_COMPLETED' => $this->formatDateTime('DATETIME_COMPLETED', strtotime($passed_datetime)), 'LP_FIRST_ACCESS' => $this->formatDateTime('LP_FIRST_ACCESS', (int) $lp_crs['first_access']), 'LP_LAST_ACCESS' => $this->formatDateTime('LP_LAST_ACCESS', (int) $lp_crs['last_access']), 'LP_SPENT_TIME' => $lp_spent_time, 'LP_SPENT_SECONDS' => $lp_crs['childs_spent_seconds'], 'LP_READ_COUNT' => $lp_crs['read_count'], 'LP_STATUS' => $lp_crs['status'], 'LP_AVG_PERCENTAGE' => $lp_avg); }
?> <#6> <?php // Update database schema, added created_at timestamp and active flag to certificates require_once './Customizing/global/plugins/Services/UIComponent/UserInterfaceHook/Certificate/classes/Certificate/class.srCertificate.php'; srCertificate::updateDB(); ?> <#7> <?php // Flag latest version of each certificate as active require_once './Customizing/global/plugins/Services/UIComponent/UserInterfaceHook/Certificate/classes/Certificate/class.srCertificate.php'; /** @var ilDB $ilDB */ $set = $ilDB->query('SELECT user_id, definition_id, MAX(file_version) AS max_file_version FROM cert_obj GROUP BY definition_id, user_id'); while ($row = $ilDB->fetchObject($set)) { /** @var srCertificate $cert */ $cert = srCertificate::where(array('definition_id' => $row->definition_id, 'user_id' => $row->user_id, 'file_version' => $row->max_file_version))->first(); if ($cert) { $cert->setActive(true); $cert->save(); } } ?> <#8> <?php if (!$ilDB->tableColumnExists('cert_type_setting', 'value')) { $ilDB->renameTableColumn('cert_type_setting', 'default_value', 'value'); } ?> <#9> <?php require_once './Customizing/global/plugins/Services/UIComponent/UserInterfaceHook/Certificate/classes/CustomSetting/class.srCertificateCustomTypeSetting.php';
/** * Load anonymized placholders * * @param bool $anonymized */ protected function loadPlaceholders($anonymized = false) { parent::loadPlaceholders(true); }
/** * Get data from model based on filter * */ protected function buildData() { $filters = $this->filter_names; // Always display latest version of certificates aka "active" if the table was initialized with this option // Otherwise, check if the checkbox of the filter was checked if ($this->getOption('newest_version_only')) { $filters['active'] = 1; } if ($this->getOption('definition_id')) { $filters['definition_id'] = $this->getOption('definition_id'); } if ($this->getOption('user_id')) { $filters['user_id'] = $this->getOption('user_id'); } $this->setExternalSorting(true); $this->setExternalSegmentation(true); $this->setDefaultOrderField($this->columns[0]); $this->determineLimit(); $this->determineOffsetAndOrder(); $options = array('filters' => $filters, 'count' => true, 'limit' => array($this->getOffset(), $this->getLimit()), 'sort' => array($this->getOrderField() => $this->getOrderDirection())); $count = srCertificate::getCertificateData($options); $data = srCertificate::getCertificateData(array_merge($options, array('count' => false))); foreach ($data as $cert) { if ($cert["status"] == srCertificate::STATUS_PROCESSED) { $this->has_any_certs = true; } } $this->setMaxCount($count); $this->setData($data); }
/** * Download the given IDs of certificates as ZIP-File. * Note: No permission checking, this must be done by the controller calling this method * * @param array $cert_ids * @param string $filename Filename of zip, appended to the current date */ public static function downloadAsZip(array $cert_ids = array(), $filename = 'certificates') { if (count($cert_ids)) { $zip_filename = date('d-m-Y') . '-' . $filename; // Make a random temp dir in ilias data directory $tmp_dir = ilUtil::ilTempnam(); ilUtil::makeDir($tmp_dir); $zip_base_dir = $tmp_dir . DIRECTORY_SEPARATOR . $zip_filename; ilUtil::makeDir($zip_base_dir); // Copy all PDFs in folder foreach ($cert_ids as $cert_id) { /** @var srCertificate $cert */ $cert = srCertificate::find((int) $cert_id); if (!is_null($cert) && $cert->getStatus() == srCertificate::STATUS_PROCESSED) { copy($cert->getFilePath(), $zip_base_dir . DIRECTORY_SEPARATOR . $cert->getFilename(true)); } } $tmp_zip_file = $tmp_dir . DIRECTORY_SEPARATOR . $zip_filename . '.zip'; try { ilUtil::zip($zip_base_dir, $tmp_zip_file); rename($tmp_zip_file, $zip_file = ilUtil::ilTempnam()); ilUtil::delDir($tmp_dir); ilUtil::deliverFile($zip_file, $zip_filename . '.zip', '', false, true); } catch (ilFileException $e) { ilUtil::sendInfo($e->getMessage()); } } }
/** * Get timestamp of the last_status according to LP * * @param srCertificate $cert * @return int|null */ protected function getLastLPStatus(srCertificate $cert) { $ref_id = $cert->getDefinition()->getRefId(); $obj_id = ilObject::_lookupObjectId($ref_id); $lp_data = ilTrQuery::getObjectsDataForUser($cert->getUserId(), $obj_id, $ref_id, '', '', 0, 9999, null, array('last_access')); $last_status = null; foreach ($lp_data['set'] as $data) { if ($data['type'] == 'crs') { $last_status = $data['last_access']; break; } } return (int) $last_status; }
/** * Download multiple certificates as ZIP file * */ public function downloadCertificates() { $cert_ids = isset($_POST['cert_id']) ? (array) $_POST['cert_id'] : array(); srCertificate::downloadAsZip($cert_ids); $this->index(); }