Only one record per user

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
sjohn
Veteran Member
Posts: 86
Joined: 2018-05-23 09:32

Only one record per user

Post by sjohn » 2018-08-14 17:11

Scenario :

A system where clients are able to have one record ( in a table eng_firms ) where they give in some information of their firm, and then in a child-table creates some records where they can inform of their oroducts and services.

First I had in mind that the system-owner ( as admin ) should create a user and then create one record in eng_firm and then transfer the ownership to the client/user newly created. Then someone came up with an idea : Why not check if a user has a record in eng_firm - and if, then tell the user that only one record ( the really smart would be if one could specify a number - but as for now only if one exist ) is allowed. This way the system owner only has to create the user as member and then send login-information - then the user should be able to do the rest himself.

What (code) should be done to achieve this?
Has someone made something like this already?

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Only one record per user

Post by pbottcher » 2018-08-16 07:53

Hi,

if the client is allowed to only view his records in the eng_firms table view you can achieve this by adding a check to the hooks/eng_firm.php -> eng_firm_header function. Also, if you want to make it variable, you can extend the solution to check on a certain amount of records.

See https://forums.appgini.com/phpbb/viewto ... f=4&t=2757
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

sjohn
Veteran Member
Posts: 86
Joined: 2018-05-23 09:32

Re: Only one record per user

Post by sjohn » 2018-08-16 08:23

Hello pböttcher

All ( both public and logged in ) are allowed to view the eng_firms.
But editing records in eng_firms are only allowed for the owner.
A registered user can also create records in eng_firms.
What I want is, that when a record is existing for the actual user, then he should not be allowed to create a new. It is OK he sees the button for that, but a message should be displayed when he tries to create a new, and there is already one record he has earlier created.

This is to prevent each user to create more than one record in eng_firms

I have a feeling that it should be a code in hooks/eng_firms.php in the section for the insert operation.
I also have a feeling that it should be rather simple, but it is specially the check for the logged-in user that is a problem for me.
I am not sure how the code should be.

sjohn
Veteran Member
Posts: 86
Joined: 2018-05-23 09:32

Re: Only one record per user

Post by sjohn » 2018-08-16 09:47

I will try to explain the "luxury" version in pseudo dode :
-----------------------------------------------------
Find actualuser usergroup
define for group=basicfree maxallowedrecords=1
define for group=silvermember maxallowedrecords=2
define for group=goldmember maxallowedrecords=5

Check in eng_firms

If numberofrecords for actualuser < maxallowedrecords then OK
else message "As member of <usergroup> you are only allowed to create <maxallowedrecords> records" - and then abort
------------------------------------------------------------

This will do, that a user that is member of the basicfree group will be allowed to create only one record.
If he was member of the group goldmember he was allowed to create 5 records.

The same code could be used for the child-table eng_items

Here it could be :

-----------------------------------------------------
Find actualuser usergroup
define for group=basicfree maxallowedrecords=10
define for group=silvermember maxallowedrecords=50
define for group=goldmember maxallowedrecords=250

Check in eng_items

If numberofrecords for actualuser < maxallowedrecords then OK
else message "As member of <usergroup> you are only allowed to create <maxallowedrecords> records" - and then abort
------------------------------------------------------------

Then a user that is member of the basicfree group was allowed to create 10 records in eng_items
If a user was a member of the group silvermember, then he was allowed to create 50 records in one of his 2 parent-tables, OR he was allowed to create 20 records on the one parent-record and 30 records on the other parent-record.
Same with the goldmember - he is allowed to create 250 records and can freely choose how many records on each parent-record he want to create - as long it does not exceed 250.

Hope I have been able to explain what it is I want to achieve.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Only one record per user

Post by pbottcher » 2018-08-16 18:22

Hi,

thanks for the explanation.

You can try this:

in hooks/eng_firms.php -> eng_firms_header function add

Code: Select all

$mi = getMemberInfo();
$group = $mi["group"];
switch ($group) {
   case "basicfree": 
        $maxallowedrecords=1;
        break;
   case "silvermember": 
        $maxallowedrecords=2;
        break;
   case "goldmember": 
        $maxallowedrecords=5;
        break;
   default: 
        $maxallowedrecords=0;
    }

$recordcount=sqlvalue("SELECT count(1) FROM `membership_userrecords` where memberID=".$mi['username']."and tableName='eng_firms'");

if ($recordcount >= $maxallowedrecords  && $group != 'admin') {
				$header.="<script type=\"text/javascript\">
							\$j(function(){
							\$j('#addNew').hide();
}
In the eng_firms.php -> eng_firms_dv function

Code: Select all

	function eng_firms_dv($selectedID, $memberInfo, &$html, &$args){
		if(isset($_REQUEST['dvprint_x'])) return;	
		$mi = getMemberInfo();
		$group = $mi["group"];
		if ($group == 'admin') return;

		$recordcount=sqlvalue("SELECT count(1) FROM `membership_userrecords` where memberID=".$mi['username']."and tableName='eng_firms'");

		switch ($group) {
		   case "basicfree": 
		        $maxallowedrecords=1;
		        break;
		   case "silvermember": 
		        $maxallowedrecords=2;
		        break;
		   case "goldmember": 
		        $maxallowedrecords=5;
		        break;
		   default: 
		        $maxallowedrecords=0;
		    }
		ob_start(); ?>
		<script type=\"text/javascript\">
		var counter=<?php echo $count;?>;
		var group=<?php echo $group;?>;
		var recordcount=<?php echo $recordcount;?>;
		if (recordcount >= maxallowedrecords) {
			$j(function(){
				$j('#addNew,#insert').hide();
			    }
		}
		</script>
<?php
		$new_layout = ob_get_contents();
		ob_end_clean();
		
		$html .= $new_layout;
	}
Like this you will hide the "AddNew" botton in the table view and hide "AddNew, "Save As Copy" Buttons in the DetailView.

You can apply the same to the eng_items, only thing is you need to think about how to identify the amount of records the user has already stored via the SQL statement.
You could even extend this and have a table where you have the maxallowed entried for the different groups and catch the data from there :-).

Hope that gives you the right direction.
As I did not test the code there might be some syntax errors. So please be careful.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

sjohn
Veteran Member
Posts: 86
Joined: 2018-05-23 09:32

Re: Only one record per user

Post by sjohn » 2018-08-16 19:28

Thank you, Pböttcher

I am glad you will help with this. I have not tested this yet, but will of course do this.

I have a couple of questions :

There seems to be no message informing the user that his max is reached, and that inserting therefore not was performed. Is this because you "remove" the buttons and a message should therefore not be needed?


Is the code to put in the eng_firms.php only made to hide the insert/add new buttons?
pböttcher wrote:
2018-08-16 18:22

You can apply the same to the eng_items, only thing is you need to think about how to identify the amount of records the user has already stored via the SQL statement.
What do you mean by this? Do you mean if it is for an "old" system where there for a user could already be more entries than the allowed?

The default is set to 0. Is this a value that is used if a user belongs to a newly created group that is not yet defined in the code?

Again - Thanks for helping

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Only one record per user

Post by pbottcher » 2018-08-16 22:12

Hi,
There seems to be no message informing the user that his max is reached, and that inserting therefore not was performed. Is this because you "remove" the buttons and a message should therefore not be needed?
yes exactly.
Is the code to put in the eng_firms.php only made to hide the insert/add new buttons?
The code in the eng_firms_header function is to hide the add new button in the table view if the limit is reached
What do you mean by this?
This means that you need to create your SQL query in such a way that you know how many records the user has already stored in the eng_items table corresponding to the eng_firms table. I have no structure of those table.
The default is set to 0
This is for the public users as I assume they are not allowed to add firms/items.
But it applies also to all other groups if you have more of them.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

sjohn
Veteran Member
Posts: 86
Joined: 2018-05-23 09:32

Re: Only one record per user

Post by sjohn » 2018-08-20 09:41

Hello pböttcher

I have some problems with placing part of the code. It is the code "in hooks/eng_firms.php -> eng_firms_header function add".
I am not quite sure exactly where in section to place the code. When I try, there seems to go something wrong with the structure in the eng_firms.php

Could it be made so that a message is displayed instead of removing buttons. I think this will be more transparent for the user. He will then be informed why he is not allowed to create more entries.

Should such a check not be put in the before_insert section then ?

Could you help with this?

In advance - thanks for helping.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Only one record per user

Post by pbottcher » 2018-08-20 10:53

Hi,

of course you can also put code to the before_insert function. From my perspective it does seem a little strange that the user will enter all his data, press the save button and then gets alerted that he is not allowed to enter this data due to some restrictions.

The code above just needs to go to the eng_firms_header function in the

Code: Select all

switch($contentType){
			case 'tableview':
section

or the

Code: Select all

			case 'tableview+detailview':
depending on your setup.


Maybe you can post what you have as function to see why it does not work.

For the before_insert you need to get the amount of record already stored in the database and compare it to your limits.

you could add

Code: Select all

$recordcount=sqlvalue("SELECT count(1) FROM `membership_userrecords` where memberID=".$memberInfo['username']."and tableName='eng_firms'");
$group = $memberInfo["group"];
switch ($group) {
   case "basicfree": 
        $maxallowedrecords=1;
        break;
   case "silvermember": 
        $maxallowedrecords=2;
        break;
   case "goldmember": 
        $maxallowedrecords=5;
        break;
   default: 
        $maxallowedrecords=0;
    }
if ($recordcount >= $maxallowedrecords  && $group != 'admin') {
return false; 
}
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

sjohn
Veteran Member
Posts: 86
Joined: 2018-05-23 09:32

Re: Only one record per user

Post by sjohn » 2018-08-20 13:01

Hello pböttcher

You are completely right, that it should happen before the user fills in data. I thought it would happen when the user hit the "Add new" button.
I thought the before_insert was performed before a new record was created.

I will now try if I can have it to function - one way or another.
Again - thank you for helping.

Post Reply