File: /var/www/selfanalyse/emu-aptitude-test/test-report.php
<?php
/**
*
* Title : Test Report
* Developer : Rohit Kashyap
* Date : 04 Mar,2020
* Description : User test report and their sten score with highchart.
*/
// ini_set('dsiplay_errors', 1);
// error_reporting(E_ALL);
include_once('../wp-config.php');
if (is_user_logged_in()) {
$user_id = $current_user->ID;
global $wpdb;
$PSY_SP_QUESTION = $wpdb->prefix . PSY_SP_QUESTION;
$PSY_EXAMS = $wpdb->prefix . PSY_EXAMS;
$PSY_ABILITY = $wpdb->prefix . PSY_ABILITY;
$PSY_ABILITY_CLASS = $wpdb->prefix . PSY_ABILITY_CLASS;
$PSY_USERS_DETAILS = $wpdb->prefix . PSY_USERS_DETAILS;
$PSY_ABILITY_RAW_SCORE = $wpdb->prefix . PSY_ABILITY_RAW_SCORE;
$PSY_TEST_GEN_TEST_TAKEN = $wpdb->prefix . PSY_TEST_GEN_TEST_TAKEN;
$PSY_TEST_GEN_TEST_TAKEN_QUESTION_ANSWER = $wpdb->prefix . PSY_TEST_GEN_TEST_TAKEN_QUESTION_ANSWER;
$PSY_API_REQUEST_DATA = $wpdb->prefix . PSY_API_REQUEST_DATA;
$testID = $_REQUEST['testId'];
$curDate = date('Y-m-d H:i:s');
if (is_numeric($testID) && strlen($testID) <= 5) {
$checkTest = $wpdb->get_var("SELECT count(test_taken_id) FROM $PSY_TEST_GEN_TEST_TAKEN where test_taken_id='$testID' and user_id='$user_id' and test_type='EMU-APT' and status='Y'");
if ($checkTest != '0') {
$userData = $wpdb->get_row("SELECT ud.first_name,ud.last_name,ud.date_of_birth FROM $PSY_USERS_DETAILS as ud where user_id='$user_id'");
$studentName = $userData->first_name . ' ' . $userData->last_name;
$dob = date('d F, Y', strtotime($userData->date_of_birth));
$getTestDetails = $wpdb->get_row("SELECT tt.total_questions,tt.class_id,tt.exam_category_id,tt.reference_id,tt.time_taken_in_seconds,tt.start_date,e.category_name,cl.class_name FROM $PSY_TEST_GEN_TEST_TAKEN as tt LEFT JOIN $PSY_EXAMS as e on e.exam_category_id=tt.exam_category_id LEFT JOIN $PSY_ABILITY_CLASS as cl on cl.class_id=tt.class_id where tt.test_taken_id='$testID' and tt.status='Y'");
$class_id = $getTestDetails->class_id != '0' ? $getTestDetails->class_id : '9';
$class = $getTestDetails->class_name;
$dateAttempt = date('d F, Y', strtotime($getTestDetails->start_date));
$seconds = $getTestDetails->time_taken_in_seconds;
$testDate = date('Y-m-d H:i:s', strtotime($getTestDetails->start_date));
$date1 = str_replace('-', '/', $testDate);
$tomorrow = date('Y-m-d H:i:s', strtotime($date1 . '+1 days'));
$exam_id = $getTestDetails->exam_category_id;
$subs_id = $getTestDetails->reference_id;
$emuUserdetail = $wpdb->get_row("SELECT emu_unique_user_id, emu_payment_ref_id FROM $PSY_API_REQUEST_DATA where subs_id='$subs_id'");
$emu_student_id = $emuUserdetail->emu_unique_user_id;
$emu_ref_id = $emuUserdetail->emu_payment_ref_id;
$getsubject = $wpdb->get_results("SELECT e.category_name,e.exam_category_id from $PSY_EXAMS as e where e.parent_id='$exam_id' order by e.exam_category_id");
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<title>Aptitude Test Report - ThinkEmu</title>
<link rel="icon" href="/wp-content/themes/psychometrics/images/emu.ico" type="image/x-icon" />
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css">
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/highcharts.js"></script>
<script src="js/highcharts-3d.js"></script>
</head>
<style>
@import url(https://fonts.googleapis.com/css?family=Lato);
body {
font-family: 'Lato', sans-serif !important;
}
.bold-txt {
font-weight: bold;
color: #000;
}
.col-md-2 {
width: 13.666667% !important;
}
/* MARGIN CSS */
.mt-20 {
margin-top: 20px
}
.mb-20 {
margin-bottom: 20px
}
table {
border-collapse: collapse;
border-spacing: 0;
width: 100%;
border: 1px solid #ddd;
}
th,
td {
text-align: left;
padding: 8px;
}
.header-logo {
float: right;
}
.chart-header-box {
float: left;
width: 100%;
border-radius: 4px;
background: #fff;
padding: 10px;
border-bottom: 1px solid #cecece;
}
.chart-header-box h4 {
font-size: 16px;
}
.intro-box {
margin: 0;
padding-left: 14px;
background: #fff;
padding: 10px;
float: left;
width: 100%;
}
.intro-box h2 {
margin: 0;
color: #464646;
font-weight: 600;
font-size: 24px;
padding: 0 0 20px;
}
.one-circle-box {
border-radius: 50%;
text-align: center;
padding: 24px 0;
width: 140px;
margin: 0 auto;
}
.one-circle-box i {
font-size: 28px;
}
.chart-box {
border: 1px solid #cccccc;
box-shadow: 0 2px 4px 2px #c7c7c7;
margin: 2em 0;
}
#container {
/* height: 550px; */
}
.highcharts-data-table table {
font-family: Verdana, sans-serif;
border-collapse: collapse;
border: 1px solid #EBEBEB;
margin: 10px auto;
text-align: center;
width: 100%;
max-width: 500px;
}
.highcharts-data-table caption {
padding: 1em 0;
font-size: 1.2em;
color: #555;
}
.highcharts-data-table th {
font-weight: 600;
padding: 0.5em;
}
.highcharts-data-table td,
.highcharts-data-table th,
.highcharts-data-table caption {
padding: 0.5em;
}
.highcharts-data-table thead tr,
.highcharts-data-table tr:nth-child(even) {
background: #f8f8f8;
}
.highcharts-data-table tr:hover {
background: #f1f7ff;
}
svg:not(:root) {
overflow: visible !important;
}
.highcharts-container {
overflow: visible !important;
}
.intro-box h5 {
margin: 0;
color: #fff;
font-size: 18px;
padding: 10px;
text-align: center;
background: #f1802b;
}
.ability-desc-box h2 {
margin: 0;
color: #464646;
font-weight: 600;
font-size: 20px;
padding: 30px 0 10px;
}
.intro-box p {
margin: 0;
font-size: 16px;
}
.ability-desc-box {
border-bottom: 1px solid #434348;
padding-bottom: 20px;
}
.sten-score {
border: 1px solid #b1b1b1;
box-shadow: 0 2px 4px 2px #9a9a9a;
padding: 10px;
margin-top: 1em;
/* background: #90ed7d; */
}
.sten-score h3 {
font-size: 18px;
margin: 0;
padding-bottom: 10px;
}
.description-ul {
padding: 20px;
}
.description-ul li {
list-style: decimal;
color: #000;
margin-bottom: 20px;
font-size: 15px;
}
.test-head {
font-size: 26px !important;
padding-bottom: 10px;
}
.chart-header-box h6 {
margin: 0;
font-size: 17px;
padding: 10px 0 30px;
line-height: 24px;
font-weight: 600;
}
.back-home-btn {
font-size: 14px;
background: #0085f7;
padding: 10px;
display: table;
margin: 0 auto;
color: #fff;
cursor: pointer;
border-radius: 4px;
font-weight: bold;
text-decoration: none;
}
.fb-share {
background: #3b5998;
border-radius: 3px;
font-weight: 600;
padding: 5px 8px;
float: right;
position: relative;
top: -4px;
display: inline-flex;
}
.fb-share:hover {
cursor: pointer;
background: #213A6F
}
.fb-share svg {
width: 18px;
fill: white;
vertical-align: middle;
border-radius: 2px
}
.fb-share span {
vertical-align: middle;
color: white;
font-size: 14px;
padding: 0 3px
}
/* aptitude-test CSS */
#btnBack {
float: left;
margin-bottom: 12px;
background: #f53e10;
border: none;
color: #fff;
padding: 4px 10px;
font-weight: 600;
border-radius: 4px;
}
#btnPrint {
float: right;
margin-bottom: 12px;
background: #154479;
border: none;
color: #fff;
padding: 4px 10px;
font-weight: 600;
border-radius: 4px;
}
/* END aptitude-test CSS */
</style>
<body>
<!-- TOP TABLE SECTION -->
<section style="background: #f1f1f1;padding: 2em 0;">
<div class="container">
<!----User details--->
<div class="row">
<div class="chart-header-box" style="margin-bottom: 2em;">
<div class="col-md-8">
<h4 class="test-head"><?php echo $getTestDetails->category_name; ?></h4>
<h4>Name : <span style="font-weight: bold;"> <?php echo $studentName; ?> </span>, <span> Class :</span> <span style="font-weight: bold;"><?php echo $class ?></span></h4>
<h4>Date : <span style="font-weight: bold;"><?php echo $dateAttempt; ?></span></h4>
</div>
<div class="col-md-4">
<img class="header-logo" src="https://selfanalyse.com/wp-content/themes/psychometrics/images/emu-logo.png" alt="">
</div>
</div>
</div>
<div class="chart-header-box" style="background: #d8e6f1;text-align: center;">
<h6>Your reports will be created by our experts and can be seen on your EMU dashboard in an hour.</h6>
<a href="https://thinkemu.com/psychometric-test-results" style="text-decoration: none;">
<h3 class="back-home-btn"><i class="fas fa-angle-double-left"></i> Back to Profile</h3>
</a>
</div>
<?php
$data = array();
$string = array();
foreach ($getsubject as $sub) {
$subject = $sub->exam_category_id;
$abilityData = $wpdb->get_row("SELECT ability_id, instruction, color FROM $PSY_ABILITY where FIND_IN_SET($subject,ref_id)");
$ability_id = $abilityData->ability_id;
$ability_desc = $abilityData->instruction;
$color = $abilityData->color;
$getTestSummary = $wpdb->get_row("SELECT count(case when ttqt.correct_answer=ttqat.answer then 1 end) as correctQn, count(case when ttqt.correct_answer!=ttqat.answer and ttqat.answer!=0 then 1 end) as inCorrectQn, count(case when ttqat.answer=0 then 1 end) as unAttemptQn from $PSY_TEST_GEN_TEST_TAKEN_QUESTION_ANSWER as ttqat Inner join $PSY_SP_QUESTION as ttqt on ttqt.question_id = ttqat.test_question_id where ttqat.test_taken_id='$testID' and ttqat.subject_id='$subject'");
$correctQn = (int) $getTestSummary->correctQn;
$inCorrectQn = $getTestSummary->inCorrectQn;
$unAttemptQn = $getTestSummary->unAttemptQn;
$rawScore = $wpdb->get_var("SELECT sten_score FROM $PSY_ABILITY_RAW_SCORE where $correctQn BETWEEN raw_score_start and raw_score_end and class_id='$class_id' and ability_id='$ability_id'");
$stenScore = (int) $rawScore;
//$stenScore;
$data[] = array("y" => $stenScore, "name" => $sub->category_name, "ability_id" => $ability_id, "topic_id" => $sub->exam_category_id, "color" => $color);
//color condition for raw score
if ($rawScore <= 3 && $rawScore >= 0) {
$backColor = '#e4d354';
} else if ($rawScore <= 7 && $rawScore >= 4) {
$backColor = '#f7a35c';
} else if ($rawScore <= 10 && $rawScore >= 8) {
$backColor = '#90ed7d';
}
$percentage = ($stenScore / 10 * 100) . '%';
$showcategory = 'Current Ability - ' . $percentage;
$array = array("y" => $stenScore, "name" => $showcategory, "color" => $color, "text" => 'Current Ability');
$remainValue = 10 - $stenScore;
$remainPercent = ($remainValue / 10 * 100) . '%';
$remainText = 'Area of Improvement - ' . $remainPercent;
$createName = strtolower(str_replace(" ", "-", $sub->category_name));
$image_name = $testID . '-' . $createName . '.png';
?>
<script>
// Load the fonts
Highcharts.createElement('link', {
href: 'https://fonts.googleapis.com/css?family=Ubuntu:400,700',
rel: 'stylesheet',
type: 'text/css'
}, null, document.getElementsByTagName('head')[0]);
// Add the background image to the container
Highcharts.addEvent(Highcharts.Chart, 'afterGetContainer', function() {
// eslint-disable-next-line no-invalid-this
this.container.style.background =
'url(https://www.highcharts.com/samples/graphics/sand.png)';
});
Highcharts.theme = {
chart: {
backgroundColor: null,
style: {
fontFamily: 'Ubuntu, sans-serif'
}
},
title: {
style: {
color: 'black',
fontSize: '16px',
fontWeight: 'bold'
}
},
subtitle: {
style: {
color: 'black'
}
},
tooltip: {
borderWidth: 0
},
labels: {
style: {
color: '#6e6e70'
}
},
legend: {
backgroundColor: '#E0E0E8',
itemStyle: {
fontWeight: 'bold',
fontSize: '13px'
}
},
xAxis: {
labels: {
style: {
color: '#6e6e70'
}
}
},
yAxis: {
labels: {
style: {
color: '#6e6e70'
}
}
},
plotOptions: {
series: {
shadow: true
},
candlestick: {
lineColor: '#404048'
},
map: {
shadow: false
}
},
// Highstock specific
navigator: {
xAxis: {
gridLineColor: '#D0D0D8'
}
},
rangeSelector: {
buttonTheme: {
fill: 'white',
stroke: '#C0C0C8',
'stroke-width': 1,
states: {
select: {
fill: '#D0D0D8'
}
}
}
},
scrollbar: {
trackBorderColor: '#C0C0C8'
}
};
// Apply the theme
Highcharts.setOptions(Highcharts.theme);
//pie chart
var options = {
chart: {
type: 'pie',
options3d: {
enabled: true,
alpha: 45,
beta: 0
},
},
title: {
text: '<?php echo $sub->category_name ?>'
},
accessibility: {
point: {
valueSuffix: '%'
}
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
depth: 35,
dataLabels: {
enabled: true,
format: '{point.text}'
},
showInLegend: true
}
},
legend: {
align: 'center',
verticalAlign: 'bottom',
floating: false,
layout: 'horizontal',
itemStyle: '{ "word-wrap": "break-word" }',
},
series: [{
name: 'Sten Score',
colorByPoint: true,
data: [<?php echo json_encode($array); ?>, {
"y": <?php echo $remainValue; ?>,
"name": '<?php echo $remainText; ?>',
"text": 'Area of Improvement'
}]
}]
}
//var chart = Highcharts.chart('container_<?php echo $ability_id; ?>', options);
</script>
<?php
$path = '/var/www/selfanalyse/emu-aptitude-test/chart_images/' . $image_name;
if (file_exists($path)) {
echo "";
} else {
?>
<script type="text/javascript">
//save image
var data = {
options: JSON.stringify(options),
resources: {
// You can add your CSS styles here
css: ".highcharts-background { fill: #d7d2cc; stroke: #a4edba; stroke-width: 2px} .highcharts-title tspan {font-size: 25px;font-weight: bold;} .highcharts-label text{font-size:16px!important;} .highcharts-legend{font-size:15px;}"
},
width: 400,
height: 600,
filename: 'test.png',
type: 'image/png',
async: true
};
var exportUrl = 'https://export.highcharts.com/';
$.post(exportUrl, data, function(data) {
var imageUrl = exportUrl + data;
console.log(imageUrl)
var urlCreator = window.URL || window.webkitURL;
$.ajax({
type: "POST",
url: "/emu-aptitude-test/save_image.php",
dataType: 'JSON',
data: {
"type": "emu-pie",
"image_name": "<?php echo $image_name ?>",
"image": imageUrl
},
success: function(data) {
}
});
//document.querySelector("#image").src = imageUrl;
//window.location = "https://wa.me/?text=" + encodeURIComponent(imageUrl);
});
</script>
<?php
}
?>
<?php
}
?>
</div>
</section>
<!-- END TOP TABLE SECTION -->
</body>
</html>
<?php
if ($emu_student_id != '') {
/***
* send curl response to emu
*/
// API URL
$url = 'https://thinkemu.com/api/v1/aptitude-test/response';
$link = "https://selfanalyse.com/emu-aptitude-test/print_page.php?testId=$testID&student_id=$user_id";
// Create a new cURL resource
$ch = curl_init($url);
// Setup request to send json via POST
$emuData = array(
'student_id' => $emu_student_id,
'test_id' => $emu_ref_id,
'link' => $link
);
$payload = json_encode($emuData);
$headers = array(
'Content-Type: application/json',
'x-api-key: d6b20680-5fdc-4e4c-855e-8744fd22ccb2'
);
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'x-api-key: d6b20680-5fdc-4e4c-855e-8744fd22ccb2'
));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
curl_close($ch);
$resultResponse = json_decode($result);
}
} else {
?>
<script>
window.location.href = 'http://www.thinkemu.com'
</script>
<?php
}
} else {
?>
<script>
window.location.href = 'http://www.thinkemu.com'
</script>
<?php
}
} else {
?>
<script>
window.location.href = 'http://www.thinkemu.com'
</script>
<?php
}
?>