Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Adding an additional attribute to the User entity

  • unitb_lime
  • unitb_lime's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 5 months ago #207161 by unitb_lime
Hi, I'm developing customizations for a LimeSurvey Instance (Version 3.22).I want to add an additional attribute to the LimeSurvey entity 'User' in order to display the company a user belongs to.
Unfortunately I couldn't find any forum entries about this topic, so I decided to ask it here.


I'll try to explain what I did so far:
I was able to create an attribute '$company' in /application/models/User.php and change the dialogue for adding a user, so that you can now add a company name. But it just doesn't save it internally. It always jumps back to my default value 'Not assigned', so that means it's not stored in the database or I'm doing something wrong there.
So my question would be how do I permanently save this additional attribute in the database?


This is my customized menu:



And til here it works:


Here it jumps back to the default value 'Not assigned':



This is my modified 'insertUser' function in User.php:
Code:
public static function insertUser($new_user, $new_pass, $new_full_name, $parent_user, $new_email, /* unitb edit start */ $new_company = "Hello World" /* unitb edit end */)    {
        $oUser = new self;
        $oUser->users_name = $new_user;
        $oUser->setPassword($new_pass);
        $oUser->full_name = $new_full_name;
        $oUser->parent_id = $parent_user;
        $oUser->lang = 'test';
        $oUser->email = $new_email;
        /* unitb edit start */
        $oUser->company($new_company);
        /* unitb edit end */
        if ($oUser->save()) { 
           return $oUser->uid;
        } else {
            return false;
        }
    }


And this is how I call it in /application/core/plugins/Authdb/Authdb.php :
Code:
public function createNewUser()    {
[...]
$new_full_name = flattenText(Yii::app()->request->getPost('new_full_name'), false, true);
        /* unitb edit start */
        $new_company = flattenText(Yii::app()->request->getPost('new_company'), false, true);
        $new_pass = createPassword();
        $iNewUID = User::model()->insertUser($new_user, $new_pass, $new_full_name, Yii::app()->session['loginID'], $new_email, $new_company);
        /* unitb edit end */
        if (!$iNewUID) {
            $oEvent->set('errorCode', self::ERROR_ALREADY_EXISTING_USER);
            $oEvent->set('errorMessageTitle', '');
            $oEvent->set('errorMessageBody', gT("Failed to add user"));
            return;
        }
        Permission::model()->setGlobalPermission($iNewUID, 'auth_db');
        $oEvent->set('newUserID', $iNewUID);
        $oEvent->set('newPassword', $new_pass);
        $oEvent->set('newEmail', $new_email);
        $oEvent->set('newFullName', $new_full_name);
        /* unitb edit start */
        $oEvent->set('newCompany', $new_company);
        /* unitb edit end */
        $oEvent->set('errorCode', self::ERROR_NONE);
[...]


This function I call in /application/controllers/admin/useraction.php


Thanks so much for your help!
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago #207184 by DenisChenu
Replied by DenisChenu on topic Adding an additional attribute to the User entity
It can be very intersting to have a functionality for plugin …


Nut in my opinion : it's bet to use existing database for this : github.com/LimeSurvey/LimeSurvey/blob/ma...ls/PluginSetting.php

I almready add setting for user (but not needed in list) , i use a js solution to be able to update without issue.
gitlab.com/SondagesPro/mailing/smtpByUser

But it's seems broken in 3.X (it work but HTML is not cool …)

Here some of my feature request:
15078: Allow adding plugin settings for user
13222: Unable for AuthPlugin to update User Creation form (even the dropdown) i have another one (but don't find it) to allow exactly what you want.

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • unitb_lime
  • unitb_lime's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 5 months ago #207230 by unitb_lime
Replied by unitb_lime on topic Adding an additional attribute to the User entity
Hi DenisChenu,

thank you for your answer.

I don't know too much about plugins, so unfortunately I don't know exactly what approach you have with the PluginSetting.php. Is it possible to use plugins for this change in the Database?

As far as I understand, I need to do an sql query like this on my db "ALTER TABLE {{users}} ADD company string(50)" in order to add this extra property, right?

So my LimeSurvey system is already up and running on four identical stages. Is there a way to make this change in the db automatically through LimeSurvey, so that there's no need to execute this query manually on every stage? Maybe through plugins as well?

Thank you so much!
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago #207234 by DenisChenu
Replied by DenisChenu on topic Adding an additional attribute to the User entity
Hi,

Currently : it's really hard to do itin plugin :) then you must hack code.

But if at a time : it was inside a plugin : we surely don't use users table for this , but pluginsettings table , with object as User.

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • unitb_lime
  • unitb_lime's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 5 months ago #207622 by unitb_lime
Replied by unitb_lime on topic Adding an additional attribute to the User entity
I solved it! Here's how I did it:

As I assumed it was a problem with the db. You can only add new properties to an entity by changing the entry in the db.

So I accessed the db my limesurvey instance is running on and executed this:
Code:
"ALTER TABLE `lime_users`  
ADD COLUMN `company` VARCHAR(50) NULL DEFAULT 'Not assigned' AFTER `one_time_pw`"


And I changed this code in the file "/installer/create.database.php" at line 785:
Code:
// users
$oDB->createCommand()->createTable('{{users}}', array(
            'uid' => "pk",
            'users_name' => "string(64) NOT NULL default ''",
            'password' => "text NOT NULL",
            'full_name' => "string(50) NOT NULL",
            'parent_id' => "integer NOT NULL",
            'lang' => "string(20)",
            'email' => "string(192)",
            'htmleditormode' => "string(7) default 'default'",
            'templateeditormode' => "string(7) NOT NULL default 'default'",
            'questionselectormode' => "string(7) NOT NULL default 'default'",
            'one_time_pw' => "text",
            /* unitb change start */
            'company' => "string(50) default 'Not assigned'",
            /* unitb change end */
            'dateformat' => "integer NOT NULL DEFAULT 1",
            'created' => "datetime",
            'modified' => "datetime",
        ));
This way the property is still added when installed again. I didn't try this code though.


Now I can access the property everywhere.

Have a great day!
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago #207629 by DenisChenu
Replied by DenisChenu on topic Adding an additional attribute to the User entity
But you still can not update …

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • unitb_lime
  • unitb_lime's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 5 months ago #207631 by unitb_lime
Replied by unitb_lime on topic Adding an additional attribute to the User entity
Hi DenisChenu,

I'm not sure what kind of 'update' you mean.

If you're talking about the property 'company' and if I can save it permanently: Yes that works.

If you're talking about future limesurvey updates: In order to make it work, I would have to transfer all my code customizations to that version. I did that previously and it's a lot of work, but functions.

Thank you!
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago #207636 by DenisChenu
Replied by DenisChenu on topic Adding an additional attribute to the User entity

unitb_lime wrote: If you're talking about future limesurvey updates: In order to make it work, I would have to transfer all my code customizations to that version. I did that previously and it's a lot of work, but functions.

Yes, this part.

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: unitb_lime
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose