관리-도구
편집 파일: CFCourse_quiz_questions_control.php
<?php if (!class_exists('CFCourse_quiz_questions_control')) { class CFCourse_quiz_questions_control { var $loader; function __construct($arr) { $this->loader = $arr['loader']; } public function AddBasicSettingsforQuiz(array $data) { global $mysqli; global $dbpref; $table = $dbpref . "content_options"; $part = $mysqli->real_escape_string($data['cfcourse_part']); $cfcourse_quiz_id = $mysqli->real_escape_string($data['cfcourse_quiz_id']); $time = date("Y-m-d H:i:s"); if ($part == "basic") { $r_c = [ '0' => [ 'text' => 'You must study harder!', 'start' => '0', 'end' => '39', ], '1' => [ 'text' => 'Good Job, Try to do better next time!', 'start' => '40', 'end' => '59', ], '2' => [ 'text' => 'Great Job! You are a Fast Learner', 'start' => '60', 'end' => '79', ], '3' => [ 'text' => 'Brilliant, You are awesome in learning!', 'start' => '80', 'end' => '100', ] ]; $res_c = json_encode($r_c); $cfcourse_quiz_duration = $mysqli->real_escape_string($data['cfcourse_quiz_duration']); $cfcourse_quiz_title = $mysqli->real_escape_string($data['cfcourse_quiz_title']); $cfcourse_quiz_desc = $mysqli->real_escape_string($data['cfcourse_quiz_desc']); $cfcourse_show_cert = $mysqli->real_escape_string($data['end_cirtificate']); $sql = "UPDATE `$table` SET `title`='$cfcourse_quiz_title',`description`='$cfcourse_quiz_desc',`duration`='$cfcourse_quiz_duration',`show_cert`='$cfcourse_show_cert', `like_status`=0 , `comment_status`=0,`comment_approve`=0, `admin_check`=0, `delay_result`=0, `delay_result_days`=0, `replay_allowed`=0,`result_comments`='$res_c', `updated_at`='$time' WHERE `id`=" . $cfcourse_quiz_id; } else if ($part == "settings") { $r_c_t = $data['cfcourse_r_com']; $s_p = $data['start_percetange']; $e_p = $data['end_percetange']; $like_status = (isset($data['cf-show-like']) && $data['cf-show-like'] == "on") ? "1" : "0"; $replay_allow = (isset($data['cf-quiz-allow']) && $data['cf-quiz-allow'] == "on") ? "1" : "0"; $comment_status = (isset($data['cf-show-comment']) && $data['cf-show-comment'] == "on") ? "1" : "0"; $comment_approve = (isset($data['cf-comment-approve']) && $data['cf-comment-approve'] == "on") ? "1" : "-1"; $admin_check = (isset($data['admin_check']) && $data['admin_check'] == "on") ? "1" : "0"; if (isset($data['q_delay_result']) && $data['q_delay_result'] == "on") { $quiz_result = 1; $quiz_result_day = (isset($data['q_result_days'])) ? $data['q_result_days'] : 0; $quiz_result_days = $mysqli->real_escape_string($quiz_result_day); } else { $quiz_result = 0; $quiz_result_days = 0; } $re_c = []; for ($i = 0; $i < count($r_c_t); $i++) { $new_a["text"] = $mysqli->real_escape_string($r_c_t[$i]); $new_a["start"] = $mysqli->real_escape_string($s_p[$i]); $new_a["end"] = $mysqli->real_escape_string($e_p[$i]); $re_c[] = $new_a; } $res_c = json_encode($re_c); $sql = "UPDATE `$table` SET `like_status`=$like_status , `comment_status`=$comment_status,`comment_approve`=$comment_approve, `admin_check`=$admin_check, `delay_result`=$quiz_result, `delay_result_days`=$quiz_result_days, `replay_allowed`=$replay_allow,`result_comments`='$res_c', `updated_at`='$time' WHERE `id`=" . $cfcourse_quiz_id; } $return_insert = $mysqli->query($sql) ? 1 : -1; if ($return_insert) { echo json_encode(array("status" => 1, "success" => "Data Updated Successfully")); die(); } else { echo json_encode(array("status" => 0, "success" => "Data not Updated Successfully")); die(); } } //to delete a quiz question function delete_a_question($data) { global $mysqli; global $dbpref; $table = $dbpref . 'quiz_questions'; $cfques_id = $mysqli->real_escape_string($data['id']); $sql = "DELETE FROM `" . $table . "` WHERE `id`=" . $cfques_id; $return_delete = $mysqli->query($sql) ? 1 : -1; if ($return_delete == 1) { echo json_encode(array("status" => 1, "success" => "Data Deleted Successfully")); die(); } else { echo json_encode(array("status" => 0, "failure" => "Data Not Added")); die(); } } //load a question into input boxes before edit function load_before_edit($cfques_id, $quiz_id) { global $mysqli; global $dbpref; $table = $dbpref . 'quiz_questions'; $cond = ""; if ($quiz_id) { $quiz_id = $mysqli->real_escape_string($quiz_id); $cond = " where `quiz_id`=" . $quiz_id . " and `id`=" . $cfques_id; } $qry = $mysqli->query("SELECT `id`, `quiz_id`, `question`, `options`, `correct_options`,`timer`, `quiz_type`, `added_on` FROM `" . $table . "`" . $cond . " order by `id` desc"); return $qry; } //It will insert the question into database function addQuizQuestion($qdata) { global $mysqli; global $dbpref; $cfcourse_question = $qdata['cfcourse_question']; $cfcourse_quiz_type = $mysqli->real_escape_string($qdata['type']); $param = $mysqli->real_escape_string($qdata['param']); $cfcourse_id = $mysqli->real_escape_string($qdata['course_id']); $quiz_id = $mysqli->real_escape_string($qdata['quiz_id']); $cfcourse_answer_chkbx = ""; if (!isset($qdata['answer_chkbx'])) { echo json_encode(array("status" => 3, "message" => "success")); die(); } else { $cfcourse_answers = $qdata['answer_chkbx']; //this one will be array .. $cfcourse_answer_chkbx = join("@brk@", $cfcourse_answers); } $arr_options = array(); if (isset($qdata['quiz_options'])) { $quiz_options = $qdata['quiz_options']; } $add_options = ""; $option_check = true; foreach ($quiz_options as $options) { if (empty($quiz_options)) { $option_check = false; break; } } $date = date('Y-m-d H:i:s'); $table = $dbpref . 'quiz_questions'; $quiz_id = $mysqli->real_escape_string($quiz_id); $cfcourse_question = base64_encode($cfcourse_question); if ($param == "save") { if ($option_check) { foreach ($quiz_options as $key => $value) { $key = $key + 1; $arr_options["opt" . $key] = base64_encode($value); } } else { echo json_encode(array("status" => 4, "message" => "success")); die(); } $arr_options = json_encode($arr_options); $sql = "INSERT INTO `" . $table . "`(`course_id`, `quiz_id`, `question`, `options`, `quiz_type`, `correct_options`, `added_on`) VALUES (" . $cfcourse_id . " ,'" . $quiz_id . "','" . $cfcourse_question . "','" . $arr_options . "','" . $cfcourse_quiz_type . "','" . $cfcourse_answer_chkbx . "','" . $date . "')"; $total_count = getTotalCount($table); $site_token_for_dashboard = get_option('site_token'); if ($_SESSION['user_plan_type' . $site_token_for_dashboard] == 2 && $total_count >= 1) { $affected_rows = 0; } else { $mysqli->query($sql); $affected_rows = $mysqli->affected_rows; } if ($affected_rows > 0) { $last_id = isset($mysqli->insert_id) ? $mysqli->insert_id : 0; $data = $this->show_all_question(false, $last_id); echo json_encode(array("status" => 1, "type" => "save", "message" => "saved", "data" => $data)); die(); } else { echo json_encode(array("status" => 0, "message" => "not saved")); die(); } } else if ($param == "update") { $question_id = $mysqli->real_escape_string($qdata['question_id']); if ($option_check) { foreach ($quiz_options as $key => $value) { $key = $key + 1; $arr_options["opt" . $question_id . $key] = base64_encode($value); } } else { echo json_encode(array("status" => 4, "message" => "success")); die(); } $arr_options = json_encode($arr_options); $sql = "UPDATE `" . $table . "` SET `question`='" . $cfcourse_question . "',`options`='" . $arr_options . "',`quiz_type`='" . $cfcourse_quiz_type . "',`correct_options`='" . $cfcourse_answer_chkbx . "' WHERE `id`=" . $question_id; $return_insert = $mysqli->query($sql) ? 1 : -1; if ($return_insert) { $data = $this->show_all_question(false, $question_id); echo json_encode(array("status" => 1, "message" => "updated", "type" => "update", "data" => $data)); die(); } else { echo json_encode(array("status" => 0, "message" => "not udpated")); die(); } } } //It will show all questions at admin side function show_all_question($quiz_id = false, $question_id = false) { global $mysqli; global $dbpref; $table = $dbpref . 'quiz_questions'; $cond = ""; if ($quiz_id) { $quiz_id = $mysqli->real_escape_string($quiz_id); $cond = " WHERE `quiz_id`=" . $quiz_id; } elseif ($question_id) { $question_id = $mysqli->real_escape_string($question_id); $cond = " WHERE `id`=" . $question_id; } $sql = "SELECT * FROM `" . $table . "`" . $cond . " "; $qry = $mysqli->query($sql); $htmlCont = ''; if ($qry->num_rows == 0) { $htmlCont .= '<div class="col-sm-12"> <h4 class="text-center cf-course-quiz-no-q" style="opacity:0.8;">' . t('No Questions Found, Let\'s Add Some Questions') . '</h4> </div>'; } else { $counter = 0; while ($r = $qry->fetch_object()) { $question = base64_decode($r->question); $options = json_decode($r->options); $id = $r->id; $i = 1; $j = 1; $counter++; ob_start(); $button = $r->quiz_type == "0" ? "radio" : "checkbox"; $class1 = $r->quiz_type == "0" ? "cf-course-quiz-checkmark" : "cfcourse_quiz_checkmark_chkbox"; $class2 = $r->quiz_type == "0" ? "cf-course-quiz-container-check" : "cf-course-quiz_container_chkbox"; echo '<div class=" px-4 py-2 cf-course-quiz-q-one" tabindex="-1" id="cf-course-quiz-question-' . $id . '"> <div class="cf-course-assign-question" > <div class="mb-3 py-0"> <div class="cf-course-label-6"> <span >' . t('Question') . ' <span class="cf-course-quiz-q-counter">' . $counter . '</span></span></div> <div class="pt-1">' . $question . '</div> <div class="d-flex pt-4 justify-content-between"> <div class="text-primary cf-course-label-6 mb-3" >' . t('Options') . '</div> <div>'; echo ($r->quiz_type == "0") ? t("Only One Correct Answer") : t("Multiple Correct Answers"); echo '</div> </div>'; foreach ($options as $opt => $opt_value) { $i++; echo '<div class="mb-3 cf-co-quiz-opt py-0 mx-1"> <div class="d-flex"> <div class="cf-co-quiz-opt1">'; if (strpos($r->correct_options, $opt) !== false) { echo '<span><i class="fas text-success fa-check"></i></span>'; } else { echo '<span><i class="fas text-danger fa-times"" aria-hidden="true"></i></span>'; } echo '</div> <div class="cf-co-quiz-opt2 px-2">' . htmlentities(base64_decode($opt_value)) . '</div> </div></div>'; } echo ' </div> <div class="d-inline-flex"> <button class="btn btn-sm btn-primary cf-course-quiz-edit px-2" >' . t('Edit') . '</button> <button class="btn btn-sm btn-danger cf-course-quiz-del" data-id="' . $id . '">' . t('Delete') . '</button> </div> </div> <div class="cf-course-assign-question-edit"> <form action="" class="cfquizo_AddQuestion"> <div class="mb-3 my-3"> <input type="hidden" name="quiz_id" value="' . $r->quiz_id . '" /> <input type="hidden" name="course_id" value="' . $r->course_id . '" /> <input type="hidden" name="question_id" value="' . $id . '" /> <input type="hidden" name="type" value="' . $r->quiz_type . '" /> <input type="hidden" name="param" value="update" /> <label class="text-primary">' . t('Edit Question') . '</label> <textarea name="cfcourse_question" class="cfcourse_quiz_question form-control" placeholder="' . t('Enter Question') . '">' . htmlentities($question) . '</textarea> <div class="cfcourse-quiz-q-e cfcourse-error" tabindex="-1"></div> </div> <label class="text-primary">' . t('Edit Answers') . '</label> <ul class="cf-course-quiz-op-cont mt-3">'; foreach ($options as $opt => $opt_value) { echo '<li class="cf-course-quiz-one w-100 mb-3"> <div class="d-flex"> <div class="cf-course-quiz-box">'; if (strpos($r->correct_options, $opt) !== false) { echo '<label class="' . $class2 . '"> <input type="' . $button . '" name="answer_chkbx[]" checked class="cf-course-quiz-opt-check" id="cfopt' . $id . $j . '" value="cfopt' . $id . $j . '"> <span class="' . $class1 . '"></span> </label>'; } else { echo '<label class="' . $class2 . '"> <input type="' . $button . '" name="answer_chkbx[]" class="cf-course-quiz-opt-check" id="cfopt' . $id . $j . '" value="cfopt' . $id . $j . '"> <span class="' . $class1 . '"></span> </label>'; } echo '</div> <div class="cf-course-quiz-text"><textarea name="quiz_options[]" class="cf-course-quiz-opt-val form-control">' . htmlentities(base64_decode($opt_value)) . '</textarea></div>'; if ($j > 2) : echo '<div class="cf-course-quiz-trash"> <button type="button" class="btn btn-danger cf-course-quiz-trash-btn" data-id="init"><i class="fas fa-trash cf-course-quiz-trash-btn" data-id="init"></i></button> </div>'; else : echo '<div style="width:47px" class="cf-course-quiz-trash"> </div>'; endif; echo '</div> </li>'; $j++; } echo '</ul> <div class="cfcourse-quiz-all-e cfcourse-error" tabindex="-1"></div> <p class="p-2 cf-course-qadd-opt-con"> <div class="d-flex"> <div style="width:50px"></div> <div class="w-100" ><button type="button" class="btn float-right btn-primary cf-course-qadd-opt" data-type="' . $r->quiz_type . '" data-count="' . $j . '" data-questionId="' . $id . '"> ' . t("Add Options") . ' + </button></div> <div style="width:76px"></div> </div> </p> <p class="p-2"> </p> <hr> <div class="my-2"> <button type="submit" class="btn theme-button cfcourse_add-quiz-op">' . t("Save Changes") . '</button> <button type="button" class="btn mx-2 btn-danger cfcourse_can-edit-quiz-op">' . t("Cancel") . '</button> </div> </form> </div> <hr /> </div>'; $htmlCont .= ob_get_clean(); } } return $htmlCont; } //It will show all questions at student side function show_all_question_stud($quiz_id) { global $mysqli; global $dbpref; $table = $dbpref . 'quiz_questions'; $cond = ""; if (isset($_GET['quiz_id'])) { $quiz_id = $_GET['quiz_id']; } $quiz_id = $mysqli->real_escape_string($quiz_id); $cond = " where `quiz_id`=" . $quiz_id; $qry = $mysqli->query("SELECT `id`, `course_id`,`section_id`,`quiz_id`, `question`, `options`, `correct_options`, `quiz_type`, `timer`, `added_on` FROM `" . $table . "`" . $cond); return $qry; } } //class ends }//if exist class ends