123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- ini_set ( 'max_execution_time' , '300' );//5 min
- require_once("../../include/constantes.php");
- require_once("../../include/nocache.php");
- require_once("../../include/util.php");
- require_once("../../include/bd_pdo.php");
- require_once("../../classes/ValidaSesion.php");
- require "../../include/phpSpreadsheet/autoload.php";
- use PhpOffice\PhpSpreadsheet\IOFactory;
- function valoresCompletos($data, $max){
- for($i = 0; $i<$max; $i++){
- if(!isset($data[$i]) || (trim($data[$i]) == "" || strlen(trim($data[$i])) <= 1))
- return false;
- }
- return true;
- }
- function obtieneNumeros ($texto) {
- preg_match_all('/\d+/', $texto, $matches);
- return implode($matches[0]);
- }
- function obtieneLetras ($texto) {
- preg_match_all('/[a-zA-Z]+/', $texto, $matches);
- return implode($matches[0]);
- }
- function quitaCarrera($plan_rs, $shortname){
- foreach($plan_rs AS $plan){
- if( mb_strpos($shortname, $plan["Carrera_prefijo"]) !== false){
- return str_replace($plan["Carrera_prefijo"], "",$shortname);
- }
- }
- }
- $pag = "../materiasalumnos_moodle.php";
- //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
- $objSesion = new ValidaSesion($pdo, 102, GEMA);
- if(!$objSesion->tieneAcceso()){
- $return["error"] = "No tienes permisos para acceder a esa sección o la sesión caducó.";
- }else if(!isset($_FILES['archivo']['name'][0])){
- $return["error"] = "No se recibieron los datos de los alumnos.";
- }else{
-
- //$inputFileName =__DIR__. '/tmp/alumnos_tmp.xlsx';
- $archivo = $_FILES['archivo']['name'][0];
- // Check file size
- if ($_FILES["archivo"]["size"][0] > 10 * 1024 * 1024) {//en bytes
- $return["error"] = "El archivo supera los 10MB y no se puede cargar.";
- $return["json"] = json_encode($return);
- echo json_encode($return);
- exit();
- }
- $exts_arr = array("xls","xlsx");
- $dot_arr = explode('.', $archivo);
- $ext = end($dot_arr);
- if(!in_array($ext, $exts_arr)){
- $return["error"] = "No es una extensión válida. Solo se permiten archivos XLS o XLSX.";
- $return["json"] = json_encode($return);
- echo json_encode($return);
- exit();
- }
- //--------
- $stmt = $pdo->prepare('Select DISTINCT "Carrera_prefijo", "Carrera_id" from fs_planestudio(:nivel, NULL, NULL, true, 0, NULL) ORDER BY "Carrera_prefijo"');
- $stmt->bindParam(":nivel", $_SESSION["nivel_id"]);
- if(!$stmt->execute()){
- $return["error"] = "Ocurrió un error al cargar los planes de estudios";
- $return["json"] = json_encode($return);
- echo json_encode($return);
- exit();
- }else{
- $plan_rs = $stmt->fetchAll();
- }
- $stmt->closeCursor();
- //---------
- //$newfilename = "tmp_al_".$_SESSION["gpo_id"].".".$ext;
- $inputFileName = $_FILES["archivo"]["tmp_name"][0];
- $inputFileType = IOFactory::identify($inputFileName);
- $reader = IOFactory::createReader($inputFileType);
- $spreadsheet = $reader->load($inputFileName);
- $spreadsheet->setActiveSheetIndex(0);
- $sheetData = $spreadsheet->getActiveSheet()->toArray();
- try {
- $pdo->beginTransaction();
- $alumnoArr = array();
- $row = 1;
- $omitidoArr = array();
- $insertada = 0;
- $debug = "";
-
- $i=0;
- $al_old;
- foreach($sheetData as $data){//Carrera / Clave / Nombre completo
- if($row > 1){//ignora header
- //if(trim($data[0]) != "" && trim($data[1]) != "" && trim($data[2]) != "" && trim($data[3]) != ""){
- if(valoresCompletos($data, 2)){
- if(strpos( $data[1], "ELECTIVA") ===false){
- $clave = $data[0];//OK
- $tmpArr = explode("-", $data[1]);
- $shortname = $tmpArr[0];
- $mat_id = $tmpArr[3];//OK
- $grupo = quitaCarrera($plan_rs, $shortname );//OK
-
- //echo "SELECT * from fi_alumnomaterias_excel($clave, ".$_SESSION["periodo_id"].", $grupo, $mat_id) ||";
- $stmt = $pdo->prepare('SELECT * from fi_alumnomaterias_excel(:clave, :per, :gpo, :mat)');
- $stmt->bindParam(":clave", $clave);
- $stmt->bindParam(":per", $_SESSION["periodo_id"]);
- $stmt->bindParam(":gpo", $grupo);
- $stmt->bindParam(":mat", $mat_id);
- if(!$stmt->execute()){
- $pdo->rollBack();
- $return["error"] = "Ocurrió un error al cargar los planes de estudios";
- $return["json"] = json_encode($return);
- echo json_encode($return);
- exit();
- }else{
- $mat_rs = $stmt->fetch();
- }
- $stmt->closeCursor();
- if($mat_rs["Materia_insertada"]){
- $insertada++;
- }else{
- if($mat_rs["Materia_repetida"]){
- $omitidoArr[] = array("clave"=>$clave, "shortname"=>$data[1], "desc"=> $row.". La materia <b>$mat_id</b> ya estaba asignada");
- }else{
- $omitidoArr[] = array("clave"=>$clave, "shortname"=>$data[1], "desc"=> $row.". Error al insertar la materia <b>$mat_id</b> para el alumno <b>$clave</b> en el grupo <b>$grupo</b>");
- }
-
- $debug .= 'SELECT * from fi_alumnomaterias_excel(\''.$clave.'\', '.$_SESSION["periodo_id"].', '.$grupo.', '.$mat_id.')'." |";
- }
- }
- }else{
- $omitidoArr[] = array("clave"=>$clave, "shortname"=>$data[1], "desc"=> $row.". El renglón no tiene los datos requeridos");
- }
- }
- $row++;
- }
- //$pdo->commit();
- /*$stmt = null; // cierra conexion
- if($repetido == 0){
- header("Location: ".$pag."?ok=0");
- }else if($repetido < count($apellidos)){
- header("Location: ".$pag."?ok=1&rep=".$repetido);
- }else{
- header("Location: ".$pag."?error=4");
- }*/
- //$alumnoArr;
- $return["insertados"] = $insertada;
- $return["omitidosArr"] = $omitidoArr;
- //$return["debug"] = $debug;
- } catch(PDOException $e) {
- //$pdo->rollBack();
- //header("Location:".$pag."?error=1");
- print_r($e);
- }
- }
- $return["json"] = json_encode($return);
- echo json_encode($return);
|