From a104e476102a4e36abb9788165aa50303a76114e Mon Sep 17 00:00:00 2001
From: Astor Bizard <astor.bizard@grenoble-inp.fr>
Date: Thu, 19 Mar 2020 12:26:37 +0100
Subject: [PATCH] Fixed diff algorithm. Removed zero-diff files from diff
 graph.

---
 similarity/diff.class.php   |  6 +++---
 views/vpl_grapher.class.php | 18 +++++++++++++++++-
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/similarity/diff.class.php b/similarity/diff.class.php
index 97f5835f..b17072fc 100644
--- a/similarity/diff.class.php
+++ b/similarity/diff.class.php
@@ -474,7 +474,7 @@ class vpl_diff {
      * @param string $filedata1
      * @param string $filedata2
      * @param int $timelimit The maximum amount of time to spend on this computation (in milliseconds).
-     *  If provided and the time limit is exceeded, an exception will be thrown.
+     *  If provided and the time limit is exceeded, a moodle_exception with code 'difftoolarge' will be thrown.
      * @return int The diff in number of chars.
      */
     static public function compute_filediff($filedata1, $filedata2, $timelimit=0) {
@@ -500,7 +500,7 @@ class vpl_diff {
         while (true) {
             if ($timelimit > 0 && microtime(true) - $starttime > $timelimit / 1000) {
                 // Time out.
-                throw new Exception();
+                throw new moodle_exception('difftoolarge');
             }
             while (count($queue[$priority]) == 0) {
                 $priority++;
@@ -525,7 +525,7 @@ class vpl_diff {
                 // Found shortest diff amongst lines.
                 // Backtrack to compute total diff as a sum of lines diff.
                 $totaldiff = 0;
-                while ($i1 > 0 && $i2 > 0) {
+                while ($i1 > 0 || $i2 > 0) {
                     if (!isset($prev[$i1][$i2])) {
                         $i1--;
                         $i2--;
diff --git a/views/vpl_grapher.class.php b/views/vpl_grapher.class.php
index 6429780e..236aab2b 100644
--- a/views/vpl_grapher.class.php
+++ b/views/vpl_grapher.class.php
@@ -186,9 +186,25 @@ class vpl_grapher {
             if ($diff && count($names) > 1) {
                 $names[] = get_string('total');
                 $series[get_string('total')] = $totalseries;
+
+                // Filter out all zero-diff files.
+                $names = array_filter($names, function($name) use(&$series){
+                    foreach ($series[$name] as $value){
+                        if ($value != 0) {
+                            return true;
+                        }
+                    }
+                    // All diffs are zero for this file - do not include it.
+                    unset($series[$name]);
+                    return false;
+                });
             }
             self::draw( $title, get_string( 'submissions', VPL ) , get_string( "sizeb" ), $subsn, $series, $names );
-        } catch (Exception $e) {
+        } catch (moodle_exception $e) {
+            if ($e->errorcode != 'difftoolarge') {
+                // The exception was not thrown by diff calculation, don't catch it.
+                throw $e;
+            }
             global $PAGE;
             echo '<span class="chart-area nograph">';
             echo '<b>' . $title . '</b><br>';
-- 
GitLab