tagging as ATutor 1.5.4-release
[atutor.git] / include / html / dropdowns / poll.inc.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2006 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 if (isset($_POST['poll_submit'], $_POST['choice'])) {
21         $poll_id = intval($_POST['poll_id']);
22
23         $sql = "INSERT INTO ".TABLE_PREFIX."polls_members VALUES($poll_id, $_SESSION[member_id])";
24         if ($result = mysql_query($sql, $db)) {
25                 $n = intval($_POST['choice']);
26
27                 $sql = "UPDATE ".TABLE_PREFIX."polls SET count$n=count$n+1, total=total+1 WHERE poll_id=$poll_id AND course_id=$_SESSION[course_id]";
28                 $result = mysql_query($sql, $db);
29         }
30 }
31
32 ob_start(); 
33
34 if (!isset($include_all, $include_one)) {
35         $include_one = ' checked="checked"';
36 }
37
38 $sql = "SELECT * FROM ".TABLE_PREFIX."polls WHERE course_id=$_SESSION[course_id] ORDER BY created_date DESC LIMIT 1";
39 $result = mysql_query($sql, $db);
40
41 if ($row = mysql_fetch_assoc($result)) {
42         echo '<table width="100%">';
43
44         if (!authenticate(AT_PRIV_POLLS, AT_PRIV_RETURN)) {
45                 $sql = "SELECT * FROM ".TABLE_PREFIX."polls_members WHERE poll_id=$row[poll_id] AND member_id=$_SESSION[member_id]";
46                 $result = mysql_query($sql, $db);
47         }
48         if (authenticate(AT_PRIV_POLLS, AT_PRIV_RETURN) || ($my_row = mysql_fetch_assoc($result))) {
49                 echo '<tr>';
50                 echo '<td valign="top" class="dropdown-heading" align="left"><strong>' . AT_print($row['question'], 'polls.question') . '</strong>';
51                 echo '</td></tr>';
52
53                 // we already voted
54                 for ($i=1; $i<= AT_NUM_POLL_CHOICES; $i++) {
55                         if ($row['choice' . $i]) {
56                                 if ($row['total']) {
57                                         $width = round($row['count' . $i] / $row['total'] * 110);
58                                 } else {
59                                         $width = 0;
60                                 }
61
62                                 echo '<tr>';
63                                 echo '<td valign="top" class="dropdown"  align="left">';
64                                 echo '<small>' . AT_print($row['choice' . $i], 'polls.choice') . '</small><br />';
65                                 echo '<img src="'.$_base_path . 'images/blue.gif" height="5" width="'.$width.'" alt="" /> '.$row['count' . $i];
66                                 echo '</td></tr>';
67                         }
68                 }
69         } else {
70                 // show the form to vote
71                 echo '<tr>';
72                 echo '<td valign="top" class="dropdown" align="left"><strong>' . AT_print($row['question'], 'polls.question') . '</strong>';
73                 echo '<form method="post" action="'.htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES).'"><input type="hidden" name="poll_id" value="'.$row['poll_id'].'" />';
74                 echo '<table width="100%" border="0" cellspacing="0" cellpadding="0" summary="">';
75                 for ($i=1; $i<= AT_NUM_POLL_CHOICES; $i++) {
76                         if ($row['choice' . $i]) {
77                                 echo '<tr>';
78                                 echo '<td valign="top" align="left">';
79                                 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>';
80                         }
81                 }
82
83                 echo '<tr>';
84                 echo '<td valign="top" align="center"><input type="submit" name="poll_submit" value="'._AT('submit').'" class="button" />';
85                 echo '<br /><small>'._AT('vote_to_see_results').'</small>';
86                 echo '</td></tr>';
87
88                 echo '</table></form></td></tr>';
89         }
90         echo '</table>';
91
92 } else {
93         echo '<em>'._AT('none_found').'</em><br />';
94 }
95
96 $savant->assign('dropdown_contents', ob_get_contents());
97 ob_end_clean();
98
99 $savant->assign('title', _AT('polls'));
100 $savant->display('include/box.tmpl.php');
101 ?>