<?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/>.

/**
 * letter_embedded certificate type
 *
 * @package    mod
 * @subpackage certificate
 * @copyright  Mark Nelson <markn@moodle.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

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->AddPage();
$pdf->SetAutoPageBreak(false, 0);
$pdf->SetFont('helvetica', '', 10);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//$pdf->setFontSubsetting(false);
//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set a barcode on the page footer
$pdf->setBarcode(date('Y-m-d H:i:s'));

// set font
$pdf->SetFont('helvetica', '', 11);

$style2 = array(
    'position' => 'C',
    'align' => 'C',
    'stretch' => false,
    'fitwidth' => true,
    'cellfitalign' => '',
    'border' => false,
	'padding'=>5,
    'hpadding' => 'auto',
    'vpadding' => 'auto',
    'fgcolor' => array(0,0,0),
    'bgcolor' => false, //array(255,255,255),
    'text' => true,
    'font' => 'helvetica',
    'fontsize' => 8,
    'stretchtext' => 4
);
// Define variables
// Landscape
if ($certificate->orientation == 'L') {
    $x = 28;
    $y = 50;
    $sealx = 590;
    $sealy = 425;
    $sigx = 130;
    $sigy = 440;
    $custx = 133;
    $custy = 440;
    $wmarkx = 2;
    $wmarky = 2;
    $wmarkw = 787;
    $wmarkh = 607;
    $brdrx = 0;
    $brdry = 0;
    $brdrw = 842;
    $brdrh = 612;
    $codey = 505;
} else { // Portrait
    $x = 28;
    $y = 50;
    $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(1);
$pdf->setJPEGQuality(100);
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, '', '');

// Add text
//$pdf->SetTextColor(0, 0, 120);
//certificate_print_text($pdf, $x, $y, 'C', 'freesans', '', 30, get_string('title', 'certificate'));
$pdf->SetTextColor(0, 0, 0);
$cc=utf8_decode($USER->username);

//certificate_print_text($pdf, $x, $y+10 , 'C', 'helvetica', '', 22,'INSTITUTO NACIONAL DE SALUD');

//certificate_print_text($pdf, $x, $y+30 , 'C', 'helvetica', '', 18,'SUBDIRECCIÓN DE VIGILANCIA Y CONTROL  EN SALUD PÚBLICA');
$pdf->SetTextColor(0, 0, 0);
certificate_print_text($pdf, $x, $y+30, 'C', 'helvetica', '',18,'<strong>La Secretarìa Distrital de Salud</strong>');
certificate_print_text($pdf, $x, $y+50 , 'C', 'helvetica', '', 16,'<strong>Subdirecciòn del Laboratorio de Salud Pùblica</strong>');
certificate_print_text($pdf, $x, $y+80 , 'C', 'helvetica', '', 14,'<strong>Otorgan el presente certificado a:</strong>');
$pdf->SetTextColor(0, 0,0);
certificate_print_text($pdf, $x, $y + 95, 'C', 'helvetica', '', 22,'<STRONG>'. fullname($USER).'</STRONG>');
certificate_print_text($pdf, $x, $y + 130, 'C', 'helvetica', '', 16, 'C.C.'.$cc);
certificate_print_text($pdf, $x, $y + 170, 'C', 'helvetica', '', 14, 'Como conferencista capitulo Emisión de resultados - Fase Pos analítica de la:');

//$pdf->SetTextColor(232, 24, 118);
certificate_print_text($pdf, $x, $y + 220, 'C', 'helvetica', '', 22,'<STRONG>'. $course->fullname.'</STRONG>');
//certificate_print_text($pdf, $x, $y + 220, 'C', 'helvetica', '', 14, '(Sistema de Información Distrital y de Monitoreo del Acceso)');
$pdf->SetTextColor(87, 87, 86);

//certificate_print_text($pdf, $x, $y + 205, 'C', 'helvetica', '', 30, $course->fullname);
$pdf->SetTextColor(0, 0, 0);
//certificate_print_text($pdf, $x, $y + 265, 'C', 'helvetica', '', 14, certificate_get_date($certificate, $certrecord, $course));
certificate_print_text($pdf, $x, $y + 203, 'C', 'helvetica', '', 10, certificate_get_grade($certificate, $course));
certificate_print_text($pdf, $x, $y + 201, 'C', 'helvetica', '', 10, certificate_get_outcome($certificate, $course));
$meses = array("Enero", "Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
$mes=$meses[date('n', $USER->firstaccess+36000)-1];
$anio=date('Y', $USER->firstaccess+36000);
$dia=date('d', $USER->firstaccess+36000);
$salida=date('d-M-Y', $USER->lastaccess+36000);
$pdf->SetTextColor(0, 0, 0);

/*certificate_print_text($pdf, $x, $y + 300, 'C', 'helvetica', '', 12, 'El día '.certificate_get_date($certificate, $certrecord, $course));*/

certificate_print_text($pdf, $x, $y + 260, 'C', 'helvetica', '', 11, $certificate->customtext);

//$pdf->Ln();
$pdf->write1DBarcode(certificate_get_code($certificate, $certrecord), 'C39', $x,$y +320, '', 50, 1, $style2, 'N');
//$pdf->Ln();


$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', 'helvetica', '', 12, fullname($teacher));
        }
    }
}



?>