move code up one directory
[atutor.git] / mods / _standard / faq / edit_question.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_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                 //These will truncate the content of the length to 240 as defined in the db.
56                 $_POST['question'] = validate_length($_POST['question'], 250);
57                 $_POST['answer'] = validate_length($_POST['answer'], 250);
58
59                 $sql = "UPDATE ".TABLE_PREFIX."faq_entries SET question='$_POST[question]', answer='$_POST[answer]', topic_id=$_POST[topic_id] WHERE entry_id=$id";
60                 $result = mysql_query($sql,$db);
61
62                 $msg->addFeedback('QUESTION_UPDATED');
63                 header('Location: index_instructor.php');
64                 exit;
65         }
66 }
67 $onload = 'document.form.topic.focus();';
68
69 require(AT_INCLUDE_PATH.'header.inc.php');
70
71 if ($id == 0) {
72         $msg->printErrors('ITEM_NOT_FOUND');
73         require (AT_INCLUDE_PATH.'footer.inc.php');
74         exit;
75 }
76
77 $sql = "SELECT * FROM ".TABLE_PREFIX."faq_entries WHERE entry_id=$id";
78 $result = mysql_query($sql,$db);
79 if (!($row = mysql_fetch_assoc($result))) {
80         $msg->printErrors('ITEM_NOT_FOUND');
81         require (AT_INCLUDE_PATH.'footer.inc.php');
82         exit;
83 }
84
85
86 $sql    = "SELECT name, topic_id FROM ".TABLE_PREFIX."faq_topics WHERE course_id=$_SESSION[course_id] ORDER BY name";
87 $result = mysql_query($sql, $db);
88 $num_topics = mysql_num_rows($result);
89 if (!$num_topics) {
90         $msg->printErrorS('NO_FAQ_TOPICS');
91         require(AT_INCLUDE_PATH.'footer.inc.php');
92         exit;
93 }
94
95 $sql    = "SELECT name, topic_id FROM ".TABLE_PREFIX."faq_topics WHERE course_id=$_SESSION[course_id] ORDER BY name";
96 $result = mysql_query($sql, $db);
97 $faq_topics = array();
98 while ($topic_row = mysql_fetch_assoc($result)){
99         $faq_topics[] = $topic_row;
100 }
101                                 
102
103
104 $savant->assign('row', $row);
105 $savant->assign('result', $result);
106 $savant->assign('faq_topics', $faq_topics);
107 $savant->display('instructor/faq/edit_question.tmpl.php');
108 require (AT_INCLUDE_PATH.'footer.inc.php'); ?>