Commit de513ce2 authored by Νίκος Κατσαούνος's avatar Νίκος Κατσαούνος

updated reports Controller

parents 8e0d7866 81522c0f
......@@ -29,171 +29,175 @@ define("NO_CLASS_LIM_DOWN", -2);
define("SMALL_CLS", 1);
define("NON_SMALL_CLS", 2);
class ReportsCreator extends ControllerBase {
protected $entity_query;
protected $entityTypeManager;
protected $logger;
protected $connection;
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
QueryFactory $entity_query,
Connection $connection,
LoggerChannelFactoryInterface $loggerChannel)
{
$this->entityTypeManager = $entityTypeManager;
$this->entity_query = $entity_query;
$connection = Database::getConnection();
$this->connection = $connection;
$this->logger = $loggerChannel->get('epal');
class ReportsCreator extends ControllerBase
{
protected $entity_query;
protected $entityTypeManager;
protected $logger;
protected $connection;
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
QueryFactory $entity_query,
Connection $connection,
LoggerChannelFactoryInterface $loggerChannel
) {
$this->entityTypeManager = $entityTypeManager;
$this->entity_query = $entity_query;
$connection = Database::getConnection();
$this->connection = $connection;
$this->logger = $loggerChannel->get('epal');
}
public static function create(ContainerInterface $container)
public static function create(ContainerInterface $container)
{
return new static(
$container->get('entity_type.manager'),
$container->get('entity.query'),
$container->get('database'),
$container->get('logger.factory')
);
$container->get('entity_type.manager'),
$container->get('entity.query'),
$container->get('database'),
$container->get('logger.factory')
);
}
public function makeReportUsers(Request $request) {
try {
if (!$request->isMethod('GET')) {
return $this->respondWithStatus([
"message" => t("Method Not Allowed")
], Response::HTTP_METHOD_NOT_ALLOWED);
}
//user validation
$authToken = $request->headers->get('PHP_AUTH_USER');
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
$user = reset($users);
if (!$user) {
return $this->respondWithStatus([
'message' => t("User not found"),
], Response::HTTP_FORBIDDEN);
}
//user role validation
$roles = $user->getRoles();
$validRole = false;
foreach ($roles as $role)
if ($role === "ministry") {
$validRole = true;
break;
}
if (!$validRole) {
return $this->respondWithStatus([
'message' => t("User Invalid Role"),
], Response::HTTP_FORBIDDEN);
}
$list = array();
//υπολογισμός αριθμού αιτήσεων
$sCon = $this->connection
->select('epal_student', 'eStudent')
->fields('eStudent', array('id'));
$numApplications = $sCon->countQuery()->execute()->fetchField();
array_push($list,(object) array('name' => "Αριθμός Αιτήσεων (συνολικά)", 'numStudents' => $numApplications));
//υπολογισμός αριθμού αιτήσεων ανά τάξη
$classes = [1 => 'Α', 2 => 'Β', 3 => 'Γ', 4 => 'Δ'];
foreach ($classes as $i => $label) {
$sCon = $this->connection
->select('epal_student', 'eStudent')
->fields('eStudent', array('id'))
->condition('eStudent.currentclass', strval($i) , '=');
$numApplications = $sCon->countQuery()->execute()->fetchField();
array_push($list,(object) array('name' => "Αριθμός Αιτήσεων για {$label} Τάξη", 'numStudents' => $numApplications));
}
//υπολογισμός αριθμού αιτήσεων για δεύτερη περίοδο
$sCon = $this->connection
->select('epal_student', 'eStudent')
->fields('eStudent', array('id'))
->condition('eStudent.second_period', 1 , '=');
$numApplications = $sCon->countQuery()->execute()->fetchField();
array_push($list,(object) array('name' => "Αριθμός Αιτήσεων B' περιόδου", 'numStudents' => $numApplications));
//υπολογισμός αριθμού χρηστών
$sCon = $this->connection
->select('epal_users', 'eUser')
->fields('eUser', array('id'));
$numUsers = $sCon->countQuery()->execute()->fetchField();
array_push($list,(object) array('name' => "Αριθμός Εγγεγραμένων Χρηστών με ρόλο Αιτούντα", 'numStudents' => $numUsers));
return $this->respondWithStatus($list, Response::HTTP_OK);
} //end try
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return $this->respondWithStatus([
"message" => t("An unexpected problem occured during report")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
public function makeReportUsers(Request $request)
{
try {
if (!$request->isMethod('GET')) {
return $this->respondWithStatus([
"message" => t("Method Not Allowed")
], Response::HTTP_METHOD_NOT_ALLOWED);
}
//user validation
$authToken = $request->headers->get('PHP_AUTH_USER');
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
$user = reset($users);
if (!$user) {
return $this->respondWithStatus([
'message' => t("User not found"),
], Response::HTTP_FORBIDDEN);
}
//user role validation
$roles = $user->getRoles();
$validRole = false;
foreach ($roles as $role) {
if ($role === "ministry") {
$validRole = true;
break;
}
}
if (!$validRole) {
return $this->respondWithStatus([
'message' => t("User Invalid Role"),
], Response::HTTP_FORBIDDEN);
}
$list = array();
//υπολογισμός αριθμού αιτήσεων
$sCon = $this->connection
->select('epal_student', 'eStudent')
->fields('eStudent', array('id'));
$numApplications = $sCon->countQuery()->execute()->fetchField();
array_push($list, (object) array('name' => "Αριθμός Αιτήσεων (συνολικά)", 'numStudents' => $numApplications));
//υπολογισμός αριθμού αιτήσεων ανά τάξη
$classes = [1 => 'Α', 2 => 'Β', 3 => 'Γ', 4 => 'Δ'];
foreach ($classes as $i => $label) {
$sCon = $this->connection
->select('epal_student', 'eStudent')
->fields('eStudent', array('id'))
->condition('eStudent.currentclass', strval($i), '=');
$numApplications = $sCon->countQuery()->execute()->fetchField();
array_push($list, (object) array('name' => "Αριθμός Αιτήσεων για {$label} Τάξη", 'numStudents' => $numApplications));
}
//υπολογισμός αριθμού αιτήσεων για δεύτερη περίοδο
$sCon = $this->connection
->select('epal_student', 'eStudent')
->fields('eStudent', array('id'))
->condition('eStudent.second_period', 1, '=');
$numApplications = $sCon->countQuery()->execute()->fetchField();
array_push($list, (object) array('name' => "Αριθμός Αιτήσεων B' περιόδου", 'numStudents' => $numApplications));
//υπολογισμός αριθμού χρηστών
$sCon = $this->connection
->select('epal_users', 'eUser')
->fields('eUser', array('id'));
$numUsers = $sCon->countQuery()->execute()->fetchField();
array_push($list, (object) array('name' => "Αριθμός Εγγεγραμένων Χρηστών με ρόλο Αιτούντα", 'numStudents' => $numUsers));
return $this->respondWithStatus($list, Response::HTTP_OK);
} //end try
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return $this->respondWithStatus([
"message" => t("An unexpected problem occured during report")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
public function makeGeneralReport(Request $request) {
try {
if (!$request->isMethod('GET')) {
return $this->respondWithStatus([
"message" => t("Method Not Allowed")
], Response::HTTP_METHOD_NOT_ALLOWED);
}
//user validation
$authToken = $request->headers->get('PHP_AUTH_USER');
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
$user = reset($users);
if (!$user) {
return $this->respondWithStatus([
'message' => t("User not found"),
], Response::HTTP_FORBIDDEN);
}
//user role validation
$roles = $user->getRoles();
$validRole = false;
foreach ($roles as $role)
if ($role === "ministry") {
$validRole = true;
break;
}
if (!$validRole) {
return $this->respondWithStatus([
'message' => t("User Invalid Role"),
], Response::HTTP_FORBIDDEN);
}
//υπολογισμός αριθμού δηλώσεων
$sCon = $this->connection
->select('epal_student', 'eStudent')
->fields('eStudent', array('id'));
$numTotal = $sCon->countQuery()->execute()->fetchField();
//υπολογισμός αριθμού δηλώσεων που ικανοποιήθηκαν στην i προτίμηση
$numData = array();
for ($i=0; $i < 3; $i++) {
$sCon = $this->connection
->select('epal_student_class', 'eStudent')
->fields('eStudent', array('id', 'distribution_id'))
->condition('eStudent.distribution_id', $i+1, '=')
->condition('eStudent.finalized', 1 , '=');
array_push($numData, $sCon->countQuery()->execute()->fetchField());
}
public function makeGeneralReport(Request $request)
{
// υπολογισμός αριθμού δηλώσεων που ΔΕΝ ικανοποιήθηκαν
/*
try {
if (!$request->isMethod('GET')) {
return $this->respondWithStatus([
"message" => t("Method Not Allowed")
], Response::HTTP_METHOD_NOT_ALLOWED);
}
//user validation
$authToken = $request->headers->get('PHP_AUTH_USER');
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
$user = reset($users);
if (!$user) {
return $this->respondWithStatus([
'message' => t("User not found"),
], Response::HTTP_FORBIDDEN);
}
//user role validation
$roles = $user->getRoles();
$validRole = false;
foreach ($roles as $role) {
if ($role === "ministry") {
$validRole = true;
break;
}
}
if (!$validRole) {
return $this->respondWithStatus([
'message' => t("User Invalid Role"),
], Response::HTTP_FORBIDDEN);
}
//υπολογισμός αριθμού δηλώσεων
$sCon = $this->connection
->select('epal_student', 'eStudent')
->fields('eStudent', array('id'));
$numTotal = $sCon->countQuery()->execute()->fetchField();
//υπολογισμός αριθμού δηλώσεων που ικανοποιήθηκαν στην i προτίμηση
$numData = array();
for ($i=0; $i < 3; $i++) {
$sCon = $this->connection
->select('epal_student_class', 'eStudent')
->fields('eStudent', array('id', 'distribution_id'))
->condition('eStudent.distribution_id', $i+1, '=')
->condition('eStudent.finalized', 1, '=');
array_push($numData, $sCon->countQuery()->execute()->fetchField());
}
// υπολογισμός αριθμού δηλώσεων που ΔΕΝ ικανοποιήθηκαν
/*
$sCon = $this->connection
->select('epal_student_class', 'eStudent')
->fields('eStudent', array('student_id'));
......@@ -713,355 +717,804 @@ class ReportsCreator extends ControllerBase {
} //end foreach school
return $this->respondWithStatus(
$list
, Response::HTTP_OK);
public function makeReportCompleteness(Request $request, $regionId, $adminId, $schId)
{
} //end try
try {
if (!$request->isMethod('GET')) {
return $this->respondWithStatus([
"message" => t("Method Not Allowed")
], Response::HTTP_METHOD_NOT_ALLOWED);
}
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
//user validation
$authToken = $request->headers->get('PHP_AUTH_USER');
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
$user = reset($users);
if (!$user) {
return $this->respondWithStatus([
"message" => t("An unexpected problem occured in makeReportCompleteness Method")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
private function isSmallClass($schoolId, $numStud, $classId, $sectorOrcourseId, $regionId) {
$limitDown = $this->retrieveLimitDown($classId, $regionId);
if ($limitDown === NO_CLASS_LIM_DOWN)
return NO_CLASS_LIM_DOWN;
else if ($limitDown === ERR_DB)
return ERR_DB;
$numStudents = (int) $numStud;
if ( ($numStudents < $limitDown) /*&& ($numStudents > 0)*/ )
return SMALL_CLS;
else
return NON_SMALL_CLS;
}
private function retrieveLimitDown($classId, $regionId) {
try {
$sCon = $this->connection->select('epal_class_limits', 'eClassLimit')
->fields('eClassLimit', array('limit_down'))
->condition('eClassLimit.name', $classId, '=')
->condition('eClassLimit.category', $regionId, '=');
$classLimits = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
if (sizeof($classLimits) === 1) {
$classLimit = reset($classLimits);
return $classLimit->limit_down;
}
else {
return NO_CLASS_LIM_DOWN;
}
} //end try
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return ERR_DB;
}
} //end function
public function retrieveUserRegistryNo(Request $request) {
'message' => t("User not found"),
], Response::HTTP_FORBIDDEN);
}
//user role validation
$roles = $user->getRoles();
$validRole = false;
foreach ($roles as $role) {
if ($role === "ministry" || $role === "regioneduadmin" || $role === "eduadmin") {
$validRole = true;
break;
}
}
if (!$validRole) {
return $this->respondWithStatus([
'message' => t("User Invalid Role"),
], Response::HTTP_FORBIDDEN);
}
$list = array();
//βρες ανώτατο επιτρεπόμενο όριο μαθητών
$limitUp = $this->retrieveUpLimit();
//βρες όλα τα σχολεία που πληρούν τα κριτήρια / φίλτρα
$sCon = $this->connection->select('eepal_school_field_data', 'eSchool');
$sCon->join('eepal_region_field_data', 'eRegion', 'eRegion.id = eSchool.region_edu_admin_id');
$sCon->join('eepal_admin_area_field_data', 'eAdmin', 'eAdmin.id = eSchool.edu_admin_id');
$sCon->leftJoin('eepal_sectors_in_epal_field_data', 'sectors', 'sectors.epal_id = eSchool.id');
$sCon->fields('eSchool', array('id', 'name', 'capacity_class_a', 'region_edu_admin_id', 'edu_admin_id', 'operation_shift'))
->fields('eRegion', ['name'])
->fields('eAdmin', ['name'])
->groupBy('id')
->groupBy('name')
->groupBy('capacity_class_a')
->groupBy('region_edu_admin_id')
->groupBy('edu_admin_id')
->groupBy('operation_shift')
->groupBy('eRegion_name')
->groupBy('eAdmin_name');
$sCon->addExpression('sum(sectors.capacity_class_sector)', 'capacity_class_b');
if ($regionId != 0) {
$sCon->condition('eSchool.region_edu_admin_id', $regionId, '=');
}
if ($adminId != 0) {
$sCon->condition('eSchool.edu_admin_id', $adminId, '=');
}
if ($schId != 0) {
$sCon->condition('eSchool.id', $schId, '=');
}
$epalSchools = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
foreach ($epalSchools as $epalSchool) {
//εύρεση ονόματος ΠΔΕ που ανήκει το σχολείο
$regionColumn = $epalSchool->eRegion_name;
//εύρεση ονόματος ΔΙΔΕ που ανήκει το σχολείο
$adminColumn = $epalSchool->eAdmin_name;
//βρες μέγιστη χωρητικότητα για κάθε τάξη
$capacity = array();
//χωρητικότητα για Α' τάξη
array_push($capacity, $epalSchool->capacity_class_a * $limitUp );
//χωρητικότητα για Β' τάξη
array_push($capacity, $epalSchool->capacity_class_b * $limitUp);
//χωρητικότητα για Γ' τάξη
$sCon = $this->connection
->select('eepal_specialties_in_epal_field_data', 'eSchool')
->fields('eSchool', array('id', 'capacity_class_specialty'))
->condition('eSchool.epal_id', $epalSchool->id, '=');
$specialtiesInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$numClassSpecialties = 0;
foreach ($specialtiesInEpals as $specialtiesInEpal) {
$numClassSpecialties += $specialtiesInEpal->capacity_class_specialty;
}
array_push($capacity, $numClassSpecialties * $limitUp);
//χωρητικότητα για Δ' τάξη
if ($epalSchool->operation_shift === "ΕΣΠΕΡΙΝΟ") {
$sCon = $this->connection
->select('eepal_specialties_in_epal_field_data', 'eSchool')
->fields('eSchool', array('id', 'capacity_class_specialty_d'))
->condition('eSchool.epal_id', $epalSchool->id, '=');
$specialtiesInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$numClassSpecialtiesNight = 0;
foreach ($specialtiesInEpals as $specialtiesInEpal) {
$numClassSpecialtiesNight += $specialtiesInEpal->capacity_class_specialty_d;
}
array_push($capacity, $numClassSpecialtiesNight * $limitUp);
} else {
array_push($capacity, -1);
}
//χωρητικότητα για όλο το σχολείο
$capacityTotal = array_sum($capacity);
//βρες αριθμό μαθητών γισ κάθε τάξη
$num = array();
$perc = array();
$stat_complete = true;
for ($classId = 1; $classId <= 4; $classId++) {
$sCon = $this->connection
->select('epal_student_class', 'eStudent')
->fields('eStudent', array('id', 'epal_id', 'currentclass'))
->condition('eStudent.epal_id', $epalSchool->id, '=')
->condition('eStudent.currentclass', $classId, '=');
array_push( $num, $sCon->countQuery()->execute()->fetchField());
//βρες ποσοστά συμπλήρωσης
if (isset($capacity[$classId-1]) && $capacity[$classId-1] > 0) {
$perc_str = number_format($num[$classId-1] / $capacity[$classId-1] * 100, 2);
} else {
$perc_str = '-';
$stat_complete = false;
}
array_push( $perc, $perc_str);
}
if ($stat_complete === true && $capacityTotal > 0) {
$percTotal = number_format(array_sum($num) / $capacityTotal * 100, 2);
} else {
$percTotal = '-';
}
//αποστολή αποτελεσμάτων / στατιστικών
array_push($list, (object) array(
'name' => $epalSchool->name,
'region' => $regionColumn,
'admin' => $adminColumn,
'percTotal' => $percTotal,
'percA' => $perc[0],
'percB' => $perc[1],
'percC' => $perc[2],
'percD' => $perc[3]
));
}
try {
return $this->respondWithStatus($list, Response::HTTP_OK);
} //end try
if (!$request->isMethod('GET')) {
return $this->respondWithStatus([
"message" => t("Method Not Allowed")
], Response::HTTP_METHOD_NOT_ALLOWED);
}
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return $this->respondWithStatus([
"message" => t("An unexpected problem occured in makeReportCompleteness Method") .
$e->getMessage()
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
//user validation
$authToken = $request->headers->get('PHP_AUTH_USER');
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
$user = reset($users);
if (!$user) {
return $this->respondWithStatus([
'message' => t("User not found"),
], Response::HTTP_FORBIDDEN);
}
//user role validation
$roles = $user->getRoles();
$validRole = false;
foreach ($roles as $role)
if ( $role === "regioneduadmin" || $role === "eduadmin") {
$validRole = true;
break;
}
if (!$validRole) {
return $this->respondWithStatus([
'message' => t("User Invalid Role"),
], Response::HTTP_FORBIDDEN);
}
public function makeReportAllStat(Request $request, $regionId, $adminId, $schId, $classId, $sectorId, $courseId, $finalized)
{
try {
if (!$request->isMethod('GET')) {
return $this->respondWithStatus([
'message' => t("retrieve ID successful"),
'id' => $user->init->value,
], Response::HTTP_OK);
}
"message" => t("Method Not Allowed")
], Response::HTTP_METHOD_NOT_ALLOWED);
}
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
//user validation
$authToken = $request->headers->get('PHP_AUTH_USER');
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
$user = reset($users);
if (!$user) {
return $this->respondWithStatus([
"message" => t("An unexpected problem occured in retrievePDEId Method")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
public function makeReportNoCapacity(Request $request, $capacityEnabled) {
try {
if (!$request->isMethod('GET')) {
return $this->respondWithStatus([
"message" => t("Method Not Allowed")
], Response::HTTP_METHOD_NOT_ALLOWED);
}
//user validation
$authToken = $request->headers->get('PHP_AUTH_USER');
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
$user = reset($users);
if (!$user) {
return $this->respondWithStatus([
'message' => t("User not found"),
], Response::HTTP_FORBIDDEN);
}
//user role validation
$roles = $user->getRoles();
$validRole = false;
foreach ($roles as $role)
if ($role === "ministry") {
$validRole = true;
break;
}
if (!$validRole) {
return $this->respondWithStatus([