a9e526201d45f9e2deaffbc5215ccb6b9da732b7
[atutor.git] / docs / mods / _standard / reading_list / add_resource_book.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2008                                      */
6 /* Written by Greg Gay, Joel Kronenberg & Chris Ridpath         */
7 /* Inclusive Design Institute                                   */
8 /* http://atutor.ca                                                                                             */
9 /*                                                              */
10 /* This program is free software. You can redistribute it and/or*/
11 /* modify it under the terms of the GNU General Public License  */
12 /* as published by the Free Software Foundation.                                */
13 /****************************************************************/
14 // $Id$
15 define('AT_INCLUDE_PATH', '../../../include/');
16 require (AT_INCLUDE_PATH.'vitals.inc.php');
17 authenticate(AT_PRIV_READING_LIST);
18
19 // initial values for form
20 $id = intval($_REQUEST['id']);
21 $title = "";
22 $author = "";
23 $publisher = ""; 
24 $date = ""; 
25 $comments = "";
26 $isbn = "";
27 $page_return = $_GET['page_return'];
28
29 // check if user has submitted form
30 if (isset($_POST['cancel'])) {
31         $msg->addFeedback('CANCELLED');
32
33         header('Location: display_resources.php');
34         exit;
35 } else if (isset($_POST['submit'])) {
36         $missing_fields = array();
37
38         if (trim($_POST['title']) == '') {
39                 $missing_fields[] = _AT('title');
40         }
41         if (trim($_POST['author']) == '') {
42                 $missing_fields[] = _AT('author');
43         }
44
45         if ($missing_fields) {
46                 $missing_fields = implode(', ', $missing_fields);
47                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
48         }
49
50
51         if (!$msg->containsErrors()) {
52                 $_POST['title'] = $addslashes(validate_length($_POST['title'], 255));
53                 $_POST['author'] = $addslashes(validate_length($_POST['author'], 150));
54                 $_POST['publisher'] = $addslashes(validate_length($_POST['publisher'], 150));
55                 $_POST['date']      = $addslashes($_POST['date']);
56                 $_POST['comments'] = $addslashes(validate_length($_POST['comments'], 255));
57                 $_POST['isbn']      = $addslashes($_POST['isbn']);
58                 
59                 if ($id == 0){ // creating a new book resource
60                         $sql = "INSERT INTO ".TABLE_PREFIX."external_resources VALUES (NULL, $_SESSION[course_id],
61                                 ".RL_TYPE_BOOK.", 
62                                 '$_POST[title]', 
63                                 '$_POST[author]', 
64                                 '$_POST[publisher]', 
65                                 '$_POST[date]', 
66                                 '$_POST[comments]',
67                                 '$_POST[isbn]',
68                                 '')";
69                         $result = mysql_query($sql,$db);
70
71                         // index to new book resource
72                         $id_new = mysql_insert_id($db);
73
74                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
75                 } else { // modifying an existing book resource
76
77                         $sql = "UPDATE ".TABLE_PREFIX."external_resources SET title='$_POST[title]', author='$_POST[author]', publisher='$_POST[publisher]', date='$_POST[date]', comments='$_POST[comments]', id='$_POST[isbn]' WHERE resource_id='$id' AND course_id=$_SESSION[course_id]";
78
79                         $result = mysql_query($sql,$db);
80
81                         // index to book resource
82                         $id_new = $id;
83
84                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
85                 }
86
87                 if (trim($_POST['page_return']) != ''){
88                         header('Location: '. $_POST['page_return']. '?existingbook='. $id_new);
89                 }
90                 else {
91                         header('Location: index_instructor.php');
92                 }
93                 exit;
94         } else { // submission contained an error, update form values for redisplay
95                 $title       = $stripslashes($_POST['title']);
96                 $author      = $stripslashes($_POST['author']);
97                 $publisher   = $stripslashes($_POST['publisher']);
98                 $date        = $stripslashes($_POST['date']);
99                 $comments    = $stripslashes($_POST['comments']);
100                 $isbn        = $stripslashes($_POST['isbn']);
101                 $page_return = $stripslashes($_POST['page_return']);
102         }
103 }
104
105 // is user modifying an existing book resource?
106 if ($id && !isset($_POST['submit'])){
107         // yes, get resource from database
108
109         $sql = "SELECT * FROM ".TABLE_PREFIX."external_resources WHERE course_id=$_SESSION[course_id] AND resource_id=$id";
110         $result = mysql_query($sql, $db);
111         if ($row = mysql_fetch_assoc($result)){
112                 $title     = AT_print($row['title'], 'input.text');
113                 $author    = AT_print($row['author'], 'input.text');
114                 $publisher = AT_print($row['publisher'], 'input.text');
115                 $date      = AT_print($row['date'], 'input.text'); 
116                 $comments  = AT_print($row['comments'], 'input.text');
117                 $isbn      = AT_print($row['id'], 'input.text');
118         }
119         // change title of page to 'edit book resource' (default is 'add book resource')
120         $_pages['mods/_standard/reading_list/add_resource_book.php']['title_var'] = 'rl_edit_resource_book';
121 } else if ($id) {
122         $_pages['mods/_standard/reading_list/add_resource_book.php']['title_var'] = 'rl_edit_resource_book';
123 }
124
125 $onload = 'document.form.name.focus();';
126
127 require(AT_INCLUDE_PATH.'header.inc.php');
128 ?>
129
130 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
131 <input type="hidden" name="id" value="<?php echo $id ?>" />
132 <input type="hidden" name="page_return" value="<?php echo $page_return ?>" />
133
134 <div class="input-form">        
135         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('rl_add_resource_book'); ?></legend>
136         <div class="row">
137                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="title"><?php  echo _AT('title'); ?></label><br />
138                 <input type="text" name="title" size="35" id="title" value="<?php echo $title; ?>" />
139         </div>
140
141         <div class="row">
142                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="author"><?php  echo _AT('author'); ?></label><br />
143                 <input type="text" name="author" size="25" id="author" value="<?php echo $author; ?>" />
144         </div>
145
146         <div class="row">
147                 <label for="date"><?php  echo _AT('rl_year_written'); ?></label><br />
148                 <input type="text" name="date" size="6" id="date" value="<?php echo $date; ?>" />
149         </div>
150
151         <div class="row">
152                 <label for="publisher"><?php  echo _AT('rl_publisher'); ?></label><br />
153                 <input type="text" name="publisher" size="20" id="publisher" value="<?php echo $publisher; ?>" />
154         </div>
155
156         <div class="row">
157                 <label for="isbn"><?php  echo _AT('rl_isbn_number'); ?></label><br />
158                 <input type="text" name="isbn" size="15" id="isbn" value="<?php echo $isbn; ?>" />
159         </div>
160
161         <div class="row">
162                 <label for="comments"><?php  echo _AT('comment'); ?></label><br />
163                 <textarea name="comments" cols="30" rows="2" id="comments"><?php echo $comments; ?></textarea>
164         </div>
165
166         <div class="row buttons">
167                 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" accesskey="s" />
168                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
169         </div>
170         </fieldset>
171 </div>
172 </form>
173
174 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>