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 $survey The Survey object
70 * @param object $question The Question object to which this survey Response is associated.
72 function __construct(&$survey, &$question ) {
75 if (!$survey || !is_object($survey)) {
76 $this->setError(_('No valid Survey Object'));
79 if ($survey->isError()) {
80 $this->setError(_('Survey')._(': ').$survey->getErrorMessage());
83 if (!$question || !is_object($question)) {
84 $this->setError(_('No valid Question Object'));
87 if ($question->isError()) {
88 $this->setError(_('Survey')._(': ').$question->getErrorMessage());
92 $this->Survey = &$survey;
93 $this->Question = &$question;
97 * getGroup - get the Group object this SurveyResponse is associated with.
99 * @return object The Group object.
101 function &getGroup() {
102 $Survey = $this->getSurvey();
103 return $Survey->Group;
107 * getSurvey - get the Survey object this SurveyResponse is associated with.
109 * @return object The Survey object.
111 function &getSurvey() {
112 return $this->Survey;
116 * getQuestion - get the Question object this SurveyResponse is associated with.
118 * @return object The Question object.
120 function &getQuestion() {
121 return $this->Question;
125 * getSurveyResponses - get an array of Survey Response objects
126 * for the Survey and Question
128 * @return array The array of Survey Response objects.
130 function getSurveyResponses() {
131 /* We already have it */
132 if ($this->Responses) {
133 return $this->Responses;
136 $group = $this->getGroup();
137 $group_id = $group->GetID();
138 $survey = $this->getSurvey();
139 $survey_id = $survey->GetID();
140 $question = $this->getQuestion();
141 $question_id = $question->GetID();
143 $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',
148 $this->setError(_('No Survey Response is found').db_error());
151 while ($arr = db_fetch_array($result)) {
152 $this->Responses[] = new SurveyResponse($this->getGroup(), $arr);
154 db_free_result($result);
156 return $this->Responses;
160 * getSurveyAllResponses - get an array of Survey Response objects
161 * for the Survey and Question
163 * @return array The array of Survey Response objects.
165 function getSurveyAllResponses() {
166 /* We already have it */
167 if ($this->Responses) {
168 return $this->Responses;
171 $group = $this->getGroup();
172 $group_id = $group->GetID();
173 $survey = $this->getSurvey();
174 $survey_id = $survey->GetID();
176 $result = db_query_params('SELECT * FROM survey_responses WHERE survey_id=$1 AND group_id=$2 ORDER BY post_date DESC',
177 array($survey_id, $group_id));
179 $this->setError(_('No Survey Response is found').db_error());
182 while ($arr = db_fetch_array($result)) {
183 $this->Responses[] = new SurveyResponse($this->getGroup(), $arr);
185 db_free_result($result);
187 return $this->Responses;
191 * getNumberOfSurveyResponses - get the number of Survey Responses
193 * @return int the number of survey responses
195 function getNumberOfSurveyResponses() {
196 $arr = $this->getSurveyResponses();
197 if (!$arr || !is_array($arr)) {
204 * getResults - get the array of result for yes/no and 1-5 question
206 * @return int the array of result
207 * for the yes/no question, it returns counts in arr[1] and arr[5];
208 * for the 1-5 question, it returns counts in arr[1], arr[1], ..., arr[5];
209 * for comments, we return arr[1], ...arr[n] with comments
211 function getResults() {
213 return $this->Result;
216 $arr = $this->getSurveyResponses();
217 if (!$arr || !is_array($arr)) {
220 $count = count($arr);
222 $question = $this->getQuestion();
223 if ($question->getQuestionType()=='1' ||
224 $question->getQuestionType()=='3') {
225 /* This is a radio-button question. Values 1-5 or yes(1) no (5)question */
227 $this->Result = array(0, 0, 0, 0, 0, 0);
232 for($i=0; $i<$count; $i++) {
233 if ($arr[$i]->isError()) {
234 echo $arr[$i]->getErrorMessage();
238 $response = $arr[$i]->getResponse();
242 // ugly hack to avoid php warning, and count correctly 'no answer' response.
243 if (is_string($response) && !strlen($response))
246 $this->Result[$response]++;
249 $this->Result[] = $response;
253 return $this->Result;
257 * getDetailResults - get the array of result
259 * @return int the array of result
260 * return all responses for one survey
262 function getDetailResults() {
263 if ($this->Results) {
264 return $this->Results;
266 $arr = $this->getSurveyAllResponses();
267 if (!$arr || !is_array($arr)) {
270 $count = count($arr);
272 for($i=0; $i<$count; $i++) {
273 $user_id = $arr[$i]->getUserId();
274 $qid = $arr[$i]->GetQuestionID();
275 if ($arr[$i]->isError()) {
276 echo $arr[$i]->getErrorMessage();
279 $response = $arr[$i]->getResponse();
280 $this->Result[$user_id][$qid] = $response;
282 return $this->Result;
288 // c-file-style: "bsd"