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

<form id="overrideform" action="override.php" method="post">
    <fieldset class="invisiblefieldset">
    <input type="hidden" name="contextid" value="<?php p($contextid) ?>" />
    <input type="hidden" name="roleid" value="<?php p($roleid) ?>" />
    <input type="hidden" name="sesskey" value="<?php p(sesskey()) ?>" />
    <input type="hidden" name="userid" value="<?php p($userid) ?>" />
    <input type="hidden" name="courseid" value="<?php p($courseid) ?>" />
    </fieldset>

    <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'));

        foreach ($capabilities as $capability) {

            // legacy caps should not be overriden - we must use proper capabilities if needed
            if (islegacy($capability->name)) {
                continue;
            }

        // prints a breaker if component or name or context level
            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;

            // check the capability override for this cap, this role in this context
            if (isset($localoverrides[$capability->name])) {
                $localpermission = $localoverrides[$capability->name]->permission;
            } else {
                $localpermission = 0;  // Just inherit
            }

            if (isset($r_caps[$capability->name])) {
                $isallow = $r_caps[$capability->name] > 0;
                $isprevent = $r_caps[$capability->name] < 0 && $r_caps[$capability->name] > -500;
                $isprohibit = $r_caps[$capability->name] <= -500;
            } else {
                $isallow = 0;
                $isprevent = 0;
                $isprohibit = 0;
            }

            $isdisabled = $isprohibit;

            $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>';


        ?>

        <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">
             <input type="radio" title="<?php p($strinherit); ?>" name="<?php echo $capability->name; ?>" value="0"
                <?php
                    if ($localpermission == CAP_INHERIT) {echo 'checked="checked" ';}
                    if ($isdisabled)           {echo 'disabled="disabled"';}?> />
             </td>

             <td class="allow <?php if ($isallow) {echo ' capcurrent';} ?>">
             <input type="radio" title="<?php p($strallow); ?>" name="<?php echo $capability->name; ?>"
                <?php echo ' value="'.CAP_ALLOW.'"';
                   if ($localpermission == CAP_ALLOW) {echo ' checked="checked" ';}
                   if ($isdisabled)                   {echo ' disabled="disabled"';}?> />
             </td>

             <td class="prevent <?php if ($isprevent) {echo ' capcurrent';} ?>">
             <input type="radio" title="<?php p($strprevent); ?>" name="<?php echo $capability->name; ?>"
                <?php echo ' value="'.CAP_PREVENT.'"';
                   if ($localpermission == CAP_PREVENT) {echo ' checked="checked" ';}
                   if ($isdisabled)                     {echo ' disabled="disabled"';}?> />
             </td>

             <td class="prohibit <?php if ($isprohibit) {echo ' capcurrent';} ?>">
             <input type="radio" title="<?php p($strprohibit); ?>" name="<?php echo $capability->name; ?>"
                <?php echo ' value="'.CAP_PROHIBIT.'"';
                   if ($localpermission == CAP_PROHIBIT) {echo ' checked="checked" ';}
                   if ($isdisabled)                      {echo ' disabled="disabled"';}?> />
             </td>
            <?php echo $riskinfo; ?>
        </tr>

        <?php } ?>
    </table>
    <div class="submit buttons">
        <input type="submit" value="<?php print_string('savechanges') ?>" />
        <input type="submit" name="cancel" value="<?php print_string('cancel') ?>" />
    </div>
</form>
