--- loncom/interface/lonmodifycourse.pm	2014/04/28 22:20:13	1.73
+++ loncom/interface/lonmodifycourse.pm	2015/03/07 23:17:15	1.75
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # handler for DC-only modifiable course settings
 #
-# $Id: lonmodifycourse.pm,v 1.73 2014/04/28 22:20:13 raeburn Exp $
+# $Id: lonmodifycourse.pm,v 1.75 2015/03/07 23:17:15 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -261,7 +261,8 @@ sub print_modification_menu {
         }
         $cat_text = 'View/Modify catalog settings for course';
     }
-    my $anon_text = 'Responder threshold required to display anonymous survey submissions';
+    my $anon_text = &mt('Responder threshold required to display anonymous survey submissions.');
+    my $postsubmit_text = &mt('Override defaults for submit button behavior post-submission for this specific course.'); 
 
     my %domconf = &Apache::lonnet::get_dom('configuration',['coursecategories'],$dom);
     my @additional_params = &catalog_settable($domconf{'coursecategories'},$type);
@@ -332,6 +333,14 @@ sub print_modification_menu {
                 permission => &manage_selfenrollment($cdom,$cnum,$type,$coursehash),
                 linktitle => 'Configure user self-enrollment.',
             },
+            {
+                linktext => 'View/Modify submit button behavior, post-submission',
+                icon => 'emblem-readonly.png',
+                #help => '',
+                url => &phaseurl('setpostsubmit'),
+                permission => 1,
+                linktitle => '',
+            },
         ]
         },
         );
@@ -360,7 +369,8 @@ sub print_modification_menu {
     }
     $menu_html .= '<li>'.$setquota_text.'</li>'."\n".
                   '<li>'.$setuploadquota_text.'</li>'."\n".
-                  '<li>'.$anon_text.'</li>'."\n";
+                  '<li>'.$anon_text.'</li>'."\n".
+                  '<li>'.$postsubmit_text.'</li>'."\n";
     foreach my $item (@additional_params) {
         if ($type eq 'Community') {
             if ($item eq 'togglecats') {
@@ -555,6 +565,98 @@ ENDDOCUMENT
     return;
 }
 
+sub print_postsubmit_config {
+    my ($r,$cdom,$cnum,$cdesc,$type) = @_;
+    my %lt = &Apache::lonlocal::texthash (
+                'conf' => 'Configure submit button behavior after student makes a submission',
+                'disa' => 'Disable submit button/keypress following student submission',
+                'nums' => 'Number of seconds submit is disabled',
+                'modi' => 'Save',
+                'back' => 'Pick another action',
+                'yes'  => 'Yes',
+                'no'   => 'No',
+    );
+    my %settings = &Apache::lonnet::get('environment',['internal.postsubmit','internal.postsubtimeout',
+                                                       'internal.coursecode','internal.textbook'],$cdom,$cnum);
+    my $postsubmit = $settings{'internal.postsubmit'};
+    if ($postsubmit eq '') {
+        my %domconfig =
+            &Apache::lonnet::get_dom('configuration',['coursedefaults'],$cdom);
+        $postsubmit = 1; 
+        if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
+            if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
+                if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
+                    $postsubmit = 0; 
+                }
+            }
+        }
+    }
+    my ($checkedon,$checkedoff,$display);
+    if ($postsubmit) {
+        $checkedon = 'checked="checked"';
+        $display = 'block';
+    } else {
+        $checkedoff = 'checked="checked"';
+        $display = 'none';
+    }
+    my $postsubtimeout = $settings{'internal.postsubtimeout'};
+    my $default = &domain_postsubtimeout($cdom,$type,\%settings);
+    my $zero = &mt('(Enter 0 to disable until next page reload, or leave blank to use the domain default: [_1])',$default);
+    if ($postsubtimeout eq '') {
+        $postsubtimeout = $default;
+    }
+    &print_header($r,$type);
+    my $hidden_elements = &hidden_form_elements();
+    my $helpitem = &Apache::loncommon::help_open_topic('Modify_Postsubmit_Config');
+    $r->print(<<ENDDOCUMENT);
+<form action="/adm/modifycourse" method="post" name="setpostsubmit" onsubmit="return verify_postsubmit();">
+<h3>$lt{'conf'} <span class="LC_nobreak">($cdesc)</span></h3>
+<p>
+$helpitem $lt{'disa'}: 
+<label><input type="radio" name="postsubmit" $checkedon onclick="togglePostsubmit('studentsubmission');" value="1" />
+$lt{'yes'}</label>&nbsp;&nbsp;
+<label><input type="radio" name="postsubmit" $checkedoff onclick="togglePostsubmit('studentsubmission');" value="0" />
+$lt{'no'}</label>
+<div id="studentsubmission" style="display: $display">
+$lt{'nums'} <input type="text" name="postsubtimeout" value="$postsubtimeout" /><br />
+$zero</div>
+<br />     
+<input type="submit" value="$lt{'modi'}" />
+</p>
+$hidden_elements
+<a href="javascript:changePage(document.setpostsubmit,'menu')">$lt{'back'}</a>
+</form>
+ENDDOCUMENT
+    return;
+}
+
+sub domain_postsubtimeout {
+    my ($cdom,$type,$settings) = @_;
+    return unless (ref($settings) eq 'HASH'); 
+    my $lctype = lc($type);
+    unless ($type eq 'Community') {
+        $lctype = 'unofficial';
+        if ($settings->{'internal.coursecode'}) {
+            $lctype = 'official';
+        } elsif ($settings->{'internal.textbook'}) {
+            $lctype = 'textbook';
+        }
+    }
+    my %domconfig =
+        &Apache::lonnet::get_dom('configuration',['coursedefaults'],$cdom);
+    my $postsubtimeout = 60;
+    if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
+        if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
+            if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
+                if ($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$lctype} ne '') {
+                    $postsubtimeout = $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$lctype};
+                }
+            }
+        }
+    }
+    return $postsubtimeout;
+}
+
 sub print_catsettings {
     my ($r,$cdom,$cnum,$cdesc,$type) = @_;
     &print_header($r,$type);
@@ -1543,7 +1645,83 @@ sub modify_anonsurvey_threshold {
     }
     $r->print('<p>'.
               '<a href="javascript:changePage(document.processthreshold,'."'menu'".')">'.
-              &mt('Pick another action').'</a>');
+              &mt('Pick another action').'</a></p>');
+    $r->print(&hidden_form_elements().'</form>');
+    return;
+}
+
+sub modify_postsubmit_config {
+    my ($r,$cdom,$cnum,$cdesc,$domdesc,$type) = @_;
+    &print_header($r,$type);
+    my %lt = &Apache::lonlocal::texthash(
+                subb => 'Submit button behavior after student makes a submission:',
+                unch => 'Post submission behavior of the Submit button is unchanged.',
+                erro => 'An error occurred when saving your proposed changes.',
+                inva => 'An invalid response was recorded.',
+                pick => 'Pick another action',
+             );
+    $r->print('<form action="/adm/modifycourse" method="post" name="processpostsubmit">'."\n".
+              '<h3>'.$lt{'subb'}.' <span class="LC_nobreak">('.$cdesc.')</span></h3><br />');
+    my %oldsettings = 
+        &Apache::lonnet::get('environment',['internal.postsubmit','internal.postsubtimeout','internal.coursecode','internal.textbook'],$cdom,$cnum);
+    my $postsubmit = $env{'form.postsubmit'};
+    if ($postsubmit eq '1') {
+        my $postsubtimeout = $env{'form.postsubtimeout'};
+        $postsubtimeout =~ s/[^\d\.]+//g;
+        if (($oldsettings{'internal.postsubmit'} eq $postsubmit) && ($oldsettings{'internal.postsubtimeout'} eq $postsubtimeout)) {
+            $r->print($lt{'unch'}); 
+        } else {
+            my %cenv = (
+                         'internal.postsubmit' => $postsubmit,
+                       );
+            if ($postsubtimeout eq '') {
+                my $putreply = &Apache::lonnet::put('environment',\%cenv,$cdom,$cnum);
+                if ($putreply eq 'ok') {
+                    my $defaulttimeout = &domain_postsubtimeout($cdom,$type,\%oldsettings);
+                    $r->print(&mt('The proposed duration for disabling the Submit button post-submission was blank, so the domain default of [quant,_1,second] will be used.',$defaulttimeout));
+                    if (exists($oldsettings{'internal.postsubtimeout'})) {
+                        &Apache::lonnet::del('environment',['internal.postsubtimeout'],$cdom,$cnum);
+                    }
+                } else {
+                    $r->print($lt{'erro'});
+                }
+            } else { 
+                $cenv{'internal.postsubtimeout'} = $postsubtimeout;
+                my $putreply = &Apache::lonnet::put('environment',\%cenv,$cdom,$cnum);
+                if ($putreply eq 'ok') {
+                    if ($postsubtimeout eq '0') {
+                        $r->print(&mt('Submit button will be disabled after student submission until page is reloaded.')); 
+                    } else {
+                        $r->print(&mt('Submit button will be disabled after student submission for [quant,_1,second].',$postsubtimeout));
+                    }
+                } else {
+                    $r->print($lt{'erro'});
+                }
+            }
+        }
+    } elsif ($postsubmit eq '0') {
+        if ($oldsettings{'internal.postsubmit'} eq $postsubmit) {
+            $r->print($lt{'unch'});
+        } else {
+            if (exists($oldsettings{'internal.postsubtimeout'})) {
+                &Apache::lonnet::del('environment',['internal.postsubtimeout'],$cdom,$cnum);  
+            }
+            my %cenv = (
+                         'internal.postsubmit' => $postsubmit,
+                       );
+            my $putreply = &Apache::lonnet::put('environment',\%cenv,$cdom,$cnum);
+            if ($putreply eq 'ok') {
+                $r->print(&mt('Submit buttin will not be disabled after student submission'));
+            } else {
+                $r->print($lt{'erro'});
+            }
+        }
+    } else {
+        $r->print($lt{'inva'}.' '.$lt{'unch'});
+    }
+    $r->print('<p>'.
+              '<a href="javascript:changePage(document.processpostsubmit,'."'menu'".')">'.
+              &mt('Pick another action').'</a></p>');
     $r->print(&hidden_form_elements().'</form>');
     return;
 }
@@ -1763,8 +1941,83 @@ function verify_anon_threshold() {
 }
 
 ENDSCRIPT
+    } elsif ($phase eq 'setpostsubmit') {
+        my $invalid = &mt('The choice entered for disabling the submit button is invalid.');
+        my $invalidtimeout = &mt('The timeout you entered for disabling the submit button is invalid.');
+        my $alert = &mt('Enter one of: a positive integer, 0 (for no timeout), or leave blank to use domain default');
+        my $regexp = ' /^\s*\d+\s*$/';
+
+        $js .= <<"ENDSCRIPT"; 
+
+function verify_postsubmit() {
+    var optionsElement = document.setpostsubmit.postsubmit;
+    var verified = '';
+    if (optionsElement.length) {
+        var currval;
+        for (var i=0; i<optionsElement.length; i++) {
+            if (optionsElement[i].checked) {
+               currval = optionsElement[i].value;
+            }
+        }
+        if (currval == 1) {
+            var newtimeout = document.setpostsubmit.postsubtimeout.value;
+            if (newtimeout == '') {
+                verified = 'ok';
+            } else {
+                var num_reg = $regexp;
+                if (num_reg.test(newtimeout)) {
+                    if (newtimeout>= 0) {
+                        verified = 'ok';
+                    } else {
+                        alert("$invalidtimeout\\n$alert");
+                        return false;
+                    }
+                } else {
+                    alert("$invalid\\n$alert");
+                    return false;
+                }
+            }
+        } else {
+            if (currval == 0) {
+               verified = 'ok'; 
+            } else {
+               alert('$invalid');
+               return false;
+            }
+        }
+        if (verified == 'ok') {
+            changePage(document.setpostsubmit,'processpostsubmit');
+            return true;
+        }
+    }
+    return false;
+}
+
+function togglePostsubmit(caller) {
+    var optionsElement = document.setpostsubmit.postsubmit;
+    if (document.getElementById(caller)) {
+        var divitem = document.getElementById(caller);
+        var optionsElement = document.setpostsubmit.postsubmit; 
+        if (optionsElement.length) {
+            var currval;
+            for (var i=0; i<optionsElement.length; i++) {
+                if (optionsElement[i].checked) {
+                   currval = optionsElement[i].value;
+                }
+            }
+            if (currval == 1) {
+                divitem.style.display = 'block';
+            } else {
+                divitem.style.display = 'none';
+            }
+        }
     }
+    return;
+}
 
+ENDSCRIPT
+
+    }
     my $starthash;
     if ($env{'form.phase'} eq 'ccrole') {
         $starthash = {
@@ -1772,7 +2025,7 @@ ENDSCRIPT
                      };
     } elsif ($phase eq 'courselist') {
         $starthash = {
-           add_entries => {'onload' => "hide_searching();"},
+           add_entries => {'onload' => "hide_searching(); courseSet(document.filterpicker.official, 'load');"},
                      };
     }
     $r->print(&Apache::loncommon::start_page('View/Modify Course/Community Settings',
@@ -1866,8 +2119,9 @@ sub hidden_form_elements {
       &Apache::lonhtmlcommon::echo_form_input(['gosearch','updater','coursecode',
           'prevphase','numlocalcc','courseowner','login','coursequota','intarg',
           'locarg','krbarg','krbver','counter','hidefromcat','usecategory',
-          'threshold','defaultcredits','uploadquota','selfenrollmgrdc','selfenrollmgrcc',
-          'action','state','currsec_st','sections','newsec'],['^selfenrollmgr_'])."\n".
+          'threshold','postsubmit','postsubtimeout','defaultcredits','uploadquota',
+          'selfenrollmgrdc','selfenrollmgrcc','action','state','currsec_st',
+          'sections','newsec'],['^selfenrollmgr_'])."\n".
           '<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" />';
     return $hidden_elements;
 }
@@ -1971,7 +2225,6 @@ sub handler {
                             ({href=>"javascript:changePage(document.$phase,'$phase')",
                               text=>"Threshold for anonymous submissions display"});
                             &print_set_anonsurvey_threshold($r,$cdom,$cnum,$cdesc,$type);
-
                         } elsif ($phase eq 'processthreshold') {
                             &Apache::lonhtmlcommon::add_breadcrumb
                             ({href=>"javascript:changePage(document.$phase,'setanon')",
@@ -1980,6 +2233,16 @@ sub handler {
                             ({href=>"javascript:changePage(document.$phase,'$phase')",
                               text=>"Result"});
                             &modify_anonsurvey_threshold($r,$cdom,$cnum,$cdesc,$domdesc,$type);
+                        } elsif ($phase eq 'setpostsubmit') {
+                            &Apache::lonhtmlcommon::add_breadcrumb
+                            ({href=>"javascript:changePage(document.$phase,'$phase')",
+                              text=>"Configure submit button behavior post-submission"});
+                            &print_postsubmit_config($r,$cdom,$cnum,$cdesc,$type);
+                        } elsif ($phase eq 'processpostsubmit') {
+                            &Apache::lonhtmlcommon::add_breadcrumb
+                            ({href=>"javascript:changePage(document.$phase,'$phase')",
+                              text=>"Result"});
+                            &modify_postsubmit_config($r,$cdom,$cnum,$cdesc,$domdesc,$type);
                         } elsif ($phase eq 'viewparms') {
                             &Apache::lonhtmlcommon::add_breadcrumb
                             ({href=>"javascript:changePage(document.$phase,'viewparms')",