5 * Copyright 2004, Sung Kim/GForge, LLC
6 * Copyright 2009, Roland Mas
7 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge 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 along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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.
47 * @param bool $question_id
49 * @internal param \The $object Group object to which this Survey Question is associated.
50 * @internal param \The $int question_id.
51 * @internal param \The $array associative array of data.
52 * @return \SurveyQuestion
54 function __construct(&$Group, $question_id = false, $arr = false) {
56 if (!$Group || !is_object($Group)) {
57 $this->setError(_('No Valid Group Object'));
60 if ($Group->isError()) {
61 $this->setError('Survey:: '.$Group->getErrorMessage());
64 $this->Group =& $Group;
67 if (!$arr || !is_array($arr)) {
68 if (!$this->fetchData($question_id)) {
72 $this->data_array =& $arr;
73 if ($this->data_array['group_id'] != $this->Group->getID()) {
74 $this->setError(_('group_id in db result does not match Group Object'));
75 $this->data_array = null;
83 * create - use this function to create a survey question
85 * @param string $question The question
86 * @param int $question_type The question type
87 * 1: Radio Buttons 1-5
89 * 3: Radio Buttons Yes/No
93 * @return bool success.
95 function create($question, $question_type = 1) {
96 if (strlen($question) < 3) {
97 $this->setError(_('Question is too short'));
100 // Current permissions check.
101 // permission should be checked in higher level to facilitate usability
104 $group_id = $this->Group->GetID();
106 $res = db_query_params('INSERT INTO survey_questions (group_id,question,question_type) VALUES ($1,$2,$3)',
108 htmlspecialchars($question),
111 $this->setError(_('Question Added').db_error());
115 /* Load question to data array */
116 $question_id = db_insertid($res,'survey_questions','question_id');
117 return $this->fetchData($question_id);
121 * update - use this function to update a survey question
123 * @param string $question The question
124 * @param int $question_type The question type
125 * 1: Radio Buttons 1-5
127 * 3: Radio Buttons Yes/No
131 * @return boolean success.
133 function update($question, $question_type = 1) {
134 if (strlen($question) < 3) {
135 $this->setError(_('Question is too short'));
138 // Current permissions check.
139 // permission should be checked in higher level to facilitate usability
142 $group_id = $this->Group->GetID();
143 $question_id = $this->getID();
145 $res = db_query_params('UPDATE survey_questions SET question=$1, question_type=$2 where question_id=$3 AND group_id=$4',
146 array(htmlspecialchars($question),
150 if (!$res || db_affected_rows($res) < 1) {
151 $this->setError(_('Update failed').db_error());
154 return $this->fetchData($question_id);
158 * delete - use this function to delete a survey question
160 * @return boolean success.
163 $group_id = $this->Group->GetID();
164 $question_id = $this->getID();
166 $res = db_query_params('DELETE FROM survey_questions where question_id=$1 AND group_id=$2',
169 if (!$res || db_affected_rows($res) < 1) {
170 $this->setError(_('Delete failed').db_error());
174 $this->data_array = null;
179 * fetchData - re-fetch the data for this survey question from the database.
181 * @param int $question_id The survey question_id.
182 * @return bool success.
184 function fetchData($question_id) {
185 $group_id = $this->Group->GetID();
187 $res = db_query_params('SELECT survey_questions.*, survey_question_types.type
188 FROM survey_questions ,survey_question_types
189 WHERE survey_question_types.id=survey_questions.question_type
190 AND survey_questions.question_id=$1
191 AND survey_questions.group_id=$2',
195 if (!$res || db_numrows($res) < 1) {
196 $this->setError(_('Error finding question').db_error());
199 $this->data_array = db_fetch_array($res);
200 db_free_result($res);
205 * getGroup - get the Group object this SurveyQuestion is associated with.
207 * @return object The Group object.
209 function &getGroup() {
214 * getID - Get the id of this Survey Question
216 * @return int The question_id
219 return $this->data_array['question_id'];
223 * getQuestion - Get the question
225 * @return string the question
227 function getQuestion() {
228 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'];
241 * getQuestionStringType - Get the type from survey_question_types
243 * @return string the question type
245 function getQuestionStringType() {
246 return $this->data_array['type'];
252 // c-file-style: "bsd"