diff --git a/admin/settings/plugins.php b/admin/settings/plugins.php index 14050729da3..7ff697de226 100644 --- a/admin/settings/plugins.php +++ b/admin/settings/plugins.php @@ -330,6 +330,7 @@ if ($hassiteconfig) { $ADMIN->add('webservicesettings', new admin_externalpage('externalservice', get_string('externalservice', 'webservice'), "$CFG->wwwroot/$CFG->admin/webservice/service.php", 'moodle/site:config', true)); $ADMIN->add('webservicesettings', new admin_externalpage('externalservicefunctions', get_string('externalservicefunctions', 'webservice'), "$CFG->wwwroot/$CFG->admin/webservice/service_functions.php", 'moodle/site:config', true)); $ADMIN->add('webservicesettings', new admin_externalpage('externalserviceusers', get_string('externalserviceusers', 'webservice'), "$CFG->wwwroot/$CFG->admin/webservice/service_users.php", 'moodle/site:config', true)); + $ADMIN->add('webservicesettings', new admin_externalpage('externalserviceusersettings', get_string('serviceusersettings', 'webservice'), "$CFG->wwwroot/$CFG->admin/webservice/service_user_settings.php", 'moodle/site:config', true)); /// manage protocol page link $temp = new admin_settingpage('webserviceprotocols', get_string('manageprotocols', 'webservice')); $temp->add(new admin_setting_managewebserviceprotocols()); diff --git a/admin/webservice/forms.php b/admin/webservice/forms.php index 2c77dd6c34a..9f26ce9a6fc 100644 --- a/admin/webservice/forms.php +++ b/admin/webservice/forms.php @@ -25,6 +25,32 @@ require_once $CFG->libdir.'/formslib.php'; +/** + * Display the authorised user settings form + * Including IP Restriction, Valid until and (TODO) capability + */ +class external_service_authorised_user_settings_form extends moodleform { + function definition() { + global $CFG, $USER, $DB; + + $mform = $this->_form; + $data = $this->_customdata; + + $mform->addElement('header', 'serviceusersettings', get_string('serviceusersettings', 'webservice')); + + $mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice')); + $mform->addHelpButton('iprestriction', 'iprestriction', 'webservice'); + + $mform->addElement('date_selector', 'validuntil', get_string('validuntil', 'webservice'), array('optional'=>true)); + $mform->addHelpButton('validuntil', 'validuntil', 'webservice'); + + $this->add_action_buttons(true, get_string('updateusersettings', 'webservice')); + + $this->set_data($data); + } +} + + class external_service_form extends moodleform { function definition() { global $CFG, $USER; diff --git a/admin/webservice/script.js b/admin/webservice/script.js deleted file mode 100644 index a7d9f7a2f1c..00000000000 --- a/admin/webservice/script.js +++ /dev/null @@ -1,13 +0,0 @@ - -/* This function disable the valid until field of a user into service_users.php*/ -function external_disablevaliduntil(event, userid) { - var disabled; - if (document.getElementById('enablevaliduntil'+userid).checked) { - disabled = false; - } else { - disabled = true; - } - document.getElementById('menufromday'+userid).disabled = disabled; - document.getElementById('menufromyear'+userid).disabled = disabled; - document.getElementById('menufrommonth'+userid).disabled = disabled; -} \ No newline at end of file diff --git a/admin/webservice/service_user_settings.php b/admin/webservice/service_user_settings.php new file mode 100644 index 00000000000..f6aacd9bbf0 --- /dev/null +++ b/admin/webservice/service_user_settings.php @@ -0,0 +1,84 @@ +. + +/** + * Web services user settings UI + * + * @package webservice + * @copyright 2009 Moodle Pty Ltd (http://moodle.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +require_once('../../config.php'); +require_once($CFG->libdir . '/adminlib.php'); +require_once($CFG->dirroot . '/webservice/lib.php'); +require_once($CFG->dirroot . '/admin/webservice/forms.php'); + + +$serviceid = required_param('serviceid', PARAM_INT); +$userid = required_param('userid', PARAM_INT); + +$PAGE->set_url('/admin/webservice/service_user_settings.php', array('id' => $serviceid, 'userid' => $userid)); +$PAGE->navbar->ignore_active(true); +$PAGE->navbar->add(get_string('administrationsite')); +$PAGE->navbar->add(get_string('plugins', 'admin')); +$PAGE->navbar->add(get_string('webservices', 'webservice')); +$PAGE->navbar->add(get_string('externalservices', 'webservice'), + new moodle_url('/admin/settings.php?section=externalservices')); +$PAGE->navbar->add(get_string('serviceusers', 'webservice'), + new moodle_url('/admin/webservice/service_users.php', array('id' => $serviceid))); +$PAGE->navbar->add(get_string('serviceusersettings', 'webservice')); + +//$PAGE->requires->js('/admin/webservice/script.js'); + +admin_externalpage_setup('externalserviceusersettings'); + +$formaction = new moodle_url('', array('id' => $serviceid, 'userid' => $userid)); +$returnurl = new moodle_url('/admin/webservice/service_users.php', array('id' => $serviceid)); + +$webservicemanager = new webservice(); +$serviceuser = $webservicemanager->get_ws_authorised_user($serviceid, $userid); +$usersettingsform = new external_service_authorised_user_settings_form($formaction, $serviceuser); +$settingsformdata = $usersettingsform->get_data(); + +if ($usersettingsform->is_cancelled()) { + redirect($returnurl); + +} else if (!empty($settingsformdata) and confirm_sesskey()) { + /// save user settings (administrator clicked on update button) + $settingsformdata = (object)$settingsformdata; + + $serviceuserinfo = new stdClass(); + $serviceuserinfo->id = $serviceuser->serviceuserid; + $serviceuserinfo->iprestriction = $settingsformdata->iprestriction; + $serviceuserinfo->validuntil = $settingsformdata->validuntil; + + $webservicemanager->update_ws_authorised_user($serviceuserinfo); + + //TODO: assign capability + + //display successful notification + $notification = $OUTPUT->notification(get_string('usersettingssaved', 'webservice'), 'success'); +} + +echo $OUTPUT->header(); +echo $OUTPUT->heading(get_string('serviceusersettings', 'webservice'), 3, 'main'); +if (!empty($notification)) { + echo $notification; +} +$usersettingsform->display(); + +echo $OUTPUT->footer(); diff --git a/admin/webservice/service_users.php b/admin/webservice/service_users.php index ce6cee2f51a..c9974db946c 100644 --- a/admin/webservice/service_users.php +++ b/admin/webservice/service_users.php @@ -16,187 +16,88 @@ // along with Moodle. If not, see . /** - * Web services function UI + * Web services services UI * * @package webservice * @copyright 2009 Moodle Pty Ltd (http://moodle.com) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - require_once('../../config.php'); -require_once($CFG->libdir.'/adminlib.php'); -require_once($CFG->dirroot.'/admin/webservice/lib.php'); +require_once($CFG->libdir . '/adminlib.php'); +require_once($CFG->dirroot . '/admin/webservice/lib.php'); +require_once($CFG->dirroot . '/webservice/lib.php'); $id = required_param('id', PARAM_INT); -$PAGE->set_url('/admin/webservice/service_users.php', array('id'=>$id)); +$PAGE->set_url('/admin/webservice/service_users.php', array('id' => $id)); $PAGE->navbar->ignore_active(true); $PAGE->navbar->add(get_string('administrationsite')); $PAGE->navbar->add(get_string('plugins', 'admin')); $PAGE->navbar->add(get_string('webservices', 'webservice')); -$PAGE->navbar->add(get_string('externalservices', 'webservice'), new moodle_url('/admin/settings.php?section=externalservices')); +$PAGE->navbar->add(get_string('externalservices', 'webservice'), + new moodle_url('/admin/settings.php?section=externalservices')); $PAGE->navbar->add(get_string('serviceusers', 'webservice')); -$PAGE->requires->js('/admin/webservice/script.js'); - admin_externalpage_setup('externalserviceusers'); -echo $OUTPUT->header(); +$webservicemanager = new webservice(); /// Get the user_selector we will need. -$potentialuserselector = new service_user_selector('addselect', array('serviceid' => $id, 'displayallowedusers' => 0)); -$alloweduserselector = new service_user_selector('removeselect', array('serviceid' => $id, 'displayallowedusers' => 1)); +$potentialuserselector = new service_user_selector('addselect', + array('serviceid' => $id, 'displayallowedusers' => 0)); +$alloweduserselector = new service_user_selector('removeselect', + array('serviceid' => $id, 'displayallowedusers' => 1)); /// Process incoming user assignments to the service - if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) { - $userstoassign = $potentialuserselector->get_selected_users(); - if (!empty($userstoassign)) { - - foreach ($userstoassign as $adduser) { - $serviceuser = new object(); - $serviceuser->externalserviceid = $id; - $serviceuser->userid = $adduser->id; - $serviceuser->timecreated = mktime(); - $DB->insert_record('external_services_users', $serviceuser); - add_to_log(1, 'core', 'assign', $CFG->admin.'/webservice/service_users.php?id='.$id, 'add', '', $adduser->id); - } - - $potentialuserselector->invalidate_selected_users(); - $alloweduserselector->invalidate_selected_users(); - } +if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) { + $userstoassign = $potentialuserselector->get_selected_users(); + if (!empty($userstoassign)) { + foreach ($userstoassign as $adduser) { + $serviceuser = new object(); + $serviceuser->externalserviceid = $id; + $serviceuser->userid = $adduser->id; + $webservicemanager->add_ws_authorised_user($serviceuser); + add_to_log(1, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id=' + . $id, 'add', '', $adduser->id); } + $potentialuserselector->invalidate_selected_users(); + $alloweduserselector->invalidate_selected_users(); + } +} /// Process removing user assignments to the service - if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) { - $userstoremove = $alloweduserselector->get_selected_users(); - if (!empty($userstoremove)) { - - foreach ($userstoremove as $removeuser) { - $DB->delete_records('external_services_users', array('externalserviceid' => $id, 'userid' => $removeuser->id)); - add_to_log(1, 'core', 'assign', $CFG->admin.'/webservice/service_users.php?id='.$id, 'remove', '', $removeuser->id); - } - - $potentialuserselector->invalidate_selected_users(); - $alloweduserselector->invalidate_selected_users(); - } +if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) { + $userstoremove = $alloweduserselector->get_selected_users(); + if (!empty($userstoremove)) { + foreach ($userstoremove as $removeuser) { + $webservicemanager->remove_ws_authorised_user($removeuser, $id); + add_to_log(1, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id=' + . $id, 'remove', '', $removeuser->id); } + $potentialuserselector->invalidate_selected_users(); + $alloweduserselector->invalidate_selected_users(); + } +} /// Print the form. /// display the UI -?> -
- +$renderer = $PAGE->get_renderer('core', 'webservice'); - - - - - - -
-

- display() ?> -
-
-
-
+echo $OUTPUT->header(); -
- -
-
-

- display() ?> -
-
- -id = $serviceuserid; - if ($enablevaliduntil) { - $serviceuser->validuntil = $validuntil; - } else { - $serviceuser->validuntil = null; //the valid until field is disabled, we reset the value - } - $serviceuser->iprestriction = $iprestriction; - $DB->update_record('external_services_users', $serviceuser); - - //TODO: assign capability -} +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); //display the list of allowed users with their options (ip/timecreated / validuntil...) //check that the user has the service required capability (if needed) -$sql = " SELECT u.id as id, esu.id as serviceuserid, u.email as email, u.firstname as firstname, u.lastname as lastname, - esu.iprestriction as iprestriction, esu.validuntil as validuntil, - esu.timecreated as timecreated - FROM {user} u, {external_services_users} esu - WHERE username <> 'guest' AND deleted = 0 AND confirmed = 1 - AND esu.userid = u.id - AND esu.externalserviceid = ?"; -$allowedusers = $DB->get_records_sql($sql, array($id)); +$allowedusers = $webservicemanager->get_ws_authorised_users($id); if (!empty($allowedusers)) { - echo $OUTPUT->box_start('generalbox', 'alloweduserlist'); - - echo ""; - echo "

"; //reduce font of the user settings - foreach($allowedusers as $user) { - - echo print_collapsible_region_start('', 'usersettings'.$user->id,$user->firstname." ".$user->lastname.", ".$user->email,false,true,true); - - //user settings form - $contents = "
"; - - //ip restriction textfield - $iprestid = 'iprest'.$user->id; - $contents .= "
"; - $contents .= ''; - $contents .= "
"; - //valid until date selector - $contents .= "
"; - // the following date selector needs to have specific day/month/year field ids because we use javascript (enable/disable). - $contents .= html_writer::select_time('days', 'fromday'.$user->id, $user->validuntil); - $contents .= html_writer::select_time('months', 'frommonth'.$user->id, $user->validuntil); - $contents .= html_writer::select_time('years', 'fromyear'.$user->id, $user->validuntil);; - $contents .= html_writer::checkbox('enablevaliduntil', 1, !empty($user->validuntil), get_string('enabled', 'webservice'), array('id'=>'enablevaliduntil'.$user->id)); - // TODO: init date selector using standard $PAGE->requires->js_init_call(); - - $contents .= "
"; - //TO IMPLEMENT : assign the required capability (if needed) - $contents .= "
"; - $contents .= html_writer::checkbox('addcap', 1, 'TODO:'.get_string('addrequiredcapability', 'webservice')); - $contents .= '
'; - $contents .= ''; - $contents .= ''; - $contents .= ''; - $contents .= ''; - $contents .= ''; - $contents .= "
"; - - echo html_writer::tag('form', array('target'=>'service_users.php', 'method'=>'post', 'id'=>'usersetting'.$user->id), $contents); - - echo print_collapsible_region_end(true); - - - } - echo ""; - echo $OUTPUT->box_end(); + $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(); diff --git a/lang/en/webservice.php b/lang/en/webservice.php index 7aaa4609605..642c0062282 100644 --- a/lang/en/webservice.php +++ b/lang/en/webservice.php @@ -96,6 +96,7 @@ $string['invalidtimedtoken'] = 'Invalid token - token expired'; $string['invalidtoken'] = 'Invalid token - token not found'; $string['invalidtokensession'] = 'Invalid session based token - session not found or expired'; $string['iprestriction'] = 'IP restriction'; +$string['iprestriction_help'] = 'The user will need to call web service from the listed IPs.'; $string['key'] = 'Key'; $string['keyshelp'] = 'The keys are used to access your Moodle account from external applications.'; $string['manageprotocols'] = 'Manage protocols'; @@ -130,6 +131,7 @@ $string['restparam'] = 'REST (POST parameters)'; $string['restrictedusers'] = 'Authorised users only'; $string['securitykey'] = 'Security key (token)'; $string['securitykeys'] = 'Security keys'; +$string['selectauthorisedusers'] = 'Select authorised users'; $string['selectedcapability'] = 'Selected'; $string['selectedcapabilitydoesntexit'] = 'The currently set required capability ({$a}) doesn\'t exist any more. Please change it and save the changes.'; $string['selectservice'] = 'Select a service'; @@ -141,6 +143,7 @@ $string['servicename'] = 'Service name'; $string['servicesbuiltin'] = 'Built-in services'; $string['servicescustom'] = 'Custom services'; $string['serviceusers'] = 'Authorised users'; +$string['serviceusersettings'] = 'User settings'; $string['serviceusersmatching'] = 'Authorised users matching'; $string['serviceuserssettings'] = 'Change settings for the authorised users'; $string['simpleauthlog'] = 'Simple authentication login'; @@ -154,9 +157,12 @@ $string['token'] = 'Token'; $string['tokenauthlog'] = 'Token authentication'; $string['tokencreatedbyadmin'] = 'Can only be reset by administrator (*)'; $string['tokencreator'] = 'Creator'; +$string['updateusersettings'] = 'Update'; $string['userasclients'] = 'Users as clients with token'; $string['userasclientsdescription'] = 'The following steps help you to set up the Moodle web service for users as clients. These steps also help to set up the recommended token (security keys) authentication method. In this use case, the user will generate his token from his Security keys profile page.'; +$string['usersettingssaved'] = 'User settings saved'; $string['validuntil'] = 'Valid until'; +$string['validuntil_help'] = 'If set, the service will be inactivated after this date for this user.'; $string['webservice'] = 'Web service'; $string['webservices'] = 'Web services'; $string['webservicesoverview'] = 'Overview'; diff --git a/webservice/lib.php b/webservice/lib.php index 04ca6135851..a85cb10d70a 100644 --- a/webservice/lib.php +++ b/webservice/lib.php @@ -34,6 +34,84 @@ define('WEBSERVICE_AUTHMETHOD_SESSION_TOKEN', 2); */ class webservice { + /** + * Add a user to the list of authorised user of a given service + * @param object $user + */ + public function add_ws_authorised_user($user) { + global $DB; + $serviceuser->timecreated = mktime(); + $DB->insert_record('external_services_users', $user); + } + + /** + * Remove a user from a list of allowed user of a service + * @param object $user + * @param int $serviceid + */ + public function remove_ws_authorised_user($user, $serviceid) { + global $DB; + $DB->delete_records('external_services_users', + array('externalserviceid' => $serviceid, 'userid' => $user->id)); + } + + /** + * Update service allowed user settings + * @param object $user + */ + public function update_ws_authorised_user($user) { + global $DB; + $DB->update_record('external_services_users', $user); + } + + /** + * Return list of allowed users with their options (ip/timecreated / validuntil...) + * for a given service + * @param int $serviceid + * @return array $users + */ + public function get_ws_authorised_users($serviceid) { + global $DB; + $params = array($serviceid); + $sql = " SELECT u.id as id, esu.id as serviceuserid, u.email as email, u.firstname as firstname, + u.lastname as lastname, + esu.iprestriction as iprestriction, esu.validuntil as validuntil, + esu.timecreated as timecreated + FROM {user} u, {external_services_users} esu + WHERE username <> 'guest' AND deleted = 0 AND confirmed = 1 + AND esu.userid = u.id + AND esu.externalserviceid = ?"; + if (!empty($userid)) { + $sql .= ' AND u.id = ?'; + $params[] = $userid; + } + + $users = $DB->get_records_sql($sql, $params); + return $users; + } + + /** + * Return a authorised user with his options (ip/timecreated / validuntil...) + * @param int $serviceid + * @param int $userid + * @return object + */ + public function get_ws_authorised_user($serviceid, $userid) { + global $DB; + $params = array($serviceid, $userid); + $sql = " SELECT u.id as id, esu.id as serviceuserid, u.email as email, u.firstname as firstname, + u.lastname as lastname, + esu.iprestriction as iprestriction, esu.validuntil as validuntil, + esu.timecreated as timecreated + FROM {user} u, {external_services_users} esu + WHERE username <> 'guest' AND deleted = 0 AND confirmed = 1 + AND esu.userid = u.id + AND esu.externalserviceid = ? + AND u.id = ?"; + $user = $DB->get_record_sql($sql, $params); + return $user; + } + /** * Generate all ws token needed by a user * @param int $userid diff --git a/webservice/renderer.php b/webservice/renderer.php index e25637cb254..bdf80afc6dc 100644 --- a/webservice/renderer.php +++ b/webservice/renderer.php @@ -1,4 +1,5 @@ 'sesskey', 'value' => sesskey(), 'type' => 'hidden')); + + $table = new html_table(); + $table->size = array('45%', '10%', '45%'); + $table->attributes['class'] = 'roleassigntable generaltable generalbox boxaligncenter'; + $table->summary = ''; + $table->cellspacing = 0; + $table->cellpadding = 0; + + //create the add and remove button + $addinput = html_writer::empty_tag('input', + array('name' => 'add', 'id' => 'add', 'type' => 'submit', + 'value' => '◀' . ' ' . get_string('add'), + 'title' => get_string('add'))); + $addbutton = html_writer::tag('div', $addinput, array('id' => 'addcontrols')); + $removeinput = html_writer::empty_tag('input', + array('name' => 'remove', 'id' => 'remove', 'type' => 'submit', + 'value' => '▶' . ' ' . get_string('remove'), + 'title' => get_string('remove'))); + $removebutton = html_writer::tag('div', $removeinput, array('id' => 'removecontrols')); + + + //create the three cells + $label = html_writer::tag('label', get_string('serviceusers', 'webservice'), + array('for' => 'removeselect')); + $label = html_writer::tag('p', $label); + $authoriseduserscell = new html_table_cell($label . $options->alloweduserselector->display(true)); + $authoriseduserscell->id = 'existingcell'; + $buttonscell = new html_table_cell($addbutton . html_writer::empty_tag('br') . $removebutton); + $buttonscell->id = 'buttonscell'; + $label = html_writer::tag('label', get_string('potusers', 'webservice'), + array('for' => 'addselect')); + $label = html_writer::tag('p', $label); + $otheruserscell = new html_table_cell($label . $options->potentialuserselector->display(true)); + $otheruserscell->id = 'potentialcell'; + + $cells = array($authoriseduserscell, $buttonscell, $otheruserscell); + $row = new html_table_row($cells); + $table->data[] = $row; + $formcontent .= html_writer::table($table); + + $formcontent = html_writer::tag('div', $formcontent); + + $actionurl = new moodle_url('/admin/webservice/service_users.php', + array('id' => $options->serviceid)); + $html = html_writer::tag('form', $formcontent, + array('id' => 'assignform', 'action' => $actionurl, 'method' => 'post')); + return $html; + } + + /** + * Display list of authorised user + * @param array $users + * @return string $html + */ + public function admin_authorised_user_list($users, $serviceid) { + global $OUTPUT; + $html = $OUTPUT->box_start('generalbox', 'alloweduserlist'); + foreach ($users as $user) { + $modifiedauthoriseduserurl = new moodle_url('/admin/webservice/service_user_settings.php', + array('userid' => $user->id, 'serviceid' => $serviceid)); + $html .= html_writer::tag('a', $user->firstname . " " . $user->lastname . ", " . $user->email, + array('href' => $modifiedauthoriseduserurl)); + $html .= html_writer::empty_tag('br'); + } + $html .= $OUTPUT->box_end(); + return $html; + } + /** * Display Reset token confirmation box * @param object $token to reset @@ -36,19 +114,17 @@ class core_webservice_renderer extends plugin_renderer_base { */ public function user_reset_token_confirmation($token) { global $OUTPUT, $CFG; - $managetokenurl = $CFG->wwwroot."/user/managetoken.php?sesskey=" . sesskey(); - $optionsyes = array('tokenid'=>$token->id, 'action'=>'resetwstoken', 'confirm'=>1, 'sesskey'=>sesskey()); - $optionsno = array('section'=>'webservicetokens', 'sesskey'=>sesskey()); + $managetokenurl = $CFG->wwwroot . "/user/managetoken.php?sesskey=" . sesskey(); + $optionsyes = array('tokenid' => $token->id, 'action' => 'resetwstoken', 'confirm' => 1, 'sesskey' => sesskey()); + $optionsno = array('section' => 'webservicetokens', 'sesskey' => sesskey()); $formcontinue = new single_button(new moodle_url($managetokenurl, $optionsyes), get_string('reset')); $formcancel = new single_button(new moodle_url($managetokenurl, $optionsno), get_string('cancel'), 'get'); - $html = $OUTPUT->confirm(get_string('resettokenconfirm', 'webservice', - (object)array('user'=>$token->firstname." ".$token->lastname, 'service'=>$token->name)), - $formcontinue, $formcancel); + $html = $OUTPUT->confirm(get_string('resettokenconfirm', 'webservice', + (object) array('user' => $token->firstname . " " . $token->lastname, 'service' => $token->name)), + $formcontinue, $formcancel); return $html; } - - /** * Display user tokens with buttons to reset them * @param object $tokens @@ -72,30 +148,30 @@ class core_webservice_renderer extends plugin_renderer_base { $return .= get_string('keyshelp', 'webservice'); $table = new html_table(); - $table->head = array($strtoken, $strservice, $strvaliduntil, $strcreator, $stroperation); + $table->head = array($strtoken, $strservice, $strvaliduntil, $strcreator, $stroperation); $table->align = array('left', 'left', 'left', 'center', 'left', 'center'); $table->width = '100%'; - $table->data = array(); + $table->data = array(); if (!empty($tokens)) { foreach ($tokens as $token) { //TODO: retrieve context if ($token->creatorid == $userid) { - $reset = "wwwroot."/user/managetoken.php?sesskey=".sesskey(). - "&action=resetwstoken&tokenid=".$token->id."\">"; - $reset .= get_string('reset').""; - $creator = $token->firstname." ".$token->lastname; + $reset = "wwwroot . "/user/managetoken.php?sesskey=" . sesskey() . + "&action=resetwstoken&tokenid=" . $token->id . "\">"; + $reset .= get_string('reset') . ""; + $creator = $token->firstname . " " . $token->lastname; } else { //retrive administrator name - require_once($CFG->dirroot.'/user/lib.php'); + require_once($CFG->dirroot . '/user/lib.php'); $creators = user_get_users_by_id(array($token->creatorid)); $admincreator = $creators[$token->creatorid]; - $creator = $admincreator->firstname." ".$admincreator->lastname; + $creator = $admincreator->firstname . " " . $admincreator->lastname; $reset = ''; } - $userprofilurl = new moodle_url('/user/view.php?id='.$token->creatorid); + $userprofilurl = new moodle_url('/user/view.php?id=' . $token->creatorid); $creatoratag = html_writer::start_tag('a', array('href' => $userprofilurl)); $creatoratag .= $creator; $creatoratag .= html_writer::end_tag('a'); @@ -108,7 +184,6 @@ class core_webservice_renderer extends plugin_renderer_base { $table->data[] = array($token->token, $token->name, $validuntil, $creatoratag, $reset); } $return .= html_writer::table($table); - } else { $return .= get_string('notoken', 'webservice'); } @@ -117,25 +192,23 @@ class core_webservice_renderer extends plugin_renderer_base { return $return; } - - - /** + /** * Return documentation for a ws description object * ws description object can be 'external_multiple_structure', 'external_single_structure' or 'external_value' * Example of documentation for moodle_group_create_groups function: - list of ( - object { - courseid int //id of course - name string //multilang compatible name, course unique - description string //group description text - enrolmentkey string //group enrol secret phrase - } - ) + list of ( + object { + courseid int //id of course + name string //multilang compatible name, course unique + description string //group description text + enrolmentkey string //group enrol secret phrase + } + ) * @param object $params a part of parameter/return description * @return string the html to display */ public function detailed_description_html($params) { - /// retrieve the description of the description object + /// retrieve the description of the description object $paramdesc = ""; if (!empty($params->desc)) { $paramdesc .= html_writer::start_tag('span', array('style' => "color:#2A33A6")); @@ -146,41 +219,41 @@ class core_webservice_renderer extends plugin_renderer_base { if ($params->default === null) { $params->default = "null"; } - $required = html_writer::start_tag('b', array()).get_string('default', 'webservice', $params->default).html_writer::end_tag('b'); + $required = html_writer::start_tag('b', array()) . get_string('default', 'webservice', $params->default) . html_writer::end_tag('b'); } if ($params->required == VALUE_OPTIONAL) { - $required = html_writer::start_tag('b', array()).get_string('optional', 'webservice').html_writer::end_tag('b'); + $required = html_writer::start_tag('b', array()) . get_string('optional', 'webservice') . html_writer::end_tag('b'); } - $paramdesc .= " ".$required." "; + $paramdesc .= " " . $required . " "; $paramdesc .= html_writer::start_tag('i', array()); $paramdesc .= "//"; $paramdesc .= $params->desc; $paramdesc .= html_writer::end_tag('i'); - + $paramdesc .= html_writer::end_tag('span'); $paramdesc .= html_writer::empty_tag('br', array()); } - /// description object is a list + /// description object is a list if ($params instanceof external_multiple_structure) { return $paramdesc . "list of ( " . html_writer::empty_tag('br', array()) . $this->detailed_description_html($params->content) . ")"; } else if ($params instanceof external_single_structure) { - /// description object is an object - $singlestructuredesc = $paramdesc."object {". html_writer::empty_tag('br', array()); + /// description object is an object + $singlestructuredesc = $paramdesc . "object {" . html_writer::empty_tag('br', array()); foreach ($params->keys as $attributname => $attribut) { $singlestructuredesc .= html_writer::start_tag('b', array()); $singlestructuredesc .= $attributname; $singlestructuredesc .= html_writer::end_tag('b'); - $singlestructuredesc .= " ".$this->detailed_description_html($params->keys[$attributname]); + $singlestructuredesc .= " " . $this->detailed_description_html($params->keys[$attributname]); } $singlestructuredesc .= "} "; $singlestructuredesc .= html_writer::empty_tag('br', array()); return $singlestructuredesc; } else { - /// description object is a primary type (string, integer) - switch($params->type) { + /// description object is a primary type (string, integer) + switch ($params->type) { case PARAM_BOOL: // 0 or 1 only for now case PARAM_INT: $type = 'int'; @@ -191,7 +264,7 @@ class core_webservice_renderer extends plugin_renderer_base { default: $type = 'string'; } - return $type." ".$paramdesc; + return $type . " " . $paramdesc; } } @@ -208,26 +281,26 @@ class core_webservice_renderer extends plugin_renderer_base { EOF; - /// description object is a list + /// description object is a list if ($returndescription instanceof external_multiple_structure) { - $return = $indentation."".$brakeline; + $return = $indentation . "" . $brakeline; $return .= $this->description_in_indented_xml_format($returndescription->content, $indentation); - $return .= $indentation."".$brakeline; + $return .= $indentation . "" . $brakeline; return $return; } else if ($returndescription instanceof external_single_structure) { - /// description object is an object - $singlestructuredesc = $indentation."".$brakeline; - $keyindentation = $indentation." "; + /// description object is an object + $singlestructuredesc = $indentation . "" . $brakeline; + $keyindentation = $indentation . " "; foreach ($returndescription->keys as $attributname => $attribut) { - $singlestructuredesc .= $keyindentation."".$brakeline. - $this->description_in_indented_xml_format($returndescription->keys[$attributname], $keyindentation). - $keyindentation."".$brakeline; + $singlestructuredesc .= $keyindentation . "" . $brakeline . + $this->description_in_indented_xml_format($returndescription->keys[$attributname], $keyindentation) . + $keyindentation . "" . $brakeline; } - $singlestructuredesc .= $indentation."".$brakeline; + $singlestructuredesc .= $indentation . "" . $brakeline; return $singlestructuredesc; } else { - /// description object is a primary type (string, integer) - switch($returndescription->type) { + /// description object is a primary type (string, integer) + switch ($returndescription->type) { case PARAM_BOOL: // 0 or 1 only for now case PARAM_INT: $type = 'int'; @@ -238,11 +311,11 @@ EOF; default: $type = 'string'; } - return $indentation."".$type."".$brakeline; + return $indentation . "" . $type . "" . $brakeline; } } - /** + /** * Create indented XML-RPC param description * @param object $paramdescription * @param string $indentation composed by space only @@ -254,30 +327,30 @@ EOF; EOF; - /// description object is a list + /// description object is a list if ($paramdescription instanceof external_multiple_structure) { - $return = $brakeline.$indentation."Array "; + $return = $brakeline . $indentation . "Array "; $indentation = $indentation . " "; - $return .= $brakeline.$indentation."("; - $return .= $brakeline.$indentation."[0] =>"; + $return .= $brakeline . $indentation . "("; + $return .= $brakeline . $indentation . "[0] =>"; $return .= $this->xmlrpc_param_description_html($paramdescription->content, $indentation); - $return .= $brakeline.$indentation.")"; + $return .= $brakeline . $indentation . ")"; return $return; } else if ($paramdescription instanceof external_single_structure) { - /// description object is an object - $singlestructuredesc = $brakeline.$indentation."Array "; - $keyindentation = $indentation." "; - $singlestructuredesc .= $brakeline.$keyindentation."("; + /// description object is an object + $singlestructuredesc = $brakeline . $indentation . "Array "; + $keyindentation = $indentation . " "; + $singlestructuredesc .= $brakeline . $keyindentation . "("; foreach ($paramdescription->keys as $attributname => $attribut) { - $singlestructuredesc .= $brakeline.$keyindentation."[".$attributname."] =>". - $this->xmlrpc_param_description_html($paramdescription->keys[$attributname], $keyindentation). + $singlestructuredesc .= $brakeline . $keyindentation . "[" . $attributname . "] =>" . + $this->xmlrpc_param_description_html($paramdescription->keys[$attributname], $keyindentation) . $keyindentation; } - $singlestructuredesc .= $brakeline.$keyindentation.")"; + $singlestructuredesc .= $brakeline . $keyindentation . ")"; return $singlestructuredesc; } else { - /// description object is a primary type (string, integer) - switch($paramdescription->type) { + /// description object is a primary type (string, integer) + switch ($paramdescription->type) { case PARAM_BOOL: // 0 or 1 only for now case PARAM_INT: $type = 'int'; @@ -288,7 +361,7 @@ EOF; default: $type = 'string'; } - return " ".$type; + return " " . $type; } } @@ -301,21 +374,20 @@ EOF; */ public function colored_box_with_pre_tag($title, $content, $rgb = 'FEEBE5') { $coloredbox = html_writer::start_tag('ins', array()); //TODO: this tag removes xhtml strict error but cause warning - $coloredbox .= html_writer::start_tag('div', array('style' => "border:solid 1px #DEDEDE;background:#".$rgb.";color:#222222;padding:4px;")); + $coloredbox .= html_writer::start_tag('div', array('style' => "border:solid 1px #DEDEDE;background:#" . $rgb . ";color:#222222;padding:4px;")); $coloredbox .= html_writer::start_tag('pre', array()); $coloredbox .= html_writer::start_tag('b', array()); $coloredbox .= $title; $coloredbox .= html_writer::end_tag('b', array()); $coloredbox .= html_writer::empty_tag('br', array()); - $coloredbox .= "\n".$content."\n"; + $coloredbox .= "\n" . $content . "\n"; $coloredbox .= html_writer::end_tag('pre', array()); $coloredbox .= html_writer::end_tag('div', array()); $coloredbox .= html_writer::end_tag('ins', array()); return $coloredbox; } - - /** + /** * Return indented REST param description * @param object $paramdescription * @param string $indentation composed by space only @@ -326,24 +398,24 @@ EOF; EOF; - /// description object is a list + /// description object is a list if ($paramdescription instanceof external_multiple_structure) { - $paramstring = $paramstring.'[0]'; + $paramstring = $paramstring . '[0]'; $return = $this->rest_param_description_html($paramdescription->content, $paramstring); return $return; } else if ($paramdescription instanceof external_single_structure) { - /// description object is an object + /// description object is an object $singlestructuredesc = ""; $initialparamstring = $paramstring; foreach ($paramdescription->keys as $attributname => $attribut) { - $paramstring = $initialparamstring.'['.$attributname.']'; + $paramstring = $initialparamstring . '[' . $attributname . ']'; $singlestructuredesc .= $this->rest_param_description_html($paramdescription->keys[$attributname], $paramstring); } return $singlestructuredesc; } else { - /// description object is a primary type (string, integer) - $paramstring = $paramstring.'='; - switch($paramdescription->type) { + /// description object is a primary type (string, integer) + $paramstring = $paramstring . '='; + switch ($paramdescription->type) { case PARAM_BOOL: // 0 or 1 only for now case PARAM_INT: $type = 'int'; @@ -354,11 +426,10 @@ EOF; default: $type = 'string'; } - return $paramstring." ".$type.$brakeline; + return $paramstring . " " . $type . $brakeline; } } - /** * This display all the documentation * @param array $functions contains all decription objects @@ -374,55 +445,55 @@ EOF; EOF; - /// Some general information + /// Some general information $documentationhtml = html_writer::start_tag('table', array('style' => "margin-left:auto; margin-right:auto;")); $documentationhtml .= html_writer::start_tag('tr', array()); $documentationhtml .= html_writer::start_tag('td', array()); $documentationhtml .= get_string('wsdocumentationintro', 'webservice', $authparams['wsusername']); - $documentationhtml .= $br.$br; - + $documentationhtml .= $br . $br; - /// Print button + + /// Print button $authparams['print'] = true; //$parameters = array ('token' => $token, 'wsusername' => $username, 'wspassword' => $password, 'print' => true); $url = new moodle_url('/webservice/wsdoc.php', $authparams); // Required - $documentationhtml .= $OUTPUT->single_button($url, get_string('print','webservice')); + $documentationhtml .= $OUTPUT->single_button($url, get_string('print', 'webservice')); $documentationhtml .= $br; - - - /// each functions will be displayed into a collapsible region (opened if printableformat = true) + + + /// each functions will be displayed into a collapsible region (opened if printableformat = true) foreach ($functions as $functionname => $description) { if (empty($printableformat)) { $documentationhtml .= print_collapsible_region_start('', - 'aera_'.$functionname, - html_writer::start_tag('strong', array()).$functionname.html_writer::end_tag('strong'), - false, - !$printableformat, - true); + 'aera_' . $functionname, + html_writer::start_tag('strong', array()) . $functionname . html_writer::end_tag('strong'), + false, + !$printableformat, + true); } else { $documentationhtml .= html_writer::tag('strong', $functionname); $documentationhtml .= $br; } - /// function global description + /// function global description $documentationhtml .= $br; $documentationhtml .= html_writer::start_tag('div', array('style' => 'border:solid 1px #DEDEDE;background:#E2E0E0;color:#222222;padding:4px;')); $documentationhtml .= $description->description; $documentationhtml .= html_writer::end_tag('div'); - $documentationhtml .= $br.$br; + $documentationhtml .= $br . $br; - /// function arguments documentation + /// function arguments documentation $documentationhtml .= html_writer::start_tag('span', array('style' => 'color:#EA33A6')); $documentationhtml .= get_string('arguments', 'webservice'); $documentationhtml .= html_writer::end_tag('span'); $documentationhtml .= $br; foreach ($description->parameters_desc->keys as $paramname => $paramdesc) { - /// a argument documentation + /// a argument documentation $documentationhtml .= html_writer::start_tag('span', array('style' => 'font-size:80%')); - + if ($paramdesc->required == VALUE_REQUIRED) { - $required = get_string('required', 'webservice'); + $required = get_string('required', 'webservice'); } if ($paramdesc->required == VALUE_DEFAULT) { if ($paramdesc->default === null) { @@ -433,38 +504,38 @@ EOF; $required = get_string('default', 'webservice', $default); } if ($paramdesc->required == VALUE_OPTIONAL) { - $required = get_string('optional', 'webservice'); + $required = get_string('optional', 'webservice'); } - + $documentationhtml .= html_writer::start_tag('b', array()); $documentationhtml .= $paramname; $documentationhtml .= html_writer::end_tag('b'); - $documentationhtml .= " (" .$required. ")"; // argument is required or optional ? + $documentationhtml .= " (" . $required . ")"; // argument is required or optional ? $documentationhtml .= $br; - $documentationhtml .= "        ".$paramdesc->desc; // argument description - $documentationhtml .= $br.$br; + $documentationhtml .= "        " . $paramdesc->desc; // argument description + $documentationhtml .= $br . $br; ///general structure of the argument - $documentationhtml .= $this->colored_box_with_pre_tag(get_string('generalstructure', 'webservice'), - $this->detailed_description_html($paramdesc), - 'FFF1BC'); + $documentationhtml .= $this->colored_box_with_pre_tag(get_string('generalstructure', 'webservice'), + $this->detailed_description_html($paramdesc), + 'FFF1BC'); ///xml-rpc structure of the argument in PHP format if (!empty($activatedprotocol['xmlrpc'])) { - $documentationhtml .= $this->colored_box_with_pre_tag(get_string('phpparam', 'webservice'), - htmlentities('['.$paramname.'] =>'.$this->xmlrpc_param_description_html($paramdesc)), - 'DFEEE7'); + $documentationhtml .= $this->colored_box_with_pre_tag(get_string('phpparam', 'webservice'), + htmlentities('[' . $paramname . '] =>' . $this->xmlrpc_param_description_html($paramdesc)), + 'DFEEE7'); } ///POST format for the REST protocol for the argument if (!empty($activatedprotocol['rest'])) { - $documentationhtml .= $this->colored_box_with_pre_tag(get_string('restparam', 'webservice'), - htmlentities($this->rest_param_description_html($paramdesc,$paramname)), - 'FEEBE5'); + $documentationhtml .= $this->colored_box_with_pre_tag(get_string('restparam', 'webservice'), + htmlentities($this->rest_param_description_html($paramdesc, $paramname)), + 'FEEBE5'); } $documentationhtml .= html_writer::end_tag('span'); } - $documentationhtml .= $br.$br; + $documentationhtml .= $br . $br; - /// function response documentation + /// function response documentation $documentationhtml .= html_writer::start_tag('span', array('style' => 'color:#EA33A6')); $documentationhtml .= get_string('response', 'webservice'); $documentationhtml .= html_writer::end_tag('span'); @@ -473,66 +544,65 @@ EOF; $documentationhtml .= html_writer::start_tag('span', array('style' => 'font-size:80%')); if (!empty($description->returns_desc->desc)) { $documentationhtml .= $description->returns_desc->desc; - $documentationhtml .= $br.$br; + $documentationhtml .= $br . $br; } if (!empty($description->returns_desc)) { ///general structure of the response - $documentationhtml .= $this->colored_box_with_pre_tag(get_string('generalstructure', 'webservice'), - $this->detailed_description_html($description->returns_desc), - 'FFF1BC'); + $documentationhtml .= $this->colored_box_with_pre_tag(get_string('generalstructure', 'webservice'), + $this->detailed_description_html($description->returns_desc), + 'FFF1BC'); ///xml-rpc structure of the response in PHP format if (!empty($activatedprotocol['xmlrpc'])) { - $documentationhtml .= $this->colored_box_with_pre_tag(get_string('phpresponse', 'webservice'), - htmlentities($this->xmlrpc_param_description_html($description->returns_desc)), - 'DFEEE7'); + $documentationhtml .= $this->colored_box_with_pre_tag(get_string('phpresponse', 'webservice'), + htmlentities($this->xmlrpc_param_description_html($description->returns_desc)), + 'DFEEE7'); } ///XML response for the REST protocol if (!empty($activatedprotocol['rest'])) { - $restresponse = "".$brakeline."".$brakeline; + $restresponse = "" . $brakeline . "" . $brakeline; $restresponse .= $this->description_in_indented_xml_format($description->returns_desc); - $restresponse .="".$brakeline; - $documentationhtml .= $this->colored_box_with_pre_tag(get_string('restcode', 'webservice'), - htmlentities($restresponse), - 'FEEBE5'); + $restresponse .="" . $brakeline; + $documentationhtml .= $this->colored_box_with_pre_tag(get_string('restcode', 'webservice'), + htmlentities($restresponse), + 'FEEBE5'); } } $documentationhtml .= html_writer::end_tag('span'); - $documentationhtml .= $br.$br; + $documentationhtml .= $br . $br; - /// function errors documentation for REST protocol + /// function errors documentation for REST protocol if (!empty($activatedprotocol['rest'])) { $documentationhtml .= html_writer::start_tag('span', array('style' => 'color:#EA33A6')); $documentationhtml .= get_string('errorcodes', 'webservice'); $documentationhtml .= html_writer::end_tag('span'); - $documentationhtml .= $br.$br; + $documentationhtml .= $br . $br; $documentationhtml .= html_writer::start_tag('span', array('style' => 'font-size:80%')); $errormessage = get_string('invalidparameter', 'debug'); - $restexceptiontext =<< {$errormessage} EOF; - $documentationhtml .= $this->colored_box_with_pre_tag(get_string('restexception', 'webservice'), - htmlentities($restexceptiontext), - 'FEEBE5'); + $documentationhtml .= $this->colored_box_with_pre_tag(get_string('restexception', 'webservice'), + htmlentities($restexceptiontext), + 'FEEBE5'); - $documentationhtml .= html_writer::end_tag('span'); + $documentationhtml .= html_writer::end_tag('span'); } - $documentationhtml .= $br.$br; + $documentationhtml .= $br . $br; if (empty($printableformat)) { $documentationhtml .= print_collapsible_region_end(true); } } - /// close the table and return the documentation + /// close the table and return the documentation $documentationhtml .= html_writer::end_tag('td'); $documentationhtml .= html_writer::end_tag('tr'); $documentationhtml .= html_writer::end_tag('table'); return $documentationhtml; - } /** @@ -553,30 +623,29 @@ EOF; // $htmlloginpage .= get_string('error','webservice',$errormessage); // $htmlloginpage .= html_writer::empty_tag('br', array()); // $htmlloginpage .= html_writer::empty_tag('br', array()); - //login form - we cannot use moodle form as we don't have sessionkey $target = new moodle_url('/webservice/wsdoc.php', array()); // Required $contents = get_string('entertoken', 'webservice'); - $contents .= $br.$br; - $contents .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>'token', 'style'=>'width: 30em;')); - - $contents .= $br.$br; + $contents .= $br . $br; + $contents .= html_writer::empty_tag('input', array('type' => 'text', 'name' => 'token', 'style' => 'width: 30em;')); + + $contents .= $br . $br; $contents .= get_string('wsdocumentationlogin', 'webservice'); - $contents .= $br.$br; - $contents .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>'wsusername', 'style'=>'width: 30em;', 'value'=>get_string('wsusername', 'webservice'))); - $contents .= $br.$br; - $contents .= html_writer::empty_tag('input', array('type'=>'text', 'name'=>'wspassword', 'style'=>'width: 30em;', 'value'=>get_string('wspassword', 'webservice'))); - $contents .= $br.$br; - $contents .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'submit', 'value'=>get_string('wsdocumentation', 'webservice'))); - - $htmlloginpage .= html_writer::tag('form', "
$contents
", array('method'=>'post', 'target'=>$target)); + $contents .= $br . $br; + $contents .= html_writer::empty_tag('input', array('type' => 'text', 'name' => 'wsusername', 'style' => 'width: 30em;', 'value' => get_string('wsusername', 'webservice'))); + $contents .= $br . $br; + $contents .= html_writer::empty_tag('input', array('type' => 'text', 'name' => 'wspassword', 'style' => 'width: 30em;', 'value' => get_string('wspassword', 'webservice'))); + $contents .= $br . $br; + $contents .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'submit', 'value' => get_string('wsdocumentation', 'webservice'))); + + $htmlloginpage .= html_writer::tag('form', "
$contents
", array('method' => 'post', 'target' => $target)); $htmlloginpage .= html_writer::end_tag('td'); $htmlloginpage .= html_writer::end_tag('tr'); $htmlloginpage .= html_writer::end_tag('table'); return $htmlloginpage; - } + }