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 The question
86 * @param int The question type
87 * 1: Radio Buttons 1-5
89 * 3: Radio Buttons Yes/No
93 * @return boolean 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 higer level to faciliate 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);
123 * update - use this function to update a survey question
125 * @param string The question
126 * @param int The question type
127 * 1: Radio Buttons 1-5
129 * 3: Radio Buttons Yes/No
133 * @return boolean success.
135 function update($question, $question_type = 1) {
136 if (strlen($question) < 3) {
137 $this->setError(_('Question is too short'));
140 // Current permissions check.
141 // permission should be checked in higer level to faciliate usability
144 $group_id = $this->Group->GetID();
145 $question_id = $this->getID();
147 $res = db_query_params('UPDATE survey_questions SET question=$1, question_type=$2 where question_id=$3 AND group_id=$4',
148 array(htmlspecialchars($question),
152 if (!$res || db_affected_rows($res) < 1) {
153 $this->setError(_('Update failed').db_error());
156 return $this->fetchData($question_id);
160 * delete - use this function to delete a survey question
162 * @return boolean success.
165 $group_id = $this->Group->GetID();
166 $question_id = $this->getID();
168 $res = db_query_params('DELETE FROM survey_questions where question_id=$1 AND group_id=$2',
171 if (!$res || db_affected_rows($res) < 1) {
172 $this->setError(_('Delete failed').db_error());
176 $this->data_array = null;
181 * fetchData - re-fetch the data for this survey question from the database.
183 * @param int The survey question_id.
184 * @return boolean success.
186 function fetchData($question_id) {
187 $group_id = $this->Group->GetID();
189 $res = db_query_params('SELECT survey_questions.*, survey_question_types.type
190 FROM survey_questions ,survey_question_types
191 WHERE survey_question_types.id=survey_questions.question_type
192 AND survey_questions.question_id=$1
193 AND survey_questions.group_id=$2',
197 if (!$res || db_numrows($res) < 1) {
198 $this->setError(_('Error finding question').db_error());
201 $this->data_array = db_fetch_array($res);
202 db_free_result($res);
207 * getGroup - get the Group object this SurveyQuestion is associated with.
209 * @return object The Group object.
211 function &getGroup() {
216 * getID - Get the id of this Survey Question
218 * @return int The question_id
221 return $this->data_array['question_id'];
225 * getQuestion - Get the question
227 * @return string the question
229 function getQuestion() {
230 return $this->data_array['question'];
235 * getQuestionType - Get the question type
237 * @return int the question type
239 function getQuestionType() {
240 return $this->data_array['question_type'];
245 * getQuestionStringType - Get the type from survey_question_types
247 * @return string the question type
249 function getQuestionStringType() {
250 return $this->data_array['type'];
256 // c-file-style: "bsd"