Moodle 2.7需要知道如何为不同的用户组创建自定义菜单

时间:2014-11-11 10:40:47

标签: php moodle

我一直在研究moodle 2.7框架,我的要求是我想为教师和学生角色显示2个不同的自定义菜单,目前我已经在模板选项中创建了自定义菜单,但我不知道如何创建多个自定义moodle中的菜单。到目前为止,我尝试通过搜索组ID然后使用if语句来分配自定义菜单但仍然没有输出,所以我请求任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

您可以扩展自定义菜单 - https://docs.moodle.org/dev/Extending_the_theme_custom_menu

然后使用has_capability()查看用户是否可以看到菜单项。

像这样的东西(我还没试过)

// theme/themename/renderers.php
class theme_themename_core_renderer extends core_renderer {

    protected function render_custom_menu(custom_menu $menu) {

        $context = context_system::instance();
        if (has_capability('local_plugin/menuname:view', $context)) {
            $branchlabel = get_string('menuname', 'local_myplugin');
            $branchurl   = new moodle_url('/local/myplugin/index.php');
            $branchtitle = $branchlabel;
            $branchsort  = 10000;

            $branch = $menu->add($branchlabel, $branchurl, $branchtitle, $branchsort);
         }

        return parent::render_custom_menu($menu);
    }
}

答案 1 :(得分:0)

罗素 以下是renderes.php的代码

<?php

/ *

此程序是免费软件:您可以重新分发和/或修改 它是根据GNU通用公共许可证的条款发布的 自由软件基金会,许可证的第3版,或 (根据您的选择)任何更高版本。

这个程序的发布是希望它有用, 但没有任何保证;甚至没有暗示的保证 适销性或适用于特定用途的适用性。见 GNU通用公共许可证了解更多详情。

此插件是Archaius主题的一部分。 @copyright 2013 Daniel Munera Sanchez

* /

class theme_archaius_core_renderer扩展core_renderer {

/**
 * Prints a nice side block with an optional header.
 *
 * The content is described
 * by a {@link block_contents} object.
 *
 * @param block_contents $bc HTML for the content
 * @param string $region the region the block is appearing in.
 * @return string the HTML to be output.
 */
function block(block_contents $bc, $region) {

    $bc = clone($bc); // Avoid messing up the object passed in.
    if (empty($bc->blockinstanceid) || !strip_tags($bc->title)) {
        $bc->collapsible = block_contents::NOT_HIDEABLE;
    }
    if ($bc->collapsible == block_contents::HIDDEN) {
        $bc->add_class('hidden');
    }
    if (!empty($bc->controls)) {
        $bc->add_class('block_with_controls');
    }

    $skiptitle = strip_tags($bc->title);
    if (empty($skiptitle)) {
        $output = '';
        $skipdest = '';
    } else {
        $output = html_writer::tag('a', get_string('skipa', 'access', $skiptitle), array('href' => '#sb-' . $bc->skipid, 'class' => 'skip-block'));
        $skipdest = html_writer::tag('span', '', array('id' => 'sb-' . $bc->skipid, 'class' => 'skip-block-to'));
    }

     $title = '';
    if ($bc->title) {
        $title = html_writer::tag('h2', $bc->title);
    }

    $controlshtml = $this->block_controls($bc->controls);

    if ($title || $controlshtml) {
        $output .= html_writer::tag('div', html_writer::tag('div',  $title , array('class' => 'title')), array('class' => 'header-tab'));
    }

    $output .= html_writer::start_tag('div', $bc->attributes);

    if ($title || $controlshtml) {
        $output .= html_writer::tag('div', html_writer::tag('div', html_writer::tag('div', '', array('class'=>'block_action')). $title . $controlshtml, array('class' => 'title')), array('class' => 'header'));
    }

    $output .= html_writer::start_tag('div', array('class' => 'content'));
    if (!$title && !$controlshtml) {
        $output .= html_writer::tag('div', '', array('class'=>'block_action notitle'));
    }
    $output .= $bc->content;

    if ($bc->footer) {
        $output .= html_writer::tag('div', $bc->footer, array('class' => 'footer'));
    }

    $output .= html_writer::end_tag('div');


    $output .= html_writer::end_tag('div');

    if ($bc->annotation) {
        $output .= html_writer::tag('div', $bc->annotation, array('class' => 'blockannotation'));
    }
    $output .= $skipdest;

    $this->init_block_hider_js($bc);

    return $output;
}


/*
 * From boostrapbase
 * Overriding the custom_menu function ensures the custom menu is
 * always shown, even if no menu items are configured in the global
 * theme settings page.
 */
public function custom_menu($custommenuitems = '') {
    global $CFG;

    //TODO: Check this in a different way
    $langs = get_string_manager()->get_list_of_translations();
    if ( (count($langs) < 2) and (empty($CFG->custommenuitems))) {
        return '';
    }else{
        if (!empty($CFG->custommenuitems))
            $custommenuitems .= $CFG->custommenuitems;
        $custommenu = new custom_menu($custommenuitems, current_language());
        return $this->render_custom_menu($custommenu);
    }
}

// http://docs.moodle.org/dev/Extending_the_theme_custom_menu
protected function render_custom_menu(custom_menu $menu) {

    global $CFG;
    require_once($CFG->dirroot.'/course/lib.php');

    //navigation mycourses is no supported since 2.4
    if (isloggedin() && !isguestuser() && 
        $mycourses = enrol_get_my_courses(NULL, 'visible DESC, fullname ASC')) {  

        $branchlabel = get_string('mycourses') ;
        $branchurl   = new moodle_url('/course/index.php');
        $branchtitle = $branchlabel;
        $branchsort  = 8000 ;
        $branch = $menu->add($branchlabel, $branchurl, $branchtitle, $branchsort);

        foreach ($mycourses as $mycourse) {
            $branch->add($mycourse->shortname, new moodle_url(
                '/course/view.php', 
                array('id' => $mycourse->id)), 
                $mycourse->fullname
            );
        }
    }

    $course_id = $this->page->course->id;
    if (isloggedin() && $course_id > 1) {
        $branchlabel = get_string('grades');
        $branchurl   = new moodle_url('/grade/report/index.php?id='.$this->page->course->id);
        $branchtitle = $branchlabel;
        $branchsort  = 9000;
        $branch = $menu->add($branchlabel, $branchurl, $branchtitle, $branchsort);
    }

    //From boostrapbase

    // TODO: eliminate this duplicated logic, it belongs in core, not
    // here. See MDL-39565.
    $addlangmenu = true;
    $langs = get_string_manager()->get_list_of_translations();
    if (
        count($langs) < 2
        or empty($CFG->langmenu)
        or ($this->page->course != SITEID 
        and !empty($this->page->course->lang))) {

        $addlangmenu = false;
    }

    if ($addlangmenu) {
        $branchlabel =  get_string('language');
        $branchurl = new moodle_url('#');
        $branch = $menu->add($branchlabel, $branchurl, $branchlabel, 10000);

        foreach ($langs as $langtype => $langname) {
            $branch->add($langname, 
                new moodle_url(
                    $this->page->url, 
                    array('lang' => $langtype)
                ), 
                $langname
            );
        }
    }
    return parent::render_custom_menu($menu);
}

protected function render_custom_menu_item(custom_menu_item $menunode) {
    $transmutedmenunode = new theme_archaius_transmuted_custom_menu_item($menunode);
    return parent::render_custom_menu_item($transmutedmenunode);
}

}