Open source survey management
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

270 lines
6.3 KiB

  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5. require_once("moduleClass.php");
  6. class OpenForm
  7. {
  8. private $modules = array();
  9. //MySQL
  10. public $conn = null;
  11. public $table = "";
  12. public $id = "";
  13. private function start($title, $subtext)
  14. {
  15. echo "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\"><title>$title</title></head><body><form method=\"POST\">";
  16. echo "<input type=\"hidden\" name=\"submitted\" value=\"7E\">"; //Easy way to check if page was loaded with survey data in $_POST or not
  17. echo "<div id=\"container\"><h1>$title</h1><p>$subtext</p>";
  18. //Ready for <div templates>
  19. }
  20. private function end()
  21. {
  22. echo "<hr><input type=\"submit\" value=\"Submit\" />"; //TODO: "Submit" should be customizable
  23. echo "</div></form></body></html>";
  24. }
  25. //Return values:
  26. //0 - Survey generated
  27. //1 - Results put in db
  28. //2 - Error saving results
  29. public function create($title, $subtext)
  30. {
  31. if(isset($_POST['submitted']) && $_POST['submitted'] = "7E")
  32. {
  33. //Make sure that the received fields match what we expect in $methods
  34. $names = array();
  35. foreach ($this->modules as $module)
  36. {
  37. if ($module->type != ModuleType::RANKING)
  38. {
  39. if (strlen($module->name) != 0) //!isNullOrEmpty
  40. {
  41. $names[] = $module->name;
  42. }
  43. }
  44. else
  45. {
  46. foreach($module->options as $r_name=>$option)
  47. {
  48. $names[] = $r_name;
  49. }
  50. }
  51. }
  52. $data = array();
  53. foreach ($names as $name)
  54. {
  55. $data[$name] = $_POST[$name];
  56. }
  57. //Time to put it into the database
  58. //The following table structure is expected:
  59. //survey_id - varchar - the id of this survey(So that multiple surveys can use one table)
  60. //response_id - varchar - the id of the response (So that it's possible to tell which responses were submitted together) TODO: Add check to make sure it isn't generated the same for 2 responses
  61. //name - varchar - the name of the field
  62. //value - text - value of the field. If an array, it will be imploded.
  63. //timestamp - int - the timestamp when the result was recorded
  64. if ($this->conn->connect_errno || $this->table == "")
  65. {
  66. return 2;
  67. }
  68. $response_id = bin2hex(openssl_random_pseudo_bytes(32));
  69. $stmt = $this->conn->prepare("INSERT INTO $this->table (survey_id, response_id, name, value, timestamp) VALUES (?, ?, ?, ?, UNIX_TIMESTAMP())");
  70. if (!$stmt)
  71. {
  72. die("Error creating prepared statement");
  73. }
  74. $bind_stat = $stmt->bind_param("ssss", $this->id, $response_id, $name, $value);
  75. if (!$bind_stat)
  76. {
  77. die("Error binding variables to prepared statement");
  78. }
  79. foreach($names as $name)
  80. {
  81. //Name -> $name
  82. //Value -> $_POST[$name]
  83. $value = $_POST[$name];
  84. //Instead, this should probably insert multiple rows with the same name and different values for arrays.
  85. if (is_array($value))
  86. {
  87. $value = implode(",", $value);
  88. }
  89. $exe_stat = $stmt->execute();
  90. if(!$exe_stat)
  91. {
  92. $stmt->close();
  93. $this->conn->close();
  94. return 2;
  95. }
  96. }
  97. $stmt->close();
  98. $this->conn->close();
  99. return 1;
  100. }
  101. else
  102. {
  103. $this->start($title, $subtext);
  104. foreach($this->modules as $module)
  105. {
  106. //var_dump($module);
  107. switch($module->type)
  108. {
  109. case ModuleType::RADIO:
  110. $this->gen_recurring($module, 'radio');
  111. break;
  112. case ModuleType::CHECKBOX:
  113. $this->gen_recurring($module, 'checkbox');
  114. break;
  115. case ModuleType::NUM_INPUT:
  116. $this->gen_num_input($module);
  117. break;
  118. case ModuleType::RANKING:
  119. $this->gen_ranking($module);
  120. break;
  121. case ModuleType::DROPDOWN:
  122. $this->gen_dropdown($module);
  123. break;
  124. case ModuleType::TEXTBOX:
  125. $this->gen_textbox($module);
  126. break;
  127. case ModuleType::TEXTAREA:
  128. $this->gen_textarea($module);
  129. }
  130. }
  131. $this->end();
  132. return 0;
  133. }
  134. }
  135. //TODO: Add default values
  136. public function add_module($title, $subtext, $name, $type, $options, $rankings, $unit)
  137. {
  138. $m = new Module($title, $subtext, $name, $options, $rankings, $type, $unit);
  139. $this->modules[] = $m;
  140. }
  141. private function gen_recurring($m, $type)
  142. {
  143. echo "<hr><div class=\"template\">";
  144. echo "<h2>$m->title</h2>";
  145. echo "<p>$m->subtext</p>";
  146. $checkboxBr = "";
  147. if ($m->type == ModuleType::CHECKBOX)
  148. {
  149. $checkboxBr = "[]";
  150. }
  151. foreach ($m->options as $option => $value)
  152. {
  153. echo "<div><label>$option</label><input type=\"$type\" name=\"$m->name$checkboxBr\" value=\"$value\"/></div>";
  154. }
  155. echo "</div>";
  156. }
  157. function gen_num_input($m) //TODO: Min/max/default values
  158. {
  159. echo "<hr><div class=\"template\">";
  160. echo "<h2>$m->title</h2>";
  161. echo "<p>$m->subtext</p>";
  162. echo "<input type=\"number\" class=\"number other\" name=\"$m->name\"/> $m->unit";
  163. echo "</div>";
  164. }
  165. function gen_ranking($m) //Rankings go along the top, options go along the side
  166. {
  167. echo "<hr><div class=\"template\">";
  168. echo "<h2>$m->title</h2>";
  169. echo "<p>$m->subtext</p>";
  170. echo "<table>";
  171. //Generate top bar
  172. echo "<tr><th></th>";
  173. foreach($m->rankings as $ranking)
  174. {
  175. echo "<th>$ranking</th>";
  176. }
  177. echo "</tr>";
  178. //Generate options
  179. foreach($m->options as $op_name => $op_val)
  180. {
  181. echo "<tr>";
  182. echo "<td>$op_val</td>";
  183. foreach($m->rankings as $r_name=>$ranking)
  184. {
  185. echo "<td><input class=\"except\" type=\"radio\" name=\"$op_name\" value=\"$r_name\" /></td>";
  186. }
  187. echo "</tr>";
  188. }
  189. echo "</table></div>";
  190. }
  191. function gen_dropdown($m)
  192. {
  193. echo "<hr><div class=\"template\">";
  194. echo "<h2>$m->title</h2>";
  195. echo "<p>$m->subtext</p>";
  196. echo "<select name=\"$m->name\">";
  197. echo "<option value=\"\" disabled=\"disabled\" selected=\"selected\">Please select</option>";
  198. foreach($m->options as $value=>$name)
  199. {
  200. echo "<option value=\"$value\">$name</option>";
  201. }
  202. echo "</select></div>";
  203. }
  204. function gen_textbox($m)
  205. {
  206. echo "<hr><div class=\"template\">";
  207. echo "<h2>$m->title</h2>";
  208. echo "<p>$m->subtext</p>";
  209. echo "<input type=\"text\" class=\"except\" style=\"margin: 5px;\" name=\"$m->name\" />";
  210. echo "</div>";
  211. }
  212. function gen_textarea($m)
  213. {
  214. echo "<hr><div class=\"template\">";
  215. echo "<h2>$m->title</h2>";
  216. echo "<p>$m->subtext</p>";
  217. echo "<textarea class=\"except\" style=\"width: 100%;\" name=\"$m->name\"></textarea>";
  218. echo "</div>";
  219. }
  220. }
  221. ?>