CreateDemoData.php 8.87 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 <= 1000; $i++) {
76 77
				//srand($this->make_seed());

78 79
			  $curclass = rand(1,3);
				//$curclass = 2;
80 81 82

				//$currentepal = rand(137,165);
				$currentepal = rand(5,399);
83 84 85 86 87 88 89

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

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



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

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
				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, "=")
151 152
							//->condition('epal_id', 137, ">=")
							//->condition('epal_id', 165, "<=")
153 154 155 156 157 158 159
							->execute();
						$eepalSpecialtiesInEpal = $eepalSpecialtiesInEpal_storage->loadMultiple($ids);

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

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

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
						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
						);
179

180 181 182 183 184 185 186 187 188 189
						$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, "=")
190 191
							//->condition('epal_id', 137, ">=")
							//->condition('epal_id', 165, "<=")
192 193 194 195 196 197 198
							->execute();
						$eepalSectorsInEpal = $eepalSectorsInEpal_storage->loadMultiple($ids);

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

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

205 206 207 208 209 210
					$entity_storage_sector->resetCache();
					$eepalSectorsInEpal_storage->resetCache();
				}

				else if ($curclass === 1)	{
					print_r("<br> CLASS A: ");
211 212 213 214
					//$school_id_start = 137;
					$school_id_start = 5;
					//for ($l=0; $l < 29; $l++)
					for ($l=0; $l < 395; $l++)
215 216 217 218 219 220 221 222 223 224 225 226 227
						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]]);
				}
228 229 230 231

				for ($j = 0; $j < $numEpalsChosen ; $j++) {
						$epalchosen = array(
							'student_id' => $created_student_id,
232
							'epal_id' => $availableSchools[$epal_id_index[$j]],
233 234 235 236 237 238 239
							'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);
				}

240 241
				$entity_storage_epalchosen->resetCache();
				$entity_storage_student->resetCache();
242 243


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

		}
	}

	catch (\Exception $e) {
258
			$this->logger->warning($e->getMessage());
259 260 261

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


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

	}

}