Fetching data from an external MySQL server.

Fetching data from an external MySQL server.

by Gustavo Lopes -
Number of replies: 5

Greetings!

I am fairly new to Moodle systems and am developing a system integration plugin for my university. Currently, I am stuck at a certain step, detailed below:


I need to fetch a course information from an external database, not related to Moodle, compare it to user data and then work it into creating new courses and populating them. So far I've created the block plugin and messed around with interface properties. I have searched for a couple days now on how to proceed for my plugin to be able to access and retrieve info from this external source, but all I've been able to find was various methods on allowing external applications to access Moodle via tokens.


Is there a known way that I can access an external database using PHP in the block_myblock.php frankenstein file? Is there any sort of documentation or guide tackling this problem that I could adapt to my own problem?

I'm currently using the latest Moodle distribution available. My final objective is to have a plugin that works for any version higher than 3.1.*


Asking the community is my last resource, as I'm sure that someone else had to deal with this problem at some point. I'm simply not able to find anything about it on Google or forums. Sorry. sad


I really appreciate any help provided.


Sincerely,

Gustavo

Average of ratings: -
In reply to Gustavo Lopes

Re: Fetching data from an external MySQL server.

by Richard Jones -
Picture of Plugin developers Picture of Testers

Hi Gustavo

By no means an expert but I would think the external database enrolment should provide some clues (enrol_database plugin).  This plugin accesses a remote database (of almost any type) to get user data from.

Richard

In reply to Richard Jones

Re: Fetching data from an external MySQL server.

by Gustavo Lopes -

Hi Richard,

I don't think I can directly use the enrol_database plugin to access data from remote sources, given that it is very restrictive and specific on how it obtains and works the data.

I can, however, dwelve into its core files and see if I can implement parts of it in my own plugin.

Thank you very much for your support!

Gustavo

In reply to Gustavo Lopes

Re: Fetching data from an external MySQL server.

by Luis de Vasconcelos -
Picture of Particularly helpful Moodlers

What does "then work it into creating new courses and populating them" mean? Do you want to automatically create courses in Moodle and populate them based on info gathered from the external database? Or are you just gathering MI so that you can create the courses manually?

One way to access the external database from Moodle is via web services. Start here: https://docs.moodle.org/dev/Web_services and http://docs.moodle.org/en/Web_services_FAQ

In reply to Luis de Vasconcelos

Re: Fetching data from an external MySQL server.

by Gustavo Lopes -

Ideally, my project will do the following:

• A block is shown at the dashboard. This block accepts an entry which will be used as the shortname of an external course;

• As soon as the user clicks on the button and confirms the action, the page will refresh with the shortname passed as a GET parameter;

• The block will then notice the parameter and proceed to connect with the external database and query for courses with the same given shortname, the user's username(obtained via $USER->username) and a role value of 2, which means "teacher" in my local database;

• Holding the info of what courses the user teaches, as well as what students are enrolled in what courses(in the external library), the block will then call upon Moodle functions to automatically create courses and populate them with the correct students.

It's important to notice that all students are already signed in the Moodle, the only things really created are the courses and the enrollments.

I hope I was able to give you a clear view of what I want my plugin to do.


I researched a lot about how to access external databases via web services, but all I could find were methods using tokens for external applications to access Moodle, and not the other way around.

I'll keep on studying them to see if I can find a way to fetch data from an external source.

Thanks for your support!