public function view_issued_certificates(moodle_url $url)
 {
     global $OUTPUT, $DB, $CFG;
     // Declare some variables
     $strcertificates = get_string('modulenameplural', 'simplecertificate');
     $strcertificate = get_string('modulename', 'simplecertificate');
     $strto = html_writer::link($url->out(false, array('orderby' => 'username')), get_string('awardedto', 'simplecertificate'));
     $strdate = html_writer::link($url->out(false, array('orderby' => 'issuedate')), get_string('receiveddate', 'simplecertificate'));
     $strgrade = get_string('grade', 'simplecertificate');
     $strcode = get_string('code', 'simplecertificate');
     $strreport = get_string('report', 'simplecertificate');
     $groupmode = groups_get_activity_groupmode($this->get_course_module());
     $page = $url->get_param('page');
     $perpage = $url->get_param('perpage');
     $orderby = $url->get_param('orderby');
     $usercount = 0;
     $users = $this->get_issued_certificate_users($orderby, $groupmode);
     if ($users) {
         $usercount = count($users);
         $users = array_slice($users, intval($page * $perpage), $perpage);
     }
     if (!$url->get_param('action')) {
         echo $OUTPUT->header();
         $this->show_tabs($url);
         if ($groupmode) {
             groups_get_activity_group($this->coursemodule, true);
         }
         groups_print_activity_menu($this->coursemodule, $url);
         if (!$users) {
             notify(get_string('nocertificatesissued', 'simplecertificate'));
             echo $OUTPUT->footer();
             exit;
         }
         // Create the table for the users
         $table = new html_table();
         $table->width = "95%";
         $table->tablealign = "center";
         $table->head = array($strto, $strdate, $strgrade, $strcode);
         $table->align = array("left", "left", "center", "center");
         foreach ($users as $user) {
             $name = $OUTPUT->user_picture($user) . fullname($user);
             $date = userdate($user->timecreated) . simplecertificate_print_issue_certificate_file($this->get_issue($user));
             $code = $user->code;
             $table->data[] = array($name, $date, $this->get_grade($user->id), $code);
         }
         // Create table to store buttons
         $tablebutton = new html_table();
         $tablebutton->attributes['class'] = 'downloadreport';
         $btndownloadods = $OUTPUT->single_button($url->out_as_local_url(false, array('action' => 'download', 'type' => 'ods')), get_string("downloadods"));
         $btndownloadxls = $OUTPUT->single_button($url->out_as_local_url(false, array('action' => 'download', 'type' => 'xls')), get_string("downloadexcel"));
         $btndownloadtxt = $OUTPUT->single_button($url->out_as_local_url(false, array('action' => 'download', 'type' => 'txt')), get_string("downloadtext"));
         $tablebutton->data[] = array($btndownloadods, $btndownloadxls, $btndownloadtxt);
         echo $OUTPUT->paging_bar($usercount, $page, $perpage, $url);
         echo '<br />';
         echo html_writer::table($table);
         echo html_writer::tag('div', html_writer::table($tablebutton), array('style' => 'margin:auto; width:50%'));
     } else {
         if ($url->get_param('action') == 'download') {
             $page = $perpage = 0;
             $type = $url->get_param('type');
             // Calculate file name
             $filename = clean_filename($this->get_instance()->coursename . '-' . strip_tags(format_string($this->get_instance()->name, true)) . '.' . strip_tags(format_string($type, true)));
             switch ($type) {
                 case 'ods':
                     require_once "{$CFG->libdir}/odslib.class.php";
                     // Creating a workbook
                     $workbook = new MoodleODSWorkbook("-");
                     // Send HTTP headers
                     $workbook->send(format_text($filename, true));
                     // Creating the first worksheet
                     $myxls = $workbook->add_worksheet($strreport);
                     // Print names of all the fields
                     $myxls->write_string(0, 0, get_string("fullname"));
                     $myxls->write_string(0, 1, get_string("idnumber"));
                     $myxls->write_string(0, 2, get_string("group"));
                     $myxls->write_string(0, 3, $strdate);
                     $myxls->write_string(0, 4, $strgrade);
                     $myxls->write_string(0, 5, $strcode);
                     // Generate the data for the body of the spreadsheet
                     $i = 0;
                     $row = 1;
                     if ($users) {
                         foreach ($users as $user) {
                             $myxls->write_string($row, 0, fullname($user));
                             $studentid = !empty($user->idnumber) ? $user->idnumber : " ";
                             $myxls->write_string($row, 1, $studentid);
                             $ug2 = '';
                             if ($usergrps = groups_get_all_groups($this->get_course()->id, $user->id)) {
                                 foreach ($usergrps as $ug) {
                                     $ug2 = $ug2 . $ug->name;
                                 }
                             }
                             $myxls->write_string($row, 2, $ug2);
                             $myxls->write_string($row, 3, userdate($user->timecreated));
                             $myxls->write_string($row, 4, $this->get_grade($user->id));
                             $myxls->write_string($row, 5, $user->code);
                             $row++;
                         }
                         $pos = 5;
                     }
                     // Close the workbook
                     $workbook->close();
                     break;
                 case 'xls':
                     require_once "{$CFG->libdir}/excellib.class.php";
                     // Creating a workbook
                     $workbook = new MoodleExcelWorkbook("-");
                     // Send HTTP headers
                     $workbook->send(format_text($filename, true));
                     // Creating the first worksheet
                     $myxls = $workbook->add_worksheet($strreport);
                     // Print names of all the fields
                     $myxls->write_string(0, 0, get_string("fullname"));
                     $myxls->write_string(0, 1, get_string("idnumber"));
                     $myxls->write_string(0, 2, get_string("group"));
                     $myxls->write_string(0, 3, $strdate);
                     $myxls->write_string(0, 4, $strgrade);
                     $myxls->write_string(0, 5, $strcode);
                     // Generate the data for the body of the spreadsheet
                     $i = 0;
                     $row = 1;
                     if ($users) {
                         foreach ($users as $user) {
                             $myxls->write_string($row, 0, fullname($user));
                             $studentid = !empty($user->idnumber) ? $user->idnumber : " ";
                             $myxls->write_string($row, 1, $studentid);
                             $ug2 = '';
                             if ($usergrps = groups_get_all_groups($this->get_course()->id, $user->id)) {
                                 foreach ($usergrps as $ug) {
                                     $ug2 = $ug2 . $ug->name;
                                 }
                             }
                             $myxls->write_string($row, 2, $ug2);
                             $myxls->write_string($row, 3, userdate($user->timecreated));
                             $myxls->write_string($row, 4, $this->get_grade($user->id));
                             $myxls->write_string($row, 5, $user->code);
                             $row++;
                         }
                         $pos = 5;
                     }
                     // Close the workbook
                     $workbook->close();
                     break;
                 case 'txt':
                     header("Content-Type: application/download\n");
                     header("Content-Disposition: attachment; filename=\"" . format_text($filename, true) . "\"");
                     header("Expires: 0");
                     header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
                     header("Pragma: public");
                     // Print names of all the fields
                     echo get_string("fullname") . "\t" . get_string("idnumber") . "\t";
                     echo get_string("group") . "\t";
                     echo $strdate . "\t";
                     echo $strgrade . "\t";
                     echo $strcode . "\n";
                     // Generate the data for the body of the spreadsheet
                     $i = 0;
                     $row = 1;
                     if ($users) {
                         foreach ($users as $user) {
                             echo fullname($user);
                             $studentid = " ";
                             if (!empty($user->idnumber)) {
                                 $studentid = $user->idnumber;
                             }
                             echo "\t" . $studentid . "\t";
                             $ug2 = '';
                             if ($usergrps = groups_get_all_groups($this->get_course()->id, $user->id)) {
                                 foreach ($usergrps as $ug) {
                                     $ug2 = $ug2 . $ug->name;
                                 }
                             }
                             echo $ug2 . "\t";
                             echo userdate($user->timecreated) . "\t";
                             echo $this->get_grade($user->id) . "\t";
                             echo $user->code . "\n";
                             $row++;
                         }
                     }
                     break;
             }
             exit;
         }
     }
     echo $OUTPUT->footer();
 }
    } else {
        $username = get_string('notavailable');
    }
    $strto = get_string('awardedto', 'simplecertificate');
    $strdate = get_string('issueddate', 'simplecertificate');
    $strcode = get_string('code', 'simplecertificate');
    $table = new html_table();
    $table->width = "95%";
    $table->tablealign = "center";
    $table->head = array(get_string('course'), $strto, $strdate, $strcode);
    $table->align = array("left", "left", "center", "center");
    //Try to get coursename
    if (!($coursename = $DB->get_field('simplecertificate', 'coursename', array('id' => $issuedcert->certificateid)))) {
        $coursename = get_string('coursenotfound', 'simplecertificate');
    }
    $table->data[] = array($coursename, $username, userdate($issuedcert->timecreated) . simplecertificate_print_issue_certificate_file($issuedcert), $issuedcert->code);
    echo html_writer::table($table);
    // Add to log
    //add_to_log($context->instanceid, 'simplecertificate', 'verify', "verify.php?code=$code", '$issuedcert->id');
    $event = \mod_simplecertificate\event\certificate_verified::create(array('objectid' => $issuedcert->id, 'context' => $context, 'relateduserid' => $issuedcert->userid, 'other' => array('issuedcertcode' => $issuedcert->code)));
    $event->trigger();
}
echo $OUTPUT->footer();
function get_issued_cert($code = null)
{
    global $DB;
    if (!($issuedcert = $DB->get_record("simplecertificate_issues", array('code' => $code)))) {
        print_error(get_string('invalidcode', 'simplecertificate'));
    }
    return $issuedcert;
}