2 /***************************************
3 example of a MoodleWS client in PHP5.
4 with SoapClient trace enabled
5 this demo code is meant to be run as it
6 from the directory $CFG->wwwroot/wspp/clients.
8 To use this code from another machine you
9 MUST run on THAT machine the supplied utility script
10 wsdl2php.php against your Moodle server
11 to generate the MoodleWs class
12 and all support classes for returned datatypes.
13 eg: php wsdl2php.php http://youmoodle/wspp/wsdl_pp.php.
16 Running that utility several times is harmless.
17 If you modify server code and wsdl to add new remote calls
18 you MUST rerun the wsdl2php utilty to keep your clients in sync.
21 Don't forget to adjust the paths to required files
22 **********************************************/
24 ini_set('soap.wsdl_cache_enabled', '0'); // Set to '0' for debugging.
27 require("MoodleWS.php");
29 $moodle= new MoodleWS("http://localhost:8888/moodle19/wspp/wsdl_pp.php",
33 heading ("login IN ");
34 $lr= $moodle->login(LOGIN,PASSWORD);
35 print_r_pre($lr,$moodle);
38 print_r_pre($moodle->get_last_changes($lr->client,$lr->sessionkey,2,'id',5),$moodle);
41 heading ("get courses by category 2");
43 print_r_pre($moodle->get_courses_bycategory($lr->client,$lr->sessionkey,2),$moodle);
46 heading ("get roles");
48 print_r_pre($moodle->get_roles($lr->client,$lr->sessionkey),$moodle);
51 heading ("get categories");
53 print_r_pre($moodle->get_categories($lr->client,$lr->sessionkey),$moodle);
56 heading ("get my courses (as current user)");
58 print_r_pre($moodle->get_my_courses($lr->client,$lr->sessionkey),$moodle);
60 heading ("get my courses (guest)");
62 print_r_pre($moodle->get_my_courses($lr->client,$lr->sessionkey,1),$moodle);
65 heading ("get users (ppollet,pguy,toto)");
67 print_r_pre($moodle->get_users($lr->client,$lr->sessionkey,array('ppollet','pguy','toto'),'username'),$moodle);
71 heading ("get courses ('C2I_101','1PC_PASSINFO','unknown']");
73 //TODO retourne le cours 0 si 'idnumber' est absent
75 print_r_pre($moodle->get_courses($lr->client,$lr->sessionkey,array('C2I_101','1PC_PASSINFO','unknown'),'idnumber'),$moodle);
77 heading ("get course C2I_101");
79 print_r_pre($moodle->get_course($lr->client,$lr->sessionkey,'C2I_101'),,$moodle);
81 heading ("get course by id 2 ");
83 print_r_pre($moodle->get_course_byid($lr->client,$lr->sessionkey,2),$moodle);
85 heading ("get course by id -1 unknown ");
87 print_r_pre($moodle->get_course_byid($lr->client,$lr->sessionkey,-1),$moodle);
91 heading ("get user astrid");
93 print_r_pre($moodle->get_user($lr->client,$lr->sessionkey,'astrid','username'),$moodle);
97 heading ("get users by course id=2 role =0, any ");
99 $myc=$moodle->get_users_bycourse($lr->client,$lr->sessionkey,2,'id',0);
101 print (count($myc->users));
102 print(" users found <br />\n");
104 heading ("get users by course id=2 role =3, teachers");
106 $myc=$moodle->get_users_bycourse($lr->client,$lr->sessionkey,2,'id',3);
108 print(count($myc->users));
109 print(" users found <br />\n");
112 heading ("get users by course id=2 role =5, students");
114 $myc=$moodle->get_users_bycourse($lr->client,$lr->sessionkey,2,'id',5);
116 print(count($myc->users));
117 print(" users found <br />\n");
121 heading ("get user by name pguy");
124 print_r_pre($moodle->get_user_byusername($lr->client,$lr->sessionkey,'pguy'),$moodle);
126 heading ("get user by id 77");
128 print_r_pre($moodle->get_user_byid($lr->client,$lr->sessionkey,77),$moodle);
130 heading ("get user by id -1 (unknown)");
132 print_r_pre($moodle->get_user_byid($lr->client,$lr->sessionkey,-1),$moodle);
137 heading ("logout and bye");
140 $moodle->logout($lr->client,$lr->sessionkey,$moodle);
143 function heading ($msg) {
144 print "<h2>$msg</h2>\n";
148 * print something in <pre> tags for better viewing on a browser
149 * @param $var any type : the info to dump
150 * @param $client : an instance of MoodleWS contructed with option trace=1
151 * thus allowing a call to debug functions __getLast*
154 function print_r_pre($var,$client=null) {
159 echo "REQUEST HEADERS:\n" . $client->client->__getLastRequestHeaders() . "<br>\n";
160 echo "REQUEST:\n" . $client->client->__getLastRequest() . "<br>\n";
161 echo "RESPONSE HEADERS:\n" . $client->client->__getLastResponseHeaders() . "<br>\n";
162 echo "RESPONSE:\n" . $client->client->__getLastResponse() . "<br>\n";