在 Moodle 中查找块内的上下文 ID

Find the context id inside a block in Moodle

如何在 Moodle 2.9.1.

中获取 contextcontext id

目前我在一个街区:问题文件

在表单提交操作页面中,我需要 context id。我不知道如何让 context 在块(或模块)内。我的代码是这样的:

question_action.php

require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/locallib.php');


global $DB, $CFG;
require_once("$CFG->libdir/resourcelib.php");

if(isset($_GET['id'])){
 $cid = $_GET['id'];}  //course id
 if(isset($_GET['poolid'])){
 $paper= $_GET['paper'];} //question paper id

我如何在这里找到 contextcontext id..

在块 get_content() 函数中,您可以从 $this->context->id 获取 contextid。

如果您的块中有一个额外的 PHP 页面,您将需要确保任何链接都添加了某种标识符作为参数(可以是课程 ID、块 ID 或上下文 ID ).

假设所有链接的末尾都有 courseid(可能是最明智的选择),您可以在页面本身上写:

$courseid = required_param('id', PARAM_INT); // Do not use $_GET directly.
$course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST); // Optional, but you often need the course object.
$context = context_course::instance($courseid);
$contextid = $context->id;