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/FFError.class.php';
27 require_once $gfcommon.'survey/SurveyResponse.class.php';
29 class SurveyResponseFactory extends FFError {
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.
67 * @param object $survey The Survey object
68 * @param object $question The Question object to which this survey Response is associated.
70 function __construct(&$survey, &$question ) {
71 parent::__construct();
73 if (!$survey || !is_object($survey)) {
74 $this->setError(_('No valid Survey Object'));
77 if ($survey->isError()) {
78 $this->setError(_('Survey')._(': ').$survey->getErrorMessage());
81 if (!$question || !is_object($question)) {
82 $this->setError(_('No valid Question Object'));
85 if ($question->isError()) {
86 $this->setError(_('Survey')._(': ').$question->getErrorMessage());
90 $this->Survey = &$survey;
91 $this->Question = &$question;
95 * getGroup - get the Group object this SurveyResponse is associated with.
97 * @return object The Group object.
99 function &getGroup() {
100 $Survey = $this->getSurvey();
101 return $Survey->Group;
105 * getSurvey - get the Survey object this SurveyResponse is associated with.
107 * @return object The Survey object.
109 function &getSurvey() {
110 return $this->Survey;
114 * getQuestion - get the Question object this SurveyResponse is associated with.
116 * @return object The Question object.
118 function &getQuestion() {
119 return $this->Question;
123 * getSurveyResponses - get an array of Survey Response objects
124 * for the Survey and Question
126 * @return array The array of Survey Response objects.
128 function getSurveyResponses() {
129 /* We already have it */
130 if ($this->Responses) {
131 return $this->Responses;
134 $group = $this->getGroup();
135 $group_id = $group->GetID();
136 $survey = $this->getSurvey();
137 $survey_id = $survey->GetID();
138 $question = $this->getQuestion();
139 $question_id = $question->GetID();
141 $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',
146 $this->setError(_('No Survey Response is found').db_error());
149 while ($arr = db_fetch_array($result)) {
150 $this->Responses[] = new SurveyResponse($this->getGroup(), $arr);
152 db_free_result($result);
154 return $this->Responses;
158 * getSurveyAllResponses - get an array of Survey Response objects
159 * for the Survey and Question
161 * @return array The array of Survey Response objects.
163 function getSurveyAllResponses() {
164 /* We already have it */
165 if ($this->Responses) {
166 return $this->Responses;
169 $group = $this->getGroup();
170 $group_id = $group->GetID();
171 $survey = $this->getSurvey();
172 $survey_id = $survey->GetID();
174 $result = db_query_params('SELECT * FROM survey_responses WHERE survey_id=$1 AND group_id=$2 ORDER BY post_date DESC',
175 array($survey_id, $group_id));
177 $this->setError(_('No Survey Response is found').db_error());
180 while ($arr = db_fetch_array($result)) {
181 $this->Responses[] = new SurveyResponse($this->getGroup(), $arr);
183 db_free_result($result);
185 return $this->Responses;
189 * getNumberOfSurveyResponses - get the number of Survey Responses
191 * @return int the number of survey responses
193 function getNumberOfSurveyResponses() {
194 $arr = $this->getSurveyResponses();
195 if (!$arr || !is_array($arr)) {
202 * getResults - get the array of result for yes/no and 1-5 question
204 * @return int the array of result
205 * for the yes/no question, it returns counts in arr[1] and arr[5];
206 * for the 1-5 question, it returns counts in arr[1], arr[1], ..., arr[5];
207 * for comments, we return arr[1], ...arr[n] with comments
209 function getResults() {
211 return $this->Result;
214 $arr = $this->getSurveyResponses();
215 if (!$arr || !is_array($arr)) {
218 $count = count($arr);
220 $question = $this->getQuestion();
221 if ($question->getQuestionType()=='1' ||
222 $question->getQuestionType()=='3') {
223 /* This is a radio-button question. Values 1-5 or yes(1) no (5)question */
225 $this->Result = array(0, 0, 0, 0, 0, 0);
230 for($i=0; $i<$count; $i++) {
231 if ($arr[$i]->isError()) {
232 echo $arr[$i]->getErrorMessage();
236 $response = $arr[$i]->getResponse();
240 // ugly hack to avoid php warning, and count correctly 'no answer' response.
241 if (is_string($response) && !strlen($response)) {
244 $this->Result[$response]++;
247 $this->Result[] = $response;
251 return $this->Result;
255 * getDetailResults - get the array of result
257 * @return int the array of result
258 * return all responses for one survey
260 function getDetailResults() {
261 if ($this->Results) {
262 return $this->Results;
264 $arr = $this->getSurveyAllResponses();
265 if (!$arr || !is_array($arr)) {
268 $count = count($arr);
270 for($i=0; $i<$count; $i++) {
271 $user_id = $arr[$i]->getUserId();
272 $qid = $arr[$i]->GetQuestionID();
273 if ($arr[$i]->isError()) {
274 echo $arr[$i]->getErrorMessage();
277 $response = $arr[$i]->getResponse();
278 $this->Result[$user_id][$qid] = $response;
280 return $this->Result;
286 // c-file-style: "bsd"