<?php
$_SESSION['upload_dir'] = '/content-library/' . $person_info->url . '/my-work-file/' . $quiz_url . '/';
$_SESSION['current_path'] = '../content-library/' . $person_info->url . '/my-work-file/' . $quiz_url . '/';

$person_id = $this->session->userdata('person_id');
$directory = 'content-library/' . $person_info->url . '/my-work-file/' . $quiz_url;
if (is_dir($directory)) {
    $_SESSION['upload_dir'] = '/content-library/' . $person_info->url . '/my-work-file/' . $quiz_url . '/';
    $_SESSION['current_path'] = '../content-library/' . $person_info->url . '/my-work-file/' . $quiz_url . '/';
} else {
    $_SESSION['upload_dir'] = '/source/' . $person_info->url . '/';
    $_SESSION['current_path'] = '../source/' . $person_info->url . '/';
}
?>
<div class="col-sm-10">
    <div class="row gray_ttc">
        <div class="col-sm-10">
            <div class="col-sm-12">
                <h3>Edit Quiz</h3>  
                <hr>
            </div>

            <div class="col-sm-12"><br></div>
            <div class="col-sm-12" id="max499">
                <?php
//                echo validation_errors(); //formID2
                $url = 'super-admin/quiz/' . $quiz_info->url . '/edit-quiz/';
                echo form_open($url, 'class="" id="formID2" name="add_quiz_form"');
                ?>
                <div class="form-group col-sm-12">
                    <div class="input-group">
                        <input type="text" 
                               data-errormessage-value-missing="Quiz Name is required!" 
                               class="form-control validate[required] text-input"
                               id="Quiz" name="quiz_name" placeholder="quiz name"  
                               value="<?php
                               if (set_value('quiz_name')) {
                                   echo set_value('quiz_name');
                               } else {
                                   echo $quiz_info->quiz_name;
                               }
                               ?>" 
                               placeholder="Please enter your  Quiz Name.."    placeholder="Please enter your  Quiz Name.."   >
                        <span class="input-group-addon"  title="Please enter your  Quiz Name"><i class="fa fa-book"></i></span>
                    </div>
                    <div id="quiz_name_message" style="color:red">
                        <?php echo form_error('quiz_name'); ?>
                    </div>
                </div>
                <div class="col-sm-12"><br></div>

                <div class="form-group col-sm-6">
                    <div class="input-group">
                        <input  type="hidden" name="pre_code" id="pre_code" value="<?php echo $quiz_info->quiz_public_id; ?>">
                        <input type="text"  
                               data-errormessage-value-missing="invitation code is required!"
                               class="form-control validate[required,custom[groupinvitation],custom[spaceFunction],custom[quizPublicIdExistence]] text-input" 
                               name="quiz_public_id" id="quiz_public_id"
                               value="<?php
                               if (set_value('quiz_public_id')) {
                                   echo set_value('quiz_public_id');
                               } else {
                                   echo $quiz_info->quiz_public_id;
                               }
                               ?>" 
                               placeholder="Please enter Quiz Invitation Code.."  
                               oninput="check_quiz_public_id(this.value);">
                        <span class="input-group-addon" title="Please enter Quiz Invitation Code" ><i class="fa fa-question-circle"></i></span>
                        <input type="hidden" id="incode" name='incode' />
                    </div>
                    <div id="quiz_public_id_message" style="color:red">
                        <?php echo form_error('quiz_public_id'); ?>
                    </div>
                </div>

                <div class="form-group col-sm-6">
                    <div class="input-group">
                        <input type="hidden" name="old_url" id="old_url" value="<?php echo $quiz_info->url; ?>">
                        <input type="hidden" name="url_id" id="url_id" value="<?php echo $quiz_info->url_id; ?>">
                        <input type="text" data-errormessage-value-missing="Group url is required!"
                               class="form-control validate[required, custom[quizurl], custom[spaceFunction], custom[quizUrlExistence]] text-input" 
                               name="quiz_url" id="quiz_url"
                               value="<?php
                               if (set_value('quiz_url')) {
                                   echo set_value('quiz_url');
                               } else {
                                   echo $quiz_info->url;
                               }
                               ?>"
                               onkeyup="checkurl(this.value);" onblur="checkurl(this.value);"
                               placeholder="Please enter Quiz URl.."   >
                        <span class="input-group-addon"  title="Please enter Quiz URl"><i class="fa fa-link"></i></span>
                        <input type="hidden" id="urlcode" name='urlcode' value=''/>
                        <input type="hidden" name="url_exist" id="url_exist" value="">
                    </div>
                    <div id="quiz_url_message" style="color:red">
                        <?php echo form_error('quiz_url'); ?>
                    </div>
                </div>
                <div class=" col-sm-12"><br></div>



                <div class="form-group col-sm-12 padding-zero">
                    <div class="form-group col-sm-6">
                        <div class="input-group">
                            <select data-errormessage-value-missing="Parent Topic is required!"  
                                    class="form-control  validate[required]" 
                                    name="parent_topic_id" 
                                    id="parent_topic_id"
                                    onchange="getParentTopic(this.value, '<?php echo base_url() . 'admin/view-child-topic' ?>', '')">
                                <option value="">Select Parent Topic</option>
                                <?php
                                foreach ($quiz_topics as $topic_value) {
                                    echo '<option value="' . $topic_value->topic_id . '">' . $topic_value->topic_name . '</option>';
                                }
                                ?>
                            </select>
                            <span class="input-group-addon" id="basic-addon1" title="Please Select Parent Topic"><i class="fa fa-book"></i></span>
                        </div>
                        <span style="color: red;" id="parent_topic_id_error"><?php echo form_error('parent_topic_id'); ?></span>
                        <script>
                            documentID('parent_topic_id').value = '<?php echo set_value('parent_topic_id'); ?>';
                        </script>
                    </div>


                    <div class="form-group col-sm-6">
                        <div class="input-group">
                            <select data-errormessage-value-missing="Topic is required!"  
                                    class="form-control  validate[required]" 
                                    name="quiz_topics_specification" 
                                    id="quiz_topics_specification">
                                <option value="">Select Topic</option>

                            </select>
                            <span class="input-group-addon" id="basic-addon1" title="Please Select Topic"><i class="fa fa-book"></i></span>
                        </div>
                        <span style="color: red;" id="topic_id_error"><?php echo form_error('quiz_topics_specification'); ?></span>
                    </div>
                </div>


                <div class="col-sm-12"><br></div>

                <div class="form-group col-sm-12" >
                    <!--                    <div class="input-group " id="">-->
                    <textarea class="form-control  resoulation1280 " 
                              name="quiz_description"
                              rows="4" ><?php
                                  if (set_value('quiz_description')) {
                                      echo set_value('quiz_description');
                                  } else {
                                      //  echo $quiz_info->quiz_description;
                                      echo $this->tinymce_model->view_tinyMce_information($quiz_info->quiz_created_by, $quiz_url, $quiz_info->quiz_description);
                                  }
                                  ?></textarea>
                    <!--                    </div>-->

                </div>
                <div class="col-sm-12"><br></div>

                <div class="form-group col-sm-6">
                    <select data-errormessage-value-missing="Please select a status!"
                            class="form-control validate[required] text-input"
                            id="quiz_random_static"  name="quiz_random_static" >
                        <option value=""> -Select Quiz Status- </option>
                        <!--<option value="1">Random</option>-->
                        <option value="2">Static</option>
                    </select>
                    <div id="quiz_random_static_message" style="color:red">
                        <?php echo form_error('quiz_random_static'); ?>
                    </div>
                </div>



                <div class="col-md-12"><br></div>
                <div class="form-group col-md-6">
                    <select data-errormessage-value-missing="Permission is required!"
                            class="form-control validate[required] text-input"
                            id="quiz_pulication_status"  name="quiz_pulication_status">
                        <option value=""> -Select Permission Status- </option>
                        <option value="1">Public</option>
                        <option value="2">TT User</option>
                        <option value="4">Only Group</option> 
                        <option value="5">Course</option>
                        <option value="6">Video Course</option>
                    </select>
                </div>

                <div id="quiz_permission"> </div>
                <div id="selection_table"> </div>

                <div class="col-sm-12">
                    <div id="quiz_pulication_status_message" style="color:red">
                        <?php echo form_error('quiz_pulication_status'); ?>
                    </div>
                </div>
                <div class="col-sm-12"><br></div>

                <div class="form-group col-sm-6">
                    <div class="text-left padding-zero col-sm-4">  <label >Status  :  &nbsp;&nbsp;</label></div>
                    <div class="radio radio-success col-sm-8" style="margin-top: -3px" >
                        <input  data-errormessage-value-missing="Status is required!"
                                class="validate[required] text-input"
                                type="radio" name="quiz_status" id="Active" value="1"  >
                        <label for="Active">Active</label>
                        <span style="margin-right: 20px"></span>
                        <?php if (!isset($quiz_attendees)) { ?>
                            <input   data-errormessage-value-missing="Status is required!"
                                     class="validate[required] text-input"
                                     type="radio" name="quiz_status" id="Inactive" value="0" >
                            <label for="Inactive">Inactive</label>
                        <?php } ?>
                    </div>
                    <div id="quiz_status_message" style="color:red">
                        <?php echo form_error('quiz_status'); ?>
                    </div>
                </div>

                <div class="col-sm-12"><br></div>
                <div class="col-sm-1"></div>
                <div class="form-group col-sm-5">
                    <input type="submit" class="green-btn btn btn-default" value="Update Quiz !">
                </div>

                <div class="form-group col-sm-3">
                    <a href="<?php echo base_url() . 'super-admin/quiz/' . $quiz_info->url . '/add-quiz-question' ?>" class="green-btn btn btn-default">Add question</a>
                </div>

                <div class="form-group col-sm-2">
                    <a href="<?php echo base_url() . 'super-admin/manage-quiz'; ?>" class="green-btn btn btn-default">Cancel</a>
                    <br/><br/>
                </div>
                <div class="col-sm-3"></div>
                <?php echo form_close(); ?>
            </div>
        </div>
    </div>
    <br>
    <div class="row gray">
        <div class="col-sm-12 ">
            <label><a href="<?php echo base_url() ?>super_admin_quiz">Dashboard </a>->> Create Quiz </label>
        </div>
    </div>
</div>

<!--Start Single selection's with Search-->

<script>
    /*
     * @Function            : getParentTopic 
     * @Used IN             : @function-'admin/course/add-course-from.php'
     * @Description         : get parent Topic for view child topic
     * @Developed By        : MD Jaherul Islam
     * @Start DateTime      : 21-04-2016 05:47 PM 
     * @Parameter           : topicID[INTEGER], url[STRING],childTopicID[INTEGRE]
     * @Return              : NULL      
     * @Status              : available      
     */
    var getParentTopic = function (topicID, url, childTopicID, personID1) {
        var personID = (personID1 === '') ? '' : personID1;
        var output = '<option value="">Select Topic</option>';

        if (topicID !== '') {
            var serverUrl = url + '/' + topicID + '/' + personID;
            $.get(serverUrl, function (data, status) {
                if (status === 'success') {
                    var topicInfo = JSON.parse(data);

                    for (var i = 0; i < topicInfo.length; i++) {
                        output += '<option value="' + topicInfo[i].topic_id + '">' + topicInfo[i].topic_name + '</option>';
                    }
                    $('#quiz_topics_specification').html(output);

                    if (childTopicID !== '') {
                        $('#quiz_topics_specification').val(childTopicID);
                    }
                }
            });
        } else {
            $('#quiz_topics_specification').html(output);
        }
    };
</script>
<!--End Single selection's with Search-->
<script>
                            getParentTopic('<?php echo $parent_topic_info->parent_topic_id; ?>',
                                    '<?php echo base_url() . 'admin/view-child-topic' ?>',
                                    '<?php echo (set_value('quiz_topics_specification') != '') ? set_value('quiz_topics_specification') : $quiz_info->quiz_topics_specification; ?>',
                                    '<?php echo $quiz_info->quiz_created_by; ?>');
</script>
   <script>
        document.forms['add_quiz_form'].elements['parent_topic_id'].value = '<?php echo $parent_topic_info->parent_topic_id; ?>';
    </script>
<script>

    function check_quiz_public_id(given_text)
    {
        _('quiz_public_id_message').innerHTML = '';
        if (given_text == '') {
            _("incode").setAttribute("value", 0);
        } else {
            var cqpid_xmlhttp = create_xmlhttp();
            var serverPage = base_url + 'super-admin/check-quiz-public-id/' + given_text;
            cqpid_xmlhttp.open("GET", serverPage);
            cqpid_xmlhttp.onreadystatechange = function ()
            {
                if (cqpid_xmlhttp.readyState == 4 && cqpid_xmlhttp.status == 200)
                {
                    _("incode").setAttribute("value", cqpid_xmlhttp.responseText);
                }
            };
            cqpid_xmlhttp.send(null);
        }
    }


    //START quiz urlprocessing
    var urlValue = '';
    function checkUrlValue(courseName, urlInput) {
        urlValue = urlInput;
        if (urlValue === '') {
            makeUrl(courseName);
        } else {
            checkUrl(urlInput);
        }
    }

    function makeUrl(profileID) {
        if (profileID == '') {
            _('quiz_url').value = '';
        } else {
            profileID = profileID.replace(/  +/g, '');
            for (i = 0; profileID.length > i; i++) {
                if (profileID.charAt(i) === ' ') {
                    profileID = profileID.replace(/ +/g, '-');
                }
                if (profileID.charAt(i) === '.') {
                    profileID = profileID.replace(/\./g, '');
                }
                if (profileID.charAt(i) === '-') {
                    profileID = profileID.replace(/--/g, '-');
                }
            }

            profileID = profileID.replace(/[&\/\\#@!^_=,+()$~%.'":*?<>{}]/g, '');
            if (profileID.charAt(profileID.length - 1) === '-') {
                profileID = setCharAt(profileID, profileID.length - 1, '');
            }
            profileID = profileID.toLowerCase();
            _('quiz_url').value = profileID;
            checkUrl(profileID);
        }
    }

    function checkUrl(given_text) {
        var cu_xhr = create_xmlhttp();
        if (given_text != '') {
            var serverPage = base_url + 'super-admin/check-quiz-url-existance' + given_text;
            cu_xhr.open("GET", serverPage);
            cu_xhr.onreadystatechange = function ()
            {
                if (cu_xhr.readyState == 4 && cu_xhr.status == 200)
                {
                    _('quiz_url_message').innerHTML = '';
                    _('url_exist').value = cu_xhr.responseText;
                }
            };
            cu_xhr.send(null);
        }
    }     //END quiz url processing


</script>




<?php if (set_value('quiz_random_static')) { ?>
    <script>
        document.forms['add_quiz_form'].elements['quiz_random_static'].value = '<?php echo set_value('quiz_random_static'); ?>';
    </script>
<?php } else { ?>
    <script>
        document.forms['add_quiz_form'].elements['quiz_random_static'].value = '<?php echo $quiz_info->quiz_random_static; ?>';
    </script>
<?php } ?>




<?php if (set_value('quiz_status')) { ?>
    <script>
        document.forms['add_quiz_form'].elements['quiz_status'].value = '<?php echo set_value('quiz_status'); ?>';
    </script>
<?php } else { ?>
    <script>
        document.forms['add_quiz_form'].elements['quiz_status'].value = '<?php echo $quiz_info->quiz_status; ?>';
    </script>
<?php } ?>




<?php if (set_value('quiz_pulication_status')) { ?>
    <script>
        document.forms['add_quiz_form'].elements['quiz_pulication_status'].value = '<?php echo set_value('quiz_pulication_status'); ?>';
    </script>
<?php } else { ?>
    <script>
        document.forms['add_quiz_form'].elements['quiz_pulication_status'].value = '<?php echo $quiz_info->quiz_pulication_status; ?>';
    </script>
<?php } 
