General developer forum

Tracking user Progress

 
Picture of Gareth Jones
Tracking user Progress
 

Hi there,

I am creating a new dashboard for our Moodle site, I want to be able to display the percentage of each course progress on the dashboard. I have had a browse through some of the documentation but a lot of it is outdated. Studying the Moodle Database, I have found several tables which might allow for me to calculate a users progress however, I am struggling to understand where the tables link. I was wondering if there might be an easy way - possibly a function - which allows for us to get the users progress on a particular course which can then be displayed on the user dashboard. I figured out a way to list a users enrolled courses using the following query:


$enrolled = $DB->get_records_sql("SELECT `mdl_user_enrolments`.`id` ,`mdl_course`.`fullname` FROM `moodle`.`mdl_user_enrolments` INNER JOIN `moodle`.`mdl_enrol` ON (`mdl_user_enrolments`.`enrolid` = `mdl_enrol`.`id`) INNER JOIN `moodle`.`mdl_course` ON (`mdl_enrol`.`courseid` = `mdl_course`.`id`) INNER JOIN `moodle`.`mdl_user` ON (`mdl_user_enrolments`.`userid` = `mdl_user`.`id`) WHERE `mdl_user`.`id` = " . $USER->id . " "); print_r($enrolled);

However, I want to be able to get the progress of each of their enrolled courses.

 
Average of ratings: -
Picture of Yogesh Nahar
Re: Tracking user Progress
 

Hi Gareth, the latest version of Moodle would cover this feature for you. You don't need to do any custom coding. The latest version has some great features. 


Check out the features in the latest version at https://moodle.org/downloads

 
Average of ratings: -
Picture of Mario Zappavigna
Re: Tracking user Progress
 

Could you be more specific? How to enable this feature? Thanx

 
Average of ratings: -
Picture of Gareth Jones
Re: Tracking user Progress
 

Hi Yogesh,

I have the latest version of Moodle, however, I am completely re-coding the user dashboard which means I need a method of displaying the course progression through the means of code. 

Thank you!

 
Average of ratings: -
Picture of Richard Oelmann
Re: Tracking user Progress
Core developersParticularly helpful MoodlersPlugin developersTesters

Even if you are totally recoding your own dashboard, take a look at how the new course overview does it. That calculates and displays completino progress, so you can use that methodology, even if you display it differently. I took taht and reused it on the student tab of the course pages in the handlebar theme (still awaiting plugin approval but available from github)

 
Average of ratings: -
Picture of Mihir J
Re: Tracking user Progress
 

here is a snippet for you

$completion = new \completion_info($course);

// if user having edit access then ignore it

$coursecontext = context_course::instance($course->id);

if (has_capability('moodle/course:update', $coursecontext)) {

continue;

}

// First, let's make sure completion is enabled.

if (!$completion->is_enabled()) {

continue;

}


$percentage = progress::get_course_progress_percentage($course);

if (!is_null($percentage)) {

$percentage = floor($percentage);

}


The variable $percentage is your progress percentage

thanks!

 
Average of ratings: -
Picture of Gareth Jones
Re: Tracking user Progress
 

I get an error that progress is not define?

 
Average of ratings: -
Picture of Mihir J
Re: Tracking user Progress
 

on top of the page you should mention

use core_completion\progress;


thanks!

 
Average of ratings: -
Picture of Gareth Jones
Re: Tracking user Progress
 

I'm now getting this error?

Can not find data record in database table course.
Debug info: SELECT id,category FROM {course} WHERE id IS NULL
[array (
)]
Error code: invalidrecord
Stack trace:
  • line 1533 of /lib/dml/moodle_database.php: dml_missing_record_exception thrown
  • line 1509 of /lib/dml/moodle_database.php: call to moodle_database->get_record_select()
  • line 6644 of /lib/accesslib.php: call to moodle_database->get_record()
  • line 103 of /theme/aaatheme/layout/mydashboard.php: call to context_course::instance()
  • line 1162 of /lib/outputrenderers.php: call to include()
  • line 1092 of /lib/outputrenderers.php: call to core_renderer->render_page_layout()
  • line 164 of /my/index.php: call to core_renderer->header()

 
Average of ratings: -
Picture of Mihir J
Re: Tracking user Progress
 

Sorry, i was thinking you already have a line for global $COURSE. No problem here is a complete snippet again


use core_completion\progress;

global $COURSE;

$course = $COURSE;

$completion = new \completion_info($course);


// if user having edit access then ignore it

$coursecontext = context_course::instance($course->id);

if (has_capability('moodle/course:update', $coursecontext)) {


continue;


}


// First, let's make sure completion is enabled.

if (!$completion->is_enabled()) {

continue;

}


$percentage = progress::get_course_progress_percentage($course);


if (!is_null($percentage)) {

$percentage = floor($percentage);

}

 
Average of ratings: Useful (1)