ReportsCreator.php 36.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
<?php
/**
 * @file
 * Contains \Drupal\query_example\Controller\QueryExampleController.
 */

namespace Drupal\epal\Controller;

use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Controller\ControllerBase;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;

//use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\TypedData\Plugin\DataType\TimeStamp;

use Drupal\Core\Language\LanguageManagerInterface;

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)
    {
        return new static(
          $container->get('entity_type.manager'),
          $container->get('entity.query'),
          $container->get('database'),
          $container->get('logger.factory')
      );
    }


59
	public function makegGeneralReport(Request $request) {
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

			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'))
102 103
																		->condition('eStudent.distribution_id', $i+1, '=')
																		->condition('eStudent.finalized', 1 , '=');
104 105 106 107 108 109
					array_push($numData, $sCon->countQuery()->execute()->fetchField());
				}

				// υπολογισμός αριθμού αιτήσεων που ΔΕΝ ικανοποιήθηκαν
				//Σημείωση: υπολογισμός με queries στη βάση
				$sCon = $this->connection->select('epal_student_class', 'eStudent')
110
																	->fields('eStudent', array('student_id'));
111 112 113
				$epalStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
				$studentIds = array();
				foreach ($epalStudents as $epalStudent)
114
					array_push($studentIds, $epalStudent->student_id);
115 116 117 118 119
				$sCon = $this->connection->select('epal_student', 'eStudent')
																	->fields('eStudent', array('id'))
																	->condition('eStudent.id', $studentIds, 'NOT IN');
				$numNoAllocated = $sCon->countQuery()->execute()->fetchField();

120 121 122 123 124 125 126 127
				//υπολογισμός αριθμού αιτήσεων που τοποθετήθηκαν προσωρινά σε ολιγομελή τμήματα
				$numInSmallClasses = 0;
				$sCon = $this->connection->select('epal_student_class', 'eStudent')
																		->fields('eStudent', array('id'))
																		->condition('eStudent.finalized', 0 , '=');
				$numInSmallClasses = $sCon->countQuery()->execute()->fetchField();


128 129 130 131 132 133 134
				 $list = array();

				 array_push($list,(object) array('name' => "Αριθμός Αιτήσεων", 'numStudents' => $numTotal));
				 array_push($list,(object) array('name' => "Αριθμός μαθητών που τοποθετήθηκαν στην πρώτη τους προτίμηση", 'numStudents' => $numData[0]));
				 array_push($list,(object) array('name' => "Αριθμός μαθητών που τοποθετήθηκαν στην δεύτερή τους προτίμηση", 'numStudents' => $numData[1]));
				 array_push($list,(object) array('name' => "Αριθμός μαθητών που τοποθετήθηκαν στην τρίτη τους προτίμηση", 'numStudents' => $numData[2]));
				 array_push($list,(object) array('name' => "Αριθμός μαθητών που δεν τοποθετήθηκαν σε καμμία τους προτίμηση", 'numStudents' => $numNoAllocated));
135
				 array_push($list,(object) array('name' => "Αριθμός μαθητών που τοποθετήθηκαν προσωρινά σε ολιγομελή τμήματα", 'numStudents' => $numInSmallClasses));
136 137 138 139 140 141 142 143 144

				 return $this->respondWithStatus(
								 $list
						 , Response::HTTP_OK);
			}	 //end try

			catch (\Exception $e) {
				$this->logger->warning($e->getMessage());
				return $this->respondWithStatus([
145
							"message" => t("An unexpected problem occured during makegGeneralReport Method")
146 147 148 149 150 151
						], Response::HTTP_INTERNAL_SERVER_ERROR);
			}

		}


152
		public function makeReportCompleteness(Request $request, $regionId, $adminId, $schId) {
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

			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)
176
					if ($role === "ministry" || $role === "regioneduadmin" || $role === "eduadmin") {
177 178 179 180 181 182 183 184 185 186 187
						$validRole = true;
						break;
					}
				if (!$validRole) {
						return $this->respondWithStatus([
										'message' => t("User Invalid Role"),
								], Response::HTTP_FORBIDDEN);
				}

				$list = array();

188 189 190
				//βρες ανώτατο επιτρεπόμενο όριο μαθητών
				$limitUp = $this->retrieveUpLimit();

191
				//βρες όλα τα σχολεία που πληρούν τα κριτήρια / φίλτρα
192
				$sCon = $this->connection->select('eepal_school_field_data', 'eSchool')
193
																	->fields('eSchool', array('id', 'name', 'capacity_class_a', 'region_edu_admin_id', 'edu_admin_id', 'operation_shift'));
194 195 196 197 198 199
				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, '=');
200
				$epalSchools = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
201

202
				foreach ($epalSchools as $epalSchool)	{
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

					//εύρεση ονόματος ΠΔΕ που ανήκει το σχολείο
					$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;

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
					//βρες μέγιστη χωρητικότητα για κάθε τάξη
					$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;
241
					foreach ($specialtiesInEpals as $specialtiesInEpal)	{
242
						$numClassSpecialties += $specialtiesInEpal->capacity_class_specialty;
243
					}
244
					array_push($capacity,  $numClassSpecialties * $limitUp);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

					//χωρητικότητα για Δ' τάξη
					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);


261 262 263 264 265 266
					//χωρητικότητα για όλο το σχολείο
					$capacityTotal = array_sum($capacity);

					//βρες αριθμό μαθητών γισ κάθε τάξη
					$num = array();
					$perc = array();
267
					for ( $classId = 1; $classId <= 4; $classId++)	{
268 269 270
						$sCon = $this->connection->select('epal_student_class', 'eStudent')
																			->fields('eStudent', array('id', 'epal_id', 'currentclass'))
																			->condition('eStudent.epal_id', $epalSchool->id , '=')
271 272
																			->condition('eStudent.currentclass', $classId , '=')
																			->condition('eStudent.finalized', 1 , '=');
273 274
						array_push( $num, $sCon->countQuery()->execute()->fetchField() );
						//βρες ποσοστά συμπλήρωσης
275
						array_push( $perc, number_format($num[$classId-1] / $capacity[$classId-1] * 100, 2));
276 277
					}

278
					$percTotal = number_format (array_sum($num) / array_sum($capacity) * 100, 2);
279 280 281

					//αποστολή αποτελεσμάτων / στατιστικών
						array_push($list,(object) array(
282 283 284 285
							'name' => $epalSchool->name,
							'region' => $regionColumn,
							'admin' => $adminColumn,
							'percTotal' => $percTotal,
286 287
							'percA' => $perc[0],
							'percB' => $perc[1],
288 289 290
							'percC' => $perc[2],
							'percD' => $perc[3]
							));
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

				}

				 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);
			}

		}

308 309


310
		public function makeReportAllStat(Request $request, $regionId, $adminId, $schId, $classId, $sectorId, $courseId, $finalized)	{
311 312

			try  {
313

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
				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)
334
					if ($role === "ministry" || $role === "regioneduadmin" || $role === "eduadmin") {
335 336 337 338 339 340 341 342
						$validRole = true;
						break;
					}
				if (!$validRole) {
						return $this->respondWithStatus([
										'message' => t("User Invalid Role"),
								], Response::HTTP_FORBIDDEN);
				}
343

344 345
				$limitup = $this->retrieveUpLimit();

346 347
				$list = array();

348
				//βρες όλα τα σχολεία που πληρούν τα κριτήρια / φίλτρα
349
				$sCon = $this->connection->select('eepal_school_field_data', 'eSchool')
350
																	->fields('eSchool', array('id', 'name', 'capacity_class_a', 'region_edu_admin_id', 'edu_admin_id','operation_shift'));
351 352 353 354 355 356 357
																	//->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, '=');
358 359 360 361 362
				$epalSchools = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);

				foreach ($epalSchools as $epalSchool)	{		//για κάθε σχολείο

					$schoolNameColumn = array();
363 364
					$regionColumn = array();
					$adminColumn = array();
365 366
					$schoolSectionColumn = array();
					$numColumn = array();
367 368 369
					$capacityColumn = array();
					$percColumn = array();

370 371
					$numClassSec = 0;
					$numClassCour = 0;
372
					$numClassCour_D = 0;
373

374
					//εύρεση ονόματος ΠΔΕ που ανήκει το σχολείο
375 376 377 378 379 380
					$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);

381
					//εύρεση ονόματος ΔΙΔΕ που ανήκει το σχολείο
382 383 384 385 386 387
					$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);

388

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
					//εύρεση αριθμού μαθητών για κάθε ειδικότητα της Γ' τάξης
					if ($classId === "0" || $classId === "3")	{
								$sCon = $this->connection->select('eepal_specialties_in_epal_field_data', 'eSchool')
																					->fields('eSchool', array('specialty_id', 'capacity_class_specialty'))
																					->condition('eSchool.epal_id', $epalSchool->id , '=');

								if ($courseId !== "0")
									$sCon->condition('eSchool.specialty_id', $courseId, '=');

								$specialtiesInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);

								foreach ($specialtiesInEpals as $specialtiesInEpal)	{
										$sCon = $this->connection->select('eepal_specialty_field_data', 'eSpecialties')
																							->fields('eSpecialties', array('name'))
																							->condition('eSpecialties.id', $specialtiesInEpal->specialty_id , '=');

										if ($courseId === "0" && $sectorId !== "0")
											$sCon->condition('eSpecialties.sector_id', $sectorId, '=');

										$specialtiesNamesInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
										foreach ($specialtiesNamesInEpals as $specialtiesNamesInEpal)	{
												array_push($regionColumn, $epalRegion->name);
												array_push($adminColumn, $epalAdmin->name);
												array_push($schoolNameColumn, $epalSchool->name);
												array_push($schoolSectionColumn, 'Γ τάξη / ' . $specialtiesNamesInEpal->name );
												$sCon = $this->connection->select('epal_student_class', 'eStudent')
																									->fields('eStudent', array('id'))
																									->condition('eStudent.epal_id', $epalSchool->id , '=')
																									->condition('eStudent.currentclass', 3 , '=')
418 419 420 421
																									->condition('eStudent.specialization_id', $specialtiesInEpal->specialty_id , '=')
																									->condition('eStudent.finalized', $finalized , '=');
												//if ($finalized === 1)
												//	$sCon->condition('eStudent.finalized', 1 , '=');
422 423 424 425 426 427 428 429 430
												$numStud = $sCon->countQuery()->execute()->fetchField();
												array_push( $numColumn, $numStud );
												array_push($capacityColumn, $specialtiesInEpal->capacity_class_specialty * $limitup);

												array_push($percColumn, number_format($numStud / ($specialtiesInEpal->capacity_class_specialty * $limitup) * 100, 2 ));

										}	//end foreach $specialtiesNamesInEpal
										$numClassCour += $specialtiesInEpal->capacity_class_specialty;
								} //end foreach $specialtiesInEpals
431
					}	//end if
432

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

					//ΝΕΟ!!!
					//εύρεση αριθμού μαθητών για κάθε ειδικότητα της Δ' τάξης

					if ($epalSchool->operation_shift === "ΕΣΠΕΡΙΝΟ")	{

						if ($classId === "0" || $classId === "4")	{
								$sCon = $this->connection->select('eepal_specialties_in_epal_field_data', 'eSchool')
																					->fields('eSchool', array('specialty_id', 'capacity_class_specialty_d'))
																					->condition('eSchool.epal_id', $epalSchool->id , '=');

								if ($courseId !== "0")
									$sCon->condition('eSchool.specialty_id', $courseId, '=');

								$specialtiesInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);

								foreach ($specialtiesInEpals as $specialtiesInEpal)	{
										$sCon = $this->connection->select('eepal_specialty_field_data', 'eSpecialties')
																							->fields('eSpecialties', array('name'))
																							->condition('eSpecialties.id', $specialtiesInEpal->specialty_id , '=');

										if ($courseId === "0" && $sectorId !== "0")
											$sCon->condition('eSpecialties.sector_id', $sectorId, '=');

										$specialtiesNamesInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
										foreach ($specialtiesNamesInEpals as $specialtiesNamesInEpal)	{
												array_push($regionColumn, $epalRegion->name);
												array_push($adminColumn, $epalAdmin->name);
												array_push($schoolNameColumn, $epalSchool->name);
												array_push($schoolSectionColumn, 'Δ τάξη / ' . $specialtiesNamesInEpal->name );
												$sCon = $this->connection->select('epal_student_class', 'eStudent')
																									->fields('eStudent', array('id'))
																									->condition('eStudent.epal_id', $epalSchool->id , '=')
																									->condition('eStudent.currentclass', 4 , '=')
467 468 469 470
																									->condition('eStudent.specialization_id', $specialtiesInEpal->specialty_id , '=')
																									->condition('eStudent.finalized', $finalized , '=');
												//if ($finalized === 1)
												//	$sCon->condition('eStudent.finalized', 1 , '=');
471 472 473 474 475 476 477 478 479 480 481 482 483
												$numStud = $sCon->countQuery()->execute()->fetchField();
												array_push( $numColumn, $numStud );
												array_push($capacityColumn, $specialtiesInEpal->capacity_class_specialty_d * $limitup);
												array_push($percColumn, number_format($numStud / ($specialtiesInEpal->capacity_class_specialty_d * $limitup) * 100, 2 ));

										}	//end foreach $specialtiesNamesInEpal
										$numClassCour_D += $specialtiesInEpal->capacity_class_specialty_d;
								} //end foreach $specialtiesInEpals
					}	//end if "classid == 0 or 4"
				} //end if "ΕΣΠΕΡΙΝΟ"



484
					//εύρεση αριθμού μαθητών για κάθε τομέα της Β' τάξης
485 486
					if ($classId === "0" || $classId === "2")	{

487
					$sCon = $this->connection->select('eepal_sectors_in_epal_field_data', 'eSchool')
488
																		->fields('eSchool', array('sector_id','capacity_class_sector'))
489
																		->condition('eSchool.epal_id', $epalSchool->id , '=');
490 491 492 493

					if ($sectorId != "0")
						$sCon->condition('eSchool.sector_id', $sectorId, '=');

494
					$sectorsInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
495

496 497 498 499 500 501
					foreach ($sectorsInEpals as $sectorsInEpal)	{
						$sCon = $this->connection->select('eepal_sectors_field_data', 'eSectors')
																			->fields('eSectors', array('name'))
																			->condition('eSectors.id', $sectorsInEpal->sector_id , '=');
						$sectorsNamesInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
						foreach ($sectorsNamesInEpals as $sectorsNamesInEpal)	{
502 503
							array_push($regionColumn, $epalRegion->name);
							array_push($adminColumn, $epalAdmin->name);
504 505 506 507 508 509
							array_push($schoolNameColumn, $epalSchool->name);
							array_push($schoolSectionColumn, 'Β τάξη / ' . $sectorsNamesInEpal->name );
							$sCon = $this->connection->select('epal_student_class', 'eStudent')
																				->fields('eStudent', array('id'))
																				->condition('eStudent.epal_id', $epalSchool->id , '=')
																				->condition('eStudent.currentclass', 2 , '=')
510 511 512 513
																				->condition('eStudent.specialization_id', $sectorsInEpal->sector_id , '=')
																				->condition('eStudent.finalized', $finalized , '=');
							//if ($finalized === 1)
							//	$sCon->condition('eStudent.finalized', 1 , '=');
514 515 516 517
							$numStud = $sCon->countQuery()->execute()->fetchField();
							array_push( $numColumn, $numStud );
							array_push($capacityColumn, $sectorsInEpal->capacity_class_sector * $limitup);

518 519 520 521 522
							array_push($percColumn, number_format($numStud / ($sectorsInEpal->capacity_class_sector * $limitup) * 100, 2) );

						}	//end foreach sectorsNamesInEpals
						$numClassSec += $sectorsInEpal->capacity_class_sector;
					}	//end foreach sectorsInEpal
523
				}	//end if
524

525 526 527 528 529 530

				//εύρεση αριθμού μαθητών για κάθε τάξη
				$numClasses = array();
				array_push($numClasses, $epalSchool->capacity_class_a);
				array_push($numClasses,$numClassSec);
				array_push($numClasses, $numClassCour);
531
				array_push($numClasses, $numClassCour_D);
532

533
				if ($sectorId === "0" && $courseId === "0")	{
534 535

					$clidstart = 1;
536
					$clidend = 4;
537 538 539
					//Για ολιγομελή τμήματα, μην εμφανίζεις συνολικά τις Β', Γ', Δ' τάξεις ???
					//if ($finalized === "0")
					//	$clidend = 1;
540 541 542 543 544 545 546 547 548 549

					if ($classId !== "0")	{
						$clidstart = $classId;
						$clidend = $classId;
						if ($classId === "1")
							array_push($schoolSectionColumn, 'Α τάξη');
						else if ($classId === "2")
							array_push($schoolSectionColumn, 'Β τάξη');
						else if ($classId === "3")
							array_push($schoolSectionColumn, 'Γ τάξη');
550 551
						else if ($classId === "4")
							array_push($schoolSectionColumn, 'Δ τάξη');
552 553 554 555 556
					}
					else {
						array_push($schoolSectionColumn, 'Α τάξη');
						array_push($schoolSectionColumn, 'Β τάξη');
						array_push($schoolSectionColumn, 'Γ τάξη');
557
						array_push($schoolSectionColumn, 'Δ τάξη');
558 559 560 561 562 563
					}

					for ( $clId = $clidstart; $clId <= $clidend; $clId++)	{
						$sCon = $this->connection->select('epal_student_class', 'eStudent')
																			->fields('eStudent', array('id', 'epal_id', 'currentclass'))
																			->condition('eStudent.epal_id', $epalSchool->id , '=')
564 565 566 567
																			->condition('eStudent.currentclass', $clId , '=')
																			->condition('eStudent.finalized', $finalized , '=');
						//if ($finalized === 1)
						//		$sCon->condition('eStudent.finalized', 1 , '=');
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
						$numStud =  $sCon->countQuery()->execute()->fetchField();
						array_push($schoolNameColumn, $epalSchool->name);
						array_push($regionColumn, $epalRegion->name);
						array_push($adminColumn, $epalAdmin->name);
						array_push($numColumn, $numStud);
						//Modify here!
						//array_push($capacityColumn, $epalSchool->capacity_class_a * $limitup);
						//array_push($percColumn, number_format($numStud / ($epalSchool->capacity_class_a * $limitup) * 100, 2) . '%' );


						array_push($capacityColumn, $numClasses[$clId-1] * $limitup);
						array_push($percColumn, number_format($numStud / ($numClasses[$clId-1] * $limitup) * 100, 2) );

					}

583
				}	//end if
584 585


586
				for ($j = 0; $j < sizeof($schoolNameColumn); $j++)	{
587 588 589 590 591
						array_push($list,(object) array(
							'name' => $schoolNameColumn[$j],
							'region' => $regionColumn[$j],
							'admin' => $adminColumn[$j],
							'section' => $schoolSectionColumn[$j],
592 593 594 595
							'num' => $numColumn[$j],
							'capacity' => $capacityColumn[$j],
							'percentage' => $percColumn[$j],
						));
596 597 598 599 600 601 602 603 604 605
					}


				}	//end foreach school


				return $this->respondWithStatus(
								$list
						, Response::HTTP_OK);

606
			}	//end try
607

608 609 610
			catch (\Exception $e) {
				$this->logger->warning($e->getMessage());
				return $this->respondWithStatus([
611
							"message" => t("An unexpected problem occured in makeReportCompleteness Method")
612 613 614
						], Response::HTTP_INTERNAL_SERVER_ERROR);
			}

615 616 617 618




619 620 621
		}


622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
		public function retrieveUserRegistryNo(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)
646
					if ( $role === "regioneduadmin" || $role === "eduadmin") {
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
						$validRole = true;
						break;
					}
				if (!$validRole) {
						return $this->respondWithStatus([
										'message' => t("User Invalid Role"),
								], Response::HTTP_FORBIDDEN);
				}

				return $this->respondWithStatus([
						'message' => t("retrieve ID successful"),
						'id' => $user->init->value,
				], Response::HTTP_OK);

			}

			catch (\Exception $e) {
				$this->logger->warning($e->getMessage());
				return $this->respondWithStatus([
							"message" => t("An unexpected problem occured in retrievePDEId Method")
						], Response::HTTP_INTERNAL_SERVER_ERROR);
			}


671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
		}

		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([
										'message' => t("User Invalid Role"),
								], Response::HTTP_FORBIDDEN);
				}

				$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'));

				//if ($capacityEnabled === "0")
				//	$sCon->condition('eSchool.capacity_class_a', 0, '=');

				$epalSchools = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);

				foreach ($epalSchools as $epalSchool)	{		//για κάθε σχολείο

					$schoolNameColumn = array();
					$regionColumn = array();
					$adminColumn = array();
					$schoolSectionColumn = array();
					$capacityColumn = array();

					//εύρεση ονόματος ΠΔΕ που ανήκει το σχολείο
					$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($epalAdmins);


					//εύρεση αριθμού τμημάτων (χωρητικότητα) για κάθε τμήμα της Α' τάξης
741 742
					//$epalSchool->capacity_class_a === "0" ||
					if ( ($capacityEnabled === '0' && ( !isset($epalSchool->capacity_class_a)) )  ||  ($capacityEnabled === "1")  )	{
743 744 745 746 747 748 749 750
							array_push($regionColumn, $epalRegion->name);
							array_push($adminColumn, $epalAdmin->name);
							array_push($schoolNameColumn, $epalSchool->name);
							array_push($schoolSectionColumn, 'Α\' τάξη');
							array_push($capacityColumn, $epalSchool->capacity_class_a);
					}

					//εύρεση αριθμού τμημάτων (χωρητικότητα) για κάθε τομέα της Β' τάξης
751
					//ΠΡΟΣΟΧΗ: χειρισμ΄ός τιμών: 0 (ΟΧΙ??) και null
752 753 754 755 756 757 758 759 760 761

					$sCon = $this->connection->select('eepal_sectors_in_epal_field_data', 'eSchool')
																		->fields('eSchool', array('sector_id','capacity_class_sector'))
																		->condition('eSchool.epal_id', $epalSchool->id , '=');
					//$db_or = db_or();
					//$db_or->condition('eSchool.capacity_class_sector', 0, '=');
					//$db_or->condition('eSchool.capacity_class_sector', null, 'is');
					//$sCon->condition($db_or) ;
					if ($capacityEnabled === "0")	{
						$sCon->condition( db_or()
762
															//->condition('eSchool.capacity_class_sector', 0, '=')
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
															->condition('eSchool.capacity_class_sector', null, 'is')  ) ;
					}
					$sectorsInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);

					foreach ($sectorsInEpals as $sectorsInEpal)	{
						$sCon = $this->connection->select('eepal_sectors_field_data', 'eSectors')
																			->fields('eSectors', array('name'))
																			->condition('eSectors.id', $sectorsInEpal->sector_id , '=');

						$sectorsNamesInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
						foreach ($sectorsNamesInEpals as $sectorsNamesInEpal)	{
							array_push($regionColumn, $epalRegion->name);
							array_push($adminColumn, $epalAdmin->name);
							array_push($schoolNameColumn, $epalSchool->name);
							array_push($schoolSectionColumn, 'Β\' τάξη / ' . $sectorsNamesInEpal->name );
							array_push($capacityColumn, $sectorsInEpal->capacity_class_sector);
						}	//end foreach sectorsNamesInEpals

					}	//end foreach sectorsInEpal

					//εύρεση αριθμού τμημάτων (χωρητικότητα) για κάθε ειδικότητα της Γ' τάξης

					$sCon = $this->connection->select('eepal_specialties_in_epal_field_data', 'eSchool')
																					->fields('eSchool', array('specialty_id', 'capacity_class_specialty'))
																					->condition('eSchool.epal_id', $epalSchool->id , '=');

					if ($capacityEnabled === "0")	{
							$sCon->condition( db_or()
791
																//->condition('eSchool.capacity_class_specialty', 0, '=')
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
																->condition('eSchool.capacity_class_specialty', null, 'is')  ) ;
						}

					$specialtiesInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);

					foreach ($specialtiesInEpals as $specialtiesInEpal)	{
							$sCon = $this->connection->select('eepal_specialty_field_data', 'eSpecialties')
																							->fields('eSpecialties', array('name'))
																							->condition('eSpecialties.id', $specialtiesInEpal->specialty_id , '=');

							$specialtiesNamesInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
							foreach ($specialtiesNamesInEpals as $specialtiesNamesInEpal)	{
												array_push($regionColumn, $epalRegion->name);
												array_push($adminColumn, $epalAdmin->name);
												array_push($schoolNameColumn, $epalSchool->name);
												array_push($schoolSectionColumn, 'Γ\' τάξη / ' . $specialtiesNamesInEpal->name );
												array_push($capacityColumn, $specialtiesInEpal->capacity_class_specialty);

							}	//end foreach $specialtiesNamesInEpal

					} //end foreach $specialtiesInEpals


					//εύρεση αριθμού τμημάτων (χωρητικότητα) για κάθε ειδικότητα της Δ' τάξης

					$sCon = $this->connection->select('eepal_specialties_in_epal_field_data', 'eSchool')
																					->fields('eSchool', array('specialty_id', 'capacity_class_specialty_d'))
																					->condition('eSchool.epal_id', $epalSchool->id , '=');

					if ($capacityEnabled === "0")	{
							$sCon->condition( db_or()
823
																//->condition('eSchool.capacity_class_specialty_d', 0, '=')
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
																->condition('eSchool.capacity_class_specialty_d', null, 'is')  ) ;
						}

					$specialtiesInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);

					foreach ($specialtiesInEpals as $specialtiesInEpal)	{
							$sCon = $this->connection->select('eepal_specialty_field_data', 'eSpecialties')
																							->fields('eSpecialties', array('name'))
																							->condition('eSpecialties.id', $specialtiesInEpal->specialty_id , '=');

							$specialtiesNamesInEpals = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
							foreach ($specialtiesNamesInEpals as $specialtiesNamesInEpal)	{
												if ($epalSchool->operation_shift === "ΕΣΠΕΡΙΝΟ")	{
													array_push($regionColumn, $epalRegion->name);
													array_push($adminColumn, $epalAdmin->name);
													array_push($schoolNameColumn, $epalSchool->name);
													array_push($schoolSectionColumn, 'Δ\' τάξη / ' . $specialtiesNamesInEpal->name );
													array_push($capacityColumn, $specialtiesInEpal->capacity_class_specialty_d);
												}

							}	//end foreach $specialtiesNamesInEpal

					} //end foreach $specialtiesInEpals


					//εισαγωγή εγγραφών στο tableschema
					for ($j = 0; $j < sizeof($schoolNameColumn); $j++)	{
										array_push($list,(object) array(
											'name' => $schoolNameColumn[$j],
											'region' => $regionColumn[$j],
											'admin' => $adminColumn[$j],
											'section' => $schoolSectionColumn[$j],
											'capacity' => $capacityColumn[$j],
										));
									}


				} //end foreach school




				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 makeReportNoCapacity Method")
 					], Response::HTTP_INTERNAL_SERVER_ERROR);
 		}

880 881
		}

882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
		public function retrieveUpLimit()	{

			//βρες ανώτατο επιτρεπόμενο όριο μαθητών
			//$limitup = 1;
			try {
				$sCon = $this->connection->select('epal_class_limits', 'eSchool')
																	->fields('eSchool', array('name', 'limit_up'))
																	->condition('eSchool.name', '1' , '=');
				$epalLimitUps = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
				$epalLimitUp = reset($epalLimitUps);
				//$limitup = $epalLimitUp->limit_up;
				//return $limitup;
				return $epalLimitUp->limit_up;
			}
			catch (\Exception $e) {
				$this->logger->warning($e->getMessage());
				return -1;
			}
900 901 902 903 904 905 906 907 908 909 910
		}

		private function respondWithStatus($arr, $s) {
					$res = new JsonResponse($arr);
					$res->setStatusCode($s);
					return $res;
			}




911 912


913
}