<?php
require_once("DBManager.class.php");

class mp_chat{
	private $idprivado;///
	private $name_r;
	private $name_e;
	private $message;
	private $timestamp;
	private $iddoc_curso;
	
	private $status;
	/////////////////constructor/////////////////
	
	public function Crear($name_r,$name_e,$message,$iddoc_curso,$status)
	{
		$resp=false;		
		$db= new DBManager();		
if($db->_execute("INSERT INTO mp_chat(idprivado,name_r,name_e,message,iddoc_curso,status) VALUES(null,'".$name_r."','".$name_e."','".$message."',".$iddoc_curso.",".$status.")"))   
		{
			$resp=true;
		}
		return $resp;		
	}
	
	
	public function ListarPorDoccur($v,$nr,$ne)
	{
		$vector=null;		
		$db= new DBManager();		
		$rs=$db->execute("select * from mp_chat where(iddoc_curso =".$v." and (name_r='".$nr."' and name_e='".$ne."') or (name_e='".$nr."' and name_r='".$ne."')) order by idprivado desc");
		$num_rows=$rs->getNumOfRows();
		if($num_rows>0)
		{	$row=0;
			$rs->firstRow(); // opcional, pero recomendado
			while (!$rs->EOF) 
			{
   				$vector[$row]=array("idprivado"	=> $rs->fields["idprivado"],
									"name_r" 	=> $rs->fields["name_r"],
									"name_e" 	=> $rs->fields["name_e"],
									"message" 	=> $rs->fields["message"],
									"timestamp" 	=> $rs->fields["timestamp"],
									"iddoc_curso" 	=> $rs->fields["iddoc_curso"],
									"status" 	=> $rs->fields["status"]);
                $row++;
				$rs->nextRow(); // Nota: nextRow() Esta situado al final				
         	}			
        	return $vector;			
		}else return null;
		$rs->close(); 
		$db->closeConnection();//opcional cierra el enlace de la Base de Datos              						
	}
	
	public function Ultimaactividad($v,$nr,$ne)
	{
		$vector=null;		
		$db= new DBManager();		
		$rs=$db->execute("select * from mp_chat where(iddoc_curso =".$v."  and name_e='".$ne."' and name_r='".$nr."') order by idprivado desc limit 1");
		$num_rows=$rs->getNumOfRows();
		if($num_rows>0)
		{	$row=0;
			$rs->firstRow(); // opcional, pero recomendado
			while (!$rs->EOF) 
			{
   				$vector[$row]=array("idprivado"	=> $rs->fields["idprivado"],
									"name_r" 	=> $rs->fields["name_r"],
									"name_e" 	=> $rs->fields["name_e"],
									"message" 	=> $rs->fields["message"],
									"timestamp" 	=> $rs->fields["timestamp"],
									"iddoc_curso" 	=> $rs->fields["iddoc_curso"],
									"status" 	=> $rs->fields["status"]);
                $row++;
				$rs->nextRow(); // Nota: nextRow() Esta situado al final				
         	}			
        	return $vector;			
		}else return null;
		$rs->close(); 
		$db->closeConnection();//opcional cierra el enlace de la Base de Datos              						
	}
		
			
	public function BuscarPorCodigo($vcod)
	{
		
		$resp=false;
		$num_rows=0;//numero de filas afectada por la consulta
		$db= new DBManager();
		$rs=$db->execute("select * from mp_chat where(idprivado = ".$vcod.")");
		$num_rows=$rs->getNumOfRows();
		if($num_rows>0)
		{	$resp=true;
			$rs->firstRow(); // opcional, pero recomendado
			while (!$rs->EOF) {
                // Coleccion de campos accesible mediante arrays asociativos también
	            $this->idprivado=$rs->fields["idprivado"];
				$this->name_r=$rs->fields["name_r"];
				$this->name_e=$rs->fields["name_e"];				
				$this->message=$rs->fields["message"];	
				$this->timestamp=$rs->fields["timestamp"];
				$this->iddoc_curso=$rs->fields["iddoc_curso"];
				$this->status=$rs->fields["status"];
 				$rs->nextRow(); // Nota: nextRow() Esta situado al final				
        	}			
		}
		$rs->close(); 
		$db->closeConnection();//opcional cierra el enlace de la Base de Datos              
		return $resp;							
	}
	
	public function ActualizarStatus($nr,$ne)			
	{
		$resp=false;		
		$db= new DBManager();	
		if($db->_execute("update  mp_chat set status = '0' where (status='1' and name_e='".$ne."' and name_r='".$nr."') "))//si es exitosa la consulta			   
		{
		  $resp=true; 
		}	
		return $resp;
	}
	
	//////////////////////////////
	public function Eliminar($vcod)
	{
		$resp=false;		
		$db= new DBManager();		
		if($db->_execute("DELETE FROM mp_chat WHERE (idprivado=".$vcod.");"))   
		{
			$resp=true;
		}
		return $resp;		
	}	
	/////////////////////////////////////////							
	
	public function getIdprivado(){ return $this->idprivado;}
	public function getIddoc_curso(){ return $this->iddoc_curso;}
	public function getName_r(){ return $this->name_r;}
	public function getName_e(){ return $this->name_e;}
	public function getMessage(){ return $this->message;}
	public function getTimestamp(){ return $this->timestamp;}
	public function getStatus(){ return $this->status;}
}
?>