构建一个 <select> 来显示特定类别和儿童类别的课程
Contruct a <select> that show courses IN specific category and category childrens too
我正在使用 Moodle 2.9.7
我想获得特定类别中的课程列表以及该类别的儿童类别中的课程(如果存在的话)。
这应该在选项将是课程列表的地方进行
我几乎是 moodle 的新手,所以无法想象或设置线索我将如何做到这一点。
如果有人能帮忙就太好了。
谢谢
从我的头顶开始,我会选择这样的东西:
$categoryid = 1; // Change this.
$category = coursecat::get($categoryid);
$childrencategories = $category->get_children();
$coursesincategory = $category->get_courses();
使用coursecat
的好处是不会显示当前用户不可见的类别。
要构建 select 框,您可以决定在扩展 moodleform
class 时使用 html_writer
或 select
元素。 Moodle中有很多后者的例子。
测试文件
这是一个测试文件,您可以将其命名为 test_coursecat.php
并放置在 Moodle 的根目录中,其中 index.php
是。通过 yoursiteurl/test_coursecat.php
访问它,您可以选择添加 ?categoryid=NUMBERHERE
来更改开始的类别。
<?php
// Require config.php, and coursecatlib.
require('config.php');
require($CFG->libdir . '/coursecatlib.php');
// Get the parameter 'categoryid' passed via GET parameter. Else the value will be 0.
$categoryid = optional_param('categoryid', 0, PARAM_INT);
// Require a user to be logged in to access this page.
require_login();
// Set the minimal page requirements.
$PAGE->set_context(context_system::instance());
$PAGE->set_url('/test_coursecat.php');
// Start the output.
echo $OUTPUT->header();
// Fetch out root category with ID $categoryid.
$rootcategory = coursecat::get($categoryid);
// Get 2 sub categories.
$categories = $rootcategory->get_children(array('limit' => 2));
// Output each category fetched, and their associated courses.
echo '<pre>';
foreach ($categories as $category) {
echo $category->name . PHP_EOL;
foreach ($category->get_courses() as $course) {
echo ' - ' . $course->shortname . PHP_EOL;
}
}
echo '</pre>';
// End the output.
echo $OUTPUT->footer();
我正在使用 Moodle 2.9.7
我想获得特定类别中的课程列表以及该类别的儿童类别中的课程(如果存在的话)。
这应该在选项将是课程列表的地方进行
我几乎是 moodle 的新手,所以无法想象或设置线索我将如何做到这一点。
如果有人能帮忙就太好了。
谢谢
从我的头顶开始,我会选择这样的东西:
$categoryid = 1; // Change this.
$category = coursecat::get($categoryid);
$childrencategories = $category->get_children();
$coursesincategory = $category->get_courses();
使用coursecat
的好处是不会显示当前用户不可见的类别。
要构建 select 框,您可以决定在扩展 moodleform
class 时使用 html_writer
或 select
元素。 Moodle中有很多后者的例子。
测试文件
这是一个测试文件,您可以将其命名为 test_coursecat.php
并放置在 Moodle 的根目录中,其中 index.php
是。通过 yoursiteurl/test_coursecat.php
访问它,您可以选择添加 ?categoryid=NUMBERHERE
来更改开始的类别。
<?php
// Require config.php, and coursecatlib.
require('config.php');
require($CFG->libdir . '/coursecatlib.php');
// Get the parameter 'categoryid' passed via GET parameter. Else the value will be 0.
$categoryid = optional_param('categoryid', 0, PARAM_INT);
// Require a user to be logged in to access this page.
require_login();
// Set the minimal page requirements.
$PAGE->set_context(context_system::instance());
$PAGE->set_url('/test_coursecat.php');
// Start the output.
echo $OUTPUT->header();
// Fetch out root category with ID $categoryid.
$rootcategory = coursecat::get($categoryid);
// Get 2 sub categories.
$categories = $rootcategory->get_children(array('limit' => 2));
// Output each category fetched, and their associated courses.
echo '<pre>';
foreach ($categories as $category) {
echo $category->name . PHP_EOL;
foreach ($category->get_courses() as $course) {
echo ' - ' . $course->shortname . PHP_EOL;
}
}
echo '</pre>';
// End the output.
echo $OUTPUT->footer();