SVN tag for release should only contain scripts in "docs" folder without "mods",...
[atutor.git] / docs / mods / _standard / polls / tools / add.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 define('AT_INCLUDE_PATH', '../../../../include/');
14 require (AT_INCLUDE_PATH.'vitals.inc.php');
15
16 authenticate(AT_PRIV_POLLS);
17
18 if ($_POST['cancel']) {
19         $msg->addFeedback('CANCELLED');
20         Header('Location: index.php');
21         exit;
22 }
23
24 if ($_POST['add_poll'] && (authenticate(AT_PRIV_POLLS, AT_PRIV_RETURN))) {
25         if (trim($_POST['question']) == '') {
26                 $msg->addError(array('EMPTY_FIELDS', _AT('question')));
27         }
28
29         if ((trim($_POST['c1']) == '') || (trim($_POST['c2']) == '')) {
30                 $msg->addError('POLL_QUESTION_MINIMUM');
31         }
32
33         if (!$msg->containsErrors()) {
34                 $_POST['question'] = $addslashes($_POST['question']);
35                 //Check if the question has exceeded the words amount - 100, decided in the db
36                 if ($strlen($_POST['question']) > 100){
37                         $_POST['question'] = $substr($_POST['question'], 0, 100);
38                 }
39
40                 for ($i=1; $i<= AT_NUM_POLL_CHOICES; $i++) {
41                         $trimmed_word = $addslashes($_POST['c' . $i]);
42                         if ($strlen($trimmed_word) > 100){
43                                 $trimmed_word = $substr($trimmed_word, 0, 100);
44                         }
45                         $choices .= "'" . $trimmed_word . "',0,";
46                 }
47                 $choices = substr($choices, 0, -1);     //Remove the last comma.
48
49                 $sql    = "INSERT INTO ".TABLE_PREFIX."polls VALUES (NULL, $_SESSION[course_id], '$_POST[question]', NOW(), 0, $choices)";
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 ?>
65
66 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
67 <input type="hidden" name="add_poll" value="true" />
68
69 <div class="input-form">        
70 <fieldset class="group_form"><legend class="group_form"><?php echo _AT('add_poll'); ?></legend>
71         <div class="row">
72                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="question"><?php  echo _AT('question'); ?></label><br />
73                 <textarea name="question" cols="45" rows="3" id="question" style="width:90%;"><?php if (isset ($_POST['question'])) echo htmlspecialchars($_POST['question']);  ?></textarea>
74         </div>
75
76 <?php for ($i=1; $i<= AT_NUM_POLL_CHOICES; $i++): ?>
77         <div class="row">
78                 <?php if (($i==1) || ($i==2)) { ?>
79                         <span class="required" title="<?php echo _AT('required_field'); ?>">*</span>
80                 <?php } ?>
81                 <label for="c<?php echo $i; ?>"><?php echo _AT('choice'); ?> <?php echo $i; ?></label><br />
82                 <input type="text" name="c<?php echo $i; ?>" value="<?php if (isset($_POST['c' . $i])) echo htmlspecialchars($_POST['c' . $i]);  ?>" size="50" id="c<?php echo $i; ?>" />
83         </div>
84 <?php endfor; ?>
85
86         <div class="row buttons">
87                 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" accesskey="s" />
88                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
89         </div>
90         </fieldset>
91 </div>
92 </form>
93
94 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>