upload users through phpMySql problem

upload users through phpMySql problem

by Jorge Blancarte -
Number of replies: 3

I have uploaded users through Phpmysql, already confirmed them, I can see their profiles, and I can add them to courses but I can't access moodle with their username and password. I have manually inserted all the info into phpmysql to confirm them. Still no success.

Average of ratings: -
In reply to Jorge Blancarte

Re: upload users through phpMySql problem

by Luis de Vasconcelos -

If users can't login then it means you aren't creating the users correctly. But you really shouldn't upload users through Phpmysql. Creating a user involves a lot more than just inserting their record into the mdl_user table.

It's much safer to create the users via a php page that uses the Moodle core API's to create the user profiles. Example: you could create a web service that uses user_create_user() to create the users, as explained in https://moodle.org/mod/forum/discuss.php?d=333851#p1345396 or https://moodle.org/mod/forum/discuss.php?d=326159#p1311484

Average of ratings: Useful (1)
In reply to Jorge Blancarte

Re: upload users through phpMySql problem

by Perry Way -

I went down this road a long time ago and then decided it would be better to use the Moodle code to create users using user_create_user() function. 

One reason why pertains to the password which needs to be encoded. I would advise you look into calling the function update_internal_user_password() to set the password. You can pass a text password and it will encode it properly and store it in the database in the correct manner. If your case is such that the password you provide is only a temporary one and you want the user to change the password upon their first login, then you should make a call to set_user_preference() right after update_internal_user_password(). I will show you here what I mean:

        $user = new object();
        $user->password = "password"; // not sure but I think password cannot be blank, maybe I'm wrong, see below..
        $user->idnumber = $values["StudentID"];
        $user->firstname = $values["FirstName"];
        $user->lastname = $values["LastName"];
        $user->username = $values["Username"];
        $user->phone1 = $values["HomePhone"];
        $user->phone2 = $values["MobilePhone"];
        $user->email = $values["Email"];
        $user->mnethostid = 1;
        $user->confirmed = 1;
        $date = new DateTime($values["StartDate"]);
        $user->timecreated = strtotime(date('Y-m-d', $date->getTimestamp()));
        $new_userid = user_create_user($user, false, false);
        $new_user = $DB->get_record('user', array('id'=>$new_userid));
        // set default password
        update_internal_user_password($new_user, 'password');
        // force password change
        set_user_preference('auth_forcepasswordchange', 1, $new_user);


Average of ratings: Useful (1)
In reply to Perry Way

Re: upload users through phpMySql problem

by Nicolas Martignoni -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

You may use "changeme" as the initial password. This would force the users to change their password at the first login.

Average of ratings: Useful (2)