5d84e89a77e97b6921850ab748b01346e06d3592
[atutor.git] / docs / mods / _standard / polls / tools / edit.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
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                 //Check if the question has exceeded the words amount - 100, decided in the db
44                 $_POST['question'] = validate_length($_POST['question'], 100);
45
46                 for ($i=1; $i<= AT_NUM_POLL_CHOICES; $i++) {
47                         $trimmed_word = validate_length($_POST['c' . $i], 100);                 
48                         $trimmed_word = $addslashes($trimmed_word);
49                         $choices .= "choice$i = '" . $trimmed_word . "',";
50                 }
51                 $choices = substr($choices, 0, -1);
52
53                 $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]";
54                 $result = mysql_query($sql,$db);
55
56                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
57                 Header('Location: index.php');
58                 exit;
59         }
60         for ($i=1; $i<= AT_NUM_POLL_CHOICES; $i++) {
61                 $_POST['c' . $i] = $stripslashes($_POST['c' . $i]);
62         }
63         $_POST['question'] = $stripslashes($_POST['question']);
64 }
65
66 require(AT_INCLUDE_PATH.'header.inc.php');
67
68         if ($poll_id == 0) {
69                 $msg->printErrors('ITEM_NOT_FOUND');
70                 require (AT_INCLUDE_PATH.'footer.inc.php');
71                 exit;
72         }
73         
74         $sql = "SELECT * FROM ".TABLE_PREFIX."polls WHERE poll_id=$poll_id AND course_id=$_SESSION[course_id]";
75         $result = mysql_query($sql,$db);
76         if (!($row = mysql_fetch_assoc($result))) {
77                 $msg->printErrors('ITEM_NOT_FOUND');
78                 require (AT_INCLUDE_PATH.'footer.inc.php');
79                 exit;
80         }
81
82 $savant->assign('row', $row);
83 $savant->display('instructor/polls/edit.tmpl.php');
84 require (AT_INCLUDE_PATH.'footer.inc.php'); ?>