<?php
class vote extends plug {
// Bezeichnung des Plugs
var $label = "Abstimmung";
var $cache_output = FALSE;
var $form_notice = "Trennen Sie die einzelnen Antwortoptionen durch ein Semikolon.
<br />ACHTUNG: BEI JEDER ÄNDERUNG WERDEN BISHERIGE ABSTIMMUNGSERGEBNISSE GELÖSCHT!";
var $form = array (
"main" => array (
"property" => array("question", "options")
)
);
// Parameter die in Datenbank gespeichert werden
// save_as kann Werte 'none', 'number' und 'string' annehmen
var $property = array (
"id" => array("type" => "INT NOT NULL AUTO_INCREMENT PRIMARY KEY", "save_as" => "none", "label" => ""),
"question" => array("type" => "TEXT", "save_as" => "string", "label" => "Frage"),
"options" => array("type" => "TEXT", "save_as" => "string", "label" => "Auswahlmöglichkeiten")
);
function output($content_id, $content_list, $mode) {
global $vote_id;
$values = $this->get_content_params($content_id);
// Ergebnisse anzeigen
//if ($mode == SINGLE_CENTER)
if (!empty($_POST['submit_vote'])) return $this->insert_result($_POST['x_content']);
if (!empty($_POST['view_vote'])) return $this->output_result($_POST['x_content']);
$str ="";
$values = $this->get_content_params($content_id);
$instance_id = $values['instance_id'];
$options = explode(";", $values['options']);
// Ergebnis in Randspalte anzeigen?
if ($content_list->margin_column) {
$str .= "<form name=\"vote_$instance_id\" method=\"post\" action=\"index.php?page_id=".$page_id."\"><fieldset>";
for ($i = 1; $i <= count($options); $i++) {
if ($option = trim($options[($i - 1)])) {
$str .= "<input type=\"radio\" id=\"vote_$i\" name=\"vote_choice\" value=\"$i\" /><label for=\"vote_$i\">$option</label><br />\n";
}
}
$str .= "</fieldset>\n<input type=\"hidden\" name=\"x_content\" value=\"$content_id," . SINGLE_CENTER . "\" />\n";
$str .= "<input type=\"submit\" name=\"submit\" value=\"abstimmen\" />\n <input type=\"submit\" name=\"view\" value=\"ansehen\" />";
$str .= "</form>\n";
$content = $values['question'].$str;
}
// Abstimmung anzeigen
$str .= "<form id=\"vote_$instance_id\" method=\"post\" action=\"\"><fieldset>";
$str .= "<legend for='vote_$instance_id'>" . $values['question'] . "</legend>";
for ($i = 1; $i <= count($options); $i++) {
if ($option = trim($options[($i - 1)])) {
$str .= "<p><input type=\"radio\" name=\"vote_choice\" value=\"$i\" />$option</p>";
}
}
$str .= "<input type=\"hidden\" name=\"x_content\" value=\"$content_id\" />";
$str .= "<input class=\"voteButton\" type=\"submit\" name=\"submit_vote\" value=\"abstimmen\" />";
$str .= "<input class=\"voteButton\" type=\"submit\" name=\"view_vote\" value=\"ansehen\" />";
$str .= "</fieldset></form>";
return $str;
}
function insert_result($content_id){
global $database, $paths, $vote_choice,$core,$page_id;
// Parameter incl. Optionen auslesen
$values = $this->get_content_params($content_id);
$vote_id = $values['instance_id'];
$options = explode(";", $values['options']);
extract($_POST);
// Aktuelle Stimme zählen
$core->query("INSERT INTO " . $database["prefix"] . "_vote_choices (choice, vote_id) VALUES ($vote_choice, $vote_id)");
$str = "<div id=\"vote\"><h1>Ihre Stimme wurde gezählt</h1><p>Vielen Dank für Ihre Wertung. Ihre Stimme wurde gespeichert.</p>";
$str .= "<p><a href=\"index.php?page_id=$page_id\">Weiter</a></p></div>";
return $str;
}
function output_result($content_id) {
global $database, $paths, $vote_choice,$core;
// Parameter incl. Optionen auslesen
$values = $this->get_content_params($content_id);
$vote_id = $values['instance_id'];
$options = explode(";", $values['options']);
// Auswertung des Ergebnisses
$cnt_total_choices = $core->sv_query("SELECT COUNT(*) FROM $database[prefix]" . "_vote_choices WHERE vote_id=$vote_id");
if (!empty($cnt_total_choices) && $cnt_total_choices >0) {
for ($i = 0; $i < count($options); $i++) {
if ($options[$i] = trim($options[$i])) {
$count[$i] = $core->sv_query("SELECT COUNT(*) FROM $database[prefix]" . "_vote_choices WHERE choice=" . ($i + 1) . " AND vote_id=$vote_id");
$percent[$i] = number_format(($count[$i] * 100) / $cnt_total_choices, 2);
$width[$i] = (int) ($count[$i] * 350) / $cnt_total_choices;
}
}
}
$options_index = $vote_choice - 1;
$str = "<div id=\"vote\"><h1>".$values['question']."</h1>";
if (!$cnt_total_choices) {
$str .= "<p>Es liegen noch keine Abstimmungsergebnisse vor.</p>";
}
else {
$str .= "<h2>Abstimmungsergebnis:</h2>";
for ($i = 0; $i < count($options); $i++) {
if ($options[$i] = trim($options[$i])) {
$str .= "<p style=\"font-size:9pt;\">$options[$i]: $percent[$i]% - ( $count[$i] )</p>";
$str .= "<p><img src=\"img_si/1x1.gif\" width=\"$width[$i]\" alt=\"Anzahl: ".$count[$i]."\" height=\"10\" class=\"vote_bar\" /></p>";
}
}
}
return $str . "</div>";
}
function show_form_element($property_name, &$value, $form_id = 0) {
if ($property_name == "question") {
$str = "<span class=\"controlHeader\">Frage</span><br />";
$str .= "<textarea name=\"question\" cols=\"68\" rows=\"2\">$value</textarea><br /><br />\n";
return $str;
}
else return plug::show_form_element($property_name, $value, $form_id);
}
function update(&$POST_VARS, $instance_id, $form_id = 0) {
global $database;
plug::update($POST_VARS, $instance_id, $form_id);
// Alle bisherigen Abstimmungsergebnisse löschen
$core->query("DELETE FROM " . $database["prefix"] . "_vote_choices WHERE vote_id=$instance_id");
}
function register() {
global $database,$core;
// Basisregistrierung durchführen
plug::register();
// Zusätzliche Registrierung: Tabelle mit Abstimmungsergebnissen
// echo "<h2>Plug $this->label wird zusätzlich registriert. (von Objekt vote)</h2>";
$core->query("DROP TABLE IF EXISTS " . $database["prefix"] . "_vote_choices");
$sql = "CREATE TABLE " . $database["prefix"] . "_vote_choices
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, choice INT, vote_id INT)";
$core->query($sql);
}
function output_javascript() {
return;
}
}
?>