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

	//moduli
	$servizio['attach'] = array();
	$result = $GLOBALS['mysqli']->query("SELECT * FROM attachments WHERE anagr_id = ".$anagr_id);
	while($row=$result->fetch_assoc())
		$servizio['attach'][] = $row;
	
	return $servizio;
}
function get_servmen_attach($anagr_id=false){
	
	$attach = array();
	
	if($anagr_id)
		$w = "WHERE anagrafiche.anagr_id = ".$anagr_id." AND anagrafiche.ente_id = ".$_SESSION['user']['ente_id'];
		
	$result = $GLOBALS['mysqli']->query("
	SELECT attachments_name.*, attachments.att_fname, attachments.att_id FROM attachments_name 
	LEFT JOIN attachments ON attachments_name.attach_id = attachments.attach_id
	LEFT JOIN anagrafiche ON attachments.anagr_id = anagrafiche.anagr_id
	".$w." ORDER BY attachments_name.attach_ord");
	while($row = $result->fetch_assoc())
		$attach[$row['attach_id']] = $row;

	return $attach;
}
function get_presenze($classe,$data){
	$presenze = array();
	$result = $GLOBALS['mysqli']->query("
	SELECT serv_men_presenze.* FROM serv_men_presenze 
	LEFT JOIN serv_men ON serv_men_presenze.anagr_id = serv_men.anagr_id
	LEFT JOIN anagrafiche ON serv_men.anagr_id = anagrafiche.anagr_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_men_presenze.*,DATE_FORMAT(date,'%e') AS giorno FROM serv_men_presenze 
	LEFT JOIN serv_men ON serv_men_presenze.anagr_id = serv_men.anagr_id
	LEFT JOIN anagrafiche ON serv_men.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']] = $row['status'];
		
	return $presenze;
}
?>