Note: You are currently viewing documentation for Moodle 2.2. Up-to-date documentation for the latest stable version is available here: Certificate customizing.

Certificate customizing: Difference between revisions

From MoodleDocs
Line 55: Line 55:


====Adjusting transparency of an image====
====Adjusting transparency of an image====
It is possible to put in a strong image as a watermark and adjust the transparency setting when the PDF is created.  Adjust this code <nowiki> $pdf->SetAlpha(0.1);</nowiki> . It is currently set to .1, a .9 would be less transparent.
It is possible to put in a strong image as a watermark and adjust the transparency setting when the PDF is created.  Look for the SetAlpha code in front of a print element. For example infront of the "print_watermark.." line, you will see $pdf_SetAlpha(0.1);  It is currently set to .1, a .5 would be less transparent.


You should see code which is commented out to alert you to the SetAlpha settings.
:''Tip:'' In older version of certificate, the watermark image itself was dimmed before it was placed in the folder.  If you had a custom watermark, this is the way it was done.  You can change the SetAlpha in the code to 1 and it should appear as it did in previous version of certificate.


===Changing Printed Text===
===Changing Printed Text===

Revision as of 18:02, 11 November 2011

This page requires updating for Moodle 2.2. Please do so and remove this template when finished.


Certificate is a contributed module and it can be customized.

Often customization can be accomplished by adding images to one of the /mod/certificate/pix folders, so they will appear as an option in the certificate settings page. Additionally, you can change the language strings located in /mod/certificate/lang.

In November 2011 the Certificate "Type" php code has been modified for Moodle 2.x. These code changes will impact string and image positions in the generated pdf document. Please see the "#Nov 2011 changes" section below.


Certificate examples


Customizing

The certificate code is located in the /mod/certificate folder. There are sub-folders for backup, db, lang (holds language sub-folders), pix (holds sub-folders for borders, seals, signatures and watermarks) and type (holds standard certificate types, each in their own sub-folder).

Adding images

Creative customized example

The easiest way to customize your certificate is to add your own JPG or PNG images. Place these in the appropriate mod/certificate/pix folder on your site. The added JPG or PNG image will appear in the dropdown list by its file name.

Make sure your new files are the same size as the standard images. For example, the standard signature file is 150x31 pixels. If the signature file is a different size, you may need to modify the code line. The same will be true for seals, borders and watermarks.

In the creative example, a custom border, watermark, signature and seal were added to the right folders. All were sized and in the same file format as the one supplied with the initial certificate install.

Customize format

It is a good idea to create your own custom certificate type, that will appear on a list along with the standard certificates types. You will know which is your custom certificate type and it will survive when you upgrade. Here is how:

1. Choose the certificate/type folder with the size/orientation you desire, copy and paste it in the certificate/type folder and give the copied folder a new name. For example copy the /type/letter_non_embedded folder and rename it 'mycertificate'.

2. Open the certificate/lang/en/certificate.php file and add the name of your new folder type. Following the above example, you would add:

$string['typemycertificate'] = 'My New Certificate';

Now, when you add a certificate to a course, your new type will appear on the drop down list as "My New Certificate".

Tip: You should add the proper string as shown in step 2 above or you may receive an error message and it will appear as if the code is broken. And remember if you update your certificate module that this file maybe erased with a new version.


Modifying image position code example

We will change the signature position that is found in the type/letter_non_embedded/certificate.php file when we generate a pdf in landscape mode. There are at least 2 ways to change the code. Say we want to move it to the left at 100 and up to 435 line.

  • Around line 100 you should notice there are a set of variables for "Landscape" which are followed by another set for "Portrait". We can overrule some of the landscape and portrait codes the $sigx and $sigy position variables, so they read $sigx = 100 and $sigy = 435 .
  • Alternatively, we can overrule some of the landscape and portrait codes, in this case the signature block. Around line 145 we see the code that actually prints in the correct position:
print_signature($pdf, $certificate, $sigx, $sigy, , );

By changing the variables to fix numbers it will not matter if the teacher selects landscape or portrait. For example:

print_signature($pdf, $certificate, 100, 435, , );

If others have not added a standard signature line, you might want to fix it to a certain size. For example, you want the image to print 150px by 75px.

print_signature($pdf, $certificate, $sigx, $sigy, '150', '75');

This should give you a good idea about how to change the position of other images.

Tip: Call your custom folder "a_landscape_special". Now you can use the second method and nobody should be surprised if they can read and been trained properly.

Adjusting transparency of an image

It is possible to put in a strong image as a watermark and adjust the transparency setting when the PDF is created. Look for the SetAlpha code in front of a print element. For example infront of the "print_watermark.." line, you will see $pdf_SetAlpha(0.1); It is currently set to .1, a .5 would be less transparent.

Tip: In older version of certificate, the watermark image itself was dimmed before it was placed in the folder. If you had a custom watermark, this is the way it was done. You can change the SetAlpha in the code to 1 and it should appear as it did in previous version of certificate.

Changing Printed Text

There are different lang strings in the certificate/lang/en_utf8/certificate.php file for each certificate type, so be sure to change the correct one for the type you are using (or create your own custom type--see above). For example, if you want to customize the text for the landscape type, you can change these lines in the above mentioned lang file:

Default:

$string['titlelandscape'] = 'CERTIFICATE OF ACHIEVEMENT';
$string['introlandscape'] = 'This is to certify that';
$string['statementlandscape'] = 'has successfully completed the course';

Customized:

$string['statementlandscape'] = 'has successfully passed the final exam';

Adding a New Line of Text

1. Open the certificate/lang/en_utf8/certificate.php file and add your new lang string giving it a unique name, e.g.:

$string['mynewtext'] = 'This is what I want to print on the certificate';

2. Open the file for your certificate type, e.g. certificate/type/mycertificate/certificate.php. At the bottom of the page below '// Add Text' is the code that prints--you guessed it--text on the certificate. Find the current line below which you would like your new text to be printed. For example, if you want your text to print below the course name, find the line:

cert_printtext(170, 330, 'C', 'Helvetica', '', 20, utf8_decode($classname));

3. Add a new line below that to print your new text using the name you gave your new lang string like this:

cert_printtext(170, 350, 'C', 'Helvetica', '', 20, utf8_decode(get_string('mynewtext', 'certificate')));
note: Make sure you a string to the language file called mynewtext. The above line of code gets that string.

4. You can adjust the placement over from the left by increasing or decreasing the '170' number. You can adjust the placement down on the page by increasing or decreasing the '350' number. Changing 'C' to 'L' or 'R' will print on the left or right side. For different fonts, see the moodle/lib/fpdf font folder for non-unicode types, and the moodle/lib/tcpdf/fonts folder for unicode types, for available fonts. (NOTE: for unicode certificates, using more than one font is NOT recommended since the entire font gets embedded in the certificate--increasing the certificate file size.) In the after the font name, you can add 'B' for bold, 'I' for italic, or 'U' for underline. Changing the '10' number will change the font size.

cert_printtext(170, 350, 'C', 'Times', , 10, utf8_decode($USER->idnumber));

5. You can add a hard coded line of text. For example, a signature title block line:

cert_printtext(150, 490, 'L', 'Times', , 10, utf8_decode('Martin Dougiamas, Lead Developer'));

Add your string to the pdf image

You can add your own custom string to the pdf print section. We wanted to add "These Continuing Credits are from Our University" below the title of the course. We used the landscape certificate type.

We added the string to the language file.

$string(ceusfromus) = 'These Continuing Credits are from Our University';

In the type folder's certificate.php file, in the PDF area near the end of the file, we put the following line:

cert_printtext(170, 380, 'C', 'Helvetica', '', 16, utf8_decode (get_string('ceusfromus', 'certificate')));

Nov 2011 changes

PukunuiAustralia / moodle-mod_certificate version 2011110107 is the basis for these comments. There maybe earlier versions which are different.

  • Standard types of certificates. There are 4 standard certificate types based upon paper size and if the fonts are embedded or non-embedded in the pdf documents. The orientation of landscape or portrait is located within each of these types. This is different from the 1.9.x versions.
  • The code for printing strings has changed. While similar to the pre 2.0, be careful.

Tips & Tricks

  • Start with one of the existing certificate types, copy it and modify it.
  • Try your customized code on a non production site, such as a local host.
  • In Administration>>Server set debugging to maximum (to show any coding error messages).
  • While you can leave a php file open in edit mode, you must save the file before your changes become active.
  • Make code changes one at a time, test and then backup after each successful change. For example,after adding a new string to the certificate lang file), go to your course page and click on a certificate to see if it appears on the list.
  • If you broke the code you may get a blank screen for a certificate.
    • So it might be a good idea to make a backup of each successful change before starting the next change. For example, after the 3rd code change, save certificate.php as certificate3.txt.
  • Certificate icon location-If you are using your own theme you have to put the icon.gif inside a folder called certificate and put in your mod folder image. For example, if your theme uses custom icons, you must put a certificate folder with the certificate icon.gif in it under your theme/pix/mod folder.
  • Consider editing you language pack rather than the /certificate/lang/en/certificate.php file. You can do this through the Settings > Site administration >Language > Language customization menu. Look for mod/certificate.php. Then when you update your certificate and it's language,it will keep your changes for the standard strings.

See also