Igor преди 5 години
родител
ревизия
c303335357
променени са 5 файла, в които са добавени 345 реда и са изтрити 2 реда
  1. 86 2
      controller/forms.php
  2. 4 0
      explorer/destroy.php
  3. 53 0
      explorer/export.php
  4. 180 0
      explorer/index.php
  5. 22 0
      explorer/login.php

+ 86 - 2
controller/forms.php

@@ -27,8 +27,6 @@
 
  function store ($data) {
     global $DB; connect();
-
-    echo $data;
     
     $sql = "INSERT INTO records (id, form)
             VALUES (null, ?)";
@@ -48,4 +46,90 @@
     $DB->close();
  }
 
+ function get_forms () {
+    global $DB; connect();
+
+    $sql = "SELECT form FROM records";
+
+    if (!($stmt = $DB->prepare($sql))) {
+        echo "Prepare failed: (" . $DB->errno . ") " . $DB->error;
+    }
+
+    $stmt->execute();
+
+    $res_data = mysqli_stmt_get_result($stmt);
+
+    $all_forms = array();
+
+    while($row = mysqli_fetch_array($res_data)){
+        $data = json_decode($row['form']);
+        if(!in_array($data->form, $all_forms)){
+            $all_forms[] = $data->form;
+        }
+    }
+
+    sort($all_forms);
+
+    return $all_forms;
+ }
+
+ function get_total_rows ($form_id) {
+    global $DB; connect();
+    
+    $sql = "SELECT form FROM records";
+
+    if (!($stmt = $DB->prepare($sql))) {
+        echo "Prepare failed: (" . $DB->errno . ") " . $DB->error;
+    }
+    
+    $stmt->execute();
+
+    $res_data = mysqli_stmt_get_result($stmt);
+
+    $count = 0;
+
+    while($row = mysqli_fetch_array($res_data)){
+        $data = json_decode($row['form']);
+        if ($data->form == $form_id) {
+            $count ++;
+        }
+    }
+
+    return $count;
+ }
+
+ function get_records_pagination($form_id, $offset, $no_of_records_per_page) {
+    global $DB; connect();
+
+    $sql = "SELECT * FROM records";
+
+    if (!($stmt = $DB->prepare($sql))) {
+        echo "Prepare failed: (" . $DB->errno . ") " . $DB->error;
+    }
+    
+    $stmt->execute();
+    
+    $res_data = mysqli_stmt_get_result($stmt);
+    
+    $all_data = array();
+
+    $count = 0;
+    $included = 0;
+    
+    while($row = mysqli_fetch_array($res_data)){
+        $data = json_decode($row['form']);
+        if ($data->form == $form_id) {
+            $data->id = $row['id'];
+            $count++;
+            if ($count-1 >= $offset) {
+                $included ++;
+                if ($included-1 == $no_of_records_per_page) break;
+                $all_data[] = $data;
+            }
+            
+        }
+    }
+    return $all_data;
+ }
+
  ?>

+ 4 - 0
explorer/destroy.php

@@ -0,0 +1,4 @@
+<?php 
+session_start();
+session_destroy();
+?>

+ 53 - 0
explorer/export.php

@@ -0,0 +1,53 @@
+<?php
+    session_start();
+    if (!isset($_SESSION['hash_user'])) {
+        header('Location: login.php');
+        exit;
+    }
+?>  
+
+<?php
+
+    $form_id = $_GET['formulario'];
+
+    header('Content-Type: text/csv; charset=utf-8');
+    header('Content-Disposition: attachment; filename='.$form_id.'.csv');
+
+    require_once('../controller/forms.php');
+
+    $total_rows = get_total_rows($form_id);
+
+    $res_data = get_records_pagination($form_id, 0, $total_rows);
+
+    $output = fopen('php://output', 'w');
+
+    
+    $all_fields = array();
+
+    foreach($res_data as $form) {
+        foreach($form as $key => $val) {
+            if(!in_array($key, $all_fields)) {
+                $all_fields[] = $key;
+            }
+        }
+    }
+
+    fputcsv($output, $all_fields);
+
+
+    foreach($res_data as $form) {
+
+        $prepared = array();
+        foreach($all_fields as $field) {
+            
+            if ($field === 'timestamp') {
+                $prepared[] = date("H:i:s d/m/Y", $form->$field);
+                continue;
+            }
+            $prepared[] = $form->$field;
+            
+        }
+        fputcsv($output, $prepared);
+    }
+
+?>

+ 180 - 0
explorer/index.php

@@ -0,0 +1,180 @@
+
+<?php
+    session_start();
+    if (!isset($_SESSION['hash_user'])) {
+        header('Location: login.php');
+        exit;
+    }
+?>  
+    
+
+
+<html>
+<head>
+    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
+    <title>LInE Quest</title>
+    <style>
+        body {
+            font-family: Arial;
+        }
+        table {
+            border-collapse: collapse;
+            width: 100%;
+            margin-bottom: 1em;
+        }
+
+        table td, table th {
+            border: 1px solid #ddd;
+            padding: 8px;
+        }
+
+        table tr:nth-child(even){background-color: #f2f2f2;}
+
+        table tr:hover {background-color: #ddd;}
+
+        table th {
+            padding-top: 12px;
+            padding-bottom: 12px;
+            text-align: left;
+            background-color: #4CAF50;
+            color: white;
+        }
+
+        .pagination {
+            display: inline-block;
+            margin-top: 1em;
+        }
+
+        .pagination a {
+            color: black;
+            float: left;
+            padding: 8px 16px;
+            text-decoration: none;
+            border: 1px solid #ddd;
+        }
+
+        .pagination a:hover:not(.active) {background-color: #ddd;}
+
+        .pagination a:first-child {
+            border-top-left-radius: 5px;
+            border-bottom-left-radius: 5px;
+        }
+
+        .pagination a:last-child {
+            border-top-right-radius: 5px;
+            border-bottom-right-radius: 5px;
+        }
+
+        .disabled {
+
+            pointer-events: none;
+            cursor: default;
+            text-decoration: none;
+            color: #918a8a !important;;
+            background-color: #f0efef;
+        }
+
+        .export {
+            float: right;
+        }
+
+        .total {
+            float: left;
+        }
+    </style>
+</head>
+<body>
+
+    <?php 
+        require_once('../controller/forms.php');
+        $all_forms = get_forms();
+    ?>
+
+    <form>
+        Selecione o identificador do formulário: 
+            <select name="formulario" onchange="this.form.submit()">
+                <option></option>";
+                <?php
+                    foreach($all_forms as $form) {
+                        if ((isset($_GET['formulario'])
+                            && $_GET['formulario'] == $form)) {
+                            print "<option selected>$form</option>";
+                        } else {
+                            print "<option>$form</option>";
+                        }
+                    }
+                ?>
+            </select>
+    </form>
+        
+    <table>
+    <?php
+        if ((!isset($_GET['formulario']))) exit;
+
+        if (isset($_GET['pageno'])) {
+            $pageno = $_GET['pageno'];
+        } else {
+            $pageno = 1;
+        }
+
+        $no_of_records_per_page = 10;
+        $offset = ($pageno-1) * $no_of_records_per_page;
+        $form_id = $_GET['formulario'];
+
+        $total_rows = get_total_rows($form_id);
+        $total_pages = ceil($total_rows / $no_of_records_per_page);
+
+        $res_data = get_records_pagination($form_id, $offset, $no_of_records_per_page);
+        
+        $all_fields = array();
+
+        foreach($res_data as $form) {
+            foreach($form as $key => $val) {
+                if(!in_array($key, $all_fields)) {
+                    $all_fields[] = $key;
+                }
+            }
+        }
+
+        print '<tr>';
+        foreach($all_fields as $field) {
+            print "<th>$field</th>";
+        }
+        print '</tr>';
+
+        foreach($res_data as $form) {
+
+            print '<tr>';
+            foreach($all_fields as $field) {
+                //if ($field === 'form') continue;
+                print '<td>';
+                if ($field === 'timestamp') {
+                    print date("H:i:s d/m/Y", $form->$field).'</td>';
+                    continue;
+                }
+                print $form->$field.'</td>';
+            
+            }
+            print '</tr>';
+        }
+        
+    ?>
+    </table>
+    <div class="total">
+        Total de registros: <b><?= $total_rows ?></b>
+    </div>
+    <div class="export">
+        Exportar como: <a href="export.php?formulario=<?= $form_id ?>&format=csv">CSV</a>
+    </div>
+    <center>
+        <div class="pagination">
+            <a class="<?php if($pageno <= 1){ echo 'disabled'; } ?>" href="?formulario=<?= $form_id ?>&pageno=1">Primeira</a>
+
+            <a class="<?php if($pageno <= 1){ echo 'disabled'; } ?>" href="<?php if($pageno <= 1){ echo '#'; } else { echo "?formulario=$form_id&pageno=".($pageno - 1); } ?>">Anterior</a>
+            
+            <a class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>" href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?formulario=$form_id&pageno=".($pageno + 1); } ?>">Próxima</a>
+            <a class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>" href="?formulario=<?= $form_id ?>&pageno=<?php echo $total_pages; ?>">Última</a>
+        </div>
+    </center>
+</body>
+</html>

+ 22 - 0
explorer/login.php

@@ -0,0 +1,22 @@
+
+    <form method="post">
+        Informe a senha: 
+        <input type="password" name="senha">
+        <input type="submit" value="Enviar">
+        
+        <?php
+            if (isset($_POST['senha'])) {
+                require_once ('../config/linequest.php');
+
+                global $CFG;
+                if ($CFG->viewpass == $_POST['senha']) {
+                    session_start();
+                    $_SESSION['hash_user'] = md5($_POST['senha'].time());
+                    header('Location: index.php');
+                } else {
+                    echo "<b style='color: red'>Senha incorreta!</b>";
+                }
+            }
+            
+        ?>  
+    </form>