mirror of
https://github.com/moodle/moodle.git
synced 2025-08-08 10:26:40 +02:00
MDL-37122 core get_max_upload_sizes: Include "Course/Site/Activity limit (X)" in list of options
This change removes the "0 bytes" option from the get_max_upload_sizes list and replaces it with "Course limit (X)" or "Site limit (X)" (whichever is smaller). This means we can remove all custom handling in the modules that were removing and adding these options. It only affects pages that pass valid options for sitelimit and courselimit - so admin pages will work correctly. It also orders the list so the course/site limit options will be first (as it will be the largest). AMOS START REM [courseuploadlimit, core] has been parameterized to get [uploadlimitwithsize, core] AMOS END
This commit is contained in:
parent
8fd56c1942
commit
6b219310cf
10 changed files with 122 additions and 18 deletions
|
@ -6022,7 +6022,10 @@ function get_user_max_upload_file_size($context, $sitebytes=0, $coursebytes=0, $
|
|||
* array of possible sizes in an array, translated to the
|
||||
* local language.
|
||||
*
|
||||
* @todo Finish documenting this function
|
||||
* The list of options will go up to the minimum of $sitebytes, $coursebytes or $modulebytes.
|
||||
*
|
||||
* If $coursebytes or $sitebytes is not 0, an option will be included for "Course/Site upload limit (X)"
|
||||
* with the value set to 0. This option will be the first in the list.
|
||||
*
|
||||
* @global object
|
||||
* @uses SORT_NUMERIC
|
||||
|
@ -6041,7 +6044,7 @@ function get_max_upload_sizes($sitebytes = 0, $coursebytes = 0, $modulebytes = 0
|
|||
}
|
||||
|
||||
$filesize = array();
|
||||
$filesize[intval($maxsize)] = display_size($maxsize);
|
||||
$filesize[(string)intval($maxsize)] = display_size($maxsize);
|
||||
|
||||
$sizelist = array(10240, 51200, 102400, 512000, 1048576, 2097152,
|
||||
5242880, 10485760, 20971520, 52428800, 104857600);
|
||||
|
@ -6063,12 +6066,31 @@ function get_max_upload_sizes($sitebytes = 0, $coursebytes = 0, $modulebytes = 0
|
|||
}
|
||||
|
||||
foreach ($sizelist as $sizebytes) {
|
||||
if ($sizebytes < $maxsize) {
|
||||
$filesize[intval($sizebytes)] = display_size($sizebytes);
|
||||
if ($sizebytes < $maxsize && $sizebytes > 0) {
|
||||
$filesize[(string)intval($sizebytes)] = display_size($sizebytes);
|
||||
}
|
||||
}
|
||||
|
||||
krsort($filesize, SORT_NUMERIC);
|
||||
$limitlevel = '';
|
||||
$displaysize = '';
|
||||
if ($modulebytes &&
|
||||
(($modulebytes < $coursebytes || $coursebytes == 0) &&
|
||||
($modulebytes < $sitebytes || $sitebytes == 0))) {
|
||||
$limitlevel = get_string('activity', 'core');
|
||||
$displaysize = display_size($modulebytes);
|
||||
} else if ($coursebytes && ($coursebytes < $sitebytes || $sitebytes == 0)) {
|
||||
$limitlevel = get_string('course', 'core');
|
||||
$displaysize = display_size($coursebytes);
|
||||
} else if ($sitebytes) {
|
||||
$limitlevel = get_string('site', 'core');
|
||||
$displaysize = display_size($sitebytes);
|
||||
}
|
||||
|
||||
if ($limitlevel) {
|
||||
$params = (object) array('contextname'=>$limitlevel, 'displaysize'=>$displaysize);
|
||||
$filesize = array('0'=>get_string('uploadlimitwithsize', 'core', $params)) + $filesize;
|
||||
}
|
||||
|
||||
return $filesize;
|
||||
}
|
||||
|
|
|
@ -2245,4 +2245,95 @@ class moodlelib_testcase extends advanced_testcase {
|
|||
set_config('phpunit_test_get_config_4', 'test c', 'mod_forum');
|
||||
$this->assertFalse($cache->get('mod_forum'));
|
||||
}
|
||||
|
||||
function test_get_max_upload_sizes() {
|
||||
// Test with very low limits so we are not affected by php upload limits.
|
||||
// Test activity limit smallest.
|
||||
$sitebytes = 102400;
|
||||
$coursebytes = 51200;
|
||||
$modulebytes = 10240;
|
||||
$result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
|
||||
|
||||
$this->assertEquals('Activity upload limit (10KB)', $result['0']);
|
||||
$this->assertEquals(2, count($result));
|
||||
|
||||
// Test course limit smallest.
|
||||
$sitebytes = 102400;
|
||||
$coursebytes = 10240;
|
||||
$modulebytes = 51200;
|
||||
$result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
|
||||
|
||||
$this->assertEquals('Course upload limit (10KB)', $result['0']);
|
||||
$this->assertEquals(2, count($result));
|
||||
|
||||
// Test site limit smallest.
|
||||
$sitebytes = 10240;
|
||||
$coursebytes = 102400;
|
||||
$modulebytes = 51200;
|
||||
$result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
|
||||
|
||||
$this->assertEquals('Site upload limit (10KB)', $result['0']);
|
||||
$this->assertEquals(2, count($result));
|
||||
|
||||
// Test site limit not set.
|
||||
$sitebytes = 0;
|
||||
$coursebytes = 102400;
|
||||
$modulebytes = 51200;
|
||||
$result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
|
||||
|
||||
$this->assertEquals('Activity upload limit (50KB)', $result['0']);
|
||||
$this->assertEquals(3, count($result));
|
||||
|
||||
$sitebytes = 0;
|
||||
$coursebytes = 51200;
|
||||
$modulebytes = 102400;
|
||||
$result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
|
||||
|
||||
$this->assertEquals('Course upload limit (50KB)', $result['0']);
|
||||
$this->assertEquals(3, count($result));
|
||||
|
||||
// Test no limits.
|
||||
$sitebytes = 0;
|
||||
$coursebytes = 0;
|
||||
$modulebytes = 0;
|
||||
$result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
|
||||
|
||||
$this->assertEquals(6, count($result));
|
||||
|
||||
// Test custom bytes in range.
|
||||
$sitebytes = 102400;
|
||||
$coursebytes = 51200;
|
||||
$modulebytes = 51200;
|
||||
$custombytes = 10240;
|
||||
$result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes, $custombytes);
|
||||
|
||||
$this->assertEquals(3, count($result));
|
||||
|
||||
// Test custom bytes in range but non-standard.
|
||||
$sitebytes = 102400;
|
||||
$coursebytes = 51200;
|
||||
$modulebytes = 51200;
|
||||
$custombytes = 25600;
|
||||
$result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes, $custombytes);
|
||||
|
||||
$this->assertEquals(4, count($result));
|
||||
|
||||
// Test custom bytes out of range.
|
||||
$sitebytes = 102400;
|
||||
$coursebytes = 51200;
|
||||
$modulebytes = 51200;
|
||||
$custombytes = 102400;
|
||||
$result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes, $custombytes);
|
||||
|
||||
$this->assertEquals(3, count($result));
|
||||
|
||||
// Test custom bytes out of range and non-standard.
|
||||
$sitebytes = 102400;
|
||||
$coursebytes = 51200;
|
||||
$modulebytes = 51200;
|
||||
$custombytes = 256000;
|
||||
$result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes, $custombytes);
|
||||
|
||||
$this->assertEquals(3, count($result));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,10 @@ information provided here is intended especially for developers.
|
|||
|
||||
=== 2.5 ===
|
||||
|
||||
* Function get_max_file_sizes now returns an option for (for example) "Course limit (500MB)" or
|
||||
"Site limit (200MB)" when appropriate with the option set to 0. This function no longer returns
|
||||
an option for 0 bytes. Existing code that was replacing the 0 option in the return
|
||||
from this function with a more sensible message, can now use the return from this function directly.
|
||||
* Functions responsible for output in course/lib.php are deprecated, the code is moved to
|
||||
appropriate renderers: print_section_add_menus()
|
||||
See functions' phpdocs in lib/deprecatedlib.php
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue