Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix]:- Error Handling while importing the CSV #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions moodle_users/import_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function display_page($usertype) {
}

function import_users($mform, $usertype) {
global $OUTPUT;
$importid = csv_import_reader::get_new_iid('academi');
$cir = new csv_import_reader($importid, 'academi');
$content = $mform->get_file_content('userfile');
Expand All @@ -64,17 +65,25 @@ function import_users($mform, $usertype) {
if (!$iscolumnvalidated) {
redirect(new moodle_url('/theme/academi/moodle_users/manage_users.php'));
}
$errors = [];

while ($row = $cir->next()) {
create_new_user($columns, $row, $usertype);
$error = create_new_user($columns, $row, $usertype);
if ($error) {
$errors[] = $error;
}
}
$csvloaderror = $cir->get_error();
unset($content);

if (!is_null($csvloaderror)) {
redirect(new moodle_url('/theme/academi/moodle_users/manage_users.php'));
}
redirect(new moodle_url('/theme/academi/moodle_users/manage_users.php'));
if (!empty($errors)) {
echo $OUTPUT->notification(implode('<br>', $errors), 'notifyproblem');
} else {
redirect(new moodle_url('/theme/academi/moodle_users/manage_users.php'));
}
}

function validate_columns($columns) {
Expand Down Expand Up @@ -111,6 +120,7 @@ function create_new_user($columns, $row, $usertype) {
}
}

try {
$userid = $helper->create_user($user);
$role = $helper->get_role_id_by_name($usertype);

Expand All @@ -136,4 +146,12 @@ function create_new_user($columns, $row, $usertype) {
$helper->create_user_grade($usergrade);

$result = $helper->assign_role($userrole);
if (!$result) {
return "Error assigning role to user: {$user->username}";
}
} catch (Exception $e) {
return "Error creating user: {$user->username}";
}

return null;
}