Moodle得到类别

时间:2017-02-02 21:09:05

标签: php moodle

我试图在主页上创建一个主题,它会显示类别和课程,我能够通过 get_courses()这样的功能获得课程:

<?php $courses = get_courses(); ?>

<?php foreach ($courses as $key => $value) { ?>    
    <a href="course/view.php?id=<?php echo $key; ?>">
        <h2 class="title"><?php echo $courses[$key]->fullname; ?></h2>            
    </a>
    <p class="summary"><?php echo $courses[$key]->summary; ?></p> 
    <p class="category"><?php echo $courses[$key]->category; ?></p>       
<?php } ?>

现在我试图获取类别,
我唯一能得到的是带有[$ key] - &gt; category

的类别ID

如何获取类别名称并显示它?

1 个答案:

答案 0 :(得分:2)

根据this post,您应该可以直接从数据库获取此信息:

<?php
$courses = get_courses();
foreach ($courses as $id=>$course) {
    $category = $DB->get_record('course_categories',array('id'=>$course->category));
    $course->categoryName = $category->name;
    $allcourses[$id] = $course;
}
?>

然后,在您的显示代码中:

<?php foreach($allcourses as $id=>$course):?>
    <a href="course/view.php?id=<?=$id?>">
        <h2 class="title"><?=$course->fullname?></h2>            
    </a>
    <p class="summary"><?=$course->summary?></p> 
    <p class="category"><?=$course->categoryName?></p>       
<?php endforeach;?>