CreateDemoData.php 8.76 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
<?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\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;



class CreateDemoData 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;
			$this->connection = $connection;
39
			$this->logger = $loggerChannel->get('epal');
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    }

	public static function create(ContainerInterface $container)
    {
        return new static(
          $container->get('entity.manager'),
          $container->get('entity.query'),
          $container->get('database'),
          $container->get('logger.factory')
      );
    }


	public function make_seed() {
		  list($usec, $sec) = explode(' ', microtime());
		  return $sec + $usec * 1000000;
	}

	public function UniqueRandNum($min, $max, $quantity) {
    $numbers = range($min, $max);
    shuffle($numbers);
    return array_slice($numbers, 0, $quantity);
	}


	public function createData() {

67 68
    $transaction = $this->connection->startTransaction();

69 70 71 72 73 74
		try {
			//insert demo records in entity: epal_student
			$entity_manager = \Drupal::entityTypeManager();

			$epaluserid = \Drupal::currentUser()->id();

75
			for ($i = 1; $i <= 500; $i++) {
76 77
				//srand($this->make_seed());

78 79 80
			  $curclass = rand(1,3);
				//$curclass = 2;
				$currentepal = rand(137,165);
81 82 83 84 85 86 87

				$student = array(
					//'epaluser_id' => $aitisi[0][epaluser_id],
					'epaluser_id' => $epaluserid,
					'name' => "firstname" . $i,
					'studentsurname' => "surname" . $i,
					//'birthdate' => $aitisi[0][birthdate],
88
					'birthdate' => '01/01/1970',
89
					//'fatherfirstname' => $aitisi[0][fatherfirstname],
90
					'fatherfirstname' => "fatherfirstname" . $i,
91
					//'fathersurname' => $aitisi[0][fathersurname],
92
					'fathersurname' => "fathersurname" . $i,
93
					//'motherfirstname' => $aitisi[0][motherfirstname],
94
					'motherfirstname' => "motherfirstname" . $i,
95
					//'mothersurname' => $aitisi[0][mothersurname],
96
					'mothersurname' => "mothersurname" . $i,
97 98
					//'studentamka' => $aitisi[0][studentamka],
					//'regionaddress' => $aitisi[0][regionaddress],
99
					'regionaddress' => "regionaddress" . $i,
100
					//'regionarea' => $aitisi[0][regionarea],
101
					'regionarea' => "regionarea" . $i,
102
					//'regiontk' => $aitisi[0][regiontk],
103
					'regiontk' => "tk" . $i,
104
					//'certificatetype' => $aitisi[0][certificatetype],
105
					'certificatetype' => 'Απολυτήριο Γυμνασίου',
106 107
					//'lastam' => $aitisi[0][lastam],
					'currentclass' => $curclass,
108
					'currentepal' => $currentepal,
109 110
					//'currentsector' => $aitisi[0][currentsector],
					//'relationtostudent' => $aitisi[0][relationtostudent],
111
					'relationtostudent' => 'Μαθητής',
112
					//'telnum' => $aitisi[0][telnum],
113 114
					'telnum' => '6944123456',
					'points' => rand(0,20)
115 116 117 118 119 120 121 122 123 124
        );

				$entity_storage_student = $entity_manager->getStorage('epal_student');
				$entity_object = $entity_storage_student->create($student);
				$entity_storage_student->save($entity_object);

				$created_student_id = $entity_object->id();



125 126 127
				//insert records in entity: 	epal_student_course_field (αφορά μαθητές Γ' Λυκείου)
				//						or:		epal_student_sector_field (αφορά μαθητές Β' Λυκείου)
				$availableSchools  = array();
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
				if ($curclass === 3)	{

					do {
						$coursefield_id = rand(1,54);
						$course = array(
							'student_id' => $created_student_id,
							'coursefield_id' => $coursefield_id
						);

						$entity_storage_course = $entity_manager->getStorage('epal_student_course_field');
						$entity_object = $entity_storage_course->create($course);
						//$entity_storage_course->save($entity_object);

						//εύρεση ΕΠΑΛ που διαθέτουν την αντίστοιχη ειδικότητα
						print_r("<br> EIDIKOTHTA: " . $coursefield_id);
						$eepalSpecialtiesInEpal_storage = $this->entityTypeManager->getStorage('eepal_specialties_in_epal');
						//$eepalSpecialtiesInEpal = $eepalSpecialtiesInEpal_storage->loadByProperties(array('specialty_id' => $coursefield_id) );

						$ids  =  $eepalSpecialtiesInEpal_storage->getQuery()
							->condition('specialty_id', $coursefield_id, "=")
							->condition('epal_id', 137, ">=")
							->condition('epal_id', 165, "<=")
							->execute();
						$eepalSpecialtiesInEpal = $eepalSpecialtiesInEpal_storage->loadMultiple($ids);

						print_r("<br> NUM_SCHOOLS: " . sizeof($eepalSpecialtiesInEpal));

						foreach ($eepalSpecialtiesInEpal as $eepalSpecialInEp)	{
							array_push($availableSchools, $eepalSpecialInEp->epal_id->getString());
158 159
						}

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
						if (sizeof($availableSchools) != 0)
							$entity_storage_course->save($entity_object);
					}	//end do
					while (sizeof($availableSchools) == 0);

					$entity_storage_course->resetCache();
					$eepalSpecialtiesInEpal_storage->resetCache();
				}

				else if ($curclass === 2)	{

					do {
						$sectorfield_id = rand(1,9);
						$sector = array(
							'student_id' => $created_student_id,
							'sectorfield_id' => $sectorfield_id
						);
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
						$entity_storage_sector = $entity_manager->getStorage('epal_student_sector_field');
						$entity_object = $entity_storage_sector->create($sector);
						//$entity_storage_sector->save($entity_object);

						//εύρεση ΕΠΑΛ που διαθέτουν τον αντίστοιχο τομέα
						print_r("<br> TOMEAS: " . $sectorfield_id);
						$eepalSectorsInEpal_storage = $this->entityTypeManager->getStorage('eepal_sectors_in_epal');

						$ids  =  $eepalSectorsInEpal_storage->getQuery()
							->condition('sector_id', $sectorfield_id, "=")
							->condition('epal_id', 137, ">=")
							->condition('epal_id', 165, "<=")
							->execute();
						$eepalSectorsInEpal = $eepalSectorsInEpal_storage->loadMultiple($ids);

						print_r("<br> NUM_SCHOOLS: " . sizeof($eepalSectorsInEpal));

						foreach ($eepalSectorsInEpal as $eepalSecInEp)	{
							array_push($availableSchools, $eepalSecInEp->epal_id->getString());
197
						}
198 199 200 201
						if (sizeof($availableSchools) != 0)
							$entity_storage_sector->save($entity_object);
					}	//end do
					while (sizeof($availableSchools) == 0);
202

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
					$entity_storage_sector->resetCache();
					$eepalSectorsInEpal_storage->resetCache();
				}

				else if ($curclass === 1)	{
					print_r("<br> CLASS A: ");
					$school_id_start = 137;
					for ($l=0; $l < 29; $l++)
						array_push($availableSchools, $school_id_start + $l);
				}

				$numEpalsChosen = rand(1,3);
				if ($numEpalsChosen > sizeof($availableSchools))
					$numEpalsChosen =  sizeof($availableSchools);
				print_r("<br> NUM_EPAL_CHOSEN " . $numEpalsChosen);

				$epal_id_index = $this->UniqueRandNum(0,sizeof($availableSchools)-1,$numEpalsChosen);
				for ($j=0; $j < $numEpalsChosen; $j++)	{
					print_r("<br> EPAL_CHOSEN_ID: " . $epal_id_index[$j]);
					print_r("<br> EPAL_CHOSEN_REAL_ID: " . $availableSchools[$epal_id_index[$j]]);
				}
224 225 226 227

				for ($j = 0; $j < $numEpalsChosen ; $j++) {
						$epalchosen = array(
							'student_id' => $created_student_id,
228
							'epal_id' => $availableSchools[$epal_id_index[$j]],
229 230 231 232 233 234 235
							'choice_no' => $j+1
						);
						$entity_storage_epalchosen = $entity_manager->getStorage('epal_student_epal_chosen');
						$entity_object = $entity_storage_epalchosen->create($epalchosen);
						$entity_storage_epalchosen->save($entity_object);
				}

236 237
				$entity_storage_epalchosen->resetCache();
				$entity_storage_student->resetCache();
238 239


240 241 242 243 244 245 246
				/*
				//TO BE CONSIDERED
				if ($curclass === 2 || $curclass === 3) {
					//33% των μαθητών της Β' και Γ' Λυκείου δηλώνουν προτίμηση στο σχολείο που ήδη φοιτούν
					if (rand(1,3) === 1) {
						$epal_id[0] = $currentepal;
					}
247
				}
248
				*/
249 250 251 252 253

		}
	}

	catch (\Exception $e) {
254
			$this->logger->warning($e->getMessage());
255 256 257

			$returnmsg = "Αποτυχία καταχώρησης demo data!";
			$response = new JsonResponse([$returnmsg]);
258
    //  $transaction->rollback();
259 260 261 262 263 264 265 266 267 268 269
			return $response;
	}


  $response = new JsonResponse(['hello' => 'world']);
  $response->headers->set('X-AUTH-TOKEN', 'HELLOTOKEN');
  return $response;

	}

}