conf = &$CONF; $this->lng = &$LNG; $this->sql_tables = array( 'groups' => 'grupo_horario', 'teachers' => 'profesor_horario', 'candidates' => 'candidato_horario' ); $SQL_query = db_query('SELECT id, TIME_FORMAT(hora, "%H:%i") AS hora FROM ref_horas ORDER BY hora ASC'); if ( db_num_rows($SQL_query) > 0 ) { $this->hours = array(); while ( $SQL_values = db_fetch_array($SQL_query) ) $this->hours[$SQL_values['id']] = $SQL_values['hora']; } } function getHourIntervals( $halfHours = 0 ) { if ( $halfHours == 1 ) { $SQL_query = mysql_query('SELECT id, TIME_FORMAT(hora, "%H:%i") AS hora FROM ref_horas WHERE ( id %2 ) > 0 ORDER BY hora ASC'); if ( mysql_num_rows($SQL_query) > 0 ) { $this->hours = array(); while ( $SQL_values = db_fetch_array($SQL_query) ) $this->hours[$SQL_values['id']] = $SQL_values['hora']; } } if ( is_array($this->intervals) ) return $this->intervals; elseif ( is_array($this->hours) ) { $this->intervals = array(); $prevId = ''; while ( list($id, $hour) = each($this->hours) ) { if ( !empty($prevId) ) { $this->intervals[$prevId]['start'] = $this->hours[$prevId]; $this->intervals[$prevId]['end'] = $this->hours[$id]; $prevId = $id; } else $prevId = $id; } return $this->intervals; } } function getRecordValues( $section, $id, $texts = true ) { if ( isset($this->sql_tables[$section]) ) { $SQL_query = db_query('SELECT dia_inicio, dia_fin, hora_inicio, hora_fin, CONCAT(dia_inicio, \'-\', dia_fin, \'-\', hora_inicio, \'-\', hora_fin) AS time_record FROM ' . $this->sql_tables[$section] . ' WHERE id = \'' . $id . '\' ORDER BY dia_inicio, dia_fin, hora_inicio, hora_fin'); if ( db_num_rows($SQL_query) > 0 ) { $timetable = array(); while ( $SQL_values = db_fetch_array($SQL_query) ) { if ( $texts ) $timetable['timetable'][$SQL_values['time_record']] = $this->lng['select_day_' . $SQL_values['dia_inicio']] . ( $SQL_values['dia_fin'] > $SQL_values['dia_inicio'] ? ' - ' . $this->lng['select_day_' . $SQL_values['dia_fin']] : '' ) . ' [' . $this->hours[$SQL_values['hora_inicio']] . ' - ' . $this->hours[$SQL_values['hora_fin']] . ']'; else $timetable[] = $SQL_values['time_record']; } return $timetable; } return false; } else return false; } function manageRecord($params, $section, $record) { $error = ''; $error .= ( !isset($this->sql_tables[$section]) ? 1 : 0 ); $error .= ( empty($record) ? 1 : 0 ); $record_values = $this->getRecordValues($section, $record, false); $old_values = array(); $new_values = array(); $_post_values = array(); if ( !is_array($record_values) ) $record_values = array(); // Comprobacion de que existe cada ID for ( $i=0; $ihours[$regs[3]]) || !isset($this->hours[$regs[4]]) ) break; if ( $regs[1] > $regs[2] ) break; if ( $regs[3] >= $regs[4] ) break; $_post_values[] = $regs; } else break; } $error .= ( $i == count($params['timetable']) ? 0 : 1 ) ; if ( ereg("^0+$", $error) ) { // Comprobacion de que no haya registros que se solapen $num_post_values = count($_post_values); for ( $i=0; $i<$num_post_values; $i++ ) { if ( in_array($_post_values[$i][0], $record_values) ) { $old_values[] = $_post_values[$i][0]; continue; } for ( $c=0; $c<$num_post_values; $c++ ) { if ( $c != $i ) { if ( ( ( $_post_values[$i][1] == $_post_values[$c][1] ) && ( $_post_values[$i][2] == $_post_values[$c][2] ) ) || ( ( $_post_values[$i][1] == $_post_values[$c][1] ) && ( $_post_values[$i][1] == $_post_values[$c][2] ) ) || ( ( $_post_values[$i][2] == $_post_values[$c][1] ) && ( $_post_values[$i][2] == $_post_values[$c][2] ) ) || ( ( $_post_values[$i][1] >= $_post_values[$c][1] ) && ( $_post_values[$i][1] < $_post_values[$c][2] ) ) || ( ( $_post_values[$i][2] > $_post_values[$c][1] ) && ( $_post_values[$i][2] <= $_post_values[$c][2] ) ) ) { if ( ( ( $_post_values[$i][3] == $_post_values[$c][3] ) && ( $_post_values[$i][4] == $_post_values[$c][4] ) ) || ( ( $_post_values[$i][3] >= $_post_values[$c][3] ) && ( $_post_values[$i][3] < $_post_values[$c][4] ) ) || ( ( $_post_values[$i][4] > $_post_values[$c][3] ) && ( $_post_values[$i][4] <= $_post_values[$c][4] ) ) ) break(2); } } if ( $c == ( $num_post_values - 1 ) ) $new_values[] = $_post_values[$i]; } } if ( $i == $num_post_values ) { $error .= 0; for ( $a=0; $asql_tables[$section] . ' (id, dia_inicio, dia_fin, hora_inicio, hora_fin) VALUES (\'' . $record . '\', \'' . $new_values[$a][1] . '\', \'' . $new_values[$a][2] . '\', \'' . $new_values[$a][3] . '\', \'' . $new_values[$a][4] . '\')'); $delete_values = array_diff($record_values, $old_values); if ( count($delete_values) > 0 ) { while ( list($id) = each($delete_values) ) { if ( ereg("^([1-7]{1})-([1-7]{1})-([0-9]{1,2})-([0-9]{1,2})$", $delete_values[$id], $regs) ) @db_query('DELETE FROM ' . $this->sql_tables[$section] . ' WHERE id = \'' . $record . '\' AND dia_inicio = \'' . $regs[1] . '\' AND dia_fin = \'' . $regs[2] . '\' AND hora_inicio = \'' . $regs[3] . '\' AND hora_fin = \'' . $regs[4] . '\''); } } } else $error .= 1; return $error; } else return $error; } function manageTimeTable( &$params ) { if ( is_array($params['timetable']) ) { $time_values = array(); for ( $i=0; $ilng['select_day_' . $regs[1]] . ( $regs[2] > $regs[1] ? ' - ' . $this->lng['select_day_' . $regs[2]] : '' ) . ' [ ' . $this->hours[$regs[3]] . ' - ' . $this->hours[$regs[4]] . ' ]'; } $params['timetable'] = $time_values; } } function sectionTimeTable( $section, $record, $format = false ) { if ( isset($this->sql_tables[$section]) ) { $SQL_query = db_query('SELECT dia_inicio, dia_fin, hora_inicio, hora_fin FROM ' . $this->sql_tables[$section] . ' WHERE id = \'' . $record . '\' ORDER BY dia_inicio'); if ( db_num_rows($SQL_query) > 0 ) { $timeTable = array(); while ( $SQL_values = db_fetch_array($SQL_query) ) { if ( $SQL_values['dia_inicio'] == $SQL_values['dia_fin'] ) $timeTable[$SQL_values['dia_inicio'] . '-' . ( $format ? $SQL_values['dia_inicio'] . '-' : '' ) . $SQL_values['hora_inicio'] . '-' . $SQL_values['hora_fin']] = $this->lng['select_day_' . $SQL_values['dia_inicio']] . ' [ ' . $this->hours[$SQL_values['hora_inicio']] . ' - ' . $this->hours[$SQL_values['hora_fin']] . ' ]'; else { for ( $i=$SQL_values['dia_inicio']; $i<=$SQL_values['dia_fin']; $i++ ) $timeTable[$i . '-' . ( $format ? $i . '-' : '' ) . $SQL_values['hora_inicio'] . '-' . $SQL_values['hora_fin']] = $this->lng['select_day_' . $i] . ' [ ' . $this->hours[$SQL_values['hora_inicio']] . ' - ' . $this->hours[$SQL_values['hora_fin']] . ' ]'; } } return $timeTable; } } } function getCurrentWeek( $date ) { $excludeDays = 0; $currentWeek = 0; $firstDay = date( 'w', mktime(0,0,0,1,1,date('Y', $date)) ); $dayOfYear = date('z', $date); if ( $firstDay > 1 ) { $excludeDays = 8 - $firstDay; $currentWeek++; } elseif ( $firstDay == 0 ) { $excludeDays = 1; $currentWeek++; } $currentWeek = ceil( ( $dayOfYear + ( 7 - $firstDay ) ) / 7 ); return $currentWeek; } function DateFromWeek( $year, $week, $day ) { $FirstDayOfWeek = 1; $BaseDate = 4; $CJDDelta = 2415019; $StartDate = $this->DelphiDate(mktime(1, 0, 0, 01, $BaseDate, $year)); $Offset = ($week-1) * 7 - $this->mod(floor($StartDate) + $CJDDelta + 8 - $FirstDayOfWeek,7) + $day - 1; return $this->PHPUnixTimeStamp($StartDate + $Offset); } function DelphiDate($aPHPTime) { return $this->div($aPHPTime,86400)+25569; } function PHPUnixTimeStamp($aDelphiDate) { return (($aDelphiDate-25569)*86400)-7200; } function mod($number, $div) { return $number - floor($number/$div)*$div; } function div($number, $div) { return floor($number/$div); } function numYearWeeks( $year ) { $excludeDays = 0; $yearWeeks = 0; $firstDay = date( 'w', mktime(0,0,0,1,1,$year) ); if ( $firstDay > 1 ) { $excludeDays = 8 - $firstDay; $yearWeeks++; } elseif ( $firstDay == 0 ) { $excludeDays = 1; $yearWeeks++; } $yearWeeks += ceil( ( $this->numYearDays( $year ) - $excludeDays) / 7 ); return $yearWeeks; } function numYearDays( $year ) { $days = date( 'z', mktime(0,0,0,12,31,$year) ) + 1; return $days; } function numMonths( $startDate, $endDate ) { $date = $startDate; $months = 0; while ( $date <= $endDate ) { $months++; $date = mktime(0,0,0,date('n', $date), (date('t', $date) + 1), date('Y', $date) ); } return $months; } } ?>