3 * GForge Survey Facility: Get Survey Response Factory
5 * Copyright 2004 GForge, LLC
8 * This file is part of GForge.
10 * GForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * GForge 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
21 * along with GForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 Survery Response Factory
33 require_once $gfcommon.'include/Error.class.php';
34 require_once $gfcommon.'survey/SurveyResponse.class.php';
36 class SurveyResponseFactory extends Error {
41 * @var object $Survey.
46 * The Question object.
48 * @var object $Question.
60 * The Aggregated Result array for question.
69 * @param object The Survey object
70 * @param object The Question object to which this survey Response is associated.
71 * @param int The survey_id
73 function SurveyResponseFactory(&$Survey, &$Question ) {
76 if (!$Survey || !is_object($Survey)) {
77 $this->setError(_('No valid Survey Object'));
80 if ($Survey->isError()) {
81 $this->setError(_('Survey').':: '.$Survey->getErrorMessage());
84 if (!$Question || !is_object($Question)) {
85 $this->setError(_('No valid Question Object'));
88 if ($Question->isError()) {
89 $this->setError(_('Survey').':: '.$Question->getErrorMessage());
93 $this->Survey = &$Survey;
94 $this->Question = &$Question;
100 * getGroup - get the Group object this SurveyResponse is associated with.
102 * @return object The Group object.
104 function &getGroup() {
105 $Survey = $this->getSurvey();
106 return $Survey->Group;
110 * getSurvey - get the Survey object this SurveyResponse is associated with.
112 * @return object The Survey object.
114 function &getSurvey() {
115 return $this->Survey;
119 * getQuestion - get the Question object this SurveyResponse is associated with.
121 * @return object The Question object.
123 function &getQuestion() {
124 return $this->Question;
128 * getSurveyResponses - get an array of Survey Response objects
129 * for the Survey and Question
131 * @return array The array of Survey Response objects.
133 function &getSurveyResponses() {
134 /* We alread have it */
135 if ($this->Responses) {
136 return $this->Responses;
139 $group = $this->getGroup();
140 $group_id = $group->GetID();
141 $survey = $this->getSurvey();
142 $survey_id = $survey->GetID();
143 $question = $this->getQUestion();
144 $question_id = $question->GetID();
146 $sql="SELECT * FROM survey_responses ".
147 "WHERE survey_id='$survey_id' ".
148 "AND question_id='$question_id' ".
149 "AND group_id='$group_id' ORDER BY post_date DESC";
151 $result = db_query ($sql);
153 $this->setError(_('No Survey Response is found').db_error());
156 while ($arr = db_fetch_array($result)) {
157 $this->Responses[] = new SurveyResponse($this->getGroup(), $arr);
159 db_free_result($result);
161 return $this->Responses;
166 * getNumberOfSurveyResponses - get the number of Survey Responses
168 * @return int the number of survey responses
170 function &getNumberOfSurveyResponsess() {
171 $arr = &$this->getSurveyResponses();
172 if (!$arr || !is_array($arr)) {
180 * getResults - get the array of result for yes/no and 1-5 question
182 * @return int the array of result
183 * for the yes/no question, it returns counts in arr[1] and arr[5];
184 * for the 1-5 question, it returns counts in arr[1], arr[1], ..., arr[5];
185 * for comments, we return arr[1], ...arr[n] with comments
187 function &getResults() {
188 if ($this->Results) {
189 return $this->Results;
192 $arr = &$this->getSurveyResponses();
193 if (!$arr || !is_array($arr)) {
196 $count = count($arr);
198 $question = $this->getQuestion();
199 if ($question->getQuestionType()=='1' ||
200 $question->getQuestionType()=='3') {
201 /* This is a radio-button question. Values 1-5 or yes(1) no (5)question */
203 $this->Result = array(0, 0, 0, 0, 0, 0);
208 for($i=0; $i<$count; $i++) {
209 if ($arr[$i]->isError()) {
210 echo $arr[$i]->getErrorMessage();
214 $response = $arr[$i]->getResponse();
218 $this->Result[$response]++;
221 $this->Result[] = $response;
225 return $this->Result;
231 // c-file-style: "bsd"