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
94 function __construct(&$Group, $survey_id = false, $arr = false) {
95 parent::__construct();
96 if (!$Group || !is_object($Group)) {
97 $this->setError(_('Invalid Project'));
100 if ($Group->isError()) {
101 $this->setError('Survey: '.$Group->getErrorMessage());
104 $this->Group =& $Group;
107 if (!$arr || !is_array($arr)) {
108 if (!$this->fetchData($survey_id)) {
112 $this->data_array =& $arr;
113 if ($this->data_array['group_id'] != $this->Group->getID()) {
114 $this->setError(_('group_id in db result does not match Group Object'));
115 $this->data_array = null;
123 * create - use this function to create a survey
125 * @param string $survey_title The survey title
126 * @param array $add_questions The question numbers to be added
127 * @param int $is_active 1: Active, 0: Inactive
128 * @param int $is_result_public
129 * @param Allow|int $double_vote
130 * @return bool success.
132 function create($survey_title, $add_questions, $is_active = 0, $is_public = 1, $is_result_public = 0, $double_vote = 0) {
133 if (!$survey_title) {
134 $this->setError(_('Update Failed: Survey Title Required'));
136 /* We need at least one survey question at this point */
137 } elseif (!$add_questions || !is_array($add_questions) || count($add_questions)<1) {
138 $this->setError(_('Update Failed: Survey Questions Required'));
142 $group_id = $this->Group->GetID();
144 /* Make old style survey string from array: 1, 2, 3, ..., n */
145 $survey_questions = $this->_makeQuestionString($add_questions);
147 $result = db_query_params('INSERT INTO surveys (survey_title,group_id,survey_questions,is_active) VALUES ($1,$2,$3,$4)',
148 array(htmlspecialchars($survey_title),
154 $this->setError(_('Insert Error').db_error());
158 /* Load question to data array */
159 $survey_id=db_insertid($result,'surveys','survey_id');
160 return $this->fetchData($survey_id);
164 * update - use this function to update a survey
166 * @param string $survey_title The survey title
167 * @param array $add_questions The question numbers to be added
168 * @param array $del_questions The question numbers to be deleted
169 * @param int $is_active 1: Active, 0: Inactive
170 * @param int $is_result_public
171 * @param Allow|int $double_vote
172 * @return bool success.
174 function update($survey_title, &$add_questions, &$del_questions, $is_active = 0, $is_public = 1, $is_result_public = 0, $double_vote = 0) {
175 if (!$survey_title) {
176 $this->setError(_('Update Failed: Survey Title Required'));
178 /* We need at least one survey question at this point */
181 $group_id = $this->Group->GetID();
182 $survey_id = $this->getID();
184 /* Ths Survey is not ready to update */
186 $this->setError(_('The Survey data is not filled'));
190 $survey_questions = $this->_updateQuestionString($add_questions, $del_questions);
191 $result = db_query_params('UPDATE surveys SET survey_title=$1, survey_questions=$2, is_active=$3 WHERE survey_id=$4 AND group_id=$5',
192 array (htmlspecialchars($survey_title),
198 if (db_affected_rows($result) < 1) {
199 $this->setError(_('Update failed').db_error());
202 /* Update internal data */
203 return $this->fetchData($survey_id);
207 * updateOrder - use this function to update question order
209 * @param int $question_number Question number
210 * @param bool $is_up decide up or down. it is up if it is true
211 * @return bool success.
213 function updateOrder($question_number, $is_up = true) {
214 $group_id = $this->Group->GetID();
215 $survey_id = $this->getID();
217 /* Ths Survey is not ready to update */
219 $this->setError(_('The Survey data is not filled'));
230 $survey_questions = $this->_updateQuestionStringOrder($question_number, $delta);
231 $result = db_query_params('UPDATE surveys SET survey_questions=$1 WHERE survey_id=$2 AND group_id=$3',
232 array ($survey_questions,
236 if (db_affected_rows($result) < 1) {
237 $this->setError(_('Update failed').db_error());
241 /* Update internal data */
242 return $this->fetchData($survey_id);
246 * delete - use this function to delete this survey
247 * (We don't support delete yet)
249 * @return boolean success.
252 $group_id = $this->Group->GetID();
253 $survey_id = $this->getID();
255 $res = db_query_params('DELETE FROM surveys where survey_id=$1 AND group_id=$2',
256 array($survey_id, $group_id)
258 if (!$res || db_affected_rows($res) < 1) {
259 $this->setError(_('Delete failed').db_error());
263 /* Delete internal data */
264 $this->data_array = null;
269 * fetchData - re-fetch the data for this survey from the database.
271 * @param int $survey_id The survey_id.
272 * @return boolean success.
274 function fetchData($survey_id) {
275 $group_id = $this->Group->GetID();
277 $res = db_query_params('SELECT * FROM surveys where survey_id=$1 AND group_id=$2',
278 array($survey_id, $group_id)) ;
280 if (!$res || db_numrows($res) < 1) {
281 $this->setError(_('No Survey is found').db_error());
284 $this->data_array = db_fetch_array($res);
285 db_free_result($res);
290 * getGroup - get the Group object this Survey is associated with.
292 * @return object The Group object.
294 function &getGroup() {
299 * getID - Get the id of this Survey
301 * @return int The question_id
304 return $this->data_array['survey_id'];
308 * isActive - return if it is active
310 * @return int is active
312 function isActive() {
313 return $this->data_array['is_active'];
317 * getTitle - Get the Survey title
319 * @return string the survey title
321 function getTitle() {
322 return $this->data_array['survey_title'];
326 * getQuestionString - Get the question string
328 * @return string the question
330 function getQuestionString() {
331 return $this->data_array['survey_questions'];
335 * getNumberOfQuestion - Get the number of questions
337 * @return int the number questions
339 function getNumberOfQuestions() {
340 return count($this->getQuestionArray());
344 * getNumberOfVotes - Get the number of votes
346 * @return int the number votes
348 function getNumberOfVotes() {
349 $group_id = $this->Group->GetID();
350 $survey_id = $this->getID();
352 $res = db_query_params ('SELECT 1 FROM survey_responses WHERE survey_id=$1 AND group_id=$2 GROUP BY user_id',
355 $ret = db_numrows($res);
356 db_free_result($res);
362 * isUserVote - Figure out the user voted or not
364 * @param int $user_id
365 * @return true or false
367 function isUserVote($user_id) {
368 $group_id = $this->Group->GetID();
369 $survey_id = $this->getID();
371 $res = db_query_params ('SELECT 1 FROM survey_responses where survey_id=$1 AND group_id=$2 AND user_id=$3',
375 $ret = db_numrows($res);
376 db_free_result($res);
382 * getQuestionArray - Get the question string numbers in array
384 * @return string the question
386 function &getQuestionArray() {
388 $questions = $this->getQuestionString();
393 $arr_from_str = explode(',', $questions);
395 /* Remove non existed questions */
396 for ($i=0; $i<count($arr_from_str); $i++) {
397 if ($this->_isValidQuestionID($arr_from_str[$i])) {
398 $ret_arr[] = $arr_from_str[$i];
406 * getQuestionInstances - Get the SurveyQuestion array belongs to this Survey by order
408 * @return string the question
410 function &getQuestionInstances() {
413 if (!$this->all_question_array || !is_array($this->all_question_array)) {
414 $this->_fillSurveyQuestions();
417 $arr = & $this->getQuestionArray();
419 for ($i=0; $i<count($arr); $i++) {
420 for ($j=0; $j<count($this->all_question_array); $j++) {
421 /* If it is, copy into new array in order */
422 if ($this->all_question_array[$j]->getID()==$arr[$i]) {
423 $ret[] = $this->all_question_array[$j];
433 * getAddableQuestionInstances - Get the addable SurveyQuestion from all questions
435 * @return string the question
437 function &getAddableQuestionInstances() {
440 if (!$this->all_question_array || !is_array($this->all_question_array)) {
441 $this->_fillSurveyQuestions();
444 $arr = & $this->getQuestionArray();
446 /* Copy questions only if it is not in question string */
447 for ($i=0; $i<count($this->all_question_array); $i++) {
448 if (array_search($this->all_question_array[$i]->getID(), $arr) == false &&
449 $this->all_question_array[$i]->getID()!=$arr[0]) {
450 $ret[] = $this->all_question_array[$i];
454 $ret = $this->all_question_array;
460 /***************************************************************
461 * private question string deal methods
462 * TODO: Add a joint table for surveys and survey_questions.
463 * Deal with DBMS not comma separated string
464 ***************************************************************/
467 * _fillSurveyQuestions - Get all Survey Questions using SurveyQuestionFactory
469 * @return boolean success
471 function _fillSurveyQuestions() {
472 $sqf = new SurveyQuestionFactory($this->getGroup());
473 $this->all_question_array = & $sqf->getSurveyQuestions();
477 * _isValidQuestionID - Check it is correct question id
479 * @param int $question_id question id
480 * @return boolean true if it is valid question id
482 function _isValidQuestionID($question_id) {
483 if (!$this->all_question_array || !is_array($this->all_question_array)) {
484 $this->_fillSurveyQuestions();
487 for ($i=0; $i<count($this->all_question_array); $i++) {
488 if ($question_id == $this->all_question_array[$i]->getID()) {
496 * _makeQuestionString - Make comma separated question number string
498 * @param int $arr array Array of question number
499 * @return string question_strong (example: 1, 2, 3, 7);
501 function _makeQuestionString($arr) {
503 /* No questions to add */
504 if (!$arr || !is_array($arr) || count($arr)<1) {
507 return join(',', $arr);
511 * _updateQuestionString - Update comma separated question number string
513 * @param int array Array of questions to add
514 * @param int array Array of questions to delete
515 * @return string question_strong (example: 1, 2, 3, 7);
517 function _updateQuestionString(&$arr_to_add, &$arr_to_del) {
518 /* Get array of current question string */
519 $arr = & $this->getQuestionArray();
521 /* questions to add */
525 if ($arr_to_add && is_array($arr_to_add) && count($arr_to_add)>0) {
526 for ($i = 0; $i < count($arr_to_add); $i++) {
527 /* Avoid double question */
528 if ($arr_to_add[$i] && array_search($arr_to_add[$i], $arr) == false && $arr_to_add[$i]!=$arr[0]) {
529 $arr[] = $arr_to_add[$i];
535 /* questions to delete */
536 if ($arr_to_del && is_array($arr_to_del) && count($arr_to_del)>0) {
538 for ($i = 0; $i < count($arr); $i++) {
539 /* If the value is no in the delete array, copy it into new array */
540 if ($arr[$i] && array_search($arr[$i], $arr_to_del)==false && $arr_to_del[0]!=$arr[$i]) {
541 $new_arr[] = $arr[$i];
544 /* copy new_arr to arr */
548 /* convert array to String */
549 return $this->_makeQuestionString($arr);
553 * _updateArrayOrder - Update array order
555 * @param int $question_number
556 * @param int $delta increment or decrement (must be 1 or -1)
557 * @return string question_strong (example: 1, 2, 3, 7);
559 function _updateQuestionStringOrder($question_number, $delta) {
560 /* Get array of current question string */
561 $arr = & $this->getQuestionArray();
563 /* We are expecting array */
564 if (!$arr || !is_array($arr)) {
565 return $this->getQuestionString();
568 $index = array_search($question_number, $arr);
570 /* The question number is not in the array
571 * We have nothing to change
573 if ($index==false && $question_number!=$arr[0]) {
574 return $this->getQuestionString();
577 $new_index = $index + $delta;
579 /* Out of boundary */
580 if ($new_index < 0 || $new_index >= count($arr)) {
581 return $this->getQuestionString();
586 $arr[$index] = $arr[$new_index];
587 $arr[$new_index] = $tmp;
589 /* convert array to String */
590 return $this->_makeQuestionString($arr);
596 // c-file-style: "bsd"