added InformUnallocatedStudents Controller, added reports in ReportCreator...

added InformUnallocatedStudents Controller, added reports in ReportCreator Controller, updated CreateDemoData Controller Utility
parent 21f6547a
......@@ -7,7 +7,7 @@ epal.user.send_verification_code:
requirements:
_user_is_logged_in: 'TRUE'
epal.user.verify_verification_code:
path: '/epal/user/verifyvercode'
path: '/epal/user/verifyvercode'
options:
_auth: [ 'basic_auth' ]
defaults:
......@@ -148,7 +148,7 @@ epal.ministry.general_report:
_controller: '\Drupal\epal\Controller\ReportsCreator::makegGeneralReport'
requirements:
_user_is_logged_in: 'TRUE'
epal.ministry.report1:
epal.ministry.report-completeness:
path: '/ministry/report-completeness'
options:
_auth: [ 'basic_auth' ]
......@@ -156,6 +156,22 @@ epal.ministry.report1:
_controller: '\Drupal\epal\Controller\ReportsCreator::makeReportCompleteness'
requirements:
_user_is_logged_in: 'TRUE'
epal.ministry.report-all-stat:
path: '/ministry/report-all-stat'
options:
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\ReportsCreator::makeReportAllStat'
requirements:
_user_is_logged_in: 'TRUE'
epal.ministry.massive_mail:
path: '/ministry/send-massive-mail'
options:
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\InformUnlocatedStudents::sendMailToStudents'
requirements:
_user_is_logged_in: 'TRUE'
regionview:
path: '/epal/ScoolperPerf/{perfectureId}'
options:
......@@ -171,4 +187,4 @@ coursesperschools:
defaults:
_controller: '\Drupal\epal\Controller\DirectorView::getCoursesPerSchool'
requirements:
_user_is_logged_in: 'TRUE'
\ No newline at end of file
_user_is_logged_in: 'TRUE'
......@@ -36,7 +36,7 @@ class CreateDemoData extends ControllerBase {
$this->entityTypeManager = $entityTypeManager;
$this->entity_query = $entity_query;
$this->connection = $connection;
//$this->logger = $loggerChannel->get('oauthost');
$this->logger = $loggerChannel->get('epal');
}
public static function create(ContainerInterface $container)
......@@ -64,19 +64,20 @@ class CreateDemoData extends ControllerBase {
public function createData() {
$transaction = $this->connection->startTransaction();
$transaction = $this->connection->startTransaction();
try {
//insert demo records in entity: epal_student
$entity_manager = \Drupal::entityTypeManager();
$epaluserid = \Drupal::currentUser()->id();
for ($i = 1; $i <= 10000; $i++) {
for ($i = 1; $i <= 500; $i++) {
//srand($this->make_seed());
//$curclass = rand(1,3);
$curclass = 1;
$currentepal = rand(147,156);
$curclass = rand(1,3);
//$curclass = 2;
$currentepal = rand(137,165);
$student = array(
//'epaluser_id' => $aitisi[0][epaluser_id],
......@@ -84,22 +85,33 @@ class CreateDemoData extends ControllerBase {
'name' => "firstname" . $i,
'studentsurname' => "surname" . $i,
//'birthdate' => $aitisi[0][birthdate],
'birthdate' => '01/01/1970',
//'fatherfirstname' => $aitisi[0][fatherfirstname],
'fatherfirstname' => "fatherfirstname" . $i,
//'fathersurname' => $aitisi[0][fathersurname],
'fathersurname' => "fathersurname" . $i,
//'motherfirstname' => $aitisi[0][motherfirstname],
'motherfirstname' => "motherfirstname" . $i,
//'mothersurname' => $aitisi[0][mothersurname],
'mothersurname' => "mothersurname" . $i,
//'studentamka' => $aitisi[0][studentamka],
//'regionaddress' => $aitisi[0][regionaddress],
'regionaddress' => "regionaddress" . $i,
//'regionarea' => $aitisi[0][regionarea],
'regionarea' => "regionarea" . $i,
//'regiontk' => $aitisi[0][regiontk],
'regiontk' => "tk" . $i,
//'certificatetype' => $aitisi[0][certificatetype],
'certificatetype' => 'Απολυτήριο Γυμνασίου',
//'lastam' => $aitisi[0][lastam],
'currentclass' => $curclass,
'currentepal' => $currentepal
'currentepal' => $currentepal,
//'currentsector' => $aitisi[0][currentsector],
//'relationtostudent' => $aitisi[0][relationtostudent],
'relationtostudent' => 'Μαθητής',
//'telnum' => $aitisi[0][telnum],
//'moria' => rand(0,10)
'telnum' => '6944123456',
'points' => rand(0,20)
);
$entity_storage_student = $entity_manager->getStorage('epal_student');
......@@ -109,85 +121,141 @@ class CreateDemoData extends ControllerBase {
$created_student_id = $entity_object->id();
//insert demo records in entity: epal_student_epal_chosen
//$epal_id = rand(147,149); //TO BE REMOVED
//insert records in entity: epal_student_course_field (αφορά μαθητές Γ' Λυκείου)
// or: epal_student_sector_field (αφορά μαθητές Β' Λυκείου)
$availableSchools = array();
$numEpalsChosen = rand(1,3);
$epal_id = $this->UniqueRandNum(147,156,$numEpalsChosen);
//for ($j = 1; $j <= $numEpalsChosen ; $j++) {
if ($curclass === 2 || $curclass === 3) {
//33% των μαθητών της Β' και Γ' Λυκείου δηλώνουν προτίμηση στο σχολείο που ήδη φοιτούν
if (rand(1,3) === 1) {
$epal_id[0] = $currentepal;
//$epal_id = array($currentepal ,$currentepal, $currentepal);
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());
}
else {
//$epal_id = rand(147,149);
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
);
//print_r("<br>". $epal_id[0] . $epal_id[1] . $epal_id[2]);
$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());
}
}
if (sizeof($availableSchools) != 0)
$entity_storage_sector->save($entity_object);
} //end do
while (sizeof($availableSchools) == 0);
$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]]);
}
for ($j = 0; $j < $numEpalsChosen ; $j++) {
// print_r("<br>Data: Student" . $created_student_id . "EPAL" . $epal_id . "CHOICE" . $j );
$epalchosen = array(
'student_id' => $created_student_id,
'epal_id' => $epal_id[$j],
'epal_id' => $availableSchools[$epal_id_index[$j]],
'choice_no' => $j+1
);
//print_r("<br>Test1");
$entity_storage_epalchosen = $entity_manager->getStorage('epal_student_epal_chosen');
//print_r("<br>Test2");
$entity_object = $entity_storage_epalchosen->create($epalchosen);
//print_r("<br>Test3");
$entity_storage_epalchosen->save($entity_object);
//print_r("<br>Test4");
}
$entity_storage_epalchosen->resetCache();
$entity_storage_student->resetCache();
//insert records in entity: epal_student_course_field (αφορά μαθητές Γ' Λυκείου)
// or: epal_student_sector_field (αφορά μαθητές Β' Λυκείου)
if ($curclass === 3) {
$course = array(
//'name' => $aitisi[3][name],
//'student_id' => $aitis[3][student_i],
'student_id' => $created_student_id,
'coursefield_id' => rand(1,54)
);
$entity_storage_course = $entity_manager->getStorage('epal_student_course_field');
$entity_object = $entity_storage_course->create($course);
$entity_storage_course->save($entity_object);
}
else if ($curclass === 2) {
$sector = array(
//'name' => $aitisi[3][name],
'student_id' => $created_student_id,
//'student_id' => $aitis[3][student_i],
'sectorfield_id' => rand(1,9)
);
$entity_storage_sector = $entity_manager->getStorage('epal_student_sector_field');
$entity_object = $entity_storage_sector->create($sector);
$entity_storage_sector->save($entity_object);
/*
//TO BE CONSIDERED
if ($curclass === 2 || $curclass === 3) {
//33% των μαθητών της Β' και Γ' Λυκείου δηλώνουν προτίμηση στο σχολείο που ήδη φοιτούν
if (rand(1,3) === 1) {
$epal_id[0] = $currentepal;
}
}
*/
}
}
catch (\Exception $e) {
//$this->logger->warning($e->getMessage());
$this->logger->warning($e->getMessage());
$returnmsg = "Αποτυχία καταχώρησης demo data!";
$response = new JsonResponse([$returnmsg]);
$transaction->rollback();
// $transaction->rollback();
return $response;
}
......
......@@ -24,8 +24,6 @@ use Drupal\Core\TypedData\Plugin\DataType\TimeStamp;
use Drupal\Core\Language\LanguageManagerInterface;
class Distribution extends ControllerBase {
protected $entity_query;
......@@ -112,7 +110,8 @@ class Distribution extends ControllerBase {
//initialize/empty epal_student_class if there are already data in it!
$this->connection->delete('epal_student_class')->execute();
$limitUp_class = $this->retrieveCapacityLimitUp("Α");
//$limitUp_class = $this->retrieveCapacityLimitUp("Α");
$limitUp_class = $this->retrieveCapacityLimitUp("1");
//print_r("<br> ΑΝΩΤΑΤΟ ΟΡΙΟ ΜΑΘΗΤΩΝ: " . $limitUp_class);
while ($this->choice_id <= $numDistributions) {
......@@ -575,6 +574,7 @@ class Distribution extends ControllerBase {
->condition('eStudent.id', $studentIds, 'NOT IN');
$numNoAllocated = $sCon->countQuery()->execute()->fetchField();
/*
$list[] = array(
'num_applications' => $numTotal,
'numchoice1' => $numData[0],
......@@ -582,6 +582,26 @@ class Distribution extends ControllerBase {
'numchoice3' => $numData[2],
'num_noallocated' => $numNoAllocated,
);
*/
$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);
return $this->respondWithStatus(
$list
......@@ -599,5 +619,72 @@ class Distribution extends ControllerBase {
/*
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);
}
}
*/
}
<?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 InformUnlocatedStudents 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')
);
}
public function sendMailToStudents(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);
}
// εύρεση μαθητών που η αίτησή τους ΔΕΝ ικανοποιήθηκε
$sCon = $this->connection->select('epal_student_class', 'eStudent')
->fields('eStudent', array('student_id'));
$epalStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$studentIds = array();
foreach ($epalStudents as $epalStudent)
array_push($studentIds, $epalStudent->student_id);
$sCon = $this->connection->select('epal_student', 'eStudent')
->fields('eStudent', array('id', 'user_id', 'created'))
->condition('eStudent.id', $studentIds, 'NOT IN');
//$numNoAllocated = $sCon->countQuery()->execute()->fetchField();
$epalNonLocatedStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$cnt_success = 0;
$cnt_fail = 0;
foreach ($epalNonLocatedStudents as $epalNonLocatedStudent) {
$epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('user_id' => $epalNonLocatedStudent->user_id));
$epalUser = reset($epalUsers);
if ($epalUser) {
$user = $this->entityTypeManager->getStorage('user')->load($epalUser->user_id->target_id);
//$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('uid' => $epalUser->user_id));
//$user = reset($users);
if ($user) {
$langcode = $user->getPreferredLangcode();
if ($this->sendEmail($user->getEmail(), $epalNonLocatedStudent->id, $epalNonLocatedStudent->created, $langcode))
$cnt_success++;
}
else {
return $this->respondWithStatus([
'message' => t("user not found"),
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
} else {
/*
return $this->respondWithStatus([
'message' => t("EPAL user not found"),
], Response::HTTP_FORBIDDEN);
*/
$cnt_fail++;
}
} //end foreach
return $this->respondWithStatus([
'message' => t("post successful"),
'num_success_mail' => $cnt_success,
'num_fail_mail' => $cnt_fail,
//'test' => $test,
], Response::HTTP_OK);
} //end try
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return $this->respondWithStatus([
"message" => t("An unexpected problem occured during sendMailToStudents Method ")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
//sendEmail(e-mail address, A/A, application Date, lang)
private function sendEmail($email, $appId, $appDate, $langcode) {
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'epal';
$key = 'massive_mail'; //to be checked..
$to = $email;
$params['message'] = t('Σας ενημερώνουμε ότι η αίτησή σας με Α/Α: ') . $appId . t('/') . date('d/m/Y', $epalNonLocatedStudent->created)
.t(' δεν ικανοποιήθηκε. Παρακαλώ επικοινωνήστε άμεσα τηλεφωνικά με τη Διεύθυνση Δευτεροβάθμιας Εκπαίδευσης / Τμήμα Επαγγελματικής Εκπαίδευσης,
στην οποία ανήκετε, ώστε να διερευνηθεί η δυνατότητα εγγραφής σας σε αντίστοιχο ΕΠΑΛ'); //e-mail body
//add new lines with SENDER name - check it
$params['subject'] = t('Μη ικανοποίηση ηλεκτρονικής αίτησης για εγγραφή σε ΕΠΑΛ');
//$langcode = $user->getPreferredLangcode();
$send = true;
$mail_sent = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
//$mail_sent = 1;
if ($mail_sent) {
$this->logger->info("Το mail στάλθηκε με επιτυχία");
return 1;
}
else {
$this->logger->info("Αποτυχία αποστολής mail.");
return 0;
}
}
private function respondWithStatus($arr, $s) {
$res = new JsonResponse($arr);
$res->setStatusCode($s);
return $res;
}