tagging as ATutor 1.5.4-release
[atutor.git] / tools / polls / edit.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2006 by Greg Gay & Joel Kronenberg        */
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 define('AT_INCLUDE_PATH', '../../include/');
15 require (AT_INCLUDE_PATH.'vitals.inc.php');
16
17 authenticate(AT_PRIV_POLLS);
18
19
20 if ($_POST['cancel']) {
21         $msg->addFeedback('CANCELLED');
22         header('Location: index.php');
23         exit;
24 }
25
26 if (isset($_GET['poll_id'])) {
27         $poll_id = intval($_GET['poll_id']);
28 } else {
29         $poll_id = intval($_POST['poll_id']);
30 }
31
32 if ($_POST['edit_poll']) {
33         if (trim($_POST['question']) == '') {
34                 $msg->addError(array('EMPTY_FIELDS', _AT('question')));
35         }
36
37         if ((trim($_POST['c1']) == '') || (trim($_POST['c2']) == '')) {
38                 $msg->addError('POLL_QUESTION_MINIMUM');
39         }
40
41         if (!$msg->containsErrors()) {
42                 $_POST['question'] = $addslashes($_POST['question']);
43
44                 for ($i=1; $i<= AT_NUM_POLL_CHOICES; $i++) {
45                         $choices .= "choice$i = '" . $addslashes($_POST['c' . $i]) . "',";
46                 }
47                 $choices = substr($choices, 0, -1);
48
49                 $sql = "UPDATE ".TABLE_PREFIX."polls SET question='$_POST[question]', created_date=created_date $choices WHERE poll_id=$poll_id AND course_id=$_SESSION[course_id]";
50                 $result = mysql_query($sql,$db);
51
52                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
53                 Header('Location: index.php');
54                 exit;
55         }
56         for ($i=1; $i<= AT_NUM_POLL_CHOICES; $i++) {
57                 $_POST['c' . $i] = $stripslashes($_POST['c' . $i]);
58         }
59         $_POST['question'] = $stripslashes($_POST['question']);
60 }
61
62 require(AT_INCLUDE_PATH.'header.inc.php');
63
64         if ($poll_id == 0) {
65                 $msg->printErrors('ITEM_NOT_FOUND');
66                 require (AT_INCLUDE_PATH.'footer.inc.php');
67                 exit;
68         }
69         
70         $sql = "SELECT * FROM ".TABLE_PREFIX."polls WHERE poll_id=$poll_id AND course_id=$_SESSION[course_id]";
71         $result = mysql_query($sql,$db);
72         if (!($row = mysql_fetch_assoc($result))) {
73                 $msg->printErrors('ITEM_NOT_FOUND');
74                 require (AT_INCLUDE_PATH.'footer.inc.php');
75                 exit;
76         }
77
78 ?>
79
80 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
81 <input type="hidden" name="edit_poll" value="true" />
82 <input type="hidden" name="poll_id" value="<?php echo $row['poll_id']; ?>" />
83
84 <div class="input-form">
85         <div class="row">
86                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="question"><?php echo _AT('question'); ?>:</label><br />
87                 <textarea name="question" cols="55" rows="3" id="question"><?php if (isset ($_POST['question'])) { echo htmlspecialchars($_POST['question']); } else { echo htmlspecialchars($row['question']); } ?></textarea>
88         </div>
89
90 <?php
91         for ($i=1; $i<= AT_NUM_POLL_CHOICES; $i++): ?>
92                 <div class="row">
93                         <?php if (($i==1) || ($i==2)) { ?>
94                                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div>
95                         <?php } ?>
96                         <label for="c<?php echo $i; ?>"><?php echo _AT('choice'); ?> <?php echo $i; ?>:</label><br />
97                         <input type="text" name="c<?php echo $i; ?>" id="c<?php echo $i; ?>" value="<?php if (isset ($_POST['c' . $i])) { echo htmlspecialchars($_POST['c' . $i]); } else { echo htmlspecialchars($row['choice' . $i]); }?>" size="40" />
98                 </div>
99
100 <?php endfor; ?>
101         <div class="row buttons">
102                 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" accesskey="s" />
103                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?> " />
104         </div>
105
106 </div>
107 </form>
108 <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>