Support

  1. ialearn
  2. General
  3. Thursday, February 21 2019, 02:50 PM
Hi

I have taken a look at the article
https://www.easy-profile.com/support/auto-creation-of-username.html

I was wondering what would I need to do if I wanted to generate a username based on the user ID
We work in online education so I would like to issue a username (student number) in the following format

two to three letter text followed by current year followed by user ID eg (int20191532)
admin Accepted Answer
Admin
Hi,
unfortunately there is not a efficient way to do this, for following reasons:

1) You need to make username available in registration form because it is required by Joomla and it need to be available a value
2) You does not have any user ID because user is still not registered

How to deal with these problems

Problem 1
- you can hide username with a CSS code like
.control-group.username-group {display:none !important;}

- you can assign a temporary random value with a javascript code like
jQuery(document).ready(function(){jQuery('#member-registration #jform_username').val(Math.random().toString(36).substring(7))});


Problem 2
You can change username after user has registered with a plugin:
- Download (at the end of docs page at http://docs.easy-profile.com/index.php/article/triggers ), install and enable (Extensions->Plugins) skeleton plugin
- Open the edit the file at /plugins/jsn/skeleton/skeleton.php
- Replace entire code with this
<?php

defined('_JEXEC') or die;

class PlgJsnSkeleton extends JPlugin
{
public function triggerProfileUpdate($user, &$data, $changed, $isNew)
{
if($isNew && $user->id > 0)
{
$new_username = 'int'.date(Y).$user->id;
$db = JFactory::getDbo();
$query = 'UPDATE #__users SET username = ' . $db->quote($new_username) . ' WHERE id = '. (int) $user->id;
$db->setQuery($query);
$db->execute();
}
}
}


Other Problems
When a user register an account then it will receive an email with username and password. Unfortunately the email will contain the temp username.
You cannot solve this problem without hack Joomla code (not recommended), so you can only remove login data from generated email with language override.
  1. more than a month ago
  2. General
  3. # 1
ialearn Accepted Answer
Hi

Thank you for the feedback on problem 2 would it not be possible to use the last user ID issued +1 in the username.
  1. more than a month ago
  2. General
  3. # 2
admin Accepted Answer
Admin
Hi,
of course this is possible but I think this will not change the behavior, the database ID is generated by DB server and it is an autoincrement value. I suppose you look for continuity between usernames.

For example
- my last user have ID equal to 20, so the username could be int201920
- I register a new account, the database ID will be 21 and the username "int201921" (21 = previous ID 20 + 1)
- I delete previous user "int201921"
- Now the next ID when I make registration will be 22 (database ID is an auto increment number), so the username will be "int201921" ( 21 = 20 the last ID +1 )
- The next registered user will have ID 23, the username will be "int201923" (23 = previous ID 22 + 1)

So you have these users
int201920 with Database ID 20
int201921 with Database ID 22
int201923 with Database ID 23

As you can see you still do not have continuity between usernames.
  1. more than a month ago
  2. General
  3. # 3
ialearn Accepted Answer
Hi

I see your point, what if I was to create a variable that simply stores a value that increments.
I assume I would need a variable where the "2019" is stored would it be possible to then simply start the variable at 1 and then increment it throughout the year and then start it again next year that way if the value does not coincide with the user ID it does not matter.
eg
int20190001
int20190002
int20190105
int20200001

The main thing is I would like to automatically generate a username that is always guaranteed to be unique but is not simply a random bunch of characters that is difficult for someone to remember
  1. more than a month ago
  2. General
  3. # 4
admin Accepted Answer
Admin
Hi,
of course this is possible, in skeleton plugin you can write something like this:
<?php

defined('_JEXEC') or die;

class PlgJsnSkeleton extends JPlugin
{
public function triggerProfileUpdate($user, &$data, $changed, $isNew)
{
if($isNew && $user->id > 0)
{
$year_file_path = JPATH_SITE . '/' . date('Y').'.txt'; /* path of file where is store last id, the name depends from the year */
if(file_exists($year_file_path)){
$last_id = file_get_contents($year_file_path); /* if file exists then get last id from year file */
}
else{
$last_id = 0; /* if file does not exists then last id is 0 */
}
$new_id = (int) $last_id + 1; /* new id = last id +1 */
file_put_contents($year_file_path, $new_id); /* save the new last id in year file*/
$new_username = 'int'.date('Y').sprintf("%04d", $new_id); /* generate username with a format like int20190023*/

/* save new username in DB */
$db = JFactory::getDbo();
$query = 'UPDATE #__users SET username = ' . $db->quote($new_username) . ' WHERE id = '. (int) $user->id;
$db->setQuery($query);
$db->execute();
}
}
}

Basically this function store a file called 2019.txt with last id, when the years will be 2020 then it store a file called 2020.txt with last id that start from 0, these files are store in the root directory of the site.
  1. more than a month ago
  2. General
  3. # 5
ialearn Accepted Answer
Hi

Thank you for the feedback, I have tested the plugin and it works.
Unfortunately, the elearning system we use with Joomla does not like having the username changed and the fact that it changes the username after the registration email is sent is less than ideal, so in order for this to work I would basically need this plugin to generate the username like it is doing but instead of triggering on update it would need to trigger on opening the registration screen and then populating the username field with the username it generated.

Would this be at all possible?
  1. more than a month ago
  2. General
  3. # 6
admin Accepted Answer
Admin
Hi,
so the problem is the generated email? if yes then you can simply remove login data from generated email with language override.

For example you can override the language constant "COM_USERS_EMAIL_REGISTERED_WITH_ACTIVATION_BODY" from:
Hello %s,\n\nThank you for registering at %s. Your account is created and must be activated before you can use it.\nTo activate the account select the following link or copy-paste it in your browser:\n%s \n\nAfter activation you may login to %s using the following username and password:\n\nUsername: %s\nPassword: %s
to
Hello %s,\n\nThank you for registering at %s. Your account is created and must be activated before you can use it.\nTo activate the account select the following link or copy-paste it in your browser:\n%s
  1. more than a month ago
  2. General
  3. # 7
ialearn Accepted Answer
Hi
The email is only partially the problem, the main problem is that we use Joomla and Moodle(E-learning system) the moment a person clicks "register" a user account is created in both Joomla and Moodle using the details entered in the registration form.
Due to the fact that the two systems are setup to stay in sync for some reason trying to change a user causes conflicts between the two systems.

As a result, the user name must be generated before the user is registered if this is not possible I understand.
  1. more than a month ago
  2. General
  3. # 8
admin Accepted Answer
Admin
Hi,
user name must be generated before the user is registered
Something like this could be possible, by changing the username input before registration process with a function like this:
public function onAfterRoute()
{
$app=JFactory::getApplication();
if(JFactory::getApplication()->input->get('task')=='registration.register' || ($app->isAdmin() && JFactory::getApplication()->input->get('option')=='com_users' && JFactory::getApplication()->input->get('layout')=='edit' && JFactory::getApplication()->input->get('id',-1)==0 ))
{
$year_file_path = JPATH_SITE . '/' . date('Y').'.txt'; /* path of file where is store last id, the name depends from the year */
if(file_exists($year_file_path)){
$last_id = file_get_contents($year_file_path); /* if file exists then get last id from year file */
}
else{
$last_id = 0; /* if file does not exists then last id is 0 */
}
$new_id = (int) $last_id + 1; /* new id = last id +1 */
file_put_contents($year_file_path, $new_id); /* save the new last id in year file*/
$new_username = 'int'.date('Y').sprintf("%04d", $new_id); /* generate username with a format like int20190023*/

/* replace username var */
$input = JFactory::getApplication()->input;
$form=$input->post->get('jform', array(), 'array');
$form['username'] = $new_username
$input->post->set('jform', $form);
JFactory::getApplication()->input->set('jform', $form);
}
}


NOTE: this code is not tested, it need to replace function entire function "triggerProfileUpdate" from my previous reply.
  1. more than a month ago
  2. General
  3. # 9
ialearn Accepted Answer
Wow

Thank you for the assistance, I will test the code and revert back to you.
  1. more than a month ago
  2. General
  3. # 10
ialearn Accepted Answer
Here is what I got after installing
0 syntax error, unexpected '$input' (T_VARIABLE)
  1. more than a month ago
  2. General
  3. # 11
admin Accepted Answer
Admin
At line 20 in my code missing ";" at the end :o
$form['username'] = $new_username;
  1. more than a month ago
  2. General
  3. # 12
  • Page :
  • 1


There are no replies made for this post yet.
However, you are not allowed to reply to this post.

Request Support

Support is currently Offline

Support Availability

Working days: Monday to Friday. The support staff is not available on weekends; in the most of cases tickets will not be answered during that time.

Reply time: Depending on the complexity of your support issue it's usually between a few minutes and 24 hours for paid members and about one week for free members. When we expect longer delays we will notify you.

Guidelines

Before you post: read the documentation and search the forums for an answer to your question.

When you post: include Site Details if you request a support (you can use the form below the reply in Site Details tab).

Auto Solved Question: If after a week the author of the post does not reply to a request by moderator, the question will be marked as resolved.

Language: only English

Search Users

Easy Profile® is not affiliated with or endorsed by Open Source Matters or the Joomla Project. Joomla is Free Software released under the GNU/GPL License.