From 519524a7a4cc033477d2f709b3dd724052a91d69 Mon Sep 17 00:00:00 2001
From: Astor Bizard <astor.bizard@grenoble-inp.fr>
Date: Tue, 25 Feb 2020 17:27:06 +0100
Subject: [PATCH] Added default resources limits values in Maximum execution
 resources limits form.

---
 forms/executionlimits.php | 55 ++++++++++++++++++++++++---------------
 settings.php              |  2 +-
 2 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/forms/executionlimits.php b/forms/executionlimits.php
index 87e3b442..21a2de70 100644
--- a/forms/executionlimits.php
+++ b/forms/executionlimits.php
@@ -42,24 +42,19 @@ class mod_vpl_executionlimits_form extends moodleform {
         $mform->addElement( 'hidden', 'id', $id );
         $mform->setType( 'id', PARAM_INT );
         $mform->addElement( 'header', 'header_execution_limits', get_string( 'resourcelimits', VPL ) );
-        $mform->addElement( 'select', 'maxexetime', get_string( 'maxexetime', VPL )
-                           , vpl_get_select_time( ( int ) $plugincfg->maxexetime ) );
-        $mform->setType( 'maxexetime', PARAM_INT );
-        if ($instance->maxexetime) {
-            $mform->setDefault( 'maxexetime', $instance->maxexetime );
-        }
-        $mform->addElement( 'select', 'maxexememory', get_string( 'maxexememory', VPL )
-                           , vpl_get_select_sizes( 16 * 1024 * 1024, ( int ) $plugincfg->maxexememory ) );
-        $mform->setType( 'maxexememory', PARAM_INT );
-        if ($instance->maxexememory) {
-            $mform->setDefault( 'maxexememory', $instance->maxexememory );
-        }
-        $mform->addElement( 'select', 'maxexefilesize', get_string( 'maxexefilesize', VPL )
-                           , vpl_get_select_sizes( 1024 * 256, ( int ) $plugincfg->maxexefilesize ) );
-        $mform->setType( 'maxexefilesize', PARAM_INT );
-        if ($instance->maxexefilesize) {
-            $mform->setDefault( 'maxexefilesize', $instance->maxexefilesize );
-        }
+
+        self::add_resource_limit_select($mform, 'maxexetime', get_string( 'maxexetime', VPL ),
+                vpl_get_select_time( ( int ) $plugincfg->maxexetime ),
+                $plugincfg->defaultexetime, $instance->maxexetime);
+
+        self::add_resource_limit_select($mform, 'maxexememory', get_string( 'maxexememory', VPL ),
+                vpl_get_select_sizes( 16 * 1024 * 1024, ( int ) $plugincfg->maxexememory ),
+                $plugincfg->defaultexememory, $instance->maxexememory);
+
+        self::add_resource_limit_select($mform, 'maxexefilesize', get_string( 'maxexefilesize', VPL ),
+                vpl_get_select_sizes( 1024 * 256, ( int ) $plugincfg->maxexefilesize ),
+                $plugincfg->defaultexefilesize, $instance->maxexefilesize);
+
         $mform->addElement( 'text', 'maxexeprocesses', get_string( 'maxexeprocesses', VPL ) );
         $mform->setType( 'maxexeprocesses', PARAM_INT );
         if ($instance->maxexeprocesses) {
@@ -67,6 +62,26 @@ class mod_vpl_executionlimits_form extends moodleform {
         }
         $mform->addElement( 'submit', 'savelimitoptions', get_string( 'saveoptions', VPL ) );
     }
+
+    /**
+     * Adds a select element to the resource limits form.
+     * @param MoodleQuickForm $mform The form to which the element will be added.
+     * @param string $name The name of the element.
+     * @param string $label The label of the element.
+     * @param array $selectoptions The selectable options of the element.
+     *  The [0] => 'select' option will be replaced by a localized string describing the default value.
+     * @param int $defaultvalue The default value to use when no other value is selected.
+     * @param int $currentvalue The value to which the element should be set when displaying the form.
+     */
+    private static function add_resource_limit_select($mform, $name, $label, $selectoptions, $defaultvalue, $currentvalue) {
+        $defaultvaluestring = $selectoptions[ vpl_get_array_key($selectoptions, $defaultvalue) ];
+        $selectoptions[0] = get_string('default') . ' (' . $defaultvaluestring . ')';
+        $mform->addElement( 'select', $name, $label, $selectoptions );
+        $mform->setType( $name, PARAM_INT );
+        if ($currentvalue) {
+            $mform->setDefault( $name, $currentvalue );
+        }
+    }
 }
 
 require_login();
@@ -79,11 +94,9 @@ $vpl->prepare_page( 'forms/executionlimits.php', array (
 vpl_include_jsfile( 'hideshow.js' );
 $vpl->require_capability( VPL_MANAGE_CAPABILITY );
 // Display page.
-$vpl->print_header( get_string( 'execution', VPL ) );
+$vpl->print_header( get_string( 'resourcelimits', VPL ) );
 $vpl->print_heading_with_help( 'resourcelimits' );
 
-$course = $vpl->get_course();
-$fgp = $vpl->get_fgm('execution'); 
 $mform = new mod_vpl_executionlimits_form( 'executionlimits.php', $vpl );
 if ($fromform = $mform->get_data()) {
     if (isset( $fromform->savelimitoptions )) {
diff --git a/settings.php b/settings.php
index c20c670a..8ff8dde5 100644
--- a/settings.php
+++ b/settings.php
@@ -33,7 +33,7 @@ $gigabyte = 1024 * $megabyte;
 $minute = 60;
 $listmaxfilesize = vpl_get_select_sizes( 64 * $kbyte, vpl_get_max_post_size() );
 $listmaxtime = vpl_get_select_time( 120 * $minute );
-$listmaxexefilesize = vpl_get_select_sizes( 16 * $megabyte );
+$listmaxexefilesize = vpl_get_select_sizes( 256 * $kbyte );
 $listmaxexememory = vpl_get_select_sizes( 16 * $megabyte );
 $defaultmaxfilesize = vpl_get_array_key( $listmaxfilesize, $megabyte );
 $defaultmaxtime = vpl_get_array_key( $listmaxtime, 16 * $minute );
-- 
GitLab