userXtreme

project code

Mar 20th, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.22 KB | None | 0 0
  1. <?php
  2.     session_start();
  3.     //ini_set('max_execution_time', 300);
  4.     require("/externalLibraries/PHPExcel/Classes/PHPExcel/IOFactory.php");
  5.     require("/externalLibraries/PHPExcel/Classes/PHPExcel.php");
  6.     require("/externalLibraries/PHPExcel/Classes/PHPExcel/Writer/Excel2007.php");
  7.     use excelgeneration\dutySheetGenerator;
  8.     include("/src/excelgeneration/dutySheetGenerator.php");
  9.  
  10.     //seting the variables and objects.
  11.     $objDuty= new dutySheetGenerator();
  12.     $objPhpExcel= new PHPExcel();
  13.  
  14.     $pathGenerated=__DIR__."/files/generatedFiles/";
  15.     $pathXml=__DIR__."/files/xml/";
  16.     $staffArray=array();
  17.     $dutyArray=array();
  18.  
  19.    
  20.     $columnIndex="A";
  21.     //echo $text;
  22.     if(file_exists($pathXml.$_SESSION['examName'].".xml"))
  23.     {
  24.         //echo "Reading xml file.";
  25.         $xml=simplexml_load_file($pathXml.$_SESSION['examName'].".xml");
  26.         //print_r($xml);
  27.     }
  28.     else
  29.     {
  30.         echo "The file ".$_SESSION['examName'].".xml is missing.";
  31.     }
  32.    
  33.    
  34.     //setting the properties
  35.     $objPhpExcel->getProperties()->setTitle($_SESSION['examName']);
  36.     $objPhpExcel->getProperties()->setSubject($_SESSION['examName']);
  37.  
  38.     //setting the values
  39.     $objPhpExcel->setActiveSheetIndex(0);
  40.     $objPhpExcel->getActiveSheet()->setCellValue('A1',"Staff Name");
  41.  
  42.  
  43.     //setting the date fields.
  44.     //fetching dates
  45.  
  46.     $index=0;
  47.     $dataArray = array();
  48.     foreach ($xml->datas->data as $key) {
  49.         # code...
  50.         //echo $key->examDate."<br>".$key->examTime."<br>";
  51.  
  52.         $dataArray[$index]=$key->examDate.$key->examTime;
  53.         //echo $dataArray[$index]."<br>";
  54.         $index++;
  55.  
  56.     }
  57.  
  58.  
  59.     //setting the dates in excel sheet.
  60.  
  61.     $columnIndex="B";
  62.     for($i=0;$i<$_SESSION['numberOfExams'];$i++)
  63.     {
  64.         //echo $columnIndex."1<br>";
  65.         $objPhpExcel->getActiveSheet()->setCellValue($columnIndex."1",$dataArray[$i]);
  66.         $columnIndex++;
  67.  
  68.     }
  69.  
  70.  
  71.  
  72.     /*getting staff details.*/
  73.  
  74.     $noOfStudents=$xml->numberOfStudents;
  75.     $DutiesPerDay=intval($noOfStudents/30);
  76.     //echo $DutiesPerDay;
  77.     if($noOfStudents%30>0)
  78.     {
  79.         $DutiesPerDay++;
  80.     }
  81.     //echo $DutiesPerDay."<br>";
  82.     $noOfExams=$xml->numberOfExams;
  83.     $totalDuties=$DutiesPerDay*$noOfExams;
  84.     //echo $totalDuties."<br>";
  85.     $staffArray=$objDuty->getStaff($totalDuties);
  86.     $dutyArray=$objDuty->getDutyAsArray($staffArray);
  87.     foreach ($staffArray as $row) {
  88.         # code...
  89.         //$dutyArray[$row];
  90.         //echo "<br>".$row." ".$dutyArray[$row]."<br>";
  91.     }
  92.    
  93.     //writing staff name to the excel file
  94.     $index=2;
  95.     foreach ($staffArray as $name) {
  96.         # code...
  97.         $objPhpExcel->getActiveSheet()->setCellValue('A'.$index,$name);
  98.         $index++;
  99.     }
  100.  
  101.  
  102.  
  103.     //assigning duty to staff
  104.  
  105.     $columnStart='B';
  106.     $columnOffset=$noOfExams;
  107.     $rowStart=2;
  108.     $rowOffset=count($staffArray);
  109.    
  110.     for($i=0;$i<$columnOffset;$i++)
  111.     {
  112.         $dutyCount=$DutiesPerDay;
  113.         while ($dutyCount>0) {
  114.             # code...
  115.             $rnum=rand($rowStart,$rowStart+$rowOffset-1);
  116.             $cellValue=$objPhpExcel->getActiveSheet()->getCell($columnStart.$rnum)->getValue();
  117.             if(is_null($cellValue)||$cellValue=="")
  118.             {
  119.                 $staffName=$objPhpExcel->getActiveSheet()->getCell('A'.$rnum)->getValue();
  120.                 if($dutyArray[$staffName]>0)
  121.                 {
  122.                     $objPhpExcel->getActiveSheet()->setCellValue($columnStart.$rnum,"Yes");
  123.                     $dutyArray[$staffName];
  124.                     $dutyCount--;
  125.                 }
  126.                
  127.             }
  128.         }
  129.         $columnStart++;
  130.         //echo $columnStart."<br>";
  131.     }
  132.  
  133.  
  134.     //saving the data
  135.     $objWriter = new PHPExcel_Writer_Excel2007($objPhpExcel);
  136.     $objWriter->save($pathGenerated.$_SESSION['examName'].".xlsx");
  137.  
  138.     $path=__DIR__."/files/generatedFiles/";
  139.     $filename=$_SESSION['examName'].".xlsx";
  140.     $objReader = PHPExcel_IOFactory::createReader('Excel2007');
  141.     $objReader = $objReader->load($path.$filename);
  142.     $objReader->setActiveSheetIndex(0);
  143.     $highestRow=$objReader->setActiveSheetIndex(0)->getHighestRow();
  144.     $highestColumn=$objReader->setActiveSheetIndex(0)->getHighestColumn();
  145.     //echo "<br>".$highestRow."<br>".$highestColumn;
  146.     $startingRow=1;
  147.     $startingColumn='A';
  148.     $htmlTable="<table>";
  149.     for ($i=$startingRow; $i <=$highestRow; $i++) {
  150.         # code...
  151.         $tr="<tr>";
  152.         for ($j=$startingColumn; $j <=$highestColumn ; $j++) {
  153.             # code...
  154.             if($i==1)
  155.             {
  156.                 $th="<th>";
  157.                 $cellValue=$objReader->getActiveSheet()->getCell($j.$i)->getValue();
  158.                 $th.=$cellValue;
  159.                 $th.="</th>";
  160.                 $tr.=$th;
  161.             }
  162.             else
  163.             {
  164.                 $td="<td>";
  165.                 $cellValue=$objReader->getActiveSheet()->getCell($j.$i)->getValue();
  166.                 $td.=$cellValue;
  167.                 $td.="</td>";
  168.                 $tr.=$td;
  169.             }
  170.            
  171.         }
  172.         $tr.="</tr>";
  173.         $htmlTable.=$tr;
  174.     }
  175.     $htmlTable.="</table>";
  176.  
  177. ?>
  178.  
  179. <!DOCTYPE HTML>
  180. <html>
  181.     <head>
  182.         <meta charset="utf-8">
  183.         <meta name="viewport" content="width=device-width,initial-scale=1.0">
  184.         <link rel="stylesheet" href="css/bootstrap.min.css">
  185.         <title>Duty Sheet Generation</title>
  186.         <style >
  187.             table,td,th{
  188.                 border: 1px solid black;
  189.                 padding: 2px;
  190.                 margin: 1px;
  191.             }
  192.         </style>
  193.     </head>
  194.     <body>
  195.         <div class="container">
  196.  
  197.             <div class="row">
  198.                 <div class="text-center">
  199.                     <h1>Generated Table</h1>
  200.                 </div>
  201.                 <div class="col-md-2"></div>
  202.                 <div class="col-md-8 ">
  203.                     <?php echo $htmlTable; ?>
  204.                 </div>
  205.                 <div class="col-md-2"></div>
  206.             </div>
  207.            
  208.         </div>
  209.        
  210.         <!-- all the script goes here-->
  211.         <script src="js/jquery.min.js"></script>
  212.         <script src="js/bootstrap.js"></script>
  213.         <script src="js/script.js"></script>
  214.     </body>
  215. </html>
Advertisement
Add Comment
Please, Sign In to add comment