File: /var/www/selfanalyse/counselor/aptitude-test-excel.php
<?php
include("../wp-config.php");
include_once('sten_score_functions.php');
global $wpdb;
$PSY_EXAMS = $wpdb->prefix . PSY_EXAMS;
$PSY_STATES = $wpdb->prefix . PSY_STATES;
$PSY_ABILITY = $wpdb->prefix . PSY_ABILITY;
$PSY_ABILITY_CLASS = $wpdb->prefix . PSY_ABILITY_CLASS;
$PSY_USERS_DETAILS = $wpdb->prefix . PSY_USERS_DETAILS;
$PSY_TEST_GEN_TEST_TAKEN = $wpdb->prefix . PSY_TEST_GEN_TEST_TAKEN;
$type = $_REQUEST['type'];
$offset = $_REQUEST['offset'];
$total = $_REQUEST['total'];
if($type == "APT"){
$file = "Aptitude Test";
} else if($type == "PER"){
$file = "Personality Test";
}
$getTopics = $wpdb->get_results("SELECT ab.ability_id,ab.short_name,ab.ref_id,e.category_name from $PSY_EXAMS as e LEFT JOIN $PSY_ABILITY as ab on ab.ref_id=e.exam_category_id where e.parent_id='48282' order by e.exam_category_id");
$csv_output = 'Sr. No., Student name, Student Email, Student Mobile,Class, Date of Birth ,Gender, State , Test Date , Verbal Ability, Clerical Ability, Reasoning Ablility, Numerical Ability, Spatial Ability, Closure Ability, Mechanical Ability';
$csv_output .= "\n";
$getDataQuery = "SELECT t.test_taken_id, t.class_id,t.exam_category_id, t.start_date, t.test_type, u.first_name, u.mobile_number, u.email_address, e.category_name, u.date_of_birth, u.gender_id,s.state_name,cl.class_name FROM $PSY_TEST_GEN_TEST_TAKEN as t LEFT JOIN $PSY_USERS_DETAILS as u ON u.user_id=t.user_id left join $PSY_ABILITY_CLASS as cl on cl.class_id=u.current_education_level LEFT JOIN $PSY_EXAMS as e ON e.exam_category_id=t.exam_category_id LEFT JOIN $PSY_STATES as s on s.state_id=u.state_id where t.status='Y' AND t.test_type='APT' order by t.test_taken_id desc limit $offset,$total";
$getData = $wpdb->get_results($getDataQuery);
if( sizeof($getData) != 0)
{
$i = $offset+1;
foreach ($getData as $vd)
{
$s = $vd->date_of_birth;
$dt = new DateTime($s);
$date = $dt->format('d-m-Y');
$srno=$i+$indexN;
$csv_output .= $i . ',';
$csv_output .= $vd->first_name . ',';
$csv_output .= $vd->email_address . ',';
$csv_output .= $vd->mobile_number . ',';
$csv_output .= $vd->class_name . ',';
$csv_output .= $date . ',';
$csv_output .= $vd->gender_id . ',';
$csv_output .= $vd->state_name . ',';
$csv_output .= $vd->start_date . ',';
$getsubject = $wpdb->get_results("SELECT e.category_name,e.exam_category_id from $PSY_EXAMS as e LEFT JOIN $PSY_ABILITY as ab on ab.ref_id=e.exam_category_id where e.parent_id='$vd->exam_category_id' order by e.exam_category_id");
$rawScore = '';
foreach ($getTopics as $sub) {
$subject = $sub->ref_id;
$ability_id = $sub->ability_id;
$class_id = $vd->class_id;
$test_id = $vd->test_taken_id;
$rawscore = aptitudeScores($subject,$ability_id,$class_id,$test_id);
$csv_output .= $rawscore . ',';
}
$csv_output .= "\n";
$contents = strip_tags($csv_output);
$i++;
}
} else {
$csv_output = "No Record Found!";
}
$filename = $file;
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: filename=" . $filename . ".csv");
//print chr(255) . chr(254) . mb_convert_encoding($csv_output, 'UTF-16LE', 'UTF-8');
print $csv_output;
exit;
?>