tagging as ATutor 1.5.4-release
[atutor.git] / faq / add_question.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_FAQ);
18
19 if (isset($_POST['cancel'])) {
20         $msg->addFeedback('CANCELLED');
21         header('Location: index_instructor.php');
22         exit;
23 } else if (isset($_POST['submit'])) {
24         $_POST['question'] = trim($_POST['question']);
25         $_POST['answer'] = trim($_POST['answer']);
26
27         $missing_fields = array();
28         
29         if (!$_POST['question']) {
30                 $missing_fields[] = _AT('question');
31         }
32
33         if (!$_POST['answer']) {
34                 $missing_fields[] = _AT('answer');
35         }
36
37         if ($missing_fields) {
38                 $missing_fields = implode(', ', $missing_fields);
39                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
40         }
41
42
43         if (!$msg->containsErrors()) {
44                 $_POST['question'] = $addslashes($_POST['question']);
45                 $_POST['answer']   = $addslashes($_POST['answer']);
46                 $_POST['topic_id'] = intval($_POST['topic_id']);
47
48                 // check that this topic_id belongs to this course:
49                 $sql    = "SELECT topic_id FROM ".TABLE_PREFIX."faq_topics WHERE topic_id=$_POST[topic_id] AND course_id=$_SESSION[course_id]";
50                 $result = mysql_query($sql, $db);
51                 if ($row = mysql_fetch_assoc($result)) {
52                         $sql    = "INSERT INTO ".TABLE_PREFIX."faq_entries VALUES (NULL, $_POST[topic_id], NOW(), 1, '$_POST[question]', '$_POST[answer]')";
53                         $result = mysql_query($sql,$db);
54                 }
55                 
56                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
57                 header('Location: index_instructor.php');
58                 exit;
59         }
60 }
61
62 $onload = 'document.form.topic.focus();';
63
64 require(AT_INCLUDE_PATH.'header.inc.php');
65
66         $sql    = "SELECT name, topic_id FROM ".TABLE_PREFIX."faq_topics WHERE course_id=$_SESSION[course_id] ORDER BY name";
67         $result = mysql_query($sql, $db);
68         $num_topics = mysql_num_rows($result);
69         if (!$num_topics) {
70                 $msg->printErrors('NO_FAQ_TOPICS');
71                 require(AT_INCLUDE_PATH.'footer.inc.php');
72                 exit;
73         }
74 ?>
75
76 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
77
78 <div class="input-form">
79         <div class="row">
80
81                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="topic"><?php  echo _AT('topic'); ?></label><br />
82                 <select name="topic_id" id="topic">
83                         <?php while ($row = mysql_fetch_assoc($result)): ?>
84                                 <option value="<?php echo $row['topic_id']; ?>"<?php if (isset($_POST['topic_id']) && ($row['topic_id'] == $_POST['topic_id'])) { echo ' selected="selected"'; } ?>><?php echo htmlspecialchars($row['name']); ?></option>
85                         <?php endwhile; ?>
86                 </select>
87         </div>
88         <div class="row">
89                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="question"><?php  echo _AT('question'); ?></label><br />
90                 <input type="text" name="question" size="50" id="question" value="<?php if (isset($_POST['question'])) echo $stripslashes($_POST['question']);  ?>" />
91
92         </div>
93         <div class="row">
94                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="answer"><?php  echo _AT('answer'); ?></label><br />
95                 <textarea name="answer" cols="45" rows="3" id="answer" style="width:90%;"><?php if (isset ($_POST['answer'])) echo $stripslashes($_POST['answer']);  ?></textarea>
96         </div>
97
98
99         <div class="row buttons">
100                 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" accesskey="s" />
101                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
102         </div>
103 </div>
104 </form>
105
106 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>