[Moodle 4.1] Retrieve course custom fields

[Moodle 4.1] Retrieve course custom fields

Patrick Lemaire發表於
Number of replies: 6
Particularly helpful Moodlers的相片 Testers的相片
Hi,
I create a course custom field for a dedicate application exporting datas from courses.
Here the value for courseID=12 :

I had to export the value of this custom field called 'rate' ('Taux' in french) for every courses in the list. I search in documentation and found this : https://docs.moodle.org/dev/Custom_fields_API#Example_code_for_course_custom_fields
So I wrote this function :

Despite the record in database :

The result is… empty 🙃


Where is my mistake?

Thanks for your attention.
Patrick
評比平均分數: -
In reply to Patrick Lemaire

Re: [Moodle 4.1] Retrieve course custom fields

Renaat Debleu發表於
Core developers的相片 Particularly helpful Moodlers的相片 Plugin developers的相片
The function export_instance_data_object needs an instance id of a core_course_list_element, not a course id.

I work like this:

$course = get_course($courseid);
$coursele = new \core_course_list_element($course);
if ($coursele->has_custom_fields()) {
   $fields = $courseele->get_custom_fields();
   foreach ($fields as $key => $value) {
      ...
   }
}

評比平均分數:Useful (1)
In reply to Renaat Debleu

Re: [Moodle 4.1] Retrieve course custom fields

Patrick Lemaire發表於
Particularly helpful Moodlers的相片 Testers的相片
Thanks for your help! I really appreciate it.
Especially when you get that strange feeling that it's something that's easy for everyone else but you just can't do it. And you start to go crazy

So I try to change my function like you suggest :
function course_get_taux($courseid) {
    $course = get_course($courseid);

    $courseelement = new \core_course_list_element($course);

    if ($courseelement->has_custom_fields()) {
        $fields = $courseelement->get_custom_fields();
        $metadata = [];
        foreach ($fields as $field) {
            if (empty($field->get_value())) {
                continue;
            }
            $cat = $field->get_field()->get_category()->get('name');
            $metadata[$field->get_field()->get('shortname')] = $cat . ': ' . $field->get_value();
        }
        return $metadata;
    } else {
        return false;
    }
}
But the behaviour is the same : the function 'has_custom_fields' return false despite the fact that the custom fields are in course settings. 🤪

Furthermore, when I look at the test  test_custom_fields_data() in /admin/tool/uploadcourse/tests/course_test.php (direct link) they seems to use 
export_instance_data_object with simple courseid.

What am I doing wrong?
In reply to Patrick Lemaire

Re: [Moodle 4.1] Retrieve course custom fields

Patrick Lemaire發表於
Particularly helpful Moodlers的相片 Testers的相片
I think I've figured out what the problem is!
The idea came to me while driving on a trip and I haven't had a chance to check it out yet.
The custom field must be "visible to" nobody, since users must not be aware of it.
I thought the plugin overrode rights, but I'm not convinced at all. If this is the case, how can I grant Admin-type permissions to my function?
In reply to Patrick Lemaire

Re: [Moodle 4.1] Retrieve course custom fields

Mark Sharp發表於
Core developers的相片 Particularly helpful Moodlers的相片 Plugin developers的相片

Ah yes, that always catches me out too. You can do it, if you take the example given at: https://docs.moodle.org/dev/Custom_fields_API#Example_code_for_course_custom_fields and change

$datas = $handler->get_instance_data($courseid);

for

$datas = $handler->get_instance_data($courseid, true);

that will fix it. The second argument is "returnall", so you don't need to make the fields visible.

評比平均分數:Useful (2)
In reply to Patrick Lemaire

Re: [Moodle 4.1] Retrieve course custom fields

Charles Krengel eLearn Solutions發表於
Hi Patrick

Although not directly related to your query I struggled to find reference to adding a custom course field to an ad hoc Configurable Report query.

The Query below adds the custom field value named 'Course Cost' (coursecost) to the matching course in the report

(SELECT cfd.charvalue FROM prefix_customfield_data AS cfd INNER JOIN prefix_customfield_field AS cf ON cf.id = cfd.fieldid WHERE cf.shortname = 'coursecost' AND cfd.instanceid = c.id) AS 'Course Cost',

I hope this helps someone

Charles
評比平均分數:Useful (2)