forms.php 11 KB

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