tagging as ATutor 1.5.4-release
[atutor.git] / faq / edit_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
20 if (isset($_POST['cancel'])) {
21         $msg->addFeedback('CANCELLED');
22         header('Location: index_instructor.php');
23         exit;
24
25
26 if (isset($_GET['id'])) {
27         $id = intval($_GET['id']);
28 } else {
29         $id = intval($_POST['id']);
30 }
31
32 if (isset($_POST['submit'])) {
33         $_POST['question'] = trim($_POST['question']);
34         $_POST['answer'] = trim($_POST['answer']);
35
36         $missing_fields = array();
37         
38         if (!$_POST['question']) {
39                 $missing_fields[] = _AT('question');
40         }
41
42         if (!$_POST['answer']) {
43                 $missing_fields[] = _AT('answer');
44         }
45
46         if ($missing_fields) {
47                 $missing_fields = implode(', ', $missing_fields);
48                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
49         }
50
51         if (!$msg->containsErrors()) {
52                 $_POST['question'] = $addslashes($_POST['question']);
53                 $_POST['answer'] = $addslashes($_POST['answer']);
54                 $_POST['topic_id'] = intval($_POST['topic_id']);
55
56                 $sql = "UPDATE ".TABLE_PREFIX."faq_entries SET question='$_POST[question]', answer='$_POST[answer]', topic_id=$_POST[topic_id] WHERE entry_id=$id";
57                 $result = mysql_query($sql,$db);
58
59                 $msg->addFeedback('QUESTION_UPDATED');
60                 header('Location: index_instructor.php');
61                 exit;
62         }
63 }
64 $onload = 'document.form.topic.focus();';
65
66 require(AT_INCLUDE_PATH.'header.inc.php');
67
68 if ($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."faq_entries WHERE entry_id=$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
83 $sql    = "SELECT name, topic_id FROM ".TABLE_PREFIX."faq_topics WHERE course_id=$_SESSION[course_id] ORDER BY name";
84 $result = mysql_query($sql, $db);
85 $num_topics = mysql_num_rows($result);
86 if (!$num_topics) {
87         $msg->printErrorS('NO_FAQ_TOPICS');
88         require(AT_INCLUDE_PATH.'footer.inc.php');
89         exit;
90 }
91
92 ?>
93
94 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
95 <input type="hidden" name="id" value="<?php echo $row['entry_id']; ?>" />
96
97 <div class="input-form">
98         <div class="row">
99                 <?php
100                         $sql    = "SELECT name, topic_id FROM ".TABLE_PREFIX."faq_topics WHERE course_id=$_SESSION[course_id] ORDER BY name";
101                         $result = mysql_query($sql, $db);
102                 ?>
103
104                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="topic"><?php  echo _AT('topic'); ?></label><br />
105                 <select name="topic_id" id="topic">
106                         <?php while ($topic_row = mysql_fetch_assoc($result)): ?>
107                                 <option value="<?php echo $topic_row['topic_id']; ?>"<?php if ($topic_row['topic_id'] == $row['topic_id']) { echo ' selected="selected"'; } ?>><?php echo htmlspecialchars($topic_row['name']); ?></option>
108                         <?php endwhile; ?>
109                 </select>
110         </div>
111
112         <div class="row">
113                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="question"><?php echo _AT('question'); ?>:</label><br />
114                 <input type="text" name="question" size="50" id="question" value="<?php if (isset ($_POST['question'])) { echo $stripslashes($_POST['question']); } else { echo $row['question']; } ?>" />
115         </div>
116
117         <div class="row">
118                 <div class="required" title="<?php echo _AT('required_field'); ?>">*</div><label for="answer"><?php  echo _AT('answer'); ?></label><br />
119                 <textarea name="answer" cols="45" rows="3" id="answer" style="width:90%;"><?php if (isset ($_POST['answer'])) { echo $stripslashes($_POST['answer']); } else { echo $row['answer']; } ?></textarea>
120         </div>
121
122         <div class="row buttons">
123                 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" accesskey="s" />
124                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?> " />
125         </div>
126
127 </div>
128 </form>
129 <?php require (AT_INCLUDE_PATH.'footer.inc.php'); ?>