3 * GForge Survey Question Facility
5 * Copyright 2004 GForge, LLC
8 * This file is part of GForge.
10 * GForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * GForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with GForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 require_once('common/include/Error.class');
33 class SurveyQuestion extends Error {
36 * Associative array of data from db.
38 * @var array $data_array.
47 var $Group; //group object
52 * @param object The Group object to which this Survey Question is associated.
53 * @param int The questtion_id.
54 * @param array The associative array of data.
55 * @return boolean success.
57 function SurveyQuestion(&$Group, $question_id=false, $arr=false) {
60 if (!$Group || !is_object($Group)) {
61 $this->setError(sprintf(_('%1$s:: No Valid Group Object'), 'Survey Question'));
64 if ($Group->isError()) {
65 $this->setError('Survey:: '.$Group->getErrorMessage());
68 $this->Group =& $Group;
71 if (!$arr || !is_array($arr)) {
72 if (!$this->fetchData($question_id)) {
76 $this->data_array =& $arr;
77 if ($this->data_array['group_id'] != $this->Group->getID()) {
78 $this->setError(_('Group_id in db result does not match Group Object'));
79 $this->data_array = null;
88 * create - use this function to create a survey question
90 * @param string The question
91 * @param int The question type
92 * 1: Radio Buttons 1-5
94 * 3: Radio Buttons Yes/No
98 * @return boolean success.
100 function create($question, $question_type=1) {
103 if (strlen($question) < 3) {
104 $this->setError(_('Question is too short'));
107 // Current permissions check.
108 // permission should be checked in higer level to faciliate usability
111 $group_id = $this->Group->GetID();
112 $sql="INSERT INTO survey_questions (group_id,question,question_type)
113 VALUES ($group_id,'".htmlspecialchars($question)."',$question_type)";
117 $this->setError(_('Question Added').db_error());
121 /* Load question to data array */
122 $question_id=db_insertid($res,'survey_questions','question_id');
123 return $this->fetchData($question_id);
129 * update - use this function to update a survey question
131 * @param string The question
132 * @param int The question type
133 * 1: Radio Buttons 1-5
135 * 3: Radio Buttons Yes/No
139 * @return boolean success.
141 function update($question, $question_type=1) {
144 if (strlen($question) < 3) {
145 $this->setError(_('Question is too short'));
148 // Current permissions check.
149 // permission should be checked in higer level to faciliate usability
152 $group_id = $this->Group->GetID();
153 $question_id = $this->getID();
155 $sql="UPDATE survey_questions SET question='".htmlspecialchars($question)."',
156 question_type='$question_type'
157 where question_id='$question_id' AND group_id='$group_id'";
160 if (!$res || db_affected_rows($res) < 1) {
161 $this->setError(_('UPDATE FAILED').db_error());
164 return $this->fetchData($question_id);
168 * delete - use this function to delete a survey question
170 * @return boolean success.
176 $group_id = $this->Group->GetID();
177 $question_id = $this->getID();
179 $sql="DELETE FROM survey_questions where question_id='$question_id' AND group_id='$group_id'";
182 if (!$res || db_affected_rows($res) < 1) {
183 $this->setError(_('MISSINGTEXT:survey_edit_question/delete_failed:TEXTMISSING').db_error());
187 $this->data_array = null;
192 * fetchData - re-fetch the data for this survey question from the database.
194 * @param int The survey question_id.
195 * @return boolean success.
197 function fetchData($question_id) {
199 $group_id = $this->Group->GetID();
201 $sql="SELECT survey_questions.*, survey_question_types.type
202 FROM survey_questions ,survey_question_types
203 WHERE survey_question_types.id=survey_questions.question_type
204 AND survey_questions.question_id='$question_id'
205 AND survey_questions.group_id='$group_id'";
208 if (!$res || db_numrows($res) < 1) {
209 $this->setError(_('Error finding question').db_error());
212 $this->data_array =& db_fetch_array($res);
213 db_free_result($res);
218 * getGroup - get the Group object this SurveyQuestion is associated with.
220 * @return object The Group object.
222 function &getGroup() {
227 * getID - Get the id of this Survey Question
229 * @return int The question_id
232 return $this->data_array['question_id'];
237 * getQuestion - Get the question
239 * @return string the question
241 function getQuestion() {
242 return $this->data_array['question'];
247 * getQuestionType - Get the question type
249 * @return int the question type
251 function getQuestionType() {
252 return $this->data_array['question_type'];
257 * getQuestionStringType - Get the type from survey_question_types
259 * @return String the question type
261 function getQuestionStringType() {
262 return $this->data_array['type'];
268 // c-file-style: "bsd"