Skip to content

Commit

Permalink
Merge branch 'MDL-37122-master' of git://github.com/damyon/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Feb 11, 2013
2 parents acd561c + 6b21931 commit 2ae00d0
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@
$string['coursesummary'] = 'Course summary';
$string['coursesummary_help'] = 'The course summary is displayed in the list of courses. A course search searches course summary text in addition to course names.';
$string['courseupdates'] = 'Course updates';
$string['courseuploadlimit'] = 'Course upload limit';
$string['create'] = 'Create';
$string['createaccount'] = 'Create my new account';
$string['createcategory'] = 'Create category';
Expand Down Expand Up @@ -1715,6 +1714,7 @@
$string['uploadfilelog'] = 'Upload log for file {$a}';
$string['uploadformlimit'] = 'Uploaded file {$a} exceeded the maximum size limit set by the form';
$string['uploadlabel'] = 'Title:';
$string['uploadlimitwithsize'] = '{$a->contextname} upload limit ({$a->displaysize})';
$string['uploadnewfile'] = 'Upload new file';
$string['uploadnofilefound'] = 'No file was found - are you sure you selected one to upload?';
$string['uploadnotallowed'] = 'Uploads are not allowed';
Expand Down
30 changes: 26 additions & 4 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down
91 changes: 91 additions & 0 deletions lib/tests/moodlelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2245,4 +2245,95 @@ public function test_get_config() {
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));
}
}
4 changes: 4 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions mod/assign/submission/file/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ public function get_settings(MoodleQuickForm $mform) {
$COURSE->maxbytes,
get_config('assignsubmission_file', 'maxbytes'));

// Remove the option for 0 bytes.
unset($choices[0]);

if ($COURSE->maxbytes == 0) {
$choices = array(0=>get_string('siteuploadlimit', 'assignsubmission_file')) + $choices;
} else {
$choices = array(0=>get_string('courseuploadlimit') . ' (' . display_size($COURSE->maxbytes) . ')') + $choices;
}
$settings[] = array('type' => 'select',
'name' => 'maxsubmissionsizebytes',
'description' => get_string('maximumsubmissionsize', 'assignsubmission_file'),
Expand Down
1 change: 0 additions & 1 deletion mod/assignment/type/upload/assignment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,6 @@ function setup_elements(&$mform) {
$ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));

$choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
$choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
$mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
$mform->setDefault('maxbytes', $CFG->assignment_maxbytes);

Expand Down
1 change: 0 additions & 1 deletion mod/assignment/type/uploadsingle/assignment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ function setup_elements(&$mform) {
$mform->setDefault('emailteachers', 0);

$choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
$choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
$mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
$mform->setDefault('maxbytes', $CFG->assignment_maxbytes);

Expand Down
1 change: 0 additions & 1 deletion mod/forum/mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ function definition() {

$choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
$choices[1] = get_string('uploadnotallowed');
$choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
$mform->addElement('select', 'maxbytes', get_string('maxattachmentsize', 'forum'), $choices);
$mform->addHelpButton('maxbytes', 'maxattachmentsize', 'forum');
$mform->setDefault('maxbytes', $CFG->forum_maxbytes);
Expand Down
1 change: 0 additions & 1 deletion mod/workshop/mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ public function definition() {
$mform->setDefault('nattachments', 1);

$options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes);
$options[0] = get_string('courseuploadlimit') . ' ('.display_size($this->course->maxbytes).')';
$mform->addElement('select', 'maxbytes', get_string('maxbytes', 'workshop'), $options);
$mform->setDefault('maxbytes', $workshopconfig->maxbytes);

Expand Down
1 change: 0 additions & 1 deletion mod/workshop/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@

if (isset($CFG->maxbytes)) {
$options = get_max_upload_sizes($CFG->maxbytes);
$options[0] = get_string('courseuploadlimit');
$settings->add(new admin_setting_configselect('workshop/maxbytes', get_string('maxbytes', 'workshop'),
get_string('configmaxbytes', 'workshop'), 0, $options));
}
Expand Down

0 comments on commit 2ae00d0

Please sign in to comment.