Distribution.php 26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?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;
16
use Drupal\Core\Database\Database;
17 18 19 20 21
use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;

22 23 24 25 26
//use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\TypedData\Plugin\DataType\TimeStamp;

use Drupal\Core\Language\LanguageManagerInterface;

27 28 29 30 31 32
class Distribution extends ControllerBase {

	protected $entity_query;
  protected $entityTypeManager;
  protected $logger;
  protected $connection;
33 34
	protected $language;
	protected $currentuser;
35 36 37

	protected $pendingStudents = array();
	protected $choice_id = 1;
38
	protected $globalCounterId = 1;
39 40 41 42 43

	public function __construct(
		EntityTypeManagerInterface $entityTypeManager,
		QueryFactory $entity_query,
		Connection $connection,
44

45 46 47 48
		LoggerChannelFactoryInterface $loggerChannel)
		{
			$this->entityTypeManager = $entityTypeManager;
			$this->entity_query = $entity_query;
49
			$connection = Database::getConnection();
50
			$this->connection = $connection;
51 52 53 54
			$language =  \Drupal::languageManager()->getCurrentLanguage()->getId();
			$this->language = $language;
			$currentuser = \Drupal::currentUser()->id();
			$this->currentuser = $currentuser;
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
			$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')
      );
    }


	public function createDistribution(Request $request) {

71 72 73
		$numDistributions = 3;
		$sizeOfBlock = 100000;

74
		//POST method is checked
75 76 77 78
		if (!$request->isMethod('POST')) {
			return $this->respondWithStatus([
					"message" => t("Method Not Allowed")
				], Response::HTTP_METHOD_NOT_ALLOWED);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    }

		//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);
		}
104 105


106 107 108
		$transaction = $this->connection->startTransaction();

		try {
109

110 111
			//initialize/empty epal_student_class if there are already data in it!
			$this->connection->delete('epal_student_class')->execute();
112

113 114
			//$limitUp_class = $this->retrieveCapacityLimitUp("Α");
			$limitUp_class = $this->retrieveCapacityLimitUp("1");
115
			//print_r("<br> ΑΝΩΤΑΤΟ ΟΡΙΟ ΜΑΘΗΤΩΝ: " . $limitUp_class);
116

117 118
			while ($this->choice_id <= $numDistributions)	 {

119
				//print_r("<br>ΠΕΡΑΣΜΑ: " . $this->choice_id);
120 121 122

				//υπολογισμός πλήθους non-finalized αιτήσεων για να καθοριστεί ο αριθμός των fetches που θα κάνουμε με συγκεκριμένο sizeOfBlock
				if ($this->choice_id === 1)	{
123 124 125
					$sCon = $this->connection->select('epal_student', 'eStudent')
																		->fields('eStudent', array('id'));
			 	  $numData = $sCon->countQuery()->execute()->fetchField();
126
					//print_r("<br>numData: " .  $numData);
127 128 129 130 131 132
				}

				$j = 1;
				$num = 1;
				if ($this->choice_id === 1) {
							while ($num <= $numData)	{
133

134
								//print_r("<br>FETCH: " .  $j);
135 136 137 138 139 140
								$sCon = $this->connection->select('epal_student', 'eStudent')
																					->fields('eStudent', array('id', 'name', 'currentclass', 'currentepal', 'points'))
																			    ->condition('eStudent.id', 1+ $sizeOfBlock*($j-1), '>=')
																					->condition('eStudent.id', $j*$sizeOfBlock, '<=');
								$epalStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);

141 142
								$this->locateStudent($this->choice_id, $epalStudents);

143
								$num = $num + sizeof($epalStudents);
144 145 146 147
								$j = $j + 1;
							}
						}
				else {
148 149 150 151 152 153 154 155 156 157 158 159

								if (sizeof($this->pendingStudents) != 0)	{
									$sCon = $this->connection->select('epal_student', 'eStudent')
																						->fields('eStudent', array('id', 'name', 'currentclass', 'currentepal', 'points'))
																						->condition('eStudent.id', $this->pendingStudents, 'IN');
									$epalStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);

									$this->locateStudent($this->choice_id, $epalStudents);
								}
								else {	//αν δεν υπάρχουν εκκρεμότητες, μην συνεχίζεις με άλλο πέρασμα
									break;
								}
160 161 162 163 164 165
						}

				//Για κάθε σχολείο βρες τα τμήματα
				//Για κάθε τμήμα βρες αν χωράνε και διευθέτησε (checkCapacityAndArrange)
				//checkCapacityAndArrange (school_id, class_id, sectorORcourse_id, limitUp, schoolCapacity)

166 167 168 169 170
				$sCon = $this->connection->select('eepal_school_field_data', 'eSchool')
																	->fields('eSchool', array('id', 'capacity_class_a'));
																	//->condition('eSchool.id', 151, '>=')
																	//->condition('eSchool.id', 153, '<=');	//προσαρμοσμένο για τα demo data --> να αλλάξει
				$eepalSchools = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
171 172 173

				foreach ($eepalSchools as $eepalSchool)	{

174
						$this->checkCapacityAndArrange($eepalSchool->id, "1", "-1", $limitUp_class, $eepalSchool->capacity_class_a);
175

176 177 178 179
						$sCon = $this->connection->select('eepal_sectors_in_epal_field_data', 'eSchool')
																			->fields('eSchool', array('epal_id', 'sector_id', 'capacity_class_sector'))
																			->condition('eSchool.epal_id', $eepalSchool->id, '=');
						$eepalSectorsInEpal = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
180
						foreach ($eepalSectorsInEpal as $eepalSecInEp)	{
181
							$this->checkCapacityAndArrange($eepalSchool->id, "2", $eepalSecInEp->sector_id, $limitUp_class, $eepalSecInEp->capacity_class_sector);
182 183
					  }

184 185 186 187
						$sCon = $this->connection->select('eepal_specialties_in_epal_field_data', 'eSchool')
																			->fields('eSchool', array('epal_id', 'specialty_id', 'capacity_class_specialty'))
																			->condition('eSchool.epal_id', $eepalSchool->id, '=');
						$eepalSpecialtiesInEpal = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
188
						foreach ($eepalSpecialtiesInEpal as $eepalSpecialInEp)	{
189
							$this->checkCapacityAndArrange($eepalSchool->id, "3", $eepalSpecialInEp->specialty_id, $limitUp_class, $eepalSpecialInEp->capacity_class_specialty);
190 191 192 193 194 195 196 197
						}

					} //end for each school/department

					$this->choice_id++;

	  	} //end while

198

199 200 201 202 203
		}	//end try

		catch (\Exception $e) {
			$this->logger->warning($e->getMessage());
			$transaction->rollback();
204
			return $this->respondWithStatus([
205 206 207 208
					"message" => t("An unexpected problem occured")
				], Response::HTTP_INTERNAL_SERVER_ERROR);
		}

209 210 211 212
		//return new RedirectResponse($this->redirectUrl . '?auth_token=' . $epalToken.'&auth_role=director', 302, []);
		//return new RedirectResponse("../eepal/dist/#/minister/minister-view");

		/*
213
		return $this->respondWithStatus([
214 215
					"message" => t("Distribution has made successfully")
				], Response::HTTP_OK);
216 217 218 219 220 221 222 223 224 225 226 227 228
		*/
		$postData = null;
		if ($content = $request->getContent()) {
				$postData = json_decode($content);
				return $this->respondWithStatus([
						'message' => "Distribution has made successfu",
				], Response::HTTP_OK);
			}
			else {
				return $this->respondWithStatus([
						'message' => t("post with no data"),
				], Response::HTTP_BAD_REQUEST);
			}
229 230 231 232 233 234 235 236 237

	}


	public function locateStudent($choice_id, &$epalStudents)	{

		$epal_dist_id = -1;
		$specialization_id = -1;

238
		$transaction = $this->connection->startTransaction();
239

240
		try {
241 242

			foreach ($epalStudents as $epalStudent)	{
243 244
				//print_r("<br>ΚΑΤΑΝΟΜΗ ΜΑΘΗΤΩΝ ΝΟ: " . $choice_id);
				//print_r("<br>ΜΑΘΗΤΗΣ: " .  $epalStudent->id);
245 246 247 248 249 250

				$clCon = $this->connection->select('epal_student_epal_chosen', 'epals')
					->fields('epals', array('student_id', 'epal_id', 'choice_no'))
					->condition('epals.student_id', $epalStudent->id , '=')
					->condition('epals.choice_no', $choice_id , '=');
				$epalSchoolsChosen = $clCon->execute()->fetchAll(\PDO::FETCH_OBJ);
251 252 253

				if (sizeof($epalSchoolsChosen) !==  0)	{
					$epalSchoolChos = reset($epalSchoolsChosen);
254
					//print_r(" SCHOOL_ID:" . $epalSchoolChos->epal_id . " STUDENT_ID " . $epalStudent->id);
255 256 257 258 259 260 261
					$epal_dist_id = $epalSchoolChos->epal_id;

					if ($epalStudent->currentclass === "2")	{
						$clCon = $this->connection->select('epal_student_sector_field', 'sectors')
							->fields('sectors', array('student_id', 'sectorfield_id'))
							->condition('sectors.student_id', $epalStudent->id , '=');
						$epalSectorChosen = $clCon->execute()->fetchAll(\PDO::FETCH_OBJ);
262 263
						$epalSecChos = reset($epalSectorChosen);
					}
264 265 266 267 268
					elseif ($epalStudent->currentclass === "3")	{
						$clCon = $this->connection->select('epal_student_course_field', 'courses')
							->fields('courses', array('student_id', 'coursefield_id'))
							->condition('courses.student_id', $epalStudent->id , '=');
						$epalCourseChosen = $clCon->execute()->fetchAll(\PDO::FETCH_OBJ);
269 270 271
						$epalCourChos = reset($epalCourseChosen);
					}

272 273 274 275
					if ($epalStudent->currentclass === "2")
						$specialization_id = $epalSecChos->sectorfield_id;
					elseif ($epalStudent->currentclass === "3")
						$specialization_id = $epalCourChos->coursefield_id;
276 277 278
					else
						$specialization_id = -1;

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

				 //$currentTime = \Drupal\Core\TypedData\Plugin\DataType\TimeStamp::getDateTime();
					$timestamp = strtotime(date("Y-m-d"));
					$this->connection->insert('epal_student_class')->fields(
						array('id' => $this->globalCounterId++,
							'uuid' => \Drupal::service('uuid')->generate(),
							'langcode' => $this->language,
							'user_id' => $this->currentuser,
							'student_id'=> $epalStudent->id,
							'epal_id'=> $epal_dist_id,
							'currentclass' => $epalStudent->currentclass,
							'currentepal' => $epalStudent->currentepal,
							'specialization_id' => $specialization_id,
							'points' => $epalStudent->points,
							'distribution_id' => $choice_id,
							'status' => 1,
							'created' => $timestamp,
							'changed' => $timestamp,)
					)->execute();
298 299 300 301 302 303 304 305 306

			} //end if

		}	//foreach

	}

	catch (\Exception $e) {
		$this->logger->warning($e->getMessage());
307
		$transaction->rollback();
308 309 310 311 312 313 314 315 316 317 318 319 320 321
		return $this->respondWithStatus([
					"message" => t("An unexpected problem occured during locateStudent Method of Distribution")
				], Response::HTTP_INTERNAL_SERVER_ERROR);
	}

	return $this->respondWithStatus([
			"message" => t("locateStudent Method of Distribution has made successfully")
		], Response::HTTP_OK);

	}


 public function retrieveCapacityLimitUp($className) {

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	 $transaction = $this->connection->startTransaction();

	 try {
		 $clCon = $this->connection->select('epal_class_limits', 'classLimits')
			 ->fields('classLimits', array('limit_up'))
			 ->condition('classLimits.name', $className, '=');
		 $results = $clCon->execute()->fetchAll(\PDO::FETCH_OBJ);
		 $row = reset($results);
 	}
	catch (\Exception $e) {
		$this->logger->warning($e->getMessage());
		$transaction->rollback();
		return $this->respondWithStatus([
					"message" => t("An unexpected problem occured during retrieveCapacityLimitUp Method of Distribution")
				], Response::HTTP_INTERNAL_SERVER_ERROR);
	}

	return $row->limit_up;
340 341 342 343
 }

	public function checkCapacityAndArrange($epalId, $classId, $secCourId, $limitup, $capacity)	{

344 345 346 347 348 349 350 351 352 353 354
		$transaction = $this->connection->startTransaction();

		try {

			$clCon = $this->connection->select('epal_student_class', 'studentClass')
				->fields('studentClass', array('epal_id', 'student_id', 'points', 'currentepal', 'currentclass', 'specialization_id'))
				->condition('studentClass.epal_id', $epalId, '=')
				->condition('studentClass.currentclass', $classId, '=')
				->condition('studentClass.specialization_id', $secCourId, '=');
			$epalStudentClass = $clCon->execute()->fetchAll(\PDO::FETCH_OBJ);

355
			//print_r("<br> ΣΧΟΛΕΙΟ: " .  $epalId . " ΤΑΞΗ: "  . $classId . " ΤΟΜΕΑΣ/ΕΙΔΙΚΟΤΗΤΑ: " . $secCourId .  " ΧΩΡΗΤΙΚΟΤΗΤΑ: " . sizeof($epalStudentClass));
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

			//ΕΠΙΠΛΕΟΝ ΕΠΙΠΕΔΟ ΑΣΦΑΛΕΙΑΣ: αν δεν υπάρχει ο συγκεκριμένος τομέας/ειδικότητα στο σχολείο
			//ο μαθητής που τοποθετήθηκε με την locateStudent να διαγραφεί
			//Σημείωση: κανονικά κάτι τέτοιο δεν μπορεί να συμβεί από το front-end (δηλ. μαθητής να δηλώσει τομέα/ειδικότητα που δεν προσφέρεται..)
			//ΑΝ ΜΠΕΙ ΠΡΕΠΕΙ ΝΑ ΕΝΣΩΜΑΤΩΘΕΙ ΣΤΗΝ LOCATESTUDENT..
			/*
			if (sizeof($epalStudentClass) === 0)	{
				//print_r("<br>ΜΠΗΚΑ! ");
				foreach ($epalStudentClass as $epalStudCl)	{
					//print_r("<br>ΜΠΗΚΑ! ΜΑΘΗΤΗΣ: " .  $epalStudCl->student_id);
					$query = $this->connection->delete('epal_student_class')
													->condition('student_id', $epalStudCl->student_id)
													->execute();
					}
			}
			*/
			//ΤΕΛΟΣ

			$limit = $limitup * $capacity;
			if (sizeof($epalStudentClass) > $limit)	{
376
				//print_r("<br>ΥΠΕΡΧΕΙΛΙΣΗ!");
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
				foreach ($epalStudentClass as $epalStudCl)	{
					//Υπολογισμός μορίων του μαθητή και (πιθανή) αποθήκευσή τους
					//ΣΗΜΕΙΩΣΗ: Ο υπoλογισμός γίνεται στο front-end

					//$points = $this->calculatePoints();
					/*
					$query = $this->connection->update('epal_student');
					$query->fields([
						'points' =>$points,
					]);
					$query->condition('id',$epalStudCl->student_id);
					$query->execute();
					*/

				}
				$this->makeSelectionOfStudents($epalStudentClass,$limit);
393
			}
394 395 396 397 398 399
			else { //αφαίρεσε όσους μαθητές βρίσκονται στον πίνακα εκκρεμοτήτων
				foreach ($epalStudentClass as $epalStudCl) {
					if ($this->choice_id !== 1)
						////διέγραψε τον μαθητή από τον πίνακα εκκρεμοτήτων (αν βρίσκεται εκεί)
						$this->removeFromPendingStudents($epalStudCl->student_id);
				}
400 401
			}

402
	}	//end try
403

404 405 406
	catch (\Exception $e) {
		$this->logger->warning($e->getMessage());
		$transaction->rollback();
407
		return $this->respondWithStatus([
408 409 410 411 412
					"message" => t("An unexpected problem occured during checkCapacityAndArrange Method of Distribution")
				], Response::HTTP_INTERNAL_SERVER_ERROR);
	}

	return $this->respondWithStatus([
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
				"message" => t("checkCapacityAndArrange Method of Distribution has made successfully")
			], Response::HTTP_OK);

	}


	public function removeFromPendingStudents($val)	{
		if(($key = array_search($val, $this->pendingStudents)) !== false) {
		   unset($this->pendingStudents[$key]);
		}
		//$this->pendingStudents = array_diff($this->pendingStudents, array($val));
	}

	public function makeSelectionOfStudents(&$students, $limit)	{
		//συνάρτηση επιλογής μαθητών σε περίπτωση υπερχείλισης
		// (1) έχουν απόλυτη προτεραιότητα όσοι ήδη φοιτούσαν στο σχολείο (σε περίπτωση που φοιτούσαν περισσότεροι από την χωρητικότητα, τους δεχόμαστε όλους)
		// (2) αν απομένουν κενές θέσεις, επέλεξε από τους εναπομείναντες μαθητές αυτούς με τα περισσότερα μόρια. Σε περίπτωση ισοβαθμίας δεχόμαστε όλους όσους ισοβαθμούν.
		//αυτοδίκαια έχουν προτεραιότητα όσοι ήδη φοιτούσαν στο σχολείο

		foreach($students as $student)	{
433
			$student->student_id;
434
			//print_r("<br>STUDENT_ID:" . $student->student_id);
435 436 437 438 439
		}

		//εύρεση αριθμού μαθητών που ήδη φοιτούσαν στο σχολείο
		$cnt = 0;
		foreach($students as $student)	{
440
			if ($student->currentepal === $student->epal_id) {
441 442 443
				$cnt++;
				if ($this->choice_id !== 1)
					////διέγραψε τον μαθητή από τον πίνακα εκκρεμοτήτων (αν βρίσκεται εκεί)
444
					$this->removeFromPendingStudents($student->student_id);
445 446
			}
		}
447
		//print_r("<br>#ΕΓΓΡΑΦΩΝ ΠΟΥ ΟΙ ΜΑΘΗΤΕΣ ΦΟΙΤΟΥΣΑΝ ΗΔΗ:" . $cnt);
448 449

		$newlimit = $limit - $cnt;
450 451
		//print_r("<br>ΑΝΩΤΑΤΟ ΟΡΙΟ ΜΑΘΗΤΩΝ:" . $limit);
		//print_r("<br>#ΜΑΘΗΤΩΝ ΓΙΑ ΝΑ ΕΠΙΛΕΓΟΥΝ ΜΕ ΜΟΡΙΑ:" . $newlimit);
452 453 454

		$points_arr = [];
		foreach($students as $student)	{
455 456
			if ($student->currentepal !== $student->epal_id)
				$points_arr[] = $student->points;
457
		}
458

459
		rsort($points_arr);
460 461
		//for ($i=0; $i < sizeof($points_arr); $i++)
			//print_r("<br>ΜΟΡΙΑ ΜΕΤΑ ΤΗΝ ΤΑΞΙΝΟΜΙΣΗ: " . $points_arr[$i]);
462

463
		//print_r("<br>ΟΡΙΟ ΜΟΡΙΩΝ: " . $points_arr[$newlimit-1]);
464

465 466
		$transaction = $this->connection->startTransaction();

467
		foreach($students as $student)	{
468 469
			if ($student->currentepal !== $student->epal_id)	{
				if ($student->points < $points_arr[$newlimit-1]) {
470
					//print_r("<br>ΣΕ ΕΚΚΡΕΜΟΤΗΤΑ - ΔΙΑΓΡΑΦΗ: " . $student->student_id);
471
					//βάλε τον μαθητή στον πίνακα εκκρεμοτήτων και διέγραψέ τον από τον προσωρινό πίνακα αποτελεσμάτων
472 473 474 475 476 477 478 479 480 481 482 483 484 485
					array_push($this->pendingStudents, $student->student_id);
					try {
						$this->connection->delete('epal_student_class')
													->condition('student_id', $student->student_id)
													->execute();
					}
					catch (\Exception $e) {
						$this->logger->warning($e->getMessage());
						$transaction->rollback();
						return $this->respondWithStatus([
									"message" => t("An unexpected problem occured during DELETE proccess in makeSelectionOfStudents Method of Distribution")
								], Response::HTTP_INTERNAL_SERVER_ERROR);
					}

486 487 488 489
				}
				else {
					if ($this->choice_id !== 1)
						//διέγραψε τον μαθητή από τον πίνακα εκκρεμοτήτων (αν βρίσκεται εκεί)
490
						$this->removeFromPendingStudents($student->student_id);
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
				}
			}
		}

		return $this->respondWithStatus([
				"message" => t("makeSelectionOfStudents Method of Distribution has made successfully")
			], Response::HTTP_OK);
	}

	public function calculatePoints()	{

			return rand(0,20);

	}

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




515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
		public function makegGeneralReport(Request $request) {

			try  {
				if (!$request->isMethod('GET')) {
					 return $this->respondWithStatus([
							"message" => t("Method Not Allowed")
							 ], Response::HTTP_METHOD_NOT_ALLOWED);
				}

				//user validation
				//Note:  $authToken =  $postData->username
				$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
				//$user = \Drupal\user\Entity\User::load($user->id());
				$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, '=');
					array_push($numData, $sCon->countQuery()->execute()->fetchField());
				}

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

577
				/*
578 579 580 581 582 583 584
				$list[] = array(
					 'num_applications' => $numTotal,
				 	 'numchoice1' => $numData[0],
					 'numchoice2' => $numData[1],
					 'numchoice3' => $numData[2],
					 'num_noallocated' => $numNoAllocated,
				 );
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
				*/



				 $list = array();

				 //$record = new generalReportSchema;
				 //$record->name = "nikos";
				 //$record->numStudents = 20;

				 array_push($list,(object) array('name' => "Αριθμός Αιτήσεων", 'numStudents' => $numTotal));
				 // $record = (object) array('name' => "Αριθμός Αιτήσεων:", 'numStudents' => $numTotal);
				 array_push($list,(object) array('name' => "Αριθμός μαθητών που τοποθετήθηκαν στην πρώτη τους προτίμηση", 'numStudents' => $numData[0]));
				 //$record = (object) array('name' => "Αριθμός μαθητών που τοποθετήθηκαν στην πρώτη τους προτίμηση:", 'numStudents' => $numData[0]);
				 array_push($list,(object) array('name' => "Αριθμός μαθητών που τοποθετήθηκαν στην δεύτερή τους προτίμηση", 'numStudents' => $numData[1]));
				 //$record = (object) array('name' => "Αριθμός μαθητών που τοποθετήθηκαν στην δεύτερή τους προτίμηση:", 'numStudents' => $numData[1]);
				 array_push($list,(object) array('name' => "Αριθμός μαθητών που τοποθετήθηκαν στην τρίτη τους προτίμηση", 'numStudents' => $numData[2]));
				 //$record = (object) array('name' => "Αριθμός μαθητών που τοποθετήθηκαν στην τρίτη τους προτίμηση:", 'numStudents' => $numData[2]);
				 array_push($list,(object) array('name' => "Αριθμός μαθητών που δεν τοποθετήθηκαν σε καμμία τους προτίμηση", 'numStudents' => $numNoAllocated));
				 //$record = (object) array('name' => "Αριθμός μαθητών που δεν τοποθετήθηκαν σε καμμία τους προτίμηση:", 'numStudents' => $numNoAllocated);
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

				 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 DELETE proccess in makeSelectionOfStudents Method of Distribution")
						], Response::HTTP_INTERNAL_SERVER_ERROR);
			}

		}



622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
		/*
		public function makeReport1(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('eepal_school_field_data', 'eSchool')
																	->fields('eSchool', array('id', 'name'));
				$epalSchools = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
				//$schoolIds = array();
				foreach ($epalSchools as $epalSchool)	{
					//array_push($schoolIds, $schoolId->id);
					$sCon = $this->connection->select('epal_student_class', 'eStudent')
																		->fields('eStudent', array('id', 'epal_id'))
																		->condition('eStudent.epal_id', $epalSchool->id , '=');
					$num = $sCon->countQuery()->execute()->fetchField();
					if ($num !== "0")
						array_push($list,(object) array('schoolName' => $epalSchool->name, 'numStudents' => $num));
				}

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

		}
		*/



689

690
}