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