Example #1
0
function task_change_stage()
{
    global $wpdb;
    $unix_now = current_time('timestamp');
    // Current unix timestamp
    $post_id = $_POST['post_id'];
    // Post id posted from ajax function
    $user_id = $_POST['user_id'];
    // User id posted from ajax function
    $status = (int) $_POST['status'];
    // Task's status posted from ajax function
    $page_id = $_POST['page_id'];
    // Page id posted from ajax function
    $admin_name = $_POST['admin_name'];
    $undo = $_POST['undo'];
    // Boolean which determines if the button clicked is an undo button or not (True or False)
    $pass = $_POST['pass'];
    // Contains the user-entered admin password
    $url = $_POST['url'];
    // Contains user-entered url
    $repeat_button = $_POST['repeat'];
    // Boolean which determines if the task is repeatable or not (True or False)
    $points_array = $_POST['points'];
    // Serialized array of points rewarded for each stage
    $currency_array = $_POST['currency'];
    // Serialized array of currency rewarded for each stage
    $bonus_currency_array = $_POST['bonus_currency'];
    // Serialized array of bonus currency awarded for each stage
    $date_update_percent = $_POST['date_update_percent'];
    // Float which is used to modify values saved to database
    $chain_name = $_POST['chain_name'];
    // String which is used to display next task in a quest chain
    $next_post_id_in_chain = $_POST['next_post_id_in_chain'];
    // Integer which is used to display next task in a quest chain
    $last_in_chain = $_POST['last_in_chain'];
    // Boolean which determines if the current quest is last in chain
    $final_chain_message = $_POST['final_chain_message'];
    $number_of_stages = $_POST['number_of_stages'];
    // Integer with number of stages in the task
    $go_table_name = "{$wpdb->prefix}go";
    $task_count = $wpdb->get_var("SELECT `count` FROM {$go_table_name} WHERE post_id = {$post_id} AND uid = {$user_id}");
    $custom_fields = get_post_custom($post_id);
    // Just gathering some data about this task with its post id
    $mastery_active = !empty($custom_fields['go_mta_task_mastery'][0]) ? !$custom_fields['go_mta_task_mastery'][0] : true;
    // whether or not the mastery stage is active
    $repeat = !empty($custom_fields['go_mta_task_repeat'][0]) ? $custom_fields['go_mta_task_repeat'][0] : '';
    // Whether or not you can repeat the task
    $e_admin_lock = !empty($custom_fields['go_mta_encounter_admin_lock'][0]) ? unserialize($custom_fields['go_mta_encounter_admin_lock'][0]) : null;
    if (!empty($e_admin_lock)) {
        $e_is_locked = $e_admin_lock[0];
        if ($e_is_locked === 'true') {
            $e_pass_lock = $e_admin_lock[1];
        }
    }
    $a_admin_lock = !empty($custom_fields['go_mta_accept_admin_lock'][0]) ? unserialize($custom_fields['go_mta_accept_admin_lock'][0]) : null;
    if (!empty($a_admin_lock)) {
        $a_is_locked = $a_admin_lock[0];
        if ($a_is_locked === 'true') {
            $a_pass_lock = $a_admin_lock[1];
        }
    }
    $c_admin_lock = !empty($custom_fields['go_mta_completion_admin_lock'][0]) ? unserialize($custom_fields['go_mta_completion_admin_lock'][0]) : null;
    if (!empty($c_admin_lock)) {
        $c_is_locked = $c_admin_lock[0];
        if ($c_is_locked === 'true') {
            $c_pass_lock = $c_admin_lock[1];
        }
    }
    $m_admin_lock = !empty($custom_fields['go_mta_mastery_admin_lock'][0]) ? unserialize($custom_fields['go_mta_mastery_admin_lock'][0]) : null;
    if (!empty($m_admin_lock)) {
        $m_is_locked = $m_admin_lock[0];
        if ($m_is_locked === 'true') {
            $m_pass_lock = $m_admin_lock[1];
        }
    }
    $r_admin_lock = !empty($custom_fields['go_mta_repeat_admin_lock'][0]) ? unserialize($custom_fields['go_mta_repeat_admin_lock'][0]) : null;
    if (!empty($r_admin_lock)) {
        $r_is_locked = $r_admin_lock[0];
        if ($r_is_locked === 'true') {
            $r_pass_lock = $r_admin_lock[1];
        }
    }
    $e_url_is_locked = !empty($custom_fields['go_mta_encounter_url_key'][0]) ? true : false;
    $a_url_is_locked = !empty($custom_fields['go_mta_accept_url_key'][0]) ? true : false;
    $c_url_is_locked = !empty($custom_fields['go_mta_completion_url_key'][0]) ? true : false;
    $m_url_is_locked = !empty($custom_fields['go_mta_mastery_url_key'][0]) ? true : false;
    if (!empty($pass) || "0" === $pass) {
        if ($status == 4) {
            $temp_status = $status;
        } else {
            $temp_status = $status - 1;
        }
        switch ($temp_status) {
            case 1:
                $pass_lock = $e_pass_lock;
                break;
            case 2:
                $pass_lock = $a_pass_lock;
                break;
            case 3:
                $pass_lock = $c_pass_lock;
                break;
            case 4:
                if ($repeat === 'on') {
                    $pass_lock = $r_pass_lock;
                } else {
                    $pass_lock = $m_pass_lock;
                }
                break;
        }
        if (!empty($pass_lock) && $pass !== $pass_lock) {
            echo 0;
            die;
        }
    }
    $test_e_active = !empty($custom_fields['go_mta_test_encounter_lock'][0]) ? $custom_fields['go_mta_test_encounter_lock'][0] : false;
    $test_a_active = !empty($custom_fields['go_mta_test_accept_lock'][0]) ? $custom_fields['go_mta_test_accept_lock'][0] : false;
    $test_c_active = !empty($custom_fields['go_mta_test_completion_lock'][0]) ? $custom_fields['go_mta_test_completion_lock'][0] : false;
    if ($test_e_active) {
        $test_e_array = go_get_test_meta_content($custom_fields, 'encounter');
        $test_e_returns = $test_e_array[0];
        $test_e_num = $test_e_array[1];
        $test_e_all_questions = $test_e_array[2][0];
        $test_e_all_types = $test_e_array[2][1];
        $test_e_all_answers = $test_e_array[2][2];
        $test_e_all_keys = $test_e_array[2][3];
    }
    $encounter_upload = !empty($custom_fields['go_mta_encounter_upload'][0]) ? $custom_fields['go_mta_encounter_upload'][0] : false;
    if ($test_a_active) {
        $test_a_array = go_get_test_meta_content($custom_fields, 'accept');
        $test_a_returns = $test_a_array[0];
        $test_a_num = $test_a_array[1];
        $test_a_all_questions = $test_a_array[2][0];
        $test_a_all_types = $test_a_array[2][1];
        $test_a_all_answers = $test_a_array[2][2];
        $test_a_all_keys = $test_a_array[2][3];
    }
    $accept_upload = !empty($custom_fields['go_mta_accept_upload'][0]) ? $custom_fields['go_mta_accept_upload'][0] : false;
    if ($test_c_active) {
        $test_c_array = go_get_test_meta_content($custom_fields, 'completion');
        $test_c_returns = $test_c_array[0];
        $test_c_num = $test_c_array[1];
        $test_c_all_questions = $test_c_array[2][0];
        $test_c_all_types = $test_c_array[2][1];
        $test_c_all_answers = $test_c_array[2][2];
        $test_c_all_keys = $test_c_array[2][3];
    }
    $completion_message = !empty($custom_fields['go_mta_complete_message'][0]) ? $custom_fields['go_mta_complete_message'][0] : '';
    $completion_upload = !empty($custom_fields['go_mta_completion_upload'][0]) ? $custom_fields['go_mta_completion_upload'][0] : false;
    if ($mastery_active) {
        $test_m_active = !empty($custom_fields['go_mta_test_mastery_lock'][0]) ? $custom_fields['go_mta_test_mastery_lock'][0] : false;
        $bonus_loot = !empty($custom_fields['go_mta_mastery_bonus_loot'][0]) ? unserialize($custom_fields['go_mta_mastery_bonus_loot'][0]) : null;
        if ($test_m_active) {
            $test_m_array = go_get_test_meta_content($custom_fields, 'mastery');
            $test_m_returns = $test_m_array[0];
            $test_m_num = $test_m_array[1];
            $test_m_all_questions = $test_m_array[2][0];
            $test_m_all_types = $test_m_array[2][1];
            $test_m_all_answers = $test_m_array[2][2];
            $test_m_all_keys = $test_m_array[2][3];
        }
        $mastery_message = !empty($custom_fields['go_mta_mastery_message'][0]) ? $custom_fields['go_mta_mastery_message'][0] : '';
        $mastery_upload = !empty($custom_fields['go_mta_mastery_upload'][0]) ? $custom_fields['go_mta_mastery_upload'][0] : false;
        if ($repeat == 'on') {
            $repeat_amount = !empty($custom_fields['go_mta_repeat_amount'][0]) ? $custom_fields['go_mta_repeat_amount'][0] : 0;
            $repeat_message = !empty($custom_fields['go_mta_repeat_message'][0]) ? $custom_fields['go_mta_repeat_message'][0] : '';
            $repeat_upload = !empty($custom_fields['go_mta_repeat_upload'][0]) ? $custom_fields['go_mta_repeat_upload'][0] : false;
        }
    }
    $description = !empty($custom_fields['go_mta_quick_desc'][0]) ? $custom_fields['go_mta_quick_desc'][0] : '';
    // Array of badge switch and badges associated with a stage
    // E.g. array( true, array( 263, 276 ) ) means that stage has badges (true) and the badge IDs are 263 and 276
    $stage_badges = array(!empty($custom_fields['go_mta_stage_one_badge'][0]) ? unserialize($custom_fields['go_mta_stage_one_badge'][0]) : null, !empty($custom_fields['go_mta_stage_two_badge'][0]) ? unserialize($custom_fields['go_mta_stage_two_badge'][0]) : null, !empty($custom_fields['go_mta_stage_three_badge'][0]) ? unserialize($custom_fields['go_mta_stage_three_badge'][0]) : null, !empty($custom_fields['go_mta_stage_four_badge'][0]) ? unserialize($custom_fields['go_mta_stage_four_badge'][0]) : null, !empty($custom_fields['go_mta_stage_five_badge'][0]) ? unserialize($custom_fields['go_mta_stage_five_badge'][0]) : null);
    // Stage Stuff
    $content_post = get_post($post_id);
    $task_content = $content_post->post_content;
    if ($task_content == '') {
        $accept_message = !empty($custom_fields['go_mta_accept_message'][0]) ? $custom_fields['go_mta_accept_message'][0] : '';
    } else {
        $accept_message = $content_post->post_content;
    }
    // Tests failed.
    if (isset($_SESSION['test_encounter_fail_count'])) {
        $e_fail_count = $_SESSION['test_encounter_fail_count'];
    } else {
        $e_fail_count = 0;
    }
    if (isset($_SESSION['test_accept_fail_count'])) {
        $a_fail_count = $_SESSION['test_accept_fail_count'];
    } else {
        $a_fail_count = 0;
    }
    if (isset($_SESSION['test_completion_fail_count'])) {
        $c_fail_count = $_SESSION['test_completion_fail_count'];
    } else {
        $c_fail_count = 0;
    }
    if (isset($_SESSION['test_mastery_fail_count'])) {
        $m_fail_count = $_SESSION['test_mastery_fail_count'];
    } else {
        $m_fail_count = 0;
    }
    // Tests passed.
    if (isset($_SESSION['test_encounter_passed'])) {
        $e_passed = $_SESSION['test_encounter_passed'];
    } else {
        $e_passed = 0;
    }
    if (isset($_SESSION['test_accept_passed'])) {
        $a_passed = $_SESSION['test_accept_passed'];
    } else {
        $a_passed = 0;
    }
    if (isset($_SESSION['test_completion_passed'])) {
        $c_passed = $_SESSION['test_completion_passed'];
    } else {
        $c_passed = 0;
    }
    if (isset($_SESSION['test_mastery_passed'])) {
        $m_passed = $_SESSION['test_mastery_passed'];
    } else {
        $m_passed = 0;
    }
    $db_status = (int) $wpdb->get_var("SELECT `status` FROM {$go_table_name} WHERE uid = {$user_id} AND post_id = {$post_id}");
    $future_switches = !empty($custom_fields['go_mta_time_filters'][0]) ? unserialize($custom_fields['go_mta_time_filters'][0]) : null;
    //determine which future date modifier is on, if any
    $date_picker = !empty($custom_fields['go_mta_date_picker'][0]) && unserialize($custom_fields['go_mta_date_picker'][0]) ? array_filter(unserialize($custom_fields['go_mta_date_picker'][0])) : false;
    // If there are dates in the date picker
    if (!empty($date_picker) && (!empty($future_switches) && $future_switches['calendar'] == 'on')) {
        $dates = $date_picker['date'];
        $times = $date_picker['time'];
        $percentages = $date_picker['percent'];
        $past_dates = array();
        foreach ($dates as $key => $date) {
            if ($unix_now >= strtotime($date) + strtotime($times[$key], 0)) {
                $past_dates[$key] = abs($unix_now - strtotime($date));
            }
        }
        if (!empty($past_dates)) {
            asort($past_dates);
            $date_update_percent = (double) ((100 - $percentages[key($past_dates)]) / 100);
        } else {
            $date_update_percent = 1;
        }
        ?>
		<script type='text/javascript'>
			var num_of_stages = parseInt( <?php 
        echo $number_of_stages;
        ?>
 );
			var date_modifier = parseFloat( <?php 
        echo $date_update_percent;
        ?>
 );
			for ( i = 1; i <= num_of_stages; i++ ) {
				var stage_points = jQuery( '#go_stage_' + i + '_points' );
				var stage_currency = jQuery( '#go_stage_' + i + '_currency' );
				var stage_bonus_currency = jQuery( '#go_stage_' + i + '_bonus_currency' );
				if ( stage_points.length && ! stage_points.hasClass( 'go_updated' ) ) {
					stage_points.html( Math.floor( parseInt( stage_points.html() ) * date_modifier ) ).addClass( 'go_updated' );
				}
				if ( stage_currency.length && ! stage_currency.hasClass( 'go_updated' ) ) {
					stage_currency.html( Math.floor( parseInt( stage_currency.html() ) * date_modifier ) ).addClass( 'go_updated' );
				}
				if ( stage_bonus_currency.length && ! stage_bonus_currency.hasClass( 'go_updated' ) ) {
					stage_bonus_currency.html( Math.floor( parseInt( stage_bonus_currency.html() ) * date_modifier ) ).addClass( 'go_updated' );
				}
			}
		</script>
		<?php 
    } else {
        $date_update_percent = 1;
    }
    $future_modifier = !empty($custom_fields['go_mta_time_modifier'][0]) ? unserialize($custom_fields['go_mta_time_modifier'][0]) : null;
    if (!empty($future_modifier) && (!empty($future_switches['future']) && $future_switches['future'] == 'on') && !($future_modifier['days'] == 0 && $future_modifier['hours'] == 0 && $future_modifier['minutes'] == 0 && $future_modifier['seconds'] == 0)) {
        $user_timers = get_user_meta($user_id, 'go_timers');
        $accept_timestamp_db = strtotime(str_replace('@', ' ', $wpdb->get_var("SELECT timestamp FROM {$wpdb->prefix}go WHERE uid='{$user_id}' AND post_id='{$post_id}'")));
        $accept_timestamp = !empty($user_timers[0][$post_id]) ? $user_timers[0][$post_id] : (!empty($accept_timestamp_db) ? $accept_timestamp_db : ($status == 2 ? $unix_now : 0));
        $days = (int) $future_modifier['days'];
        $hours = (int) $future_modifier['hours'];
        $minutes = (int) $future_modifier['minutes'];
        $seconds = (int) $future_modifier['seconds'];
        $future_time = strtotime("{$days} days", 0) + strtotime("{$hours} hours", 0) + strtotime("{$minutes} minutes", 0) + strtotime("{$seconds} seconds", 0) + $accept_timestamp;
        if ($status == 2 || ($undo == 'true' || $undo === true) && $status >= 2 && $db_status < 4) {
            go_task_timer($post_id, $user_id, $future_modifier);
        } elseif ($status > 2) {
            ?>
          	<script type='text/javascript'>
				jQuery( '#go_future_notification' ).hide();
				jQuery( '#go_task_timer' ).remove();
			</script>
            <?php 
        }
        if ($unix_now >= $future_time) {
            $future_update_percent = (double) ((100 - $future_modifier['percentage']) / 100);
        } else {
            $future_update_percent = 1;
        }
    } else {
        $future_update_percent = 1;
    }
    $complete_stage = $undo ? $status - 1 : $status;
    if (!empty($future_switches['calendar']) && $future_switches['calendar'] == 'on') {
        $update_percent = $date_update_percent;
    } elseif (!empty($future_switches['future']) && $future_switches['future'] == 'on' && $complete_stage == 3 && $db_status < 4) {
        $update_percent = $future_update_percent;
    } else {
        $update_percent = 1;
    }
    // if the button pressed IS the repeat button...
    if ($repeat_button == 'on') {
        if ($undo == 'true' || $undo === true) {
            if ($task_count > 0) {
                go_add_post($user_id, $post_id, $status, -floor($update_percent * $points_array[$status]), -floor($update_percent * $currency_array[$status]), -floor($update_percent * $bonus_currency_array[$status]), null, $page_id, $repeat_button, -1, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed);
                if ($bonus_loot[0]) {
                    if (!empty($bonus_loot[1])) {
                        foreach ($bonus_loot[1] as $store_item => $on) {
                            if ($on === 'on') {
                                $store_custom_fields = get_post_custom($store_item);
                                $store_cost = !empty($store_custom_fields['go_mta_store_cost'][0]) ? unserialize($store_custom_fields['go_mta_store_cost'][0]) : array();
                                $currency = $store_cost[0] < 0 ? $store_cost[0] : 0;
                                $points = $store_cost[1] < 0 ? $store_cost[1] : 0;
                                $bonus_currency = $store_cost[2] < 0 ? $store_cost[2] : 0;
                                $penalty = $store_cost[3] > 0 ? $store_cost[3] : 0;
                                $minutes = $store_cost[4] < 0 ? $store_cost[4] : 0;
                                $user_id = get_current_user_id();
                                $received = $wpdb->query($wpdb->prepare("SELECT * FROM {$go_table_name}  WHERE uid = %d AND status = %d AND gifted = %d AND post_id = %d AND reason = 'Quest' OR reason = 'Bonus' ORDER BY timestamp DESC, reason DESC, id DESC LIMIT 1", $user_id, -1, 0, $store_item));
                                if ($received) {
                                    go_update_totals($user_id, $points, $currency, $bonus_currency, 0, $minutes, null, false, true);
                                }
                                $wpdb->query($wpdb->prepare("DELETE FROM {$go_table_name}  WHERE uid = %d AND status = %d AND gifted = %d AND post_id = %d AND reason = 'Quest' OR reason = 'Bonus' ORDER BY timestamp DESC, reason DESC, id DESC LIMIT 1", $user_id, -1, 0, $store_item));
                            }
                        }
                    }
                }
            } else {
                go_add_post($user_id, $post_id, $status - 1, -floor($update_percent * $points_array[$status - 1]), -floor($update_percent * $currency_array[$status - 1]), -floor($update_percent * $bonus_currency_array[$status - 1]), null, $page_id, $repeat_button, 0, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed);
                if ($bonus_loot[0]) {
                    if (!empty($bonus_loot[1])) {
                        foreach ($bonus_loot[1] as $store_item => $on) {
                            if ($on === 'on') {
                                $store_custom_fields = get_post_custom($store_item);
                                $store_cost = !empty($store_custom_fields['go_mta_store_cost'][0]) ? unserialize($store_custom_fields['go_mta_store_cost'][0]) : array();
                                $currency = $store_cost[0] < 0 ? $store_cost[0] : 0;
                                $points = $store_cost[1] < 0 ? $store_cost[1] : 0;
                                $bonus_currency = $store_cost[2] < 0 ? $store_cost[2] : 0;
                                $penalty = $store_cost[3] > 0 ? $store_cost[3] : 0;
                                $minutes = $store_cost[4] < 0 ? $store_cost[4] : 0;
                                $loot_reason = $bonus_loot[2][$store_item] * 10 > 999 ? 'quest' : 'bonus';
                                $user_id = get_current_user_id();
                                $received = $wpdb->query($wpdb->prepare("SELECT * FROM {$go_table_name}  WHERE uid = %d AND status = %d AND gifted = %d AND post_id = %d AND reason = 'Quest' OR reason = 'Bonus' ORDER BY timestamp DESC, reason DESC, id DESC LIMIT 1", $user_id, -1, 0, $store_item));
                                if ($received) {
                                    go_update_totals($user_id, $points, $currency, $bonus_currency, 0, $minutes, null, $loot_reason, true, false);
                                }
                                $wpdb->query($wpdb->prepare("DELETE FROM {$go_table_name}  WHERE uid = %d AND status = %d AND gifted = %d AND post_id = %d AND reason = 'Quest' OR reason = 'Bonus' ORDER BY timestamp DESC, reason DESC, id DESC LIMIT 1", $user_id, -1, 0, $store_item));
                            }
                        }
                    }
                }
                if ($stage_badges[$status][0] == 'true') {
                    foreach ($stage_badges[$status][1] as $badge_id) {
                        go_remove_badge($user_id, $badge_id);
                    }
                }
            }
        } else {
            // if repeat is on and undo is not hit...
            go_add_post($user_id, $post_id, $status, floor($update_percent * $points_array[$status]), floor($update_percent * $currency_array[$status]), floor($update_percent * $bonus_currency_array[$status]), null, $page_id, $repeat_button, 1, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, $url);
            if ($stage_badges[$status][0] == 'true') {
                foreach ($stage_badges[$status][1] as $badge_id) {
                    do_shortcode("[go_award_badge id='{$badge_id}' repeat='off' uid='{$user_id}']");
                }
            }
        }
        // if the button pressed is NOT the repeat button...
    } else {
        $db_status = (int) $wpdb->get_var("SELECT `status` FROM {$go_table_name} WHERE uid = {$user_id} AND post_id = {$post_id}");
        if ($db_status == 0 || $db_status < $status) {
            if ($undo == 'true' || $undo === true) {
                if ($task_count > 0) {
                    go_add_post($user_id, $post_id, $status, -floor($update_percent * $points_array[$status - 1]), -floor($update_percent * $currency_array[$status - 1]), -floor($update_percent * $bonus_currency_array[$status - 1]), null, $page_id, $repeat_button, -1, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed);
                } else {
                    go_add_post($user_id, $post_id, $status - 2, -floor($update_percent * $points_array[$status - 2]), -floor($update_percent * $currency_array[$status - 2]), -floor($update_percent * $bonus_currency_array[$status - 2]), null, $page_id, $repeat_button, 0, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed);
                    if ($stage_badges[$status - 2][0] == 'true') {
                        foreach ($stage_badges[$status - 2][1] as $badge_id) {
                            go_remove_badge($user_id, $badge_id);
                        }
                    }
                }
            } else {
                $update_time = $status == 2 ? true : false;
                go_add_post($user_id, $post_id, $status, floor($update_percent * $points_array[$status - 1]), floor($update_percent * $currency_array[$status - 1]), floor($update_percent * $bonus_currency_array[$status - 1]), null, $page_id, $repeat_button, 0, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, $url, $update_time);
                if ($stage_badges[$status - 1][0] == 'true') {
                    foreach ($stage_badges[$status - 1][1] as $badge_id) {
                        do_shortcode("[go_award_badge id='{$badge_id}' repeat='off' uid='{$user_id}']");
                    }
                }
            }
        }
    }
    // redefine the status and task_count because they have been updated as soon as the above go_add_post() calls are made.
    $status = $wpdb->get_var("SELECT `status` FROM {$go_table_name} WHERE uid = {$user_id} AND post_id = {$post_id}");
    $task_count = $wpdb->get_var("SELECT `count` FROM {$go_table_name} WHERE post_id = {$post_id} AND uid = {$user_id}");
    if ($undo == "false") {
        if ($task_count == 1) {
            go_record_stage_time($post_id, 5);
        } else {
            go_record_stage_time($post_id, $status);
        }
    }
    switch ($status) {
        case 0:
            $db_task_stage_upload_var = 'e_uploaded';
            break;
        case 1:
            $db_task_stage_upload_var = 'a_uploaded';
            break;
        case 2:
            $db_task_stage_upload_var = 'c_uploaded';
            break;
        case 3:
            $db_task_stage_upload_var = 'm_uploaded';
            break;
        case 4:
            $db_task_stage_upload_var = 'r_uploaded';
            break;
    }
    if (!empty($db_task_stage_upload_var)) {
        $is_uploaded = $wpdb->get_var("SELECT {$db_task_stage_upload_var} FROM {$go_table_name} WHERE uid = {$user_id} AND post_id = {$post_id}");
    } else {
        $is_uploaded = 0;
    }
    // Controls output of the page, after the #go_button has been pressed for the first time while viewing the page.
    switch ($status) {
        case 1:
            echo '<div id="new_content">' . '<div class="go_stage_message">' . do_shortcode(wpautop($accept_message, false)) . '</div>';
            if ($test_e_active) {
                echo "<p id='go_test_error_msg' style='color: red;'></p>";
                if ($test_e_num > 1) {
                    for ($i = 0; $i < $test_e_num; $i++) {
                        echo do_shortcode("[go_test type='" . $test_e_all_types[$i] . "' question='" . $test_e_all_questions[$i] . "' possible_answers='" . $test_e_all_answers[$i] . "' key='" . $test_e_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_e_num . "']");
                    }
                    echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                } else {
                    echo do_shortcode("[go_test type='" . $test_e_all_types[0] . "' question='" . $test_e_all_questions[0] . "' possible_answers='" . $test_e_all_answers[0] . "' key='" . $test_e_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                }
            }
            if ($encounter_upload) {
                echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
            }
            echo "<p id='go_stage_error_msg' style='display: none; color: red;'></p>";
            if ($e_is_locked === 'true' && !empty($e_pass_lock)) {
                echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
            } elseif ($e_url_is_locked === true) {
                echo "<input id='go_url_key' type='url' placeholder='Enter Url'/></br>";
            }
            echo "<button id='go_button' status='2' onclick='task_stage_change( this );'";
            if ($e_is_locked === 'true' && empty($e_pass_lock)) {
                echo "admin_lock='true'";
            }
            echo ">" . go_return_options('go_second_stage_button') . "</button>\n\t\t\t<button id='go_abandon_task' onclick='go_task_abandon();this.disabled = true;'>" . get_option('go_abandon_stage_button', 'Abandon') . "</button></div>" . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "");
            break;
        case 2:
            echo '<div id="new_content">' . '<div class="go_stage_message">' . do_shortcode(wpautop($accept_message, false)) . '</div>';
            if ($test_a_active) {
                echo "<p id='go_test_error_msg' style='color: red;'></p>";
                if ($test_a_num > 1) {
                    for ($i = 0; $i < $test_a_num; $i++) {
                        echo do_shortcode("[go_test type='" . $test_a_all_types[$i] . "' question='" . $test_a_all_questions[$i] . "' possible_answers='" . $test_a_all_answers[$i] . "' key='" . $test_a_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_a_num . "']");
                    }
                    echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                } else {
                    echo do_shortcode("[go_test type='" . $test_a_all_types[0] . "' question='" . $test_a_all_questions[0] . "' possible_answers='" . $test_a_all_answers[0] . "' key='" . $test_a_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                }
            }
            if ($accept_upload) {
                echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
            }
            echo "<p id='go_stage_error_msg' style='display: none; color: red;'></p>";
            if ($a_is_locked === 'true' && !empty($a_pass_lock)) {
                echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
            } elseif ($a_url_is_locked === true) {
                echo "<input id='go_url_key' type='url' placeholder='Enter Url'/></br>";
            }
            echo "<button id='go_button' status='3' onclick='task_stage_change( this );'";
            if ($a_is_locked === 'true' && empty($a_pass_lock)) {
                echo "admin_lock='true'";
            }
            echo '>' . go_return_options('go_third_stage_button') . '</button> <button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button></div>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "");
            break;
        case 3:
            echo '<div class="go_stage_message">' . do_shortcode(wpautop($accept_message, false)) . '</div>' . '<div id="new_content"><div class="go_stage_message">' . do_shortcode(wpautop($completion_message)) . '</div>';
            if ($mastery_active) {
                if ($test_c_active) {
                    echo "<p id='go_test_error_msg' style='color: red;'></p>";
                    if ($test_c_num > 1) {
                        for ($i = 0; $i < $test_c_num; $i++) {
                            echo do_shortcode("[go_test type='" . $test_c_all_types[$i] . "' question='" . $test_c_all_questions[$i] . "' possible_answers='" . $test_c_all_answers[$i] . "' key='" . $test_c_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_c_num . "']");
                        }
                        echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    } else {
                        echo do_shortcode("[go_test type='" . $test_c_all_types[0] . "' question='" . $test_c_all_questions[0] . "' possible_answers='" . $test_c_all_answers[0] . "' key='" . $test_c_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    }
                }
                if ($completion_upload) {
                    echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                }
                echo "<p id='go_stage_error_msg' style='display: none; color: red;'></p>";
                if ($c_is_locked === 'true' && !empty($c_pass_lock)) {
                    echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
                } elseif ($c_url_is_locked === true) {
                    echo "<input id='go_url_key' type='url' placeholder='Enter Url'/></br>";
                }
                echo "<button id='go_button' status='4' onclick='task_stage_change( this );'";
                if ($c_is_locked === 'true' && empty($c_pass_lock)) {
                    echo "admin_lock='true'";
                }
                echo '>' . go_return_options('go_fourth_stage_button') . '</button> 
				<button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>';
                if ($next_post_id_in_chain != 0 && $last_in_chain !== 'true') {
                    echo '<div class="go_chain_message">Next ' . strtolower(go_return_options('go_tasks_name')) . ' in ' . $chain_name . ': <a href="' . get_permalink($next_post_id_in_chain) . '">' . get_the_title($next_post_id_in_chain) . '</a></div>';
                } else {
                    echo '<div class="go_chain_message">' . $final_chain_message . '</div>';
                }
                echo "</div>" . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "");
            } else {
                if ($test_c_active) {
                    echo "<p id='go_test_error_msg' style='color: red;'></p>";
                    if ($test_c_num > 1) {
                        for ($i = 0; $i < $test_c_num; $i++) {
                            echo do_shortcode("[go_test type='" . $test_c_all_types[$i] . "' question='" . $test_c_all_questions[$i] . "' possible_answers='" . $test_c_all_answers[$i] . "' key='" . $test_c_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_c_num . "']");
                        }
                        echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    } else {
                        echo do_shortcode("[go_test type='" . $test_c_all_types[0] . "' question='" . $test_c_all_questions[0] . "' possible_answers='" . $test_c_all_answers[0] . "' key='" . $test_c_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    }
                }
                if ($completion_upload) {
                    echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                }
                echo '<span id="go_button" status="4" style="display:none;"></span><button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>';
                if ($next_post_id_in_chain != 0 && $last_in_chain !== 'true') {
                    echo '<div class="go_chain_message">Next ' . strtolower(go_return_options('go_tasks_name')) . ' in ' . $chain_name . ': <a href="' . get_permalink($next_post_id_in_chain) . '">' . get_the_title($next_post_id_in_chain) . '</a></div>';
                } else {
                    echo '<div class="go_chain_message">' . $final_chain_message . '</div>';
                }
                echo "</div>" . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "");
            }
            break;
        case 4:
            echo '<div class="go_stage_message">' . do_shortcode(wpautop($accept_message, false)) . '</div><div class="go_stage_message">' . do_shortcode(wpautop($completion_message)) . '</div><div id="new_content"><div class="go_stage_message">' . do_shortcode(wpautop($mastery_message)) . '</div>';
            // if the task can be repeated...
            if ($repeat == 'on') {
                // if the number of times that the page has been repeated is less than the total amount of repeats allowed OR if the
                // total repeats allowed is equal to zero (infinte amount allowed)...
                if ($task_count < $repeat_amount || $repeat_amount == 0) {
                    if ($task_count == 0) {
                        if ($test_m_active) {
                            echo "<p id='go_test_error_msg' style='color: red;'></p>";
                            if ($test_m_num > 1) {
                                for ($i = 0; $i < $test_m_num; $i++) {
                                    echo do_shortcode("[go_test type='" . $test_m_all_types[$i] . "' question='" . $test_m_all_questions[$i] . "' possible_answers='" . $test_m_all_answers[$i] . "' key='" . $test_m_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_m_num . "']");
                                }
                                echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                            } else {
                                echo do_shortcode("[go_test type='" . $test_m_all_types[0] . "' question='" . $test_m_all_questions[0] . "' possible_answers='" . $test_m_all_answers[0] . "' key='" . $test_m_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                            }
                        }
                        if ($mastery_upload) {
                            echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                        }
                        echo '
							<div id="repeat_quest">
								<div id="go_repeat_clicked" style="display:none;"><div class="go_stage_message">' . do_shortcode(wpautop($repeat_message)) . "</div><p id='go_stage_error_msg' style='display: none; color: red;'></p>";
                        if ($m_is_locked === 'true' && !empty($m_pass_lock)) {
                            echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
                        } elseif ($m_url_is_locked === true) {
                            echo "<input id='go_url_key' type='url' placeholder='Enter Url'/></br>";
                        }
                        echo "<button id='go_button' status='4' onclick='go_repeat_hide( this );' repeat='on'";
                        if ($m_is_locked === 'true' && empty($m_pass_lock)) {
                            echo "admin_lock='true'";
                        }
                        echo '>' . go_return_options('go_fourth_stage_button') . " Again" . '</button>
									<button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>
								</div>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "") . '<div id="go_repeat_unclicked">
									<button id="go_button" status="4" onclick="go_repeat_replace();">' . get_option('go_fifth_stage_button') . '</button>
									<button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>
								</div>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "") . '</div>
						';
                    } else {
                        if ($repeat_upload) {
                            echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                        }
                        echo '
							<div id="repeat_quest">
								<div id="go_repeat_clicked" style="display:none;"><div class="go_stage_message">' . do_shortcode(wpautop($repeat_message)) . "</div><p id='go_stage_error_msg' style='display: none; color: red;'></p>";
                        if ($r_is_locked === 'true' && !empty($r_pass_lock)) {
                            echo "<input id='go_pass_lock' type='password' placeholder='Enter Password'/></br>";
                        }
                        echo "<button id='go_button' status='4' onclick='go_repeat_hide( this );' repeat='on'";
                        if ($r_is_locked === 'true' && empty($r_pass_lock)) {
                            echo "admin_lock='true'";
                        }
                        echo '>' . go_return_options('go_fourth_stage_button') . " Again" . '</button>
									<button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>
								</div>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "") . '<div id="go_repeat_unclicked">
									<button id="go_button" status="4" onclick="go_repeat_replace();">' . get_option('go_fifth_stage_button') . '</button>
									<button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>
								</div>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "") . '</div>
						';
                    }
                } else {
                    echo '<span id="go_button" status="4" repeat="on" style="display:none;"></span><button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "");
                }
            } else {
                if ($test_m_active) {
                    echo "<p id='go_test_error_msg' style='color: red;'></p>";
                    if ($test_m_num > 1) {
                        for ($i = 0; $i < $test_m_num; $i++) {
                            echo do_shortcode("[go_test type='" . $test_m_all_types[$i] . "' question='" . $test_m_all_questions[$i] . "' possible_answers='" . $test_m_all_answers[$i] . "' key='" . $test_m_all_keys[$i] . "' test_id='" . $i . "' total_num='" . $test_m_num . "']");
                        }
                        echo "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    } else {
                        echo do_shortcode("[go_test type='" . $test_m_all_types[0] . "' question='" . $test_m_all_questions[0] . "' possible_answers='" . $test_m_all_answers[0] . "' key='" . $test_m_all_keys[0] . "' test_id='0']") . "<div class='go_test_submit_div' style='display: none;'><button class='go_test_submit'>Submit</button></div>";
                    }
                }
                if ($mastery_upload) {
                    echo do_shortcode("[go_upload is_uploaded={$is_uploaded} status={$status} user_id={$user_id} post_id={$post_id}]") . "<br/>";
                }
                $mastered = (array) get_user_meta($user_id, 'mastered_tasks', true);
                $user_id = get_current_user_id();
                echo !empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "";
                if ($bonus_loot[0]) {
                    if (!empty($bonus_loot[1])) {
                        foreach ($bonus_loot[1] as $store_item => $on) {
                            if ($on === 'on') {
                                $random_number = rand(1, 999);
                                $store_custom_fields = get_post_custom($store_item);
                                $store_cost = !empty($store_custom_fields['go_mta_store_cost'][0]) ? unserialize($store_custom_fields['go_mta_store_cost'][0]) : array();
                                $currency = $store_cost[0] < 0 ? -$store_cost[0] : 0;
                                $points = $store_cost[1] < 0 ? -$store_cost[1] : 0;
                                $bonus_currency = $store_cost[2] < 0 ? -$store_cost[2] : 0;
                                $penalty = $store_cost[3] > 0 ? -$store_cost[3] : 0;
                                $minutes = $store_cost[4] < 0 ? -$store_cost[4] : 0;
                                $loot_reason = $bonus_loot[2][$store_item] * 10 > 999 ? 'Quest' : 'Bonus';
                                if ($random_number < $bonus_loot[2][$store_item] * 10) {
                                    if (!in_array($post_id, $mastered) && $loot_reason == 'Bonus') {
                                        go_add_post($user_id, $store_item, -1, $points, $currency, $bonus_currency, $minutes, null, 'off', 0, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, null, false, $loot_reason, 'bonus', false, false);
                                        echo "Congrats, " . do_shortcode('[go_get_displayname]') . "!  You received an item: <a href='#' onclick='go_lb_opener({$store_item})'>" . get_the_title($store_item) . "</a></br>";
                                    } elseif ($loot_reason == 'Quest') {
                                        go_add_post($user_id, $store_item, -1, $points, $currency, $bonus_currency, $minutes, null, 'off', 0, $e_fail_count, $a_fail_count, $c_fail_count, $m_fail_count, $e_passed, $a_passed, $c_passed, $m_passed, null, false, $loot_reason, 'quest', false, false);
                                        echo "Congrats, " . do_shortcode('[go_get_displayname]') . "!  You received an item: <a href='#' onclick='go_lb_opener({$store_item})'>" . get_the_title($store_item) . "</a></br>";
                                    }
                                }
                            }
                        }
                    }
                    if (!in_array($post_id, $mastered)) {
                        $mastered[] = $post_id;
                        update_user_meta($user_id, 'mastered_tasks', $mastered);
                    }
                    echo "</br>";
                }
                add_user_meta($user_id, 'ever_mastered', $post_id, true);
                echo '<span id="go_button" status="4" repeat="on" style="display:none;"></span><button id="go_back_button" onclick="task_stage_change( this );" undo="true">Undo</button>';
            }
            if ($next_post_id_in_chain != 0 && $last_in_chain !== 'true') {
                echo '<div class="go_chain_message"><p>Next ' . strtolower(go_return_options('go_tasks_name')) . ' in ' . $chain_name . ': <a href="' . get_permalink($next_post_id_in_chain) . '">' . get_the_title($next_post_id_in_chain) . '</a></div>';
            } else {
                echo '<div class="go_chain_message">' . $final_chain_message . '</div>';
            }
            echo '</div>' . (!empty($task_pods) && !empty($pods_array) ? "<br/><a href='{$pod_link}'>Return to Pod Page</a>" : "");
    }
    die;
}
Example #2
0
function go_task_abandon($user_id = null, $post_id = null, $e_points = null, $e_currency = null, $e_bonus_currency = null)
{
    global $wpdb;
    if (empty($user_id) && empty($post_id) && empty($e_points) && empty($e_currency) && empty($e_bonus_currency)) {
        $user_id = get_current_user_id();
        $post_id = $_POST['post_id'];
        $e_points = intval($_POST['encounter_points']);
        $e_currency = intval($_POST['encounter_currency']);
        $e_bonus_currency = intval($_POST['encounter_bonus']);
    }
    $table_name_go = "{$wpdb->prefix}go";
    $accept_timestamp = strtotime(str_replace('@', ' ', $wpdb->get_var("SELECT timestamp FROM {$wpdb->prefix}go WHERE uid='{$user_id}' AND post_id='{$post_id}'")));
    go_update_totals($user_id, -$e_points, -$e_currency, -$e_bonus_currency, 0, 0);
    $wpdb->query($wpdb->prepare("\n\t\t\tDELETE FROM {$table_name_go} \n\t\t\tWHERE uid = %d \n\t\t\tAND post_id = %d", $user_id, $post_id));
    $custom_fields = get_post_custom($post_id);
    $future_modifier = !empty($custom_fields['go_mta_time_modifier'][0]) ? unserialize($custom_fields['go_mta_time_modifier'][0]) : '';
    $user_timers = get_user_meta($user_id, 'go_timers', true);
    if (!empty($future_modifier) && empty($user_timers[$post_id])) {
        $user_timers[$post_id] = $accept_timestamp;
        update_user_meta($user_id, 'go_timers', $user_timers);
    }
}
Example #3
0
function go_task_abandon($user_id = null, $post_id = null, $e_points = null, $e_currency = null, $e_bonus_currency = null)
{
    global $wpdb;
    if (empty($user_id) && empty($post_id) && empty($e_points) && empty($e_currency) && empty($e_bonus_currency)) {
        $user_id = get_current_user_id();
        $post_id = $_POST['post_id'];
        $e_points = intval($_POST['encounter_points']);
        $e_currency = intval($_POST['encounter_currency']);
        $e_bonus_currency = intval($_POST['encounter_bonus']);
    }
    $table_name_go = "{$wpdb->prefix}go";
    go_update_totals($user_id, -$e_points, -$e_currency, -$e_bonus_currency, 0, 0);
    $wpdb->query($wpdb->prepare("\n\t\tDELETE FROM {$table_name_go} \n\t\tWHERE uid = %d \n\t\tAND post_id = %d", $user_id, $post_id));
}
Example #4
0
function go_add_penalty($user_id, $penalty, $reason)
{
    global $wpdb;
    $table_name_go = $wpdb->prefix . "go";
    if (!empty($_POST['qty'])) {
        $penalty = $penalty * $_POST['qty'];
    }
    $time = date('m/d@H:i', current_time('timestamp', 0));
    $wpdb->insert($table_name_go, array('uid' => $user_id, 'penalty' => $penalty, 'reason' => $reason, 'timestamp' => $time));
    go_update_totals($user_id, 0, 0, 0, $penalty, $status);
}