This implementation of course quotas works only for Moodle 1.8.5+ and 1.9.3+. While it can work for other versions, it was tested for those. The goal of this quota system is to prevent instructors from going over the quota, however the students are able to upload files if the course is over the quota as their (homework) submissions are important.
The quota works on two levels; site wide and course level. The site wide quota applies to all courses that do not have a course level quota and course level quotas apply to a specific course while overriding site wide quota. The quota enforcement only works on total size and not number of files, however it can be expanded to handle that case.
To begin installation, we must first manually create a table in the Moodle database. We run the following SQL query to do that assuming the Moodle database prefix is ‘mdl_’ -
create table mdl_sitequota(id int not null auto_increment, primary key(id), courseid int not null, quota int not null, unit varchar(1));
The ‘id’ primary key is required by Moodle’s update_record() function which is used for portability. The fields ‘courseid’ (Moodle course ID) and ‘quota’ (numeric limit) are integers, and a one character field ‘unit’ for the storage unit. To avoid issues with large integers as file sizes are typically reported in bytes by various utilities and portability as well, the unit field was included which is denoted by a single character of ‘B’ for bytes, ‘K’ for kilobytes, ‘M’ for megabytes, and ‘G’ for gigabytes.
We should set a default, site wide quota of 100 MB by running the following SQL query -
insert into mdl_sitequota (courseid, quota, unit) values (0, 100, 'M');
A courseid of 0 denotes the sitewide default; any sites that do not have an entry in this table are limited by this value.
Next are the files needed to make this work. Go into the Moodle directory, then admin/report directories. Extract the following files in there – ZIP / TAR.GZ
The files interface must be changed, which is located in the Moodle directory /files/index.php. In ‘index.php’, look for the following lines of code -
case "upload":
html_header($course, $wdir);
require_once($CFG->dirroot.'/lib/uploadlib.php');
Below that, add
require_once($CFG->libdir.'/umnlib.php');
$tablename = "sitequota";
$coursequota = getquotadetails($COURSE->id);
if ($coursequota == NULL) {
$coursequota = getquotadetails(0);
}
$coursesize = get_directory_size("{$CFG->dataroot}/$COURSE->id"); // in bytes - may overflow for large directories
// this switch is for the size of the course directory in a the same unit as the quota unit is defined in
switch ($coursequota->unit) {
case "B":
break;
case "K":
$coursesize /= 1024;
break;
case "M":
$coursesize /= 1048576;
break;
case "G":
$coursesize /= 1073741824;
break;
case "T":
$coursesize /= 1099511627776;
break;
case "P":
$coursesize /= 1125899906842624;
break;
case "E":
$coursesize /= 1152921504606846976;
break;
}
if ($coursesize > (int) $coursequota->quota && isteacher()) {
error("Unable to upload files: Total site size is $coursesize $coursequota->unit and quota is $coursequota->quota $coursequota->unit", "{$CFG->wwwroot}/files/index.php?id=$COURSE->id");
}
print_simple_box_start();
echo "Using $coursesize " . readablestorageunit($coursequota->unit) . " out of $coursequota->quota " . readablestorageunit($coursequota->unit) . " allowed";
print_simple_box_end();
And we’re almost done. We add a library file which stores some of the functions used by the site quotas and the language file as well. For the library, go into Moodle’s /lib directory and create the file ‘umnlib.php’. In it, insert
function readablestorageunit($unitchar=NULL) {
if ($unitchar == NULL) {
return "";
}
switch ($unitchar) {
case "B":
return "bytes";
break;
case "K":
return "KB (Kilobytes)";
break;
case "M":
return "MB (Megabytes)";
break;
case "G":
return "GB (Gigabytes)";
break;
case "T":
return "TB (Terabytes)";
break;
case "P":
return "PB (Petabytes)";
break;
case "E":
return "EB (Exabytes)";
break;
}
return "";
}
// if no argument given, assume $courseid=-1 as $courseid = 0 means sitewide default
function getquotadetails($courseid=-1) {
global $CFG, $tablename;
$courseid = (int) $courseid;
if ($courseid == -1 || $courseid < 0) {
return NULL;
}
$details = get_record($tablename, 'courseid', $courseid);
if (empty($details)) {
return NULL;
}
else {
return $details;
}
}
These first function prints the unit of storage in a easily readable form while the second is a convenience function. Our last step of installing is adding the language file. Go into Moodle's /lang/en_utf8/ directory and create the file 'umn.php'. In it, insert
$string['sitequota'] = 'Site Quota';
$string['sitequotadefaultsettings'] = 'Default Settings';
$string['sitequotaquota'] = 'Quota: ';
$string['sitequotaunit'] = 'Storage Unit: ';
$string['sitequotacourseid'] = 'Course ID: ';
$string['nodefaultquota'] = 'Default quota does not exist and manual addition of 100 GB quota failed - check that site quota table exists';
$string['newdefaultunitfail'] = 'New default unit of storage was not added - database error';
$string['updatedefaultunitfail'] = 'Unable to update default unit of storage - database error';
$string['invalidstorageunit'] = 'The unit of storage was not valid';
$string['defaultquotazero'] = 'Default quota size cannot be less than 0';
$string['newdefaultquotafail'] = 'New default quota was not added - database error';
$string['updatedefaultquotafail'] = 'Unable to update default quota - database error';
$string['sitequotanocourseid'] = 'Quota update failed - must include course ID';
$string['newunitfail'] = 'New unit of storage for course was not added - database error';
$string['updateunitfail'] = 'Unit of storage was not updated for course - database error';
$string['newquotafail'] = 'New quota for course was not added - database error';
$string['updatequotafail'] = 'Quota was not updated for course - database error';
$string['backupdatasize'] = 'Backupdata Size';
$string['totalsitesize'] = 'Total site size';
$string['sitequotacourseid'] = 'Moodle Course ID';
$string['noinstorsdne'] = 'No instructors or site does not exist';
$string['sitequotainstructors'] = 'Instructor';
?>
These can be changed from the languages interface for localization. Finally, we are able to use the quotas interface. Go to http://www.yourmoodlesite.com/admin After logging in as an administrator, go to Reports and Site Quotas. If a link appears as "[[sitequota]]" then add the string below into Moodle's lang/en_utf8/admin.php
$string['sitequota'] = 'Site Quota';
If there are no errors thrown, you are able to use site quotas. Note that when adding a new quota for a course, if either the quota or the storage unit are left out, they are taken from the site level quota. For updating a course level quota, if one field is left out, it will be unchanged.
Please note that this has not been thoroughly tested so I would like feedback about bugs.
Sorry for the formatting, I got tired of wrestling Wordpress with it. Copy the language file that goes into /lang/en_utf8/ from here, the /files/index.php modification from here, and the umnlib.php that goes into /lib from here.
Hi
The quota subsystem is very important to me do you plan to submit to moodle.org ? try to make it part from the core code?
Comment by Ângelo — June 8, 2009 @ 7:52 am
Hi I test in moodle 1.9.4 when clicking in admin/report/sitequota gives me the message :
Catchable fatal error: Object of class admin_root could not be converted to string in C:\wamp\www\moodle194\admin\pagelib.php on line 149
Comment by Ângelo — June 8, 2009 @ 8:26 am
Hi
I´m trying to implement just for the blog system do you have any tip ?
Comment by Ângelo — June 8, 2009 @ 11:17 am
Yes, look for anywhere in the code that calls for file attachments.
Comment by Elvedin — June 23, 2009 @ 10:19 am