$context = get_context_instance(CONTEXT_COURSE, $id);
require_capability('moodle/grade:export', $context);
require_capability('gradeexport/fusion:view', $context);
print_grade_page_head($COURSE->id, 'export', 'fusion', get_string('exportto', 'grades') . ' ' . get_string('modulename', 'gradeexport_fusion'));
if (!empty($CFG->gradepublishing)) {
    $CFG->gradepublishing = has_capability('gradeexport/fusion:publish', $context);
}
$mform = new fusion_grade_export_form(null, array('includeseparator' => true, 'publishing' => true));
$groupmode = groups_get_course_groupmode($course);
// Groups are being used
$currentgroup = groups_get_course_group($course, true);
if ($groupmode == SEPARATEGROUPS and !$currentgroup and !has_capability('moodle/site:accessallgroups', $context)) {
    echo $OUTPUT->heading(get_string("notingroup"));
    echo $OUTPUT->footer();
    die;
}
// process post information
if ($data = $mform->get_data()) {
    $export = new grade_export_fusion($course, $currentgroup, '', false, false, $data->display, $data->decimals, $data->tablename);
    // print the grades on screen for feedback
    $export->process_form($data);
    $export->set_table($data->tablename);
    $export->print_continue();
    $export->display_preview();
    echo $OUTPUT->footer();
    exit;
}
groups_print_course_menu($course, 'index.php?id=' . $id);
echo '<div class="clearer"></div>';
$mform->display();
echo $OUTPUT->footer();
        print_error('cannotaccessgroup', 'grades');
    }
}
// parameters to preserve
$preserve = array('id' => $id, 'groupid' => $groupid, 'itemids' => $itemids, 'export_feedback' => $export_feedback, 'separator' => $separator, 'updatedgradesonly' => $updatedgradesonly, 'displaytype' => $displaytype, 'decimalpoints' => $decimalpoints, 'tablename' => $tablename);
// check OAuth
//$oauth = new fusion_grade_export_fusion();
$returnurl = new moodle_url('/grade/export/fusion/export.php');
foreach ($preserve as $k => $v) {
    $returnurl->param($k, $v);
}
$returnurl->param('sesskey', sesskey());
// check the config
$clientid = get_config('gradeexport_fusion', 'clientid');
$secret = get_config('gradeexport_fusion', 'secret');
if (empty($clientid) || empty($secret)) {
    print_error('noconfig', 'gradeexport_fusion');
}
$fusion_realm = 'https://www.googleapis.com/auth/fusiontables';
$googleoauth = new google_oauth($clientid, $secret, $returnurl, $fusion_realm);
if (!$googleoauth->is_logged_in()) {
    $url = $googleoauth->get_login_url();
    redirect($url, get_string('login', 'gradeexport_fusion'), 2);
    //     echo '<a target="_blank" href="'.$url->out(false).'">'.get_string('login', 'repository').'</a>';
}
$oauth = new fusion_grade_export_oauth_fusion($googleoauth);
$oauth->show_tables();
// print all the exported data here
$export = new grade_export_fusion($course, $groupid, $itemids, $export_feedback, $updatedgradesonly, $displaytype, $decimalpoints, $separator, $tablename);
$export->set_table($tablename);
$export->export_grades($oauth);