<?php 
class eventclass_senuc_aluno_regional  extends eventsBase
{ 
	function eventclass_senuc_aluno_regional()
	{
	// fill list of events
		$this->events["BeforeProcessAdd"]=true;

		$this->events["BeforeAdd"]=true;

		$this->events["AfterDelete"]=true;

		$this->events["AfterAdd"]=true;

		$this->events["AfterEdit"]=true;

		$this->events["BeforeShowEdit"]=true;




//	onscreen events


	}
// Captchas functions	

//	handlers

		
		
				// Add page: Before process
function BeforeProcessAdd(&$conn,&$pageObject)
{

		 // Place event code here.
// Use "Add Action" button to add code snippets.
;		
} // function BeforeProcessAdd

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		

		
		
		
		
		
		
		
		
		
		
				// Before record added
function BeforeAdd(&$values,&$message,$inline,&$pageObject)
{

		$numero3=0;
include "conexao_mdl.php";

$strSQLExists5 = "select count(*) as n from mdl_user where username='".$values['email']."'";
$rsExists5 = mysql_query($strSQLExists5,$conexao2);
while($data5=mysql_fetch_array($rsExists5))
$numero3 =$data5["n"];
if($numero3==0){
//matricula moodle

// função para separar nome e sobrenome
$nomecompleto = $values['nome'];
$sobrenome = "";
$array_nome = explode ( " ",$nomecompleto);
$primeiro_nome = $array_nome[0];
for($i=1; $i < count($array_nome); $i++) {
$sobrenome.=" ".$array_nome[$i];

}

$id_nucleo = $_SESSION["idnucleo"];

$id_curso = $_SESSION['id_curso'];


	
$sql_chave2 = "SELECT id FROM mdl_context WHERE instanceid='".$id_curso."' AND contextlevel=50";
$busca_chave2 = mysql_query($sql_chave2, $conexao2);
 while($dados_chave2 = mysql_fetch_assoc($busca_chave2)){
$id_context =$dados_chave2['id'];}


//BUSCA CURSO
$sql9="INSERT INTO mdl_user (firstname,lastname,email,username,password,confirmed,description, mnethostid,lang) VALUES ('$primeiro_nome','$sobrenome','".$values['email']."','".$values['email']."',MD5('".$values['email']."'),1,'Aluno Núcleo',1, 'pt_br')";
$result9 = mysql_query($sql9, $conexao2)or die("Falha na execuçao da instruçao"); 
$id_user = mysql_insert_id();
$_SESSION['$id_user']=$id_user;

$sql10 = "INSERT INTO mdl_role_assignments (roleid,contextid,userid,timemodified,modifierid,sortorder,itemid) VALUES (10,'".$id_context."','".$id_user."',UNIX_TIMESTAMP(NOW()),'2','".$id_nucleo."','0')";
$result10 = mysql_query($sql10, $conexao2)or die("Falha na execuçao da instruçao");

$sql_chave = "SELECT id FROM mdl_enrol WHERE courseid='".$id_curso."' AND enrol='manual'";
$busca_chave = mysql_query($sql_chave, $conexao2);
 while($dados_chave = mysql_fetch_assoc($busca_chave)){
$chave =$dados_chave['id'];}

/////INSERE CHAVe///

$sql_insere_chave = "INSERT INTO mdl_user_enrolments (status,enrolid,userid,timestart,timeend,timecreated,timemodified,modifierid) VALUES ('0','".$chave."','".$id_user."',UNIX_TIMESTAMP(NOW()),'0',UNIX_TIMESTAMP(NOW()),UNIX_TIMESTAMP(NOW()),'2')";
mysql_query($sql_insere_chave, $conexao2);

   

 
return true;
}
else{
echo"<script type='text/javascript'>";

        echo "alert('USUÁRIO JÁ CADASTRADO NA PLATAFORMA MOODLE!');";

echo "</script>";
return false;
}
// Place event code here.
// Use "Add Action" button to add code snippets.


;		
} // function BeforeAdd

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		

		
		
		
		
		
		
		
		
		
		
		
		
		
				// After record deleted
function AfterDelete($where,&$deleted_values,&$message,&$pageObject)
{

		$numero3=0;
$conexao2 = mysql_connect("localhost","root","") or die ("Não foi possível conectar ao servidor");
mysql_set_charset("utf8",$conexao2);
//seleciona a base de dados
mysql_select_db("moodle", $conexao2)or die("Não foi possível selecionar a base de dados!");


$strSQLExists5 = "select count(*) as n from mdl_user where username='".$deleted_values['email']."'";
$rsExists5 = mysql_query($strSQLExists5,$conexao2);
while($data5=mysql_fetch_array($rsExists5))
$numero3 =$data5["n"];
if($numero3!=0){
//matricula moodle

	
//BUSCA CURSO
$sql9="DELETE from mdl_user where id='".$deleted_values['id_mdl']."'";
$result9 = mysql_query($sql9, $conexao2)or die("Falha na execuçao da instruçao 1"); 

$sql_insere_chave = "DELETE from mdl_user_enrolments where userid=".$deleted_values['id_mdl'];
mysql_query($sql_insere_chave, $conexao2);

$sql10 = "DELETE from mdl_role_assignments where userid=".$deleted_values['id_mdl'];
$result10 = mysql_query($sql10, $conexao2)or die("Falha na execuçao da instruçao 2");



 
return true;
}
return true;


// Place event code here.
// Use "Add Action" button to add code snippets.
;		
} // function AfterDelete

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		

		
		
		
		
		
		
		
		
		
		
		
				// After record added
function AfterAdd(&$values,&$keys,$inline,&$pageObject)
{

		global $dal;
$tblUsers = $dal->Table("senuc_aluno");
$tblUsers->Param["id"]=$values['id'];
$tblUsers->Value["id_mdl"]=$_SESSION['$id_user'];
$tblUsers->Value["idregional"]=$_SESSION["idregional"] ;
$tblUsers->Update();



// Place event code here.
// Use "Add Action" button to add code snippets.
;		
} // function AfterAdd

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
				// After record updated
function AfterEdit(&$values,$where,&$oldvalues,&$keys,$inline,&$pageObject)
{

		if($values['cidade']==""||$values['cidade']==0){

global $dal;
$tblUsers = $dal->Table("senuc_aluno");
$tblUsers->Param["id"]=$oldvalues['id'];
$tblUsers->Value["cidade"]=$oldvalues['cidade'];
$tblUsers->Update();
}




if($oldvalues['ativo']!=$values['ativo']){
include "conexao_mdl.php";
if($values['ativo']==0){

$sql9="UPDATE mdl_user  set suspended=1 where id=".$values['id_mdl'];
$result9 = mysql_query($sql9, $conexao2)or die("Falha na execuçao da instruçao"); 


}
else{

$sql9="UPDATE mdl_user  set suspended=0 where id=".$values['id_mdl'];
$result9 = mysql_query($sql9, $conexao2)or die("Falha na execuçao da instruçao"); 


}


















}

// Place event code here.
// Use "Add Action" button to add code snippets.
;		
} // function AfterEdit

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
				// Before display
function BeforeShowEdit(&$xt,&$templatefile,$values,&$pageObject)
{

		global $dal;
$tblOrders = $dal->Table("senuc_aluno");
$rs = $tblOrders->Query("id=".$values['id'],"");
$data = db_fetch_array($rs);
//$values['cidade'] =$data['cidade'];
 $id_cidade=$data['cidade'];
$tblOrders = $dal->Table("tb_cidades");
$rs = $tblOrders->Query("id=".$id_cidade,"");
$data = db_fetch_array($rs);
//$values['cidade'] =$data['cidade'];
 $cidade=$data['nome'];
$pageObject->setProxyValue("cidade", $cidade);


// Place event code here.
// Use "Add Action" button to add code snippets.
;		
} // function BeforeShowEdit

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
//	onscreen events

} 
?>
