forms.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 id FROM answers WHERE uuid = ?";
  27. if (!($stmt = $DB->prepare($sql))) {
  28. die("Prepare failed: (" . $DB->errno . ") " . $DB->error);
  29. }
  30. $stmt->bind_param('s', $uuid);
  31. $stmt->execute();
  32. $stmt->store_result();
  33. $stmt->bind_result($userID);
  34. if ($stmt->fetch()) {
  35. $stmt->free_result();
  36. $stmt->close();
  37. $DB->close();
  38. return $userID;
  39. }
  40. $stmt->free_result();
  41. $stmt->close();
  42. $DB->close();
  43. return -1;
  44. }
  45. function getQuestionaireInfo ($hash) {
  46. global $DB; connect();
  47. $sql = "SELECT id,title FROM questionnaire WHERE view_hash = ?";
  48. if (!($stmt = $DB->prepare($sql))) {
  49. die("Prepare failed: (" . $DB->errno . ") " . $DB->error);
  50. }
  51. $stmt->bind_param('s', $hash);
  52. $stmt->execute();
  53. $stmt->store_result();
  54. $stmt->bind_result($qid, $title);
  55. if ($stmt->fetch()) {
  56. $stmt->free_result();
  57. $stmt->close();
  58. $DB->close();
  59. return ['qid' => $qid, 'title' => $title];
  60. }
  61. throw new Exception("User attempted to submit data to a non-existent questionnaire.");
  62. }
  63. function getQuestionaireInfoBySecret ($hash) {
  64. global $DB; connect();
  65. $sql = "SELECT id, title, view_hash, created_at FROM questionnaire WHERE edit_hash = ?";
  66. if (!($stmt = $DB->prepare($sql))) {
  67. die("Prepare failed: (" . $DB->errno . ") " . $DB->error);
  68. }
  69. $stmt->bind_param('s', $hash);
  70. $stmt->execute();
  71. $stmt->store_result();
  72. $stmt->bind_result($qid, $title,$view_hash, $date);
  73. if ($stmt->fetch()) {
  74. $stmt->free_result();
  75. $stmt->close();
  76. $DB->close();
  77. return ['qid' => $qid, 'title' => $title, 'view_hash'=>$view_hash, 'date'=>$date];
  78. }
  79. throw new Exception("User attempted to submit data to a non-existent questionnaire.");
  80. }
  81. function storeUserSubmission ($data, $qid, $uuid) {
  82. $id_p = get_id_by_uuid($uuid);
  83. if ($id_p >= 0) {
  84. updateUserRecord($id_p, $qid, $data);
  85. return;
  86. }
  87. $sql = "INSERT INTO answers (q_id, uuid, json)
  88. VALUES (?, ?, ?)";
  89. global $DB; connect();
  90. if (!($stmt = $DB->prepare($sql))) {
  91. die("Prepare failed: ($DB->errno) $DB->error");
  92. }
  93. if (!$stmt->bind_param("iss", $qid, $uuid, $data)) {
  94. die("Binding parameters failed: ($stmt->errno) $stmt->error");
  95. }
  96. if (!$stmt->execute()) {
  97. die("Execute failed: ($stmt->errno) $stmt->error");
  98. }
  99. $stmt->close();
  100. $DB->close();
  101. }
  102. function updateUserRecord ($id, $qid, $data) {
  103. global $DB; connect();
  104. $sql = "UPDATE answers SET json = ?
  105. WHERE id = ?";
  106. if (!($stmt = $DB->prepare($sql))) {
  107. die("Prepare failed: ($DB->errno) $DB->error");
  108. }
  109. if (!$stmt->bind_param("si", $data, $id)) {
  110. die("Binding parameters failed: ($stmt->errno) $stmt->error");
  111. }
  112. if (!$stmt->execute()) {
  113. die("Execute failed: ($stmt->errno) $stmt->error");
  114. }
  115. $stmt->close();
  116. $DB->close();
  117. }
  118. function storeNewForm ($form, $keys, $source) {
  119. global $DB; connect();
  120. $sql = "INSERT INTO questionnaire(email, title, description, thanks, view_hash, edit_hash, source) VALUES (?, ?, ?, ?, ?, ?, ?)";
  121. if (!($stmt = $DB->prepare($sql))) {
  122. die("Prepare failed: (" . $DB->errno . ") " . $DB->error);
  123. }
  124. if (!$stmt->bind_param("sssssss", $form['email'], $form['title'], $form['description'], $form['thanks'], $keys[1], $keys[0], $source)) {
  125. die("Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
  126. }
  127. if (!$stmt->execute()) {
  128. die("Execute failed: (" . $stmt->errno . ") " . $stmt->error);
  129. }
  130. $qid = $stmt->insert_id;
  131. $stmt->close();
  132. $DB->close();
  133. $questions = $form['questions'];
  134. foreach ($questions as $question) {
  135. if ($question->type === 'PAGEBREAK') continue;
  136. storeQuestionText($qid, $question);
  137. if (in_array($question->type, ['M','S'])) {
  138. storeQuestionMapping($qid, $question);
  139. }
  140. }
  141. }
  142. function storeQuestionText($qid, $question) {
  143. global $DB; connect();
  144. $sql = "INSERT INTO question_text(q_id, question, text) VALUES (?, ?, ?)";
  145. $number = substr($question->number, 1);
  146. if (!($stmt = $DB->prepare($sql))) {
  147. die("Prepare failed: ($DB->errno) $DB->error");
  148. }
  149. if (!$stmt->bind_param("iis", $qid, $number, $question->text)) {
  150. die("Binding parameters failed: ($stmt->errno) $stmt->error");
  151. }
  152. if (!$stmt->execute()) {
  153. die("Execute failed: ($stmt->errno) $stmt->error");
  154. }
  155. $stmt->close();
  156. $DB->close();
  157. }
  158. function storeQuestionMapping ($qid, $question) {
  159. global $DB; connect();
  160. $sql = "INSERT INTO question_mapping(q_id, question, value, text) VALUES (?, ?, ?, ?)";
  161. $number = substr($question->number, 1);
  162. if (!($stmt = $DB->prepare($sql))) {
  163. die("Prepare failed: (" . $DB->errno . ") " . $DB->error);
  164. }
  165. foreach ($question->choices as $choice) {
  166. if (!$stmt->bind_param("iiss", $qid, $number, $choice->value, $choice->text)) {
  167. die("Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
  168. }
  169. if (!$stmt->execute()) {
  170. die("Execute failed: (" . $stmt->errno . ") " . $stmt->error);
  171. }
  172. }
  173. $stmt->close();
  174. $DB->close();
  175. }
  176. function getFormThanks ($hash) {
  177. global $DB; connect();
  178. $sql = "SELECT title, thanks FROM questionnaire WHERE view_hash = ?";
  179. if (!($stmt = $DB->prepare($sql))) {
  180. die("Prepare failed: (" . $DB->errno . ") " . $DB->error);
  181. }
  182. if (!$stmt->bind_param("s", $hash)) {
  183. die("Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
  184. }
  185. if (!$stmt->execute()) {
  186. die("Execute failed: (" . $stmt->errno . ") " . $stmt->error);
  187. }
  188. $stmt->store_result();
  189. $stmt->bind_result($title, $thanks);
  190. if ($stmt->fetch()) {
  191. $result = ['title'=>$title, 'thanks'=>$thanks];
  192. $stmt->free_result();
  193. $stmt->close();
  194. $DB->close();
  195. return $result;
  196. }
  197. throw new Exception("User attempted to access data of a non-existent questionnaire.");
  198. }
  199. function checkFormAccess ($email, $secret) {
  200. global $DB; connect();
  201. $sql = "SELECT id FROM questionnaire WHERE edit_hash = ? AND email = ?";
  202. if (!($stmt = $DB->prepare($sql))) {
  203. die("Prepare failed: (" . $DB->errno . ") " . $DB->error);
  204. }
  205. if (!$stmt->bind_param("ss", $secret, $email)) {
  206. die("Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
  207. }
  208. if (!$stmt->execute()) {
  209. die("Execute failed: (" . $stmt->errno . ") " . $stmt->error);
  210. }
  211. $stmt->store_result();
  212. $stmt->bind_result($qid);
  213. $found = false;
  214. if ($stmt->fetch()) {
  215. $found = true;
  216. }
  217. $stmt->free_result();
  218. $stmt->close();
  219. $DB->close();
  220. return $found;
  221. }
  222. function get_user_form ($secret) {
  223. global $DB; connect();
  224. $sql = "SELECT id FROM questionnaire WHERE edit_hash = ?";
  225. if (!($stmt = $DB->prepare($sql))) {
  226. die("Prepare failed: (" . $DB->errno . ") " . $DB->error);
  227. }
  228. if (!$stmt->bind_param("s", $secret)) {
  229. die("Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
  230. }
  231. if (!$stmt->execute()) {
  232. die("Execute failed: (" . $stmt->errno . ") " . $stmt->error);
  233. }
  234. $stmt->store_result();
  235. $stmt->bind_result($qid);
  236. if ($stmt->fetch()) {
  237. $stmt->free_result();
  238. $stmt->close();
  239. $DB->close();
  240. return $qid;
  241. }
  242. die('User attempted to acess data from an unknown questionnaire!');
  243. }
  244. function get_total_rows ($form_id) {
  245. global $DB; connect();
  246. $sql = "SELECT count(id) FROM answers WHERE q_id = ?";
  247. if (!($stmt = $DB->prepare($sql))) {
  248. die("Prepare failed: (" . $DB->errno . ") " . $DB->error);
  249. }
  250. if (!$stmt->bind_param("i", $form_id)) {
  251. die("Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
  252. }
  253. if (!$stmt->execute()) {
  254. die("Execute failed: (" . $stmt->errno . ") " . $stmt->error);
  255. }
  256. $stmt->store_result();
  257. $stmt->bind_result($count);
  258. $stmt->fetch();
  259. $stmt->free_result();
  260. $stmt->close();
  261. $DB->close();
  262. return $count;
  263. }
  264. function get_records_pagination($form_id, $page, $no_of_records_per_page) {
  265. global $DB; connect();
  266. $sql = "SELECT id, json FROM answers WHERE q_id = ? LIMIT ?, ?";
  267. if (!($stmt = $DB->prepare($sql))) {
  268. die("Prepare failed: (" . $DB->errno . ") " . $DB->error);
  269. }
  270. if (!$stmt->bind_param("iii", $form_id, $page,$no_of_records_per_page)) {
  271. die("Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
  272. }
  273. if (!$stmt->execute()) {
  274. die("Execute failed: (" . $stmt->errno . ") " . $stmt->error);
  275. }
  276. $stmt->store_result();
  277. $stmt->bind_result($id, $json);
  278. $all_data = array();
  279. while ($stmt->fetch()) {
  280. $data = json_decode($json);
  281. $data->id = $id;
  282. $all_data[] = $data;
  283. }
  284. $stmt->free_result();
  285. $stmt->close();
  286. $DB->close();
  287. return $all_data;
  288. }
  289. function loadQuestionsMapping ($qid) {
  290. global $DB; connect();
  291. $sql = "SELECT question, value, text FROM question_mapping WHERE q_id = ?";
  292. if (!($stmt = $DB->prepare($sql))) {
  293. die("Prepare failed: (" . $DB->errno . ") " . $DB->error);
  294. }
  295. $stmt->bind_param('i', $qid);
  296. $stmt->execute();
  297. $stmt->store_result();
  298. $result = array();
  299. $stmt->bind_result($number, $value, $text);
  300. while ($stmt->fetch()){
  301. $question = new stdClass;
  302. $question->number = $number;
  303. $question->value = $value;
  304. $question->text = $text;
  305. $result[] = $question;
  306. }
  307. $stmt->free_result();
  308. $stmt->close();
  309. $DB->close();
  310. return $result;
  311. }
  312. function loadQuestionsText ($qid) {
  313. global $DB; connect();
  314. $sql = "SELECT question, text FROM question_text WHERE q_id = ?";
  315. if (!($stmt = $DB->prepare($sql))) {
  316. die("Prepare failed: (" . $DB->errno . ") " . $DB->error);
  317. }
  318. $stmt->bind_param('i', $qid);
  319. $stmt->execute();
  320. $stmt->store_result();
  321. $result = array();
  322. $stmt->bind_result($number, $text);
  323. while ($stmt->fetch()){
  324. $question = new stdClass;
  325. $question->number = $number;
  326. $question->text = $text;
  327. $result[] = $question;
  328. }
  329. $stmt->free_result();
  330. $stmt->close();
  331. $DB->close();
  332. return $result;
  333. }
  334. ?>