InformUnlocatedStudents.php 6.69 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 39 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 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 158 159 160 161 162 163 164 165 166 167 168
<?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
169 170 171 172
				//$params['subject'] = t('Μη ικανοποίηση ηλεκτρονικής αίτησης για εγγραφή σε ΕΠΑΛ');

				//$this->logger->warning($params['message']);

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
				//$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;
			}




}