Moodle SQL report - moodle

I am trying to make an SQL report. In this reports there would be "Duplicated users", "Special users", "Test users" and "Normal users". There is a checkbox for users (only the administrator sees this checkbox) and if the admin ticks this chechkbox the user will be a "Duplicated user", special users will be those who have xy.com in their email addresses and test users will be those who have the word "test" in their firstnames and all the other users will be normal users. My problem is if a user is a duplicated user then they appear twice in this report, I think I know why but I don't know how to solve it, they appear once as normal users and once as duplicated user but I only want them to appear as duplicated users, here is my code:
SELECT distinct(u.email), u.username, u.lastname, if(DATE_FORMAT(FROM_UNIXTIME(u.firstaccess),'%Y-%m-%d') = '1970-01-01', 'Never logged in', DATE_FORMAT(FROM_UNIXTIME(u.firstaccess),'%Y-%m-%d')) AS 'First log in', u.firstname,
CASE
WHEN uif.shortname = 'duplicated' and uid.data = 1 THEN 'Duplicated user'
WHEN u.firstname like '%test%' THEN 'Test user'
WHEN u.email like '%#xy.com%' THEN 'Special user'
ELSE
'Normal user'
END AS Usertype
FROM prefix_user AS u
JOIN prefix_user_info_data AS uid ON uid.userid = u.id
JOIN prefix_user_info_field AS uif ON uid.fieldid = uif.id
where u.deleted = 0 and u.username <> 'guest'

You might be getting duplicates because there is more than one custom field
Try changing the join to this
LEFT JOIN (
SELECT uid.userid, uid.data
FROM prefix_user_info_data AS uid
JOIN prefix_user_info_field AS uif ON uid.fieldid = uif.id AND uif.shortname = 'duplicated'
) uid ON uid.userid = u.id
and the case to this
WHEN uid.data = '1' THEN 'Duplicated user'

Related

Add select COUNT events in common between the current user and the others in DQL query

I have this DQL query in Symfony 4 project, it does select all users who are registered in all the same events as current user
$query = $this->_em->createQuery(
"select u as user, er.dateRegistration from App:EventRegistration er
left join App:User u with er.user = u.id
where er.user != :user_id and
er.event in (select identity(er2.event) from App:EventRegistration er2
where er2.user = :user_id)")
->setParameter('user_id', $user->getId());
return $query->getResult();
Now, in the same query, I'd like to select COUNT how many times each one of the other users has registered in the same event of the current user.
I mean the number of events in common between the current user and the others.
if I add count(er.id) to the select
select u as user, er.dateRegistration, count(er.id)
I get this error :
SQLSTATE[42803]: Grouping error: 7 ERREUR: la colonne « u0_.username » doit apparaître dans la clause GROUP BY ou doit être utilisé dans une fonction d'agrégat
LINE 1: SELECT u0_.username AS username_0, u0_.username_canonical AS..
You need to add group by;
$query = $this->_em->createQuery(
"select u as user, count(er.id) as register_count from App:EventRegistration er
left join App:User u with er.user = u.id
where er.user != :user_id and
er.event in (select identity(er2.event) from App:EventRegistration er2
where er2.user = :user_id) group by u.id")
->setParameter('user_id', $user->getId());
return $query->getResult();
It will give you register count of each user but in this case you can not expose the "er.dateRegistration" because now you are grouping counts for every user.

Keycloak - Get all Users mapped to roles

I know keycloak has exposed below api,
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-services</artifactId>
<version>2.0.0.Final</version>
</dependency>
With complete documentation here. I cannot find the required api here to fetch all users with specific role mapped to them.
Problem Statement - I need to pick all users from keycloak server who have a specific role. I need to send email to all users with role mapped to them.
Based on the documentation it appears to be this API:
GET /{realm}/clients/{id}/roles/{role-name}/users
It is there for a while. In this older version however it was not possible to get more than 100 users this way. It was fixed later and pagination possibility was added.
There is an outstanding feature request asking for this function via the API.
In the meantime if your requirement is once-off you could obtain the user names (or email addresses) by interrogating the database joining KEYCLOAK_ROLE to USER_ROLE_MAPPING to USER_ENTITY
Something like:
SELECT username
FROM keycloak_role kr
JOIN user_role_mapping rm ON kr.id = rm.role_id
JOIN user_entity ue ON rm.user_id = ue.id
WHERE kr.name = 'your_role_name';
This should be now possible with the updated rest endpoint.
Set<UserRepresentation> usersOfRole = realmResource.roles().get(roleName).getRoleUserMembers();
Here is another interesting query, which would also display other useful fields.
SELECT kr_role.REALM_ID 'Realm', cl.CLIENT_ID 'Realm Client',
kr_role.NAME 'Role Name',
kr_role.DESCRIPTION 'Role Description',
user_ent.USERNAME 'Domain ID', user_ent.EMAIL 'Email'
FROM keycloak_role kr_role, user_role_mapping role_map,
user_entity user_ent, client cl
WHERE role_map.USER_ID = user_ent.ID
AND kr_role.ID = role_map.ROLE_ID
AND kr_role.CLIENT = cl.ID
AND cl.REALM_ID = '<realm_name>'
AND cl.CLIENT_ID = '<client_name>'
ORDER BY 1, 2, 3;
If anyone is still searching for a Postgres Query to find information regarding users/roles/groups in keycloak database, I came up with this one lately.
It uses two CTEs to have the groups and roles straight (recursing for groups in groups, because they can nest in arbitrary depth and fetching composite roles with their parents) and a simple UNION for group and direct assignments.
Please note the WHERE clause, where you can limit the realm and different aspects. You can search for
all roles from a specific user (just matching username)
all users, that have a particular role assigned (matching role_name)
everything coming from a specific group (I sometimes use it without the username column in the projection to just see, what roles a group has. Please note the prefix in the group column)
-- flat out GROUPS in GROUPS
WITH RECURSIVE groups AS (
SELECT
g.id,
g.id AS parent_group,
g.name,
g.name AS parent_name,
g.realm_id,
1 AS iter
FROM
keycloak_group g
UNION
SELECT
groups.id,
parents.parent_group,
groups.name,
parents.name,
groups.realm_id,
groups.iter + 1
FROM
groups
INNER JOIN keycloak_group parents ON groups.parent_group = parents.id
),
-- Collect roles and composite roles
roles AS (
SELECT
r.id,
r.name AS role_name,
null AS base_role,
c.client_id
FROM
keycloak_role r
LEFT JOIN client c ON r.client = c.id
UNION
SELECT
r.id,
r2.name,
r.name,
c.client_id
FROM
keycloak_role r
JOIN composite_role cr ON r.id = cr.composite
JOIN keycloak_role r2 ON r2.id = cr.child_role
LEFT JOIN client c ON r.client = c.id
)
SELECT DISTINCT
username,
role_name,
base_role, -- for composite roles
client_id,
source,
realm_name
FROM
(
-- Roles from Groups
SELECT
ue.username,
roles.role_name,
roles.base_role,
roles.client_id,
ue.realm_id,
'group ' || g.name AS source,
realm.name AS realm_name
FROM
user_entity ue
JOIN realm ON ue.realm_id = realm.id
JOIN user_group_membership ugm ON ue.id = ugm.user_id
JOIN groups g ON g.id = ugm.group_id
JOIN group_role_mapping grm ON g.parent_group = grm.group_id
JOIN roles roles ON roles.id = grm.role_id
UNION
-- direct role assignments on User
SELECT
ue.username,
roles.role_name,
roles.base_role,
roles.client_id,
ue.realm_id,
'direct',
realm.name
FROM
user_entity ue
JOIN realm ON ue.realm_id = realm.id
JOIN user_role_mapping urm ON ue.id = urm.user_id
JOIN roles roles ON roles.id = urm.role_id
) AS a
WHERE
realm_name = 'realm_name'
AND (
-- username = 'username'
role_name IN ('roleName')
-- source = 'group GROUPNAME'
)
ORDER BY
username,
role_name
;
This query works from keycloak 9 to 16.1.1 (the last jboss/keycloak version I got from docker hub).
SELECT username,
kr.NAME,
kr.REALM_ID
FROM KEYCLOAK_ROLE kr
JOIN USER_ROLE_MAPPING rm ON kr.id = rm.role_id
JOIN USER_ENTITY ue ON rm.user_id = ue.id
ORDER BY USERNAME,
NAME,
REALM_ID;

SBO Layout bring random record incase of draft or unadded documents

am having a layout which is working fine , except for one part ,
If we use this layout to view or print any added document (AR Invoice) it will ring the write data and records.
but if the user click view or print before adding the document to the system , or if the document was saved as draft then : a random records will come into the layout , in a very randomly way , meaning if close the view and open it again another record different than the precious shown one will come .
What am looking for is simple as i thought , if a user tried to view or print a document thats not added into system then show blank page ,
any help ?
here is the query am using in CR
SELECT ohem.[U_Employee_Code] as'Cashier',OUSR.USER_CODE, OUSR.U_NAME,OUDG.[Code],OUDG.Phone1,OUDG.Phone2, OINV.DocEntry,oinv.docnum, OINV.DocType, OINV.DocStatus, OINV.InvntSttus, OINV.DocDate, OINV.DocDueDate, OINV.CardCode, nnm1.seriesname,
OINV.Address2, OINV.Printed, OINV.CardName, OINV.U_Address, OINV.NumAtCard, OINV.VatSum, OINV.DiscPrcnt, OINV.DiscSum, OINV.DocTotal, OINV.DocRate, OINV.u_deldate,
OINV.ReceiptNum, OINV.GroupNum, OINV.OwnerCode,OINV.JrnlMemo, OINV.VatPaid, OINV.Address2 AS Expr2, OINV.U_CUSTNAME1 as 'Customer Name' , OINV.U_PHONE1, OINV.ExepAmnt, OINV.ExepAmntSC, OINV.ExepAmntFC, OINV.VatDate,OINV.TotalExpns,
OINV.PaidSum, OINV.OwnerCode, OINV.BillToOW, OINV.ShipToOW, OINV.RetInvoice, OINV.ReqName, OINV.Requester, INV1.ItemCode, INV1.Dscription, INV1.Quantity, inv1.PriceBefDi,
INV1.SubCatNum, INV1.ShipDate, INV1.Price, INV1.Currency, INV1.Rate, INV1.DiscPrcnt AS Expr1, INV1.LineTotal, INV1.OpenSum, INV1.VatPrcnt, INV1.PriceAfVAT,
INV1.unitMsr, OINV.TaxDate, INV1.GTotal, OINV.Comments, OCTG.PymntGroup, OINV.LicTradNum, INV1.BaseDocNum, INV1.LineVat, OINV.WTSum, oinv.u_cancelled,
OCRN.CurrName, oslp.SlpName,OUDG.Phone1,OUDG.phone2,
oinv.u_empid as'Sales man',OUDG.Phone1 as 'Branch Phone',OUDG.phone2 as 'Customer Support Phone',OINV.U_PHONE1 as 'Customer Phone',inv1.shipdate as 'Item Delivery Date', OINV.TotalExpns AS 'FREIGHT CHARGES',OINV.U_Address as 'Customer Address',oinv.UpdateDate, OINV.DocEntry
FROM OINV INNER JOIN
INV1 ON OINV.DocEntry = INV1.DocEntry INNER JOIN
OUSR ON OINV.UserSign = OUSR.USERID INNER JOIN
OCTG ON OINV.GroupNum = OCTG.GroupNum INNER JOIN
OCRN ON OINV.DocCur = OCRN.CurrCode inner join
oslp on oinv.SlpCode = oslp.SlpCode INNER JOIN
OUDG ON OUSR.[DfltsGroup] = OUDG.[Code] inner join
nnm1 on OINV.series = nnm1.series LEFT JOIN
OHEM on OUSR.[userId] = OHEM.[USERID]
where OINV.DocEntry = {?DocKey#}
the solution is to use ObjectId to determine if the source is system document or draft . this is it
if {?ObjectId#} =13
SELECT ohem.[U_Employee_Code] as'Cashier',OUSR.USER_CODE, OUSR.U_NAME,OUDG.[Code],OUDG.Phone1,OUDG.Phone2, OINV.DocEntry,oinv.docnum, OINV.DocType, OINV.DocStatus, OINV.InvntSttus, OINV.DocDate, OINV.DocDueDate, OINV.CardCode, nnm1.seriesname,
OINV.Address2, OINV.Printed, OINV.CardName, OINV.U_Address, OINV.NumAtCard, OINV.VatSum, OINV.DiscPrcnt, OINV.DiscSum, OINV.DocTotal, OINV.DocRate, OINV.u_deldate,
OINV.ReceiptNum, OINV.GroupNum, OINV.OwnerCode,OINV.JrnlMemo, OINV.VatPaid, OINV.Address2 AS Expr2, OINV.U_CUSTNAME1 as 'Customer Name' , OINV.U_PHONE1, OINV.ExepAmnt, OINV.ExepAmntSC, OINV.ExepAmntFC, OINV.VatDate,OINV.TotalExpns,
OINV.PaidSum, OINV.OwnerCode, OINV.BillToOW, OINV.ShipToOW, OINV.RetInvoice, OINV.ReqName, OINV.Requester, INV1.ItemCode, INV1.Dscription, INV1.Quantity, inv1.PriceBefDi,
INV1.SubCatNum, INV1.ShipDate, INV1.Price, INV1.Currency, INV1.Rate, INV1.DiscPrcnt AS Expr1, INV1.LineTotal, INV1.OpenSum, INV1.VatPrcnt, INV1.PriceAfVAT,
INV1.unitMsr, OINV.TaxDate, INV1.GTotal, OINV.Comments, OCTG.PymntGroup, OINV.LicTradNum, INV1.BaseDocNum, INV1.LineVat, OINV.WTSum, oinv.u_cancelled,
OCRN.CurrName, oslp.SlpName,OUDG.Phone1,OUDG.phone2,
oinv.u_empid as'Sales man',OUDG.Phone1 as 'Branch Phone',OUDG.phone2 as 'Customer Support Phone',OINV.U_PHONE1 as 'Customer Phone',inv1.shipdate as 'Item Delivery Date', OINV.TotalExpns AS 'FREIGHT CHARGES',OINV.U_Address as 'Customer Address',oinv.UpdateDate
FROM OINV INNER JOIN
INV1 ON OINV.DocEntry = INV1.DocEntry INNER JOIN
OUSR ON OINV.UserSign = OUSR.USERID INNER JOIN
OCTG ON OINV.GroupNum = OCTG.GroupNum INNER JOIN
OCRN ON OINV.DocCur = OCRN.CurrCode inner join
oslp on oinv.SlpCode = oslp.SlpCode INNER JOIN
OUDG ON OUSR.[DfltsGroup] = OUDG.[Code] inner join
nnm1 on OINV.series = nnm1.series LEFT JOIN
OHEM on OUSR.[userId] = OHEM.[USERID]
where OINV.DocEntry = {?DocKey#}
else select ' '

Moodle Student list with Groups and Course

I am using moodle2.4.6. I want a list of user with their groups and their courses.
You will need to do an SQL query to get this information.
Assuming you are using the default 'mdl_' prefix for tables, you will need to join together the following tables:
mdl_user - the details of the users
mdl_user_enrolments - (user_enrolments.userid = user.id) which course enrolments the user
has
mdl_enrol - (enrol.id = user_enrolments.enrolid) details of which
enrolment instances these are
mdl_course - (course.id = enrol.courseid) details of the courses these users are enroled in
mdl_groups_members - (groups_members.userid = user.id) details of the
groups these users are in
mdl_groups - (groups.id =
groups_members.groupid AND groups.courseid = course.id) name and
description of the groups the user is in (for each course)
Please comment if you need help turning that pseudo code into actual SQL, or if you need help with the Moodle database access API ( http://docs.moodle.org/dev/Data_manipulation_API )
SELECT
mdl_user.username,
mdl_user.firstname,
mdl_user.lastname,
mdl_course.fullname,
mdl_course.idnumber,
mdl_groups_members.groupid,
mdl_groups.name
FROM (mdl_groups_members
INNER JOIN ((mdl_course
INNER JOIN (mdl_user_enrolments
INNER JOIN mdl_enrol ON mdl_user_enrolments.enrolid = mdl_enrol.id)
ON mdl_course.id = mdl_enrol.courseid) INNER JOIN mdl_user ON mdl_user_enrolments.userid = mdl_user.id)
ON mdl_groups_members.userid = mdl_user.id) INNER JOIN mdl_groups
ON (mdl_groups.courseid = mdl_course.id) AND (mdl_groups_members.groupid = mdl_groups.id)
WHERE (((mdl_user.username) = "102993") AND ((mdl_course.category) = "36"))

customize the prospects exports of a prospectlist in SugarCrm

I using SugarCRM and when i exports prospects I can get all the fields, including the custom filed I added. but when i try to download the prospects from prospect list then only a few fields are exporting from the database. I need to get all the fields like the prospect download
I understand that Prospect_list.php file contains the code for exporting this one and I changed this one but database failure happening please help me to do this one.
$prospects_query = "SELECT p.id AS id, 'Prospects' AS related_type, '' AS \"name\", p.first_name AS first_name, p.last_name AS last_name,p.title AS title, p.salutation AS salutation,
p.primary_address_street AS primary_address_street,p.primary_address_city AS primary_address_city, p.primary_address_state AS primary_address_state, p.primary_address_postalcode AS primary_address_postalcode, p.primary_address_country AS primary_address_country,
p.account_name AS account_name,
ea.email_address AS email_address, ea.invalid_email AS invalid_email, ea.opt_out AS opt_out, ea.deleted AS ea_deleted, ear.deleted AS ear_deleted, ear.primary_address AS primary_address,
p.do_not_call AS do_not_call, p.phone_fax AS phone_fax, p.phone_other AS phone_other, p.phone_home AS phone_home, p.phone_mobile AS phone_mobile, p.phone_work AS phone_work , p.description As Description
FROM prospect_lists_prospects plp
INNER JOIN prospects p ON plp.related_id=p.id
LEFT JOIN email_addr_bean_rel ear ON ear.bean_id=p.id AND ear.deleted=0
LEFT JOIN email_addresses ea ON ear.email_address_id=ea.id
WHERE plp.prospect_list_id = $record_id AND plp.deleted=0
AND p.deleted=0
AND (ear.deleted=0 OR ear.deleted IS NULL)";
here the sugar team specified the fields to export but i need to get all the fields in the table[prospects] how to do this one.
Trying changing the query to this...
$prospects_query = "SELECT p.id AS id, 'Prospects' AS related_type, '' AS 'name', p.first_name AS first_name, p.last_name AS last_name,p.title AS title, p.salutation AS salutation,
p.primary_address_street AS primary_address_street,p.primary_address_city AS primary_address_city, p.primary_address_state AS primary_address_state, p.primary_address_postalcode AS primary_address_postalcode, p.primary_address_country AS primary_address_country,
p.account_name AS account_name,
ea.email_address AS email_address, ea.invalid_email AS invalid_email, ea.opt_out AS opt_out, ea.deleted AS ea_deleted, ear.deleted AS ear_deleted, ear.primary_address AS primary_address,
p.do_not_call AS do_not_call, p.phone_fax AS phone_fax, p.phone_other AS phone_other, p.phone_home AS phone_home, p.phone_mobile AS phone_mobile, p.phone_work AS phone_work , p.description As Description
FROM prospect_lists_prospects plp
INNER JOIN prospects p ON plp.related_id=p.id
LEFT JOIN email_addr_bean_rel ear ON ear.bean_id=p.id AND ear.deleted=0
LEFT JOIN email_addresses ea ON ear.email_address_id=ea.id
WHERE plp.prospect_list_id = '$record_id' AND plp.deleted=0
AND p.deleted=0
AND (ear.deleted=0 OR ear.deleted IS NULL)";