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 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题