| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
function watu_questions() { |
| 5 |
global $wpdb; |
| 6 |
|
| 7 |
$action = 'new'; |
| 8 |
if(!empty($_GET['action']) and $_GET['action'] == 'edit') $action = 'edit'; |
| 9 |
$quiz_id = intval($_GET['quiz']); |
| 10 |
|
| 11 |
if(isset($_POST['submit']) and check_admin_referer('watu_questions')) { |
| 12 |
$content = wp_kses_post($_POST['content']); |
| 13 |
$answer_type = $_POST['answer_type']; |
| 14 |
if(!in_array($answer_type, array('radio', 'checkbox', 'textarea'))) $answer_type = 'radio'; |
| 15 |
$is_required = empty($_POST['is_required']) ? 0 : 1; |
| 16 |
$is_inactive = empty($_POST['is_inactive']) ? 0 : 1; |
| 17 |
$is_survey = empty($_POST['is_survey']) ? 0 : 1; |
| 18 |
$feedback = wp_kses_post($_POST['feedback']); |
| 19 |
$num_columns = empty($_POST['num_columns']) ? 1 : intval($_POST['num_columns']); |
| 20 |
|
| 21 |
// question category? |
| 22 |
if(!empty($_POST['new_cat'])) { |
| 23 |
$wpdb->query($wpdb->prepare("INSERT INTO ".WATU_QCATS." (name) VALUES (%s) ", sanitize_text_field($_POST['new_cat']))); |
| 24 |
$cat_id = $wpdb->insert_id; |
| 25 |
} |
| 26 |
else $cat_id = intval($_POST['cat_id']); |
| 27 |
|
| 28 |
$question_id = 0; |
| 29 |
|
| 30 |
if($action == 'edit'){ //Update goes here |
| 31 |
$question_id = intval($_POST['question']); |
| 32 |
|
| 33 |
$wpdb->query($wpdb->prepare("UPDATE ".WATU_QUESTIONS." |
| 34 |
SET question=%s, answer_type=%s, is_required=%d, feedback=%s, is_inactive=%d, is_survey=%d, num_columns=%d, cat_id=%d |
| 35 |
WHERE ID=%d", $content, $answer_type, $is_required, |
| 36 |
$feedback, $is_inactive, $is_survey, $num_columns, $cat_id, $question_id)); |
| 37 |
$wpdb->query($wpdb->prepare("DELETE FROM ".WATU_ANSWERS." WHERE question_id=%d", $question_id)); |
| 38 |
|
| 39 |
} else { |
| 40 |
// select max sort order in this quiz |
| 41 |
$sort_order = $wpdb->get_var($wpdb->prepare("SELECT MAX(sort_order) FROM ".WATU_QUESTIONS." WHERE exam_id=%d", $quiz_id)); |
| 42 |
$sort_order++; |
| 43 |
|
| 44 |
$sql = $wpdb->prepare("INSERT INTO ".WATU_QUESTIONS." (exam_id, question, answer_type, is_required, feedback, sort_order, |
| 45 |
is_inactive, is_survey, num_columns, cat_id) |
| 46 |
VALUES(%d, %s, %s, %d, %s, %d, %d, %d, %d, %d)", $quiz_id, $content, $answer_type, |
| 47 |
$is_required, $feedback, $sort_order, $is_inactive, $is_survey, $num_columns, $cat_id); |
| 48 |
$wpdb->query($sql); |
| 49 |
|
| 50 |
$question_id = $wpdb->insert_id; |
| 51 |
$action='edit'; |
| 52 |
} |
| 53 |
|
| 54 |
if($question_id > 0) { |
| 55 |
// the $counter will skip over empty answers, $sort_order_counter will track the provided answers order. |
| 56 |
$counter = 1; |
| 57 |
$sort_order_counter = 1; |
| 58 |
$correctArry = watu_int_array(@$_POST['correct_answer']); |
| 59 |
$pointArry = $_POST['point']; |
| 60 |
|
| 61 |
if(is_array($_POST['answer']) and !empty($_POST['answer'])) { |
| 62 |
|
| 63 |
foreach ($_POST['answer'] as $key => $answer_text) { |
| 64 |
$correct=0; |
| 65 |
if( @in_array($counter, $correctArry) ) $correct=1; |
| 66 |
$point = floatval($pointArry[$key]); |
| 67 |
if($answer_text!='') { |
| 68 |
$answer_text = wp_kses_post($answer_text); |
| 69 |
$wpdb->query($wpdb->prepare("INSERT INTO ".WATU_ANSWERS." (question_id,answer,correct,point, sort_order) |
| 70 |
VALUES(%d, %s, %s, %f, %d)", $question_id, $answer_text, $correct, $point, $sort_order_counter)); |
| 71 |
$sort_order_counter++; |
| 72 |
} |
| 73 |
$counter++; |
| 74 |
} |
| 75 |
} // end if(is_array($_POST['answer']) and !empty($_POST['answer'])) |
| 76 |
|
| 77 |
// save & reuse clicked? |
| 78 |
if(!empty($_POST['reuse'])) { |
| 79 |
watu_redirect("admin.php?page=watu_question&action=new&quiz=".intval($_GET['quiz'])."&question=" . $question_id); |
| 80 |
} |
| 81 |
// save & add new blank clicked? |
| 82 |
if(!empty($_POST['add_blank'])) { |
| 83 |
watu_redirect("admin.php?page=watu_question&action=new&quiz=".intval($_GET['quiz'])); |
| 84 |
} |
| 85 |
|
| 86 |
} // end if $question_id |
| 87 |
} // end add/save |
| 88 |
|
| 89 |
if(!empty($_GET['action']) and $_GET['action'] == 'delete' and check_admin_referer('watu_questions')) { |
| 90 |
$wpdb->query($wpdb->prepare("DELETE FROM ".WATU_ANSWERS." WHERE question_id=%d", intval($_GET['question']))); |
| 91 |
$wpdb->query($wpdb->prepare("DELETE FROM ".WATU_QUESTIONS." WHERE ID=%d", intval($_GET['question']))); |
| 92 |
} |
| 93 |
$exam_name = stripslashes($wpdb->get_var($wpdb->prepare("SELECT name FROM ".WATU_EXAMS." WHERE ID=%d", $quiz_id))); |
| 94 |
|
| 95 |
// mass delete questions |
| 96 |
if(!empty($_POST['mass_delete']) and check_admin_referer('watu_questions')) { |
| 97 |
$qids = is_array($_POST['qids']) ? watu_int_array($_POST['qids']) : array(0); |
| 98 |
|
| 99 |
if(!empty($qids)) { |
| 100 |
$placeholders = implode(',', array_fill(0, count($qids), '%d')); |
| 101 |
$wpdb->query($wpdb->prepare("DELETE FROM ".WATU_ANSWERS." WHERE question_id IN ($placeholders)", $qids)); |
| 102 |
$wpdb->query($wpdb->prepare("DELETE FROM ".WATU_QUESTIONS." WHERE ID IN ($placeholders)", $qids)); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// mass delete questions |
| 107 |
if(!empty($_POST['mass_update']) and check_admin_referer('watu_questions')) { |
| 108 |
$qids = is_array($_POST['qids']) ? watu_int_array($_POST['qids']) : array(0); |
| 109 |
|
| 110 |
// constructing SQL this way because we may add new properties in the next version |
| 111 |
$is_required_sql = ''; |
| 112 |
$params = array(); |
| 113 |
if($_POST['is_required'] != -1) { |
| 114 |
$is_required_sql = ", is_required=%d"; |
| 115 |
$params[] = intval($_POST['is_required']); |
| 116 |
} |
| 117 |
|
| 118 |
if(!empty($qids)) { |
| 119 |
$placeholders = implode(',', array_fill(0, count($qids), '%d')); |
| 120 |
$wpdb->query($wpdb->prepare("UPDATE ".WATU_QUESTIONS." SET ID=ID $is_required_sql WHERE ID IN ($placeholders)", array_merge($params, $qids))); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
$filter_sql = $filter_params = ''; |
| 125 |
$offset = empty($_GET['offset']) ? 0 : intval($_GET['offset']); |
| 126 |
$page_limit = empty($_GET['page_limit']) ? 50 : intval($_GET['page_limit']); |
| 127 |
$filter_params .= '&page_limit='.$page_limit; |
| 128 |
|
| 129 |
// reorder questions |
| 130 |
if(!empty($_GET['move'])) { |
| 131 |
WatuQuestion::reorder($_GET['move'], $_GET['quiz'], $_GET['dir']); |
| 132 |
watu_redirect("admin.php?page=watu_questions&quiz=".intval($_GET['quiz']).'&offset='.$offset.'&page_limit='.$page_limit); |
| 133 |
} |
| 134 |
|
| 135 |
// filter by type |
| 136 |
if(!empty($_GET['filter_answer_type'])) { |
| 137 |
$filter_answer_type = $_GET['filter_answer_type']; |
| 138 |
if( !in_array($filter_answer_type, array('radio', 'checkbox', 'textarea'))) $filter_answer_type = 'radio'; |
| 139 |
$filter_sql .= $wpdb->prepare(" AND Q.answer_type = %s ", $filter_answer_type); |
| 140 |
$filter_params .= '&filter_answer_type='.$filter_answer_type; |
| 141 |
} |
| 142 |
|
| 143 |
// filter by cat |
| 144 |
if(!empty($_GET['filter_cat'])) { |
| 145 |
$filter_cat = intval($_GET['filter_cat']); |
| 146 |
|
| 147 |
$filter_sql .= $wpdb->prepare(" AND Q.cat_id = %d ", (($filter_cat == -1) ? 0 : $filter_cat)); |
| 148 |
$filter_params .= '&filter_cat='.$filter_cat; |
| 149 |
} |
| 150 |
|
| 151 |
// filter by ID |
| 152 |
if(!empty($_GET['filter_id'])) { |
| 153 |
// cleanup everything that is not comma or number |
| 154 |
$filter_id = $_GET['filter_id']; |
| 155 |
$filter_id = preg_replace('/[^0-9\s\,]/', '', $filter_id); |
| 156 |
$filter_sql .= " AND Q.ID IN ($filter_id) "; |
| 157 |
$filter_params .= '&filter_id='.$filter_id; |
| 158 |
} |
| 159 |
|
| 160 |
// filter by contents |
| 161 |
if(!empty($_GET['filter_contents'])) { |
| 162 |
$filter_sql .= $wpdb->prepare(" AND Q.question LIKE %s ", '%'.sanitize_text_field($_GET['filter_contents']).'%'); |
| 163 |
$filter_params .= '&filter_contents='.esc_attr($_GET['filter_contents']); |
| 164 |
} |
| 165 |
|
| 166 |
// SQL limit will be used for optimization UNLESS "fix sort order" is selected |
| 167 |
$limit_sql = ''; |
| 168 |
if(empty($_GET['fix_sort_order'])) { |
| 169 |
$limit_sql = " LIMIT $offset, $page_limit"; |
| 170 |
} |
| 171 |
|
| 172 |
// Retrieve the questions |
| 173 |
$all_question = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS Q.ID,Q.question, Q.answer_type as answer_type, |
| 174 |
Q.is_required as is_required, Q.is_inactive as is_inactive, Q.sort_order as sort_order, |
| 175 |
Q.cat_id as cat_id, |
| 176 |
(SELECT tC.name FROM ".WATU_QCATS." tC WHERE tC.ID = Q.cat_id) AS cat, |
| 177 |
(SELECT COUNT(*) FROM ".WATU_ANSWERS." WHERE question_id=Q.ID) AS answer_count |
| 178 |
FROM `".WATU_QUESTIONS."` as Q |
| 179 |
WHERE Q.exam_id=$quiz_id $filter_sql ORDER BY Q.sort_order, Q.ID |
| 180 |
$limit_sql"); |
| 181 |
|
| 182 |
if(empty($filter_sql)) WatuQuestion::fix_sort_order($all_question); |
| 183 |
$count = $num_questions = $wpdb->get_var("SELECT FOUND_ROWS()"); |
| 184 |
|
| 185 |
// question categories |
| 186 |
$cats = $wpdb->get_results("SELECT * FROM ".WATU_QCATS." ORDER BY name"); |
| 187 |
|
| 188 |
if(@file_exists(get_stylesheet_directory().'/watu/questions.html.php')) include get_stylesheet_directory().'/watu/questions.html.php'; |
| 189 |
else include(WATU_PATH . '/views/questions.html.php'); |
| 190 |
} |
| 191 |
|
| 192 |
function watu_question() { |
| 193 |
global $wpdb; |
| 194 |
|
| 195 |
$action = 'new'; |
| 196 |
if($_REQUEST['action'] == 'edit') $action = 'edit'; |
| 197 |
$question_id = intval($_GET['question'] ?? 0); |
| 198 |
|
| 199 |
$all_answers = array(); |
| 200 |
|
| 201 |
if(!empty($question_id)) { |
| 202 |
$question= $wpdb->get_row($wpdb->prepare("SELECT * FROM ".WATU_QUESTIONS." WHERE ID=%d", $question_id)); |
| 203 |
$all_answers = $wpdb->get_results($wpdb->prepare("SELECT answer, correct, point FROM ".WATU_ANSWERS." |
| 204 |
WHERE question_id=%d ORDER BY sort_order", $question_id)); |
| 205 |
} |
| 206 |
|
| 207 |
$ans_type = ($action =='new' and empty($_GET['question'])) ? get_option('watu_answer_type'): $question->answer_type; |
| 208 |
$answer_count = 4; |
| 209 |
if( ($action == 'edit' or !empty($_GET['question'])) and $answer_count < count($all_answers)) $answer_count = count($all_answers) ; |
| 210 |
|
| 211 |
// question categories if any |
| 212 |
$cats = $wpdb->get_results("SELECT * FROM ".WATU_QCATS." ORDER BY name"); |
| 213 |
|
| 214 |
wp_enqueue_editor(); |
| 215 |
wp_enqueue_media(); |
| 216 |
if(@file_exists(get_stylesheet_directory().'/watu/question-form.html.php')) include get_stylesheet_directory().'/watu/question-form.html.php'; |
| 217 |
else include(WATU_PATH . '/views/question-form.html.php'); |
| 218 |
} |
| 219 |
|
| 220 |
// import questions page |
| 221 |
function watu_import_questions() { |
| 222 |
global $wpdb; |
| 223 |
$quiz_id = intval($_GET['quiz_id']); |
| 224 |
$quiz = $wpdb->get_row($wpdb->prepare("SELECT ID, name FROM ".WATU_EXAMS." WHERE ID=%d", $quiz_id)); |
| 225 |
|
| 226 |
if(!empty($_POST['watu_import']) and check_admin_referer('watu_import_questions')) { |
| 227 |
if(empty($_FILES['csv']['name'])) wp_die(__('Please upload file', 'watu')); |
| 228 |
|
| 229 |
// check for non UTF-8 encoding |
| 230 |
$content = file_get_contents($_FILES['csv']['tmp_name']); |
| 231 |
if(!mb_detect_encoding($content, 'UTF-8', true)) $non_utf8_error = true; |
| 232 |
|
| 233 |
$row = 0; |
| 234 |
ini_set("auto_detect_line_endings", true); |
| 235 |
$delimiter = sanitize_text_field($_POST['delimiter']); |
| 236 |
if($delimiter == "tab") $delimiter="\t"; |
| 237 |
|
| 238 |
if (($handle = fopen($_FILES['csv']['tmp_name'], "r")) !== FALSE) { |
| 239 |
if(empty($_POST['import_fails'])) { |
| 240 |
while (($data = fgetcsv($handle, 10000, $delimiter)) !== FALSE) { |
| 241 |
$row++; |
| 242 |
if(empty($data)) continue; |
| 243 |
if(!empty($_POST['skip_title_row']) and $row == 1) continue; |
| 244 |
watu_import_question($data, $quiz_id); |
| 245 |
} // end while |
| 246 |
} else { |
| 247 |
// the customer says that import fails - let's try the handmade import function |
| 248 |
while(($csv_line = fgets($handle, 10000)) !== FALSE) { |
| 249 |
$row++; |
| 250 |
if(empty($csv_line)) continue; |
| 251 |
if(!empty($_POST['skip_title_row']) and $row == 1) continue; |
| 252 |
$data = watu_parse_csv_line($csv_line); |
| 253 |
watu_import_question($data, $quiz_id); |
| 254 |
} // end while |
| 255 |
} // end alternate CSV parsing |
| 256 |
$result = true; |
| 257 |
} // end if $handle |
| 258 |
else $result = false; |
| 259 |
} // end import |
| 260 |
|
| 261 |
if(@file_exists(get_stylesheet_directory().'/watu/import.html.php')) include get_stylesheet_directory().'/watu/import.html.php'; |
| 262 |
else include(WATU_PATH . '/views/import.html.php'); |
| 263 |
} |
| 264 |
|
| 265 |
function watu_import_question($data, $exam_id) { |
| 266 |
global $wpdb; |
| 267 |
if(!function_exists('get_magic_quotes_gpc') or !get_magic_quotes_gpc()) $data[0] = addslashes($data[0]); |
| 268 |
|
| 269 |
// get max sort order |
| 270 |
$sort_order = $wpdb->get_var($wpdb->prepare("SELECT MAX(sort_order) FROM ".WATU_QUESTIONS." |
| 271 |
WHERE exam_id=%d", $exam_id)); |
| 272 |
$sort_order++; |
| 273 |
|
| 274 |
$sql = $wpdb->prepare("INSERT INTO ".WATU_QUESTIONS." SET exam_id=%d, question=%s, answer_type=%s, sort_order=%d, is_required=%d, feedback=%s", |
| 275 |
$exam_id, watu_strip_tags($data[0]), sanitize_text_field($data[1]), $sort_order, intval($data[2]), watu_strip_tags($data[3])); |
| 276 |
$wpdb->query($sql); |
| 277 |
$qid = $wpdb->insert_id; |
| 278 |
|
| 279 |
// add answers |
| 280 |
$data = array_slice($data, 4); |
| 281 |
|
| 282 |
$answers = array(); |
| 283 |
$step = 1; |
| 284 |
foreach($data as $cnt=>$d) { |
| 285 |
if($step == 1) { |
| 286 |
$answer = array(); |
| 287 |
$answer['answer'] = watu_strip_tags($d); |
| 288 |
$step=2; |
| 289 |
continue; |
| 290 |
} |
| 291 |
if($step == 2) { |
| 292 |
$answer['is_correct'] = intval($d); |
| 293 |
$step = 3; |
| 294 |
continue; |
| 295 |
} |
| 296 |
if($step == 3) { |
| 297 |
$answer['points'] = floatval($d); |
| 298 |
$step = 1; |
| 299 |
$answers[] = $answer; |
| 300 |
} |
| 301 |
} // end filling answers array |
| 302 |
|
| 303 |
// finally insert them |
| 304 |
$vals = array(); |
| 305 |
foreach($answers as $cnt=>$answer) { |
| 306 |
if($answer['answer'] === '') continue; |
| 307 |
$cnt++; |
| 308 |
$vals[] = $wpdb->prepare("(%d, %s, %s, %s, %d)", $qid, $answer['answer'], |
| 309 |
$answer['is_correct'], $answer['points'], $cnt); |
| 310 |
} |
| 311 |
$values_sql = implode(",",$vals); |
| 312 |
|
| 313 |
if(count($answers)) { $wpdb->query("INSERT INTO ".WATU_ANSWERS." (question_id,answer,correct,point, sort_order) |
| 314 |
VALUES $values_sql"); } |
| 315 |
} |
| 316 |
|
| 317 |
|