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

raeburn lon-capa-cvs@mail.lon-capa.org
Thu, 15 Dec 2005 00:53:30 -0000


This is a MIME encoded message

--raeburn1134608010
Content-Type: text/plain

raeburn		Wed Dec 14 19:53:30 2005 EDT

  Modified files:              
    /loncom/interface	lonwhatsnew.pm lonnavmaps.pm 
  Log:
  Separate checking for new posts from counting number of new posts for each resource.  Should improve efficiency by replacing multiple restores with a single dump.  Menu still to be added to allow $countunread (whether to count or not) to be user-configurable. 
  
  
--raeburn1134608010
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20051214195330.txt"

Index: loncom/interface/lonwhatsnew.pm
diff -u loncom/interface/lonwhatsnew.pm:1.36 loncom/interface/lonwhatsnew.pm:1.37
--- loncom/interface/lonwhatsnew.pm:1.36	Tue Dec  6 11:37:28 2005
+++ loncom/interface/lonwhatsnew.pm	Wed Dec 14 19:53:29 2005
@@ -1,5 +1,5 @@
 #
-# $Id: lonwhatsnew.pm,v 1.36 2005/12/06 16:37:28 raeburn Exp $
+# $Id: lonwhatsnew.pm,v 1.37 2005/12/15 00:53:29 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -300,13 +300,14 @@
         $timediff = time;
     } 
     my $starttime = $now - $timediff;
+    my $countunread = 1;
 
     my %headings = &Apache::lonlocal::texthash(
                 coursediscussion =>  'Unread course discussion posts',
                 handgrading =>  'Problems requiring handgrading',
                 haserrors => 'Problems with errors',
                 versionchanges => 'Resources in course with version changes '.$interval,
-                coursenormalmail => 'New course message',
+                coursenormalmail => 'New course messages',
                 coursecritmail => 'New critical messages in course',
     );
 
@@ -337,7 +338,7 @@
     }
 
     if ($needitems) {
-        &getitems(\%unread,\%ungraded,\%bombed,\%triggered,\%changed,\@newdiscussions,\@tograde,\@bombs,\@warnings,$rowColor1,$rowColor2,\%threshold,$cdom,$crs,\%res_title,\%show,$starttime);
+        &getitems(\%unread,\%ungraded,\%bombed,\%triggered,\%changed,\@newdiscussions,\@tograde,\@bombs,\@warnings,$rowColor1,$rowColor2,\%threshold,$cdom,$crs,\%res_title,\%show,$starttime,$countunread);
     }
     if ($show{'coursenormalmail'}) {
         &getnormalmail(\@newmsgs);
@@ -365,7 +366,7 @@
             if ($displayed == $halfway) {
                 $r->print('</td><td width="5%">&nbsp;</td><td align="left" valign="top" width-"50%">');
             }
-            &display_launcher($r,$actionitem,$checkallowed,$tabbg,$rowColor1,$rowColor2,\%show,\%headings,\%res_title,\@tograde,\%ungraded,\@bombs,\%bombed,\%changed,\@warnings,\%triggered,\@newdiscussions,\%unread,$msgcount,\@newmsgs,$critmsgcount,\@critmsgs,$interval);
+            &display_launcher($r,$actionitem,$checkallowed,$tabbg,$rowColor1,$rowColor2,\%show,\%headings,\%res_title,\@tograde,\%ungraded,\@bombs,\%bombed,\%changed,\@warnings,\%triggered,\@newdiscussions,\%unread,$msgcount,\@newmsgs,$critmsgcount,\@critmsgs,$interval,$countunread);
             $displayed ++; 
         }
     }
@@ -481,7 +482,7 @@
     my ($r,$action,$checkallowed,$tabbg,$rowColor1,$rowColor2,$show,
         $headings,$res_title,$tograde,$ungraded,$bombs,$bombed,$changed,
         $warnings,$triggered,$newdiscussions,$unread,$msgcount,$newmsgs,
-                                       $critmsgcount,$critmsgs,$interval) = @_;
+                          $critmsgcount,$critmsgs,$interval,$countunread) = @_;
 
     if ($$checkallowed{$action}) {
         &start_box($r,$tabbg,$show,$headings,$action);
@@ -501,7 +502,7 @@
                                                         $rowColor1,$rowColor2);
             } elsif ($action eq 'coursediscussion') { # UNREAD COURSE DISCUSSION
                 &display_coursediscussion($r,$newdiscussions,$unread,
-                                             $res_title,$rowColor1,$rowColor2);
+                                $countunread,$res_title,$rowColor1,$rowColor2);
             } elsif ($action eq 'coursenormalmail') { # NORMAL MESSAGES
                 &display_coursenormalmail($r,$msgcount,$newmsgs,$rowColor1,
                                                                    $rowColor2);
@@ -518,19 +519,21 @@
 sub getitems {
     my ($unread,$ungraded,$bombed,$triggered,$changed,$newdiscussions,
         $tograde,$bombs,$warnings,$rowColor1,$rowColor2,$threshold,$cdom,$crs,
-                                             $res_title,$show,$starttime) = @_;
+                                 $res_title,$show,$starttime,$countunread) = @_;
     my $navmap = Apache::lonnavmaps::navmap->new();
     # force retrieve Resource to seed the part id cache we'll need it later
-    my @allres=$navmap->retrieveResources(undef,sub {if ($_[0]->is_problem) { $_[0]->parts();} return 1;});
+    my @allres=$navmap->retrieveResources(undef,
+                     sub {if ($_[0]->is_problem) { $_[0]->parts();} return 1;});
     my %lastreadtime;
     my %resourcetracker;
+    my $discussiontime;
 
 # Resource version changes
     if ($$show{'versionchanges'}) {
         &checkversions($cdom,$crs,$navmap,$changed,$starttime);
     }
 
-    if ($$show{'coursediscussions'}) {   
+    if ($$show{'coursediscussion'}) {   
         my %lastread = &Apache::lonnet::dump('nohist_'.
                         $env{'request.course.id'}.'_discuss',
                         $env{'user.domain'},$env{'user.name'},'lastread');
@@ -559,10 +562,10 @@
         $$res_title{$symb} = $title;
         my $ressymb = $resource->wrap_symb();
 
-# Check for unread discussion postings
+# Check if there are unread discussion postings
         if ($$show{'coursediscussion'}) {
             &check_discussions($cdom,$crs,$resource,$symb,$ressymb,$title,
-                                       $newdiscussions,$unread,\%lastreadtime);
+                                                      $newdiscussions,$unread);
         }
 
 # Check for ungraded problems
@@ -585,38 +588,82 @@
         }
 
     }
+    my $hasdiscussion = @{$newdiscussions};
+    if ($$show{'coursediscussion'} && $hasdiscussion) { # Get time of last post; 
+        $discussiontime = $navmap->{DISCUSSION_TIME};
+        foreach my $ressymb (@{$newdiscussions}) {
+            $$unread{$ressymb}{'lastpost'} = $$discussiontime{$ressymb};
+        }
+        if ($countunread) { #Get count of unread postings for each resource 
+            my $discussiondata = $navmap->get_discussion_data();
+            foreach my $ressymb (@{$newdiscussions}) {
+                &get_discussions($cdom,$crs,$discussiondata,$ressymb,
+                                                       $unread,\%lastreadtime);
+            }
+        }
+    }
 }
 
 sub check_discussions {
-    my ($cdom,$crs,$resource,$symb,$ressymb,$title,$newdiscussions,$unread,
-                                                           $lastreadtime) = @_;  
-# Check for unread discussion postings
+    my ($cdom,$crs,$resource,$symb,$ressymb,$title,$newdiscussions,
+                                                                 $unread) = @_;
     if ($resource->hasDiscussion()) {
-        my $prevread = 0;
-        my $unreadcount = 0;
         %{$$unread{$ressymb}} = ();
         $$unread{$ressymb}{'title'} = $title;
         $$unread{$ressymb}{'symb'} = $symb;
-        if (defined($$lastreadtime{$ressymb})) {
-            $prevread = $$lastreadtime{$ressymb};
+        push(@{$newdiscussions}, $ressymb);
+    }
+}
+
+sub get_discussions {
+    my ($cdom,$crs,$discussiondata,$ressymb,$unread,$lastreadtime) = @_;
+    my $prevread = 0;
+    my $unreadcount = 0;
+    if (defined($$lastreadtime{$ressymb})) {
+        $prevread = $$lastreadtime{$ressymb};
+    }
+    my $version = $$discussiondata{'version:'.$ressymb};
+    if ($version) {
+        my $hiddenflag = 0;
+        my $deletedflag = 0;
+        my ($hidden,$deleted);
+        for (my $id=$version; $id>0; $id--) {
+            my $vkeys=$$discussiondata{$id.':keys:'.$ressymb};
+            my @keys=split(/:/,$vkeys);
+            if (grep/^hidden$/,@keys) {
+                unless ($hiddenflag) {
+                    $hidden = $$discussiondata{$id.':'.$ressymb.':hidden'};
+                    $hiddenflag = 1;
+                }
+            }
+            if (grep/^deleted$/,@keys) {
+                unless ($deletedflag) {
+                    $deleted = $$discussiondata{$id.':'.$ressymb.':deleted'};
+                    $deletedflag = 1;
+                }
+            }
+            if ($deletedflag && $hiddenflag) {
+                last;
+            }
         }
-        my %contrib = &Apache::lonnet::restore($ressymb,
-                                         $env{'request.course.id'},$cdom,$crs);
-        if ($contrib{'version'}) {
-            for (my $id=1;$id<=$contrib{'version'};$id++) {
-                unless (($contrib{'hidden'}=~/\.$id\./) || 
-                        ($contrib{'deleted'}=~/\.$id\./)) {
-                    if ($prevread <$contrib{$id.':timestamp'}) {
-                        $$unread{$ressymb}{$unreadcount} = $id.': '.$contrib{$id.':subject'};
+        for (my $id=$version; $id>0; $id--) {
+            unless (($hidden =~/\.$id\./) || ($deleted =~/\.$id\./)) {
+                if ($prevread <$$discussiondata{$id.':'.$ressymb.':timestamp'}) {
+                    unless((exists($$discussiondata{$id.':'.$ressymb.':hidden'})) ||
+                         (exists($$discussiondata{$id.':'.$ressymb.':deleted'}))) {
+
                         $unreadcount ++;
+                        $$unread{$ressymb}{$unreadcount} = $id.': '.
+                                 $$discussiondata{$id.':'.$ressymb.':subject'};
                     }
                 }
             }
         }
-        if ($unreadcount) { push(@{$newdiscussions}, $ressymb); }
+        $$unread{$ressymb}{'unreadcount'} = $unreadcount;
     }
 }
 
+
 sub check_handgraded {
     my ($resource,$symb,$title,$cdom,$cnum,$ungraded,$tograde) = @_;
     if ($resource->is_problem()) {
@@ -1072,19 +1119,29 @@
 }
  
 sub display_coursediscussion {
-    my ($r,$newdiscussions,$unread,$res_title,$rowColor1,$rowColor2) = @_;
+    my ($r,$newdiscussions,$unread,$countunread,$res_title,$rowColor1,
+                                                              $rowColor2) = @_;
     my %lt = &Apache::lonlocal::texthash(
                 'loca' => 'Location',
                 'type' => 'Type',
                 'numn' => 'Number of new posts',
                 'noun' => 'No unread posts in course discussions',
+                'tmlp' => 'Time of last post', 
     );
     my $rowColor;
     if (@{$newdiscussions} > 0) {
         $r->print('<tr bgcolor="#cccccc"><td><b><small>'.$lt{'loca'}.
                   '</small></b></td><td><b><small>'.$lt{'type'}.
-                  '</small></b><td align="right"><b><small>'.$lt{'numn'}.
-                  '</small></b></td></tr>');
+                  '</small></b>');
+        if ($countunread) {
+            $r->print('<td><b><small>'.$lt{'tmlp'}.'</small></b></td>'.
+                      '<td align="right"><b><small>'.$lt{'numn'}.
+                      '</small></b></td>');
+        } else {
+            $r->print('<td align="right"><b><small>'.$lt{'tmlp'}.
+                         '</small></b></td>');
+        }
+        $r->print("</tr>\n");
         @{$newdiscussions} = sort { &cmp_title($a,$b,$res_title) }
                                                             @{$newdiscussions};
         my $rowNum = 0;
@@ -1095,17 +1152,23 @@
             if ($feedurl =~ /bulletinboard/) {
                 $type = 'Bulletin Board';
             }
-            my $unreadnum = keys(%{$$unread{$ressymb}});
-            $unreadnum = $unreadnum - 2;
-            if ($unreadnum > 0) {
-                if ($rowNum %2 == 1) {
-                    $rowColor = $rowColor1;
-                } else {
-                    $rowColor = $rowColor2;
-                }
-                $r->print('<tr bgcolor="'.$rowColor.'"><td><small><a href="'.$feedurl.'?symb='.$$unread{$ressymb}{symb}.'">'.$forum_title.'</a>&nbsp;</td><td><small>'.$type.'</small></td><td align="right">'.$unreadnum.'&nbsp;</td></tr>');
-                $rowNum ++;
+            if ($rowNum %2 == 1) {
+                $rowColor = $rowColor1;
+            } else {
+                $rowColor = $rowColor2;
             }
+            my $lastpost = &Apache::lonnavmaps::timeToHumanString(
+                                               $$unread{$ressymb}{'lastpost'});
+            $r->print('<tr bgcolor="'.$rowColor.'"><td><small><a href="'.$feedurl.'?symb='.$$unread{$ressymb}{symb}.'">'.$forum_title.'</a>&nbsp;</td><td><small>'.$type.'&nbsp;</small></td>');
+            if ($countunread) {
+                my $unreadnum = $$unread{$ressymb}{'unreadcount'};
+                $r->print('<td><small>'.$lastpost.'<small></td><td align="right">'.
+                          '<small>',$unreadnum.'&nbsp;</small></td>');
+            } else {
+                $r->print('<td align="right"><small>'.$lastpost.'</small></td>');
+            }
+            $r->print("</tr>\n");
+            $rowNum ++;
         }
     } else {
         $r->print('<tr><td bgcolor="#ffffff"><br><center>&nbsp;<i><b><small>'.
Index: loncom/interface/lonnavmaps.pm
diff -u loncom/interface/lonnavmaps.pm:1.353 loncom/interface/lonnavmaps.pm:1.354
--- loncom/interface/lonnavmaps.pm:1.353	Mon Dec  5 14:30:22 2005
+++ loncom/interface/lonnavmaps.pm	Wed Dec 14 19:53:30 2005
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Navigate Maps Handler
 #
-# $Id: lonnavmaps.pm,v 1.353 2005/12/05 19:30:22 raeburn Exp $
+# $Id: lonnavmaps.pm,v 1.354 2005/12/15 00:53:30 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -2265,6 +2265,25 @@
     $self->{RETRIEVED_USER_DATA} = 1;
 }
 
+sub get_discussion_data {
+    my $self = shift;
+    if ($self->{RETRIEVED_DISCUSSION_DATA}) {
+         return  $self->{DISCUSSION_DATA};
+    }
+                                                                                
+    my $cid=$env{'request.course.id'};
+    my $cdom=$env{'course.'.$cid.'.domain'};
+    my $cnum=$env{'course.'.$cid.'.num'};
+                                                                                
+    # Retrieve discussion data for resources in course
+    my %discussion_data = &Apache::lonnet::dump($cid,$cdom,$cnum);
+                                                                                
+    $self->{DISCUSSION_DATA} = \%discussion_data;
+    $self->{RETRIEVED_DISCUSSION_DATA} = 1;
+    return $self->{DISCUSSION_DATA};
+}
+
+
 # Internal function: Takes a key to look up in the nav hash and implements internal
 # memory caching of that key.
 sub navhash {

--raeburn1134608010--