[LON-CAPA-cvs] cvs: loncom(version_2_11_X) /interface coursecatalog.pm

raeburn raeburn at source.lon-capa.org
Fri Jul 19 09:18:28 EDT 2019


raeburn		Fri Jul 19 13:18:28 2019 EDT

  Modified files:              (Branch: version_2_11_X)
    /loncom/interface	coursecatalog.pm 
  Log:
  - For 2.11
    Backport 1.96
  
  
Index: loncom/interface/coursecatalog.pm
diff -u loncom/interface/coursecatalog.pm:1.88.2.3 loncom/interface/coursecatalog.pm:1.88.2.4
--- loncom/interface/coursecatalog.pm:1.88.2.3	Sun Dec 16 03:19:47 2018
+++ loncom/interface/coursecatalog.pm	Fri Jul 19 13:18:27 2019
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler for displaying the course catalog interface
 #
-# $Id: coursecatalog.pm,v 1.88.2.3 2018/12/16 03:19:47 raeburn Exp $
+# $Id: coursecatalog.pm,v 1.88.2.4 2019/07/19 13:18:27 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -207,21 +207,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);
@@ -231,9 +241,29 @@
         my $catjs = <<"ENDSCRIPT";
 
 function setCatDepth(depth) {
-    document.coursecats.catalog_maxdepth.value = depth;
-    if (depth == '') {
+    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;
@@ -340,6 +370,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;




More information about the LON-CAPA-cvs mailing list