Open source survey management

survey_func.php 6.3KB

    <?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); require_once("moduleClass.php"); class OpenForm { private $modules = array(); //MySQL public $conn = null; public $table = ""; public $id = ""; private function start($title, $subtext) { echo "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\"><title>$title</title></head><body><form method=\"POST\">"; echo "<input type=\"hidden\" name=\"submitted\" value=\"7E\">"; //Easy way to check if page was loaded with survey data in $_POST or not echo "<div id=\"container\"><h1>$title</h1><p>$subtext</p>"; //Ready for <div templates> } private function end() { echo "<hr><input type=\"submit\" value=\"Submit\" />"; //TODO: "Submit" should be customizable echo "</div></form></body></html>"; } //Return values: //0 - Survey generated //1 - Results put in db //2 - Error saving results public function create($title, $subtext) { if(isset($_POST['submitted']) && $_POST['submitted'] = "7E") { //Make sure that the received fields match what we expect in $methods $names = array(); foreach ($this->modules as $module) { if ($module->type != ModuleType::RANKING) { if (strlen($module->name) != 0) //!isNullOrEmpty { $names[] = $module->name; } } else { foreach($module->options as $r_name=>$option) { $names[] = $r_name; } } } $data = array(); foreach ($names as $name) { $data[$name] = $_POST[$name]; } //Time to put it into the database //The following table structure is expected: //survey_id - varchar - the id of this survey(So that multiple surveys can use one table) //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 //name - varchar - the name of the field //value - text - value of the field. If an array, it will be imploded. //timestamp - int - the timestamp when the result was recorded if ($this->conn->connect_errno || $this->table == "") { return 2; } $response_id = bin2hex(openssl_random_pseudo_bytes(32)); $stmt = $this->conn->prepare("INSERT INTO $this->table (survey_id, response_id, name, value, timestamp) VALUES (?, ?, ?, ?, UNIX_TIMESTAMP())"); if (!$stmt) { die("Error creating prepared statement"); } $bind_stat = $stmt->bind_param("ssss", $this->id, $response_id, $name, $value); if (!$bind_stat) { die("Error binding variables to prepared statement"); } foreach($names as $name) { //Name -> $name //Value -> $_POST[$name] $value = $_POST[$name]; //Instead, this should probably insert multiple rows with the same name and different values for arrays. if (is_array($value)) { $value = implode(",", $value); } $exe_stat = $stmt->execute(); if(!$exe_stat) { $stmt->close(); $this->conn->close(); return 2; } } $stmt->close(); $this->conn->close(); return 1; } else { $this->start($title, $subtext); foreach($this->modules as $module) { //var_dump($module); switch($module->type) { case ModuleType::RADIO: $this->gen_recurring($module, 'radio'); break; case ModuleType::CHECKBOX: $this->gen_recurring($module, 'checkbox'); break; case ModuleType::NUM_INPUT: $this->gen_num_input($module); break; case ModuleType::RANKING: $this->gen_ranking($module); break; case ModuleType::DROPDOWN: $this->gen_dropdown($module); break; case ModuleType::TEXTBOX: $this->gen_textbox($module); break; case ModuleType::TEXTAREA: $this->gen_textarea($module); } } $this->end(); return 0; } } //TODO: Add default values public function add_module($title, $subtext, $name, $type, $options, $rankings, $unit) { $m = new Module($title, $subtext, $name, $options, $rankings, $type, $unit); $this->modules[] = $m; } private function gen_recurring($m, $type) { echo "<hr><div class=\"template\">"; echo "<h2>$m->title</h2>"; echo "<p>$m->subtext</p>"; $checkboxBr = ""; if ($m->type == ModuleType::CHECKBOX) { $checkboxBr = "[]"; } foreach ($m->options as $option => $value) { echo "<div><label>$option</label><input type=\"$type\" name=\"$m->name$checkboxBr\" value=\"$value\"/></div>"; } echo "</div>"; } function gen_num_input($m) //TODO: Min/max/default values { echo "<hr><div class=\"template\">"; echo "<h2>$m->title</h2>"; echo "<p>$m->subtext</p>"; echo "<input type=\"number\" class=\"number other\" name=\"$m->name\"/> $m->unit"; echo "</div>"; } function gen_ranking($m) //Rankings go along the top, options go along the side { echo "<hr><div class=\"template\">"; echo "<h2>$m->title</h2>"; echo "<p>$m->subtext</p>"; echo "<table>"; //Generate top bar echo "<tr><th></th>"; foreach($m->rankings as $ranking) { echo "<th>$ranking</th>"; } echo "</tr>"; //Generate options foreach($m->options as $op_name => $op_val) { echo "<tr>"; echo "<td>$op_val</td>"; foreach($m->rankings as $r_name=>$ranking) { echo "<td><input class=\"except\" type=\"radio\" name=\"$op_name\" value=\"$r_name\" /></td>"; } echo "</tr>"; } echo "</table></div>"; } function gen_dropdown($m) { echo "<hr><div class=\"template\">"; echo "<h2>$m->title</h2>"; echo "<p>$m->subtext</p>"; echo "<select name=\"$m->name\">"; echo "<option value=\"\" disabled=\"disabled\" selected=\"selected\">Please select</option>"; foreach($m->options as $value=>$name) { echo "<option value=\"$value\">$name</option>"; } echo "</select></div>"; } function gen_textbox($m) { echo "<hr><div class=\"template\">"; echo "<h2>$m->title</h2>"; echo "<p>$m->subtext</p>"; echo "<input type=\"text\" class=\"except\" style=\"margin: 5px;\" name=\"$m->name\" />"; echo "</div>"; } function gen_textarea($m) { echo "<hr><div class=\"template\">"; echo "<h2>$m->title</h2>"; echo "<p>$m->subtext</p>"; echo "<textarea class=\"except\" style=\"width: 100%;\" name=\"$m->name\"></textarea>"; echo "</div>"; } } ?>