Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added alternative SQL query

...

  1. Right-click the Course Enrollments Data Source and select Edit. The Edit Data Source page opens.

  2. Underneath the Data Source Design heading, click the Edit with Data Source Designer link. The Data Source Designer opens to the Display tab.

  3. Navigate to the Derived Tables tab.

  4. In the Query ID field, enter the name "der_custom_user_fields" or "der_custom_course_fields" depending on which custom profile field you intend to add.

  5. In the Query field, you will need a add an SQL statement. Use one of the following formulas:

    Custom User Profile SQL

    Code Block
    languagesql
    themeConfluence
    SELECT ud.iduserid AS userid,
            MAX(CASE WHEN f.shortname = 'Field1 Short Name' THEN d.data ELSE '' END) AS FieldName1,
            MAX(CASE WHEN f.shortname = 'Field2 Short Name' THEN d.data ELSE '' END) AS FieldName2
    FROM mdl_user u
    JOIN mdl_user_info_data d ON
    d.userid = u.id
    JOIN mdl_user_info_field f ON d.fieldid = f.id
    GROUP BY ud.iduserid


    Custom Course Profile SQL

    Code Block
    languagesql
    themeConfluence
    SELECT ud.idcourseid AS courseid,
           MAX(CASE WHEN f.shortname = 'Field1 Short Name' THEN d.data ELSE '' END) AS FieldName1,
           MAX(CASE WHEN f.shortname = 'Field2 Short Name' THEN d.data ELSE '' END) AS FieldName2
    FROM mdl_course u
    JOIN mdl_course_info_data d
    ON d.courseid = u.id
    JOIN mdl_course_info_field f ON d.fieldid = f.id
    GROUP BY ud.idcourseid



    The Field Short Name is the short name given to the course/user profile field in Moodle/Totara:


     
  6. Click Run Query.

  7. If your query runs successfully, you will need to select from the returned results. Select the entire list by holding Command/Shift/CTRL and selecting each field.

  8. Click Save Table. Your new Derived Table will appear in the Available Objects panel with the unique Derived Table / Calculated Field identifier.

  9. Navigate to the Joins tab and create one of the following joins, depending on your query:

    • From the Left Table, select the id field from the mdl_user table, then from the Right Table select the userid field from the der_custom_user_fields Derived Table. Click the Left Outer Join button.

    • Or, from the Left Table, select the id field from the mdl_course table, then from the Right Table select the courseid field from the der_custom_course_fields Derived Table. Click the Left Outer Join button.

  10. Navigate to the Display tab.

  11. In the Resources panel, select to View as: Join Tree.

  12. Expand JoinTree_1, then expand the der_custom_user_fields / der_custom_course_fields table:

  13. Select the relevant field(s) and drag it into the appropriate set in the Sets and Items panel.

  14. Click the OK button to save your changes in the Data Source Designer and return to the Edit Data Source page.

  15. Under the Required Information heading, rename the Course Enrollments Data Source in the Name field, in order to avoid confusing it with the Public Course Enrollments Data Source. We will call this Data Source the "CompanyABC Course Enrollments Data Source".

  16. To save your changes to the Data Source, click the Submit button.

Alternative Solution

The first method of getting access to custom profile or course fields is the best approach if you have many custom fields and you need them all as part of your Ad Hoc View. If you only require one or two custom profile fields the below alternative solution may provide slightly better performance. Replace the FieldName# place holder with the information associated with the custom fields you are intending to use. All other instructions are the same as the original custom user profile and course fields, including the requirement to Left Outer Join the mdl_user table to the der_custom_user_fields or der_course_user_fields Derived Table.

 

Custom User Profile SQL

Code Block
languagesql
SELECT u.id AS userid,
      d1.data AS FieldName1,
      d2.data AS FieldName2
FROM mdl_user u
LEFT OUTER JOIN (
   mdl_user_info_data d1
   JOIN mdl_user_info_field f1 ON d1.fieldid = f1.id AND f1.shortname = 'Programs'
) ON d1.userid = u.id
LEFT OUTER JOIN (
   mdl_user_info_data d2
   JOIN mdl_user_info_field f2 ON d2.fieldid = f2.id AND f2.shortname = 'Programs1'
) ON d2.userid = u.id

 

Custom Course Profile SQL

Code Block
languagesql
SELECT u.id AS userid,
      d1.data AS FieldName1,
      d2.data AS FieldName2
FROM mdl_user u
LEFT OUTER JOIN (
   mdl_course_info_data d1
   JOIN mdl_course_info_field f1 ON d1.fieldid = f1.id AND f1.shortname = 'Programs'
) ON d1.userid = u.id
LEFT OUTER JOIN (
   mdl_course_info_data d2
   JOIN mdl_course_info_field f2 ON d2.fieldid = f2.id AND f2.shortname = 'Programs1'
) ON d2.userid = u.id