1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Moodleのプロファイルページにナビゲーションを追加する

Last updated at Posted at 2018-07-16

Moodleのプロファイルページに画像のようにナビゲーション(Category nameの部分)を追加する場合のメモ。

image.png

プロフィールページ中にナビゲーションを表示している部分は下記になる。

user/classes/output/myprofile/manager.php
// Core components.
$components = \core_component::get_core_subsystems();
foreach ($components as $component => $directory) {
    if (empty($directory)) {
        continue;
    }
    $file = $directory . "/lib.php";
    if (is_readable($file)) {
        require_once($file);
        $function = "core_" . $component . "_myprofile_navigation";
        if (function_exists($function)) {
           $function($tree, $user, $iscurrentuser, $course);
        }
    }
}

// Plugins.
$pluginswithfunction = get_plugins_with_function('myprofile_navigation', 'lib.php');
foreach ($pluginswithfunction as $plugins) {
    foreach ($plugins as $function) {
        $function($tree, $user, $iscurrentuser, $course);
    }
}

ここでデフォルトだと各変数の値は下記のようになっている。自前で作成する場合は$pluginwithfunctionの方に注目することになる。

$components = array(68) {
    ["access"]=> NULL
    ["admin"]=> string(21) "/var/www/moodle/admin"
    ["analytics"]=> string(25) "/var/www/moodle/analytics"
    ["antivirus"]=> string(29) "/var/www/moodle/lib/antivirus"
    ["auth"]=> string(20) "/var/www/moodle/auth"
    ["availability"]=> string(28) "/var/www/moodle/availability"
    ["backup"]=> string(30) "/var/www/moodle/backup/util/ui"
    ["badges"]=> string(22) "/var/www/moodle/badges"
(snip)
} 

$pluginwithfunction = array(4) {
    ["mod"]=> array(1) {
        ["forum"]=> string(30) "mod_forum_myprofile_navigation"
    }
    ["report"]=> array(5) {
        ["insights"]=> string(36) "report_insights_myprofile_navigation"
        ["log"]=> string(31) "report_log_myprofile_navigation"
        ["outline"]=> string(35) "report_outline_myprofile_navigation"
        ["stats"]=> string(33) "report_stats_myprofile_navigation"
        ["usersessions"]=> string(40) "report_usersessions_myprofile_navigation"
    }
    ["gradereport"]=> array(2) {
        ["overview"]=> string(41) "gradereport_overview_myprofile_navigation"
        ["user"]=> string(37) "gradereport_user_myprofile_navigation"
    }
    ["tool"]=> array(2) {
        ["lp"]=> string(28) "tool_lp_myprofile_navigation"
        ["mobile"]=> string(32) "tool_mobile_myprofile_navigation"
    }
} 

プラグイン名がmod_pluginnameの場合は、lib.phpに下記のようにcategoryを作成してその中でnodeを付与する。

function mod_pluginname_myprofile_navigation(\core_user\output\myprofile\tree $tree, $user, $iscurrentuser) {
    $category = new core_user\output\myprofile\category(
            'catname',
            'Category name'
        );
    $tree->add_category($category);

    $node = new core_user\output\myprofile\node(
            'catname',
            'nodename',
            'contents'
        );
    $tree->add_node($node);
}

参考に両クラスの初期化部分を記載しておく。

user/classes/output/myprofile/category.php
class category implements \renderable {
    ...
    public function __construct($name, $title, $after = null, $classes = null) 
    ...
user/classes/output/myprofile/node.php
class node implements \renderable {
    ...
    public function __construct($parentcat, $name, $title, $after = null, $url = null, $content = null, $icon = null, $classes = null) 
    ...
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?