{"id":3421,"date":"2019-11-10T20:51:49","date_gmt":"2019-11-10T18:51:49","guid":{"rendered":"http:\/\/9v.lt\/blog\/?p=3421"},"modified":"2022-01-19T08:34:33","modified_gmt":"2022-01-19T06:34:33","slug":"moodle-additional-functions-web-service","status":"publish","type":"post","link":"https:\/\/9v.lt\/blog\/moodle-additional-functions-web-service\/","title":{"rendered":"Moodle additional functions web service"},"content":{"rendered":"<p>Not long ago I was tasked to make a tool to manage Moodle courses by moving them through categories, creating and cleaning them up. Now I had to make some way to &#8220;refresh&#8221; the course participants and that means unenrolling teachers and managers, assign different roles, suspending students, adding students, all that stuff.<\/p>\n<p>This was going pretty good and easy until I hit upon a web service function <code>core_enrol_edit_user_enrolment<\/code>. It&#8217;s an <code><em>External function that updates a given user enrolment<\/em><\/code>, it is needed to suspend students from a course.<br \/>\nFrom the official API docs, we can see that it takes in required arguments <strong>courseid<\/strong> (User enrolment ID), <strong>ueid<\/strong> (User enrolment ID) and <strong>status<\/strong> (Enrolment status).<\/p>\n<p>The problem here is with a <strong>ueid<\/strong> (User enrolment ID) argument because there&#8217;s no function that returns such an ID. At least I wasn&#8217;t able to find one easily accessible and I had to make my own web service function to get that ID.<br \/>\n<!--more--><br \/>\nIt appears that Moodle plugin development isn&#8217;t thoroughly documented and straightforward, so I took an <a href=\"https:\/\/github.com\/moodlehq\/moodle-local_wstemplate\" rel=\"noopener noreferrer\" target=\"_blank\">example template<\/a> and built on it by reading here and there.<\/p>\n<p>The sources that I used were:<br \/>\n<a href=\"https:\/\/docs.moodle.org\/dev\/Enrolment_API\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/docs.moodle.org\/dev\/Enrolment_API<\/a><br \/>\n<a href=\"https:\/\/docs.moodle.org\/dev\/Adding_a_web_service_to_a_plugin\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/docs.moodle.org\/dev\/Adding_a_web_service_to_a_plugin<\/a><br \/>\n<a href=\"http:\/\/www.examulator.com\/er\/components\/course.html\" rel=\"noopener noreferrer\" target=\"_blank\">http:\/\/www.examulator.com\/er\/components\/course.html<\/a><br \/>\n<a href=\"https:\/\/docs.moodle.org\/dev\/Tutorial\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/docs.moodle.org\/dev\/Tutorial<\/a><br \/>\nand some various forum posts.<\/p>\n<p>From the table diagram I made out this SQL query to get the record ID:<\/p>\n<pre lang=\"sql\">SELECT {user_enrolments}.id AS id, {user_enrolments}.enrolid AS enrolid, {user_enrolments}.userid AS userid, {enrol}.courseid AS courseid\r\nFROM {user_enrolments}, {enrol}\r\nWHERE {user_enrolments}.enrolid = {enrol}.id AND {enrol}.courseid = ? AND {user_enrolments}.userid = ?<\/pre>\n<p>Because the function expects not the Enrol ID (ID of the record in <code>enrol<\/code> table), but the ID of the record from <code>user_enrolments<\/code> table, that is what I returned.<\/p>\n<p>And so I made a functioning plugin which will be as a container for functions that I might need later in life :P the plugin can be found on my Github: <a href=\"https:\/\/github.com\/kulverstukas1\/moodle-additional-functions\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/github.com\/kulverstukas1\/moodle-additional-functions<\/a><\/p>\n<p>To use it, upload the contents of that repo to a folder <moodle folder>\/local\/additional_functions and install in Moodle as normal. A function <code>local_get_user_enrolment_id<\/code> appears, you can add it to your web service and query it as normal by giving a user ID to find and a course ID to search in.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Not long ago I was tasked to make a tool to manage Moodle courses by<\/p>\n","protected":false},"author":2,"featured_media":3422,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,750],"tags":[1051,1049,893,1050],"class_list":["post-3421","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects","category-software-projects","tag-enrolment-id","tag-moodle","tag-php","tag-web-service"],"_links":{"self":[{"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/posts\/3421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/comments?post=3421"}],"version-history":[{"count":0,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/posts\/3421\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/media\/3422"}],"wp:attachment":[{"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/media?parent=3421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/categories?post=3421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/tags?post=3421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}