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

raeburn raeburn at source.lon-capa.org
Mon Sep 17 22:18:44 EDT 2018


raeburn		Tue Sep 18 02:18:44 2018 EDT

  Modified files:              
    /loncom/interface	coursecatalog.pm loncommon.pm 
  Log:
  - Ensure catalog_maxdepth form element has an appropriate (integer) value.
  
  
-------------- next part --------------
Index: loncom/interface/coursecatalog.pm
diff -u loncom/interface/coursecatalog.pm:1.95 loncom/interface/coursecatalog.pm:1.96
--- loncom/interface/coursecatalog.pm:1.95	Thu Feb  1 04:51:02 2018
+++ loncom/interface/coursecatalog.pm	Tue Sep 18 02:18:43 2018
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler for displaying the course catalog interface
 #
-# $Id: coursecatalog.pm,v 1.95 2018/02/01 04:51:02 raeburn Exp $
+# $Id: coursecatalog.pm,v 1.96 2018/09/18 02:18:43 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -225,21 +225,31 @@
         $r->print('<br />'.&Apache::loncommon::end_page());
         return OK;
     } else {
-        if ($env{'form.coursenum'}) {
-            $cnum = $env{'form.coursenum'};
+        if ($env{'form.coursenum'} ne '') {
+            if ($env{'form.coursenum'} =~ /^$LONCAPA::match_courseid$/) {
+                $cnum = $env{'form.coursenum'};
+            } else {
+                delete($env{'form.coursenum'});
+            }
         }
     }
 
-    if ($env{'form.catalog_maxdepth'} ne '') {
-        $env{'form.catalog_maxdepth'} =~ s{\D}{}g;
-    }
-
-    my (@cats, at trails,%allitems,%idx, at jsarray,%subcathash,$subcats);
+    my (@cats, at trails,%allitems,%idx, at jsarray,%subcathash,$subcats,%maxd,
+        $toplevelstr,$maxdepthstr);
     if ($env{'form.withsubcats'}) {
         $subcats = \%subcathash;
     }
     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems,
-                                           \%idx,\@jsarray,$subcats);
+                                           \%idx,\@jsarray,$subcats,\%maxd);
+    if (ref($cats[0]) eq 'ARRAY') {
+        foreach my $item (@{$cats[0]}) {
+            $toplevelstr .= "'".&js_escape($item)."::0',";
+            $maxdepthstr .= "'$maxd{$item}',";
+        }
+        $toplevelstr =~ s/,$//;
+        $maxdepthstr =~ s/,$//;
+    }
+    &validate_input(\@cats,\%maxd);
     my ($numtitles, at codetitles);
     if (($env{'form.coursenum'} ne '') && ($knownuser)) {
         &course_details($r,$codedom,$formname,$domdesc,\@trails,\%allitems,\@codetitles);
@@ -249,9 +259,29 @@
         my $catjs = <<"ENDSCRIPT";
 
 function setCatDepth(depth) {
-    document.coursecats.catalog_maxdepth.value = depth;
-    if (depth == '') {
-        document.coursecats.currcat_0.value = '';
+    var depth = parseInt(depth);
+    if (depth !== NaN) {
+        if (depth > 0) {
+            var possmaxd = 0;
+            var toplevel = new Array($toplevelstr);
+            var maxdepths = new Array($maxdepthstr);
+            if (toplevel.length) {
+                for (var i=0; i<toplevel.length; i++) {
+                    var item = unescape(toplevel[i]);
+                    if (item == document.coursecats.currcat_0.value) {
+                        possmaxd = maxdepths[i];
+                        break;
+                    }
+                }
+            }
+            if (depth > possmaxd) {
+                depth = possmaxd;
+            }
+        }
+        document.coursecats.catalog_maxdepth.value = depth;
+    } else {
+        document.coursecats.currcat_0.value = ''; 
+        document.coursecats.catalog_maxdepth.value = '';
     }
     document.coursecats.submit();
     return;
@@ -360,6 +390,67 @@
     return OK;
 }
 
+sub validate_input {
+    my ($cats,$maxd) = @_;
+    my $currcat = '';
+    my $depth = 0;
+    if ($env{'form.catalog_maxdepth'} ne '') {
+        $env{'form.catalog_maxdepth'} =~ s{\D}{}g;
+    }
+    if ((ref($cats) eq 'ARRAY') && (ref($maxd) eq 'HASH')) {
+        if (ref($cats->[0]) eq 'ARRAY') {
+            if (@{$cats->[0]} == 1) {
+                if ($cats->[0][0] eq 'instcode') {
+                    $currcat = 'instcode::0';
+                } elsif ($cats->[0][0] eq 'communities') {
+                    $currcat = 'communities::0';
+                } elsif ($cats->[0][0] eq 'placement') {
+                    $currcat = 'placement::0';
+                } else {
+                    my $name = $cats->[0][0];
+                    $currcat = &escape($name).'::0';
+                }
+                if (exists($maxd->{$cats->[0][0]})) {
+                    if ($env{'form.catalog_maxdepth'} <= $maxd->{$cats->[0][0]}) {
+                        $depth = $env{'form.catalog_maxdepth'};
+                    } else {
+                        $depth = $maxd->{$cats->[0][0]};
+                    }
+                }
+            } elsif ((@{$cats->[0]} > 1) && ($env{'form.currcat_0'} ne '')) {
+                my ($escname) = ($env{'form.currcat_0'} =~ /^([^:]+)\:\:0$/);
+                if ($escname =~ /^instcode|communities|placement$/) {
+                    $currcat = $env{'form.currcat_0'};
+                    if (exists($maxd->{$escname})) {
+                        if ($env{'form.catalog_maxdepth'} <= $maxd->{$escname}) {
+                            $depth = $env{'form.catalog_maxdepth'};
+                        } else {
+                            $depth = $maxd->{$escname};
+                        }
+                    } else {
+                        $depth = 1;
+                    }
+                } elsif ($escname ne '') {
+                    my $name = &unescape($escname);
+                    if (grep(/^\Q$name\E$/,@{$cats->[0]})) {
+                        $currcat = $env{'form.currcat_0'};
+                        if (exists($maxd->{$name})) {
+                            if ($env{'form.catalog_maxdepth'} <= $maxd->{$name}) {
+                                $depth = $env{'form.catalog_maxdepth'};
+                            } else {
+                                $depth = $maxd->{$name};
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+    $env{'form.currcat_0'} = $currcat;
+    $env{'form.catalog_maxdepth'} = $depth;
+    return;
+}
+
 sub course_details {
     my ($r,$codedom,$formname,$domdesc,$trails,$allitems,$codetitles) = @_;
     my $output;
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.1320 loncom/interface/loncommon.pm:1.1321
--- loncom/interface/loncommon.pm:1.1320	Wed Jul  4 16:58:22 2018
+++ loncom/interface/loncommon.pm	Tue Sep 18 02:18:43 2018
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.1320 2018/07/04 16:58:22 raeburn Exp $
+# $Id: loncommon.pm,v 1.1321 2018/09/18 02:18:43 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -15245,6 +15245,8 @@
 subcats (reference to hash of arrays containing all subcategories within each 
          category, -recursive)
 
+maxd (reference to hash used to hold max depth for all top-level categories).
+
 Returns: nothing
 
 Side effects: populates trails and allitems hash references.
@@ -15252,7 +15254,7 @@
 =cut
 
 sub extract_categories {
-    my ($categories,$cats,$trails,$allitems,$idx,$jsarray,$subcats) = @_;
+    my ($categories,$cats,$trails,$allitems,$idx,$jsarray,$subcats,$maxd) = @_;
     if (ref($categories) eq 'HASH') {
         &gather_categories($categories,$cats,$idx,$jsarray);
         if (ref($cats->[0]) eq 'ARRAY') {
@@ -15280,12 +15282,15 @@
                         if (ref($subcats) eq 'HASH') {
                             push(@{$subcats->{$item}},&escape($category).':'.&escape($name).':1');
                         }
-                        &recurse_categories($cats,2,$category,$trails,$allitems,\@parents,$subcats);
+                        &recurse_categories($cats,2,$category,$trails,$allitems,\@parents,$subcats,$maxd);
                     }
                 } else {
                     if (ref($subcats) eq 'HASH') {
                         $subcats->{$item} = [];
                     }
+                    if (ref($maxd) eq 'HASH') {
+                        $maxd->{$name} = 1;
+                    }
                 }
             }
         }
@@ -15323,13 +15328,13 @@
 =cut
 
 sub recurse_categories {
-    my ($cats,$depth,$category,$trails,$allitems,$parents,$subcats) = @_;
+    my ($cats,$depth,$category,$trails,$allitems,$parents,$subcats,$maxd) = @_;
     my $shallower = $depth - 1;
     if (ref($cats->[$depth]{$category}) eq 'ARRAY') {
         for (my $k=0; $k<@{$cats->[$depth]{$category}}; $k++) {
             my $name = $cats->[$depth]{$category}[$k];
             my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
-            my $trailstr = join(' -> ',(@{$parents},$category));
+            my $trailstr = join(' » ',(@{$parents},$category));
             if ($allitems->{$item} eq '') {
                 push(@{$trails},$trailstr);
                 $allitems->{$item} = scalar(@{$trails})-1;
@@ -15350,16 +15355,21 @@
                 }
             }
             &recurse_categories($cats,$deeper,$name,$trails,$allitems,$parents,
-                                $subcats);
+                                $subcats,$maxd);
             pop(@{$parents});
         }
     } else {
         my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower;
-        my $trailstr = join(' -> ',(@{$parents},$category));
+        my $trailstr = join(' » ',(@{$parents},$category));
         if ($allitems->{$item} eq '') {
             push(@{$trails},$trailstr);
             $allitems->{$item} = scalar(@{$trails})-1;
         }
+        if (ref($maxd) eq 'HASH') {
+            if ($depth > $maxd->{$parents->[0]}) {
+                $maxd->{$parents->[0]} = $depth;
+            }
+        }
     }
     return;
 }
@@ -15391,8 +15401,8 @@
     my ($cathash,$currcat,$type,$disabled) = @_;
     my $output;
     if (ref($cathash) eq 'HASH') {
-        my (@cats, at trails,%allitems,%idx, at jsarray, at path,$maxdepth);
-        &extract_categories($cathash,\@cats,\@trails,\%allitems,\%idx,\@jsarray);
+        my (@cats, at trails,%allitems,%idx, at jsarray,%maxd, at path,$maxdepth);
+        &extract_categories($cathash,\@cats,\@trails,\%allitems,\%idx,\@jsarray,\%maxd);
         $maxdepth = scalar(@cats);
         if (@cats > 0) {
             my $itemcount = 0;


More information about the LON-CAPA-cvs mailing list