ReportsCreator.php 38.4 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
<?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;

27
28
29
30
define("ERR_DB", -1);
define("NO_CLASS_LIM_DOWN", -2);
define("SMALL_CLS", 1);
define("NON_SMALL_CLS", 2);
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
59
60
61
62
63
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')
      );
    }


64
	public function makegGeneralReport(Request $request) {
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

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

97
				//υπολογισμός αριθμού δηλώσεων
98
99
100
101
				$sCon = $this->connection->select('epal_student', 'eStudent')
																	->fields('eStudent', array('id'));
				$numTotal = $sCon->countQuery()->execute()->fetchField();

102
				//υπολογισμός αριθμού δηλώσεων που ικανοποιήθηκαν στην i προτίμηση
103
104
105
106
				$numData = array();
				for ($i=0; $i < 3; $i++)	{
					$sCon = $this->connection->select('epal_student_class', 'eStudent')
																		->fields('eStudent', array('id', 'distribution_id'))
107
108
																		->condition('eStudent.distribution_id', $i+1, '=')
																		->condition('eStudent.finalized', 1 , '=');
109
110
111
					array_push($numData, $sCon->countQuery()->execute()->fetchField());
				}

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

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


133
134
				 $list = array();

135
				 array_push($list,(object) array('name' => "Αριθμός Δηλώσεων Προτίμησης", 'numStudents' => $numTotal));
136
137
138
139
				 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));
140
				 array_push($list,(object) array('name' => "Αριθμός μαθητών που τοποθετήθηκαν προσωρινά σε ολιγομελή τμήματα", 'numStudents' => $numInSmallClasses));
141
142
143
144
145
146
147
148
149

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

			catch (\Exception $e) {
				$this->logger->warning($e->getMessage());
				return $this->respondWithStatus([
150
							"message" => t("An unexpected problem occured during makegGeneralReport Method")
151
152
153
154
155
156
						], Response::HTTP_INTERNAL_SERVER_ERROR);
			}

		}


157
		public function makeReportCompleteness(Request $request, $regionId, $adminId, $schId) {
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

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

				$list = array();

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

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

207
				foreach ($epalSchools as $epalSchool)	{
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

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

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

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


266
267
268
269
270
271
					//χωρητικότητα για όλο το σχολείο
					$capacityTotal = array_sum($capacity);

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

283
					$percTotal = number_format (array_sum($num) / array_sum($capacity) * 100, 2);
284
285
286

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

				}

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

		}

313
314


315
		public function makeReportAllStat(Request $request, $regionId, $adminId, $schId, $classId, $sectorId, $courseId, $finalized)	{
316
317

			try  {
318

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

349
350
				$limitup = $this->retrieveUpLimit();

351
352
				$list = array();

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

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

					$schoolNameColumn = array();
368
369
					$regionColumn = array();
					$adminColumn = array();
370
371
					$schoolSectionColumn = array();
					$numColumn = array();
372
373
374
					$capacityColumn = array();
					$percColumn = array();

375
376
					$smallClass = array();

377
378
					$numClassSec = 0;
					$numClassCour = 0;
379
					$numClassCour_D = 0;
380

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

388
					//εύρεση ονόματος ΔΙΔΕ που ανήκει το σχολείο
389
390
391
392
393
394
					$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);

395

396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
					//εύρεση αριθμού μαθητών για κάθε τομέα της Β' τάξης
					if ($classId === "0" || $classId === "2")	{

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

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

					$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 );
								$sCon = $this->connection->select('epal_student_class', 'eStudent')
																					->fields('eStudent', array('id'))
																					->condition('eStudent.epal_id', $epalSchool->id , '=')
																					->condition('eStudent.currentclass', 2 , '=')
																					->condition('eStudent.specialization_id', $sectorsInEpal->sector_id , '=');
																					//->condition('eStudent.finalized', $finalized , '=');
								$numStud = $sCon->countQuery()->execute()->fetchField();

								$smCl = $this->isSmallClass($epalSchool->id, $numStud, "2", $sectorsInEpal->sector_id, $epalSchool->metathesis_region);
								array_push($smallClass, $smCl);

								array_push( $numColumn, $numStud );
								array_push($capacityColumn, $sectorsInEpal->capacity_class_sector * $limitup);

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

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

					}	//end if


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
467
468
469
					//εύρεση αριθμού μαθητών για κάθε ειδικότητα της Γ' τάξης
					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 , '=')
470
471
																									->condition('eStudent.specialization_id', $specialtiesInEpal->specialty_id , '=');
																									//->condition('eStudent.finalized', $finalized , '=');
472
												$numStud = $sCon->countQuery()->execute()->fetchField();
473
474
475
476
477

												$smCl = $this->isSmallClass($epalSchool->id, $numStud, "3", $specialtiesInEpal->specialty_id, $epalSchool->metathesis_region);
												//print_r(" SCHOOL_ID:" . $epalSchool->id, . " NumStudents: " . $numStud);
												array_push($smallClass, $smCl);

478
479
480
481
482
483
484
485
												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
486
					}	//end if
487

488

489

490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
					//εύρεση αριθμού μαθητών για κάθε ειδικότητα της Δ' τάξης

					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 , '=')
522
523
																									->condition('eStudent.specialization_id', $specialtiesInEpal->specialty_id , '=');
																									//->condition('eStudent.finalized', $finalized , '=');
524
												$numStud = $sCon->countQuery()->execute()->fetchField();
525
526
527
528

												$smCl = $this->isSmallClass($epalSchool->id, $numStud, "4", $specialtiesInEpal->specialty_id, $epalSchool->metathesis_region);
												array_push($smallClass, $smCl);

529
530
531
532
533
534
535
536
537
538
539
540
												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 "ΕΣΠΕΡΙΝΟ"



541

542

543
544

				//εύρεση αριθμού μαθητών για κάθε τάξη
545

546
547
548
549
				$numClasses = array();
				array_push($numClasses, $epalSchool->capacity_class_a);
				array_push($numClasses,$numClassSec);
				array_push($numClasses, $numClassCour);
550
				array_push($numClasses, $numClassCour_D);
551

552
				if ($sectorId === "0" && $courseId === "0")	{
553

554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
						$clidstart = 1;
						$clidend = 4;

						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, 'Γ τάξη');
							else if ($classId === "4")
								array_push($schoolSectionColumn, 'Δ τάξη');
						}
						else {
570
571
572
							array_push($schoolSectionColumn, 'Α τάξη');
							array_push($schoolSectionColumn, 'Β τάξη');
							array_push($schoolSectionColumn, 'Γ τάξη');
573
							array_push($schoolSectionColumn, 'Δ τάξη');
574
						}
575

576
577
578
579
580
581
582
						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 , '=')
																				->condition('eStudent.currentclass', $clId , '=');
																				//->condition('eStudent.finalized', $finalized , '=');
							$numStud =  $sCon->countQuery()->execute()->fetchField();
583

584
585
							$smCl = $this->isSmallClass($epalSchool->id, $numStud, "1", "-1", $epalSchool->metathesis_region);
							array_push($smallClass, $smCl);
586

587
588
589
590
591
592
							array_push($schoolNameColumn, $epalSchool->name);
							array_push($regionColumn, $epalRegion->name);
							array_push($adminColumn, $epalAdmin->name);
							array_push($numColumn, $numStud);
							array_push($capacityColumn, $numClasses[$clId-1] * $limitup);
							array_push($percColumn, number_format($numStud / ($numClasses[$clId-1] * $limitup) * 100, 2) );
593

594
						}
595

596
597
598
599
600
601
602
603
604
605
				} //endif






					for ($j = 0; $j < sizeof($schoolNameColumn); $j++)	{

						//αν έγινε αίτημα για εμφάνιση ολιγομελών και είναι το τρέχον τμήμα ολιγομελές
606
607
						if ( ($finalized === "1") ||  ($finalized === "0" && $smallClass[$j] === SMALL_CLS
										&&  $schoolSectionColumn[$j] !== "Β τάξη" && $schoolSectionColumn[$j] !== "Γ τάξη" &&  $schoolSectionColumn[$j] !== "Δ τάξη"  ) )
608
609
610
611
612
613
614
615
616
617
618

								array_push($list,(object) array(
									'name' => $schoolNameColumn[$j],
									'region' => $regionColumn[$j],
									'admin' => $adminColumn[$j],
									'section' => $schoolSectionColumn[$j],
									'num' => $numColumn[$j],
									'capacity' => $capacityColumn[$j],
									'percentage' => $percColumn[$j],
								));
						}
619
620
621
622
623
624
625
626
627


				}	//end foreach school


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

628
			}	//end try
629

630
631
632
			catch (\Exception $e) {
				$this->logger->warning($e->getMessage());
				return $this->respondWithStatus([
633
							"message" => t("An unexpected problem occured in makeReportCompleteness Method")
634
635
636
						], Response::HTTP_INTERNAL_SERVER_ERROR);
			}

637

638
639
640
641
642
643
644
645
		}




		private function isSmallClass($schoolId, $numStud, $classId, $sectorOrcourseId, $regionId)	{

			$limitDown = $this->retrieveLimitDown($classId, $regionId);
646

647
648
649
650
			if ($limitDown === NO_CLASS_LIM_DOWN)
				return NO_CLASS_LIM_DOWN;
			else if ($limitDown === ERR_DB)
				return ERR_DB;
651

652
653
			$numStudents = (int) $numStud;
			if ( ($numStudents < $limitDown) /*&& ($numStudents > 0)*/ )
654
				return SMALL_CLS;
655
			else
656
				return NON_SMALL_CLS;
657

658
659
660
		}


661
662
663
664
665
666
667
668
669
670
671
672
673
		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 {
674
					return NO_CLASS_LIM_DOWN;
675
676
677
678
				}
			}	//end try
			catch (\Exception $e) {
				$this->logger->warning($e->getMessage());
679
				return ERR_DB;
680
681
682
683
684
685
686
687
688
			}

		}	//end function






689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
		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)
713
					if ( $role === "regioneduadmin" || $role === "eduadmin") {
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
						$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);
			}


738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
		}

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


					//εύρεση αριθμού τμημάτων (χωρητικότητα) για κάθε τμήμα της Α' τάξης
808
809
					//$epalSchool->capacity_class_a === "0" ||
					if ( ($capacityEnabled === '0' && ( !isset($epalSchool->capacity_class_a)) )  ||  ($capacityEnabled === "1")  )	{
810
811
812
813
814
815
816
817
							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);
					}

					//εύρεση αριθμού τμημάτων (χωρητικότητα) για κάθε τομέα της Β' τάξης
818
					//ΠΡΟΣΟΧΗ: χειρισμ΄ός τιμών: 0 (ΟΧΙ??) και null
819
820
821
822
823
824
825
826
827
828

					$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()
829
															//->condition('eSchool.capacity_class_sector', 0, '=')
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
															->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()
858
																//->condition('eSchool.capacity_class_specialty', 0, '=')
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
																->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()
890
																//->condition('eSchool.capacity_class_specialty_d', 0, '=')
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
																->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);
 		}

947
948
		}

949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
		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;
			}
967
968
969
970
971
972
973
974
975
976
977
		}

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




978
979


980
}