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/FFError.class.php';
27 class SurveyQuestion extends FFError {
30 * Associative array of data from db.
32 * @var array $data_array.
45 * @param bool $question_id
48 function __construct(&$Group, $question_id = false, $arr = false) {
49 parent::__construct();
50 if (!$Group || !is_object($Group)) {
51 $this->setError(_('Invalid Project'));
54 if ($Group->isError()) {
55 $this->setError('Survey: '.$Group->getErrorMessage());
58 $this->Group =& $Group;
61 if (!$arr || !is_array($arr)) {
62 if (!$this->fetchData($question_id)) {
66 $this->data_array =& $arr;
67 if ($this->data_array['group_id'] != $this->Group->getID()) {
68 $this->setError(_('group_id in db result does not match Group Object'));
69 $this->data_array = null;
77 * create - use this function to create a survey question
79 * @param string $question The question
80 * @param int $question_type The question type
81 * 1: Radio Buttons 1-5
83 * 3: Radio Buttons Yes/No
87 * @return bool success.
89 function create($question, $question_type = 1) {
90 if (strlen($question) < 3) {
91 $this->setError(_('Question is too short'));
94 // Current permissions check.
95 // permission should be checked in higher level to facilitate usability
98 $group_id = $this->Group->GetID();
100 $res = db_query_params('INSERT INTO survey_questions (group_id,question,question_type) VALUES ($1,$2,$3)',
102 htmlspecialchars($question),
105 $this->setError(_('Question Added').db_error());
109 /* Load question to data array */
110 $question_id = db_insertid($res,'survey_questions','question_id');
111 return $this->fetchData($question_id);
115 * update - use this function to update a survey question
117 * @param string $question The question
118 * @param int $question_type The question type
119 * 1: Radio Buttons 1-5
121 * 3: Radio Buttons Yes/No
125 * @return bool success.
127 function update($question, $question_type = 1) {
128 if (strlen($question) < 3) {
129 $this->setError(_('Question is too short'));
132 // Current permissions check.
133 // permission should be checked in higher level to facilitate usability
136 $group_id = $this->Group->GetID();
137 $question_id = $this->getID();
139 $res = db_query_params('UPDATE survey_questions SET question=$1, question_type=$2 where question_id=$3 AND group_id=$4',
140 array(htmlspecialchars($question),
144 if (!$res || db_affected_rows($res) < 1) {
145 $this->setError(_('Update failed').db_error());
148 return $this->fetchData($question_id);
152 * delete - use this function to delete a survey question
154 * @return bool success.
157 $group_id = $this->Group->GetID();
158 $question_id = $this->getID();
160 $res = db_query_params('DELETE FROM survey_questions where question_id=$1 AND group_id=$2',
163 if (!$res || db_affected_rows($res) < 1) {
164 $this->setError(_('Delete failed').db_error());
168 $this->data_array = null;
173 * fetchData - re-fetch the data for this survey question from the database.
175 * @param int $question_id The survey question_id.
176 * @return bool success.
178 function fetchData($question_id) {
179 $group_id = $this->Group->GetID();
181 $res = db_query_params('SELECT survey_questions.*, survey_question_types.type
182 FROM survey_questions ,survey_question_types
183 WHERE survey_question_types.id=survey_questions.question_type
184 AND survey_questions.question_id=$1
185 AND survey_questions.group_id=$2',
189 if (!$res || db_numrows($res) < 1) {
190 $this->setError(_('Error finding question').db_error());
193 $this->data_array = db_fetch_array($res);
194 db_free_result($res);
199 * getGroup - get the Group object this SurveyQuestion is associated with.
201 * @return object The Group object.
203 function &getGroup() {
208 * getID - Get the id of this Survey Question
210 * @return int The question_id
213 if (isset($this->data_array['question_id'])) {
214 return $this->data_array['question_id'];
220 * getQuestion - Get the question
222 * @return string the question
224 function getQuestion() {
225 return $this->data_array['question'];
229 * getQuestionType - Get the question type
231 * @return int the question type
233 function getQuestionType() {
234 return $this->data_array['question_type'];
238 * getQuestionStringType - Get the type from survey_question_types
240 * @return string the question type
242 function getQuestionStringType() {
243 return $this->data_array['type'];
249 // c-file-style: "bsd"