General developer forum

 
 
Picture of Ge Heim
How to access Course completion status?
 

(in advance, please excuse my poor english)

hello,

I'm new to using/customizing moodle and realy need some help.

I am using Moodle 2.0.2 and began to customize it to fit my needs, therefore I designed a whole new "homescreen" every student sees when he logs in. The admin sees the "normal" layout and whole admin-section.

The homescreen is basically a table with an overview of all the courses/tasks the students have to do, with an "X" for unfinished tasks and a check mark for finished tasks. At the moment, the icon just changes, once the link is visited (simple CSS).

Moodle 2.0 has the Course completion status, which for example automatically checks a quiz if a certain percentage is reached.

I want to "connect" the icons on my homescreen to this information,  so when a student reached for example 80% in a quiz and the course completion status would check mark this activity, i want to change my icon from "X" to "check mark".

 

So how/where do i access this information and whats the best way to integrate it to my site? Javascript?

 

Thank you very much in advance, every bit of advice would help me.

 
Average of ratings: -
Picture of Aaron Barnes
Re: How to access Course completion status?
Group Developers

Hi,

I'm not sure about the best way to integrate this into your specific site, PHP sounds like the best option to me.

Here is the code for finding the status of a logged in user in a course with completion enabled:

<?php
require_once("{$CFG->libdir}/completionlib.php");

$cinfo = new completion_info($course_object);
$iscomplete = $cinfo->is_course_complete($USER->id);
?>

Hope that helps,
Aaron

 
Average of ratings:Useful (1)
Picture of Ge Heim
Re: How to access Course completion status?
 

Hi,

 

thank you very very much! This is exactly what I was looking for.

There is only one little problem left, how do I give the function my course_id? I've been browsing trough completionlib.php, but i don't get it.

How do I create a new course object with an specific id?

 

Thank you very much.

 
Average of ratings: -
Davo
Re: How to access Course completion status?
Group DevelopersGroup Particularly helpful Moodlers

$course_object = $DB->get_record('course', ('id'=>$course_id));

 
Average of ratings:Useful (1)
Picture of Ge Heim
Re: How to access Course completion status?
 

thank you very much, but I don't get it to work properly.

to be as precise as possible:

in the original moodle index.php in the part of

case FRONTPAGECOURSELIST: 

I added

include ("ownhomescreen/ownhomescreen.php"); 

so my site is loaded.

in ownhomescreen.php I have

 

<?php
require_once("{$CFG->libdir}/completionlib.php");

$cinfo = new completion_info($course_object);
$iscomplete = $cinfo->is_course_complete($USER->id);
if ($iscomplete) {
  echo '<img src="/ownhomescreen/green.gif">';
} else {
  echo '<img src="/ownhomescreen/red.gif">';
}
?>

 

I just want to add a specific course id!

 

<?php
require_once("{$CFG->libdir}/completionlib.php");

$course_object = 74; //something like this line
$cinfo = new completion_info($course_object);
$iscomplete = $cinfo->is_course_complete($USER->id);
if ($iscomplete) {
  echo '<img src="/ownhomescreen/green.gif">';
} else {
  echo '<img src="/ownhomescreen/red.gif">';
}
?>

 

what do i do with your code? something like this? :

 

<?php
require_once("{$CFG->libdir}/completionlib.php");
require_once("{$CFG->libdir}/accesslib.php");


$course_object = $DB->get_record('course', ('id'=>$course_id));
$cinfo = new completion_info($course_object);
$iscomplete = $cinfo->is_course_complete($USER->id);
if ($iscomplete) {
  echo '<img src="/ownhomescreen/green.gif">';
} else {
  echo '<img src="/ownhomescreen/red.gif">';
}
?>

 

thanks in advance for any help

 
Average of ratings: -
Davo
Re: How to access Course completion status?
Group DevelopersGroup Particularly helpful Moodlers

Yes, that is pretty much it, except that if you need to set the $course_id on the line above, e.g.

 $course_id = 74;

 
$course_object = $DB->get_record('course', array('id'=>$course_id));

(And you need to include the important word 'array' that I missed out of my first reply!)

 
Average of ratings:Useful (1)
Picture of Ge Heim
Re: How to access Course completion status?
 

Thank you so much for your help, but there is still a mistake I can't fint.

My code is this:

 

<?php
error_reporting(E_ALL);

require_once("{$CFG->libdir}/completionlib.php");
require_once("{$CFG->libdir}/accesslib.php");

$course_id = 74;
$course_object = $DB->get_record('course', array('id'=>$course_id));

$cinfo = new completion_info($course_object);
$iscomplete = $cinfo->is_course_complete($USER->id);
if ($iscomplete) {
  echo '<img src="/ownhomescreen/green.gif">';
} else {
  echo '<img src="/ownhomescreen/red.gif">';
}
var_dump($cinfo)
?>


So I see:
Notice: Trying to get property of non-object in C:\xampp\htdocs\lib\completionlib.php on line 157

object(completion_info)#667 (3) { ["course":"completion_info":private]=> bool(false) ["course_id"]=> NULL ["criteria":"completion_info":private]=> NULL}

 

The course_id in the object is still NULL, so building this object does not work properly, but I have no clue how to fix this. I am really getting desperate and I hope you can help me.

 
Average of ratings: -
Picture of Ge Heim
Re: How to access Course completion status?
 

Excuse me, I don't want to be impatient, but I really really need to finish this project.

 

Does anyone have any clue how to create a working "course_object" with a specific ID ?

 

Thank you guys so much for your work.

 
Average of ratings: -
Davo
Re: How to access Course completion status?
Group DevelopersGroup Particularly helpful Moodlers

As already stated:

$courseid = //Insert the correct course ID here

$course_object = $DB->get_record('course', array('id'=>$courseid));

If that does not work, then you have supplied a nonexistent courseid.

Try visiting the course and looking at the URL, it should look something like this:

http://[moodlesite]/course/view.php?id=XX

Where XX is the courseid. Put that number in as the courseid and the DB line will return a valid course object.

 
Average of ratings:Useful (2)
Picture of Ge Heim
Re: How to access Course completion status?
 

Thank you very very much for your help!

I am using the EXACT same code and I am definitly using an existing course ID (but to be sure I've tried several ID's..) but still no course object.

 

object(completion_info)#677 (3) { ["course":"completion_info":private]=> bool(false) ["course_id"]=> NULL ["criteria":"completion_info":private]=> NULL } 

 


I've looked at the functions your code is using and I would do it the same way, but somewhere must be a problem. I am so sorry to bother you, but I have spent hours of frustration with this thing and I am not able to find a solution

 
Average of ratings: -
Davo
Re: How to access Course completion status?
Group DevelopersGroup Particularly helpful Moodlers

Try adding this, after the $DB->get_record('course' ... line:

print_r($course_object);

This should output the contents of the course record you have pulled out of the database.

 
Average of ratings:Useful (1)
Picture of Ge Heim
Re: How to access Course completion status?
 

print_r does not print out anything. the var_dump gives me the output as statet in my last post.

there is no correct course object, even tough the id is correct.

 
Average of ratings: -
Davo
Re: How to access Course completion status?
Group DevelopersGroup Particularly helpful Moodlers

If you take a look in course/view.php (around line 34), you will see this is the one and only method for getting a course object from a course id.

An alternative test would be to log directly into the database and run this query:

SELECT * from mdl_course WHERE id = 74;

(Which is pretty what the $DB->get_record does behind the scenes)

If the direct database query works, but $DB->get_record doesn't then there is something very odd going on.

 
Average of ratings:Useful (1)
Picture of Ge Heim
Re: How to access Course completion status?
 

Without you, I would be lost! Database error fixed, it finally works!

$course_object = $DB->get_record('course', array('id' => $courseid));
$cinfo = new completion_info($course_object);
$iscomplete = $cinfo->is_course_complete($USER->id);

 

gives me now the info if a course is complete. Now I have to worry how to get the information if a specific activity (in this case each quiz a student has to do) is done.

So I am searching for a function like "is_course_complete()" but for activitys, which I can call with userid and activityid..

 

But I finally made some great progress. Thank you, thank you, thank you!

 

 

PS: I am trying something like this

 

$activity = $DB->get_record('course_modules', array('id' => 108));
$ccinfo = new completion_info($activity);
$iscompletee = $ccinfo->is_course_complete($USER->id);

 

which gives me no error but does not work properly yet.

 
Average of ratings: -
Davo
Re: How to access Course completion status?
Group DevelopersGroup Particularly helpful Moodlers

$course_object = $DB->get_record('course', array('id' => $courseid));

$activity = $DB->get_record('course_modules', array('id' => 108));

$cinfo = new completion_info($course_object);

$cdata = $cinfo->get_data($activity, false, $USER->id);

if ($cdata->completionstate == COMPLETION_COMPLETE || $cdata->completionstate == COMPLETION_COMPLETE_PASS) {

...

 

I've not looked at the code for a while, there may be better ways of doing this, but that should work.

 
Average of ratings:Useful (1)
Picture of Ge Heim
Re: How to access Course completion status?
 

AMAZING!

You're a genius! Thank you soo much for your help!! Have a nice day.

 
Average of ratings: -
Picture of kanchan gupta
Re: How to access Course completion status?
 

i want to display student's progress report categorywise as below format:

 

    category        
student(enrolled) topic1 quiz1(grade) topic2

quiz2

(grade)

topics

completion resuls

category completion (%)
student1 yes 6/10 no 7/10 1/2 50%
student2 no not attempted no - 0/2 0%
             
 
Average of ratings: -
Picture of kanchan gupta
Re: How to access Course completion status?
 

Please reply for above forum as soon as possible!!!!!!!!!

 
Average of ratings: -
Picture of Tracey Roebuck
Re: How to access Course completion status?
 

Hi,

I have been trying to achieve a similar modification to the Moodle front page using your code but have been unable to make it work. I am probably doing lots of things incorrect as I am quite new to PHP and the Moodle functions and would appreciate any guidance you could provide.

Could you tell me exactly where you insert the statement include ("ownhomescreen/ownhomescreen.php");

I have tried this in multiple places under case FRONTPAGECOURSELIST but cannot get the image to show at the side or underneath each course in the list. The only way I have achieved this is by inserting the statement into print_course in the bit that builds the course URL in a <h3> tag in course/lib.php.

My Courses

This does not work however as the image is alway the red image even though one of my courses is complete.

At the moment I have commented out the statement in index.php, added it into course/lib.php as:

(LINE 2436)

echo html_writer::link($linkhref, $linktext, $linkparams);
include ("ownhomescreen/ownhomescreen.php");
echo html_writer::end_tag('h3');

and ownhomescreen.php itself is as follows:

<?php
require_once("{$CFG->libdir}/completionlib.php");
require_once("{$CFG->libdir}/accesslib.php");

$courseid = 2;
$course_object = $DB->get_record('course', array('id' => $courseid));
$cinfo = new completion_info($course_object);
$iscomplete = $cinfo->is_course_complete($USER->id);
 if ($is_complete) { 
 echo '<img src="/ownhomescreen/green.png">';
 } else { 
 echo '<img src="/ownhomescreen/red.png">';
 }
?>

Other info:
I am using a local install of Moodle 2.2 containing 2 courses with 1 test learner. 1 course is complete and 1 incomplete for the learner. I have made sure that I ran cron.php and checked that the course is showing as completed and it is.

Any tips are much appreciated.

Thanks,

Tracey

 
Average of ratings: -