Access moodle gradebook into another sytem

Access moodle gradebook into another sytem

by Gautam Bishwas -
Number of replies: 10

Hi All,

I am using Moodle2.1 up and running all nicely. my question is can we use  Moodle web services to "push" grade information from gradebook to an external system.

Can the Moodle web services do this?If yes please help me on this.

Thanks

Gautam

In reply to Gautam Bishwas

Re: Access moodle gradebook into another sytem

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I think it works better if the other system can PULL grades using the webservices.

At the OU we made a simple local plugin to manage pushing grades to another system. It just automated calling some code in grade/export/xml every night on cron, and then copying the XML file to the other system.

In reply to Tim Hunt

Re: Access moodle gradebook into another sytem

by Teresa Gibbison -

Hi Tim

We're looking into this at UOW.  How do you handle metacourse papers in your other system at OU?

Let's say we have a metacourse (eg ABC100/200) where the teaching occurs that uses metalinks to associate students from ABC100 as well as ABC200.   When ABC100/200 'Assignment 1' activity has grades associated we would like the ABC100 students to have this associated in our other system against ABC100 (rather than the metacourse ABC100/200).  Our other system must record the official course the student is enrolled in not the metacourse.  Do you have any advice?

I hope this makes sense!

Cheers
Teresa smile 

In reply to Teresa Gibbison

Re: Access moodle gradebook into another sytem

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

We don't use meta courses, at least not like that.

But, acutally, we don't rely on coures names at all. Instead, you need to notice the fact that all activities, and all grade items have an 'idnumber' field. This is a place where you can put in any identifier you like, and we use these idnumbers for linking activities in Moodle with activities in our student information system. That is what they are there for.

In reply to Tim Hunt

Re: Access moodle gradebook into another sytem

by Teresa Gibbison -

Thanks Tim, I'll remind the team about the idnumber.

We need to ensure each individual course records the relevant activities even if they are located in a metacourse.  We will not have all grade activities in our student information system - as it's an awful system to work with and to modify and grade items cannot be created on the fly during an 'import'.

Cheers
Teresa 

In reply to Tim Hunt

Re: Access moodle gradebook into another sytem

by Chris Richter -

Hi Tim,

I am interested in the best way to get gradebook results into other systems.

Is the local plugin you mentioned for XML Grades available?

In reply to Chris Richter

Re: Access moodle gradebook into another sytem

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

It looks like we have never shared that plugin. It is rather OU-specific, so we would not want to properly release it, becuase then people might expect us to support it. However, I will see if we can do a dump of the code, so that you can pick it apart, and use the bits you like in your own plugin.

In reply to Tim Hunt

Re: Access moodle gradebook into another sytem

by Gilles-Philippe Leblanc -
Hi Tim,

I would also need to develop a plugin for recovering student grades from an external database and insert them into Moodle.

Question to avoid unnecessary work, do you think you could share it with me so I adapts it to our needs or at least inspire me?

Thank you very much.
In reply to Gilles-Philippe Leblanc

Re: Access moodle gradebook into another sytem

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The error handling makes it a bit ugly, but the basic idea is:

  1. Get the grade data in a simple XML format, by fetching it from a URL that the other system provides.
  2. Pass that XML to the standard import_xml_grades function provided by the gradebook.
  3. If that seems to have worked OK, call grade_import_commit to actually save the data to the database.

Here is the full version of the key function, copied and pasted from our code. (This runs on cron, so error handling involves sending an email to a certain email address to notify of the error.)

    /**
     * Import the remote data into the gradebook
     * @param string $cronuser the user to email status information to.
     * @return boolean true if successful, false on failure.
     */
    public function import_into_gradebook($cronuser) {
        global $DB;

        // Large files are likely to take their time and memory. Let PHP know
        // that we'll take longer, and that the process should be recycled soon
        // to free up memory.
        set_time_limit(0);
        raise_memory_limit(MEMORY_EXTRA);

        //read the data from the url into a string
        $errormess = '';
        $commitmess = '';

        if (!$text = file_get_contents($this->urlused)) {
            mtrace('sending email to ' . $cronuser->email . ' ...');
            email_to_user($cronuser, $cronuser, get_string('importerroremailsubj', 'externalquiz'),
                    externalquiz_autoimport::format_mail_error_message('importerrorreadfile',
                                                                $this->urlused));
            mtrace(externalquiz_autoimport::format_message('importerrorreadfile', $this->urlused));

            return false;
        }

        if (!$course = $DB->get_record('course', array('id'=>$this->course))) {
            mtrace(externalquiz_autoimport::format_message('nocourseid', $this->course));
        }

        //check its valid XML
        if ($text === false) {
            $errormess = externalquiz_autoimport::format_message(
                    'importerrorreadfile', $this->urlused);
        } else {
            $olderrorsetting = libxml_use_internal_errors(true);
            $a = simplexml_load_string($text);
            libxml_use_internal_errors($olderrorsetting);
            if ($a === false || $a->getName() != 'results') {
                $errormess = externalquiz_autoimport::format_message(
                        'importerrornotvalidxml', $this->urlused);
            } else if ($a->count() == 0) {
                // The file was OK, but empty. In this case, we cannot pass it
                // to Moodle because Moodle treats this as an error.
                $commitmess = 'The file was empty, so nothing to import.';
            } else {
                //It was a valid XML string so lets import
                $error = '';

                if (!$course = $DB->get_record('course', array('id'=> $this->course))) {
                    $errormess = externalquiz_autoimport::format_message(
                            'nocourseid', $this->course);
                } else {
                    $importcode = import_xml_grades($text, $course, $error);
                    if ($importcode !== false) {
                        // comit the code if we are up this far, stick the output
                        // in a string so we can email it.
                        ob_start();
                        if (grade_import_commit($this->course, $importcode, '', true)) {
                            // Bug 8970 we don't want output in the cron log on success.
                        } else {
                            $errormess = externalquiz_autoimport::format_message(
                                    'importcommitedfailed', $this->urlused .
                                    get_string('witherror', 'externalquiz', $error));
                        }
                        $commitmess = ob_get_contents();
                        ob_end_clean();
                    } else {
                        $errormess = externalquiz_autoimport::format_message(
                                'importtoGBfailed', $this->urlused .
                                get_string('witherror', 'externalquiz', $error));
                    }
                }
            }
        }

        if ($errormess) {
            mtrace('sending error email to ' . $cronuser->email . ' ...');
            mtrace($errormess);
            email_to_user($cronuser, $cronuser, get_string('importerroremailsubj', 'externalquiz'),
                    externalquiz_autoimport::format_mail_error_message('importgradeerror',
                                                                $errormess));
        }

        return $commitmess && !$errormess;
    }