<?php
include("../db.php");



$ch=$_REQUEST["type"];$status="";
if($ch=="PreCheckExist"){
	$detail=$db->getRow("select * from student where status=1 and  school_id='".$_SESSION["admin"]["school_id"]."' and name='".$_REQUEST["name"]."' and fname='".$_REQUEST["fname"]."' and dob='".$_REQUEST["dob"]."'");
	if(is_array($detail) && count($detail)>0){
		echo "user already register on ".$detail["session"]." session in class ".$detail["class"];
	}else{
		echo 0;
	}
}elseif($ch=="findTopic"){

	if(isset($_REQUEST['class_id']) && $_REQUEST['class_id']!="" && $_REQUEST['class_id']!="0"){

		$where=" status=1 ";$checked='';

		if(isset($_REQUEST['subject_id']) && $_REQUEST['subject_id']!="" && $_REQUEST['subject_id']!="0")

			$where.=" AND subject='".$_REQUEST['subject_id']."'";

		$questionList=$db->getRows("select distinct(topic) from question where $where AND class='".$_REQUEST['class_id']."' ");

		if(is_array($questionList) && count($questionList)>0){

			$i=0; ?> <div class="form-group"><label class="col-lg-2 control-label">Topic List</label> <div class="col-lg-10"><?php

			foreach($questionList as $qlist){

				$checked='';$tmsrlks="";$i++;

				?>

				<div class="input-group">

                    <span class="input-group-addon">

                      <input type="checkbox" onchange="fetchQuetion(this,'<?php echo $qlist['topic'] ?>','<?php echo $i ?>')" name="topics[]" id="topic_<?php echo $i ?>"  <?php echo $checked; ?> value="<?php echo $qlist['topic'] ?>">
                      
                    </span>

                    <label type="text" for="topic_<?php echo $i ?>" class="form-control"><?php if($qlist['topic']==""){echo "Question Without Topic";}else{echo $qlist['topic'];} ?></label>

                </div>

                <div class="input-group" id="qus_<?php echo $i ?>"></div>

				<?php	

				

			}

			?></div><?php

		}else{

			?>

            <div class="callout callout-danger alert-dismissable" style="margin: 5px;" id="mycmsg"><button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button><i class="fa fa-bell"></i> No topic Found <?php echo $db->getErMsg(); ?></div>

            <?php

		}

	}else{

		?>

		<div class="callout callout-danger alert-dismissable" style="margin: 5px;" id="mycmsg"><button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button><i class="fa fa-bell"></i> Please Select Class Fisrt</div>

		<?php

	}

}elseif($ch=="editpapercontent"){

//print_r($_REQUEST);	exit;
	if($_REQUEST['dtype']=='paperName')
	{
	 $uAry=array('name'=>$_REQUEST['val']);
	}elseif($_REQUEST['dtype']=='order'){
	 $uAry=array('lorder'=>$_REQUEST['val']);
	}

   $update=$db->updateAry("papers",$uAry,"where id=".$_REQUEST['id']);
   if($update>0){
	        echo "ok";
	   }else{
	        echo $db->getErMsg()."||".$db->getLastQuery();
	   }

}

elseif($ch=="manage"){
    $aryData = array(
            "teacher" => "$_POST[teacher]",
            "date"    => "$_POST[date]",
            "time"    => "$_POST[time]",
            "class"   => "$_POST[class]",
            "chapter" => "$_POST[chapter]",
            "topic"   => "$_POST[topic]"
        );
    $flgIn=$db->insertAry("live_upcoming_class",$aryData);

    if($flgIn > 0){
	        echo "success";
	   }else{
	       echo "error";
	        //echo $db->getErMsg()."||".$db->getLastQuery();
	   }

}

elseif($ch=="updateclass"){
    $aryData = array(
            "teacher" => "$_POST[teacher]",
            "date"    => "$_POST[date]",
            "time"    => "$_POST[time]",
            "class"   => "$_POST[class]",
            "chapter" => "$_POST[chapter]",
            "topic"   => "$_POST[topic]"
        );
    $flgIn=$db->updateAry("live_upcoming_class",$aryData, "where id='$_POST[id]' ");

    if($flgIn > 0){
	        echo "success";
	   }else{
	       echo "error";
	        //echo $db->getErMsg()."||".$db->getLastQuery();
	   }

}

elseif($ch=="delete_upcomingclass"){
	$id = $_POST['id'];

    $res=$db->delete("live_upcoming_class", "where id='".$id."' ");

}

elseif($ch=="delete_previouslive"){
	$id = $_POST['id'];

    $res=$db->delete("liveclass", "where id='$id' ");

}

elseif($ch=="filter_upcomingclass"){
	$id = $_POST['id'];
	$where = "channel_id!='' and session='".$_SESSION['admin']['session']."' ";
	if($id != ""){
        $where .= " and idno='$id' ";
    }
    ?>
                <table class="table table-sm table-striped table-hover">
                    <tr>
                        <th>S.No.</th>
                        <th>Date</th>
                        <th>Time</th>
                        <th>Teacher Name</th>
                        <th>Class</th>
                        <th>Chapter</th>
                        <th>Topic</th>
                        <th>Action</th>
                    </tr>
                    <?php $allteachers = $db->getRows("select * from staff where $where ");

                                $current_date = date('Y-m-d');
                                $current_time = date('H:i');
                                foreach($allteachers as $teacher){
                                $upcomingclass = $db->getRows("select * from live_upcoming_class where teacher='$teacher[idno]' and date >= '$current_date' order by date DESC ");

                                foreach($upcomingclass as $class){ $i++;
                                $date=date_create($class[date]);
                                $date = date_format($date,"d-m-Y");
                                    ?>
                                    <tr>
                                        <td><?php echo $i; ?></td>
                                        <td><?php echo $date; ?></td>
                                        <td><?php echo $class[time]; ?></td>
                                        <td><?php echo $teacher[name]; ?></td>
                                        <td><?php echo $class['class']; ?></td>
                                        <td><?php echo $class[chapter]; ?></td>
                                        <td><?php echo $class[topic];?></td>
                                        <td>
                                            <a onclick="edit(<?php echo $class[id]; ?>)"><img src="edit.jpg" style="width:20px;height:20px;cursor:pointer;" title="Edit" /></a>&ensp;
                                            <a onclick="delete_upcomingclass(<?php echo $class[id]; ?>)"><img src="delete.png" style="width:20px;height:20px;cursor:pointer;" title="Delete"/></a>
                                        </td>
                                    </tr>
                                    <?php
                                }
                            }
                    ?>

                </table>
<?php
}

elseif($ch=="editclass"){
    $id = $_POST[id];
    $data = $db->getRow("select * from live_upcoming_class where id='$id' ");
    ?>
                    <input type="hidden" name="type" value="updateclass" />
                    <input type="hidden" name="id" value="<?php echo $id; ?>" />
                    <div class="row">
                        <div class="col-md-6 form-group">
                            <label>Teachers Lists</label>
                              <select class="form-control" name="teacher" required id="teacherlist">
                                    <option value="">--Select Teacher--</option>
                                  <?php $allteachers = $db->getRows("select * from staff where channel_id!='' and session='".$_SESSION['admin']['session']."' ");
                                      foreach($allteachers as $teacher){
                                          ?>
                                        <option <?php if($data['teacher']==$teacher['idno']){echo "selected";} ?> value="<?php echo $teacher[idno]; ?>"><?php echo $teacher[name]; ?></option>
                                <?php
                                      }
                                  ?>
                              </select>
                        </div>
    
                        <div class="col-md-6 form-group">
                            <label>Date</label>
                            <input type="date" name="date" class="form-control"  value="<?php echo $data[date]; ?>">
                        </div>
                        
                    </div>

                      <div class="row">
                        <div class="col-md-6 form-group">
                            <label>Time</label>
                            <input type="time" name="time" class="form-control"  value="<?php echo $data[time]; ?>">
                        </div>
                        <div class="col-md-6 form-group">
                            <label>Class</label>
                          <select class="form-control" name="class" required>
                                <option value="">--Select Class--</option>
                                <option value="allclass">All Classes</option>
                              <?php $allclasses = $db->getRows("select * from class");
                                  foreach($allclasses as $class){
                                      ?>
                                    <option <?php if($data['class']==$class['class']){echo "selected";} ?> value="<?php echo $class['class']; ?>"><?php echo $class['class']; ?></option>
                            <?php
                                  }
                              ?> 
                          </select>
                        </div>
                      </div>
                      
                      <div class="row">
                        <div class="col-md-6 form-group">
                            <label>Chapter</label>
                            <input type="text" name="chapter" class="form-control" value="<?php echo $data[chapter]; ?>"  placeholder="">
                        </div>
                        <div class="col-md-6 form-group">
                            <label>Topic</label>
                            <input type="text" name="topic" class="form-control" value="<?php echo $data[topic]; ?>" placeholder="">
                        </div>
                      </div>

                      <div class="form-group row">
                        <div class="col-md-6 offset-3">
                          <input type="submit" class="form-control btn btn-success" value="SAVE" >
                        </div>
                      </div>


    <?php
}


elseif($ch=="findQuestions"){

	if(isset($_REQUEST['class_id']) && $_REQUEST['class_id']!="" && $_REQUEST['class_id']!="0"){

		$where=" status=1 ";$checked='';

		if(isset($_REQUEST['subject_id']) && $_REQUEST['subject_id']!="" && $_REQUEST['subject_id']!="0")

			$where.=" AND subject='".$_REQUEST['subject_id']."'";

		$questionList=$db->getRows("select * from question where $where AND class='".$_REQUEST['class_id']."' and topic='".$_REQUEST['topic']."'");

		if(is_array($questionList) && count($questionList)>0){$o=1000;

			?> <label class="col-lg-2 control-label">Question Lists</label> <div class="col-lg-10"><table >
  		<?php
		
			foreach($questionList as $qlist){$o++;

				$checked='';$tmsrlks="";

				$questionAryList=$db->getVal("select questions from papers where id='".$_REQUEST['paper_id']."'");

				if(isset($questionAryList) && $questionAryList!="" && $questionAryList!="0"){

					$sid=json_decode($questionAryList,true);

					if(is_array($sid) && count($sid)>0){

						foreach($sid as $s){

							if($s["question"]==$qlist["id"]){

								$checked=' checked="checked" ';

								$tmsrlks=($s["marks"]);

							}

						}

					}

				}

				$question_ary=json_decode($qlist["question"],true);

				$question=$question_ary["question"];

				if($question_ary["type"]=="image")

					$question="<img src='".$qlist['question']."' height='50'  />";


				?>

				<!--<div class="input-group">

                    <span class="input-group-addon">

                      <input type="hidden" name="qus[]" value="<?php echo $question;?>" />

                      <input type="checkbox" <?php echo $checked; ?> name="qus_check[<?php echo $qlist["id"];?>]" value="<?php echo $qlist['id'] ?>">

                    </span>

                    <label type="text" class="form-control"><?php echo POST($question); if($qlist['is_used']>0)echo "(".$qlist['is_used'].")" ?></label>

                    <span class="input-group-addon" style="padding:0; width:100px;">

                    <input tabindex="<?php echo $o; ?>" type="text" name="qus_marks[<?php echo $qlist["id"];?>]" value="<?php echo number_format($tmsrlks,2) ?>" class="form-control" placeholder="marks" style="height:31px;" />

                    </span>

                </div>--> 

  
  <tr style="border-bottom: 1px solid #ccc;padding: 2px;   margin-bottom: 5px;background-color: #fff;
">
  		<td  class="topic_load_cbox">  <input type="hidden" name="qus[]" value="<?php echo $question;?>" />

                      <input type="checkbox" <?php echo $checked; ?> name="qus_check[<?php echo $qlist["id"];?>]" value="<?php echo $qlist['id'] ?>">
</td>
        <td  class="topic_load_question">
                            <label type="text" class="form-control"><?php echo POST($question); if($qlist['is_used']>0)echo "(".$qlist['is_used'].")" ?></label>

        </td>

        <td class="topic_load_marks">
                           <input tabindex="<?php echo $o; ?>" type="text" name="qus_marks[<?php echo $qlist["id"];?>]" value="<?php echo number_format($tmsrlks,2) ?>" class="form-control" placeholder="marks" style="    margin: 9px 0;" />

        </td>
  </tr>            
				<?php	

			}

			?></table>  </div><?php

		}

	}

}
elseif($ch=="addStream"){
    	$_POST["school_id"]=$_SESSION["admin"]["school_id"];
        $_POST["added_by"]=$_SESSION["admin"]["uid"];
        $_POST["session"]=$_SESSION["admin"]["session"];
        	$_POST["school_id"]=1;
        	 $_POST["added_by"]=1;
		$flgIn=$db->insertAry("liveclass",$_POST);
		if(!is_null($flgIn)){
	        echo mysqli_insert_id($db);
	   }else{
	        echo $db->getErMsg()."||".$db->getLastQuery();
	   }
}
elseif($ch=="updateStream"){
        $incData['status']=$_POST["status"];	
           $incData['video_id']=$_POST["video_id"];	
        if($_POST["video_id"]){
             $url="https://www.youtube.com/watch?v=".$_POST["video_id"];
              $incData['video_url']=$url;	
        }
         
		$flgIn=	$db->updateAry("liveclass",$incData,"where  streamid='".$_POST["streamid"]."'");
		if($flgIn){
	        echo 1;
	   }else{
	        echo $db->getErMsg()."||".$db->getLastQuery();
	   }
}

?>