public function test_call_external_function() { global $PAGE, $COURSE; $this->resetAfterTest(true); // Call some webservice functions and verify they are correctly handling $PAGE and $COURSE. // First test a function that calls validate_context outside a course. $this->setAdminUser(); $category = $this->getDataGenerator()->create_category(); $params = array('contextid' => context_coursecat::instance($category->id)->id, 'name' => 'aaagrrryyy', 'idnumber' => '', 'description' => ''); $cohort1 = $this->getDataGenerator()->create_cohort($params); $cohort2 = $this->getDataGenerator()->create_cohort(); $beforepage = $PAGE; $beforecourse = $COURSE; $params = array('cohortids' => array($cohort1->id, $cohort2->id)); $result = external_api::call_external_function('core_cohort_get_cohorts', $params); $this->assertSame($beforepage, $PAGE); $this->assertSame($beforecourse, $COURSE); // Now test a function that calls validate_context inside a course. $course = $this->getDataGenerator()->create_course(); $beforepage = $PAGE; $beforecourse = $COURSE; $params = array('courseid' => $course->id, 'options' => array()); $result = external_api::call_external_function('core_enrol_get_enrolled_users', $params); $this->assertSame($beforepage, $PAGE); $this->assertSame($beforecourse, $COURSE); }
$requests = json_decode($rawjson, true); if ($requests === null) { $lasterror = json_last_error_msg(); throw new coding_exception('Invalid json in request: ' . $lasterror); } $responses = array(); // Defines the external settings required for Ajax processing. $settings = external_settings::get_instance(); $settings->set_file('pluginfile.php'); $settings->set_fileurl(true); $settings->set_filter(true); $settings->set_raw(false); foreach ($requests as $request) { $response = array(); $methodname = clean_param($request['methodname'], PARAM_ALPHANUMEXT); $index = clean_param($request['index'], PARAM_INT); $args = $request['args']; $response = external_api::call_external_function($methodname, $args, true); $responses[$index] = $response; if ($response['error']) { // Do not process the remaining requests. break; } } echo json_encode($responses);