File: /var/www/content-sp/work-sp/nqb_add_question_secondary.php
<?php
include_once('header.php');
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
$fileName = 'nqb_add_question';
//check edit permissions
if (in_array($fileName, $pageArray)) {
echo "Yes";
/***define tables */
$SP_MEDIUM = 'csp_sp_medium';
$SP_QUESTION = 'csp_sp_question';
$SP_QUESTION_MEDIUM_WISE = 'csp_sp_question_medium_wise';
$TT_PREVIOUS_EXAM_TABLE = 'csp_sp_previous_exam';
$SP_MASTER_LEVEL = 'csp_sp_master_level';
$SP_QUESTION_DIFFICULTY = 'csp_sp_question_difficulty';
$SP_MASTER_STRUCTURE = 'csp_sp_master_structure';
$SP_QUES_HISTORY = 'csp_sp_question_history';
$curDate = date('Y-m-d H:i:s');
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$root_link = $protocol . $_SERVER['HTTP_HOST'] . '/';
$allMedium = $conn->query("SELECT medium_id,medium_name from $SP_MEDIUM");
/**request param */
$quesId = $_REQUEST['quesId'];
$addMedium = isset($_REQUEST['medium']) ? $_REQUEST['medium'] : '2';
// $addMedium = isset($_SESSION['TAR_MED']) ? $_SESSION['TAR_MED'] : '2';
$mType = $_REQUEST['mType'];
$role = $_REQUEST['role'];
include 'str_replace_type.php';
# hide repo radio button
if ($role == 'proofreader') {
$style = "";
} else {
$style = "";
}
/***add question */
if (isset($_REQUEST['btn_add'])) {
//$question_id = $_REQUEST['question_id'];
$instructionQ = isset($_POST['instruction']) ? (mysqli_real_escape_string($conn, $_REQUEST['instruction'])) : "";
$questionQ = isset($_POST['question']) ? (mysqli_real_escape_string($conn, $_POST['question'])) : "";
$correctAnswer = isset($_POST['radio_ans']) ? $_POST['radio_ans'] : "";
$ans1Q = isset($_POST['ans_1']) ? (mysqli_real_escape_string($conn, $_POST['ans_1'])) : "";
$ans2Q = isset($_POST['ans_2']) ? (mysqli_real_escape_string($conn, $_POST['ans_2'])) : "";
$ans3Q = isset($_POST['ans_3']) ? (mysqli_real_escape_string($conn, $_POST['ans_3'])) : "";
$ans4Q = isset($_POST['ans_4']) ? (mysqli_real_escape_string($conn, $_POST['ans_4'])) : "";
$ans5Q = isset($_POST['ans_5']) ? (mysqli_real_escape_string($conn, $_POST['ans_5'])) : "";
$question_solutionQ = isset($_POST['solution']) ? (mysqli_real_escape_string($conn, $_POST['solution'])) : "";
$medium = $_REQUEST['medium'];
$ques_sts = $_REQUEST['ques_sts'];
$ques_of = explode(',', $_REQUEST['ques_of']);
$quesOfjson = json_encode($ques_of, JSON_UNESCAPED_UNICODE);
if ($ques_sts == '3' || $ques_sts == '4') {
$prfQuery = ",last_proofread_by='$editor_id',last_proofread_date='$curDate'";
} else {
$prfQuery = '';
}
if ($correctAnswer != '') {
$checkExist = $conn->query("SELECT count(question_id) as ifexist from $SP_QUESTION_MEDIUM_WISE where question_statement='$questionQ' and option_1='$ans1Q' and option_2='$ans2Q' and option_3='$ans3Q' and option_4='$ans4Q' and option_5='$ans5Q' and medium_id='$medium' and question_id='$quesId'");
$existResult = $checkExist->fetch_assoc();
$existRows = $existResult['ifexist'];
if ($existRows == '0') {
$insertQuery = "INSERT into $SP_QUESTION_MEDIUM_WISE set question_id='$quesId', medium_id='$medium',question_instruction='$instructionQ', question_statement='$questionQ', option_1='$ans1Q', option_2='$ans2Q', option_3='$ans3Q', option_4='$ans4Q', option_5='$ans5Q',question_solution='$question_solutionQ',add_by='$editor_id', add_date='$curDate',status='$ques_sts' $prfQuery";
if ($conn->query($insertQuery)) {
$ref_id = $conn->insert_id;
//insert for history
$insertHistory = $conn->query("INSERT into $SP_QUES_HISTORY set medium_id='$medium', question_id='$quesId',ref_id='$ref_id',action='$ques_sts',action_by='$editor_id', action_date='$curDate'");
$updateQuesof = $conn->query("UPDATE $SP_QUESTION set question_of='$quesOfjson' where question_id='$quesId'");
$msg = "Question Added successfully";
} else {
$msg = "Question not Added" . $conn->error;
}
} else {
$msg = "This question is already exist";
}
} else {
$msg = "Please Checked Correct answer radio button";
}
}
$getQuestionData = $conn->query("Select medium_id,question_instruction, question_statement, option_1, option_2, option_3, option_4, option_5, correct_answer, question_solution,question_of from $SP_QUESTION where question_id='$quesId'");
$pmData = $getQuestionData->fetch_assoc();
$correct_old = $pmData['correct_answer'];
$exist_medium = $pmData['medium_id'];
/***question of which server */
$ft_ques_of = json_decode($pmData['question_of']);
?>
<meta http-equiv="Content-Language" content="hi">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src='/tinymce_8/js/tinymce/tinymce.min.js'></script>
<script type="text/javascript">
tinymce.init({
selector: "#instruction",
license_key: 'gpl',
theme: 'silver',
plugins: "save ,tiny_mce_wiris, pagebreak, image, lists, table, fullscreen, link, anchor, charmap, code",
toolbar: "save newdocument | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | blocks | fontfamily | fontsize | cut copy paste | bullist numlist | | outdent indent blockquote | undo redo | link unlink anchor | forecolor backcolor | table | hr removeformat | superscript subscript pagebreak | tiny_mce_wiris_formulaEditor tiny_mce_wiris_formulaEditorChemistry charmap image | fullscreen code help | customInsertButton",
invalid_styles: {
'*': 'color background background-color font-family font-size'
},
paste_data_images: true,
automatic_uploads: true,
toolbar_mode: 'wrap',
extended_valid_elements: '*[.*]',
convert_urls: false,
images_upload_url: 'nqb_upload_image_new.php?folder=<?php echo $mType; ?>', //php file upload path
images_upload_credentials: true,
setup: function(editor) {
editor.ui.registry.addButton('customInsertButton', {
text: 'Auto Set',
onAction: function() {
let content = editor.getContent();
// Create temporary DOM
let div = document.createElement('div');
div.innerHTML = content;
let lines = [];
// Case 1: Multiple <p> tags
let paragraphs = div.querySelectorAll('p');
if (paragraphs.length > 1) {
paragraphs.forEach(function(p) {
let txt = p.innerHTML.trim();
if (txt !== '') {
lines.push(txt);
}
});
} else {
// Case 2: Single <p> with <br>
let html = div.innerHTML;
html = html
.replace(/<br\s*\/?>/gi, '\n')
.replace(/<\/p>/gi, '\n')
.replace(/<p[^>]*>/gi, '');
let temp = document.createElement('div');
temp.innerHTML = html;
lines = temp.innerText
.split('\n')
.map(x => x.trim())
.filter(x => x != '');
}
// console.log(lines);
var subStr = lines[0];
tinymce.get('question').setContent(subStr);
var subStr1 = lines[1];
tinymce.get('ans_1').setContent(subStr1);
var subStr2 = lines[2];
tinymce.get('ans_2').setContent(subStr2);
var subStr3 = lines[3];
tinymce.get('ans_3').setContent(subStr3);
var subStr4 = lines[4];
tinymce.get('ans_4').setContent(subStr4);
var subStr5 = lines[5];
if (subStr5 != '' && subStr5 != 'undefined' && subStr5 != undefined) {
tinymce.get('ans_5').setContent(subStr5);
}
tinymce.get('instruction').setContent("");
}
});
}
});
tinymce.init({
selector: "textarea",
license_key: 'gpl',
theme: 'silver',
plugins: "save ,tiny_mce_wiris, pagebreak, image, lists, table, fullscreen, link, anchor, charmap, code",
toolbar: "save newdocument | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | blocks | fontfamily | fontsize | cut copy paste | bullist numlist | | outdent indent blockquote | undo redo | link unlink anchor | forecolor backcolor | table | hr removeformat | superscript subscript pagebreak | tiny_mce_wiris_formulaEditor tiny_mce_wiris_formulaEditorChemistry charmap image | fullscreen code help | customInsertButton",
//paste_as_text: false,
paste_data_images: true,
automatic_uploads: true,
toolbar_mode: 'wrap',
extended_valid_elements: '*[.*]',
convert_urls: false,
images_upload_url: 'nqb_upload_image_new.php?folder=<?php echo $mType; ?>', //php file upload path
images_upload_credentials: true,
paste_merge_formats: true,
paste_remove_styles_if_webkit: true,
content_style: `body{line-height:2;} p,span,div,li,td,th{line-height:2;}`,
setup: function(editor) {
editor.on('PastePostProcess', function(e) {
const root = e.node;
root.querySelectorAll('*').forEach(function(el) {
if (el.hasAttribute('style')) {
let style = el.getAttribute('style');
style = style.replace(/mso-[^:]+:[^;]+;?/gi, '');
style = style.replace(/font-family:[^;]+;?/gi, '');
style = style.replace(/font-size:[^;]+;?/gi, '');
style = style.replace(/tab-stops:[^;]+;?/gi, '');
style = style.replace(/text-indent:[^;]+;?/gi, '');
style = style.replace(/page-break-before:[^;]+;?/gi, '');
style = style.replace(/page-break-after:[^;]+;?/gi, '');
style = style.replace(/line-height:[^;]+;?/gi, '');
style = style.replace(/margin-left:[^;]+;?/gi, '');
style = style.replace(/margin-right:[^;]+;?/gi, '');
style = style.replace(/margin-top:[^;]+;?/gi, '');
style = style.replace(/margin-bottom:[^;]+;?/gi, '');
style = style.replace(/text-align:[^;]+;?/gi, '');
style = style.replace(/background[^;]*;?/gi, '');
style = style.replace(/border-image:[^;]+;?/gi, '');
style = style.replace(/;;+/g, ';').trim();
if (style === '' || style === ';') {
el.removeAttribute('style');
} else {
el.setAttribute('style', style);
}
}
});
root.innerHTML = root.innerHTML.replace(
/(?:·|•|·)\s*<span[^>]*>\s*(?: |\s)*<\/span>/gi,
''
);
root.querySelectorAll("o\\:p").forEach(function(el) {
el.remove();
});
root.querySelectorAll('*').forEach(function(el) {
if (el.className && el.className.indexOf('Mso') !== -1) {
el.removeAttribute('class');
}
});
root.querySelectorAll('div[align]').forEach(function(div) {
div.removeAttribute('align');
div.style.textAlign = 'left';
});
root.querySelectorAll('table').forEach(function(table) {
// Remove Word alignment
table.removeAttribute('align');
// Prevent floating
table.style.float = 'none';
table.style.clear = 'both';
// Keep table left aligned
table.style.marginLeft = '0';
table.style.marginRight = '0';
table.style.marginTop = '10px';
table.style.marginBottom = '10px';
table.style.display = 'table';
});
});
}
});
function validateForm() {
if (jQuery('#exist_medium').val() == jQuery('#medium').val()) {
alert("Please Select different medium");
return false;
}
if (jQuery("input[name='ques_of']:checked").length == 0) {
alert("Select Question Repository");
return false;
}
}
</script>
<style>
.modal-backdrop {
z-index: 999 !important;
}
.form-group {
margin-bottom: 10px;
margin-top: 10px;
}
label {
font-size: 16px;
}
.form-control {
font-weight: bold;
}
.radio_ans {
top: -5px;
left: 10px;
height: 25px !important;
width: 25px !important;
background-color: #eee;
border-radius: 50%;
position: relative !important;
opacity: 1 !important;
}
.panel-body #frmaddQues div.form-group div.tox.tox-tinymce {
height: 396px !important;
}
.panel-body #frmaddQues div.form-group div.tox.tox-tinymce.tox-fullscreen {
height: 100% !important;
}
</style>
<div class="page-inner">
<div class="page-title">
<h3>New Question Bank</h3>
<div class="page-breadcrumb">
<ol class="breadcrumb">
<li><a href="dashboard">Home</a></li>
<li class="active">Add Questions</li>
</ol>
</div>
</div>
<div id="main-wrapper">
<div class="row">
<div class="col-md-12">
<div class="panel panel-info">
<div class="panel-heading clearfix">
<h4 class="panel-title">Add Question
</h4>
<a style="float: right;color: #000;background: #fff;padding: 6px 10px 6px 8px;
border-radius: 6px;
text-decoration: none;" href="nqb_upload_image?mType=<?php echo $mType; ?>" target="_blank">Upload Image</a>
</div>
<label class="error" style="display: block;float: none;"><?php if (isset($msg)) {
echo $msg;
} ?></label>
<div class="panel-body">
<form id="frmaddQues" method="post" action="" onSubmit="return validateForm();">
<div class="form-group" style="float:left; width:100%;">
<label for="input-Default" class="col-sm-3 control-label">Question Instructions</label>
<div class="col-sm-9">
<textarea name="instruction" id="instruction">
<?php
$question_instruction = str_replace('../', $root_link, $pmData['question_instruction']);
$question_instruction = str_replace($spaceArray, $replaceSpaceArray, $question_instruction);
echo $role == 'proofreader' ? $question_instruction : '';
?>
</textarea>
</div>
</div>
<div class="form-group" style="float:left; width:100%;">
<label for="input-Default" class="col-sm-3 control-label">Question Statement</label>
<div class="col-sm-9">
<textarea name="question" id="question">
<?php
$question_statement = str_replace('../', $root_link, $pmData['question_statement']);
$question_statement = str_replace($spaceArray, $replaceSpaceArray, $question_statement);
echo $role == 'proofreader' ? $question_statement : '';
?>
</textarea>
</div>
</div>
<div class="form-group" style="float:left; width:100%;">
<label for="input-Default" class="col-sm-3 control-label">Options & Answer</label>
<table border="1" style="text-align: center;vertical-align: top">
<tr>
<td style="text-align: center;">Sl. No</td>
<td style="text-align: center;">Correct Answer</td>
<td style="text-align: center;">Answer</td>
</tr>
<tr>
<td>1</td>
<td><input type="radio" value="1" name="radio_ans" class="radio_ans" <?php echo $correct_old == 1 ? 'checked' : ''; ?> /></td>
<td><textarea name="ans_1" id="ans_1">
<?php
$option_1 = str_replace('../', $root_link, $pmData['option_1']);
$option_1 = str_replace($spaceArray, $replaceSpaceArray, $option_1);
echo $role == 'proofreader' ? htmlspecialchars($option_1) : '';
?>
</textarea>
</td>
</tr>
<tr>
<td>2</td>
<td><input type="radio" value="2" name="radio_ans" class="radio_ans" <?php echo $correct_old == 2 ? 'checked' : ''; ?> /></td>
<td><textarea name="ans_2" id="ans_2">
<?php
$option_2 = str_replace('../', $root_link, $pmData['option_2']);
$option_2 = str_replace($spaceArray, $replaceSpaceArray, $option_2);
echo $role == 'proofreader' ? htmlspecialchars($option_2) : '';
?>
</textarea>
</td>
</tr>
<tr>
<td>3</td>
<td><input type="radio" value="3" name="radio_ans" class="radio_ans" <?php echo $correct_old == 3 ? 'checked' : ''; ?> /></td>
<td><textarea name="ans_3" id="ans_3">
<?php
$option_3 = str_replace('../', $root_link, $pmData['option_3']);
$option_3 = str_replace($spaceArray, $replaceSpaceArray, $option_3);
echo $role == 'proofreader' ? htmlspecialchars($option_3) : '';
?>
</textarea>
</td>
</tr>
<tr>
<td>4</td>
<td><input type="radio" value="4" name="radio_ans" class="radio_ans" <?php echo $correct_old == 4 ? 'checked' : ''; ?> /></td>
<td><textarea name="ans_4" id="ans_4">
<?php
$option_4 = str_replace('../', $root_link, $pmData['option_4']);
$option_4 = str_replace($spaceArray, $replaceSpaceArray, $option_4);
echo $role == 'proofreader' ? htmlspecialchars($option_4) : '';
?>
</textarea>
</td>
</tr>
<tr>
<td>5</td>
<td><input type="radio" value="5" name="radio_ans" class="radio_ans" <?php echo $correct_old == 5 ? 'checked' : ''; ?> /></td>
<td><textarea name="ans_5" id="ans_5">
<?php
$option_5 = str_replace('../', $root_link, $pmData['option_5']);
$option_5 = str_replace($spaceArray, $replaceSpaceArray, $option_5);
echo $role == 'proofreader' ? htmlspecialchars($option_5) : '';
?>
</textarea>
</td>
</tr>
</table>
</div>
<div class="form-group" style="float:left; width:100%;">
<label for="input-Default" class="col-sm-3 control-label">Question Solution</label>
<div class="col-sm-9">
<textarea name="solution" id="solution">
<?php
$question_solution = str_replace('../', $root_link, $pmData['question_solution']);
$question_solution = str_replace($spaceArray, $replaceSpaceArray, $question_solution);
echo $role == 'proofreader' ? $question_solution : '';
?>
</textarea>
</div>
</div>
<div class="form-group" style="float:left; width:100%;">
<label for="input-Default" class="col-sm-3 control-label">Medium</label>
<div class="col-sm-9">
<input type="hidden" value="<?php echo $exist_medium; ?>" id="exist_medium">
<select id="medium" name="medium" class="form-control m-b-sm">
<?php foreach ($allMedium as $md) { ?>
<option value="<?php echo $md['medium_id']; ?>" <?php if ($addMedium == $md['medium_id']) echo "selected"; ?>><?php echo $md['medium_name'] ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group" style="float:left; width:100%;">
<label for="input-Default" class="col-sm-3 control-label">Status:</label>
<div class="col-sm-9">
<select name="ques_sts" id="ques_sts" class="form-control">
<option value="2">--Status--</option>
<option value="2">Add</option>
<option value="0">Expire</option>
<option value="1">Draft</option>
<option value="3">Proof Read</option>
<option value="4">ProofRead & Active</option>
</select>
</div>
</div>
<div class="form-group" style="float:left; width:100%;<?php echo $style; ?>">
<label for="input-Default" class="col-sm-3 control-label">Question Repository<span class="badge badge-danger">(required*)</span></label>
<div class="col-sm-3">
<input type="radio" name="ques_of" value="sarkari-pariksha" class="form-control" <?php if ($mType == 'sp') {
echo 'checked';
} ?> />
<label class="control-label" for="exampleInputName">Sarkari Pariksha</label>
</div>
<?php if ($editor_role != '5') { ?>
<div class="col-sm-3">
<input type="radio" name="ques_of" value="csc-olympiad" class="form-control" <?php if ($mType == 'oly') {
echo 'checked';
} ?> />
<label class="control-label" for="exampleInputName">CSC Olympiad</label>
</div>
<div class="col-sm-3">
<input type="radio" name="ques_of" value="sarkari-pariksha,csc-olympiad" class="form-control" <?php ?> />
<label class="control-label" for="exampleInputName">Both Repository</label>
</div>
<?php } ?>
</div>
<div class="form-group" style="float:left; width:100%;">
<label for="input-Default" class="col-sm-3 control-label"> </label>
<div class="col-sm-9">
<button type="submit" class="btn btn-primary" name="btn_add" value="Add">Add Question</button>
</div>
</div>
</form>
</div>
</div>
<!--panel info-->
</div>
<div class="page-footer">
<p class="no-s">Graposs Connect ©</p>
</div>
</div>
</div>
</div>
<?php
} else {
echo "<script>alert('You dont have access to add question');</script>";
echo "<script type='text/javascript'>location.href='/work-sp';</script>";
}
include_once('footer.php');
?>