5 * Copyright 2004, Sung Kim/GForge, LLC
6 * Copyright 2009, Roland Mas
7 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
8 * http://fusionforge.org
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/SurveyQuestionFactory.class.php';
29 $SURVEY_OBJ = array();
32 * survey_get_object() - Get the survey object.
34 * survey_get_object() is useful so you can pool survey objects/save database queries
35 * You should always use this instead of instantiating the object directly.
37 * You can now optionally pass in a db result handle. If you do, it re-uses that query
38 * to instantiate the objects.
40 * IMPORTANT! That db result must contain all fields
41 * from surveys table or you will have problems
43 * @param int $survey_id Required
44 * @param int|bool $res Result set handle ("SELECT * FROM surveys WHERE survey_id = xx")
45 * @return Survey|bool A survey object or false on failure
47 function &survey_get_object($survey_id, $res = false) {
49 if (!isset($SURVEY_OBJ["_".$survey_id."_"])) {
51 //the db result handle was passed in
53 $res = db_query_params('SELECT * FROM surveys WHERE survey_id=$1', array($survey_id));
55 if (!$res || db_numrows($res) < 1) {
56 $SURVEY_OBJ["_".$survey_id."_"] = false;
58 $arr = db_fetch_array($res);
59 $groupObject = group_get_object($arr['group_id']);
60 $SURVEY_OBJ["_".$survey_id."_"] = new Survey($groupObject, $survey_id, $arr);
63 return $SURVEY_OBJ["_".$survey_id."_"];
66 class Survey extends FFError {
69 * Associative array of data from db.
71 * @var array $data_array.
76 * Questions array in this survey
78 * @var array $question_array.
80 var $all_question_array;
91 * @param bool $survey_id
93 * @internal param \The $object Group object to which this survey is associated.
94 * @internal param \The $int survey_id.
95 * @internal param \The $array associative array of data.
97 function __construct(&$Group, $survey_id = false, $arr = false) {
98 parent::__construct();
99 if (!$Group || !is_object($Group)) {
100 $this->setError(_('Invalid Project'));
103 if ($Group->isError()) {
104 $this->setError('Survey: '.$Group->getErrorMessage());
107 $this->Group =& $Group;
110 if (!$arr || !is_array($arr)) {
111 if (!$this->fetchData($survey_id)) {
115 $this->data_array =& $arr;
116 if ($this->data_array['group_id'] != $this->Group->getID()) {
117 $this->setError(_('group_id in db result does not match Group Object'));
118 $this->data_array = null;
126 * create - use this function to create a survey
128 * @param string $survey_title The survey title
129 * @param array $add_questions The question numbers to be added
130 * @param int $is_active 1: Active, 0: Inactive
131 * @param int $is_result_public
132 * @param Allow|int $double_vote
133 * @internal param \The $string survey title
134 * @internal param array $int The question numbers to be added
135 * @internal param $is_active 1: Active, 0: Inactive
137 * @internal param $is_result_public 0: Admins Only, 1: Group Members, 2: FusionForge user, 3:voted user 4:Every body
138 * @internal param \Allow $double_vote double vote if it is 1
139 * @return boolean success.
141 function create($survey_title, $add_questions, $is_active = 0, $is_public = 1, $is_result_public = 0, $double_vote = 0) {
142 if (!$survey_title) {
143 $this->setError(_('Update Failed: Survey Title Required'));
145 /* We need at least one survey question at this point */
146 } elseif (!$add_questions || !is_array($add_questions) || count($add_questions)<1) {
147 $this->setError(_('Update Failed: Survey Questions Required'));
151 $group_id = $this->Group->GetID();
153 /* Make old style survey string from array: 1, 2, 3, ..., n */
154 $survey_questions = $this->_makeQuestionString($add_questions);
156 $result = db_query_params('INSERT INTO surveys (survey_title,group_id,survey_questions,is_active) VALUES ($1,$2,$3,$4)',
157 array(htmlspecialchars($survey_title),
163 $this->setError(_('Insert Error').db_error());
167 /* Load question to data array */
168 $survey_id=db_insertid($result,'surveys','survey_id');
169 return $this->fetchData($survey_id);
173 * update - use this function to update a survey
175 * @param string $survey_title The survey title
176 * @param array $add_questions The question numbers to be added
177 * @param array $del_questions The question numbers to be deleted
178 * @param int $is_active 1: Active, 0: Inactive
179 * @param int $is_result_public
180 * @param Allow|int $double_vote
181 * @internal param \The $string survey title
182 * @internal param array $int The question numbers to be added
183 * @internal param array $int The question numbers to be deleted
184 * @internal param $is_active 1: Active, 0: Inactive
185 * @internal param $is_result_public 0: Admins Only, 1: Group Members, 2: FusionForge user, 3:voted user 4:Every body
186 * @internal param \Allow $double_vote double vote if it is 1
187 * @return boolean success.
189 function update($survey_title, &$add_questions, &$del_questions, $is_active = 0, $is_public = 1, $is_result_public = 0, $double_vote = 0) {
190 if (!$survey_title) {
191 $this->setError(_('Update Failed: Survey Title Required'));
193 /* We need at least one survey question at this point */
196 $group_id = $this->Group->GetID();
197 $survey_id = $this->getID();
199 /* Ths Survey is not ready to update */
201 $this->setError(_('The Survey data is not filled'));
205 $survey_questions = $this->_updateQuestionString($add_questions, $del_questions);
206 $result = db_query_params('UPDATE surveys SET survey_title=$1, survey_questions=$2, is_active=$3 WHERE survey_id=$4 AND group_id=$5',
207 array (htmlspecialchars($survey_title),
213 if (db_affected_rows($result) < 1) {
214 $this->setError(_('Update failed').db_error());
217 /* Update internal data */
218 return $this->fetchData($survey_id);
222 * updateOrder - use this function to update question order
224 * @param int $question_number Question number
225 * @param bool $is_up decide up or down. it is up if it is true
226 * @return bool success.
228 function updateOrder($question_number, $is_up = true) {
229 $group_id = $this->Group->GetID();
230 $survey_id = $this->getID();
232 /* Ths Survey is not ready to update */
234 $this->setError(_('The Survey data is not filled'));
245 $survey_questions = $this->_updateQuestionStringOrder($question_number, $delta);
246 $result = db_query_params('UPDATE surveys SET survey_questions=$1 WHERE survey_id=$2 AND group_id=$3',
247 array ($survey_questions,
251 if (db_affected_rows($result) < 1) {
252 $this->setError(_('Update failed').db_error());
256 /* Update internal data */
257 return $this->fetchData($survey_id);
261 * delete - use this function to delete this survey
262 * (We don't support delete yet)
264 * @return boolean success.
267 $group_id = $this->Group->GetID();
268 $survey_id = $this->getID();
270 $res = db_query_params('DELETE FROM surveys where survey_id=$1 AND group_id=$2',
271 array($survey_id, $group_id)
273 if (!$res || db_affected_rows($res) < 1) {
274 $this->setError(_('Delete failed').db_error());
278 /* Delete internal data */
279 $this->data_array = null;
284 * fetchData - re-fetch the data for this survey from the database.
286 * @param int $survey_id The survey_id.
287 * @return boolean success.
289 function fetchData($survey_id) {
290 $group_id = $this->Group->GetID();
292 $res = db_query_params('SELECT * FROM surveys where survey_id=$1 AND group_id=$2',
293 array($survey_id, $group_id)) ;
295 if (!$res || db_numrows($res) < 1) {
296 $this->setError(_('No Survey is found').db_error());
299 $this->data_array = db_fetch_array($res);
300 db_free_result($res);
305 * getGroup - get the Group object this Survey is associated with.
307 * @return object The Group object.
309 function &getGroup() {
314 * getID - Get the id of this Survey
316 * @return int The question_id
319 return $this->data_array['survey_id'];
323 * isActive - return if it is active
325 * @return int is active
327 function isActive() {
328 return $this->data_array['is_active'];
332 * getTitle - Get the Survey title
334 * @return string the survey title
336 function getTitle() {
337 return $this->data_array['survey_title'];
341 * getQuestionString - Get the question string
343 * @return string the question
345 function getQuestionString() {
346 return $this->data_array['survey_questions'];
350 * getNumberOfQuestion - Get the number of questions
352 * @return int the number questions
354 function getNumberOfQuestions() {
355 return count($this->getQuestionArray());
359 * getNumberOfVotes - Get the number of votes
361 * @return int the number votes
363 function getNumberOfVotes() {
364 $group_id = $this->Group->GetID();
365 $survey_id = $this->getID();
367 $res = db_query_params ('SELECT 1 FROM survey_responses WHERE survey_id=$1 AND group_id=$2 GROUP BY user_id',
370 $ret = db_numrows($res);
371 db_free_result($res);
377 * isUserVote - Figure out the user voted or not
379 * @param int $user_id
380 * @return true or false
382 function isUserVote($user_id) {
383 $group_id = $this->Group->GetID();
384 $survey_id = $this->getID();
386 $res = db_query_params ('SELECT 1 FROM survey_responses where survey_id=$1 AND group_id=$2 AND user_id=$3',
390 $ret = db_numrows($res);
391 db_free_result($res);
397 * getQuestionArray - Get the question string numbers in array
399 * @return string the question
401 function &getQuestionArray() {
403 $questions = $this->getQuestionString();
408 $arr_from_str = explode(',', $questions);
410 /* Remove non existed questions */
411 for ($i=0; $i<count($arr_from_str); $i++) {
412 if ($this->_isValidQuestionID($arr_from_str[$i])) {
413 $ret_arr[] = $arr_from_str[$i];
421 * getQuestionInstances - Get the SurveyQuestion array belongs to this Survey by order
423 * @return string the question
425 function &getQuestionInstances() {
428 if (!$this->all_question_array || !is_array($this->all_question_array)) {
429 $this->_fillSurveyQuestions();
432 $arr = & $this->getQuestionArray();
434 for ($i=0; $i<count($arr); $i++) {
435 for ($j=0; $j<count($this->all_question_array); $j++) {
436 /* If it is, copy into new array in order */
437 if ($this->all_question_array[$j]->getID()==$arr[$i]) {
438 $ret[] = $this->all_question_array[$j];
448 * getAddableQuestionInstances - Get the addable SurveyQuestion from all questions
450 * @return string the question
452 function &getAddableQuestionInstances() {
455 if (!$this->all_question_array || !is_array($this->all_question_array)) {
456 $this->_fillSurveyQuestions();
459 $arr = & $this->getQuestionArray();
461 /* Copy questions only if it is not in question string */
462 for ($i=0; $i<count($this->all_question_array); $i++) {
463 if (array_search($this->all_question_array[$i]->getID(), $arr) == false &&
464 $this->all_question_array[$i]->getID()!=$arr[0]) {
465 $ret[] = $this->all_question_array[$i];
469 $ret = $this->all_question_array;
475 /***************************************************************
476 * private question string deal methods
477 * TODO: Add a joint table for surveys and survey_questions.
478 * Deal with DBMS not comma separated string
479 ***************************************************************/
482 * _fillSurveyQuestions - Get all Survey Questions using SurveyQuestionFactory
484 * @return boolean success
486 function _fillSurveyQuestions() {
487 $sqf = new SurveyQuestionFactory($this->getGroup());
488 $this->all_question_array = & $sqf->getSurveyQuestions();
492 * _isValidQuestionID - Check it is correct question id
494 * @param int $question_id question id
495 * @return boolean true if it is valid question id
497 function _isValidQuestionID($question_id) {
498 if (!$this->all_question_array || !is_array($this->all_question_array)) {
499 $this->_fillSurveyQuestions();
502 for ($i=0; $i<count($this->all_question_array); $i++) {
503 if ($question_id == $this->all_question_array[$i]->getID()) {
511 * _makeQuestionString - Make comma separated question number string
513 * @param int $arr array Array of question number
514 * @return string question_strong (example: 1, 2, 3, 7);
516 function _makeQuestionString($arr) {
518 /* No questions to add */
519 if (!$arr || !is_array($arr) || count($arr)<1) {
522 return join(',', $arr);
526 * _updateQuestionString - Update comma separated question number string
528 * @param int array Array of questions to add
529 * @param int array Array of questions to delete
530 * @return string question_strong (example: 1, 2, 3, 7);
532 function _updateQuestionString(&$arr_to_add, &$arr_to_del) {
533 /* Get array of current question string */
534 $arr = & $this->getQuestionArray();
536 /* questions to add */
540 if ($arr_to_add && is_array($arr_to_add) && count($arr_to_add)>0) {
541 for ($i = 0; $i < count($arr_to_add); $i++) {
542 /* Avoid double question */
543 if ($arr_to_add[$i] && array_search($arr_to_add[$i], $arr) == false && $arr_to_add[$i]!=$arr[0]) {
544 $arr[] = $arr_to_add[$i];
550 /* questions to delete */
551 if ($arr_to_del && is_array($arr_to_del) && count($arr_to_del)>0) {
553 for ($i = 0; $i < count($arr); $i++) {
554 /* If the value is no in the delete array, copy it into new array */
555 if ($arr[$i] && array_search($arr[$i], $arr_to_del)==false && $arr_to_del[0]!=$arr[$i]) {
556 $new_arr[] = $arr[$i];
559 /* copy new_arr to arr */
563 /* convert array to String */
564 return $this->_makeQuestionString($arr);
568 * _updateArrayOrder - Update array order
570 * @param int $question_number
571 * @param int $delta increment or decrement (must be 1 or -1)
572 * @return string question_strong (example: 1, 2, 3, 7);
574 function _updateQuestionStringOrder($question_number, $delta) {
575 /* Get array of current question string */
576 $arr = & $this->getQuestionArray();
578 /* We are expecting array */
579 if (!$arr || !is_array($arr)) {
580 return $this->getQuestionString();
583 $index = array_search($question_number, $arr);
585 /* The question number is not in the array
586 * We have nothing to change
588 if ($index==false && $question_number!=$arr[0]) {
589 return $this->getQuestionString();
592 $new_index = $index + $delta;
594 /* Out of boundary */
595 if ($new_index < 0 || $new_index >= count($arr)) {
596 return $this->getQuestionString();
601 $arr[$index] = $arr[$new_index];
602 $arr[$new_index] = $tmp;
604 /* convert array to String */
605 return $this->_makeQuestionString($arr);
611 // c-file-style: "bsd"