Add a custom user field to the certificate

Add a custom user field to the certificate

by Joseph Thibault -
Number of replies: 30
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

I'm interested in printing a custom field automatically onto the certificate once it's created/generated.  The idea would be that students enter their own "license number" upon registering or to their profiles later, and then when they have earned the certificate that custom user field (the license number) is automatically included/printed on their certificate.  Any thoughts on how I can accomplish this?  

Average of ratings: -
In reply to Joseph Thibault

Re: Add a custom user field to the certificate

by Mat P -

I would love to know the answer to this as well

In reply to Mat P

Re: Add a custom user field to the certificate

by Mark Nelson -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Guys,

I suggest creating a new certificate type for this, just copy one of the existing folders in /mod/certificate/type and rename it to what you want. The following are the steps required -

1) Edit/Create the custom profile field you want to display and make note of the shortname you have given it.

2) In the certificate.php file you have created in the new type folder, add the following code before the 'cert_printtext' functions are called. Replace, shortname with the shortname of the field you have created.

3) Add cert_printtext($pdf, $x, $y+400, 'C', 'freeserif', '', 10, 'License Number: '.$value); to wherever you want this displayed in the certificate. If you want to, you can go to the file lang/en/certificate.php and create another entry for the string you want listed. So, for example, if you wanted 'License number:' you could add $string['licensenumber'] = 'License Number'; then call this in certificate.php with get_string('licensenumber', 'certificate'). So, the code would be - Add cert_printtext($pdf, $x, $y+400, 'C', 'freeserif', '', 10, get_string('licensenumber', 'certificate').$value). I plan on re-writing the certificate type section, so they can have their own language file, but atm you will have to manually add it to the lang/en/certificate.php file.

4) Keep testing this and changing the x and y values to wherever you want it positioned.

 

 

In reply to Mark Nelson

Re: Add a custom user field to the certificate

by Mark Nelson -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

I plan on adding the ability to add custom profile fields, and changing their position in the certificate settings, but for now the above will have to do.

Average of ratings: Useful (1)
In reply to Mark Nelson

Re: Add a custom user field to the certificate

by Mat P -

I added     cert_printtext($pdf, $x, $y+145, 'C', 'freesans', '', 11, get_string('QICPDACRRMno', 'certificate').$value);
 to the certificate.php page, it displays the text from the en folder but it does not grab the custom registration field.

In reply to Mat P

Re: Add a custom user field to the certificate

by Mark Nelson -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Mat, Sorry I should have been more clear. When I said add the following code, I meant what was included in the file I attached. I suspect you are missing that code.

In reply to Mark Nelson

Re: Add a custom user field to the certificate

by Mat P -

I added the code see below but i get a syntax error for the line with the INNER JOIN

// Add text
$sql = "SELECT ud.data " .
       "FROM {user_info_data} ud "
       "INNER JOIN {user_info_field} uf "
       "ON ud.fieldid = uf.id " .
       "WHERE ud.userid = $USER->id " .
       "AND uf.QICPDACRRMno = 'QICPDACRRMno'";

if ($value = $DB->get_record_sql($sql)) {
        $value = $value->data;
} else {
        $value = '';
}
    $pdf->SetTextColor(0,0,0);
    cert_printtext($pdf, $x, $y+100, 'C', 'freesans', '', 25, get_string('title', 'certificate'));
    $pdf->SetTextColor(0,0,0);
    cert_printtext($pdf, $x, $y+120, 'C', 'freesans', '', 17, get_string('certify', 'certificate'));
    cert_printtext($pdf, $x, $y+136, 'C', 'freesans', '', 25, $studentname);
    cert_printtext($pdf, $x, $y+145, 'C', 'freesans', '', 11, get_string('QICPDACRRMno', 'certificate').$value);
    cert_printtext($pdf, $x, $y+158, 'C', 'freesans', '', 20, get_string('statement', 'certificate'));
    cert_printtext($pdf, $x, $y+172, 'C', 'freesans', '', 20, $classname);
    cert_printtext($pdf, $x, $y+192, 'C', 'freesans', '', 14, $certificatedate);
    cert_printtext($pdf, $x, $y+102, 'C', 'freeserif', '', 10, $grade);
    cert_printtext($pdf, $x, $y+112, 'C', 'freesans', '', 10, $outcome);
    cert_printtext($pdf, $x, $y+122, 'C', 'freeserif', '', 10, $credithours);
    cert_printtext($pdf, $x, $codey, 'C', 'freeserif', '', 10, $code);
    $i = 0 ;
    if($certificate->printteacher){
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
    if ($teachers = get_users_by_capability($context, 'mod/certificate:printteacher', '', $sort='u.lastname ASC','','','','',false)) {
        foreach ($teachers as $teacher) {
            $i++;
    cert_printtext($pdf, $sigx, $sigy+($i *4) , 'L', 'freeserif', '', 12, fullname($teacher));
}}}

    cert_printtext($pdf, $custx, $custy, 'L', '', '', '', $certificate->customtext);
?>

Average of ratings: Useful (1)
In reply to Mat P

Re: Add a custom user field to the certificate

by Mark Nelson -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Mat,

It should look like:

$sql = "SELECT ud.data " .
       "FROM {user_info_data} ud " .
       "INNER JOIN {user_info_field} uf " .
       "ON ud.fieldid = uf.id " .
       "WHERE ud.userid = $USER->id " .
       "AND uf.shortname = 'QICPDACRRMno'";

Please copy and paste that.

Average of ratings: Useful (2)
In reply to Mark Nelson

Re: Add a custom user field to the certificate

by Mat P -

Thanks that worked a charm..

In reply to Mark Nelson

Re: Add a custom user field to the certificate

by Mark Leonas -

Hi Mark,

I recently upgraded moodle 1.9-2.0, and likewise with the certificate plugin.   had a custom field that was working properly before, but now is giving me the following error:

"get_record_sql() not available anymore"

Here is my code:

$sql = "SELECT mdl_user_info_data.data
               FROM mdl_user_info_data
               INNER JOIN mdl_user_info_field
               ON mdl_user_info_data.fieldid = mdl_user_info_field.id
               WHERE mdl_user_info_data.userid = " . $USER->id . "
               AND mdl_user_info_field.shortname = 'license'";

if ($value = get_record_sql($sql)) {
    $value = $value->data;
    } else {
    $value = '';
}

certificate_print_text($pdf, $x, $y + 45, 'C', 'Helvetica', '', 10, 'Nursing License Number: '.$value);

Any idea what is going on with the error?  I searched my file structure and many other files are using that function, so I'm wondering what the issue is.

 

In reply to Mark Leonas

Re: Add a custom user field to the certificate

by Jean-Michel Védrine -

Hello,

The answer is just a few lines above your post in a previous message smile

Since Moodle 2.0 it's $DB->get_record_sql

In reply to Jean-Michel Védrine

Re: Add a custom user field to the certificate

by Mark Leonas -

Aha!  Awesome- works like a charm.  Not sure how I missed that, but thanks!

In reply to Mark Leonas

Re: Add a custom user field to the certificate

by Nicolás Sanguinetti -

I've noticed that in the already defined $USER variable, the custom user fields are already loaded, so it's just a matter of using them:

$USER->profile['CUSTOM_FIELD_SHORT_NAME'] returns the value of the custom user field whith "short name" CUSTOM_FIELD_SHORT_NAME

Average of ratings: Useful (3)
In reply to Nicolás Sanguinetti

Re: Add a custom user field to the certificate

by Adolfo Franco -

You absolutely right! Thumbs up! With:

echo $USER->profile['shortname']

It's worked just fine.

Thanks!!

In reply to Adolfo Franco

Re: Add a custom user field to the certificate

by Gary Lyon -

I am not a programmer but here's something I came up with that I am testing for printing the institution name instead of a user's name (WARNING ...all you programmers please make sure this doesn't create problems before you implement...as I said, I am not a programmer...just desperate to find a solution...even a blind squirrel occasionally trips over an acorn!)

from the certificate.php file...

.

// Add text
$pdf->SetTextColor(0, 0, 120);
certificate_print_text($pdf, $x, $y, 'C', 'Helvetica', '', 30, get_string('title', 'certificate'));
$pdf->SetTextColor(0, 0, 0);
certificate_print_text($pdf, $x, $y + 55, 'C', 'Times', '', 20, get_string('certify', 'certificate'));
//certificate_print_text($pdf, $x, $y + 105, 'C', 'Helvetica', '', 30, fullname($USER)); First I commented out this line
certificate_print_text($pdf, $x, $y + 105, 'C', 'Helvetica', '', 30, $USER->institution); then I added this line, calling the 'institution' field from the user profile.
certificate_print_text($pdf, $x, $y + 155, 'C', 'Helvetica', '', 20, get_string('statement', 'certificate'));
certificate_print_text($pdf, $x, $y + 205, 'C', 'Helvetica', '', 20, $course->fullname);
certificate_print_text($pdf, $x, $y + 255, 'C', 'Helvetica', '', 14, certificate_get_date($certificate, $certrecord, $course));
certificate_print_text($pdf, $x, $y + 283, 'C', 'Times', '', 10, certificate_get_grade($certificate, $course));
certificate_print_text($pdf, $x, $y + 311, 'C', 'Times', '', 10, certificate_get_outcome($certificate, $course));
if ($certificate->printhours) {
    certificate_print_text($pdf, $x, $y + 339, 'C', 'Times', '', 10, get_string('credithours', 'certificate') . ': ' . $certificate->printhours);
}
certificate_print_text($pdf, $x, $codey, 'C', 'Times', '', 10, certificate_get_code($certificate, $certrecord));
$i = 0;

Cheers!

Average of ratings: Useful (1)
In reply to Gary Lyon

Re: Add a custom user field to the certificate

by Ruth Cheesley -

Hi Gary & others, I've tried this but I can't get it to echo out the field. I have checked that the field is populated in the user account, I've also print_r the array to ensure that I'm using the correct field name.

Is there something I'm missing?  Here's the syntax I'm using, nothing appears & no errors:

certificate_print_text($pdf, $x + 140, $y + 50, 'L', 'Helvetica', '', 20, $USER->licensenumber); 

The fieldname is licensenumber.

Ruth

In reply to Ruth Cheesley

Re: Add a custom user field to the certificate

by Ruth Cheesley -

As is always the way, three seconds after posting I realised my mistake, and something to add to Gary's post too.

User profile custom fields require the following syntax (note the bold part)

certificate_print_text($pdf, $x + 140, $y + 50, 'L', 'Helvetica', '', 20, $USER->profile['licensenumber']);

Hurrah!

Ruth

Average of ratings: Useful (1)
In reply to Gary Lyon

Re: Add a custom user field to the certificate

by Helen Maffin -

Can someone let me know how to modify a line of the certifcate to get it display bold please? I have basically worked out what al lthe parameters are but now how to do this! Thank you!

In reply to Helen Maffin

Re: Add a custom user field to the certificate

by Helen Maffin -

Never mind! I found it in another post.

In reply to Gary Lyon

Re: Add a custom user field to the certificate

by Daniel Dubbeldam -

when adding a custom rule to a certificate, I want to add a custom profile field to my certificate. 
it's the "date of birth" (geboortedatum) of the user.

I've added the following rule: 

certificate_print_text($pdf, $x + 55, $y + 53, 'L', 'freesans', '', 14, $USER->profile['geboortedatum']);


However, the date that is printed on my certificate is now in a unix timestamp instead of human readable format..

How do I now convert this unix timestamp to the normal format in my certificate?????

thnx!

In reply to Daniel Dubbeldam

Re: Add a custom user field to the certificate

by Daniel Dubbeldam -

is there someone that knows this answer? it's kinda strange to give out a certificate with "born on 446338800" on it ;) surely there must be someone that knows how to convert this on the  certificate?

In reply to Daniel Dubbeldam

Re: Add a custom user field to the certificate

by Jean-Michel Védrine -

Hello Daniel,

Something like

certificate_print_text($pdf, $x + 55, $y + 53, 'L', 'freesans', '', 14, userdate($USER->profile['geboortedatum'], '%B %d, %Y'));

should do the trick.

If the date is not formatted as you want, you must modify the '%B %d, %Y' for instance '%d %B %Y' may suit your needs better.

To learn all the formats available for dates, you can read https://php.net/manual/en/function.strftime.php

In reply to Jean-Michel Védrine

Re: Add a custom user field to the certificate

by Wendi Daniels -

fow whatever reason, I am not able to call a piece of data from the database in quiz_attempts. How the hell do I do this? I have been able to program 2 columns in this table, and they are called "certificate" and "certid". If they pass the test, then "certificate" will equal "1" (otherwise, it is "0").

 

ONLY IF it "certificate" equals "1", then I need the certificate to recall the "certid" that is associated with that individual attempt.

    

I added this to the beginning of the certificate.php file:

*************************************************************************************

  

require_once($CFG->dirroot.'/user/profile/lib.php');

profile_load_data($USER); 

  

***************************************************************************************

 

and I have tried these lines....

******************************************************************************************

 

certificate_print_text($pdf, $x, $y + 210, 'C', $fontserif, '', 30, 'Certificate # ' . $USER->profile['certid']);

certificate_print_text($pdf, $x, $y + 210, 'C', $fontserif, '', 30, 'Certificate # ' . $USER->'certid');

certificate_print_text($pdf, $x, $y + 210, 'C', $fontserif, '', 30, 'Certificate # ' . $attempts->['certid']);

certificate_print_text($pdf, $x, $y + 210, 'C', $fontserif, '', 30, 'Certificate # ' . $attempts->certid);

certificate_print_text($pdf, $x, $y + 210, 'C', $fontserif, '', 30, 'Certificate # ' . $USER->attempts['certid']);


...and several others. I'm at the end of my rope. Can anyone guide me? I am only following examples and going by trial and error. I don't know how to do this sort of thing.


Please help...

In reply to Wendi Daniels

Re: Add a custom user field to the certificate

by Wendi Daniels -

Jean-Michel, do you have an idea? You usually have wonderful ideas and I would love your insights.

In reply to Wendi Daniels

Re: Add a custom user field to the certificate

by Wendi Daniels -
I see my error...I have added a column to the table quiz_attempts, and I want to add the value of that column to the certificate. This value is an id number that I automatically receive from another entity (the student passes the test, the API automatically sends the information to the state and receives a confirmation number, that number is stored in a column in quiz_attempts).
 
  
So, what I need to know is how to I either shift that value to a user profile field in user_info_fields, or how do I recall that data from a column in quiz_attempts?
 
 
A programmer who made the API a while ago created lines similar to this line in the api, and I added this and created the column in quiz_attempts:

$DB->set_field('quiz_attempts', 'certid', (result from the entity), array('id' => $attemptid));
 
 
Seeing my error, I added a user profile field, made it invisible, and added this line in the api, but it is not working:
 
$DB->set_field('user_info_field', 'certidnew', (result from the entity), array('id' => $attemptid));
 
 
I am thinking that "field" refers to the column, but what would refer to a student's value in one particular column, and how would I properly program that?






In reply to Nicolás Sanguinetti

Re: Add a custom user field to the certificate

by Léhane van der Merwe -

This post helped me solve my suspicion too, I knew it was possible to draw with  $USER, Thanks Nicolás!Yes

I just did not have the syntax correct. approve

In reply to Joseph Thibault

Re: Add a custom user field to the certificate

by Jason E -

I've been trying to piece all this together from these posts going back a couple years. Since I'm not too good with scratching out code it's kind of hard for me to follow in pieces. What I understand is that I need some kind of sql query and then the print commands. The print commands look pretty straightforward, but I'm a little lost on the sql part since there appear to be several versions here and the code in posts looks incomplete at times. 

Would someone be a kind soul and lay out the code for the whole procedure as it would work to add a custom user field to certificate using the latest version in a single post so beginners like myself can add a field? I actually have 3 fields that may or may not be null which I want to add. I hope that others could benefit from this as well. 

 

Thanks

 

 

In reply to Jason E

Re: Add a custom user field to the certificate

by Jason E -

Again, I'm not a programmer. I have a lot of trouble with syntax. But I have been poking around at this and trying to figure it out. 

I'm wondering why there needs to be an inner join. 

the table `mdl_user_info_data` looks like this.

id userid fieldid data dataformat
1 3 1 user3licenseA 0
2 7 2 user7licenseB 0
3 4 1 user4licenseA 0

I know the userid, and I just want what's in the column 'data' where fieldid matches the one I want. 

Is there a simpler way to call out the variables I want? Say I want something like 

$licenseAforcertificate = data where userid = $userid and fieldid = 1

?

Thanks

In reply to Jason E

Re: Add a custom user field to the certificate

by Jason E -

Using Moodle 2.5.1+ (Build: 20130712)

This is what works for me. I'm not sure why. I'm still trying to figure out how to have the 3 license numbers show up on one line.

//call out variables

$sql = "SELECT mdl_user_info_data.data
FROM mdl_user_info_data
INNER JOIN mdl_user_info_field
ON mdl_user_info_data.fieldid = mdl_user_info_field.id
WHERE mdl_user_info_data.userid = " . $USER->id . "
AND mdl_user_info_field.shortname = 'license1'";

if ($value = $DB->get_record_sql($sql)) {
$value = $value->data;
} else {
$value = '';
}

//And then in the body to print them

certificate_print_text($pdf, $x, $y + 100, 'C', 'freeserif', '', 15, 'License Info: '.$value);
certificate_print_text($pdf, $x, $y + 120, 'C', 'freeserif', '', 15, $USER->profile['license2']);
certificate_print_text($pdf, $x, $y + 130, 'C', 'freeserif', '', 15, $USER->profile['license3']);