<commit_before><?php
namespace app\views\admin;

use app\views\AbstractView;
use app\libraries\FileUtils;

class PlagiarismView extends AbstractView {

    public function plagiarismMainPage($semester, $course, $gradeables_with_plagiarism_result, $refresh_page, $nightly_rerun_info) {
        $return = "";
        $return .= <<<HTML
<div class="content">
    <h1 style="text-align: center">Lichen Plagiarism Detection -- WORK IN PROGRESS</h1><br>
    <div class="nav-buttons">
        <a class="btn btn-primary" href="{$this->core->buildUrl(array('component' => 'admin', 'page' => 'plagiarism', 'semester' => $semester, 'course'=> $course, 'action' => 'configure_new_gradeable_for_plagiarism_form'))}">+ Configure New Gradeable for Plagiarism Detection</a>
    </div><br /><br />
    <div class="sub">
    <center>
    <table style="border-collapse: separate;border-spacing: 15px 10px;">
HTML;
        $course_path = $this->core->getConfig()->getCoursePath();
        foreach ($gradeables_with_plagiarism_result as $gradeable) {
            $title = $gradeable['g_title'];
            $id = $gradeable['g_id'];
        
            $delete_form_action = $this->core->buildUrl(array('component' => 'admin', 'semester' => $semester, 'course'=> $course, 'page' => 'plagiarism', 'action' => 'delete_plagiarism_result_and_config', 'gradeable_id' => $id));
            
            if(file_exists($course_path."/lichen/ranking/".$id.".txt")) {
                $timestamp = date("F d Y H:i:s.",filemtime($course_path."/lichen/ranking/".$id.".txt"));
                $students = array_diff(scandir($course_path."/lichen/concatenated/".$id), array('.', '..'));
                $submissions =0;
                foreach($students as $student) {
                    $submissions += count(array_diff(scandir($course_path."/lichen/concatenated/".$id."/".$student), array('.', '..')));
                }
                $students = count($students);    
            }
            else {
                $timestamp = "N/A";
                $students = "N/A";
                $submissions = "N/A";
            }
            $night_rerun_status= "";
            if($nightly_rerun_info[$id] ==true) {
                $night_rerun_status = "checked";
            }

            #lichen job in queue for this gradeable but processing not started
            if (file_exists("/var/local/submitty/daemon_job_queue/lichen__" . $semester . "__" . $course . "__" . $id . ".json")) {
                $return .= <<<HTML
        <tr style="color:grey;">
            <td>$title
            </td>
            <td colspan=3><i>in queue</i>
            </td>
            <td>
                Last run: $timestamp
            </td>
            <td>
                $students students, $submissions submissions
            </td>
            <td>
                <label><input type="checkbox" {$night_rerun_status} >Nightly Re-run </label>
            </td>
        </tr>
HTML;
            }

            #lichen job in processing stage for this gradeable but not completed
            else if (file_exists("/var/local/submitty/daemon_job_queue/PROCESSING_lichen__" . $semester . "__" . $course . "__" . $id . ".json")) {
                $return .= <<<HTML
        <tr style="color:green;">
            <td>$title
            </td>
            <td colspan=3><i>running</i>
            </td>
            <td>
                Last run: $timestamp
            </td>
            <td>
                $students students, $submissions submissions
            </td>
            <td>
                <label><input type="checkbox" {$night_rerun_status} >Nightly Re-run </label>
            </td>
        </tr>
HTML;
            }

            #no lichen job
            else {
                $return .= <<<HTML
        <tr>
            <td><a href="{$this->core->buildUrl(array('component' => 'admin', 'semester' => $semester, 'course'=> $course, 'page' => 'plagiarism', 'action' => 'show_plagiarism_result', 'gradeable_id' => $id))}">$title</a>
            </td>
            <td><a href="{$this->core->buildUrl(array('component' => 'admin', 'semester' => $semester, 'course'=> $course, 'page' => 'plagiarism', 'action' => 'edit_plagiarism_saved_config', 'gradeable_id' => $id))}"><i class="fa fa-pencil" aria-hidden="true"></i></a>
            </td>
            <td><a href="{$this->core->buildUrl(array('component' => 'admin', 'semester' => $semester, 'course'=> $course, 'page' => 'plagiarism', 'action' => 're_run_plagiarism', 'gradeable_id' => $id))}"><i class="fa fa-refresh" aria-hidden="true"></i></a>
            </td>
            <td><a onclick="deletePlagiarismResultAndConfigForm('{$delete_form_action}', '{$title}');"><i class="fa fa-trash" aria-hidden="true"></i></a>
            </td>
            <td>
                Last run: $timestamp
            </td>
            <td>
                $students students, $submissions submissions
            </td>
            <td>
                <label><input type="checkbox" onclick='window.location.href = buildUrl({"component":"admin", "page" :"plagiarism", "course":"{$course}", "semester": "{$semester}", "action": "toggle_nightly_rerun", "gradeable_id":"{$id}"});' {$night_rerun_status} >Nightly Re-run </label>
            </td>
        </tr>
HTML;<commit_msg>Add top match and highest match percentage to Lichen main page (#2626)<commit_after><?php
namespace app\views\admin;

use app\views\AbstractView;
use app\libraries\FileUtils;

class PlagiarismView extends AbstractView {

    public function plagiarismMainPage($semester, $course, $gradeables_with_plagiarism_result, $refresh_page, $nightly_rerun_info) {
        $return = "";
        $return .= <<<HTML
<div class="content">
    <h1 style="text-align: center">Lichen Plagiarism Detection -- WORK IN PROGRESS</h1><br>
    <div class="nav-buttons">
        <a class="btn btn-primary" href="{$this->core->buildUrl(array('component' => 'admin', 'page' => 'plagiarism', 'semester' => $semester, 'course'=> $course, 'action' => 'configure_new_gradeable_for_plagiarism_form'))}">+ Configure New Gradeable for Plagiarism Detection</a>
    </div><br /><br />
    <div class="sub">
    <center>
    <table style="border-collapse: separate;border-spacing: 15px 10px;">
HTML;
        $course_path = $this->core->getConfig()->getCoursePath();
        foreach ($gradeables_with_plagiarism_result as $gradeable) {
            $title = $gradeable['g_title'];
            $id = $gradeable['g_id'];
        
            $delete_form_action = $this->core->buildUrl(array('component' => 'admin', 'semester' => $semester, 'course'=> $course, 'page' => 'plagiarism', 'action' => 'delete_plagiarism_result_and_config', 'gradeable_id' => $id));
            
            if(file_exists($course_path."/lichen/ranking/".$id.".txt")) {
                $timestamp = date("F d Y H:i:s.",filemtime($course_path."/lichen/ranking/".$id.".txt"));
                $students = array_diff(scandir($course_path."/lichen/concatenated/".$id), array('.', '..'));
                $submissions =0;
                foreach($students as $student) {
                    $submissions += count(array_diff(scandir($course_path."/lichen/concatenated/".$id."/".$student), array('.', '..')));
                }
                $students = count($students);    
            }
            else {
                $timestamp = "N/A";
                $students = "N/A";
                $submissions = "N/A";
            }
            $night_rerun_status= "";
            if($nightly_rerun_info[$id] ==true) {
                $night_rerun_status = "checked";
            }

            #lichen job in queue for this gradeable but processing not started
            if (file_exists("/var/local/submitty/daemon_job_queue/lichen__" . $semester . "__" . $course . "__" . $id . ".json")) {
                $return .= <<<HTML
        <tr style="color:grey;">
            <td>$title
            </td>
            <td colspan=3><i>in queue</i>
            </td>
            <td>
                Last run: $timestamp
            </td>
            <td>
                $students students, $submissions submissions
            </td>
            <td>
                <label><input type="checkbox" {$night_rerun_status} >Nightly Re-run </label>
            </td>
        </tr>
HTML;
            }

            #lichen job in processing stage for this gradeable but not completed
            else if (file_exists("/var/local/submitty/daemon_job_queue/PROCESSING_lichen__" . $semester . "__" . $course . "__" . $id . ".json")) {
                $return .= <<<HTML
        <tr style="color:green;">
            <td>$title
            </td>
            <td colspan=3><i>running</i>
            </td>
            <td>
                Last run: $timestamp
            </td>
            <td>
                $students students, $submissions submissions
            </td>
            <td>
                <label><input type="checkbox" {$night_rerun_status} >Nightly Re-run </label>
            </td>
        </tr>
HTML;
            }

            #no lichen job
            else {
                $ranking_file_path= "/var/local/submitty/courses/".$semester."/".$course."/lichen/ranking/".$id.".txt";
                if(file_get_contents($ranking_file_path) == "") {
                    $matches_and_topmatch= "0 students matched, N/A top match";
                    
                    $return .= <<<HTML
        <tr>
            <td>$title
            </td>
HTML;
                }
                else {
                    $content =file_get_contents($ranking_file_path);
                    $content = trim(str_replace(array("\r", "\n"), '', $content));
                    $rankings = preg_split('/ +/', $content);
                    $rankings = array_chunk($rankings,3);
                    $matches_and_topmatch = count($rankings)." students matched, ".$rankings[0][0]." top match";
                    
                    $return .= <<<HTML
        <tr>
            <td><a href="{$this->core->buildUrl(array('component' => 'admin', 'semester' => $semester, 'course'=> $course, 'page' => 'plagiarism', 'action' => 'show_plagiarism_result', 'gradeable_id' => $id))}">$title</a>
            </td>
HTML;
                }
                
                $return .= <<<HTML
            <td><a href="{$this->core->buildUrl(array('component' => 'admin', 'semester' => $semester, 'course'=> $course, 'page' => 'plagiarism', 'action' => 'edit_plagiarism_saved_config', 'gradeable_id' => $id))}"><i class="fa fa-pencil" aria-hidden="true"></i></a>
            </td>
            <td><a href="{$this->core->buildUrl(array('component' => 'admin', 'semester' => $semester, 'course'=> $course, 'page' => 'plagiarism', 'action' => 're_run_plagiarism', 'gradeable_id' => $id))}"><i class="fa fa-refresh" aria-hidden="true"></i></a>
            </td>
            <td><a onclick="deletePlagiarismResultAndConfigForm('{$delete_form_action}', '{$title}');"><i class="fa fa-trash" aria-hidden="true"></i></a>
            </td>
            <td>
                Last run: $timestamp
            </td>
            <td>
                $students students, $submissions submissions
            </td>
            <td>
                $matches_and_topmatch
            </td>
            <td>
                <label><input type="checkbox" onclick='window.location.href = buildUrl({"component":"admin", "page" :"plagiarism", "course":"{$course}", "semester": "{$semester}", "action": "toggle_nightly_rerun", "gradeable_id":"{$id}"});' {$night_rerun_status} >Nightly Re-run </label>
            </td>
        </tr>
HTML;
{'fraction_non_alphanumeric': 0.17584690974915956, 'fraction_numerical': 0.002241186104646151, 'mean_word_length': 2.0515518148342977, 'pattern_counts': {'":': 10, '<': 200, '<?xml version=': 0, '>': 252, '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': '7045068', 'n_tokens_mistral': 3667, 'n_tokens_neox': 3496, 'n_words': 663}