<?php  //$Id$

    switch ($action) {
        case 'add':
            $submitlabel = get_string('addrole', 'role');
            break;
        case 'edit':
        default:
            $submitlabel = get_string('savechanges');
    }

    if ($action == 'view') {
?>
<?php
    }
?>
<form id="rolesform" action="manage.php" method="post">
<fieldset class="invisiblefieldset">
<input type="hidden" name="roleid" value="<?php p($roleid) ?>" />
<input type="hidden" name="sesskey" value="<?php p(sesskey()) ?>" />
<input type="hidden" name="action" value="<?php if ($action != 'view') { echo p($action); } ?>" />
</fieldset>
<table class="roledesc" cellpadding="9" cellspacing="0">
<?php if ($action == 'view') { ?>
    <tr valign="top">
        <td align="right"><?php print_string('name') ?>:</td>
        <td><?php p(format_string($role->name)); ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><?php print_string('shortname') ?>:</td>
        <td><?php p($role->shortname); ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><?php print_string('description') ?>:</td>
        <td><?php p(format_text($role->description, FORMAT_HTML)); $usehtmleditor = false; ?></td>
    </tr>
    <tr valign="top">
        <td align="right"><?php print_string('legacytype', 'role') ?>:</td>
        <td><?php
                $usehtmleditor = false;
                if (empty($role->legacytype)) {
                    print_string('none');
                } else {
                    print_string('legacy:'.$role->legacytype, 'role');
                }
            ?>
        </td>
    </tr>
<?php } else { ?>
    <tr valign="top">
        <td align="right"><label for="name"><?php print_string('name') ?></label></td>
        <td><?php
            echo '<input type="text" id="name" name="name" maxlength="254" size="50" value="'.s($role->name).'" />';
            if (isset($errors["name"])) formerr($errors["name"]);
            ?>
        </td>
    </tr>
    <tr valign="top">
        <td align="right"><label for="shortname"><?php print_string('shortname') ?></label></td>
        <td><?php
            echo '<input type="text" id="shortname" name="shortname" maxlength="100" size="15" value="'.s($role->shortname).'" />';
            if (isset($errors["shortname"])) formerr($errors["shortname"]);
            ?>
        </td>
    </tr>
    <tr valign="top">
        <td align="right"><label for="edit-description"><?php print_string('description') ?></label></td>
        <td><?php
            print_textarea($usehtmleditor, 10, 50, 50, 10, 'description', $role->description);
            ?>
        </td>
    </tr>
    <tr valign="top">
        <td align="right"><label for="menulegacytype"><?php print_string('legacytype', 'role') ?></label></td>
        <td><?php
            $options = array();
            $options[''] = get_string('none');
            $legacyroles = get_legacy_roles();
            foreach($legacyroles as $ltype=>$lcap) {
                $options[$ltype] = get_string('legacy:'.$ltype, 'role');
            }
            choose_from_menu($options, 'legacytype', $role->legacytype, '');
            ?>
        </td>
    </tr>
<?php } ?>
</table>

<?php
    print_heading_with_help(get_string('permissions','role'), 'permissions');

    $strinherit = get_string('notset','role');
    $strallow = get_string('allow','role');
    $strprevent = get_string('prevent','role');
    $strprohibit = get_string('prohibit','role');
?>

<table class="rolecap">

<tr>
<th class="name" align="left" scope="col"><?php print_string('capability','role') ?></th>
<th class="inherit" scope="col"><?php p($strinherit); ?></th>
<th class="allow" scope="col"><?php p($strallow); ?></th>
<th class="prevent" scope="col"><?php p($strprevent); ?></th>
<th class="prohibit" scope="col"><?php p($strprohibit); ?></th>
<th class="risk" colspan="5" scope="col"><?php print_string('risks','role') ?></th>
</tr>

<?php

// init these 2
$contextlevel = 0;
$component = '';

$strrisks = s(get_string('risks', 'role'));
// MDL-11687
$strcapabilities = 'Capabilities';//s(get_string('capabilities', 'role'));

// prepare legacy defaults
if (!empty($role->legacytype)) {
    $defaultcaps = get_default_capabilities($role->legacytype);
} else {
    $defaultcaps = false;
}

foreach ($capabilities as $capability) {

    //legacy caps have their own selector
    if (islegacy($capability->name)) {
        continue;
    }

    // prints a breaker if component or name or context level
    if (component_level_changed($capability, $component, $contextlevel)) {
    //if ($capability->component != $component or $capability->contextlevel != $contextlevel) {
        echo ('<tr class="rolecapheading header"><td colspan="10" class="header"><strong>'.
               get_component_string($capability->component, $capability->contextlevel).'</strong></td></tr>');
    }

    // these 2 are used to see to group same mod/core capabilities together
    $contextlevel = $capability->contextlevel;
    $component = $capability->component;

    if (empty($errors)) {
       // check the capability override for this cap, this role in this context
        $localoverride = get_local_override($roleid, $sitecontext->id, $capability->name);
    } else {
        $localoverride = new object();
        $localoverride->permission = $role->{$capability->name};
    }

    $disabled = ($action != 'edit' and $action != 'add') ? ' disabled="disabled" ' : '';

    $riskinfo = '<td class="risk managetrust">';
    $rowclasses = '';
    if (RISK_MANAGETRUST & (int)$capability->riskbitmask) {
        $riskinfo .= '<a onclick="this.target=\'docspopup\'" title="'.get_string('riskmanagetrust', 'admin').'" href="'.$CFG->docroot.'/'.$lang.'/'.$strrisks.'">';
        $riskinfo .= '<img src="'.$CFG->pixpath.'/i/risk_managetrust.gif" alt="'.get_string('riskmanagetrustshort', 'admin').'" /></a>';
        $rowclasses .= ' riskmanagetrust';
    }
    $riskinfo .= '</td><td class="risk config">';
    if (RISK_CONFIG & (int)$capability->riskbitmask) {
        $riskinfo .= '<a onclick="this.target=\'docspopup\'" title="'.get_string('riskconfig', 'admin').'" href="'.$CFG->docroot.'/'.$lang.'/'.$strrisks.'">';
        $riskinfo .= '<img src="'.$CFG->pixpath.'/i/risk_config.gif" alt="'.get_string('riskconfigshort', 'admin').'" /></a>';
        $rowclasses .= ' riskconfig';
    }
    $riskinfo .= '</td><td class="risk xss">';
    if (RISK_XSS & (int)$capability->riskbitmask) {
        $riskinfo .= '<a onclick="this.target=\'docspopup\'" title="'.get_string('riskxss', 'admin').'" href="'.$CFG->docroot.'/'.$lang.'/'.$strrisks.'">';
        $riskinfo .= '<img src="'.$CFG->pixpath.'/i/risk_xss.gif" alt="'.get_string('riskxssshort', 'admin').'" /></a>';
        $rowclasses .= ' riskxss';
    }
    $riskinfo .= '</td><td class="risk personal">';
    if (RISK_PERSONAL & (int)$capability->riskbitmask) {
        $riskinfo .= '<a onclick="this.target=\'docspopup\'" title="'.get_string('riskpersonal', 'admin').'" href="'.$CFG->docroot.'/'.$lang.'/'.$strrisks.'">';
        $riskinfo .= '<img src="'.$CFG->pixpath.'/i/risk_personal.gif" alt="'.get_string('riskpersonalshort', 'admin').'" /></a>';
        $rowclasses .= ' riskpersonal';
    }
    $riskinfo .= '</td><td class="risk spam">';
    if (RISK_SPAM & (int)$capability->riskbitmask) {
        $riskinfo .= '<a onclick="this.target=\'docspopup\'" title="'.get_string('riskspam', 'admin').'" href="'.$CFG->docroot.'/'.$lang.'/'.$strrisks.'">';
        $riskinfo .= '<img src="'.$CFG->pixpath.'/i/risk_spam.gif" alt="'.get_string('riskspamshort', 'admin').'" /></a>';
        $rowclasses .= ' riskspam';
    }
    $riskinfo .= '</td>';

    $isinherit  = (!isset($defaultcaps[$capability->name]) or $defaultcaps[$capability->name] == CAP_INHERIT) ? 'capdefault' : '';
    $isallow    = (isset($defaultcaps[$capability->name]) and $defaultcaps[$capability->name] == CAP_ALLOW) ? 'capdefault' : '';
    $isprevent  = (isset($defaultcaps[$capability->name]) and $defaultcaps[$capability->name] == CAP_PREVENT) ? 'capdefault' : '';
    $isprohibit = (isset($defaultcaps[$capability->name]) and $defaultcaps[$capability->name] == CAP_PROHIBIT) ? 'capdefault' : '';

    ?>

        <tr class="rolecap <?php echo $rowclasses; ?>">
        <td class="name"><span class="cap-desc"><a onclick="this.target='docspopup'" href="<?php echo $CFG->docroot.'/'.$lang.'/'.$strcapabilities.'/'.$capability->name ?>"><?php echo get_capability_string($capability->name); ?></a><span class="cap-name"><?php echo $capability->name ?></span></span></td>
        <td class="inherit <?php echo $isinherit ?>">
          <input type="radio" title="<?php p($strinherit); ?>" name="<?php echo $capability->name; ?>" value="<?php echo CAP_INHERIT ?>" <?php if (!isset($localoverride->permission) || $localoverride->permission==CAP_INHERIT){ echo 'checked="checked"'; }?> <?php echo $disabled; ?>/></td>
        <td class="allow <?php echo $isallow ?>">
          <input type="radio" title="<?php p($strallow); ?>" name="<?php echo $capability->name; ?>" value="<?php echo CAP_ALLOW ?>" <?php if (isset($localoverride->permission) && $localoverride->permission==CAP_ALLOW){ echo 'checked="checked"'; }?> <?php echo $disabled; ?>/></td>
        <td class="prevent <?php echo $isprevent ?>" >
          <input type="radio" title="<?php p($strprevent); ?>" name="<?php echo $capability->name; ?>" value="<?php echo CAP_PREVENT ?>" <?php if (isset($localoverride->permission) && $localoverride->permission==CAP_PREVENT){ echo 'checked="checked"'; }?> <?php echo $disabled; ?>/></td>
        <td class="prohibit <?php echo $isprohibit ?>" >
          <input type="radio" title="<?php p($strprohibit); ?>" name="<?php echo $capability->name; ?>" value="<?php echo CAP_PROHIBIT ?>" <?php if (isset($localoverride->permission) && $localoverride->permission==CAP_PROHIBIT){ echo 'checked="checked"'; }?> <?php echo $disabled; ?>/></td>

        <?php echo $riskinfo; ?>

        </tr>

<?php } ?>
</table>

<?php if ($action != 'view') { ?>
<div class="submit buttons">
<input type="submit" value="<?php p($submitlabel) ?>" />
<input type="submit" name="cancel" value="<?php print_string('cancel') ?>" />
</div>
<?php } ?>

</form>
