<?php function distribute_timeavail_into_calendar($timeavails,$mb_id,$ta_cs_id_map,$csmap,$timezone_bias_seconds) { $ta_intervals=array(); foreach ($timeavails as $ta) { array_push($ta_intervals, array( 'begin_local'=>datetime_to_seconds($ta['ta_begin'],-$timezone_bias_seconds), 'end_local'=>datetime_to_seconds($ta['ta_end'],-$timezone_bias_seconds), 'record'=>$ta ) ); } $monthtable=array(); foreach ($ta_intervals as $tai) { $sec_ta_begin=$tai['begin_local']; $sec_ta_end=$tai['end_local']; $dp_ta_begin=split_datetime(seconds_to_datetime($sec_ta_begin)); $dp_ta_end=split_datetime(seconds_to_datetime($sec_ta_end)); unset($dp_ta_begin['hour'],$dp_ta_begin['minute'],$dp_ta_begin['second']); unset($dp_ta_end['hour'],$dp_ta_end['minute'],$dp_ta_end['second']); $dp_ta_begin=array_key_prepend($dp_ta_begin,'since_'); $dp_ta_end=array_key_prepend($dp_ta_end,'expire_'); $sec_begin_day_since=seconds_set_time_in_day($sec_ta_begin,0); $sec_end_day_since=seconds_set_time_in_day($sec_ta_end,0); $sec_curday_since=seconds_set_time_in_day($sec_ta_begin,0); while ($sec_curday_since<$sec_ta_end) { $sec_curday_expire=$sec_curday_since+24*3600; $sec_interval_since=$sec_curday_since>$sec_ta_begin?$sec_curday_since:$sec_ta_begin; $sec_interval_expire=$sec_curday_expire<$sec_ta_end?$sec_curday_expire:$sec_ta_end; $date_cur=seconds_to_datetime($sec_curday_since); $dcparts=split_datetime($date_cur); $dayi=0+$dcparts['day']; $monthkey=$dcparts['year'].'-'.$dcparts['month']; if (!isset($monthtable[$monthkey])) $monthtable[$monthkey]=month_calendar($date_cur,$timezone_bias_seconds); $dayset=&$monthtable[$monthkey]['day2weekday'][$dayi]; if (!isset($dayset['INTERVAL'])) $dayset['INTERVAL']=array(); $interval=$tai['record']; $interval+=$dp_ta_begin; $interval+=$dp_ta_end; $interval+=array_key_prepend(split_datetime(seconds_to_datetime($sec_interval_since)),'since_'); $interval+=array_key_prepend(split_datetime(seconds_to_datetime($sec_interval_expire)),'expire_'); if ($sec_begin_day_since!=$sec_curday_since) $interval['SINCEDAYDIFFER']=array(0); if ($sec_ta_end-$sec_curday_since>24*3600) $interval['EXPIREDAYDIFFER']=array(0); if ($sec_interval_expire-$sec_curday_since==24*3600) $interval['expire_hour']='24'; if ($interval['ta_status']=='VALID') $interval['STATUS-VALID']=array(0); if ($interval['ta_status']=='INVALID') $interval['STATUS-INVALID']=array(0); $interval['COURSE']=array(); if (is_array($ta_cs_id_map[$interval['ta_id']])) foreach ($ta_cs_id_map[$interval['ta_id']] as $cs_id) array_push($interval['COURSE'],$csmap[$cs_id]); array_push($dayset['INTERVAL'],$interval); unset($dayset); $timecount[$dayi]+=floor(($sec_interval_expire-$sec_interval_since)/60); $sec_curday_since=$sec_curday_expire; } } $daymap=&$month_calendar['day2weekday']; ksort($monthtable); foreach ($monthtable as $monthkey=>&$month_elem) { foreach ($month_elem['dataset']['WEEK'] as &$week) foreach ($week['DAY'] as &$day) { if (isset($day['INTERVAL'])) $day['HAS-INTERVAL']=array(0); else $day['NO-INTERVAL']=array(0); if ($monthkey==$day['yearmonth']) $day['IS-CUR-MONTH']=array(0); else { $day['NOT-CUR-MONTH']=array(0); if ($day['yearmonth']<$monthkey) $day['IS-PREV-MONTH']=array(0); else if ($day['yearmonth']>$monthkey) $day['IS-NEXT-MONTH']=array(0); } } } return array_values($monthtable); } function copy_timeavail($dbconn,$mb_id_from,$mb_id_tos,$ta_ids=array()) { if (!$mb_id_tos) return; if (!is_array($mb_id_tos)) $mb_id_tos=array($mb_id_tos); global $Config; if ($ta_ids) $timeavails=db_load_timeavails($dbconn,$ta_ids); else $timeavails=db_load_timeavail_of_compere($dbconn,$mb_id_from); $ta_ids=array_projection($timeavails,'ta_id'); $id2timeavail=rowset2map($timeavails,'ta_id'); $ta_cs_ids=db_load_timeavail_course_ids($dbconn,$ta_ids); $ta_id2cs_id=rowset2multimap($ta_cs_ids,'ta_id','cs_id'); foreach ($mb_id_tos as $mb_id_to) { if ($mb_id_to==$mb_id_from) continue; db_clear_timeavail($dbconn,$mb_id_to); foreach ($ta_ids as $ta_id) { $ta=$id2timeavail[$ta_id]; $ta['mb_id']=$mb_id_to; $ta_id_new=db_insert_timeavail($dbconn,$ta); db_save_timeavail_course($dbconn,$ta_id_new,$mb_id_to,$ta_id2cs_id[$ta_id]); } } } ?>