Welcome email sent by bulk upload in 1.9

Welcome email sent by bulk upload in 1.9

by A Z -
Number of replies: 2

Hello

I was hoping to add users by the CSV file and have a welcome email sent right away.

I am using 1.9 as there was an issue uploading large courses (2+GB) with newer versions as well as hoping to keep some databases from a previous install.

I have found a modification i was looking for at https://moodle.org/mod/forum/discuss.php?d=85333&mode=3 (halfway down in the uploaduser.php) but this is for 2.0.x . I was hoping for some help in using this on 1.9. The section of code that I would be inserting this into is different:

original solution from url above:

if (!$isinternalauth) {
$user->password = 'not cached';
$upt->track('password', 'not cached');
} else {
$clearpassword = $usernew->newpassword; # <====== Add this line
$user->password = hash_internal_user_password($user->password);
}
$user->id = $DB->insert_record('user', $user);
$info = ': ' . $user->username .' (ID = ' . $user->id . ')';
$upt->track('status', $struseradded);
$upt->track('id', $user->id, 'normal', false);
$usersnew++;
if ($createpasswords && $isinternalauth) {
if (empty($user->password) || $forcechangepassword) {
// passwords will be created and sent out on cron
set_user_preference('create_password', 1, $user->id);
set_user_preference('auth_forcepasswordchange', 1, $user->id);
$upt->track('password', get_string('new'));
} else {
set_user_preference('auth_forcepasswordchange', 1, $user->id);
}
}
// Add lines from here ====>
else {
$site = get_site();
$mainadmin = get_admin();
$a = new object();
$a->firstname = $user->firstname;
$a->sitename = format_string($site->fullname);
$a->username = $user->username;
$a->newpassword = $clearpassword;
$a->link = $CFG->wwwroot .'/login/';
$a->signoff = fullname($mainadmin, true).' ('. $mainadmin->email .')';
$message = get_string('newusernewpasswordtext', '', $a);
$subject = format_string($site->fullname) .': '. get_string('newusernewpasswordsubj');
email_to_user($usernew, $mainadmin, $subject, $message);
}
// Add lines to here <=====


similar code found in 1.9 version of uploaduser.php

if ($user->id = insert_record('user', addslashes_recursive($user))) {
$info = ': ' . $user->username .' (ID = ' . $user->id . ')';
$upt->track('status', $struseradded);
$upt->track('id', $user->id, 'normal', false);
$usersnew++;
if ($createpasswords and empty($user->password)) {
// passwords will be created and sent out on cron
set_user_preference('create_password', 1, $user->id);
set_user_preference('auth_forcepasswordchange', 1, $user->id);
$upt->track('password', get_string('new'));
}
if ($forcechangepassword) {
set_user_preference('auth_forcepasswordchange', 1, $user->id);
}
} else {
// Record not added -- possibly some other error
$upt->track('status', $strusernotaddederror, 'error');
$userserrors++;
continue;
}
// save custom profile fields data
profile_save_data(addslashes_recursive($user));

// make sure user context exists
get_context_instance(CONTEXT_USER, $user->id);

if ($bulk == 1 or $bulk == 3) {
if (!in_array($user->id, $SESSION->bulk_users)) {
$SESSION->bulk_users[] = $user->id;
}
}



I would be very appreciative of any help. Thank you.

Average of ratings: -
In reply to A Z

Re: Welcome email sent by bulk upload in 1.9

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Hi,

Around line 203 in uploaduser.php (in Moodle 1.9.19+ current as of today), you need to add the marked line:


// password is special field
if ($key == 'password') {
if ($value !== '') {
$clearpassword = $value; // <========= Add this line
$user->password = hash_internal_user_password($value);
if (!empty($CFG->passwordpolicy) and !check_password_policy($value, $errmsg)) {
$forcechangepassword = true;


Then, around line 546 in uploaduser.php (in Moodle 1.9.19+ current as of today) you need to add all the lines between the 'Add lines from here' line to the 'Addd lines to here' line.


if ($createpasswords and empty($user->password)) {
// passwords will be created and sent out on cron
set_user_preference('create_password', 1, $user->id);
set_user_preference('auth_forcepasswordchange', 1, $user->id);
$upt->track('password', get_string('new'));
}
// ============> Add lines from here
else {
$site = get_site();
$supportuser = generate_email_supportuser();
$a = new object();
$a->firstname = fullname($user, true);
$a->sitename = format_string($site->fullname);
$a->username = $user->username;
$a->newpassword = $clearpassword;
$a->link = $CFG->wwwroot .'/login/';
$a->signoff = generate_email_signoff();
$message = get_string('newusernewpasswordtext', '', $a);
$subject = format_string($site->fullname) .': '. get_string('newusernewpasswordsubj');
email_to_user($user, $supportuser, $subject, $message);
}
// ============> Add lines to here
if ($forcechangepassword) {
set_user_preference('auth_forcepasswordchange', 1, $user->id);
}


Hope that helps.

Saludos.
Iñaki.
In reply to Iñaki Arenaza

Re: Welcome email sent by bulk upload in 1.9

by A Z -

Hello Mr Arenaza

 

  Thank you so much for your help! You are wonderful.