protected function prepare_sql_courses() {
        global $DB;
        if (!preg_match('/\\%COURSEFILTER\\%/', $this->sql)) {
            return;
        }
        $this->init_course_access();
        $unaccessiblecourses = array_filter($this->courseaccess, function($item) {
            return !$item;
        });
        $idx = 0;
        while (preg_match('/^([^\\0]*?)\\%COURSEFILTER\\%([^\\0]*)$/', $this->sql, $matches)) {
            list($sql, $params) = $DB->get_in_or_equal(array_keys($unaccessiblecourses),
                    SQL_PARAMS_NAMED, 'ca_'.($idx++).'_', false, 0);
            $this->sql = $matches[1].' '.$sql.' '.$matches[2];
            $this->params += $params;
        }
    }