Clase Online

De KumbiaPHP Framework Wiki

Descripción

La clase online lo que hace es crear un registro del tiempo de conección para usuario logeado, para esto vamos asumir que ya tenemos nuestra tabla usuarios más una tabla de perfiles "roles", Tambien la forma de obtener los id de usuarios conectados, habra que crear una variable de session con la class Session y poder almacenar los valores en la tabla logs.

Tablas

Lo primero es crear nuestra tabla logs

CREATE TABLE  `logs` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `usuarios_id` int(4) DEFAULT NULL,
  `fecha_at` datetime DEFAULT NULL,
  `fecha_in` datetime DEFAULT NULL,
  `ip` varchar(16) CHARACTER SET latin1 DEFAULT NULL,
  `perfiles_id` int(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8


clase online

Esta class es de tipo estatica y la creamos como una libreria en nuestra app. "apps/libs/online.php"

class Online{
    static  function setUser(){
        Load::model('logs');
        $last_id                 =      Session::get('logs_id');
        $rec                     =      new Logs();
        $rec->find($last_id);
        $rec->usuarios_id        =      Session::get('usuarios_id');
        $rec->perfiles_id        =      Session::get('perfiles_id');
        $rec->ip                 =      $_SERVER['REMOTE_ADDR'];
        $rec->save();
        if(ActiveRecord::static_select_one("LAST_INSERT_ID()")!=0)
            Session::set('logs_id', ActiveRecord::static_select_one("LAST_INSERT_ID()"));
    }
}

Forma de uso

Con esto ya estamos listo para que nuestra app comienze almacenar datos en la base. Entonces el primer paso es crear las dos varianles de sesion "usuarios_id" y "perfiles_id", para esto la mejor forma es cuando el usuario se autentifique en el sistema "LOGIN" la creamos,

Session::set('usuarios_id', $usuarios->id);
Session::set('perfiles_id', $usuarios->perfiles_id);

y en el archivo application.php creamos un initialize de esta manera.


    public function initialize (){
        //si login esta ok
	if (....){
	    Load::lib('online');
	    Online::setUser();
        }
    }

Mostrar los datos

En esta parte cada programador puede mostrar los datos como quiera. solo deben hacer una consulta y recorrer el modelo con un foreach para poder crear una tabla. Yo lo haré con el JQuery_UI_en_Kumbiaphp_Spirit_beta2_calendar

controlador utils_controller.php

  • Kumbiaphp 1.0 Beta1
    public function logs(){
        Flash::notice('Logs del sistema. Fecha hora: ' . date("d-m-Y H:m:s"));
        $this->fecha    =   date('d-m-Y');
        $this->items    =   array();
        if($this->has_post('fecha')){
            $this->fecha    =   $this->post('fecha');
            $this->items    =   Load::model('logs')->getLogs($this->fecha);
        }
    }
  • Kumbiaphp 1.0 Beta2
    public function logs(){
        Flash::info('Logs del sistema. Fecha hora: ' . date("d-m-Y H:m:s"));
        $this->fecha    =   date('d-m-Y');
        $this->items    =   array();
        if(Input::hasPost('fecha')){
            $this->fecha    =   Input::post('fecha');
            $this->items    =   Load::model('logs')->getLogs($this->fecha);
        }
    }


Vista

  • Kumbiaphp 1.0 Beta1
<?php View::content(); ?>
<?php echo form_tag($controller_name . '/' . $action_name); ?>
<div align="center">
   <?php echo date_field_tag('fecha','size: 10'); ?>
   <?php echo submit_tag('Ir')?>
</div>
<?php echo end_form_tag() ?>
<table class='table_grilla'>
   <tr>
   <th>NONBRES</th>
   <th>APELLIDOS</th>
   <th>PERFIL</th>
   <th>IP</th>
   <th>FECHA INI</th>
   <th>HORA INI</th>
   <th>FECHA FIN</th>
   <th>HORA FIN</th>
   <th>DURACION</th>
</tr>
<?php foreach($items as $item): ?>
      <tr>
         <td><?php echo $item->nombres ?></td>
         <td><?php echo $item->apellidos ?></td>
         <td><?php echo $item->perfil ?></td>
         <td><?php echo $item->ip ?></td>
         <td><?php echo $item->fecha_ini ?></td>
         <td><?php echo $item->hora_ini ?></td>
         <td><?php echo $item->fecha_fin ?></td>
         <td><?php echo $item->hora_fin ?></td>
         <td><?php echo $item->duracion ?></td>
      </tr>
  <?php endforeach; ?>
</table>


  • Kumbiaphp 1.0 Beta2
<?php View::content(); ?>
<?php echo Form::open() ?>
<div align="center">
   <?php echo Calendar::text('fecha','size=10'); ?>
   <?php echo Form::submit('Ir', array('class' => 'boton'))?>
</div>
<?php echo Form::close() ?>
<table class='table_grilla'>
   <tr>
   <th>NONBRES</th>
   <th>APELLIDOS</th>
   <th>PERFIL</th>
   <th>IP</th>
   <th>FECHA INI</th>
   <th>HORA INI</th>
   <th>FECHA FIN</th>
   <th>HORA FIN</th>
   <th>DURACION</th>
</tr>
<?php foreach($items as $item): ?>
      <tr>
         <td><?php echo $item->nombres ?></td>
         <td><?php echo $item->apellidos ?></td>
         <td><?php echo $item->perfil ?></td>
         <td><?php echo $item->ip ?></td>
         <td><?php echo $item->fecha_ini ?></td>
         <td><?php echo $item->hora_ini ?></td>
         <td><?php echo $item->fecha_fin ?></td>
         <td><?php echo $item->hora_fin ?></td>
         <td><?php echo $item->duracion ?></td>
      </tr>
  <?php endforeach; ?>
</table>

Modelo

class Logs extends ActiveRecord{
    public function getLogs($fecha){
        Load::lib('date');
        $fecha = fentrada($fecha);
        $sql = "SELECT usuarios.id AS id,
                usuarios.nombres AS nombres,
                usuarios.apellidos AS apellidos,
                perfiles.nombre AS perfil,
                logs.ip AS ip,
                DATE_FORMAT(logs.fecha_at, '%d-%m-%Y') AS fecha_ini,
                DATE_FORMAT(logs.fecha_at, '%H:%i:%s') as hora_ini,
                DATE_FORMAT(logs.fecha_in, '%d-%m-%Y') AS fecha_fin,
                DATE_FORMAT(logs.fecha_in, '%H:%i:%s') as hora_fin,
                SUBTIME(DATE_FORMAT(logs.fecha_in, '%H:%i:%s'),
                DATE_FORMAT(logs.fecha_at, '%H:%i:%s')) as duracion from logs
                inner join usuarios on usuarios.id = logs.usuarios_id
                inner join perfiles on perfiles.id = usuarios.perfiles_id
                where
                logs.fecha_in > '$fecha 00:00' and logs.fecha_in < '$fecha 23:59'
                ";
        return $this->find_all_by_sql($sql);
    }
}