IESA
Programa de Feedback Gerencial y Liderazgo
 Todo Estructuras de Datos Archivos Funciones Variables
Inventario.class.php
Ir a la documentación de este archivo.
00001 <?php
00002 
00003 class Inventario{
00004 
00005         protected $inv_id;
00006         protected $inv_nombre;
00007         protected $inv_descripcion;
00008         protected $inv_tipo;
00009 
00010         protected $inv_cuestionarios;
00011 
00012         protected $inv_rpt_introduccion;
00013 
00014         const TIPO_SIMPLE = 0;
00015         const TIPO_360 = 1;
00016         const TIPO_DEMOGRAFICO = 2;
00017 
00018         public function __construct(  ){  }
00019 
00025         private function limpiar(  ){
00026 
00027                 foreach( get_object_vars($this) as $name => $value )
00028                         switch( $name ){
00029                                 default: $this->{$name} = NULL; break;
00030                         }
00031 
00032         }
00033 
00045         public function getInventario( $data ){
00046 
00047                 $this->limpiar(  );
00048 
00049                 $db = Database::getInstance(  );
00050 
00051                 $query =
00052                         'SELECT inv_id, inv_nombre, inv_descripcion, inv_tipo, inv_rpt_introduccion FROM inventarios WHERE '.$data['where'];
00053 
00054                 $consulta = $db->prepare( $query );
00055                 $consulta->execute( $data['data'] );
00056 
00057                 $resultado = $consulta->fetch( PDO::FETCH_ASSOC );
00058                 unset( $consulta );
00059 
00060                 if( !$resultado ) return false;
00061 
00062                 foreach( $resultado as $name => $value ) $this->{$name} = $value;
00063 
00064                 return true;
00065         }
00066 
00079         public function getInventarioPorId( $id ){
00080 
00081                 return $this->getInventario( array(
00082                         'where'=>'inv_id = :id',
00083                         'data'=>array( ':id' => $id )
00084                 ));
00085 
00086         }
00087 
00097         public function getInventarioDemografico(  ){
00098 
00099                 $exist = $this->getInventario( array( 'where'=>'inv_tipo = :tipo', 'data'=>array(
00100                         ':tipo' => Inventario::TIPO_DEMOGRAFICO
00101                 )));
00102 
00103                 if( !$exist ){
00104                         $this->setNombre( utf8_encode('Inventario Demográfico') );
00105                         $this->setDescripcion( 'N/A' );
00106                         $this->setReporteIntroduccion( 'N/A' );
00107                         $this->setTipo( Inventario::TIPO_DEMOGRAFICO );
00108 
00109                         $this->guardar(  );
00110                         $exist = true;
00111                 }
00112 
00113                 return $exist;
00114 
00115         }
00116 
00129         public function getInventarioPorNombre( $nombre ){
00130 
00131                 return $this->getInventario( array(
00132                         'where'=>'inv_nombre = :nombre',
00133                         'data'=>array( ':nombre' => $nombre )
00134                 ));
00135 
00136         }
00137 
00151         public function eliminar(  ){
00152 
00153                 if( !$this->getId(  ) ) return 0;
00154 
00155                 $db = Database::getInstance();
00156                 $rt = $db->exec( 'DELETE FROM inventarios WHERE inv_id = '.$this->getId(  ) );
00157 
00158                 $this->limpiar(  );
00159                 return $rt ? $rt : 0;
00160         }
00161 
00171         public function guardar(  ){
00172 
00173                 $db = Database::getInstance(  );
00174 
00175                 $data = array( 
00176                         ':inv_nombre'=>$this->inv_nombre,
00177                         ':inv_descripcion'=>$this->inv_descripcion,
00178                         ':inv_tipo'=>$this->inv_tipo,
00179                         ':inv_rpt_introduccion'=>$this->inv_rpt_introduccion
00180                 );
00181 
00182                 if( !$this->getId(  ) )
00183                         $query =
00184                                 'INSERT INTO inventarios ( '
00185                                 .'inv_nombre, inv_descripcion, inv_tipo, inv_rpt_introduccion '
00186                                 .')VALUES( :inv_nombre, :inv_descripcion, :inv_tipo, :inv_rpt_introduccion )';
00187                 else{
00188                         $data[':inv_id']=$this->inv_id;
00189                         $query = 'UPDATE inventarios SET '
00190                                 .'inv_nombre=:inv_nombre, inv_descripcion=:inv_descripcion, inv_tipo=:inv_tipo, '
00191                                 .'inv_rpt_introduccion=:inv_rpt_introduccion '
00192                                 .'WHERE inv_id = :inv_id';
00193                 }
00194 
00195                 $consulta = $db->prepare( $query );
00196                 $consulta->execute( $data );
00197                 if( !$this->getId(  ) ) $this->getInventarioPorId( Database::lastInsertId( 'inventarios_seq' ) );
00198 
00199                 $consulta->closeCursor(  ); unset( $consulta );
00200 
00201         }
00202 
00210         public function getId(  ){ return $this->inv_id ? $this->inv_id : NULL; }
00211 
00219         public function getNombre(  ){ return $this->inv_nombre; }
00220 
00226         public function setNombre( $val ){ $this->inv_nombre = $val; }
00227 
00235         public function getTipo(  ){ return $this->inv_tipo; }
00236 
00245         public function setTipo( $val ){ $this->inv_tipo = $val; }
00246         
00254         public function getDescripcion(  ){ return $this->inv_descripcion; }
00255 
00256 
00264         public function getReporteIntroduccion(  ){ return $this->inv_rpt_introduccion; }
00265 
00273         public function setReporteIntroduccion( $val ){ $this->inv_rpt_introduccion = $val; }
00274 
00280         public function setDescripcion( $val ){ $this->inv_descripcion = $val; }
00281 
00289         public function getCuestionarios(  ){
00290 
00291                 if( $this->inv_cuestionarios != NULL ) return $this->inv_cuestionarios;
00292 
00293                 $db = Database::getInstance(  );
00294 
00295                 $query = 'SELECT * FROM cuestionarios WHERE cue_inv_id = :inv_id AND cue_origen_id = :cue_origen_id ORDER BY cue_id ASC';
00296 
00297                 $ori_cue = array(  );
00298                 $result = array(  );
00299 
00300                 $consulta = $db->prepare( $query );
00301                 $consulta->execute( array( ':inv_id'=>$this->getId(  ), ':cue_origen_id'=>0 ) );
00302                 while( $resultado = $consulta->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT ) ){
00303                         $cue = new stdClass(  );
00304                         $cue->Id=$resultado['cue_id'];
00305                         $cue->Nombre=$resultado['cue_nombre'];
00306                         $cue->Tipo=$resultado['cue_tipo'];
00307                         $cue->Origen=$resultado['cue_origen_id'];
00308                         array_push( $ori_cue, $cue );
00309                 }
00310 
00311                 foreach( $ori_cue as $k => $cue ){
00312                         array_push($result, $cue);
00313                         $consulta->execute( array( ':inv_id'=>$this->getId(  ), ':cue_origen_id'=>$cue->Id ) );
00314                         while( $resultado = $consulta->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT ) ){
00315                                 $subcue = new stdClass(  );
00316                                 $subcue->Id=$resultado['cue_id'];
00317                                 $subcue->Nombre=$resultado['cue_nombre'];
00318                                 $subcue->Tipo=$resultado['cue_tipo'];
00319                                 $subcue->Origen=$resultado['cue_origen_id'];
00320                                 array_push($result, $subcue);
00321                         }
00322                 }
00323                 $consulta->closeCursor(  );
00324                 unset( $consulta, $resultado );
00325 
00326                 $this->inv_cuestionarios = $result;
00327                 return $this->inv_cuestionarios;
00328 
00329         }
00330 
00339         public function Asignado(  ){
00340                 if( !$this->getId(  ) ) return false;
00341 
00342                 $count = Database::count(
00343                         'SELECT count(*) FROM evaluaciones eva JOIN evaluaciones_inventarios eva_inv ON ( eva.eva_id = eva_inv.eva_id )'
00344                         .'WHERE eva_inv.inv_id = '.$this->getId(  )
00345                 );
00346 
00347                 return $count > 0 ? true : false ;
00348         }
00349 
00362         public function getEvaluacionesLista( $limit=10, $pg=1 ){
00363 
00364                 $rt = ( object ) array(
00365                                 'PageCount'=>1,
00366                                 'Pagina'=>1,
00367                                 'Total'=>0,
00368                                 'Evaluaciones'=>array(  )
00369                         );
00370 
00371                 if( !$this->getId(  ) ) return $rt;
00372 
00373                 $db = Database::getInstance(  );
00374 
00375                 $rt->Total =  Database::count(
00376                         'evaluaciones eva JOIN evaluaciones_inventarios eva_inv ON ( eva.eva_id = eva_inv.eva_id ) '
00377                         .'JOIN vw_actividades vw_act ON ( vw_act.dic_codigo = eva.eva_act_id ) WHERE eva_inv.inv_id = '. $this->getId(  )
00378                 );
00379 
00380                 if( $rt->Total == 0) return $rt;
00381 
00382                 $rt->PageCount = ceil( $rt->Total / $limit );
00383 
00384                 $query = Database::limit(
00385                         'SELECT '
00386                         .'eva.eva_id, eva.eva_nombre, eva.eva_fecha_inicio, eva.eva_fecha_fin, '
00387                                 .'vw_act.act_descriptor, vw_act.tact_descriptor, vw_act.loc_descriptor, vw_act.mat_descriptor, vw_act.docente '
00388                         .'FROM evaluaciones eva '
00389                         .'JOIN evaluaciones_inventarios eva_inv ON ( eva.eva_id = eva_inv.eva_id ) '
00390                         .'JOIN vw_actividades vw_act ON ( vw_act.dic_codigo = eva.eva_act_id ) '
00391                         .'WHERE eva_inv.inv_id = '.$this->getId(  ).' ORDER BY eva.eva_id DESC',
00392                         $limit,
00393                         $limit * ( ( $pg > $rt->PageCount ? $rt->PageCount : $pg ) -1 )
00394                 );
00395                 $consulta = $db->prepare( $query ); $consulta->execute( array(  ) );
00396 
00397                 while( $resultado = $consulta->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT ) ){
00398 
00399                         $FechaI = preg_split( '[-]', $resultado['eva_fecha_inicio'] ); $FechaI = $FechaI[2].'/'.$FechaI[1].'/'.$FechaI[0]; 
00400                         $FechaF = preg_split( '[-]', $resultado['eva_fecha_fin'] ); $FechaF = $FechaF[2].'/'.$FechaF[1].'/'.$FechaF[0]; 
00401 
00402                         array_push( $rt->Evaluaciones, ( object ) array(
00403                                 'Id'=>$resultado['eva_id'],
00404                                 'Nombre'=>$resultado['eva_nombre'],
00405                                 'FechaInicio'=>$FechaI,
00406                                 'FechaFin'=>$FechaF,
00407                                 'Actividad'=>( object ) array(
00408                                         'Nombre'=>$resultado['act_descriptor'],
00409                                         'Tipo'=>$resultado['tact_descriptor'],
00410                                         'Localidad'=>$resultado['loc_descriptor'],
00411                                         'Materia'=>$resultado['mat_descriptor'],
00412                                         'Docente'=>$resultado['docente'],
00413                                 )
00414                         ) );
00415                 }
00416 
00417                 $consulta->closeCursor(  ); unset( $consulta );
00418 
00419                 $rt->Pagina = $pg > $rt->PageCount ? $rt->PageCount : $pg ;
00420 
00421                 return $rt;
00422 
00423         }
00424 
00425 
00437         public function Estructura( $force_encode = false ){
00438 
00439                 if( !$this->getId(  ) ) return '';
00440 
00441                 $Inventario = (object)array(
00442                         'Id' => $this->getId(  ),
00443                         'Tipo'=> $this->getTipo(  ),
00444                         'Nombre' => $force_encode ? utf8_encode( $this->getNombre(  ) ) : $this->getNombre(  ),
00445                         'Descripcion' => $force_encode ? utf8_encode( $this->getDescripcion(  ) ) : $this->getDescripcion(  ),
00446                         'ReporteIntroduccion' =>
00447                                 $force_encode ? utf8_encode( $this->getReporteIntroduccion(  ) ) : $this->getReporteIntroduccion(  ),
00448                         'Cuestionarios' => array(  )
00449                 );
00450 
00451                 $db = Database::getInstance(  );
00452                 $consulta = $db->prepare( 'SELECT cue_id FROM cuestionarios WHERE cue_inv_id = :inv_id ' );
00453                 $consulta->execute( array( 'inv_id'=>$this->getId(  ) ) );
00454                 $cue_list = $consulta->fetchall(  );
00455                 $consulta->CloseCursor(  ); unset( $consulta );
00456 
00457                 $Cuestionario = NULL;
00458                 foreach( $cue_list as $i => $Cue ){
00459                         $Cuestionario = new Cuestionario(  ); $Cuestionario->getCuestionarioPorId( $Cue['cue_id'] );
00460                         array_push( $Inventario->Cuestionarios, $Cuestionario->Estructura( $force_encode ) );
00461                 }
00462 
00463                 return $Inventario;
00464         }
00465 
00479         public function Importar( $inv ){
00480 
00481                 if( $this->getId(  ) )
00482                         throw new Exception('No se puede importar un estructura, si el objeto ya tiene cargado un inventario',101);
00483 
00484                 $this->setTipo( $inv->Tipo );
00485                 $this->setNombre( $inv->Nombre );
00486                 $this->setDescripcion( $inv->Descripcion );
00487                 $this->setReporteIntroduccion( $inv->ReporteIntroduccion );
00488 
00489                 $this->guardar(  );
00490 
00491                 $cue_match = array(  );
00492                 $Cuestionario = NULL;
00493                 foreach( $inv->Cuestionarios as $i => $Cue ){
00494                         if( $Cue->Origen == 0  ){
00495                                 $Cuestionario = new Cuestionario(  );
00496                                 $Cuestionario->setNombre( $Cue->Nombre );
00497                                 $Cuestionario->setTipo( $Cue->Tipo );
00498 
00499                                 $Cuestionario->setTipoDeGrafico( $Cue->TipoGrafico );
00500                                 $Cuestionario->setTipoDeEstadistica( $Cue->Estadistico );
00501                                 $Cuestionario->setAnalisis( $Cue->AnalisisGrafico );
00502                                 $Cuestionario->setRangosDelGrafico( $Cue->Rangos );
00503 
00504                                 $Cuestionario->setEscalaMinimo( $Cue->Escala->Minimo );
00505                                 $Cuestionario->setEscalaMaximo( $Cue->Escala->Maximo );
00506 
00507                                 $Cuestionario->setInstrucciones( $Cue->Instrucciones );
00508                                 $Cuestionario->setInventarioId( $this->getId(  ) );
00509 
00510                                 $Cuestionario->setDimensiones( $Cue->Dimensiones );
00511                                 $Cuestionario->setPreguntas( $Cue->Preguntas );
00512 
00513                                 $Cuestionario->guardar(  );
00514 
00515                                 $cue_match[ $Cue->Id ] = $Cuestionario->getId(  );
00516                         }else{
00517                                 $Cuestionario = new Cuestionario(  );
00518                                 $Cuestionario->setNombre( $Cue->Nombre );
00519                                 $Cuestionario->setTipo( $Cue->Tipo );
00520 
00521                                 $Cuestionario->setCuestionarioOrigen( $cue_match[ $Cue->Origen ] );
00522 
00523                                 $Cuestionario->setTipoDeGrafico( $Cue->TipoGrafico );
00524                                 $Cuestionario->setTipoDeEstadistica( $Cue->Estadistico );
00525                                 $Cuestionario->setAnalisis( $Cue->AnalisisGrafico );
00526                                 $Cuestionario->setRangosDelGrafico( $Cue->Rangos );
00527 
00528                                 $Cuestionario->setEscalaMinimo( $Cue->Escala->Minimo );
00529                                 $Cuestionario->setEscalaMaximo( $Cue->Escala->Maximo );
00530 
00531                                 $Cuestionario->setInstrucciones( $Cue->Instrucciones );
00532                                 $Cuestionario->setInventarioId( $this->getId(  ) );
00533 
00534                                 $Cuestionario->setDimensiones( $Cue->Dimensiones );
00535                                 $Cuestionario->setPreguntas( $Cue->Preguntas );
00536 
00537                                 $Cuestionario->guardar(  );
00538 
00539                         }
00540                 }
00541 
00542         }
00543 
00544 }