| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
// ajax calls |
| 5 |
function watu_submit() { |
| 6 |
if (!check_ajax_referer('watu_submit_nonce', 'watu_nonce', false)) { |
| 7 |
wp_die(__('Security check failed.', 'watu'), 403); |
| 8 |
} |
| 9 |
require_once(WATU_PATH."/controllers/show_exam.php"); |
| 10 |
} |
| 11 |
|
| 12 |
function watu_already_rated() { |
| 13 |
update_option('watu_rated', 1, false); |
| 14 |
} |
| 15 |
|
| 16 |
function watu_reorder_questions() { |
| 17 |
global $wpdb; |
| 18 |
|
| 19 |
if(!current_user_can('watu_manage') and !current_user_can('manage_options')) return; |
| 20 |
|
| 21 |
// Verify nonce for CSRF protection |
| 22 |
if(!check_admin_referer('watu_reorder_questions', 'watu_nonce')) return; |
| 23 |
|
| 24 |
// fill all question IDs in array in the same way they come from the sortable Ajax call |
| 25 |
$qids = [-1]; |
| 26 |
$questions = $_POST['questions'] ?? []; |
| 27 |
$exam_id = intval($_POST['exam_id']); |
| 28 |
|
| 29 |
foreach($questions as $question) { |
| 30 |
$id = intval(str_replace('question-', '', $question)); |
| 31 |
$qids[] = intval($id); |
| 32 |
} |
| 33 |
//print_r($qids); |
| 34 |
// find the min sort order for the group |
| 35 |
$min_sort_order = $wpdb->get_var($wpdb->prepare("SELECT MIN(sort_order) FROM ".WATU_QUESTIONS." |
| 36 |
WHERE exam_id=%d AND ID IN (".implode(',', $qids).")", $exam_id)); |
| 37 |
|
| 38 |
// go through the questions and increment the min for each of them |
| 39 |
foreach($qids as $qid) { |
| 40 |
if($qid == -1) continue; |
| 41 |
$wpdb->query($wpdb->prepare("UPDATE ".WATU_QUESTIONS." SET sort_order=%d WHERE ID=%d", $min_sort_order, $qid)); |
| 42 |
$min_sort_order++; |
| 43 |
} |
| 44 |
} |
| 45 |
|