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.
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 __construct(&$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;
80 * create - use this function to create a survey question
82 * @param string The question
83 * @param int The question type
84 * 1: Radio Buttons 1-5
86 * 3: Radio Buttons Yes/No
90 * @return boolean success.
92 function create($question, $question_type = 1) {
93 if (strlen($question) < 3) {
94 $this->setError(_('Question is too short'));
97 // Current permissions check.
98 // permission should be checked in higer level to faciliate usability
101 $group_id = $this->Group->GetID();
103 $res = db_query_params('INSERT INTO survey_questions (group_id,question,question_type) VALUES ($1,$2,$3)',
105 htmlspecialchars($question),
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 $res = db_query_params('UPDATE survey_questions SET question=$1, question_type=$2 where question_id=$3 AND group_id=$4',
145 array(htmlspecialchars($question),
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 $res = db_query_params('DELETE FROM survey_questions where question_id=$1 AND group_id=$2',
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 $res = db_query_params('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=$1
190 AND survey_questions.group_id=$2',
194 if (!$res || db_numrows($res) < 1) {
195 $this->setError(_('Error finding question').db_error());
198 $this->data_array = db_fetch_array($res);
199 db_free_result($res);
204 * getGroup - get the Group object this SurveyQuestion is associated with.
206 * @return object The Group object.
208 function &getGroup() {
213 * getID - Get the id of this Survey Question
215 * @return int The question_id
218 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"