+2004-02-19 Sung Kim <hunkim@cs.ucsc.edu>
+ * Rewrite the Survey module
+ * Added a general graph module for Survey results
+
2004-02-19 Guillaume Smet <guillaume-gforge@smet.org>
* merged [ #690 ] by Benoît Sibaud with French.tab cvs version
* improved localization of task manager
* fixed ShowResultSet function
+
+ * merged [ #690 ] by Benoît Sibaud with French.tab cvs version
+ * updated french localization again
+ * improved localization of task manager
+ * fixed ShowResultSet function
+
2004-02-18 Guillaume Smet <guillaume-gforge@smet.org>
* updated french localization
--- /dev/null
+<?php
+/**
+ * GForge Survey Question Facility
+ *
+ * Copyright 2004 GForge, LLC
+ * http://gforge.org/
+ *
+ * This file is part of GForge.
+ *
+ * GForge 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GForge 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 GForge; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/*
+ Survery
+ By Sung Kim 2004/2/13
+*/
+
+require_once('common/include/Error.class');
+require_once('common/survey/SurveyQuestionFactory.class');
+
+class Survey extends Error {
+
+ /**
+ * Associative array of data from db.
+ *
+ * @var array $data_array.
+ */
+ var $data_array;
+
+ /**
+ * Questions array in this survey
+ *
+ * @var array $question_array.
+ */
+ var $all_question_array;
+
+ /**
+ * The Group object.
+ *
+ * @var object $Group.
+ */
+ var $Group;
+
+ /**
+ * Constructor.
+ *
+ * @param object The Group object to which this servey is associated.
+ * @param int The servey_id.
+ * @param array The associative array of data.
+ * @return boolean success.
+ */
+ function Survey(&$Group, $survey_id=false, $arr=false) {
+ global $Language;
+ $this->Error();
+ if (!$Group || !is_object($Group)) {
+ $this->setError($Language->getText('general','error_no_valid_group_object','Survey'));
+ return false;
+ }
+ if ($Group->isError()) {
+ $this->setError('Survey:: '.$Group->getErrorMessage());
+ return false;
+ }
+ $this->Group =& $Group;
+
+ if ($survey_id) {
+ if (!$arr || !is_array($arr)) {
+ if (!$this->fetchData($survey_id)) {
+ return false;
+ }
+ } else {
+ $this->data_array =& $arr;
+ if ($this->data_array['group_id'] != $this->Group->getID()) {
+ $this->setError($Language->getText('general','error_group_id'));
+ $this->data_array = null;
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ /**
+ * create - use this function to create a survey
+ *
+ * @param string The survey title
+ * @param int array The question numbers to be added
+ * @param is_active 1: Active, 0: Inactive
+ * For future options
+ * @param is_public 0: Admins Only, 1: Group Members, 2: Gforge user, 3:Every body
+ * @param is_result_public 0: Admins Only, 1: Group Members, 2: Gforge user, 3:voted user 4:Every body
+ * @param double_vote Allow dobule vote if it is 1
+ * @return boolean success.
+ */
+ function create($survey_title, $add_questions, $is_active=0, $is_public=1, $is_result_public=0, $double_vote=0) {
+ global $Language;
+
+ if (!$survey_title) {
+ $this->setError($Language->getText('survey_edit','survey_title_required'));
+ return false;
+ /* We need at least one survey question at this point */
+ } else if (!$add_questions || !is_array($add_questions) || count($add_questions)<1) {
+ $this->setError($Language->getText('survey_edit','survey_question_required'));
+ return false;
+ }
+
+ $group_id = $this->Group->GetID();
+
+ /* Make old style survey string from array: 1, 2, 3, ..., n */
+ $survey_questions = $this->_makeQuestionString($add_questions);
+
+ $sql="insert into surveys (survey_title,group_id,survey_questions) values ('".htmlspecialchars($survey_title)."','$group_id','$survey_questions')";
+
+ $result=db_query($sql);
+ if (!$result) {
+ $this->setError($Language->getText('survey_add_question','error_in_insert').db_error());
+ return false;
+ }
+
+ /* Load question to data array */
+ $survey_id=db_insertid($res,'surveys','survey_id');
+ return $this->fetchData($survey_id);
+ }
+
+
+
+ /**
+ * update - use this function to update a survey
+ *
+ * @param string The survey title
+ * @param int array The question numbers to be added
+ * @param int array The question numbers to be deleted
+ * @param is_active 1: Active, 0: Inactive
+ * @param is_public 0: Admins Only, 1: Group Members, 2: Gforge user, 3:Every body
+ * @param is_result_public 0: Admins Only, 1: Group Members, 2: Gforge user, 3:voted user 4:Every body
+ * @param double_vote Allow dobule vote if it is 1
+ * @return boolean success.
+ */
+ function update($survey_title, &$add_questions, &$del_questions, $is_active=0, $is_public=1, $is_result_public=0, $double_vote=0) {
+ global $Language;
+
+ if (!$survey_title) {
+ $this->setError($Language->getText('survey_edit','survey_title_required'));
+ return false;
+ /* We need at least one survey question at this point */
+ }
+
+ $group_id = $this->Group->GetID();
+ $survey_id = $this->getID();
+
+ /* Ths Survey is not ready to update */
+ if (!$survey_id) {
+ $this->setError($Language->getText('survey_error','no_survey_data_filled'));
+ return false;
+ }
+
+ $survey_questions = $this->_updateQuestionString($add_questions, $del_questions);
+ $sql="UPDATE surveys SET survey_title='".htmlspecialchars($survey_title)."', survey_questions='$survey_questions', is_active='$is_active' ".
+ "WHERE survey_id='$survey_id' AND group_id='$group_id'";
+ $result=db_query($sql);
+ if (db_affected_rows($result) < 1) {
+ $this->setError($Language->getText('survey_edit','update_failed').db_error());
+ return false;
+ }
+ /* Update internal data */
+ return $this->fetchData($survey_id);
+ }
+
+ /**
+ * updateOrder - use this function to update question order
+ *
+ * @param int Question number
+ * @param boolean decide up or down. it is up if it is true
+ * @return boolean success.
+ */
+ function updateOrder($question_number, $is_up=true) {
+ global $Language;
+
+ $group_id = $this->Group->GetID();
+ $survey_id = $this->getID();
+
+ /* Ths Survey is not ready to update */
+ if (!$survey_id) {
+ $this->setError($Language->getText('survey_error','no_survey_data_filled'));
+ return false;
+ }
+
+ /* Decide delta */
+ if ($is_up) {
+ $delta = -1;
+ } else {
+ $delta = 1;
+ }
+
+ $survey_questions = $this->_updateQuestionStringOrder($question_number, $delta);
+ $sql="UPDATE surveys SET survey_questions='$survey_questions' ".
+ "WHERE survey_id='$survey_id' AND group_id='$group_id'";
+ $result=db_query($sql);
+ if (db_affected_rows($result) < 1) {
+ $this->setError($Language->getText('survey_edit','update_failed').db_error());
+ return false;
+ }
+
+ /* Update internal data */
+ return $this->fetchData($survey_id);
+ }
+
+ /**
+ * delete - use this function to delete this survey
+ * (We don't support delete yet)
+ *
+ * @return boolean success.
+ */
+ function delete() {
+ global $Language;
+
+ $group_id = $this->Group->GetID();
+ $survey_id = $this->getID();
+
+ $sql="DELETE FROM surveys where survey_id='$survey_id' AND group_id='$group_id'";
+
+ $res=db_query($sql);
+ if (!$res || db_affected_rows($res) < 1) {
+ $this->setError($Language->getText('survey_edit_question','delete_failed').db_error());
+ return false;
+ }
+
+ /* Delete internal data */
+ $this->data_array = null;
+ return true;
+ }
+
+ /**
+ * fetchData - re-fetch the data for this survey from the database.
+ *
+ * @param int The survey_id.
+ * @return boolean success.
+ */
+ function fetchData($survey_id) {
+ global $Language;
+ $group_id = $this->Group->GetID();
+
+ $sql="SELECT * FROM surveys WHERE survey_id='$survey_id' AND group_id='$group_id'";
+ $res=db_query($sql);
+
+ if (!$res || db_numrows($res) < 1) {
+ $this->setError($Language->getText('survey_error','no_survey_found').db_error());
+ return false;
+ }
+ $this->data_array =& db_fetch_array($res);
+ db_free_result($res);
+ return true;
+ }
+
+ /**
+ * getGroup - get the Group object this Survey is associated with.
+ *
+ * @return object The Group object.
+ */
+ function &getGroup() {
+ return $this->Group;
+ }
+
+ /**
+ * getID - Get the id of this Survey
+ *
+ * @return int The question_id
+ */
+ function getID() {
+ return $this->data_array['survey_id'];
+ }
+
+ /**
+ * isActriv - return if it is active
+ *
+ * @return int is active
+ */
+ function isActive() {
+ return $this->data_array['is_active'];
+ }
+
+ /**
+ * getTitle - Get the Survey title
+ *
+ * @return string the survey title
+ */
+ function getTitle() {
+ return $this->data_array['survey_title'];
+ }
+
+ /**
+ * getQuestionString - Get the question string
+ *
+ * @return string the question
+ */
+ function getQuestionString() {
+ return $this->data_array['survey_questions'];
+ }
+
+ /**
+ * getNumberOfQuestion - Get the number of questions
+ *
+ * @return int the number questions
+ */
+ function getNumberOfQuestions() {
+ return count($this->getQuestionArray());
+ }
+
+ /**
+ * getNumberOfVotes - Get the number of votes
+ *
+ * @return int the number votes
+ */
+ function getNumberOfVotes() {
+ $group_id = $this->Group->GetID();
+ $survey_id = $this->getID();
+
+ $sql = "SELECT 1 from survey_responses where group_id='$group_id' and survey_id='$survey_id' group by user_id";
+ $res=db_query($sql);
+ $ret = db_numrows($res);
+ db_free_result($res);
+
+ return $ret;
+ }
+
+ /**
+ * isUserVote - Figure out the user voted or not
+ *
+ * @param int user_id
+ * @return true of false
+ */
+ function isUserVote($user_id) {
+ $group_id = $this->Group->GetID();
+ $survey_id = $this->getID();
+
+ $sql = "SELECT 1 from survey_responses where group_id='$group_id' and survey_id='$survey_id' and user_id='$user_id'";
+
+ $res=db_query($sql, 1);
+ $ret = db_numrows($res);
+ db_free_result($res);
+
+ return $ret;
+ }
+
+ /**
+ * getQuestionArray - Get the question string numbers in array
+ *
+ * @return string the question
+ */
+ function &getQuestionArray() {
+ $questions = $this->getQuestionString();
+ if (!$questions) {
+ return array();
+ }
+
+
+ $arr_from_str = explode(',', $questions);
+
+ /* Remove non existed questions */
+ for ($i=0; $i<count($arr_from_str); $i++) {
+ if ($this->_isValidQuestionID($arr_from_str[$i])) {
+ $ret_arr[] = $arr_from_str[$i];
+ }
+ }
+
+ return $ret_arr;
+ }
+
+ /**
+ * getQuestionInstances - Get the SurveyQuestion array belongs to this Survey by order
+ *
+ * @return string the question
+ */
+ function &getQuestionInstances() {
+ $ret = array();
+
+ if (!$this->all_question_array || !is_array($this->all_question_array)) {
+ $this->_fillSurveyQuestions();
+ }
+
+ $arr = & $this->getQuestionArray();
+
+ for ($i=0; $i<count($arr); $i++) {
+ for ($j=0; $j<count($this->all_question_array); $j++) {
+ /* If it is, copy into new array in order */
+ if ($this->all_question_array[$j]->getID()==$arr[$i]) {
+ $ret[] = $this->all_question_array[$j];
+ break;
+ }
+ }
+ }
+
+ return $ret;
+ }
+
+ /**
+ * getAddableQuestionInstances - Get the addable SurveyQuestion from all questions
+ *
+ * @return string the question
+ */
+ function &getAddableQuestionInstances() {
+ $ret = array();
+
+ if (!$this->all_question_array || !is_array($this->all_question_array)) {
+ $this->_fillSurveyQuestions();
+ }
+
+ $arr = & $this->getQuestionArray();
+
+ /* Copy questions only if it is not in question string */
+ for ($i=0; $i<count($this->all_question_array); $i++) {
+ if (array_search($this->all_question_array[$i]->getID(), $arr)==false &&
+ $this->all_question_array[$i]->getID()!=$arr[0]) {
+ $ret[] = $this->all_question_array[$i];
+ }
+ }
+
+ return $ret;
+ }
+
+ /***************************************************************
+ * private question string deal methods
+ * TODO: Add a joint table for surveys and survey_questions.
+ * Deal with DBMS not comma separated string
+ ***************************************************************/
+
+ /**
+ * _fillSurveyQuestions - Get all Survey Questions using SurveyQuestionFactory
+ *
+ * @return booelan suesssness
+ */
+ function _fillSurveyQuestions() {
+ $sqf = new SurveyQuestionFactory($this->getGroup());
+ $this->all_question_array = & $sqf->getSurveyQuestions();
+ }
+
+
+ /**
+ * _isValidQuestionID - Check it is correct question id
+ *
+ * @param int questioin id
+ * @return boolean true if it is valid question id
+ */
+ function _isValidQuestionID($question_id) {
+ if (!$this->all_question_array || !is_array($this->all_question_array)) {
+ $this->_fillSurveyQuestions();
+ }
+
+ for ($i=0; $i<count($this->all_question_array); $i++) {
+ if ($question_id == $this->all_question_array[$i]->getID()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+
+ /**
+ * _makeQuestionString - Make comma saparated question number string
+ *
+ * @param int array Array of question number
+ * @return string question_strong (example: 1, 2, 3, 7);
+ */
+ function _makeQuestionString($arr) {
+ $ret = "";
+
+ /* No questions to add */
+ if (!$arr || !is_array($arr) || count($arr)<1) {
+ return $ret;
+ }
+ $ret.=$arr[0];
+ for ($i=1; $i<count($arr); $i++) {
+ $ret.=','.$arr[$i];
+
+ }
+
+ return $ret;
+ }
+
+ /**
+ * _updateQuestionString - Update comma saparated question number string
+ *
+ * @param int array Array of questions to add
+ * @param int array Array of questions to delete
+ * @return string question_strong (example: 1, 2, 3, 7);
+ */
+ function _updateQuestionString(&$arr_to_add, &$arr_to_del) {
+ /* Get array of current question string */
+ $arr = & $this->getQuestionArray();
+
+ /* questions to add */
+ if ($arr_to_add && is_array($arr_to_add) && count($arr_to_add)>0) {
+ for ($i = 0; $i < count($arr_to_add); $i++) {
+ /* Avoid double question */
+ if ($arr_to_add[$i] && array_search($arr_to_add[$i], $arr)==false && $arr_to_add[$i]!=$arr[0]) {
+ $arr[] = $arr_to_add[$i];
+ }
+ }
+ }
+
+ /* questions to delete */
+ if ($arr_to_del && is_array($arr_to_del) && count($arr_to_del)>0) {
+ for ($i = 0; $i < count($arr); $i++) {
+ /* If the value is no in the delete array, copy it into new array */
+ echo $arr[$i] + "HHH<BR>";
+
+ if ($arr[$i] && array_search($arr[$i], $arr_to_del)==false && $arr_to_del[0]!=$arr[$i]) {
+ $new_arr[] = $arr[$i];
+ }
+ }
+ /* copy new_arr to arr */
+ $arr = $new_arr;
+ }
+
+ /* converty array to String */
+ return $this->_makeQuestionString($arr);
+ }
+
+ /**
+ * _updateArrayOrder - Update array order
+ *
+ * @param int question number
+ * @param int increment or decrement (must be 1 or -1)
+ * @return string question_strong (example: 1, 2, 3, 7);
+ */
+ function _updateQuestionStringOrder($question_number, $delta) {
+ /* Get array of current question string */
+ $arr = & $this->getQuestionArray();
+
+ /* We are expectiong array */
+ if (!$arr || !is_array($arr)) {
+ return $this->getQuestionString();
+ }
+
+ $index = array_search($question_number, $arr);
+
+ /* The question number is not in the array
+ * We have nothing to change
+ */
+ if ($index==false && $question_number!=$arr[0]) {
+ return $this->getQuestionString();
+ }
+
+ $new_index = $index + $delta;
+
+ /* Out of boundary */
+ if ($new_index < 0 || $new_index >= count($arr)) {
+ return $this->getQuestionString();
+ }
+
+ /* swap */
+ $tmp = $arr[$index];
+ $arr[$index] = $arr[$new_index];
+ $arr[$new_index] = $tmp;
+
+ /* converty array to String */
+ return $this->_makeQuestionString($arr);
+ }
+}
+
+// Local Variables:
+// mode: php
+// c-file-style: "bsd"
+// End:
+?>
--- /dev/null
+<?php
+/**
+ * GForge Survey Facility: Get Survey
+ *
+ * Copyright 2004 GForge, LLC
+ * http://gforge.org/
+ *
+ * This file is part of GForge.
+ *
+ * GForge 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GForge 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 GForge; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/*
+ Survery Factory
+ By Sung Kim 2004/2/13
+*/
+
+require_once('common/include/Error.class');
+require_once('common/survey/Survey.class');
+
+class SurveyFactory extends Error {
+
+ /**
+ * The Group object.
+ *
+ * @var object $Group.
+ */
+ var $Group;
+
+ /**
+ * The survey array.
+ *
+ * @var array survey.
+ */
+ var $surveys;
+
+ /**
+ * Constructor.
+ *
+ * @param object The Group object to which this survey is associated.
+ * @param int The survey_id
+ */
+ function SurveyFactory(&$Group) {
+ global $Language;
+
+ $this->Error();
+ if (!$Group || !is_object($Group)) {
+ $this->setError($Language->getText('survey_error','error_no_valid_group_object'));
+ return false;
+ }
+
+ if ($Group->isError()) {
+ $this->setError($Language->getText('survey','survey').':: '.$Group->getErrorMessage());
+ return false;
+ }
+ $this->Group =& $Group;
+
+ return true;
+ }
+
+ /**
+ * getGroup - get the Group object this SurveyQuestionFactory is associated with.
+ *
+ * @return object The Group object.
+ */
+ function &getGroup() {
+ return $this->Group;
+ }
+
+ /**
+ * getSurveyQuestion - get an array of Survey Question objects
+ * for this Group and Survey id if survey_id is given.
+ *
+ * @return array The array of Survey Question objects.
+ */
+ function &getSurveys() {
+ global $Language;
+
+ /* We alread have it */
+ if ($this->surveys) {
+ return $this->surveys;
+ }
+
+ $group_id = $this->Group->GetID();
+
+ $sql="SELECT * FROM surveys WHERE group_id='$group_id' order by survey_id DESC";
+ $result = db_query ($sql);
+
+ if (!$result) {
+ $this->setError($Language->getText('survey_error','no_survey_found').db_error());
+ return false;
+ } else {
+ while ($arr = db_fetch_array($result)) {
+ $this->surveys[] = new Survey($this->Group, $arr['survey_id'], $arr);
+ }
+ db_free_result($result);
+ }
+ return $this->surveys;
+ }
+
+}
+
+// Local Variables:
+// mode: php
+// c-file-style: "bsd"
+// End:
+
+?>
--- /dev/null
+<?php
+/**
+ * GForge Survey Question Facility
+ *
+ * Copyright 2004 GForge, LLC
+ * http://gforge.org/
+ *
+ * This file is part of GForge.
+ *
+ * GForge 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GForge 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 GForge; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+
+/*
+ Survery Question
+ By Sung Kim 2004/2/13
+*/
+
+require_once('common/include/Error.class');
+
+class SurveyQuestion extends Error {
+
+ /**
+ * Associative array of data from db.
+ *
+ * @var array $data_array.
+ */
+ var $data_array;
+
+ /**
+ * The Group object.
+ *
+ * @var object $Group.
+ */
+ var $Group; //group object
+
+ /**
+ * Constructor.
+ *
+ * @param object The Group object to which this Survey Question is associated.
+ * @param int The questtion_id.
+ * @param array The associative array of data.
+ * @return boolean success.
+ */
+ function SurveyQuestion(&$Group, $question_id=false, $arr=false) {
+ global $Language;
+ $this->Error();
+ if (!$Group || !is_object($Group)) {
+ $this->setError($Language->getText('general','error_no_valid_group_object','Survey Question'));
+ return false;
+ }
+ if ($Group->isError()) {
+ $this->setError('Survey:: '.$Group->getErrorMessage());
+ return false;
+ }
+ $this->Group =& $Group;
+
+ if ($question_id) {
+ if (!$arr || !is_array($arr)) {
+ if (!$this->fetchData($question_id)) {
+ return false;
+ }
+ } else {
+ $this->data_array =& $arr;
+ if ($this->data_array['group_id'] != $this->Group->getID()) {
+ $this->setError($Language->getText('general','error_group_id'));
+ $this->data_array = null;
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ /**
+ * create - use this function to create a survey question
+ *
+ * @param string The question
+ * @param int The question type
+ * 1: Radio Buttons 1-5
+ * 2: Text Area
+ * 3: Radio Buttons Yes/No
+ * 4: Comment Only
+ * 5: Text Field
+ * 6: None
+ * @return boolean success.
+ */
+ function create($question, $question_type=1) {
+ global $Language;
+
+ if (strlen($question) < 3) {
+ $this->setError($Language->getText('survey_error','min_question_length'));
+ return false;
+ } else {
+ // Current permissions check.
+ // permission should be checked in higer level to faciliate usability
+ }
+
+ $group_id = $this->Group->GetID();
+ $sql="INSERT INTO survey_questions (group_id,question,question_type)
+ VALUES ($group_id,'".htmlspecialchars($question)."',$question_type)";
+
+ $res=db_query($sql);
+ if (!$res) {
+ $this->setError($Language->getText('survey_add_question','question_added').db_error());
+ return false;
+ }
+
+ /* Load question to data array */
+ $question_id=db_insertid($res,'survey_questions','question_id');
+ return $this->fetchData($question_id);
+ }
+
+
+
+ /**
+ * update - use this function to update a survey question
+ *
+ * @param string The question
+ * @param int The question type
+ * 1: Radio Buttons 1-5
+ * 2: Text Area
+ * 3: Radio Buttons Yes/No
+ * 4: Comment Only
+ * 5: Text Field
+ * 6: None
+ * @return boolean success.
+ */
+ function update($question, $question_type=1) {
+ global $Language;
+
+ if (strlen($question) < 3) {
+ $this->setError($Language->getText('survey_error','min_question_length'));
+ return false;
+ } else {
+ // Current permissions check.
+ // permission should be checked in higer level to faciliate usability
+ }
+
+ $group_id = $this->Group->GetID();
+ $question_id = $this->getID();
+
+ $sql="UPDATE survey_questions SET question='".htmlspecialchars($question)."',
+ question_type='$question_type'
+ where question_id='$question_id' AND group_id='$group_id'";
+
+ $res=db_query($sql);
+ if (!$res || db_affected_rows($res) < 1) {
+ $this->setError($Language->getText('survey_edit_question','update_failed').db_error());
+ return false;
+ }
+ return $this->fetchData($question_id);
+ }
+
+ /**
+ * delete - use this function to delete a survey question
+ *
+ * @return boolean success.
+ */
+ function delete() {
+ global $Language;
+
+
+ $group_id = $this->Group->GetID();
+ $question_id = $this->getID();
+
+ $sql="DELETE FROM survey_questions where question_id='$question_id' AND group_id='$group_id'";
+
+ $res=db_query($sql);
+ if (!$res || db_affected_rows($res) < 1) {
+ $this->setError($Language->getText('survey_edit_question','delete_failed').db_error());
+ return false;
+ }
+
+ $this->data_array = null;
+ return true;
+ }
+
+ /**
+ * fetchData - re-fetch the data for this survey question from the database.
+ *
+ * @param int The survey question_id.
+ * @return boolean success.
+ */
+ function fetchData($question_id) {
+ global $Language;
+ $group_id = $this->Group->GetID();
+
+ $sql="SELECT survey_questions.*, survey_question_types.type
+ FROM survey_questions ,survey_question_types
+ WHERE survey_question_types.id=survey_questions.question_type
+ AND survey_questions.question_id='$question_id'
+ AND survey_questions.group_id='$group_id'";
+ $res=db_query($sql);
+
+ if (!$res || db_numrows($res) < 1) {
+ $this->setError($Language->getText('survey_edit_question','error_finding_question').db_error());
+ return false;
+ }
+ $this->data_array =& db_fetch_array($res);
+ db_free_result($res);
+ return true;
+ }
+
+ /**
+ * getGroup - get the Group object this SurveyQuestion is associated with.
+ *
+ * @return object The Group object.
+ */
+ function &getGroup() {
+ return $this->Group;
+ }
+
+ /**
+ * getID - Get the id of this Survey Question
+ *
+ * @return int The question_id
+ */
+ function getID() {
+ return $this->data_array['question_id'];
+ }
+
+
+ /**
+ * getQuestion - Get the question
+ *
+ * @return string the question
+ */
+ function getQuestion() {
+ return $this->data_array['question'];
+ }
+
+
+ /**
+ * getQuestionType - Get the question type
+ *
+ * @return int the question type
+ */
+ function getQuestionType() {
+ return $this->data_array['question_type'];
+ }
+
+
+ /**
+ * getQuestionStringType - Get the type from survey_question_types
+ *
+ * @return String the question type
+ */
+ function getQuestionStringType() {
+ return $this->data_array['type'];
+ }
+}
+
+// Local Variables:
+// mode: php
+// c-file-style: "bsd"
+// End:
+
+?>
--- /dev/null
+<?php
+/**
+ * GForge Survey Facility: Get Survey Questions
+ *
+ * Copyright 2004 GForge, LLC
+ * http://gforge.org/
+ *
+ * This file is part of GForge.
+ *
+ * GForge 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GForge 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 GForge; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+
+
+/*
+ Survery Question Factory
+ By Sung Kim 2004/2/13
+*/
+
+
+require_once('common/include/Error.class');
+require_once('common/survey/SurveyQuestion.class');
+
+class SurveyQuestionFactory extends Error {
+
+ /**
+ * The Group object.
+ *
+ * @var object $Group.
+ */
+ var $Group;
+
+ /**
+ * The question array.
+ *
+ * @var array questionss.
+ */
+ var $questions;
+
+ /**
+ * Constructor.
+ *
+ * @param object The Group object to which this survey question is associated.
+ * @param int The survey_id
+ */
+ function SurveyQuestionFactory(&$Group) {
+ global $Language;
+ $this->Error();
+ if (!$Group || !is_object($Group)) {
+ $this->setError($Language->getText('survey_error','error_no_valid_group_object'));
+ return false;
+ }
+ if ($Group->isError()) {
+ $this->setError($Language->getText('survey','survey').':: '.$Group->getErrorMessage());
+ return false;
+ }
+ $this->Group =& $Group;
+
+ return true;
+ }
+
+ /**
+ * getGroup - get the Group object this SurveyQuestionFactory is associated with.
+ *
+ * @return object The Group object.
+ */
+ function &getGroup() {
+ return $this->Group;
+ }
+
+ /**
+ * getSurveyQuestion - get an array of Survey Question objects
+ * for this Group and Survey id if survey_id is given.
+ *
+ * @return array The array of Survey Question objects.
+ */
+ function &getSurveyQuestions() {
+ global $Language;
+
+ /* We alread have it */
+ if ($this->questions) {
+ return $this->questions;
+ }
+
+ $group_id = $this->Group->GetID();
+ $sql="SELECT survey_questions.*,survey_question_types.type ".
+ "FROM survey_questions,survey_question_types ".
+ "WHERE survey_question_types.id=survey_questions.question_type ".
+ "AND survey_questions.group_id='$group_id' ".
+ "ORDER BY survey_questions.question_id DESC";
+
+ $result = db_query ($sql);
+
+ if (!$result) {
+ $this->setError($Language->getText('survey_error','no_question_found').db_error());
+ return false;
+ } else {
+ while ($arr = db_fetch_array($result)) {
+ $this->questions[] = new SurveyQuestion($this->Group, $arr['question_id'], $arr);
+ }
+ db_free_result($result);
+ }
+ return $this->questions;
+ }
+
+}
+
+// Local Variables:
+// mode: php
+// c-file-style: "bsd"
+// End:
+
+?>
--- /dev/null
+<?php
+/**
+ * GForge Survey Response Facility
+ *
+ * Copyright 2004 GForge, LLC
+ * http://gforge.org/
+ *
+ * This file is part of GForge.
+ *
+ * GForge 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GForge 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 GForge; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+
+/*
+ Survery Response
+ By Sung Kim 2004/2/13
+*/
+
+require_once('common/include/Error.class');
+
+class SurveyResponse extends Error {
+ /**
+ * Associative array of data from db.
+ *
+ * @var array $data_array.
+ */
+ var $data_array;
+
+ /**
+ * The Group object.
+ *
+ * @var object $Group.
+ */
+ var $Group; //group object
+
+ /**
+ * Constructor.
+ *
+ * @param object The Group object to which this Survey Response is associated.
+ * @param int The questtion_id.
+ * @param array The associative array of data.
+ * @return boolean success.
+ */
+ function SurveyResponse(&$Group, $arr=false) {
+ global $Language;
+ $this->Error();
+ if (!$Group || !is_object($Group)) {
+ $this->setError($Language->getText('general','error_no_valid_group_object','Survey Question'));
+ return false;
+ }
+ if ($Group->isError()) {
+ $this->setError('Survey:: '.$Group->getErrorMessage());
+ return false;
+ }
+ $this->Group =& $Group;
+
+ if ($arr && is_array($arr)) {
+ $this->data_array =& $arr;
+ }
+ return true;
+ }
+
+ /**
+ * create - use this function to create a survey response
+ *
+ * @param string The question
+ * @param int The question type
+ * 1: Radio Buttons 1-5
+ * 2: Text Area
+ * 3: Radio Buttons Yes/No
+ * 4: Comment Only
+ * 5: Text Field
+ * 6: None
+ * @return boolean success.
+ */
+ function create($user_id, $survey_id, $question_id, $response) {
+ global $Language;
+ $group_id = $this->Group->GetID();
+ $now = time();
+
+ $sql="INSERT INTO survey_responses (user_id,group_id,survey_id,question_id,response,post_date) ".
+ "VALUES ('". $user_id. "','" . addslashes($group_id) . "','" .
+ addslashes($survey_id) . "','" . addslashes($question_id) . "','".
+ htmlspecialchars($response) . "','$now')";
+ $res=db_query($sql);
+ if (!$res) {
+ $this->setError($Language->getText('survey_resp','error').db_error());
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * getGroup - get the Group object this SurveyResponse is associated with.
+ *
+ * @return object The Group object.
+ */
+ function &getGroup() {
+ return $this->Group;
+ }
+
+ /**
+ * getUserID - Get the user id of this Survey response
+ *
+ * @return int The user_id
+ */
+ function getUserID() {
+ return $this->data_array['user_id'];
+ }
+
+ /**
+ * getGroup - Get the group id of this Survey response
+ *
+ * @return int The group_id
+ */
+ function getGroupID() {
+ return $this->data_array['group_id'];
+ }
+
+ /**
+ * getSurveyID - Get the survey id of this Survey response
+ *
+ * @return int The survey_id
+ */
+ function getSurveyID() {
+ return $this->data_array['survey_id'];
+ }
+
+
+ /**
+ * getQuestionID - Get the question id of this Survey response
+ *
+ * @return int The question_id
+ */
+ function getQuestionID() {
+ return $this->data_array['question_id'];
+ }
+
+ /**
+ * getUserID - Get the response of this Survey response
+ *
+ * @return int The response
+ */
+ function getResponse() {
+ return $this->data_array['response'];
+ }
+
+
+ /**
+ * getPostDate - Get the post date of this Survey response
+ *
+ * @return int The post date
+ */
+ function getPostDate() {
+ return $this->data_array['post_date'];
+ }
+}
+
+// Local Variables:
+// mode: php
+// c-file-style: "bsd"
+// End:
+
+?>
--- /dev/null
+<?php
+/**
+ * GForge Survey Facility: Get Survey Response Factory
+ *
+ * Copyright 2004 GForge, LLC
+ * http://gforge.org/
+ *
+ * This file is part of GForge.
+ *
+ * GForge 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GForge 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 GForge; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+
+
+/*
+ Survery Response Factory
+ By Sung Kim 2004/2/13
+*/
+
+
+require_once('common/include/Error.class');
+require_once('common/survey/SurveyResponse.class');
+
+class SurveyResponseFactory extends Error {
+
+ /**
+ * The Survey object.
+ *
+ * @var object $Survey.
+ */
+ var $Survey;
+
+ /**
+ * The Question object.
+ *
+ * @var object $Question.
+ */
+ var $Question;
+
+ /**
+ * The Response array.
+ *
+ * @var array Response
+ */
+ var $Responses;
+
+ /**
+ * The Aggregated Result array for question.
+ *
+ * @var array Response
+ */
+ var $Result;
+
+ /**
+ * Constructor.
+ *
+ * @param object The Survey object
+ * @param object The Question object to which this survey Response is associated.
+ * @param int The survey_id
+ */
+ function SurveyResponseFactory(&$Survey, &$Question ) {
+ global $Language;
+ $this->Error();
+
+ if (!$Survey || !is_object($Survey)) {
+ $this->setError($Language->getText('survey_error','error_no_valid_survey_object'));
+ return false;
+ }
+ if ($Survey->isError()) {
+ $this->setError($Language->getText('survey','survey').':: '.$Survey->getErrorMessage());
+ return false;
+ }
+ if (!$Question || !is_object($Question)) {
+ $this->setError($Language->getText('survey_error','error_no_valid_question_object'));
+ return false;
+ }
+ if ($Question->isError()) {
+ $this->setError($Language->getText('survey','survey').':: '.$Question->getErrorMessage());
+ return false;
+ }
+
+ $this->Survey = &$Survey;
+ $this->Question = &$Question;
+
+ return true;
+ }
+
+ /**
+ * getGroup - get the Group object this SurveyResponse is associated with.
+ *
+ * @return object The Group object.
+ */
+ function &getGroup() {
+ $Survey = $this->getSurvey();
+ return $Survey->Group;
+ }
+
+ /**
+ * getSurvey - get the Survey object this SurveyResponse is associated with.
+ *
+ * @return object The Survey object.
+ */
+ function &getSurvey() {
+ return $this->Survey;
+ }
+
+ /**
+ * getQuestion - get the Question object this SurveyResponse is associated with.
+ *
+ * @return object The Question object.
+ */
+ function &getQuestion() {
+ return $this->Question;
+ }
+
+ /**
+ * getSurveyResponses - get an array of Survey Response objects
+ * for the Survey and Question
+ *
+ * @return array The array of Survey Response objects.
+ */
+ function &getSurveyResponses() {
+ global $Language;
+
+ /* We alread have it */
+ if ($this->Responses) {
+ return $this->Responses;
+ }
+
+ $group = $this->getGroup();
+ $group_id = $group->GetID();
+ $survey = $this->getSurvey();
+ $survey_id = $survey->GetID();
+ $question = $this->getQUestion();
+ $question_id = $question->GetID();
+
+ $sql="SELECT * FROM survey_responses ".
+ "WHERE survey_id='$survey_id' ".
+ "AND question_id='$question_id' ".
+ "AND group_id='$group_id' ORDER BY post_date DESC";
+
+ $result = db_query ($sql);
+ if (!$result) {
+ $this->setError($Language->getText('survey_error','no_survey_response_found').db_error());
+ return false;
+ } else {
+ while ($arr = db_fetch_array($result)) {
+ $this->Responses[] = new SurveyResponse($this->getGroup(), $arr);
+ }
+ db_free_result($result);
+ }
+ return $this->Responses;
+ }
+
+
+ /**
+ * getNumberOfSurveyResponses - get the number of Survey Responses
+ *
+ * @return int the number of survey responses
+ */
+ function &getNumberOfSurveyResponsess() {
+ $arr = &$this->getSurveyResponses();
+ if (!$arr || !is_array($arr)) {
+ return 0;
+ }
+
+ return count($arr);
+ }
+
+ /**
+ * getResults - get the array of result for yes/no and 1-5 question
+ *
+ * @return int the array of result
+ * for the yes/no question, it returns counts in arr[1] and arr[5];
+ * for the 1-5 question, it returns counts in arr[1], arr[1], ..., arr[5];
+ * for comments, we return arr[1], ...arr[n] with comments
+ */
+ function &getResults() {
+ if ($this->Results) {
+ return $this->Results;
+ }
+
+ $arr = &$this->getSurveyResponses();
+ if (!$arr || !is_array($arr)) {
+ return false;
+ }
+ $count = count($arr);
+
+ $question = $this->getQuestion();
+ if ($question->getQuestionType()=='1' ||
+ $question->getQuestionType()=='3') {
+ /* This is a radio-button question. Values 1-5 or yes(1) no (5)question */
+ $is_radio = true;
+ $this->Result = array(0, 0, 0, 0, 0, 0);
+ } else {
+ $is_radio=false;
+ }
+
+ for($i=0; $i<$count; $i++) {
+ if ($arr[$i]->isError()) {
+ echo $arr[$i]->getErrorMessage();
+ continue;
+ }
+
+ $response = $arr[$i]->getResponse();
+
+ if($is_radio) {
+ /* We only counts */
+ $this->Result[$response]++;
+ } else {
+ /* Save response */
+ $this->Result[] = $response;
+ }
+ }
+
+ return $this->Result;
+ }
+}
+
+// Local Variables:
+// mode: php
+// c-file-style: "bsd"
+// End:
+
+?>
--- /dev/null
+<?php
+/**
+ *
+ * General Graph showing module for Survey
+ *
+ * This script produces PNG image which shows graph of SCM metrics
+ *
+ *
+ */
+
+/**
+ * Expecting data
+ * @ legend[] - array of legends
+ * @ value[] - array of values
+ * @ type - grap type. We support pie and vertical bar graph (pie, vbar)
+ * @ width
+ * @ hight
+ */
+
+require_once('pre.php');
+
+// Check if we have jpgraph
+if (!file_exists($sys_path_to_jpgraph.'/jpgraph.php')) {
+ //# TODO: Need to show the message as a image file
+ exit_error('Error', 'Package JPGraph not installed');
+}
+
+// Read jPGraph libraries. Make sure the $sys_path_to_jpgraph is correct in local.inc
+require_once($sys_path_to_jpgraph.'/jpgraph.php');
+require_once($sys_path_to_jpgraph.'/jpgraph_line.php');
+require_once($sys_path_to_jpgraph.'/jpgraph_bar.php');
+require_once($sys_path_to_jpgraph.'/jpgraph_pie.php');
+require_once($sys_path_to_jpgraph.'/jpgraph_pie3d.php');
+
+if ($type=='pie') {
+ ShowPie($legend, $value);
+} else {
+ ShowHBar($legend, $value);
+}
+
+/**
+ * Show 3D Pie graph
+ */
+function ShowPie(&$legend, &$value) {
+
+ $graph = new PieGraph(330,200,"auto");
+ $graph->SetFrame(false);
+ //$graph->title->Set("A simple 3D Pie plot");
+ //$graph->title->SetFont(FF_FONT1,FS_BOLD);
+
+ $p1 = new PiePlot3D($value);
+ $p1->ExplodeSlice(1);
+ $p1->SetCenter(0.45);
+ $p1->SetLegends($legend);
+ $graph->legend->SetPos(0.01,0.01,'right','top');
+
+ $graph->Add($p1);
+ $graph->Stroke();
+}
+
+
+/**
+ * Show Horizontal Bar graph
+ */
+function ShowHBar(&$legend, &$value) {
+
+ $height=50+count($value)*18;
+ $width=500;
+
+ // Set the basic parameters of the graph
+ $graph = new Graph($width,$height,'auto');
+
+ $graph->SetScale("textlin");
+ $top = 30;
+ $bottom = 20;
+ $left = 100;
+ $right = 50;
+ $graph->Set90AndMargin($left,$right,$top,$bottom);
+ $graph->xaxis->SetTickLabels($legend);
+ $graph->SetFrame(false);
+
+ // Label align for X-axis
+ $graph->xaxis->SetLabelAlign('right','center','right');
+
+ // Label align for Y-axis
+ $graph->yaxis->SetLabelAlign('center','bottom');
+
+ // Create a bar pot
+ $bplot = new BarPlot($value);
+ $bplot->SetFillColor("orange");
+ $bplot->SetWidth(0.5);
+ // We want to display the value of each bar at the top
+ $graph->yaxis->scale->SetGrace(10);
+ $graph->yaxis->SetLabelAlign('center','bottom');
+ $graph->yaxis->SetLabelFormat('%d');
+
+ $bplot->value->Show();
+ $bplot->value->SetFormat('%.d votes');
+ // Setup color for gradient fill style
+ $bplot->SetFillGradient("navy","lightsteelblue",GRAD_MIDVER);
+
+ $graph->Add($bplot);
+
+ $graph->Stroke();
+}
+?>
require_once('pre.php');
-require_once('www/survey/survey_utils.php');
+require_once('www/survey/include/SurveyHTML.class');
$is_admin_page='y';
-survey_header(array('title'=>$Language->getText('survey_admin_index','title'),'pagename'=>'survey_admin'));
+$sh = new SurveyHtml();
+$sh->header(array('title'=>$Language->getText('survey_admin_index','title'),'pagename'=>'survey_admin'));
if (!session_loggedin() || !user_ismember($group_id,'A')) {
echo '<h1>'.$Language->getText('survey_admin_index','permission_denied').'</h1>';
- survey_footer(array());
+ $sh->footer(array());
exit;
}
?>
<p>
-<a href="/survey/admin/add_survey.php?group_id=<?php echo $group_id; ?>"><?php echo $Language->getText('survey_admin_index','add_survey'); ?></a><br />
-<a href="/survey/admin/edit_survey.php?group_id=<?php echo $group_id; ?>"><?php echo $Language->getText('survey_admin_index','edir_existing_survey'); ?></a><br />
-<a href="/survey/admin/add_question.php?group_id=<?php echo $group_id; ?>"><?php echo $Language->getText('survey_admin_index','add_question'); ?></a><br />
-<a href="/survey/admin/show_questions.php?group_id=<?php echo $group_id; ?>"><?php echo $Language->getText('survey_admin_index','edit_existing_question'); ?></a><br />
-<a href="/survey/admin/show_results.php?group_id=<?php echo $group_id; ?>"><?php echo $Language->getText('survey_admin_index','show_results'); ?></a><br />
+<UL>
+<LI><a href="/survey/admin/question.php?group_id=<?php echo $group_id; ?>"><?php echo $Language->getText('survey_admin_index','add_question'); ?></a><br />
+<LI><a href="/survey/admin/survey.php?group_id=<?php echo $group_id; ?>"><?php echo $Language->getText('survey_admin_index','add_survey'); ?></a><br />
+<LI><a href="/survey/admin/show_results.php?group_id=<?php echo $group_id; ?>"><?php echo $Language->getText('survey_admin_index','show_results'); ?></a><br />
+</UL>
</p>
<?php echo $Language->getText('survey_admin_index','its_simple_to_create', array('<p><strong>/survey/survey.php?group_id='.$group_id.'&survey_id=XX</strong>')); ?>
</li>
</ol>
</p>
-<p><?php echo $Language->getText('survey_admin_index','you_can_now_activate',array('<a href="/survey/admin/edit_survey.php?group_id='.$group_id.'">','</a>')); ?>
+<p><?php echo $Language->getText('survey_admin_index','you_can_now_activate',array('<a href="/survey/admin/survey.php?group_id='.$group_id.'">','</a>')); ?>
</p>
<p>
<?php
-survey_footer(array());
+$sh->footer(array());
?>
</p>
--- /dev/null
+<?php
+/**
+ *
+ * GForge Survey Facility: Question handle program
+ *
+ * Copyright 2004 (c) GForge
+ * http://gforge.org
+ *
+ *
+ */
+require_once('pre.php');
+require_once('common/survey/SurveyQuestion.class');
+require_once('common/survey/SurveyQuestionFactory.class');
+require_once('www/survey/include/SurveyHTML.class');
+
+/* We need a group_id */
+if (!$group_id) {
+ exit_no_group();
+}
+
+$g =& group_get_object($group_id);
+if (!$g || !is_object($g) || $g->isError()) {
+ exit_no_group();
+}
+
+$is_admin_page='y';
+$sh = new SurveyHtml();
+$sh->header(array('title'=>$Language->getText('survey_add_question','title'),'pagename'=>'survey_admin_add_question'));
+
+if (!session_loggedin() || !user_ismember($group_id,'A')) {
+ echo "<h1>".$Language->getText('survey_error','permission_denied')."</h1>";
+ $sh->footer(array());
+ exit;
+}
+
+/* Create a Survey Question for general purpose */
+$sq = new SurveyQuestion($g, $question_id);
+if (!$sq || !is_object($sq)) {
+ echo "<h3>".$Language->getText('general','error'). ' Can not get Survey Question' ."</H3>";
+} else if ( $sq->isError()) {
+ echo "<h3>".$Language->getText('general','error'). $sq->getErrorMessage() ."</H3>";
+}
+
+/* Delete a question */
+if ($delete=="Y" && $question_id) {
+ $sq->delete();
+
+ /* Error */
+ if ( $sq->isError()) {
+ $msg = $Language->getText('survey_edit','delete_failed').' '.$sq->getErrorMessage();
+ } else {
+ $msg = $Language->getText('survey_edit','delete_successful');
+ }
+ echo "<H3>".$msg ."</H3>";
+} else if ($post=="Y") {
+ /* Modification */
+ if ($question_id) {
+ $sq->update($question, $question_type);
+ $msg = $Language->getText('survey_edit_question','update_successful');
+ } else { /* adding new question */
+ $sq->create($question, $question_type);
+ $msg = $Language->getText('survey_add_question', 'question_added');
+ }
+
+ /* Error */
+ if ( $sq->isError()) {
+ $msg = $sq->getErrorMessage();
+ }
+
+ echo "<H3>".$msg ."</H3>";
+
+ /* Add now Question */
+ $sq = false;
+}
+
+/* Show Add/Modify form
+ * If $question is null it is add form, otherwise modify
+ */
+echo($sh->showAddQuestionForm($sq));
+
+/* Show existing questions
+ */
+$sqf = new SurveyQuestionFactory($g);
+$sqs = & $sqf->getSurveyQuestions();
+if (!$sqs) {
+ echo ($Language->getText('survey_error', 'no_questions_found'));
+} else {
+ echo($sh->showQuestions($sqs));
+}
+
+$sh->footer(array());
+?>
require_once('pre.php');
-require_once('www/survey/survey_utils.php');
-$is_admin_page='y';
-survey_header(array('title'=>$Language->getText('survey_show_results','title'),'pagename'=>'survey_admin_show_results'));
-if (!session_loggedin() || !user_ismember($group_id,'A')) {
- echo "<h1>".$Language->getText('survey_show_results','permission_denied')."</h1>";
- survey_footer(array());
- exit;
+require_once('common/survey/Survey.class');
+require_once('common/survey/SurveyFactory.class');
+require_once('common/survey/SurveyQuestion.class');
+require_once('common/survey/SurveyQuestionFactory.class');
+require_once('common/survey/SurveyResponse.class');
+require_once('common/survey/SurveyResponseFactory.class');
+require_once('www/survey/include/SurveyHTML.class');
+
+/* We need a group_id */
+if (!$group_id) {
+ exit_no_group();
}
-Function ShowResultsSurvey($result) {
- global $group_id,$PHP_SELF;
- $rows = db_numrows($result);
- $cols = db_numfields($result);
- echo "<h3>$rows" .$Language->getText('survey_show_results','found')."</h3>";
-
- echo /*"<table bgcolor=\"NAVY\"><tr><td bgcolor=\"NAVY\">*/ "<table border=\"0\">\n";
- /* Create the headers */
- echo "<tr style=\"background=color:$GLOBALS[COLOR_MENUBARBACK]\">\n";
-
- for($i = 0; $i < $cols; $i++) {
- printf( "<th><span style=\"color:white\"><strong>%s</strong></span></th>\n", db_fieldname($result,$i));
- }
- echo "</tr>";
-
- for($j = 0; $j < $rows; $j++) {
-
- if ($j%2==0) {
- $row_bg="white";
- } else {
- $row_bg="$GLOBALS[COLOR_LTBACK1]";
- }
-
- echo "<tr style=\"background-color:$row_bg\">\n";
-
- echo "<td><a href=\"$PHP_SELF?group_id=$group_id&survey_id=".db_result($result,$j,"survey_id")."\">".db_result($result,$j,"survey_id")."</a></td>\n";
-
- for($i = 1; $i < $cols; $i++) {
- printf("<td>%s</td>\n",db_result($result,$j,$i));
- }
-
- echo "</tr>";
- }
- echo "</table>"; //</td></tr></TABLE>";
+$g =& group_get_object($group_id);
+if (!$g || !is_object($g) || $g->isError()) {
+ exit_no_group();
}
+$is_admin_page='y';
+$sh = new SurveyHtml();
-Function ShowResultsAggregate($result) {
- global $group_id;
- global $Language;
- $rows = db_numrows($result);
- $cols = db_numfields($result);
- echo "<h3>$rows" .$Language->getText('survey_show_results','found')."</h3>";
-
- echo /*"<table bgcolor=\"NAVY\"><tr><td bgcolor=\"NAVY\">*/ "<table border=\"0\">\n";
- /* Create the headers */
- echo "<tr style=\"background=color:$GLOBALS[COLOR_MENUBARBACK]\">\n";
-
- for($i = 0; $i < $cols; $i++) {
- printf( "<th><span style=\"color:white\"><strong>%s</strong></span></th>\n", db_fieldname($result,$i));
- }
- echo "</tr>";
-
- for($j = 0; $j < $rows; $j++) {
-
- if ($j%2==0) {
- $row_bg="white";
- } else {
- $row_bg="$GLOBALS[COLOR_LTBACK1]";
- }
-
- echo "<tr style=\"background-color:$row_bg\">\n";
-
- echo "<td><a href=\"show_results_aggregate.php?group_id=$group_id&survey_id=".db_result($result,$j,"survey_id")."\">".db_result($result,$j,"survey_id")."</a></td>\n";
-
- for($i = 1; $i < $cols; $i++) {
- printf("<td>%s</td>\n",db_result($result,$j,$i));
- }
+$is_admin_page='y';
+$sh->header(array('title'=>$Language->getText('survey_show_results','title'),'pagename'=>'survey_admin_show_results'));
- echo "</tr>";
- }
- echo "</table>"; //</td></tr></TABLE>";
+if (!session_loggedin() || !user_ismember($group_id,'A')) {
+ echo "<h1>".$Language->getText('survey_show_results','permission_denied')."</h1>";
+ $sh->footer(array());
+ exit;
}
-
-Function ShowResultsCustomer($result) {
- global $survey_id,$group_id;
- global $Language;
- $rows = db_numrows($result);
- $cols = db_numfields($result);
- echo "<h3>$rows".$Language->getText('survey_show_results','found')."</h3>";
-
- echo /*"<table bgcolor=\"NAVY\"><tr><td bgcolor=\"NAVY\">*/ "<table border=\"0\">\n";
- /* Create the headers */
- echo "<tr style=\"background=color:$GLOBALS[COLOR_MENUBARBACK]\">\n";
-
- for($i = 0; $i < $cols; $i++) {
- printf( "<th><span style=\"color:white\"><strong>%s</strong></span></th>\n", db_fieldname($result,$i));
+/* Show detailed results of a survey */
+if ($survey_id) {
+ $s = new Survey($g, $survey_id);
+
+ if (!$s || !is_object($s)) {
+ echo "<h3>".$Language->getText('general','error'). ' Can not get Survey' ."</H3>";
+ $sh->footer(array());
+ exit;
+ } else if ( $s->isError()) {
+ echo "<h3>".$Language->getText('general','error'). $s->getErrorMessage() ."</H3>";
+ $sh->footer(array());
+ exit;
+ }
+
+ /* A specific question */
+ if ($question_id) {
+ /* Create a Survey Question for general purpose */
+ $sq = new SurveyQuestion($g, $question_id);
+ if (!$sq || !is_object($sq)) {
+ echo "<h3>".$Language->getText('general','error'). ' Can not get Survey Question' ."</H3>";
+ } else if ( $sq->isError()) {
+ echo "<h3>".$Language->getText('general','error'). $sq->getErrorMessage() ."</H3>";
+ } else {
+ showResult($sh, $s, $sq, 1, 0, $graph);
}
- echo "</tr>";
-
- for($j = 0; $j < $rows; $j++) {
-
- if ($j%2==0) {
- $row_bg="white";
- } else {
- $row_bg="$GLOBALS[COLOR_LTBACK1]";
+
+ } else {
+ echo '<h2>'.$s->getTitle().' ( '. $s->getNumberOfVotes() .' Votes )</h2><p/>';
+
+ /* Get questions of this survey */
+ $questions = & $s->getQuestionInstances();
+
+ $question_number = 1;
+ for ($i=0; $i<count($questions); $i++) {
+ if ($questions[$i]->isError()) {
+ echo $questions[$i]->getErrorMessage();
+ } else {
+ if ($questions[$i]->getQuestionType()!='4') {
+ showResult($sh, $s, $questions[$i], $show_comment, $question_number++, $graph);
}
-
- echo "<tr style=\"background-color:$row_bg\">\n";
-
- echo "<td><a href=\"show_results_individual.php?group_id=$group_id&survey_id=$survey_id&customer_id=".db_result($result,$j,"cust_id")."\">".db_result($result,$j,"cust_id")."</a></td>\n";
-
- for($i = 1; $i < $cols; $i++) {
- printf("<td>%s</td>\n",db_result($result,$j,$i));
- }
-
- echo "</tr>";
+ }
}
- echo "</table>"; //</td></tr></TABLE>";
+ }
}
+/* Show list of Surveys with result link */
+/* Show list of Servey */
+$sf = new SurveyFactory($g);
+$ss = & $sf->getSurveys();
+if (!$ss) {
+ echo ($Language->getText('survey_error', 'no_question_found'));
+} else {
+ echo($sh->ShowSurveys($ss, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1));
+}
+$sh->footer(array());
-if (!$survey_id) {
-
- /*
- Select a list of surveys, so they can click in and view a particular set of responses
- */
-
- $sql="SELECT survey_id,survey_title FROM surveys WHERE group_id='$group_id'";
-
- $result=db_query($sql);
-
-// echo "\n<h2>View Individual Responses</h2>\n\n";
-// ShowResultsSurvey($result);
-
- echo "\n<h2>".$Language->getText('survey_show_results','view_aggregate_responses')."</h2>\n\n";
- ShowResultsAggregate($result);
-
-} /* else {
-
- / *
- Pull up a list of customer IDs for people that responded to this survey
- * /
-
- $sql="select people.cust_id, people.first_name, people.last_name ".
- "FROM people,responses where responses.customer_id=people.cust_id AND responses.survey_id='$survey_id' ".
- "GROUP BY people.cust_id, people.first_name, people.last_name";
-
- $result=db_query($sql);
-
- ShowResultsCustomer($result);
-
-} */
-
-survey_footer(array());
-
+/**
+ * ShowResult - Get Result from Survey and Question. Pass the reuslt to Show Result HTML class
+ *
+ * @param object a survey object
+ * @param object a qustsion object
+ * @param int wheather print out export(csv) format
+ */
+function ShowResult(&$SurveyHTML, &$Survey, &$Question, $show_comment=0, $q_num="", $graph=0) {
+ /* Get results */
+ $srf = new SurveyResponseFactory(&$Survey, &$Question);
+ if (!$srf || !is_object($srf)) {
+ echo "<h3>".$Language->getText('general','error'). ' Can not get Survey Response Factory' ."</H3>";
+ } else if ( $srf->isError()) {
+ echo "<h3>".$Language->getText('general','error'). $srf->getErrorMessage() ."</H3>";
+ } else {
+ /* Show result in HTML*/
+ echo ($SurveyHTML->ShowResult($srf, $show_comment, $q_num, $graph));
+ }
+}
?>
--- /dev/null
+<?php
+
+/**
+ *
+ * GForge Survey Facility: Question handle program
+ *
+ * Copyright 2004 (c) GForge
+ * http://gforge.org
+ *
+ *
+ */
+require_once('pre.php');
+require_once('common/survey/Survey.class');
+require_once('common/survey/SurveyFactory.class');
+require_once('common/survey/SurveyQuestion.class');
+require_once('common/survey/SurveyQuestionFactory.class');
+require_once('www/survey/include/SurveyHTML.class');
+
+/* We need a group_id */
+if (!$group_id) {
+ exit_no_group();
+}
+
+$g =& group_get_object($group_id);
+if (!$g || !is_object($g) || $g->isError()) {
+ exit_no_group();
+}
+
+$is_admin_page='y';
+$sh = new SurveyHtml();
+$s = new Survey($g, $survey_id);
+
+$sh->header(array('title'=>$Language->getText('survey_add_survey','title'),'pagename'=>'survey_admin_add_survey'));
+
+if (!session_loggedin() || !user_ismember($group_id,'A')) {
+ echo "<h1>". $Language->getText('survey_add_question','permission_denied')."</h1>";
+ $sh->footer(array());
+ exit;
+}
+
+if ($post=="Y") {
+ if ($survey_id) { /* Modify */
+ $s->update($survey_title, $to_add, $to_del, $is_active);
+ $feedback = $Language->getText('survey_edit','update_successful');
+ } else { /* Add */
+ $s->create($survey_title, $to_add, $is_active);
+ $feedback = $Language->getText('survey_add_survey', 'survey_inserted');
+ }
+}
+
+/* Order changes */
+if ($updown=="Y") {
+ $s->updateOrder($question_id, $is_up);
+ $feedback = $Language->getText('survey_edit','update_successful');
+}
+
+/* Error on previous transactions? */
+if ($s->isError()) {
+ $feedback = $s->getErrorMessage();
+}
+
+echo ($sh->ShowAddSurveyForm($s));
+
+/* Show list of Servey */
+$sf = new SurveyFactory($g);
+$ss = & $sf->getSurveys();
+if (!$ss) {
+ echo ($Language->getText('survey_error', 'no_question_found'));
+} else {
+ echo($sh->ShowSurveys($ss, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1));
+}
+
+$sh->footer(array());
+?>
--- /dev/null
+<?php
+/**
+ * GForge Survey HTML Facility
+ *
+ * Copyright 2004 GForge, LLC
+ * http://gforge.org/
+ *
+ */
+
+/*
+ Survey related HTML
+ By Sung Kim, GForge, 02/2004
+*/
+
+require_once('pre.php');
+require_once('note.php');
+
+/**
+ * Survey HTML related functions
+ */
+class SurveyHTML extends Error {
+
+ /**
+ * Dummy constructor
+ */
+ function SurveyHTML() {
+ return true;
+ }
+
+ /**
+ * Show survey header
+ */
+ function header($params) {
+ global $group_id,$is_admin_page,$DOCUMENT_ROOT,$Language,$sys_use_survey;
+
+ if (!$sys_use_survey) {
+ exit_disabled();
+ }
+
+ $params['toptab']='surveys';
+ $params['group']=$group_id;
+
+ if ($project =& group_get_object($group_id)){
+ if (!$project->usesSurvey()) {
+ exit_error($Language->getText('general','error'), $Language->getText('survey_utils','error_this_group_has_turned_off'));
+ }
+
+ site_project_header($params);
+
+ echo "<p><strong><a href=\"/survey/admin/?group_id=$group_id\">".$Language->getText('survey_utils','admin')."</a>";
+
+ if ($is_admin_page && $group_id) {
+ echo " | <a href=\"/survey/admin/survey.php?group_id=$group_id\">".$Language->getText('survey_utils','add_edit_survey')."</a>";
+ echo " | <a href=\"/survey/admin/question.php?group_id=$group_id\">".$Language->getText('survey_utils','add_edit_question')."</a>";
+ echo " | <a href=\"/survey/admin/show_results.php?group_id=$group_id\">".$Language->getText('survey_utils','show_results')."</a></strong>";
+ }
+
+ echo "</p>";
+ }// end if (valid group id)
+ }
+
+ /**
+ * Show Survey footer
+ */
+ function footer($params) {
+ site_project_footer($params);
+ }
+
+ /**
+ * Show Add/Modify Question Forums
+ * @param Survey Question Question Object
+ * Return string
+ */
+ function showAddQuestionForm( &$q ) {
+ global $Language;
+ global $group_id;
+
+ /* Default is add */
+ $title = $Language->getText('survey_add_question','title');
+ $question_button = $Language->getText('survey_add_question','add_this_question');
+
+ /* If we have a question object, it is a Modify */
+ if ($q && is_object($q) && !$q->isError() && $q->getID()) {
+ $title = $Language->getText('survey_edit_question','title');
+ $warning = '<h3><span style="color:red">'.
+ $Language->getText('survey_edit_question','warning_change_after_response').
+ '</span></h3>';
+ $question_id = $q->getID();
+ $question = $q->getQuestion();
+ $question_type = $q->getQuestionType();
+ $question_button = $Language->getText('survey_edit', 'submit_changes');
+ }
+
+ $ret = '<h2>'. $title. '</h2>';
+ $ret.= $warning;
+ $ret.='<form action="'.$PHP_SELF.'" method="post">';
+ $ret.='<input type="hidden" name="post" value="Y" />';
+ $ret.='<input type="hidden" name="group_id" value="'.$group_id.'" />';
+ $ret.='<input type="hidden" name="question_id" value="'.$question_id.'" />';
+
+ $ret.=$Language->getText('survey_add_question','question').':<br />';
+ $ret.='<input type="text" name="question" value="'.$question.'" size="60" maxlength="150" />';
+ $ret.='<p>'. $Language->getText('survey_edit_question','question_type').':<br />';
+
+ $sql="SELECT * from survey_question_types";
+ $result=db_query($sql);
+ $ret.= html_build_select_box($result,'question_type',$question_type,false);
+
+ $ret.='</p><p><input type="submit" name="submit" value="'.$question_button.'"></p>';
+ $ret.='</form>';
+
+ return $ret;
+ }
+
+ /**
+ * Show Add/Modify Question Forums
+ * @param Survey Question Question Object
+ * Return string
+ */
+ function showAddSurveyForm( &$s) {
+ global $Language;
+ global $group_id;
+ global $survey_id;
+
+ /* Default is add */
+ $title = $Language->getText('survey_add_survey','title');
+ $survey_button = $Language->getText('survey_add_survey','add_this_survey');
+ $active = ' checked="checked" ';
+ $inactive = '';
+
+ /* If we have a survey object, it is a Modify */
+ if ($s && is_object($s) && !$s->isError() && $s->getID()) {
+ $title = $Language->getText('survey_edit','title');
+ $warning = '<h3><span style="color:red">'.
+ $Language->getText('survey_edit','warning_survey_after_response').'</span></h3>';
+ $survey_id = $s->getID();
+ $survey_title = $s->getTitle();
+ $survey_questions = $s->getQuestionString();
+ $survey_button = $Language->getText('survey_edit', 'submit_changes');
+ if (!$s->isActive()) {
+ $inactive = 'checked ="checked" ';
+ $active ='';
+ }
+ }
+
+ $ret = '<h2>'. $title. '</h2>';
+ $ret.= $warning;
+ $ret.='<form action="'.$PHP_SELF.'" method="post">';
+ $ret.='<input type="hidden" name="post" value="Y" />';
+ $ret.='<input type="hidden" name="group_id" value="'.$group_id.'" />';
+ $ret.='<input type="hidden" name="survey_id" value="'.$survey_id.'" />';
+ $ret.='<input type="hidden" name="survey_questions" value="'.$survey_questions.'" />';
+
+ $ret.='<strong>'. $Language->getText('survey_add_survey','name_of_survey').'</strong>' .utils_requiredField();
+ $ret.= '<input type="text" name="survey_title" value="'.$survey_title.'" length="60" maxlength="150" /><p>';
+
+ $ret.='<p><strong>'. $Language->getText('survey_add_survey','is_active').'</strong>';
+ $ret.='<br /><input type="radio" name="is_active" value="1"' .$active. '/>'.$Language->getText('survey_add_survey','yes');
+ $ret.='<br /><input type="radio" name="is_active" value="0"' .$inactive. '/>'.$Language->getText('survey_add_survey','no');
+
+ $arr_to_add = & $s->getAddableQuestionInstances();
+ $arr_to_del = & $s->getQuestionInstances();
+
+ if (count($arr_to_add)>0) {
+ $ret.='<p><strong>'. $Language->getText('survey_edit','addable_question').'</strong>';
+ $title_arr[] = " ";
+ $title_arr[] = $Language->getText('survey_index', 'survey_questions');
+ $title_arr[] = " ";
+ $ret.=$GLOBALS['HTML']->listTableTop ($title_arr);
+ }
+
+ for($i = 0; $i < count($arr_to_add); $i++) {
+
+ if ($arr_to_add[$i]->isError()) {
+ echo $arr_to_add[$i]->getErrorMessage();
+ continue;
+ }
+
+ if ($i%3==0) {
+ $ret.= "<tr ". $GLOBALS['HTML']->boxGetAltRowStyle($j++) .">\n";
+ }
+
+ $ret.= '<td><input type="checkbox" name="to_add[]" value="'.$arr_to_add[$i]->getID().'">'.
+ $arr_to_add[$i]->getQuestion().'('.
+ $arr_to_add[$i]->getQuestionStringType().')</td>';
+
+ if ($i%3==2) {
+ $ret.= "</tr>";
+ }
+ }
+
+ if (count($arr_to_add)>0) {
+ /* Fill the remain cells */
+ if ($i%3==1) {
+ $ret.='<td> </td><td> </td></tr>';
+ } else if ($i%3==2) {
+ $ret.='<td> </td></tr>';
+ }
+
+ $ret.= $GLOBALS['HTML']->listTableBottom();
+ }
+
+ /* Deletable questions */
+ if (count($arr_to_del) > 0) {
+ $ret.='<p><strong>'. $Language->getText('survey_edit','existing_question').'</strong>';
+ $title_arr = array('Question ID', 'Question', 'Type', 'Order', 'Delete from this Survey');
+ $ret.=$GLOBALS['HTML']->listTableTop ($title_arr);
+ }
+
+ for($i = 0; $i < count($arr_to_del); $i++) {
+ if ($arr_to_del[$i]->isError()) {
+ echo $arr_to_del[$i]->getErrorMessage();
+ continue;
+ }
+
+
+ $ret.= "<tr ". $GLOBALS['HTML']->boxGetAltRowStyle($i) .">\n";
+
+ $ret.= '<td>'.$arr_to_del[$i]->getID().'</td>';
+ $ret.= '<td>'.$arr_to_del[$i]->getQuestion().'</td>';
+ $ret.= '<td>'.$arr_to_del[$i]->getQuestionStringType().'</td>';
+ $ret.= '<td><center>[<a href="survey.php?group_id='.$group_id.'&survey_id='.
+ $survey_id.'&is_up=1&updown=Y'.
+ '&question_id='.$arr_to_del[$i]->getID().'">Up</a>]';
+ $ret.= '[<a href="/survey/admin/survey.php?group_id='.$group_id.'&survey_id='.
+ $survey_id.'&is_up=0&updown=Y'.
+ '&question_id='.$arr_to_del[$i]->getID().'">Down</a>]</center></td>';
+
+ $ret.= '<td><center><input type="checkbox" name="to_del[]" value="'.$arr_to_del[$i]->getID().'"></center></td>';
+ $ret.= '</tr>';
+
+ }
+
+ if (count($arr_to_del)) {
+ $ret.= $GLOBALS['HTML']->listTableBottom();
+ }
+
+ /* Privous style question input text box. deprecated.
+ $ret.= $Language->getText('survey_add_survey','list_questions_numbers');
+ $ret.='<br /><input type="text" name="survey_questions" value="" length="90" maxlength="1500" /></p>';
+ */
+
+ $ret.='<p><input type="submit" name="submit" value="'.$survey_button.'"></p>';
+ $ret.='</form>';
+
+ return $ret;
+ }
+
+ /**
+ * Show list of questions
+ */
+ function ShowQuestions(&$questions) {
+ global $Language;
+ global $group_id;
+
+ $ret = "<h3>" . count($questions).' '.$Language->getText('survey_show_questions','found',array($rows))."</h3>";
+
+ /* Head information */
+ $title_arr = array ('Question ID', 'Question', 'Type', 'Edit/Delete');
+ $ret.=$GLOBALS['HTML']->listTableTop ($title_arr);
+
+ for($i = 0; $i < count($questions); $i++) {
+ if ($questions[$i]->isError()) {
+ echo $questions[$i]->getErrorMessage();
+ continue;
+ }
+
+ $ret.= "<tr ". $GLOBALS['HTML']->boxGetAltRowStyle($i) .">\n";
+ $ret.= "<td><a href=\"question.php?group_id=$group_id&question_id=".
+ $questions[$i]->getID()."\">".$questions[$i]->getID()."</a></td>\n";
+
+ $ret.= '<td>'.$questions[$i]->getQuestion().'</td>';
+ $ret.= '<td>'.$questions[$i]->getQuestionStringType().'</td>';
+
+ /* Edit/Delete Link */
+ $ret.= "<td>[<a href=\"question.php?group_id=$group_id&question_id=".$questions[$i]->getID().'">';
+ $ret.= $Language->getText('survey_admin', 'edit').'</a>] ';
+ $ret.= "[<a href=\"question.php?delete=Y&group_id=$group_id&question_id=".$questions[$i]->getID().'">';
+ $ret.= $Language->getText('survey_admin', 'delete').'</a>]</td>';
+
+ $ret.= "</tr>";
+ }
+ $ret.= $GLOBALS['HTML']->listTableBottom();
+ return $ret;
+ }
+
+ /**
+ * Show list of surveys
+ *
+ * Show surveys with many options
+ * have to set $user_id to get the right show_vote option
+ *
+ *
+ */
+ function ShowSurveys(&$surveys, $show_id=0, $show_questions=0,
+ $show_number_questions=0, $show_number_votes=0,
+ $show_vote=0, $show_edit=0, $show_result=0,
+ $show_result_graph=0, $show_result_comment=0,
+ $show_inactive=0 ) {
+ global $Language;
+ global $user_id;
+ global $group_id;
+
+ $ret = '<h2>'. $Language->getText('survey_edit','existing_surveys'). '</h2>';
+
+ /* Head information */
+ if ($show_id) {
+ $title_arr[] = $Language->getText('survey_index', 'survey_id');
+ }
+
+ $title_arr[] = $Language->getText('survey_index', 'survey_title');
+
+ if ($show_questions) {
+ $title_arr[] = $Language->getText('survey_index', 'survey_questions');
+ }
+ if ($show_number_questions) {
+ $title_arr[] = $Language->getText('survey_index', 'survey_number_questions');
+ }
+ if ($show_number_votes) {
+ $title_arr[] = $Language->getText('survey_index', 'survey_number_votes');
+ }
+ if ($show_vote && $user_id) {
+ $title_arr[] = $Language->getText('survey_index', 'survey_vote');
+ }
+ if ($show_edit) {
+ $title_arr[] = $Language->getText('survey_index', 'survey_edit');
+ }
+ if ($show_result) {
+ $title_arr[] = $Language->getText('survey_index', 'survey_result');
+ }
+ if ($show_result_graph) {
+ $title_arr[] = $Language->getText('survey_index', 'survey_result_graph');
+ }
+ if ($show_result_comment) {
+ $title_arr[] = $Language->getText('survey_index', 'survey_result_comments');
+ }
+
+ $ret.=$GLOBALS['HTML']->listTableTop ($title_arr);
+
+ /* Color index for table */
+ $color_indxe=0;
+ for($i = 0; $i < count($surveys); $i++) {
+ if ($surveys[$i]->isError()) {
+ echo $surveys[$i]->getErrorMessage();
+ continue;
+ }
+
+ if (!$surveys[$i]->isActive()) {
+ if ($show_inactive) {
+ $strike_open="<strike>";
+ $strike_close="</strike>";
+ } else {
+ continue;
+ }
+ } else {
+ $strike_open="";
+ $strike_close="";
+
+ }
+
+ $ret.= "<tr ". $GLOBALS['HTML']->boxGetAltRowStyle($color_index++) .">\n";
+ if ($show_id) {
+ $ret.= '<td>'.$surveys[$i]->getID().'</td>';
+ }
+
+ $ret.= '<td>'.$strike_open.'<a href="/survey/survey.php?group_id='.$group_id.'&survey_id='.
+ $surveys[$i]->getID().'">'.$surveys[$i]->getTitle().'</A>'.$strike_close.'</td>';
+
+ if ($show_questions) {
+ $ret.= '<td>'.$surveys[$i]->getQuestionString().'</td>';
+ }
+ if ($show_number_questions) {
+ $ret.= '<td>'.$surveys[$i]->getNumberOfQuestions().'</td>';
+ }
+ if ($show_number_votes) {
+ $ret.= '<td>'.$surveys[$i]->getNumberOfVotes().'</td>';
+ }
+ if ($show_vote && $user_id) {
+ if ($surveys[$i]->isUserVote($user_id)) {
+ $ret.='<td>YES</td>';
+ } else {
+ $ret.='<td>NO</td>';
+ }
+ }
+ if ($show_edit) {
+ /* Edit/Delete Link */
+ $ret.= "<td>[<a href=\"/survey/admin/survey.php?group_id=$group_id&survey_id=".
+ $surveys[$i]->getID().'">';
+ $ret.= $Language->getText('survey_admin', 'edit').'</a>] ';
+
+ /* We don;t support delete yet. Need to delete all results as well */
+ /*
+ $ret.= "[<a href=\"/survey/admin/survey.php?delete=Y&group_id=$group_id&survey_id=".
+ $surveys[$i]->getID().'">';
+ $ret.= $Language->getText('survey_admin', 'delete').'</a>]
+ */
+ $ret.='</td>';
+ }
+ if ($show_result) {
+ /* Edit/Delete Link */
+ $ret.= "<td>[<a href=\"/survey/admin/show_results.php?group_id=$group_id&survey_id=".
+ $surveys[$i]->getID().'">';
+ $ret.= $Language->getText('survey_index', 'survey_result').'</a>]</td>';
+ }
+ if ($show_result_graph) {
+ /* Edit/Delete Link */
+ $ret.= "<td>[<a href=\"/survey/admin/show_results.php?graph=yes&group_id=$group_id&survey_id=".
+ $surveys[$i]->getID().'">';
+ $ret.= $Language->getText('survey_index', 'survey_result_graph').'</a>]</td>';
+ }
+ if ($show_result_comment) {
+ /* Edit/Delete Link */
+ $ret.= "<td>[<a href=\"/survey/admin/show_results.php?graph=yes&show_comment=yes&group_id=$group_id&survey_id=".$surveys[$i]->getID().'">';
+ $ret.= $Language->getText('survey_index', 'survey_result_comments').'</a>]</td>';
+ }
+ $ret.= "</tr>\n";
+ }
+
+ $ret.= $GLOBALS['HTML']->listTableBottom();
+ return $ret;
+ }
+
+ /**
+ * Show survey form - Show all forums of Survey
+ */
+ function ShowSurveyForm( &$s ) {
+ global $Language;
+ global $group_id;
+ global $survey_id;
+
+ if (!$s->isActive()) {
+ return '<h3><span style="color:red">'. $Language->getText('survey_error','no_vote_for_inactive').'</span></h3>';
+ }
+ /* Get questions of this survey */
+ $questions = & $s->getQuestionInstances();
+
+ $ret="";
+ if ($s->isUserVote(user_getid())) {
+ $ret.= '<h3><span style="color:red">'. $Language->getText('survey','warning_double_vote').'</span></h3>';
+ }
+ $ret.= '<form action="/survey/survey_resp.php" method="post">'.
+ '<input type="hidden" name="group_id" value="'.$group_id.'" />'.
+ '<input type="hidden" name="survey_id" value="'.$survey_id. '" />';
+
+ $ret.= '<h3>'.$s->getTitle().'</h3>';
+ $ret.= '<table border="0">';
+
+ /* Keep question numbers */
+ $index = 1;
+ for($i = 0; $i < count($questions); $i++) {
+ if ($questions[$i]->isError()) {
+ echo $questions[$i]->getErrorMessage();
+ continue;
+ }
+ $question_type = $questions[$i]->getQuestionType();
+ $question_id = $questions[$i]->getID();
+ $question_title = stripslashes($questions[$i]->getQuestion());
+
+ if ($question_type == '4') {
+ /* Don't show question number if it's just a comment */
+ $ret.='<tr><td valign="top"> </td><td>';
+ } else {
+ $ret.= '<tr><td valign="top"><strong>';
+ /* If it's a 1-5 question box and first in series, move Quest number down a bit */
+ if (($question_type != $last_question_type) && (($question_type == '1') || ($question_type == '3'))) {
+ $ret.= ' <br />';
+ }
+
+ $ret.= $index++.' <br /></td><td>';
+ }
+
+
+
+ switch($question_type) {
+ case 1: /* This is a radio-button question. Values 1-5.
+ Show the 1-5 markers only if this is the first in a series */
+ if ($question_type != $last_question_type) {
+ $ret.=' <strong>1</strong>'.$Language->getText('survey','low').
+ ' <strong>5</strong>' .
+ $Language->getText('survey','high').'<br />';
+ }
+
+ for ($j=1; $j<=5; $j++) {
+ $ret.= '<input type="radio" name="_'.$question_id.'" value="'.$j.'" />';
+ }
+
+ $ret.= ' '.$question_title;
+ break;
+
+ case 2: /* This is a text-area question. */
+ $ret.= $question_title.'<br />';
+ $ret.='<textarea name="_'.$question_id.'" rows="5" cols="60" wrap="soft"></textarea>';
+ break;
+ case 3: /* This is a Yes/No question.
+ Show the Yes/No only if this is the first in a series */
+ if ($question_type != $last_question_type) {
+ $ret.= '<strong>Yes / No</strong><br />';
+ }
+
+ $ret.='<input type="radio" name="_'.$question_id.'" value="1" />';
+ $ret.='<input type="radio" name="_'.$question_id.'" value="5" />';
+ $ret.=' '.$question_title;
+ break;
+ case 4: /* This is a comment only. */
+ $ret.= ' <br /><strong>'.util_make_links($question_title).'</strong>';
+ $ret.= '<input type="hidden" name="_'.$question_id.'" value="-666" />';
+ break;
+ case 5: /* This is a text-field question. */
+ $ret.= $question_title. '<br />';
+ $ret.= '<input type="text" name="_'.$question_id.'" size="20" maxlength="70" />';
+ break;
+ default:
+ $ret.= $question_title. '<br />';
+ }
+
+ $ret.= '</td></tr>';
+ $last_question_type=$question_type;
+ }
+
+ $ret.='<tr><td align="center" colspan="2">'.
+ '<input type="submit" name="submit" value="'.$Language->getText('general','submit').'" />'.
+ '<br /><a href="/survey/privacy.php">'.$Language->getText('survey','survey_privacy').'</a>'.
+ '</td></tr></form></table>';
+
+ return $ret;
+ }
+
+
+ /**
+ * Show survey Result
+ *
+ * @param Object a Survey Response Factory
+ */
+ function ShowResult( &$sr, $show_comment=0, $q_num="", $show_graph=0) {
+ global $Language;
+ global $group_id;
+
+ $Survey = $sr->getSurvey();
+ $Question = $sr->getQuestion();
+
+ $ret='<strong>';
+ if ($q_num) {
+ $ret.= $q_num . '. ';
+ }
+
+ $ret.=$Question->getQuestion().'</strong><br />';
+ $results = $sr->getResults();
+ if ($sr->isError()){
+ echo ($sr->getErrorMessage());
+ }
+
+ $totalCount = $sr->getNumberOfSurveyResponsess();
+ $votes = $Survey->getNumberOfVotes();
+
+ /* No votes, no result to show */
+ if ($votes==0) {
+ $ret.= '<ul><li/>'.$Language->getText('survey_show_results_aggregate','no_votes').'</ul>';
+ return $ret;
+ }
+
+ switch($Question->getQuestionType()) {
+ case 1: /* This is a radio-button question. Values 1-5.
+ Show the 1-5 markers only if this is the first in a series */
+ $arr_name=array('No Answer', 'Low 1', '2', '3', '4', 'High 5', 'No Answer');
+ $arr_color=array('black', 'red', 'blue', 'yellow', 'green', 'brown', 'black');
+ $results[0] = $votes - $results[1] - $results[2] - $results[3] - $results[4] - $results[5];
+
+ if ($show_graph) {
+ $url ='graphs.php?type=vbar';
+ for ($j=5; $j>=0; $j--) {
+ $percent = sprintf("%02.1f%%", (float)$results[$j]*100/$votes);
+
+ $url.='&legend[]='.urlencode($arr_name[$j].' ('. $percent.')');
+ $url.='&value[]='.urlencode($results[$j]);
+ }
+ $ret.= '<img border="0" src="'.$url.'" alt="Graph of '.$Question->getQuestion().'"></img>';
+ } else {
+ $ret.= '<dd><table border="0" cellspacing="0" cellpadding="0" width=100%>';
+
+ for ($j=5; $j>=0; $j--) {
+ $percent = (float)$results[$j]*100/$votes;
+ $ret.= $this->_makeBar($arr_name[$j].' ('.$results[$j].')', $percent, $arr_color[$j]);
+ }
+ $ret.= '</table></dd>';
+ }
+ $ret.='<p/>';
+ break;
+
+ case 3: /* This is a Yes/No question. */
+
+ $arr_name=array('', 'YES', 'NO', 'No Answer');
+ $arr_color=array('', 'red', 'blue', 'black');
+
+ $res[1] = $results[1]; /* Yes */
+ $res[2] = $results[5]; /* No */
+ $res[3] = $votes - $res[1] -$res[2];
+
+ if ($show_graph) {
+ $url ='graphs.php?type=pie';
+ for ($j=1; $j<=3; $j++) {
+ $url.='&legend[]='.urlencode($arr_name[$j].'('.$res[$j].')');
+ $url.='&value[]='.urlencode($res[$j]);
+ }
+ $ret.= '<img border="0" src="'.$url.'" alt="Graph of '.$Question->getQuestion().'"></img>';
+ } else {
+ $ret.= '<dd><table border="0" cellspacing="0" cellpadding="0" width=100%>';
+ for ($j=1; $j<=3; $j++) {
+ $result_per[$j] = (float)$res[$j]*100/$votes;
+ $ret.= $this->_makeBar($arr_name[$j].' ('.$res[$j].')', $result_per[$j], $arr_color[$j]);
+ }
+ $ret.= '</table></dd>';
+ }
+ $ret.='<p/>';
+ break;
+
+ case 4: /* This is a comment only. */
+ break;
+
+ case 2: /* This is a text-area question. */
+ case 5: /* This is a text-field question. */
+ if ($show_comment) {
+ for($j=0; $j<$totalCount; $j++) {
+ $ret.='<hr.><strong>'.$Language->getText('survey_show_results_aggregate','comments').
+ ' # '.($j+1).'/'.$totalCount. '</strong><p/>';
+ $ret.='<pre>'.$results[$j].'</pre>';
+ }
+ } else {
+ $ret.='<ul><li><a href="show_results.php?survey_id='.$Survey->getID().
+ '&question_id='.$Question->getID().
+ '&group_id='.$group_id.'">'.
+ $Language->getText('survey_show_results_aggregate','view_comments', $totalCount).
+ '</a></ul><p/>';
+ }
+
+ break;
+ default:
+ break;
+ }
+
+ return $ret;
+ }
+
+ /**
+ * _makeBar - make Precentage bar as a cell in a table. Starts with <tr> and ends with </tr>
+ *
+ * @param String name Name
+ * @param int percentage of the name
+ * @return string
+ */
+ function _makeBar($name, $percent, $color) {
+ $ret = '<tr><td width="30%">'.$name.'</td><td>';
+ $ret.= '<table width="'.$percent.'%" border="0" cellspacing="0" cellpadding="0"><tr>';
+ if ($percent) {
+ $ret.='<td width="90%" bgcolor="'.$color.'"> </td>';
+ }
+
+ $ret.= '<td>'.sprintf("%.2f", $percent).'%</td></tr></table></td></tr\>'."\n";
+
+ return $ret;
+ }
+}
+
+// Local Variables:
+// mode: php
+// c-file-style: "bsd"
+// End:
+?>
*
*/
-
require_once('pre.php');
-require_once('vote_function.php');
-require_once('www/survey/survey_utils.php');
+require_once('common/survey/SurveyFactory.class');
+require_once('www/survey/include/SurveyHTML.class');
+/* We need a group_id */
if (!$group_id) {
- echo "<h1>".$Language->getText('survey_index','for_some_reason')."</h1>";
+ exit_no_group();
}
-survey_header(array('title'=>$Language->getText('survey_index','title'),'pagename'=>'survey','titlevals'=>array(group_getname($group_id))));
-
-Function ShowResultsGroupSurveys($result) {
- global $group_id;
- global $Language;
- $rows = db_numrows($result);
- $cols = db_numfields($result);
- $title_arr=array();
- $title_arr[]=$Language->getText('survey_index','survey_title');
- echo $GLOBALS['HTML']->listTableTop ($title_arr);
-
- for($j=0; $j<$rows; $j++) {
- echo "<tr ". $GLOBALS['HTML']->boxGetAltRowStyle($j) .">\n";
- for ($i=1; $i<$cols; $i++) {
- echo "<td><a href=\"survey.php?group_id=$group_id&survey_id=".db_result($result,$j,"survey_id")."\">";
- printf("%s",db_result($result,$j,$i));
- echo "</a></td>\n";
- }
- echo "</tr>";
- }
- echo $GLOBALS['HTML']->listTableBottom();
+$g =& group_get_object($group_id);
+if (!$g || !is_object($g) || $g->isError()) {
+ exit_no_group();
}
-$sql="SELECT survey_id,survey_title FROM surveys WHERE group_id='$group_id' AND is_active='1'";
+$user_id = user_getid();
-$result=db_query($sql);
+/* Show header */
+$sh = new SurveyHtml();
+$sh->header(array('title'=>$Language->getText('survey_index','title'),'pagename'=>'survey','titlevals'=>array(group_getname($group_id))));
-if (!$result || db_numrows($result) < 1) {
- echo "<h2>".$Language->getText('survey_index','this_group_has')."</h2>";
- echo db_error();
+/* Show list of Servey */
+$sf = new SurveyFactory($g);
+$ss = & $sf->getSurveys();
+if (!$ss) {
+ echo ($Language->getText('survey_error', 'no_survey_found'));
} else {
- ShowResultsGroupSurveys($result);
+ echo($sh->showSurveys($ss, 0, 0, 1, 1, 1, 0));
}
-survey_footer(array());
-
+$sh->footer(array());
?>
require_once('pre.php');
-require_once('vote_function.php');
-require_once('www/survey/survey_utils.php');
+require_once('common/survey/Survey.class');
+require_once('www/survey/include/SurveyHTML.class');
+
+
+/* We need a group_id */
+if (!$group_id) {
+ exit_no_group();
+}
+
+$g =& group_get_object($group_id);
+if (!$g || !is_object($g) || $g->isError()) {
+ exit_no_group();
+}
// Check to make sure they're logged in.
if (!session_loggedin()) {
exit_not_logged_in();
}
-survey_header(array('title'=>$Language->getText('survey','title'),'pagename'=>'survey_survey'));
+$sh = new SurveyHtml();
+$s = new Survey($g, $survey_id);
+
+$sh->header(array('title'=>$Language->getText('survey','title'),'pagename'=>'survey_survey'));
-if (!$survey_id || !$group_id) {
- echo "<h1>".$Language->getText('survey','for_some_reason')."</h1>";
+if (!$survey_id) {
+ echo "<h1>".$Language->getText('survey','for_some_reason')."</h1>";
} else {
- show_survey($group_id,$survey_id);
+ echo($sh->ShowSurveyForm($s));
}
-survey_footer(array());
+$sh->footer(array());
?>
require_once('pre.php');
-require_once('www/survey/survey_utils.php');
+require_once('common/survey/Survey.class');
+require_once('common/survey/SurveyResponse.class');
+require_once('www/survey/include/SurveyHTML.class');
-survey_header(array('title'=>$Language->getText('survey_resp','title'),'pagename'=>'survey_survey_resp'));
+$g =& group_get_object($group_id);
+if (!$g || !is_object($g) || $g->isError()) {
+ exit_no_group();
+}
+
+$sh = new SurveyHtml();
+$sh->header(array('title'=>$Language->getText('survey_resp','title'),'pagename'=>'survey_survey_resp'));
-if (!$survey_id || !$group_id) {
+if (!$survey_id) {
/*
Quit if params are not provided
*/
echo "<h1>".$Language->getText('survey_resp','error_some_reason')."</h1>";
- survey_footer(array());
+ $sh->footer(array());
exit;
}
*/
echo "<h1>".$Language->getText('survey_resp','you_nedd_to_be_logged_in')."</h1>";
echo "<p>".$Language->getText('survey_resp','unfortunately_you_have_to_be')."</p>";
- survey_footer(array());
+ $sh->footer(array());
exit;
}
/*
Delete this customer's responses in case they had back-arrowed
*/
-
$result=db_query("DELETE FROM survey_responses WHERE survey_id='" . addslashes($survey_id) . "' AND group_id='" . addslashes($group_id) . "' AND user_id='".user_getid()."'");
/*
Select this survey from the database
*/
-
-$sql="select * from surveys where survey_id='$survey_id'";
-
-$result=db_query($sql);
-
-/*
- Select the questions for this survey
-*/
-$questions = db_result($result, 0, "survey_questions");
-$questions = str_replace(" ", "", $questions);
-$quest_array=explode(',', $questions);
+$s = new Survey($g, $survey_id);
+$quest_array= & $s->getQuestionArray();
$count=count($quest_array);
$now=time();
-for ($i=0; $i<$count; $i++) {
+/* Make a dummy SurveyResponses for creating */
+$sr = new SurveyResponse($g);
- /*
- Insert each form value into the responses table
- */
-
- $val="_" . $quest_array[$i];
-
- $sql="INSERT INTO survey_responses (user_id,group_id,survey_id,question_id,response,post_date) ".
- "VALUES ('".user_getid()."','" . addslashes($group_id) . "','" . addslashes($survey_id) . "','" . addslashes($quest_array[$i]) . "','". htmlspecialchars(addslashes($$val)) . "','$now')";
- $result=db_query($sql);
- if (!$result) {
- echo $Language->getText('survey_resp','error');
- }
+for ($i=0; $i<$count; $i++) {
+ /* Insert each form value into the responses table */
+
+ $val="_" . $quest_array[$i];
+ $response = $$val;
+
+ $sr->create(user_getid(), $survey_id, $quest_array[$i], $response);
+ if ($sr->isError()) {
+ echo $sr->getErrorMessage();
+ }
}
-survey_footer(array());
+$sh->footer(array());
?>