Display user profile pic on header

Re: Display user profile pic on header

by Kulin Shah -
Number of replies: 0

Hi Darko,

I am using Moodle 3.0 and am trying to attach an image to the Certificate module by Mark Nelson

I tried many approaches but failed in all

I also tried your approach but am unable to print the image in pdf, the certificate module uses tcpdf

Can you guide please


The code I am using is as below

certificate.php

<?php


// This file is part of the Certificate module for Moodle - http://moodle.org/

//

// Moodle is free software: you can redistribute it and/or modify

// it under the terms of the GNU General Public License as published by

// the Free Software Foundation, either version 3 of the License, or

// (at your option) any later version.

//

// Moodle is distributed in the hope that it will be useful,

// but WITHOUT ANY WARRANTY; without even the implied warranty of

// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

// GNU General Public License for more details.

//

// You should have received a copy of the GNU General Public License

// along with Moodle.  If not, see <http://www.gnu.org/licenses/>;.


/**

 * A4_non_embedded certificate type

 *

 * @package    mod_certificate

 * @copyright  Mark Nelson <markn@moodle.com>

 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

 */


defined('MOODLE_INTERNAL') || die();


$pdf = new PDF($certificate->orientation, 'mm', 'A4', true, 'UTF-8', false);


$pdf->SetTitle($certificate->name);

$pdf->SetProtection(array('modify'));

$pdf->setPrintHeader(false);

$pdf->setPrintFooter(false);

$pdf->SetAutoPageBreak(false, 0);

$pdf->AddPage();


// Define variables

// Landscape

if ($certificate->orientation == 'L') {

    $x = 10;

    $y = 30;

    $sealx = 230;

    $sealy = 150;

    $sigx = 162;

    $sigy = 148;

    $custx = 10;

    $custy = 155;

    $wmarkx = 40;

    $wmarky = 31;

    $wmarkw = 212;

    $wmarkh = 148;

    $brdrx = 0;

    $brdry = 0;

    $brdrw = 297;

    $brdrh = 210;

    $codey = 10;

  

} else { // Portrait

    $x = 10;

    $y = 40;

    $sealx = 150;

    $sealy = 220;

    $sigx = 30;

    $sigy = 230;

    $custx = 30;

    $custy = 230;

    $wmarkx = 26;

    $wmarky = 58;

    $wmarkw = 158;

    $wmarkh = 170;

    $brdrx = 0;

    $brdry = 0;

    $brdrw = 210;

    $brdrh = 297;

    $codey = 250;

   

}

// Darko


global $USER, $PAGE;

$userpicture = new user_picture($USER);

$url = $userpicture->get_url($PAGE);


// Or display the image

//echo html_writer::tag('img', $url->out(false), array('target' => '_blank', 'class' => 'aaa bbb', 'style' => 'font-weight: bold;')); <<< This hangs the PDF creation


//User picture

$picx = 100;  // Picture horizontal position.

$picy = 40;   // Picture vertical position.

$picw = 50; // Picture width.

$pich = 50; // Picture height.

$context = context_user::instance($USER->id, IGNORE_MISSING);

if ($context) {

    $fs = get_file_storage();

     

    // Prepare file record object

    $fileinfo = array(

        'component' => 'user',     // usually = table name

        'filearea' => 'icon',     // usually = table name

        'itemid' => 0,               // usually = ID of row in table

        'contextid' => $context->id, // ID of context

        'filepath' => '/',           // any path beginning and ending in /

        'filename' => 'f3.png'); // any filename 

    // Get file.

  //  $file = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'],

  //                       null, $fileinfo['filepath'], $fileinfo['filename']);

$file = $fs->get_file($context->id,'user','icon',null,'/',$fileinfo['filename']);

  //  $fileinfo['itemid']

    // Print image.

    if ($file) {

        $contenthash = $file->get_contenthash();

        $l1 = $contenthash[0] . $contenthash[1];

        $l2 = $contenthash[2] . $contenthash[3];

        $location = $CFG->wwwroot . '/filedir' . '/' . $l1 . '/' . $l2 . '/' . $contenthash; (also used dataroot instead of wwwroot)

        $pdf->image($location, $picx, $picy, $picw, $pich);

      }

}

// Add images and lines

certificate_print_image($pdf, $certificate, CERT_IMAGE_BORDER, $brdrx, $brdry, $brdrw, $brdrh);

certificate_draw_frame($pdf, $certificate);

// Set alpha to semi-transparency

$pdf->SetAlpha(0.2);

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, '20', '20');

// certificate_print_image($pdf, $certificate, $location, $x, $y, '', '');


// $pdf->Image($a, $picx, $picy, $picw, $pich);

// 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 + 20, 'C', 'Times', '', 20, get_string('certify', 'certificate'));

certificate_print_text($pdf, $x + 53, $y + 35, 'L', 'Helvetica', '', 30, fullname($USER));

certificate_print_text($pdf, $x, $y + 55, 'C', 'Helvetica', '', 20, get_string('statement', 'certificate'));

// certificate_print_text($pdf, $x + 103, $y + 77, 'L', 'Helvetica', '', 20, format_string($course->fullname));

certificate_print_text($pdf, $x + 57, $y + 111, 'L', 'Helvetica', '', 14, certificate_get_date($certificate, $certrecord, $course). ' at '. $USER->profile['instname']. ', '. $USER->profile['instarea']);

certificate_print_text($pdf, $x + 95, $y + 96, 'L', 'Helvetica', '', 14, certificate_get_grade($certificate, $course));

// certificate_print_text($pdf, $x, $y + 115, 'C', 'Times', '', 10, certificate_get_outcome($gradeercent, $course));

// certificate_print_text($pdf, $x + 37, $y + 95, 'L', 'Helvetica', '', 15, $USER->profile['instname']. ', '. $USER->profile['instarea']);

// certificate_print_text($pdf, $x + 37, $y + 100, 'L', 'Helvetica', '', 15, $USER->profile['instarea']);

if ($certificate->printhours) {

//    certificate_print_text($pdf, $x + 37, $y + 117, 'L', 'Helvetica', '', 12, get_string('credithours', 'certificate') . ': ' . $certificate->printhours);

}

certificate_print_text($pdf, $x + 8, $codey + 27, 'L', 'Times', '', 20,' Code : '. certificate_get_code($certificate, $certrecord));

$i = 0;

if ($certificate->printteacher) {

    $context = context_module::instance($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 * 4), 'L', 'Times', '', 12, fullname($teacher));

        $pdf->image($url, $picx, $picy, $picw, $pich);

        }

    }

}