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

raeburn raeburn at source.lon-capa.org
Tue Dec 20 17:43:35 EST 2011


raeburn		Tue Dec 20 22:43:35 2011 EDT

  Modified files:              
    /loncom/interface	loncommon.pm 
  Log:
  - More information about reserving slots for items with access controlled by slots.
    - "future" slots determination includes checks for section, group or user-specific
      slots, and slots restricted to use with a particular symb.
    - &get_future_slots() -- student-schedulable slots opening in future -- modified
      version moved from lonnavmaps.pm
  - &sorted_slots() - additional (optional) arg: sortkey (default is sort by starttime).
  
  
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.1039 loncom/interface/loncommon.pm:1.1040
--- loncom/interface/loncommon.pm:1.1039	Mon Dec 19 18:18:28 2011
+++ loncom/interface/loncommon.pm	Tue Dec 20 22:43:34 2011
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.1039 2011/12/19 18:18:28 www Exp $
+# $Id: loncommon.pm,v 1.1040 2011/12/20 22:43:34 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -8448,7 +8448,8 @@
 
 =item * sorted_slots()
 
-Sorts an array of slot names in order of slot start time (earliest first). 
+Sorts an array of slot names in order of an optional sort key,
+default sort is by slot start time (earliest first). 
 
 Inputs:
 
@@ -8458,15 +8459,16 @@
 
 slots     - Reference to hash of hash, where outer hash keys are slot names.
 
+sortkey   - Name of key in inner hash to be sorted on (e.g., starttime).
+
 =back
 
 Returns:
 
 =over 4
 
-sorted   - An array of slot names sorted by the start time of the slot.
-
-=back
+sorted   - An array of slot names sorted by a specified sort key 
+           (default sort key is start time of the slot).
 
 =back
 
@@ -8474,13 +8476,16 @@
 
 
 sub sorted_slots {
-    my ($slotsarr,$slots) = @_;
+    my ($slotsarr,$slots,$sortkey) = @_;
+    if ($sortkey eq '') {
+        $sortkey = 'starttime';
+    }
     my @sorted;
     if ((ref($slotsarr) eq 'ARRAY') && (ref($slots) eq 'HASH')) {
         @sorted =
             sort {
                      if (ref($slots->{$a}) && ref($slots->{$b})) {
-                         return $slots->{$a}{'starttime'} <=> $slots->{$b}{'starttime'}
+                         return $slots->{$a}{$sortkey} <=> $slots->{$b}{$sortkey}
                      }
                      if (ref($slots->{$a})) { return -1;}
                      if (ref($slots->{$b})) { return 1;}
@@ -8490,6 +8495,131 @@
     return @sorted;
 }
 
+=pod
+
+=item * get_future_slots()
+
+Inputs:
+
+=over 4
+
+cnum - course number
+
+cdom - course domain
+
+now - current UNIX time
+
+symb - optional symb
+
+=back
+
+Returns:
+
+=over 4
+
+sorted_reservable - ref to array of student_schedulable slots currently 
+                    reservable, ordered by end date of reservation period.
+
+reservable_now - ref to hash of student_schedulable slots currently
+                 reservable.
+
+    Keys in inner hash are:
+    (a) symb: either blank or symb to which slot use is restricted.
+    (b) endreserve: end date of reservation period. 
+
+sorted_future - ref to array of student_schedulable slots reservable in
+                the future, ordered by start date of reservation period.
+
+future_reservable - ref to hash of student_schedulable slots reservable
+                    in the future.
+
+    Keys in inner hash are:
+    (a) symb: either blank or symb to which slot use is restricted.
+    (b) startreserve:  start date of reservation period.
+
+=back
+
+=cut
+
+sub get_future_slots {
+    my ($cnum,$cdom,$now,$symb) = @_;
+    my (%reservable_now,%future_reservable, at sorted_reservable, at sorted_future);
+    my %slots = &Apache::lonnet::get_course_slots($cnum,$cdom);
+    foreach my $slot (keys(%slots)) {
+        next unless($slots{$slot}->{'type'} eq 'schedulable_student');
+        if ($symb) {
+            next if (($slots{$slot}->{'symb'} ne '') && 
+                     ($slots{$slot}->{'symb'} ne $symb));
+        }
+        if (($slots{$slot}->{'starttime'} > $now) &&
+            ($slots{$slot}->{'endtime'} > $now)) {
+            if (($slots{$slot}->{'allowedsections'}) || ($slots{$slot}->{'allowedusers'})) {
+                my $userallowed = 0;
+                if ($slots{$slot}->{'allowedsections'}) {
+                    my @allowed_sec = split(',',$slots{$slot}->{'allowedsections'});
+                    if (!defined($env{'request.role.sec'})
+                        && grep(/^No section assigned$/, at allowed_sec)) {
+                        $userallowed=1;
+                    } else {
+                        if (grep(/^\Q$env{'request.role.sec'}\E$/, at allowed_sec)) {
+                            $userallowed=1;
+                        }
+                    }
+                    unless ($userallowed) {
+                        if (defined($env{'request.course.groups'})) {
+                            my @groups = split(/:/,$env{'request.course.groups'});
+                            foreach my $group (@groups) {
+                                if (grep(/^\Q$group\E$/, at allowed_sec)) {
+                                    $userallowed=1;
+                                    last;
+                                }
+                            }
+                        }
+                    }
+                }
+                if ($slots{$slot}->{'allowedusers'}) {
+                    my @allowed_users = split(',',$slots{$slot}->{'allowedusers'});
+                    my $user = $env{'user.name'}.':'.$env{'user.domain'};
+                    if (grep(/^\Q$user\E$/, at allowed_users)) {
+                        $userallowed = 1;
+                    }
+                }
+                next unless($userallowed);
+            }
+            my $startreserve = $slots{$slot}->{'startreserve'};
+            my $endreserve = $slots{$slot}->{'endreserve'};
+            my $symb = $slots{$slot}->{'symb'};
+            if (($startreserve < $now) &&
+                (!$endreserve || $endreserve > $now)) {
+                my $lastres = $endreserve;
+                if (!$lastres) {
+                    $lastres = $slots{$slot}->{'starttime'};
+                }
+                $reservable_now{$slot} = {
+                                           symb       => $symb,
+                                           endreserve => $lastres
+                                         };
+            } elsif (($startreserve > $now) &&
+                     (!$endreserve || $endreserve > $startreserve)) {
+                $future_reservable{$slot} = {
+                                              symb         => $symb,
+                                              startreserve => $startreserve
+                                            };
+            }
+        }
+    }
+    my @unsorted_reservable = keys(%reservable_now);
+    if (@unsorted_reservable > 0) {
+        @sorted_reservable = 
+            &sorted_slots(\@unsorted_reservable,\%reservable_now,'endreserve');
+    }
+    my @unsorted_future = keys(%future_reservable);
+    if (@unsorted_future > 0) {
+        @sorted_future =
+            &sorted_slots(\@unsorted_future,\%future_reservable,'startreserve');
+    }
+    return (\@sorted_reservable,\%reservable_now,\@sorted_future,\%future_reservable);
+}
 
 =pod
 




More information about the LON-CAPA-cvs mailing list