2011-05-19 08:46:58 +00:00
< ? php
/*
* Clase Oferta
*/
include_once ( " BD.php " );
include_once ( " Automata.php " );
include_once ( " ListaCandidatos.php " );
include_once ( " Candidato.php " );
class Oferta {
// Atributos:
/* Usuario que tiene activa la sesi<73> n. */
private $usuario ;
protected $campos = array ();
// Constructores:
function Oferta ( $oid , $usuario ) {
$this -> usuario = $usuario ;
$consulta = " SELECT oid FROM candidato_pedido WHERE oid = ' $oid ' " ;
$bd = new BD ();
$num = $bd -> numFilas ( $consulta );
if ( $num > 0 ) {
$this -> campos [ 'oid' ] = $oid ;
} else {
$error = " Oferta no encontrada. " ;
throw new Exception ( $error );
}
}
2011-05-30 09:15:00 +00:00
function eliminar () {
2011-06-01 18:24:45 +00:00
//Antes de nada quitamos el candidato asociado a la oferta para que se haga la l<> gica que corresponda
$idCand = $this -> getValor ( " candidato " );
if ( ! empty ( $idCand )) {
if ( ! $this -> quitarCandidato ()) {
return false ;
}
}
// eliminamos en la oferta de la BD
2011-05-30 09:15:00 +00:00
$consulta = " DELETE FROM candidato_pedido WHERE oid = " . $this -> campos [ 'oid' ];
$bd = new BD ();
if ( ! $bd -> execQuery ( $consulta )) {
return false ;
} else {
2011-06-01 18:24:45 +00:00
$mensaje = " Oferta eliminada " ;
$this -> actualizarHistorial ( $mensaje );
return true ;
2011-05-30 09:15:00 +00:00
}
}
2011-05-19 08:46:58 +00:00
function actualizarHistorial ( $mensaje ) {
$oid = $this -> getValor ( " oid " );
$usuario = $this -> usuario -> getValor ( " nombre " );
$consulta = " INSERT INTO historial_oferta (oid_h, fecha_h, persona_h, texto_h) VALUES(' $oid ', now(), ' $usuario ', ' $mensaje ') " ;
$bd = new BD ();
return $bd -> execQuery ( $consulta );
}
function getValor ( $nombre ) {
if ( array_key_exists ( $nombre , $this -> campos )) {
// El campo ya lo hab<61> amos recuperamos, lo mostramos
return $this -> campos [ $nombre ];
} else {
switch ( $nombre ) {
case " nombre_estado " :
$idEstado = $this -> getValor ( " estado " );
$idioma = $this -> usuario -> getValor ( " idioma " );
$consulta = " SELECT nombre FROM candidaturas_estados WHERE cod= \" " . $idEstado . " \" AND idioma= \" " . $idioma . " \" " ;
break ;
case " nombre_solicitud " :
$oidPedido = $this -> getValor ( " pedido " );
$consulta = " SELECT nombre FROM pedidos WHERE oid= \" " . $oidPedido . " \" " ;
break ;
case " nombre_candidato " :
$oidCandidato = $this -> getValor ( " candidato " );
$consulta = " SELECT concat(nombre, ' ', apellidos) FROM usuarios WHERE oid= \" " . $oidCandidato . " \" " ;
break ;
default : $consulta = " SELECT " . $nombre . " FROM candidato_pedido WHERE oid= \" " . $this -> campos [ 'oid' ] . " \" " ;
break ;
}
// Lo insertamos para nosotros
$bd = new BD ();
$valor = $bd -> getCampo ( $consulta );
// Lo insertamos para nosotros
$arrayAct = array ( $nombre => $valor );
$this -> campos = $this -> campos + $arrayAct ;
return $valor ;
}
}
/**
* Acceso a los campos del pedido .
*
* @ return los campos del pedido .
*/
function getCampos () {
return $this -> campos ;
}
function getSiguientes () {
$estado = $this -> getValor ( " estado " );
$idioma = $this -> usuario -> getValor ( " idioma " );
$rol = $this -> usuario -> getValor ( " rol " );
$a = new Automata ( " candidaturas " , $idioma , $rol );
$siguientes = $a -> getSiguientes ( $estado );
return $siguientes ;
}
function transita ( $destino , $argumentos ) {
$origen = $this -> getValor ( " estado " );
$idioma = $this -> usuario -> getValor ( " idioma " );
$rol = $this -> usuario -> getValor ( " rol " );
$a = new Automata ( " candidaturas " , $idioma , $rol );
$transita = $a -> getTransicion ( $origen , $destino );
if (( $transita == " " ) || ! ( $transita >= 0 )) {
return false ;
} else {
$res = $this -> ejecutaTransicion ( $transita , $argumentos );
if ( $res ) {
$this -> setAutomatico = true ;
$this -> setCampo ( " msgEstado " , $argumentos );
$this -> setCampo ( " estado " , $destino );
$this -> setAutomatico = false ;
}
return $res ;
}
}
private function ejecutaTransicion ( $codigo , $argumentos ) {
// Si no hace nada al transitar salimos sin m<> s.
if ( $codigo == 0 )
return true ;
$funcion = " ejecutar $codigo " ;
$res = call_user_func ( array ( " Oferta " , $funcion ), $argumentos );
return $res ;
}
/**
* En configuraci<EFBFBD> n -> Configurada
*/
private function ejecutar110120 () {
// Comprobamos que haya un candidato asignado.
$candidato = $this -> getValor ( " candidato " );
if ( ! empty ( $candidato )) {
return true ;
} else {
$error = " No se puede cambiar de estado porque no hay ning<6E> n candidato asignado a esta oferta " ;
throw new Exception ( $error );
return false ;
exit ;
}
}
function setCampo ( $nombre , $valor ) {
// PERMISOS:
/*
* Admin ( 1 ) - Todos
* Gestor ( 3 ) - Los suyos
* Otro - Excepci<EFBFBD> n
*/
$gerente = $this -> getValor ( " gerente " );
$sesion = $this -> usuario -> getValor ( " oid " );
if ( $this -> usuario -> tieneRol ( 1 ) || ( $this -> usuario -> tieneRol ( 3 ) && $sesion == $gerente ) || $this -> setAutomatico ) {
$viejo = $this -> getValor ( $nombre );
if ( $viejo != $valor ) {
// Comprobamos multivaluado y casos especiales antes del cambio:
$viejo = $this -> getValorMulti ( $nombre , $viejo );
$this -> campos [ $nombre ] = $valor ;
$oid = $this -> getValor ( " oid " );
$consulta = " UPDATE candidato_pedido SET $nombre = ' $valor ' WHERE oid=' $oid ' " ;
$bd = new BD ();
if ( $bd -> execQuery ( $consulta )) {
// Guardar en el historial
$valor = $this -> getValorMulti ( $nombre , $valor );
if ( $viejo == " " && $valor != " " ) {
$historial = " $nombre (de - a $valor ) " ;
} else {
$historial = " $nombre (de $viejo a $valor ) " ;
}
if ( $nombre = " estado " ) {
$this -> actualizarHistorial ( $historial );
}
}
}
} else {
$error = " El usuario no tiene permisos para editar la solicitud de oferta. " ;
throw new Exception ( $error );
}
}
private function getValorMulti ( $nombre , $antiguo ) {
switch ( $nombre ) {
/* case " procedencia " :
$valor = nombre_procedencia ( $antiguo );
break ;
case " estado " :
$valor = nombre_estado_pedido ( $antiguo );
break ;
case " localidad " :
$valor = nombre_localidad ( $antiguo );
break ;
case " perfil " :
$valor = nombre_perfil ( $antiguo );
break ;
case " cliente " :
$valor = nombre_cliente ( $antiguo );
break ; */
default :
$valor = $antiguo ;
break ;
}
return $valor ;
}
/**
2011-05-25 10:16:19 +00:00
* Devuelve una lista de candidatos disponibles para una oferta , que son todos menos los no disponibles #679 Peticion
2011-05-19 08:46:58 +00:00
*/
function getCandidatosDisponibles () {
2011-06-01 18:24:45 +00:00
return $this -> getCandidatos ( " 510,521,522,523,540,550,560 " );
2011-05-19 08:46:58 +00:00
}
/**
* Devuelve una lista de candidatos de un estado dado
*/
private function getCandidatos ( $estado ) {
$lista = new ListaCandidatos ( $this -> usuario , " " , " " , $estado );
return $lista ;
}
function getHistorial () {
$historial = " " ;
$oid = $this -> getValor ( " oid " );
$consulta = " SELECT * FROM historial_oferta WHERE oid_h=' $oid ' ORDER BY fecha_h DESC " ;
$bd = new BD ();
$resultado = $bd -> execQuery ( $consulta );
while ( $rows = mysql_fetch_array ( $resultado )) {
$fecha = $rows [ " fecha_h " ];
$persona = $rows [ " persona_h " ];
$texto = $rows [ " texto_h " ];
$historial .= " [ $fecha ] $persona - $texto\n " ;
}
return $historial ;
}
2011-06-01 18:24:45 +00:00
function getOfertasCandidato ( $oid ) {
$consulta = " SELECT * FROM candidato_pedido WHERE candidato=' $oid ' ORDER BY fecha DESC " ;
$bd = new BD ();
$resultado = $bd -> execQuery ( $consulta );
return $resultado ;
}
2011-05-19 08:46:58 +00:00
function quitarCandidato () {
$idCand = $this -> getValor ( " candidato " );
if ( ! empty ( $idCand )) {
$candidato = new Candidato ( $this -> usuario , $idCand );
$estadoCand = $candidato -> getValor ( " estado " );
2011-06-01 18:24:45 +00:00
$numOfertasCandidato = $this -> getOfertasCandidato ( $idCand );
2011-05-19 08:46:58 +00:00
switch ( $estadoCand ) {
2011-06-01 18:24:45 +00:00
//Disponible asignado exclusivo, Disponible asignado, No disponible.
//El estado del candidato no cambia si est<73> asociado a alguna otra oferta, sino pasar<61> a Disponible
2011-05-19 08:46:58 +00:00
case " 560 " :
2011-06-01 18:24:45 +00:00
case " 550 " :
case " 530 " :
if ( count ( $numOfertasCandidato ) <= 1 ) {
$candidato -> transita ( " 540 " , " " );
$nombre_candidato = $candidato -> getValor ( " nombre " ) . " " . $candidato -> getValor ( " apellidos " );
$mensaje = " El candidato " . $nombre_candidato . " pasa a estado Disponible " ;
$this -> actualizarHistorial ( $mensaje );
}
return true ;
break ;
//Pendiente de clasificar, Rechazados tipo1, tipo2, tipo3, Disponible.
//No podr<64> darse el caso ya que todo candidato asociado a una oferta debe estar Disponible asignado, Disponible asignado exclusivo, o no disponible
2011-05-19 08:46:58 +00:00
default :
$error = " [quitarCandidato]. El candidato tiene un estado no permitido (' " . $estadoCand . " ') " ;
throw new Exception ( $error );
}
}
}
2011-06-01 18:24:45 +00:00
function asignarCandidato ( $idCandidato ) {
2011-06-10 15:03:20 +00:00
/*
2011-05-19 08:46:58 +00:00
$idCand = $this -> getValor ( " candidato " );
if ( ! empty ( $idCand )) {
if ( ! $this -> quitarCandidato ()) {
return false ;
}
}
2011-06-10 15:03:20 +00:00
*/
2011-05-19 08:46:58 +00:00
$candidato = new Candidato ( $this -> usuario , $idCandidato );
$estadoCand = $candidato -> getValor ( " estado " );
2011-06-01 18:24:45 +00:00
$numOfertasCandidato = $this -> getOfertasCandidato ( $idCand );
2011-06-10 15:03:20 +00:00
2011-05-19 08:46:58 +00:00
switch ( $estadoCand ) {
2011-06-01 18:24:45 +00:00
//Disponible asignado, el estado del candidato no cambia, solo se asigna a la oferta y ya est<73> .
2011-06-10 15:03:20 +00:00
case 560 :
2011-06-01 18:24:45 +00:00
$this -> setCampo ( " candidato " , $idCandidato );
return true ;
break ;
//Pendiente de clasificar, Rechazados tipo1, tipo2, tipo3, Disponible. Todos los candidatos con alguno de estos estados cambiar<61> n a Disponible asignado (560)
2011-06-10 15:03:20 +00:00
case 510 : ;
case 521 : ;
case 522 : ;
case 523 : ;
case 540 :
2011-06-01 18:24:45 +00:00
$this -> setCampo ( " candidato " , $idCandidato );
if ( $candidato -> transita ( " 560 " , " " )){
$nombre_candidato = $candidato -> getValor ( " nombre " ) . " " . $candidato -> getValor ( " apellidos " );
$mensaje = " Asignado el candidato " . $nombre_candidato . " --> " . $candidato -> getValor ( " estado " );
2011-05-19 08:46:58 +00:00
$this -> actualizarHistorial ( $mensaje );
return true ;
2011-06-01 18:24:45 +00:00
}
else {
$error = " ERROR: El candidato no ha podido cambiar de estado " ;
throw new Exception ( $error );
}
break ;
//("550")Disponible asignado exclusivo,u otro que no exista
default :
$error = " El candidato tiene un estado no permitido (' " . $candidato -> getValor ( " estado " ) . " ') " ;
throw new Exception ( $error );
2011-05-19 08:46:58 +00:00
}
}
}
?>