1616
1717namespace tool_trigger \steps \actions ;
1818
19- require_once ($ CFG ->libdir . '/externallib.php ' );
2019use core_external \external_api ;
2120
2221/**
@@ -112,7 +111,7 @@ private function run_function() {
112111 $ params = $ this ->render_datafields ($ this ->params );
113112
114113 // Execute the provided function name passing with the given parameters.
115- $ response = self ::call_external_function ($ functionname , json_decode ($ params , true ));
114+ $ response = external_api ::call_external_function ($ functionname , json_decode ($ params , true ));
116115 return $ response ;
117116 }
118117
@@ -155,7 +154,7 @@ public function execute($step, $trigger, $event, $stepresults) {
155154 // Throw an error if step results are not being returned.
156155 $ stepresults ['error ' ] = json_encode ($ response ['exception ' ]);
157156
158- return [ false , $ stepresults ];
157+ $ status = [ true , $ response ];
159158 }
160159 } catch (\Throwable $ e ) {
161160 // Restore the previous user to avoid any side-effects occuring in later steps / code.
@@ -176,8 +175,8 @@ public function execute($step, $trigger, $event, $stepresults) {
176175 \core \session \manager::set_user ($ previoususer );
177176 $ SESSION = $ session ;
178177
179- // Return stepresults .
180- return [ true , $ stepresults ] ;
178+ // Return the function call response as is. The shape is already normalised .
179+ return $ status ;
181180 }
182181
183182 /**
@@ -203,12 +202,6 @@ public function form_definition_extra($form, $mform, $customdata) {
203202 $ mform ->addElement ('textarea ' , 'params ' , get_string ('webserviceactionparams ' , 'tool_trigger ' ), $ attributes );
204203 $ mform ->setType ('params ' , PARAM_RAW_TRIMMED );
205204 $ mform ->addHelpButton ('params ' , 'webserviceactionparams ' , 'tool_trigger ' );
206-
207- // Params.
208- $ attributes = ['cols ' => '50 ' , 'rows ' => '5 ' ];
209- $ mform ->addElement ('textarea ' , 'alphaparams ' , get_string ('webserviceactionalphaparmas ' , 'tool_trigger ' ), $ attributes );
210- $ mform ->setType ('alphaparams ' , PARAM_RAW_TRIMMED );
211- $ mform ->addHelpButton ('alphaparams ' , 'webserviceactionalphaparmas ' , 'tool_trigger ' );
212205 }
213206
214207 /**
@@ -337,106 +330,4 @@ public function transform_form_data($data) {
337330 }
338331 return $ data ;
339332 }
340-
341- /**
342- * Call an external function validating all params/returns correctly.
343- *
344- * Note that an external function may modify the state of the current page, so this wrapper
345- * saves and restores tha PAGE and COURSE global variables before/after calling the external function.
346- * This is a fork of the external_api::call_external_function method.
347- * Due the nature of the WS API, without a real user session the function may not work as expected.
348- *
349- * @param string $function A webservice function name.
350- * @param array $args Params array (named params)
351- * @param boolean $ajaxonly If true, an extra check will be peformed to see if ajax is required.
352- * @return array containing keys for error (bool), exception and data.
353- */
354- public static function call_external_function ($ function , $ args , $ ajaxonly =false ) {
355- global $ PAGE , $ COURSE , $ CFG , $ SITE ;
356-
357- require_once ($ CFG ->libdir . "/pagelib.php " );
358-
359- $ externalfunctioninfo = \external_api::external_function_info ($ function );
360-
361- // Eventually this should shift into the various handlers and not be handled via config.
362- $ readonlysession = $ externalfunctioninfo ->readonlysession ?? false ;
363-
364- if (!$ readonlysession || empty ($ CFG ->enable_read_only_sessions )) {
365- \core \session \manager::restart_with_write_lock ($ readonlysession );
366- }
367-
368- $ currentpage = $ PAGE ;
369- $ currentcourse = $ COURSE ;
370- $ response = array ();
371-
372- try {
373- // Taken straight from from setup.php.
374- if (!empty ($ CFG ->moodlepageclass )) {
375- if (!empty ($ CFG ->moodlepageclassfile )) {
376- require_once ($ CFG ->moodlepageclassfile );
377- }
378- $ classname = $ CFG ->moodlepageclass ;
379- } else {
380- $ classname = 'moodle_page ' ;
381- }
382-
383- $ PAGE = new $ classname ();
384- $ COURSE = clone ($ SITE );
385-
386- // Validate params, this also sorts the params properly, we need the correct order in the next part.
387- $ callable = array ($ externalfunctioninfo ->classname , 'validate_parameters ' );
388-
389- $ params = call_user_func ($ callable ,
390- $ externalfunctioninfo ->parameters_desc ,
391- $ args );
392-
393- $ params = array_values ($ params );
394- // Allow any Moodle plugin a chance to override this call. This is a convenient spot to
395- // make arbitrary behaviour customisations. The overriding plugin could call the 'real'
396- // function first and then modify the results, or it could do a completely separate
397- // thing.
398- $ callbacks = get_plugins_with_function ('override_webservice_execution ' );
399- $ result = false ;
400-
401- foreach ($ callbacks as $ plugintype => $ plugins ) {
402- foreach ($ plugins as $ plugin => $ callback ) {
403- $ result = $ callback ($ externalfunctioninfo , $ params );
404- if ($ result !== false ) {
405- break 2 ;
406- }
407- }
408- }
409-
410- // If the function was not overridden, call the real one.
411- if ($ result === false ) {
412- $ callable = array ($ externalfunctioninfo ->classname , $ externalfunctioninfo ->methodname );
413- $ result = call_user_func_array ($ callable , $ params );
414- }
415-
416- // Validate the return parameters.
417- if ($ externalfunctioninfo ->returns_desc !== null ) {
418- $ callable = array ($ externalfunctioninfo ->classname , 'clean_returnvalue ' );
419- $ result = call_user_func ($ callable , $ externalfunctioninfo ->returns_desc , $ result );
420- }
421-
422- $ response ['error ' ] = false ;
423- $ response ['data ' ] = $ result ;
424- } catch (\Throwable $ e ) {
425- $ exception = get_exception_info ($ e );
426- unset($ exception ->a );
427- $ exception ->backtrace = format_backtrace ($ exception ->backtrace , true );
428- if (!debugging ('' , DEBUG_DEVELOPER )) {
429- unset($ exception ->debuginfo );
430- unset($ exception ->backtrace );
431- }
432- $ response ['error ' ] = true ;
433- $ response ['exception ' ] = $ exception ;
434- // Do not process the remaining requests.
435- }
436-
437- $ PAGE = $ currentpage ;
438- $ COURSE = $ currentcourse ;
439-
440- return $ response ;
441- }
442333}
0 commit comments