config = &$CONF; $this->fields = array( '1' => 'id', '2' => 'nombre', '3' => 'fecha_inicio', '4' => 'fecha_fin', '5' => 'ejercicio' ); $this->query_order = array( 'a' => 'ASC', 'd' => 'DESC' ); $this->records_view = array('order' => array('5','1','2','3','4'), 'width' => array('22','25', '250', '90', '90'), 'orderby' => array(false, true, true, true, true) ); $exercise = ''; if ( !empty($_SESSION['exercise']) ) $this->setExercise($_SESSION['exercise']); else $this->setExercise(); } //----------------------------------------------------------------------------------- // setExercise // Establece el ejercico lectivo sobre el cual va a trabajar la aplicacion. function setExercise($record = '') { $exercise = ''; if ( !empty($record) ) { if ( $this->checkRecord($record) ) { $_SESSION['exercise'] = $record; $this->exercise = $this->getRecordValues($record); $exercise = $record; } } if ( empty($exercise) ) { $SQL_current_exercise_query = db_query('SELECT id FROM ejercicios_lectivos WHERE fecha_inicio <= "' . date('Y-m-d') . '" AND fecha_fin >="' . date('Y-m-d') . '" LIMIT 0,1'); if ( db_num_rows($SQL_current_exercise_query) == 1 ) { $SQL_current_exercise = db_fetch_array($SQL_current_exercise_query); $this->exercise = $this->getRecordValues($SQL_current_exercise['id']); $_SESSION['exercise'] = $this->exercise['id']; } } } //----------------------------------------------------------------------------------- // getCurrentExercise function getCurrentExercise() { $SQL_query = db_query('SELECT id FROM ejercicios_lectivos WHERE fecha_inicio <= "' . date('Y-m-d') . '" AND fecha_fin >="' . date('Y-m-d') . '" LIMIT 0,1'); if ( db_num_rows($SQL_query) == 1 ) { $SQL_values = db_fetch_array($SQL_query); $SQL_values = $this->getRecordValues($SQL_values['id']); return $SQL_values; } else return false; } //----------------------------------------------------------------------------------- // getRecordList // Obtiene un listado de registros de la BBDD. function getRecordList(&$page, $order_field = 4, $order_type = 'd') { $records_list = array(); if ( !isset( $this->fields[$order_field] ) ) $order_field = 4; if ( !isset( $this->query_order[$order_type] ) ) $order_type = 'd'; $SQL_records_query = db_query('SELECT COUNT(*) AS total FROM ejercicios_lectivos' . $SQL_records_filter); $SQL_records = db_fetch_array($SQL_records_query); if ( $SQL_records['total'] > 0 ) { $page = check_records_page($page, $SQL_records['total']); $this->num_records = $SQL_records['total']; $SQL_records_order = ' ORDER BY ' . $this->fields[$order_field] . ' ' . $this->query_order[$order_type]; $SQL_records_limit = ' LIMIT ' . ( ( $page - 1 ) * $this->config['max_num_regs_list'] ) . ', ' . $this->config['max_num_regs_list']; $SQL_query = db_query('SELECT id, nombre, fecha_inicio, fecha_fin FROM ejercicios_lectivos' . $SQL_records_filters . $SQL_records_order . $SQL_records_limit); while ( $SQL_record = db_fetch_array($SQL_query) ) { strip_slashes($SQL_record); $records_list[$SQL_record['id']]['1'] = $SQL_record['id']; $records_list[$SQL_record['id']]['2'] = $SQL_record['nombre']; $records_list[$SQL_record['id']]['3'] = $SQL_record['fecha_inicio']; $records_list[$SQL_record['id']]['4'] = $SQL_record['fecha_fin']; $records_list[$SQL_record['id']]['5'] = 'exercise['id'] ? ' checked' : ' onClick="setDefault(this)"' ) . '>'; } } return $records_list; } //----------------------------------------------------------------------------------- // deleteRecord // Elimina un registro de la BBDD. function deleteRecord($record) { $error .= ''; if ( $this->checkRecord($record) ) { $SQL_query = @db_query('SELECT count(*) AS total FROM grupos WHERE id_periodo = \'' . $record . '\''); $SQL_values = db_fetch_array($SQL_query); if ( $SQL_values['total'] > 0 ) $error .= 2; else { if ( @db_query('DELETE FROM ejercicios_lectivos WHERE id = \'' . $record . '\'') ) $error .= '0'; else $error .= '3'; } } else $error .= '1'; return $error; } //----------------------------------------------------------------------------------- // checkRecord // Comprueba si existe o no un registro function checkRecord(&$record) { settype($record, "integer"); $SQL_check_record = db_query('SELECT count(*) AS total FROM ejercicios_lectivos WHERE id = \'' . $record . '\''); $SQL_num_records = db_fetch_array($SQL_check_record); if ( $SQL_num_records['total'] == 1 ) return true; else return false; } //----------------------------------------------------------------------------------- // getRecordValues // Obtiene los valores para el registro indicado. function getRecordValues($record) { if ( $this->checkRecord($record) ) { $SQL_check_record = db_query('SELECT id, nombre AS exercise_name, UNIX_TIMESTAMP(fecha_inicio) AS start_date, UNIX_TIMESTAMP(fecha_fin) AS end_date, DATE_FORMAT(fecha_inicio, "%d/%m/%Y") AS exercise_start_date, DATE_FORMAT(fecha_fin, "%d/%m/%Y") AS exercise_end_date FROM ejercicios_lectivos WHERE id = \'' . $record . '\''); if ( db_num_rows($SQL_check_record) == 1 ) { $SQL_values = db_fetch_array($SQL_check_record); strip_slashes($SQL_values); return $SQL_values; } else return false; } else return false; } //----------------------------------------------------------------------------------- // checkDate // Comprueba que la fecha del ejercicio lectivo que estamos tratando no se solapa con // otro ejercicio ya existente. function checkDate( $dates = array(), $record = '') { $SQL_check_record = db_query('SELECT count(*) AS total FROM ejercicios_lectivos WHERE fecha_inicio <= "' . date('Y-m-d', $dates[1]) . '" AND fecha_fin >= "' . date('Y-m-d', $dates[0]) . '"' . ( !empty($record) ? ' AND id != \'' . $record . '\'' : '' ) ); $SQL_num_records = db_fetch_array($SQL_check_record); if ( $SQL_num_records['total'] == 0 ) return true; else return false; } //----------------------------------------------------------------------------------- // manageRecord // - Recibe los parametros enviados desde el formulario mediante metodo POST. // - Determina el tipo de accion a realizar (INSERT o UPDATE). // - Trata los valores recibidos. // - Genera un codigo de error correspondiente a las acciones realizadas. // // [1] > Tipo de accion // [2] > Campo nombre // [3] > Campo Fecha Inicio // [4] > Campo Fecha Fin // [5] > Chequeo antes de realizar una accion sobre la BBDD // [6] > Resultado de la accion sobre la BBDD. function manageRecord(&$params) { $action = ''; $error = ''; // Chequeo y tratamiento de los valores recibidos if ( isset($params['record_id']) ) { if ( ereg("^[0-9]+$", $params['record_id'] ) && $params['record_id'] > 0 ) { if ( $this->checkRecord($params['record_id']) ) $action = 'update'; } } else $action = 'insert'; if ( !empty($params['exercise_start_date']) && ereg("^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$", $params['exercise_start_date'], $regs ) ) { if ( checkdate($regs[2], $regs[1], $regs[3]) ) $exercise_start_date = mktime(0,0,0,$regs[2], $regs[1], $regs[3]); } if ( !empty($params['exercise_end_date']) && ereg("^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$", $params['exercise_end_date'], $regs ) ) { if ( checkdate($regs[2], $regs[1], $regs[3]) ) $exercise_end_date = mktime(0,0,0,$regs[2], $regs[1], $regs[3]); } // Generacion del codigo de error $error = ( $action == '' ? 1 : 0 ); $error .= ( empty($params['exercise_name']) ? 1 : 0 ); $error .= ( empty($exercise_start_date) ? 1 : 0 ); if ( empty($exercise_end_date) ) $error .= 1; elseif ( !empty($exercise_start_date) && ( $exercise_start_date > $exercise_end_date ) ) $error .= 2; elseif ( !empty($exercise_start_date) && ( $exercise_start_date == $exercise_end_date ) ) $error .= 3; else $error .= 0; // Acciones sobre la BBDD if ( ereg("^0+$", $error) ) { format_record($params); if ( $action == 'insert' ) { if ( $this->checkDate(array($exercise_start_date, $exercise_end_date ) ) ) { if ( db_query('INSERT INTO ejercicios_lectivos (nombre, fecha_inicio, fecha_fin) VALUES (\'' . $params['exercise_name'] . '\', \'' . date('Y-m-d', $exercise_start_date) . '\', \'' . date('Y-m-d', $exercise_end_date) . '\')') ) { $params['record_id'] = db_insert_id(); $error .= 0; } else $error .= 2; } else $error .= 1; } elseif ( $action == 'update' ) { if ( $this->checkDate(array($exercise_start_date, $exercise_end_date ), $params['record_id'] ) ) { if ( db_query('UPDATE ejercicios_lectivos SET nombre = \'' . $params['exercise_name'] . '\', fecha_inicio = \'' . date('Y-m-d', $exercise_start_date) . '\', fecha_fin = \'' . date('Y-m-d', $exercise_end_date) . '\' WHERE id = \'' . $params['record_id'] . '\'') ) $error .= 0; else $error .= 3; } else $error .= 1; } } strip_slashes($params); return $error; } //----------------------------------------------------------------------------------- // getNextExerciseId function getNextExerciseId( $date_end ) { $SQL_query = db_query('SELECT id FROM ejercicios_lectivos WHERE fecha_inicio > "' . $date_end . '" ORDER BY fecha_inicio ASC LIMIT 1'); if ( db_num_rows($SQL_query) == 1 ) { $SQL_values = db_fetch_array($SQL_query); return $SQL_values['id']; } else return false; } //----------------------------------------------------------------------------------- // getLastExerciseId function getLastExerciseId( $date_start ) { $SQL_query = db_query('SELECT id FROM ejercicios_lectivos WHERE fecha_fin > "' . $date_start . '" ORDER BY fecha_fin DESC LIMIT 1'); if ( db_num_rows($SQL_query) == 1 ) { $SQL_values = db_fetch_array($SQL_query); return $SQL_values['id']; } else return false; } } ?>