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