Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- //ini_set('max_execution_time', 300);
- require("/externalLibraries/PHPExcel/Classes/PHPExcel/IOFactory.php");
- require("/externalLibraries/PHPExcel/Classes/PHPExcel.php");
- require("/externalLibraries/PHPExcel/Classes/PHPExcel/Writer/Excel2007.php");
- use excelgeneration\dutySheetGenerator;
- include("/src/excelgeneration/dutySheetGenerator.php");
- //seting the variables and objects.
- $objDuty= new dutySheetGenerator();
- $objPhpExcel= new PHPExcel();
- $pathGenerated=__DIR__."/files/generatedFiles/";
- $pathXml=__DIR__."/files/xml/";
- $staffArray=array();
- $dutyArray=array();
- $columnIndex="A";
- //echo $text;
- if(file_exists($pathXml.$_SESSION['examName'].".xml"))
- {
- //echo "Reading xml file.";
- $xml=simplexml_load_file($pathXml.$_SESSION['examName'].".xml");
- //print_r($xml);
- }
- else
- {
- echo "The file ".$_SESSION['examName'].".xml is missing.";
- }
- //setting the properties
- $objPhpExcel->getProperties()->setTitle($_SESSION['examName']);
- $objPhpExcel->getProperties()->setSubject($_SESSION['examName']);
- //setting the values
- $objPhpExcel->setActiveSheetIndex(0);
- $objPhpExcel->getActiveSheet()->setCellValue('A1',"Staff Name");
- //setting the date fields.
- //fetching dates
- $index=0;
- $dataArray = array();
- foreach ($xml->datas->data as $key) {
- # code...
- //echo $key->examDate."<br>".$key->examTime."<br>";
- $dataArray[$index]=$key->examDate.$key->examTime;
- //echo $dataArray[$index]."<br>";
- $index++;
- }
- //setting the dates in excel sheet.
- $columnIndex="B";
- for($i=0;$i<$_SESSION['numberOfExams'];$i++)
- {
- //echo $columnIndex."1<br>";
- $objPhpExcel->getActiveSheet()->setCellValue($columnIndex."1",$dataArray[$i]);
- $columnIndex++;
- }
- /*getting staff details.*/
- $noOfStudents=$xml->numberOfStudents;
- $DutiesPerDay=intval($noOfStudents/30);
- //echo $DutiesPerDay;
- if($noOfStudents%30>0)
- {
- $DutiesPerDay++;
- }
- //echo $DutiesPerDay."<br>";
- $noOfExams=$xml->numberOfExams;
- $totalDuties=$DutiesPerDay*$noOfExams;
- //echo $totalDuties."<br>";
- $staffArray=$objDuty->getStaff($totalDuties);
- $dutyArray=$objDuty->getDutyAsArray($staffArray);
- foreach ($staffArray as $row) {
- # code...
- //$dutyArray[$row];
- //echo "<br>".$row." ".$dutyArray[$row]."<br>";
- }
- //writing staff name to the excel file
- $index=2;
- foreach ($staffArray as $name) {
- # code...
- $objPhpExcel->getActiveSheet()->setCellValue('A'.$index,$name);
- $index++;
- }
- //assigning duty to staff
- $columnStart='B';
- $columnOffset=$noOfExams;
- $rowStart=2;
- $rowOffset=count($staffArray);
- for($i=0;$i<$columnOffset;$i++)
- {
- $dutyCount=$DutiesPerDay;
- while ($dutyCount>0) {
- # code...
- $rnum=rand($rowStart,$rowStart+$rowOffset-1);
- $cellValue=$objPhpExcel->getActiveSheet()->getCell($columnStart.$rnum)->getValue();
- if(is_null($cellValue)||$cellValue=="")
- {
- $staffName=$objPhpExcel->getActiveSheet()->getCell('A'.$rnum)->getValue();
- if($dutyArray[$staffName]>0)
- {
- $objPhpExcel->getActiveSheet()->setCellValue($columnStart.$rnum,"Yes");
- $dutyArray[$staffName];
- $dutyCount--;
- }
- }
- }
- $columnStart++;
- //echo $columnStart."<br>";
- }
- //saving the data
- $objWriter = new PHPExcel_Writer_Excel2007($objPhpExcel);
- $objWriter->save($pathGenerated.$_SESSION['examName'].".xlsx");
- $path=__DIR__."/files/generatedFiles/";
- $filename=$_SESSION['examName'].".xlsx";
- $objReader = PHPExcel_IOFactory::createReader('Excel2007');
- $objReader = $objReader->load($path.$filename);
- $objReader->setActiveSheetIndex(0);
- $highestRow=$objReader->setActiveSheetIndex(0)->getHighestRow();
- $highestColumn=$objReader->setActiveSheetIndex(0)->getHighestColumn();
- //echo "<br>".$highestRow."<br>".$highestColumn;
- $startingRow=1;
- $startingColumn='A';
- $htmlTable="<table>";
- for ($i=$startingRow; $i <=$highestRow; $i++) {
- # code...
- $tr="<tr>";
- for ($j=$startingColumn; $j <=$highestColumn ; $j++) {
- # code...
- if($i==1)
- {
- $th="<th>";
- $cellValue=$objReader->getActiveSheet()->getCell($j.$i)->getValue();
- $th.=$cellValue;
- $th.="</th>";
- $tr.=$th;
- }
- else
- {
- $td="<td>";
- $cellValue=$objReader->getActiveSheet()->getCell($j.$i)->getValue();
- $td.=$cellValue;
- $td.="</td>";
- $tr.=$td;
- }
- }
- $tr.="</tr>";
- $htmlTable.=$tr;
- }
- $htmlTable.="</table>";
- ?>
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width,initial-scale=1.0">
- <link rel="stylesheet" href="css/bootstrap.min.css">
- <title>Duty Sheet Generation</title>
- <style >
- table,td,th{
- border: 1px solid black;
- padding: 2px;
- margin: 1px;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="row">
- <div class="text-center">
- <h1>Generated Table</h1>
- </div>
- <div class="col-md-2"></div>
- <div class="col-md-8 ">
- <?php echo $htmlTable; ?>
- </div>
- <div class="col-md-2"></div>
- </div>
- </div>
- <!-- all the script goes here-->
- <script src="js/jquery.min.js"></script>
- <script src="js/bootstrap.js"></script>
- <script src="js/script.js"></script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment