Commit e16e85b1 authored by Hugh Davenport's avatar Hugh Davenport
Browse files

Add group quotas



Change-Id: Id4880764ab395ea56395a57f52258968097bb02b
Signed-off-by: default avatarHugh Davenport <hugh@catalyst.net.nz>
parent dcf1ff80
......@@ -34,13 +34,46 @@ require_once('pieforms/pieform.php');
$group = get_record_select('group', 'id = ? AND deleted = 0', array(param_integer('id')));
define('TITLE', get_string('groupadminsforgroup', 'admin', $group->name));
define('TITLE', get_string('administergroups', 'admin'));
$quotasform = pieform(array(
'name' => 'groupquotasform',
'elements' => array(
'groupid' => array(
'type' => 'hidden',
'value' => $group->id,
),
'quota' => array(
'type' => 'bytes',
'title' => get_string('filequota', 'admin'),
'description' => get_string('groupfilequotadescription', 'admin'),
'defaultvalue' => $group->quota,
),
'submit' => array(
'type' => 'submit',
'value' => get_string('save'),
),
),
));
function groupquotasform_submit(Pieform $form, $values) {
global $SESSION;
$group = new StdClass;
$group->id = $values['groupid'];
$group->quota = $values['quota'];
update_record('group', $group);
$SESSION->add_ok_msg(get_string('groupquotaupdated', 'admin'));
redirect(get_config('wwwroot').'admin/groups/groups.php');
}
$admins = get_column_sql(
"SELECT gm.member FROM {group_member} gm WHERE gm.role = 'admin' AND gm.group = ?", array($group->id)
);
$form = pieform(array(
$groupadminsform = pieform(array(
'name' => 'groupadminsform',
'renderer' => 'table',
'plugintype' => 'core',
......@@ -106,5 +139,7 @@ function groupadminsform_submit(Pieform $form, $values) {
$smarty = smarty();
$smarty->assign('PAGEHEADING', TITLE);
$smarty->assign('managegroupform', $form);
$smarty->assign('quotasform', $quotasform);
$smarty->assign('groupname', $group->name);
$smarty->assign('managegroupform', $groupadminsform);
$smarty->display('admin/groups/manage.tpl');
......@@ -271,5 +271,10 @@ function xmldb_artefact_file_upgrade($oldversion=0) {
}
}
if ($oldversion < 2011052500) {
// Set default quota to 50MB
set_config_plugin('artefact', 'file', 'defaultgroupquota', 52428800);
}
return $status;
}
......@@ -689,13 +689,25 @@ function pieform_element_filebrowser_upload(Pieform $form, $element, $data) {
catch (QuotaExceededException $e) {
prepare_upload_failed_message($result, $e, $parentfoldername, $originalname);
// update the file listing
$result['quota'] = $USER->get('quota');
$result['quotaused'] = $USER->get('quotaused');
if (defined('GROUP')) {
$group = group_current_group();
$result['quota'] = $group->quota;
$result['quotaused'] = $group->quotaused;
}
else {
$result['quota'] = $USER->get('quota');
$result['quotaused'] = $USER->get('quotaused');
}
$result['newlist'] = pieform_element_filebrowser_build_filelist($form, $element, $parentfolder);
return $result;
}
catch (UploadException $e) {
prepare_upload_failed_message($result, $e, $parentfoldername, $originalname);
if (defined('GROUP')) {
$group = group_current_group();
$result['quota'] = $group->quota;
$result['quotaused'] = $group->quotaused;
}
return $result;
}
......@@ -737,8 +749,15 @@ function pieform_element_filebrowser_upload(Pieform $form, $element, $data) {
$result['highlight'] = $newid;
$result['uploaded'] = true;
$result['newlist'] = pieform_element_filebrowser_build_filelist($form, $element, $parentfolder, $newid);
$result['quota'] = $USER->get('quota');
$result['quotaused'] = $USER->get('quotaused');
if (defined('GROUP')) {
$group = group_current_group();
$result['quota'] = $group->quota;
$result['quotaused'] = $group->quotaused;
}
else {
$result['quota'] = $USER->get('quota');
$result['quotaused'] = $USER->get('quotaused');
}
$result['maxuploadsize'] = display_size(get_max_upload_size(!$institution && !$group));
return $result;
}
......@@ -905,18 +924,27 @@ function pieform_element_filebrowser_delete(Pieform $form, $element, $artefact)
}
$parentfolder = $artefact->get('parent');
$artefact->delete();
return array(
$result = array(
'error' => false,
'deleted' => true,
'message' => get_string('filethingdeleted', 'artefact.file',
get_string($artefact->get('artefacttype'), 'artefact.file') . ' ' . $artefact->get('title')),
'quotaused' => $USER->get('quotaused'),
'quota' => $USER->get('quota'),
'maxuploadsize' => display_size(get_max_upload_size(!$institution && !$group)),
'newlist' => pieform_element_filebrowser_build_filelist($form, $element, $parentfolder),
);
}
if (defined('GROUP')) {
$group = group_current_group();
$result['quota'] = $group->quota;
$result['quotaused'] = $group->quotaused;
}
else {
$result['quota'] = $USER->get('quota');
$result['quotaused'] = $USER->get('quotaused');
}
return $result;
}
function pieform_element_filebrowser_move(Pieform $form, $element, $data) {
global $USER;
......
......@@ -49,7 +49,20 @@ $pagebase = get_config('wwwroot') . 'artefact/file/groupfiles.php?group=' . $gro
$form = pieform(ArtefactTypeFileBase::files_form($pagebase, $group->id));
$js = ArtefactTypeFileBase::files_js();
$smarty = smarty();
$smarty = smarty(
array(),
array(),
array(),
array(
'sideblocks' => array(
array(
'name' => 'groupquota',
'weight' => -10,
'data' => array(),
),
),
)
);
$smarty->assign('heading', $group->name);
$smarty->assign('form', $form);
$smarty->assign('INLINEJAVASCRIPT', $js);
......
......@@ -59,6 +59,8 @@ $string['maxquotatoolow'] = 'The maximum quota cannot be lower than the default
$string['maxquotaexceeded'] = 'You specified a quota above the maximum available setting for this site (%s). Try specifying a lower value or contact the site administrator to have them increase the maximum quota.';
$string['maxquotaexceededform'] = 'Please specify a file quota of less than %s.';
$string['maxquotadescription'] = 'You can set the maximum quota that an administrator can give to a user. Existing user quotas will not be affected.';
$string['defaultgroupquota'] = 'Default Group Quota';
$string['defaultgroupquotadescription'] = 'You can set the amount of disk space that new groups can use in their file area. Existing group quotas will not be changed.';
$string['deletingfailed'] = 'Deleting failed: file or folder does not exist any more';
$string['deletefile?'] = 'Are you sure you want to delete this file?';
$string['deletefolder?'] = 'Are you sure you want to delete this folder?';
......
<h3>Group Quota</h3>
<p>This group has been allocated a set amount of storage that group members may use to upload files to the system. If you are getting close to your quota limit, contact your site administrator about having your limit increased.</p>
......@@ -89,7 +89,9 @@ class PluginArtefactFile extends PluginArtefact {
public static function postinst($prevversion) {
if ($prevversion == 0) {
// Set default quotas to 50MB
set_config_plugin('artefact', 'file', 'defaultquota', 52428800);
set_config_plugin('artefact', 'file', 'defaultgroupquota', 52428800);
}
set_config_plugin('artefact', 'file', 'commentsallowedimage', 1);
self::resync_filetype_list();
......@@ -896,6 +898,12 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
throw new QuotaExceededException(get_string('uploadexceedsquota', 'artefact.file'));
}
}
if (!empty($data->group)) {
require_once('group.php');
if (!group_quota_allowed($data->group, $size)) {
throw new QuotaExceededException(get_string('uploadexceedsquota', 'artefact.file'));
}
}
$data->size = $size;
if ($um->file['type'] == 'application/octet-stream') {
......@@ -921,6 +929,9 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
$owner->quota_add($size);
$owner->commit();
}
else if (!empty($data->group)) {
group_quota_add($data->group, $size);
}
return $id;
}
......@@ -1014,6 +1025,10 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
$USER->quota_remove($size);
$USER->commit();
}
if (!empty($this->group)) {
require_once('group.php');
group_quota_remove($this->group, $size);
}
}
delete_records('artefact_attachment', 'attachment', $this->id);
......@@ -1153,6 +1168,26 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
'collapsible' => true
);
$defaultgroupquota = get_config_plugin('artefact', 'file', 'defaultgroupquota');
if (empty($defaultgroupquota)) {
$defaultgroupquota = 1024 * 1024 * 10;
}
$elements['groupquotafieldset'] = array(
'type' => 'fieldset',
'legend' => get_string('defaultgroupquota', 'artefact.file'),
'elements' => array(
'defaultgroupquotadescription' => array(
'value' => '<tr><td colspan="2">' . get_string('defaultgroupquotadescription', 'artefact.file') . '</td></tr>'
),
'defaultgroupquota' => array(
'title' => get_string('defaultgroupquota', 'artefact.file'),
'type' => 'bytes',
'defaultvalue' => $defaultgroupquota,
)
),
'collapsible' => true
);
// Require user agreement before uploading files
// Rework this when/if we provide translatable agreements
$uploadagreement = get_config_plugin('artefact', 'file', 'uploadagreement');
......@@ -1265,6 +1300,7 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
public static function save_config_options($values) {
global $USER;
set_config_plugin('artefact', 'file', 'defaultquota', $values['defaultquota']);
set_config_plugin('artefact', 'file', 'defaultgroupquota', $values['defaultgroupquota']);
set_config_plugin('artefact', 'file', 'institutionaloverride', $values['institutionaloverride']);
set_config_plugin('artefact', 'file', 'maxquota', $values['maxquota']);
set_config_plugin('artefact', 'file', 'maxquotaenabled', $values['maxquotaenabled']);
......
......@@ -28,5 +28,5 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2010012702;
$config->version = 2011052500;
$config->release = '1.2.0';
......@@ -141,10 +141,13 @@ $string['groupoptionsset'] = 'Group options have been updated.';
$string['groupcategorydeleted'] = 'Category deleted';
$string['confirmdeletecategory'] = 'Do you really want to delete this category?';
$string['groupcategoriespagedescription'] = 'The categories listed here can be assigned to groups during group creation, and used to filter groups during searches.';
$string['groupquotas'] = "Group Quota for '%s'";
$string['groupfilequotadescription'] = 'Total storage available in the group\'s files area.';
$string['groupadminsforgroup'] = "Group administrators for '%s'";
$string['potentialadmins'] = 'Potential administrators';
$string['currentadmins'] = 'Current administrators';
$string['groupadminsupdated'] = 'Group admins have been updated';
$string['groupquotaupdated'] = 'Group quota has been updated';
// Register your Mahara
$string['Field'] = 'Field';
......@@ -736,4 +739,5 @@ $string['grouptype'] = 'Group Type';
$string['groupvisible'] = 'Visibility';
$string['groupmanage'] = 'Manage';
$string['groupdelete'] = 'Delete';
$string['managegroupquotadescription'] = 'Use the form below to change the group file quota for this group.';
$string['managegroupdescription'] = 'Use the form below to promote and demote admins for this group. If you remove a group administrator they will remain a group member.';
......@@ -125,6 +125,7 @@ $string['selfsearch'] = 'Search My Portfolio';
// Quota strings
$string['quota'] = 'Quota';
$string['quotausage'] = 'You have used <span id="quota_used">%s</span> of your <span id="quota_total">%s</span> quota.';
$string['groupquota'] = 'Group Quota';
$string['updatefailed'] = 'Update failed';
......
......@@ -297,6 +297,8 @@
<FIELD NAME="deleted" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" />
<FIELD NAME="category" TYPE="int" LENGTH="10" NOTNULL="false" />
<FIELD NAME="viewnotify" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" />
<FIELD NAME="quota" TYPE="int" LENGTH="10" NOTNULL="false"/>
<FIELD NAME="quotaused" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
......
......@@ -2343,5 +2343,15 @@ function xmldb_core_upgrade($oldversion=0) {
add_field($table, $field);
}
if ($oldversion < 2011053101) {
$table = new XMLDBTable('group');
$field = new XMLDBField('quota');
$field->setAttributes(XMLDB_TYPE_INTEGER, 10);
add_field($table, $field);
$field = new XMLDBField('quotaused');
$field->setAttributes(XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, null, 0);
add_field($table, $field);
}
return $status;
}
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2011 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage dwoo
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2011 Catalyst IT Ltd http://catalyst.net.nz
*
*/
function Dwoo_Plugin_loadgroupquota(Dwoo $dwoo) {
$group = group_current_group();
$quota = $group->quota;
$quotaused = $group->quotaused;
if ($quota >= 1048576) {
$quota_message = get_string('quotausage', 'mahara', sprintf('%0.1fMB', $group->quotaused / 1048576), sprintf('%0.1fMB', $quota / 1048567));
}
else if ($quota >= 1024) {
$quota_message = get_string('quotausage', 'mahara', sprintf('%0.1fKB', $group->quotaused / 1024), sprintf('%0.1fKB', $quota / 1024));
}
else {
$quota_message = get_string('quotausage', 'mahara', sprintf('%d bytes', $group->quotaused), sprintf('%d bytes', $quota));
}
$dwoo->assignInScope($quota_message, 'GROUPQUOTA_MESSAGE');
if ($quota == 0) {
$dwoo->assignInScope(100, 'GROUPQUOTA_PERCENTAGE');
}
else {
$dwoo->assignInScope(round($quotaused / $quota * 100), 'GROUPQUOTA_PERCENTAGE');
}
}
......@@ -255,6 +255,8 @@ function group_create($data) {
$data['public'] = (isset($data['public'])) ? intval($data['public']) : 0;
$data['usersautoadded'] = (isset($data['usersautoadded'])) ? intval($data['usersautoadded']) : 0;
$data['quota'] = get_config_plugin('artefact', 'file', 'defaultgroupquota');
db_begin();
$id = insert_record(
......@@ -269,6 +271,7 @@ function group_create($data) {
'mtime' => $data['ctime'],
'public' => $data['public'],
'usersautoadded' => $data['usersautoadded'],
'quota' => $data['quota'],
),
'id',
true
......@@ -1510,3 +1513,42 @@ function get_forum_list($groupid, $userid = 0) {
return $forums;
}
function group_quota_allowed($groupid, $bytes) {
if (!is_numeric($bytes) || $bytes < 0) {
throw new InvalidArgumentException('parameter must be a positive integer to add to the quota');
}
if (!$group = get_record('group', 'id', $groupid, 'deleted', 0)) {
throw new GroupNotFoundException(get_string('groupnotfound', 'group', $groupid));
}
if ($group->quotaused + $bytes > $group->quota) {
return false;
}
return true;
}
function group_quota_add($groupid, $bytes) {
if (!group_quota_allowed($groupid, $bytes)) {
throw new QuotaExceededException('Adding ' . $bytes . ' bytes would exceed the group\'s quota');
}
$newquota = $group->quotaused + $bytes;
$group = new StdClass;
$group->id = $groupid;
$group->quotaused = $newquota;
update_record('group', $group);
}
function group_quota_remove($groupid, $bytes) {
if (!is_numeric($bytes) || $bytes < 0) {
throw new InvalidArgumentException('parameter must be a positive integer to add to the quota');
}
if (!$group = get_record('group', 'id', $groupid, 'deleted', 0)) {
throw new GroupNotFoundException(get_string('groupnotfound', 'group', $groupid));
}
$newquota = max(0, $group->quotaused - $bytes);
$group = new StdClass;
$group->id = $groupid;
$group->quotaused = $newquota;
update_record('group', $group);
}
......@@ -28,7 +28,7 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2011053100;
$config->version = 2011053101;
$config->release = '1.5.0dev';
$config->minupgradefrom = 2008040200;
$config->minupgraderelease = '1.0.0 (release tag 1.0.0_RELEASE)';
......
{include file="header.tpl"}
<h3>{str tag=groupquotas section=admin args=$groupname}</h3>
<p>{str tag=managegroupquotadescription section=admin}</p>
<div>
{$quotasform|safe}
</div>
<h3>{str tag=groupadminsforgroup section=admin args=$groupname}</h3>
<p>{str tag=managegroupdescription section=admin}</p>
<div class="userlistform">
{$managegroupform|safe}
......
{loadgroupquota}
<div class="sidebar-header"><h3>{str tag="groupquota"}
{contextualhelp plugintype='artefact' pluginname='file' section='groupquota_message'}</h3></div>
<div class="sidebar-content">
<p id="quota_message">
{$GROUPQUOTA_MESSAGE|safe}
</p>
<div id="quotawrap">
{if $GROUPQUOTA_PERCENTAGE < 100}
<div id="quota_fill" style="width: {$GROUPQUOTA_PERCENTAGE*2}px;">&nbsp;</div>
<p id="quota_bar">
<span id="quota_percentage">{$GROUPQUOTA_PERCENTAGE}%</span>
</p>
{else}
<div id="quota_fill" style="display: none; width: 200px;">&nbsp;</div>
<p id="quota_bar_100">
<span id="quota_percentage">{$GROUPQUOTA_PERCENTAGE}%</span>
</p>
{/if}
</div>
</div>
......@@ -12,7 +12,7 @@
<span id="quota_percentage">{$QUOTA_PERCENTAGE}%</span>
</p>
{else}
<div id="quota_fill" style="display: none; width: {$QUOTA_PERCENTAGE*2}px;">&nbsp;</div>
<div id="quota_fill" style="display: none; width: 200px;">&nbsp;</div>
<p id="quota_bar_100">
<span id="quota_percentage">{$QUOTA_PERCENTAGE}%</span>
</p>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment