1 <?php // $Id: ws_course_xls.php,v 1.3 2008/07/10 16:06:24 ppollet Exp $
3 /*******************************
4 example of a MoodleWS client in PHP5.
5 this demo code is meant to be run as it
6 from the directory $CFG->wwwroot/wspp/clients.
10 MUST run on THAT machine (including this directory on server)
11 the supplied utility script
12 wsdl2php.php against your Moodle server as follow
13 to generate the MoodleWs class
14 and all support classes for returned datatypes.
16 eg: php wsdl2php.php http://youmoodle/wspp/wsdl_pp.php.
18 Running that utility several times is harmless.
20 If you modify server code and wsdl to add new remote calls
21 you MUST rerun the wsdl2php utility on every client,
22 including this demo directory, to keep them in sync.
25 Don't forget to adjust the paths to required files
26 *******************************/
28 // Return in CSV format the users within a given course
29 // having the given role
30 // using MoodleWS services
31 // Current user role (as teacher of that course) is tested and required
32 // by the Web Service call get_users_bycourse.
35 require_once('MoodleWS.php'); // adjust on remote clients
36 require("auth.php"); // see example in wspp/clients directory
38 /* TESTS in command line */
39 if( $_SERVER['argc'] == 3 ) { // php ws_course_xls courseid roleid
40 $courseid= $_SERVER['argv'][1];
41 $roleid= $_SERVER['argv'][2];
42 } else { //http://yourmoodle/wspp/clients/ws_course_xls.php?courseid=xx&roleid=zz
43 $courseid = $_GET['courseid'];
44 $roleid = $_GET['roleid']; // Role , 0= all roles
47 ini_set('soap.wsdl_cache_enabled', '0'); // Set to '0' for debugging.
49 $moodle= new MoodleWS();
52 $lr= $moodle->login(LOGIN,PASSWORD);
55 $users=$moodle->get_users_bycourse($lr->client,$lr->sessionkey,$courseid,'id',$roleid);
57 catch (Exception $e) {
59 $user->error=$e->getMessage();
60 $users->users[0]=$user;
64 $fields=array('id','lastname','firstname','idnumber','department','institution','address','email');
67 $strFic=$courseid."_all.csv";
69 $strFic=$courseid."_".$roleid.".csv";
73 header("content-type: application/vnd.ms-excel");
74 header("content-disposition: attachment; filename=$strFic");
76 echo (implode("\t",$fields)."\n");
77 foreach ($users->users as $user) {
79 foreach($fields as $field) {
80 print($user->$field."\t");
84 print("$user->error\n");
87 $moodle->logout($lr->client,$lr->sessionkey);