Merge branch 'distr-review' into 'develop'

Refine step 2

See merge request !207
parents a376b7fb 722143e4
......@@ -73,7 +73,7 @@ class Distribution extends ControllerBase
public function createDistribution(Request $request)
{
set_time_limit(600); // dev
$numDistributions = 3;
$sizeOfBlock = 1000000;
......@@ -127,16 +127,15 @@ class Distribution extends ControllerBase
if ($this->initializeResults() === self::ERROR_DB) {
$transaction->rollback();
return $this->respondWithStatus([
"message" => t("Unexpected Error in initializeResults function")
"message" => t("Unexpected Error")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
if (($limitUp_class = $this->retrieveCapacityLimitUp("1")) === self::ERROR_DB) {
$transaction->rollback();
return $this->respondWithStatus([
"message" => t("Unexpected Error in retrieveCapacityLimitUp function")
"message" => t("Unexpected Error")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
// print_r("<br> ΑΝΩΤΑΤΟ ΟΡΙΟ ΜΑΘΗΤΩΝ: " . $limitUp_class);
while ($this->choice_id <= $numDistributions) {
......@@ -144,42 +143,30 @@ class Distribution extends ControllerBase
// υπολογισμός πλήθους για να καθοριστεί ο αριθμός των fetches που θα κάνουμε με συγκεκριμένο sizeOfBlock
$sCon = $this->connection->select('epal_student', 'eStudent')
->fields('eStudent', array('id'));
$sCon->join('epal_student_epal_chosen', 'epals', "eStudent.id = epals.student_id AND epals.choice_no = {$this->choice_id}");
$numData = $sCon->countQuery()->execute()->fetchField();
//print_r("<br>numData: " . $numData);
$num = 1;
$j = 1;
while ($num <= $numData) {
//print_r("<br>FETCH: " . $j);
$sCon = $this->connection->select('epal_student', 'eStudent')
->fields('eStudent', array('id', 'currentclass', 'currentepal', 'second_period'))
->condition('eStudent.id', 1 + $sizeOfBlock * ($j-1), '>=')
->condition('eStudent.id', $j * $sizeOfBlock, '<=');
$epalStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
if ($this->locateStudent($this->choice_id, $epalStudents) === self::ERROR_DB) {
if (($total_located = $this->locateStudent($this->choice_id, null, 1 + $sizeOfBlock * ($j-1), $j * $sizeOfBlock, null)) === self::ERROR_DB) {
$transaction->rollback();
return $this->respondWithStatus([
"message" => t("Unexpected Error in locateStudent function")
"message" => t("Unexpected Error")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
$num = $num + sizeof($epalStudents);
$j = $j + 1;
$num += $total_located;
$j += 1;
}
}
else { // $this->choice_id !== 1
if (sizeof($this->pendingStudents) != 0) {
$sCon = $this->connection->select('epal_student', 'eStudent')
->fields('eStudent', array('id', 'currentclass', 'currentepal', 'second_period'))
->condition('eStudent.id', $this->pendingStudents, 'IN');
$epalStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
if ($this->locateStudent($this->choice_id, $epalStudents) === self::ERROR_DB) {
if ($this->locateStudent($this->choice_id, null, null, null, $this->pendingStudents) === self::ERROR_DB) {
$transaction->rollback();
return $this->respondWithStatus([
"message" => t("Unexpected Error in locateStudent function")
"message" => t("Unexpected Error")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
} else { // αν δεν υπάρχουν εκκρεμότητες, μην συνεχίζεις με άλλο πέρασμα
break;
......@@ -198,7 +185,7 @@ class Distribution extends ControllerBase
if ($this->checkCapacityAndArrange($eepalSchool->id, "1", "-1", $limitUp_class, $eepalSchool->capacity_class_a) === self::ERROR_DB) {
$transaction->rollback();
return $this->respondWithStatus([
"message" => t("Unexpected Error in checkCapacityAndArrange function")
"message" => t("Unexpected Error")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
......@@ -210,13 +197,13 @@ class Distribution extends ControllerBase
if ($this->checkCapacityAndArrange($eepalSchool->id, "2", $eepalSecInEp->sector_id, $limitUp_class, $eepalSecInEp->capacity_class_sector) === self::ERROR_DB) {
$transaction->rollback();
return $this->respondWithStatus([
"message" => t("Unexpected Error in checkCapacityAndArrange function")
"message" => t("Unexpected Error")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
$sCon = $this->connection->select('eepal_specialties_in_epal_field_data', 'eSchool')
->fields('eSchool', array('epal_id', 'specialty_id', 'capacity_class_specialty'))
->fields('eSchool', array('epal_id', 'specialty_id', 'capacity_class_specialty', 'capacity_class_specialty_d'))
->condition('eSchool.epal_id', $eepalSchool->id, '=');
$eepalSpecialtiesInEpal = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
foreach ($eepalSpecialtiesInEpal as $eepalSpecialInEp) {
......@@ -224,15 +211,15 @@ class Distribution extends ControllerBase
if ($this->checkCapacityAndArrange($eepalSchool->id, "3", $eepalSpecialInEp->specialty_id, $limitUp_class, $eepalSpecialInEp->capacity_class_specialty) === self::ERROR_DB) {
$transaction->rollback();
return $this->respondWithStatus([
"message" => t("Unexpected Error in checkCapacityAndArrange function")
"message" => t("Unexpected Error")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
//Δ' Λυκείου
if ($eepalSchool->operation_shift === "ΕΣΠΕΡΙΝΟ") {
if ($this->checkCapacityAndArrange($eepalSchool->id, "4", $eepalSpecialInEp->specialty_id, $limitUp_class, $eepalSpecialInEp->capacity_class_specialty) === self::ERROR_DB) {
if ($this->checkCapacityAndArrange($eepalSchool->id, "4", $eepalSpecialInEp->specialty_id, $limitUp_class, $eepalSpecialInEp->capacity_class_specialty_d) === self::ERROR_DB) {
$transaction->rollback();
return $this->respondWithStatus([
"message" => t("Unexpected Error in checkCapacityAndArrange function")
"message" => t("Unexpected Error")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
......@@ -245,7 +232,7 @@ class Distribution extends ControllerBase
if ($this->findSmallClasses() === self::ERROR_DB) {
$transaction->rollback();
return $this->respondWithStatus([
"message" => t("Unexpected Error in findSmallClasses function AFTER initial Distribution!")
"message" => t("Unexpected Error")
], Response::HTTP_INTERNAL_SERVER_ERROR);
// αν αποτύχει, δεν γίνεται rollback. --> Λύση: διαγραφή των όποιων αποτελεσμάτων
// if ($this->initializeResults() === self::ERROR_DB) {
......@@ -256,95 +243,90 @@ class Distribution extends ControllerBase
}
}
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
$this->logger->error($e->getMessage());
$transaction->rollback();
return $this->respondWithStatus([
"message" => t("An unexpected problem occured in createDistribution Method")
"message" => t("Unexpected Error")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->respondWithStatus([
'message' => "Distribution has made successfully",
'message' => "Distribution completed successfully",
], Response::HTTP_OK);
}
public function locateStudent($choice_id, &$epalStudents)
/**
* @return int Return self::ERROR_DB in case of error, else return number of 'locatedStudents'
*/
public function locateStudent($choice_id, $epalStudents = null, $id_range_start = null, $id_range_end = null, $id_list = null)
{
$epal_dist_id = -1;
$specialization_id = -1;
$total_count = 0;
try {
foreach ($epalStudents as $epalStudent) {
//print_r("<br>ΚΑΤΑΝΟΜΗ ΜΑΘΗΤΩΝ ΝΟ: " . $choice_id);
//print_r("<br>ΜΑΘΗΤΗΣ: " . $epalStudent->id);
$clCon = $this->connection->select('epal_student_epal_chosen', 'epals')
->fields('epals', array('student_id', 'epal_id', 'choice_no'))
->condition('epals.student_id', $epalStudent->id, '=')
->condition('epals.choice_no', $choice_id, '=');
$epalSchoolsChosen = $clCon->execute()->fetchAll(\PDO::FETCH_OBJ);
if (sizeof($epalSchoolsChosen) !== 0) {
$epalSchoolChos = reset($epalSchoolsChosen);
//print_r(" SCHOOL_ID:" . $epalSchoolChos->epal_id . " STUDENT_ID " . $epalStudent->id);
$epal_dist_id = $epalSchoolChos->epal_id;
if ($epalStudent->currentclass === "2") {
$clCon = $this->connection->select('epal_student_sector_field', 'sectors')
->fields('sectors', array('student_id', 'sectorfield_id'))
->condition('sectors.student_id', $epalStudent->id, '=');
$epalSectorChosen = $clCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$epalSecChos = reset($epalSectorChosen);
} //Δ'Λυκείου - Γ' Λυκείου
elseif ($epalStudent->currentclass === "3" || $epalStudent->currentclass === "4") {
$clCon = $this->connection->select('epal_student_course_field', 'courses')
->fields('courses', array('student_id', 'coursefield_id'))
->condition('courses.student_id', $epalStudent->id, '=');
$epalCourseChosen = $clCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$epalCourChos = reset($epalCourseChosen);
}
if ($epalStudent->currentclass === "2") {
$specialization_id = $epalSecChos->sectorfield_id;
} //Δ'Λυκείου - Γ' Λυκείου
elseif ($epalStudent->currentclass === "3" || $epalStudent->currentclass === "4") {
$specialization_id = $epalCourChos->coursefield_id;
} else {
$specialization_id = -1;
}
$timestamp = strtotime(date("Y-m-d"));
$this->connection->insert('epal_student_class')->fields(
array(
'id' => $this->globalCounterId++,
'uuid' => \Drupal::service('uuid')->generate(),
'langcode' => $this->language,
'user_id' => $this->currentuser,
'student_id'=> $epalStudent->id,
'epal_id'=> $epal_dist_id,
'currentclass' => $epalStudent->currentclass,
'currentepal' => $epalStudent->currentepal,
'specialization_id' => $specialization_id,
//'points' => $epalStudent->points,
'distribution_id' => $choice_id,
'finalized' => 1,
'second_period' => $epalStudent->second_period,
'status' => 1,
'created' => $timestamp,
'changed' => $timestamp
)
)->execute();
} //end if
} //foreach
$sCon = $this->connection->select('epal_student', 'eStudent')
->fields('eStudent', array('id', 'currentclass', 'currentepal', 'second_period'));
if ($id_range_start !== null) {
$sCon->condition('eStudent.id', $id_range_start, '>=');
}
if ($id_range_end !== null) {
$sCon->condition('eStudent.id', $id_range_end, '<=');
}
if (is_array($id_list) && count($id_list) > 0) {
$sCon->condition('eStudent.id', $id_list, 'IN');
}
$sCon->join('epal_student_epal_chosen', 'epals', "eStudent.id = epals.student_id AND epals.choice_no = {$choice_id}");
$sCon->fields('epals', array('epal_id', 'choice_no'));
$sCon->leftJoin('epal_student_sector_field', 'sectors', "sectors.student_id = eStudent.id");
$fieldname1 = $sCon->addField('sectors', 'sectorfield_id');
$sCon->leftJoin('epal_student_course_field', 'courses', "courses.student_id = eStudent.id");
$fieldname2 = $sCon->addField('courses', 'coursefield_id');
// $sCon->addExpression('-1', 'specialization_id'); // no need, will have to check anyway
// $epalStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
// if ($epalStudents === false) {
// throw new \Exception("Cannot fetch data");
// }
$results = $sCon->execute();
while ($epalStudent = $results->fetchObject()) {
$total_count++;
// echo "- {$total_count}\n";
if ($epalStudent->currentclass === "2") {
$specialization_id = $epalStudent->sectorfield_id;
} else if ($epalStudent->currentclass === "3" || $epalStudent->currentclass === "4") {
$specialization_id = $epalStudent->coursefield_id;
} else {
$specialization_id = -1;
}
$epal_dist_id = $epalStudent->epal_id;
$timestamp = strtotime(date("Y-m-d"));
$this->connection->insert('epal_student_class')->fields([
'id' => $this->globalCounterId++,
'uuid' => \Drupal::service('uuid')->generate(),
'langcode' => $this->language,
'user_id' => $this->currentuser,
'student_id'=> $epalStudent->id,
'epal_id'=> $epal_dist_id,
'currentclass' => $epalStudent->currentclass,
'currentepal' => $epalStudent->currentepal,
'specialization_id' => $specialization_id,
// 'points' => $epalStudent->points,
'distribution_id' => $choice_id,
'finalized' => 1,
'second_period' => $epalStudent->second_period,
'status' => 1,
'created' => $timestamp,
'changed' => $timestamp
])->execute();
}
} catch (\Exception $e) {
$this->logger->warning($e->getMessage());
$this->logger->error($e->getMessage());
return self::ERROR_DB;
}
return self::SUCCESS;
// return self::SUCCESS;
return $total_count;
}
......@@ -352,16 +334,16 @@ class Distribution extends ControllerBase
{
try {
$clCon = $this->connection->select('epal_class_limits', 'classLimits')
->fields('classLimits', array('limit_up'))
->condition('classLimits.name', $className, '=');
->fields('classLimits', array('limit_up'))
->condition('classLimits.name', $className, '=');
$results = $clCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$row = reset($results);
} catch (\Exception $e) {
$this->logger->warning($e->getMessage());
$this->logger->error($e->getMessage());
return self::ERROR_DB;
}
return $row->limit_up;
return $row->limit_up;
}
......@@ -369,8 +351,6 @@ class Distribution extends ControllerBase
{
if (!isset($capacity)) {
//print_r("<br> ΜΠΗΚΑ!!! ");
//print_r("<br> ΣΧΟΛΕΙΟ: " . $epalId . " ΤΑΞΗ: " . $classId . " ΤΟΜΕΑΣ/ΕΙΔΙΚΟΤΗΤΑ: " . $secCourId . " ΧΩΡΗΤΙΚΟΤΗΤΑ: " . $capacity);
$capacity = 0;
}
......@@ -384,24 +364,16 @@ class Distribution extends ControllerBase
$limit = $limitup * $capacity;
if (sizeof($epalStudentClass) > $limit) {
//print_r("<br>ΥΠΕΡΧΕΙΛΙΣΗ!");
//foreach ($epalStudentClass as $epalStudCl) {
//Υπολογισμός μορίων του μαθητή και (πιθανή) αποθήκευσή τους
//ΣΗΜΕΙΩΣΗ: Ο υπoλογισμός γίνεται στο front-end
//}
$this->makeSelectionOfStudents($epalStudentClass, $limit);
} else { //αφαίρεσε όσους μαθητές βρίσκονται στον πίνακα εκκρεμοτήτων
foreach ($epalStudentClass as $epalStudCl) {
if ($this->choice_id !== 1) {
////διέγραψε τον μαθητή από τον πίνακα εκκρεμοτήτων (αν βρίσκεται εκεί)
$this->removeFromPendingStudents($epalStudCl->student_id);
}
}
} elseif ($this->choice_id !== 1) {
// αφαίρεσε όσους μαθητές βρίσκονται στον πίνακα εκκρεμοτήτων
$clear_ids = array_map(function ($epalStudCl) {
return $epalStudCl->student_id;
}, $epalStudentClass);
$this->removeFromPendingStudents($clear_ids);
}
} // end try
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
return self::ERROR_DB;
}
......@@ -411,10 +383,11 @@ class Distribution extends ControllerBase
public function removeFromPendingStudents($val)
{
if (($key = array_search($val, $this->pendingStudents)) !== false) {
if (is_array($val) && count($val) > 0) {
$this->pendingStudents = array_diff($this->pendingStudents, $val);
} elseif (($key = array_search($val, $this->pendingStudents)) !== false) {
unset($this->pendingStudents[$key]);
}
//$this->pendingStudents = array_diff($this->pendingStudents, array($val));
}
public function makeSelectionOfStudents_VERSION_WITH_POINTS(&$students, $limit)
......@@ -471,7 +444,7 @@ class Distribution extends ControllerBase
->condition('student_id', $student->student_id)
->execute();
} catch (\Exception $e) {
$this->logger->warning($e->getMessage());
$this->logger->error($e->getMessage());
$transaction->rollback();
return $this->respondWithStatus([
"message" => t("An unexpected problem occured during DELETE proccess in makeSelectionOfStudents Method of Distribution")
......@@ -501,16 +474,16 @@ class Distribution extends ControllerBase
//αν capacity = 0, ..διέγραψέ τους από εκεί που τοποθετήθηκαν προσωρινά
if ($limit === 0) {
foreach ($students as $student) {
array_push($this->pendingStudents, $student->student_id);
try {
$this->connection->delete('epal_student_class')
->condition('student_id', $student->student_id)
->execute();
} catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return self::ERROR_DB;
}
try {
$clear_ids = array_map(function ($student) {
return $student->student_id;
}, $students);
$this->connection->delete('epal_student_class')
->condition('student_id', $clear_ids, 'IN')
->execute();
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
return self::ERROR_DB;
}
return self::SUCCESS;
}
......@@ -527,35 +500,33 @@ class Distribution extends ControllerBase
}
}
}
//print_r("<br>#ΕΓΓΡΑΦΩΝ ΠΟΥ ΟΙ ΜΑΘΗΤΕΣ ΦΟΙΤΟΥΣΑΝ ΗΔΗ:" . $cnt);
$newlimit = $limit - $cnt;
//print_r("<br>ΑΝΩΤΑΤΟ ΟΡΙΟ ΜΑΘΗΤΩΝ:" . $limit);
//Αν δεν απέμειναν θέσεις (δηλαδή αν η χωρητικότητα είναι μικρότερη ή ίση από το πλήθος μαθητών που ήδη φοιτούν στο σχολείο)
//τότε διέγραψέ τους από τον προσωρινό πίνακα αποτελεσμάτων και βάλε τους στον στον πίνακα εκκρεμοτήτων
foreach ($students as $student) {
if ($student->currentepal !== $student->epal_id) {
$not_current_students = array_filter($students, function ($student) {
return $student->currentepal !== $student->epal_id;
});
if (count($not_current_students) > 0) {
try {
$clear_ids = array_map(function ($student) {
return $student->student_id;
}, $not_current_students);
if ($newlimit <= 0) {
//print_r("<br>ΣΕ ΕΚΚΡΕΜΟΤΗΤΑ - ΔΙΑΓΡΑΦΗ: " . $student->student_id);
array_push($this->pendingStudents, $student->student_id);
try {
$this->connection->delete('epal_student_class')
->condition('student_id', $student->student_id)
->execute();
} catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return self::ERROR_DB;
}
} // endif new limit
else { // $newlimit > 0
$this->pendingStudents = array_merge($this->pendingStudents, $clear_ids);
$this->connection->delete('epal_student_class')
->condition('student_id', $clear_ids, 'IN')
->execute();
} else { // $newlimit > 0
if ($this->choice_id !== 1) {
$this->removeFromPendingStudents($student->student_id);
$this->removeFromPendingStudents($clear_ids);
}
}
} // endif currentepal
} // end foreach
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
return self::ERROR_DB;
}
}
return self::SUCCESS;
}
......@@ -573,7 +544,7 @@ class Distribution extends ControllerBase
try {
$this->connection->delete('epal_student_class')->execute();
} catch (\Exception $e) {
$this->logger->warning($e->getMessage());
$this->logger->error($e->getMessage());
return self::ERROR_DB;
}
......@@ -687,7 +658,7 @@ class Distribution extends ControllerBase
}
} //end try
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
$this->logger->error($e->getMessage());
return self::ERROR_DB;
}
} //end function
......@@ -702,7 +673,7 @@ class Distribution extends ControllerBase
->condition('eStudent.specialization_id', $sectorOrcourseId, '=');
return $sCon->countQuery()->execute()->fetchField();
} catch (\Exception $e) {
$this->logger->warning($e->getMessage());
$this->logger->error($e->getMessage());
return self::ERROR_DB;
}
}
......@@ -719,7 +690,7 @@ class Distribution extends ControllerBase
}
$query->execute();
} catch (\Exception $e) {
$this->logger->warning($e->getMessage());
$this->logger->error($e->getMessage());
return self::ERROR_DB;
}
return self::SUCCESS;
......@@ -821,7 +792,7 @@ class Distribution extends ControllerBase
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
} catch (\Exception $e) {
$this->logger->warning($e->getMessage());
$this->logger->error($e->getMessage());
$transaction->rollback();
return $this->respondWithStatus([
"message" => t("An unexpected problem occured in locateSecondPeriodStudents Method")
......@@ -842,7 +813,7 @@ class Distribution extends ControllerBase
]);
$query->execute();
} catch (\Exception $e) {
$this->logger->warning($e->getMessage());
$this->logger->error($e->getMessage());
return self::ERROR_DB;
}
return self::SUCCESS;
......@@ -857,7 +828,7 @@ class Distribution extends ControllerBase
$con->execute();
//$num = $con->rowCount();
} catch (\Exception $e) {
$this->logger->warning($e->getMessage());
$this->logger->error($e->getMessage());
return self::ERROR_DB;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment