<?php
function get_servpps($anagr_id){
	$anagr_id = intval($anagr_id);
	$result = $GLOBALS['mysqli']->query("SELECT * FROM serv_pps WHERE anagr_id = ".$anagr_id);
	if($result->num_rows == 0)
		return false;
	else
		$servizio = $result->fetch_assoc();

	//delegati ritiro 
	$servizio['delegati'] = array();
	$result = $GLOBALS['mysqli']->query("SELECT deleg_id, CONCAT_WS(' ', deleg_cognome, deleg_nome) AS nome FROM servizi_delegati WHERE serv_cod = 'pps' AND anagr_id = ".$anagr_id);
	while($row=$result->fetch_assoc())
		$servizio['delegati'][$row['deleg_id']] = $row['nome'];
	
	return $servizio;
}
function get_presenze($classe,$data){
	$presenze = array();
	$result = $GLOBALS['mysqli']->query("
	SELECT serv_pps_presenze.*, user_cell FROM serv_pps_presenze 
	LEFT JOIN serv_pps ON serv_pps_presenze.anagr_id = serv_pps.anagr_id

	LEFT JOIN anagrafiche ON serv_pps.anagr_id = anagrafiche.anagr_id	
	LEFT JOIN users ON anagrafiche.user_id = users.user_id	

	WHERE date = '".$data."' AND classe_id = ".$classe." AND anagrafiche.ente_id=".$_SESSION['user']['ente_id']);
	while($row = $result->fetch_assoc())
		$presenze[$row['anagr_id']] = $row;
	
	return $presenze;
}
function get_presenze_all($scuola_id,$classe_id,$anno,$mese){
	$presenze = array();
	
	$scuola_id = intval($scuola_id);
	$classe_id = intval($classe_id);
	$anno = intval($anno);
	$mese = intval($mese);
	
	if($scuola_id > 0)
		$w .= " AND scuola_id = ".$scuola_id;
	if($classe_id > 0)
		$w .= " AND classe_id = ".$classe_id;
	
	$result = $GLOBALS['mysqli']->query("
	SELECT serv_pps_presenze.*,DATE_FORMAT(date,'%e') AS giorno FROM serv_pps_presenze 
	LEFT JOIN serv_pps ON serv_pps_presenze.anagr_id = serv_pps.anagr_id
	LEFT JOIN anagrafiche ON serv_pps.anagr_id = anagrafiche.anagr_id
	WHERE anagrafiche.ente_id=".$_SESSION['user']['ente_id']." AND DATE_FORMAT(date,'%Y-%c') = '".intval($anno)."-".intval($mese)."' ".$w);
	while($row = $result->fetch_assoc())
		$presenze[$row['anagr_id']][$row['giorno']] = array('pre'=>$row['pre'],'post'=>$row['post']);
	return $presenze;
}

function get_delegati_all(){
	$result = $GLOBALS['mysqli']->query("
	SELECT servizi_delegati.anagr_id, CONCAT_WS(' ',deleg_cognome,deleg_nome) AS anagrafica FROM servizi_delegati 
	LEFT JOIN serv_pps ON servizi_delegati.anagr_id = serv_pps.anagr_id
	LEFT JOIN anagrafiche ON serv_pps.anagr_id = anagrafiche.anagr_id
	WHERE anagrafiche.ente_id=".$_SESSION['user']['ente_id']." AND servizi_delegati.serv_cod = 'pps'");
	while($row = $result->fetch_assoc())
		$delegati[$row['anagr_id']][] = $row['anagrafica'];
	return $delegati;
}

function print_delegati_item($arr){
	if(!is_array($arr)){
		$li = '<li role="presentation" class="dropdown-header">Nessun delegato al recupero</li>';
	}else{

	  $li = '<li role="presentation" class="dropdown-header">Delegati al recupero</li>';

		foreach($arr as $v)
			$li .= '<li role="presentation"><a role="menuitem" tabindex="-1">'.entities(welltxt($v)).'</a></li>';
	}
	
	return $li;
}
?>