<?php if (!empty($in_shortcode)) { WTPReports::print_scripts(); } ?> <div class="wrap"> <?php if (empty($in_shortcode)) { ?> <h1><?php _e('Chart by grades', 'watupro'); ?> </h1> <p><?php _e('Quiz:', 'watupro'); ?> <b><?php echo $exam->name; ?> </b></p> <p><a href="admin.php?page=watupro_takings&exam_id=<?php echo $exam->ID; ?> "><?php _e("Back to the users list", 'watupro'); ?> </a></p>
static function chart_by_grade($in_shortcode = false) { global $wpdb; // select exam $exam = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . WATUPRO_EXAMS . " WHERE ID=%d", $_GET['exam_id'])); // the code below is the same as in the play plugin controllers/charts.php. Think about a way to re-use it? // select all completed takings $takings = $wpdb->get_results($wpdb->prepare("SELECT tT.ID as ID, tT.points as points, tG.gtitle as grade_title \n\t\t\tFROM " . WATUPRO_TAKEN_EXAMS . " tT JOIN " . WATUPRO_GRADES . " tG ON tG.ID = tT.grade_id\n\t\t\tWHERE tT.exam_id=%d AND tT.in_progress=0 ORDER BY tT.points DESC", $exam->ID)); $num_takings = 0; $grades_arr = array(); // this array will hold grade_ids(keys) and no. users who got that grade (vals) foreach ($takings as $taking) { $num_takings++; if (isset($grades_arr[$taking->grade_title])) { $grades_arr[$taking->grade_title]++; } else { $grades_arr[$taking->grade_title] = 1; } } // now recalculate grade values in % foreach ($grades_arr as $key => $val) { // avoid division by zero error if ($num_takings == 0) { $grades_arr[$key] = 0; continue; } // now calculate $percent = round(100 * $val / $num_takings); $grades_arr[$key] = array("percent" => $percent, "num_takings" => $val); if ($percent == 0) { unset($grades_arr[$key]); } } // ksort($grades_arr); WTPReports::$add_scripts = true; WTPReports::register_scripts(); if (@file_exists(get_stylesheet_directory() . '/watupro/reports/chart-by-grade.html.php')) { require get_stylesheet_directory() . '/watupro/reports/chart-by-grade.html.php'; } else { require WATUPRO_PATH . "/modules/reports/views/chart-by-grade.html.php"; } WTPReports::print_scripts(); }