1

I need help understanding WHERE the completion state of a resource (file) in Moodle is stored.

Please see attached images for more information.

  1. The flag next to the file is marked as completed once the user viewed View of resource/file
  2. This is setup in the Completion settings of this file/resource.

Completion setup

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.

2 Answers2

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
1
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
Russell England
  • 9,436
  • 1
  • 27
  • 41