made a copy
[atutor.git] / include / html / dropdowns / poll.inc.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto                  */
7 /* http://atutor.ca                                                                                                             */
8 /*                                                                                                                                              */
9 /* This program is free software. You can redistribute it and/or                */
10 /* modify it under the terms of the GNU General Public License                  */
11 /* as published by the Free Software Foundation.                                                */
12 /************************************************************************/
13 // $Id$
14
15 if (!defined('AT_INCLUDE_PATH')) { exit; }
16 global $_base_path, $include_all, $include_one;
17 global $savant;
18 global $db;
19
20 // global $course_id is set when a guest accessing a public course. 
21 // This is to solve the issue that the google indexing fails as the session vars are lost.
22 global $course_id;
23 if (isset($_SESSION['course_id'])) $course_id = $_SESSION['course_id'];
24
25 if (isset($_POST['poll_submit'], $_POST['choice'])) {
26         $poll_id = intval($_POST['poll_id']);
27
28         $sql = "INSERT INTO ".TABLE_PREFIX."polls_members VALUES($poll_id, $_SESSION[member_id])";
29         if ($result = mysql_query($sql, $db)) {
30                 $n = intval($_POST['choice']);
31
32                 $sql = "UPDATE ".TABLE_PREFIX."polls SET count$n=count$n+1, total=total+1 WHERE poll_id=$poll_id AND course_id=$course_id";
33                 $result = mysql_query($sql, $db);
34         }
35 }
36
37 ob_start(); 
38
39 if (!isset($include_all, $include_one)) {
40         $include_one = ' checked="checked"';
41 }
42
43 $sql = "SELECT * FROM ".TABLE_PREFIX."polls WHERE course_id=$course_id ORDER BY created_date DESC LIMIT 1";
44 $result = mysql_query($sql, $db);
45
46 if ($row = mysql_fetch_assoc($result)) {
47         echo '<table width="100%">';
48
49         if (!authenticate(AT_PRIV_POLLS, AT_PRIV_RETURN)) {
50                 $sql = "SELECT * FROM ".TABLE_PREFIX."polls_members WHERE poll_id=$row[poll_id] AND member_id=$_SESSION[member_id]";
51                 $result = mysql_query($sql, $db);
52         }
53         if (authenticate(AT_PRIV_POLLS, AT_PRIV_RETURN) || ($my_row = mysql_fetch_assoc($result))) {
54                 echo '<tr>';
55                 echo '<td valign="top" class="dropdown-heading" align="left"><strong>' . AT_print($row['question'], 'polls.question') . '</strong>';
56                 echo '</td></tr>';
57
58                 // we already voted
59                 for ($i=1; $i<= AT_NUM_POLL_CHOICES; $i++) {
60                         if ($row['choice' . $i]) {
61                                 if ($row['total']) {
62                                         $width = round($row['count' . $i] / $row['total'] * 110);
63                                 } else {
64                                         $width = 0;
65                                 }
66
67                                 echo '<tr>';
68                                 echo '<td valign="top" class="dropdown"  align="left">';
69                                 echo '<small>' . AT_print($row['choice' . $i], 'polls.choice') . '</small><br />';
70                                 echo '<img src="'.$_base_path . 'images/blue.gif" height="5" width="'.$width.'" alt="" /> '.$row['count' . $i];
71                                 echo '</td></tr>';
72                         }
73                 }
74         } else {
75                 // show the form to vote
76                 echo '<tr>';
77                 echo '<td valign="top" class="dropdown" align="left"><strong>' . AT_print($row['question'], 'polls.question') . '</strong>';
78                 echo '<form method="post" action="'.htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES).'"><input type="hidden" name="poll_id" value="'.$row['poll_id'].'" />';
79                 echo '<table width="100%" border="0" cellspacing="0" cellpadding="0" summary="">';
80                 for ($i=1; $i<= AT_NUM_POLL_CHOICES; $i++) {
81                         if ($row['choice' . $i]) {
82                                 echo '<tr>';
83                                 echo '<td valign="top" align="left">';
84                                 echo '<small><input type="radio" name="choice" value="'.$i.'" id="c'.$i.'" /><label for="c'.$i.'">' . AT_print($row['choice' . $i], 'polls.choice') . '</label></small></td></tr>';
85                         }
86                 }
87
88                 echo '<tr>';
89                 echo '<td valign="top" align="center"><input type="submit" name="poll_submit" value="'._AT('submit').'" class="button" />';
90                 echo '<br /><small>'._AT('vote_to_see_results').'</small>';
91                 echo '</td></tr>';
92
93                 echo '</table></form></td></tr>';
94         }
95         echo '</table>';
96
97 } else {
98         echo '<em>'._AT('none_found').'</em><br />';
99 }
100
101 $savant->assign('dropdown_contents', ob_get_contents());
102 ob_end_clean();
103
104 $savant->assign('title', _AT('polls'));
105 $savant->display('include/box.tmpl.php');
106 ?>