Source for file course_import.php
Documentation is available at course_import.php
// $Id: course_import.php 8216 2006-03-15 16:33:13Z turboke $
==============================================================================
Dokeos - elearning and course management software
Copyright (c) 2005 Bart Mollet <bart.mollet@hogent.be>
For a full list of contributors, see "credits.txt".
The full license can be read in "license.txt".
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
See the GNU General Public License for more details.
Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
==============================================================================
==============================================================================
* This tool allows platform admins to create courses by uploading a CSV file
* @todo Add some langvars to DLTT
==============================================================================
* validate the imported data
foreach ($courses as $index => $course)
$course['line'] = $index + 1;
//1. check if 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');
//2. check if code isn't in use
if (isset ($course['Code']) && strlen($course['Code']) != 0)
//2.1 check if code allready used in this CVS-file
if (isset ($coursecodes[$course['Code']]))
$course['error'] = get_lang('CodeTwiceInFile');
elseif(strlen($course['Code']) > 20)
//2.3 check if code allready used in DB
$course['error'] = get_lang('CodeExists');
$coursecodes[$course['Code']] = 1;
//3. check if teacher exists
if (isset ($course['Teacher']) && strlen($course['Teacher']) != 0)
$course['error'] = get_lang('UnknownTeacher');
//4. check if category exists
if (isset ($course['CourseCategory']) && strlen($course['CourseCategory']) != 0)
$course['error'] = get_lang('UnkownCategory');
global $_configuration, $firstExpirationDelay;
foreach($courses as $index => $course)
$sql = "SELECT user_id, CONCAT(lastname,' ',firstname) AS name FROM $user_table WHERE username = '". mysql_real_escape_string($course['Teacher']). "'";
$visual_code = $keys["currentCourseCode"];
$code = $keys["currentCourseId"];
$db_name = $keys["currentCourseDbName"];
$directory = $keys["currentCourseRepository"];
$expiration_date = time() + $firstExpirationDelay;
register_course($code, $visual_code, $directory, $db_name, $teacher->name, $course['CourseCategory'], $course['Title'], api_get_setting('platformLanguage'), $teacher->user_id, $expiration_date);
echo $code. ' CREATED<br />';
* @param string $file Path to the CSV-file
* @return array All course-information read from the file
$language_file = array ('admin', 'registration','create_course', 'document');
include ('../inc/global.inc.php');
require_once (api_get_path(LIBRARY_PATH). 'fileManage.lib.php');
require_once (api_get_path(LIBRARY_PATH). 'import.lib.php');
require_once (api_get_path(LIBRARY_PATH). 'usermanager.lib.php');
require_once (api_get_path(CONFIGURATION_PATH). 'add_course.conf.php');
require_once (api_get_path(LIBRARY_PATH). 'add_course.lib.inc.php');
$tool_name = get_lang('AddCourse'). ' CSV';
$interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
if(empty($_FILES['import_file']['tmp_name']))
$error_message = get_lang('UplUploadFailed');
$file_type = $_POST['file_type'];
//$users = complete_missing_data($courses);
//header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('FileImported')));
foreach ($errors as $index => $error_course)
$error_message .= '<li>'. get_lang('Line'). ' '. $error_course['line']. ': <b>'. $error_course['error']. '</b>: ';
$error_message .= $error_course['Code']. ' '. $error_course['Title'];
$error_message .= '</li>';
$error_message .= '</ul>';
<form method="post" action=" <?php echo api_get_self(); ?>" enctype="multipart/form-data" style="margin:0px;">
<input type="file" name="import_file"/>
<input type="hidden" name="formSent" value="1"/>
<input type="submit" value=" <?php echo get_lang('Ok'); ?>"/>
<p> <?php echo get_lang('CSVMustLookLike'). ' ('. get_lang('MandatoryFields'). ')'; ?> :</p>
<b>Code</b>;<b>Title</b>;<b>CourseCategory</b>;<b>Teacher</b>
BIO0015;Biology;BIO;username
==============================================================================
==============================================================================
|