Sandeep
Sandeep

Reputation: 722

Moodle webservice function to get course completion status

I am trying to create a mobile application for Moodle. This involves creating and enabling webservice in moodle. I have succeeded in creating the webservice and able to get the courses and course details using the functions core_course_get_courses and core_course_get_contents.

Now I need to get the completion status of a course from moodle. Is there any function which can be used to do this. I have gone through the webservices and did not find any function that provides this data. Is there any external plugin to accomplish this? or any other help?

Upvotes: 0

Views: 4466

Answers (1)

franzlorenzon
franzlorenzon

Reputation: 5943

I had a similar problem some time ago. I think that you have two ways to solve this problem:

  1. Develop a local webservice that, inside Moodle, returns the completion course's status. The function to do that would be something along these lines:

    <?php
    require_once("{$CFG->libdir}/completionlib.php");
    $cinfo = new completion_info($course_object);
    $iscomplete = $cinfo->is_course_complete($USER->id);
    ?>
    
  2. Create a webservice that, outside Moodle, gets the course's completion page, parse it, and returns the values back. This is the path I personally choosed (it is a bit dangerous though, because it is easily broken by UI updates of Moodle).

    I did it with Python. This is the code to get initially the web page:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import urllib, urllib2, cookielib
    
    username = 'USER'
    password = 'PASSWORD'
    wwwroot = 'wwwroot of your moodle'
    remember = 0;
    
    def get_nv_page(url):
        cj = cookielib.CookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        login_data = urllib.urlencode({'username' : username, 'password' : password, 'rememberusername': remember})
        opener.open(wwwroot + '/login/index.php', login_data)
        resp = opener.open(url)
        return resp.read()
    
    def get_completion_course(course_id):
        url = wwwroot + '/course/report/completion/index.php?course=' + str(course_id)
        return get_nv_page(url)
    

    And to parse it...

    import requests, re
    from bs4 import BeautifulSoup
    
    def grab_completion_table(self, raw_doc):
        """ Convert an html course's completion page from moodle into a python list """
        list_video_activities = []
        completion_table = {}
        doc = BeautifulSoup(raw_doc)
        html_table = doc.find('table', id="completion-progress")
        activities = html_table.find_all('th', attrs={ 'class': 'criteriaicon'})
    
        for activity in activities:
            url = activity.find('a', href=re.compile('.*mod\/url\/view.*'))
            if url is not None:
                list_video_activities.append(url['title'])
            else:
                list_video_activities.append(False)
    
        user_rows = html_table.find_all('tr', id=re.compile('user-[0-9]*'))
    
        for user_row in user_rows:
            user_id = user_row['id'].split('-')[1]
            activities_completion_info = user_row.find_all('td', attrs={ 'class': 'completion-progresscell'})
            completion_user_table = []
    
            for i, video_activity in enumerate(list_video_activities):
                if video_activity != False:
                    activity_info = activities_completion_info[i].find('img')['title'].split(',')[1].strip()
                    activity_completion_info = activity_info.split(':')[1].strip()
                    # I put this: [ video_title, completion_status ] 
                    completion_user_table.append((video_activity, activity_completion_info))
    
            completion_table[user_id] = completion_user_table
    
        return completion_table
    

    With the data in Python, it's easy to add a webservice with it. I don't know if you can use python in your project, but I think this approach is replicable in other languages.

Upvotes: 2

Related Questions