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?
I would love to know the answer to this as well
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.
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.
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.
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.
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);
?>
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.
Thanks that worked a charm..
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.
Hello,
The answer is just a few lines above your post in a previous message
Since Moodle 2.0 it's $DB->get_record_sql
Aha! Awesome- works like a charm. Not sure how I missed that, but thanks!
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
You absolutely right! Thumbs up! With:
echo $USER->profile['shortname']
It's worked just fine.
Thanks!!
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!
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
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
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!
Never mind! I found it in another post.
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!
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?
Re: Add a custom user field to the certificate
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
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...
Jean-Michel, do you have an idea? You usually have wonderful ideas and I would love your insights.
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?
Re: Add a custom user field to the certificate
This post helped me solve my suspicion too, I knew it was possible to draw with $USER, Thanks Nicolás!
I just did not have the syntax correct.
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
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
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']);
Re: Add a custom user field to the certificate
the simple certificate plugin can do that without any code change, can print any custom field!