move code up one directory
[atutor.git] / mods / _standard / faq / add_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 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                 //These will truncate the content of the length to 240 as defined in the db.
48                 $_POST['question'] = validate_length($_POST['question'], 250);
49                 $_POST['answer'] = validate_length($_POST['answer'], 250);
50
51                 // check that this topic_id belongs to this course:
52                 $sql    = "SELECT topic_id FROM ".TABLE_PREFIX."faq_topics WHERE topic_id=$_POST[topic_id] AND course_id=$_SESSION[course_id]";
53                 $result = mysql_query($sql, $db);
54                 if ($row = mysql_fetch_assoc($result)) {
55                         $sql    = "INSERT INTO ".TABLE_PREFIX."faq_entries VALUES (NULL, $_POST[topic_id], NOW(), 1, '$_POST[question]', '$_POST[answer]')";
56                         $result = mysql_query($sql,$db);
57                 }
58                 
59                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
60                 header('Location: index_instructor.php');
61                 exit;
62         }
63 }
64
65 $onload = 'document.form.topic.focus();';
66
67 require(AT_INCLUDE_PATH.'header.inc.php');
68
69         $sql    = "SELECT name, topic_id FROM ".TABLE_PREFIX."faq_topics WHERE course_id=$_SESSION[course_id] ORDER BY name";
70         $result = mysql_query($sql, $db);
71         $num_topics = mysql_num_rows($result);
72         if (!$num_topics) {
73                 $msg->printErrors('NO_FAQ_TOPICS');
74                 require(AT_INCLUDE_PATH.'footer.inc.php');
75                 exit;
76         }
77 $savant->assign('result', $result);
78 $savant->display('instructor/faq/add_question.tmpl.php');
79 require(AT_INCLUDE_PATH.'footer.inc.php'); ?>