tagging as ATutor 1.5.4-release
[atutor.git] / include / lib / course.inc.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2006 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto                  */
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 if (!defined('AT_INCLUDE_PATH')) { exit; }
15
16 function add_update_course($_POST, $isadmin = FALSE) {
17         require(AT_INCLUDE_PATH.'lib/filemanager.inc.php');
18
19         global $addslashes;
20         global $db;
21         global $system_courses;
22         global $MaxCourseSize;
23         global $msg;
24         global $_config;
25         global $_config_defaults;
26
27         $Backup =& new Backup($db);
28         $missing_fields = array();
29
30         if ($_POST['title'] == '') {
31                 $missing_fields[] = _AT('title');
32         } 
33         if (!$_POST['instructor']) {
34                 $missing_fields[] = _AT('instructor');
35         }
36
37         if ($missing_fields) {
38                 $missing_fields = implode(', ', $missing_fields);
39                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
40         }
41
42         $_POST['access']      = $addslashes($_POST['access']);
43         $_POST['title']       = $addslashes($_POST['title']);
44         $_POST['description'] = $addslashes($_POST['description']);
45         $_POST['hide']        = $addslashes($_POST['hide']);
46         $_POST['pri_lang']        = $addslashes($_POST['pri_lang']);
47         $_POST['created_date']= $addslashes($_POST['created_date']);
48         $_POST['copyright']       = $addslashes($_POST['copyright']);
49         $_POST['icon']            = $addslashes($_POST['icon']);
50         $_POST['banner']      = $addslashes($_POST['banner']);
51
52         $_POST['course']        = intval($_POST['course']);
53         $_POST['notify']        = intval($_POST['notify']);
54         $_POST['hide']          = intval($_POST['hide']);
55         $_POST['instructor']= intval($_POST['instructor']);
56         $_POST['category_parent']       = intval($_POST['category_parent']);
57         $_POST['rss']       = intval($_POST['rss']);
58
59         if ($_POST['release_date']) {
60                 $day_release    = intval($_POST['day_release']);
61                 $month_release  = intval($_POST['month_release']);
62                 $year_release   = intval($_POST['year_release']);
63                 $hour_release   = intval($_POST['hour_release']);
64                 $min_release    = intval($_POST['min_release']);
65
66                 if (!checkdate($month_release, $day_release, $year_release)) { //or date is in the past
67                         $msg->addError('RELEASE_DATE_INVALID');
68                 }
69
70                 if (strlen($month_release) == 1){
71                         $month_release = "0$month_release";
72                 }
73                 if (strlen($day_release) == 1){
74                         $day_release = "0$day_release";
75                 }
76                 if (strlen($hour_release) == 1){
77                         $hour_release = "0$hour_release";
78                 }
79                 if (strlen($min_release) == 1){
80                         $min_release = "0$min_release";
81                 }
82                 $release_date = "$year_release-$month_release-$day_release $hour_release:$min_release:00";
83         } else {
84                 $release_date = "0000-00-00 00:00:00";
85         }
86
87         $initial_content_info = explode('_', $_POST['initial_content'], 2);
88         //admin
89         $course_quotas = '';
90         if ($isadmin) {
91                 $instructor             = $_POST['instructor'];
92                 $quota                  = intval($_POST['quota']);
93                 $quota_entered  = intval($_POST['quota_entered']);
94                 $filesize               = intval($_POST['filesize']);
95                 $filesize_entered= intval($_POST['filesize_entered']);
96
97                 //if they checked 'other', set quota=entered value, if it is empty or negative, set to default (-2)
98                 if ($quota == '2') {
99                         if ($quota_entered=='' || empty($quota_entered) || $quota_entered<0 ) {
100                                 $quota = AT_COURSESIZE_DEFAULT;                         
101                         } else {
102                                 $quota = floatval($quota_entered);
103                                 $quota = megabytes_to_bytes($quota);
104                         }
105                 }
106
107                 //if they checked 'other', set filesize=entered value, if it is empty or negative, set to default 
108                 if ($filesize=='2') {
109                         if ($filesize_entered=='' || empty($filesize_entered) || $filesize_entered<0 ) {
110                                 $filesize = AT_FILESIZE_DEFAULT;
111                                 $msg->addFeedback('COURSE_DEFAULT_FSIZE');
112                         } else {
113                                 $filesize = floatval($filesize_entered);
114                                 $filesize = megabytes_to_bytes($filesize);
115                         }
116                 }
117
118                 $course_quotas  =  "max_quota='$quota', max_file_size='$filesize',";
119
120         } else {
121                 $instructor = $_SESSION['member_id'];
122                 if (!$_POST['course'])  {
123                         $course_quotas    =  "max_quota=".AT_COURSESIZE_DEFAULT.", max_file_size=".AT_FILESIZE_DEFAULT.",";
124                         $row = $Backup->getRow($initial_content_info[0], $initial_content_info[1]);
125
126                         if ((count($initial_content_info) == 2) 
127                                 && ($system_courses[$initial_content_info[1]]['member_id'] == $_SESSION['member_id'])) {
128                                 
129                                         if ($MaxCourseSize < $row['contents']['file_manager']) {
130                                                 $msg->addError('RESTORE_TOO_BIG');      
131                                         }
132                         } else {
133                                 $initial_content_info = intval($_POST['initial_content']);
134                         }
135
136                 } else {
137                         unset($initial_content_info);
138                         $course_quotas  =  "max_quota='{$system_courses[$_POST[course]][max_quota]}', max_file_size='{$system_courses[$_POST[course]][max_file_size]}',";
139                 }
140         }
141
142         if ($msg->containsErrors()) {
143                 return FALSE;
144         }
145
146         //display defaults
147         if (!$_POST['course']) {
148                 $menu_defaults = ",home_links='$_config[home_defaults]', main_links='$_config[main_defaults]', side_menu='$_config[side_defaults]'";
149         } else {
150                 $menu_defaults = ',home_links=\''.$system_courses[$_POST['course']]['home_links'].'\', main_links=\''.$system_courses[$_POST['course']]['main_links'].'\', side_menu=\''.$system_courses[$_POST['course']]['side_menu'].'\'';
151         }
152
153         $sql    = "REPLACE INTO ".TABLE_PREFIX."courses SET course_id=$_POST[course], member_id='$_POST[instructor]', access='$_POST[access]', title='$_POST[title]', description='$_POST[description]', cat_id='$_POST[category_parent]', content_packaging='$_POST[content_packaging]', notify=$_POST[notify], hide=$_POST[hide], $course_quotas primary_language='$_POST[pri_lang]', created_date='$_POST[created_date]', rss=$_POST[rss], copyright='$_POST[copyright]', icon='$_POST[icon]', banner='$_POST[banner]', release_date='$release_date' $menu_defaults";
154
155         $result = mysql_query($sql, $db);
156         if (!$result) {
157                 echo mysql_error($db);
158                 echo 'DB Error';
159                 exit;
160         }
161         $_SESSION['is_admin'] = 1;
162         $new_course_id = $_SESSION['course_id'] = mysql_insert_id($db);
163         if ($isadmin) {
164                 write_to_log(AT_ADMIN_LOG_REPLACE, 'courses', mysql_affected_rows($db), $sql);
165         }
166
167         if ($isadmin) {
168                 //get current instructor and unenroll from course if different from POST instructor     
169                 $old_instructor = $system_courses[$_POST['course']]['member_id'];
170                 
171                 if ($old_instructor != $_POST['instructor']) {
172                         //remove old from course enrollment
173                         $sql = "DELETE FROM ".TABLE_PREFIX."course_enrollment WHERE course_id=".$_POST['course']." AND member_id=".$old_instructor;
174                         $result = mysql_query($sql, $db);
175                         write_to_log(AT_ADMIN_LOG_DELETE, 'course_enrollment', mysql_affected_rows($db), $sql);
176                 } 
177         }
178
179         //enroll new instructor
180         $sql = "INSERT INTO ".TABLE_PREFIX."course_enrollment VALUES ($_POST[instructor], $new_course_id, 'y', 0, '"._AT('instructor')."', 0)";
181         $result = mysql_query($sql, $db);
182         if ($isadmin) {
183                 write_to_log(AT_ADMIN_LOG_REPLACE, 'course_enrollment', mysql_affected_rows($db), $sql);
184         }
185
186         // create the course content directory
187         $path = AT_CONTENT_DIR . $new_course_id . '/';
188         @mkdir($path, 0700);
189         @copy(AT_CONTENT_DIR . 'index.html', AT_CONTENT_DIR . $new_course_id . '/index.html');
190
191         // create the course backup directory
192         $path = AT_BACKUP_DIR . $new_course_id . '/';
193         @mkdir($path, 0700);
194         @copy(AT_CONTENT_DIR . 'index.html', AT_BACKUP_DIR . $new_course_id . '/index.html');
195
196         /* insert some default content: */
197
198         if (!$_POST['course_id'] && ($_POST['initial_content'] == '1')) {
199                 $contentManager = new ContentManager($db, $new_course_id);
200                 $contentManager->initContent( );
201
202                 $cid = $contentManager->addContent($new_course_id, 0, 1,_AT('welcome_to_atutor'),
203                                                                                         addslashes(_AT('this_is_content')),
204                                                                                         '', '', 1, date('Y-m-d H:00:00'));
205
206                 $announcement = _AT('default_announcement');
207                 
208                 $sql    = "INSERT INTO ".TABLE_PREFIX."news VALUES (NULL, $new_course_id, $instructor, NOW(), 1, '"._AT('welcome_to_atutor')."', '$announcement')";
209                 $result = mysql_query($sql,$db);
210                 
211                 if ($isadmin) {
212                         write_to_log(AT_ADMIN_LOG_INSERT, 'news', mysql_affected_rows($db), $sql);
213                 }
214
215                 // create forum for Welcome Course
216                 $sql    = "INSERT INTO ".TABLE_PREFIX."forums VALUES (NULL, '"._AT('forum_general_discussion')."', '', 0, 0, NOW())";
217                 $result = mysql_query($sql,$db);
218
219                 if ($isadmin) {
220                         write_to_log(AT_ADMIN_LOG_INSERT, 'forums', mysql_affected_rows($db), $sql);
221                 }
222
223                 $sql = "INSERT INTO ".TABLE_PREFIX."forums_courses VALUES (LAST_INSERT_ID(), $new_course_id)";
224                 $result = mysql_query($sql,$db);
225
226                 if ($isadmin) {
227                         write_to_log(AT_ADMIN_LOG_INSERT, 'forums_courses', mysql_affected_rows($db), $sql);
228                 }
229
230         } else if (!$_POST['course'] && (count($initial_content_info) == 2)){
231
232                 $Backup->setCourseID($new_course_id);
233                 $Backup->restore($material = TRUE, 'append', $initial_content_info[0], $initial_content_info[1]);
234         }
235  
236         /* delete the RSS feeds just in case: */
237         if (file_exists(AT_CONTENT_DIR . 'feeds/' . $new_course_id . '/RSS1.0.xml')) {
238                 @unlink(AT_CONTENT_DIR . 'feeds/' . $_POST['course'] . '/RSS1.0.xml');
239         }
240         if (file_exists(AT_CONTENT_DIR . 'feeds/' . $new_course_id . '/RSS2.0.xml')) {
241                 @unlink(AT_CONTENT_DIR . 'feeds/' . $new_course_id . '/RSS2.0.xml');
242         }
243
244         if ($isadmin) {
245                 $_SESSION['course_id'] = -1;
246         }
247
248         $_SESSION['course_title'] = stripslashes($_POST['title']);
249         return $new_course_id;
250 }
251
252 ?>