1 <?php // $Id: ws-test.php,v 1.3 2008/07/10 16:06:24 ppollet Exp $
4 Inspired by OKTech sample client
5 Do not used generated class MoodleWS_NS, so can be used with PHP4
6 See ws_course_xsl_nusoap.php for a php5 example using nusoap
9 require_once('../../config.php');
10 // use Moodle distributed nusoap lib
11 require_once($CFG->dirroot . '/lib/soap/nusoap.php');
14 ini_set('soap.wsdl_cache_enabled', '0'); // Set to '0' for debugging.
18 * For the examples below I have assigned the following idnumber fields:
19 * - For the guest user: 'USER-guest'
20 * - For the admin user: 'USER-admin'
21 * - Two manually created users: 'USER-one' and 'USER-two'
22 * - The user created by this script is now assigned an idnumber of: 'USER-wstest'
24 * - For the SITEID course: 'COURSE-1'
25 * - Two simple courses I created: 'COURSE-2' and 'COURSE-3'
27 define('WS_PRINT_DEBUG', true);
28 define('WS_WSDL_URL', $CFG->wwwroot . '/wspp/wsdl_pp.php');
29 define('WS_USER_IDFIELD', 'username');
30 define('WS_COURSE_IDFIELD', 'idnumber');
31 define('WS_USER_GET_IDS', 'ppollet,alexis,astrid,pguy');
32 define('WS_COURSE_GET_IDS', '1PC_PASSINFO,C2I_101');
33 define('WS_GRADES_USER_ID', 'USER-one');
34 define('WS_GRADES_COURSE_IDS', 'COURSE-2,COURSE-4,COURSE-3');
35 define('WS_ENROL_COURSE_ID', 'COURSE-wstest');
36 define('WS_ENROL_USER_IDS', 'astorck,toto,ppollet,alexis,astrid,pguy,uribeiro');
45 /// User-spefic tests.
49 // 'get_users_bycourse',
52 /// User-specific tests.
66 function proc_result($result, $client, $debug = WS_PRINT_DEBUG) {
67 echo '<p><b>Sent server call...</b></p>';
70 echo 'Request: <xmp>' . $client->request . '</xmp>';
71 echo 'Response: <xmp>' . $client->response . '</xmp>';
72 echo 'Debug log: <pre>' . $client->debug_str . '</pre>';
75 /// Check for a fault.
77 echo '<p><b>Fault: ' . $result['faultstring'] . '</b></p>';
80 $err = $client->getError();
83 echo '<p><b>Error: ' . $err . '</b></p>';
84 print_object($result);
86 /// Check for server sent errors.
87 if (!empty($result->error)) {
88 /// Display the error.
89 echo '<p><b>Received error...</b></p>';
92 print_object($result->error);
93 print_object($result->stat);
94 print_object($result->client);
95 print_object($result->sessionkey);
100 /// Display the result.
101 echo '<p><b>Received result...</b></p>';
102 if (WS_PRINT_DEBUG) {
103 print_object($result);
111 /// === Start tests code. ===
114 echo '<p><b>Attempting to create a server connection...</b></p>';
116 /// Create the client instance
117 $client = new soap_client(WS_WSDL_URL, true);
119 /// Check for an error
120 $err = $client->getError();
122 /// Display the error.
123 echo '<p><b>Constructor error: ' . $err . '</b></p>';
126 echo '<p><b>Client connected...</b></p>';
129 $proxy = $client->getProxy();
133 if (in_array('login', $tests)) {
134 print_heading('Login:');
136 $result = proc_result($proxy->login(LOGIN, PASSWORD), $proxy);
138 $mycid = $result['client'];
139 $mykey = $result['sessionkey'];
144 if (in_array('add_user', $tests)) {
145 print_object('<h2>Edit User (ADD):</h2>');
150 'idnumber' => 'USER-wstest',
151 'username' => 'wstest',
153 'lastname' => 'Test',
154 'password' => md5('wstest'),
155 'email' => 'jfilip+ws@gmail.com',
162 'action' => 'Delete',
163 'idnumber'=> 'Lyon3_014'
167 'action' => 'Update',
168 'idnumber' => 'USER-wstest',
169 'username' => 'wstest',
170 'firstname' => 'W S',
171 'lastname' => 'last Test PP',
172 'password' => md5('wstest256'),
173 'email' => 'jfilip+ws@gmail.com',
178 proc_result($proxy->edit_users($mycid, $mykey, array('users' => $users)), $proxy);
182 /// EDIT_USER (UPDATE)
183 if (in_array('update_user', $tests)) {
184 print_heading('Edit User (UPDATE):');
188 'action' => 'Update',
189 'idnumber' => '123456789'
193 'action' => 'Update',
194 'idnumber' => 'USER-wstest',
195 'firstname' => 'Web Services',
199 proc_result($proxy->edit_users($mycid, $mykey, array('users' => $users)), $proxy);
204 if (in_array('get_users', $tests)) {
205 print_heading('Get Users:');
209 if ($uids = explode(',', WS_USER_GET_IDS)) {
210 foreach ($uids as $uid) {
215 proc_result($proxy->get_users($mycid, $mykey, $userids, WS_USER_IDFIELD), $proxy);
218 /// GET_USERS_BYCOURSE
219 if (in_array('get_users_bycourse', $tests)) {
220 print_heading('Get Users by course : course id=2 role =5');
222 proc_result($proxy->get_users_bycourse($mycid, $mykey, 2,'id',5), $proxy);
227 /// EDIT_USER (DELETE)
228 if (in_array('delete_user', $tests)) {
229 print_heading('Edit User (DELETE):');
233 'action' => 'Delete',
234 'idnumber' => '123456789'
238 'action' => 'Delete',
239 'idnumber' => 'USER-wstest'
242 proc_result($proxy->edit_user($mycid, $mykey, array('users' => $users)), $proxy);
246 /// EDIT_COURSE (ADD)
247 if (in_array('add_course', $tests)) {
248 print_heading('Edit Course (ADD):');
253 'idnumber' => 'COURSE-wstest',
254 'shortname' => 'WS_01',
255 'fullname' => 'Web Services Test Course #1',
256 'summary' => 'Web Services Test Course #1<br /><br /><b>Created with the Moodle Web Services layer</b>',
263 proc_result($proxy->edit_courses($mycid, $mykey, array('courses' => $courses)), $proxy);
267 /// EDIT_COURSE (UPDATE)
268 if (in_array('update_course', $tests)) {
269 print_heading('Edit Course (UPDATE):');
273 'action' => 'Update',
278 'action' => 'Update',
279 'idnumber' => 'COURSE-wstest',
280 'summary' => 'Web Services Test Course #1<br /><br /><b><u>Modified</u> with the Moodle Web Services layer</b>.',
284 proc_result($proxy->edit_courses($mycid, $mykey, array('courses' => $courses)), $proxy);
289 if (in_array('get_courses', $tests)) {
290 print_heading('Get Courses:');
292 $courseids = array();
294 if ($cids = explode(',', WS_COURSE_GET_IDS)) {
295 foreach ($cids as $cid) {
300 proc_result($proxy->get_courses($mycid, $mykey, $courseids, WS_COURSE_IDFIELD), $proxy);
304 /// EDIT_COUSE (DELETE)
305 if (in_array('delete_course', $tests)) {
306 print_heading('Edit Course (DELETE):');
310 'action' => 'Delete',
311 'idnumber' => 'COURSE-wstest'
314 proc_result($proxy->edit_courses($mycid, $mykey, array('courses' => $courses)), $proxy);
319 if (in_array('get_grades', $tests)) {
320 print_heading('Get Grades:');
322 $courseids = array();
324 if ($cids = explode(',', WS_GRADES_COURSE_IDS)) {
325 foreach ($cids as $cid) {
326 $courseids['courseid'][] = $cid;
330 proc_result($proxy->get_grades($mycid, $mykey, WS_GRADES_USER_ID, $courseids, WS_COURSE_IDFIELD), $proxy);
335 if (in_array('enrol_students', $tests)) {
336 print_heading('Enrol Students:');
340 if ($uids = explode(',', WS_ENROL_USER_IDS)) {
341 foreach ($uids as $uid) {
346 proc_result($proxy->enrol_students($mycid, $mykey, WS_ENROL_COURSE_ID, $userids, WS_USER_IDFIELD), $proxy);
351 if (in_array('logout', $tests)) {
352 print_heading('Logout:');
354 proc_result($proxy->logout($mycid, $mykey), $proxy);