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

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Mon, 30 Jun 2008 03:56:28 -0000


raeburn		Sun Jun 29 23:56:28 2008 EDT

  Modified files:              
    /loncom/interface	loncommon.pm 
  Log:
  - Fix perldoc
  - Add two subroutines used to construct data table for assigning categories to a course.
    - &assign_categories_table()
    - &assign_category_row() 
  
  
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.662 loncom/interface/loncommon.pm:1.663
--- loncom/interface/loncommon.pm:1.662	Tue Jun 24 12:44:22 2008
+++ loncom/interface/loncommon.pm	Sun Jun 29 23:56:27 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.662 2008/06/24 16:44:22 bisitz Exp $
+# $Id: loncommon.pm,v 1.663 2008/06/30 03:56:27 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -8272,11 +8272,15 @@
 generate Domain Coordinator interface for editing Course Categories.
 
 Inputs:
+
 categories (reference to hash of category definitions).
+
 cats (reference to array of arrays/hashes which encapsulates hierarchy of
       categories and subcategories).
+
 idx (reference to hash of counters used in Domain Coordinator interface for 
       editing Course Categories).
+
 jsarray (reference to array of categories used to create Javascript arrays for
          Domain Coordinator interface for editing Course Categories).
 
@@ -8319,14 +8323,20 @@
 Used to generate breadcrumb trails for course categories.
 
 Inputs:
+
 categories (reference to hash of category definitions).
+
 cats (reference to array of arrays/hashes which encapsulates hierarchy of
       categories and subcategories).
+
 trails (reference to array of breacrumb trails for each category).
+
 allitems (reference to hash - key is category key 
          (format: escaped(name):escaped(parent category):depth in hierarchy).
+
 idx (reference to hash of counters used in Domain Coordinator interface for
       editing Course Categories).
+
 jsarray (reference to array of categories used to create Javascript arrays for
          Domain Coordinator interface for editing Course Categories).
 
@@ -8374,13 +8384,19 @@
 Recursively used to generate breadcrumb trails for course categories.
 
 Inputs:
+
 cats (reference to array of arrays/hashes which encapsulates hierarchy of
       categories and subcategories).
+
 depth (current depth in hierarchy of categories and sub-categories - 0 indexed).
-category (current course category, for which breadcrumb trail is being generated).   
-trails (reference to array of breacrumb trails for each category).
+
+category (current course category, for which breadcrumb trail is being generated).
+
+trails (reference to array of breadcrumb trails for each category).
+
 allitems (reference to hash - key is category key
          (format: escaped(name):escaped(parent category):depth in hierarchy).
+
 parents (array containing containers directories for current category, 
          back to top level). 
 
@@ -8388,8 +8404,6 @@
 
 Side effects: populates trails and allitems hash references
 
-=back
-
 =cut
 
 sub recurse_categories {
@@ -8420,6 +8434,132 @@
     return;
 }
 
+=pod
+
+=item *&assign_categories_table()
+
+Create a datatable for display of hierarchical categories in a domain,
+with checkboxes to allow a course to be categorized. 
+
+Inputs:
+
+cathash - reference to hash of categories defined for the domain (from
+          configuration.db)
+
+currcat - scalar with an & separated list of categories assigned to a course. 
+
+Returns: $output (markup to be displayed) 
+
+=cut
+
+sub assign_categories_table {
+    my ($cathash,$currcat) = @_;
+    my $output;
+    if (ref($cathash) eq 'HASH') {
+        my (@cats,@trails,%allitems,%idx,@jsarray,@path,$maxdepth);
+        &extract_categories($cathash,\@cats,\@trails,\%allitems,\%idx,\@jsarray);
+        $maxdepth = scalar(@cats);
+        if (@cats > 0) {
+            my $itemcount = 0;
+            if (ref($cats[0]) eq 'ARRAY') {
+                $output = &Apache::loncommon::start_data_table();
+                my @currcategories;
+                if ($currcat ne '') {
+                    @currcategories = split('&',$currcat);
+                }
+                for (my $i=0; $i<@{$cats[0]}; $i++) {
+                    my $parent = $cats[0][$i];
+                    my $css_class = $itemcount%2?' class="LC_odd_row"':'';
+                    next if ($parent eq 'instcode');
+                    my $item = &escape($parent).'::0';
+                    my $checked = '';
+                    if (@currcategories > 0) {
+                        if (grep(/^\Q$item\E$/,@currcategories)) {
+                            $checked = ' checked="checked" ';
+                        }
+                    }
+                    $output .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
+                               .'<input type="checkbox" name="usecategory" value="'.
+                               $item.'"'.$checked.' />'.&escape($parent).'</span></td>';
+                    my $depth = 1;
+                    push(@path,$parent);
+                    $output .= &assign_category_rows($itemcount,\@cats,$depth,$parent,\@path,\@currcategories);
+                    pop(@path);
+                    $output .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
+                    $itemcount ++;
+                }
+                $output .= &Apache::loncommon::end_data_table();
+            }
+        }
+    }
+    return $output;
+}
+
+=pod
+
+=item *&assign_category_rows()
+
+Create a datatable row for display of nested categories in a domain,
+with checkboxes to allow a course to be categorized,called recursively.
+
+Inputs:
+
+itemcount - track row number for alternating colors
+
+cats - reference to array of arrays/hashes which encapsulates hierarchy of
+      categories and subcategories.
+
+depth - current depth in hierarchy of categories and sub-categories - 0 indexed.
+
+parent - parent of current category item
+
+path - Array containing all categories back up through the hierarchy from the
+       current category to the top level.
+
+currcategories - reference to array of current categories assigned to the course
+
+Returns: $output (markup to be displayed).
+
+=cut
+
+sub assign_category_rows {
+    my ($itemcount,$cats,$depth,$parent,$path,$currcategories) = @_;
+    my ($text,$name,$item,$chgstr);
+    if (ref($cats) eq 'ARRAY') {
+        my $maxdepth = scalar(@{$cats});
+        if (ref($cats->[$depth]) eq 'HASH') {
+            if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
+                my $numchildren = @{$cats->[$depth]{$parent}};
+                my $css_class = $itemcount%2?' class="LC_odd_row"':'';
+                $text .= '<td><table class="LC_datatable">';
+                for (my $j=0; $j<$numchildren; $j++) {
+                    $name = $cats->[$depth]{$parent}[$j];
+                    $item = &escape($name).':'.&escape($parent).':'.$depth;
+                    my $deeper = $depth+1;
+                    my $checked = '';
+                    if (ref($currcategories) eq 'ARRAY') {
+                        if (@{$currcategories} > 0) {
+                            if (grep(/^\Q$item\E$/,@{$currcategories})) {
+                                $checked = ' checked="checked" ';
+                            }
+                        }
+                    }
+                    $text .= '<tr><td><label><input type="checkbox" name="usecategory" value="'
+                             .$item.'"'.$checked.' />'.$name.'</label></span></td><td>';
+                    if (ref($path) eq 'ARRAY') {
+                        push(@{$path},$name);
+                        $text .= &assign_category_rows($itemcount,$cats,$deeper,$name,$path,$currcategories);
+                        pop(@{$path});
+                    }
+                    $text .= '</td></tr>';
+                }
+                $text .= '</table></td>';
+            }
+        }
+    }
+    return $text;
+}
+
 ############################################################
 ############################################################