I am using Moodle 3.11.
I have a custom course field named discount.
how can i fetch names of all the course that provides discount on a new page?
You could use this query for Moodle - replace d.value with the required field
$sql = 'SELECT c.id AS courseid, c.fullname AS coursename, d.value AS discount
FROM mdl_customfield_category cat
JOIN mdl_customfield_field f ON f.categoryid = cat.id AND f.shortname = :shortname
JOIN mdl_customfield_data d ON d.fieldid = f.id
JOIN mdl_course c ON c.id = d.instanceid
WHERE cat.area = :area'
$courses = $DB->get_records_sql($sql, array('area' => 'course' 'shortname' => 'discount'));
for each ($courses as $course) {
// Display $course->fullame and $course->discount
}
Or this for Totara
$sql = 'SELECT c.id AS courseid, c.fullname, d.data AS discount
FROM {course_info_field} f
JOIN {course_info_data} d ON d.fieldid = f.id AND d.data > ''
JOIN {course} c ON c.id = d.courseid
WHERE f.shortname = :shortname'
$courses = $DB->get_records_sql($sql, array('shortname' => 'discount'));
for each ($courses as $course) {
// Display $course->fullame and $course->discount
}
Related
I have this query:
with users_having_connected as (
select u.id as user_id,
(a.connected_at is not null) has_connected
from core_user u
join core_profile p on u.id = p.user_id
join core_conversation c on (c.profile1_id = p.id or c.profile2_id = p.id)
join analytics_connection a on c.id = a.conversation_id
group by u.id, (a.connected_at is not null)
)
select u.id as user_id,
date_trunc('month', u.created at time zone 'UTC')::date as month,
p.community_id,
p.organization_id,
p.profile_type_intention,
(p.basic_account_completed and (p.is_mentor or p.is_entrepreneur)) as profile_is_completed,
exists(select 1 from core_message where core_message.sender_id = p.id) as has_sent_a_message,
(EXISTS (SELECT 1 FROM core_message WHERE core_message.receiver_id = p.id)) AS has_received_a_message,
(EXISTS (SELECT 1 FROM core_admin_conversation_w_resp WHERE core_admin_conversation_w_resp.initiator_id = p.id or core_admin_conversation_w_resp.responder_id = p.id)) AS has_one_by_one,
exists(select 1 from users_having_connected where user_id = u.id) as has_two_by_two
from core_user as u
join core_profile p on u.id = p.user_id
where
p.profile_type_intention is not null
Which worked fine until I added this line:
(EXISTS (SELECT 1 FROM core_admin_conversation_w_resp WHERE core_admin_conversation_w_resp.initiator_id = p.id or core_admin_conversation_w_resp.responder_id = p.id)) AS has_one_by_one,
This specific variable add causes this query to infinite loop. What do I need to do to fix it? Am I missing a set of parentheses somewhere?
How can I write a linq to entities query that includes a group by and a having clause?
For example in SQL:
SELECT * FROM dbo.tblParent p
INNER JOIN
(
SELECT a.ID
FROM dbo.tblParent a
join dbo.tblChild c ON a.ID = c.FkParentID
WHERE a.ColValue = 167
GROUP BY A.ID
HAVING COUNT(c.ID) = 1
) t ON p.ID = t.ID
I found my own answer.
// this is far from pretty but it works as far as I can tell
Apps = (from x in context.tblParents
join t in (
from p in context.tblParents
join c in context.tblChilds
on p.ID equals c.FkParentID
where p.ColValue == 167
group c.ID by p.ID into grouped
where grouped.Count() == 1
select new { grouped.Key }) on x.ID equals t.Key
select x);
the values returned in column a.user_id are id numbers. I would like to return them as a name or initials e.g. 1 = Chris O or C.ONeill
I have tried CASE function but did not know where to put it
SELECT DISTINCT
sb.start_time::date AS shift_date,
i.title AS industry,
wl.name AS venue_name,
w.first_name,
w.last_name,
MIN(cs.start_time::date) AS first_completed_shift,
i.title AS industry,
w.interviewed_on AS induction_date,
d.issue_date AS edbs_issue_date,
d.created_at AS date_dbs_added,
a.user_id --IS IT POSSIBLE TO GET THESE AS VALUES,e.g. a.user_id '1' = 'Chris O or C.ONeill' USING USER NAMES INSTEAD OF USER ID?
FROM shift_bookings sb
JOIN jobs j ON sb.job_id = j.id
JOIN listings l ON j.listing_id = l.id
JOIN work_locations wl ON l.venue_id = wl.id
JOIN workers w ON sb.worker_id = w.id
JOIN completed_shifts cs ON w.id = cs.worker_id
JOIN documents d ON w.id = d.documentable_id
JOIN audits a ON d.id = a.auditable_id
JOIN industries i ON j.industry_id = i.id
WHERE sb.shift_id IN (253106)
AND d.document_type_id = 33
AND a.auditable_type = 'Document'
GROUP BY
sb.start_time::date,
wl.name,
w.first_name,
w.last_name,
i.title,
w.interviewed_on,
d.issue_date,
d.created_at,
a.user_id
a.user_id output will be something else I can set
I have the following SQL query which I am trying to translate to a LINQ query:
SELECT
CustomerId,
SUM(Bills.BillAmount) AS BillAmountTotal,
SUM(COALESCE(Pay.Paid, 0)) AS [Payments]
FROM
Bills
LEFT OUTER JOIN
(SELECT
BillId,
SUM(PaymentAmount) AS [Paid]
FROM
BillPayments
GROUP BY
BillId) Pay ON Pay.BillId = Bills.Id
GROUP BY
CustomerId
I solved it using the following code :
var pay = from p in db.BillPayments
group p by new { p.BillId } into g
select new
{
payKey = g.Key,
TotalPayments = g.Sum(p => p.PaymentAmount)
};
var query = from c in db.Customers
join b in db.Bills on c.Id equals b.CustomerId
join p in pay on b.Id equals p.payKey.BillId into cs
from xx in cs.DefaultIfEmpty()
group new { c, b, xx } by new { c.Name, c.MobilePhone, c.Id, c.CustomerCode } into g
select new
{
Received = g.Key,
TotalPayment = g.Sum(p => p.xx.TotalPayments == null ? 0 : p.xx.TotalPayments),
TotlalBilling = g.Sum(p => p.b.BillAmount),
GrandTotal = g.Sum(p => p.b.BillAmount) - g.Sum(p => p.xx.TotalPayments == null ? 0 : p.xx.TotalPayments)
};
SELECT ra.id,
DATE_FORMAT(FROM_UNIXTIME(ra.timemodified),'%d-%m-%Y') AS Enrolment_Date,
c.fullname,
IFNULL(cmc.completed, 0) as 'Activities Completed',
u.firstname,
u.lastname,
DATE_FORMAT(FROM_UNIXTIME(cc.timecompleted),'%d-%m-%Y') AS complete_date,
CONCAT('<a target="_new" href="%%WWWROOT%%/user/profile.php?id=',ra.userid,'">', u.firstname, ' ', u.lastname, '</a>') AS Username
FROM prefix_role_assignments AS ra
JOIN prefix_user u ON ra.userid = u.id
JOIN prefix_context AS ctx ON ctx.id = ra.contextid
JOIN prefix_course c ON c.id = ctx.instanceid
JOIN prefix_enrol e ON e.courseid = c.id
JOIN prefix_user_enrolments ue ON ue.enrolid = e.id AND ue.userid = u.id
LEFT JOIN prefix_course_completions cc ON cc.course = c.id AND cc.userid = u.id
LEFT JOIN prefix_grade_items gi ON gi.courseid = c.id AND gi.itemtype = 'course'
LEFT JOIN prefix_grade_grades g ON g.itemid = gi.id AND g.userid =u.id
LEFT JOIN (SELECT cmc.userid, cm.course, COUNT(cmc.id) as completed FROM prefix_course_modules_completion cmc, prefix_course_modules cm WHERE cm.visible = 1 AND cmc.coursemoduleid = cm.id AND cmc.completionstate IN ('1','2') GROUP BY cm.course, cmc.userid) cmc ON cmc.course = c.id AND cmc.userid = u.id
WHERE u.id > 0 AND (c.id IN(
SELECT DISTINCT(ctx.instanceid) as id
FROM prefix_role_assignments ra JOIN prefix_context ctx ON ra.contextid = ctx.id
WHERE ra.userid = %%USERID%% AND ctx.contextlevel = 50 AND ra.roleid IN ('1','9','2','3','4','10'))
OR c.id IN(
SELECT distinct(c.id) as id FROM prefix_role_assignments ra
JOIN prefix_context ctx ON ra.contextid = ctx.id
JOIN prefix_course c ON c.category = ctx.instanceid
WHERE ra.userid = %%USERID%% AND ctx.contextlevel = 40 AND ra.roleid IN ('1','9','2','3','4','10'))
) AND u.deleted = 0 AND u.suspended = 0 AND u.username <> 'guest' AND c.visible = 1 AND ue.status = 0 AND e.status = 0 AND ra.roleid = '5'
GROUP BY ra.userid, ctx.instanceid HAVING u.firstname LIKE 'student' ESCAPE '\\'
Hi all,
Im struggling here, I cant seem to get how to only return results from the above query where the user running the query (%%USERID%%) shares the same groups as the student. This is a query cobbled together from a number of other queries and works perfectly except for that one thing.
Ive tried to tack on the following in various ways into this but cant get it to work at all
SELECT g.name, gm.groupid, u.firstname
FROM mdl_groups as g
JOIN mdl_groups_members as gm ON g.id = gm.groupid
JOIN mdl_user as u ON u.id = gm.userid AND u.id = 'student', %%USERID%%'
Any help at all even a point in the right direction would be great, this isnt my job role as such but Ive been tasked to make something like this happen.
Thanks for reading
It seems that the subquery boils down to just some userids, but this needs to return rows.
SELECT DISTINCT
gms.userid
FROM mdl_groups_members gms
INNER JOIN mdl_groups_members AS gmu ON gms.groupid = gmu.groupid
WHERE gmu.userid = %%USERID%%
AND gms.userid LIKE 'student%'
If it does then it needs to link to the correct place. Now his is quiet a leap, but and it "might" be used this way: (select * is not recommended, just an abbreviation):
SELECT
*
FROM prefix_role_assignments AS ra
INNER JOIN prefix_user u ON ra.userid = u.id
INNER JOIN (
SELECT DISTINCT
gms.userid
FROM mdl_groups_members gms
INNER JOIN mdl_groups_members AS gmu ON gms.groupid = gmu.groupid
WHERE gmu.userid = %%USERID%%
AND gms.userid LIKE 'student%'
) sg ON ra.userid = sg.userid
INNER JOIN prefix_context AS ctx ON ctx.id = ra.contextid
INNER JOIN prefix_course c ON c.id = ctx.instanceid
INNER JOIN prefix_enrol e ON e.courseid = c.id
INNER JOIN prefix_user_enrolments ue ON ue.enrolid = e.id AND ue.userid = u.id
LEFT JOIN prefix_course_completions cc ON cc.course = c.id AND cc.userid = u.id
LEFT JOIN prefix_grade_items gi ON gi.courseid = c.id AND gi.itemtype = 'course'
LEFT JOIN prefix_grade_grades g ON g.itemid = gi.id AND g.userid = u.id
LEFT JOIN (
SELECT
cmc.userid
, cm.course
, COUNT(cmc.id) AS completed
FROM prefix_course_modules_completion cmc
, prefix_course_modules cm
WHERE cm.visible = 1
AND cmc.coursemoduleid = cm.id
AND cmc.completionstate IN ('1', '2')
GROUP BY
cm.course
, cmc.userid
) cmc ON cmc.course = c.id AND cmc.userid = u.id
WHERE u.id > 0
AND (
c.id IN (
SELECT DISTINCT
(ctx.instanceid) AS id
FROM prefix_role_assignments ra
INNER JOIN prefix_context ctx ON ra.contextid = ctx.id
WHERE ra.userid = %%USERID%% AND ctx.contextlevel = 50
AND ra.roleid IN ('1', '9', '2', '3', '4', '10')
)
OR c.id IN (
SELECT DISTINCT
(c.id) AS id
FROM prefix_role_assignments ra
INNER JOIN prefix_context ctx ON ra.contextid = ctx.id
INNER JOIN prefix_course c ON c.category = ctx.instanceid
WHERE ra.userid = %%USERID%% AND ctx.contextlevel = 40
AND ra.roleid IN ('1', '9', '2', '3', '4', '10')
)
)
AND u.deleted = 0
AND u.suspended = 0
AND u.username <> 'guest'
AND c.visible = 1
AND ue.STATUS = 0
AND e.STATUS = 0
AND ra.roleid = '5'
GROUP BY
ra.userid
, ctx.instanceid
HAVING u.firstname LIKE 'student%' ESCAPE '\\'