/** * Delete a course * This function deletes a whole course-area from the platform. When the * given course is a virtual course, the database and directory will not be * deleted. * When the given course is a real course, also all virtual courses refering * to the given course will be deleted. * Considering the fact that we remove all traces of the course in the main * database, it makes sense to remove all tracking as well (if stats databases exist) * so that a new course created with this code would not use the remains of an older * course. * * @param string The code of the course to delete * @todo When deleting a virtual course: unsubscribe users from that virtual * course from the groups in the real course if they are not subscribed in * that real course. * @todo Remove globals */ public static function delete_course($code) { $table_course = Database::get_main_table(TABLE_MAIN_COURSE); $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER); $table_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); $table_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); $table_course_survey = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY); $table_course_survey_question = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION); $table_course_survey_question_option = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION); $table_course_rel_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); $table_stats_hotpots = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES); $table_stats_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); $table_stats_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); $table_stats_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS); $table_stats_lastaccess = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS); $table_stats_course_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); $table_stats_online = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE); $table_stats_default = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT); $table_stats_downloads = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS); $table_stats_links = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LINKS); $table_stats_uploads = Database::get_main_table(TABLE_STATISTIC_TRACK_E_UPLOADS); $codeFiltered = Database::escape_string($code); $sql = "SELECT * FROM {$table_course} WHERE code='" . $codeFiltered . "'"; $res = Database::query($sql); if (Database::num_rows($res) == 0) { return; } $sql = "SELECT * FROM {$table_course}\n WHERE code = '" . $codeFiltered . "'"; $res = Database::query($sql); $course = Database::fetch_array($res); $courseId = $course['id']; $count = 0; if (api_is_multiple_url_enabled()) { $url_id = 1; if (api_get_current_access_url_id() != -1) { $url_id = api_get_current_access_url_id(); } UrlManager::delete_url_rel_course($courseId, $url_id); $count = UrlManager::getCountUrlRelCourse($courseId); } if ($count == 0) { self::create_database_dump($code); $course_tables = AddCourse::get_course_tables(); // Cleaning c_x tables if (!empty($courseId)) { foreach ($course_tables as $table) { $table = Database::get_course_table($table); $sql = "DELETE FROM {$table} WHERE c_id = {$courseId} "; Database::query($sql); } } $course_dir = api_get_path(SYS_COURSE_PATH) . $course['directory']; $archive_dir = api_get_path(SYS_ARCHIVE_PATH) . $course['directory'] . '_' . time(); if (is_dir($course_dir)) { rename($course_dir, $archive_dir); } // Unsubscribe all users from the course $sql = "DELETE FROM {$table_course_user} WHERE c_id='" . $courseId . "'"; Database::query($sql); // Delete the course from the sessions tables $sql = "DELETE FROM {$table_session_course} WHERE c_id='" . $courseId . "'"; Database::query($sql); $sql = "DELETE FROM {$table_session_course_user} WHERE c_id='" . $courseId . "'"; Database::query($sql); // Delete from Course - URL $sql = "DELETE FROM {$table_course_rel_url} WHERE c_id = '" . $courseId . "'"; Database::query($sql); $sql = 'SELECT survey_id FROM ' . $table_course_survey . ' WHERE course_code="' . $codeFiltered . '"'; $result_surveys = Database::query($sql); while ($surveys = Database::fetch_array($result_surveys)) { $survey_id = $surveys[0]; $sql = 'DELETE FROM ' . $table_course_survey_question . ' WHERE survey_id="' . $survey_id . '"'; Database::query($sql); $sql = 'DELETE FROM ' . $table_course_survey_question_option . ' WHERE survey_id="' . $survey_id . '"'; Database::query($sql); $sql = 'DELETE FROM ' . $table_course_survey . ' WHERE survey_id="' . $survey_id . '"'; Database::query($sql); } // Cleaning group categories $groupCategories = GroupManager::get_categories($course['code']); if (!empty($groupCategories)) { foreach ($groupCategories as $category) { GroupManager::delete_category($category['id'], $course['code']); } } // Cleaning groups $groups = GroupManager::get_groups(); if (!empty($groups)) { $groupList = array_column($groups, 'id'); GroupManager::delete_groups($groupList); } // Delete the course from the stats tables $sql = "DELETE FROM {$table_stats_hotpots} WHERE c_id = {$courseId}"; Database::query($sql); $sql = "DELETE FROM {$table_stats_attempt} WHERE c_id = {$courseId}"; Database::query($sql); $sql = "DELETE FROM {$table_stats_exercises} WHERE c_id = {$courseId}"; Database::query($sql); $sql = "DELETE FROM {$table_stats_access} WHERE c_id = {$courseId}"; Database::query($sql); $sql = "DELETE FROM {$table_stats_lastaccess} WHERE c_id = {$courseId}"; Database::query($sql); $sql = "DELETE FROM {$table_stats_course_access} WHERE c_id = {$courseId}"; Database::query($sql); $sql = "DELETE FROM {$table_stats_online} WHERE c_id = {$courseId}"; Database::query($sql); // Do not delete rows from track_e_default as these include course // creation and other important things that do not take much space // but give information on the course history //$sql = "DELETE FROM $table_stats_default WHERE c_id = $courseId"; //Database::query($sql); $sql = "DELETE FROM {$table_stats_downloads} WHERE c_id = {$courseId}"; Database::query($sql); $sql = "DELETE FROM {$table_stats_links} WHERE c_id = {$courseId}"; Database::query($sql); $sql = "DELETE FROM {$table_stats_uploads} WHERE c_id = {$courseId}"; Database::query($sql); // Delete the course from the database $sql = "DELETE FROM {$table_course} WHERE code = '" . $codeFiltered . "'"; Database::query($sql); // delete extra course fields $extraFieldValues = new ExtraFieldValue('course'); $extraFieldValues->deleteValuesByItem($courseId); // Add event to system log $user_id = api_get_user_id(); Event::addEvent(LOG_COURSE_DELETE, LOG_COURSE_CODE, $code, api_get_utc_datetime(), $user_id, $courseId); } }
/** * * @param array $groupData * @param bool $deleteNotInArray * @return array */ public static function importCategoriesAndGroupsFromArray($groupData, $deleteNotInArray = false) { $result = array(); $elementsFound = array('categories' => array(), 'groups' => array()); $groupCategories = GroupManager::get_categories(); if (empty($groupCategories)) { $result['error'][] = get_lang('CreateACategory'); return $result; } foreach ($groupData as $data) { $isCategory = empty($data['group']) ? true : false; if ($isCategory) { $categoryInfo = self::getCategoryByTitle($data['category']); $categoryId = $categoryInfo['id']; if (!empty($categoryInfo)) { // Update self::update_category($categoryId, $data['category'], $data['description'], $data['doc_state'], $data['work_state'], $data['calendar_state'], $data['announcements_state'], $data['forum_state'], $data['wiki_state'], $data['chat_state'], $data['self_reg_allowed'], $data['self_unreg_allowed'], $data['max_student'], $data['groups_per_user']); $data['category_id'] = $categoryId; $result['updated']['category'][] = $data; } else { // Add $categoryId = self::create_category($data['category'], $data['description'], $data['doc_state'], $data['work_state'], $data['calendar_state'], $data['announcements_state'], $data['forum_state'], $data['wiki_state'], $data['chat_state'], $data['self_reg_allowed'], $data['self_unreg_allowed'], $data['max_student'], $data['groups_per_user']); if ($categoryId) { $data['category_id'] = $categoryId; $result['added']['category'][] = $data; } } $elementsFound['categories'][] = $categoryId; } else { $groupInfo = self::getGroupByName($data['group']); $categoryInfo = self::getCategoryByTitle($data['category']); $categoryId = null; if (!empty($categoryInfo)) { $categoryId = $categoryInfo['id']; } else { if (!empty($groupCategories) && isset($groupCategories[0])) { $defaultGroupCategory = $groupCategories[0]; $categoryId = $defaultGroupCategory['id']; } } if (empty($groupInfo)) { // Add $groupId = self::create_group($data['group'], $categoryId, null, $data['max_students']); if ($groupId) { self::set_group_properties($groupId, $data['group'], $data['description'], $data['max_students'], $data['doc_state'], $data['work_state'], $data['calendar_state'], $data['announcements_state'], $data['forum_state'], $data['wiki_state'], $data['chat_state'], $data['self_reg_allowed'], $data['self_unreg_allowed'], $categoryId); $data['group_id'] = $groupId; $result['added']['group'][] = $data; } } else { // Update $groupId = $groupInfo['id']; self::set_group_properties($groupId, $data['group'], $data['description'], $data['max_students'], $data['doc_state'], $data['work_state'], $data['calendar_state'], $data['announcements_state'], $data['forum_state'], $data['wiki_state'], $data['chat_state'], $data['self_reg_allowed'], $data['self_unreg_allowed'], $categoryId); $data['group_id'] = $groupId; $result['updated']['group'][] = $data; } $elementsFound['groups'][] = $groupId; } } if ($deleteNotInArray) { // Check categories $categories = GroupManager::get_categories(); foreach ($categories as $category) { if (!in_array($category['id'], $elementsFound['categories'])) { GroupManager::delete_category($category['id']); $category['category'] = $category['title']; $result['deleted']['category'][] = $category; } } $groups = GroupManager::get_groups(); foreach ($groups as $group) { if (!in_array($group['id'], $elementsFound['groups'])) { GroupManager::delete_groups(array($group['id'])); $group['group'] = $group['name']; $result['deleted']['group'][] = $group; } } } return $result; }
exit; } break; } } // Get-actions if (isset($_GET['action'])) { switch ($_GET['action']) { case 'swap_cat_order': GroupManager::swap_category_order($my_get_id1, $my_get_id2); Display::addFlash(Display::return_message(get_lang('CategoryOrderChanged'))); header("Location: {$currentUrl}"); exit; break; case 'delete_one': GroupManager::delete_groups($my_get_id); Display::addFlash(Display::return_message(get_lang('GroupDel'))); header("Location: {$currentUrl}"); exit; break; case 'fill_one': GroupManager::fill_groups($my_get_id); Display::addFlash(Display::return_message(get_lang('GroupFilledGroups'))); header("Location: {$currentUrl}"); exit; break; case 'delete_category': GroupManager::delete_category($my_get_id); Display::addFlash(Display::return_message(get_lang('CategoryDeleted'))); header("Location: {$currentUrl}"); exit;