Skip to content

Commit

Permalink
Fix/php8 (#869)
Browse files Browse the repository at this point in the history
* first commit

* fix calculate numeric grades

* fix 'There is already an active transaction'

* fix/division-by-zero

---------

Co-authored-by: gabriel-IPTI <ti.gabriel@ipti.org.br>
  • Loading branch information
GabrielChagas14 and gabriel-IPTI authored Nov 7, 2024
1 parent 8b3d5aa commit b7c218d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 24 deletions.
43 changes: 27 additions & 16 deletions app/controllers/GradesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ public function actionSaveGradesRelease()
$givenClasses += (int) $std['grades'][$key]['givenClasses'];
}

$frequency = (($givenClasses - $totalFaults) / $givenClasses) * 100;
if($givenClasses != 0) {
$frequency = (($givenClasses - $totalFaults) / $givenClasses) * 100;
} else {
$frequency = null;
}


if (!$gradeResult->validate()) {
Expand Down Expand Up @@ -583,28 +587,35 @@ public function actionGetGrades()

public function actionCalculateFinalMedia()
{
$classroomId = Yii::app()->request->getPost("classroom");
$disciplineId = Yii::app()->request->getPost("discipline");
$transaction = Yii::app()->db->beginTransaction();
try {

$classroom = Classroom::model()->with("activeStudentEnrollments.studentFk")->findByPk($classroomId);
$classroomId = Yii::app()->request->getPost("classroom");
$disciplineId = Yii::app()->request->getPost("discipline");

$gradeRules = GradeRules::model()->findByAttributes([
"edcenso_stage_vs_modality_fk" => $classroom->edcenso_stage_vs_modality_fk
]);
$classroom = Classroom::model()->with("activeStudentEnrollments.studentFk")->findByPk($classroomId);

TLog::info("Começado processo de calcular média final.", ["Classroom" => $classroom->id, "GradeRules" => $gradeRules->id]);
$gradeRules = GradeRules::model()->findByAttributes([
"edcenso_stage_vs_modality_fk" => $classroom->edcenso_stage_vs_modality_fk
]);

foreach ($classroom->activeStudentEnrollments as $enrollment) {
$gradeUnities = new GetGradeUnitiesByDisciplineUsecase($gradeRules->edcenso_stage_vs_modality_fk);
$gradesStudent = $gradeUnities->exec();
$countUnities = $gradeUnities->execCount();
TLog::info("Começado processo de calcular média final.", ["Classroom" => $classroom->id, "GradeRules" => $gradeRules->id]);

TLog::info("Unidades por disciplina", ["GradeUnities" => CHtml::listData($gradesStudent, 'id', 'id')]);
foreach ($classroom->activeStudentEnrollments as $enrollment) {
$gradeUnities = new GetGradeUnitiesByDisciplineUsecase($gradeRules->edcenso_stage_vs_modality_fk);
$gradesStudent = $gradeUnities->exec();
$countUnities = $gradeUnities->execCount();

$gradeResult = (new GetStudentGradesResultUsecase($enrollment->id, $disciplineId))->exec();
(new CalculateFinalMediaUsecase($gradeResult, $gradeRules, $countUnities, $gradesStudent))->exec();
(new ChageStudentStatusByGradeUsecase($gradeResult, $gradeRules, $countUnities))->exec();
TLog::info("Unidades por disciplina", ["GradeUnities" => CHtml::listData($gradesStudent, 'id', 'id')]);

$gradeResult = (new GetStudentGradesResultUsecase($enrollment->id, $disciplineId))->exec();
(new CalculateFinalMediaUsecase($gradeResult, $gradeRules, $countUnities, $gradesStudent))->exec();
(new ChageStudentStatusByGradeUsecase($gradeResult, $gradeRules, $countUnities))->exec();
}
$transaction->commit();
} catch (Exception $e) {
$transaction->rollback();
TLog::error("Erro ao atualizar status da matrícula", ["Exception" => $e]);
}

}
Expand Down
5 changes: 2 additions & 3 deletions app/domain/grades/usecases/CalculateFinalMediaUsecase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function __construct($gradesResult, $gradeRule, $countUnities, $gradesStu

public function exec()
{
$transaction = Yii::app()->db->beginTransaction();
try {
$grades = [];
if ($this->gradeRule->gradeCalculationFk->name == 'Média Semestral') {
Expand Down Expand Up @@ -76,9 +75,9 @@ public function exec()
TLog::info("Média final calculada", ["finalMedia" => $finalMedia]);

$this->saveFinalMedia($this->gradesResult, $finalMedia);
$transaction->commit();

} catch (Exception $e) {
$transaction->rollback();

TLog::error("Erro ao salvar média final", ["Exception" => $e]);
}

Expand Down
10 changes: 8 additions & 2 deletions app/domain/grades/usecases/CalculateNumericGradeUsecase.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ private function calculateNumericGrades($studentEnrollment, $discipline, $unitie
}

}
/* */

$gradeResult["sem_avarage_1"] = is_nan(($semAvarage1 / $unitiesSem1) ?? NAN) ? null : round(($semAvarage1 / $unitiesSem1), 1);
$gradeResult["sem_avarage_2"] = is_nan(($semAvarage2 / $unitiesSem2) ?? NAN) ? null : round(($semAvarage2 / $unitiesSem2), 1);
if($unitiesSem1 != 0) {
$gradeResult["sem_avarage_1"] = is_nan(($semAvarage1 / $unitiesSem1) ?? NAN) ? null : round(($semAvarage1 / $unitiesSem1), 1);
}

if($unitiesSem2 != 0) {
$gradeResult["sem_avarage_2"] = is_nan(($semAvarage2 / $unitiesSem2) ?? NAN) ? null : round(($semAvarage2 / $unitiesSem2), 1);
}
$gradeResult = $this->calculatePartialRecovery($gradeResult, $studentEnrollment, $discipline, $gradesRecoveries);

$gradeResult->setAttribute("final_media", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct($gradeResult, $gradeRule, $numUnities, $frequency =

public function exec()
{
$transaction = Yii::app()->db->beginTransaction();

try {

$enrollment = $this->getStudentEnrollment($this->gradeResult->enrollment_fk);
Expand All @@ -49,9 +49,8 @@ public function exec()
}

$this->updateStudentSituation();
$transaction->commit();

} catch (Exception $e) {
$transaction->rollback();
TLog::error("Erro ao atualizar status da matrícula", ["Exception" => $e]);
}
}
Expand Down
1 change: 1 addition & 0 deletions js/enrollment/gradesRelease/_initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ $('#discipline').change(function (e, triggerEvent) {
let totalGivenClasses = 0;
let html = `
<h3>Aulas Dadas</h3>
<div><div class="t-badge-info t-margin-none--left"><span class="t-info_positive t-badge-info__icon"></span>Para o cáculo da frequência preencha as aulas dadas e as faltas</div></div>
<div class="row">
`;
for (let i = 0; i < unitiesLength; i++) {
Expand Down

0 comments on commit b7c218d

Please sign in to comment.