Commit e9e1969e authored by Francois Marier's avatar Francois Marier
Browse files

bulkimport: import one user at a time redirecting back to the page each time



It turns out that the import code assumes that only one user will be imported
at a time and uses static variables in a few places.

By importing only one user per page load and redirecting back to the import
page, we can work-around this problem.
Signed-off-by: default avatarFrancois Marier <francois@catalyst.net.nz>
parent f34f3cd6
......@@ -40,6 +40,29 @@ define('TITLE', get_string('bulkleap2aimport', 'admin'));
// Turn on autodetecting of line endings, so mac newlines (\r) will work
ini_set('auto_detect_line_endings', 1);
$ADDEDUSERS = $SESSION->get('bulkimport_addedusers');
if (empty($ADDEDUSERS)) {
$ADDEDUSERS = array();
}
$FAILEDUSERS = $SESSION->get('bulkimport_failedusers');
if (empty($FAILEDUSERS)) {
$FAILEDUSERS = array();
}
$LEAP2AFILES = $SESSION->get('bulkimport_leap2afiles');
if (empty($LEAP2AFILES)) {
$LEAP2AFILES = array();
}
$AUTHINSTANCE = $SESSION->get('bulkimport_authinstance');
$EMAILUSERS = $SESSION->get('bulkimport_emailusers');
// Import in progress
if (!empty($LEAP2AFILES)) {
import_next_user();
}
elseif (!empty($ADDEDUSERS) or !empty($FAILEDUSERS)) {
finish_import();
}
$authinstances = auth_get_auth_instances();
if (count($authinstances) > 0) {
......@@ -85,6 +108,16 @@ $form = array(
)
);
/**
* Work-around the redirection limit of Firefox (http://kb.mozillazine.org/Network.http.redirection-limit)
*/
function meta_redirect() {
$url = get_config('wwwroot') . '/admin/users/bulkimport.php';
print '<html><head><meta http-equiv="Refresh" content="0; url=' . $url . '">';
print '</head><body><p>Please follow <a href="'.$url.'">link</a>!</p></body></html>';
exit;
}
/**
* The CSV file is parsed here so validation errors can be returned to the
* user. The data from a successful parsing is stored in the <var>$LEAP2AFILES</var>
......@@ -156,7 +189,6 @@ function bulkimport_validate(Pieform $form, $values) {
return;
}
$LEAP2AFILES = array();
foreach ($csvdata->data as $user) {
$username = $user[0];
$filename = $user[1];
......@@ -170,21 +202,33 @@ function bulkimport_validate(Pieform $form, $values) {
function bulkimport_submit(Pieform $form, $values) {
global $SESSION, $LEAP2AFILES;
require_once(get_config('docroot') . 'import/lib.php');
safe_require('import', 'leap');
log_info('Attempting to import ' . count($LEAP2AFILES) . ' users from LEAP2A files');
$authinstance = (int) $values['authinstance'];
$authobj = get_record('auth_instance', 'id', $authinstance);
$institution = new Institution($authobj->institution);
$SESSION->set('bulkimport_leap2afiles', $LEAP2AFILES);
$SESSION->set('bulkimport_authinstance', (int)$values['authinstance']);
$SESSION->set('bulkimport_emailusers', $values['emailusers']);
$SESSION->set('bulkimport_addedusers', '');
$SESSION->set('bulkimport_failedusers', '');
log_info('Attempting to import ' . count($LEAP2AFILES) . ' users from LEAP2A files');
redirect(get_config('wwwroot') . '/admin/users/bulkimport.php');
}
$addedusers = array();
$failedusers = array();
foreach ($LEAP2AFILES as $username => $filename) {
function import_next_user() {
global $SESSION, $ADDEDUSERS, $FAILEDUSERS, $LEAP2AFILES, $AUTHINSTANCE;
require_once(get_config('docroot') . 'import/lib.php');
safe_require('import', 'leap');
// Pop the last element off of the LEAP2AFILES array
$filename = end($LEAP2AFILES);
$username = key($LEAP2AFILES);
unset($LEAP2AFILES[$username]);
log_debug('adding user ' . $username . ' from ' . $filename);
$authobj = get_record('auth_instance', 'id', $AUTHINSTANCE);
$institution = new Institution($authobj->institution);
$date = time();
$nicedate = date('Y/m/d h:i:s', $date);
$niceuser = preg_replace('/[^a-zA-Z0-9_-]/', '-', $username);
......@@ -203,15 +247,15 @@ function bulkimport_submit(Pieform $form, $values) {
$output = array();
exec($command, $output, $returnvar);
if ($returnvar != 0) {
$failedusers[$username] = get_string('unzipfailed', 'admin', hsc($filename));
$FAILEDUSERS[$username] = get_string('unzipfailed', 'admin', hsc($filename));
log_debug("unzip command failed with return value $returnvar");
continue;
}
$leap2afilename = $uploaddir . 'leap2a.xml';
if (!is_file($leap2afilename)) {
$failedusers[$username] = get_string('noleap2axmlfiledetected', 'admin');
log_debug($failedusers[$username]);
$FAILEDUSERS[$username] = get_string('noleap2axmlfiledetected', 'admin');
log_debug($FAILEDUSERS[$username]);
continue;
}
......@@ -221,7 +265,7 @@ function bulkimport_submit(Pieform $form, $values) {
}
$user = (object)array(
'authinstance' => $authinstance,
'authinstance' => $AUTHINSTANCE,
'username' => $username,
'firstname' => 'Imported',
'lastname' => 'User',
......@@ -264,7 +308,7 @@ function bulkimport_submit(Pieform $form, $values) {
}
catch (ImportException $e) {
log_info("LEAP2A import failed: " . $e->getMessage());
$failedusers[$username] = get_string("leap2aimportfailed");
$FAILEDUSERS[$username] = get_string("leap2aimportfailed");
db_rollback();
continue;
}
......@@ -273,19 +317,30 @@ function bulkimport_submit(Pieform $form, $values) {
// Reload the user details, as various fields are changed by the
// importer when importing (e.g. firstname/lastname)
$addedusers[] = get_record('usr', 'id', $user->id);
}
$ADDEDUSERS[] = get_record('usr', 'id', $user->id);
$SESSION->set('bulkimport_leap2afiles', $LEAP2AFILES);
$SESSION->set('bulkimport_addedusers', $ADDEDUSERS);
$SESSION->set('bulkimport_failedusers', $FAILEDUSERS);
meta_redirect();
}
log_info('Imported ' . count($addedusers) . '/' . count($LEAP2AFILES) . ' users successfully');
function finish_import() {
global $SESSION, $ADDEDUSERS, $FAILEDUSERS, $EMAILUSERS;
if (!empty($addedusers)) {
$SESSION->add_ok_msg(get_string('importednuserssuccessfully', 'admin', count($addedusers), count($LEAP2AFILES)));
$totalusers = count($ADDEDUSERS) + count($FAILEDUSERS);
log_info('Imported ' . count($ADDEDUSERS) . '/' . $totalusers . ' users successfully');
if (!empty($ADDEDUSERS)) {
$SESSION->add_ok_msg(get_string('importednuserssuccessfully', 'admin', count($ADDEDUSERS), $totalusers));
}
// Only send e-mail to users after we're sure they have been inserted
// successfully
if ($values['emailusers'] && $addedusers) {
foreach ($addedusers as $user) {
if ($EMAILUSERS && $ADDEDUSERS) {
foreach ($ADDEDUSERS as $user) {
$noemailusers = array();
try {
email_user($user, null, get_string('accountcreated', 'mahara', get_config('sitename')),
......@@ -309,15 +364,21 @@ function bulkimport_submit(Pieform $form, $values) {
}
}
if (!empty($failedusers)) {
$message = get_string('importfailedfornusers', 'admin', count($failedusers), count($LEAP2AFILES)) . "\n<ul>\n";
foreach ($failedusers as $username => $error) {
if (!empty($FAILEDUSERS)) {
$message = get_string('importfailedfornusers', 'admin', count($FAILEDUSERS), $totalusers) . "\n<ul>\n";
foreach ($FAILEDUSERS as $username => $error) {
$message .= '<li>' . hsc($username) . ': ' . hsc($error) . "</li>\n";
}
$message .= "</ul>\n";
$SESSION->add_err_msg($message, false);
}
$SESSION->set('bulkimport_leap2afiles', '');
$SESSION->set('bulkimport_authinstance', '');
$SESSION->set('bulkimport_emailusers', '');
$SESSION->set('bulkimport_addedusers', '');
$SESSION->set('bulkimport_failedusers', '');
redirect(get_config('wwwroot') . '/admin/users/bulkimport.php');
}
......@@ -327,5 +388,3 @@ $smarty = smarty();
$smarty->assign('form', $form);
$smarty->assign('PAGEHEADING', hsc(TITLE));
$smarty->display('admin/users/bulkimport.tpl');
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment