Skip to content

Commit

Permalink
Merge pull request #46 from EscolaDeSaudePublica/develop
Browse files Browse the repository at this point in the history
Gerar Certificados
  • Loading branch information
jeff-doliveira1 authored Oct 27, 2022
2 parents c85a0f7 + 80c0a37 commit 008cc13
Show file tree
Hide file tree
Showing 42 changed files with 607 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lumen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
steps:
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '7.4'
php-version: '7.3'
- uses: actions/checkout@v2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
Expand Down
199 changes: 199 additions & 0 deletions app/DAO/EnsinoPesquisaExtensao/CertificadoDAO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php

namespace App\DAO\EnsinoPesquisaExtensao;

use Illuminate\Support\Facades\DB;

class CertificadoDAO
{

/**
* Pega informações referentes a OfertaCurso e OfertaTurma
*
* @param integer $turmaid
* @return array
*/
public function getOfertaCursoTurma($turmaid)
{
$select = DB::select(
"SELECT
OT.habilitada, OT.codigo, OT.ofertaturmaid, OT.ofertacursoid, OT.descricao as descricao_turma, OT.minimoalunos, OT.maximoalunos, OT.datainicialoferta, OT.datafinaloferta, OT.datainicialaulas, OT.datafinalaulas, OT.datainicialinscricao, OT.datafinalinscricao, OT.datainicialmatricula, OT.datafinalmatricula, OT.gradehorarioid, OT.situacao as situacao_turma, OT.centerid, OT.localid,
OFC.ocorrenciacursoid, OFC.descricao as descricao_ofertacurso, OFC.situacao as situacao_ofertacurso,
OC.situacao as situacao_ocorrenciacurso, OC.cursoid, OC.turnid
FROM acpofertaturma OT
INNER JOIN acpofertacurso OFC
ON OT.ofertacursoid = OFC.ofertacursoid
INNER JOIN acpocorrenciacurso OC
ON OFC.ocorrenciacursoid = OC.ocorrenciacursoid
WHERE OT.ofertaturmaid = :turmaid",
['turmaid' => $turmaid]
);

$result = [];

if (count($select)) {

foreach ($select[0] as $key => $value) {
$result[$key] = $value;
}
}

return $result;
}

/**
* Unifica consultas e exibe unidades didáticas do curso via turmaid
*
* @param integer $turmaid
* @return array
*/
public function getCursoMatrizCurricular($turmaid)
{
$ofertaCursoTurma = $this->getOfertaCursoTurma($turmaid);
$cursoid = $ofertaCursoTurma['cursoid'];

$select = DB::select(
"SELECT
C.nome AS nome_curso,
C.datainicio, C.datafim,
MC.descricao AS descricao_matriz,
MCG.matrizcurriculargrupoid AS id_modulo,
MCG.descricao AS descricao_modulo,
CC.codigo cod_unidadedidatica,
CC.nome AS nome_unidadedidatica,
CCD.cargahorariapresencial,
CCD.cargahorariaextraclasse,
CCD.cargahoraria
FROM acpcurso C
INNER JOIN acpmatrizcurricular MC
ON C.cursoid = MC.cursoid
INNER JOIN acpmatrizcurriculargrupo MCG
ON MC.matrizcurricularid = MCG.matrizcurricularid
INNER JOIN acpcomponentecurricularmatriz CCM
ON MCG.matrizcurriculargrupoid = CCM.matrizcurriculargrupoid
INNER JOIN acpcomponentecurricular CC
ON CCM.componentecurricularid = CC.componentecurricularid
INNER JOIN acpcomponentecurriculardisciplina CCD
ON CC.componentecurricularid = CCD.componentecurricularid
WHERE C.cursoid = :cursoid
ORDER BY MCG.ordem, CC.codigo, CC.nome",
['cursoid' => $cursoid]
);

$curso = [];
$modulos = [];

if (count($select)) {

$cargaHorariaCurso = 0;

foreach ($select as $item) {

if (!array_key_exists($item->id_modulo, $modulos)) {

$modulos[$item->id_modulo] = [
'id_modulo' => $item->id_modulo,
'descricao_modulo' => $item->descricao_modulo,
'carga_horaria_modulo' => 0,
'unidadedidatica' => [],
];
}

$modulos[$item->id_modulo]['unidadedidatica'][] = [
'cod_unidadedidatica' => $item->cod_unidadedidatica,
'nome_unidadedidatica' => $item->nome_unidadedidatica,
'cargahoraria_presencial' => $item->cargahorariapresencial,
'cargahoraria_extraclasse' => $item->cargahorariaextraclasse,
];

$modulos[$item->id_modulo]['carga_horaria_modulo'] += $item->cargahoraria;

$cargaHorariaCurso += $item->cargahoraria;
}

$curso = [
'id_curso' => $cursoid,
'curso' => $select[0]->nome_curso,
'descricao_matriz' => $select[0]->descricao_matriz,
'datainicial' => $ofertaCursoTurma['datainicialoferta'],
'datafinal' => $ofertaCursoTurma['datafinaloferta'],
'cargahoraria_curso' => $cargaHorariaCurso
];
}

return [
'curso' => $curso,
'modulos' => $modulos,
];
}

/**
* Pegar a lista de alunos inscritos na turma
*
* @param integer $ofertaturmaid
* @param integer $inscricaoid
* @param string $situacaoaluno 'Aprovado' | 'Reprovado' | 'Cancelado' | 'Pendente' | 'Matriculado'
* @return array
*/
public function getInscricoesDaTurma(
$ofertaturmaid,
$inscricaoid = null,
$situacaoaluno = 'Aprovado'
) {

// sem inscricaoid
if (is_null($inscricaoid)) {

$select = DB::select(
"SELECT DISTINCT
ITG.inscricaoid,
PERSON.name as nome,
acp_obtersituacaopedagogicadainscricao(ITG.inscricaoid) as situacaoaluno
FROM acpinscricaoturmagrupo ITG
LEFT JOIN acpmatricula MAT
ON (ITG.inscricaoturmagrupoid = MAT.inscricaoturmagrupoid)
LEFT JOIN ONLY basperson PERSON
ON (MAT.personid = PERSON.personid)
WHERE ITG.ofertaturmaid = :ofertaturmaid
AND acp_obtersituacaopedagogicadainscricao(ITG.inscricaoid) = :situacaoaluno
ORDER BY PERSON.name",
[
'ofertaturmaid' => $ofertaturmaid,
'situacaoaluno' => $situacaoaluno,
]
);

return $select;
}

// com inscricaoid
$select = DB::select(
"SELECT DISTINCT
ITG.inscricaoid,
PERSON.name as nome,
acp_obtersituacaopedagogicadainscricao(ITG.inscricaoid) as situacaoaluno
FROM acpinscricaoturmagrupo ITG
LEFT JOIN acpmatricula MAT
ON (ITG.inscricaoturmagrupoid = MAT.inscricaoturmagrupoid)
LEFT JOIN ONLY basperson PERSON
ON (MAT.personid = PERSON.personid)
WHERE ITG.ofertaturmaid = :ofertaturmaid
AND ITG.inscricaoid = :inscricaoid
AND acp_obtersituacaopedagogicadainscricao(ITG.inscricaoid) = :situacaoaluno
ORDER BY PERSON.name",
[
'ofertaturmaid' => $ofertaturmaid,
'inscricaoid' => $inscricaoid,
'situacaoaluno' => $situacaoaluno,
]
);

return $select;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Http\Controllers\EnsinoPesquisaExtensao;

use App\DAO\EnsinoPesquisaExtensao\CertificadoDAO;
use App\Http\Controllers\Controller;
use App\Services\EnsinoPesquisaExtensao\CertificateService;

class CertificateController extends Controller
{
private $certificate_service;

function __construct()
{
$this->certificate_service = new CertificateService();
}

public function generateCertificateByStudent($turmaId, $incricaoid)
{
$info = $this->generateCertificate($turmaId, $incricaoid);

$this->certificate_service->generatePDF($info);
}

public function generateCertificateByClass($turmaId)
{
$info = $this->generateCertificate($turmaId);

$this->certificate_service->generatePDF($info);
}

private function generateCertificate($turmaId, $incricaoid = null)
{
$certificadoDao = new CertificadoDAO();
$estudantes = $certificadoDao->getInscricoesDaTurma($turmaId, $incricaoid);
$cursoMatrizCurricular = $certificadoDao->getCursoMatrizCurricular(
$turmaId
);

return [
'curso' => $cursoMatrizCurricular['curso'],
'modulos' => $cursoMatrizCurricular['modulos'],
'estudantes' => $estudantes
];
}
}
120 changes: 120 additions & 0 deletions app/Services/EnsinoPesquisaExtensao/CertificateService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace App\Services\EnsinoPesquisaExtensao;

use Carbon\Carbon;
use DateTime;
use ZipArchive;

class CertificateService
{
public function generatePDF($info)
{
$zip = new ZipArchive();
$initial_date = new DateTime($info["curso"]["datainicial"]);
$final_date = new DateTime($info["curso"]["datafinal"]);

$period = "{$initial_date->format('d/m/Y')} a {$final_date->format('d/m/Y')}";
$curriculum_matrix_info = $this->mountCurriculumMatrixInfo($info, $period);
$charToFilter = array('+', '-', ',', '\\', '/', ' ');
$course_name_doc = str_replace($charToFilter, '_', trim($info["curso"]["curso"]));

$zip->open("/tmp/mpdf/{$course_name_doc}.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);

foreach ($info["estudantes"] as $student) {
$mpdf = $this->setMPDFSettings();
$pdf_info = $this->mountInfoPDF($student, $info, $period);
$student_name_doc = str_replace($charToFilter, '_', trim($student->nome));

$mpdf->SetImportUse();
$mpdf->SetDocTemplate('/var/www/public/assets/docs/certificate/template.pdf');
$mpdf->WriteHTML($this->PDFInfo($pdf_info));
$mpdf->AddPage('P');
$mpdf->WriteHTML($this->renderCurriculumMatrix($curriculum_matrix_info));

$content = $mpdf->Output('', 'S');

$zip->addFromString("{$student_name_doc}.pdf", $content);
}

$zip->close();

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename('/tmp/mpdf/' . $course_name_doc . '.zip') . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile('/tmp/mpdf/' . $course_name_doc . '.zip');

exit;
}

private function setMPDFSettings()
{
$defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$defaultFontConfig = (new \Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];

$mpdf = new \Mpdf\Mpdf([
'tempDir' => '/tmp/mpdf',
'mode' => 'utf-8',
'format' => [210, 297],
'orientation' => 'L',
'margin_top' => 0,
'fontDir' => array_merge($fontDirs, [
'/var/www/public/assets/fonts/archivo',
'/var/www/public/assets/fonts/open-sans'
]),
'fontdata' => $fontData + [
'archivo' => [
'R' => 'Archivo-Regular.ttf'
],
'open-sans' => [
'R' => 'OpenSans-Regular.ttf'
]
]
]);

return $mpdf;
}

private function mountInfoPDF($student, $info, $period)
{
$today_date = Carbon::now('America/Fortaleza')->locale('pt-BR')->isoFormat('DD \d\e MMMM \d\e YYYY');

$pdf_info = [
'student_name' => ucwords(mb_strtolower($student->nome)),
'course_name' => ucwords(mb_strtolower($info["curso"]["curso"])),
'course_period' => $period,
'course_workload' => $info["curso"]["cargahoraria_curso"],
'today_date' => $today_date
];

return $pdf_info;
}

private function mountCurriculumMatrixInfo($info, $period)
{
$curriculum_matrix_info = [
'course_name' => $info["curso"]["curso"],
'matrix_name' => $info["curso"]["descricao_matriz"],
'course_period' => $period,
'modules' => $info["modulos"],
'course_workload' => $info["curso"]["cargahoraria_curso"]
];

return $curriculum_matrix_info;
}

private function PDFInfo($pdf_info)
{
return view('certificate.info', $pdf_info);
}

private function renderCurriculumMatrix($curriculum_matrix_info)
{
return view('certificate.curriculum-matrix', $curriculum_matrix_info);
}
}
1 change: 1 addition & 0 deletions bootstrap/app.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
*/

$app->register(LaravelLegends\PtBrValidator\ValidatorProvider::class);
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);
// $app->register(App\Providers\AuthServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class);

Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
"firebase/php-jwt": "^5.0",
"palanik/lumen-cors": "dev-master",
"phpmailer/phpmailer": "~6.0",
"laravellegends/pt-br-validator": "5.1.*"
"laravellegends/pt-br-validator": "5.1.*",
"flipbox/lumen-generator": "^6.0",
"mpdf/mpdf": "7.1.7",
"nesbot/carbon": "2.61.0 as 1.39.0",
"kylekatarnls/laravel-carbon-2": "^1.0.0"
},
"require-dev": {
"fzaninotto/faker": "^1.9",
Expand Down
Loading

0 comments on commit 008cc13

Please sign in to comment.