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

bowersj2 lon-capa-cvs@mail.lon-capa.org
Thu, 30 Jan 2003 21:36:57 -0000


This is a MIME encoded message

--bowersj21043962617
Content-Type: text/plain

bowersj2		Thu Jan 30 16:36:57 2003 EDT

  Modified files:              
    /loncom/interface	lonnavmaps.pm 
  Log:
  Beginning the splitting of rendering and lonnavmap interface, so nav
  maps can be easily rendered in a variety of manners and contexts.
  
  This commit should have no visible effects on the nav maps screen itself.
  If it does, that's a bug.
  
  
--bowersj21043962617
Content-Type: text/plain
Content-Disposition: attachment; filename="bowersj2-20030130163657.txt"

Index: loncom/interface/lonnavmaps.pm
diff -u loncom/interface/lonnavmaps.pm:1.131 loncom/interface/lonnavmaps.pm:1.132
--- loncom/interface/lonnavmaps.pm:1.131	Tue Jan 28 17:25:54 2003
+++ loncom/interface/lonnavmaps.pm	Thu Jan 30 16:36:57 2003
@@ -2,7 +2,7 @@
 # The LearningOnline Network with CAPA
 # Navigate Maps Handler
 #
-# $Id: lonnavmaps.pm,v 1.131 2003/01/28 22:25:54 bowersj2 Exp $
+# $Id: lonnavmaps.pm,v 1.132 2003/01/30 21:36:57 bowersj2 Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -350,7 +350,7 @@
     
     undef $res; # so we don't accidentally use it later
     my $indentLevel = 0;
-    my $indentString = "<img src=\"/adm/lonIcons/whitespace1.gif\" width=\"25\" height=\"1\" alt=\"\" border=\"0\" />";
+    my $indentString = "<img src='/adm/lonIcons/whitespace1.gif' width='25' height='1' alt='' border='0' />";
 
     my $isNewBranch = 0;
     my $now = time();
@@ -388,13 +388,6 @@
 
         if (ref($curRes)) { $counter++; }
 
-        # Is this resource being ignored because it is in a random-out
-        # map and it was not selected?
-        if (ref($curRes) && !advancedUser() && $curRes->randomout()) {
-            $curRes = $mapIterator->next();
-            next; # if yes, then just ignore this resource
-        }
-
         if (ref($curRes)) {
 
             my $deltalevel = $isNewBranch? 1 : 0; # reserves space for branch icon
@@ -427,6 +420,9 @@
                         # Otherwise, only display part 0 if we want to 
                         # attach feedback or email information to it
                         if ($curRes->hasDiscussion() || $curRes->getFeedback()) {
+                            # Is this right? I think this will toss it
+                            # if it DOES have discussion, not if it doesn't?
+                            # - Jeremy (yes, commenting on his own code)
                             shift @parts;
                         } else {
                             # Now, we decide whether to condense the
@@ -449,7 +445,7 @@
                                 }
                             }
                             
-                            # $allSame is true if all the statuses were
+                            # $*allSame is true if all the statuses were
                             # the same. Now, if they are all the same and
                             # match one of the statuses to condense, or they
                             # are all open with the same due date, or they are
@@ -960,7 +956,7 @@
 
 =pod
 
-lonnavmaps provides functions and objects for dealing with the compiled course hashes generated when a user enters the course, and also provides the Apache handler for the "Navigation Map" button.
+lonnavmaps provides functions and objects for dealing with the compiled course hashes generated when a user enters the course, the Apache handler for the "Navigation Map" button, and a flexible prepared renderer for navigation maps that are easy to use anywhere.
 
 =head1 navmap object: Encapsulating the compiled nav map
 
@@ -1353,6 +1349,287 @@
     return '';
 }
 
+
+1;
+
+package Apache::lonnavmaps::renderer;
+
+=pod
+
+=head1 navmap renderer
+
+The navmaprenderer package provides a sophisticated rendering of the standard navigation maps interface into HTML. The provided nav map handler is actually just a glorified call to this. 
+
+Because of the large number of parameters this function presents, instead of passing it arguments as is normal, pass it in an anonymous hash with the given options. This is because there is no obvious order you may wish to override these in and a hash is easier to read and understand then "undef, undef, undef, 1, undef, undef, renderButton, undef, 0" when you mostly want default behaviors.
+
+The package provides a function called 'render', called as Apache::lonnavmaps::renderer->render({}).
+
+=head2 Overview of Columns
+
+The renderer will build an HTML table for the navmap and return it. The table is consists of several columns, and a row for each resource (or possibly each part). You tell the renderer how many columns to create and what to place in each column, optionally using one or more of the preparent columns, and the renderer will assemble the table.
+
+Any additional generally useful column types should be placed in the renderer code here, so anybody can use it anywhere else. Any code specific to the current application (such as the addition of <input> elements in a column) should be placed in the code of the thing using the renderer.
+
+At the core of the renderer is the array reference COLS (see Example section below for how to pass this correctly). The COLS array will consist of entries of one of two types of things: Either an integer representing one of the pre-packaged column types, or a sub reference that takes a resource reference, a part number, and a reference to the argument hash passed to the renderer, and returns a string that will be inserted into the HTML representation as it.
+
+The pre-packaged column names are refered to by constants in the Apache::lonnavmaps::renderer namespace. The following currently exist:
+
+=over 4
+
+=item * B<resource>: The general info about the resource: Link, icon for the type, etc. The first column in the standard nav map display. This column also accepts the following parameter in the renderer hash:
+
+=over 4
+
+=item * B<resource_nolink>: If true, the resource will not be linked. Default: false, resource will have links.
+
+=item * B<resource_part_count>: If true (default), the resource will show a part count if the full part list is not displayed. If false, the resource will never show a part count.
+
+=back
+
+=item B<communication_status>: Whether there is discussion on the resource, email for the user, or (lumped in here) perl errors in the execution of the problem. This is the second column in the main nav map.
+
+=item B<quick_status>: An icon for the status of a problem, with four possible states: Correct, incorrect, open, or none (not open yet, not a problem). The third column of the standard navmap.
+
+=item B<long_status>: A text readout of the details of the current status of the problem, such as "Due in 22 hours". The fourth column of the standard navmap.
+
+=back
+
+If you add any others please be sure to document them here.
+
+An example of a column renderer that will show the ID number of a resource, along with the part name if any:
+
+ sub { 
+  my ($resource, $part, $params) = @_;   
+  if ($part) { return '<td>' . $resource->{ID} . ' ' . $part . '</td>'; }
+  return '<td>' . $resource->{ID} . '</td>';
+ }
+
+Note these functions are responsible for the TD tags, which allow them to override vertical and horizontal alignment, etc.
+
+=head2 Parameters
+
+=over 4
+
+=item * B<iterator>: A reference to a fresh ::iterator to use from the navmaps. The rendering will reflect the options passed to the iterator, so you can use that to just render a certain part of the course, if you like.
+
+=item * B<cols>: An array reference
+
+=item * B<showParts>: A flag. If yes (default), a line for the resource itself, and a line for each part will be displayed. If not, only one line for each resource will be displayed.
+
+=item * B<condenseParts>: A flag. If yes (default), if all parts of the problem have the same status and that status is Nothing Set, Correct, or Network Failure, then only one line will be displayed for that resource anyhow. If no, all parts will always be displayed. If showParts is 0, this is ignored.
+
+=item * B<jumpCount>: A string identifying the URL to place the anchor 'curloc' at. Default to no anchor at all. It is the responsibility of the renderer user to ensure that the #curloc is in the URL.
+
+=item * B<hereURL>: A URL identifying where to place the 'here' marker.
+
+=item * B<hereSymb>: A Symb identifying where to place the 'here' marker.
+
+=item * B<indentString>: A string identifying the indentation string to use. By default, this is a 25 pixel whitespace image with no alt text.
+
+=back
+
+=cut
+
+sub resource { return 0; }
+sub communication_status { return 1; }
+sub quick_status { return 2; }
+sub long_status { return 3; }
+
+sub setDefault {
+    my ($val, $default) = @_;
+    if (!defined($val)) { return $default; }
+    return $val;
+}
+
+sub render {
+    my $args = shift;
+    
+    # Configure the renderer.
+    my $cols = $args->{'cols'};
+    if (!defined($cols)) {
+        # no columns, no nav maps.
+        return '';
+    }
+    my $it = $args->{'iterator'};
+    if (!defined($it)) {
+        # no iterator, no nav map.
+        return '';
+    }
+    
+    my $showParts = setDefault($args->{'showParts'}, 1);
+    my $condenseParts = setDefault($args->{'condenseParts'}, 1);
+    my $jumpToURL = $args->{'jumpToURL'};
+    my $jumpToSymb = $args->{'jumpToSymb'};
+    my $indentString = setDefault($args->{'indentString'}, "<img src='/adm/lonIcons/whitespace1.gif' width='25' height='1' alt='' border='0' />");
+
+    # End parameter setting
+
+    # Data
+    my $result .= '<table cellspacing="0" cellpadding="3" border="0" bgcolor="#FFFFFF">' ."\n";
+    my $res = "Apache::lonnavmaps::resource";
+    my %condenseStatuses =
+        ( $res->NETWORK_FAILURE    => 1,
+          $res->NOTHING_SET        => 1,
+          $res->CORRECT            => 1 );
+    my @backgroundColors = ("#FFFFFF", "#F6F6F6");
+
+    # Set up iteration.
+    my $depth = 1;
+    $it->next(); # discard initial BEGIN_MAP
+    my $curRes = $it->next();
+    my $indentLevel = 0;
+    my $isNewBranch = 0;
+    my $now = time();
+    my $in24Hours = $now + 24 * 60 * 60;
+    my $displayedHereMarker = 0;
+    my $displayedJumpMarker = 0;
+    my $rownum = 0;
+
+    while ($depth > 0) {
+        if ($curRes == $it->BEGIN_MAP()) { $depth++; }
+        if ($curRes == $it->END_MAP()) { $depth--; }
+
+        # Maintain indentation level.
+        if ($curRes == $it->BEGIN_MAP() ||
+            $curRes == $it->BEGIN_BRANCH() ) {
+            indentLevel++;
+        }
+        if ($curRes == $it->END_MAP() ||
+            $curRes == $it->END_BRANCH() ) {
+            $indentLevel--;
+        }
+        # Notice new branches
+        if ($curRes == $it->BEGIN_BRANCH()) {
+            $isNewBranch = 1;
+        }
+        
+        my $deltalevel = $isNewBranch? 1 : 0; # reserve space for branch symbol
+        if ($indentLevel - $deltalevel < 0) {
+            # If this would be at a negative depth (top-level maps in
+            # new-style courses, we want to suppress their title display)
+            # then ignore it.
+            $curRes = $it->next();
+            next;
+        }
+
+        # Does it have multiple parts?
+        my $multipart = 0;
+        my $condensed = 0;
+        my @parts;
+            
+        # Decide what parts to show.
+        if ($showParts) {
+            @parts = @{$curRes->parts()};
+            $multipart = scalar(@parts) > 1;
+            
+            if ($condenseParts) { # do the condensation
+                if (!$curRes->opendate("0")) {
+                    @parts = ("0");
+                    $condensed = 1;
+                }
+                if (!$condensed) {
+                    # Decide whether to condense based on similarity
+                    my $status = $curRes->status($parts[1]);
+                    my $due = $curRes->duedate($parts[1]);
+                    my $open = $curRes->opendate($parts[1]);
+                    my $statusAllSame = 1;
+                    my $dueAllSame = 1;
+                    my $openAllSame = 1;
+                    for (my $i = 2; $i < scalar(@parts); $i++) {
+                        if ($curRes->status($parts[$i]) != $status){
+                            $statusAllSame = 0;
+                        }
+                        if ($curRes->duedate($parts[$i]) != $due ) {
+                            $dueAllSame = 0;
+                        }
+                        if ($curRes->opendate($parts[$i]) != $open) {
+                            $openAllSame = 0;
+                        }
+                    }
+                    # $*allSame is true if all the statuses were
+                    # the same. Now, if they are all the same and
+                    # match one of the statuses to condense, or they
+                    # are all open with the same due date, or they are
+                    # all OPEN_LATER with the same open date, display the
+                    # status of the first non-zero part (to get the 'correct'
+                    # status right, since 0 is never 'correct' or 'open').
+                    if (($statusAllSame && defined($condenseStatuses{$status})) ||
+                        ($dueAllSame && $status == $curRes->OPEN && $statusAllSame)||
+                        ($openAllSame && $status == $curRes->OPEN_LATER && $statusAllSame) ){
+                        @parts = ($parts[1]);
+                        $condensed = 1;
+                    }
+                    
+                }
+            }
+            
+        } else {
+            # Not showing parts
+            @parts = ("0"); # show main part only
+        }
+
+        # If the multipart problem was condensed, "forget" it was multipart
+        if (scalar(@parts) == 1) {
+            $multipart = 0;
+        }
+
+        # In the event of a network error, display one part.
+        # If this is a single part, we can at least show the correct
+        # status, but if it's multipart, we're lost, since we can't
+        # retreive the metadata to count the parts
+        if ($curRes->{RESOURCE_ERROR}) {
+            @parts = ("0");
+        }
+
+        # Now, we've decided what parts to show. Loop through them and
+        # show them.
+        foreach my $part (@parts) {
+            $rownum ++;
+            my $backgroundColor = $backgroundColors[$rownum % scalar(@backgroundColors)];
+            
+            $result .= "  <tr bgcolor='$backgroundColor'>\n";
+            
+            # Now, display each column.
+            foreach my $col (@$cols) {
+                $result .= "    <td>";
+
+                # Decide what to display
+                
+
+                $result .= "</td>\n";
+            }
+
+            $result .= "  </tr>\n";
+        }
+        
+
+        $curRes = $it->next();
+    }
+    
+    $result .= "</table>";
+
+    return $result;
+}
+
+sub render_resource {
+    my ($resource, $part, $params) = @_;
+    return "<td align='center'>resource</td>";
+}
+sub render_communication_status {
+    my ($resource, $part, $params) = @_;
+    return "<td align='center'>comm_status</td>";
+}
+sub render_quick_status {
+    my ($resource, $part, $params) = @_;
+    return "<td align='center'>quick_status</td>";
+}
+sub render_long_status {
+    my ($resource, $part, $params) = @_;
+    return "<td align='center'>long_status</td>";
+}
+
+my @preparedColumns = (\&render_resource, \&render_communication_status,
+                       \&render_quick_status, \&render_long_status);
 
 1;
 

--bowersj21043962617--