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/Error.class.php';
27 require_once $gfcommon.'survey/SurveyQuestionFactory.class.php';
29 class Survey extends Error {
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;
55 * @param object The Group object to which this servey is associated.
56 * @param int The servey_id.
57 * @param array The associative array of data.
58 * @return boolean success.
60 function __construct(&$Group, $survey_id = false, $arr = false) {
62 if (!$Group || !is_object($Group)) {
65 if ($Group->isError()) {
66 $this->setError('Survey:: '.$Group->getErrorMessage());
69 $this->Group =& $Group;
72 if (!$arr || !is_array($arr)) {
73 if (!$this->fetchData($survey_id)) {
77 $this->data_array =& $arr;
78 if ($this->data_array['group_id'] != $this->Group->getID()) {
79 $this->setError(_('Group_id in db result does not match Group Object'));
80 $this->data_array = null;
88 * create - use this function to create a survey
90 * @param string The survey title
91 * @param int array The question numbers to be added
92 * @param is_active 1: Active, 0: Inactive
94 * @param is_public 0: Admins Only, 1: Group Members, 2: FusionForge user, 3:Every body
95 * @param is_result_public 0: Admins Only, 1: Group Members, 2: FusionForge user, 3:voted user 4:Every body
96 * @param double_vote Allow double vote if it is 1
97 * @return boolean success.
99 function create($survey_title, $add_questions, $is_active = 0, $is_public = 1, $is_result_public = 0, $double_vote = 0) {
100 if (!$survey_title) {
101 $this->setError(_('Update Failed: Survey Title Required'));
103 /* We need at least one survey question at this point */
104 } elseif (!$add_questions || !is_array($add_questions) || count($add_questions)<1) {
105 $this->setError(_('Update Failed: Survey Questions Required'));
109 $group_id = $this->Group->GetID();
111 /* Make old style survey string from array: 1, 2, 3, ..., n */
112 $survey_questions = $this->_makeQuestionString($add_questions);
114 $result = db_query_params('INSERT INTO surveys (survey_title,group_id,survey_questions,is_active) VALUES ($1,$2,$3,$4)',
115 array(htmlspecialchars($survey_title),
121 $this->setError(_('Insert Error').db_error());
125 /* Load question to data array */
126 $survey_id=db_insertid($result,'surveys','survey_id');
127 return $this->fetchData($survey_id);
133 * update - use this function to update a survey
135 * @param string The survey title
136 * @param int array The question numbers to be added
137 * @param int array The question numbers to be deleted
138 * @param is_active 1: Active, 0: Inactive
139 * @param is_public 0: Admins Only, 1: Group Members, 2: FusionForge user, 3:Every body
140 * @param is_result_public 0: Admins Only, 1: Group Members, 2: FusionForge user, 3:voted user 4:Every body
141 * @param double_vote Allow double vote if it is 1
142 * @return boolean success.
144 function update($survey_title, &$add_questions, &$del_questions, $is_active = 0, $is_public = 1, $is_result_public = 0, $double_vote = 0) {
145 if (!$survey_title) {
146 $this->setError(_('Update Failed: Survey Title Required'));
148 /* We need at least one survey question at this point */
151 $group_id = $this->Group->GetID();
152 $survey_id = $this->getID();
154 /* Ths Survey is not ready to update */
156 $this->setError(_('The Survey data is not filled'));
160 $survey_questions = $this->_updateQuestionString($add_questions, $del_questions);
161 $result = db_query_params('UPDATE surveys SET survey_title=$1, survey_questions=$2, is_active=$3 WHERE survey_id=$4 AND group_id=$5',
162 array (htmlspecialchars($survey_title),
168 if (db_affected_rows($result) < 1) {
169 $this->setError(_('Update failed').db_error());
172 /* Update internal data */
173 return $this->fetchData($survey_id);
177 * updateOrder - use this function to update question order
179 * @param int Question number
180 * @param boolean decide up or down. it is up if it is true
181 * @return boolean success.
183 function updateOrder($question_number, $is_up = true) {
184 $group_id = $this->Group->GetID();
185 $survey_id = $this->getID();
187 /* Ths Survey is not ready to update */
189 $this->setError(_('The Survey data is not filled'));
200 $survey_questions = $this->_updateQuestionStringOrder($question_number, $delta);
201 $result = db_query_params('UPDATE surveys SET survey_questions=$1 WHERE survey_id=$2 AND group_id=$3',
202 array ($survey_questions,
206 if (db_affected_rows($result) < 1) {
207 $this->setError(_('Update failed').db_error());
211 /* Update internal data */
212 return $this->fetchData($survey_id);
216 * delete - use this function to delete this survey
217 * (We don't support delete yet)
219 * @return boolean success.
222 $group_id = $this->Group->GetID();
223 $survey_id = $this->getID();
225 $res = db_query_params('DELETE FROM surveys where survey_id=$1 AND group_id=$2',
226 array($survey_id, $group_id)
228 if (!$res || db_affected_rows($res) < 1) {
229 $this->setError(_('Delete failed').db_error());
233 /* Delete internal data */
234 $this->data_array = null;
239 * fetchData - re-fetch the data for this survey from the database.
241 * @param int The survey_id.
242 * @return boolean success.
244 function fetchData($survey_id) {
245 $group_id = $this->Group->GetID();
247 $res = db_query_params('SELECT * FROM surveys where survey_id=$1 AND group_id=$2',
248 array($survey_id, $group_id)) ;
250 if (!$res || db_numrows($res) < 1) {
251 $this->setError(_('No Survey is found').db_error());
254 $this->data_array = db_fetch_array($res);
255 db_free_result($res);
260 * getGroup - get the Group object this Survey is associated with.
262 * @return object The Group object.
264 function &getGroup() {
269 * getID - Get the id of this Survey
271 * @return int The question_id
274 return $this->data_array['survey_id'];
278 * isActriv - return if it is active
280 * @return int is active
282 function isActive() {
283 return $this->data_array['is_active'];
287 * getTitle - Get the Survey title
289 * @return string the survey title
291 function getTitle() {
292 return $this->data_array['survey_title'];
296 * getQuestionString - Get the question string
298 * @return string the question
300 function getQuestionString() {
301 return $this->data_array['survey_questions'];
305 * getNumberOfQuestion - Get the number of questions
307 * @return int the number questions
309 function getNumberOfQuestions() {
310 return count($this->getQuestionArray());
314 * getNumberOfVotes - Get the number of votes
316 * @return int the number votes
318 function getNumberOfVotes() {
319 $group_id = $this->Group->GetID();
320 $survey_id = $this->getID();
322 $res = db_query_params ('SELECT 1 FROM survey_responses WHERE survey_id=$1 AND group_id=$2 GROUP BY user_id',
325 $ret = db_numrows($res);
326 db_free_result($res);
332 * isUserVote - Figure out the user voted or not
335 * @return true of false
337 function isUserVote($user_id) {
338 $group_id = $this->Group->GetID();
339 $survey_id = $this->getID();
341 $res = db_query_params ('SELECT 1 FROM survey_responses where survey_id=$1 AND group_id=$2 AND user_id=$3',
345 $ret = db_numrows($res);
346 db_free_result($res);
352 * getQuestionArray - Get the question string numbers in array
354 * @return string the question
356 function &getQuestionArray() {
358 $questions = $this->getQuestionString();
363 $arr_from_str = explode(',', $questions);
365 /* Remove non existed questions */
366 for ($i=0; $i<count($arr_from_str); $i++) {
367 if ($this->_isValidQuestionID($arr_from_str[$i])) {
368 $ret_arr[] = $arr_from_str[$i];
376 * getQuestionInstances - Get the SurveyQuestion array belongs to this Survey by order
378 * @return string the question
380 function &getQuestionInstances() {
383 if (!$this->all_question_array || !is_array($this->all_question_array)) {
384 $this->_fillSurveyQuestions();
387 $arr = & $this->getQuestionArray();
389 for ($i=0; $i<count($arr); $i++) {
390 for ($j=0; $j<count($this->all_question_array); $j++) {
391 /* If it is, copy into new array in order */
392 if ($this->all_question_array[$j]->getID()==$arr[$i]) {
393 $ret[] = $this->all_question_array[$j];
403 * getAddableQuestionInstances - Get the addable SurveyQuestion from all questions
405 * @return string the question
407 function &getAddableQuestionInstances() {
410 if (!$this->all_question_array || !is_array($this->all_question_array)) {
411 $this->_fillSurveyQuestions();
414 $arr = & $this->getQuestionArray();
416 /* Copy questions only if it is not in question string */
417 for ($i=0; $i<count($this->all_question_array); $i++) {
418 if (array_search($this->all_question_array[$i]->getID(), $arr) == false &&
419 $this->all_question_array[$i]->getID()!=$arr[0]) {
420 $ret[] = $this->all_question_array[$i];
424 $ret = $this->all_question_array;
430 /***************************************************************
431 * private question string deal methods
432 * TODO: Add a joint table for surveys and survey_questions.
433 * Deal with DBMS not comma separated string
434 ***************************************************************/
437 * _fillSurveyQuestions - Get all Survey Questions using SurveyQuestionFactory
439 * @return boolean success
441 function _fillSurveyQuestions() {
442 $sqf = new SurveyQuestionFactory($this->getGroup());
443 $this->all_question_array = & $sqf->getSurveyQuestions();
448 * _isValidQuestionID - Check it is correct question id
450 * @param int questioin id
451 * @return boolean true if it is valid question id
453 function _isValidQuestionID($question_id) {
454 if (!$this->all_question_array || !is_array($this->all_question_array)) {
455 $this->_fillSurveyQuestions();
458 for ($i=0; $i<count($this->all_question_array); $i++) {
459 if ($question_id == $this->all_question_array[$i]->getID()) {
468 * _makeQuestionString - Make comma separated question number string
470 * @param int array Array of question number
471 * @return string question_strong (example: 1, 2, 3, 7);
473 function _makeQuestionString($arr) {
475 /* No questions to add */
476 if (!$arr || !is_array($arr) || count($arr)<1) {
479 return join(',', $arr);
483 * _updateQuestionString - Update comma saparated question number string
485 * @param int array Array of questions to add
486 * @param int array Array of questions to delete
487 * @return string question_strong (example: 1, 2, 3, 7);
489 function _updateQuestionString(&$arr_to_add, &$arr_to_del) {
490 /* Get array of current question string */
491 $arr = & $this->getQuestionArray();
493 /* questions to add */
497 if ($arr_to_add && is_array($arr_to_add) && count($arr_to_add)>0) {
498 for ($i = 0; $i < count($arr_to_add); $i++) {
499 /* Avoid double question */
500 if ($arr_to_add[$i] && array_search($arr_to_add[$i], $arr) == false && $arr_to_add[$i]!=$arr[0]) {
501 $arr[] = $arr_to_add[$i];
507 /* questions to delete */
508 if ($arr_to_del && is_array($arr_to_del) && count($arr_to_del)>0) {
510 for ($i = 0; $i < count($arr); $i++) {
511 /* If the value is no in the delete array, copy it into new array */
512 if ($arr[$i] && array_search($arr[$i], $arr_to_del)==false && $arr_to_del[0]!=$arr[$i]) {
513 $new_arr[] = $arr[$i];
516 /* copy new_arr to arr */
520 /* converty array to String */
521 return $this->_makeQuestionString($arr);
525 * _updateArrayOrder - Update array order
527 * @param int question number
528 * @param int increment or decrement (must be 1 or -1)
529 * @return string question_strong (example: 1, 2, 3, 7);
531 function _updateQuestionStringOrder($question_number, $delta) {
532 /* Get array of current question string */
533 $arr = & $this->getQuestionArray();
535 /* We are expectiong array */
536 if (!$arr || !is_array($arr)) {
537 return $this->getQuestionString();
540 $index = array_search($question_number, $arr);
542 /* The question number is not in the array
543 * We have nothing to change
545 if ($index==false && $question_number!=$arr[0]) {
546 return $this->getQuestionString();
549 $new_index = $index + $delta;
551 /* Out of boundary */
552 if ($new_index < 0 || $new_index >= count($arr)) {
553 return $this->getQuestionString();
558 $arr[$index] = $arr[$new_index];
559 $arr[$new_index] = $tmp;
561 /* converty array to String */
562 return $this->_makeQuestionString($arr);
568 // c-file-style: "bsd"