<?php
$user_id = $this->session->userdata('user_id');
?>
<input id="baseUrl" type="hidden" value="<?php echo base_url(); ?>">
<div class="col-sm-8">
    <div class="row ">
        <div class="col-sm-12 " style="padding: 0">

            <div id="all_quiz_rowstalha" class="col-sm-12">

                <div class="col-sm-12 padding-zero gray_ttc text-center" style="border-bottom: 2px solid #8CC73F;padding-top: 10px;padding-bottom: 10px;font-weight: bold">
                    <div class="col-sm-4" style="padding:10px 0">Quiz Name</div>
                    <div class="col-sm-2" style="padding:10px 0">Permission</div>
                    <div class="col-sm-1" style="padding:10px 0"> Question</div>
                    <div class="col-sm-2" style="padding:10px 0">Time</div>
                    <div class="col-sm-3" style="padding:10px 0">Action</div>
                </div>

                <div class="col-sm-12"><br></div> 
                <?php
                if ($all_quiz) {
                    foreach ($all_quiz as $v_quiz) {
                        $quiz_total_time = 0;
                        if ($v_quiz->quiz_random_static == 1) {
                            $quiz_total_time = ($quiz_questions[$v_quiz->quiz_id][0]->time_limit * 60) * $v_quiz->total_number_of_question;
                        } else {
                            foreach ($quiz_questions[$v_quiz->quiz_id] as $value) {
                                $quiz_total_time += $value->time_limit;
                            }
                            $quiz_total_time = $quiz_total_time * 60;
                        }

                        $quiz_id = $v_quiz->quiz_id;
                        $quiz_status = $v_quiz->quiz_status;
                        $url = $v_quiz->url;
                        $quiz_pulication_status = $v_quiz->quiz_pulication_status;
                        ?>
                        <div id="actions<?php echo $quiz_id; ?>">
                            <div class="col-sm-12 gray text-center" >
                                <div class="col-sm-4" style="padding:10px 0">
                                    <a href="<?php echo base_url() . 'quiz/' . $url; ?>"><?php echo $v_quiz->quiz_name; ?>
                                    </a>
                                </div>
                                <div class="col-sm-2" style="padding:10px 0">
                                    <?php
                                    $status_array = array('', 'Public', 'TT Users', 'My Link', 'Group', 'Course', 'Video Course');
                                    echo $status_array[$quiz_pulication_status];
                                    ?>
                                </div>
                                <div class="col-sm-1" style="padding:10px 0"><?php echo count($quiz_questions[$quiz_id]); ?></div>
                                <div class="col-sm-2" style="padding:10px 0"><?php echo $this->cf->sec2view($quiz_total_time); ?></div>

                                <div class="col-sm-3" style="padding:10px 0">
                                    <a href="<?php echo base_url() . 'quiz/' . $url . '/add-quiz-question'; ?>">
                                        <i class="fa fa-question-circle" title="Add question"></i>
                                    </a>
                                    <span>&nbsp;&nbsp;</span>
                                    <a href="<?php echo base_url() . 'quiz/' . $url . '/question' ?>"><i class="fa  fa-th" title="View Question"></i></a>
                                    <span>&nbsp;&nbsp;</span>
                                    <a href="<?php echo base_url() . 'quiz/' . $url . '/edit-quiz'; ?>"><i class="fa fa-edit" title="Edit"></i></a>
                                    <span>&nbsp;&nbsp;</span>
                                    <?php
                                    if ($quiz_status == 0) {
                                        ?>
                                        <a href="javascript:void(0)" onclick="checkForAction('<?php echo base_url() . 'change-quiz-status/' . $url . '/1'; ?>', 'Publish', '<?php echo $quiz_id; ?>')">
                                            <i class="fa  fa-user-plus" title="Publish"></i>
                                        </a>&nbsp;&nbsp;
                                    <?php } else if ($quiz_status == 1) { ?>
                                        <a href="javascript:void(0)" onclick="checkForAction('<?php echo base_url() . 'change-quiz-status/' . $url . '/0'; ?>', 'Unpublish', '<?php echo $quiz_id; ?>')">
                                            <i class="fa  fa-user-times" title="Unpublish"></i>
                                        </a>&nbsp;&nbsp;
                                    <?php } else if ($quiz_status == 2) { ?>
                                        <a href="javascript:void(0)" onclick="checkForAction('<?php echo base_url() . 'change-quiz-status/' . $url . '/1'; ?>', 'Publish', '<?php echo $quiz_id; ?>')">
                                            <i class="fa  fa-user-plus" title="Publish"></i>
                                        </a>&nbsp;&nbsp;
                                    <?php } ?>

                                    <?php if ($quiz_status != 2) { ?>
                                        <a  href="javascript:void(0)" onclick="checkForAction('<?php echo base_url() . 'change-quiz-status/' . $url . '/2'; ?>', 'Delete', '<?php echo $quiz_id; ?>')">
                                            <i class="fa fa-trash-o" title="Delete"></i>
                                        </a>&nbsp;&nbsp;
                                    <?php } ?>
                                </div>
                            </div>
                            <div class="col-sm-12"><br></div> 
                        </div>
                        <?php
                    }
                } else {
                    ?>
                   <p>Express your talent by <a href="<?php echo base_url().'create-quiz'?>" target="_blank">Creating Quiz</a>!</p>
                <?php                
                }
                ?>

                <!--START Modal-->
                <div id="assign_certificate" class="modal fade" role="dialog">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                                <h4 class="modal-title">Join Group</h4>
                            </div>
                            <div class="modal-body">

                                <form id="form3" action="<?php echo base_url() . 'join-group/'; ?>" method="post">

                                    <div class="col-sm-6 col-sm-offset-3">
                                        <br><br>


                                        <div class="input-group">
                                            <input type="text" 
                                                   name="group_public_id" id="group_public_id" 
                                                   data-errormessage-value-missing="Invitation code is required!"
                                                   class="form-control validate[required, custom[groupinvitation],custom[spaceFunction],custom[codeExistence]] text-input" 
                                                   placeholder="Please Type Group Code.." 
                                                   onkeyup="makerequest(this.value);" onblur="makerequest(this.value);">
                                            <input type="hidden" id="incode" name='incode' />
                                            <span class="input-group-addon" ><i class="fa fa-group"></i></span>
                                        </div>
                                        <script>
                                            function makerequest(given_text)
                                            {
                                                alert(given_text);
                                                //var obj = document.getElementById(objID);
                                                serverPage = '<?php echo base_url(); ?>group_con/check_join_group_code/' + given_text;
                                                xmlhttp.open("GET", serverPage);
                                                xmlhttp.onreadystatechange = function ()
                                                {
                                                    //	alert(xmlhttp.readyState);
                                                    //	alert(xmlhttp.status);
                                                    if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
                                                    {
                                                        if (xmlhttp.responseText === 1)
                                                        {
                                                            document.getElementById("incode").setAttribute("value", 1);
                                                        }
                                                        if (xmlhttp.responseText === 2)
                                                        {
                                                            document.getElementById("incode").setAttribute("value", 0);
                                                        }
                                                    }
                                                }
                                                xmlhttp.send(null);
                                            }
                                        </script>
                                    </div>   
                                    <div class="col-sm-12"><br> <br></div>



                                    <div class="col-sm-3"></div>
                                    <div class="col-sm-6">
                                        <input type="submit" class="green-btn btn btn-default" value="Join Group !"><br/><br/>
                                    </div>
                                    <div class="col-sm-3"></div>
                                </form>
                            </div>
                            <div class="col-sm-12"><hr></div>
                            <div class="modal-footer">

                                <button type="button" class="btn btn-danger pull-right" data-dismiss="modal">Close</button>
                            </div>
                        </div>
                    </div>
                </div>
                <?php 
                echo $pagination_link;
                ?>
            </div>
        </div><!-- /.row gray -->
    </div>
</div>
<script>
     $(document).on('click', "a[id^=total_quiz_paginationtalha]", function(){
        var page_number = this.id.replace('total_quiz_paginationtalha', '');        
        var url = $('#baseUrl').val() + 'quizes/teacher-quiz-ajax-view';  
        $.post(url, 
              {
                 page_number: page_number, 
              }, 
              function(data){                  
           $('#all_quiz_rowstalha').html(data);
       })
    })
</script>

<script>

    function checkForAction(url, action, quizID) { // check for Active Inactives
        swal({
            title: "",
            text: "Are you sure you want to " + action + " this Group?",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: '#8CC73F',
            confirmButtonText: 'Yes',
            cancelButtonText: "No",
            closeOnConfirm: true,
            closeOnCancel: true
        },
                function (isConfirm) {
                    if (isConfirm) {
                        var serverPage = url;
                        xmlhttp.open("GET", serverPage);
                        xmlhttp.onreadystatechange = function ()
                        {
                            if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
                            {
                                if (action === 'Delete') {
                                    location.reload();
                                } else {
                                    document.getElementById("actions" + quizID).innerHTML = xmlhttp.responseText;
                                }
                            }
                        };
                        xmlhttp.send(null);
                    }

                });
    }

</script>
