le 28/06/2013 à 02:25
Probleme calcul plage horaire
Je suis généreux aujourd'hui, je t'ai accordé une petite heure de mon temps. Oui, même pour un pro, ce genre de chose c'est long et assez pénible à faire. Voici donc une version un peu plus rationnelle :
<?php
/** Format and execute a SQL query
*/
function do_query($sql_fmt/*, args, ...*/)
{
$args = func_get_args();
array_shift($args);
$args = array_map($args, 'mysql_real_escape_string');
$sql = call_user_func_array('sprintf', $args);
$result = mysql_query($sql);
if(false === $result)
trigger_error(mysql_error());
return $result;
}
function sql_value_format($length)
{
return implode(', ', array_pad(array(), $length, '\'%s\''))
}
function do_query_insert($table_name, $value_fields, $expr_fields = array())
{
$fields = array_merge(array_keys($value_fields), array_keys($expr_fields));
$values_fmt = array();
$values_fmt[] = sql_value_format(count($value_fields));
$values_fmt[] = implode(', ', $expr_fields));
$values_fmt = implode(', ', $values_fmt);
$sql_fmt = sprintf('INSERT INTO %s (%s) values (%s)'
, $table_name
, implode(', ', $fields)
, $values_fmt
);
$result = do_query($sql_fmt, array_values($value_fields));
return mysql_insert_id();
}
//// TODO: put that wild code in a function
$infos = array();
$etdate = date('Y-m-d', date_offset(date_lundi($_SESSION['date']),$_POST['jour']-1));
$etdebut_new = "{$_POST['hh_deb']}:{$_POST['mm_deb']}:00";
$etduree = "{$_POST['hh_dur']}:{$_POST['mm_dur']}:00";
$etdebut_exist = $_SESSION['paperboard'][4]['et_debut'];
// result isn't a good var name, it should be renamed with semantic
$result = do_query('SELECT id_et FROM emptps
WHERE id_mat=\'%s\'
AND et_date=\'%s\'
AND et_debut=\'%s\'
AND id_gr=%s
AND id_form=%s
', $_POST['mat'], $etdate, $etdebut_new, $_POST['group'], $_SESSION['form']);
// Le code que j'ai ajouter //
echo "<br>";
// 15 minutes en dec
$temps = 25*3600/100;
// Heure de durée en dec
$etduree = $etduree*3600;
// Heure de début en dec
$etdebut = $etdebut_new*3600;
// Heure de fin en dec
$etfin = $etduree + $etdebut;
for($i = $etdebut; $i != $etfin; $i += $temps)
{
$heure = date('H:i:s', $i);
echo'<option value="'.$heure.'">'.$heure.'</option>';
$result2 = do_query('SELECT et_debut FROM emptps
WHERE et_date=\'%s\'
AND et_debut=\'%s\'
', $etdate, $etdebut_exist);
if($result && mysql_num_rows($result) != 0)
{
$infos[] = "--INSERTION NON EFFECTUEE--\\n La plage horaire a déjà été utilisée!";
}
}
// Le code que j'ai ajouter //
if($result && mysql_num_rows($result) != 0)
{
$infos[] = "--INSERTION NON EFFECTUEE--\\n Cette intervention existe déja!";
}
elseif($_POST['hh_dur'] == 0 && $_POST['mm_dur'] == 0)
{
$infos[] = "--INSERTION NON EFFECTUEE--\\n La durée ne peut etre nulle!";
}
else
{
$es = $_POST['attention'] == 'on' ? 'S' : 'A';
$et = $_POST['devoir'] ? $_POST['devoir'] : 'N';
$ecpt = $_POST['et_cpt'] == 'on' ? 'P' : 'N';
$paus = $_POST['pause'] == 'on' ? 'Y' : 'N';
$video = $_POST['video'] == 'on' ? 'Y' : 'N';
//Vérification (de quoi ?)
$chevauchement = "( ( et_debut < sec_to_time( time_to_sec('{$_POST['hh_deb']}:{$_POST['mm_deb']}:00')";
. "+time_to_sec('{$_POST['hh_dur']}:{$_POST['mm_dur']}:00'))";
. "AND sec_to_time(time_to_sec('{$_POST['hh_deb']}:{$_POST['mm_deb']}:00')";
. "+time_to_sec('{$_POST['hh_dur']}:{$_POST['mm_dur']}:00'))";
. " <= sec_to_time(time_to_sec(et_debut)+time_to_sec(et_duree)))"; // fin au milieu
. "OR ( et_debut >= '{$_POST['hh_deb']}:{$_POST['mm_deb']}:00'";
. "AND sec_to_time( time_to_sec('{$_POST['hh_deb']}:{$_POST['mm_deb']}:00')";
. "+time_to_sec('{$_POST['hh_dur']}:{$_POST['mm_dur']}:00'))";
. " >= sec_to_time(time_to_sec(et_debut)+time_to_sec(et_duree)))"; //debut avant et fin apres
. "OR ( et_debut <= '{$_POST['hh_deb']}:{$_POST['mm_deb']}:00'";
. "AND '{$_POST['hh_deb']}:{$_POST['mm_deb']}:00'";
. " < (sec_to_time(time_to_sec(et_debut)+time_to_sec(et_duree))))) "; // debut au milieu
$infos[] = verif_salle(0,$_POST['salle'],$etdate,$chevauchement);
$infos[] = verif_ens(0,$_POST['prof'],$etdate,$chevauchement);
$infos[] = verif_gr(0,$_POST['group'],$etdate,$chevauchement);
// Il faut une pause a 10h et a 16h si pause = Y dans formation et TP ala modif on fera ce qu'on veut
if ($paus=='N')
{
$sql="SELECT pause FROM formation WHERE id_form={$_SESSION['form']}";
$result=mysql_query($sql) or $debug.=$sql;
if($result&&($row=mysql_fetch_array($result))&&($row['pause']=='Y')&&
($_POST['et_ens']=="TP")&&
(
(($_POST['hh_deb']*60+$_POST['mm_deb']<600)&&(($_POST['hh_deb']+$_POST['hh_dur'])*60+$_POST['mm_deb']+$_POST['mm_dur']>600))||
(($_POST['hh_deb']*60+$_POST['mm_deb']<960)&&(($_POST['hh_deb']+$_POST['hh_dur'])*60+$_POST['mm_deb']+$_POST['mm_dur']>960))
)
)
$paus='Y';
}
if (!isset($_POST['et_ens']))
$_POST['et_ens'] = 'TD';
//recup du id_form dans groupe pour mutualisation
$result = do_query('SELECT id_form FROM groupe WHERE id_gr=%s'
, $_POST['group']);
$row = mysql_fetch_array($result);
$form = $row['id_form'];
//INSERTION même si il y a des conflits
$field_values = array
( 'id_mat' => $_POST['mat']
, 'id_sem' => $_SESSION['sem']
, 'id_gr' => $_POST['group']
, 'id_prof' => $_POST['prof']
, 'id_salle' => $_POST['salle']
, 'et_type' => $et
, 'et_ens' => $_POST['et_ens']
, 'et_status' => $es
, 'et_cpt' => $ecpt
, 'et_date' => $etdate
, 'et_debut' => sprintf('%s:%s:00', $_POST['hh_deb'], $_POST['mm_deb'])
, 'et_duree' => sprintf('%s:%s:00', $_POST['hh_dur'], $_POST['mm_dur'])
, 'id_form' => $form
, 'pause' => $paus
, 'divers' => $_POST['divers']
, 'video' => $video;
);
$expr_values = array
( 'date_enreg'
);
$_SESSION['id_et'] = do_query_insert('emptps', $field_values, $expr_fields);
if($video == 'Y')
$infos[] = verif_video(0, $_POST['salle'], $etdate, $chevauchement);
if(!empty($infos))
$infos = "Anomalie(s) détectée(s):<br>" . implode("<br>\n", $infos);
}