require_once $CFG->libdir . '/phpxml/xml.php';
$visname = required_param('visid');
$courseid = required_param('id');
/// basic access checks
if (isset($DB) && !is_null($DB)) {
    $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);
$vis = grade_report_visual::get_visualization($visname, $context);
/// Make sure the user is allowed see this visualization
require_capability($vis->capability, $context);
/// 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 XML format and stop the
/// front end from working.
error_reporting(0);
/// Define array that repersents the XML output from the
/// visualization class selected.
$settings = array();
$settings['visualization'] = array();
$settings['visualization']['name'] = $vis->name;
$settings['visualization']['classname'] = get_class($vis);
$settings['visualization']['layout'] = array();
$settings['visualization']['layout']['type'] = $vis->layout;
if (isset($DB) && !is_null($DB)) {
    $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;
 /**
  * Constructor. Initialises grade_tree, sets up group, baseurl
  * and pbarurl.
  * @param int $courseid the coures id for the report
  * @param object $gpr grade plugin tracking object
  * @context string $context
  */
 public function __construct($courseid, $gpr, $context, $visid = null)
 {
     global $CFG;
     parent::__construct($courseid, $gpr, $context, null);
     $this->canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $this->course->id));
     /// Set up urls
     $this->baseurl = 'index.php?id=' . $this->courseid;
     $this->pbarurl = 'index.php?id=' . $this->courseid;
     /// Set the position of the aggregation categorie based on pref
     $switch = $this->get_pref('visual', 'aggregationposition');
     if ($switch == '' && isset($CFG->grade_aggregationposition)) {
         $switch = grade_get_setting($this->courseid, 'aggregationposition', $CFG->grade_aggregationposition);
     }
     /// Build grade tree
     $this->gtree = new grade_tree($this->courseid, false, $switch);
     $this->load_visualizations();
     if (!is_null($visid) && !empty($visid) && array_key_exists($visid, grade_report_visual::$visualizations)) {
         $this->visid = $visid;
     } else {
         $keys = array_keys(grade_report_visual::$visualizations);
         $this->visid = $keys[0];
     }
     $this->set_up_flashvars();
     /// Set up Groups
     if ($this->visid && grade_report_visual::get_visualization($this->visid)->usegroups) {
         $this->setup_groups();
     }
 }