/** * Builds the XHTML to display the control * * @param string $data Unused * @param string $query * @return string */ public function output_html($data, $query = '') { global $CFG, $OUTPUT, $DB, $USER; // display strings $stroperation = get_string('operation', 'webservice'); $strtoken = get_string('token', 'webservice'); $strservice = get_string('service', 'webservice'); $struser = get_string('user'); $strcontext = get_string('context', 'webservice'); $strvaliduntil = get_string('validuntil', 'webservice'); $striprestriction = get_string('iprestriction', 'webservice'); $return = $OUTPUT->box_start('generalbox webservicestokenui'); $table = new html_table(); $table->head = array($strtoken, $struser, $strservice, $striprestriction, $strvaliduntil, $stroperation); $table->align = array('left', 'left', 'left', 'center', 'center', 'center'); $table->width = '100%'; $table->data = array(); $tokenpageurl = "{$CFG->wwwroot}/{$CFG->admin}/webservice/tokens.php?sesskey=" . sesskey(); //TODO: in order to let the administrator delete obsolete token, split this request in multiple request or use LEFT JOIN //here retrieve token list (including linked users firstname/lastname and linked services name) $sql = "SELECT t.id, t.token, u.id AS userid, u.firstname, u.lastname, s.name, t.iprestriction, t.validuntil, s.id AS serviceid\n FROM {external_tokens} t, {user} u, {external_services} s\n WHERE t.creatorid=? AND t.tokentype = ? AND s.id = t.externalserviceid AND t.userid = u.id"; $tokens = $DB->get_records_sql($sql, array($USER->id, EXTERNAL_TOKEN_PERMANENT)); if (!empty($tokens)) { foreach ($tokens as $token) { //TODO: retrieve context $delete = "<a href=\"" . $tokenpageurl . "&action=delete&tokenid=" . $token->id . "\">"; $delete .= get_string('delete') . "</a>"; $validuntil = ''; if (!empty($token->validuntil)) { $validuntil = date("F j, Y"); //TODO: language support (look for moodle function) } $iprestriction = ''; if (!empty($token->iprestriction)) { $iprestriction = $token->iprestriction; } $userprofilurl = new moodle_url('/user/profile.php?id=' . $token->userid); $useratag = html_writer::start_tag('a', array('href' => $userprofilurl)); $useratag .= $token->firstname . " " . $token->lastname; $useratag .= html_writer::end_tag('a'); //check user missing capabilities require_once $CFG->dirroot . '/webservice/lib.php'; $webservicemanager = new webservice(); $usermissingcaps = $webservicemanager->get_missing_capabilities_by_users(array(array('id' => $token->userid)), $token->serviceid); if (!is_siteadmin($token->userid) and key_exists($token->userid, $usermissingcaps)) { $missingcapabilities = implode(', ', $usermissingcaps[$token->userid]); if (!empty($missingcapabilities)) { $useratag .= html_writer::tag('div', get_string('usermissingcaps', 'webservice', $missingcapabilities) . ' ' . $OUTPUT->help_icon('missingcaps', 'webservice'), array('class' => 'missingcaps')); } } $table->data[] = array($token->token, $useratag, $token->name, $iprestriction, $validuntil, $delete); } $return .= html_writer::table($table); } else { $return .= get_string('notoken', 'webservice'); } $return .= $OUTPUT->box_end(); // add a token to the table $return .= "<a href=\"" . $tokenpageurl . "&action=create\">"; $return .= get_string('add') . "</a>"; return highlight($query, $return); }
$potentialuserselector->invalidate_selected_users(); $alloweduserselector->invalidate_selected_users(); } } /// Print the form. /// display the UI $renderer = $PAGE->get_renderer('core', 'webservice'); echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('selectauthorisedusers', 'webservice'), 3, 'main'); $selectoroptions = new stdClass(); $selectoroptions->serviceid = $id; $selectoroptions->alloweduserselector = $alloweduserselector; $selectoroptions->potentialuserselector = $potentialuserselector; echo $renderer->admin_authorised_user_selector($selectoroptions); /// get the missing capabilities for all users (will be displayed into the renderer) $allowedusers = $webservicemanager->get_ws_authorised_users($id); $usersmissingcaps = $webservicemanager->get_missing_capabilities_by_users($allowedusers, $id); //add the missing capabilities to the allowed users object to be displayed by renderer foreach ($allowedusers as &$alloweduser) { if (!is_siteadmin($alloweduser->id) and key_exists($alloweduser->id, $usersmissingcaps)) { $alloweduser->missingcapabilities = implode(',', $usersmissingcaps[$alloweduser->id]); } } /// display the list of allowed users with their options (ip/timecreated / validuntil...) //check that the user has the service required capability (if needed) if (!empty($allowedusers)) { $renderer = $PAGE->get_renderer('core', 'webservice'); echo $OUTPUT->heading(get_string('serviceuserssettings', 'webservice'), 3, 'main'); echo $renderer->admin_authorised_user_list($allowedusers, $id); } echo $OUTPUT->footer();