<?php
/*!
* Copyright Anucha Puangpaka
* Contacts anucha.ppk@icloud.com 062-479-9836
*/
include('../../config.php');
$sql = "select * from ap_settings";
$query = mysqli_query($conn,$sql);
$rows = mysqli_fetch_array($query);
$title = $rows['title'];
$logo = $rows['logo'];
if($_GET['nu']==""){
if($_GET['xcode']==""){
echo "<meta http-equiv='refresh' content='0;URL=$ap_site$ap_folder' />";
echo "Please wait ...";
}else{
$sql = "select * from ap_products where type='$_GET[type]' and code='$_GET[xcode]'";
$query = mysqli_query($conn,$sql);
$rows = mysqli_fetch_array($query);
$today = date('Y-m-d H:i:s');
$ap_expire = "หมดอายุวันที่ ";
require_once('../../mpdf/mpdf.php');
$header = "
<table width=\"100%\" style=\"border-bottom: 1px solid #000000; color: #000088;\">
<tr>
<td align='center'><img src='$logo' width='70px' border='0' /></td>
</tr>
<tr>
<td align='center'><font style='font-weight:bold'>แบบสอบถาม</font></td>
</tr>
<tr>
<td align='center'>$rows[name]</td>
</tr>
<tr>
<td align='center'>$title</td>
</tr>
</table>";
$html = "<table width=\"100%\" style=\"line-height: 18pt; border-bottom: 0px solid #000000;\">
<tr>
<td height='80'></td>
</tr>
<tr>
<td height='50'></td>
</tr>
<tr>
<td align='center'><img src='$ap_site$ap_folder"."ap/qrcode/qrcode.php?data=$ap_site$ap_folder"."ap/questionnaire/?xcode=$rows[code]&size=156x158&logo=$logo' border='0' /></td>
</tr>
<tr>
<td align='center'>$rows[code]</td>
</tr>
<tr>
<td height='50'></td>
</tr>
<tr>
<td> ด้วย$title เป็นหน่วยงานสนับสนุนที่ให้บริการด้านสารสนเทศ ของมหาวิทยาลัยราชภัฏกำแพงเพชร ทั้งนี้เพื่อประเมินผล$rows[name] โดยจะนำผลการประเมินมาปรับปรุงการบริการให้ดียิ่งขึ้น</td>
</tr>
<tr>
<td> ในการนี้$title จึงขอความอนุเคราะห์ผู้ใช้บริการตอบแบบสอบถามความพึงพอใจของผู้รับบริการ ผ่านระบบ QRCODE เพื่อ$title จะได้ดำเนินการต่อไป</td>
</tr>
<tr>
<td height='20'></td>
</tr>
<tr>
<td><p>$today</p></td>
</tr>
</table>";
$footer = "
<table width=\"100%\" style=\"border-top: 1px solid #000000;\">
<tr>
<td valign='bottom'><img src='$ap_site$ap_folder"."ap/qrcode/qrcode.php?data=$ap_site$ap_folder&size=78x78&logo=$logo' border='0' /></td>
<td valign='bottom' align=\"right\">
<p><font style='color:#000000'>Date : ".date('Y-m-d H:i:s')."</font></p>
<p><font style='color:red'>Expires on : ".date('Y-m-d H:i:s',strtotime("$today +1 month"))."</font></p></td>
</tr>
</table>";
$pdf = new mPDF('th', 'A4', '0', '', 20, 15, 30, 35);
$pdf->mirrorMargins = 1;
$pdf->SetDisplayMode('fullpage');
$stylesheet = file_get_contents('../../theme.css');
$pdf->WriteHTML($stylesheet,1);
$pdf->SetHTMLHeader($header);
$pdf->SetHTMLHeader($header,'E');
$pdf->SetHTMLFooter($footer);
$pdf->SetHTMLFooter($footer,'E');
/*$pdf->SetWatermarkText("$ap_watermark");
$pdf->watermark_font = 'DejaVuSansCondensed';
$pdf->SetWatermarkImage('../../contents/images/arit-logo.png');
$pdf->showWatermarkImage = true;
$pdf->watermarkImageAlpha = 0.03;*/
$pdf->SetTitle("$rows[name]");
$pdf->SetAuthor("Anucha Puangpaka");
$pdf->SetCreator("Anucha Puangpaka");
$pdf->SetSubject("$rows[name]");
$pdf->SetKeywords("$rows[name]");
$pdf->WriteHTML($html, 2);
$pdf->Output();
}
}
if($_GET['nu']=="print"){
if($_GET['xcode']==""){
echo "<meta http-equiv='refresh' content='0;URL=$ap_site$ap_folder' />";
echo "Please wait ...";
}else{
$sql = "select * from ap_products where code='$_GET[xcode]'";
$query = mysqli_query($conn,$sql);
$rows = mysqli_fetch_array($query);
$num_rows = mysqli_num_rows($query);
$questionnaire_name = $rows['name'];
$sqla = "select * from ap_questionnaire_answer where code='$_GET[xcode]' group by code_answer";
$querya = mysqli_query($conn,$sqla);
$num_rowsa = mysqli_num_rows($querya);
$today = date('Y-m-d H:i:s');
$ap_expire = "หมดอายุวันที่ ";
require_once('../../mpdf/mpdf.php');
$header = "
<table width=\"100%\" style=\"border-bottom: 1px solid #000000; color: #000088;\">
<tr>
<td align='center'><img src='$logo' width='70px' border='0' /></td>
</tr>
<tr>
<td align='center'><font style='font-weight:bold'>สรุปผล</font></td>
</tr>
<tr>
<td align='center'>$questionnaire_name</td>
</tr>
<tr>
<td align='center'>$title</td>
</tr>
</table>";
$html = "
<div class=\"table-responsive\">
<table width=\"100%\" class=\"table table-striped style4\" border='1' cellpadding=\"3\" cellspacing=\"0\">
";
$sql = "select * from ap_questionnaire_part where code='$rows[code]' order by code_part asc";
$query = mysqli_query($conn,$sql);
while($rows = mysqli_fetch_array($query)){
$html .= "<tr><td bgcolor='#cccccc' colspan='3'><strong>$rows[name]</strong></td></tr>";
//$html .= "<tr><td colspan='3'>$rows[details]</td></tr>";
$sql_2 = "select * from ap_questionnaire_question where code='$rows[code]' and code_part='$rows[code_part]' order by code_question asc";
$query_2 = mysqli_query($conn,$sql_2);
$num_rows_2 = mysqli_num_rows($query_2);
$i = 0;
$i2 = 0;
$i3 = 0;
while($rows_2 = mysqli_fetch_array($query_2)){
$i3++;
$sql_answer_ = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]')";
$query_answer_ = mysqli_query($conn,$sql_answer_);
$i4 = 0;
while($rows_answer_ = mysqli_fetch_array($query_answer_)){
$i4++;
if(!empty($rows_answer_['open_ended'])){
$open_ended .= "$i4) $rows_answer_[open_ended]<br>";
}
}
$sql_answer = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='1'";
$query_answer = mysqli_query($conn,$sql_answer);
$num_rows_answer = mysqli_num_rows($query_answer);
$sql_answer2 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='2'";
$query_answer2 = mysqli_query($conn,$sql_answer2);
$num_rows_answer2 = mysqli_num_rows($query_answer2);
$sql_answer3 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='3'";
$query_answer3 = mysqli_query($conn,$sql_answer3);
$num_rows_answer3 = mysqli_num_rows($query_answer3);
$sql_answer4 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='4'";
$query_answer4 = mysqli_query($conn,$sql_answer4);
$num_rows_answer4 = mysqli_num_rows($query_answer4);
$sql_answer5 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='5'";
$query_answer5 = mysqli_query($conn,$sql_answer5);
$num_rows_answer5 = mysqli_num_rows($query_answer5);
$sql_answer6 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='6'";
$query_answer6 = mysqli_query($conn,$sql_answer6);
$num_rows_answer6 = mysqli_num_rows($query_answer6);
$sql_answer7 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='7'";
$query_answer7 = mysqli_query($conn,$sql_answer7);
$num_rows_answer7 = mysqli_num_rows($query_answer7);
$sql_answer8 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='8'";
$query_answer8 = mysqli_query($conn,$sql_answer8);
$num_rows_answer8 = mysqli_num_rows($query_answer8);
$sql_answer9 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='9'";
$query_answer9 = mysqli_query($conn,$sql_answer9);
$num_rows_answer9 = mysqli_num_rows($query_answer9);
$sql_answer10 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='10'";
$query_answer10 = mysqli_query($conn,$sql_answer10);
$num_rows_answer10 = mysqli_num_rows($query_answer10);
$sql_answer11 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='11'";
$query_answer11 = mysqli_query($conn,$sql_answer11);
$num_rows_answer11 = mysqli_num_rows($query_answer11);
$sql_answer12 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='12'";
$query_answer12 = mysqli_query($conn,$sql_answer12);
$num_rows_answer12 = mysqli_num_rows($query_answer12);
$sql_answer13 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='13'";
$query_answer13 = mysqli_query($conn,$sql_answer13);
$num_rows_answer13 = mysqli_num_rows($query_answer13);
$sql_answer14 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='14'";
$query_answer14 = mysqli_query($conn,$sql_answer14);
$num_rows_answer14 = mysqli_num_rows($query_answer14);
$sql_answer15 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and choice='15'";
$query_answer15 = mysqli_query($conn,$sql_answer15);
$num_rows_answer15 = mysqli_num_rows($query_answer15);
$sql_answer_2 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and level='1'";
$query_answer_2 = mysqli_query($conn,$sql_answer_2);
$num_rows_answer_2 = mysqli_num_rows($query_answer_2);
$sql_answer_2_2 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and level='2'";
$query_answer_2_2 = mysqli_query($conn,$sql_answer_2_2);
$num_rows_answer_2_2 = mysqli_num_rows($query_answer_2_2);
$sql_answer_2_3 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and level='3'";
$query_answer_2_3 = mysqli_query($conn,$sql_answer_2_3);
$num_rows_answer_2_3 = mysqli_num_rows($query_answer_2_3);
$sql_answer_2_4 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and level='4'";
$query_answer_2_4 = mysqli_query($conn,$sql_answer_2_4);
$num_rows_answer_2_4 = mysqli_num_rows($query_answer_2_4);
$sql_answer_2_5 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and level='5'";
$query_answer_2_5 = mysqli_query($conn,$sql_answer_2_5);
$num_rows_answer_2_5 = mysqli_num_rows($query_answer_2_5);
$sql_answer_21 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and need='1'";
$query_answer_21 = mysqli_query($conn,$sql_answer_21);
$num_rows_answer_21 = mysqli_num_rows($query_answer_21);
$sql_answer_21_2 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and need='2'";
$query_answer_21_2 = mysqli_query($conn,$sql_answer_21_2);
$num_rows_answer_21_2 = mysqli_num_rows($query_answer_21_2);
$sql_answer_21_3 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and need='3'";
$query_answer_21_3 = mysqli_query($conn,$sql_answer_21_3);
$num_rows_answer_21_3 = mysqli_num_rows($query_answer_21_3);
$sql_answer_21_4 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and need='4'";
$query_answer_21_4 = mysqli_query($conn,$sql_answer_21_4);
$num_rows_answer_21_4 = mysqli_num_rows($query_answer_21_4);
$sql_answer_21_5 = "select * from ap_questionnaire_answer where (code='$rows[code]' and code_question='$rows_2[id]') and need='5'";
$query_answer_21_5 = mysqli_query($conn,$sql_answer_21_5);
$num_rows_answer_21_5 = mysqli_num_rows($query_answer_21_5);
$qid = $rows_2[id];
if($rows_2['format']==1){
$i++;
$html .= "<tr><td bgcolor='#E59866'><b>$i. $rows_2[title]</b></td><td bgcolor='#E59866'><b>จำนวน</b></td><td bgcolor='#E59866'><b>ค่าร้อยละ</b></td></tr>";
if(!empty($rows_2[choice_1])){
$html .= "<tr><td>$rows_2[choice_1]</td><td>$num_rows_answer</td><td>".number_format(($num_rows_answer*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_2])){
$html .= "<tr><td>$rows_2[choice_2]</td><td>$num_rows_answer2</td><td>".number_format(($num_rows_answer2*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_3])){
$html .= "<tr><td>$rows_2[choice_3]</td><td>$num_rows_answer3</td><td>".number_format(($num_rows_answer3*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_4])){
$html .= "<tr><td>$rows_2[choice_4]</td><td>$num_rows_answer4</td><td>".number_format(($num_rows_answer4*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_5])){
$html .= "<tr><td>$rows_2[choice_5]</td><td>$num_rows_answer5</td><td>".number_format(($num_rows_answer5*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_6])){
$html .= "<tr><td>$rows_2[choice_6]</td><td>$num_rows_answer6</td><td>".number_format(($num_rows_answer6*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_7])){
$html .= "<tr><td>$rows_2[choice_7]</td><td>$num_rows_answer7</td><td>".number_format(($num_rows_answer7*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_8])){
$html .= "<tr><td>$rows_2[choice_8]</td><td>$num_rows_answer8</td><td>".number_format(($num_rows_answer8*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_9])){
$html .= "<tr><td>$rows_2[choice_9]</td><td>$num_rows_answer9</td><td>".number_format(($num_rows_answer9*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_10])){
$html .= "<tr><td>$rows_2[choice_10]</td><td>$num_rows_answer10</td><td>".number_format(($num_rows_answer10*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_11])){
$html .= "<tr><td>$rows_2[choice_11]</td><td>$num_rows_answer11</td><td>".number_format(($num_rows_answer11*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_12])){
$html .= "<tr><td>$rows_2[choice_12]</td><td>$num_rows_answer12</td><td>".number_format(($num_rows_answer12*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_13])){
$html .= "<tr><td>$rows_2[choice_13]</td><td>$num_rows_answer13</td><td>".number_format(($num_rows_answer13*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_14])){
$html .= "<tr><td>$rows_2[choice_14]</td><td>$num_rows_answer14</td><td>".number_format(($num_rows_answer14*100)/$num_rowsa,2)."%</td></tr>";
}
if(!empty($rows_2[choice_15])){
$html .= "<tr><td>$rows_2[choice_15]</td><td>$num_rows_answer15</td><td>".number_format(($num_rows_answer15*100)/$num_rowsa,2)."%</td></tr>";
}
}
if($rows_2['format']==2){
$i++;
$i2++;
if($i2==1){
$html .= "</table>
</div>
<div class=\"table-responsive\">
<table width=\"100%\" class=\"table table-striped style4\" border='1' cellpadding=\"3\" cellspacing=\"0\">
<thead>
<tr>
<th rowspan=\"2\">รายการ</th>
<th colspan=\"5\"><center>ระดับความพึงพอใจ n=$num_rowsa</center></th>
<th rowspan=\"2\" width='60'>ค่าเฉลี่ย</th>
</tr>
<tr>
<th width='40'>1</th>
<th width='40'>2</th>
<th width='40'>3</th>
<th width='40'>4</th>
<th width='40'>5</th>
</tr>
</thead>
<tbody>
";
}
if($rows_2['title_status']==1){
$x = 0;
$html .= "<tr>
<td bgcolor=\"#E59866\" colspan=\"7\"><strong>$rows_2[title]</strong></td>
</tr>
";
}else{
if(empty($rows_2['format_status'])){
if($rows_2['title_status']==1){
$i2 = 0;
}
$total = (($num_rows_answer_2*1)+($num_rows_answer_2_2*2)+($num_rows_answer_2_3*3)+($num_rows_answer_2_4*4)+($num_rows_answer_2_5*5))/$num_rowsa;
$totalx += $total;
$x++;
$code_question = substr($rows_2['code_question'],0,1);
$sql_3 = "select * from ap_questionnaire_question where (code='$rows_2[code]' and code_part='$rows_2[code_part]') and code_question like '$code_question%' order by code_question asc";
$query_3 = mysqli_query($conn,$sql_3);
$num_rows_3 = mysqli_num_rows($query_3);
$html .= "<tr>
<td>$rows_2[title]</td>
<td align='center'>$num_rows_answer_2</td>
<td align='center'>$num_rows_answer_2_2</td>
<td align='center'>$num_rows_answer_2_3</td>
<td align='center'>$num_rows_answer_2_4</td>
<td align='center'>$num_rows_answer_2_5</td>
<td align='center'>".number_format($total,4)."</td>
</tr>";
if($x==($num_rows_3-1)){
$html .= "<tr class=\"warning\">
<td align='center'><strong>รวม</strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td align='center'>".number_format(($totalx/($num_rows_3-1)),4)."</td>
</tr>";// number_format(($totalx/($num_rows_3-1)),4)
$totalx = 0;
}
}else{
$i2 = 0;
$html .= "<tr><td colspan=\"6\"><lable><b>$rows_2[title]</b></label>";
$html .= "$rows_answer_[open_ended]</td></tr>";
}
}
if($i==$num_rows_2){
$html .= "</tbody>
</table>
</div>
<br>
<div class=\"table-responsive style4\">";
}
}
if($rows_2['format']==3){
$i++;
if($rows_2['title']!=""){
$html .= $rows_2['title'];
}
$html .= "<br>$open_ended";
}
}
}
$html .= "
</div>
<table width=\"100%\" style=\"line-height: 18pt; border-bottom: 0px solid #000000;\">
<tr>
<td height='30'></td>
</tr>
<tr>
<td align='right'>$today</td>
</tr>
</table>
";
$footer = "
<table width=\"100%\" style=\"border-top: 1px solid #000000;\">
<tr>
<td valign='bottom' align=\"right\">
<font style='color:#000000;font-size:10px'>Date : ".date('Y-m-d H:i:s')."</font> / <font style='color:red;font-size:10px'>Expires on : ".date('Y-m-d H:i:s',strtotime("$today +1 month"))."</font></p></td>
</tr>
</table>";
$pdf = new mPDF('th', 'A4', '0', '', 20, 15, 50, 35);
$pdf->mirrorMargins = 1;
$pdf->SetDisplayMode('fullpage');
$stylesheet = file_get_contents('../../theme.css');
$pdf->WriteHTML($stylesheet,1);
$pdf->SetHTMLHeader($header);
$pdf->SetHTMLHeader($header,'E');
$pdf->SetHTMLFooter($footer);
$pdf->SetHTMLFooter($footer,'E');
/*$pdf->SetWatermarkText("$ap_watermark");
$pdf->watermark_font = 'DejaVuSansCondensed';
$pdf->SetWatermarkImage('../../contents/images/arit-logo.png');
$pdf->showWatermarkImage = true;
$pdf->watermarkImageAlpha = 0.03;*/
$pdf->SetTitle("$rows[name]");
$pdf->SetAuthor("Anucha Puangpaka");
$pdf->SetCreator("Anucha Puangpaka");
$pdf->SetSubject("$rows[name]");
$pdf->SetKeywords("$rows[name]");
$pdf->WriteHTML($html, 2);
$pdf->Output();
}
}
include('logs.php');
mysqli_close($conn);
?>