function can_retake($exam) { global $wpdb, $user_ID; // no login required but have a restriction by IP if (!empty($exam->takings_by_ip)) { // select number of takings by this IP address $num_taken = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM " . WATUPRO_TAKEN_EXAMS . "\n\t\t\t\tWHERE exam_id=%d AND ip=%s AND in_progress=0", $exam->ID, $_SERVER['REMOTE_ADDR'])); if ($exam->takings_by_ip <= $num_taken) { echo "<p><b>"; printf(__("Sorry, you can take this quiz only %d times.", 'watupro'), $exam->takings_by_ip); echo "</b></p>"; return false; } } // end IP based check // no limits if login is not required if (!$exam->require_login) { return true; } if ($exam->take_again) { // Intelligence limitations if (watupro_intel()) { require_once WATUPRO_PATH . "/i/models/exam_intel.php"; if (!WatuPROIExam::can_retake($exam)) { return false; } } if (empty($exam->times_to_take) and (empty($exam->retake_grades) or strlen($exam->retake_grades) <= 2)) { return true; } // 0 = unlimited // now select number of takings if (!is_user_logged_in()) { echo __("Sorry, you are not allowed to submit this quiz.", 'watupro'); return false; } $cnt_takings = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM " . WATUPRO_TAKEN_EXAMS . "\n\t\t\t\tWHERE exam_id=%d AND user_id=%d AND in_progress=0", $exam->ID, $user_ID)); if (!$cnt_takings) { return true; } // if there are no takings no need to check further if (!empty($exam->times_to_take) and $cnt_takings >= $exam->times_to_take) { echo "<p><b>"; printf(__("Sorry, you can take this quiz only %d times.", 'watupro'), $exam->times_to_take); echo "</b></p>"; return false; } // all OK so far? Let's see if we have grade-based limitation and there are previous takings if (!empty($exam->retake_grades) and strlen($exam->retake_grades) > 2 and $cnt_takings) { $grids = explode("|", $exam->retake_grades); $grids = array_filter($grids); if (sizeof($grids)) { // get latest taking $latest_taking = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_TAKEN_EXAMS . " \n\t\t\t\t\t\tWHERE exam_id=%d AND user_id=%d AND in_progress=0 ORDER BY ID DESC LIMIT 1", $exam->ID, $user_ID)); if (!in_array($latest_taking->grade_id, $grids)) { echo "<p><b>"; _e("You can't take this quiz again because of the latest grade you achieved on it.", 'watupro'); echo "</b></p>"; return false; } } } // end grade-related limitation check } else { // Only 1 taking allowed: see if exam is already taken by this user $taking = $this->get_taking($exam); if (!empty($taking->ID) and !$taking->in_progress) { echo "<p><b>"; printf(__("Sorry, you can take this %s only once!", 'watupro'), __('quiz', 'watupro')); echo "</b></p>"; return false; } } // just in case return true; }