[LON-CAPA-cvs] cvs: loncom /interface loncommon.pm loncreateuser.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Tue, 29 May 2007 17:48:44 -0000


This is a MIME encoded message

--raeburn1180460924
Content-Type: text/plain

raeburn		Tue May 29 13:48:44 2007 EDT

  Modified files:              
    /loncom/interface	loncommon.pm loncreateuser.pm 
  Log:
  - user's portfolio quota is either a custom value or a default.
  - if the default, do not store this in the user's environment.
  - instead the default value is determined when needed based on the institutional status of the user (inststatus - stored in the user's environment).
  - allows changes to quota default(s) specified in domain preferences to take effect immediately for users who do not have a custom quota specified.
  
  
--raeburn1180460924
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20070529134844.txt"

Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.535 loncom/interface/loncommon.pm:1.536
--- loncom/interface/loncommon.pm:1.535	Tue May 15 16:05:13 2007
+++ loncom/interface/loncommon.pm	Tue May 29 13:48:44 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.535 2007/05/15 20:05:13 albertel Exp $
+# $Id: loncommon.pm,v 1.536 2007/05/29 17:48:44 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -5227,11 +5227,18 @@
 2. user's domain
 
 Returns:
-1. Disk quota (in Mb) assigned to student. 
+1. Disk quota (in Mb) assigned to student.
+2. (Optional) Type of setting: custom or default
+   (individually assigned or default for user's 
+   institutional status).
+3. (Optional) - User's institutional status (e.g., faculty, staff
+   or student - types as defined in localenroll::inst_usertypes 
+   for user's domain, which determines default quota for user.
+4. (Optional) - Default quota which would apply to the user.
 
 If a value has been stored in the user's environment, 
-it will return that, otherwise it returns the default
-for users in the domain.
+it will return that, otherwise it returns the maximal default
+defined for the user's instituional status(es) in the domain.
 
 =cut
 
@@ -5240,7 +5247,7 @@
 
 sub get_user_quota {
     my ($uname,$udom) = @_;
-    my $quota;
+    my ($quota,$quotatype,$settingstatus,$defquota);
     if (!defined($udom)) {
         $udom = $env{'user.domain'};
     }
@@ -5250,23 +5257,38 @@
     if (($udom eq '' || $uname eq '') ||
         ($udom eq 'public') && ($uname eq 'public')) {
         $quota = 0;
+        $quotatype = 'default';
+        $defquota = 0; 
     } else {
+        my $inststatus;
         if ($udom eq $env{'user.domain'} && $uname eq $env{'user.name'}) {
             $quota = $env{'environment.portfolioquota'};
+            $inststatus = $env{'environment.inststatus'};
         } else {
-            my %userenv = &Apache::lonnet::dump('environment',$udom,$uname);
+            my %userenv = 
+                &Apache::lonnet::get('environment',['portfolioquota',
+                                     'inststatus'],$udom,$uname);
             my ($tmp) = keys(%userenv);
             if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                 $quota = $userenv{'portfolioquota'};
+                $inststatus = $userenv{'inststatus'};
             } else {
                 undef(%userenv);
             }
         }
+        ($defquota,$settingstatus) = &default_quota($udom,$inststatus);
         if ($quota eq '') {
-            $quota = &default_quota($udom);
+            $quota = $defquota;
+            $quotatype = 'default';
+        } else {
+            $quotatype = 'custom';
         }
     }
-    return $quota;
+    if (wantarray) {
+        return ($quota,$quotatype,$settingstatus,$defquota);
+    } else {
+        return $quota;
+    }
 }
 
 ###############################################
@@ -5275,32 +5297,68 @@
 
 =item * &default_quota()
 
-Retrieves default quota assigned for storage of user portfolio files
+Retrieves default quota assigned for storage of user portfolio files,
+given an (optional) user's institutional status.
 
 Incoming parameters:
 1. domain
+2. (Optional) institutional status(es).  This is a : separated list of 
+   status types (e.g., faculty, staff, student etc.)
+   which apply to the user for whom the default is being retrieved.
+   If the institutional status string in undefined, the domain
+   default quota will be returned. 
 
 Returns:
 1. Default disk quota (in Mb) for user portfolios in the domain.
+2. (Optional) institutional type which determined the value of the
+   default quota.
 
 If a value has been stored in the domain's configuration db,
 it will return that, otherwise it returns 20 (for backwards 
 compatibility with domains which have not set up a configuration
 db file; the original statically defined portfolio quota was 20 Mb). 
 
+If the user's status includes multiple types (e.g., staff and student),
+the largest default quota which applies to the user determines the
+default quota returned.
+
 =cut
 
 ###############################################
 
 
 sub default_quota {
-    my ($udom) = @_;
-    my %defaults = &Apache::lonnet::get_dom('configuration',
-                                            ['portfolioquota'],$udom);
-    if ($defaults{'portfolioquota'} ne '') {
-        return $defaults{'portfolioquota'};
+    my ($udom,$inststatus) = @_;
+    my ($defquota,$settingstatus);
+    my %quotahash = &Apache::lonnet::get_dom('configuration',
+                                            ['quota'],$udom);
+    if (ref($quotahash{'quota'}) eq 'HASH') {
+        if ($inststatus ne '') {
+            my @statuses = split(/:/,$inststatus);
+            foreach my $item (@statuses) {
+                if ($quotahash{'quota'}{$item} ne '') {
+                    if ($defquota eq '') {
+                        $defquota = $quotahash{'quota'}{$item};
+                        $settingstatus = $item;
+                    } elsif ($quotahash{'quota'}{$item} > $defquota) {
+                        $defquota = $quotahash{'quota'}{$item};
+                        $settingstatus = $item;
+                    }
+                }
+            }
+        }
+        if ($defquota eq '') {
+            $defquota = $quotahash{'quota'}{'default'};
+            $settingstatus = 'default';
+        }
+    } else {
+        $settingstatus = 'default';
+        $defquota = 20;
+    }
+    if (wantarray) {
+        return ($defquota,$settingstatus);
     } else {
-        return '20';
+        return $defquota;
     }
 }
 
Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.148 loncom/interface/loncreateuser.pm:1.149
--- loncom/interface/loncreateuser.pm:1.148	Mon Mar 12 13:06:59 2007
+++ loncom/interface/loncreateuser.pm	Tue May 29 13:48:44 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.148 2007/03/12 17:06:59 albertel Exp $
+# $Id: loncreateuser.pm,v 1.149 2007/05/29 17:48:44 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -122,11 +122,72 @@
     my ($ccuname,$ccdomain) = @_;
     my %lt = &Apache::lonlocal::texthash(
                    'disk' => "Disk space allocated to user's portfolio files",
+                   'cuqu' => "Current quota",
+                   'cust' => "Custom quota",
+                   'defa' => "Default",
+                   'chqu' => "Change quota",
     );
-    my $output = '<h3>'.$lt{'disk'}.'</h3>'.
+    my ($currquota,$quotatype,$inststatus,$defquota) = 
+        &Apache::loncommon::get_user_quota($ccuname,$ccdomain);
+    my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
+    my ($longinsttype,$showquota,$custom_on,$custom_off,$defaultinfo);
+    if ($inststatus ne '') {
+        if ($usertypes->{$inststatus} ne '') {
+            $longinsttype = $usertypes->{$inststatus};
+        }
+    }
+    $custom_on = ' ';
+    $custom_off = ' checked="checked" ';
+    my $quota_javascript = <<"END_SCRIPT";
+<script type="text/javascript">
+function quota_changes(caller) {
+    if (caller == "custom") {
+        if (document.cu.customquota[0].checked) {
+            document.cu.portfolioquota.value = "";
+        }
+    }
+    if (caller == "quota") {
+        document.cu.customquota[1].checked = true;
+    }
+}
+</script>
+END_SCRIPT
+    if ($quotatype eq 'custom') {
+        $custom_on = $custom_off;
+        $custom_off = ' ';
+        $showquota = $currquota;
+        if ($longinsttype eq '') {
+            $defaultinfo = &mt('For this user, the default quota would be [_1]
+                            Mb.',$defquota);
+        } else {
+            $defaultinfo = &mt("For this user, the default quota would be [_1] 
+                            Mb, as determined by the user's institutional
+                           affiliation ([_2]).",$defquota,$longinsttype);
+        }
+    } else {
+        if ($longinsttype eq '') {
+            $defaultinfo = &mt('For this user, the default quota is [_1]
+                            Mb.',$defquota);
+        } else {
+            $defaultinfo = &mt("For this user, the default quota of [_1]
+                            Mb, is determined by the user's institutional
+                            affiliation ([_2]).",$defquota,$longinsttype);
+        }
+    }
+    my $output = $quota_javascript.
+                 '<h3>'.$lt{'disk'}.'</h3>'.
+                 $lt{'cuqu'}.': '.$currquota.'&nbsp;Mb.&nbsp;&nbsp;'.
+                 $defaultinfo.'<br /><span class="LC_nobreak">'.$lt{'chqu'}.
+                 ': <label>'.
+                 '<input type="radio" name="customquota" value="0" '.
+                 $custom_off.' onchange="javascript:quota_changes('."'custom'".')"
+                  />'.$lt{'defa'}.'&nbsp;('.$defquota.' Mb).</label>&nbsp;'.
+                 '&nbsp;<label><input type="radio" name="customquota" value="1" '. 
+                 $custom_on.'  onchange="javascript:quota_changes('."'custom'".')" />'.
+                 $lt{'cust'}.':</label>&nbsp;'.
                  '<input type="text" name="portfolioquota" size ="5" value="'.
-                 &Apache::loncommon::get_user_quota($ccuname,$ccdomain).
-                 '" />&nbsp;Mb';
+                 $showquota.'" onfocus="javascript:quota_changes('."'quota'".')" '.
+                 '/>&nbsp;Mb';
     return $output;
 }
 
@@ -234,7 +295,7 @@
                   );
     $loginscript  = &Apache::loncommon::authform_header(%param);
     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
-						 
+
     $ccuname =&LONCAPA::clean_username($ccuname);
     $ccdomain=&LONCAPA::clean_domain($ccdomain);
     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
@@ -1115,7 +1176,8 @@
         # Check for need to change
         my %userenv = &Apache::lonnet::get
             ('environment',['firstname','middlename','lastname','generation',
-             'portfolioquota'],$env{'form.ccdomain'},$env{'form.ccuname'});
+             'portfolioquota','inststatus'],$env{'form.ccdomain'},
+              $env{'form.ccuname'});
         my ($tmp) = keys(%userenv);
         if ($tmp =~ /^(con_lost|error)/i) { 
             %userenv = ();
@@ -1125,22 +1187,55 @@
             # Strip leading and trailing whitespace
             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g; 
         }
-        my ($quotachanged,$namechanged,$oldportfolioquota);
+        my ($quotachanged,$namechanged,$oldportfolioquota,$newportfolioquota,
+            $inststatus,$isdefault,$defquotatext);
+        my ($defquota,$settingstatus) = 
+            &Apache::loncommon::default_quota($env{'form.ccdomain'},$inststatus);
         my %changeHash;
-        if (exists($userenv{'portfolioquota'})) {
+        if ($userenv{'portfolioquota'} ne '') {
             $oldportfolioquota = $userenv{'portfolioquota'};
-            if (exists($env{'form.portfolioquota'})) {
-                if ($env{'form.portfolioquota'} ne $userenv{'portfolioquota'}) {
-                    if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
-                        # Current user has quota modification privileges
-                        $quotachanged = 1;
-                        $changeHash{'portfolioquota'} = $env{'form.portfolioquota'};
-                    }
+            if ($env{'form.customquota'} == 1) {
+                if ($env{'form.portfolioquota'} eq '') {
+                    $newportfolioquota = 0;
+                } else {
+                    $newportfolioquota = $env{'form.portfolioquota'};
+                    $newportfolioquota =~ s/[^\d\.]//g;
                 }
+                if ($newportfolioquota != $userenv{'portfolioquota'}) {
+                    $quotachanged = &quota_admin($newportfolioquota,\%changeHash);
+                }
+            } else {
+                $quotachanged = &quota_admin('',\%changeHash);
+                $newportfolioquota = $defquota;
+                $isdefault = 1; 
             }
         } else {
-            $oldportfolioquota = 
-                  &Apache::loncommon::default_quota($env{'form.ccdomain'});
+            $oldportfolioquota = $defquota;
+            if ($env{'form.customquota'} == 1) {
+                if ($env{'form.portfolioquota'} eq '') {
+                    $newportfolioquota = 0;
+                } else {
+                    $newportfolioquota = $env{'form.portfolioquota'};
+                    $newportfolioquota =~ s/[^\d\.]//g;
+                }
+                $quotachanged = &quota_admin($newportfolioquota,\%changeHash);
+            } else {
+                $newportfolioquota = $defquota;
+                $isdefault = 1;
+            }
+        }
+        if ($isdefault) {
+            if ($settingstatus eq '') {
+                $defquotatext = &mt('(default)');
+            } else {
+                my ($usertypes,$order) = 
+                    &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
+                if ($usertypes->{$settingstatus} eq '') {
+                    $defquotatext = &mt('(default)');
+                } else { 
+                    $defquotatext = &mt('(default for [_1])',$usertypes->{$settingstatus});
+                }
+            }
         }
         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}) && 
             ($env{'form.cfirstname'}  ne $userenv{'firstname'}  ||
@@ -1184,16 +1279,24 @@
     <td>$userenv{'middlename'} </td>
     <td>$userenv{'lastname'}   </td>
     <td>$userenv{'generation'} </td>
-    <td>$oldportfolioquota</td>
+    <td>$oldportfolioquota Mb</td>
 </tr>
 <tr><td>$lt{'chto'}</td>
     <td>$env{'form.cfirstname'}  </td>
     <td>$env{'form.cmiddlename'} </td>
     <td>$env{'form.clastname'}   </td>
     <td>$env{'form.cgeneration'} </td>
-    <td>$env{'form.portfolioquota'} Mb</td></tr>
+    <td>$newportfolioquota Mb $defquotatext </td></tr>
 </table>
 END
+                if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
+                    ($env{'form.ccuname'} eq $env{'user.name'})) {
+                    my %newenvhash;
+                    foreach my $key (keys(%changeHash)) {
+                        $newenvhash{'environment.'.$key} = $changeHash{$key};
+                    }
+                    &Apache::lonnet::appenv(%newenvhash);
+                }
             } else { # error occurred
                 $r->print("<h2>".&mt('Unable to successfully change environment for')." ".
                       $env{'form.ccuname'}." ".&mt('in domain')." ".
@@ -1220,8 +1323,10 @@
 <h4>$lt{'gen'}: $userenv{'generation'}</h4>
 END
             if ($putresult eq 'ok') {
-                if ($oldportfolioquota ne $env{'form.portfolioquota'}) {
-                    $r->print('<h4>'.$lt{'disk'}.': '.$env{'form.portfolioquota'}.' Mb</h4>');
+                if ($oldportfolioquota != $newportfolioquota) {
+                    $r->print('<h4>'.$lt{'disk'}.': '.$newportfolioquota.' Mb '. 
+                              $defquotatext.'</h4>');
+                    &Apache::lonnet::appenv('environment.portfolioquota' => $changeHash{'portfolioquota'});
                 }
             }
         }
@@ -1446,6 +1551,17 @@
     $r->print(&Apache::loncommon::end_page());
 }
 
+sub quota_admin {
+    my ($setquota,$changeHash) = @_;
+    my $quotachanged;
+    if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
+        # Current user has quota modification privileges
+        $quotachanged = 1;
+        $changeHash->{'portfolioquota'} = $setquota;
+    }
+    return $quotachanged;
+}
+
 sub build_roles {
     my ($sectionstr,$sections,$role) = @_;
     my $num_sections = 0;

--raeburn1180460924--