f9f3db8a9df5cb69b2c349dd8bcc9c6e19d3a71d
[atutor.git] / docs / mods / _standard / reading_list / add_resource_av.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 $page_return = $_GET['page_return'];
27
28 // check if user has submitted form
29 if (isset($_POST['cancel'])) {
30         $msg->addFeedback('CANCELLED');
31
32         header('Location: display_resources.php');
33         exit;
34 } else if (isset($_POST['submit'])) {
35         $missing_fields = array();
36
37         if (trim($_POST['title']) == '') {
38                 $missing_fields[] = _AT('title');
39         }
40         if (trim($_POST['author']) == '') {
41                 $missing_fields[] = _AT('author');
42         }
43
44         if ($missing_fields) {
45                 $missing_fields = implode(', ', $missing_fields);
46                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
47         }
48
49
50         if (!$msg->containsErrors()) {
51                 $_POST['title'] = $addslashes(validate_length($_POST['title'], 255));
52                 $_POST['author'] = $addslashes(validate_length($_POST['author'], 150));
53                 $_POST['publisher'] = $addslashes(validate_length($_POST['publisher'], 150));
54                 $_POST['date'] = $addslashes($_POST['date']);
55                 $_POST['comments'] = $addslashes(validate_length($_POST['comments'], 255));
56                 
57                 if ($id == '0'){ // creating a new URL resource
58                         $sql = "INSERT INTO ".TABLE_PREFIX."external_resources VALUES (NULL, $_SESSION[course_id],
59                         ".RL_TYPE_AV.", 
60                         '$_POST[title]', 
61                         '$_POST[author]', 
62                         '$_POST[publisher]', 
63                         '$_POST[date]', 
64                         '$_POST[comments]',
65                         '',
66                         '')";
67                         $result = mysql_query($sql,$db);
68
69                         // index to new URL resource
70                         $id_new = mysql_insert_id($db);
71
72                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
73                 } else { // modifying an existing URL resource
74
75                         $sql = "UPDATE ".TABLE_PREFIX."external_resources SET title='$_POST[title]', author='$_POST[author]', publisher='$_POST[publisher]', date='$_POST[date]', comments='$_POST[comments]' WHERE resource_id='$id' AND course_id=$_SESSION[course_id]";
76
77                         $result = mysql_query($sql,$db);
78
79                         // index to URL resource
80                         $id_new = $id;
81
82                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
83                 }
84
85                 if (trim($_POST['page_return']) != ''){
86                         header('Location: '. $_POST['page_return']. '?existingbook='. $id_new);
87                 } else {
88                         header('Location: index_instructor.php');
89                 }
90                 exit;
91         } else { // submission contained an error, update form values for redisplay
92                 $title       = $stripslashes($_POST['title']);
93                 $author      = $stripslashes($_POST['author']);
94                 $publisher   = $stripslashes($_POST['publisher']);
95                 $date        = $stripslashes($_POST['date']);
96                 $comments    = $stripslashes($_POST['comments']);
97                 $page_return = $stripslashes($_POST['page_return']);
98         }
99 }
100
101 // is user modifying an existing AV resource?
102 if ($id && !isset($_POST['submit'])){
103         // yes, get resource from database
104         $id = intval ($_GET['id']);
105
106         $sql = "SELECT * FROM ".TABLE_PREFIX."external_resources WHERE course_id=$_SESSION[course_id] AND resource_id=$id";
107         $result = mysql_query($sql, $db);
108         if ($row = mysql_fetch_assoc($result)){
109                 $title     = AT_print($row['title'], 'input.text');
110                 $author    = AT_print($row['author'], 'input.text');
111                 $publisher = AT_print($row['publisher'], 'input.text');
112                 $date      = AT_print($row['date'], 'input.text'); 
113                 $comments  = AT_print($row['comments'], 'input.text');
114         }
115         // change title of page to 'edit AV resource' (default is 'add AV resource')
116         $_pages['mods/_standard/reading_list/add_resource_av.php'][title_var] = 'rl_edit_resource_av';
117 } else if ($id) {
118         $_pages['mods/_standard/reading_list/add_resource_av.php'][title_var] = 'rl_edit_resource_av';
119 }
120
121 $onload = 'document.form.name.focus();';
122
123 require(AT_INCLUDE_PATH.'header.inc.php');
124 ?>
125
126 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
127 <input type="hidden" name="id" value="<?php echo $id ?>" />
128 <input type="hidden" name="page_return" value="<?php echo $page_return ?>" />
129
130 <div class="input-form">        
131         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('rl_add_resource_av'); ?></legend>
132         <div class="row">
133                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="title"><?php  echo _AT('title'); ?></label><br />
134                 <input type="text" name="title" size="35" id="title" value="<?php echo $title; ?>" />
135         </div>
136
137         <div class="row">
138                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="author"><?php  echo _AT('author'); ?></label><br />
139                 <input type="text" name="author" size="25" id="author" value="<?php echo $author; ?>" />
140         </div>
141
142         <div class="row">
143                 <label for="date"><?php  echo _AT('rl_year_written'); ?></label><br />
144                 <input type="text" name="date" size="6" id="date" value="<?php echo $date; ?>" />
145         </div>
146
147         <div class="row">
148                 <label for="publisher"><?php  echo _AT('rl_publisher'); ?></label><br />
149                 <input type="text" name="publisher" size="20" id="publisher" value="<?php echo $publisher; ?>" />
150         </div>
151
152         <div class="row">
153                 <label for="comments"><?php  echo _AT('comment'); ?></label><br />
154                 <textarea name="comments" cols="30" rows="2" id="comments"><?php echo $comments; ?></textarea>
155         </div>
156
157         <div class="row buttons">
158                 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" accesskey="s" />
159                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
160         </div>
161         </fieldset>
162 </div>
163 </form>
164
165 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>