webservice MDL-21792 refactor webservice authorised users pages
authorjerome mouneyrac <jerome@moodle.com>
Fri, 2 Jul 2010 06:40:36 +0000 (06:40 +0000)
committerjerome mouneyrac <jerome@moodle.com>
Fri, 2 Jul 2010 06:40:36 +0000 (06:40 +0000)
admin/settings/plugins.php
admin/webservice/forms.php
admin/webservice/script.js [deleted file]
admin/webservice/service_user_settings.php [new file with mode: 0644]
admin/webservice/service_users.php
lang/en/webservice.php
webservice/lib.php
webservice/renderer.php

index 1405072..7ff697d 100644 (file)
@@ -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());
index 2c77dd6..9f26ce9 100644 (file)
 
 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 (file)
index a7d9f7a..0000000
+++ /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 (file)
index 0000000..f6aacd9
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * 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();
index ce6cee2..c9974db 100644 (file)
 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
 /**
- * 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
-?>
-<form id="assignform" method="post" action="service_users.php?id=<?php echo $id ?>"><div>
-  <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
-
-  <table summary="" class="roleassigntable generaltable generalbox boxaligncenter" cellspacing="0">
-    <tr>
-      <td id="existingcell">
-          <p><label for="removeselect"><?php print_string('serviceusers', 'webservice'); ?></label></p>
-          <?php $alloweduserselector->display() ?>
-      </td>
-      <td id="buttonscell">
-          <div id="addcontrols">
-              <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
-          </div>
+$renderer = $PAGE->get_renderer('core', 'webservice');
 
-          <div id="removecontrols">
-              <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
-          </div>
-      </td>
-      <td id="potentialcell">
-          <p><label for="addselect"><?php print_string('potusers', 'webservice'); ?></label></p>
-          <?php $potentialuserselector->display() ?>
-      </td>
-    </tr>
-  </table>
-</div></form>
-
-<?php
-
-/// save user settings (administrator clicked on update button)
-if (optional_param('updateuser', false, PARAM_BOOL) && confirm_sesskey()) {
-    $useridtoupdate = optional_param('userid', false, PARAM_INT);
-    $iprestriction = optional_param('iprestriction', '', PARAM_TEXT);
-    $serviceuserid = optional_param('serviceuserid', '', PARAM_INT);
-    $fromday = optional_param('fromday'.$useridtoupdate, '', PARAM_INT);
-    $frommonth = optional_param('frommonth'.$useridtoupdate, '', PARAM_INT);
-    $fromyear = optional_param('fromyear'.$useridtoupdate, '', PARAM_INT);
-    $addcap = optional_param('addcap', false, PARAM_INT);
-    $enablevaliduntil = optional_param('enablevaliduntil', false, PARAM_INT);
-    if (!empty($fromday) && !empty($frommonth) && !empty($fromyear)) {
-        $validuntil = mktime(23, 59, 59, $frommonth, $fromday, $fromyear);
-    } else {
-        $validuntil = "";
-    }
-
-    $serviceuser = new object();
-    $serviceuser->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);
+echo $OUTPUT->header();
 
-    //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 "<label><strong>".get_string('serviceuserssettings', 'webservice').":</strong></label>";
-    echo "<br/><br/><span style=\"font-size:85%\">"; //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 = "<div class=\"fcontainer clearfix\">";
-
-        //ip restriction textfield
-        $iprestid = 'iprest'.$user->id;
-        $contents .= "<div class=\"fitem\"><div class=\"fitemtitle\"><label for=\"$iprestid\">".get_string('iprestriction','webservice')." </label></div><div class=\"felement\">";
-        $contents .= '<input type="text" id="'.$iprestid.'" name="iprestriction" style="width: 30em;" value="'.s($user->iprestriction).'" />';
-        $contents .= "</div></div>";
-        //valid until date selector
-        $contents .= "<div class=\"fitem\"><div class=\"fitemtitle\"><label>".get_string('validuntil','webservice')." </label></div><div class=\"felement\">";
-        // 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 .= "</div></div>";
-        //TO IMPLEMENT : assign the required capability (if needed)
-        $contents .=  "<div class=\"fitem\"><div class=\"fitemtitle\"><label>".get_string('addrequiredcapability','webservice')." </label></div><div class=\"felement fcheckbox\">";
-        $contents .= html_writer::checkbox('addcap', 1, 'TODO:'.get_string('addrequiredcapability', 'webservice'));
-        $contents .= '<div><input type="submit" name="submit" value="'.s(get_string('update')).'" /></div>';
-        $contents .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
-        $contents .= '<input type="hidden" name="id" value="'.$id.'" />';
-        $contents .= '<input type="hidden" name="userid" value="'.$user->id.'" />';
-        $contents .= '<input type="hidden" name="serviceuserid" value="'.$user->serviceuserid.'" />';
-        $contents .= '<input type="hidden" name="updateuser" value="1" />';
-        $contents .= "</div>";
-
-        echo html_writer::tag('form', array('target'=>'service_users.php', 'method'=>'post', 'id'=>'usersetting'.$user->id), $contents);
-
-        echo print_collapsible_region_end(true);
-
-
-    }
-    echo "</span>";
-    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();
index 7aaa460..642c006 100644 (file)
@@ -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 <strong>Security keys</strong> 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';
index 04ca613..a85cb10 100644 (file)
@@ -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
index e25637c..bdf80af 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+
 ///////////////////////////////////////////////////////////////////////////
 //                                                                       //
 // This file is part of Moodle - http://moodle.org/                      //
  * @author    Jerome Mouneyrac
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-
 class core_webservice_renderer extends plugin_renderer_base {
 
+    /**
+     * Display the authorised user selector
+     * @param object $options
+     * @return string html
+     */
+    public function admin_authorised_user_selector(&$options) {
+        global $OUTPUT;
+
+        $formcontent = html_writer::empty_tag('input',
+                array('name' => '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 = "<a href=\"".$CFG->wwwroot."/user/managetoken.php?sesskey=".sesskey().
-                            "&amp;action=resetwstoken&amp;tokenid=".$token->id."\">";
-                    $reset .= get_string('reset')."</a>";
-                    $creator = $token->firstname." ".$token->lastname;
+                    $reset = "<a href=\"" . $CFG->wwwroot . "/user/managetoken.php?sesskey=" . sesskey() .
+                            "&amp;action=resetwstoken&amp;tokenid=" . $token->id . "\">";
+                    $reset .= get_string('reset') . "</a>";
+                    $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."<MULTIPLE>".$brakeline;
+            $return = $indentation . "<MULTIPLE>" . $brakeline;
             $return .= $this->description_in_indented_xml_format($returndescription->content, $indentation);
-            $return .= $indentation."</MULTIPLE>".$brakeline;
+            $return .= $indentation . "</MULTIPLE>" . $brakeline;
             return $return;
         } else if ($returndescription instanceof external_single_structure) {
-    /// description object is an object
-            $singlestructuredesc = $indentation."<SINGLE>".$brakeline;
-            $keyindentation = $indentation."    ";
+            /// description object is an object
+            $singlestructuredesc = $indentation . "<SINGLE>" . $brakeline;
+            $keyindentation = $indentation . "    ";
             foreach ($returndescription->keys as $attributname => $attribut) {
-                $singlestructuredesc .= $keyindentation."<KEY name=\"".$attributname."\">".$brakeline.
-                        $this->description_in_indented_xml_format($returndescription->keys[$attributname], $keyindentation).
-                        $keyindentation."</KEY>".$brakeline;
+                $singlestructuredesc .= $keyindentation . "<KEY name=\"" . $attributname . "\">" . $brakeline .
+                        $this->description_in_indented_xml_format($returndescription->keys[$attributname], $keyindentation) .
+                        $keyindentation . "</KEY>" . $brakeline;
             }
-            $singlestructuredesc .= $indentation."</SINGLE>".$brakeline;
+            $singlestructuredesc .= $indentation . "</SINGLE>" . $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."<VALUE>".$type."</VALUE>".$brakeline;
+            return $indentation . "<VALUE>" . $type . "</VALUE>" . $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 .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$paramdesc->desc; // argument description
-                $documentationhtml .= $br.$br;
+                $documentationhtml .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $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  = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>".$brakeline."<RESPONSE>".$brakeline;
+                    $restresponse = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" . $brakeline . "<RESPONSE>" . $brakeline;
                     $restresponse .= $this->description_in_indented_xml_format($description->returns_desc);
-                    $restresponse .="</RESPONSE>".$brakeline;
-                    $documentationhtml .= $this->colored_box_with_pre_tag(get_string('restcode', 'webservice'), 
-                                                                          htmlentities($restresponse),
-                                                                          'FEEBE5');
+                    $restresponse .="</RESPONSE>" . $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 =<<<EOF
+                $restexceptiontext = <<<EOF
 <?xml version="1.0" encoding="UTF-8"?>
 <EXCEPTION class="invalid_parameter_exception">
     <MESSAGE>{$errormessage}</MESSAGE>
     <DEBUGINFO></DEBUGINFO>
 </EXCEPTION>
 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', "<div>$contents</div>", 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', "<div>$contents</div>", 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;
-
     }
+
 }