2009-10-12 21:46:16 +00:00
< ? 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 function UI
*
* @ package webservice
* @ copyright 2009 Moodle Pty Ltd ( http :// moodle . com )
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL v3 or later
*/
2009-10-21 19:58:50 +00:00
require_once ( '../../config.php' );
2009-10-12 21:46:16 +00:00
require_once ( $CFG -> libdir . '/adminlib.php' );
2009-10-21 21:24:05 +00:00
require_once ( $CFG -> libdir . '/externallib.php' );
2009-10-12 21:46:16 +00:00
$id = required_param ( 'id' , PARAM_INT );
$fid = optional_param ( 'fid' , 0 , PARAM_INT );
$action = optional_param ( 'action' , '' , PARAM_ACTION );
$confirm = optional_param ( 'confirm' , 0 , PARAM_BOOL );
2009-10-21 19:58:50 +00:00
$PAGE -> set_url ( 'admin/websevice/service_functions.php' , array ( 'id' => $id ));
2009-10-12 21:46:16 +00:00
admin_externalpage_setup ( 'externalservicefunctions' );
$returnurl = " $CFG->wwwroot / $CFG->admin /settings.php?section=externalservices " ;
2009-10-21 19:58:50 +00:00
$thisurl = " service_functions.php?id= $id " ;
2009-10-12 21:46:16 +00:00
$service = $DB -> get_record ( 'external_services' , array ( 'id' => $id ), '*' , MUST_EXIST );
if ( $action === 'delete' and confirm_sesskey () and $service and empty ( $service -> component )) {
$function = $DB -> get_record ( 'external_functions' , array ( 'id' => $fid ), '*' , MUST_EXIST );
if ( ! $confirm ) {
admin_externalpage_print_header ();
$optionsyes = array ( 'id' => $id , 'action' => 'delete' , 'confirm' => 1 , 'sesskey' => sesskey (), 'fid' => $function -> id );
$optionsno = array ( 'id' => $id );
2009-10-21 19:58:50 +00:00
$formcontinue = html_form :: make_button ( 'service_functions.php' , $optionsyes , get_string ( 'delete' ), 'post' );
$formcancel = html_form :: make_button ( 'service_functions.php' , $optionsno , get_string ( 'cancel' ), 'get' );
2009-10-12 21:46:16 +00:00
echo $OUTPUT -> confirm ( get_string ( 'removefunctionconfirm' , 'webservice' , ( object ) array ( 'service' => $service -> name , 'function' => $function -> name )), $formcontinue , $formcancel );
echo $OUTPUT -> footer ();
die ;
}
$DB -> delete_records ( 'external_services_functions' , array ( 'externalserviceid' => $service -> id , 'functionname' => $function -> name ));
redirect ( $thisurl );
2009-10-21 21:24:05 +00:00
} else if ( $action === 'add' and confirm_sesskey () and $service and empty ( $service -> component )) {
2009-10-12 21:46:16 +00:00
2009-10-21 21:24:05 +00:00
if ( $fid and $function = $DB -> get_record ( 'external_functions' , array ( 'id' => $fid ))) {
2009-10-12 21:46:16 +00:00
ignore_user_abort ( true ); // no interruption here!
2009-10-21 21:24:05 +00:00
// make sure the function is not there yet
if ( $DB -> record_exists ( 'external_services_functions' , array ( 'externalserviceid' => $service -> id , 'functionname' => $function -> name ))) {
2009-10-12 21:46:16 +00:00
redirect ( $thisurl );
}
2009-10-21 21:24:05 +00:00
$new = new object ();
$new -> externalserviceid = $service -> id ;
$new -> functionname = $function -> name ;
$DB -> insert_record ( 'external_services_functions' , $new );
redirect ( $thisurl );
2009-10-19 15:09:22 +00:00
}
// Prepare the list of function to choose from
$select = " name NOT IN (SELECT s.functionname
FROM { external_services_functions } s
WHERE s . externalserviceid = : sid
) " ;
2009-10-21 21:24:05 +00:00
$functionchoices = $DB -> get_records_select_menu ( 'external_functions' , $select , array ( 'sid' => $id ), 'name' , 'id, name' );
2009-10-12 21:46:16 +00:00
2009-10-19 15:09:22 +00:00
// Javascript for the function search/selection fields
$PAGE -> requires -> yui_lib ( 'event' );
2009-10-21 19:58:50 +00:00
$PAGE -> requires -> js ( $CFG -> admin . '/webservice/script.js' );
2009-10-19 15:09:22 +00:00
$PAGE -> requires -> js_function_call ( 'capability_service.cap_filter_init' , array ( get_string ( 'search' ))); //TODO generalize javascript
2009-10-12 21:46:16 +00:00
admin_externalpage_print_header ();
2009-10-21 21:24:05 +00:00
2009-10-12 21:46:16 +00:00
echo $OUTPUT -> heading ( $service -> name );
2009-10-21 21:24:05 +00:00
echo $OUTPUT -> box_start ( 'generalbox boxwidthwide boxaligncenter centerpara' );
//TODO: hmm, is this supposed to be replaced by the roles UI, right? If not the use of output lib is definitely wrong, we need more buttons (Cancel!) and custom JS, etc.
//TODO: add JS disabling of submit button if no functio nselected, the error string is not user friendly
2009-10-19 15:09:22 +00:00
//the service form
$form = new html_form ();
2009-10-21 19:58:50 +00:00
$form -> url = new moodle_url ( 'service_functions.php' , array ( 'id' => $id , 'action' => 'add' , 'save' => 1 )); // Required
2009-10-19 15:09:22 +00:00
$form -> button = new html_button ();
$form -> button -> id = 'settingssubmit' ;
$form -> button -> text = get_string ( 'addfunction' , 'webservice' ); // Required
$form -> button -> disabled = false ;
$form -> button -> title = get_string ( 'addfunction' , 'webservice' );
$form -> method = 'post' ;
$form -> id = 'settingsform' ;
//help text
$contents = '<p id="intro">' . get_string ( 'addfunctionhelp' , 'webservice' ) . '</p>' ;
//function section (search field + selection field)
$select = new html_select ();
$select -> options = $functionchoices ;
2009-10-21 21:24:05 +00:00
$select -> name = 'fid' ;
2009-10-19 15:09:22 +00:00
$select -> id = 'menucapability' ; //TODO generalize javascript
2009-10-21 21:24:05 +00:00
$select -> nothinglabel = '' ;
2009-10-19 15:09:22 +00:00
$select -> nothingvalue = '' ;
$select -> listbox = true ;
$select -> tabindex = 0 ;
$contents .= $OUTPUT -> select ( $select );
$contents .= " <br/><br/> " ;
echo $OUTPUT -> form ( $form , $contents );
echo $OUTPUT -> box_end ();
2009-10-12 21:46:16 +00:00
echo $OUTPUT -> footer ();
die ;
}
admin_externalpage_print_header ();
echo $OUTPUT -> heading ( $service -> name );
$select = " name IN (SELECT s.functionname
FROM { external_services_functions } s
WHERE s . externalserviceid = : sid
) " ;
$functions = $DB -> get_records_select ( 'external_functions' , $select , array ( 'sid' => $service -> id ), 'name' );
$strfunction = get_string ( 'function' , 'webservice' );
$strdelete = get_string ( 'removefunction' , 'webservice' );
$stredit = get_string ( 'edit' );
$table = new html_table ();
$table -> head = array ( $strfunction );
$table -> align = array ( 'left' );
$table -> width = '100%' ;
$table -> data = array ();
2009-10-19 15:09:22 +00:00
$table -> head [] = get_string ( 'description' );
$table -> align [] = 'left' ;
2009-10-12 21:46:16 +00:00
if ( empty ( $service -> component )) {
$table -> head [] = $stredit ;
$table -> align [] = 'center' ;
}
2009-10-21 19:58:50 +00:00
$durl = " service_functions.php?sesskey= " . sesskey ();
2009-10-12 21:46:16 +00:00
foreach ( $functions as $function ) {
2009-10-21 21:24:05 +00:00
$function = external_function_info ( $function );
$description = " <span style= \" font-size:90% \" > " . $function -> description . " </span> " ; //TODO: must use class here!
2009-10-12 21:46:16 +00:00
if ( empty ( $service -> component )) {
$delete = " <a href= \" $durl &action=delete&fid= $function->id &id= $service->id\ " > $strdelete </ a > " ;
2009-10-19 15:09:22 +00:00
$table -> data [] = array ( $function -> name , $description , $delete );
2009-10-12 21:46:16 +00:00
} else {
2009-10-19 15:09:22 +00:00
$table -> data [] = array ( $function -> name , $description );
2009-10-12 21:46:16 +00:00
}
}
echo $OUTPUT -> table ( $table );
// we can edit only custom functions, the build-in would be overridden after each upgrade
if ( empty ( $service -> component )) {
$form = new html_form ();
2009-10-21 19:58:50 +00:00
$form -> url = new moodle_url ( 'service_functions.php' , array ( 'sesskey' => sesskey (), 'id' => $service -> id , 'action' => 'add' ));
2009-10-12 21:46:16 +00:00
$form -> button -> text = get_string ( 'add' );
$form -> method = 'get' ;
echo $OUTPUT -> button ( $form );
}
// simple back button
$form = new html_form ();
2009-10-21 19:58:50 +00:00
$form -> url = new moodle_url ( '../settings.php' , array ( 'section' => 'externalservices' ));
2009-10-12 21:46:16 +00:00
$form -> button -> text = get_string ( 'back' );
$form -> method = 'get' ;
echo $OUTPUT -> button ( $form );
echo $OUTPUT -> footer ();