<commit_before><?php
require_once "../../config.php";
require_once $CFG->dirroot."/pdo.php";
require_once $CFG->dirroot."/lib/lms_lib.php";

// Sanity checks
$LTI = requireData(array('user_id', 'link_id', 'role','context_id'));
$instructor = isInstructor($LTI);
if ( ! $instructor ) die("Requires instructor");
$p = $CFG->dbprefix;

// Grab our link_id
$link_id = 0;
if ( isset($_GET['link_id']) ) {
    $link_id = $_GET['link_id']+0;
}

// Load to make sure it is within our context
$link_info = false;
if ( $instructor && $link_id > 0 ) {
    $link_info = loadLinkInfo($pdo, $link_id);
}
if ( $link_info === false ) die("Invalid link");

if ( isset($_POST['resetServerGrades']) ) {
    $lstmt = pdoQueryDie($pdo,
        "UPDATE {$p}lti_result SET server_grade=NULL, retrieved_at=NULL
        WHERE link_id = :LID",
        array(":LID" => $link_id)
    );

    $msg = $lstmt->rowcount() . " Records reset.";
    $_SESSION["success"] = $msg;
    header("Location: ".sessionize('maint.php?link_id='.$link_id));
    return;
}

if ( isset($_POST['getServerGrades']) ) {
    $row = pdoRowDie($pdo,
        "SELECT COUNT(*) AS count FROM {$p}lti_result AS R
        JOIN {$p}lti_service AS S ON R.service_id = S.service_id
        WHERE link_id = :LID AND grade IS NOT NULL AND 
            server_grade IS NULL AND
            sourcedid IS NOT NULL AND service_key IS NOT NULL",
        array(":LID" => $link_id)
    );
    $total = $row['count'];
    if ( $total == 0 ) {
        $_SESSION['success'] = 'No records remain to retrieve';
        header("Location: ".sessionize('maint.php?link_id='.$link_id));
        return;
    }

    headerContent();
    echo(togglePreScript());
    echo("</head><body>\n");
    echo('<p id="abort"><a href="maint.php?link_id='.$link_id.'">Interrupt 
        Maintenance Process</a></p>'."\n");

    echo("Records to be processed: ".$total."<br/>");

    $stmt = pdoQueryDie($pdo,
        "SELECT result_id, sourcedid, service_key FROM {$p}lti_result AS R
        JOIN {$p}lti_service AS S ON R.service_id = S.service_id
        WHERE link_id = :LID AND grade IS NOT NULL AND 
            server_grade IS NULL AND
            sourcedid IS NOT NULL AND service_key IS NOT NULL",
        array(":LID" => $link_id)
    );

    $count = 0;
    $success = 0;
    $begin = microtime(true);
    $fail = 0;
    while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
        $count = $count + 1;
        $start = microtime(true);
        $grade = getGrade($row['sourcedid'], $row['service_key']);
        $et = (microtime(true) - $start) ;
        $ets = sprintf("%1.3f",$et);
        if ( $grade == false ) {
            $fail++;
            togglePre("Error at ".$count.' / '.$total.' ('.$ets.')',$LastPOXGradeResponse);
            flush();
        } else {
            $success++;
            // echo("Grade=".$grade."<br/>\n");
            // print_r($row); echo("<br/>\n");
            if ( $success % 10 == 0 ) {
                echo("Grade=$grade ($ets) $count / $total <br/>\n");
                flush();
            }
            $lstmt = pdoQueryDie($pdo,
                "UPDATE {$p}lti_result SET server_grade=:GRA, retrieved_at=NOW()
                WHERE result_id = :RID",
                array(":GRA" => $grade, ":RID" => $row['result_id'])
            );
        }
        $_SESSION['error'] = 'Interrupted at '.$count.' / '.$total. ' success='.$success.' fail='.$fail;
        if ( $count > 100 ) break;
    }
    unset($_SESSION['error']);
    $et = (microtime(true) - $begin) ;
    $ets = sprintf("%1.3f",$et);

    $msg = $count . " Records processed ".$ets." seconds. success=".$success.' fail='.$fail;
    $_SESSION["success"] = $msg;
    echo($msg."<br/>\n");
    echo('<script type="text/javascript"> document.getElementById("abort").style.display = "none"; </script>');
    echo('<a href="maint.php?link_id='.$link_id.'">Continue</a></p>'."\n");
    return;
}

if ( isset($_POST['checkServerGrades']) || isset($_POST['fixServerGrades']) ) {
    headerContent();
    echo(togglePreScript());
    echo("</head><body>\n");

    $stmt = pdoQueryDie($pdo,
        "SELECT result_id, link_id, grade, server_grade, note, 
            sourcedid, service_key, <commit_msg>Further down the quest toward server grade perfection.<commit_after><?php
require_once "../../config.php";
require_once $CFG->dirroot."/pdo.php";
require_once $CFG->dirroot."/lib/lms_lib.php";

// Sanity checks
$LTI = requireData(array('user_id', 'link_id', 'role','context_id'));
$instructor = isInstructor($LTI);
if ( ! $instructor ) die("Requires instructor");
$p = $CFG->dbprefix;

// Grab our link_id
$link_id = 0;
if ( isset($_GET['link_id']) ) {
    $link_id = $_GET['link_id']+0;
}

// Load to make sure it is within our context
$link_info = false;
if ( $instructor && $link_id > 0 ) {
    $link_info = loadLinkInfo($pdo, $link_id);
}
if ( $link_info === false ) die("Invalid link");

if ( isset($_POST['resetServerGrades']) ) {
    $lstmt = pdoQueryDie($pdo,
        "UPDATE {$p}lti_result SET server_grade=NULL, retrieved_at=NULL
        WHERE link_id = :LID",
        array(":LID" => $link_id)
    );

    $msg = $lstmt->rowcount() . " Records reset.";
    $_SESSION["success"] = $msg;
    header("Location: ".sessionize('maint.php?link_id='.$link_id));
    return;
}

if ( isset($_POST['getServerGrades']) ) {
    $row = pdoRowDie($pdo,
        "SELECT COUNT(*) AS count FROM {$p}lti_result AS R
        JOIN {$p}lti_service AS S ON R.service_id = S.service_id
        WHERE link_id = :LID AND grade IS NOT NULL AND 
            (server_grade IS NULL OR retrieved_at < R.updated_at) AND
            sourcedid IS NOT NULL AND service_key IS NOT NULL",
        array(":LID" => $link_id)
    );
    $total = $row['count'];
    if ( $total == 0 ) {
        $_SESSION['success'] = 'No records remain to retrieve';
        header("Location: ".sessionize('maint.php?link_id='.$link_id));
        return;
    }

    headerContent();
    echo(togglePreScript());
    echo("</head><body>\n");
    echo('<p id="abort"><a href="maint.php?link_id='.$link_id.'">Interrupt 
        Maintenance Process</a></p>'."\n");

    echo("Records to be processed: ".$total."<br/>");

    $stmt = pdoQueryDie($pdo,
        "SELECT result_id, sourcedid, service_key FROM {$p}lti_result AS R
        JOIN {$p}lti_service AS S ON R.service_id = S.service_id
        WHERE link_id = :LID AND grade IS NOT NULL AND 
            (server_grade IS NULL OR retrieved_at < R.updated_at) AND
            sourcedid IS NOT NULL AND service_key IS NOT NULL",
        array(":LID" => $link_id)
    );

    $count = 0;
    $success = 0;
    $begin = microtime(true);
    $fail = 0;
    while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
        $count = $count + 1;
        $start = microtime(true);
        // UPDATEs automatically on success
        $grade = getGrade($pdo, $row['result_id'], $row['sourcedid'], $row['service_key']);
        $et = (microtime(true) - $start) ;
        $ets = sprintf("%1.3f",$et);
        if ( $grade == false ) {
            $fail++;
            togglePre("Error at ".$count.' / '.$total.' ('.$ets.')',$LastPOXGradeResponse);
            flush();
        } else {
            $success++;
            // echo("Grade=".$grade."<br/>\n");
            // print_r($row); echo("<br/>\n");
            if ( $success % 10 == 0 ) {
                echo("Grade=$grade ($ets) $count / $total <br/>\n");
                flush();
            }
        }
        $_SESSION['error'] = 'Interrupted at '.$count.' / '.$total. ' success='.$success.' fail='.$fail;
        if ( $count > 100 ) break;
    }
    unset($_SESSION['error']);
    $et = (microtime(true) - $begin) ;
    $ets = sprintf("%1.3f",$et);

    $msg = $count . " Records processed ".$ets." seconds. success=".$success.' fail='.$fail;
    $_SESSION["success"] = $msg;
    echo($msg."<br/>\n");
    echo('<script type="text/javascript"> document.getElementById("abort").style.display = "none"; </script>');
    echo('<a href="maint.php?link_id='.$link_id.'">Continue</a></p>'."\n");
    return;
}

if ( isset($_POST['checkServerGrades']) || isset($_POST['fixServerGrades']) ) {
    headerContent();
    echo(togglePreScript());
    echo("</head><body>\n");

    $stmt = pdoQueryDie($pdo,
        "SELECT result_id, link_id, grade, server_grade, note, 
            sourcedid, service_key, 
{'fraction_non_alphanumeric': 0.19243204406657885, 'fraction_numerical': 0.004310860974733565, 'mean_word_length': 2.6679841897233203, 'pattern_counts': {'":': 8, '<': 43, '<?xml version=': 0, '>': 61, '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': '1627815', 'n_tokens_mistral': 2875, 'n_tokens_neox': 2805, 'n_words': 609}