include "config.php"; try { $dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); } catch(PDOException $e) { die($e->getMessage()); } ?> function doQuery($sql, $dbh, $values = array(), $debug = 0) { $stmt = $dbh->prepare($sql); if ($debug == 1) { print $sql; print_r($values); } foreach ($values as $f => $v) { $stmt->bindValue(':' . $f, $v); } $stmt->execute(); return $stmt; } function oneRowQuery($sql, $dbh, $values = array(), $resultado = PDO::FETCH_NUM, $debug=0) { $sth = doQuery($sql, $dbh, $values, $resultado, $debug); return $sth->fetch($resultado); } function hacerQuery($sql, $dbh, $values = array(), $resultado = PDO::FETCH_NUM, $debug=0) { $sth = doQuery($sql, $dbh, $values, $resultado, $debug); return $sth->fetchAll($resultado); } function ejecutar_insert($dbh, $tabla, $values = array(), $debug = 0) { foreach ($values as $field => $v) { $ins[] = ':' . $field; } $ins = implode(',', $ins); $fields = implode(',', array_keys($values)); $sql = "INSERT INTO $tabla ($fields) VALUES ($ins)"; $sth = doQuery($sql, $dbh, $values, $debug); return $sth; } function ejecutar_replace($dbh, $tabla, $values = array(), $debug = 0) { foreach ($values as $field => $v) { $ins[] = ':' . $field; } $ins = implode(',', $ins); $fields = implode(',', array_keys($values)); $sql = "REPLACE INTO $tabla ($fields) VALUES ($ins)"; $sth = doQuery($sql, $dbh, $values, $debug); return $sth; } function ejecutar_delete($dbh, $tabla, $values = array(), $debug = 0) { $where = ""; foreach ($values as $field => $v) { if ($where != "") { $where .= " AND "; } $where .= "$field = $v"; } $sql = "DELETE FROM $tabla WHERE $where"; $sth = doQuery($sql, $dbh, $values, $debug); return $sth; } function ejecutar_update($dbh, $tabla, $values = array(), $wheres = array(), $debug = 0) { $sql = "UPDATE $tabla SET "; $set_vals = ""; foreach ($values as $field => $v) { if ($set_vals != "") { $set_vals .= ", "; } $set_vals .= "$field = :" . $field; } $where_vals = ""; foreach ($wheres as $where => $cond) { if ($where_vals != "") { $where_vals .= " AND "; } else { $where_vals = " WHERE " ; } $where_vals .= $where . " = :" . $where; } $sql .= $set_vals . $where_vals; if ($debug) { print $sql; print_r($values); print_r($wheres); } $sth = $dbh->prepare($sql); foreach ($values as $f => $v) { $sth->bindValue(':' . $f, $v); } foreach ($wheres as $f => $v) { $sth->bindValue(':' . $f, $v); } $sth->execute(); return $sth; } ?> function validar() { return $_SESSION['usuario']; } // OJO QUE este codigo se ejecuta siempre! // si no tengo una sesion anterior me manda al login. De esta forma con incluir "functions.php" ya me aseguro // que esa pagina requiere login. $config['urlgithub'] = "https://www.github.com/sisoputnfrba/"; $config['cant_integrantes'] = 5; define("CONFIRMADO", 2); define("ESPERANDO_CONFIRMACION", 1); define("CONFIRMACION_PENDIENTE", 0); session_start(); if(!validar()) { session_destroy(); header("location: index.php"); } // Ahora si las funciones utiles require 'mail.php'; // dado un array bidimensional me devuelve un listado html de options function optionsFromArray($array, $selected) { $lista = ""; foreach ($array as $valor) { if ($selected != $valor[0]) { $sl = ""; } else { $sl = " SELECTED "; } $lista .= ""; } return $lista; } // dado un array bidimensional me devuelve una lista html (SELECT) con todas las opciones cargadas y si quiero puedo tener una pre-seleccionada ($selected) function selectFromArray($nombre, $array, $selected = "") { $lista = ""; return $lista; } // generador de queries berreta. asumo que todas las tablas son en plural (alumnos) y tienen un id que se llama como la tabla en singular (idalumno) function genQuery($nombre) { return "SELECT id" . $nombre . ", nombre FROM " . $nombre . "s "; // ORDER BY 2"; } // devuelve una lista HTML (select) con todos los ayudantes. util :) function selectAyudantes($dbh, $selected = "") { $nombre = "ayudante"; $sql = genQuery($nombre) . " WHERE nivel = 1"; return selectFromArray("id" . $nombre, hacerQuery($sql, $dbh), $selected); } // si tengo una tabla alumnos y cada alumno tiene un idalumno esto me devuelve una lista HTML (select) con todos los alumnos y cuando selecciono uno // puedo obtener su id desde la property HTML value function selectFromQuery($dbh, $nombre, $selected = "") { $sql = genQuery($nombre); return selectFromArray("id" . $nombre, hacerQuery($sql, $dbh), $selected); } // "wrapper" para solicitar datos a Github mediante la API REST function gitHubApiRequest($access_token, $solicitud) { $url = 'https://api.github.com' . $solicitud .'?access_token=' . $access_token; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla 5.0"); $response = curl_exec($ch); $json= json_decode($response, true); curl_close($ch); return $json; } // sin comentarios function nota_texto($nro) { $letras = array("cero", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez"); return $letras[$nro]; } // cosa rara. dado un idgrupo me devuelve una tabla HTML con los datos de los integrantes, el nombre de grupo ($grupo) y el email de todos ($email) function crear_tabla_grupo($dbh, $idgrupo, &$grupo, &$email) { $sql = "SELECT a.legajo, a.nombre, a.apellido, a.curso, a.email, g.nombre FROM alumnos a, grupos g, asignaciones s WHERE a.idAlumno = s.idAlumno AND g.idGrupo=s.idGrupo and g.idGrupo = :idgrupo"; $alumnos = hacerQuery($sql, $dbh, array("idgrupo" => "$idgrupo")); $tabla = "
| Legajo | Nombre | Apellido | Curso | |
|---|---|---|---|---|
| " .$a[0] . " | " . $a[1] . " | " . $a[2]. " | " . $a[3] . " | " .$a[4] . " |