|
|
|
@ -1,24 +1,5 @@ |
|
|
|
|
<?php |
|
|
|
|
// $Id: course_user_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 |
|
|
|
|
============================================================================== |
|
|
|
|
*/ |
|
|
|
|
<?php // $Id: course_user_import.php 8216 2006-03-15 16:33:13Z turboke $
|
|
|
|
|
/* For licensing terms, see /dokeos_license.txt */ |
|
|
|
|
/** |
|
|
|
|
============================================================================== |
|
|
|
|
* This tool allows platform admins to update course-user relations by uploading |
|
|
|
@ -92,17 +73,14 @@ function validate_data($users_courses) |
|
|
|
|
/** |
|
|
|
|
* Save the imported data |
|
|
|
|
*/ |
|
|
|
|
function save_data($users_courses) |
|
|
|
|
{ |
|
|
|
|
function save_data($users_courses) { |
|
|
|
|
$user_table= Database::get_main_table(TABLE_MAIN_USER); |
|
|
|
|
$course_user_table= Database::get_main_table(TABLE_MAIN_COURSE_USER); |
|
|
|
|
$csv_data = array(); |
|
|
|
|
foreach ($users_courses as $index => $user_course) |
|
|
|
|
{ |
|
|
|
|
foreach ($users_courses as $index => $user_course) { |
|
|
|
|
$csv_data[$user_course['UserName']][$user_course['CourseCode']] = $user_course['Status']; |
|
|
|
|
} |
|
|
|
|
foreach($csv_data as $username => $csv_subscriptions) |
|
|
|
|
{ |
|
|
|
|
foreach($csv_data as $username => $csv_subscriptions) { |
|
|
|
|
$user_id = 0; |
|
|
|
|
$sql = "SELECT * FROM $user_table u WHERE u.username = '".mysql_real_escape_string($username)."'"; |
|
|
|
|
$res = api_sql_query($sql,__FILE__,__LINE__); |
|
|
|
@ -111,8 +89,7 @@ function save_data($users_courses) |
|
|
|
|
$sql = "SELECT * FROM $course_user_table cu WHERE cu.user_id = $user_id"; |
|
|
|
|
$res = api_sql_query($sql,__FILE__,__LINE__); |
|
|
|
|
$db_subscriptions = array(); |
|
|
|
|
while($obj = mysql_fetch_object($res)) |
|
|
|
|
{ |
|
|
|
|
while($obj = Database::fetch_object($res)) { |
|
|
|
|
$db_subscriptions[$obj->course_code] = $obj->status; |
|
|
|
|
} |
|
|
|
|
//echo '<b>User '.$username.'</b><br />'; |
|
|
|
@ -128,20 +105,51 @@ function save_data($users_courses) |
|
|
|
|
// echo "TO UNSUBSCRIBE\n"; |
|
|
|
|
// print_r($to_unsubscribe); |
|
|
|
|
// echo '</pre>'; |
|
|
|
|
if($_POST['subscribe']) |
|
|
|
|
{ |
|
|
|
|
foreach($to_subscribe as $index => $course_code) |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
global $inserted_in_course; |
|
|
|
|
if (!isset($inserted_in_course)) { $inserted_in_course = array(); } |
|
|
|
|
if($_POST['subscribe']) { |
|
|
|
|
foreach($to_subscribe as $index => $course_code) { |
|
|
|
|
if(CourseManager :: course_exists($course_code)) { |
|
|
|
|
CourseManager::add_user_to_course($user_id,$course_code,$csv_subscriptions[$course_code]); |
|
|
|
|
echo get_lang('Subscription').' : '.$course_code.'<br />'; |
|
|
|
|
$c_info = CourseManager::get_course_information($course_code); |
|
|
|
|
$inserted_in_course[$course_code] = $c_info['title']; |
|
|
|
|
} |
|
|
|
|
if (CourseManager :: course_exists($course_code,true)) { |
|
|
|
|
// also subscribe to virtual courses through check on visual code |
|
|
|
|
$list = CourseManager :: get_courses_info_from_visual_code($course_code); |
|
|
|
|
foreach ($list as $vcourse) { |
|
|
|
|
if ($vcourse['code'] == $course_code) { |
|
|
|
|
//ignore, this has already been inserted |
|
|
|
|
} else { |
|
|
|
|
CourseManager::add_user_to_course($user_id,$vcourse['code'],$csv_subscriptions[$course_code]); |
|
|
|
|
$inserted_in_course[$vcourse['code']] = $vcourse['title']; |
|
|
|
|
} |
|
|
|
|
if($_POST['unsubscribe']) |
|
|
|
|
{ |
|
|
|
|
foreach($to_unsubscribe as $index => $course_code) |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//echo get_lang('Subscription').' : '.$course_code.'<br />'; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if($_POST['unsubscribe']) { |
|
|
|
|
foreach($to_unsubscribe as $index => $course_code) { |
|
|
|
|
if(CourseManager :: course_exists($course_code)) { |
|
|
|
|
CourseManager::unsubscribe_user($user_id,$course_code); |
|
|
|
|
echo get_lang('Unsubscription').' : '.$course_code.'<br />'; |
|
|
|
|
$c_info = CourseManager::get_course_information($course_code); |
|
|
|
|
$inserted_in_course[$course_code] = $c_info['title']; |
|
|
|
|
} |
|
|
|
|
if (CourseManager :: course_exists($course_code,true)) { |
|
|
|
|
// also subscribe to virtual courses through check on visual code |
|
|
|
|
$list = CourseManager :: get_courses_info_from_visual_code($course_code); |
|
|
|
|
foreach ($list as $vcourse) { |
|
|
|
|
if ($vcourse['code'] == $course_code) { |
|
|
|
|
//ignore, this has already been inserted |
|
|
|
|
} else { |
|
|
|
|
CourseManager::unsubscribe_user($user_id,$vcourse['code']); |
|
|
|
|
$inserted_in_course[$vcourse['code']] = $vcourse['title']; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//echo get_lang('Unsubscription').' : '.$course_code.'<br />'; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -151,8 +159,7 @@ function save_data($users_courses) |
|
|
|
|
* @param string $file Path to the CSV-file |
|
|
|
|
* @return array All course-information read from the file |
|
|
|
|
*/ |
|
|
|
|
function parse_csv_data($file) |
|
|
|
|
{ |
|
|
|
|
function parse_csv_data($file) { |
|
|
|
|
$courses = Import :: csv_to_array($file); |
|
|
|
|
return $courses; |
|
|
|
|
} |
|
|
|
@ -173,7 +180,7 @@ $errorMsg = ''; |
|
|
|
|
|
|
|
|
|
$tool_name = get_lang('AddUsersToACourse').' CSV'; |
|
|
|
|
|
|
|
|
|
$interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin')); |
|
|
|
|
$interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin')); |
|
|
|
|
|
|
|
|
|
set_time_limit(0); |
|
|
|
|
$form = new FormValidator('course_user_import'); |
|
|
|
@ -181,24 +188,37 @@ $form->addElement('file','import_file', get_lang('ImportFileLocation')); |
|
|
|
|
$form->addElement('checkbox','subscribe',get_lang('Action'),get_lang('SubscribeUserIfNotAllreadySubscribed')); |
|
|
|
|
$form->addElement('checkbox','unsubscribe','',get_lang('UnsubscribeUserIfSubscriptionIsNotInFile')); |
|
|
|
|
$form->addElement('submit','submit',get_lang('Ok')); |
|
|
|
|
if ($form->validate()) |
|
|
|
|
{ |
|
|
|
|
if ($form->validate()) { |
|
|
|
|
$users_courses = parse_csv_data($_FILES['import_file']['tmp_name']); |
|
|
|
|
$errors = validate_data($users_courses); |
|
|
|
|
if (count($errors) == 0) |
|
|
|
|
{ |
|
|
|
|
if (count($errors) == 0) { |
|
|
|
|
$inserted_in_course = array(); |
|
|
|
|
save_data($users_courses); |
|
|
|
|
header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('FileImported'))); |
|
|
|
|
// Build the alert message in case there were visual codes subscribed to |
|
|
|
|
if ($_POST['subscribe']) { |
|
|
|
|
$warn = get_lang('UsersSubscribedToBecauseVisualCode').': '; |
|
|
|
|
} else { |
|
|
|
|
$warn = get_lang('UsersUnsubscribedFromBecauseVisualCode').': '; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (count($inserted_in_course) > 1) { |
|
|
|
|
// The users have been inserted in more than one course |
|
|
|
|
foreach ($inserted_in_course as $code => $info) { |
|
|
|
|
$warn .= ' '.$info.' ('.$code.'),'; |
|
|
|
|
} |
|
|
|
|
$warn = substr($warn,0,-1); |
|
|
|
|
} |
|
|
|
|
Security::clear_token(); |
|
|
|
|
$tok = Security::get_token(); |
|
|
|
|
header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('FileImported')).'&warn='.urlencode($warn).'&sec_token='.$tok); |
|
|
|
|
exit (); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Display :: display_header($tool_name); |
|
|
|
|
api_display_tool_title($tool_name); |
|
|
|
|
if (count($errors) != 0) |
|
|
|
|
{ |
|
|
|
|
if (count($errors) != 0) { |
|
|
|
|
$error_message = '<ul>'; |
|
|
|
|
foreach ($errors as $index => $error_course) |
|
|
|
|
{ |
|
|
|
|
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>'; |
|
|
|
|