Merge branch 'reports-check2' into 'develop'

Change report completeness

See merge request !194
parents 22dd4e6f 669d817c
......@@ -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);
}
public function makeGeneralReport(Request $request)
{
//υπολογισμός αριθμού δηλώσεων
$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());
}
// υπολογισμός αριθμού δηλώσεων που ΔΕΝ ικανοποιήθηκαν
/*
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'));
......@@ -207,859 +211,841 @@ class ReportsCreator extends ControllerBase {
->condition('eStudent.id', $studentIds, 'NOT IN');
$numNoAllocated = $sCon->countQuery()->execute()->fetchField();
*/
$sCon = $this->connection->select('epal_student', 'epalStudent');
$sCon->leftJoin('epal_student_class', 'eStudent', 'eStudent.student_id = epalStudent.id');
$sCon->fields('eStudent', array('student_id'))
->fields('epalStudent', array('id'))
->isNull('eStudent.student_id');
$numNoAllocated = $sCon->countQuery()->execute()->fetchField();
//υπολογισμός αριθμού δηλώσεων που τοποθετήθηκαν προσωρινά σε ολιγομελή τμήματα
$numInSmallClasses = 0;
$sCon = $this->connection
->select('epal_student_class', 'eStudent')
->fields('eStudent', array('id'))
->condition('eStudent.finalized', 0 , '=');
$numInSmallClasses = $sCon->countQuery()->execute()->fetchField();
$list = array(
array('name' => "Αριθμός Δηλώσεων Προτίμησης", 'numStudents' => $numTotal),
array('name' => "Αριθμός μαθητών που τοποθετήθηκαν στην πρώτη τους προτίμηση", 'numStudents' => $numData[0]),
array('name' => "Αριθμός μαθητών που τοποθετήθηκαν στην δεύτερή τους προτίμηση", 'numStudents' => $numData[1]),
array('name' => "Αριθμός μαθητών που τοποθετήθηκαν στην τρίτη τους προτίμηση", 'numStudents' => $numData[2]),
array('name' => "Αριθμός μαθητών που δεν τοποθετήθηκαν σε καμμία τους προτίμηση", 'numStudents' => $numNoAllocated),
array('name' => "Αριθμός μαθητών που τοποθετήθηκαν προσωρινά σε ολιγομελή τμήματα", 'numStudents' => $numInSmallClasses)
);
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 makeReportCompleteness(Request $request, $regionId, $adminId, $schId) {
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" || $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')
->fields('eSchool', array('id', 'name', 'capacity_class_a', 'region_edu_admin_id', 'edu_admin_id', 'operation_shift'));
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) {
//εύρεση ονόματος ΠΔΕ που ανήκει το σχολείο
$sCon = $this->connection->select('eepal_region_field_data', 'eRegion')
->fields('eRegion', array('id','name'))
->condition('eRegion.id', $epalSchool->region_edu_admin_id, '=');
$epalRegions = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$epalRegion = reset($epalRegions);
$regionColumn = $epalRegion->name;
//εύρεση ονόματος ΔΙΔΕ που ανήκει το σχολείο
$sCon = $this->connection->select('eepal_admin_area_field_data', 'eAdmin')
->fields('eAdmin', array('id','name'))
->condition('eAdmin.id', $epalSchool->edu_admin_id, '=');
$epalAdmins = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$epalAdmin = reset($epalAdmins);
$adminColumn = $epalAdmin->name;
//βρες μέγιστη χωρητικότητα για κάθε τάξη
$capacity = array();
//χωρητικότητα για Α' τάξη
array_push($capacity, $epalSchool->capacity_class_a * $limitUp );
//χωρητικότητα για Β' τάξη
$sCon = $this->connection->select('eepal_sectors_in_epal_field_data', 'eSchool')
->fields('eSchool', array('id', 'capacity_class_sector'))
->condition('eSchool.epal_id', $epalSchool->id , '=');
$sectorsInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$numClassSectors = 0;
foreach ($sectorsInEpals as $sectorsInEpal)
$numClassSectors += $sectorsInEpal->capacity_class_sector;
array_push($capacity, $numClassSectors * $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();
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 , '=')
->condition('eStudent.finalized', 1 , '=');
array_push( $num, $sCon->countQuery()->execute()->fetchField() );
//βρες ποσοστά συμπλήρωσης
array_push( $perc, number_format($num[$classId-1] / $capacity[$classId-1] * 100, 2));
}
$percTotal = number_format (array_sum($num) / array_sum($capacity) * 100, 2);
//αποστολή αποτελεσμάτων / στατιστικών
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]
));
}
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 in makeReportCompleteness Method")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
public function makeReportAllStat(Request $request, $regionId, $adminId, $schId, $classId, $sectorId, $courseId, $finalized) {
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" || $role === "regioneduadmin" || $role === "eduadmin") {
$validRole = true;
break;
}
if (!$validRole) {
return $this->respondWithStatus([
'message' => t("User Invalid Role"),
], Response::HTTP_FORBIDDEN);
}
$limitup = $this->retrieveUpLimit();
$list = array();
//βρες όλα τα σχολεία που πληρούν τα κριτήρια / φίλτρα
$sCon = $this->connection->select('eepal_school_field_data', 'eSchool')
->fields('eSchool', array('id', 'name', 'capacity_class_a', 'region_edu_admin_id', 'edu_admin_id','operation_shift', 'metathesis_region'));
//->condition('eSchool.region_edu_admin_id', $regionId, '=');
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) { //για κάθε σχολείο
$schoolNameColumn = array();
$regionColumn = array();
$adminColumn = array();
$schoolSectionColumn = array();
$numColumn = array();
$capacityColumn = array();
$percColumn = array();
$smallClass = array();
$numClassSec = 0;
$numClassCour = 0;
$numClassCour_D = 0;
//εύρεση ονόματος ΠΔΕ που ανήκει το σχολείο
$sCon = $this->connection->select('eepal_region_field_data', 'eRegion')
->fields('eRegion', array('id','name'))
->condition('eRegion.id', $epalSchool->region_edu_admin_id, '=');
$epalRegions = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$epalRegion = reset($epalRegions);
//εύρεση ονόματος ΔΙΔΕ που ανήκει το σχολείο
$sCon = $this->connection->select('eepal_admin_area_field_data', 'eAdmin')
->fields('eAdmin', array('id','name'))
->condition('eAdmin.id', $epalSchool->edu_admin_id, '=');
$epalAdmins = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$epalAdmin = reset(