# watu/trunk/controllers/ajax.php

Watu Quiz, version trunk. 45 lines.

- Page: https://pluginprobe.com/plugins/watu/trunk/code/controllers/ajax.php
- Raw: https://pluginprobe.com/plugins/watu/trunk/raw/controllers/ajax.php
- Modified: 2026-08-04T07:34:36+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/watu/trunk/code/controllers/ajax.php#L10-L20`.

```php
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

// ajax calls
function watu_submit() {
	if (!check_ajax_referer('watu_submit_nonce', 'watu_nonce', false)) {
		wp_die(__('Security check failed.', 'watu'), 403);
	}
	require_once(WATU_PATH."/controllers/show_exam.php");
}

function watu_already_rated() {
	update_option('watu_rated', 1, false);
}

function watu_reorder_questions() {
	global $wpdb;

	if(!current_user_can('watu_manage') and !current_user_can('manage_options')) return;
	
	// Verify nonce for CSRF protection
	if(!check_admin_referer('watu_reorder_questions', 'watu_nonce')) return;

	// fill all question IDs in array in the same way they come from the sortable Ajax call
	$qids = [-1];
	$questions = $_POST['questions'] ?? [];
	$exam_id = intval($_POST['exam_id']);

	foreach($questions as $question) {
		$id = intval(str_replace('question-', '', $question));
		$qids[] = intval($id);
	}
	//print_r($qids);
	// find the min sort order for the group
	$min_sort_order = $wpdb->get_var($wpdb->prepare("SELECT MIN(sort_order) FROM ".WATU_QUESTIONS."
		WHERE exam_id=%d AND ID IN (".implode(',', $qids).")", $exam_id));

	// go through the questions and increment the min for each of them
	foreach($qids as $qid) {
		if($qid == -1) continue;
		$wpdb->query($wpdb->prepare("UPDATE ".WATU_QUESTIONS." SET sort_order=%d WHERE ID=%d", $min_sort_order, $qid));
		$min_sort_order++;
	}
}

```
