From 14bb9229846d86ae0eaf468241760ead0b04ff5d Mon Sep 17 00:00:00 2001
From: Astor Bizard <astor.bizard@grenoble-inp.fr>
Date: Tue, 9 Jun 2020 11:08:49 +0200
Subject: [PATCH] Calls to evaluation.php for multiple evaluation now more
 fluid

---
 forms/evaluation.php      | 42 ++++++++++---------
 forms/grade_form.php      | 81 ++++++++++++-------------------------
 forms/gradesubmission.php |  6 +--
 views/submissionslist.php | 85 ++++++++++++++-------------------------
 vpl.class.php             |  4 +-
 5 files changed, 85 insertions(+), 133 deletions(-)

diff --git a/forms/evaluation.php b/forms/evaluation.php
index 742bef87..e588535c 100644
--- a/forms/evaluation.php
+++ b/forms/evaluation.php
@@ -29,39 +29,43 @@ require_once(dirname(__FILE__).'/../vpl_submission_CE.class.php');
 require_once(dirname(__FILE__).'/../editor/editor_utility.php');
 require_login();
 
+global $USER, $DB, $OUTPUT;
 $id = required_param( 'id', PARAM_INT );
+$userid = optional_param( 'userid', $USER->id, PARAM_INT );
+$userlist = optional_param( 'userlist', null, PARAM_RAW );
+$nexturl = optional_param( 'returnurl', "../forms/submissionview.php?id={$id}&userid={$userid}", PARAM_RAW );
 $vpl = new mod_vpl( $id );
-$vpl->prepare_page( 'forms/evaluation.php', array (
-        'id' => $id
-) );
-$userid = optional_param( 'userid', false, PARAM_INT );
-if ((! $userid || $userid == $USER->id) && $vpl->get_instance()->evaluate) { // Evaluate own submission.
-    $userid = $USER->id;
+
+if ($userid == $USER->id && $vpl->get_instance()->evaluate) { // Evaluate own submission.
+    $vpl->prepare_page( 'forms/evaluation.php', array ( 'id' => $id ) );
     $vpl->require_capability( VPL_SUBMIT_CAPABILITY );
+    $vpl->restrictions_check();
 } else { // Evaluate other user submission.
     $vpl->prepare_page( 'forms/evaluation.php', array ( 'id' => $id, 'userid' => $userid ) );
     $vpl->require_capability( VPL_GRADE_CAPABILITY );
 }
-if ($USER->id == $userid) {
-    $vpl->restrictions_check();
-}
+
 vpl_editor_util::generate_requires_evaluation();
 // Display page.
 $vpl->print_header( get_string( 'evaluation', VPL ) );
 flush();
-$course = $vpl->get_course();
-$instance = $vpl->get_instance();
 echo '<h2>' . s( get_string( 'evaluating', VPL ) ) . '</h2>';
+$text = '';
+if ($userlist !== null) {
+    $users = explode(',', $userlist);
+    $index = array_search($userid, $users);
+    if ($index !== false) {
+        $text .= ($index + 1) . '/' . count($users) . ' ';
+        $next = $index + 1;
+        if ($next < count($users)) {
+            $escapedurl = urlencode($nexturl);
+            $nexturl = "../forms/evaluation.php?id={$id}&userid={$users[$next]}&userlist={$userlist}&returnurl={$escapedurl}";
+        }
+    }
+}
 $user = $DB->get_record( 'user', array ( 'id' => $userid ) );
-$text = ' ' . $vpl->user_picture( $user );
-$text .= ' ' . fullname( $user );
+$text .= $vpl->user_fullname_picture( $user, false );
 echo $OUTPUT->box( $text );
 $ajaxurl = "edit.json.php?id={$id}&userid={$userid}&action=";
-if (optional_param( 'grading', 0, PARAM_INT )) {
-    $inpopup = optional_param( 'inpopup', 0, PARAM_INT );
-    $nexturl = "../forms/gradesubmission.php?id={$id}&userid={$userid}&inpopup={$inpopup}";
-} else {
-    $nexturl = "../forms/submissionview.php?id={$id}&userid={$userid}";
-}
 vpl_editor_util::generate_evaluate_script( $ajaxurl, $nexturl );
 $vpl->print_footer();
diff --git a/forms/grade_form.php b/forms/grade_form.php
index 59599a54..011eb897 100644
--- a/forms/grade_form.php
+++ b/forms/grade_form.php
@@ -13,7 +13,7 @@
 //
 // You should have received a copy of the GNU General Public License
 // along with VPL for Moodle.  If not, see <http://www.gnu.org/licenses/>.
- 
+
 /**
  * Grade form definition
  *
@@ -22,14 +22,15 @@
  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  * @author Juan Carlos Rodríguez-del-Pino <jcrodriguez@dis.ulpgc.es>
  */
- 
+
 defined( 'MOODLE_INTERNAL' ) || die();
- 
+
+global $CFG;
 require_once(dirname(__FILE__).'/../../../config.php');
 require_once($CFG->libdir.'/formslib.php');
 require_once($CFG->libdir.'/gradelib.php');
 require_once(dirname(__FILE__).'/../locallib.php');
- 
+
 class mod_vpl_grade_form extends moodleform {
     protected $vpl;
     protected $submission;
@@ -82,26 +83,25 @@ class mod_vpl_grade_form extends moodleform {
         // Type value => introduce value.
         // Add advanced grading.
         $gradeid = $this->vpl->get_grade_info();
-          $gradinginstance = $this->submission->get_grading_instance();
- 
+        $gradinginstance = $this->submission->get_grading_instance();
+
         if ($gradinginstance) {
             $res = $this->submission->getCE();
             if ($res ['executed']) {
                 $graderaw = $this->submission->proposedGrade($res['execution']);
-            }else{
+            } else {
                 $graderaw = 0;
             }
             $gridscore = $gradinginstance->get_controller()->get_min_max_score()['maxscore'];
-           
+
             $mform->addElement('header','hAdvancedGrading', get_string( 'gradingmanagement','grading') );
             $mform->addElement('grading',
-                               'advancedgrading',
+                                'advancedgrading',
                                 '',
-                       array('gradinginstance' => $gradinginstance)); 
+                                array('gradinginstance' => $gradinginstance));
             $mform->addElement('hidden','advancedgradinginstanceid', $gradinginstance->get_id());
             $mform->setType('advancedgradinginstanceid', PARAM_INT);
-        }
-        // Numeric grade.
+            // Numeric grade.
             if ($grade > 0) {
                 // Link to recalculate numeric grade from comments.
                 $jscript = 'VPL.mergeGrade(' . $grade . ','.$graderaw.','.$gridscore.')';
@@ -110,9 +110,10 @@ class mod_vpl_grade_form extends moodleform {
                 $mform->addElement('button','btnmerge', get_string( 'merge', VPL ),'onclick="' . $jscript . '"' );
                 //$mform->registerNoSubmitButton('btnmerge');
             }
+        }
         $mform->addElement('header','hGrade', get_string( 'grade') );
         //$mform->addElement('html','<div id="vpl_grade_form">');
-            
+
         $buttonarray=array();
         if ($grade != 0) {
             if ($grade > 0) {
@@ -129,60 +130,31 @@ class mod_vpl_grade_form extends moodleform {
         $buttonarray[] =& $mform->createElement('submit', 'removegrade', get_string( 'removegrade', VPL ) );
         // Tranfer files to teacher's work area.
         $url = vpl_mod_href( 'forms/edit.php', 'id', $id, 'userid', $userid, 'privatecopy', 1 );
-        $options = array (
-                'height' => 550,
-                'width' => 780,
-                'directories' => 0,
-                'location' => 0,
-                'menubar' => 0,
-                'personalbar' => 0,
-                'status' => 0,
-                'toolbar' => 0
-        );
-        $action = new popup_action( 'click', $url, 'privatecopy' . ($vplinstance->id), $options );
-        //~ $mform->addElement('html', $OUTPUT->action_link( $url, get_string( 'copy', VPL ), $action,array('class' =>'btn btn-primary') ) );
-        //~ $buttonarray[] =& $mform->createElement('html', $OUTPUT->action_link( $url, get_string( 'copy', VPL ), $action,array('class' =>'btn btn-primary') ) );
-        $buttonarray[] =& $mform->createElement('button', 'copy',get_string( 'copy', VPL ),'onclick="windows.open(' .  $url. ');"' );
- 
+        $buttonarray[] =& $mform->createElement('button', 'copy', get_string( 'copy', VPL ),
+                'onclick="window.open(\'' . htmlspecialchars_decode($url) . '\');"');
+
         if ($vplinstance->evaluate) {
             // Link to recalculate numeric grade from comments.
-            $url = vpl_mod_href( 'forms/evaluation.php', 'id', $id, 'userid', $userid, 'grading', 1, 'inpopup', $inpopup );
-            $html = ' <a class="btn btn-primary" href="' . $url . '">' . s( get_string( 'evaluate', VPL ) ) . '</a>';
-            //~ $mform->addElement('html', $html );
-            $buttonarray[] =& $mform->createElement('button', 'evaluate',get_string( 'evaluate', VPL ),'onclick="windows.open(' . $url . ');"');
+            $url = vpl_mod_href( 'forms/evaluation.php', 'id', $id, 'userid', $userid,
+                    'returnurl', "../forms/gradesubmission.php?id={$id}&userid={$userid}&inpopup={$inpopup}" );
+            $buttonarray[] =& $mform->createElement('button', 'evaluate', get_string( 'evaluate', VPL ),
+                    'onclick="window.location.assign(\'' . htmlspecialchars_decode($url) . '\');"');
         }
         // Numeric grade.
         if ($grade > 0) {
             // Link to recalculate numeric grade from comments.
-            $jscript = 'VPL.calculateGrade(' . $grade . ')';
- 
-            $html = ' <a class="btn btn-primary" href="javascript:void(0);" onclick="' . $jscript . '">' . s( get_string( 'calculate', VPL ) ) . '</a>';
-            //~ $mform->addElement('html', $html );
-            $buttonarray[] =& $mform->createElement('button', 'calculate',get_string( 'calculate', VPL ),'onclick="' . $jscript . '"' );
+            $buttonarray[] =& $mform->createElement('button', 'calculate', get_string( 'calculate', VPL ),
+                    'onclick="VPL.calculateGrade(' . $grade . ')"');
         }
         $mform->addGroup($buttonarray, 'buttonar', get_string('grades'), array(' '), false);
         //$mform->addElement('html', '</div>' );
         $textarray=array();
         if ($grade != 0) {
             //$mform->addElement('html','</br><b>'.s( get_string( 'comments', VPL )).'</b></br>' );
-            $textarray[] =& $mform->createElement('textarea', 'comments', get_string( 'comments', VPL ), 'rows="18" cols="70"' );
-        }
-         
- 
-        //$output = '<div id="vpl_grade_comments">';
-        $comments = $this->vpl->get_grading_help();
-        if ($comments > '') {
-            $output .= $OUTPUT->box_start();
-            $output .= '<b>' . s(get_string( 'listofcomments', VPL )) . '</b><hr />';
-            $output .= $comments;
-            $output .= $OUTPUT->box_end();
+            //$textarray[] =& $mform->createElement('textarea', 'comments', get_string( 'comments', VPL ), 'rows="18" cols="70"' );
+            $mform->addElement('textarea', 'comments', get_string( 'comments', VPL ), 'rows="18" cols="70"' );
         }
-        //$output .= '</div>';
-        
-        $textarray[] =& $mform->createElement('static', $comments);
-        $mform->addGroup($textarray, 'textar', get_string( 'comments', VPL ), array(' '), false);
-       
-        
+
         if (! empty( $CFG->enableoutcomes )) {
             $gradinginfo = grade_get_grades( $vpl->get_course()->id, 'mod', 'vpl', $vplinstance->id, $userid );
             if (! empty( $gradinginfo->outcomes )) {
@@ -207,4 +179,3 @@ class mod_vpl_grade_form extends moodleform {
         echo $OUTPUT->box_end();
     }
 }
- 
\ No newline at end of file
diff --git a/forms/gradesubmission.php b/forms/gradesubmission.php
index 4f519312..60948c80 100644
--- a/forms/gradesubmission.php
+++ b/forms/gradesubmission.php
@@ -71,7 +71,7 @@ if (! $subinstance) {
 }
 $submissionid = $subinstance->id;
 
-if ($vpl->is_inconsistent_user( $subinstance->userid, $userid )) {
+if ($vpl->is_inconsistent_user( $subinstance->userid, $userid ) && !$vpl->has_capability( VPL_GRADE_CAPABILITY )) {
     vpl_grade_header( $vpl, $inpopup );
     vpl_redirect( $link, 'vpl submission user inconsistence', 'error' );
 }
@@ -98,8 +98,8 @@ if ($subinstance->dategraded == 0 || $subinstance->grader == $USER->id || $subin
         vpl_inmediate_redirect( $link );
     } else if ($fromform = $gradeform->get_data()) { // Grade (new or update).
         if (isset( $fromform->evaluate )) {
-            $url = vpl_mod_href( 'forms/evaluation.php', 'id', $fromform->id, 'userid'
-                                 , $fromform->userid, 'grading', 1, 'inpopup', $inpopup );
+            $url = vpl_mod_href( 'forms/evaluation.php', 'id', $fromform->id, 'userid', $fromform->userid,
+                    'returnurl', "../forms/gradesubmission.php?id={$id}&userid={$userid}&inpopup={$inpopup}" );
             vpl_inmediate_redirect( $url );
         }
         if (isset( $fromform->removegrade )) {
diff --git a/views/submissionslist.php b/views/submissionslist.php
index ccdff40b..49e00043 100644
--- a/views/submissionslist.php
+++ b/views/submissionslist.php
@@ -134,29 +134,6 @@ class vpl_submissionlist_order {
         }
     }
 }
-function vpl_evaluate($vpl, $alldata, $userinfo, $nevaluation, $groupsurl) {
-    global $OUTPUT;
-    $nevaluation ++;
-    try {
-        echo '<h2>' . s( get_string( 'evaluating', VPL ) ) . '</h2>';
-        $text = $nevaluation . '/' . count( $alldata );
-        $text .= ' ' . $vpl->user_picture( $userinfo );
-        $text .= ' ' . fullname( $userinfo );
-        $text .= ' <a href="' . $groupsurl . '">' . get_string( 'cancel' ) . '</a>';
-        echo $OUTPUT->box( $text );
-        $id = $vpl->get_course_module()->id;
-        $userid = $userinfo->id;
-        $ajaxurl = "../forms/edit.json.php?id={$id}&userid={$userinfo->id}&action=";
-        $url = vpl_url_add_param( $groupsurl, 'evaluate', optional_param( 'evaluate', 0, PARAM_INT ) );
-        $url = vpl_url_add_param( $url, 'nevaluation', $nevaluation );
-        $nexturl = str_replace( '&amp;', '&', urldecode( $url ) );
-        vpl_editor_util::generate_evaluate_script( $ajaxurl, $nexturl );
-    } catch ( Exception $e ) {
-        vpl_notice( $e->getMessage(), 'error' );
-    }
-    $vpl->print_footer();
-    die();
-}
 function vpl_submissionlist_arrow($burl, $sort, $selsort, $seldir) {
     global $OUTPUT;
     $newdir = 'down';
@@ -215,16 +192,10 @@ require_login();
 
 $id = required_param( 'id', PARAM_INT );
 $group = optional_param( 'group', - 1, PARAM_INT );
-$evaluate = optional_param( 'evaluate', 0, PARAM_INT );
-$nevaluation = optional_param( 'nevaluation', 0, PARAM_INT );
 $showgrades = optional_param( 'showgrades', 0, PARAM_INT );
 $sort = vpl_get_set_session_var( 'subsort', 'lastname', 'sort' );
 $sortdir = vpl_get_set_session_var( 'subsortdir', 'move', 'sortdir' );
 $subselection = vpl_get_set_session_var( 'subselection', 'allsubmissions', 'selection' );
-if ($evaluate > 0) {
-    require_once($CFG->dirroot.'/mod/vpl/editor/editor_utility.php');
-    vpl_editor_util::generate_requires_evaluation();
-}
 $vpl = new mod_vpl( $id );
 $vpl->prepare_page( 'views/submissionslist.php', array (
         'id' => $id
@@ -434,11 +405,15 @@ if ($showgrades) {
 // Sort by sort field.
 usort( $alldata, vpl_submissionlist_order::set_order( $sort, $sortdir != 'up' ) );
 $showphoto = count( $alldata ) < 100;
-$evaluationchoise = 0;
 $usernumber = 0;
 $ngrades = array (); // Number of revisions made by teacher.
 $nextids = array (); // Information to get next user in list.
 $lastid = 0; // Last id for next.
+$userslists = array(
+        'notexecuted' => array(),
+        'notgraded' => array(),
+        'all' => array()
+);
 foreach ($alldata as $data) {
     $actions = new action_menu();
     if ($vpl->is_group_activity()) {
@@ -485,9 +460,7 @@ foreach ($alldata as $data) {
             $prev = '';
         }
         $subid = $subinstance->id;
-        if ($evaluate == 4 && $nevaluation <= $usernumber) { // Need evaluation.
-            vpl_evaluate( $vpl, $alldata, $user, $usernumber, $groupsurl );
-        }
+        $userslists['all'][] = $user->id;
         if ($subinstance->dategraded > 0) {
             $text = $submission->get_grade_core();
             // Add proposed grade diff.
@@ -530,18 +503,16 @@ foreach ($alldata as $data) {
                 }
             }
         } else {
+            $userslists['notgraded'][] = $user->id;
             $result = $submission->getCE();
             $text = '';
-            if (($evaluate == 1 && $result ['compilation'] === 0)
-                || ($evaluate == 2 && $result ['executed'] === 0 && $nevaluation <= $usernumber)
-                || ($evaluate == 3 && $nevaluation <= $usernumber)) { // Need evaluation.
-                    vpl_evaluate( $vpl, $alldata, $user, $usernumber, $groupsurl );
-            }
             if ($result ['executed'] !== 0) {
                 $prograde = $submission->proposedGrade( $result ['execution'] );
                 if ($prograde > '') {
                     $text = get_string( 'proposedgrade', VPL, $submission->get_grade_core( $prograde ) );
                 }
+            } else {
+                $userslists['notexecuted'][] = $user->id;
             }
             if ($text == '') {
                 $text = get_string( 'nograde' );
@@ -675,22 +646,28 @@ $urls = array_merge( array (
 $urlsel = new url_select( $urls, $urlindex [$subselection] );
 $urlsel->set_label( get_string( 'submissionselection', VPL ) );
 echo $OUTPUT->render( $urlsel );
-if (($gradeable || $vpl->get_instance()->evaluate) && $subselection != 'notgraded') {
-    $urlbase = $CFG->wwwroot . "/mod/vpl/views/submissionslist.php?id=$id&sort=$sort"
-               ."&sortdir=$sortdir&selection=$subselection&evaluate=";
-    $urls = array (
-            0 => null,
-            2 => $urlbase . '2',
-            '3' => $urlbase . '3',
-            4 => $urlbase . '4'
-    );
-    $urlsel = new url_select( array (
-            $urls [2] => get_string( 'notexecuted', VPL ),
-            $urls [3] => get_string( 'notgraded', VPL ),
-            $urls [4] => get_string( 'all' )
-    ), $urls [$evaluate] );
-    $urlsel->set_label( get_string( 'evaluate', VPL ) );
-    echo $OUTPUT->render( $urlsel );
+
+// Print evaluation buttons.
+if ($gradeable || $vpl->get_instance()->evaluate) {
+    $returnurl = "../views/submissionslist.php?id={$id}&group={$group}&showgrades={$showgrades}";
+    $baseurl = vpl_mod_href('forms/evaluation.php', 'id', $id, 'returnurl', $returnurl);
+    echo '<br />';
+    echo '<span>' . get_string( 'evaluate', VPL ) . ' </span>';
+    $stringmanager = get_string_manager();
+    foreach ($userslists as $type => $userslist) {
+        if ($stringmanager->string_exists($type, VPL)) {
+            $label = get_string($type, VPL);
+        } else {
+            $label = get_string($type);
+        }
+        $label .= ' (' . count($userslist) . ')';
+        $url = vpl_url_add_param($baseurl, 'userid', reset($userslist));
+        $url = vpl_url_add_param($url, 'userlist', implode(',', $userslist));
+        $button = new single_button(new moodle_url($url), $label, 'get');
+        $button->disabled = empty($userslist);
+        echo $OUTPUT->render($button);
+    }
+    echo '<br />';
 }
 echo '<br />';
 @ob_flush();
diff --git a/vpl.class.php b/vpl.class.php
index 918e4d42..548c20a2 100644
--- a/vpl.class.php
+++ b/vpl.class.php
@@ -1055,8 +1055,8 @@ class mod_vpl {
      *            user object return HTML code to show user picture
      * @return String
      */
-    public function user_fullname_picture($user) {
-        return $this->user_picture( $user ) . ' ' . $this->fullname( $user );
+    public function user_fullname_picture($user, $withlink = true) {
+        return $this->user_picture( $user ) . ' ' . $this->fullname( $user, $withlink );
     }
 
     /**
-- 
GitLab