public function __construct()
 {
     parent::__construct();
     /*
      * User is logged ?
      */
     if (!parent::_isLogged()) {
         redirect('dashboard');
         exit;
     }
 }
 public function manageView()
 {
     /*
      * Loadings libraries and helpers
      */
     $this->load->library(array('rb'));
     $this->load->helper(array('form'));
     /*
      * User is logged?
      */
     if (!parent::_isLogged()) {
         redirect('dashboard');
     }
     /*
      * General Information
      */
     $openRequestsQry = '
         SELECT r.*
         FROM status AS s
         JOIN request AS r 
         ON r.id = s.request_id 
         WHERE 
             s.current = "Y" 
             AND ( s.type = "waiting-open" 
                 OR s.type = "request_created" 
                 OR s.type = "request_moved" 
                 OR s.type = "request_extended" 
             ) 
     ';
     $openRequests = R::getAll($openRequestsQry);
     $openRequests = R::convertToBeans('request', $openRequests);
     $deadlineRequestsQry = '
         SELECT r.*
         FROM status AS s 
         JOIN request AS r 
         ON r.id = s.request_id 
         WHERE 
             ( 
                 NOW() > DATE_ADD( r.created_at, INTERVAL 20 DAY )
                 AND
                 ( SELECT COUNT(*) FROM status AS ss WHERE request_id = r.id AND type = "request_extended" ) <= 0
             )
             OR
             (
                 NOW() > DATE_ADD( r.created_at, INTERVAL 30 DAY )
                 AND
                 ( SELECT COUNT(*) FROM status AS ss WHERE request_id = r.id AND type = "request_extended" ) > 0
             )
     ';
     $deadlineRequests = R::getAll($deadlineRequestsQry);
     $deadlineRequests = R::convertToBeans('request', $deadlineRequests);
     $repliedRequestsQry = '
         SELECT r.*
         FROM status AS s
         JOIN request AS r 
         ON r.id = s.request_id 
         WHERE 
             s.current = "Y" 
             AND s.type = "request_replied" 
     ';
     $repliedRequests = R::getAll($repliedRequestsQry);
     $repliedRequests = R::convertToBeans('request', $repliedRequests);
     $data = array('ics' => R::find('ic', ' active="yes" '), 'openRequests' => count($openRequests), 'deadlineRequests' => count($deadlineRequests), 'replyRequests' => count($repliedRequests), 'totalRequests' => R::count('request'));
     /*
      * Loading views
      */
     $this->load->view('dashboard/template/header');
     $this->load->view('dashboard/template/menu', array('menu' => parent::_getMenu(parent::_getIc()->id), 'ics' => parent::_getIcs(), 'ic' => parent::_getIc()));
     $this->load->view('dashboard/manage', $data);
     $this->load->view('dashboard/template/footer');
 }
 public function changeIcDo()
 {
     /* 
      * Loading libraries and helpers
      */
     $this->load->library(array('rb', 'form_validation', 'session'));
     /*
      * User is logged ?
      */
     if (!parent::_isLogged()) {
         redirect('dashboard');
         exit;
     }
     /* 
      * Getting Post Data
      */
     $id = $this->input->post('id');
     /* 
      * Loading User
      */
     $user = R::findOne('user', ' id=? AND active = "yes" ', array($this->session->userdata('user_id')));
     /* 
      * User has auth to this IC?
      */
     foreach ($user->via('permission')->sharedIcList as $ic) {
         if ($ic->id == $id) {
             $this->session->set_userdata(array('user_ic' => $id));
             redirect('dashboard');
             exit;
         }
     }
     /* 
      * User can't change to the IC
      */
     $this->session->set_flashdata('error', 'Você não tem autorização para utilizar este Centro de Informação');
     redirect('dashboard');
 }
 public function answeredView($id = NULL)
 {
     /*
      * Loading libraries
      */
     $this->load->library(array('rb', 'session'));
     $this->load->helper(array('form'));
     /*
      * User is logged?
      */
     if (!parent::_isLogged()) {
         redirect('dashboard');
         exit;
     }
     /*
      * Verify if user has autority to view the ic's
      */
     if (!parent::_hasAuth('view_request', $this->session->userdata('user_ic'))) {
         echo "Você não tem permissão para realizar este procedimento.";
         exit;
     }
     /* 
      * If $id is NULL, then load all answered requests. Otherwise, show only the specified request
      */
     if ($id == NULL) {
         /* 
          * Verify if the user is logged in MASTER IC or not. If it's the MASTER IC
          * then it shows all the Requests, otherwise, only show Requests from actual IC.
          */
         $requests = null;
         $isMasterIc = R::count('ic', 'id=? AND master="yes" ', array($this->session->userdata('user_ic')));
         if ($isMasterIc) {
             $rows = R::getAll('
                 SELECT r.* FROM request AS r 
                 JOIN status AS s ON s.request_id = r.id
                 WHERE s.type = "request_replied" AND s.current = "Y"
             ');
             $requests = R::convertToBeans('request', $rows);
         } else {
             $rows = R::getAll('
                 SELECT r.* FROM request AS r 
                 JOIN status AS s ON s.request_id = r.id
                 WHERE s.type = "request_replied" AND s.current = "Y" AND s.ic_id = ?
                 ', array($this->session->userdata('user_ic')));
             $requests = R::convertToBeans('request', $rows);
         }
         /*
          * Loading views
          */
         $this->load->view('dashboard/template/header');
         $this->load->view('dashboard/template/menu', array('menu' => parent::_getMenu(parent::_getIc()->id), 'ics' => parent::_getIcs(), 'ic' => parent::_getIc()));
         $this->load->view('dashboard/request/answered', array('requests' => $requests));
         $this->load->view('dashboard/template/footer');
     } else {
         /* 
          * Loading the request
          */
         $request = R::findOne('request', ' id = ? ', array($id));
         /* 
          * Request exists? 
          */
         if ($request == NULL) {
             $this->session->set_flashdata('success', 'A solicitação não existe.');
             redirect('dashboard/request/answered');
         }
         /* 
          * Getting current status of request
          */
         $status = R::findOne('status', 'request_id = ? AND current = "Y" ', array($request->id));
         /*
          * Can user access the request?
          */
         if (!self::_canAcessRequest($request)) {
             $this->session->set_flashdata('success', 'Você não tem autorização para acessar esta solicitação.');
             redirect('dashboard/request/answered');
         }
         /*
          * Loading views
          */
         $this->load->view('dashboard/template/header');
         $this->load->view('dashboard/template/menu', array('menu' => parent::_getMenu(parent::_getIc()->id), 'ics' => parent::_getIcs(), 'ic' => parent::_getIc()));
         $this->load->view('dashboard/request/answered-select', array('status' => $status, 'request' => $request));
         $this->load->view('dashboard/template/footer');
     }
 }