<commit_before><?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

use \Tsugi\Core\LTIX;
use \Tsugi\Util\LTI;
use \Tsugi\Util\Mersenne_Twister;

$MAX_UPLOAD_FILE_SIZE = 1024*1024;

require_once "sql_util.php";

$oldgrade = $RESULT->grade;

// Compute the stuff for the output
$code = $USER->id+$LINK->id+$CONTEXT->id;
$roster = makeRoster($code,3,5);

function compare_func($a, $b) {
    // Course
    if ( $a[1] < $b[1] ) return -1;
    if ( $a[1] > $b[1] ) return 1;
    // Role (1 comes first)
    if ( $a[2] < $b[2] ) return 1;
    if ( $a[2] > $b[2] ) return -1;
    // User
    if ( $a[0] < $b[0] ) return -1;
    if ( $a[0] > $b[0] ) return 1;
    return 0;
}

usort($roster,"compare_func");

if ( isset($_FILES['json']) ) {
    $fdes = $_FILES['json'];

    // Check to see if they left off the file
    if( $fdes['error'] == 4) {
        $_SESSION['error'] = 'Missing file, make sure to select a file before pressing submit';
        header( 'Location: '.addSession('index.php') ) ;
        return;
    }

    if ( $fdes['size'] > $MAX_UPLOAD_FILE_SIZE ) {
        $_SESSION['error'] = "Uploaded file must be < ".$OUTPUT->displaySize($MAX_UPLOAD_FILE_SIZE);
        header( 'Location: '.addSession('index.php') ) ;
        return;
    }

    if ( ! endsWith($fdes['name'],'.json') ) {
        $_SESSION['error'] = "Uploaded file must have .json suffix: ".$fdes['name'];
        header( 'Location: '.addSession('index.php') ) ;
        return;
    }
    $file = $fdes['tmp_name'];
    $data = file_get_contents($file);

    // Retain the submission
    $store = substr($data, 0, 5000);
    $RESULT->setJSON($store);

    $tables = load_mysql_json_export($data);
    if ( is_string($tables) ) {
        $_SESSION['error'] = $tables;
        header( 'Location: '.addSession('index.php') ) ;
        return;
    }

    if ( isset($tables['user']) && isset($tables['course']) && isset($tables['member']) ) {
        // OK
    } else {
        $_SESSION['error'] = "Expecting user, course, and member tables";
        header( 'Location: '.addSession('index.php') ) ;
        return;
    }

    if ( isset($tables['user_table']) && isset($tables['course_table']) ) {
        // OK
    } else {
        $_SESSION['error'] = "Expecting user and course tables to have primary keys like user_id";
        header( 'Location: '.addSession('index.php') ) ;
        return;
    }

    // echo("<pre>\n");print_r($tables);echo("</pre>\n");
    $_SESSION['tables'] = $tables;  // For later debugging

    // Pull out the bits we need
    $course_table = $tables['course_table'];
    $user_table = $tables['user_table'];
    $member = $tables['member'];

    // Check sanity
    if ( count($course_table) < 1 ) {
        $_SESSION['error'] = "Course table is empty";
        header( 'Location: '.addSession('index.php') ) ;
        return;<commit_msg>Add a better error message.<commit_after><?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

use \Tsugi\Core\LTIX;
use \Tsugi\Util\LTI;
use \Tsugi\Util\Mersenne_Twister;

$MAX_UPLOAD_FILE_SIZE = 1024*1024;

require_once "sql_util.php";

$oldgrade = $RESULT->grade;

// Compute the stuff for the output
$code = $USER->id+$LINK->id+$CONTEXT->id;
$roster = makeRoster($code,3,5);

function compare_func($a, $b) {
    // Course
    if ( $a[1] < $b[1] ) return -1;
    if ( $a[1] > $b[1] ) return 1;
    // Role (1 comes first)
    if ( $a[2] < $b[2] ) return 1;
    if ( $a[2] > $b[2] ) return -1;
    // User
    if ( $a[0] < $b[0] ) return -1;
    if ( $a[0] > $b[0] ) return 1;
    return 0;
}

usort($roster,"compare_func");

if ( isset($_FILES['json']) ) {
    $fdes = $_FILES['json'];

    // Check to see if they left off the file
    if( $fdes['error'] == 4) {
        $_SESSION['error'] = 'Missing file, make sure to select a file before pressing submit';
        header( 'Location: '.addSession('index.php') ) ;
        return;
    }

    if ( $fdes['size'] > $MAX_UPLOAD_FILE_SIZE ) {
        $_SESSION['error'] = "Uploaded file must be < ".$OUTPUT->displaySize($MAX_UPLOAD_FILE_SIZE);
        header( 'Location: '.addSession('index.php') ) ;
        return;
    }

    if ( ! endsWith($fdes['name'],'.json') ) {
        $_SESSION['error'] = "Uploaded file must have .json suffix: ".$fdes['name'];
        header( 'Location: '.addSession('index.php') ) ;
        return;
    }
    $file = $fdes['tmp_name'];
    $data = file_get_contents($file);

    // Retain the submission
    $store = substr($data, 0, 5000);
    $RESULT->setJSON($store);

    $tables = load_mysql_json_export($data);
    if ( is_string($tables) ) {
        $_SESSION['error'] = $tables;
        header( 'Location: '.addSession('index.php') ) ;
        return;
    }

    if ( isset($tables['user']) && isset($tables['course']) && isset($tables['member']) ) {
        // OK
    } else {
        $tstr = '';
        $tlist = array();
        if ( $tables['user'] ) $tlist[] = 'user';
        if ( $tables['course'] ) $tlist[] = 'course';
        if ( $tables['member'] ) $tlist[] = 'member';
        if ( count($tlist) > 0 ) {
            $tstr = ". Found: " . htmlentities(implode(', ', $tlist));
        }
        $_SESSION['error'] = "Expecting user, course, and member tables".$tstr;
        header( 'Location: '.addSession('index.php') ) ;
        return;
    }

    if ( isset($tables['user_table']) && isset($tables['course_table']) ) {
        // OK
    } else {
        $_SESSION['error'] = "Expecting user and course tables to have primary keys like user_id";
        header( 'Location: '.addSession('index.php') ) ;
        return;
    }

    // echo("<pre>\n");print_r($tables);echo("</pre>\n");
    $_SESSION['tables'] = $tables;  // For later debugging

    // Pull out the bits we need
    $course_table = $tables['course_table'];
    $user_table = $tables['user_table'];
    $member = $tables['member'];

    // Check sanity
    if ( count($course_table) < 1 ) {
        $_SESSION['error'] = "Course table is empty";
        header( 'Location: '.addSession('index.php') ) ;
        return;
{'fraction_non_alphanumeric': 0.19490403706154863, 'fraction_numerical': 0.012739907346128392, 'mean_word_length': 2.72, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3196213', 'n_tokens_mistral': 2144, 'n_tokens_neox': 2086, 'n_words': 463}