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 class Survey extends FFError {
32 * Associative array of data from db.
34 * @var array $data_array.
39 * Questions array in this survey
41 * @var array $question_array.
43 var $all_question_array;
54 * @param bool $survey_id
56 * @internal param \The $object Group object to which this survey is associated.
57 * @internal param \The $int survey_id.
58 * @internal param \The $array associative array of data.
60 function __construct(&$Group, $survey_id = false, $arr = false) {
61 parent::__construct();
62 if (!$Group || !is_object($Group)) {
63 $this->setError(_('No Valid Group Object'));
66 if ($Group->isError()) {
67 $this->setError('Survey: '.$Group->getErrorMessage());
70 $this->Group =& $Group;
73 if (!$arr || !is_array($arr)) {
74 if (!$this->fetchData($survey_id)) {
78 $this->data_array =& $arr;
79 if ($this->data_array['group_id'] != $this->Group->getID()) {
80 $this->setError(_('group_id in db result does not match Group Object'));
81 $this->data_array = null;
89 * create - use this function to create a survey
91 * @param string $survey_title The survey title
92 * @param array $add_questions The question numbers to be added
93 * @param int $is_active 1: Active, 0: Inactive
94 * @param int $is_result_public
95 * @param Allow|int $double_vote
96 * @internal param \The $string survey title
97 * @internal param array $int The question numbers to be added
98 * @internal param $is_active 1: Active, 0: Inactive
100 * @internal param $is_result_public 0: Admins Only, 1: Group Members, 2: FusionForge user, 3:voted user 4:Every body
101 * @internal param \Allow $double_vote double vote if it is 1
102 * @return boolean success.
104 function create($survey_title, $add_questions, $is_active = 0, $is_public = 1, $is_result_public = 0, $double_vote = 0) {
105 if (!$survey_title) {
106 $this->setError(_('Update Failed: Survey Title Required'));
108 /* We need at least one survey question at this point */
109 } elseif (!$add_questions || !is_array($add_questions) || count($add_questions)<1) {
110 $this->setError(_('Update Failed: Survey Questions Required'));
114 $group_id = $this->Group->GetID();
116 /* Make old style survey string from array: 1, 2, 3, ..., n */
117 $survey_questions = $this->_makeQuestionString($add_questions);
119 $result = db_query_params('INSERT INTO surveys (survey_title,group_id,survey_questions,is_active) VALUES ($1,$2,$3,$4)',
120 array(htmlspecialchars($survey_title),
126 $this->setError(_('Insert Error').db_error());
130 /* Load question to data array */
131 $survey_id=db_insertid($result,'surveys','survey_id');
132 return $this->fetchData($survey_id);
136 * update - use this function to update a survey
138 * @param string $survey_title The survey title
139 * @param array $add_questions The question numbers to be added
140 * @param array $del_questions The question numbers to be deleted
141 * @param int $is_active 1: Active, 0: Inactive
142 * @param int $is_result_public
143 * @param Allow|int $double_vote
144 * @internal param \The $string survey title
145 * @internal param array $int The question numbers to be added
146 * @internal param array $int The question numbers to be deleted
147 * @internal param $is_active 1: Active, 0: Inactive
148 * @internal param $is_result_public 0: Admins Only, 1: Group Members, 2: FusionForge user, 3:voted user 4:Every body
149 * @internal param \Allow $double_vote double vote if it is 1
150 * @return boolean success.
152 function update($survey_title, &$add_questions, &$del_questions, $is_active = 0, $is_public = 1, $is_result_public = 0, $double_vote = 0) {
153 if (!$survey_title) {
154 $this->setError(_('Update Failed: Survey Title Required'));
156 /* We need at least one survey question at this point */
159 $group_id = $this->Group->GetID();
160 $survey_id = $this->getID();
162 /* Ths Survey is not ready to update */
164 $this->setError(_('The Survey data is not filled'));
168 $survey_questions = $this->_updateQuestionString($add_questions, $del_questions);
169 $result = db_query_params('UPDATE surveys SET survey_title=$1, survey_questions=$2, is_active=$3 WHERE survey_id=$4 AND group_id=$5',
170 array (htmlspecialchars($survey_title),
176 if (db_affected_rows($result) < 1) {
177 $this->setError(_('Update failed').db_error());
180 /* Update internal data */
181 return $this->fetchData($survey_id);
185 * updateOrder - use this function to update question order
187 * @param int $question_number Question number
188 * @param bool $is_up decide up or down. it is up if it is true
189 * @return bool success.
191 function updateOrder($question_number, $is_up = true) {
192 $group_id = $this->Group->GetID();
193 $survey_id = $this->getID();
195 /* Ths Survey is not ready to update */
197 $this->setError(_('The Survey data is not filled'));
208 $survey_questions = $this->_updateQuestionStringOrder($question_number, $delta);
209 $result = db_query_params('UPDATE surveys SET survey_questions=$1 WHERE survey_id=$2 AND group_id=$3',
210 array ($survey_questions,
214 if (db_affected_rows($result) < 1) {
215 $this->setError(_('Update failed').db_error());
219 /* Update internal data */
220 return $this->fetchData($survey_id);
224 * delete - use this function to delete this survey
225 * (We don't support delete yet)
227 * @return boolean success.
230 $group_id = $this->Group->GetID();
231 $survey_id = $this->getID();
233 $res = db_query_params('DELETE FROM surveys where survey_id=$1 AND group_id=$2',
234 array($survey_id, $group_id)
236 if (!$res || db_affected_rows($res) < 1) {
237 $this->setError(_('Delete failed').db_error());
241 /* Delete internal data */
242 $this->data_array = null;
247 * fetchData - re-fetch the data for this survey from the database.
249 * @param int $survey_id The survey_id.
250 * @return boolean success.
252 function fetchData($survey_id) {
253 $group_id = $this->Group->GetID();
255 $res = db_query_params('SELECT * FROM surveys where survey_id=$1 AND group_id=$2',
256 array($survey_id, $group_id)) ;
258 if (!$res || db_numrows($res) < 1) {
259 $this->setError(_('No Survey is found').db_error());
262 $this->data_array = db_fetch_array($res);
263 db_free_result($res);
268 * getGroup - get the Group object this Survey is associated with.
270 * @return object The Group object.
272 function &getGroup() {
277 * getID - Get the id of this Survey
279 * @return int The question_id
282 return $this->data_array['survey_id'];
286 * isActive - return if it is active
288 * @return int is active
290 function isActive() {
291 return $this->data_array['is_active'];
295 * getTitle - Get the Survey title
297 * @return string the survey title
299 function getTitle() {
300 return $this->data_array['survey_title'];
304 * getQuestionString - Get the question string
306 * @return string the question
308 function getQuestionString() {
309 return $this->data_array['survey_questions'];
313 * getNumberOfQuestion - Get the number of questions
315 * @return int the number questions
317 function getNumberOfQuestions() {
318 return count($this->getQuestionArray());
322 * getNumberOfVotes - Get the number of votes
324 * @return int the number votes
326 function getNumberOfVotes() {
327 $group_id = $this->Group->GetID();
328 $survey_id = $this->getID();
330 $res = db_query_params ('SELECT 1 FROM survey_responses WHERE survey_id=$1 AND group_id=$2 GROUP BY user_id',
333 $ret = db_numrows($res);
334 db_free_result($res);
340 * isUserVote - Figure out the user voted or not
342 * @param int $user_id
343 * @return true or false
345 function isUserVote($user_id) {
346 $group_id = $this->Group->GetID();
347 $survey_id = $this->getID();
349 $res = db_query_params ('SELECT 1 FROM survey_responses where survey_id=$1 AND group_id=$2 AND user_id=$3',
353 $ret = db_numrows($res);
354 db_free_result($res);
360 * getQuestionArray - Get the question string numbers in array
362 * @return string the question
364 function &getQuestionArray() {
366 $questions = $this->getQuestionString();
371 $arr_from_str = explode(',', $questions);
373 /* Remove non existed questions */
374 for ($i=0; $i<count($arr_from_str); $i++) {
375 if ($this->_isValidQuestionID($arr_from_str[$i])) {
376 $ret_arr[] = $arr_from_str[$i];
384 * getQuestionInstances - Get the SurveyQuestion array belongs to this Survey by order
386 * @return string the question
388 function &getQuestionInstances() {
391 if (!$this->all_question_array || !is_array($this->all_question_array)) {
392 $this->_fillSurveyQuestions();
395 $arr = & $this->getQuestionArray();
397 for ($i=0; $i<count($arr); $i++) {
398 for ($j=0; $j<count($this->all_question_array); $j++) {
399 /* If it is, copy into new array in order */
400 if ($this->all_question_array[$j]->getID()==$arr[$i]) {
401 $ret[] = $this->all_question_array[$j];
411 * getAddableQuestionInstances - Get the addable SurveyQuestion from all questions
413 * @return string the question
415 function &getAddableQuestionInstances() {
418 if (!$this->all_question_array || !is_array($this->all_question_array)) {
419 $this->_fillSurveyQuestions();
422 $arr = & $this->getQuestionArray();
424 /* Copy questions only if it is not in question string */
425 for ($i=0; $i<count($this->all_question_array); $i++) {
426 if (array_search($this->all_question_array[$i]->getID(), $arr) == false &&
427 $this->all_question_array[$i]->getID()!=$arr[0]) {
428 $ret[] = $this->all_question_array[$i];
432 $ret = $this->all_question_array;
438 /***************************************************************
439 * private question string deal methods
440 * TODO: Add a joint table for surveys and survey_questions.
441 * Deal with DBMS not comma separated string
442 ***************************************************************/
445 * _fillSurveyQuestions - Get all Survey Questions using SurveyQuestionFactory
447 * @return boolean success
449 function _fillSurveyQuestions() {
450 $sqf = new SurveyQuestionFactory($this->getGroup());
451 $this->all_question_array = & $sqf->getSurveyQuestions();
455 * _isValidQuestionID - Check it is correct question id
457 * @param int $question_id question id
458 * @return boolean true if it is valid question id
460 function _isValidQuestionID($question_id) {
461 if (!$this->all_question_array || !is_array($this->all_question_array)) {
462 $this->_fillSurveyQuestions();
465 for ($i=0; $i<count($this->all_question_array); $i++) {
466 if ($question_id == $this->all_question_array[$i]->getID()) {
474 * _makeQuestionString - Make comma separated question number string
476 * @param int $arr array Array of question number
477 * @return string question_strong (example: 1, 2, 3, 7);
479 function _makeQuestionString($arr) {
481 /* No questions to add */
482 if (!$arr || !is_array($arr) || count($arr)<1) {
485 return join(',', $arr);
489 * _updateQuestionString - Update comma separated question number string
491 * @param int array Array of questions to add
492 * @param int array Array of questions to delete
493 * @return string question_strong (example: 1, 2, 3, 7);
495 function _updateQuestionString(&$arr_to_add, &$arr_to_del) {
496 /* Get array of current question string */
497 $arr = & $this->getQuestionArray();
499 /* questions to add */
503 if ($arr_to_add && is_array($arr_to_add) && count($arr_to_add)>0) {
504 for ($i = 0; $i < count($arr_to_add); $i++) {
505 /* Avoid double question */
506 if ($arr_to_add[$i] && array_search($arr_to_add[$i], $arr) == false && $arr_to_add[$i]!=$arr[0]) {
507 $arr[] = $arr_to_add[$i];
513 /* questions to delete */
514 if ($arr_to_del && is_array($arr_to_del) && count($arr_to_del)>0) {
516 for ($i = 0; $i < count($arr); $i++) {
517 /* If the value is no in the delete array, copy it into new array */
518 if ($arr[$i] && array_search($arr[$i], $arr_to_del)==false && $arr_to_del[0]!=$arr[$i]) {
519 $new_arr[] = $arr[$i];
522 /* copy new_arr to arr */
526 /* convert array to String */
527 return $this->_makeQuestionString($arr);
531 * _updateArrayOrder - Update array order
533 * @param int $question_number
534 * @param int $delta increment or decrement (must be 1 or -1)
535 * @return string question_strong (example: 1, 2, 3, 7);
537 function _updateQuestionStringOrder($question_number, $delta) {
538 /* Get array of current question string */
539 $arr = & $this->getQuestionArray();
541 /* We are expecting array */
542 if (!$arr || !is_array($arr)) {
543 return $this->getQuestionString();
546 $index = array_search($question_number, $arr);
548 /* The question number is not in the array
549 * We have nothing to change
551 if ($index==false && $question_number!=$arr[0]) {
552 return $this->getQuestionString();
555 $new_index = $index + $delta;
557 /* Out of boundary */
558 if ($new_index < 0 || $new_index >= count($arr)) {
559 return $this->getQuestionString();
564 $arr[$index] = $arr[$new_index];
565 $arr[$new_index] = $tmp;
567 /* convert array to String */
568 return $this->_makeQuestionString($arr);
574 // c-file-style: "bsd"