File: vote_function.php

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 (463 lines) | stat: -rw-r--r-- 12,423 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
<?php
/**
 * vote_function.php
 *
 * SourceForge: Breaking Down the Barriers to Open Source Development
 * Copyright 1999-2001 (c) VA Linux Systems
 * http://sourceforge.net
 *
 * @version   $Id: vote_function.php 3192 2004-07-28 15:18:52Z tperdue $
 */

/**
 * vote_number_to_stars() - Turns vote results into *'s
 *
 * @param		int		Raw value
 */
function vote_number_to_stars($raw) {
	$raw=intval($raw*2);
	//	echo "\n\n<!-- $raw -->\n\n";
	if ($raw % 2 == 0) {
		$show_half=0;
	} else {
		$show_half=1;
	}
	$count=intval($raw/2);
	for ($i=0; $i<$count; $i++) {
		$return .= html_image("ic/check.png","15","16",array());
	}
	if ($show_half==1) {
		$return .= html_image("ic/halfcheck.png","15","16",array());
	}
	return $return;
}

/**
 * vote_show_thumbs() - Show vote stars
 *
 * @param		int		The survey ID
 * @param		string	The rating type
 */
function vote_show_thumbs($id,$flag) {
	/*
		$flag
		project - 1
		release - 2
		forum_message - 3
		user - 4
	*/
	$rating=vote_get_rating ($id,$flag);
	if ($rating==0) {
		return "<strong>(unrated)</strong>";
	} else {
		return vote_number_to_stars($rating).'('.$rating.')';
	}
}

/**
 * vote_get_rating() - Get a vote rating
 *
 * @param		int		The survey ID
 * @param		string	The rating type
 */
function vote_get_rating ($id,$flag) {
	$sql="SELECT response FROM survey_rating_aggregate WHERE type='$flag' AND id='$id'";
	$result=db_query($sql);
	if (!$result || (db_numrows($result) < 1) || (db_result($result,0,0)==0)) {
		return '0';
	} else {
		return db_result($result,0,0);
	}
}

/**
 * vote_show_release_radios() - Show release radio buttons
 *
 * @param		int		Survey ID
 * @param		string	The rating type
 */
function vote_show_release_radios ($vote_on_id,$flag) {
	/*
		$flag
		project - 1
		release - 2
		forum_message - 3
		user - 4
	*/

//html_blankimage($height,$width)
	$rating=vote_get_rating ($vote_on_id,$flag);
	if ($rating==0) {
		$rating='2.5';
	}
	$rating=((16*vote_get_rating ($vote_on_id,$flag))-15);

	global $REQUEST_URI;
	?>
	<span style="font-size:smaller">
	<form action="/survey/rating_resp.php" method="post">
	<input type="radio" name="vote_on_id" value="<?php echo $vote_on_id; ?>" />
	<input type="radio" name="redirect_to" value="<?php echo urlencode($REQUEST_URI); ?>" />
	<input type="radio" name="flag" value="<?php echo $flag; ?>" />
	<div align="center">
	<?php echo html_image("rateit.png","100","9",array()); ?>
	<br />
	<?php
		echo html_blankimage(1,$rating);
		echo html_image("ic/caret.png","9","6",array());
	?>
	<br />
	<input type="radio" name="response" value="1" />
	<input type="radio" name="response" value="2" />
	<input type="radio" name="response" value="3" />
	<input type="radio" name="response" value="4" />
	<input type="radio" name="response" value="5" />
	<br />
	<input type="submit" name="submit" value="Rate" />
	</div>
	</form>
	</span>
	<?php

}

/**
 * show_survey() - Select and show a specific survey from the database
 *
 * @param		int		The group ID
 * @param		int		The survey ID
 */
function show_survey ($group_id,$survey_id) {
  global $Language;

/*
	Select this survey from the database
*/

$sql="SELECT * FROM surveys WHERE survey_id='$survey_id' and group_id = '$group_id'";

$result=db_query($sql);

if (db_numrows($result) > 0) {
	echo '
		<h3>'.db_result($result, 0, 'survey_title').'</h3>
		<form action="/survey/survey_resp.php" method="post">
		<input type="hidden" name="group_id" value="'.$group_id.'" />
		<input type="hidden" name="survey_id" value="'.$survey_id.'" />';

	/*
		Select the questions for this survey
	*/

	$questions=db_result($result, 0, 'survey_questions');
	$quest_array=explode(',', $questions);
	$count=count($quest_array);
	echo '
		<table border="0">';
	$q_num=1;

	for ($i=0; $i<$count; $i++) {
		/*
			Build the questions on the HTML form
		*/

		$sql="SELECT * FROM survey_questions WHERE question_id='".$quest_array[$i]."'";
		$result=db_query($sql);
		$question_type=db_result($result, 0, 'question_type');

		if ($question_type == '4') {
			/*
				Don't show question number if it's just a comment
			*/

			echo '
				<tr><td valign="top">&nbsp;</td><td>';

		} else {
			echo '
				<tr><td valign="top"><strong>';
			/*
				If it's a 1-5 question box and first in series, move Quest
				number down a bit
			*/
			if (($question_type != $last_question_type) && (($question_type == '1') || ($question_type == '3'))) {
				echo '&nbsp;<br />';
			}

			echo $q_num.'&nbsp;&nbsp;&nbsp;&nbsp;<br /></td><td>';
			$q_num++;
		}

		if ($question_type == "1") {
			/*
				This is a radio-button question. Values 1-5.
			*/
			// Show the 1-5 markers only if this is the first in a series

			if ($question_type != $last_question_type) {
				echo '
					<strong>1</strong>'.$Language->getText('survey','low').'  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>5</strong>' .$Language->getText('survey','high');
				echo '<br />';

			}

			for ($j=1; $j<=5; $j++) {
				echo '
					<input type="radio" name="_'.$quest_array[$i].'" value="'.$j.'" />';
			}

			echo '&nbsp; '.stripslashes(db_result($result, 0, 'question'));

		} else if ($question_type == '2') {
			/*
				This is a text-area question.
			*/

			echo stripslashes(db_result($result, 0, 'question')).'<br />';
			echo '
				<textarea name="_'.$quest_array[$i].'" rows="5" cols="60" wrap="soft"></textarea>';

		} else if ($question_type == '3') {
			/*
				This is a Yes/No question.
			*/

			//Show the Yes/No only if this is the first in a series

			if ($question_type != $last_question_type) {
				echo '<strong>Yes / No</strong><br />';
			}

			echo '
				<input type="radio" name="_'.$quest_array[$i].'" value="1" />';
			echo '
				<input type="radio" name="_'.$quest_array[$i].'" value="5" />';

			echo '&nbsp; '.stripslashes(db_result($result, 0, 'question'));

		} else if ($question_type == '4') {
			/*
				This is a comment only.
			*/

			echo '&nbsp;<br /><strong>'.util_make_links(stripslashes(db_result($result, 0, 'question'))).'</strong>';
			echo '
				<input type="hidden" name="_'.$quest_array[$i].'" value="-666" />';

		} else if ($question_type == '5') {
			/*
				This is a text-field question.
			*/

			echo stripslashes(db_result($result, 0, 'question')).'<br />';
			echo '
				<input type="text" name="_'.$quest_array[$i].'" size="20" maxlength="70" />';

		}
		else {
			// no answers, just show question
			echo stripslashes(db_result($result, 0, 'question')).'<br />';
		}

		echo '</td></tr>';

		$last_question_type=$question_type;
	}

	?>
	<tr><td align="center" colspan="2">

	<input type="submit" name="submit" value="<?php echo $Language->getText('general','submit') ?>" />
	<br />
	<a href="/survey/privacy.php"><?php echo $Language->getText('survey','survey_privacy') ?></a>
	</td></tr>
	</form>
	</table>
	<?php

} else {
	echo "<strong>".$Language->getText('survey','survey_not_found')."</strong>";
}

}

/**
 * Show a single question for the new user rating system
 *
 * @param		string	The question to show
 * @param		string	The array element
 */
function vote_show_a_question ($question,$element_name) {
	echo '
	<tr><td colspan="2" nowrap="nowrap">
	<input type="radio" name="Q_'. $element_name .'" value="-3">
	&nbsp; <input type="radio" name="Q_'. $element_name .'" value="-2" />
	&nbsp; <input type="radio" name="Q_'. $element_name .'" value="-1" />
	&nbsp; <input type="radio" name="Q_'. $element_name .'" value="0.1" />
	&nbsp; <input type="radio" name="Q_'. $element_name .'" value="1" />
	&nbsp; <input type="radio" name="Q_'. $element_name .'" value="2" />
	&nbsp; <input type="radio" name="Q_'. $element_name .'" value="3" />
	</td></tr>

	<tr><td colspan="2">'.$question.'
		<br />&nbsp;</td></tr>';

}

/*

	The ratings system is actually flexible enough
	to let you do N number of questions, but we are just going with 5
	that apply to everyone

*/

$USER_RATING_QUESTIONS=array();
//sorry - array starts at 1 so we can test for the questions on the receiving page
$USER_RATING_QUESTIONS[1]='Teamwork / Attitude';
$USER_RATING_QUESTIONS[2]='Code';
$USER_RATING_QUESTIONS[3]='Design / Architecture';
$USER_RATING_QUESTIONS[4]='Follow-Through / Reliability';
$USER_RATING_QUESTIONS[5]='Leadership / Management';

$USER_RATING_POPUP1[]='0 - Soloist';
$USER_RATING_POPUP1[]='1';
$USER_RATING_POPUP1[]='2';
$USER_RATING_POPUP1[]='3';
$USER_RATING_POPUP1[]='4';
$USER_RATING_POPUP1[]='5';
$USER_RATING_POPUP1[]='6 - Team Player';

$USER_RATING_POPUP2[]='0 - Beginner';
$USER_RATING_POPUP2[]='1';
$USER_RATING_POPUP2[]='2';
$USER_RATING_POPUP2[]='3';
$USER_RATING_POPUP2[]='4';
$USER_RATING_POPUP2[]='5';
$USER_RATING_POPUP2[]='6 - Master';

$USER_RATING_POPUP3[]='0 - Basic';
$USER_RATING_POPUP3[]='1';
$USER_RATING_POPUP3[]='2';
$USER_RATING_POPUP3[]='3';
$USER_RATING_POPUP3[]='4';
$USER_RATING_POPUP3[]='5';
$USER_RATING_POPUP3[]='6 - Elaborate';

$USER_RATING_POPUP4[]='0 - Unreliable';
$USER_RATING_POPUP4[]='1';
$USER_RATING_POPUP4[]='2';
$USER_RATING_POPUP4[]='3';
$USER_RATING_POPUP4[]='4';
$USER_RATING_POPUP4[]='5';
$USER_RATING_POPUP4[]='6 - Dependable';

$USER_RATING_POPUP5[]='0 - Weak';
$USER_RATING_POPUP5[]='1';
$USER_RATING_POPUP5[]='2';
$USER_RATING_POPUP5[]='3';
$USER_RATING_POPUP5[]='4';
$USER_RATING_POPUP5[]='5';
$USER_RATING_POPUP5[]='6 - Strong';

$USER_RATING_VALUES[]='-3';
$USER_RATING_VALUES[]='-2';
$USER_RATING_VALUES[]='-1';
$USER_RATING_VALUES[]='0.1';
$USER_RATING_VALUES[]='1';
$USER_RATING_VALUES[]='2';
$USER_RATING_VALUES[]='3';

/**
 * vote_show_user_rate_box() - Show user rating box
 *
 * @param		int		The user ID
 * @param		int		The user ID of the user who is rating $user_id
 */
function vote_show_user_rate_box ($user_id, $by_id=0) {
	if ($by_id) {
		$res = db_query("
			SELECT rate_field,rating FROM user_ratings
			WHERE rated_by='$by_id'
			AND user_id='$user_id'
		");
		$prev_vote = util_result_columns_to_assoc($res);
		while (list($k,$v) = each($prev_vote)) {
			if ($v == 0) {
				$prev_vote[$k] = 0.1;
			}
		}
	}

	global $USER_RATING_VALUES,$USER_RATING_QUESTIONS,$USER_RATING_POPUP1,$USER_RATING_POPUP2,$USER_RATING_POPUP3,$USER_RATING_POPUP4,$USER_RATING_POPUP5;
	echo '
	<table border="0">
		<form action="/developer/rate.php" method="post">
		<input type="hidden" name="rated_user" value="'.$user_id.'" />';

	for ($i=1; $i<=count($USER_RATING_QUESTIONS); $i++) {
		$popup="USER_RATING_POPUP$i";
		echo '<tr>
		<td colspan="2"><strong>'. $USER_RATING_QUESTIONS[$i] .':</strong><br /> '
		.html_build_select_box_from_arrays($USER_RATING_VALUES,$$popup,"Q_$i",$prev_vote[$i]/*'xzxz'*/,true,'Unrated').'</td></tr>';
	}

	echo '
		<tr><td colspan="2"><input type="submit" name="submit" value="Rate User" /></td></tr>
		</table>
	</form>';
}

/**
 * vote_show_user_rating() - Show a user rating
 *
 * @param		int		The user ID
 */
function vote_show_user_rating($user_id) {
	global $USER_RATING_QUESTIONS;
	$sql="SELECT rate_field,(avg(rating)+3) AS avg_rating,count(*) as count ".
		"FROM user_ratings ".
		"WHERE user_id='$user_id' ".
		"GROUP BY rate_field";
	$res=db_query($sql);
	$rows=db_numrows($res);
	if (!$res || $rows < 1) {

		echo '<tr><td colspan="2"><h4>Not Yet Rated</h4></td></tr>';

	} else {
		echo '<tr><td colspan="2">
			<h4>Current Ratings</h4>
			<p>
			Includes untrusted ratings.</p></td></tr>';
		for ($i=0; $i<$rows; $i++) {
			echo '
			<tr><td>'.$USER_RATING_QUESTIONS[db_result($res,$i,'rate_field')].'</td>
			<td>'.db_result($res,$i,'avg_rating').' (By '. db_result($res,$i,'count') .' Users)</td></tr>';
		}

		$res=db_query("SELECT ranking,metric,importance_factor FROM user_metric WHERE user_id='$user_id'");
		if ($res && db_numrows($res) > 0) {
			echo '<tr><td colspan="2"><strong>Trusted Overall Rating</strong></td></tr>';
			echo '<tr><td>Sitewide Ranking:</td><td><strong>'. db_result($res,0,'ranking') .'</strong></td></tr>
				<tr><td>Aggregate Score:</td><td><strong>'. number_format (db_result($res,0,'metric'),3) .'</strong></td></tr>
				<tr><td>Personal Importance:</td><td><strong>'. number_format (db_result($res,0,'importance_factor'),3) .'</strong></td></tr>';
		} else {
			echo '<tr><td colspan="2"><h4>Not Yet Included In Trusted Rankings</h4></td></tr>';
		}
	}
}

/**
 * vote_remove_all_ratings_by() - Remove all ratings by a particular user
 *
 * @param		int		The user ID
 */
function vote_remove_all_ratings_by($user_id) {
	db_query("
		DELETE FROM user_ratings
		WHERE rated_by='$user_id'
	");
}

?>