Merge branch 'component_updates' into 'develop'

Component updates

See merge request !111
parents ccb9f67e c2652204
......@@ -46,10 +46,15 @@ function epal_mail($key, &$message, $params) {
$message['subject'] = t('Σύστημα υποβολής αιτήσεων εγγραφής σε ΕΠΑΛ: Επιβεβαίωση e-mail', $options);
$message['body'][] = $params['message'];
break;
case 'massive_mail':
case 'massive_mail_unallocated':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('Μη ικανοποίηση ηλεκτρονικής αίτησης για εγγραφή σε ΕΠΑΛ', $options);
$message['body'][] = $params['message'];
break;
case 'massive_mail_located':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('Ικανοποίηση ηλεκτρονικής αίτησης για εγγραφή σε ΕΠΑΛ', $options);
$message['body'][] = $params['message'];
break;
}
}
......@@ -73,7 +73,7 @@ epal.application_submit:
epal.allocation:
path: '/epal/distribution'
options:
_auth: [ 'basic_auth' ]
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\Distribution::createDistribution'
requirements:
......@@ -151,7 +151,7 @@ epal.ministry.report-completeness:
requirements:
_user_is_logged_in: 'TRUE'
epal.ministry.report-all-stat:
path: '/ministry/report-all-stat/{regionId}/{adminId}/{schId}/{classId}/{sectorId}/{courseId}'
path: '/ministry/report-all-stat/{regionId}/{adminId}/{schId}/{classId}/{sectorId}/{courseId}/{finalized}'
options:
_auth: [ 'basic_auth' ]
defaults:
......@@ -174,12 +174,20 @@ epal.ministry.retrieve-registry-id:
_controller: '\Drupal\epal\Controller\ReportsCreator::retrieveUserRegistryNo'
requirements:
_user_is_logged_in: 'TRUE'
epal.ministry.massive_mail:
path: '/ministry/send-massive-mail'
epal.ministry.massive_mail_unallocated:
path: '/ministry/send-unallocated-massive-mail'
options:
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\InformUnlocatedStudents::sendMailToUnallocatedStudents'
requirements:
_user_is_logged_in: 'TRUE'
epal.ministry.massive_mail_located:
path: '/ministry/send-located-massive-mail'
options:
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\InformUnlocatedStudents::sendMailToStudents'
_controller: '\Drupal\epal\Controller\InformUnlocatedStudents::sendMailToLocatedStudents'
requirements:
_user_is_logged_in: 'TRUE'
epal.ministry.retrievesettings:
......@@ -254,5 +262,3 @@ getlimitspercourse:
_controller: '\Drupal\epal\Controller\DirectorView::getlimitsperCourse'
requirements:
_user_is_logged_in: 'TRUE'
......@@ -4,6 +4,7 @@
* Contains \Drupal\query_example\Controller\QueryExampleController.
*/
namespace Drupal\epal\Controller;
use Drupal\Core\Entity\Query\QueryFactory;
......@@ -24,6 +25,12 @@ use Drupal\Core\TypedData\Plugin\DataType\TimeStamp;
use Drupal\Core\Language\LanguageManagerInterface;
define("SUCCESS", 0);
define("ERROR_DB", -1);
define("NO_CLASS_LIMIT_DOWN", -2);
define("SMALL_CLASS", 1);
define("NON_SMALL_CLASS", 2);
class Distribution extends ControllerBase {
protected $entity_query;
......@@ -68,9 +75,15 @@ class Distribution extends ControllerBase {
public function createDistribution(Request $request) {
//$this->findSmallClasses();
//return;
$numDistributions = 3;
$sizeOfBlock = 100000;
//POST method is checked
if (!$request->isMethod('POST')) {
return $this->respondWithStatus([
......@@ -101,6 +114,7 @@ class Distribution extends ControllerBase {
'message' => t("User Invalid Role"),
], Response::HTTP_FORBIDDEN);
}
//check where distribution can be done now ($capacityDisabled / $directorViewDisabled settings)
$capacityDisabled = false;
......@@ -188,9 +202,7 @@ class Distribution extends ControllerBase {
//checkCapacityAndArrange (school_id, class_id, sectorORcourse_id, limitUp, schoolCapacity)
$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 --> να αλλάξει
->fields('eSchool', array('id', 'capacity_class_a', 'operation_shift'));
$eepalSchools = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
foreach ($eepalSchools as $eepalSchool) {
......@@ -212,7 +224,9 @@ class Distribution extends ControllerBase {
foreach ($eepalSpecialtiesInEpal as $eepalSpecialInEp) {
$this->checkCapacityAndArrange($eepalSchool->id, "3", $eepalSpecialInEp->specialty_id, $limitUp_class, $eepalSpecialInEp->capacity_class_specialty);
//Δ' Λυκείου
$this->checkCapacityAndArrange($eepalSchool->id, "4", $eepalSpecialInEp->specialty_id, $limitUp_class, $eepalSpecialInEp->capacity_class_specialty);
if ($eepalSchool->operation_shift === "ΕΣΠΕΡΙΝΟ") {
$this->checkCapacityAndArrange($eepalSchool->id, "4", $eepalSpecialInEp->specialty_id, $limitUp_class, $eepalSpecialInEp->capacity_class_specialty);
}
}
} //end for each school/department
......@@ -221,6 +235,7 @@ class Distribution extends ControllerBase {
} //end while
$this->findSmallClasses();
} //end try
......@@ -232,14 +247,6 @@ class Distribution extends ControllerBase {
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
//return new RedirectResponse($this->redirectUrl . '?auth_token=' . $epalToken.'&auth_role=director', 302, []);
//return new RedirectResponse("../eepal/dist/#/minister/minister-view");
/*
return $this->respondWithStatus([
"message" => t("Distribution has made successfully")
], Response::HTTP_OK);
*/
$postData = null;
if ($content = $request->getContent()) {
$postData = json_decode($content);
......@@ -305,7 +312,6 @@ class Distribution extends ControllerBase {
$specialization_id = -1;
//$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++,
......@@ -319,6 +325,7 @@ class Distribution extends ControllerBase {
'specialization_id' => $specialization_id,
'points' => $epalStudent->points,
'distribution_id' => $choice_id,
'finalized' => 1,
'status' => 1,
'created' => $timestamp,
'changed' => $timestamp,)
......@@ -367,7 +374,14 @@ class Distribution extends ControllerBase {
return $row->limit_up;
}
public function checkCapacityAndArrange($epalId, $classId, $secCourId, $limitup, $capacity) {
public function checkCapacityAndArrange($epalId, $classId, $secCourId, $limitup, $capacity) {
if (!isset($capacity)) {
//print_r("<br> ΜΠΗΚΑ!!! ");
//print_r("<br> ΣΧΟΛΕΙΟ: " . $epalId . " ΤΑΞΗ: " . $classId . " ΤΟΜΕΑΣ/ΕΙΔΙΚΟΤΗΤΑ: " . $secCourId . " ΧΩΡΗΤΙΚΟΤΗΤΑ: " . $capacity);
$capacity = 0;
}
$transaction = $this->connection->startTransaction();
......@@ -451,11 +465,10 @@ class Distribution extends ControllerBase {
//$this->pendingStudents = array_diff($this->pendingStudents, array($val));
}
public function makeSelectionOfStudents(&$students, $limit) {
public function makeSelectionOfStudents_VERSION_WITH_POINTS(&$students, $limit) {
//συνάρτηση επιλογής μαθητών σε περίπτωση υπερχείλισης
// (1) έχουν απόλυτη προτεραιότητα όσοι ήδη φοιτούσαν στο σχολείο (σε περίπτωση που φοιτούσαν περισσότεροι από την χωρητικότητα, τους δεχόμαστε όλους)
// (2) αν απομένουν κενές θέσεις, επέλεξε από τους εναπομείναντες μαθητές αυτούς με τα περισσότερα μόρια. Σε περίπτωση ισοβαθμίας δεχόμαστε όλους όσους ισοβαθμούν.
//αυτοδίκαια έχουν προτεραιότητα όσοι ήδη φοιτούσαν στο σχολείο
foreach($students as $student) {
$student->student_id;
......@@ -525,6 +538,66 @@ class Distribution extends ControllerBase {
], Response::HTTP_OK);
}
public function makeSelectionOfStudents(&$students, $limit) {
//συνάρτηση επιλογής μαθητών σε περίπτωση υπερχείλισης
// (1) έχουν απόλυτη προτεραιότητα όσοι ήδη φοιτούσαν στο σχολείο (σε περίπτωση που φοιτούσαν περισσότεροι από την χωρητικότητα, τους δεχόμαστε όλους)
// (2) αν απομένουν κενές θέσεις,...τοποθέτησε και όλους τους άλλους!!.
//εύρεση αριθμού μαθητών που ήδη φοιτούσαν στο σχολείο
$cnt = 0;
foreach($students as $student) {
if ($student->currentepal === $student->epal_id) {
$cnt++;
if ($this->choice_id !== 1)
//διέγραψε τον μαθητή από τον πίνακα εκκρεμοτήτων (αν βρίσκεται εκεί)
//Κάτι τέτοιο δεν είναι δυνατό πια! (έκδοση χωρίς μόρια..)
$this->removeFromPendingStudents($student->student_id);
}
}
//print_r("<br>#ΕΓΓΡΑΦΩΝ ΠΟΥ ΟΙ ΜΑΘΗΤΕΣ ΦΟΙΤΟΥΣΑΝ ΗΔΗ:" . $cnt);
$newlimit = $limit - $cnt;
//print_r("<br>ΑΝΩΤΑΤΟ ΟΡΙΟ ΜΑΘΗΤΩΝ:" . $limit);
$transaction = $this->connection->startTransaction();
//Αν δεν απέμειναν θέσεις (δηλαδή αν η χωρητικότητα είναι μικρότερη ή ίση από το πλήθος μαθητών που ήδη φοιτούν στο σχολείο)
//τότε διέγραψέ τους από τον προσωρινό πίνακα αποτελεσμάτων και βάλε τους στον στον πίνακα εκκρεμοτήτων
if ($newlimit <= 0) {
foreach($students as $student) {
if ($student->currentepal !== $student->epal_id) {
//print_r("<br>ΣΕ ΕΚΚΡΕΜΟΤΗΤΑ - ΔΙΑΓΡΑΦΗ: " . $student->student_id);
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);
}
} //end if currentepal
} //end foreach
} //endif newlimit
return $this->respondWithStatus([
"message" => t("makeSelectionOfStudents Method of Distribution has made successfully")
], Response::HTTP_OK);
} //end function
public function calculatePoints() {
return rand(0,20);
......@@ -537,7 +610,7 @@ class Distribution extends ControllerBase {
return $res;
}
private function initializeResults() {
private function initializeResults() {
//initialize/empty epal_student_class if there are already data in it!
try {
......@@ -549,11 +622,169 @@ class Distribution extends ControllerBase {
"message" => t("An unexpected problem occured during initializeResults Method of Distribution")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
private function findSmallClasses() {
//Για κάθε σχολείο βρες τα ολιγομελή τμήματα
$sCon = $this->connection->select('eepal_school_field_data', 'eSchool')
->fields('eSchool', array('id', 'metathesis_region','operation_shift'));
$eepalSchools = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
foreach ($eepalSchools as $eepalSchool) {
//isSmallClass (school_id, class_id, sectorORcourse_id, school_category_metathesis)
// Α' τάξη
if ($this->isSmallClass($eepalSchool->id, "1", "-1", $eepalSchool->metathesis_region) === SMALL_CLASS) {
//print_r("<br> ΚΛΗΣΗ markStudentsInSmallClass: SCHOOL_ID: " . $eepalSchool->id . " CLASSID: " . "1 " . "SECTOR/COURSE ID: " . "-1 ");
if ($this->markStudentsInSmallClass($eepalSchool->id, "1", "-1") === ERROR_DB )
return ERROR_DB;
}
//print_r("<br>");
// Β' τάξη
$sCon = $this->connection->select('eepal_sectors_in_epal_field_data', 'eSchool')
->fields('eSchool', array('epal_id', 'sector_id'))
->condition('eSchool.epal_id', $eepalSchool->id, '=');
$eepalSectorsInEpal = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
foreach ($eepalSectorsInEpal as $eepalSecInEp) {
if ($this->isSmallClass($eepalSchool->id, "2", $eepalSecInEp->sector_id, $eepalSchool->metathesis_region) === SMALL_CLASS) {
//print_r("<br> ΚΛΗΣΗ markStudentsInSmallClass: SCHOOL_ID: " . $eepalSchool->id . " CLASSID: " . "2 " . "SECTOR/COURSE ID: " . $eepalSecInEp->sector_id);
if ($this->markStudentsInSmallClass($eepalSchool->id, "2", $eepalSecInEp->sector_id) === ERROR_DB )
return ERROR_DB;
}
//print_r("<br>");
}
// Γ' τάξη
$sCon = $this->connection->select('eepal_specialties_in_epal_field_data', 'eSchool')
->fields('eSchool', array('epal_id', 'specialty_id'))
->condition('eSchool.epal_id', $eepalSchool->id, '=');
$eepalSpecialtiesInEpal = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
foreach ($eepalSpecialtiesInEpal as $eepalSpecialInEp) {
if ($this->isSmallClass($eepalSchool->id, "3", $eepalSpecialInEp->specialty_id, $eepalSchool->metathesis_region) === SMALL_CLASS) {
//print_r("<br> ΚΛΗΣΗ markStudentsInSmallClass: SCHOOL_ID: " . $eepalSchool->id . " CLASSID: " . "3 " . "SECTOR/COURSE ID: " . $eepalSpecialInEp->specialty_id);
if ($this->markStudentsInSmallClass($eepalSchool->id, "3", $eepalSpecialInEp->specialty_id) === ERROR_DB )
return ERROR_DB;
}
//print_r("<br>");
}
// Δ' τάξη
if ($eepalSchool->operation_shift === "ΕΣΠΕΡΙΝΟ") {
$sCon = $this->connection->select('eepal_specialties_in_epal_field_data', 'eSchool')
->fields('eSchool', array('epal_id', 'specialty_id'))
->condition('eSchool.epal_id', $eepalSchool->id, '=');
$eepalSpecialtiesInEpal = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
foreach ($eepalSpecialtiesInEpal as $eepalSpecialInEp) {
if ($this->isSmallClass($eepalSchool->id, "4", $eepalSpecialInEp->specialty_id, $eepalSchool->metathesis_region) === SMALL_CLASS) {
//print_r("<br> ΚΛΗΣΗ markStudentsInSmallClass: SCHOOL_ID: " . $eepalSchool->id . " CLASSID: " . "4 " . "SECTOR/COURSE ID: " . $eepalSpecialInEp->specialty_id);
if ($this->markStudentsInSmallClass($eepalSchool->id, "4", $eepalSpecialInEp->specialty_id) === ERROR_DB )
return ERROR_DB;
}
//print_r("<br>");
}
} //end if ΕΣΠΕΡΙΝΟ
} //end for each school/department
} //end function
private function isSmallClass($schoolId, $classId, $sectorOrcourseId, $regionId) {
//print_r("<br> ΚΛΗΣΗ isSmallClass: SCHOOL_ID: " . $schoolId . " CLASSID: " . $classId . "SECTOR/COURSE ID: " . $sectorOrcourseId . "ΠΕΡΙΟΧΗ ΜΕΤΑΘΕΣΗΣ: " . $regionId);
$limitDown = $this->retrieveLimitDown($classId, $regionId);
//print_r("<br> ΚΑΤΩΤΑΤΟ ΟΡΙΟ ΜΑΘΗΤΩΝ: " . $limitDown);
if ($limitDown === NO_CLASS_LIMIT_DOWN)
return NO_CLASS_LIMIT_DOWN;
else if ($limitDown === ERROR_DB)
return ERROR_DB;
$numStudents = $this->countStudents($schoolId, $classId, $sectorOrcourseId);
//print_r("<br> ΑΡΙΘΜΟΣ ΜΑΘΗΤΩΝ: " . $numStudents);
if ($numStudents === ERROR_DB)
return ERROR_DB;
//Αν $numStudents == 0, γύρισε fasle, ώστε να μη γίνει περιττή κλήση στην markStudentsInSmallClass
if ( ($numStudents < $limitDown) && ($numStudents > 0) )
return SMALL_CLASS;
else
return NON_SMALL_CLASS;
}
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 {
return NO_CLASS_LIMIT_DOWN;
}
} //end try
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return ERROR_DB;
}
} //end function
private function countStudents($schoolId, $classId, $sectorOrcourseId) {
try {
$sCon = $this->connection->select('epal_student_class', 'eStudent')
->fields('eStudent', array('id'))
->condition('eStudent.epal_id', $schoolId , '=')
->condition('eStudent.currentclass', $classId , '=')
->condition('eStudent.specialization_id', $sectorOrcourseId , '=');
return $sCon->countQuery()->execute()->fetchField();
}
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return ERROR_DB;
}
}
private function markStudentsInSmallClass($schoolId, $classId, $sectorOrcourseId) {
try {
$query = $this->connection->update('epal_student_class');
$query->fields([
'finalized' => 0,
]);
$query->condition('epal_id', $schoolId);
$query->condition('currentclass', $classId);
if ($sectorOrcourseId !== "-1")
$query->condition('specialization_id', $sectorOrcourseId);
$query->execute();
}
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return ERROR_DB;
}
return SUCCESS;
}
......
......@@ -56,7 +56,7 @@ class InformUnlocatedStudents extends ControllerBase {
}
public function sendMailToStudents(Request $request) {
public function sendMailToUnallocatedStudents(Request $request) {
try {
if (!$request->isMethod('GET')) {
......@@ -89,21 +89,26 @@ class InformUnlocatedStudents extends ControllerBase {
], Response::HTTP_FORBIDDEN);
}
$cnt_success = 0;
$cnt_fail = 0;
// εύρεση μαθητών που η αίτησή τους ΔΕΝ ικανοποιήθηκε
$sCon = $this->connection->select('epal_student_class', 'eStudent')
->fields('eStudent', array('student_id'));
$epalStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$studentIds = array();
//array_push($studentIds,-1);
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);
......@@ -114,7 +119,7 @@ class InformUnlocatedStudents extends ControllerBase {
//$user = reset($users);
if ($user) {
$langcode = $user->getPreferredLangcode();
if ($this->sendEmail($user->getEmail(), $epalNonLocatedStudent->id, $epalNonLocatedStudent->created, $langcode))
if ($this->sendEmailToUnallocated($user->getEmail(), $epalNonLocatedStudent->id, $epalNonLocatedStudent->created, $langcode))
$cnt_success++;
}
else {
......@@ -124,15 +129,10 @@ class InformUnlocatedStudents extends ControllerBase {
}
} else {
/*
return $this->respondWithStatus([
'message' => t("EPAL user not found"),
], Response::HTTP_FORBIDDEN);
*/
$cnt_fail++;
}
} //end foreach
return $this->respondWithStatus([
......@@ -155,17 +155,16 @@ class InformUnlocatedStudents extends ControllerBase {
}
//sendEmail(e-mail address, A/A, application Date, lang)
private function sendEmail($email, $appId, $appDate, $langcode) {
private function sendEmailToUnallocated($email, $appId, $appDate, $langcode) {
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'epal';
$key = 'massive_mail'; //to be checked..
$key = 'massive_mail_unallocated'; //to be checked..
$to = $email;
$params['message'] = t('Σας ενημερώνουμε ότι η αίτησή σας με Α/Α: ') . t($appId) . t(' - ') . t(date('d/m/Y', $epalNonLocatedStudent->created))
.t(' δεν ικανοποιήθηκε. Παρακαλώ επικοινωνήστε άμεσα τηλεφωνικά με τη Διεύθυνση Δευτεροβάθμιας Εκπαίδευσης / Τμήμα Επαγγελματικής Εκπαίδευσης,')
.t(' στην οποία ανήκετε, ώστε να διερευνηθεί η δυνατότητα εγγραφής σας σε αντίστοιχο ΕΠΑΛ. ')
.t('\r\nΤεστ1\nΤεστ2') ;//e-mail body
$params['message'] = "Σας ενημερώνουμε ότι η αίτησή σας με Α/Α: " . $appId . " - " . date('d-m-y H:i:s', $appDate)
. " δεν ικανοποιήθηκε. Παρακαλώ επικοινωνήστε άμεσα τηλεφωνικά με τη Διεύθυνση Δευτεροβάθμιας Εκπαίδευσης / Τμήμα Επαγγελματικής Εκπαίδευσης,"
. "\r\nΤεστ1\nΤεστ2" ;//e-mail body
//add new lines with SENDER name - check it
//$params['subject'] = t('Μη ικανοποίηση ηλεκτρονικής αίτησης για εγγραφή σε ΕΠΑΛ');
......@@ -178,12 +177,177 @@ class InformUnlocatedStudents extends ControllerBase {
//$mail_sent = 1;
if ($mail_sent) {
$this->logger->info("Το mail στάλθηκε με επιτυχία");
//$this->logger->info("Το mail στάλθηκε με επιτυχία");
return 1;
}
else {
$this->logger->info("Αποτυχία αποστολής mail.");
//$this->logger->info("Αποτυχία αποστολής mail.");
return 0;
}
}
public function sendMailToLocatedStudents(Request $request) {
//$msg = "test";
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', 'epal_id'));
$epalStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$studentIds = array();
//if sizeof(studentIds = 0..distribution has not been made!)
//array_push($studentIds, -1);
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, 'IN');
$epalLocatedStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$cnt_success = 0;
$cnt_fail = 0;
foreach ($epalLocatedStudents as $epalLocatedStudent) {
//test purpose
//$epalLocatedStudent = reset($epalLocatedStudents);
$epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('user_id' => $epalLocatedStudent->user_id));
$epalUser = reset($epalUsers);
if ($epalUser) {
$user = $this->entityTypeManager->getStorage('user')->load($epalUser->user_id->target_id);
if ($user) {
$langcode = $user->getPreferredLangcode();
//εύρεση ονόματος σχολείου
$sCon = $this->connection->select('eepal_school_field_data', 'eSchool')
->fields('eSchool', array('id', 'name','street_address','phone_number'))
->condition('eSchool.id', $epalStudent->epal_id, '=');;
$epalSchools = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$epalSchool = reset($epalSchools);
//$msg = $this->sendEmailToLocated($user->getEmail(), $epalLocatedStudent->id, $epalSchool->name, $epalSchool->street_address, $epalSchool->phone_number, $epalLocatedStudent->created, $langcode);
if ($this->sendEmailToLocated($user->getEmail(), $epalLocatedStudent->id, $epalSchool->name, $epalSchool->street_address, $epalSchool->phone_number, $epalLocatedStudent->created, $langcode))
$cnt_success++;
/*
//test purposes
return $this->respondWithStatus([
//'message' => $epalLocatedStudent->id . ", " . $epalSchool->name . ", " . $epalSchool->street_address . ", " . $epalSchool->phone_number . ", " . $epalLocatedStudent->created,
'message' => $msg,
'num_success_mail' => $cnt_success,
'num_fail_mail' => $cnt_fail,
], Response::HTTP_OK);
//end test
*/
}
else {
return $this->respondWithStatus([
'message' => t("user not found"),
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
} else {
$cnt_fail++;
}
} //end foreach
return $this->respondWithStatus([
'message' => t("post successful"),
//'message' => t($msg),
'num_success_mail' => $cnt_success,
'num_fail_mail' => $cnt_fail,
], Response::HTTP_OK);
} //end try