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.
60 * @param object The Survey object
61 * @param object The Question object to which this survey Response is associated.
62 * @param int The survey_id
64 function SurveyResponseFactory(&$Survey, &$Question ) {
67 if (!$Survey || !is_object($Survey)) {
68 $this->setError(_('No valid Survey Object'));
71 if ($Survey->isError()) {
72 $this->setError(_('Survey').':: '.$Survey->getErrorMessage());
75 if (!$Question || !is_object($Question)) {
76 $this->setError(_('No valid Question Object'));
79 if ($Question->isError()) {
80 $this->setError(_('Survey').':: '.$Question->getErrorMessage());
84 $this->Survey = &$Survey;
85 $this->Question = &$Question;
91 * getGroup - get the Group object this SurveyResponse is associated with.
93 * @return object The Group object.
95 function &getGroup() {
96 $Survey = $this->getSurvey();
97 return $Survey->Group;
101 * getSurvey - get the Survey object this SurveyResponse is associated with.
103 * @return object The Survey object.
105 function &getSurvey() {
106 return $this->Survey;
110 * getQuestion - get the Question object this SurveyResponse is associated with.
112 * @return object The Question object.
114 function &getQuestion() {
115 return $this->Question;
119 * getSurveyResponses - get an array of Survey Response objects
120 * for the Survey and Question
122 * @return array The array of Survey Response objects.
124 function &getSurveyResponses() {
125 /* We alread have it */
126 if ($this->Responses) {
127 return $this->Responses;
130 $group = $this->getGroup();
131 $group_id = $group->GetID();
132 $survey = $this->getSurvey();
133 $survey_id = $survey->GetID();
134 $question = $this->getQuestion();
135 $question_id = $question->GetID();
137 $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',
142 $this->setError(_('No Survey Response is found').db_error());
145 while ($arr = db_fetch_array($result)) {
146 $this->Responses[] = new SurveyResponse($this->getGroup(), $arr);
148 db_free_result($result);
150 return $this->Responses;
155 * getNumberOfSurveyResponses - get the number of Survey Responses
157 * @return int the number of survey responses
159 function getNumberOfSurveyResponses() {
160 $arr = $this->getSurveyResponses();
161 if (!$arr || !is_array($arr)) {
169 * getResults - get the array of result for yes/no and 1-5 question
171 * @return int the array of result
172 * for the yes/no question, it returns counts in arr[1] and arr[5];
173 * for the 1-5 question, it returns counts in arr[1], arr[1], ..., arr[5];
174 * for comments, we return arr[1], ...arr[n] with comments
176 function &getResults() {
178 return $this->Result;
181 $arr = &$this->getSurveyResponses();
182 if (!$arr || !is_array($arr)) {
185 $count = count($arr);
187 $question = $this->getQuestion();
188 if ($question->getQuestionType()=='1' ||
189 $question->getQuestionType()=='3') {
190 /* This is a radio-button question. Values 1-5 or yes(1) no (5)question */
192 $this->Result = array(0, 0, 0, 0, 0, 0);
197 for($i=0; $i<$count; $i++) {
198 if ($arr[$i]->isError()) {
199 echo $arr[$i]->getErrorMessage();
203 $response = $arr[$i]->getResponse();
207 $this->Result[$response]++;
210 $this->Result[] = $response;
214 return $this->Result;
220 // c-file-style: "bsd"