/**
  * Schedules a new video conference
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new VideoConference();
     $invitationError = "";
     if (isset($_POST['VideoConference'])) {
         $model->attributes = $_POST['VideoConference'];
         //get the rest of the attributes
         $moderator = User::model()->findByAttributes(array("username" => Yii::app()->user->getId()));
         $model->moderator_id = $moderator->id;
         //get the current users id
         $model->scheduled_on = date("Y-m-d H:i:s");
         //now date
         $dateopt = $_POST['dateopt'];
         if ($dateopt == "now") {
             //if scheduled for now, use now datetime
             $model->scheduled_for = date("Y-m-d H:i:s");
         } else {
             if ($dateopt == "later") {
                 //else get the date and validate it
                 if (isset($_POST["date"]) && isset($_POST["time"])) {
                     $format = "m/d/Y H:i a";
                     $date = DateTime::createFromFormat($format, $_POST['date'] . "  " . strtolower($_POST['time']));
                     if (!$date) {
                         $model->addError('date', "Wrong format for the date and/or time ");
                         $this->render('create', array('model' => $model));
                         exit;
                     } else {
                         $model->scheduled_for = $date->format("Y-m-d H:i:s");
                     }
                 } else {
                     $model->addError('date', "Empty date or time");
                     $this->render('create', array('model' => $model));
                     exit;
                 }
             }
         }
         if ($model->save()) {
             //if we can save the date
             $inviteeList = $_POST['invitees'];
             //emails of all the invitees
             foreach ($inviteeList as $username) {
                 if ($username != null) {
                     $lname = substr($username, 0, stripos($username, ","));
                     $fname = substr($username, stripos($username, ",") + 2);
                     $user = User::model()->findAllBySql("Select * from user where fname =:fnam AND lname =:lnam", array(":fnam" => $fname, ":lnam" => $lname));
                     if ($user == null) {
                         $invitationError .= $username . " does not appear in our records. <br>";
                     }
                     foreach ($user as $invitee) {
                         $email = $invitee->email;
                         if ($email == null) {
                             //if invitee does not exist, record the error and continue
                             $invitationError .= $username . " does not appear in our records. <br>";
                             continue;
                         }
                         //moderator cannot invite him/herself
                         if ($invitee->id == $moderator->id) {
                             continue;
                         }
                         $invitation = new VCInvitation();
                         $invitation->invitee_id = $invitee->id;
                         $invitation->videoconference_id = $model->id;
                         $invitation->status = "Unknown";
                         if (!$invitation->save()) {
                             //an error occurred
                             $invitationError .= "An error occurred upon sending the invitation to " . $username . ".";
                         } else {
                             $inviteefullName = $invitee->fname . " " . $invitee->lname;
                             VCInvitation::sendInvitationEmail($model, $inviteefullName, $email);
                         }
                     }
                     if ($invitationError != "") {
                         //if there was an error
                         Yii::app()->user->setFlash('invitation-error', $invitationError);
                     }
                 }
             }
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }