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

raeburn raeburn@source.lon-capa.org
Tue, 08 Sep 2009 13:05:01 -0000


This is a MIME encoded message

--raeburn1252415101
Content-Type: text/plain

raeburn		Tue Sep  8 13:05:01 2009 EDT

  Modified files:              
    /loncom/interface	lonrequestcourse.pm 
  Log:
  - Add javascript functions to check section names (in LON-CAPA) have a valif format (i.e., letters and/or numbers only) and are not the reserved word - 'none'.
  - Used on enrollment page and personnel page.
  - Exclude blank entries from section list on preview page (can originate from "None" option in official sections drop-down in personnel page).
  
  
--raeburn1252415101
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20090908130501.txt"

Index: loncom/interface/lonrequestcourse.pm
diff -u loncom/interface/lonrequestcourse.pm:1.30 loncom/interface/lonrequestcourse.pm:1.31
--- loncom/interface/lonrequestcourse.pm:1.30	Mon Sep  7 06:30:49 2009
+++ loncom/interface/lonrequestcourse.pm	Tue Sep  8 13:05:00 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Request a course
 #
-# $Id: lonrequestcourse.pm,v 1.30 2009/09/07 06:30:49 raeburn Exp $
+# $Id: lonrequestcourse.pm,v 1.31 2009/09/08 13:05:00 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -527,9 +527,9 @@
                      );
         if ($env{'form.sectotal'} > 0) {
             for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
-                $extras{'sec_'.$i} = 'checkbox',
-                $extras{'secnum_'.$i} = 'text',
-                $extras{'loncapasec_'.$i} = 'text',
+                $extras{'sec_'.$i} = 'radio';
+                $extras{'secnum_'.$i} = 'text';
+                $extras{'loncapasec_'.$i} = 'text';
             }
         }
         my $crosslisttotal = $env{'form.crosslisttotal'};
@@ -800,8 +800,15 @@
     }
     if ($action eq 'new') {
         my $jsextra;
-        unless (($state eq 'review') || ($state eq 'process')) {
+        if ($state eq 'courseinfo') {
             $jsextra = "\n".&Apache::loncommon::coursebrowser_javascript($dom);
+        } elsif ($state eq 'enrollment') {
+            if (($env{'form.crstype'} eq 'official') && 
+                (&Apache::lonnet::auto_run('',$dom))) {
+                $js .= "\n".&section_check_javascript()."\n".&enrollment_lcsec_js();
+            }
+        } elsif ($state eq 'personnel') {
+            $js .= "\n".&section_check_javascript()."\n".&personnel_lcsec_js();
         }
         $r->print(&header('Request a course',$js.$jscript,$loaditems,$jsextra).$crumb);
         &print_request_form($r,$action,$state,$page,$states,$dom,$newinstcode,
@@ -849,8 +856,8 @@
                                                       other => 'Modify Request',
                                                       next => 'Cancel Request',
                                                      );
-            &display_navbuttons($r,$formname,$prev,$navtxt{'prev'},$next,$navtxt{'next'},
-                                $state,$other,$navtxt{'other'});
+            &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
+                                $navtxt{'next'},$state,$other,$navtxt{'other'});
             $r->print('</form>');
         } elsif ($state eq 'cancel') {
             my ($result,$output) = &print_cancel_request($dom,$env{'form.origcnum'});
@@ -863,10 +870,11 @@
                                                       next => 'Confirm Cancellation',
                                                      );
             if ($result eq 'ok') {
-                &display_navbuttons($r,$formname,$prev,$navtxt{'prev'},$next,
+                &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
                                     $navtxt{'next'},$state);
             } else {
-                &display_navbuttons($r,$formname,$prev,$navtxt{'prev'},undef,'',$state);
+                &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},undef,
+                                    '',$state);
             }
             $r->print('</form>');
         } elsif ($state eq 'removal') {
@@ -929,6 +937,166 @@
     return;
 }
 
+sub enrollment_lcsec_js {
+    my %alerts = &section_check_alerts();
+    my $secname = $alerts{'badsec'};
+    my $secnone = $alerts{'reserved'};
+    my $output = '
+function validateEnrollSections(formname,nextstate) {
+    var badsectotal = 0;
+    var reservedtotal = 0;
+    var secTest = "";
+';
+    for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
+        $output .= "
+    var selSec = 0;
+    for (var j=0; j<document.requestcrs.sec_".$i.".length; j++) {
+        if (document.requestcrs.sec_".$i."[j].checked) {
+            selSec = document.requestcrs.sec_".$i."[j].value;
+        }
+        if (selSec == 1) {
+            secTest = validsection(document.requestcrs.loncapasec_".$i.");
+            if (secTest == 'badsec') {
+                badsectotal++;
+            }
+            if (secTest == 'reserved') {
+                reservedtotal++;
+            }
+        }
+    }
+";
+    }
+    for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
+        $output .= "
+    if (document.requestcrs.crosslist_".$i.".checked) {
+        secTest = validsection(document.requestcrs.crosslist_".$i."_lcsec);
+        if (secTest == 'badsec') {
+            badsectotal++;
+        }
+        if (secTest == 'reserved') {
+            reservedtotal++;
+        }
+    }
+";
+    }
+    $output .= "
+    if (badsectotal>0) {
+        alert('$secname');
+        return false;
+    }
+    if (reservedtotal>0) {
+        alert('$secnone');
+        return false;
+    }
+    formname.state.value= nextstate;
+    formname.submit();
+    return;
+}
+";
+    return $output;
+}
+
+sub personnel_lcsec_js {
+    my %alerts = &section_check_alerts();
+    my $secname = $alerts{'badsec'}.'\\n'.$alerts{'separate'};
+    my $secnone = $alerts{'reserved'};
+    my $output = '
+function validatePersonnelSections(formname,nextstate) {
+    var badsectotal = 0;
+    var reservedtotal = 0;
+    var secTest = "";
+';
+    for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
+        $output .= "
+    if (document.requestcrs.person_".$i."_uname.value != '') {
+        secTest = validsection(document.requestcrs.person_".$i."_newsec,'1');
+        if (secTest == 'badsec') {
+            badsectotal++; 
+        }
+        if (secTest == 'reserved') {
+            reservedtotal++;
+        }
+    }
+";
+    }
+    $output .= "
+    if (badsectotal > 0) {
+        alert('$secname');
+        return false;
+    } else {
+        if (reservedtotal > 0) {
+            alert('$secnone');
+            return false;
+        }
+    }
+    formname.state.value = nextstate;
+    formname.submit();
+    return;
+}
+";
+    return $output;
+}
+
+sub section_check_alerts {
+    my %lt = 
+        &Apache::lonlocal::texthash(
+            reserved => "You need to change one or more LON-CAPA section names - none is a reserved word in the system, and may not be used.",
+            badsec => 'You need to change one or more LON-CAPA section names - names may only contain letters or numbers.',
+            separate => 'Separate multiple sections with a comma.'
+        );
+    return %lt;
+}
+
+sub section_check_javascript {
+    return <<"END";
+function validsection(field,mult) {
+    var str = field.value;
+    var badsec=0;
+    var reserved=0;
+    if (window.RegExp) {
+        var badsecnum=0;
+        var reservednum=0;
+        var pattern=/[^a-zA-Z0-9]/; 
+        str = str.replace(/(^\\s*)|(\\s*\$)/gi,"");
+        str = str.replace(/[ ]{2,}/gi," ");
+        if (mult == '1') {
+            var sections = new Array();
+            sections = str.split(/\\s*[\\s,;:]\\s*/);
+            var i;
+            for (i=0; i<sections.length; i++) {
+                if ((sections[i] != '') && (sections[i] != undefined) && (sections[i] != null)) {
+                    if (pattern.test(sections[i])) {
+                        badsecnum++;
+                    } else {
+                        if (sections[i] == 'none') {
+                            reservednum++;
+                        }
+                    }
+                }
+            }
+        } else {
+            if ((str != '') && (str != undefined) && (str != null)) {
+                if (pattern.test(str)) {
+                    badsecnum++;
+                } else {
+                    if (str == 'none') {
+                        reservednum++;
+                    }
+                }
+            }
+        }
+        if (badsecnum > 0) {
+            return 'badsec';
+        }
+        if (reservednum > 0) {
+            return 'reserved';
+        }
+    }
+    return;
+}
+END
+}
+
 sub close_popup_form {
     my $close= &mt('Close Window');
     return << "END";
@@ -1168,7 +1336,8 @@
         push(@excluded,'crosslisttotal');
     }
     $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</form>');
-    &display_navbuttons($r,$formname,$prev,$navtxt{'prev'},$next,$navtxt{'next'},$state);
+    &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
+                        $navtxt{'next'},$state);
     return;
 }
 
@@ -1365,14 +1534,18 @@
                     &Apache::loncommon::end_data_table_row();
         for (my $i=0; $i<@sections; $i++) {
             my $colflag = $i%2;
-            my $checked = ' checked="checked"';
+            my $secon = ' checked="checked"';
+            my $secoff = '';
             if ($env{'form.origcnum'}) {
-                $checked='';
+                $secoff = $secon;
+                $secon='';
             }
             $output .= &Apache::loncommon::start_data_table_row().
-                       '<td><input type="checkbox" name="sec_'.$i.
-                       '"'.$checked.' value="1" /></td>'.
-                       '<td>'.$sections[$i].
+                       '<td><label><input type="radio" name="sec_'.$i.
+                       '"'.$secon.' value="1" />'.&mt('Yes').'</label>'.
+                       ('&nbsp;'x2).'<label><input type="radio" name="sec_'.$i.
+                        '"'.$secoff.' value="0" />'.&mt('No').'</label></td>'.
+                       '<td align="center">'.$sections[$i].
                        '<input type="hidden" name="secnum_'.$i.'" value="'.
                        $sections[$i].'" /></td>'.
                        '<td><input type="text" size="10" name="loncapasec_'.$i.
@@ -1526,10 +1699,12 @@
     my @currsecs;
     if ($env{'form.sectotal'}) {
         for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
-            if (defined($env{'form.loncapasec_'.$i})) {
-                my $lcsec = $env{'form.loncapasec_'.$i};
-                unless (grep(/^\Q$lcsec\E$/,@currsecs)) {
-                    push(@currsecs,$lcsec);
+            if ($env{'form.sec_'.$i}) { 
+                if (defined($env{'form.loncapasec_'.$i})) {
+                    my $lcsec = $env{'form.loncapasec_'.$i};
+                    unless (grep(/^\Q$lcsec\E$/,@currsecs)) {
+                        push(@currsecs,$lcsec);
+                    }
                 }
             }
         }
@@ -1933,14 +2108,22 @@
             if (ref($disallowed) eq 'ARRAY') {
                 next if (grep(/^$i$/,@{$disallowed}));
             } 
-            my @allsecs = &Apache::loncommon::get_env_multiple('form.person_'.$i.'_sec');
+            my @officialsecs = &Apache::loncommon::get_env_multiple('form.person_'.$i.'_sec');
+            my @allsecs;
+            foreach my $sec (@officialsecs) {
+                next unless ($sec =~ /\w/);
+                next if ($sec =~ /\W/);
+                next if ($sec eq 'none');
+                push(@allsecs,$sec);
+            }
             my $newsec = $env{'form.person_'.$i.'_newsec'};
             $newsec =~ s/^\s+//;
             $newsec =~s/\s+$//;
-            my @newsecs = split(/[\s,;]+/,$newsec);
+            my @newsecs = split(/\s*[\s,;:]\s*/,$newsec);
             foreach my $sec (@newsecs) {
+                next unless ($sec =~ /\w/); 
                 next if ($sec =~ /\W/);
-                next if ($newsec eq 'none');
+                next if ($sec eq 'none');
                 if ($sec ne '') {
                     unless (grep(/^\Q$sec\E$/,@allsecs)) {
                         push(@allsecs,$sec); 
@@ -2242,11 +2425,11 @@
 }
 
 sub display_navbuttons {
-    my ($r,$formname,$prev,$prevtext,$next,$nexttext,$state,$other,$othertext) = @_;
+    my ($r,$dom,$formname,$prev,$prevtext,$next,$nexttext,$state,$other,$othertext) = @_;
     $r->print('<div class="LC_navbuttons">');
     if ($prev) {
         $r->print('<input type="button" name="previous" value = "'.$prevtext.'" '.
-                  'onclick="javascript:backPage(document.'.$formname.','."'".$prev."'".')"/>'.
+                  'onclick="javascript:backPage('."document.$formname,'$prev'".')"/>'.
                   ('&nbsp;'x3));
     } elsif ($prevtext) {
         $r->print('<input type="button" name="previous" value = "'.$prevtext.'" '.
@@ -2254,16 +2437,34 @@
     }
     if ($state eq 'details') {
         $r->print(' <input type="button" name="other" value="'.$othertext.'" '.
-                  'onclick="javascript:nextPage(document.'.$formname.','."'".$other."'".
+                  'onclick="javascript:nextPage('."document.$formname,'$other'".
                   ')" />');
     }
+    my $gotnext;
     if ($state eq 'courseinfo') {
         $r->print('<input type="button" name="next" value="'.$nexttext.'" '.
                   'onclick="javascript:validateForm();" />');
-    } elsif ($next) {
-        $r->print('
-      <input type="button" name="next" value="'.$nexttext.'" '.
-      'onclick="javascript:nextPage(document.'.$formname.','."'".$next."'".')" />');
+        $gotnext = 1;
+    } elsif ($state eq 'enrollment') {
+        if (($env{'form.crstype'} eq 'official') && 
+            (&Apache::lonnet::auto_run('',$dom))) {
+            $r->print('<input type="button" name="next" value="'.$nexttext.'" '.
+                      'onclick="javascript:validateEnrollSections('."document.$formname,'$next'".');" />');
+                $gotnext = 1;
+        }
+    } elsif ($state eq 'personnel') {
+        if ($env{'form.persontotal'} > 0) { 
+            $r->print('<input type="button" name="next" value="'.$nexttext.'" '.
+                      'onclick="javascript:validatePersonnelSections('."document.$formname,'$next'".');" />');
+            $gotnext = 1;
+        }
+    }
+    unless ($gotnext) {
+        if ($next) {
+            $r->print('
+                      <input type="button" name="next" value="'.$nexttext.'" '.
+      'onclick="javascript:nextPage('."document.$formname,'$next'".')" />');
+        }
     }
     $r->print('</div>');
 }
@@ -2353,20 +2554,27 @@
                         @{$personnel{$uname.':'.$udom}{$role}{'usec'}} = ();
                     } else {
                         my @currsec = &Apache::loncommon::get_env_multiple('form.person_'.$i.'_sec');
+                        my @allsecs;
+                        foreach my $sec (@currsec) {
+                            next unless ($sec =~ /\w/);
+                            next if ($sec =~ /\W/);
+                            next if ($sec eq 'none');
+                            push(@allsecs,$sec);
+                        }
                         my $newsec = $env{'form.person_'.$i.'_newsec'};
                         $newsec =~ s/^\s+//;
                         $newsec =~s/\s+$//;
                         my @newsecs = split(/[\s,;]+/,$newsec);
                         foreach my $sec (@newsecs) {
                             next if ($sec =~ /\W/);
-                            next if ($newsec eq 'none');
+                            next if ($sec eq 'none');
                             if ($sec ne '') {
-                                unless (grep(/^\Q$sec\E$/,@currsec)) {
-                                    push(@currsec,$sec);
+                                unless (grep(/^\Q$sec\E$/,@allsecs)) {
+                                    push(@allsecs,$sec);
                                 }
                             }
                         }
-                        @{$personnel{$uname.':'.$udom}{$role}{'usec'}} = @currsec;
+                        @{$personnel{$uname.':'.$udom}{$role}{'usec'}} = @allsecs;
                     }
                 }
             } else {

--raeburn1252415101--