router(); $this->date_format = get_option('date_format'); } /** * Route various actions on application dashboard */ public function router() { $success = false; if (isset($_POST['action']) && $_POST['action'] == 'delegate') { $success = $this->save_delegation(); } if (isset($_POST['action']) && $_POST['action'] == 'change_status') { $success = $this->save_status(); } if (isset($_GET['action']) && $_GET['action'] == 'change_activity') { $success = $this->change_activity(); } if (isset($_REQUEST['application_id'])) { $this->application_id = $_REQUEST['application_id']; $this->step_id = isset($_REQUEST['step_id']) ? $_REQUEST['step_id'] : 0; $this->document_id = isset($_REQUEST['document_id']) ? $_REQUEST['document_id'] : 0; $this->display_dashboard($success); } else { $this->select_application(); } } /** * Display application dashboard */ public function display_dashboard($success) { $query = array( 'post_type' => 'application', 'p' => $this->application_id ); if (!current_user_can('manage_options')) { $query['author'] = get_current_user_id(); } $application = new WP_Query($query); //if no application with those params redirect to applications if (!$application->have_posts()) { $this->select_application(); } while ($application->have_posts()) : $application->the_post(); //get client $client_id = get_post_meta(get_the_ID(), '_aam_application_client', true); $client = get_user_by('id', $client_id); //get combination data $combination_data = get_post_meta(get_the_ID(), '_aam_application_combination', true); $combination = get_post($combination_data['combination']); //deadline $deadlines = get_post_meta($combination->ID, '_aam_combination_deadlines', true); $deadline = $deadlines[$combination_data['round'] - 1]; //school $school_id = $combination_data['school']; $school = get_post($school_id); //programs $program = $combination_data['program']; //intake $intake_id = $combination_data['intake']; $intake = get_term_by('id', $intake_id, 'intake'); $document_items = array(); if ($this->application_id && $this->step_id && $this->document_id) { $document_items = get_posts(array( 'post_type' => 'document', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => '_aam_document_application_id', 'value' => $this->application_id, 'compare' => '=' ), array( 'key' => '_aam_document_step_id', 'value' => $this->step_id, 'compare' => '=' ), array( 'key' => '_aam_document_document_id', 'value' => $this->document_id, 'compare' => '=' ) ) )); } $this->set_steps(); $this->get_completion(); ?>

step_id || !$this->document_id) : ?>

Client: data->display_name; ?>
Consultant:
School: post_title; ?>
Program:
Intake: name; ?>
Round:
Created:
Deadline:

:

get_consultants_dropdown(get_the_author_meta('ID')); ?>

steps as $sk => $step) : ?>
Steps
get_last_document($item->document_id, $item->step_id); ?> post_author) : null; ?> deadline); ?>
parent_id || $item->parent_completion == 100) { ?> name; ?> name; ?> format(get_option('date_format')); ?> display_name : '-'; ?>

-

Client: data->display_name; ?>
School: post_title; ?>
Program:
Intake: name; ?>
Round:
Created:
Deadline:

:

get_statuses_dropdown(); ?>

ID, '_aam_document_document', true)); ?> ID, '_aam_document_final', true); ?> post_author); ?>
Document Type Uploaded Author Final?
ID, '_aam_document_document', true); ?> post_mime_type; ?> post_date)); ?> display_name; ?>
prefix . 'aam_applications'; $selected = $wpdb->get_var( $wpdb->prepare( "SELECT status FROM $table_name WHERE application_id = %d AND document_id = %s AND step_id = %s", $this->application_id, $this->document_id, $this->step_id ) ); $statuses = get_terms(array('status'), array( 'hide_empty' => false )); $dd = ''; return $dd; } public function set_steps() { global $wpdb; $table_name = $wpdb->prefix . 'aam_applications'; $steps = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_name AS apps LEFT JOIN wp_posts AS posts ON posts.ID = apps.document_type WHERE apps.application_id = %d AND apps.active = 1 ORDER BY apps.sort", $this->application_id ) ); $sorted = array(); if ($steps) { $current = null; foreach ($steps as $step) { if (!isset($sorted[$step->document_id])) { $sorted[$step->document_id] = array( 'id' => $step->id, 'document' => $step->post_title, 'prompt' => $step->prompt, 'type' => $step->document_type, 'weight' => $step->document_weight, 'required' => $step->required, 'active' => $step->active, 'items' => array() ); } $step->completion = $this->get_step_completion($step->status); //get parent step completion if ($step->parent_id) { $step->parent_completion = $this->get_parent_completion($steps, $step->document_id, $step->parent_id); } $sorted[$step->document_id]['items'][] = $step; } } $this->steps = $sorted; } /** * Get parent step completion * * @param array $steps * @param string $document_id * @param string $parent_step_id * @return int */ public function get_parent_completion($steps = array(), $document_id = '', $parent_step_id = '') { foreach ($steps as $step) { if ($step->document_id == $document_id && $step->step_id == $parent_step_id) { return $this->get_step_completion($step->status); } } } /** * Calculate completion of whole application */ public function get_completion() { $total = 0; $document_total = 0; foreach ($this->steps as $step) { foreach ($step['items'] as $item) { $done = $item->step_weight * $item->completion / 100; $document_total += $done; } $total += $step['weight'] * $document_total / 100; $document_total = 0; } $this->complete = $total; } /** * @param string $document_id * * @return float|int */ public function get_document_completion($document_id = '') { $total = 0; foreach ($this->steps[$document_id]['items'] as $item) { $done = $item->step_weight * $item->completion / 100; $total += $done; } return $total; } /** * @param int $status * * @return int */ public function get_step_completion($status = 0) { if ($status) { $status = get_option('status_' . $status); return $status['percent']; } return 0; } /** * Generate consultants drop down menu * @param $owner * * @return string */ private function get_consultants_dropdown($owner) { global $wpdb; $consultants = get_users(array( 'role' => 'Consultant', 'exclude' => array($owner) )); //get delegations $table = $wpdb->prefix . 'aam_delegations'; $delegation = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE application_id = %d", array( $this->application_id ) ) ); $dd = ''; return $dd; } public function get_last_document($document_id, $step_id) { $query = new WP_Query(array( 'meta_query' => array( array( 'key' => '_aam_document_document_id', 'compare' => '=', 'value' => $document_id ), array( 'key' => '_aam_document_step_id', 'compare' => '=', 'value' => $step_id ) ), 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 1, 'post_status' => 'publish', 'post_type' => 'document' )); $posts = $query->get_posts(); return isset($posts[0]) ? $posts[0] : null; } /** * Save delegation */ public function save_delegation() { global $wpdb; $table = $wpdb->prefix . 'aam_delegations'; if (isset($_POST['application_id'])) { $delegation = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE application_id = %d", array( $_POST['application_id'] ) ) ); if (isset($_POST['aam_delegation']) && $_POST['aam_delegation'] != '') { if ($delegation) { $wpdb->update( $table, array( 'consultant_id' => $_POST['aam_delegation'] ), array( 'application_id' => $_POST['application_id'] ), array( '%d' ), array( '%d' ) ); } else { $wpdb->insert( $table, array( 'consultant_id' => $_POST['aam_delegation'], 'application_id' => $_POST['application_id'] ), array( '%d', '%d' ) ); } } else { if ($delegation) { $wpdb->delete( $table, array( 'application_id' => $_POST['application_id'] ) ); } } } return 'Delegation successfully changed'; /*wp_redirect(admin_url('admin.php?page=aam&application_id=' . $_POST['application_id'])); exit;*/ } public function save_status() { global $wpdb; $table_name = $wpdb->prefix . 'aam_applications'; $status = $_POST['status']; $application_id = $_POST['application_id']; $step_id = $_POST['step_id']; $document_id = $_POST['document_id']; $done = 0; $statuses = get_option('statuses_' . $application_id); if (!$statuses) { $statuses = array(); $statuses[$document_id] = array(); $statuses[$document_id][$step_id] = $status; } else { if (!isset($statuses[$document_id])) { $statuses[$document_id] = array(); $statuses[$document_id][$step_id] = $status; } else { $statuses[$document_id][$step_id] = $status; } } //update step completion (cache in db) if ($status) { $status_data = get_option('status_' . $status); $done = $status_data['percent']; } $wpdb->update( $table_name, array( 'status' => $status, 'done' => $done ), array( 'application_id' => $application_id, 'document_id' => $document_id, 'step_id' => $step_id ) ); //also update complete application completion $this->application_id = $application_id; $this->set_steps(); $this->get_completion(); update_post_meta($application_id, '_aam_total_completion', $this->complete); update_option('statuses_' . $application_id, $statuses); return 'Status successfully changed'; /*wp_redirect(admin_url('admin.php?page=aam&application_id=' . $application_id . '&document_id=' . $document_id . '&step_id=' . $step_id)); exit;*/ } public function change_activity() { global $wpdb; $table_name = $wpdb->prefix . 'aam_applications'; $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_name WHERE id = %d", $_GET['step_id'] ), ARRAY_A ); $new = abs($row['active'] - 1); $update = array( 'active' => $new ); if ($new == 0) { $update['deadline'] = NULL; } if ($wpdb->update( $table_name, $update, array( 'application_id' => $row['application_id'], 'document_id' => $row['document_id'] ) ) ) { $helper = new ApplicationHelper($row['application_id']); $helper->calc_deadlines(true); return 'Status changed'; /*wp_redirect(admin_url('admin.php?page=aam&application_id=' . $_GET['application_id'])); exit;*/ } } } function init_dashboard() { $dash = new Aam_App_Dashboard(); }