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';
26 require_once $gfcommon.'survey/SurveyResponse.class.php';
28 class SurveyResponseFactory extends Error {
33 * @var object $Survey.
38 * The Question object.
40 * @var object $Question.
52 * The Aggregated Result array for question.
59 * The Aggregated Results array for all questions.
68 * @param object The Survey object
69 * @param object The Question object to which this survey Response is associated.
70 * @param int The survey_id
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 alread 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 alread 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;
192 * getNumberOfSurveyResponses - get the number of Survey Responses
194 * @return int the number of survey responses
196 function getNumberOfSurveyResponses() {
197 $arr = $this->getSurveyResponses();
198 if (!$arr || !is_array($arr)) {
206 * getResults - get the array of result for yes/no and 1-5 question
208 * @return int the array of result
209 * for the yes/no question, it returns counts in arr[1] and arr[5];
210 * for the 1-5 question, it returns counts in arr[1], arr[1], ..., arr[5];
211 * for comments, we return arr[1], ...arr[n] with comments
213 function &getResults() {
215 return $this->Result;
218 $arr = &$this->getSurveyResponses();
219 if (!$arr || !is_array($arr)) {
222 $count = count($arr);
224 $question = $this->getQuestion();
225 if ($question->getQuestionType()=='1' ||
226 $question->getQuestionType()=='3') {
227 /* This is a radio-button question. Values 1-5 or yes(1) no (5)question */
229 $this->Result = array(0, 0, 0, 0, 0, 0);
234 for($i=0; $i<$count; $i++) {
235 if ($arr[$i]->isError()) {
236 echo $arr[$i]->getErrorMessage();
240 $response = $arr[$i]->getResponse();
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"