Refactor getschools in DirectorView controller

parent 137769e4
...@@ -373,6 +373,7 @@ class DirectorView extends ControllerBase ...@@ -373,6 +373,7 @@ class DirectorView extends ControllerBase
$userRole = $tmpRole; $userRole = $tmpRole;
} }
} }
if ($userRole === '') { if ($userRole === '') {
return $this->respondWithStatus([ return $this->respondWithStatus([
'error_code' => 4003, 'error_code' => 4003,
...@@ -389,6 +390,7 @@ class DirectorView extends ControllerBase ...@@ -389,6 +390,7 @@ class DirectorView extends ControllerBase
} else { } else {
$schools = []; $schools = [];
} }
if ($schools) { if ($schools) {
$list = array(); $list = array();
...@@ -581,6 +583,27 @@ class DirectorView extends ControllerBase ...@@ -581,6 +583,27 @@ class DirectorView extends ControllerBase
} }
} }
protected function getLimit($name, $categ)
{
static $limits = array();
$key = "{$name}_{$categ}";
if (isset($limits[$key])) {
$limit = $limits[$key];
} else {
$limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => $name, 'category' => $categ));
$limitdown = reset($limit_down);
if ($limitdown) {
$limit = $limitdown->limit_down->value;
} else {
$limit = -1;
}
$limits[$key] = $limit;
}
return $limit;
}
public function returnstatus($id) public function returnstatus($id)
{ {
$schoolid = $id; $schoolid = $id;
...@@ -594,13 +617,7 @@ class DirectorView extends ControllerBase ...@@ -594,13 +617,7 @@ class DirectorView extends ControllerBase
$CourseA = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id' => $schoolid)); $CourseA = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('id' => $schoolid));
if ($CourseA) { if ($CourseA) {
$limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 1, 'category' => $categ)); $limit = $this->getLimit(1, $categ);
$limitdown = reset($limit_down);
if ($limitdown) {
$limit = $limitdown->limit_down->value;
} else {
$limit = -1;
}
$studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => -1, 'currentclass' => 1)); $studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => -1, 'currentclass' => 1));
...@@ -609,42 +626,45 @@ class DirectorView extends ControllerBase ...@@ -609,42 +626,45 @@ class DirectorView extends ControllerBase
} }
} }
$CourseB = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $schoolid)); $limit = $this->getLimit(2, $categ);
if ($CourseB) { $sCon = $this->connection->select('eepal_sectors_in_epal_field_data', 'eSchool');
$limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 2, 'category' => $categ)); $sCon->leftJoin('epal_student_class', 'eStudent',
$limitdown = reset($limit_down); 'eStudent.epal_id = ' . $schoolid . ' ' .
if ($limitdown) { 'AND eStudent.specialization_id = eSchool.sector_id ' .
$limit = $limitdown->limit_down->value; 'AND eStudent.currentclass = 2');
} else { $sCon->fields('eSchool', array('sector_id'))
$limit = -1; ->fields('eStudent', array('specialization_id'))
} ->groupBy('specialization_id')
->groupBy('sector_id')
foreach ($CourseB as $object) { ->condition('eSchool.epal_id', $schoolid, '=');
$sectorid = $object->sector_id->entity->id(); $sCon->addExpression('count(eStudent.id)', 'eStudent_count');
$studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $sectorid, 'currentclass' => 2));
if (sizeof($studentPerSchool) < $limit) { $results = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
return false;
} foreach ($results as $result) {
if ($result->eStudent_count < $limit) {
return false;
} }
} }
$CourseC = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $schoolid)); $limit = $this->getLimit(3, $categ);
if ($CourseC) { $sCon = $this->connection->select('eepal_specialties_in_epal_field_data', 'eSchool');
$limit_down = $this->entityTypeManager->getStorage('epal_class_limits')->loadByProperties(array('name' => 3, 'category' => $categ)); $sCon->leftJoin('epal_student_class', 'eStudent',
$limitdown = reset($limit_down); 'eStudent.epal_id = ' . $schoolid . ' ' .
if ($limitdown) { 'AND eStudent.specialization_id = eSchool.specialty_id ' .
$limit = $limitdown->limit_down->value; 'AND eStudent.currentclass = 3');
} else { $sCon->fields('eSchool', array('specialty_id'))
$limit = -1; ->fields('eStudent', array('specialization_id'))
} ->groupBy('specialization_id')
->groupBy('specialty_id')
foreach ($CourseC as $object) { ->condition('eSchool.epal_id', $schoolid, '=');
$specialityid = $object->specialty_id->entity->id(); $sCon->addExpression('count(eStudent.id)', 'eStudent_count');
$studentPerSchool = $this->entityTypeManager->getStorage('epal_student_class')->loadByProperties(array('epal_id' => $schoolid, 'specialization_id' => $specialityid, 'currentclass' => 3));
$results = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
if (sizeof($studentPerSchool) < $limit) {
return false; foreach ($results as $result) {
} if ($result->eStudent_count < $limit) {
return false;
} }
} }
......
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