forms.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Este arquivo é parte do software linequest
  4. * Ambiente de questionários para a coleta de dados
  5. *
  6. * Laboratório de Informática na Educação - LInE
  7. * https://www.usp.br/line/
  8. *
  9. * Utilize os atributos definidos abaixo para
  10. * configurar o ambiente de questionários.
  11. *
  12. * @author Lucas Calion
  13. * @author Igor Félix
  14. */
  15. require_once ('../config/linequest.php');
  16. function connect () {
  17. global $CFG, $DB;
  18. $DB = new mysqli($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname);
  19. if ($DB->connect_error) {
  20. die("Connection failed: " . $DB->connect_error);
  21. }
  22. $DB->set_charset("utf8");
  23. }
  24. function get_id_by_uuid ($uuid) {
  25. global $DB; connect();
  26. $sql = "SELECT * FROM records";
  27. if (!($stmt = $DB->prepare($sql))) {
  28. echo "Prepare failed: (" . $DB->errno . ") " . $DB->error;
  29. }
  30. $stmt->execute();
  31. $res_data = mysqli_stmt_get_result($stmt);
  32. $all_data = array();
  33. while($row = mysqli_fetch_array($res_data)){
  34. $data = json_decode($row['form']);
  35. if (isset($data->uuid) && ($data->uuid == $uuid)) {
  36. return $row['id'];
  37. }
  38. }
  39. return -1;
  40. }
  41. function store ($data, $uuid = null) {
  42. global $DB; connect();
  43. $sql = "";
  44. $id_p = get_id_by_uuid($uuid);
  45. if ($id_p >= 0) {
  46. $sql = "UPDATE records SET form = ?
  47. WHERE id = " . $id_p;
  48. } else {
  49. $sql = "INSERT INTO records (id, form)
  50. VALUES (null, ?)";
  51. }
  52. if (!($stmt = $DB->prepare($sql))) {
  53. echo "Prepare failed: (" . $DB->errno . ") " . $DB->error;
  54. }
  55. if (!$stmt->bind_param("s", $data)) {
  56. echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
  57. }
  58. if (!$stmt->execute()) {
  59. echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
  60. }
  61. $DB->close();
  62. }
  63. function get_forms () {
  64. global $DB; connect();
  65. $sql = "SELECT form FROM records";
  66. if (!($stmt = $DB->prepare($sql))) {
  67. echo "Prepare failed: (" . $DB->errno . ") " . $DB->error;
  68. }
  69. $stmt->execute();
  70. $res_data = mysqli_stmt_get_result($stmt);
  71. $all_forms = array();
  72. while($row = mysqli_fetch_array($res_data)){
  73. $data = json_decode($row['form']);
  74. if(!in_array($data->form, $all_forms)){
  75. $all_forms[] = $data->form;
  76. }
  77. }
  78. sort($all_forms);
  79. return $all_forms;
  80. }
  81. function get_total_rows ($form_id) {
  82. global $DB; connect();
  83. $sql = "SELECT form FROM records";
  84. if (!($stmt = $DB->prepare($sql))) {
  85. echo "Prepare failed: (" . $DB->errno . ") " . $DB->error;
  86. }
  87. $stmt->execute();
  88. $res_data = mysqli_stmt_get_result($stmt);
  89. $count = 0;
  90. while($row = mysqli_fetch_array($res_data)){
  91. $data = json_decode($row['form']);
  92. if ($data->form == $form_id) {
  93. $count ++;
  94. }
  95. }
  96. return $count;
  97. }
  98. function get_records_pagination($form_id, $offset, $no_of_records_per_page) {
  99. global $DB; connect();
  100. $sql = "SELECT * FROM records";
  101. if (!($stmt = $DB->prepare($sql))) {
  102. echo "Prepare failed: (" . $DB->errno . ") " . $DB->error;
  103. }
  104. $stmt->execute();
  105. $res_data = mysqli_stmt_get_result($stmt);
  106. $all_data = array();
  107. $count = 0;
  108. $included = 0;
  109. while($row = mysqli_fetch_array($res_data)){
  110. $data = json_decode($row['form']);
  111. if ($data->form == $form_id) {
  112. $data->id = $row['id'];
  113. $count++;
  114. if ($count-1 >= $offset) {
  115. $included ++;
  116. if ($included-1 == $no_of_records_per_page) break;
  117. $all_data[] = $data;
  118. }
  119. }
  120. }
  121. return $all_data;
  122. }
  123. ?>