Adding user import using a CSV file BT#3211

skala
Julio Montoya 13 years ago
parent 6880bf31a4
commit 34d61eff01
  1. 21
      main/admin/user_import.php
  2. BIN
      main/img/icons/32/import_csv.png
  3. 13
      main/user/user.php
  4. 87
      main/user/user_import.php

@ -15,6 +15,9 @@ $language_file = array ('admin', 'registration');
$cidReset = true;
require '../inc/global.inc.php';
require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
require_once api_get_path(LIBRARY_PATH).'classmanager.lib.php';
require_once api_get_path(LIBRARY_PATH).'import.lib.php';
// Set this option to true to enforce strict purification for usenames.
$purification_option_for_usernames = false;
@ -266,9 +269,7 @@ function parse_xml_data($file) {
$this_section = SECTION_PLATFORM_ADMIN;
api_protect_admin_script(true);
require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
require_once api_get_path(LIBRARY_PATH).'classmanager.lib.php';
require_once api_get_path(LIBRARY_PATH).'import.lib.php';
$defined_auth_sources[] = PLATFORM_AUTH_SOURCE;
if (is_array($extAuthSource)) {
@ -384,10 +385,16 @@ $form = new FormValidator('user_import','post','user_import.php');
$form->addElement('header', '', $tool_name);
$form->addElement('hidden', 'formSent');
$form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
$form->addElement('radio', 'file_type', get_lang('FileType'), 'CSV (<a href="example.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)', 'csv');
$form->addElement('radio', 'file_type', null, 'XML (<a href="example.xml" target="_blank">'.get_lang('ExampleXMLFile').'</a>)', 'xml');
$form->addElement('radio', 'sendMail', get_lang('SendMailToUsers'), get_lang('Yes'), 1);
$form->addElement('radio', 'sendMail', null, get_lang('No'), 0);
$group = array();
$group[] = $form->createElement('radio', 'file_type', '', 'CSV (<a href="example.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)', 'csv');
$group[] = $form->createElement('radio', 'file_type', null, 'XML (<a href="example.xml" target="_blank">'.get_lang('ExampleXMLFile').'</a>)', 'xml');
$form->addGroup($group, '', get_lang('FileType'), '<br/>');
$group = array();
$group[] = $form->createElement('radio', 'sendMail', '', get_lang('Yes'), 1);
$group[] = $form->createElement('radio', 'sendMail', null, get_lang('No'), 0);
$form->addGroup($group, '', get_lang('SendMailToUsers'), '<br/>');
$form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"');
$defaults['formSent'] = 1;
$defaults['sendMail'] = 0;

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

@ -81,6 +81,8 @@ if (api_is_allowed_to_edit(null, true)) {
if (api_is_allowed_to_edit(null, true)) {
if ( isset ($_GET['action'])) {
switch ($_GET['action']) {
case 'import' :
break;
case 'export' :
$table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
$table_users = Database::get_main_table(TABLE_MAIN_USER);
@ -214,7 +216,8 @@ if (api_is_allowed_to_edit(null, true)) {
$description .= '<tr><td>'.get_lang('Course').': </td><td class="highlight">'.$course_info['name'].'</td>';
$description .= '<tr><td>'.get_lang('Teachers').': </td><td class="highlight">'.CourseManager::get_teacher_list_from_course_code_to_string($course_info['code']).'</td>';
$description .= '<tr><td>'.get_lang('Date').': </td><td class="highlight">'.api_convert_and_format_date(time(), DATE_TIME_FORMAT_LONG).'</td>';
$description .= '</table>';
$description .= '</table>';
Export::export_table_pdf($a_users, get_lang('UserList'), $header, $description);
exit;
}
@ -373,7 +376,7 @@ $is_allowed_to_track = ($is_courseAdmin || $is_courseTutor);
Display::display_introduction_section(TOOL_USER, 'left');
$actions = '';
if ( api_is_allowed_to_edit(null, true)) {
echo "<div class=\"actions\">";
echo '<div class="actions">';
// the action links
if (api_get_setting('allow_user_course_subscription_by_course_admin') == 'true' or api_is_platform_admin()) {
@ -381,7 +384,11 @@ if ( api_is_allowed_to_edit(null, true)) {
$actions .= "<a href=\"subscribe_user.php?".api_get_cidreq()."&type=teacher\">".Display::return_icon('teacher_subscribe_course.png', get_lang("SubscribeUserToCourseAsTeacher"),'','32')."</a> ";
}
$actions .= '<a href="user.php?'.api_get_cidreq().'&action=export&amp;type=csv">'.Display::return_icon('export_csv.png', get_lang('ExportAsCSV'),'','32').'</a> ';
$actions .= '<a href="user.php?'.api_get_cidreq().'&action=export&amp;type=xls">'.Display::return_icon('export_excel.png', get_lang('ExportAsXLS'),'','32').'</a> ';
$actions .= '<a href="user.php?'.api_get_cidreq().'&action=export&amp;type=xls">'.Display::return_icon('export_excel.png', get_lang('ExportAsXLS'),'','32').'</a> ';
if (!api_get_session_id()) {
$actions .= '<a href="user_import.php?'.api_get_cidreq().'&action=import">'.Display::return_icon('import_csv.png', get_lang('ImportCSV'),'','32').'</a> ';
}
$actions .= '<a href="user.php?'.api_get_cidreq().'&action=export&type=pdf">'.Display::return_icon('pdf.png', get_lang('ExportToPDF'),'','32').'</a> ';

@ -0,0 +1,87 @@
<?php
/* For licensing terms, see /license.txt */
$language_file = array('registration', 'admin', 'userInfo');
require_once '../inc/global.inc.php';
require_once api_get_path(LIBRARY_PATH).'import.lib.php';
$this_section = SECTION_COURSES;
// notice for unauthorized people.
api_protect_course_script(true);
$tool_name = get_lang('ImportUsersToACourse');
$interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("Users"));
$interbreadcrumb[] = array ("url" => "#", "name" => get_lang("ImportUsers"));
$form = new FormValidator('user_import','post','user_import.php');
$form->addElement('header', '', $tool_name);
$form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
$form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"');
$course_code = api_get_course_id();
if (api_get_session_id()) {
api_not_allowed();
}
if (empty($course_code)) {
api_not_allowed();
}
$message = '';
$user_to_show = array();
$type = '';
if ($form->validate()) {
if (isset($_FILES['import_file']['size']) && $_FILES['import_file']['size'] !== 0) {
$users = Import::csv_to_array($_FILES['import_file']['tmp_name']);
$invalid_users = array();
$clean_users = array();
$users_in_file = array();
if (!empty($users)) {
foreach($users as $user) {
$user_info = api_get_user_info($user['id']);
$clean_users[$user['id']] = $user_info;
$users_in_file[$user['id']] = $user;
}
foreach($users as $user) {
if (!UserManager::is_user_id_valid($user['id'])) {
$invalid_users[] = $user['id'];
}
}
if (empty($invalid_users)) {
$type = 'confirmation';
$message = get_lang('ListOfUsersSubscribedToCourse');
foreach($users as $user) {
$result = CourseManager :: subscribe_user($user['id'], $course_code, STUDENT);
//just to make sure
if (CourseManager :: is_user_subscribed_in_course($user['id'], $course_code)) {
$user_to_show []= $clean_users[$user['id']]['complete_name'];
}
}
} else {
$message = get_lang('CheckUsersWithId');
$type = 'warning';
foreach($invalid_users as $invalid_user) {
$user_to_show []= $invalid_user;
}
}
}
}
}
Display::display_header();
if (!empty($message)) {
if ($type == 'confirmation') {
Display::display_confirmation_message($message.': '.implode(', ', $user_to_show));
} else {
Display::display_warning_message($message.': '.implode(', ', $user_to_show));
}
}
$form->display();
Display::display_footer();
Loading…
Cancel
Save