[LON-CAPA-cvs] cvs: loncom /homework lonhomework.pm /lonnet/perl lonnet.pm

raeburn raeburn at source.lon-capa.org
Wed Apr 15 00:11:20 EDT 2015


raeburn		Wed Apr 15 04:11:20 2015 EDT

  Modified files:              
    /loncom/lonnet/perl	lonnet.pm 
    /loncom/homework	lonhomework.pm 
  Log:
  - Bug 6518.
    - Speed-up display of Course Contents where content blocking is in
      operation, by using a cache with short lifespan (5s) for block data.
    - Additional args added to symbread() so appropriate symb will be
      retrieved when content blocking is active.
    - Documentation updated for &symbread() and &allowed()
    - &check_docs_block() routine eliminated (no longer needed).
    - Determination of resources and/or maps with content blocking in active
      blocking events moved from &has_comm_blocking() to 
      &get_commblock_resources() so it is available for rapid look-up.
  
  
-------------- next part --------------
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.1281 loncom/lonnet/perl/lonnet.pm:1.1282
--- loncom/lonnet/perl/lonnet.pm:1.1281	Mon Apr 13 16:30:32 2015
+++ loncom/lonnet/perl/lonnet.pm	Wed Apr 15 04:11:17 2015
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.1281 2015/04/13 16:30:32 raeburn Exp $
+# $Id: lonnet.pm,v 1.1282 2015/04/15 04:11:17 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -4444,7 +4444,7 @@
 # The cached times for this user
 my %cachedtimes=();
 # When this was last done
-my $cachedtime=();
+my $cachedtime='';
 
 sub load_all_first_access {
     my ($uname,$udom)=@_;
@@ -4506,6 +4506,7 @@
     return 'already_set';
 }
 }
+
 # --------------------------------------------- Set Expire Date for Spreadsheet
 
 sub expirespread {
@@ -7199,6 +7200,32 @@
     return '';
 }
 
+# ----------------------------------------------------------- Content Blocking
+
+{
+# Caches for faster Course Contents display where content blocking
+# is in operation (i.e., interval param set) for timed quiz.
+#
+# User for whom data are being temporarily cached.
+my $cacheduser='';
+# Cached blockers for this user (a hash of blocking items). 
+my %cachedblockers=();
+# When the data were last cached.
+my $cachedlast='';
+
+sub load_all_blockers {
+    my ($uname,$udom,$blocks)=@_;
+    if (($uname ne '') && ($udom ne '')) { 
+        if (($cacheduser eq $uname.':'.$udom) &&
+            (abs($cachedlast-time)<5)) {
+            return;
+        }
+    }
+    $cachedlast=time;
+    $cacheduser=$uname.':'.$udom;
+    %cachedblockers = &get_commblock_resources($blocks);
+}
+
 sub get_comm_blocks {
     my ($cdom,$cnum) = @_;
     if ($cdom eq '' || $cnum eq '') {
@@ -7219,27 +7246,21 @@
     return %commblocks;
 }
 
-sub has_comm_blocking {
-    my ($priv,$symb,$uri,$blocks) = @_;
-    return unless ($env{'request.course.id'});
-    return unless ($priv eq 'bre');
-    return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
+sub get_commblock_resources {
+    my ($blocks) = @_;
+    my %blockers = ();
+    return %blockers unless ($env{'request.course.id'});
+    return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
     my %commblocks;
     if (ref($blocks) eq 'HASH') {
         %commblocks = %{$blocks};
     } else {
         %commblocks = &get_comm_blocks();
     }
-    return unless (keys(%commblocks) > 0);
-    if (!$symb) { $symb=&symbread($uri,1); }
-    my ($map,$resid,undef)=&decode_symb($symb);
-    my %tocheck = (
-                    maps      => $map,
-                    resources => $symb,
-                  );
-    my @blockers;
-    my $now = time;
+    return %blockers unless (keys(%commblocks) > 0); 
     my $navmap = Apache::lonnavmaps::navmap->new();
+    return %blockers unless (ref($navmap));
+    my $now = time;
     foreach my $block (keys(%commblocks)) {
         if ($block =~ /^(\d+)____(\d+)$/) {
             my ($start,$end) = ($1,$2);
@@ -7247,17 +7268,13 @@
                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
-                            if ($commblocks{$block}{'blocks'}{'docs'}{'maps'}{$map}) {
-                                unless (grep(/^\Q$block\E$/, at blockers)) {
-                                    push(@blockers,$block);
-                                }
+                            if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
+                                $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
                             }
                         }
                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
-                            if ($commblocks{$block}{'blocks'}{'docs'}{'resources'}{$symb}) {
-                                unless (grep(/^\Q$block\E$/, at blockers)) {  
-                                    push(@blockers,$block);
-                                }
+                            if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
+                                $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
                             }
                         }
                     }
@@ -7268,61 +7285,67 @@
             my @to_test;
             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
-                    my $check_interval;
-                    if (&check_docs_block($commblocks{$block}{'blocks'}{'docs'},\%tocheck)) {
-                        my @interval;
-                        my $type = 'map';
-                        if ($item eq 'course') {
-                            $type = 'course';
-                            @interval=&EXT("resource.0.interval");
+                    my @interval;
+                    my $type = 'map';
+                    if ($item eq 'course') {
+                        $type = 'course';
+                        @interval=&EXT("resource.0.interval");
+                    } else {
+                        if ($item =~ /___\d+___/) {
+                            $type = 'resource';
+                            @interval=&EXT("resource.0.interval",$item);
+                            if (ref($navmap)) {                        
+                                my $res = $navmap->getBySymb($item); 
+                                push(@to_test,$res);
+                            }
                         } else {
-                            if ($item =~ /___\d+___/) {
-                                $type = 'resource';
-                                @interval=&EXT("resource.0.interval",$item);
-                                if (ref($navmap)) {                        
-                                    my $res = $navmap->getBySymb($item); 
-                                    push(@to_test,$res);
-                                }
-                            } else {
-                                my $mapsymb = &symbread($item,1);
-                                if ($mapsymb) {
-                                    if (ref($navmap)) {
-                                        my $mapres = $navmap->getBySymb($mapsymb);
-                                        @to_test = $mapres->retrieveResources($mapres,undef,0,0,0,1);
-                                        foreach my $res (@to_test) {
-                                            my $symb = $res->symb();
-                                            next if ($symb eq $mapsymb);
-                                            if ($symb ne '') {
-                                                @interval=&EXT("resource.0.interval",$symb);
-                                                if ($interval[1] eq 'map') {
-                                                    last;
-                                                }
+                            my $mapsymb = &symbread($item,1);
+                            if ($mapsymb) {
+                                if (ref($navmap)) {
+                                    my $mapres = $navmap->getBySymb($mapsymb);
+                                    @to_test = $mapres->retrieveResources($mapres,undef,0,0,0,1);
+                                    foreach my $res (@to_test) {
+                                        my $symb = $res->symb();
+                                        next if ($symb eq $mapsymb);
+                                        if ($symb ne '') {
+                                            @interval=&EXT("resource.0.interval",$symb);
+                                            if ($interval[1] eq 'map') {
+                                                last;
                                             }
                                         }
                                     }
                                 }
                             }
                         }
-                        if ($interval[0] =~ /\d+/) {
-                            my $first_access;
-                            if ($type eq 'resource') {
-                                $first_access=&get_first_access($interval[1],$item);
-                            } elsif ($type eq 'map') {
-                                $first_access=&get_first_access($interval[1],undef,$item);
-                            } else {
-                                $first_access=&get_first_access($interval[1]);
-                            }
-                            if ($first_access) {
-                                my $timesup = $first_access+$interval[0];
-                                if ($timesup > $now) {
-                                    foreach my $res (@to_test) {
-                                        if ($res->is_problem()) {
-                                            if ($res->completable()) {
-                                                unless (grep(/^\Q$block\E$/, at blockers)) {
-                                                    push(@blockers,$block);
-                                                }
-                                                last;
-                                            }
+                    }
+                    if ($interval[0] =~ /^\d+$/) {
+                        my $first_access;
+                        if ($type eq 'resource') {
+                            $first_access=&get_first_access($interval[1],$item);
+                        } elsif ($type eq 'map') {
+                            $first_access=&get_first_access($interval[1],undef,$item);
+                        } else {
+                            $first_access=&get_first_access($interval[1]);
+                        }
+                        if ($first_access) {
+                            my $timesup = $first_access+$interval[0];
+                            if ($timesup > $now) {
+                                my $activeblock;
+                                foreach my $res (@to_test) {
+                                    if ($res->completable()) {
+                                        $activeblock = 1;
+                                        last;
+                                    }
+                                }
+                                if ($activeblock) {
+                                    if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
+                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
+                                             $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
+                                         }
+                                    }
+                                    if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
+                                        if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
+                                            $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
                                         }
                                     }
                                 }
@@ -7333,33 +7356,66 @@
             }
         }
     }
-    return @blockers;
+    return %blockers;
 }
 
-sub check_docs_block {
-    my ($docsblock,$tocheck) =@_;
-    if ((ref($docsblock) ne 'HASH') || (ref($tocheck) ne 'HASH')) {
-        return;
+sub has_comm_blocking {
+    my ($priv,$symb,$uri,$blocks) = @_;
+    my @blockers;
+    return unless ($env{'request.course.id'});
+    return unless ($priv eq 'bre');
+    return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
+    return if ($env{'request.state'} eq 'construct');
+    &load_all_blockers($env{'user.name'},$env{'user.domain'},$blocks);
+    return unless (keys(%cachedblockers) > 0);
+    my (%possibles, at symbs);
+    if (!$symb) {
+        $symb = &symbread($uri,1,1,1,\%possibles);
     }
-    if (ref($docsblock->{'maps'}) eq 'HASH') {
-        if ($tocheck->{'maps'}) {
-            if ($docsblock->{'maps'}{$tocheck->{'maps'}}) {
-                return 1;
+    if ($symb) {
+        @symbs = ($symb);
+    } elsif (keys(%possibles)) { 
+        @symbs = keys(%possibles);
+    }
+    my $noblock;
+    foreach my $symb (@symbs) {
+        last if ($noblock);
+        my ($map,$resid,$resurl)=&decode_symb($symb);
+        foreach my $block (keys(%cachedblockers)) {
+            if ($block =~ /^firstaccess____(.+)$/) {
+                my $item = $1;
+                if (($item eq $map) || ($item eq $symb)) {
+                    $noblock = 1;
+                    last;
+                }
             }
-        }
-    }
-    if (ref($docsblock->{'resources'}) eq 'HASH') {
-        if ($tocheck->{'resources'}) {
-            if ($docsblock->{'resources'}{$tocheck->{'resources'}}) {
-                return 1;
+            if (ref($cachedblockers{$block}) eq 'HASH') {
+                if (ref($cachedblockers{$block}{'resources'}) eq 'HASH') {
+                    if ($cachedblockers{$block}{'resources'}{$symb}) {
+                        unless (grep(/^\Q$block\E$/, at blockers)) {
+                            push(@blockers,$block);
+                        }
+                    }
+                }
+            }
+            if (ref($cachedblockers{$block}{'maps'}) eq 'HASH') {
+                if ($cachedblockers{$block}{'maps'}{$map}) {
+                    unless (grep(/^\Q$block\E$/, at blockers)) {
+                        push(@blockers,$block);
+                    }
+                }
             }
         }
     }
-    return;
+    return if ($noblock);
+    return @blockers;
 }
+}
+
+# -------------------------------- Deversion and split uri into path an filename   
 
 #
-#   Removes the versino from a URI and
+#   Removes the version from a URI and
 #   splits it in to its filename and path to the filename.
 #   Seems like File::Basename could have done this more clearly.
 #   Parameters:
@@ -11108,9 +11164,15 @@
 # ------------------------------------------------------ Return symb list entry
 
 sub symbread {
-    my ($thisfn,$donotrecurse)=@_;
+    my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles)=@_;
     my $cache_str='request.symbread.cached.'.$thisfn;
-    if (defined($env{$cache_str})) { return $env{$cache_str}; }
+    if (defined($env{$cache_str})) {
+        if ($ignorecachednull) {
+            return $env{$cache_str} unless ($env{$cache_str} eq '');
+        } else {
+            return $env{$cache_str};
+        }
+    }
 # no filename provided? try from environment
     unless ($thisfn) {
         if ($env{'request.symb'}) {
@@ -11172,18 +11234,46 @@
 		     my ($mapid,$resid)=split(/\./,$ids);
 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
 						    $resid,$thisfn);
-                 } elsif (!$donotrecurse) {
+                     if (ref($possibles) eq 'HASH') {
+                         $possibles->{$syval} = 1;    
+                     }
+                     if ($checkforblock) {
+                         my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids});
+                         if (@blockers) {
+                             $syval = '';
+                             return;
+                         }
+                     }
+                 } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
 # ------------------------------------------ There is more than one possibility
                      my $realpossible=0;
                      foreach my $id (@possibilities) {
 			 my $file=$bighash{'src_'.$id};
-                         if (&allowed('bre',$file)) {
-         		    my ($mapid,$resid)=split(/\./,$id);
-                            if ($bighash{'map_type_'.$mapid} ne 'page') {
-				$realpossible++;
-                                $syval=&encode_symb($bighash{'map_id_'.$mapid},
-						    $resid,$thisfn);
-                            }
+                         my $canaccess;
+                         if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
+                             $canaccess = 1;
+                         } else { 
+                             $canaccess = &allowed('bre',$file);
+                         }
+                         if ($canaccess) {
+         		     my ($mapid,$resid)=split(/\./,$id);
+                             if ($bighash{'map_type_'.$mapid} ne 'page') {
+                                 my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
+						             $resid,$thisfn);
+                                 if (ref($possibles) eq 'HASH') {
+                                     $possibles->{$syval} = 1;
+                                 }
+                                 if ($checkforblock) {
+                                     my @blockers = &has_comm_blocking('bre',$poss_syval,$file);
+                                     unless (@blockers > 0) {
+                                         $syval = $poss_syval;
+                                         $realpossible++;
+                                     }
+                                 } else {
+                                     $syval = $poss_syval;
+                                     $realpossible++;
+                                 }
+                             }
 			 }
                      }
 		     if ($realpossible!=1) { $syval=''; }
@@ -11191,7 +11281,7 @@
                      $syval='';
                  }
 	      }
-              untie(%bighash)
+              untie(%bighash);
            }
         }
         if ($syval) {
@@ -12962,29 +13052,13 @@
 
 =item *
 
-allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
-returns codes for allowed actions
-
-The first argument is required, all others are optional.
-
-$priv is the privilege being checked.
-$uri contains additional information about what is being checked for access (e.g.,
-URL, course ID etc.). 
-$symb is the unique resource instance identifier in a course; if needed,
-but not provided, it will be retrieved via a call to &symbread(). 
-$role is the role for which a priv is being checked (only used if priv is evb). 
-$clientip is the user's IP address (only used when checking for access to portfolio 
-files).
-$noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
-prevents recursive calls to &allowed.  
-
+allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
  F: full access
  U,I,K: authentication modes (cxx only)
  '': forbidden
  1: user needs to choose course
  2: browse allowed
  A: passphrase authentication needed
- B: access temporarily blocked because of a blocking event in a course.
 
 =item *
 
@@ -13378,7 +13452,20 @@
 
 =item *
 
-symbread($filename) : return symbolic list entry (filename argument optional);
+symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
+return symbolic list entry (all arguments optional). 
+
+Args: filename is the filename (including path) for the file for which a symb 
+is required; donotrecurse, if true will prevent calls to allowed() being made 
+to check access status if more than one resource was found in the bighash 
+(see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
+a randompick); ignorecachednull, if true will prevent a symb of '' being 
+returned if $env{$cache_str} is defined as ''; checkforblock if true will
+cause possible symbs to be checked to determine if they are subject to content
+blocking, if so they will not be included as possible symbs; possibles is a
+ref to a hash, which, as a side effect, will be populated with all possible 
+symbs (content blocking not tested).
+ 
 returns the data handle
 
 =item *
Index: loncom/homework/lonhomework.pm
diff -u loncom/homework/lonhomework.pm:1.349 loncom/homework/lonhomework.pm:1.350
--- loncom/homework/lonhomework.pm:1.349	Sat Feb 21 21:53:34 2015
+++ loncom/homework/lonhomework.pm	Wed Apr 15 04:11:20 2015
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Homework handler
 #
-# $Id: lonhomework.pm,v 1.349 2015/02/21 21:53:34 raeburn Exp $
+# $Id: lonhomework.pm,v 1.350 2015/04/15 04:11:20 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1343,7 +1343,7 @@
     my $file=&Apache::lonnet::filelocation("",$request->uri);
 
     #check if we know where we are
-    if ($env{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
+    if ($env{'request.course.fn'} && !&Apache::lonnet::symbread('','',1,1)) {
 	# if we are browsing we might not be able to know where we are
 	if ($Apache::lonhomework::browse ne 'F' && 
 	    $env{'request.state'} ne "construct") {


More information about the LON-CAPA-cvs mailing list