sorting table column gives an error reading the database

sorting table column gives an error reading the database

by Franky Just -
Number of replies: 0

I'm developing a block. I've created a (simple) table and everything works fine, except I run into one issue. When I click on one of the table headers (usersincohort), sorting doesn't work; I get an error reading the dbs. After this error, the table isn't showing up anymore, the error remains. Only when I'm logging out and in again, table is showing up again. The specific column isn't a dbs field, this seems to be the problem. I can create an extra dbs field for this, but I was wondering if there would be another solution?

Version Moodle 3.   Some code: 

function __construct($uniqueid) {

        parent::__construct($uniqueid);

        // Define the list of columns to show.

        $columns = array('id', 'cohortid', 'usersincohort',  'timecreated');

        $this->define_columns($columns);

       // Define the titles of columns to show in header.

        $headers = array('Id', 'Cohort', 'Number of users',  'Time created' );

        $this->define_headers($headers);

    }


function col_usersincohort($values) {

        global $DB;

        $rs = array();

        $records = $DB->get_records_sql("SELECT COUNT(cohortid) AS usersincohort FROM {cohort_members} 

            WHERE cohortid = ?", array($values->cohortid));

        foreach ($records as $record) {

            $rs[] = $record->usersincohort;

        }

        return $record->usersincohort;

    }

Average of ratings: -