Hello,
I need to create a webservice which will accept student details (first and last name, address, username etc) and creates a user account in Moodle.
Please let me know how can i acheive this. Any help would be greatly appreciated.
<?php require_once ('../MoodleWS.php'); $moodle=new MoodleWS(); require_once ('../auth.php'); /**test code for MoodleWS: add on course * @param integer $client * @param string $sesskey * @param userDatum $user * @return editUsersOutput */ $lr=$moodle->login(LOGIN,PASSWORD); $user= new userDatum(); $user->setAction(''); $user->setId(0); $user->setConfirmed(0); $user->setPolicyagreed(0); $user->setDeleted(0); $user->setUsername(''); $user->setAuth(''); $user->setPassword(''); $user->setPasswordmd5(''); $user->setIdnumber(''); $user->setFirstname(''); $user->setLastname(''); $user->setEmail(''); $user->setEmailstop(0); $user->setIcq(''); $user->setSkype(''); $user->setYahoo(''); $user->setAim(''); $user->setMsn(''); $user->setPhone1(''); $user->setPhone2(''); $user->setInstitution(''); $user->setDepartment(''); $user->setAddress(''); $user->setCity(''); $user->setCountry(''); $user->setLang(''); $user->setTimezone(0); $user->setLastip(''); $user->setTheme(''); $user->setDescription(''); $user->setMnethostid(0); $res=$moodle->add_user($lr->getClient(),$lr->getSessionKey(),$user); print_r($res); print($res->getUsers()); $moodle->logout($lr->getClient(),$lr->getSessionKey()); ?>
So just fill the blanks (or comment out data that you do not want to send to Moodle) and enjoy...
this answer is already in OKTech Ws Moodle's forum :
http://moodle.org/mod/forum/post.php?reply=615268
Similary you will find test_update_user that expect also an userDatum object filled with the requested changes and a string giving which of the field of the Userdatum record is to be used to locate the target user (likely id, idnumber, username or email that are unique in Moodle DB).
Finally you will find test_delete_user that expect just a unique value and an identifying string such as
// delete by username
$res=$moodle->delete_user($lr->getClient(),$lr->getSessionKey(),'me','username');
or
$res=$moodle->delete_user($lr->getClient(),$lr->getSessionKey(),'me@somewhere','email');
Alternatively you may the edit_users operation for bulk processing . It expect an array of userDatum records (each one built as above with more of less details depending of the wanted operation) plus for each an operation attribute named action (add, udpate or delete). Internally the add_user operation build the array expected by edit_users and calls it. See source code in mdl_soap_server.class.php
The same goes for other entities (courses, groups, groupings, categories, section, labels, wiki, forums ...)
And if you do not want to use php, sample codes are provided in java or Python on the project 's page with usage of wsdl2java or wsdl2py to create almost the same skeletons in these languages.
Or install the graphical SoapUI utility that will create for you skeleton of requests based on the sent wsdl. These skeletons will help you to test the service and guess the required parameters to be used in you favorite language.
Cheers.
Hello Patrick,
Thanks for your reply and detailed guidance. I will start working on this and will keep you posted regarding the status.
Hi Pinal,
I am in exactly same situation as you are ...am a .Net guy but need to create a webservice in PHP for Moodle version 1.9.7
I am going to follow the guidance that Patrick has mentioned in my post and will let you guys know how is it going.
Thanks.
Hey Patrick,
I was working on the steps mentioned in INSTALL file but not sure how to and where to execute the following command:
on the server go to wspp/clients and run the following
php ../wsdl2php.php http://yourmoodle/wspp/wsdl_pp.php
I am very new to PHP and so nay advice would be a great help.
Thanks a lot.
Thanks a lot Patrick for the reply.
Sorry that I did not mentioned in my previous post that my Moodle site is hosted on remote server. However I am having access to that remote server through cPanel.
Can you please tell me how can I execute the above commands using cPanel?
For using SOAPUi i will review the videos on their site as well as one avaiable on your site.
Regards.
I think i need to first install PHP on my machine(Windows Xp) to enable soap extension, right?
Thanks so much Patrick for helping me on this project. I sincerely appeaciate your time and guidance in this regard.
Hello Patrick,
As suggested by you, I installed PHP on my local machine(with Windows Xp):
1) created a directory "phpfiles" with files wspp/wsdl2php.php and wspp/clients/auth.php.dist
2) phpsoap extension installed on my local machine
3) rename auth.php.dist to auth.php and edit it to provide an account that has access to Moodle (either admin accont ot an account using the webservice authentification méthod).
But i am not sure how to do step 4 as mentioned by you:
ppollet@prope:/tmp/test$ ll
total 28
-rw-r--r-- 1 ppollet ppollet 156 2010-03-01 17:36 auth.php
-rw-r--r-- 1 ppollet ppollet 24382 2010-03-01 17:36 wsdl2php.php
All I am doing is going to the directory "phpfiles"(where I copied the two files) through the command prompt and then executing the php wsdl2pp.php command.
But I am getting error screen saying "CLI encountered a problem and needs to colse"
kindly advise.
Hi Pinal,
Yes I tried executing the php command using command prompt in windows after copying these two files to c:\phpfiles directory(the temporary directory i created for these 2 files.), but getting error screen "CLI has encountered a problem and needs to close".
Just to mention that my Moodle and database is hosted on Remote server and I am executing this command on my local machine. Is this correct way?
Please advice. Thankyou so much.
Also not sure about how to test the web service login operation as mentioned install file:
6) test the Web Service login operation. The easiest way is surely using SoapUI graphical interface.
- create a new Wsdl project with URL http://yourmoodle/wspp/wsdl_pp.php .
- test login operation, and if successfull, copy the returned client number and sessionkey in the corresponding input fields of other operation
It would be really helpful if someone can explain me about how to execute the above step and how to use SOAPUI graphical interface.
Thanks so much.
Hi Pinal,
I sincerely thanks for replying to my post and glad to know that you successfully did it .
I have few questions though...
1) how should I pass parameters from .Net file to a function in this web service. i.e. I want to use add_user($lr->getClient(),$lr->getSessionKey(),$user) funtion which requires $user object. How should i populate this object through a .Net file? Do I need to create a similar object in .net and pass that one?
2) How can I call a .net webservice from Moodle, i.e. I need to call a web service when a student receives the completion certificate and passing values such as username, course id, score etc.
Please advice. Best regards.
Hello Patrick,
I thank you for taking time to reply and help me out with this task. I greatly appreciate this.
Best Regards.
Hey Pinal,
A big thanks for such a prompt reply. I wil try this and let you know
Any clue on how to achieve the 2nd part, calling .Net web service from Moodle?
You were a great help.
Thanks again.
Ritu
Hello Guys,
I have insatlled latest WSPP folder to my Moodle 1.9.7 and I am trying to call add user function froma .net client as mentioned by Pinal.
But I am getting the Content Type in Response Headers as text/html instead of application/soap+xml. I did changed then header in wsdl_pp.php to
header('Content-Type: application/soap+xml; charset=UTF-8');
Also my Site administration block disappeared after I enabled this Web service plug.
Please advice. Thanks so much.
Thanks Patrick this did worked, my Site Admin block is back and I reverted the content type header back to original.
Also when i am browsing my wsdl http://yourmoodle/ws/wspp/wsdl_pp.php it is showing me the correct Moodlewsdl.xml document.
But still I am not able to access the webservice through .Net as mentioned by Pinal.
Pinal, you mentioned in your post that "You dont need to create any new classes, when you add a web reference Visual Studio generates proxy for the service and it creates necessary classes in .net"
This is not happening for me, Visual Studio is not generating the necessary classes and I am getting response header in text/html format as opposed to application/xml. What could be the reason for this. One more thing to mention that I am having a extra directory "ws" from my root directory i.e ws/wspp/ and so http://mymoodle.com/ws/wspp/wspp_pp.php which is still giving me correct xml file. I have edited the path to the root config.php in wsdl_pp.php file accordingly.
Any advice would be helpful. Thanks!
hi pinal,
i follow your steps and installed wspp. but i am getting the below error
Fatal error: require_once() [function.require]: Failed opening required '../config.php' (include_path='.;C:\XAMPP\php\PEAR') inC:\XAMPP\htdocs\moodle1\moodle\ws\wspp\wsdl_pp.php on line 24.
please tell me the mistak.
Thnaks