/** * A method to process POST request for adding a new meeting * @param null $post the $_POST array */ public function addPost($post = null) { # Define default header, if it's not repeating meeting, we'll change it afterwards # in order to be redirected to a meeting we've created $header = 'Location: ' . SITE_URL . 'meetings'; # If it's a post request, we'll have parameters passed if ($post) { # Create an empty meeting $meeting = $this->model('Meeting'); # Get details from post request $deadline = $post['deadline']; $deadline_time_hours = $post['deadline_time_hours']; $deadline_time_minutes = $post['deadline_time_minutes']; # Default for is repeating is 0 $isRepeating = 0; # Default time for repeat until is nothing $repeatUntil = 0; $dateTimeRepeatUntil = null; # If isRepeating was checked if (isset($post['isRepeating'])) { $isRepeating = 1; # Get date from repeatUntil input field $repeatUntil = $post['repeatUntil']; # Convert date into DB friendly format and keep $dateTimeRepeatUntil object so we can use it # in order to create more meetings until this date (because it's a repeated meeting) $dateTimeRepeatUntil = DateTime::createFromFormat('d-m-Y H:i', $repeatUntil . " 23:59"); # $repeatUntil will contain DB friendly format, whereas $dateTimeRepeatUntil will contain # object that we can manipulate with it further $repeatUntil = $dateTimeRepeatUntil->format('Y-m-d H:i:s'); } # Getting more data from post request... $isApproved = 0; if (isset($post['isApproved'])) { $isApproved = 1; } $arrivedOnTime = 0; if (isset($post['arrivedOnTime'])) { $arrivedOnTime = 1; } $takenPlace = 0; if (isset($post['takenPlace'])) { $takenPlace = 1; } # If we are logged in as a supervisor, isApproved option is automatically set to true because # supervisor adds approved meetings (only student adds meetings that need approval) if (HTTPSession::getInstance()->USER_TYPE == User::USER_TYPE_SUPERVISOR) { $isApproved = 1; } # Set correct format of provided date $dateTime = DateTime::createFromFormat('d-m-Y H:i', $deadline . " " . $deadline_time_hours . ":" . $deadline_time_minutes); $date = $dateTime->format('Y-m-d H:i:s'); # Set meeting with provided information $meeting->setDatetime($date); $meeting->setIsRepeating($isRepeating); $meeting->setRepeatUntil($repeatUntil); $meeting->setIsApproved($isApproved); $meeting->setArrivedOnTime($arrivedOnTime); $meeting->setTakenPlace($takenPlace); $meeting->setProjectId(HTTPSession::getInstance()->PROJECT_ID); # Set googleEventId to null as default $googleEventId = null; # Check if meeting is approved by a supervisor and user is logged in as google user if ($isApproved && !empty(GoogleAuth::$auth)) { # Get google auth format of datetime $datetimeGoogleStart = DatetimeConverter::getGoogleAuthDateTimeFormat($date); $datetimeGoogleEnd = $dateTime; $datetimeGoogleEnd->modify("+1 hour"); $datetimeGoogleEnd = DatetimeConverter::getGoogleAuthDateTimeFormat($datetimeGoogleEnd->format('Y-m-d H:i:s')); # Get all users associated with the project $usersArray = ProjectFactory::getAllUsersForProject(HTTPSession::getInstance()->PROJECT_ID); $users = ""; $attendees = array(); foreach ($usersArray as $user) { # Concatenate them into string $users .= $user->getFirstName() . " " . $user->getLastName() . ", "; # Add attendees $attendees[] = array('email' => $user->getEmail()); } # Remove , at the end $users = substr($users, 0, strlen($users) - 2); # Get project name from project ID $project = new Project(HTTPSession::getInstance()->PROJECT_ID); $project = $project->getName(); # We can add it to the google calendar and save the id of this event $googleEventId = GoogleAuth::getInstance()->addEventToCalendar($project, $users, $datetimeGoogleStart, $datetimeGoogleEnd, $attendees); } # If it's a google event, set it in the meeting if (isset($googleEventId)) { $meeting->setGoogleEventId($googleEventId); } # If the meeting is repeating we have to create more than one record in the database # in order to display them in the list # However other meetings won't be added to google since in this version only a single meeting # can be added to the google calendar if ($isRepeating) { # Array of meetings $meetings = []; $i = 0; # Advance current date of the meeting by a week $dateTime->modify('+1 week'); # while datetime <= datetimeRepeatUntil, create new object # each time a new object is created, datetime is advanced by 7 days while ($dateTime <= $dateTimeRepeatUntil) { # Create a copy of the existing meeting object, however with a new date $meetings[$i] = clone $meeting; $meetings[$i]->setDatetime($dateTime->format('Y-m-d H:i:s')); # Save the copy in the database $meetings[$i]->Save(); # Advance current date of the meeting by a week $dateTime->modify('+1 week'); } } # Save the meeting $meeting->Save(); if (!$isRepeating) { # If it's not repeating meeting, we want to display the only meeting we've created $header = 'Location: ' . SITE_URL . 'meetings/' . $meeting->getID(); # Create a new notification only if it's not repeating meeting, creating a notification # for repeated meeting would be a future work new NotificationMeeting($meeting->getID(), NotificationMeeting::ADDED); } } # Redirect back to action points header($header); die; }