{"id":27,"date":"2016-03-01T13:14:52","date_gmt":"2016-03-01T11:14:52","guid":{"rendered":"http:\/\/www.spanidis.eu\/?p=27"},"modified":"2019-12-05T14:26:32","modified_gmt":"2019-12-05T12:26:32","slug":"moodle-rest-web-services-tutorial-example-instructions-guidelines","status":"publish","type":"post","link":"http:\/\/www.spanidis.eu\/?p=27","title":{"rendered":"Moodle REST Web Services tutorial \u2013 example \u2013 instructions \u2013 guidelines"},"content":{"rendered":"<p>You may download my <a href=\"http:\/\/www.spanidis.eu\/wp-content\/uploads\/moodle_webservice_example.zip\" rel=\"\">php example<\/a> here.<\/p>\n<p>Here you will find <strong>instructions<\/strong> to create <strong>Web Services<\/strong> on <strong>Moodle<\/strong>.<br \/>\nI am not a Moodle expert. I have collected these information from many different sources and I have eliminated their errors to give you an easy to follow tutorial together with a working example.<br \/>\nThis tutorial creates a web service, enabled to create a new user and to enroll this user to a course.<\/p>\n<p>Please follow the steps below:<\/p>\n<p>1) Create the user which will serve the web services.<br \/>\nGo to:<br \/>\nSite Administrator &gt; Users &gt; Accounts &gt; Add new user<br \/>\nUsername: wsuser (user your own)<br \/>\nPassword: (user your own)<br \/>\nFill in the required fields.<\/p>\n<p>2) Give the wsuser the appropriate role.<br \/>\nGo to:<br \/>\nSite Administrator &gt; Users &gt; Permissions &gt; Define roles<br \/>\nPress the Add new role button.<br \/>\nSelect Role archetype = <strong>ARCHETYPE<\/strong>: Manager and press continue.<br \/>\nOn the form:<br \/>\nWeb Service User<br \/>\nContext types where this role may be assigned: System\/User\/Category\/Course<br \/>\nLeave the rest selections as they appear.<br \/>\nCapabilities<br \/>\nAdd the following capabilities:<br \/>\nwebservice\/rest:use<br \/>\nmoodle\/user:create<br \/>\nenrol\/manual:enrol<br \/>\nenrol\/category:synchronised<br \/>\nenrol\/manual:unenrolself<br \/>\nPress Create this role button.<\/p>\n<p>3) Assign System roles.<br \/>\nGo to:<br \/>\nSite Administrator &gt; Users &gt; Permissions &gt; Assign System Roles<br \/>\nAssign wsuser user.<\/p>\n<p>4) Create the needed services.<br \/>\nGo to:<br \/>\nSite Administrator &gt; Plugins &gt; Web Services &gt; External Services<br \/>\nWe need the two services below:<\/p>\n<p>a) <strong>Create user \u2013 service<\/strong><br \/>\nOn custom services table press add.<br \/>\nName: create_user<br \/>\nShort: create_user<br \/>\n&#8220;Enabled&#8221; should be checked.<br \/>\n&#8220;Authorized users only&#8221; should be checked.<br \/>\nPress &#8220;show more&#8221;.<br \/>\nSearch for capability moodle\/user:create and select it.<br \/>\nPress &#8220;add service&#8221;.<br \/>\nThis service has no functions.<br \/>\nAdd functions.<br \/>\nSearch for the <strong>core_user_create_user<\/strong><br \/>\nSelect it and press add function.<\/p>\n<p>b) <strong>Enroll user \u2013 service<\/strong><br \/>\nOn custom services table press &#8220;add&#8221;.<br \/>\nName: enrol_user<br \/>\nShort: enrol_user<br \/>\n&#8220;Enabled&#8221; should be checked.<br \/>\n&#8220;Authorized users only&#8221; should be checked.<br \/>\nPress &#8220;show more&#8221;.<br \/>\nSearch for capability enroll\/manual:enrol and select it.<br \/>\nPress &#8220;add service&#8221;.<br \/>\nThis service has no functions.<br \/>\nAdd functions.<br \/>\nSearch for the <strong>enrol_manual_enrol_users<\/strong><br \/>\nSelect it and press &#8220;add function&#8221;.<br \/>\nOn the list of custom services you have to add the authorized user to be the wsuser.<\/p>\n<p>5) Create the Tokens.<br \/>\nGo to:<br \/>\nSite Administrator &gt; Plugins &gt; Web Services &gt; External Services &gt; Manage Tokens<br \/>\nAdd<br \/>\na) Create User<br \/>\nSelect user Web Service User<br \/>\nService \u2013 create user<br \/>\nSave changes.<\/p>\n<p>b) Enroll User<br \/>\nSelect user Web Service User<br \/>\nService \u2013 enrol user<br \/>\nSave changes.<\/p>\n<p>You now have the two required tokens.<\/p>\n<p>6) All web service protocols are disabled. The \u00abEnable web services\u00bb setting can be found in Advanced features.<br \/>\nGo to:<br \/>\n\u2022 Site Administrator &gt; Advanced features &gt; Enable web services checked \u2013 Save.<br \/>\n\u2022 Site Administrator &gt; Plugins &gt; Web Services &gt; External Services &gt; Manage Protocols.<br \/>\nActivate <strong>REST<\/strong> protocol.<br \/>\nCheck the documentation box.<\/p>\n<p>7) CURL.php is required on the client side. You may download it <a href=\"http:\/\/www.spanidis.eu\/wp-content\/uploads\/2015\/08\/curl.zip\" rel=\"\">here<\/a>.<br \/>\n8) Check if create user works using php code.<br \/>\n9) Check if enroll user works using php code.<br \/>\n10) The wsuser user can not assign role without being enrolled in the course\/s!!!<\/p>\n<p>Best regards<br \/>\nPavlos<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may download my php example here. Here you will find instructions to create Web Services on Moodle. I am not a Moodle expert. I have collected these information from many different sources and I have eliminated their errors to give you an easy to follow tutorial together with a working example. This tutorial creates &hellip; <a href=\"http:\/\/www.spanidis.eu\/?p=27\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Moodle REST Web Services tutorial \u2013 example \u2013 instructions \u2013 guidelines<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[10,6,8,9],"_links":{"self":[{"href":"http:\/\/www.spanidis.eu\/index.php?rest_route=\/wp\/v2\/posts\/27"}],"collection":[{"href":"http:\/\/www.spanidis.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.spanidis.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.spanidis.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.spanidis.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=27"}],"version-history":[{"count":8,"href":"http:\/\/www.spanidis.eu\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":54,"href":"http:\/\/www.spanidis.eu\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions\/54"}],"wp:attachment":[{"href":"http:\/\/www.spanidis.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.spanidis.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.spanidis.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}