forms.php 11 KB

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