$course = $DB->get_record('course', array('id' => $courseid));
} else {
    $course = get_record('course', 'id', $courseid);
}
if (!$course) {
    print_error('nocourseid');
}
require_login($course);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
require_capability('gradereport/visual:view', $context);
/// get tracking object
$gpr = new grade_plugin_return(array('type' => 'report', 'plugin' => 'visual', 'courseid' => $courseid));
$report = new grade_report_visual($courseid, $gpr, $context, $visid);
/// Make sure the user is allowed see this visualization
require_capability(grade_report_visual::get_visualization($report->visid, $context)->capability, $context);
grade_regrade_final_grades($courseid);
/// Turn of error reporting as hummans will not be seeing
/// this and it will be read by the front end. Notices and
/// warnings will break the format and stop the
/// front end from working.
error_reporting(0);
/// Get report object
$report->load_users();
$report->harvest_data();
$report->report_data();
$report->adapt_data();
/// Clean up cookie if it was created.
if ($cookiewasset) {
    $_COOKIE['MoodleSession' . $_GET['sessioncookie']] = null;
    $_COOKIE['MoodleSessionTest' . $_GET['sessioncookie']] = null;
}