123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * This tool allows platform admins to create courses by uploading a CSV file
- * Copyright (c) 2010 Chamilo Association
- * Copyright (c) 2008 Dokeos SPRL
- * Copyright (c) 2005 Bart Mollet <bart.mollet@hogent.be>
- * @todo Add some language variables to Chamilo Translation Application
- * @package chamilo.admin
- */
- /**
- * Validates imported data.
- */
- function validate_data($courses) {
- global $purification_option_for_usernames;
- // Ensure the prefix + database name do not get over 40 characters.
- $maxlength = 40;
- $errors = array ();
- $coursecodes = array ();
- foreach ($courses as $index => $course) {
- $course['line'] = $index +1;
- // 1. Check whether mandatory fields are set.
- $mandatory_fields = array ('Code', 'Title', 'CourseCategory', 'Teacher');
- foreach ($mandatory_fields as $key => $field) {
- if (!isset($course[$field]) || strlen($course[$field]) == 0) {
- $course['error'] = get_lang($field.'Mandatory');
- $errors[] = $course;
- }
- }
- // 2. Check current course code.
- if (isset ($course['Code']) && strlen($course['Code']) != 0) {
- // 2.1 Check whether code has been allready used by this CVS-file.
- if (isset($coursecodes[$course['Code']])) {
- $course['error'] = get_lang('CodeTwiceInFile');
- $errors[] = $course;
- }
- // 2.2 Check course code length.
- elseif (api_strlen($course['Code']) > $maxlength) {
- $course['error'] = get_lang('Max');
- $errors[] = $course;
- }
- // 2.3 Check whether course code has been occupied.
- else {
- $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
- $sql = "SELECT * FROM $course_table WHERE code = '".Database::escape_string($course['Code'])."'";
- $res = Database::query($sql);
- if (Database::num_rows($res) > 0) {
- $course['error'] = get_lang('CodeExists');
- $errors[] = $course;
- }
- }
- $coursecodes[$course['Code']] = 1;
- }
- // 3. Check whether teacher exists.
- $teacherList = getTeacherListInArray($course['Teacher']);
- if (!empty($teacherList)) {
- foreach ($teacherList as $teacher) {
- $teacherInfo = api_get_user_info_from_username($teacher);
- if (empty($teacherInfo)) {
- $course['error'] = get_lang('UnknownTeacher').' ('.$teacher.')';
- $errors[] = $course;
- } else {
- if ($teacherInfo['status'] != COURSEMANAGER) {
- $course['error'] = get_lang('UserIsNotATeacher').' ('.$teacher.')';
- $errors[] = $course;
- }
- }
- }
- }
- // 4. Check whether course category exists.
- if (isset($course['CourseCategory']) && strlen($course['CourseCategory']) != 0) {
- $category_table = Database :: get_main_table(TABLE_MAIN_CATEGORY);
- $sql = "SELECT * FROM $category_table WHERE code = '".Database::escape_string($course['CourseCategory'])."'";
- $res = Database::query($sql);
- if (Database::num_rows($res) == 0) {
- //@todo this is so bad even all lang variables are wrong ...
- $course['error'] = get_lang('UnkownCategoryCourseCode').' ('.$course['CourseCategory'].')';
- $errors[] = $course;
- }
- }
- }
- return $errors;
- }
- function getTeacherListInArray($teachers)
- {
- if (!empty($teachers)) {
- return explode('|', $teachers);
- }
- return array();
- }
- /**
- * Saves imported data.
- * @param array List of courses
- */
- function save_data($courses) {
- global $purification_option_for_usernames;
- $user_table = Database::get_main_table(TABLE_MAIN_USER);
- $msg = '';
- foreach ($courses as $index => $course) {
- $course_language = api_get_valid_language($course['Language']);
- $username = '';
- $teachers = getTeacherListInArray($course['Teacher']);
- $teacherList = array();
- $creatorId = api_get_user_id();
- if (!empty($teachers)) {
- foreach ($teachers as $teacher) {
- $teacherInfo = api_get_user_info_from_username($teacher);
- if (!empty($teacherInfo)) {
- $teacherList[] = $teacherInfo;
- }
- }
- }
- $params = array();
- $params['title'] = $course['Title'];
- $params['wanted_code'] = $course['Code'];
- $params['tutor_name'] = null;
- $params['course_category'] = $course['CourseCategory'];
- $params['course_language'] = $course_language;
- $params['user_id'] = $creatorId;
- $course_info = CourseManager::create_course($params);
- if (!empty($course_info)) {
- if (!empty($teacherList)) {
- foreach ($teacherList as $teacher) {
- CourseManager::add_user_to_course($teacher['user_id'], $course_info['code'], COURSEMANAGER);
- }
- }
- $msg .= '<a href="'.api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/">
- '.$course_info['title'].'</a> '.get_lang('Created').'<br />';
- }
- }
- if (!empty($msg)) {
- Display::display_normal_message($msg, false);
- }
- }
- /**
- * Read the CSV-file
- * @param string $file Path to the CSV-file
- * @return array All course-information read from the file
- */
- function parse_csv_data($file) {
- $courses = Import :: csv_to_array($file);
- return $courses;
- }
- $language_file = array('admin', 'registration','create_course', 'document');
- $cidReset = true;
- $this_section = SECTION_PLATFORM_ADMIN;
- api_protect_admin_script();
- $defined_auth_sources[] = PLATFORM_AUTH_SOURCE;
- if (isset($extAuthSource) && is_array($extAuthSource)) {
- $defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource));
- }
- $tool_name = get_lang('ImportCourses').' CSV';
- $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
- set_time_limit(0);
- Display :: display_header($tool_name);
- if (isset($_POST['formSent']) && $_POST['formSent']) {
- if (empty($_FILES['import_file']['tmp_name'])) {
- $error_message = get_lang('UplUploadFailed');
- Display :: display_error_message($error_message, false);
- } else {
- $allowed_file_mimetype = array('csv');
- $ext_import_file = substr($_FILES['import_file']['name'], (strrpos($_FILES['import_file']['name'], '.') + 1));
- if (!in_array($ext_import_file, $allowed_file_mimetype)) {
- Display :: display_error_message(get_lang('YouMustImportAFileAccordingToSelectedOption'));
- } else {
- $courses = parse_csv_data($_FILES['import_file']['tmp_name']);
- $errors = validate_data($courses);
- if (count($errors) == 0) {
- save_data($courses);
- }
- }
- }
- }
- if (isset($errors) && count($errors) != 0) {
- $error_message = '<ul>';
- foreach ($errors as $index => $error_course) {
- $error_message .= '<li>'.get_lang('Line').' '.$error_course['line'].': <strong>'.$error_course['error'].'</strong>: ';
- $error_message .= get_lang('Course').': '.$error_course['Title'].' ('.$error_course['Code'].')';
- $error_message .= '</li>';
- }
- $error_message .= '</ul>';
- Display :: display_error_message($error_message, false);
- }
- ?>
- <form method="post" action="<?php echo api_get_self(); ?>" enctype="multipart/form-data" style="margin: 0px;">
- <legend><?php echo $tool_name; ?></legend>
- <div class="control-group">
- <label><?php echo get_lang('ImportCSVFileLocation'); ?></label>
- <div class="control">
- <input type="file" name="import_file"/>
- </div>
- </div>
- <div class="control-group ">
- <div class="control">
- <button type="submit" class="save" value="<?php echo get_lang('Import'); ?>"><?php echo get_lang('Import'); ?></button>
- </div>
- </div>
- <input type="hidden" name="formSent" value="1"/>
- </form>
- <div style="clear: both;"></div>
- <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
- <blockquote>
- <pre>
- <strong>Code</strong>;<strong>Title</strong>;<strong>CourseCategory</strong>;Teacher;Language
- BIO0015;Biology;BIO;teacher1;english
- BIO0016;Maths;MATH;teacher2|teacher3;english
- BIO0017;Language;LANG;;english
- </pre>
- </blockquote>
- <?php
- Display :: display_footer();
|