2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2006 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto */
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 /************************************************************************/
14 if (!defined('AT_INCLUDE_PATH')) { exit; }
16 function add_update_course($_POST, $isadmin = FALSE) {
17 require(AT_INCLUDE_PATH.'lib/filemanager.inc.php');
21 global $system_courses;
22 global $MaxCourseSize;
25 global $_config_defaults;
27 $Backup =& new Backup($db);
28 $missing_fields = array();
30 if ($_POST['title'] == '') {
31 $missing_fields[] = _AT('title');
33 if (!$_POST['instructor']) {
34 $missing_fields[] = _AT('instructor');
37 if ($missing_fields) {
38 $missing_fields = implode(', ', $missing_fields);
39 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
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']);
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']);
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']);
66 if (!checkdate($month_release, $day_release, $year_release)) { //or date is in the past
67 $msg->addError('RELEASE_DATE_INVALID');
70 if (strlen($month_release) == 1){
71 $month_release = "0$month_release";
73 if (strlen($day_release) == 1){
74 $day_release = "0$day_release";
76 if (strlen($hour_release) == 1){
77 $hour_release = "0$hour_release";
79 if (strlen($min_release) == 1){
80 $min_release = "0$min_release";
82 $release_date = "$year_release-$month_release-$day_release $hour_release:$min_release:00";
84 $release_date = "0000-00-00 00:00:00";
87 $initial_content_info = explode('_', $_POST['initial_content'], 2);
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']);
97 //if they checked 'other', set quota=entered value, if it is empty or negative, set to default (-2)
99 if ($quota_entered=='' || empty($quota_entered) || $quota_entered<0 ) {
100 $quota = AT_COURSESIZE_DEFAULT;
102 $quota = floatval($quota_entered);
103 $quota = megabytes_to_bytes($quota);
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');
113 $filesize = floatval($filesize_entered);
114 $filesize = megabytes_to_bytes($filesize);
118 $course_quotas = "max_quota='$quota', max_file_size='$filesize',";
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]);
126 if ((count($initial_content_info) == 2)
127 && ($system_courses[$initial_content_info[1]]['member_id'] == $_SESSION['member_id'])) {
129 if ($MaxCourseSize < $row['contents']['file_manager']) {
130 $msg->addError('RESTORE_TOO_BIG');
133 $initial_content_info = intval($_POST['initial_content']);
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]}',";
142 if ($msg->containsErrors()) {
147 if (!$_POST['course']) {
148 $menu_defaults = ",home_links='$_config[home_defaults]', main_links='$_config[main_defaults]', side_menu='$_config[side_defaults]'";
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'].'\'';
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";
155 $result = mysql_query($sql, $db);
157 echo mysql_error($db);
161 $_SESSION['is_admin'] = 1;
162 $new_course_id = $_SESSION['course_id'] = mysql_insert_id($db);
164 write_to_log(AT_ADMIN_LOG_REPLACE, 'courses', mysql_affected_rows($db), $sql);
168 //get current instructor and unenroll from course if different from POST instructor
169 $old_instructor = $system_courses[$_POST['course']]['member_id'];
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);
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);
183 write_to_log(AT_ADMIN_LOG_REPLACE, 'course_enrollment', mysql_affected_rows($db), $sql);
186 // create the course content directory
187 $path = AT_CONTENT_DIR . $new_course_id . '/';
189 @copy(AT_CONTENT_DIR . 'index.html', AT_CONTENT_DIR . $new_course_id . '/index.html');
191 // create the course backup directory
192 $path = AT_BACKUP_DIR . $new_course_id . '/';
194 @copy(AT_CONTENT_DIR . 'index.html', AT_BACKUP_DIR . $new_course_id . '/index.html');
196 /* insert some default content: */
198 if (!$_POST['course_id'] && ($_POST['initial_content'] == '1')) {
199 $contentManager = new ContentManager($db, $new_course_id);
200 $contentManager->initContent( );
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'));
206 $announcement = _AT('default_announcement');
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);
212 write_to_log(AT_ADMIN_LOG_INSERT, 'news', mysql_affected_rows($db), $sql);
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);
220 write_to_log(AT_ADMIN_LOG_INSERT, 'forums', mysql_affected_rows($db), $sql);
223 $sql = "INSERT INTO ".TABLE_PREFIX."forums_courses VALUES (LAST_INSERT_ID(), $new_course_id)";
224 $result = mysql_query($sql,$db);
227 write_to_log(AT_ADMIN_LOG_INSERT, 'forums_courses', mysql_affected_rows($db), $sql);
230 } else if (!$_POST['course'] && (count($initial_content_info) == 2)){
232 $Backup->setCourseID($new_course_id);
233 $Backup->restore($material = TRUE, 'append', $initial_content_info[0], $initial_content_info[1]);
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');
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');
245 $_SESSION['course_id'] = -1;
248 $_SESSION['course_title'] = stripslashes($_POST['title']);
249 return $new_course_id;