JezK
Edit File: marks_upload.php
<?php session_start(); include('session_check.php'); include('adm_operations.php'); include("menuheader.php"); //ini_set("display_errors",1); $school_id=$_SESSION['child']['log_school_id']; $acdid=$_SESSION['child']['choosen_academic_id']; $me=$_SESSION['child']['auth_id']; require_once 'php-excel-reader/excel_reader2.php'; $exam_names=$operations->exam_name_details($school_id,$acdid); $class=$operations->getallClassesforOrder($_SESSION['child']['log_school_id'],$_SESSION['child']['choosen_academic_id']); $clsoptions='<option value="0">--Select Class--</option>'; $scnoptions='<option value="0">--Select Section--</option>'; if(!empty($class)) { foreach($class as $cls) if($_POST['class']==$cls['class_id']) $clsoptions.="<option value='".$cls['class_id']."' selected>".$cls['class_name']."</option>"; else $clsoptions.="<option value='".$cls['class_id']."' >".$cls['class_name']."</option>"; } $sections=$operations->getallsectionsdetailsbyclassId($_POST['class']); if(!empty($sections)) foreach($sections as $sec) { if($_POST['section']==$sec['section_id']) $scnoptions.="<option value='".$sec['section_id']."' selected>".$sec['section_name']."</option>"; else $scnoptions.="<option value='".$sec['section_id']."'>".$sec['section_name']."</option>"; } if(!empty($exam_names)) { foreach($exam_names as $row) { if($_POST['examname']==$row['exam_id']) { $exmnamesss .= "<option value='{$row['exam_id']}' selected>{$row['exam_name']}</option>"; } else { $exmnamesss .="<option value='{$row['exam_id']}' >{$row['exam_name']}</option>"; } } } if(isset($_POST['submit'])) { $html=""; $selection=''; $exmid=''; $classid=''; $sectionid=''; $students=array(); $total_marks=array(); if(isset($_FILES['exl']['name'])) { $target_dir = "excel_uploads/"; $target_file = $target_dir .date('his').basename($_FILES["exl"]["name"]); $FileType = pathinfo($target_file,PATHINFO_EXTENSION); if($FileType!='xls') { $errors='Please upload a valid Excel file and please do not upload any file other than the excel template provided under "Download Excel"'; } else { if (move_uploaded_file($_FILES["exl"]["tmp_name"], $target_file)) { $data = new Spreadsheet_Excel_Reader($target_file); $i=1; if(count($data->sheets[$i][cells])>0) // checking sheet not empty { $eqry="select exam_id,exam_name from ced_t_exam_names where lower(exam_name)=lower('".$data->sheets[$i][cells][2][6]."') and academic_year_id=".(int)$acdid." and school_id=".(int)$school_id; $eres=mysql_query($eqry); if(mysql_num_rows($eres)) { $erec=mysql_fetch_array($eres); $exmid=$erec['exam_id']; if($_POST['examname']!=$erec['exam_id']) $selection='Exam name in template and the above Exam selection are not matched'; } else $selection='Exam name in template and the above Exam selection are not matched'; if($selection=='') { $cqry="select class_id,class_name from ced_t_class where lower(class_name)=lower('".$data->sheets[$i][cells][2][2]."') and academic_year_id=".(int)$acdid." and school_id=".(int)$school_id; $cres=mysql_query($cqry); if(mysql_num_rows($cres)) { $crec=mysql_fetch_array($cres); $classid=$crec['class_id']; if($_POST['class']!=$crec['class_id']) $selection='Class name in template and the above Class selection are not matched'; } else $selection='Class name in template and the above Class selection are not matched'; if($selection=='') { $sqry="select section_id,section_name from ced_t_sections where lower(section_name)=lower('".$data->sheets[$i][cells][2][4]."') and academic_year_id=".(int)$acdid." and class_id=".(int)$classid; $sres=mysql_query($sqry); if(mysql_num_rows($sres)) { $srec=mysql_fetch_array($sres); $sectionid=$srec['section_id']; if($_POST['section']!=$srec['section_id']) $selection='Section name in template and the above Section selection are not matched'; } else $selection='Section name in template and the above Section selection are not matched'; } } if($selection=='') { $sql="select s.subject_name,et.total_marks from `ced_t_exam_timetable` et inner join `ced_t_subjects` s on et.subject_id=s.subject_id where exam_id=".$_POST['examname']." and class_id=".$_POST['class']." and section_id=".$_POST['section']." order by timetable_id"; $exe=mysql_query($sql); $nosub=mysql_num_rows($exe); if($nosub) { $tm=0; while($res=mysql_fetch_array($exe)) { $total_marks[$tm]=$res['total_marks']; $tm++; } } for($j=1;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of the sheet { if($j>=5) { if(trim($data->sheets[$i][cells][$j][1])=='') { $html.="Row ".$j."- Please enter Student Roll Number<br>"; } else if(!preg_match('/^[a-zA-Z0-9 ]*$/', trim($data->sheets[$i][cells][$j][1]))) { $html.="Row ".$j."- Please enter a valid Student Roll Number<br>"; } if(trim($data->sheets[$i][cells][$j][2])=='') { $html.="Row ".$j."- Please enter Student Name<br>"; } else if(!preg_match('/^[a-zA-Z0-9 ]*$/', trim($data->sheets[$i][cells][$j][2]))) { $html.="Row ".$j."- Please enter a valid Student Name<br>"; } $ns=$nosub; $a=2; $incre=0; while($ns>0) { $x=$a+1; $y=$x+1; $z=$y+1; if(trim($data->sheets[$i][cells][$j][$y])!='' || trim($data->sheets[$i][cells][$j][$z])!='') { if(trim($data->sheets[$i][cells][$j][$x])=='') { $html.="Row ".$j."- Please enter Total Marks in column ".$x."<br>"; } else if(!is_numeric(trim($data->sheets[$i][cells][$j][$x])) || $data->sheets[$i][cells][$j][$x]<0) { $html.="Row ".$j."- Please do not change the value in protected cell at column ".$x."<br>"; } else if($data->sheets[$i][cells][$j][$x]!=$total_marks[$incre]) { $html.="Row ".$j."- Please do not change the value in protected cell at column ".$x."<br>"; } if(trim($data->sheets[$i][cells][$j][$y])=='') { $html.="Row ".$j."- Please enter Secured Marks in column ".$y."<br>"; } else if(!is_numeric(trim($data->sheets[$i][cells][$j][$y])) || (int)$data->sheets[$i][cells][$j][$y] < 0) { $html.="Row ".$j."- Please enter a valid Secured Marks in column ".$y."<br>"; } else if($data->sheets[$i][cells][$j][$y]>$data->sheets[$i][cells][$j][$x]) { $html.="Row ".$j."- Secured marks should not be greater than Total marks in column ".$y."<br>"; } if(trim($data->sheets[$i][cells][$j][$z])=='') { $html.="Row ".$j."- Please enter Exam Status in column ".$z."<br>"; } else if(strtoupper(trim($data->sheets[$i][cells][$j][$z]))=='PASS' || strtoupper(trim($data->sheets[$i][cells][$j][$z]))=='FAIL') { } else { $html.="Row ".$j."- Please enter a valid Status in column ".$z." ex:PASS or FAIL<br>"; } } $a=$z; $ns--; $incre++; } $gd=$nosub*3; if(!preg_match('/^[a-zA-Z0-9\-_\+ ]+$/',trim($data->sheets[$i][cells][$j][$gd+3])) && $data->sheets[$i][cells][$j][$gd+3]!='') { $html.="Row ".$j."- Please enter a valid Grade<br>"; } $logincheck="select student_id from ced_t_students where student_rollno='".trim($data->sheets[$i][cells][$j][1])."' and school_id=".$school_id." and academic_year_id=".$acdid." and class_id=".$_POST['class']." and section_id=".$_POST['section']; $lexe=mysql_query($logincheck); if(!mysql_num_rows($lexe)) { $html.="Row ".$j."-Roll Number does not exist<br>"; } else { $stres=mysql_fetch_array($lexe); $students[$j]=$stres['student_id']; } } } if($nosub==0) $html="This exam has not been scheduled, Upload failed"; if($j==4) $html="errfound"; } else $html=$selection; } /*INSERTING DATA INTO STUDENTS TABLE IF ALL THE FIELD ARE CORRECT AND SATISFIED ALL CONDITIONS*/ if($html=='') { $sbqry="select s.subject_id,s.subject_name,et.total_marks from `ced_t_exam_timetable` et inner join `ced_t_subjects` s on et.subject_id=s.subject_id where exam_id=".$_POST['examname']." and class_id=".$_POST['class']." and section_id=".$_POST['section']." order by timetable_id"; $sbexe=mysql_query($sbqry); if(mysql_num_rows($sbexe)) { while($sbres=mysql_fetch_assoc($sbexe)) $rowdata[]=$sbres; } if(count($data->sheets[$i][cells])>0) // checking sheet not empty { for($j=1;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of the sheet { if($j>=5) { $gp=0; $x=4; $y=$x+1; foreach($rowdata as $subj) { if(($data->sheets[$i][cells][$j][$y]!='') && trim($data->sheets[$i][cells][$j][$x])!='') { if(strtoupper($data->sheets[$i][cells][$j][$y])=='PASS') $pass=1; else $pass=2; $select="select student_marks_id from ced_t_students_marks where student_id=".$students[$j]." and subject_id=".$subj['subject_id']." and exam_id=".$exmid." and academic_year_id=".$acdid; $selectexe=mysql_query($select); if(mysql_num_rows($selectexe)) { $fetchres=mysql_fetch_array($selectexe); $updmarks="update ced_t_students_marks set marks_secured='".trim($data->sheets[$i][cells][$j][$x])."',status=".$pass." where student_marks_id=".(int)$fetchres['student_marks_id']; mysql_query($updmarks); } else { $insres="INSERT INTO `ced_t_students_marks` set `exam_id`=".$exmid.", `student_id`=".$students[$j].", `marks_secured`='".trim($data->sheets[$i][cells][$j][$x])."', `subject_id`=".$subj['subject_id'].", `status`=".$pass.", `created_by`=".$me.", `created_on`='".date('Y-m-d')."', `academic_year_id`=".$acdid; mysql_query($insres); } } $x=$x+3; $y=$x+1; $gp++; } $gp=($gp*3)+3; if(trim($data->sheets[$i][cells][$j][$gp])!='') { $grd="select student_grade_id from ` ced_t_student_grades` where exam_id=".$exmid." and `student_id`=".$students[$j]." and academic_year_id=".$acdid; $exegrd=mysql_query($grd); if(mysql_num_rows($exegrd)) { $grdres=mysql_fetch_array($exegrd); //echo "update ` ced_t_student_grades` set student_grade='".$data->sheets[$i][cells][$j][$gp]."',updated_by=".$me." where student_grade_id=".(int)$grdres['student_grade_id']; mysql_query("update ` ced_t_student_grades` set student_grade='".$data->sheets[$i][cells][$j][$gp]."',updated_by=".$me." where student_grade_id=".(int)$grdres['student_grade_id']); } else { //echo "insert into ` ced_t_student_grades` set exam_id=".$exmid.",student_id=".$students[$j].",student_grade='".$data->sheets[$i][cells][$j][$gp]."',academic_year_id=".$acdid.",created_by=".$me.",created_on='".date('Y-m-d')."'"; mysql_query("insert into ` ced_t_student_grades` set exam_id=".$exmid.",student_id=".$students[$j].",student_grade='".$data->sheets[$i][cells][$j][$gp]."',academic_year_id=".$acdid.",created_by=".$me.",created_on='".date('Y-m-d')."'"); } } } } } } unlink($target_file); if($html=='') $errors="Data uploaded successfully"; else { if($html=='errfound') $errors="Please enter data for atleast one Student"; else $errors="Following errors have been found in the excel sheet. Please correct these errors and upload again:<br>".$html; } } } } } ?> <body style=" overflow-x: hidden;"> <div class="row" style="margin-top: 30px;margin-left:5px;"> <div class="col-md-9 text-left" style="font:Arial, Helvetica, sans-serif;font-style:normal;font-size:24px; "> <i class="glyphicon glyphicon-cloud-upload" title ="Upload Student Data" style="font-size: 20px;margin: 0px 0px 0px -15px;"></i> Upload Student Marks </div> </div> <form class="form-horizontal" method="post" enctype="multipart/form-data"> <div class="box-body" style="background-color:#fff; padding: 10px; font-family:Arial, Helvetica, sans-serif; font-size:13px; "> <div class="row"> <div class="form-group col-md-6" style="margin-bottom:1.0%;"> <div class="col-md-4 col-md-offset-1 text-right"> <b style="color:#666666; ">Exam Name</b><span style="color:#FF0000; margin:2px;">*</span> </div> <div class="col-md-7"> <select class="form-control" id="examnames" name="examname" style="height:29px; width:295px!important;font-size:12px;" tabindex="2" > <option value='0' selected>Select Exam Name</option> <? echo $exmnamesss;?> </select> <!-- if there is an error--> <div class="col-md-10" id="err_examnames" style="color:#990000;margin-left:-15px;"></div> <!--- end error--> </div> </div> </div> <div class="row"> <div class="form-group col-md-6" style="margin-bottom:1.0%;"> <div class="col-md-4 col-md-offset-1 text-right"> <b style="color:#666666; ">Class</b><span style="color:#FF0000; margin:2px;">*</span> </div> <div class="col-md-7"> <select class="form-control" id="classes" name="class" style="height:29px; font-size:12px;width:300px!important;" tabindex="1" onchange="classchange()"> <?=$clsoptions;?> </select> <!-- if there is an error--> <div class="col-md-10" id="err_classes" style="color:#990000;margin-left:-15px;"></div> <!--- end error--> </div> </div> </div> <div class="row"> <div class="form-group col-md-6" style="margin-bottom:1.0%;"> <div class="col-md-4 col-md-offset-1 text-right"> <b style="color:#666666; ">Section</b><span style="color:#FF0000; margin:2px;">*</span> </div> <div class="col-md-7"> <select class="form-control" id="sections" name="section" style="height:29px; font-size:12px;width:300px!important;" tabindex="2" > <?=$scnoptions;?> </select> <!-- if there is an error--> <div class="col-md-10" id="err_sections" style="color:#990000;margin-left:-15px;"></div> <!--- end error--> </div> </div> </div> <div class="row"> <div class="form-group col-md-6" style="margin-bottom:1.0%;"> <div class="col-md-4 col-md-offset-1 text-right"> <b style="color:#666666; ">Choose File</b><span style="color:#FF0000; margin:2px;">*</span> </div> <div class="col-md-7"> <input type="file" id="brw" name="exl" class="form-control" style="height:40px; font-size:12px;width:300px!important;" tabindex="3"> <!-- if there is an error--> <div class="col-md-10" id="err_brw" style="color:#990000;margin-left:-15px;"></div> <!--- end error--> </div> </div> </div> </form> <div class="form-group" style="margin-left:-50px;"> <div class="col-md-8" align="center"> <button class="btn btn-default buttons btnclr" type="submit" name="submit" tabindex="4" value="Upload" onclick="return validations();">Upload</button> <button style="margin-left:2%;" type="button" onClick="location.href='marksof_student.php'" class="btn btn-default buttons btnclr" tabindex="5" >Cancel</button> </div> </div> <!--errors--> <div class="col-md-10 col-md-offset-2"> <span style="color:#ff0000;"><? if(isset($_POST['submit'])) echo $errors;?></span> </div> <!--errors--> </div> <script> function validations() { var exm=document.getElementById("examnames").value; var cls=document.getElementById("classes").value; var sec=document.getElementById("sections").value; var fil=document.getElementById('brw').value; $("#err_classes").text(""); $("#err_sections").text(""); $("#err_examnames").text(""); $("#err_brw").text(""); if(exm=='0') { $("#err_examnames").text("Please select Exam"); $("#err_examnames").show(); return false; } else if(cls=='0') { $("#err_classes").text("Please select Class"); $("#err_classes").show(); return false; } else if(sec=='0') { $("#err_sections").text("Please select Section"); $("#err_sections").show(); return false; } else if(fil=='') { $("#err_brw").text("Please select the excel file to upload"); $("#err_brw").show(); return false; } } function classchange() { var x=$('#classes').val(); $("#err_classes").text(""); if(x == 0) { $("#err_classes").text("Please select Class"); $("#err_classes").show(); section_option = '<option value="0">--Select Section--</option>'; $("#sections").html(section_option); return false; } else { $.ajax({ url: "ajaxcalls_attn.php", type:"POST", data:"mode="+"choose_class"+"&data="+x, success: function(result) { var result2 = eval("("+result+")"); var section_option = '<option value="0">--Select Section--</option>'; for(var i = 0;i < result2.length;i++) { section_option += '<option value="'+result2[i].section_id+'">'+result2[i].section_name+'</option>'; } $("#sections").html(section_option); }, error : function(e) { //alert(e); } }); } } </script>