Hello! I wanted to ask for some assistance about my php task.
I need to get information about courses in moodle, each course name, category in which course is in , enrolled student count and date created.
For now I have made 2 tables but each has flaw, either I have all needed information but no student count or I don't have category's name.
What should I improve in my code or should I use different method?
my current code:
require_once('../../config.php');
global $DB;
$rec=$DB->get_records_sql('SELECT mdl_course.id,mdl_course.fullname,mdl_course.timecreated,mdl_course.category
FROM mdl_course ');
$table = new html_table();
$table->head = array('Fullname','timecreated', 'category','studentu skaits' );
foreach ($rec as $records) {
$id = $records->id;
$fname = $records->fullname;
$timecreated = $records->timecreated;
$category = $records->category;
$context = get_context_instance(CONTEXT_COURSE, $id) ;
$students = get_role_users(5 , $context);
$table->data[] = array($fname,date('d-m-Y',$timecreated),$category,count($students));
}
echo html_writer::table($table);