Replace Name Field with Another Field - Not Working

Replace Name Field with Another Field - Not Working

by D Barnhart -
Number of replies: 7

I have searched this all day long.  I am trying to replace the First and Last Name that is printed on the certificate with a custom field with a shortname called BusinessName.  I have found a couple of other threads that deal with this but when I try what others have done I just come up with a blank screen.

This is what I have done in certificate.php under the certificate type mycertificate:

Replaced certificate_printtext ($pdf, $x, $y + 105, 'C', 'Helvetica', ' ', 30, fullname ($USER));

with certificate_printtext ($pdf, $x, $y + 105, 'C', 'Helvetica', ' ', 30, $USER->BusinessName);

I can view the certificate but there is just a blank spot where the First and Last Name was and it does not print the Business Name.  I double checked that there is something in this field in the database so I am not sure why the Business Name is not printing.

Any help would be appreciated.  I am new to moodle so please give me specific instructions LOL

Thanks

Average of ratings: -
In reply to D Barnhart

Re: Replace Name Field with Another Field - Not Working

by D Barnhart -

Also I just upgraded the plugin so everything is up-to-date.

I created Mycertificate file under type and copied the certificate.php file from letter_non_embedded and messed with the code a bit and added:

certificate_print_text($pdf, $x, $y + 95, 'C', 'Helvetica', '', 20, $USER->BusinessName);

certificate_print_text($pdf, $x, $y + 120, 'C', 'Helvetica', '', 15, 'Dealer Number: ' . $USER->DealerNumber);

I can view my certificate but it is just blank for Business Name and Dealer Number.  Not sure how to pull data from the user profile fields.

What I am doing wrong?

Thanks

In reply to D Barnhart

Re: Replace Name Field with Another Field - Not Working

by Jean-Michel Védrine -

Hello,

I suppose that BusinessName and DealerNumber are custom fields that you defined in the user profile ?

If yes then the solution was given in that thread :

http://moodle.org/mod/forum/discuss.php?d=186674

In reply to Jean-Michel Védrine

Re: Replace Name Field with Another Field - Not Working

by D Barnhart -

Yes those are custom fields that I have defined in user profile.  And I have tried that solution but it didn't work.  This is what I have tried:

In certificate.php under mycertificate type I added:
$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 = 'BusinessName'";

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

$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 = 'DealerNumber'";

if ($value = $DB->get_record_sql($sql)) {
        $value = $value->data;
} else {
        $value = '';
}
But the code just breaks because when I try to view the certificate it is just a blank white page. 

Then I tried this as suggested in another thread:

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

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

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

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

But again when I try to view the certificate I am just directed to a blank white page.

I've tried deleting the above code that I added and then just try to pull the data from the database with this code:

certificate_print_text($pdf, $x, $y + 95, 'C', 'Helvetica', ' ', 20, $USER->BusinessName);

certificate_print_text($pdf, $x, $y + 95, 'C', 'Helvetica', ' ', 20, 'Dealer Number:' $USER->BusinessName);

But that doesn't work either.  I simply cannot get the certificate to pull data from the database.

Any other suggestions?  I really need this to work correctly.  Thanks for the help!

In reply to D Barnhart

Re: Replace Name Field with Another Field - Not Working

by Jean-Michel Védrine -

The certificate module cannot "pull data from the database" without a query to extract the desired data. So just a simple certificate_print_text cannot work at all.

The very first thing you need is the "short name" of your custom fields. Are you sure it's BusinessName and DealerNumber ? Are you sure you are not confusing short name and name of your custom profile fields this is a very common mistake (for instance I have a custom field with name Date of birth and short name dob).

When you are sure everything is ok with your fields shortnames, then add the suggested code verifying each line.

If you get a blank page, don't remove the added code, Go to Site Administration ->  Development -> Debugging and set the dropdown menu for debug messages to DEVELOPER level.

Then go again on the certificate page and try again. Most often instead of a blank page you will see an error message that will help you to see where the problem is.

If you have trouble understanding the error message, copy it and post it here and I will help you to correct the problem.

In reply to Jean-Michel Védrine

Re: Replace Name Field with Another Field - Not Working

by Jean-Michel Védrine -

I suddently realise I forgot to ask you your Moodle version ?

In reply to Jean-Michel Védrine

Re: Replace Name Field with Another Field - Not Working

by D Barnhart -

Thank you for your reply.  My moodle version is 2.3 and I just updated the certificate module.  I will recheck my shortname names and put the code back in and use the debugger.

I will let you know what happens smile

In reply to Jean-Michel Védrine

Re: Replace Name Field with Another Field - Not Working

by D Barnhart -

YES! It worked.  Thank you so much for your help.  Very much appreciated smile

Here's how the code looks for anyone else who is adding other fields to the certificate:

<?php

if (!defined('MOODLE_INTERNAL')) {
    die('Direct access to this script is forbidden.'); // It must be included from view.php
}

$pdf = new TCPDF($certificate->orientation, 'pt', 'Letter', true, 'UTF-8', false);

$pdf->SetTitle($certificate->name);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetAutoPageBreak(false, 0);
$pdf->AddPage();

// Define variables
// Landscape
if ($certificate->orientation == 'L') {
    $x = 28;
    $y = 125;
    $sealx = 590;
    $sealy = 425;
    $sigx = 130;
    $sigy = 440;
    $custx = 133;
    $custy = 440;
    $wmarkx = 100;
    $wmarky = 90;
    $wmarkw = 600;
    $wmarkh = 420;
    $brdrx = 0;
    $brdry = 0;
    $brdrw = 792;
    $brdrh = 612;
    $codey = 505;
} else { // Portrait
    $x = 28;
    $y = 170;
    $sealx = 440;
    $sealy = 590;
    $sigx = 85;
    $sigy = 580;
    $custx = 88;
    $custy = 580;
    $wmarkx = 78;
    $wmarky = 130;
    $wmarkw = 450;
    $wmarkh = 480;
    $brdrx = 10;
    $brdry = 10;
    $brdrw = 594;
    $brdrh = 771;
    $codey = 660;
}

// Add images and lines
certificate_print_image($pdf, $certificate, CERT_IMAGE_BORDER, $brdrx, $brdry, $brdrw, $brdrh);
certificate_draw_frame_letter($pdf, $certificate);
// Set alpha to semi-transparency
$pdf->SetAlpha(0.1);
certificate_print_image($pdf, $certificate, CERT_IMAGE_WATERMARK, $wmarkx, $wmarky, $wmarkw, $wmarkh);
$pdf->SetAlpha(1);
certificate_print_image($pdf, $certificate, CERT_IMAGE_SEAL, $sealx, $sealy, '', '');
certificate_print_image($pdf, $certificate, CERT_IMAGE_SIGNATURE, $sigx, $sigy, '', '');

$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 = 'businessname'";

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

// 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 + 95, 'C', 'Helvetica', '', 20, 'Business Name: '.$value);

$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 = 'dealernumber'";

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

certificate_print_text($pdf, $x, $y + 120, 'C', 'Helvetica', '', 15, 'Dealer Number: '.$value);
certificate_print_text($pdf, $x, $y + 160, 'C', 'Helvetica', '', 20, get_string('statement', 'certificate'));
certificate_print_text($pdf, $x, $y + 207, '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;
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++;
            certificate_print_text($pdf, $sigx, $sigy + ($i * 12), 'L', 'Times', '', 12, fullname($teacher));
        }
    }
}

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