Certificate (plugin)

Using a custom certificate code on certificates

 
 
Picture of Brian Williams
Using a custom certificate code on certificates
 
Hello,
I was wondering about doing some custom modifications to the certificate module. A client would like to use their own numbering or "codes" that are printed on the certificates. I was wondering where and how the certificate codes get generated and added to the database. I would like to make the changes so that when you view the reporting the new codes are listed in the reports. Making the change where the data is inserted into the database seems like the best approach to me. I would also like to make these changes, if possible, on a course by course basis. I have more than one course in which the client would like their code to be unique to how they handle their certifications. Any help would be greatly appreciated. Thanks in advance.

Brian
 
Average of ratings: -
Picture of Michael F
Re: Using a custom certificate code on certificates
 

Yup... I also need to do the same !!!?

How to generate a custom certificate code ???

 

Michael F

 
Average of ratings: -
Picture of Jean-Michel Védrine
Re: Using a custom certificate code on certificates
Group DevelopersGroup Particularly helpful Moodlers

Hello Michael,

Please when you ask for help, always give your Moodle version and also the certificate activity module version.

Also answering a message that is 5 years old is not good practice because things changed a lot in 5 years in the certificate activity module , but as there was no answer to the first message ...

Here is the information for recent Moodle versions (no guaranty it is valid for older version, I have not checked)

The code is generated by the certificate_generate_code function lines 1474-1488 of mod/certificate/lib.php file

Currently it just generate a 10 characters random string

You can modify this function as you like but you must ensure there is no duplicated code, please also note that the corresponding field in the database is limited to 40 chars.

Complexity of this task will greatly depend on what you want to generate wink

 
Average of ratings: -
Picture of Michael F
Re: Using a custom certificate code on certificates
 

Hi Jean,

Sorry I have just forget.. I'm Using moodle 2.6 with the latest certificate module version...

I can reach to the function... But I didn't imagine it will be at that complexity !!

I would like it to be

"Course-shortname"+"completion-date"+"rand(3~4)"

I can see that course/lib.php is included but I can't call the course shortname or completiondate!

How to call them? or where to find the available variables ??

 
Average of ratings: -
Picture of Michael F
Re: Using a custom certificate code on certificates
 
hey...
Any help??
I do try to set $code = $course->shortname; .....but with no luck !!!
also $code = $courseshortname; !!
is this variable declared there ??? please advise!
Is what i want to do possible !?
Thanks a lot
 
Average of ratings: -
Picture of Jean-Michel Védrine
Re: Using a custom certificate code on certificates
Group DevelopersGroup Particularly helpful Moodlers

Hello Michael,

As I said "Complexity of this task will greatly depend on what you want to generate"

$course->shortname was the right variable but the problem is that if you look at the certificate_generate_code function you will see it doesn't take any parameter so $course is not passed to this function so when you use $course->shortname, $course is undefined and it doesn't work ! This is why your attempt was unsuccessful

So to make this working we have to change the definition of the certificate_generate_code function so that it takes $course as a parameter and there are 2 places to change:

  • The place where this function is defined
  • The place (or places) where this function is called
If you do a search for "certificate_generate_code" in all the files in mod/certificate you will get only 2 matches
and they are both in the same file mod/certificate/lib.php:
Line 653:     $certissue->code = certificate_generate_code();
Line 1474: function certificate_generate_code() {
So we change these 2 places:
Line 653:     $certissue->code = certificate_generate_code($course);
Line 1474: function certificate_generate_code($course) {
After doing that change, your idea to use:
$code = $course->shortname;
should now work as expected
 
Average of ratings: -
Picture of Jean-Michel Védrine
Re: Using a custom certificate code on certificates
Group DevelopersGroup Particularly helpful Moodlers

including the completion date in the code is more complicated, and in fact I am confused because I don't know if you are speaking of course completion or activity completion. 

Additionally I must confess that, as I don't use completion at all on my production Moodle website, I don't know this part of the Moodle code very well but there are examples of how to get course completion date in the certificate code, see for instance lines 1131-1138 of mod/certificate/lib.php

 
Average of ratings: -