$canmanage = has_capability('mod/simplecertificate:manage', $context);
$url = new moodle_url('/mod/simplecertificate/view.php', array('id' => $cm->id, 'tab' => $tab, 'page' => $page, 'perpage' => $perpage));
if ($type) {
    $url->param('type', $type);
}
if ($sort) {
    $url->param('sort', $sort);
}
if ($action) {
    $url->param('action', $action);
}
if ($issuelist) {
    $url->param('issuelist', $issuelist);
}
// Create certificate object
$simplecertificate = new simplecertificate($certificate, $context);
// Mark completion as view
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
// Initialize $PAGE, compute blocks
$PAGE->set_url($url);
$PAGE->set_context($context);
$PAGE->set_cm($cm);
$PAGE->set_title(format_string($certificate->name));
$PAGE->set_heading(format_string($course->fullname));
require_login($course->id, false, $cm);
switch ($tab) {
    case $simplecertificate::ISSUED_CERTIFCADES_VIEW:
        $simplecertificate->view_issued_certificates($url);
        break;
    case $simplecertificate::BULK_ISSUE_CERTIFCADES_VIEW:
/**
 * Obtains the automatic completion state for this forum based on any conditions
 * in simplecertificate settings.
 * 
 * @param object $course Course
 * @param object $cm Course-module
 * @param int $userid User ID
 * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
 * @return bool True if completed, false if not, $type if conditions not set.
 */
function simplecertificate_get_completion_state($course, $cm, $userid, $type)
{
    global $CFG;
    require_once $CFG->dirroot . '/mod/simplecertificate/locallib.php';
    $context = context_module::instance($cm->id);
    $simplecertificate = new simplecertificate($context, $cm, $course);
    if ($requiredtime = $simplecertificate->get_instance()->requiredtime) {
        return $simplecertificate->get_course_time($userid) >= $requiredtime;
    }
    // Completion option is not enabled so just return $type
    return $type;
}
 /**
  * Prepares the form before data are set
  *
  * Additional wysiwyg editor are prepared here, the introeditor is prepared automatically by core.
  * Grade items are set here because the core modedit supports single grade item only.
  *
  * @param array $data to be set
  * @return void
  */
 public function data_preprocessing(&$data)
 {
     global $CFG;
     require_once dirname(__FILE__) . '/locallib.php';
     parent::data_preprocessing($data);
     if ($this->current->instance) {
         // editing an existing certificate - let us prepare the added editor elements (intro done automatically), and files
         //First Page
         //Get firstimage
         $imagedraftitemid = file_get_submitted_draft_itemid('certificateimage');
         //Get firtsimage filearea information
         $imagefileinfo = simplecertificate::get_certificate_image_fileinfo($this->context);
         file_prepare_draft_area($imagedraftitemid, $imagefileinfo['contextid'], $imagefileinfo['component'], $imagefileinfo['filearea'], $imagefileinfo['itemid'], $this->get_filemanager_options_array());
         $data['certificateimage'] = $imagedraftitemid;
         //Prepare certificate text
         $data['certificatetext'] = array('text' => $data['certificatetext'], 'format' => FORMAT_HTML);
         //Second page
         //Get Back image
         $secondimagedraftitemid = file_get_submitted_draft_itemid('secondimage');
         //Get secondimage filearea info
         $secondimagefileinfo = simplecertificate::get_certificate_secondimage_fileinfo($this->context);
         file_prepare_draft_area($secondimagedraftitemid, $secondimagefileinfo['contextid'], $secondimagefileinfo['component'], $secondimagefileinfo['filearea'], $secondimagefileinfo['itemid'], $this->get_filemanager_options_array());
         $data['secondimage'] = $secondimagedraftitemid;
         //Get backpage text
         if (!empty($data['secondpagetext'])) {
             $data['secondpagetext'] = array('text' => $data['secondpagetext'], 'format' => FORMAT_HTML);
         } else {
             $data['secondpagetext'] = array('text' => '', 'format' => FORMAT_HTML);
         }
     } else {
         //Load default
         $data['certificatetext'] = array('text' => '', 'format' => FORMAT_HTML);
         $data['secondpagetext'] = array('text' => '', 'format' => FORMAT_HTML);
     }
     //completion rules
     $data['completiontimeenabled'] = !empty($data['requiredtime']) ? 1 : 0;
 }
function watermark_and_sent(stdClass $issuedcert)
{
    global $CFG, $USER, $COURSE, $DB, $PAGE;
    if ($issuedcert->haschange) {
        //This issue have a haschange flag, try to reissue
        if (empty($issuedcert->timedeleted)) {
            require_once $CFG->dirroot . '/mod/simplecertificate/locallib.php';
            try {
                // Try to get cm
                $cm = get_coursemodule_from_instance('simplecertificate', $issuedcert->certificateid, 0, false, MUST_EXIST);
                $context = context_module::instance($cm->id);
                //Must set a page context to issue ....
                $PAGE->set_context($context);
                $simplecertificate = new simplecertificate($context, null, null);
                $file = $simplecertificate->get_issue_file($issuedcert);
            } catch (moodle_exception $e) {
                // Only debug, no errors
                debugging($e->getMessage(), DEBUG_DEVELOPER, $e->getTrace());
            }
        } else {
            //Have haschange and timedeleted, somehting wrong, it will be impossible to reissue
            //add wraning
            debugging("issued certificate [{$issuedcert->id}], have haschange and timedeleted");
        }
        $issuedcert->haschange = 0;
        $DB->update_record('simplecertificate_issues', $issuedcert);
    }
    if (empty($file)) {
        $fs = get_file_storage();
        if (!$fs->file_exists_by_hash($issuedcert->pathnamehash)) {
            print_error(get_string('filenotfound', 'simplecertificate', ''));
        }
        $file = $fs->get_file_by_hash($issuedcert->pathnamehash);
    }
    $canmanage = false;
    if (!empty($COURSE)) {
        $canmanage = has_capability('mod/simplecertificate:manage', context_course::instance($COURSE->id));
    }
    if ($canmanage || !empty($USER) && $USER->id == $issuedcert->userid) {
        send_stored_file($file, 0, 0, true);
    } else {
        require_once $CFG->libdir . '/pdflib.php';
        require_once $CFG->dirroot . '/mod/simplecertificate/lib/fpdi/fpdi.php';
        // copy to a tmp file
        $tmpfile = $file->copy_content_to_temp();
        // TCPF doesn't import files yet, so i must use FPDI
        $pdf = new FPDI();
        $pageCount = $pdf->setSourceFile($tmpfile);
        for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
            // import a page
            $templateId = $pdf->importPage($pageNo);
            // get the size of the imported page
            $size = $pdf->getTemplateSize($templateId);
            // create a page (landscape or portrait depending on the imported page size)
            if ($size['w'] > $size['h']) {
                $pdf->AddPage('L', array($size['w'], $size['h']));
                // Font size 1/3 Height if it landscape
                $fontsize = $size['h'] / 3;
            } else {
                $pdf->AddPage('P', array($size['w'], $size['h']));
                // Font size 1/3 Width if it portrait
                $fontsize = $size['w'] / 3;
            }
            // use the imported page
            $pdf->useTemplate($templateId);
            // Calculating the rotation angle
            $rotAngle = atan($size['h'] / $size['w']) * 180 / pi();
            // Find the middle of the page to use as a pivot at rotation.
            $mX = $size['w'] / 2;
            $mY = $size['h'] / 2;
            // Set the transparency of the text to really light
            $pdf->SetAlpha(0.25);
            $pdf->StartTransform();
            $pdf->Rotate($rotAngle, $mX, $mY);
            $pdf->SetFont("freesans", "B", $fontsize);
            $pdf->SetXY(0, $mY);
            $boder_style = array('LTRB' => array('width' => 2, 'dash' => $fontsize / 5, 'cap' => 'round', 'join' => 'round', 'phase' => $fontsize / $mX));
            $pdf->Cell($size['w'], $fontsize, get_string('certificatecopy', 'simplecertificate'), $boder_style, 0, 'C', false, '', 4, true, 'C', 'C');
            $pdf->StopTransform();
            // Reset the transparency to default
            $pdf->SetAlpha(1);
        }
        // Set protection seems not work, so comment
        // $pdf->SetProtection(array('print', 'modify', 'print-high'),null, random_string(), '1',null);
        // For DEGUG
        // $pdf->Output($file->get_filename(), 'I');
        // Save and send tmpfiles
        $pdf->Output($tmpfile, 'F');
        send_temp_file($tmpfile, $file->get_filename());
    }
}
 public function testable_get_issued_certificate_users($sort = "ci.timecreated ASC", $groupmode = 0, $page = 0, $perpage = self::SIMPLECERT_MAX_PER_PAGE)
 {
     return parent::get_issued_certificate_users($sort, $groupmode, $page, $perpage);
 }