Copyright (C) 2005 James Grant This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ ?> questions_id] = $r->answer; } return $ans; } function questions_load_questions($section, $year) { $q = mysql_query('SELECT * FROM questions '. "WHERE year='$year' ". " AND section='$section' ". 'ORDER BY ord ASC'); print(mysql_error()); $qs = array(); while($r=mysql_fetch_object($q)) { $qs[$r->id]['id'] = $r->id; $qs[$r->id]['ord'] = $r->ord; $qs[$r->id]['section'] = $r->section; $qs[$r->id]['db_heading'] = $r->db_heading; $qs[$r->id]['type'] = $r->type; $qs[$r->id]['required'] = $r->required; $qs[$r->id]['question'] = $r->question; } return $qs; } function questions_save_answers($section, $id, $year, $answers) { $qs = questions_load_questions($section, $year); $keys = array_keys($answers); $query = "DELETE FROM question_answers ". "WHERE registrations_id='$id' ". " AND year='$year' AND ("; $n = 0; foreach($keys as $qid) { if($n == 1) $query .= " OR "; $n = 1; $query .= " questions_id='$qid'"; } $query .= ')'; mysql_query($query); $keys = array_keys($answers); foreach($keys as $qid) { /* Poll key */ mysql_query("INSERT INTO question_answers ". "(year,registrations_id,questions_id,answer) VALUES(". "'$year','$id','$qid',". "'".mysql_escape_string($answers[$qid])."'". ")" ); } } function questions_find_question_id($section, $year, $dbheading) { $q = mysql_query("SELECT id FROM questions WHERE ". " section='$section' ". " AND year='$year' ". " AND db_heading='$dbheading' "); if(mysql_num_rows($q) == 1) { $r = mysql_fetch_object($q); return $r->id; } return 0; } function questions_print_answer_editor($section, $id, $year, $array_name) { $ans = questions_load_answers($section, $id, $year); $qs = questions_load_questions($section, $year); $keys = array_keys($qs); foreach($keys as $qid) { print("\n"); print(" ".i18n($qs[$qid]['question'])."\n"); print(" "); $iname = "{$array_name}[{$qid}]"; switch($qs[$qid]['type']) { case 'yesno': if($ans[$qid]=="yes") $ch="checked=\"checked\""; else $ch=""; print("".i18n("Yes")); print("    "); if($ans[$qid]=="no") $ch="checked=\"checked\""; else $ch=""; print("".i18n("No")); break; case 'int': print("\n"); break; } print("\n"); print("\n"); } } function questions_print_answers($section, $id, $year) { $ans = questions_load_answers($section, $id, $year); $qs = questions_load_questions($section, $year); $keys = array_keys($qs); foreach($keys as $qid) { echo "\n"; echo " ".i18n($qs[$qid]['question'])."\n"; echo " {$ans[$qid]}"; echo "\n"; } } function questions_parse_from_http_headers($array_name) { $ans = array(); if(!is_array($_POST[$array_name])) return $ans; $keys = array_keys($_POST[$array_name]); foreach($keys as $qid) { $ans[$qid] = stripslashes($_POST[$array_name][$qid]); } return $ans; } function questions_update_question($qs, $year) { mysql_query("UPDATE questions SET `question`='".mysql_escape_string($qs['question'])."', `type`='".mysql_escape_string($qs['type'])."', `db_heading`='".mysql_escape_string($qs['db_heading'])."', `required`='".mysql_escape_string($qs['required'])."', `ord`=".intval($qs['ord'])." WHERE id='{$qs['id']}' AND year='$year'"); echo mysql_error(); } function questions_save_new_question($qs, $year) { mysql_query("INSERT INTO questions ". "(question,type,section,db_heading,required,ord,year) VALUES (". "'".mysql_escape_string($qs['question'])."',". "'".mysql_escape_string($qs['type'])."',". "'".mysql_escape_string($qs['section'])."',". "'".mysql_escape_string($qs['db_heading'])."',". "'".mysql_escape_string($qs['required'])."',". "'".mysql_escape_string($qs['ord'])."',". "'$year' )"); echo mysql_error(); } /* A complete question editor. Just call it with the * section you want to edit, a year, the array_name to use for * POSTing and GETting the questions (so you can put more than one * edtior on a single page), and give it $_SERVER['PHP_SELF'], because * php_self inside this function is this file. * FUTURE WORK: it would be nice to hide the order, and just implement * a bunch of up/down arrows, and dynamically compute the order for * all elements */ function questions_editor($section, $year, $array_name, $self) { global $config; if($_POST['action']=="save") { $qs = questions_parse_from_http_headers('question'); $qs['section'] = $section; if($qs['question']) { $qs['id'] = intval($_POST['save']); questions_update_question($qs, $year); echo happy(i18n("Question successfully saved")); } else { echo error(i18n("Question is required")); } } if($_POST['action']=="new") { $q = questions_load_questions($section, $year); $qs = questions_parse_from_http_headers('question'); $qs['section'] = $section; $qs['ord'] = count($q) + 1; if($qs['question']) { questions_save_new_question($qs, $year); echo happy(i18n("Question successfully added")); } else { echo error(i18n("Question is required")); } } if($_GET['action']=="remove" && $_GET['remove']) { $qid = $_GET['remove']; $qs = questions_load_questions($section, $year); /* Delete this question */ mysql_query("DELETE FROM questions WHERE id='$qid'"); /* Update the order of all questions after this one */ $keys = array_keys($qs); foreach($keys as $q) { if($q == $qid) continue; if($qs[$q]['ord'] > $qs[$qid]['ord']) { $qs[$q]['ord']--; mysql_query("UPDATE questions SET ord='{$qs[$q]['ord']}' WHERE id='$q'"); } } echo happy(i18n("Question successfully removed")); } if($_GET['action']=="import" && $_GET['impyear']) { $x=0; $q = mysql_query("SELECT * FROM questions WHERE year='{$_GET['impyear']}'"); while($r=mysql_fetch_object($q)) { $x++; mysql_query("INSERT INTO questions (id,year,section,db_heading,question,type,required,ord) VALUES ( '', '$year', '".mysql_escape_string($r->section)."', '".mysql_escape_string($r->db_heading)."', '".mysql_escape_string($r->question)."', '".mysql_escape_string($r->type)."', '".mysql_escape_string($r->required)."', '".mysql_escape_string($r->ord)."')"); } echo happy(i18n("%1 question(s) successfully imported", array($x))); } /* Load questions, then handle up and down, because with up and down we * have to modify 2 questions to maintain the order */ $qs = questions_load_questions($section, $year); /* Sanity check the order DG -- This is there to fix a bug in the .22 * database full import, anyone with a fresh install will see duplicate * order items, anyone who doesn't, won't see this bug. Anyone who has * already used the default question import will need this piece of * code to return their system to sane. I have added a db.update * script to fix the bug, but it won't fix any systems where the * questions have already been imported. This code will. */ $keys = array_keys($qs); $x=1; foreach($keys as $qid) { if($qs[$qid]['ord'] != $x) { $qs[$qid]['ord'] = $x; questions_update_question($qs[$qid], $year); } $x++; } $qdir = 0; if($_GET['action']=="up" && $_GET['up']) { $qid = $_GET['up']; if($qs[$qid]['ord'] != 1) { $qdir = -1; } } if($_GET['action']=="down" && $_GET['down']) { $qid = $_GET['down']; if($qs[$qid]['ord'] != count($qs)) { $qdir = 1; } } if($qdir != 0) { $qs[$qid]['ord'] += $qdir; /* Update the db */ mysql_query("UPDATE questions SET ord='{$qs[$qid]['ord']}' WHERE id='$qid'"); $keys = array_keys($qs); $originalq = $qs[$qid]; foreach($keys as $q) { if($q == $qid) continue; if($qs[$q]['ord'] != $qs[$qid]['ord']) continue; if($qdir == 1) { $qs[$q]['ord']--; mysql_query("UPDATE questions SET ord='{$qs[$q]['ord']}' WHERE id='$q'"); } else { $qs[$q]['ord']++; mysql_query("UPDATE questions SET ord='{$qs[$q]['ord']}' WHERE id='$q'"); } /* Swap them so we don' thave to reaload the questions * */ // $qs[$qid] = $qs[$q]; // $qs[$q] = $originalq; break; } /* Reload the questions */ $qs = questions_load_questions($section, $year); } if(($_GET['action']=="edit" && $_GET['edit']) || $_GET['action']=="new") { $showform=true; echo "
"; if($_GET['action']=="new") { $buttontext="Add a question"; echo "\n"; } else if($_GET['action']=="edit") { $buttontext="Save question"; echo "\n"; /* The question ID is passed on the URL */ $qid = $_GET['edit']; /* Load the question */ $q = $qs[$qid]; echo "\n"; if(!is_array($q)) { $showform=false; echo error(i18n("Invalid question")); } } if($showform) { echo ""; echo ""; echo ""; echo ""; echo ""; // echo ""; echo ""; echo "
".i18n("Question").""; echo "\n"; echo "
".i18n("Table Heading").""; echo "\n"; echo "
".i18n("Type").""; echo ""; echo "
".i18n("Required?").""; echo ""; echo "
".i18n("Display Order").""; // echo "\n"; // echo "
"; echo "\n"; echo "
"; echo "
"; echo "
"; echo "
"; } } else { } echo "
"; echo "".i18n("Add a new question").""; echo ""; echo "". "". "". "". "". ""; $keys = array_keys($qs); $types = array( 'check' => i18n("Check box"), 'yesno' => i18n("Yes/No"), 'text' => i18n("Text"), 'int' => i18n("Number") ); foreach($keys as $qid) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
".i18n("Question")."".i18n("Table Heading")."".i18n("Type")."".i18n("Required")."".i18n("Actions")."
{$qs[$qid]['ord']}{$qs[$qid]['question']}{$qs[$qid]['db_heading']}{$types[$qs[$qid]['type']]}{$qs[$qid]['required']}"; echo ""; echo "  "; echo ""; echo "  "; echo ""; echo "  "; echo ""; echo "
"; if(count($keys) == 0) { $default_qs = questions_load_questions($section, -1); if(count($default_qs) != 0) { print("
"); print(i18n("There are no questions for year %1, but there are %2 default questions. To import the default questions to year %1 click on the link below.", array($year, count($default_qs)))); print("
"); print("".i18n("Import default questions")."
"); } } } ?>