public function getLatestActivityDate()
 {
     $latestTicketEvent = TicketEvents::model()->findBySql("select max(event_recorded_date) as event_recorded_date, description as id  from (select event_type_id, event_recorded_date from ticket_events where ticket_id = " . $this->id . " and (event_type_id != 9 and event_type_id !=8) order by event_recorded_date desc)x left join event_type on event_type.id = event_type_id; ");
     return "" . $latestTicketEvent->id . " " . date("M d, Y", strtotime($latestTicketEvent->event_recorded_date));
 }
 public function actionReject($id)
 {
     /*Retrieve ticket Details */
     $model = Ticket::model()->findByPk($id);
     $old_mentor = $model->assign_user_id;
     $current_user = User::model()->getCurrentUserId();
     if ($old_mentor == $current_user) {
         $this->render("reject");
         //begin collecting all the data
         $tier = 1;
         if ($model->isEscalated != null) {
             $tier = 2;
         }
         $rule = ReassignRules::model()->findBySql("Select * from reassign_rules where rule_id =1");
         $count = TicketEvents::model()->findBySql("Select COUNT(id) as 'id' from ticket_events where event_type_id = 3 and ticket_id =:tid", array(":tid" => $id));
         if ($count->id >= $rule->setting) {
             //reassign to system admin to many reassigns.
             $model->assign_user_id = 5;
         } else {
             $boolean = true;
             /* Identify is the subdomain was specified by the user */
             if ($model->subdomain_id == null) {
                 $boolean = false;
                 $model->assign_user_id = User::reassignTicket($model->domain_id, $boolean, $old_mentor, $tier, $id);
             } else {
                 $model->assign_user_id = User::reassignTicket($model->subdomain_id, $boolean, $old_mentor, $tier, $id);
             }
         }
         $recordStatusChangeFromRejectToPending = false;
         /*Change the status of the ticket to Pending from Reject */
         if ($model->status == Ticket::Status_Reject) {
             $model->status = Ticket::Status_Pending;
             $recordStatusChangeFromRejectToPending = true;
         }
         $model->assigned_date = new CDbExpression('NOW()');
         /* Get the current date and time */
         //Save all the ticket with it's transactions
         $saved = true;
         $trans = Yii::app()->db->beginTransaction();
         try {
             //save the ticket
             $model->save();
             //save the Reassign event
             TicketEvents::recordEvent(EventType::Event_AssignedOrReasignedToUser, $model->id, $old_mentor, $model->assign_user_id, NULL);
             if ($recordStatusChangeFromRejectToPending) {
                 TicketEvents::recordEvent(EventType::Event_Status_Changed, $model->id, Ticket::Status_Reject, Ticket::Status_Pending, NULL);
             }
             $trans->commit();
         } catch (Exception $e) {
             $trans->rollback();
             Yii::log("Error occurred while saving the ticket or its events. Rolling back... . Failure reason as reported in exception: " . $e->getMessage(), CLogger::LEVEL_ERROR, __METHOD__);
             $saved = false;
         }
         //prepare the response
         $response = array();
         if ($saved) {
             /*If save if true send Notification the the Domain Mentor who was assigned the ticket */
             User::sendStatusReassignedEmailNotificationToOldMentor($model->id, $old_mentor, User::model()->getCurrentUserId());
             if (User::isCurrentUserAdmin()) {
                 $response['url'] = "/coplat/index.php/home/adminHome";
             } else {
                 $response['url'] = "/coplat/index.php/home/userHome";
             }
         } else {
             $response['url'] = "/coplat/index.php/home/userHome";
         }
         User::sendTicketAssignedEmailNotification($model->creator_user_id, $model->assign_user_id, $model->domain_id, $model->id);
         //
         //Yii::app()->request->redirect(Yii::app()->homeURL);
     } else {
         Yii::app()->request->redirect(Yii::app()->homeURL);
     }
 }