Get Custom Course Field

Get Custom Course Field

john lennon -
Erantzun kopurua: 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?

Puntuazioen batez bestekoa:Useful (1)
john lennon(e)ri erantzunda

Re: Get Custom Course Field

john lennon -
Also, how can I access the values of individual custom fields? This is really what I need to do. Thanks
john lennon(e)ri erantzunda

Re: Get Custom Course Field

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
Jonathan Garcia(e)ri erantzunda

Re: Get Custom Course Field

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?
john lennon(e)ri erantzunda

Re: Get Custom Course Field

john lennon -
john lennon(e)ri erantzunda

Re: Get Custom Course Field

john lennon -
john lennon(e)ri erantzunda

Re: Get Custom Course Field

Sandipa Mukherjee -
Particularly helpful Moodlers-ren irudia
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
john lennon(e)ri erantzunda

Re: Get Custom Course Field

tim st.clair -
Plugin developers-ren irudia
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.
 
Puntuazioen batez bestekoa:Useful (1)