5 * Copyright 2004, Sung Kim/GForge, LLC
6 * Copyright 2009, Roland Mas
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 require_once $gfcommon.'include/Error.class.php';
25 require_once $gfcommon.'survey/SurveyResponse.class.php';
27 class SurveyResponseFactory extends Error {
32 * @var object $Survey.
37 * The Question object.
39 * @var object $Question.
51 * The Aggregated Result array for question.
58 * The Aggregated Results array for all questions.
67 * @param object The Survey object
68 * @param object The Question object to which this survey Response is associated.
69 * @param int The survey_id
71 function SurveyResponseFactory(&$Survey, &$Question ) {
74 if (!$Survey || !is_object($Survey)) {
75 $this->setError(_('No valid Survey Object'));
78 if ($Survey->isError()) {
79 $this->setError(_('Survey').':: '.$Survey->getErrorMessage());
82 if (!$Question || !is_object($Question)) {
83 $this->setError(_('No valid Question Object'));
86 if ($Question->isError()) {
87 $this->setError(_('Survey').':: '.$Question->getErrorMessage());
91 $this->Survey = &$Survey;
92 $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();
176 $question = $this->getQuestion();
177 $question_id = $question->GetID();
179 $result = db_query_params('SELECT * FROM survey_responses WHERE survey_id=$1 AND group_id=$2 ORDER BY post_date DESC',
180 array($survey_id, $group_id));
182 $this->setError(_('No Survey Response is found').db_error());
185 while ($arr = db_fetch_array($result)) {
186 $this->Responses[] = new SurveyResponse($this->getGroup(), $arr);
188 db_free_result($result);
190 return $this->Responses;
195 * getNumberOfSurveyResponses - get the number of Survey Responses
197 * @return int the number of survey responses
199 function getNumberOfSurveyResponses() {
200 $arr = $this->getSurveyResponses();
201 if (!$arr || !is_array($arr)) {
209 * getResults - get the array of result for yes/no and 1-5 question
211 * @return int the array of result
212 * for the yes/no question, it returns counts in arr[1] and arr[5];
213 * for the 1-5 question, it returns counts in arr[1], arr[1], ..., arr[5];
214 * for comments, we return arr[1], ...arr[n] with comments
216 function getResults() {
218 return $this->Result;
221 $arr = &$this->getSurveyResponses();
222 if (!$arr || !is_array($arr)) {
225 $count = count($arr);
227 $question = $this->getQuestion();
228 if ($question->getQuestionType()=='1' ||
229 $question->getQuestionType()=='3') {
230 /* This is a radio-button question. Values 1-5 or yes(1) no (5)question */
232 $this->Result = array(0, 0, 0, 0, 0, 0);
237 for($i=0; $i<$count; $i++) {
238 if ($arr[$i]->isError()) {
239 echo $arr[$i]->getErrorMessage();
243 $response = $arr[$i]->getResponse();
247 $this->Result[$response]++;
250 $this->Result[] = $response;
254 return $this->Result;
258 * getDetailResults - get the array of result
260 * @return int the array of result
261 * return all responses for one survey
263 function getDetailResults() {
264 if ($this->Results) {
265 return $this->Results;
267 $arr = $this->getSurveyAllResponses();
268 if (!$arr || !is_array($arr)) {
271 $count = count($arr);
273 for($i=0; $i<$count; $i++) {
274 $id = $arr[$i]->getUserId();
275 $qid = $arr[$i]->GetQuestionID();
276 if ($arr[$i]->isError()) {
277 echo $arr[$i]->getErrorMessage();
280 $response = $arr[$i]->getResponse();
281 $this->Result[$id][$qid] = $response;
283 return $this->Result;
289 // c-file-style: "bsd"