MySQL - Track user activity to display content only the first time - mysql

I need to show a modal window the first time the user logs in, after logging in, that modal window should not be displayed.
Questions:
I'm thinking of creating a field in the user table to be called, modal_first_time, and adding the values 0 or 1
0 = Modal not shown
1 = Modal shown
So that when you log in for the first time, perform a logging in that table and change the value from 0 to 1, so you do not show that modal window again.
But is this optimal? What if I have 10 modal windows, do I have to create 10 additional fields?
It is well the way to add a field in the table, or there is some more optimal and simple, the best would be a session variable but these when clearing Cookies or switching computers would show again.

I would suggest making a "Modal accessed" table.
This table would hold 4 columns.
UID, UserID, ModalID, accessed
To find if the current status of a Modal for a user you would run a SELECT in your "onLogin" event.
Below is a very rough example of how this could be utilized to work for 1 or many Modals
SELECT ModalID FROM `SchemaName`.`ModalAccessedTable`
WHERE UserID = "Bob"
AND Accessed = 0
OR another usage
SELECT Accessed FROM `SchemaName`.`ModalAccessedTable`
WHERE UserID = "Bob"
AND ModalID = 'ModalName'
Update access usage
UPDATE `SchemaName`.`ModalAccessedTable`
SET Accessed = 1
WHERE UserID = "Bob"
AND ModalID = 'ModalName'
With a setup like this you can select, and update each individual entry per userID as needed, with relatively efficient lookup efficiency.
Side Note:
Assuming this table will become large, you will benefit greatly from properly build multi-column indexes.

Related

How can I clear a specific filter without clearing all filters?

Background:
I am creating an app that stores record of trainings that employees in a company took in table. I want to filter the rows of the table based on training name and/or employee name. I was able to figure out this first part, and I was able to create a button that clears all the filters and reloads the entire table using the clearFilters() function.
Problem:
I want to create two buttons that clear the filter selections one ("All Trainings") for the training name and one ("All Employees") for the employee name. To be more clear, when I click on the "All Trainings" button, I want to clear the filters on the training name, but not on the Employee name. This will become useful once I have a table with multiple fields that I want to filter, and I want to navigate the table without having to reset all fields every time.
I tried searching the functions available on Google App Maker, but there was nothing that seemed to be able to solve my problem. Any suggestions?
From the Reference about datasources:
https://developers.google.com/appmaker/models/datasources#query_datasources
Like field filters, assigning null to a relation filter property clears that restriction.
So, something like:
datasource.query.filters.Employee._contains = null;
datasource.load();
should work for you.
I am sure there is a better way to do this, so you may want to wait for someone smarter to chime in, but I BELIEVE you can just have your button clear the filters, but reapply a new filter.
So for my situation I have the table do a filter for items that are listed as "Complete", so those are hidden when the user opens the page.
Then, when the users filter another field and want to clear out that search I didn't want the "Complete" items to reappear (which is what happened when I just used the clearFilters() function. So my workaround was to make the clear button actually clear the filter, but apply the original "Complete" filter.
So for my OnClick action, for my "Clear Button" I have:
widget.datasource.query.clearFilters();
widget.datasource.load();
app.closeDialog();
var datasource = app.datasources.TestModel;
datasource.query.filters.Status._notContains = 'Complete';
datasource.load();
The
widget.datasource.load();
app.closeDialog();
May be redundant/unnecessary.
Can you simply take 3 steps in your onClick handler?
(1) clear all filter
(2) set the employee name filter
(3) load data

Dynamically selecting MySQL updates

This is more of a theory question. I have a page with a bunch of various textfields, dropdown boxes, etc. Each user has his/her "own page" that can be updated via this update page that I am referring to. He updates the fields at his choosing. It passes about 30 variables (if every field is inputted) to a "preview page". If the person likes the preview page, then they click an "update" button at the bottom of the preview page and all the various variables are updated into the appropriate MySQL table and their "own page" that others see is updated dynamically. (let me know if this explanation isn't clear).
Inserting this information for the first time is easy. However, when a user wants to update only a few of the fields for his page later, this is where I am confused. How do I make the MySQL update query dynamic to recognize an update to ONLY the fields on the page that the user wants to update (while he leaves the other fields blank, thus leaving the old information intact for those columns, and they are disregarded in the update query).
Let me know if what I'm asking doesn't make sense and I'll try again.
Thanks for your help.
The easiest way to do this would be
UPDATE MyTable m SET m.f1 = COALESCE(input1,m.f1), m.f2 = COALESCE(input2,m.f2), ....
WHERE m.id = key;
The COALESCE will return the first non-null value (or null if all values are null).
Note that you can insert a default value after the existing field value if you want to force a default.
Like so:
UPDATE MyTable m SET m.f1 = COALESCE(input1,m.f1,default1), m.f2 = COALESCE(input2,m.f2,default2), ....
WHERE m.id = key;
See: MySQL: how to use COALESCE
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce

MySQL Query - Check if data row contains a user_id

I have a sessions table and what I am trying to do is run a query to check if a certain account is logged in.
My query looks like this:
SELECT id FROM sessions WHERE data LIKE '%4%'
But with a query like that, if the user_id of "14" is logged in, that query will return True. How do I check to see if a certain number exists and matches exactly?
Include the delimiters in your search:
WHERE data LIKE '%:4;%'
why don't you add another field for user_id to the session table?
EXAMPLE:
I have a site where a user can only be logged in from one location at a time. If they try and log in from another location (IE start a new session) then i kill all of their previous logins.
So as part of my login script:
// log this user out of any other active sessions
$sql = sprintf("DELETE
FROM sessions
WHERE id_user=%s",
mysql_real_escape_string($_SESSION['id_user'])
);
// Associate this session with this user
$sql = sprintf("UPDATE sessions
SET id_user=%s
WHERE id=%s",
mysql_real_escape_string($_SESSION['id_user']),
session_id()
);
and so i can have id_user as an additional field in my session FKed to my user table... its a little more normalized and lets you do quick "Who is currently using this site" queries without too much parsing and fuss.

Setting the APP_USER as a value to be passed to target page in APEX

I'm making a student management system on apex. The short of it is a place where lecturers and students can log on. Lecturers create assignments, assign them, mark them, take attendance, record issues ...... all that, and students log on to view their attendance and results.
Now when a student clicks the "My Results" link it navigates to the same page that a lecturer sees, though the select list where a student is selected to view the results of is hidden. The select list displays the students name and returns the id for that student, which also happens to be the user name for a student to log in.
So i want to pass the value of the app-user when a student clicks the link so that only their results are shown.
I've tried to set
these items
:P10_SELECT_STUDENT
with these values
#APP_USER#
which works but no the message "no data found" is shown.
Just for testing i've set that select list to be displayed for a student, and when the page loads it loads with the null value at the top of the list which is
display value
select a student
return value
-1
I've gone and manually set the value passed to be the id of the test student. works a treat, the data loads for that student!!
So does anyone know why the #APP_USSER# value im sending isnt being set in the listbox
Thanks in advance
What you need is the session state of the APP_USER variable. In a query you would reference this with :APP_USER. When you need to pass on the value as a parameter for, for example, a link, you would use the substitution string notation &APP_USER. Much the same way you would refer any other variable/page item.
For example, setting up a button:
A good page to read up on Substitution strings: Application Builder Concepts
The hash-sign notation is commonly used for non-plsql-variables substitution, like the value of a column in report when passed through in a link,
Zac,
If the users haven't authenticate themselves then the :APP_USER parameter is null. If the user authenticated via a SSO, or via DB credentials or whatever you have in the application then the :APP_USER will get populated.
Here is what i understood :
In page 12 , P12_STUDENTS is a select list, and you want it to reflect the current student if he enters or the full list if he's a teacher right?
You don't need to pass :APP_USER via a link or a branch or whatever. It exists as a global Apex variable and is visible in page 12 via :APP_USER but again , its null if the user is not authenticated.
Your select list source should be like:
select display d, return r
from table
where (:APP_USER is NULL
OR (:APP_USER IS NOT NULL AND :APP_USER = student))
tell me if this helps?
regards,
Alex

Check if mySQL record added in the last x seconds

I have a mySQL database and a table where new records for a project are created. Each project created has a "project name" and an event created date (of type DATETIME).
There can be two projects created with the same name, but if they get created by the same user in quick succession, it is safe to assume it was a mistake on the user's part (clicking twice, refreshing the browser when event variables are passed, etc.).
How do I write a SQL statement to check if a record with the same name already exists, it was added in the last 10 seconds? So far I have the following, although I don't know how to check for the last 10 seconds.
select * from projects where user = 'johnsmith' AND projectname = 'test' AND active='y' AND DATE(projectcreatedon) = CURRENT_DATE AND DATEPART() < ....?
replace AND DATE(projectcreatedon) = CURRENT_DATE AND DATEPART() < ....? with:
AND projectcreatedon > (now() - INTERVAL 10 SECOND)
I would suggest not to keep such checks in MySQL because that might not be the perfect way of knowing mistakes because the user might well click the submit or refresh the page after 10 seconds. Instead, put checks in the front-end code to disable clicking the submit button twice or redirect the user to a page where no variables are passed.
But if that isn't what you would like to do, then this might be your query:
SELECT *
FROM `projects`
WHERE `user` = 'johnsmith'
AND `projectname` = 'test'
AND `active`='y'
AND TIMESTAMPDIFF(SECOND, projectcreatedon, now()) > 10;
You're trying to fix the problem in the wrong way. Why not eliminate the problem at the source? Make it impossible for the user to create these two projects successively.
If your app makes it possible for a user to submit a form multiple times via refresh, consider using a redirect after the GET/POST variables have been processed.
Furthermore, use simple client-side tricks to disable the submit button after it has been clicked once. You can accomplish this with a very small amount of jQuery