5 * Copyright 2004, Sung Kim/GForge, LLC
6 * Copyright 2009, Roland Mas
7 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
8 * Copyright 2013, Franck Villaume - TrivialDev
10 * This file is part of FusionForge. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 require_once $gfcommon.'include/Error.class.php';
27 require_once $gfcommon.'survey/SurveyResponse.class.php';
29 class SurveyResponseFactory extends Error {
34 * @var object $Survey.
39 * The Question object.
41 * @var object $Question.
53 * The Aggregated Result array for question.
60 * The Aggregated Results array for all questions.
69 * @param object The Survey object
70 * @param object The Question object to which this survey Response is associated.
71 * @param int The survey_id
73 function __construct(&$Survey, &$Question ) {
76 if (!$Survey || !is_object($Survey)) {
77 $this->setError(_('No valid Survey Object'));
80 if ($Survey->isError()) {
81 $this->setError(_('Survey').':: '.$Survey->getErrorMessage());
84 if (!$Question || !is_object($Question)) {
85 $this->setError(_('No valid Question Object'));
88 if ($Question->isError()) {
89 $this->setError(_('Survey').':: '.$Question->getErrorMessage());
93 $this->Survey = &$Survey;
94 $this->Question = &$Question;
98 * getGroup - get the Group object this SurveyResponse is associated with.
100 * @return object The Group object.
102 function &getGroup() {
103 $Survey = $this->getSurvey();
104 return $Survey->Group;
108 * getSurvey - get the Survey object this SurveyResponse is associated with.
110 * @return object The Survey object.
112 function &getSurvey() {
113 return $this->Survey;
117 * getQuestion - get the Question object this SurveyResponse is associated with.
119 * @return object The Question object.
121 function &getQuestion() {
122 return $this->Question;
126 * getSurveyResponses - get an array of Survey Response objects
127 * for the Survey and Question
129 * @return array The array of Survey Response objects.
131 function getSurveyResponses() {
132 /* We alread have it */
133 if ($this->Responses) {
134 return $this->Responses;
137 $group = $this->getGroup();
138 $group_id = $group->GetID();
139 $survey = $this->getSurvey();
140 $survey_id = $survey->GetID();
141 $question = $this->getQuestion();
142 $question_id = $question->GetID();
144 $result = db_query_params ('SELECT * FROM survey_responses WHERE survey_id=$1 AND question_id=$2 AND group_id=$3 ORDER BY post_date DESC',
149 $this->setError(_('No Survey Response is found').db_error());
152 while ($arr = db_fetch_array($result)) {
153 $this->Responses[] = new SurveyResponse($this->getGroup(), $arr);
155 db_free_result($result);
157 return $this->Responses;
161 * getSurveyAllResponses - get an array of Survey Response objects
162 * for the Survey and Question
164 * @return array The array of Survey Response objects.
166 function getSurveyAllResponses() {
167 /* We alread have it */
168 if ($this->Responses) {
169 return $this->Responses;
172 $group = $this->getGroup();
173 $group_id = $group->GetID();
174 $survey = $this->getSurvey();
175 $survey_id = $survey->GetID();
177 $result = db_query_params('SELECT * FROM survey_responses WHERE survey_id=$1 AND group_id=$2 ORDER BY post_date DESC',
178 array($survey_id, $group_id));
180 $this->setError(_('No Survey Response is found').db_error());
183 while ($arr = db_fetch_array($result)) {
184 $this->Responses[] = new SurveyResponse($this->getGroup(), $arr);
186 db_free_result($result);
188 return $this->Responses;
193 * getNumberOfSurveyResponses - get the number of Survey Responses
195 * @return int the number of survey responses
197 function getNumberOfSurveyResponses() {
198 $arr = $this->getSurveyResponses();
199 if (!$arr || !is_array($arr)) {
207 * getResults - get the array of result for yes/no and 1-5 question
209 * @return int the array of result
210 * for the yes/no question, it returns counts in arr[1] and arr[5];
211 * for the 1-5 question, it returns counts in arr[1], arr[1], ..., arr[5];
212 * for comments, we return arr[1], ...arr[n] with comments
214 function getResults() {
216 return $this->Result;
219 $arr = $this->getSurveyResponses();
220 if (!$arr || !is_array($arr)) {
223 $count = count($arr);
225 $question = $this->getQuestion();
226 if ($question->getQuestionType()=='1' ||
227 $question->getQuestionType()=='3') {
228 /* This is a radio-button question. Values 1-5 or yes(1) no (5)question */
230 $this->Result = array(0, 0, 0, 0, 0, 0);
235 for($i=0; $i<$count; $i++) {
236 if ($arr[$i]->isError()) {
237 echo $arr[$i]->getErrorMessage();
241 $response = $arr[$i]->getResponse();
245 // ugly hack to avoid php warning, and count correctly 'no answer' response.
246 if (is_string($response) && !strlen($response))
249 $this->Result[$response]++;
252 $this->Result[] = $response;
256 return $this->Result;
260 * getDetailResults - get the array of result
262 * @return int the array of result
263 * return all responses for one survey
265 function getDetailResults() {
266 if ($this->Results) {
267 return $this->Results;
269 $arr = $this->getSurveyAllResponses();
270 if (!$arr || !is_array($arr)) {
273 $count = count($arr);
275 for($i=0; $i<$count; $i++) {
276 $user_id = $arr[$i]->getUserId();
277 $qid = $arr[$i]->GetQuestionID();
278 if ($arr[$i]->isError()) {
279 echo $arr[$i]->getErrorMessage();
282 $response = $arr[$i]->getResponse();
283 $this->Result[$user_id][$qid] = $response;
285 return $this->Result;
291 // c-file-style: "bsd"