dongzhengzhong1282 2017-10-03 13:39
浏览 90
已采纳

使用查询填充Moodle表单中的select元素

I am creating a Moodle form using the Forms API. I tried to populate a dropdown element in my form using a query.

class my_form extends moodleform {

    function definition() {
        $mform =& $this->_form;

        $mform->addElement('html', '<h2>' . get_string('header', 'local_data')  . '</h2>');

        global $USER;
        $userid = $USER->id;
        $myCourseIds = getMyCourses($userid);

        $mform->addElement('select', 'courseid', get_string('courseid', 'local_data'), $myCourseIds);


        $this->add_action_buttons(true, get_string('send', 'local_data'));
    }

}

Here's the query:

function getMyCourses($userid) {
    global $DB;

    $sql = 'SELECT c.idnumber, c.shortname
            FROM {role_assignments} ra, {user} u, {course} c, {context} cxt, {attendance_sessions} ass, {attendance} att
            WHERE ra.userid = u.id
            AND ra.contextid = cxt.id
            and att.id = ass.attendanceid
            and c.id = att.course
            AND cxt.instanceid = c.id
            AND (roleid =12 OR roleid=3)
            and u.id = ?';

    return $DB->get_records_sql($sql, array($userid));
}

The error that is returned is a general database error. I notice that the example in the Moodle Forms API page uses a global variable, $FORUM_TYPES, where I have used the variable for my query.

Here's the error message:

Coding error detected, it must be fixed by a programmer: PHP catchable fatal error. More information about this error

So my question is -

  • can I use a variable which stores the results of a database query in the select element of the Moodle forms API?
  • 写回答

2条回答 默认 最新

  • dongyi0114 2017-10-03 14:11
    关注

    The problem was that I hadn't converted my query results to an array with a key and value pair.

    class my_form extends moodleform {
    
        function definition() {
            $mform =& $this->_form;
    
            $mform->addElement('html', '<h2>' . get_string('header', 'local_data')  . '</h2>');
    
            global $USER;
            $userid = $USER->id;
            $myCourseIds = array();
            $selectArray = array();
            $myCourseIds = getMyCourses($userid);
          // push query results into selectArray as key, value
            foreach($myCourseIds as $myCourseId) {
                $key = $myCourseId->idnumber;
                $value = $myCourseId->shortname;
                $selectArray[$key] = $value;
            }
    
            $mform->addElement('select', 'courseid', get_string('courseid', 'local_data'), $selectArray);    
    
            $this->add_action_buttons(true, get_string('send', 'local_data'));
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新
  • ¥50 深度强化学习解决能源调度问题
  • ¥15 一道计算机组成原理问题