I need help understanding WHERE the completion state of a resource (file) in Moodle is stored.
Please see attached images for more information.
The flag next to the file is marked as completed once the user viewed
This is setup in the Completion settings of this file/resource.
I need to generate a SQL report showing the file and the completion state.
I already have the "difficult part" of the query, I just need to select the completion state from "table-X" and wha-la!
Thank you.
SELECT r.id, r.name, r.course, cmc.userid, cmc.completionstate, cmc.viewed
FROM mdl_course_modules_completion cmc
JOIN mdl_course_modules cm ON cm.id = cmc.coursemoduleid
JOIN mdl_modules m ON m.id = cm.module AND m.name = 'resource'
JOIN mdl_resource r ON r.id = cm.instance
The completionstate and viewed constants are in /lib/completionlib.php
eg:
COMPLETION_INCOMPLETE = 0
COMPLETION_COMPLETE = 1
COMPLETION_COMPLETE_PASS = 2
COMPLETION_COMPLETE_FAIL = 3
COMPLETION_COMPLETE_RPL = 4 // This is used in Totara.
COMPLETION_NOT_VIEWED = 0
COMPLETION_VIEWED = 1
Okay, so I found the table and column.
Table: mdl_course_modules_completion
Column: Viewed
I will post my report code below, hopefully this might help the next guy.
Take Note: I joined all the module types to the query and filter only for type quiz, lesson and resource in the where statement. I did this because I am only interested in these 3 types. I however did not remove the joins because someone else might need the code.
SELECT DISTINCT
u.firstname AS 'Firstname'
,u.lastname AS 'Lastname'
,u.institution AS 'Institution'
,u.department AS 'Department'
,u.city AS 'City/Site'
,cc.name AS 'Course'
,c.fullname AS 'Module'
,CASE
WHEN mf.name IS NOT NULL THEN mf.name
WHEN mb.name IS NOT NULL THEN mb.name
WHEN mr.name IS NOT NULL THEN mr.name
WHEN mu.name IS NOT NULL THEN mu.name
WHEN mq.name IS NOT NULL THEN mq.name
WHEN mp.name IS NOT NULL THEN mp.name
WHEN ml.name IS NOT NULL THEN ml.name
ELSE NULL
END AS activityname
,CASE WHEN mdl.name = 'lesson' THEN CASE WHEN mlg.id IS NOT NULL AND mlg.completed IS NOT NULL THEN 'Complete' ELSE 'Incomplete' END
WHEN mdl.name = 'quiz' THEN CASE WHEN mqg.id IS NOT NULL AND mqg.timemodified IS NOT NULL THEN 'Complete' ELSE 'Incomplete' END
WHEN mdl.name = 'resource' THEN CASE WHEN cmc.viewed = 1 THEN 'Complete' ELSE 'Incomplete' END
END AS Status
FROM
mdl_user u
JOIN mdl_user_enrolments ue ON ue.userid = u.id
JOIN mdl_enrol E on E.id = ue.enrolid
JOIN mdl_course c ON c.id = E.courseid
JOIN mdl_course_categories cc ON c.category = cc.id
JOIN mdl_course_modules cm ON cm.course = c.id
JOIN mdl_course_modules_completion cmc ON cmc.coursemoduleid = cm.id
JOIN mdl_context AS ctx ON ctx.contextlevel = 70 AND ctx.instanceid = cm.id
JOIN mdl_modules AS mdl ON cm.module = mdl.id
LEFT JOIN mdl_forum AS mf ON mdl.name = 'forum' AND cm.instance = mf.id
LEFT JOIN mdl_book AS mb ON mdl.name = 'book' AND cm.instance = mb.id
LEFT JOIN mdl_resource AS mr ON mdl.name = 'resource' AND cm.instance = mr.id
LEFT JOIN mdl_url AS mu ON mdl.name = 'url' AND cm.instance = mu.id
LEFT JOIN mdl_quiz AS mq ON mdl.name = 'quiz' AND cm.instance = mq.id
LEFT JOIN mdl_quiz_grades mqg ON mqg.quiz = mq.id
LEFT JOIN mdl_page AS mp ON mdl.name = 'page' AND cm.instance = mp.id
LEFT JOIN mdl_lesson AS ml ON mdl.name = 'lesson' AND cm.instance = ml.id
LEFT JOIN mdl_lesson_grades mlg ON mlg.lessonid = ml.id
LEFT JOIN mdl_files AS f ON f.contextid = ctx.id
LEFT JOIN mdl_files_reference fr ON fr.id = f.referencefileid
WHERE mdl.name in ('quiz','lesson','resource')
ORDER BY firstname,Lastname,cc.name,c.fullname
Related
Log History:
2020/04/13 22:12:36 Completed
Ingestion completed in 9 hours 22 minutes 1 seconds.
Rows dropped: 0, Rows ingested: 1916870
Manual, Full refresh
SELECT
uc.name,
u.username AS usuario,
u.firstname,
u.lastname,
c.fullname AS titulo_bit,
CASE
WHEN EXISTS(SELECT ci.id
FROM mdl_certificate_issues ci
LEFT JOIN mdl_certificate ce ON (ci.certificateid = ce.id)
WHERE ci.userid = u.id
AND ce.course = c.id)
THEN 100
ELSE
ROUND(COUNT(cmc.completionstate) * 100 / (SELECT COUNT(*)
FROM mdl_course_modules
WHERE course = cm.course
AND deletioninprogress IS false
AND completion IS true))
END AS porcentaje_avance_bit,
uai.company_area AS area_old,
u.department,
u.address,
u.institution,
muca.name AS area,
FROM_UNIXTIME(cmc.timemodified) as course_progress_time
FROM mdl_course_modules_completion cmc
LEFT JOIN mdl_course_modules cm ON (cmc.coursemoduleid = cm.id)
LEFT JOIN mdl_course c ON (cm.course = c.id)
LEFT JOIN mdl_user u ON (cmc.userid = u.id)
LEFT JOIN mdl_u_user_additional_info uai ON (u.id = uai.mdl_user_id)
LEFT JOIN mdl_u_company_area muca ON uai.company_area_id = muca.id
RIGHT JOIN mdl_u_company uc ON (uai.mdl_u_company_id = uc.id)
WHERE cm.deletioninprogress IS false
AND cm.completion IS true
AND cmc.completionstate != 0
AND u.deleted IS FALSE
AND c.visible IS TRUE
GROUP BY cm.course, cmc.userid, uc.name, uai.company_area, muca.id, FROM_UNIXTIME(cmc.timemodified)
ORDER BY usuario, course_progress_time desc
I have the following (simplified) query:
SELECT
u.idnumber AS user_idnumber,
sst_status.value AS scorm_status,
sst_starttime.value AS scorm_starttime,
sst_duration.value AS scorm_duration,
sst_score.value AS scorm_score,
u.id as uid,
s.id as scormid,
ss.id as ssid
FROM {user} u
LEFT JOIN {prog_user_assignment} prua ON prua.userid = u.id
LEFT JOIN {prog} pr ON prua.programid = pr.id
LEFT JOIN {prog_courseset} prcs ON prcs.programid = pr.id
LEFT JOIN {prog_courseset_course} prcsc ON prcsc.coursesetid = prcs.id
LEFT JOIN {course} c ON prcsc.courseid = c.id
LEFT JOIN {scorm} s ON s.id = cm.instance
LEFT JOIN {scorm_scoes} ss ON ss.scorm = s.id
LEFT JOIN {scorm_scoes_track} sst_status
ON sst_status.userid = u.id
AND sst_status.scormid = ss.scorm
AND sst_status.element = 'cmi_core.lesson_status'
AND sst_status.scoid = ss.id
LEFT JOIN {scorm_scoes_track} sst_starttime
ON sst_starttime.userid = u.id
AND sst_starttime.scormid = ss.scorm
AND sst_starttime.element = 'x.start.time'
AND sst_starttime.scoid = ss.id
LEFT JOIN {scorm_scoes_track} sst_duration
ON sst_duration.userid = u.id
AND sst_duration.scormid = ss.scorm
AND sst_duration.element = 'cmi.core.total_time'
AND sst_duration.scoid = ss.id
LEFT JOIN {scorm_scoes_track} sst_score
ON sst_score.userid = u.id
AND sst_score.scormid = ss.scorm
AND sst_score.element = 'cmi.core.score.raw'
AND sst_score.scoid = ss.id
WHERE u.id IN([SOME_EXAMPLE_IDS_HERE]) AND u.deleted = 0
group by u.idnumber, c.id
Now the problem is that without the GROUP BY clause, some of the records have values for scorm_status, scorm_starttime etc. However, these contain duplicates. But, with the group by statement, only NULL values are returned for these columns. Does anyone have a clue on how to resolve this?
Using GROUP_CONCAT and SUBSTRING_INDEX:-
SELECT
u.idnumber AS user_idnumber,
SUBSTRING_INDEX(GROUP_CONCAT(sst_status.value), ',', 1) AS scorm_status,
SUBSTRING_INDEX(GROUP_CONCAT(sst_starttime.value), ',', 1) AS scorm_starttime,
SUBSTRING_INDEX(GROUP_CONCAT(sst_duration.value), ',', 1) AS scorm_duration,
SUBSTRING_INDEX(GROUP_CONCAT(sst_score.value), ',', 1) AS scorm_score,
u.id as uid,
s.id as scormid,
ss.id as ssid
FROM {user} u
LEFT JOIN {prog_user_assignment} prua ON prua.userid = u.id
LEFT JOIN {prog} pr ON prua.programid = pr.id
LEFT JOIN {prog_courseset} prcs ON prcs.programid = pr.id
LEFT JOIN {prog_courseset_course} prcsc ON prcsc.coursesetid = prcs.id
LEFT JOIN {course} c ON prcsc.courseid = c.id
LEFT JOIN {scorm} s ON s.id = cm.instance
LEFT JOIN {scorm_scoes} ss ON ss.scorm = s.id
LEFT JOIN {scorm_scoes_track} sst_status
ON sst_status.userid = u.id
AND sst_status.scormid = ss.scorm
AND sst_status.element = 'cmi_core.lesson_status'
AND sst_status.scoid = ss.id
LEFT JOIN {scorm_scoes_track} sst_starttime
ON sst_starttime.userid = u.id
AND sst_starttime.scormid = ss.scorm
AND sst_starttime.element = 'x.start.time'
AND sst_starttime.scoid = ss.id
LEFT JOIN {scorm_scoes_track} sst_duration
ON sst_duration.userid = u.id
AND sst_duration.scormid = ss.scorm
AND sst_duration.element = 'cmi.core.total_time'
AND sst_duration.scoid = ss.id
LEFT JOIN {scorm_scoes_track} sst_score
ON sst_score.userid = u.id
AND sst_score.scormid = ss.scorm
AND sst_score.element = 'cmi.core.score.raw'
AND sst_score.scoid = ss.id
WHERE u.id IN([SOME_EXAMPLE_IDS_HERE]) AND u.deleted = 0
group by u.idnumber, c.id
This is getting the first non null values of these fields. Note that this is relying on the fields not containing commas (but easy enough to specify a different separator for the GROUP_CONCAT). You can specify an order for the GROUP_CONCAT to get your preferred value if required.
Here is a very simplified version of your query:
select u.idnumber AS user_idnumber, sst_status.value AS scorm_status,
sst_starttime.value AS scorm_starttime, sst_duration.value AS scorm_duration,
sst_score.value AS scorm_score, u.id as uid, s.id as scormid, ss.id as ssid
. . .
group by u.idnumber, c.id;
You will notice that most of the columns in the select are not in the group by. This is using a (mis)feature of MySQL and would not generally be allowed in other databases.
What MySQL does is it chooses an abritrary value from one of the rows for the result. The idea is that this works nicely if the column is constant for the group. But, it doesn't usually work as expected in other circumstances.
If you want a non-NULL value, then you can use max() or min() to get the biggest or smallest value.
For instance:
min(sst_status.value) AS scorm_status
and so on.
I don't know if this is possible but I'm trying to make a select queries with a couple of JOINS with a WHERE statement. What I need is a field based on a particular WHERE and another field with different WHERE.
Current SQL query:
SELECT SUM(a.totaltime), b.user_name, MONTHNAME(a.date)
FROM a
JOIN c on c.id = a.id
JOIN b on b.userid = c.userid
LEFT JOIN d on a.projid = d.projectid
LEFT JOIN e on a.account_id = e.salesorderid
LEFT JOIN f on e.salesorderid = f.salesorderid
LEFT JOIN g on d.projectid = g.projectid
WHERE c.deleted != "1" AND YEAR(DATE(NOW())) AND ( f.cf_991 = '1' OR g.cf_990 = '1')
group by user_name,MONTHNAME(a.date);
This works fine but I need to include another field this time WHERE would be:
WHERE c.deleted != "1" AND YEAR(DATE(NOW())) AND ( f.cf_991 = '0' OR g.cf_990 = '0')
Any help is greatly appreciated thanks
EDIT:
CURRENT OUTPUT IS:
total_time username MONTHNAME(a.date)
22.5 admin April
21 admin June
15 max April
So the above is based on f.cf_991 = '1' OR g.cf_990 = '1'
What I need is this:
total_time username MONTHNAME(a.date) total_time_2
22.5 admin April 5
21 admin June 9
15 max April 13
total_time_2 is based on f.cf_991 = '0' OR g.cf_990 = '0'.
SELECT SUM(CASE WHEN f.cf_991 = '1' OR g.cf_990 = '1' THEN a.totaltime END) as total_time
, b.user_name, MONTHNAME(a.date)
, SUM(CASE WHEN f.cf_991 = '0' OR g.cf_990 = '0' THEN a.totaltime END) as total_time_2
FROM a
JOIN c on c.id = a.id
JOIN b on b.userid = c.userid
LEFT JOIN d on a.projid = d.projectid
LEFT JOIN e on a.account_id = e.salesorderid
LEFT JOIN f on e.salesorderid = f.salesorderid
LEFT JOIN g on d.projectid = g.projectid
WHERE c.deleted != "1"
AND YEAR(CURDATE()) = a_column_containing_a_date
AND (
( f.cf_991 = '1' OR g.cf_990 = '1')
OR
( f.cf_991 = '0' OR g.cf_990 = '0')
)
group by user_name,MONTHNAME(a.date);
Just try this
SELECT
SUM(if(f.cf_991 = '1' || g.cf_990 = '1',a.totaltime,0)),
SUM(if(f.cf_991 = '0' || g.cf_990 = '0',a.totaltime,0)),
b.user_name, MONTHNAME(a.date) FROM a
JOIN c on c.id = a.id
JOIN b on b.userid = c.userid
LEFT JOIN d on a.projid = d.projectid
LEFT JOIN e on a.account_id = e.salesorderid
LEFT JOIN f on e.salesorderid = f.salesorderid
LEFT JOIN g on d.projectid = g.projectid
WHERE c.deleted != "1" AND YEAR(DATE(NOW()))
and (( f.cf_991 = '1' OR g.cf_990 = '1') or( f.cf_991 = '0' OR g.cf_990 = '0') )
group by ser_name,MONTHNAME(a.date);
I've been at this thing for hours. Does anyone have any ideas on how to speed this query up?
SELECT SQL_CALC_FOUND_ROWS
mdl_course.fullname as course_name,
mdl_course.idnumber as course_length,
mdl_user.firstname as firstname,
mdl_user.lastname as lastname,
case when mdl_course_completions.timecompleted IS NOT NULL
then FROM_UNIXTIME(mdl_course_completions.timecompleted,'%m/%d/%Y') else 'N/A' end AS date_completed,
mdl_cohort.name AS group_name,
mdl_course_categories.name as category_name,
case when mdl_course_completions.timecompleted IS NOT NULL then 'Completed' else
'Incomplete' end AS completion_status
FROM mdl_enrol
JOIN mdl_user_enrolments ON mdl_user_enrolments.enrolid = mdl_enrol.id
JOIN mdl_course ON mdl_course.id = mdl_enrol.courseid
JOIN mdl_user ON mdl_user.id = mdl_user_enrolments.userid AND mdl_user.deleted = 0
JOIN mdl_cohort_members ON mdl_cohort_members.userid = mdl_user.id
JOIN mdl_cohort ON mdl_cohort.id = mdl_cohort_members.cohortid
JOIN mdl_course_categories ON mdl_course_categories.id = mdl_course.category
JOIN mdl_course_completions ON mdl_course_completions.course = mdl_course.id
AND mdl_course_completions.userid = mdl_user.id
AND mdl_course_completions.deleted IS NULL
GROUP BY mdl_course_completions.id
ORDER BY mdl_course.fullname ASC
mdl_course_completions has over 100,000 rows in it and is slowing everything down.
I have this query.
SELECT notes.id,enter.name as 'enter_name',step.title as 'flow status',notes.user_name as user_created,notes.created,notes.rel_client_id,td_doc_nr.value_string as 'document number',enter.enter_code,
IF(!ISNULL(td_doc_nr.value_string),
(SELECT GROUP_CONCAT(product_name SEPARATOR ',') from notes d
join note_bundles b on b.note_id = d.id
join note_products p on p.doc_bundle_id = b.id
join note_product_get_fields f on f.doc_product_id = p.id
join note_product_get_field_data fd on fd.get_field_id = f.id
where d.doc_nr = td_doc_nr.value_string
and value_string ='auto')
,NULL) as test
FROM notes notes
JOIN notes_steps step ON step.id = notes.step_id
JOIN notes_enters enter ON enter.id = notes.enter_id
LEFT JOIN notes_custom_fields tf_doc_nr ON tf_doc_nr.name = 'note_number' AND tf_doc_nr.rel_entity_id = enter.id
LEFT JOIN notes_custom_field_data td_doc_nr ON td_doc_nr.rel_entity_id = notes.id AND
td_doc_nr.field_instance_id = tf_doc_nr.id
WHERE notes.enter_id in (777) AND notes.status = 1
I added this subquery to the 'if statement'
SELECT GROUP_CONCAT(product_name SEPARATOR ',') from nontes d
join note_bundles b on b.note_id = d.id
join note_products p on p.doc_bundle_id = b.id
join note_product_get_fields f on f.doc_product_id = p.id
join note_product_get_field_data fd on fd.get_field_id = f.id
where d.doc_nr = 'G7777777'
and value_string ='auto'
After this I added a new column.
SELECT GROUP_CONCAT(product_name SEPARATOR ','),GROUP_CONCAT(DISTINCT b.msisdn SEPARATOR ',') from notes d
join note_bundles b on b.note_id = d.id
join note_products p on p.doc_bundle_id = b.id
join note_product_get_fields f on f.doc_product_id = p.id
join note_product_get_field_data fd on fd.get_field_id = f.id
where d.doc_nr = 'G7777777'
and value_string ='auto'
It returns two columns.
How can I return two columns?Is it possible? :) Thanks
A subquery inside an IF statement can't return multiple columns. You will need to join the subquery into the results, and pull out the two separate columns individually:
SELECT ...
IF(!ISNULL(td_doc_nr.value_string), sub.one, NULL) as one,
IF(!ISNULL(td_doc_nr.value_string), sub.two, NULL) as two
FROM ...
LEFT JOIN (
SELECT d.doc_nr, GROUP_CONCAT(product_name SEPARATOR ','),GROUP_CONCAT(DISTINCT b.msisdn SEPARATOR ',') from documents d
join document_bundles b on b.document_id = d.id
join document_products p on p.doc_bundle_id = b.id
join document_product_cstm_fields f on f.doc_product_id = p.id
join document_product_cstm_field_data fd on fd.cstm_field_id = f.id
where value_string ='auto'
group by d.doc_nr
) sub on sub.doc_nr = td_doc_nr.value_string
A correlated subquery inside an IF statement can only return 1 column and 1 row, this is why you are getting the error. However, looking over your query the only outer reference inside the subquery is
d.doc_nr = td_doc_nr.value_string
So you do not need actually need a correlated subquery and you can achieve the same result by moving the subquery to a join and grouping by doc_nr within the subquery, which will probably be much more efficient, and it will allow you to return the 2 columns you want:
SELECT tickets.id,
source.name as 'source_name',
flow_stage.title as 'flow status',
tickets.user_name as user_created,
tickets.created,
tickets.rel_client_id,
td_doc_nr.value_string as 'document number',
source.source_code,
IF(!ISNULL(td_doc_nr.value_string), ProductNames, NULL) as test,
d.MSISDNS
FROM tickets tickets
JOIN tickets_flow_stages flow_stage
ON flow_stage.id = tickets.flow_stage_id
JOIN tickets_sources source
ON source.id = tickets.source_id
LEFT JOIN tickets_custom_fields tf_doc_nr
ON tf_doc_nr.name = 'document_number'
AND tf_doc_nr.rel_entity_id = source.id
LEFT JOIN tickets_custom_field_data td_doc_nr
ON td_doc_nr.rel_entity_id = tickets.id
AND td_doc_nr.field_instance_id = tf_doc_nr.id
LEFT JOIN
( SELECT d.Doc_nr,
GROUP_CONCAT(product_name SEPARATOR ',') AS ProductNames,
GROUP_CONCAT(DISTINCT b.msisdn SEPARATOR ',') AS MSISDNS
from documents d
INNER JOIN document_bundles b
ON b.document_id = d.id
INNER JOIN document_products p
ON p.doc_bundle_id = b.id
INNER JOIN document_product_cstm_fields f
ON f.doc_product_id = p.id
INNER JOIN document_product_cstm_field_data fd
ON fd.cstm_field_id = f.id
WHERE value_string ='auto'
GROUP BY d.Doc_nr
) d
ON d.doc_nr = td_doc_nr.value_string
WHERE tickets.source_id IN (114,122,125,129,131)
AND tickets.status = 1