Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Remove endpoint alunos_by_guardian_cpf da API do I-Diário" #940

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions ieducar/modules/Api/Views/AlunoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ protected function canGetTodosAlunos()
return $this->validatesPresenceOf('instituicao_id') && $this->validatesPresenceOf('escola');
}

protected function canGetAlunosByGuardianCpf()
{
return $this->validatesPresenceOf('aluno_id') && $this->validatesPresenceOf('cpf');
}

protected function validateInepCode()
{
if ($this->getRequest()->aluno_inep_id) {
Expand Down Expand Up @@ -1325,6 +1330,62 @@ protected function getTodosAlunos()
}
}

protected function getIdpesFromCpf($cpf)
{
$sql = 'SELECT idpes FROM cadastro.fisica WHERE cpf = $1';

return $this->fetchPreparedQuery($sql, $cpf, true, 'first-field');
}

protected function checkAlunoIdpesGuardian($idpesGuardian, $alunoId)
{
$sql = '
SELECT 1
FROM pmieducar.aluno
INNER JOIN cadastro.fisica ON (aluno.ref_idpes = fisica.idpes)
WHERE cod_aluno = $2
AND (idpes_pai = $1
OR idpes_mae = $1
OR idpes_responsavel = $1) LIMIT 1
';

return $this->fetchPreparedQuery($sql, [$idpesGuardian, $alunoId], true, 'first-field') == 1;
}

protected function getAlunosByGuardianCpf()
{
if ($this->canGetAlunosByGuardianCpf()) {
$cpf = $this->getRequest()->cpf;
$alunoId = $this->getRequest()->aluno_id;

$idpesGuardian = $this->getIdpesFromCpf($cpf);

if (is_numeric($idpesGuardian) && $this->checkAlunoIdpesGuardian($idpesGuardian, $alunoId)) {
$sql = '
SELECT cod_aluno as aluno_id, pessoa.nome as nome_aluno
FROM pmieducar.aluno
INNER JOIN cadastro.fisica ON (aluno.ref_idpes = fisica.idpes)
INNER JOIN cadastro.pessoa ON (pessoa.idpes = fisica.idpes)
WHERE idpes_pai = $1
OR idpes_mae = $1
OR idpes_responsavel = $1
';

$alunos = $this->fetchPreparedQuery($sql, [$idpesGuardian]);
$attrs = ['aluno_id', 'nome_aluno'];
$alunos = Portabilis_Array_Utils::filterSet($alunos, $attrs);

foreach ($alunos as &$aluno) {
$aluno['nome_aluno'] = Portabilis_String_Utils::toUtf8($aluno['nome_aluno']);
}

return ['alunos' => $alunos];
} else {
$this->messenger->append('Não foi encontrado nenhum vínculos entre esse aluno e cpf.');
}
}
}

protected function getMatriculas()
{
if ($this->canGetMatriculas()) {
Expand Down Expand Up @@ -2077,6 +2138,8 @@ public function Gerar()
$this->appendResponse($this->getOcorrenciasDisciplinares());
} elseif ($this->isRequestFor('get', 'grade_ultimo_historico')) {
$this->appendResponse($this->getGradeUltimoHistorico());
} elseif ($this->isRequestFor('get', 'alunos_by_guardian_cpf')) {
$this->appendResponse($this->getAlunosByGuardianCpf());
} elseif ($this->isRequestFor('post', 'aluno')) {
$this->appendResponse($this->post());
} elseif ($this->isRequestFor('put', 'aluno')) {
Expand Down