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

Moodle开发需求:仅显示message_attribute值为2的图片

问题解决:Moodle区块仅展示指定条件的图片

问题描述

我是入门级PHP开发者,刚接触Moodle开发。需要实现仅展示数据库表blocks_table中message_attribute字段值为2的图片,目前已有对应的Mustache模板(仅用于展示图片),但当前代码逻辑无法实现该需求,请求技术帮助。

现有代码

require_once 'edit_form.php';
require_once __DIR__ . '/../../config.php';
class block_add extends block_base
{

public function init()
{
    $this->title = 'Your Block';
}
public function get_content()
{
    global $DB, $OUTPUT, $USER;


    $this->content = new stdClass();

    $mform = new edit_form();
    if ($fromform = $mform->get_data()) {
        $record = new stdClass();
        $messages = $DB->get_records('blocks_table');

        $record->id = $fromform->id;
        $a = $fromform->message['text'];
        $templatecontext = (object) [
            'show' => array_values($messages),
        ];

        // $templatecontext1=(object)[
        //     'mform' => $mform->render(),
        // ];
        $get = $DB->get_records('blocks_table');
        foreach ($get as $value) {
            // var_dump($value->message_attribute);
            if ($value->message_attribute=='2') {
                $this->content->text = $OUTPUT->render_from_template('block_add/manage', $templatecontext);
            } else {
                echo 'doesnt match';
            }
        }

        $b = preg_match_all('#src="([^\s]+)"#', $a, $matches);
        $c = implode(' ', $matches[1]);
        
        $record->message = $c;
        $record->message_type = $fromform->message_type;
        $record->message_attribute = $fromform->message_attribute;
        $DB->insert_record('blocks_table', $record);

    } else {
        $this->content->text = $mform->render();
    }

    return $this->content;

}

public function has_config()
{return true;}
}

问题分析

  1. 未做数据筛选:当前用$DB->get_records('blocks_table')拉取全表数据,没过滤message_attribute=2的记录,模板拿到的是所有数据,自然无法只展示目标图片。
  2. 循环渲染逻辑混乱:遍历全表时,每次匹配到符合条件的记录就重新渲染模板,但模板上下文还是全量数据,而且循环会反复覆盖$this->content->text,最终只保留最后一次渲染结果;同时else分支直接echo会破坏Moodle的输出结构。
  3. 数据更新后未同步:插入新记录后,没有重新获取符合条件的数据就渲染模板,导致展示的还是插入前的旧数据。

修正后的代码

require_once 'edit_form.php';
require_once __DIR__ . '/../../config.php';
class block_add extends block_base
{
    public function init()
    {
        $this->title = 'Your Block';
    }

    public function get_content()
    {
        global $DB, $OUTPUT, $USER;

        $this->content = new stdClass();
        $mform = new edit_form();

        if ($fromform = $mform->get_data()) {
            // 处理表单提交,插入新记录
            $record = new stdClass();
            $a = $fromform->message['text'];
            preg_match_all('#src="([^\s]+)"#', $a, $matches);
            $c = implode(' ', $matches[1]);
            
            $record->id = $fromform->id;
            $record->message = $c;
            $record->message_type = $fromform->message_type;
            $record->message_attribute = $fromform->message_attribute;
            $DB->insert_record('blocks_table', $record);
        }

        // 统一获取符合条件的记录(不管是否提交表单)
        $filtered_messages = $DB->get_records('blocks_table', ['message_attribute' => 2]);

        // 准备模板上下文,只传入筛选后的数据集
        $templatecontext = (object)[
            'show' => array_values($filtered_messages),
        ];

        // 渲染模板
        $this->content->text = $OUTPUT->render_from_template('block_add/manage', $templatecontext);

        // 如果需要同时显示表单,取消下面的注释
        // $this->content->text .= $mform->render();

        return $this->content;
    }

    public function has_config()
    {
        return true;
    }
}

关键修改说明

  • 精准数据查询:用$DB->get_records('blocks_table', ['message_attribute' => 2])直接筛选符合条件的记录,不用全表查询后再循环过滤,效率更高且逻辑清晰。
  • 统一渲染逻辑:无论表单是否提交,都用筛选后的数据集渲染模板,保证展示内容始终是目标图片。
  • 修复输出问题:移除直接echo的语句,所有内容通过$this->content->text返回,避免破坏Moodle页面结构。
  • 同步最新数据:插入新记录后,重新查询筛选数据,确保展示的是最新的符合条件的内容。

额外提示

检查你的Mustache模板manage.mustache,确保它正确遍历筛选后的记录并渲染图片,例如:

{{#show}}
    <img src="{{message}}" alt="展示图片">
{{/show}}

内容的提问来源于stack exchange,提问作者Mohammad-Ahmad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 10:01:17