/**
  * Describes form.
  */
 public function definition()
 {
     global $CFG;
     // Setting variables
     $mform =& $this->_form;
     // Define available targets
     if (isset($this->_customdata['aplatforms'])) {
         $achoices = $this->_customdata['aplatforms'];
         if (empty($achoices)) {
             $achoices = array(get_string('none', 'local_vmoodle'));
         }
     } else {
         $achoices = get_available_platforms();
     }
     // Define selected targets
     if (isset($this->_customdata['splatforms']) && !empty($this->_customdata['splatforms'])) {
         $schoices = $this->_customdata['splatforms'];
     } else {
         $schoices = array(get_string('none', 'local_vmoodle'));
     }
     // Adding header.
     $mform->addElement('header', 'platformschoice', get_string('virtualplatforms', 'local_vmoodle'));
     // Adding hidden field.
     $mform->addElement('hidden', 'view', 'sadmin');
     $mform->setType('view', PARAM_TEXT);
     $mform->addElement('hidden', 'what', 'sendcommand');
     $mform->setType('what', PARAM_TEXT);
     $mform->addElement('hidden', 'achoices', json_encode($achoices));
     $mform->setType('achoices', PARAM_TEXT);
     // Adding selects group.
     $selectarray = array();
     $selectarray[0] =& $mform->createElement('select', 'aplatforms', get_string('available', 'local_vmoodle'), $achoices, 'size="15"');
     $selectarray[1] =& $mform->createElement('select', 'splatforms', get_string('selected', 'local_vmoodle'), $schoices, 'size="15"');
     $selectarray[0]->setMultiple(true);
     $selectarray[1]->setMultiple(true);
     $mform->addGroup($selectarray, 'platformsgroup', null, ' ', false);
     // Adding platforms buttons group.
     $buttonarray = array();
     $buttonarray[] =& $mform->createElement('button', null, get_string('addall', 'local_vmoodle'), 'onclick="select_all_platforms(); return false;"');
     $buttonarray[] =& $mform->createElement('button', null, get_string('addtoselection', 'local_vmoodle'), 'onclick="select_platforms(); return false;"');
     $buttonarray[] =& $mform->createElement('button', null, get_string('removefromselection', 'local_vmoodle'), 'onclick="unselect_platforms(); return false;"');
     $buttonarray[] =& $mform->createElement('button', null, get_string('removeall', 'local_vmoodle'), 'onclick="unselect_all_platforms(); return false;"');
     $mform->addGroup($buttonarray);
     // Adding submit buttons group.
     $buttonarray = array();
     $buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('nextstep', 'local_vmoodle'));
     $buttonarray[] = $mform->createElement('cancel', 'cancelbutton', get_string('cancelcommand', 'local_vmoodle'));
     $mform->addGroup($buttonarray);
     // Changing renderer.
     $renderer =& $mform->defaultRenderer();
     $template = '<label class="qflabel" style="vertical-align:top">{label}</label> {element}';
     $renderer->setGroupElementTemplate($template, 'platformsgroup');
 }
 /**
  * Execute the command.
  * @param mixed $hosts The host where run the command (may be wwwroot or an array).
  * @throws Command_Exception.
  */
 public function run($hosts)
 {
     global $CFG, $USER;
     // Adding constants.
     require_once $CFG->dirroot . '/local/vmoodle/rpclib.php';
     // Checking capabilities.
     if (!has_capability('local/vmoodle:execute', \context_system::instance())) {
         throw new Command_Exception('insuffisantcapabilities');
     }
     // Getting role.
     $role = $this->getParameter('role')->getValue();
     // Getting platform.
     $platform = $this->getParameter('platform')->getValue();
     // Getting capability.
     $capability = $this->getParameter('capability')->getValue();
     // Checking hosts.
     if (array_key_exists($platform, $hosts)) {
         $platforms = get_available_platforms();
         throw new Command_Role_Exception('syncwithitself', (object) array('role' => $role, 'platform' => $platforms[$platform]));
     }
     // Creating peer to read role configuration.
     $mnet_host = new \mnet_peer();
     if (!$mnet_host->bootstrap($this->getParameter('platform')->getValue(), null, 'moodle')) {
         $response = (object) array('status' => MNET_FAILURE, 'error' => get_string('couldnotcreateclient', 'local_vmoodle', $platform));
         foreach ($hosts as $host => $name) {
             $this->results[$host] = $response;
         }
         return;
     }
     // Creating XMLRPC client to read role configuration.
     $rpc_client = new \local_vmoodle\XmlRpc_Client();
     $rpc_client->set_method('local/vmoodle/plugins/roles/rpclib.php/mnetadmin_rpc_get_role_capabilities');
     $rpc_client->add_param($role, 'string');
     $rpc_client->add_param($capability, 'string');
     // Checking result.
     if (!($rpc_client->send($mnet_host) && ($response = json_decode($rpc_client->response)) && ($response->status == RPC_SUCCESS || $response->status == RPC_FAILURE_RECORD && (in_array($response->errors, 'No capabilites for this role.') || in_array($response->error, 'No role capability found.'))))) {
         // Creating response.
         if (!isset($response)) {
             $response = new \StdClass();
             $response->status = MNET_FAILURE;
             $response->errors[] = implode('<br/>', $rpc_client->getErrors($mnet_host));
         }
         if (debugging()) {
             echo '<pre>';
             var_dump($rpc_client);
             ob_flush();
             echo '</pre>';
         }
         // Saving results.
         foreach ($hosts as $host => $name) {
             $this->results[$host] = $response;
         }
         return;
     }
     // Getting role configuration.
     if ($response->status == RPC_FAILURE_RECORD) {
         $role_capability = array($capability => null);
     } else {
         $role_capability = (array) $response->value;
     }
     unset($response);
     // Initializing responses.
     $responses = array();
     // Creating peers.
     $mnet_hosts = array();
     foreach ($hosts as $host => $name) {
         $mnet_host = new \mnet_peer();
         if ($mnet_host->bootstrap($host, null, 'moodle')) {
             $mnet_hosts[] = $mnet_host;
         } else {
             $responses[$host] = (object) array('status' => MNET_FAILURE, 'error' => get_string('couldnotcreateclient', 'local_vmoodle', $host));
         }
     }
     // Creating XMLRPC client.
     $rpc_client = new \local_vmoodle\XmlRpc_Client();
     $rpc_client->set_method('local/vmoodle/plugins/roles/rpclib.php/mnetadmin_rpc_set_role_capabilities');
     $rpc_client->add_param($role, 'string');
     $rpc_client->add_param($role_capability, 'string');
     $rpc_client->add_param(false, 'boolean');
     // Sending requests.
     foreach ($mnet_hosts as $mnet_host) {
         // Sending request.
         if (!$rpc_client->send($mnet_host)) {
             $response = new \StdClass();
             $response->status = MNET_FAILURE;
             $response->errors[] = implode('<br/>', $rpc_client->getErrors($mnet_host));
             $response->error = 'Remote Set role capability : Remote proc error';
             if (debugging()) {
                 echo '<pre>';
                 var_dump($rpc_client);
                 ob_flush();
                 echo '</pre>';
             }
         } else {
             $response = json_decode($rpc_client->response);
             $response->errors[] = implode('<br/>', $response->errors);
         }
         // Recording response
         $responses[$mnet_host->wwwroot] = $response;
     }
     // Saving results
     $this->results = $responses + $this->results;
 }
 /**
  * Process the plugin comparison.
  * @throws Commmand_Exception.
  */
 private function _process()
 {
     global $CFG, $DB, $OUTPUT, $STANDARD_PLUGIN_TYPES, $PAGE;
     $renderer = $PAGE->get_renderer('local_vmoodle');
     // Checking if command has been runned.
     if (!$this->isRunned()) {
         throw new Command_Exception('commandnotrun');
     }
     // Getting examined plugintype.
     $plugintype = $this->getParameter('plugintype')->getValue();
     // Getting hosts.
     $hosts = array_keys($this->plugins);
     $host_labels = get_available_platforms();
     // Getting local plugin info.
     $pm = \plugin_manager::instance();
     $localplugins = $pm->get_plugins();
     $localtypeplugins = $localplugins[$plugintype];
     /*
      * Creating html report.
      */
     // Creating header.
     $this->report = '<link href="' . $CFG->wwwroot . '/local/vmoodle/plugins/plugins/theme/styles.css" rel="stylesheet" type="text/css">';
     $this->report .= '<h3>' . vmoodle_get_string('compareplugins', 'vmoodleadminset_plugins', $STANDARD_PLUGIN_TYPES[$plugintype]) . '</h3>';
     // Adding link to plugin management
     /* $this->report.= '<center><p>'.$OUTPUT->single_button(new moodle_url($CFG->wwwroot.'/admin/roles/define.php', array('roleid' => $role->id, 'action' => 'edit')), get_string('editrole', 'vmoodleadminset_roles'), 'get').'</p></center>'; */
     // Creation form
     $this->report .= '<form action="' . $CFG->wwwroot . '/local/vmoodle/plugins/plugins/controller.pluginlib.sadmin.php?what=syncplugins" method="post" onsubmit="return validate_syncplugins()">';
     $this->report .= '<input id="id_plugin" type="hidden" name="plugin" value=""/>';
     $this->report .= '<input id="source_platform" type="hidden" name="source_platform" value=""/>';
     // Creating table.
     $this->report .= '<table id="plugincompare" cellspacing="1" cellpadding="5" class="generaltable boxaligncenter" style="min-width: 75%;">';
     $this->report .= '<tbody>';
     // Creating header.
     $this->report .= '<tr><th scope="col" class="header c0" style="vertical-align: bottom; text-align: left;">&nbsp</th>';
     $col = 1;
     foreach ($hosts as $host) {
         $this->report .= '<th id="plug_' . $col . '" scope="col" class="header c' . $col . '" style="vertical-align: bottom; text-align: center;">';
         $this->report .= '<label for="platform_' . $col . '"><img src="' . $CFG->wwwroot . '/local/vmoodle/plugins/plugins/draw_platformname.php?caption=' . urlencode($host_labels[$host]) . '" alt="' . $host_labels[$host] . '"/></label><br/>';
         $this->report .= '<input id="platform_' . $col . '" type="checkbox" name="platforms[]" value="' . $host . '" disabled="disabled"/></th>';
         $col++;
     }
     $this->report .= '</tr>';
     // Initializing variables.
     $row = 0;
     // Creating table data.
     foreach ($localtypeplugins as $plugin) {
         $col = 1;
         $this->report .= '<tr class="r' . $row % 2 . '">';
         $this->report .= '<td id="plug_0_' . $row . '" class="cell c0" style="vertical-align: middle; text-align: left;" onClic="setPLugin(' . $col . ',' . $row . ',\'' . $plugin->name . '\',\'' . $host . '\')">';
         $this->report .= $plugin->displayname;
         $this->report .= '</td>';
         foreach ($hosts as $host) {
             $extra_class = false;
             $title = $plugin->displayname . ' | ' . $host_labels[$host];
             if (array_key_exists($host, $this->plugins) && property_exists($this->plugins[$host], $plugin->name)) {
                 $remote_plugin = $this->plugins[$host]->{$plugin->name};
                 if (is_null($remote_plugin)) {
                     $cell = '<img src="' . $renderer->pix_url('notinstalled', 'vmoodleadminset_plugins') . ' alt="Not installed" title="' . $title . '" />';
                 } else {
                     if ($remote_plugin->enabled) {
                         $cell = '<img src="' . $renderer->pix_url('enabled', 'vmoodleadminset_plugins') . '" title="' . $title . '" />';
                     } else {
                         $cell = '<img src="' . $renderer->pix_url('disabled', 'vmoodleadminset_plugins') . '" title="' . $title . '" />';
                     }
                     if ($localtypeplugins[$plugin->name]->versiondb > $remote_plugin->versiondb) {
                         $cell .= '&nbsp;<img src="' . $renderer->pix_url('needsupgrade', 'vmoodleadminset_plugins') . '" title="' . $title . '" />';
                     }
                     if ($remote_plugin->versiondisk > $remote_plugin->versiondb) {
                         $cell .= '&nbsp;<img src="' . $renderer->pix_url('needslocalupgrade', 'vmoodleadminset_plugins') . '" title="' . $title . '" />';
                     }
                 }
             } else {
                 $cell = '<img src="' . $renderer->pix_url('notinstalled', 'vmoodleadminset_plugins') . '" alt="Not installed" title="' . $title . '"/>';
             }
             $this->report .= '<td id="plug_' . $col . '_' . $row . '" class="cell c' . $col . ($extra_class ? ' ' . $extra_class : '') . '" style="vertical-align: middle; text-align: center;" onmouseout="cellOut(' . $col . ',' . $row . ');" onmouseover="cellOver(' . $col . ',' . $row . ');">' . $cell . '</td>';
             $col++;
         }
         $this->report .= '</tr>';
         $row++;
     }
     // Closing table
     $this->report .= '</tboby></table><br/><center><input type="submit" value="' . get_string('synchronize', 'vmoodleadminset_plugins') . '"/><div id="plugincompare_validation_message"></div></center></form><br/><br/>';
 }
if (!has_capability('local/vmoodle:managevmoodles', context_system::instance())) {
    print_error('onlyadministrators', 'local_vmoodle');
}
// Declaring parameters.
$action = optional_param('what', '', PARAM_TEXT);
// Checking action to do.
switch ($action) {
    // Run sync role command.
    case 'syncrole':
        // Getting parameters.
        $capability = optional_param('capability', '', PARAM_RAW);
        $source_platform = optional_param('source_platform', '', PARAM_RAW);
        $wwwroot_platforms = optional_param_array('platforms', null, PARAM_RAW);
        // Checking platforms.
        $valid = true;
        $available_plaforms = get_available_platforms();
        if (!array_key_exists($source_platform, $available_plaforms)) {
            $valid = false;
        } else {
            $platforms = array();
            foreach ($wwwroot_platforms as $wwwroot_platform) {
                if (!array_key_exists($wwwroot_platform, $available_plaforms)) {
                    $valid = false;
                    break;
                }
                $platforms[$wwwroot_platform] = $available_plaforms[$wwwroot_platform];
            }
        }
        if (!$valid) {
            redirect(new moodle_url('/local/vmoodle/view.php', array('view' => 'sadmin')));
        }
 /**
  * Execute the command.
  * @param mixed $hosts The host where run the command (may be wwwroot or an array).
  * @throws Command_Exception
  */
 function run($hosts)
 {
     global $CFG, $USER;
     // Adding constants.
     require_once $CFG->dirroot . '/local/vmoodle/rpclib.php';
     // Checking capabilities.
     if (!has_capability('local/vmoodle:execute', context_system::instance())) {
         throw new Command_Exception('insuffisantcapabilities');
     }
     // Getting plugintype.
     $plugintype = $this->getParameter('plugintype')->getValue();
     // Checking hosts.
     $platform = $this->getParameter('platform')->getValue();
     if (array_key_exists($platform, $hosts)) {
         $platforms = get_available_platforms();
         throw new Command_Plugins_Exception('syncwithitself');
     }
     // Creating peer to read plugins configuration from the designated peer.
     $mnet_host = new mnet_peer();
     if (!$mnet_host->bootstrap($this->getParameter('platform')->getValue(), null, 'moodle')) {
         $response = (object) array('status' => MNET_FAILURE, 'error' => get_string('couldnotcreateclient', 'local_vmoodle', $platform));
         // if we fail, we fail for all.
         foreach ($hosts as $host => $name) {
             $this->results[$host] = $response;
         }
         return;
     }
     // Creating XMLRPC client to read plugins configuration.
     $rpc_client = new \local_vmoodle\XmlRpc_Client();
     $rpc_client->set_method('local/vmoodle/plugins/plugins/rpclib.php/mnetadmin_rpc_get_plugins_info');
     $rpc_client->add_param($plugintype, 'string');
     // Checking result.
     if (!($rpc_client->send($mnet_host) && ($response = json_decode($rpc_client->response)) && $response->status == RPC_SUCCESS)) {
         // Creating response.
         if (!isset($response)) {
             $response = new Stdclass();
             $response->status = MNET_FAILURE;
             $response->errors[] = implode('<br/>', $rpc_client->getErrors($mnet_host));
             $response->error = implode('<br/>', $rpc_client->getErrors($mnet_host));
         }
         if (debugging()) {
             echo '<pre>';
             var_dump($rpc_client);
             ob_flush();
             echo '</pre>';
         }
         // result is a plugin info structure that needs be replicated remotely to all targets.
         $plugininfos = $response->value;
         return;
     }
     // Initializing responses.
     $responses = array();
     // Creating peers
     $mnet_hosts = array();
     foreach ($hosts as $host => $name) {
         $mnet_host = new mnet_peer();
         if ($mnet_host->bootstrap($host, null, 'moodle')) {
             $mnet_hosts[] = $mnet_host;
         } else {
             $responses[$host] = (object) array('status' => MNET_FAILURE, 'error' => get_string('couldnotcreateclient', 'local_vmoodle', $host));
         }
     }
     // Creating XMLRPC client
     $rpc_client = new \local_vmoodle\XmlRpc_Client();
     $rpc_client->set_method('local/vmoodle/plugins/plugins/rpclib.php/mnetadmin_rpc_set_plugins_states');
     $rpc_client->add_param($plugininfos, 'object');
     // plugininfos structure
     // Sending requests.
     foreach ($mnet_hosts as $mnet_host) {
         // Sending request
         if (!$rpc_client->send($mnet_host)) {
             $response = new Stdclass();
             $response->status = MNET_FAILURE;
             $response->errors[] = implode('<br/>', $rpc_client->getErrors($mnet_host));
             $response->error = 'Set plugin state failed : Remote call error';
             if (debugging()) {
                 echo '<pre>';
                 var_dump($rpc_client);
                 ob_flush();
                 echo '</pre>';
             }
         } else {
             $response = json_decode($rpc_client->response);
         }
         // Recording response.
         $responses[$mnet_host->wwwroot] = $response;
     }
     // Saving results.
     $this->results = $responses + $this->results;
 }
 /**
  * Process the role comparision.
  * @throws Commmand_Exception.
  */
 private function _process()
 {
     global $CFG, $DB, $OUTPUT;
     // Checking if command has been runned.
     if (!$this->isRunned()) {
         throw new Command_Exception('commandnotrun');
     }
     // Getting table name.
     $table = $this->getParameter('table')->getValue();
     // Getting hosts.
     $hosts = array_keys($this->capabilities);
     $host_labels = get_available_platforms();
     // Getting local roles.
     $roles = $DB->get_records('role', null, '', 'sortorder');
     /*
      * processing results
      */
     // Creating header.
     $this->report = '<h3>' . get_string('allowcompare', 'vmoodleadminset_roles', vmoodle_get_string($table . 'table', 'vmoodleadminset_roles')) . help_button_vml('rolelib', 'allowcompare', 'vmoodleadminset_roles') . '</h3>';
     // Adding edit role link
     $this->report .= '<center><p>' . $OUTPUT->single_button(new moodle_url($CFG->wwwroot . '/admin/roles/allow.php?mode=' . $table, array('roleid' => $role->id, 'action' => 'edit')), vmoodle_get_string('editallowtable', 'vmoodleadminset_roles'), 'get') . '</p></center>';
     // Creation form.
     $this->report .= '<form action="' . $CFG->wwwroot . '/local/vmoodle/plugins/roles/controller.rolelib.sadmin.php?what=syncallow" method="post" onsubmit="return validate_syncrole()">';
     $this->report .= '<input id="target" type="hidden" name="target" value=""/>';
     $this->report .= '<input id="role" type="hidden" name="role" value=""/>';
     $this->report .= '<input id="source_platform" type="hidden" name="source_platform" value=""/>';
     // Creating table.
     $this->report .= '<table id="allowcompare" cellspacing="1" cellpadding="5" class="generaltable boxaligncenter" style="min-width: 75%;"><tbody>';
     // Creating header.
     $this->report .= '<tr><th scope="col" class="header c0" style="vertical-align: bottom; text-align: left;">&nbsp</th>';
     $col = 1;
     foreach ($hosts as $host) {
         $this->report .= '<th id="cap_' . $col . '" scope="col" class="header c' . $col . '" style="vertical-align: bottom; text-align: center;"><label for="platform_' . $col . '"><img src="' . $CFG->wwwroot . '/local/vmoodle/plugins/roles/draw_platformname.php?caption=' . urlencode($host_labels[$host]) . '" alt="' . $host_labels[$host] . '"/></label><br/><input id="platform_' . $col . '" type="checkbox" name="platforms[]" value="' . $host . '" disabled="disabled"/></th>';
         $col++;
     }
     $this->report .= '</tr>';
     // Initializing variables.
     $row = 0;
     // Creating table data.
     foreach ($allroles as $rolename => $role) {
         $localrole = $DB->get_field('role', 'name', array('shortname' => $rolename));
         $displayrole = $localrole ? $localrole : '--' . $rolename . '--';
         $this->report .= "<tr valign='top'>{$displayrole}</td>";
         $row++;
     }
     // Closing table.
     $this->report .= '</tboby></table><br/>';
     $this->report .= '<center><input type="submit" value="' . vmoodle_get_string('synchronize', 'vmoodleadminset_roles') . '"/><div id="allowcompare_validation_message"></div></center></form><br/><br/>';
 }
 /**
  * Process the role comparison.
  * @throws Commmand_Exception.
  */
 private function _process()
 {
     global $CFG, $DB, $OUTPUT;
     // Checking if command has been runned.
     if (!$this->isRunned()) {
         throw new Command_Exception('commandnotrun');
     }
     // Defining capabilities values.
     $cap_permissions = array(CAP_ALLOW => array('count' => 0, 'label' => 1, 'name' => 'allow'), CAP_PREVENT => array('count' => 0, 'label' => 2, 'name' => 'prevent'), CAP_PROHIBIT => array('count' => 0, 'label' => 3, 'name' => 'prohibit'));
     // Defining capabilities context.
     $cap_contexts = array(CONTEXT_BLOCK => array('count' => 0, 'label' => 'B', 'name' => 'block'), CONTEXT_COURSE => array('count' => 0, 'label' => 'C', 'name' => 'course'), CONTEXT_COURSECAT => array('count' => 0, 'label' => 'CC', 'name' => 'coursecat'), CONTEXT_MODULE => array('count' => 0, 'label' => 'M', 'name' => 'module'), CONTEXT_SYSTEM => array('count' => 0, 'label' => 'S', 'name' => 'system'), CONTEXT_USER => array('count' => 0, 'label' => 'U', 'name' => 'user'));
     // Getting role name.
     $role = $this->getParameter('role')->getValue();
     $role = $DB->get_record('role', array('shortname' => $role));
     // Getting hosts.
     $hosts = array_keys($this->capabilities);
     $host_labels = get_available_platforms();
     // Getting capabilities.
     $records_capabilities = $DB->get_records('capabilities', null, '', 'name,contextlevel,component');
     // Getting lang.
     $lang = str_replace('_utf8', '', current_language());
     $strcapabilities = s(get_string('capabilities', 'role'));
     // 'Capabilities' MDL-11687
     // Getting all capabilities names.
     $capability_names = array();
     foreach ($this->capabilities as $platform_capabilities) {
         $platform_capabilities = array_keys((array) $platform_capabilities);
         $capability_names = array_merge($capability_names, $platform_capabilities);
     }
     $capability_names = array_unique($capability_names);
     // Getting problematic component name
     $problematic_component_name = get_string('problematiccomponent', 'vmoodleadminset_roles');
     // Creating normalized capabilities.
     $capabilities = array();
     foreach ($capability_names as $capability_name) {
         // Creating capability.
         $capability = new \StdClass();
         $capability->name = $capability_name;
         // Initializing counters.
         $cap_permissions[CAP_ALLOW]['count'] = $cap_permissions[CAP_PREVENT]['count'] = $cap_permissions[CAP_PROHIBIT]['count'] = 0;
         $cap_contexts[CONTEXT_BLOCK]['count'] = $cap_contexts[CONTEXT_COURSE]['count'] = $cap_contexts[CONTEXT_COURSECAT]['count'] = $cap_contexts[CONTEXT_MODULE]['count'] = $cap_contexts[CONTEXT_SYSTEM]['count'] = $cap_contexts[CONTEXT_USER]['count'] = 0;
         // Counting.
         foreach ($this->capabilities as $platform_capabilities) {
             if (!property_exists($platform_capabilities, $capability_name) || is_null($platform_capabilities->{$capability_name})) {
                 continue;
             }
             $platform_capability = $platform_capabilities->{$capability_name};
             $cap_permissions[$platform_capability->permission]['count']++;
             $cap_contexts[$platform_capability->contextlevel]['count']++;
         }
         // Getting major values.
         $nbr_value_max = max(array_map(array($this, '_getCounterValue'), $cap_permissions));
         $nbr_context_max = max(array_map(array($this, '_getCounterValue'), $cap_contexts));
         // Setting major permission.
         foreach ($cap_permissions as $permission => $cap_permission) {
             if ($cap_permission['count'] == $nbr_value_max) {
                 $capability->major_permission = $permission;
                 break;
             }
         }
         // Setting major contexlevel.
         foreach ($cap_contexts as $contextlevel => $cap_context) {
             if ($cap_context['count'] == $nbr_context_max) {
                 $capability->major_contextlevel = $contextlevel;
                 break;
             }
         }
         // Setting component.
         $capability->component = isset($records_capabilities[$capability_name]) ? $records_capabilities[$capability_name]->component : $problematic_component_name;
         // Setting capability contextlevel.
         $capability->contextlevel = isset($records_capabilities[$capability_name]) ? $records_capabilities[$capability_name]->contextlevel : CONTEXT_SYSTEM;
         // Adding capability.
         $capabilities[$capability_name] = $capability;
     }
     // Sort capabilities on contextlevel, component and name.
     uasort($capabilities, array($this, '_orderCapability'));
     /*
      * Creating html report.
      */
     // Creating header.
     $this->report = '<h3>' . get_string('comparerole', 'vmoodleadminset_roles', $role->name) . help_button_vml('rolelib', 'rolecompare', 'rolecompare') . '</h3>';
     // Adding edit role link.
     $this->report .= '<center><p>' . $OUTPUT->single_button(new moodle_url('/admin/roles/define.php', array('roleid' => $role->id, 'action' => 'edit')), get_string('editrole', 'vmoodleadminset_roles'), 'get') . '</p></center>';
     // Adding a capability client side filter.
     $this->report .= get_string('capfilter', 'local_vmoodle') . ': ' . '<input type="text" name="capfilter" value="" onchange="filtercapabilitytable(this)" />';
     // Creation form.
     $rolecapsyncurl = new moodle_url('/local/vmoodle/plugins/roles/controller.rolelib.sadmin.php', array('what' => 'syncrole'));
     $this->report .= '<form action="' . $rolecapsyncurl . '" method="post" onsubmit="return validate_syncrole()">';
     $this->report .= '<input id="capability" type="hidden" name="capability" value="" />';
     $this->report .= '<input id="source_platform" type="hidden" name="source_platform" value="" />';
     // Creating table.
     $this->report .= '<table id="rolecompare" cellspacing="1" cellpadding="5" class="generaltable boxaligncenter" style="min-width: 75%;"><tbody>';
     // Creating header.
     $this->report .= '<tr><th scope="col" class="header c0" style="vertical-align: bottom; text-align: left;">&nbsp</th>';
     $col = 1;
     foreach ($hosts as $host) {
         $this->report .= '<th id="cap_' . $col . '" scope="col" class="header c' . $col . '" style="vertical-align: bottom; text-align: center;"><label for="platform_' . $col . '"><img src="' . $CFG->wwwroot . '/local/vmoodle/plugins/roles/draw_platformname.php?caption=' . urlencode($host_labels[$host]) . '" alt="' . $host_labels[$host] . '"/></label><br/><input id="platform_' . $col . '" type="checkbox" name="platforms[]" value="' . $host . '" disabled="disabled"/></th>';
         $col++;
     }
     $this->report .= '</tr>';
     // Initializing variables.
     $row = 0;
     $contextlevel = 0;
     $component = '';
     $rowtitleids = array();
     // Creating table data.
     foreach ($capabilities as $capability) {
         $col = 1;
         $componentlevelchanged = component_level_changed($capability, $component, $contextlevel);
         // Recording context.
         $contextlevel = $capability->contextlevel;
         $component = $capability->component;
         $rowtitleids[] = $capability->name;
         $rowcontent = '<tr class="r' . $row % 2 . ' capabilityrow" id="' . $capability->name . '">';
         $rowcontent .= '<td id="cap_0_' . $row . '" class="cell c0" style="vertical-align: middle; text-align: left;"><a onclick="this.target=\'docspopup\'" href="' . $CFG->docroot . '/' . $lang . '/' . $strcapabilities . '/' . $capability->name . '">' . get_capability_string($capability->name) . '</a><br/>' . $capability->name . '</td>';
         foreach ($hosts as $host) {
             $extra_class = false;
             $title = get_capability_string($capability->name) . ' | ' . $host_labels[$host];
             if (array_key_exists($host, $this->capabilities) && property_exists($this->capabilities[$host], $capability->name)) {
                 $platform_capability = $this->capabilities[$host]->{$capability->name};
                 if (is_null($platform_capability)) {
                     $cell = '<img src="' . $CFG->wwwroot . '/local/vmoodle/plugins/roles/pix/norolecapability.png" alt="No role capability" title="' . $title . '" onclick="setCapability(' . $col . ',' . $row . ',\'' . $capability->name . '\',\'' . $host . '\');"/>';
                 } else {
                     $cell = '<img src="' . $CFG->wwwroot . '/local/vmoodle/plugins/roles/pix/compare' . $cap_permissions[$platform_capability->permission]['label'] . $cap_contexts[$platform_capability->contextlevel]['label'] . '.png" alt="Permission: ' . $cap_permissions[$platform_capability->permission]['name'] . ' | Context: ' . $cap_contexts[$platform_capability->contextlevel]['name'] . '" title="' . $title . '" onclick="setCapability(' . $col . ',' . $row . ',\'' . $capability->name . '\',\'' . $host . '\');"/>';
                     if ($platform_capability->permission != $capabilities[$platform_capability->capability]->major_permission) {
                         $extra_class = 'wrongvalue';
                     } elseif ($platform_capability->contextlevel != $capabilities[$platform_capability->capability]->major_contextlevel) {
                         $extra_class = 'wrongcontext';
                     }
                 }
             } else {
                 $cell = '<img src="' . $CFG->wwwroot . '/local/vmoodle/plugins/roles/pix/nocapability.png" alt="No capability" title="' . $title . '"/>';
             }
             $rowcontent .= '<td id="cap_' . $col . '_' . $row . '" class="cell c' . $col . ($extra_class ? ' ' . $extra_class : '') . '" style="vertical-align: middle; text-align: center;" onmouseout="cellOut(' . $col . ',' . $row . ');" onmouseover="cellOver(' . $col . ',' . $row . ');">' . $cell . '</td>';
             $col++;
         }
         // Adding contextual heading.
         if ($componentlevelchanged) {
             $rowhead = '<tr class="capabilityrow" id="' . implode(',', $rowtitleids) . '"><td colspan="' . (count($hosts) + 1) . '" class="header"><strong>' . ($capability->component == $problematic_component_name ? $problematic_component_name : get_component_string($capability->component, $capability->contextlevel)) . '</strong></td></tr>';
             $rowtitleids = array();
         }
         $this->report .= $rowhead . $rowcontent . '</tr>';
         $row++;
     }
     // Closing table.
     $this->report .= '</tboby></table><br/><center><input type="submit" value="' . get_string('synchronize', 'vmoodleadminset_roles') . '"/><div id="rolecompare_validation_message"></div></center></form><br/><br/>';
 }