Commit ff000f88 authored by Νίκος Κατσαούνος's avatar Νίκος Κατσαούνος
Browse files

Updated SubmittedApplications Controller in order to support app deletions...

Updated SubmittedApplications Controller in order to support app deletions from the Director, updated InformStudent Controller/Component in order to manage two application periods
parent 67978471
......@@ -191,29 +191,35 @@ epal.ministry.retrieve-registry-id:
requirements:
_user_is_logged_in: 'TRUE'
epal.ministry.massive_mail_unallocated:
path: '/ministry/send-unallocated-massive-mail'
path: '/ministry/send-unallocated-massive-mail/{period}'
options:
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\InformUnlocatedStudents::sendMailToUnallocatedStudents'
period: 2
requirements:
_user_is_logged_in: 'TRUE'
period: ^[0,1,2]$
epal.ministry.massive_mail_unallocated_smallclass:
path: '/ministry/send-unallocated-sc-massive-mail'
path: '/ministry/send-unallocated-sc-massive-mail/{period}'
options:
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\InformUnlocatedStudents::sendMailToUnallocatedStudentsSC'
period: 2
requirements:
_user_is_logged_in: 'TRUE'
period: ^[0,1,2]$
epal.ministry.massive_mail_located:
path: '/ministry/send-located-massive-mail'
path: '/ministry/send-located-massive-mail/{period}'
options:
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\InformUnlocatedStudents::sendMailToLocatedStudents'
period: 2
requirements:
_user_is_logged_in: 'TRUE'
period: ^[0,1,2]$
epal.ministry.retrievesettings:
path: '/ministry/retrieve-settings'
options:
......@@ -326,3 +332,11 @@ epal.totalstudents:
_controller: '\Drupal\epal\Controller\HelpDesk::findTotalStudents'
requirements:
_user_is_logged_in: 'TRUE'
epal.application_delete_director:
path: '/epal/application/deleteFromDirector'
options:
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\SubmitedApplications::deleteApplicationFromDirector'
requirements:
_user_is_logged_in: 'TRUE'
<?php
/**
* @file
* Contains \Drupal\query_example\Controller\QueryExampleController.
* Contains \Drupal\query_example\Controller\QueryExampleController
*/
namespace Drupal\epal\Controller;
......@@ -35,9 +36,8 @@ class InformUnlocatedStudents extends ControllerBase {
EntityTypeManagerInterface $entityTypeManager,
QueryFactory $entity_query,
Connection $connection,
LoggerChannelFactoryInterface $loggerChannel)
{
LoggerChannelFactoryInterface $loggerChannel
) {
$this->entityTypeManager = $entityTypeManager;
$this->entity_query = $entity_query;
$connection = Database::getConnection();
......@@ -55,8 +55,7 @@ class InformUnlocatedStudents extends ControllerBase {
);
}
public function sendMailToUnallocatedStudents(Request $request) {
public function sendMailToUnallocatedStudents(Request $request, $period) {
try {
if (!$request->isMethod('GET')) {
......@@ -76,76 +75,46 @@ class InformUnlocatedStudents extends ControllerBase {
}
//user role validation
$roles = $user->getRoles();
$validRole = false;
foreach ($roles as $role)
if ($role === "ministry") {
$validRole = true;
break;
}
if (!$validRole) {
if (false === in_array('ministry', $user->getRoles())) {
return $this->respondWithStatus([
'message' => t("User Invalid Role"),
], Response::HTTP_FORBIDDEN);
}
$cnt_success = 0;
$cnt_fail = 0;
// εύρεση μαθητών που η αίτησή τους ΔΕΝ ικανοποιήθηκε,
//(δεν κατανεμήθηκαν πουθενά)
$sCon = $this->connection->select('epal_student_class', 'eStudent')
->fields('eStudent', array('student_id'));
//->condition('eStudent.finalized', 0 , '=');
$epalStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
$studentIds = array();
array_push($studentIds,-1);
foreach ($epalStudents as $epalStudent)
array_push($studentIds, $epalStudent->student_id);
// take care of second_period parameter
if ($period != 0 && $period != 1) {
$period = null;
}
$sCon = $this->connection->select('epal_student', 'eStudent')
// εύρεση μαθητών που η αίτησή τους ΔΕΝ ΙΚΑΝΟΠΟΙΗΘΗΚΕ (δεν κατανεμήθηκαν πουθενά)
$sCon = $this->connection->select('epal_student', 'eStudent');
if ($period !== null) {
$sCon->condition('eStudent.second_period', $period, '=');
}
$sCon->leftJoin('epal_student_class', 'eStudentClass', 'eStudent.id = eStudentClass.student_id');
$sCon->fields('eStudentClass', array('student_id'))
->fields('eStudent', array('id', 'user_id', 'created'))
->condition('eStudent.id', $studentIds, 'NOT IN');
->isNull('eStudentClass.student_id');
$sCon->join('epal_users', 'eUsers', 'eUsers.user_id = eStudent.user_id');
$sCon->join('users_field_data', 'users', 'users.uid = eUsers.user_id');
$sCon->fields('users', array('mail', 'preferred_langcode'));
$epalNonLocatedStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
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->sendEmailToUnallocated($user->getEmail(), $epalNonLocatedStudent->id, $epalNonLocatedStudent->created, $langcode))
$cnt_success = 0;
$cnt_fail = 0;
foreach ($epalNonLocatedStudents as $student) {
if ($this->sendEmailToUnallocated($student->mail, $student->id, $student->created, $student->preferred_langcode)) {
$cnt_success++;
}
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"),
'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([
......@@ -156,7 +125,7 @@ class InformUnlocatedStudents extends ControllerBase {
}
public function sendMailToUnallocatedStudentsSC(Request $request) {
public function sendMailToUnallocatedStudentsSC(Request $request, $period) {
try {
if (!$request->isMethod('GET')) {
......@@ -176,89 +145,64 @@ class InformUnlocatedStudents extends ControllerBase {
}
//user role validation
$roles = $user->getRoles();
$validRole = false;
foreach ($roles as $role)
if ($role === "ministry") {
$validRole = true;
break;
}
if (!$validRole) {
if (false === in_array('ministry', $user->getRoles())) {
return $this->respondWithStatus([
'message' => t("User Invalid Role"),
], Response::HTTP_FORBIDDEN);
}
$cnt_success = 0;
$cnt_fail = 0;
// take care of second_period parameter
if ($period != 0 && $period != 1) {
$period = null;
}
// εύρεση μαθητών που η αίτησή τους ΔΕΝ ικανοποιήθηκε,
//(δεν κατανεμήθηκαν πουθενά)
$sCon = $this->connection->select('epal_student_class', 'eStudent')
->fields('eStudent', array('student_id'))
->condition('eStudent.finalized', 0 , '=');
$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_class', 'eStudentClass')
->fields('eStudentClass', array('student_id'));
if ($period !== null) {
$sCon->join('epal_student', 'eStudent', 'eStudent.id = eStudentClass.student_id AND eStudent.second_period = :second_period', [':second_period' => $period]);
} else {
$sCon->join('epal_student', 'eStudent', 'eStudent.id = eStudentClass.student_id');
}
$sCon = $this->connection->select('epal_student', 'eStudent')
->fields('eStudent', array('id', 'user_id', 'created'))
->condition('eStudent.id', $studentIds, 'IN');
$sCon->fields('eStudent', array('id', 'user_id', 'created'));
$sCon->join('epal_users', 'eUsers', 'eUsers.user_id = eStudent.user_id');
$sCon->join('users_field_data', 'users', 'users.uid = eUsers.user_id');
$sCon->fields('users', array('mail', 'preferred_langcode'));
$sCon->join('eepal_school_field_data', 'eSchool', 'eSchool.id = eStudentClass.user_id');
$sCon->fields('eSchool', array('id', 'name','street_address', 'phone_number'));
$sCon->condition('eStudentClass.finalized', 0, '=');
$epalNonLocatedStudents = $sCon->execute()->fetchAll(\PDO::FETCH_OBJ);
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->sendEmailToUnallocated($user->getEmail(), $epalNonLocatedStudent->id, $epalNonLocatedStudent->created, $langcode))
$cnt_success++;
}
else {
return $this->respondWithStatus([
'message' => t("user not found"),
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
$cnt_success = 0;
$cnt_fail = 0;
foreach ($epalNonLocatedStudents as $student) {
if ($this->sendEmailToUnallocated($student->mail, $student->id, $student->created, $student->preferred_langcode)) {
$cnt_success++;
} else {
$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 ")
"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 sendEmailToUnallocated($email, $appId, $appDate, $langcode) {
$mailManager = \Drupal::service('plugin.manager.mail');
private function sendEmailToUnallocated($email, $appId, $appDate, $langcode)
{
$module = 'epal';
$key = 'massive_mail_unallocated'; //to be checked..
$to = $email;
......@@ -266,20 +210,12 @@ class InformUnlocatedStudents extends ControllerBase {
$params['message'] = "Σας ενημερώνουμε ότι η αίτησή σας με Α/Α: " . $appId . " / " . date('d-m-y H:i:s', $appDate)
. " είναι σε εκκρεμότητα. Για την τοποθέτησή σας και τις ενέργειες που πρέπει να κάνετε θα ενημερωθείτε με νέο μήνυμα, με τον ίδιο τρόπο, μετά τις 8-7-2017."
. "\r\n\r\n Ομάδα Διαχείρισης της εφαρμογής e-epal."
. "\r\n Προσοχή: το μήνυμα που διαβάζετε είναι αυτοματοποιημένο. Παρακαλώ μην απαντάτε σε αυτό το μήνυμα.." ;//e-mail body" ;//e-mail body
$send = true;
$mail_sent = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
. "\r\n Προσοχή: το μήνυμα που διαβάζετε είναι αυτοματοποιημένο. Παρακαλώ μην απαντάτε σε αυτό το μήνυμα.." ;
if ($mail_sent) {
return 1;
}
else {
return 0;
}
return $this->sendEmails($module, $key, $to, $langcode, $params);
}
public function sendMailToLocatedStudents(Request $request) {
public function sendMailToLocatedStudents(Request $request, $period) {
try {
if (!$request->isMethod('GET')) {
......@@ -299,121 +235,68 @@ class InformUnlocatedStudents extends ControllerBase {
}
//user role validation
$roles = $user->getRoles();
$validRole = false;
foreach ($roles as $role)
if ($role === "ministry") {
$validRole = true;
break;
}
if (!$validRole) {
if (false === in_array('ministry', $user->getRoles())) {
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'))
->condition('eStudent.finalized', 1 , '=');
$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');
// take care of second_period parameter
if ($period != 0 && $period != 1) {
$period = null;
}
// εύρεση μαθητών που η αίτησή τους ΙΚΑΝΟΠΟΙΗΘΗΚΕ
$sCon = $this->connection->select('epal_student_class', 'eStudentClass')
->fields('eStudentClass', array('student_id', 'epal_id'));
if ($period !== null) {
$sCon->join('epal_student', 'eStudent', 'eStudent.id = eStudentClass.student_id AND eStudent.second_period = :second_period', [':second_period' => $period]);
} else {
$sCon->join('epal_student', 'eStudent', 'eStudent.id = eStudentClass.student_id');
}
$sCon->fields('eStudent', array('id', 'user_id', 'created'));
$sCon->join('epal_users', 'eUsers', 'eUsers.user_id = eStudent.user_id');
$sCon->join('users_field_data', 'users', 'users.uid = eUsers.user_id');
$sCon->fields('users', array('mail', 'preferred_langcode'));
$sCon->join('eepal_school_field_data', 'eSchool', 'eSchool.id = eStudentClass.user_id');
$sCon->fields('eSchool', array('id', 'name','street_address', 'phone_number'));
$sCon->condition('eStudentClass.finalized', 1 , '=');
$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))
foreach ($epalLocatedStudents as $student) {
// $user->getEmail() users_mail = $student->mail
// $epalLocatedStudent->id eStudent_id = $student->id
// $epalSchool->name eSchool_name = $student->name
// $epalSchool->street_address eSchool_street_address = $student->street_address
// $epalSchool->phone_number eSchool_phone_number = $student->phone_number
// $epalLocatedStudent->created eStudent_created = $student->created
// $langcode users_preferred_langcode = $student->preferred_langcode
if ($this->sendEmailToLocated($student->mail, $student->id, $student->name, $student->street_address, $student->phone_number, $student->created, $student->preferred_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
}
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return $this->respondWithStatus([
"message" => t("An unexpected problem occured during sendMailToStudents Method ")
"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 sendEmailToLocated($email, $appId, $schName, $schStreet, $schTel, $appDate, $langcode) {
//$this->logger->warning("test");
$mailManager = \Drupal::service('plugin.manager.mail');
private function sendEmailToLocated($email, $appId, $schName, $schStreet, $schTel, $appDate, $langcode)
{
$module = 'epal';
$key = 'massive_mail_located';
$to = $email;
......@@ -424,26 +307,19 @@ class InformUnlocatedStudents extends ControllerBase {
. "προσκομίζοντας τα απαραίτητα δικαιολογητικά. Διεύθυνση σχολείου: " . $schStreet
. " Τηλέφωνο επικοινωνίας σχολείου: " . $schTel
. "\r\n\r\n Ομάδα Διαχείρισης της εφαρμογής e-epal."
. "\r\n Προσοχή: το μήνυμα που διαβάζετε είναι αυτοματοποιημένο. Παρακαλώ μην απαντάτε σε αυτό το μήνυμα." ;//e-mail body
. "\r\n Προσοχή: το μήνυμα που διαβάζετε είναι αυτοματοποιημένο. Παρακαλώ μην απαντάτε σε αυτό το μήνυμα.";
//$this->logger->info($params['message']);
return $this->sendEmails($module, $key, $to, $langcode, $params);
}
private function sendEmails($module, $key, $to, $langcode, $params)
{
$mailManager = \Drupal::service('plugin.manager.mail');
$send = true;
$mail_sent = $mailManager->mail($module, $key, $to, $langcode, $params, null, $send);
$mail_sent = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
if ($mail_sent) {
return 1;
//return $params['message'];
return ($mail_sent['result'] == true);
}
else {
return 0;
//return $params['message'];
}
}
private function respondWithStatus($arr, $s) {
$res = new JsonResponse($arr);
......@@ -451,7 +327,4 @@ class InformUnlocatedStudents extends ControllerBase {
return $res;
}
}
......@@ -668,6 +668,132 @@ class SubmitedApplications extends ControllerBase
}
public function deleteApplicationFromDirector(Request $request)
{
if (!$request->isMethod('POST')) {
return $this->respondWithStatus([
"error_code" => 2001
], Response::HTTP_METHOD_NOT_ALLOWED);
}
$authToken = $request->headers->get('PHP_AUTH_USER');
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
$user = reset($users);
if ($user) {
$epalId = $user->init->value;
$schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $epalId));
$school = reset($schools);
if (!$school) {
$this->logger->warning('no access to this school='.$user->id());
return $this->respondWithStatus([
"message" => "No access to this school"
], Response::HTTP_FORBIDDEN);
}
$userRoles = $user->getRoles();
$userRole = '';
foreach ($userRoles as $tmpRole) {
if ($tmpRole === 'epal') {
$userRole = $tmpRole;
}
}
if ($userRole === '') {
return $this->respondWithStatus([
'error_code' => 4003,
], Response::HTTP_FORBIDDEN);
} elseif ($userRole === 'epal') {
$content = $request->getContent();
$applicationId = 0;
if (!empty($content)) {
$postArr = json_decode($content, TRUE);
$applicationId = $postArr['applicationId'];
}
else {
return $this->respondWithStatus([
"error_code" => 5002
], Response::HTTP_BAD_REQUEST);
}
$transaction = $this->connection->startTransaction();
try {