File: Survey.class

package info (click to toggle)
gforge 4.5.14-22etch13
  • links: PTS
  • area: main
  • in suites: etch
  • size: 13,004 kB
  • ctags: 11,918
  • sloc: php: 36,047; sql: 29,050; sh: 10,538; perl: 6,496; xml: 3,810; makefile: 341; python: 263; ansic: 256
file content (576 lines) | stat: -rw-r--r-- 15,694 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<?php
/**
 * GForge Survey Question Facility
 *
 * Copyright 2004 GForge, LLC
 * http://gforge.org/
 *
 * This file is part of GForge.
 *
 * GForge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GForge; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
	Survery 
	By Sung Kim 2004/2/13
*/

require_once('common/include/Error.class');
require_once('common/survey/SurveyQuestionFactory.class');

class Survey extends Error {

	/**
	 * Associative array of data from db.
	 *
	 * @var	 array   $data_array.
	 */
	var $data_array;

	/**
         * Questions array in this survey
         *
         * @var array   $question_array.
         */
	var $all_question_array;
          
	/**
	 * The Group object.
	 *
	 * @var	 object  $Group.
	 */
	var $Group; 

	/**
	 *  Constructor.
	 *
	 *  @param  object	The Group object to which this servey is associated.
	 *  @param  int	        The servey_id.
	 *  @param  array	The associative array of data.
	 *  @return boolean	success.
	 */
	function Survey(&$Group, $survey_id=false, $arr=false) {
		global $Language;
		$this->Error();
		if (!$Group || !is_object($Group)) {
			$this->setError($Language->getText('general','error_no_valid_group_object','Survey'));
			return false;
		}
		if ($Group->isError()) {
			$this->setError('Survey:: '.$Group->getErrorMessage());
			return false;
		}
		$this->Group =& $Group;

		if ($survey_id) {
			if (!$arr || !is_array($arr)) {
				if (!$this->fetchData($survey_id)) {
					return false;
				}
			} else {
				$this->data_array =& $arr;
				if ($this->data_array['group_id'] != $this->Group->getID()) {
					$this->setError($Language->getText('general','error_group_id'));
					$this->data_array = null;
					return false;
				}
			}
		}
		return true;
	}

	/**
	 *	create - use this function to create a survey 
	 *
	 *	@param	string	          The survey title
	 *	@param	int array         The question numbers to be added
         *      @param  is_active         1: Active, 0: Inactive
         *      For future options
         *      @param  is_public         0: Admins Only, 1: Group Members, 2: Gforge user, 3:Every body
         *      @param  is_result_public  0: Admins Only, 1: Group Members, 2: Gforge user, 3:voted user 4:Every body
	 *      @param  double_vote       Allow dobule vote if it is 1
	 *	@return	boolean	success.
	 */
	function create($survey_title, $add_questions, $is_active=0, $is_public=1, $is_result_public=0, $double_vote=0) {
		global $Language;

		if (!$survey_title) {
			$this->setError($Language->getText('survey_edit','survey_title_required'));
			return false;
			/* We need at least one survey question at this point */	
		} else if (!$add_questions || !is_array($add_questions) || count($add_questions)<1) {
			$this->setError($Language->getText('survey_edit','survey_question_required'));
			return false;
		}

		$group_id = $this->Group->GetID();
		
		/* Make old style survey string from array: 1, 2, 3, ..., n */
		$survey_questions = $this->_makeQuestionString($add_questions);

		$sql="insert into surveys (survey_title,group_id,survey_questions) values ('".htmlspecialchars($survey_title)."','$group_id','$survey_questions')";

		$result=db_query($sql);
		if (!$result) {
			$this->setError($Language->getText('survey_add_question','error_in_insert').db_error());
			return false;
		} 

		/* Load question to data array */
		$survey_id=db_insertid($res,'surveys','survey_id');
		return $this->fetchData($survey_id);
	}



	/**
	 *	update - use this function to update a survey 
	 *
	 *	@param	string	          The survey title
	 *	@param	int array         The question numbers to be added
	 *	@param	int array         The question numbers to be deleted
         *      @param  is_active         1: Active, 0: Inactive
         *      @param  is_public         0: Admins Only, 1: Group Members, 2: Gforge user, 3:Every body
	 *      @param  is_result_public  0: Admins Only, 1: Group Members, 2: Gforge user, 3:voted user 4:Every body
	 *      @param  double_vote       Allow dobule vote if it is 1
	 *	@return	boolean	success.
	 */
	function update($survey_title, &$add_questions, &$del_questions, $is_active=0, $is_public=1, $is_result_public=0, $double_vote=0) {
		global $Language;

		if (!$survey_title) {
			$this->setError($Language->getText('survey_edit','survey_title_required'));
			return false;
			/* We need at least one survey question at this point */	
		}

		$group_id = $this->Group->GetID();
		$survey_id = $this->getID();

		/* Ths Survey is not ready to update */
		if (!$survey_id) {
			$this->setError($Language->getText('survey_error','no_survey_data_filled'));
			return false;
		}
		
		$survey_questions = $this->_updateQuestionString($add_questions, $del_questions);
		$sql="UPDATE surveys SET survey_title='".htmlspecialchars($survey_title)."', survey_questions='$survey_questions', is_active='$is_active' ".
			"WHERE survey_id='$survey_id' AND group_id='$group_id'";
		$result=db_query($sql);
		if (db_affected_rows($result) < 1) {
			 $this->setError($Language->getText('survey_edit','update_failed').db_error());
			 return false;
		} 
		/* Update internal data */
		return $this->fetchData($survey_id);
	}

        /**
	 *	updateOrder - use this function to update question order
	 *
	 *	@param	int 	          Question number
	 *	@param	boolean           decide up or down. it is up if it is true
	 *	@return	boolean	success.
	 */
	function updateOrder($question_number, $is_up=true) {
		global $Language;
		
		$group_id = $this->Group->GetID();
		$survey_id = $this->getID();

		/* Ths Survey is not ready to update */
		if (!$survey_id) {
			$this->setError($Language->getText('survey_error','no_survey_data_filled'));
			return false;
		}

		/* Decide delta */
		if ($is_up) {
			$delta = -1;
		} else { 
			$delta = 1;
		}

		$survey_questions = $this->_updateQuestionStringOrder($question_number, $delta);
		$sql="UPDATE surveys SET survey_questions='$survey_questions' ".
			"WHERE survey_id='$survey_id' AND group_id='$group_id'";
		$result=db_query($sql);
		if (db_affected_rows($result) < 1) {
			 $this->setError($Language->getText('survey_edit','update_failed').db_error());
			 return false;
		} 
		
                /* Update internal data */
		return $this->fetchData($survey_id);
	}

	/**
	 *	delete - use this function to delete this survey
         *               (We don't support delete yet)
	 *
	 *	@return	boolean	success.
	 */
	function delete() {
		global $Language;

		$group_id = $this->Group->GetID();
		$survey_id = $this->getID();

		$sql="DELETE FROM surveys where survey_id='$survey_id' AND group_id='$group_id'";
 
		$res=db_query($sql);
		if (!$res || db_affected_rows($res) < 1) {
			$this->setError($Language->getText('survey_edit_question','delete_failed').db_error());
			return false;
		}
		
		/* Delete internal data */
		$this->data_array = null;
		return true;
	}

	/**
	 *  fetchData - re-fetch the data for this survey from the database.
	 *
	 *  @param  int	 The survey_id.
	 *  @return	boolean	success.
	 */
	function fetchData($survey_id) {
		global $Language;
		$group_id = $this->Group->GetID();
		
		$sql="SELECT * FROM surveys WHERE survey_id='$survey_id' AND group_id='$group_id'";
		$res=db_query($sql);
	
		if (!$res || db_numrows($res) < 1) {
			$this->setError($Language->getText('survey_error','no_survey_found').db_error());
			return false;
		}
		$this->data_array =& db_fetch_array($res);
		db_free_result($res);
		return true;
	}

	/**
	 *	getGroup - get the Group object this Survey is associated with.
	 *
	 *	@return	object	The Group object.
	 */
	function &getGroup() {
		return $this->Group;
	}

	/**
	 *	getID - Get the id of this Survey
	 *
	 *	@return	int	The question_id
	 */
	function getID() {
		return $this->data_array['survey_id'];
	}

	/**
	 *	isActriv - return if it is active
	 *
	 *	@return	int	is active
	 */
	function isActive() {
		return $this->data_array['is_active'];
	}

	/**
	 *	getTitle - Get the Survey title
	 *
	 *	@return string the survey title
	 */
	function getTitle() {
		return $this->data_array['survey_title'];
	}

        /**
	 *	getQuestionString - Get the question string
	 *
	 *	@return string the question
	 */
	function getQuestionString() {
		return $this->data_array['survey_questions'];
	}

        /**
	 *	getNumberOfQuestion - Get the number of questions
	 *
	 *	@return int the number questions
	 */
	function getNumberOfQuestions() {
		return count($this->getQuestionArray());
	}

        /**
	 *	getNumberOfVotes - Get the number of votes
	 *
	 *	@return int the number votes
	 */
	function getNumberOfVotes() {
		$group_id = $this->Group->GetID();
		$survey_id = $this->getID();
		
		$sql = "SELECT 1 from survey_responses where group_id='$group_id' and survey_id='$survey_id' group by user_id";
		$res=db_query($sql);
		$ret  =  db_numrows($res);
		db_free_result($res);		
		
		return $ret;
	}
	
	/**
	 *	isUserVote - Figure out the user voted or not
	 *
         *      @param  int user_id
	 *	@return true of false
	 */
	function isUserVote($user_id) {
		$group_id = $this->Group->GetID();
		$survey_id = $this->getID();

		$sql = "SELECT 1 from survey_responses where group_id='$group_id' and survey_id='$survey_id' and user_id='$user_id'";
		
		$res=db_query($sql, 1);
		$ret  =  db_numrows($res);
		db_free_result($res);		
		
		return $ret;
	}
	
        /**
	 *	getQuestionArray - Get the question string numbers in array
	 *
	 *	@return string the question
	 */
	function &getQuestionArray() {
		$questions = $this->getQuestionString();
		if (!$questions) {
			return array();
		}

		
		$arr_from_str = explode(',', $questions);

		/* Remove non existed questions */ 
		for ($i=0; $i<count($arr_from_str); $i++) {
			if ($this->_isValidQuestionID($arr_from_str[$i])) {
				$ret_arr[] = $arr_from_str[$i];
			}
		}
		
		return $ret_arr;
	}

        /**
	 *	getQuestionInstances - Get the SurveyQuestion array belongs to this Survey by order
	 *
	 *	@return string the question
	 */
	function &getQuestionInstances() {
		$ret = array();

		if (!$this->all_question_array || !is_array($this->all_question_array)) {
			$this->_fillSurveyQuestions();
		}

		$arr = & $this->getQuestionArray();
		
		for ($i=0; $i<count($arr); $i++) {
			for ($j=0; $j<count($this->all_question_array); $j++) {
				/* If it is, copy into new array in order */
				if ($this->all_question_array[$j]->getID()==$arr[$i]) {
					$ret[] = $this->all_question_array[$j];
					break;
				}
			}
		}
		
		return $ret;
	}
	
        /**
	 *	getAddableQuestionInstances - Get the addable SurveyQuestion from all questions
	 *
	 *	@return string the question
	 */
	function &getAddableQuestionInstances() {
		$ret = array();

		if (!$this->all_question_array || !is_array($this->all_question_array)) {
			 $this->_fillSurveyQuestions();
		}

		$arr = & $this->getQuestionArray();
		
		/* Copy questions only if it is not in question string */
		for ($i=0; $i<count($this->all_question_array); $i++) {
			if (array_search($this->all_question_array[$i]->getID(), $arr)==false && 
			    $this->all_question_array[$i]->getID()!=$arr[0]) {
				$ret[] = $this->all_question_array[$i];
			}
		}
		
		return $ret;
	}

	/***************************************************************
         * private question string deal methods
         * TODO: Add a joint table for surveys and survey_questions. 
         *       Deal with DBMS not comma separated string
         ***************************************************************/

	/**
	 *      _fillSurveyQuestions - Get all Survey Questions using SurveyQuestionFactory
         *
         *      @return booelan suesssness
         */
	function _fillSurveyQuestions() {
		$sqf = new SurveyQuestionFactory($this->getGroup());
		$this->all_question_array = & $sqf->getSurveyQuestions();
	}

	
	/**
	 *  _isValidQuestionID - Check it is correct question id
	 *
	 *  @param int questioin id 
	 *  @return boolean true if it is valid question id
	 */
	function _isValidQuestionID($question_id) {
		if (!$this->all_question_array || !is_array($this->all_question_array)) {
			$this->_fillSurveyQuestions();
		}
		
		for ($i=0; $i<count($this->all_question_array); $i++) {
			if ($question_id == $this->all_question_array[$i]->getID()) {
				return true;
			}
		}
		return false;
	}
 	
	
	/**
	 *  _makeQuestionString - Make comma saparated question number string
	 *
	 *  @param    int array	 Array of question number
	 *  @return   string     question_strong (example: 1, 2, 3, 7);
	 */
	function _makeQuestionString($arr) {
		$ret = "";
		
		/* No questions to add */
		if (!$arr || !is_array($arr) || count($arr)<1) {
			return $ret;
		}
		$ret.=$arr[0];
		for ($i=1; $i<count($arr); $i++) {
			$ret.=','.$arr[$i];
			
		}

		return $ret;
	}

	/**
	 *  _updateQuestionString - Update comma saparated question number string
	 *
	 *  @param    int array	 Array of questions to add
	 *  @param    int array	 Array of questions to delete
	 *  @return   string     question_strong (example: 1, 2, 3, 7);
	 */
	function _updateQuestionString(&$arr_to_add, &$arr_to_del) {
		/* Get array of current question string */
		$arr = & $this->getQuestionArray();

		/* questions to add */
		if ($arr_to_add && is_array($arr_to_add) && count($arr_to_add)>0) {
			for ($i = 0; $i < count($arr_to_add); $i++) {
				/* Avoid double question */
				if ($arr_to_add[$i] && array_search($arr_to_add[$i], $arr)==false && $arr_to_add[$i]!=$arr[0]) {
					$arr[] = $arr_to_add[$i];
				}
			}  
		}
		
		/* questions to delete */
		if ($arr_to_del && is_array($arr_to_del) && count($arr_to_del)>0) {
			for ($i = 0; $i < count($arr); $i++) {
				/* If the value is no in the delete array, copy it into new array */
				echo $arr[$i] + "HHH<BR>";

				if ($arr[$i] && array_search($arr[$i], $arr_to_del)==false && $arr_to_del[0]!=$arr[$i]) {
					$new_arr[] = $arr[$i];
				}
			}  
			/* copy new_arr to arr */
			$arr = $new_arr;
		}

		/* converty array to String */
		return $this->_makeQuestionString($arr);
	}

        /**
	 *  _updateArrayOrder - Update array order
	 *
	 *  @param    int 	 question number
	 *  @param    int	 increment or decrement (must be 1 or -1)
	 *  @return   string     question_strong (example: 1, 2, 3, 7);
	 */
	function _updateQuestionStringOrder($question_number, $delta) {
		/* Get array of current question string */
		$arr = & $this->getQuestionArray();

		/* We are expectiong array */
		if (!$arr || !is_array($arr)) {
			return $this->getQuestionString();
		}

		$index = array_search($question_number, $arr);
		
		/* The question number is not in the array 
		 * We have nothing to change 
		 */ 
		if ($index==false && $question_number!=$arr[0]) {
			return $this->getQuestionString();
		}

		$new_index = $index + $delta;

		/* Out of boundary */
		if ($new_index < 0 || $new_index >= count($arr)) {
			return $this->getQuestionString();
		}

		/* swap */
		$tmp = $arr[$index];
		$arr[$index] = $arr[$new_index];
		$arr[$new_index] = $tmp;

		/* converty array to String */
		return $this->_makeQuestionString($arr);
	}
}

// Local Variables:
// mode: php
// c-file-style: "bsd"
// End:
?>