Esempio n. 1
0
function watupro_view_certificate()
{
    global $wpdb, $user_ID;
    // select certificate
    $certificate = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_CERTIFICATES . " WHERE ID=%d", $_GET['id']));
    if (empty($certificate->ID)) {
        wp_die(__("no such certificate", "watupro"));
    }
    $output = stripslashes($certificate->html);
    // no taking id? only admin can see it then
    if (empty($_GET['taking_id'])) {
        if (!current_user_can(WATUPRO_MANAGE_CAPS)) {
            wp_die(__('You do not have sufficient permissions to access this page', 'watupro') . ' 1');
        }
    } else {
        // find taking
        $taking = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_TAKEN_EXAMS . "\n\t\t\tWHERE ID=%d", $_GET['taking_id']));
        // find user_certificate record and see if the current user is allowed to see the certificate
        $user_certificate = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_USER_CERTIFICATES . "\n\t\t\tWHERE taking_id=%d AND certificate_id=%d AND user_id=%d", $taking->ID, $certificate->ID, $taking->user_id));
        if (empty($user_certificate)) {
            wp_die(__('Such certificate was never earned.', 'watupro'));
        }
        if (!is_user_logged_in() and empty($user_certificate->public_access)) {
            watupro_redirect(wp_login_url(site_url("?watupro_view_certificate=1&taking_id=" . $_GET['taking_id'] . "&id=" . $_GET['id'])));
        }
        if (empty($user_certificate->public_access) and ($taking->user_id != $user_ID or $user_certificate->pending_approval) and !current_user_can(WATUPRO_MANAGE_CAPS)) {
            wp_die(__('You do not have sufficient permissions to access this page', 'watupro') . ' 2');
        }
        $user_id = $taking->user_id;
        // select exam
        $exam = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_EXAMS . " WHERE ID=%d", $taking->exam_id));
        // select grade
        $grade = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_GRADES . " WHERE ID=%d", $taking->grade_id));
        $user_info = get_userdata($user_id);
        if (empty($taking->name)) {
            $name = (empty($user_info->first_name) or empty($user_info->last_name)) ? $user_info->display_name : $user_info->first_name . " " . $user_info->last_name;
        } else {
            $name = $taking->name;
        }
        // replace {{{name-field}}} and {{{email-field}}}
        $name_field = empty($taking->name) ? $name : $taking->name;
        $email_field = empty($taking->email) ? $user_info->user_email : $taking->email;
        $output = str_replace('{{{name-field}}}', $name_field, $output);
        $output = str_replace('{{{email-field}}}', $email_field, $output);
        $output = str_replace("%%GRADE%%", $taking->result, $output);
        $output = str_replace("%%GTITLE%%", $grade->gtitle, $output);
        $output = str_replace("%%GDESC%%", stripslashes($grade->gdescription), $output);
        $output = str_replace("%%QUIZ_NAME%%", $exam->name, $output);
        $output = str_replace("%%DESCRIPTION%%", stripslashes($exam->description), $output);
        $output = str_replace("%%USER_NAME%%", $name, $output);
        $output = str_replace("%%EMAIL%%", $email_field, $output);
        $output = str_replace("%%POINTS%%", $taking->points, $output);
        $taken_date = date(get_option('date_format'), strtotime($taking->date));
        $output = str_replace("%%DATE%%", $taken_date, $output);
        $output = str_replace("%%ID%%", sprintf('%04d', $user_certificate->ID), $output);
        $output = watupro_parse_answerto($output, $taking->ID);
        $output = apply_filters('watupro_content', $output);
    }
    if (get_option('watupro_generate_pdf_certificates') == "1") {
        $pdf_engine = get_option('watupro_pdf_engine');
        // $test_mode = 1;
        // generate through docRaptor
        if (empty($pdf_engine) or $pdf_engine == 'docraptor') {
            if (empty($user_certificate->pdf_output)) {
                $api_key = get_option('watupro_docraptor_key');
                $test_mode = get_option('watupro_docraptor_test_mode');
                include_once WATUPRO_PATH . '/lib/docraptor/DocRaptor.class.php';
                $docraptor = new DocRaptor($api_key);
                $docraptor->setDocumentContent($output)->setDocumentType('pdf')->setTest($test_mode)->setName('certificate.pdf');
                $content = $docraptor->fetchDocument();
                // store in DB to avoid more queries
                $wpdb->query($wpdb->prepare("UPDATE " . WATUPRO_USER_CERTIFICATES . " SET pdf_output = %s WHERE ID = %d", $content, $user_certificate->ID));
            } else {
                $content = $wpdb->get_var($wpdb->prepare("SELECT BINARY pdf_output FROM " . WATUPRO_USER_CERTIFICATES . " WHERE ID=%d", $user_certificate->ID));
            }
        }
        if (!empty($pdf_engine) and $pdf_engine = 'pdf-bridge') {
            $output = '<html>
			<head><title>' . $certificate->title . '</title>
			<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>
			<body>' . $output . '</body>
			</html>';
            //	die($output);
            $content = apply_filters('pdf-bridge-convert', $output);
            echo $content;
            exit;
        }
        header("Content-Length: " . strlen($content));
        header('Content-type: application/pdf');
        echo $content;
        exit;
    }
    // end pdf certificate
    // else output HTML
    ?>
	<html>
	<head><title><?php 
    echo $certificate->title;
    ?>
</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>
	<body><?php 
    echo $output;
    ?>
</body>
	</html>
	<?php 
    exit;
}
Esempio n. 2
0
$user_name = empty($_POST['taker_name']) ? $user_identity : $_POST['taker_name'];
if (empty($user_name)) {
    $user_name = __('Guest', 'watupro');
}
// replace grade and gdesc first so any variables used in them can be replaced after that
$exam->final_screen = str_replace(array('%%GRADE%%', '%%GDESC%%'), array(wpautop($grade, false), wpautop(stripslashes(@$grade_obj->gdescription), false)), $exam->final_screen);
$exam->email_output = str_replace(array('%%GRADE%%', '%%GDESC%%'), array(wpautop($grade, false), wpautop(stripslashes(@$grade_obj->gdescription), false)), $exam->email_output);
// prepare output
$replace_these = array('%%CORRECT%%', '%%TOTAL%%', '%%PERCENTAGE%%', '%%RATING%%', '%%CORRECT_ANSWERS%%', '%%QUIZ_NAME%%', '%%DESCRIPTION%%', '%%POINTS%%', '%%CERTIFICATE%%', '%%GTITLE%%', '%%UNRESOLVED%%', '%%ANSWERS%%', '%%CATGRADES%%', '%%DATE%%', '%%EMAIL%%', '%%MAX-POINTS%%', '%%watupro-share-url%%', '%%TIME-SPENT%%', '%%USER-NAME%%', '%%AVG-POINTS%%', '%%AVG-PERCENT%%');
$with_these = array($score, $total, $percent, $rating, $score, stripslashes($exam->name), wpautop(stripslashes($exam->description)), $achieved, $certificate, stripslashes(@$grade_obj->gtitle), $unresolved_questions, $result, $catgrades, date(get_option('date_format'), current_time('timestamp')), $user_email, $max_points, $share_url, $time_spent, $user_name, $avg_points, $avg_percent);
// Show the results
$output = "<div id='startOutput'>&nbsp;</div>";
$output .= str_replace($replace_these, $with_these, wpautop(stripslashes($exam->final_screen), false));
$output = watupro_parse_answerto($output, $taking_id);
$email_output = str_replace($replace_these, $with_these, wpautop(stripslashes($exam->email_output), false));
$email_output = watupro_parse_answerto($email_output, $taking_id);
// store this taking
$_watu->update_taking($taking_id, $achieved, $grade, $output, $percent, $grade_obj, $catgrades);
// send API call
do_action('watupro_completed_exam', $taking_id);
if (watupro_intel() and !empty($exam->fee) and !empty($exam->pay_always)) {
    do_action('watupro_completed_paid_exam', $taking_id, $exam);
}
$output = apply_filters('watupro_content', $output);
$email_output = apply_filters('watupro_content', $email_output);
// show output on the screen
if (empty($do_redirect)) {
    print WatuPRO::cleanup($output, 'web');
} else {
    echo "WATUPRO_REDIRECT:::" . $do_redirect;
}