ロールの説明を各言語で表示する改良

ロールの説明を各言語で表示する改良

- Tatsuya Shirai の投稿
返信数: 0

 私の管理しているMoodleサイトはインストール時に日本語パックのダウンロードに失敗したため,ロールの説明が英語です.もちろん,難しい英語では無いですし,データベースを書き換えれば日本語による説明に変更できるとは思うのですが,少し欲を出してみました.利用しているユーザが選択している言語で表示するように改良してみました.

 例えばAdministratorのショートネームはAdminで,その説明はget_string('administratordescription')で得ることが出来ます.この説明は各言語パックに含まれています.ですので,ロール名かロールのショートネームに基づいて説明文を言語パックから読み出す仕組みです.当初は,ショートネーム+descriptionがキーになって取得できるかと甘く考えていたのですが,Moodleの発展と共にロールを後から増やしたり役割を変えたりしたようで,名前とキーに一貫したルールがありません.ここがネックといえばネックです.新しくロールが追加された場合は新しく作成したget_role_description()関数を変更する必要があるでしょう.また,管理者が独自に追加したロールの説明はデータベースにのみ存在し,言語パックには含まれて居ません(当然ですね)ので,これはロール作成時に入力した言語でのみ表示されます.

(A) lib/accesslib.php
(A1) function get_role_description() : 3451行近辺 (新規追加)

function get_role_description($rolename)
{
    $sname = '';
    switch ($rolename) {
    case 'Administrator':
    case 'admin':          $sname = 'administratordescription'; break;
    case 'Course creator':
    case 'coursecreator':  $sname = 'coursecreatorsdescription'; break;
    case 'Teacher':
    case 'editingteacher': $sname = 'defaultcourseteacherdescription'; break;
    case 'Non-editing teacher':
    case 'teacher':        $sname = 'noneditingteacherdescription';break;
    case 'Student':
    case 'student':        $sname = 'defaultcoursestudentdescription'; break;
    case 'Guest':
    case 'guest':          $sname = 'guestdescription'; break;
    case 'user':           $sname = 'authenticateduserdescription'; break;
    }
    if ($sname == '') return false;
    $description = get_string($sname);
    if ($description == ''.$sname.'') return false;
    return $description;
}

(A2) 
function get_all_roles(): 3454行近辺

function get_all_roles() {
//  return get_records('role', '', '', 'sortorder ASC');
    $roles = get_records('role', '', '', 'sortorder ASC');
    // See function moodle_install_roles()
    foreach ($roles as $key=>$role) {
        if (($description = get_role_description($role->shortname)) == false) continue;
        $roles[$key]->description = $description;
    }
    return $roles;
}

(B) admin/roles/assign.php
374行近辺

        $table->tablealign = 'center';
        $table->cellpadding = 5;
        $table->cellspacing = 0;
        $table->width = '60%';
        $table->head = array(get_string('roles', 'role'), get_string('description'), get_string('users'));
        $table->wrap = array('nowrap', '', 'nowrap');
        $table->align = array('right', 'left', 'center');

        foreach ($assignableroles as $roleid => $rolename) {
            $countusers = count_role_users($roleid, $context);
            if (($description = get_role_description($rolename)) == false)  //  (ADD)
            $description = format_string(get_field('role', 'description', 'id', $roleid));
            $table->data[] = array('<a href="'.$baseurl.'&amp;roleid='.$roleid.'">'.$rolename.'</a>',$description, $countusers);
        }

        print_table($table);
    }

(C) admin/roles/override.php
222行近辺

    } else {   // Print overview table

        $table->tablealign = 'center';
        $table->cellpadding = 5;
        $table->cellspacing = 0;
        $table->width = '60%';
        $table->head = array(get_string('roles', 'role'), get_string('description'), get_string('overrides', 'role'));
        $table->wrap = array('nowrap', '', 'nowrap');
        $table->align = array('right', 'left', 'center');

        foreach ($overridableroles as $roleid => $rolename) {
            $countusers = 0;
            $overridecount = count_records_select('role_capabilities', "roleid = $roleid AND contextid = $context->id");
            if (($description = get_role_description($rolename)) == false)  //  (ADD)
            $description = format_string(get_field('role', 'description', 'id', $roleid));
            $table->data[] = array('<a href="'.$baseurl.'&amp;roleid='.$roleid.'">'.$rolename.'</a>', $description, $overridecount);
        }

        print_table($table);
    }


 別にロールの説明が英語で表示されようが構わない(特にインストール時に正しく言語パックを読み込ませることに成功している場合も)ならば,上記変更は不要です.

 当方の環境のようにインストール時に失敗してしまった場合や,日本語で表示される環境で,管理者に他国の方が含まれる場合には役立つ改良です.(ちなみに英語でもイタリア語でも表示できることを確認しています)