--- loncom/lond	2009/04/11 14:47:46	1.413
+++ loncom/lond	2009/07/29 22:32:39	1.418
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # lond "LON Daemon" Server (port "LOND" 5663)
 #
-# $Id: lond,v 1.413 2009/04/11 14:47:46 raeburn Exp $
+# $Id: lond,v 1.418 2009/07/29 22:32:39 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -54,12 +54,12 @@ use LONCAPA::lonssl;
 use Fcntl qw(:flock);
 use Apache::lonnet;
 
-my $DEBUG = 1;		       # Non zero to enable debug log entries.
+my $DEBUG = 0;		       # Non zero to enable debug log entries.
 
 my $status='';
 my $lastlog='';
 
-my $VERSION='$Revision: 1.413 $'; #' stupid emacs
+my $VERSION='$Revision: 1.418 $'; #' stupid emacs
 my $remoteVERSION;
 my $currenthostid="default";
 my $currentdomainid;
@@ -3674,8 +3674,10 @@ sub put_course_id_hash_handler {
 #                            will be returned. Pre-2.2.0 legacy entries from 
 #                            nohist_courseiddump will only contain usernames.
 #                 type     - optional parameter for selection 
-#                 regexp_ok - if true, allow the supplied institutional code
-#                            filter to behave as a regular expression.  
+#                 regexp_ok - if 1 or -1 allow the supplied institutional code
+#                            filter to behave as a regular expression:
+#	                      1 will not exclude the course if the instcode matches the RE 
+#                            -1 will exclude the course if the instcode matches the RE
 #                 rtn_as_hash - whether to return the information available for
 #                            each matched item as a frozen hash of all 
 #                            key, value pairs in the item's hash, or as a 
@@ -3829,10 +3831,14 @@ sub dump_course_id_handler {
                 if (!$is_hash) {
                     $unesc_val{'inst_code'} = &unescape($val{'inst_code'});
                 }
-                if ($regexp_ok) {
+                if ($regexp_ok == 1) {
                     if (eval{$unesc_val{'inst_code'} !~ /$instcodefilter/}) {
                         $match = 0;
                     }
+                } elsif ($regexp_ok == -1) {
+                    if (eval{$unesc_val{'inst_code'} =~ /$instcodefilter/}) {
+                        $match = 0;
+                    }
                 } else {
                     if (eval{$unesc_val{'inst_code'} !~ /\Q$instcodefilter\E/i}) {
                         $match = 0;
@@ -4343,12 +4349,12 @@ sub dump_domainroles_handler {
             my ($start,$end) = split(/:/,&unescape($value));
             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,&unescape($key));
             unless ($startfilter eq '.' || !defined($startfilter)) {
-                if ($start >= $startfilter) {
+                if ((defined($start)) && ($start >= $startfilter)) {
                     $match = 0;
                 }
             }
             unless ($endfilter eq '.' || !defined($endfilter)) {
-                if ($end <= $endfilter) {
+                if ((defined($end)) && ($end <= $endfilter)) {
                     $match = 0;
                 }
             }
@@ -4641,6 +4647,33 @@ sub enrollment_enabled_handler {
 }
 &register_handler("autorun", \&enrollment_enabled_handler, 0, 1, 0);
 
+#
+#   Validate an institutional code use for a LON-CAPA course.          
+#
+# Formal Parameters:
+#   $cmd          - The command request that got us dispatched.
+#   $tail         - The tail of the command.  In this case,
+#                   this is a colon separated set of words that will be split
+#                   into:
+#                        $inst_course_id - The institutional cod3 from the
+#                                          institutions point of view.
+#                        $cdom           - The domain from the institutions
+#                                          point of view.
+#   $client       - Socket open on the client.
+# Returns:
+#    1           - Indicating processing should continue.
+#
+sub validate_instcode_handler {
+    my ($cmd, $tail, $client) = @_;
+    my $userinput = "$cmd:$tail";
+    my ($dom,$instcode,$owner) = split(/:/, $tail);
+    my $outcome=&localenroll::validate_instcode($dom,$instcode,$owner);
+    &Reply($client, \$outcome, $userinput);
+
+    return 1;
+}
+&register_handler("autovalidateinstcode", \&validate_instcode_handler, 0, 1, 0);
+
 #   Get the official sections for which auto-enrollment is possible.
 #   Since the admin people won't know about 'unofficial sections' 
 #   we cannot auto-enroll on them.
@@ -4928,6 +4961,39 @@ sub get_institutional_defaults_handler {
 &register_handler("autoinstcodedefaults",
                   \&get_institutional_defaults_handler,0,1,0);
 
+sub get_possible_instcodes_handler {
+    my ($cmd, $tail, $client)   = @_;
+    my $userinput               = "$cmd:$tail";
+
+    my $reply;
+    my $cdom = $tail;
+    my (@codetitles,%cat_titles,%cat_order,@code_order);
+    my $formatreply = &localenroll::possible_instcodes($cdom,
+                                                       \@codetitles,
+                                                       \%cat_titles,
+                                                       \%cat_order,
+                                                       \@code_order);
+    if ($formatreply eq 'ok') {
+        my $result = join('&',map {&escape($_);} (@codetitles)).':';
+        $result .= join('&',map {&escape($_);} (@code_order)).':';
+        foreach my $key (keys(%cat_titles)) {
+            $result .= &escape($key).'='.&Apache::lonnet::freeze_escape($cat_titles{$key}).'&';
+        }
+        $result =~ s/\&$//;
+        $result .= ':';
+        foreach my $key (keys(%cat_order)) {
+            $result .= &escape($key).'='.&Apache::lonnet::freeze_escape($cat_order{$key}).'&';
+        }
+        $result =~ s/\&$//;
+        &Reply($client,\$result,$userinput);
+    } else {
+        &Reply($client, "format_error\n", $userinput);
+    }
+    return 1;
+}
+&register_handler("autopossibleinstcodes",
+                  \&get_possible_instcodes_handler,0,1,0);
+
 sub get_institutional_user_rules {
     my ($cmd, $tail, $client)   = @_;
     my $userinput               = "$cmd:$tail";