Using data from custom course fields

Using data from custom course fields

by Kerstin Schmidt -
Number of replies: 1

Hello there,

via Google I came across this post here (https://moodle.org/mod/forum/discuss.php?d=394717) about figuring out custom fields in code. The code snippet was already super helpful - thanks for that. 

I would like to influence the display of the start page according to certain criteria via the custom course fields. For this I override the function frontpage_available_courses() in the renderer of my theme. However, I would like to check not only for a certain checkbox, but for a select. Unfortunately, Moodle doesn't seem to provide the values here. At least I can't find them via var_dump. I'll attach an array for you here. 


  ["data": "core\persistent":private]=> array(11) { 

["id"]=> string(1) "5" 

["shortname"]=> string(11) "courseofstudy" 

["name"]=> string(11) "courseofstudy" 

["type"]=> string(11) "multiselect" 

["description"]=> string(0) "" 

["descriptionformat"]=> string(1) "1" 

["sortorder"]=> string(1) "3" 

["categoryid"]=> string(1) "1" 

["configdata"]=> string(118) "{"required":"0","uniquevalues":"0","options":"Math\r\nBio\r\nHistory","defaultvalue":"","locked":"0","visibility":"2"}" ["timecreated"]=> string(10) "1651755362" ["timemodified"]=> string(10) "1651755362" }


I'm pretty sure the solution is made up of the combination of sortorder and options within configdata, because in my example the third value was stored in the course. Does anyone have an idea how I can specifically search for the values (like Math, Bio, History) and thus only show the appropriate courses?


Thanks a lot!

Average of ratings: -
In reply to Kerstin Schmidt

Re: Using data from custom course fields

by tim st.clair -
Picture of Plugin developers
I think I'm doing something similar to what you are after in my filter_units plugin, though I'm using a filter as opposed to the horrorshow of overriding frontpage_available_courses().

`/course/lib.php` has a method called `course_filter_courses_by_customfield` which you could use like this:


 $category = core_course_category::get($COURSE->category); // or whatever
 $pool = $category->get_courses(array('recursive' => false, 'summary' => false));
 $courses = course_filter_courses_by_customfield($pool, 'courseofstudy', 'Math');

You might have to call it twice to modify your course listing when there are multiple custom attributes you need to match.