Get Custom Course Field

Get Custom Course Field

by john lennon -
Number of replies: 8

Hi guys

I have added a new custom field to my courses, using the new custom fields feature for courses introduced in 3.7. 

How can I now get the value of the field for the course via PHP? 

I’m able to access the course ID, fullname, shortname etc like this:

$COURSE->id

Or 

$PAGE->course->fullname

But how can I get the value of a custom course field in this way?

Average of ratings: Useful (1)
In reply to john lennon

Re: Get Custom Course Field

by john lennon -
Also, how can I access the values of individual custom fields? This is really what I need to do. Thanks
In reply to john lennon

Re: Get Custom Course Field

by Jonathan Garcia -
Hi John,

You can do the following:

$test = new \core_course_list_element($COURSE);
var_dump($test->get_custom_fields());

Which will print the existing custom fields for the given course.

Hope this helps.
Jonathan
In reply to Jonathan Garcia

Re: Get Custom Course Field

by john lennon -
Thanks Jonathan. That's somewhat helpful, but I can't access any of the data. The fields and data are all protected. Hmm... what could a solution be I wonder? Any ideas?
In reply to john lennon

Re: Get Custom Course Field

by john lennon -
bump
In reply to john lennon

Re: Get Custom Course Field

by john lennon -

bump

In reply to john lennon

Re: Get Custom Course Field

by Sandipa Mukherjee -
Picture of Particularly helpful Moodlers
Hi, 
I used this code to fetch custom checkbox course field values.

$coursecontextid = context_course::instance($COURSE->id);
 $customfield = $DB->get_record_sql('SELECT id FROM {customfield_field} WHERE ' . $DB->sql_compare_text('shortname') . ' = ' . $DB->sql_compare_text(':shortname'), ['shortname' => 'myfield']);
 $myfielddata = $DB->get_record("customfield_data", array("fieldid" => $customfield->id, "contextid" => $coursecontextid->id));

I hope this helps!
Thanks
Sandipa
In reply to john lennon

Re: Get Custom Course Field

by tim st.clair -
Picture of Plugin developers
Depending on how you grab your course records, the customfield object may already be populated. You just need to be able to get the field VALUES, which is the most important bit of the record, and it wasn't that obvious to me how to grab the value of a field when you already had a course object. The https://docs.moodle.org/dev/Custom_fields_API page is very lightly documented when it comes to everyday examples.

I needed to iterate courses and seek out just the courses with certain custom properties. I was using coursecat_helper() which helpfully already populates the customfields in a efficient manner, so the method referenced in the wiki would be double-handling the data. I came up with iterating the customfields array to seek the field using its shortname lookup, and then matching on the value. Something like this:
    $chelper = new \coursecat_helper();

	// when COURSECAT_SHOW_COURSES_EXPANDED is set, the option 'customfields' is set to true, which efficiently populates these fields
    $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->set_courses_display_options(['recursive' => true]);
    $courses = \core_course_category::top()->get_courses($chelper->get_courses_display_options());

    // i want to filter out courses from the iterator when they have a custom field called 'invisible' set to the value '1' (e.g. a checkbox is ticked).
    foreach ($courses as $index => $course) {
    	foreach ($course->customfields as $f) {
    		if ($f->get_field()->get('shortname') === 'invisible' && $f->get_value() === '1') {
    			unset($courses[$index]);
    		}
    	}
    }

    // $courses is now filtered the way I want.
 
Average of ratings: Useful (1)