<?php
$pgMod="performance";
$pgAct="view";


if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;

	
		$date=date("Y-m-d");
		//echo $date;exit;
		$studentList=array();

		
		
		
		if(!isset($_POST['detail']) || trim($_POST['detail'])=='')
		{

			$flgEr=TRUE;

			echo "error==>Please enter detail.";

		}

		else{

			$status=0;


			$aryData=array(	

						'class'	=>	POST('classname'),
						
						'section'	=>	POST('section'),

						'student_id'	=>	POST('studentname'),

							'details'	=>	$_POST['detail'],

							'Added_by'   =>$_SESSION['admin']['uid'],

							'school_id'   =>$_SESSION['admin']['school_id'],
							
							'session'   =>$_SESSION['admin']['session'],

							'date'	=> $date,

						

							);


			
//echo "<pre>";print_r($aryData);exit;




$session = $_SESSION['admin']['session'];
$msg = $_POST['detail'];
$class = $_POST['classname'];
 $set_title ='Performance';
 $_REQUEST["section"]=$_POST['section'];
$conwhere='';
if(isset($_POST["section"]) && $_POST["section"]!=""){
		
			$conwhere=" and section='".$_POST["section"]."' ";
		}

// Enabling error reporting
error_reporting(-1);
ini_set('display_errors', 'On');

 $upOne = realpath(__DIR__ . '/..');

 $upOne2 = realpath(__DIR__ . '/..'.'api_new/firebase/push.php');



require_once  $upOne.'/api_new/firebase/firebase.php';
require_once  $upOne.'/api_new/firebase/push.php';

$firebase = new Firebase();
$push = new Push();

$tokenlist=$db->getRows("SELECT idno,name,fcm_token from student where class='$class' $conwhere and session='$session' and status='1' ");

$result_arr = array();

foreach ($tokenlist as $value) {
  
             $stu_id =  $value['idno'];
             $target_user_name =  $value['name'];
            $fb_token =  $value['fcm_token'];
      

       

        if (strlen($fb_token) > 0 ) {
            // optional payload
            $payload = array();
            $payload['name'] = $target_user_name;
            $payload['msg'] = $msg; // whether to include to image or not
             $payload['stu_id'] = $stu_id;
            $include_image = TRUE;
            $push->setTitle($set_title);
            $push->setMessage($msg);
            if ($include_image) {
                $push->setImage('https://api.androidhive.info/images/minion.jpg');
            } else {
                $push->setImage('');
            }
            $push->setIsBackground(FALSE);
            $push->setPayload($payload);
            $json = '';
            $response = '';
            $json = $push->getPush();
       
            $response = $firebase->send($fb_token, $json);
            
            array_push($result_arr,$response);
        }

}




		}		

	
if($flgEr!=TRUE && $pgAct=="add")
	{
           $_POST["added_by"]=$_SESSION["admin"]["uid"];
			$_POST['school_id']=$_SESSION['admin']['school_id'];
						
	 		$flgIn=$db->insertAry("performance",$aryData);

			if(!is_null($flgIn))

			{

				$_SESSION['msg']='Saved Successfully';
				unset($_SESSION['form']);
				redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));
			}

			else

			{

			array_push($alert_err,$db->getErMsg());

			}

	}

	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{

//echo "<pre>";print_r($aryData);exit;

		$flgUp=$db->updateAry("assignment",$aryData,"where added_by='".$_SESSION["admin"]["uid"]."' and id='".$_GET['id']."'");
	
		if(!is_null($flgUp))
		{
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	} 
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("performance","where  id='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));
} 
if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b>complain</b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Assignment</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
if($pgAct=="edit" && $_GET["id"]!="")
{   
	$aryForm=$db->getRow("SELECT * FROM assignment WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");

//echo "<pre>";print_r($aryForm);exit;
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{

?>
<!-- Forms --> 
<script language="javascript">

function getsubname(subname)
{
var c=$("#classname").val();
//alert(c);
$.post("newajax.php",{type:"getsubname",clsname:c,subname:subname},function(data){
		$("#subdiv").html(data);
	});
}

$(document).ready(function(){
	getsubname($('#hidden_sname').val());
});


</script>
<style>
.top-bar-minimize{display:none;}
thead, tfoot {display: table-header-group;}
.top-bar+.well,.accordion .accordion-heading+.accordion-body{display: inline-block !important;width: 96.7% !important;overflow: inherit !important;} 
</style>
	<div class="row-fluid">
        <form method="post" enctype="multipart/form-data" action="">
			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
					<h3><i class="icon-cog"></i> Create a New Assignment</h3>
				</div>
				<!-- / Forms: Top Bar -->

				<!-- Forms: Content -->
				<div class="well no-padding">

						            <div class="control-group">
          
          <label class="control-label" for="type">Class</label>
          
          <div class="controls">
            <select  id="classname"   name="classname" onchange="findStudent()" >
              <option value="0" >Select</option>
              <?php
              $sliderContent=$db->getRows("select class from class where school_id=".$_SESSION['admin']['school_id']);
              foreach($sliderContent as $sdetail){
                    ?>
              <option  value="<?php echo $sdetail['class'];?>" <?php  if($aryForm['class']==$sdetail['class'])echo 'selected="selected"';?> ><?php echo $sdetail['class'];?></option>
              <?php } ?>
            </select>
            
            <?php if($pgAct=="edit" && isset($_REQUEST['id']) && trim($_REQUEST['id']) !=''){
            ?>
            <input type="hidden"  id="hidden_sname" value="<?php echo $aryForm['subject'];?>">
            <?php } ?>
          </div> 
          
        </div>
        
        
         <div class="control-group">
                          <label class="control-label" for="type">section</label>
                          <div class="controls" >
                             <select name="section" onchange="findStudent()" id="section">
                                    <option value="">Section</option>
                                    <?php 
                                        $class=$db->getRows("select * from section where school_id='".$_SESSION["admin"]["school_id"]."'");
                                        foreach($class as $cla){
                                            ?>
                                            <option  value="<?php echo $cla["name"] ?>">
                                                <?php echo $cla["name"] ?>
                                            </option>
                                            <?php
                                
                                        }
                                    ?>
                                </select>
                          </div>
                        </div>
        
                         
                        <div class="control-group">
                          <label class="control-label" for="type">Student</label>
                          <div class="controls" id="findStudent">
                            <select  id="studentname"  class="form-control" name="studentname">
                              <option value="0" >Please select  name first</option>
                            </select>
                          </div>
                        </div>
                        
                       
                        
                        <div class="control-group">
							<label class="control-label" for="detail">Performance Detail</label>
							<div class="controls">
								<textarea name="detail" id="detail" class="input-block-level" style="resize:none" ><?php echo $aryForm["performance"]; ?></textarea>
							</div>
						</div>
                        
            
                            
                     
                        
						<div class="form-actions">
                        
							<button type="submit" class="btn btn-primary"> Save</button>
							<button type="button" class="btn"> Cancel</button>
						</div>
                        
				</div>
				<!-- / Forms: Content -->

			</div>
			<!-- / Forms: Box -->

        </form>
	</div>
<?php   
}else{
	?>
<style>


.hoverclass{
hover:hoverclass;
}

</style>    

	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>

    <button style="float:right; margin-top:5px" onClick="window.location='./?src=Notice&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add <?php echo $pgMod; ?></button>
 
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"Notice", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM performance"); $i=0;

//echo "<pre>";print_r($aryList);echo "<pre>";exit;


	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            
            <thead>
                <tr>

                    <th>Sr No.</th>
                    
                    <th>Performance Detail</th>

                    <th>class</th>

                    <th>section</th>
        
                 
                    <th>date</th>
                     <th>Action</th> 
	            </tr>
            </thead>
            
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 


            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    
                    <td><?php echo $i ?></td>
                    
                    <td><?php echo $clist['details'];	?>
</td>				
			<td><?php echo   $clist['class'];	?>
			<td><?php echo   $clist['section'];	?>
		
			<td><?php echo  date("d/m/Y", strtotime($clist['date']));  	?>
</td>
             <!-- <td><a style="color:#000" target="_blank" href="<?php echo   URL_ROOT."uploads/assignment/".$clist['upload_assignment'];	?>">Download</a></td>  !-->
                    			
     	            
                    
                    <td>
                    <?php $aryPgAct['action']="delete"; ?>
                         <a href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>&id=<?php echo $clist['id'] ?>" class="confirmation"><button type="button" class="btn btn-danger">Delete</button></a>  
                    </td>
                </tr>
            <?php } ?>
            </tbody>
            
            <tfoot>
                <tr>
                	<th></th>
                	<th></th>
                    <th></th>
                    <th></th>
                    <th colspan="2"></th>
                </tr>
            </tfoot> 
            
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>complain</b> Record not found </i>
        </div>
        <?php
	} 
	?>
	</div></div></div>
	<?php
}
?>
<script type="text/javascript">
function findStudent(t){
	var cls=$("#classname").val();
	var section=$("#section").val();
	$.post("Notice/ajax.php", { type: "findStudent" ,cls: cls, section: section } , function( data ) {
	  	$("#findStudent").html(data);
	  	
	});
}
</script>
<!-- / Forms -->