5 * Copyright 2004, Sung Kim/GForge, LLC
7 * This file is part of FusionForge.
9 * FusionForge is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published
11 * by the Free Software Foundation; either version 2 of the License,
12 * or (at your option) any later version.
14 * FusionForge is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with FusionForge; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 require_once $gfcommon.'include/Error.class.php';
27 class SurveyQuestion extends Error {
30 * Associative array of data from db.
32 * @var array $data_array.
41 var $Group; //group object
46 * @param object The Group object to which this Survey Question is associated.
47 * @param int The questtion_id.
48 * @param array The associative array of data.
49 * @return boolean success.
51 function SurveyQuestion(&$Group, $question_id=false, $arr=false) {
53 if (!$Group || !is_object($Group)) {
54 $this->setError(sprintf(_('%1$s:: No Valid Group Object'), 'Survey Question'));
57 if ($Group->isError()) {
58 $this->setError('Survey:: '.$Group->getErrorMessage());
61 $this->Group =& $Group;
64 if (!$arr || !is_array($arr)) {
65 if (!$this->fetchData($question_id)) {
69 $this->data_array =& $arr;
70 if ($this->data_array['group_id'] != $this->Group->getID()) {
71 $this->setError(_('Group_id in db result does not match Group Object'));
72 $this->data_array = null;
81 * create - use this function to create a survey question
83 * @param string The question
84 * @param int The question type
85 * 1: Radio Buttons 1-5
87 * 3: Radio Buttons Yes/No
91 * @return boolean success.
93 function create($question, $question_type=1) {
94 if (strlen($question) < 3) {
95 $this->setError(_('Question is too short'));
98 // Current permissions check.
99 // permission should be checked in higer level to faciliate usability
102 $group_id = $this->Group->GetID();
103 $sql="INSERT INTO survey_questions (group_id,question,question_type)
104 VALUES ($group_id,'".htmlspecialchars($question)."',$question_type)";
108 $this->setError(_('Question Added').db_error());
112 /* Load question to data array */
113 $question_id=db_insertid($res,'survey_questions','question_id');
114 return $this->fetchData($question_id);
120 * update - use this function to update a survey question
122 * @param string The question
123 * @param int The question type
124 * 1: Radio Buttons 1-5
126 * 3: Radio Buttons Yes/No
130 * @return boolean success.
132 function update($question, $question_type=1) {
133 if (strlen($question) < 3) {
134 $this->setError(_('Question is too short'));
137 // Current permissions check.
138 // permission should be checked in higer level to faciliate usability
141 $group_id = $this->Group->GetID();
142 $question_id = $this->getID();
144 $sql="UPDATE survey_questions SET question='".htmlspecialchars($question)."',
145 question_type='$question_type'
146 where question_id='$question_id' AND group_id='$group_id'";
149 if (!$res || db_affected_rows($res) < 1) {
150 $this->setError(_('UPDATE FAILED').db_error());
153 return $this->fetchData($question_id);
157 * delete - use this function to delete a survey question
159 * @return boolean success.
162 $group_id = $this->Group->GetID();
163 $question_id = $this->getID();
165 $sql="DELETE FROM survey_questions where question_id='$question_id' AND group_id='$group_id'";
168 if (!$res || db_affected_rows($res) < 1) {
169 $this->setError(_('Delete failed').db_error());
173 $this->data_array = null;
178 * fetchData - re-fetch the data for this survey question from the database.
180 * @param int The survey question_id.
181 * @return boolean success.
183 function fetchData($question_id) {
184 $group_id = $this->Group->GetID();
186 $sql="SELECT survey_questions.*, survey_question_types.type
187 FROM survey_questions ,survey_question_types
188 WHERE survey_question_types.id=survey_questions.question_type
189 AND survey_questions.question_id='$question_id'
190 AND survey_questions.group_id='$group_id'";
193 if (!$res || db_numrows($res) < 1) {
194 $this->setError(_('Error finding question').db_error());
197 $this->data_array =& db_fetch_array($res);
198 db_free_result($res);
203 * getGroup - get the Group object this SurveyQuestion is associated with.
205 * @return object The Group object.
207 function &getGroup() {
212 * getID - Get the id of this Survey Question
214 * @return int The question_id
217 return $this->data_array['question_id'];
222 * getQuestion - Get the question
224 * @return string the question
226 function getQuestion() {
227 return $this->data_array['question'];
232 * getQuestionType - Get the question type
234 * @return int the question type
236 function getQuestionType() {
237 return $this->data_array['question_type'];
242 * getQuestionStringType - Get the type from survey_question_types
244 * @return String the question type
246 function getQuestionStringType() {
247 return $this->data_array['type'];
253 // c-file-style: "bsd"