You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Moodle自定义区块开发问题:HTML表格显示位置异常

问题解决:Moodle自定义区块表格显示位置错误

问题根源在于你直接使用echo输出表格,这会把内容直接发送到页面输出流,导致表格显示在页面顶部而非区块内部。正确的做法是将表格的HTML内容赋值给区块的content->text属性。

修改后的代码如下:

class block_scorecard extends block_base {

    function init() {
        $this->title = get_string('pluginname', 'block_scorecard');
    }

    function get_content()  {
        global $DB;

        if ($this->content !== NULL) {
            return $this->content;
        }

        $courses = $DB->get_records('scorm_scoes_track', ['element' => 'cmi.core.total_time']);
        $table = new html_table();
        $table->head = array('No of attempts','Time modified','status');
        
        foreach ($courses as $course) {
            $attempt = $course->attempt;
            $timemodified = userdate($course->timemodified, get_string('strftimerecentfull'));
            $status = $course->value;
        
            $table->data[] = array($attempt, $timemodified, $status);
        }

        // 关键修改:将表格HTML赋值给content文本,而非直接echo
        $this->content = new stdClass;
        $this->content->text = html_writer::table($table);       
        return $this->content;
    }
}

核心修改说明:

  • 移除了直接输出表格的echo html_writer::table($table);语句
  • 将html_writer::table($table)生成的HTML字符串直接赋值给$this->content->text
  • 补充了return $this->content,确保区块能正确返回渲染内容

内容的提问来源于stack exchange,提问作者Parul S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 18:05:18