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.
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)) {
56 if ($Group->isError()) {
57 $this->setError('Survey:: '.$Group->getErrorMessage());
60 $this->Group =& $Group;
63 if (!$arr || !is_array($arr)) {
64 if (!$this->fetchData($question_id)) {
68 $this->data_array =& $arr;
69 if ($this->data_array['group_id'] != $this->Group->getID()) {
70 $this->setError(_('Group_id in db result does not match Group Object'));
71 $this->data_array = null;
79 * create - use this function to create a survey question
81 * @param string The question
82 * @param int The question type
83 * 1: Radio Buttons 1-5
85 * 3: Radio Buttons Yes/No
89 * @return boolean success.
91 function create($question, $question_type = 1) {
92 if (strlen($question) < 3) {
93 $this->setError(_('Question is too short'));
96 // Current permissions check.
97 // permission should be checked in higer level to faciliate usability
100 $group_id = $this->Group->GetID();
102 $res = db_query_params('INSERT INTO survey_questions (group_id,question,question_type) VALUES ($1,$2,$3)',
104 htmlspecialchars($question),
107 $this->setError(_('Question Added').db_error());
111 /* Load question to data array */
112 $question_id = db_insertid($res,'survey_questions','question_id');
113 return $this->fetchData($question_id);
119 * update - use this function to update a survey question
121 * @param string The question
122 * @param int The question type
123 * 1: Radio Buttons 1-5
125 * 3: Radio Buttons Yes/No
129 * @return boolean success.
131 function update($question, $question_type = 1) {
132 if (strlen($question) < 3) {
133 $this->setError(_('Question is too short'));
136 // Current permissions check.
137 // permission should be checked in higer level to faciliate usability
140 $group_id = $this->Group->GetID();
141 $question_id = $this->getID();
143 $res = db_query_params('UPDATE survey_questions SET question=$1, question_type=$2 where question_id=$3 AND group_id=$4',
144 array(htmlspecialchars($question),
148 if (!$res || db_affected_rows($res) < 1) {
149 $this->setError(_('Update failed').db_error());
152 return $this->fetchData($question_id);
156 * delete - use this function to delete a survey question
158 * @return boolean success.
161 $group_id = $this->Group->GetID();
162 $question_id = $this->getID();
164 $res = db_query_params('DELETE FROM survey_questions where question_id=$1 AND group_id=$2',
167 if (!$res || db_affected_rows($res) < 1) {
168 $this->setError(_('Delete failed').db_error());
172 $this->data_array = null;
177 * fetchData - re-fetch the data for this survey question from the database.
179 * @param int The survey question_id.
180 * @return boolean success.
182 function fetchData($question_id) {
183 $group_id = $this->Group->GetID();
185 $res = db_query_params('SELECT survey_questions.*, survey_question_types.type
186 FROM survey_questions ,survey_question_types
187 WHERE survey_question_types.id=survey_questions.question_type
188 AND survey_questions.question_id=$1
189 AND survey_questions.group_id=$2',
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'];
221 * getQuestion - Get the question
223 * @return string the question
225 function getQuestion() {
226 return $this->data_array['question'];
231 * getQuestionType - Get the question type
233 * @return int the question type
235 function getQuestionType() {
236 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"