--- loncom/interface/lonrequestcourse.pm	2010/01/01 23:17:31	1.42
+++ loncom/interface/lonrequestcourse.pm	2010/11/19 00:01:25	1.59
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Request a course
 #
-# $Id: lonrequestcourse.pm,v 1.42 2010/01/01 23:17:31 raeburn Exp $
+# $Id: lonrequestcourse.pm,v 1.59 2010/11/19 00:01:25 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -94,8 +94,6 @@ described at http://www.lon-capa.org.
 
 =item print_request_outcome()
 
-=item get_processtype()
-
 =item check_autolimit()
 
 =item retrieve_settings()
@@ -130,7 +128,7 @@ sub handler {
     }
 
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
-        ['action','showdom','cnum','state','crstype']);
+        ['action','showdom','cnum','state','crstype','queue']);
     &Apache::lonhtmlcommon::clear_breadcrumbs();
     my $dom = &get_course_dom();
     my $action = $env{'form.action'};
@@ -140,7 +138,7 @@ sub handler {
 
     $states{'display'} = ['details'];
     $states{'view'} = ['pick_request','details','cancel','removal'];
-    $states{'log'} = ['filter','display'];
+    $states{'log'} = ['display'];
     $states{'new'} = ['courseinfo','enrollment','personnel','review','process'];
 
     if (($action eq 'new') && ($env{'form.crstype'} eq 'official')) {
@@ -168,6 +166,7 @@ sub handler {
                  details       => 'Request Details',
                  cancel        => 'Cancel Request',
                  removal       => 'Outcome',
+                 display       => 'Request Logs',
                 );
 
     if (($env{'form.crstype'} eq 'official') && (&Apache::lonnet::auto_run('',$dom))) {
@@ -178,10 +177,10 @@ sub handler {
         &get_breadcrumbs($dom,$action,\$state,\%states,\%trail);
     if ($action eq 'display') {
         if (($dom eq $env{'request.role.domain'}) && (&Apache::lonnet::allowed('ccc',$dom))) {
-            my $namespace = 'courserequestqueue';
             if ($env{'form.cnum'} ne '') {
                 my $cnum = $env{'form.cnum'};
-                my $reqkey = $cnum.'_approval';
+                my $queue = $env{'form.queue'};
+                my $reqkey = $cnum.'_'.$queue;
                 my $namespace = 'courserequestqueue';
                 my $domconfig = &Apache::lonnet::get_domainconfiguser($dom);
                 my %queued =
@@ -277,6 +276,9 @@ sub handler {
             $jscript = &mainmenu_javascript();
         } else {
             $jscript = &Apache::lonhtmlcommon::set_form_elements($elementsref,\%stored);
+            if ($state eq 'courseinfo') {
+                $jscript .= &cloning_javascript();
+            }
         }
     }
 
@@ -316,7 +318,7 @@ sub handler {
     } elsif ($action eq 'display') {
         if ($warning ne '') {
             my $args = { only_body => 1 };
-            $r->print(&header('Course/Community Requests','','',$args).$crumb.
+            $r->print(&header('Course/Community Requests','','' ,'',$args).$crumb.
                       '<h3>'.&mt('Course/Community Request Details').'</h3>'.
                       '<div class="LC_warning">'.$warning.'</div>'.
                       &close_popup_form());
@@ -325,7 +327,24 @@ sub handler {
                                     $loaditems,$crumb,'','','','','',$uname,$udom);
         }
     } elsif ($action eq 'log') {
-        &print_request_logs($jscript,$loaditems,$crumb);
+        if ($state eq 'crstype') {
+            &print_main_menu($r,\%can_request,\%states,$dom,$jscript,'',$crumb,\%request_domains);
+        } else {
+            $jscript .= <<ENDJS;
+
+function backPage(formname,prevstate) {
+    formname.state.value = prevstate;
+    formname.submit();
+}
+
+function setPage(formname) {
+    formname.page.value = '1';
+    return;
+}
+
+ENDJS
+            &print_request_logs($r,$dom,$jscript,$loaditems,$crumb);
+        }
     } else {
         &print_main_menu($r,\%can_request,\%states,$dom,$jscript,'',$crumb,\%request_domains);
     }
@@ -356,6 +375,22 @@ function setAction(courseForm) {
 END
 }
 
+sub cloning_javascript {
+    return <<"END";
+function setCloneDisplay(courseForm) {
+    if (courseForm.cloning.length > 1) {    
+        for (var i=0; i<courseForm.cloning.length; i++) {
+            if (courseForm.cloning[i].checked) {
+                if (courseForm.cloning[i].value == 1) {
+                    document.getElementById('cloneoptions').style.display="block";;
+                }
+            }
+        }
+    }
+}
+END
+}
+
 sub get_breadcrumbs {
     my ($dom,$action,$state,$states,$trail) = @_;
     my ($crumb,$newinstcode,$codechk,$checkedcode,$numtitles,$description);
@@ -464,6 +499,7 @@ sub form_elements {
             },
             courseinfo => {
                 cdescr           => 'text',
+                cloning          => 'radio', 
                 clonecrs         => 'text',
                 clonedom         => 'selectbox',
                 datemode         => 'radio',
@@ -599,7 +635,10 @@ sub onload_action {
         if ($state eq 'crstype') {
             $loaditems{'onload'} = 'javascript:setAction(document.mainmenu_action);javascript:setType(document.mainmenu_coursetype)';
         } else {
-            $loaditems{'onload'} = 'javascript:setFormElements(document.requestcrs)';
+            $loaditems{'onload'} = 'javascript:setFormElements(document.requestcrs);';
+        }
+        if ($state eq 'courseinfo') {
+            $loaditems{'onload'} .= 'javascript:setCloneDisplay(document.requestcrs);';
         }
     }
     return \%loaditems;
@@ -608,11 +647,7 @@ sub onload_action {
 sub print_main_menu {
     my ($r,$can_request,$states,$dom,$jscript,$loaditems,$crumb,$request_domains) = @_;
     my ($types,$typename) = &Apache::loncommon::course_types();
-    my $onchange;
-    unless ($env{'form.interface'} eq 'textual') {
-        $onchange = 'this.form.submit()';
-    }
-
+    my $onchange = 'this.form.submit()';
     my $nextstate_setter = "\n";
     if (ref($states) eq 'HASH') {
         foreach my $key (keys(%{$states})) {
@@ -661,7 +696,7 @@ END
         official => 'You are not permitted to request creation of an official course in this domain.',
         unofficial => 'You are not permitted to request creation of an unofficial course in this domain.',
         community => 'You are not permitted to request creation of a community this domain.',
-        all => 'You must choose a specific course type when making a new course request.\\nAll types is not allowed.',
+        all => 'You must choose a specific course type when making a new course request.\\n\"All types\" is not allowed.',
     ); 
     $js .= <<END;
     if (crschoice == 'official') {
@@ -753,7 +788,7 @@ END
  <option value="view">'.&mt('View/Modify/Cancel pending requests').'</option>
  <option value="log">'.&mt('View request history').'</option>
 </select></form>'.
-              &Apache::lonhtmlcommon::row_closure(1).
+              &Apache::lonhtmlcommon::row_closure().
               &Apache::lonhtmlcommon::row_title(&mt('Type')).'
 <form name="mainmenu_coursetype" method="post" action="">
 <select size="1" name="crstype">');
@@ -821,7 +856,7 @@ END
     }
     if ($action eq 'new') {
         my $jsextra;
-        if ($state eq 'courseinfo') {
+        if (($state eq 'courseinfo') || ($state eq 'codepick')) {
             $jsextra = "\n".&Apache::loncommon::coursebrowser_javascript($dom);
         } elsif ($state eq 'enrollment') {
             if (($env{'form.crstype'} eq 'official') && 
@@ -874,7 +909,7 @@ END
                 $title = &mt('Pending course/community requests'); 
             }
             $r->print('<h3>'.$title.'</h3><div>'."\n".$form."\n".
-                      &print_request_status($dom).'</form></div>');
+                      &print_request_status($dom,$action).'</form></div>');
         } elsif ($state eq 'details') {
             my (@codetitles,%cat_titles,%cat_order,@code_order,$instcode,$code_chk);
             my $origcnum = $env{'form.cnum'};
@@ -996,8 +1031,6 @@ END
                   &print_review($dom,\@codetitles,\%cat_titles,\%cat_order,
                                 \@code_order,$uname,$udom)."\n".'</div>'.
                   &close_popup_form());
-    } elsif ($action eq 'log') {
-        $r->print(&coursereq_log('View request log',$jscript,$loaditems).$crumb);
     }
     $r->print(&Apache::loncommon::end_page());
     return;
@@ -1344,7 +1377,11 @@ sub print_request_form {
         if ($env{'form.origcnum'} =~ /^($match_courseid)$/) {
             $cnum = $env{'form.origcnum'};
         } else {
-            $cnum = &Apache::lonnet::generate_coursenum($dom);
+            my $gentype = 'Course';
+            if ($crstype eq 'community') {
+                $gentype = 'Community';
+            }
+            $cnum = &Apache::lonnet::generate_coursenum($dom,$gentype);
         }
         &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                                                  \%cat_order,\@code_order);
@@ -1670,10 +1707,10 @@ sub date_setting_table {
     }
 
     $table = &Apache::lonhtmlcommon::row_title(&Apache::loncommon::help_open_topic($help_item{$prefix}{'start'}).
-             '&nbsp;'.$datetitles{'start'}).$startform.
+             '&nbsp;'.&mt($datetitles{'start'})).$startform.
              &Apache::lonhtmlcommon::row_closure(1).
              &Apache::lonhtmlcommon::row_title(&Apache::loncommon::help_open_topic($help_item{$prefix}{'end'}).
-             '&nbsp;'.$datetitles{'end'}).$endform.$perpetual.
+             '&nbsp;'.&mt($datetitles{'end'})).$endform.$perpetual.
              &Apache::lonhtmlcommon::row_closure($closure);
     return $table;
 }
@@ -1820,51 +1857,93 @@ sub current_lc_sections {
     return @currsecs;
 }
 
-sub print_request_status {
-    my ($dom) = @_;
+sub sorted_request_history {
+    my ($dom,$action,$curr_req) = @_;
+    my ($after,$before,$statusfilter,$crstypefilter);
+    if ($env{'form.status'} ne '') {
+        $statusfilter = $env{'form.status'};
+    }
+    if ($env{'form.crstype'} ne '') {
+        $crstypefilter = $env{'form.crstype'};
+    }
+    if (ref($curr_req) eq 'HASH') {
+        $after = $curr_req->{'requested_after_date'},
+        $before = $curr_req->{'requested_before_date'};
+        $statusfilter = $curr_req->{'status'};
+        $crstypefilter = $curr_req->{'crstype'};
+    }
     my %statusinfo = &Apache::lonnet::dump('courserequests',$env{'user.domain'},
                                            $env{'user.name'},'^status:'.$dom);
-    my ($output,$formname,%queue_by_date);
+    my %queue_by_date;
     my ($types,$typenames) = &Apache::loncommon::course_types();
     foreach my $key (keys(%statusinfo)) {
-        if (($statusinfo{$key} eq 'approval') || ($statusinfo{$key} eq 'pending')) { 
-            (undef,my($cdom,$cnum)) = split(':',$key);
-            next if ($cdom ne $dom);   
-            my $requestkey = $cdom.'_'.$cnum;
-            if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
-                my %history = &Apache::lonnet::restore($requestkey,'courserequests',
-                                                       $env{'user.domain'},$env{'user.name'});
-                my $entry;
-                my $timestamp = $history{'reqtime'};
-                my $crstype = $history{'crstype'};
-                my $disposition = $history{'disposition'};
+        if ($action eq 'view') {
+            next unless (($statusinfo{$key} eq 'approval') || ($statusinfo{$key} eq 'pending'));
+        } else {
+            next unless (($statusfilter eq 'any') ||
+                          ($statusfilter eq $statusinfo{$key}));
+        }
+        (undef,my($cdom,$cnum)) = split(':',$key);
+        next if ($cdom ne $dom);   
+        my $requestkey = $cdom.'_'.$cnum;
+        if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
+            my %history = &Apache::lonnet::restore($requestkey,'courserequests',
+                                                   $env{'user.domain'},$env{'user.name'});
+            my $entry;
+            my $reqtime = $history{'reqtime'};
+            my $lastupdate = $history{'timestamp'};
+            my $crstype = $history{'crstype'};
+            my $disposition = $history{'disposition'};
+            my $status = $history{'status'};
+            if ($action eq 'view') {
                 next if ((exists($history{'status'})) && ($history{'status'} eq 'created'));
-                next unless (($env{'form.crstype'} eq 'any') || 
-                            ($env{'form.crstype'} eq $crstype));
+            } else {
+                next if (($reqtime < $after) || ($reqtime > $before));
+            }
+            next unless (($crstypefilter eq 'any') || 
+                         ($crstypefilter eq $crstype));
+            if ($action eq 'view') {
                 next unless (($disposition eq 'approval') || 
                              ($disposition eq 'pending'));
-                if (ref($history{'details'}) eq 'HASH') {
-                    $entry = $requestkey.':'.$crstype.':'.
-                             &escape($history{'details'}{'cdescr'});
-                    if ($crstype eq 'official') {
-                        $entry .= ':'.&escape($history{'details'}{'instcode'}); 
+            }
+            if (ref($history{'details'}) eq 'HASH') {
+                $entry = $requestkey.':'.$crstype.':'.
+                         &escape($history{'details'}{'cdescr'});
+                if ($action eq 'log') {
+                    $entry .= ':'.$lastupdate.':';
+                    if ($statusinfo{$key} ne '') {
+                        $entry .= $statusinfo{$key};
+                    } elsif ($status ne '') {
+                        $entry .= $status;
+                    } else {
+                        $entry .= $disposition;
                     }
                 }
-                if ($entry ne '') {
-                    if (exists($queue_by_date{$timestamp})) {
-                        if (ref($queue_by_date{$timestamp}) eq 'ARRAY') {
-                            push(@{$queue_by_date{$timestamp}},$entry);
-                        }
-                    } else {
-                        @{$queue_by_date{$timestamp}} = ($entry);
+                if ($crstype eq 'official') {
+                    $entry .= ':'.&escape($history{'details'}{'instcode'}); 
+                }
+            }
+            if ($entry ne '') {
+                if (exists($queue_by_date{$reqtime})) {
+                    if (ref($queue_by_date{$reqtime}) eq 'ARRAY') {
+                        push(@{$queue_by_date{$reqtime}},$entry);
                     }
+                } else {
+                    @{$queue_by_date{$reqtime}} = ($entry);
                 }
             }
         }
     }
-    $formname = 'requestcrs';
+    return %queue_by_date;
+}
+
+sub print_request_status {
+    my ($dom,$action) = @_;
+    my %queue_by_date = &sorted_request_history($dom,$action);
     my @sortedtimes = sort {$a <=> $b} (keys(%queue_by_date));
-    $output = '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
+    my $formname = 'requestcrs';
+    my ($types,$typenames) = &Apache::loncommon::course_types();
+    my $output = '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
 
               '<input type="hidden" name="state" value="'.$env{'form.state'}.'" />'."\n".
               '<input type="hidden" name="crstype" value="'.$env{'form.crstype'}.'" />'."\n".
@@ -2044,9 +2123,290 @@ ENDJS
 }
 
 sub print_request_logs {
-    my ($jscript,$loaditems,$crumb) = @_;
+    my ($r,$dom,$jscript,$loaditems,$crumb) = @_;
+    my $title;
+    if ($env{'form.crstype'} eq 'community') {
+        $title = 'Community Request Logs';
+    } elsif ($env{'form.crstype'} eq 'any') {
+        $title = 'Course/Community Request Logs';
+    } else {
+        $title = 'Course Request Logs';
+    }
+    $r->print(&header($title,$jscript,$loaditems).$crumb);
+    my $formname = 'requestcrs';
+    $r->print('<form action="/adm/requestcourse" method="post" name="'.$formname.'" onsubmit="javascript:setPage(this);">'."\n".
+              '<input type="hidden" name="action" value="log" />'."\n".
+              '<input type="hidden" name="state" value="display" />'."\n");
+    # set defaults
+    my $now = time();
+    my $defstart = $now - (7*24*3600); #7 days ago
+    my %defaults = (
+                     page                => '1',
+                     show                => '10',
+                     crstype             => 'any',
+                     status              => 'any',
+                     requested_before_date => $now,
+                     requested_after_date  => $defstart,
+                   );
+    my ($types,$typenames) = &Apache::loncommon::course_types();
+    my $more_records = 0;
+    my %curr;
+    foreach my $item ('show','page','crstype','status') {
+        $curr{$item} = $env{'form.'.$item};
+    }
+    $curr{'requested_after_date'} = &Apache::lonhtmlcommon::get_date_from_form('requested_after_date');
+    $curr{'requested_before_date'} = &Apache::lonhtmlcommon::get_date_from_form('requested_before_date');
+    foreach my $key (keys(%defaults)) {
+        if ($curr{$key} eq '') {
+            $curr{$key} = $defaults{$key};
+        }
+    }
+    my ($statuses,$statusnames) = &reqstatus_names($curr{'crstype'});
+    $r->print('<input type="hidden" name="page" value="'.$curr{'page'}.'" />'.
+              &requestlog_display_filter($formname,\%curr));
+    my %queue_by_date = &sorted_request_history($dom,$env{'form.action'},\%curr);
+    my @sortedtimes = sort {$a <=> $b} (keys(%queue_by_date));
+    my $showntablehdr = 0;
+    my $tablehdr = &Apache::loncommon::start_data_table().
+                   &Apache::loncommon::start_data_table_header_row().
+                   '<th>&nbsp;</th><th>'.&mt('Request Date').'</th>'.
+                   '<th>'.&mt('Description').'</th>';
+    if ($curr{'crstype'} eq 'any') {
+        $tablehdr .= '<th>'.&mt('Course Type').'</th>';
+    }
+    if (($curr{'crstype'} eq 'official') || ($curr{'crstype'} eq 'any')) {
+        $tablehdr .= '<th>'.&mt('Institutional Code').'</th>';
+    }
+    if ($curr{'status'} eq 'any') {
+        $tablehdr .= '<th>'.&mt('Status').'</th>';
+    } elsif ($curr{'status'} eq 'created') {
+        $tablehdr .= '<th>'.&mt('Creation Date').'</th>';
+    } elsif ($curr{'status'} eq 'cancelled') {
+        $tablehdr .= '<th>'.&mt('Cancellation Date').'</th>';
+    } elsif ($curr{'status'} eq 'rejected') {
+        $tablehdr .= '<th>'.&mt('Rejection Date').'</th>';
+    }
+    $tablehdr .= &Apache::loncommon::end_data_table_header_row();
+    my ($minshown,$maxshown);
+    $minshown = 1;
+    my $count = 0;
+    if ($curr{'show'} ne &mt('all')) {
+        $maxshown = $curr{'page'} * $curr{'show'};
+        if ($curr{'page'} > 1) {
+            $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
+        }
+    }
+    my $norecords;
+    if (@sortedtimes > 0) {
+        foreach my $item (@sortedtimes) {
+            if ($curr{'show'} ne &mt('all')) {
+                if ($count >= $curr{'page'} * $curr{'show'}) {
+                    $more_records = 1;
+                    last;
+                }
+            }
+            $count ++;
+            next if ($count < $minshown);
+            if (!$showntablehdr) {
+                $r->print($tablehdr);
+                $showntablehdr = 1;
+            }
+            my $showtime = &Apache::lonlocal::locallocaltime($item);
+            if (ref($queue_by_date{$item}) eq 'ARRAY') {
+                foreach my $request (sort(@{$queue_by_date{$item}})) {
+                    my ($key,$crstype,$desc,$timestamp,$status,$instcode) = split(':',$request);
+                    my ($cdom,$cnum) = split('_',$key);
+                    my $output = &Apache::loncommon::start_data_table_row().
+     '<td>'.$count.'</td>'.
+     '<td>'.$showtime.'</td>'.
+     '<td>'.&unescape($desc).'</td>';
+                    if ($curr{'crstype'} eq 'any') {
+                        my $typename;
+                        if (ref($typenames) eq 'HASH') {
+                            $typename = &mt($typenames->{$crstype});
+                        }
+                        if ($typename eq '') {
+                            $typename = &mt('Unknown type');
+                        }
+                        $output .= '<td>'.$typename.'</td>';
+                    }
+                    if (($curr{'crstype'} eq 'any') ||
+                        ($curr{'crstype'} eq 'official')) {
+                        my $showinstcode;
+                        if ($crstype eq 'official') {
+                            $showinstcode = &unescape($instcode);
+                        } else {
+                            $showinstcode = &mt('Not applicable');
+                        }
+                        $output .= '<td>'.$showinstcode.'</td>';
+                    }
+                    if ($curr{'status'} eq 'any') {
+                        my $statusname = &mt('Unknown status');
+                        if (ref($statusnames) eq 'HASH') {
+                            if ($statusnames->{$status} ne '') {
+                                $statusname = $statusnames->{$status};
+                            }
+                        }
+                        if (($status eq 'created') || ($status eq 'cancelled') ||
+                            ($status eq 'rejected')) {
+                            $statusname .= ' '.&Apache::lonlocal::locallocaltime($timestamp);
+                        }
+                        $output .= '<td>'.$statusname.'</td>';
+                    } elsif (($status eq 'created') || ($status eq 'cancelled') ||
+                             ($status eq 'rejected')) {
+                         $output .= '<td>'.&Apache::lonlocal::locallocaltime($timestamp).'</td>';
+                    }
+                    $output .= &Apache::loncommon::end_data_table_row();
+                    $r->print($output);
+                }
+            }
+        }
+        if ($showntablehdr) {  
+            $r->print(&Apache::loncommon::end_data_table());
+            if (($curr{'page'} > 1) || ($more_records)) {
+                $r->print('<table><tr>');
+                if ($curr{'page'} > 1) {
+                    $r->print('<td><a href="javascript:chgPage('."'previous'".');">'.&mt('Previous [_1] changes',$curr{'show'}).'</a></td>');
+                }
+                if ($more_records) {
+                    $r->print('<td><a href="javascript:chgPage('."'next'".');">'.&mt('Next [_1] changes',$curr{'show'}).'</a></td>');
+                }
+                $r->print('</tr></table>');
+                $r->print(<<"ENDSCRIPT");
+<script type="text/javascript">
+// <![CDATA[
+function chgPage(caller) {
+    if (caller == 'previous') {
+        document.$formname.page.value --;
+    }
+    if (caller == 'next') {
+        document.$formname.page.value ++;
+    }
+    document.$formname.submit();
     return;
 }
+// ]]>
+</script>
+ENDSCRIPT
+            }
+        } else {
+            $norecords = 1;
+        }
+    } else {
+        $norecords = 1;
+    }
+    if ($norecords) {
+        $r->print('<p class="LC_info">'.
+                  &mt('There are no records to display').
+                  '</p>');
+    }
+    $r->print('</form>'.
+              &Apache::loncommon::end_page());
+    return;
+}
+
+sub reqstatus_names {
+    my ($crstype) = @_;
+    my @statuses = qw(created approval pending rejected cancelled);
+    my %statusnames =
+            &Apache::lonlocal::texthash (
+                        created   => 'Created',
+                        approval  => 'Queued pending approval',
+                        pending   => 'Queued pending validation',
+                        rejected  => 'Request rejected',
+                        cancelled => 'Request cancelled',
+            );
+    if (($crstype eq 'official') || ($crstype eq 'unofficial')) {
+        $statusnames{'created'} = &mt('Course created');
+    } elsif ($crstype eq 'community') {
+        $statusnames{'created'} = &mt('Community created');
+    }
+    return (\@statuses,\%statusnames);
+}
+
+sub requestlog_display_filter {
+    my ($formname,$curr) = @_;
+    my $nolink = 1;
+    my $output = '<table><tr><td valign="top">'.
+                 '<span class="LC_nobreak"><b>'.&mt('Records/page:').'</b></span><br />'.
+                 &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
+                                              (&mt('all'),5,10,20,50,100,1000,10000)).
+                 '</td><td>&nbsp;&nbsp;</td>';
+    my $startform =
+        &Apache::lonhtmlcommon::date_setter($formname,'requested_after_date',
+                                            $curr->{'requested_after_date'},undef,
+                                            undef,undef,undef,undef,undef,undef,$nolink);
+    my $endform =
+        &Apache::lonhtmlcommon::date_setter($formname,'requested_before_date',
+                                            $curr->{'requested_before_date'},undef,
+                                            undef,undef,undef,undef,undef,undef,$nolink);
+    $output .= '<td valign="top"><b>'.&mt('Window during which course/community was requested:').'</b><br />'.
+               '<table><tr><td>'.&mt('After:').
+               '</td><td>'.$startform.'</td></tr>'.
+               '<tr><td>'.&mt('Before:').'</td>'.
+               '<td>'.$endform.'</td></tr></table>'.
+               '</td>'.
+               '<td>&nbsp;&nbsp;</td>';
+    my ($types,$typenames) = &Apache::loncommon::course_types();
+    if (ref($types) eq 'ARRAY') {
+        if (@{$types} > 1) {
+            $output .= '<td valign="top"><b>'.
+                       &mt('Course Type:').'</b><br /><select name="crstype">';
+            my $selstr = '';
+            if ($curr->{'crstype'} eq 'any') {
+                $selstr = ' selected="selected"';
+            }
+            $output .= '<option value="any"'.$selstr.'>'.&mt('All types').'</option>'."\n";
+            foreach my $crstype (@{$types}) {
+                my $selstr = '';
+                if ($curr->{'crstype'} eq $crstype) {
+                    $selstr = ' selected="selected"';
+                }
+                my $typename = $crstype;
+                if (ref($typenames) eq 'HASH') {
+                    if ($typenames->{$crstype} ne '') {
+                        $typename = $typenames->{$crstype};
+                    }
+                }
+                $output .= '<option value="'.$crstype.'"'.$selstr.'>'.$typename.'</option>'."\n";
+            }
+            $output .= '</select></td>';
+        }
+    }
+    my ($statuses,$statusnames) = &reqstatus_names($curr->{'crstype'});
+    if (ref($statuses) eq 'ARRAY') {
+        if (@{$statuses} > 1) {
+            $output .= '<td valign="top"><b>'.
+                       &mt('Request Status:').'</b><br /><select name="status">';
+            my $selstr = '';
+            if ($curr->{'status'} eq 'any') {
+                $selstr = ' selected="selected"';
+            }
+            $output .= '<option value="any"'.$selstr.'>'.&mt('Any status').'</option>'."\n";
+            foreach my $status (@{$statuses}) {
+                my $selstr = '';
+                if ($curr->{'status'} eq $status) {
+                    $selstr = ' selected="selected"';
+                }
+                my $statusname = $status;
+                if (ref($statusnames) eq 'HASH') {
+                    if ($statusnames->{$status} ne '') {
+                        $statusname = $statusnames->{$status};
+                    }
+                }
+                $output .= '<option value="'.$status.'"'.$selstr.'>'.$statusname.'</option>'."\n";
+            }
+            $output .= '</select></td>';
+        }
+    }
+    $output .= '</tr></table>';
+
+    # Update Display button
+    $output .= '<p>'.
+               '<input type="submit" value="'.&mt('Update Display').'" />'.
+               '</p><hr />';
+    return $output;
+}
 
 sub print_review {
     my ($dom,$codetitles,$cat_titles,$cat_order,$code_order,$uname,$udom,
@@ -2171,10 +2531,11 @@ sub print_review {
 
     my %ctxt = &clone_text();
     $inst_headers .= '<th>'.&mt('Clone From').'</th>';
-    if (($env{'form.clonecrs'} =~ /^$match_name$/) && 
+    if (($env{'form.cloning'}) &&
+        ($env{'form.clonecrs'} =~ /^$match_name$/) && 
         ($env{'form.clonedom'} =~ /^$match_domain$/)) {
-        my $canclone = &Apache::loncoursequeueadmin::can_clone_course($env{'user.name'},
-                           $env{'user.domain'},$env{'form.clonecrs'},$env{'form.clonedom'},
+        my $canclone = &Apache::loncoursequeueadmin::can_clone_course($uname,
+                           $udom,$env{'form.clonecrs'},$env{'form.clonedom'},
                            $env{'form.crstype'});
         if ($canclone) {
             my %courseenv = &Apache::lonnet::userenvironment($env{'form.clonedom'},
@@ -2353,6 +2714,24 @@ function validateForm() {
     }
     nextPage(document.$formname,'$next');
 }
+
+function toggleCloning() {
+    var willclone;
+    if (document.$formname.cloning.length > 1) {
+        for (var i=0; i<document.$formname.cloning.length; i++) {
+            if (document.$formname.cloning[i].checked) {
+                willclone = document.$formname.cloning[i].value;
+            }
+        }
+    }
+    if (willclone == 1) {
+        document.getElementById('cloneoptions').style.display="block";
+    } else {
+        document.getElementById('cloneoptions').style.display="none";
+        document.$formname.clonecrs.value = '';
+    }
+}
+
 // ]]
 </script>
 
@@ -2363,7 +2742,7 @@ ENDJS
         $title = &mt('Brief Community Description');
         $clonetitle = &mt('Clone content and settings from an existing community?');
     }
-    my $output .= $js_validate."\n".'<div>'.&Apache::lonhtmlcommon::start_pick_box().
+    my $output .= $js_validate."\n".&Apache::lonhtmlcommon::start_pick_box().
                   &Apache::lonhtmlcommon::row_headline().
                   '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Description').'&nbsp;'.$title.'</h3>'.
                   &Apache::lonhtmlcommon::row_closure(1).
@@ -2379,10 +2758,22 @@ ENDJS
     $output .= $home_server_pick.
                &Apache::lonhtmlcommon::row_closure().
                &Apache::lonhtmlcommon::row_headline().
-               '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Clone').'&nbsp;'.$clonetitle.'</h3>'. 
+               '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Clone').'&nbsp;'.$clonetitle.
                &Apache::lonhtmlcommon::row_closure(1).
+               &Apache::lonhtmlcommon::row_title(&mt('Clone?')).
+               '<label><input type="radio" name="cloning" value="1" '.
+               'onclick="javascript:toggleCloning()" />'.
+               &mt('Yes').('&nbsp;'x2).'</label><label>'.
+               '<input type="radio" name="cloning" value="0" checked="checked" '.
+               'onclick="javascript:toggleCloning()" />'.&mt('No').'</label>'.
+               '</h3>'.
+               &Apache::lonhtmlcommon::row_closure(1).
+               &Apache::lonhtmlcommon::row_headline().
+               '<div id="cloneoptions" style="display: none" >'.
+               &Apache::lonhtmlcommon::start_pick_box().
                &clone_form($dom,$formname,$crstype).
-               &Apache::lonhtmlcommon::end_pick_box().'</div>'."\n";
+               &Apache::lonhtmlcommon::end_pick_box().'</div>'.
+               &Apache::lonhtmlcommon::end_pick_box()."\n";
     return $output;
 }
 
@@ -2392,15 +2783,16 @@ sub clone_form {
     if ($crstype eq 'community') {
         $type = 'Community';
     }
-    my $cloneform = &Apache::loncommon::select_dom_form($dom,'clonedom').'&nbsp;'.
-                    &Apache::loncommon::selectcourse_link($formname,'clonecrs','clonedom','','','',$type);
     my %lt = &clone_text();
     my $output .= 
-        &Apache::lonhtmlcommon::row_title($lt{'cid'}).'<label>'.
-        '<input type="text" size="25" name="clonecrs" value=""  />'.
-        '</label>'.&Apache::lonhtmlcommon::row_closure(1).
         &Apache::lonhtmlcommon::row_title($lt{'dmn'}).'<label>'.
-        $cloneform.'</label>'.&Apache::lonhtmlcommon::row_closure(1).
+        &Apache::loncommon::select_dom_form($dom,'clonedom').'</label>'.
+        &Apache::lonhtmlcommon::row_closure(1).
+        &Apache::lonhtmlcommon::row_title($lt{'cid'}).'<label>'.
+        '<input type="text" size="25" name="clonecrs" value="" onfocus="this.blur();opencrsbrowser('."'$formname','clonecrs','clonedom','','','','','$type'".')" />'.
+        '</label>&nbsp;'.
+        &Apache::loncommon::selectcourse_link($formname,'clonecrs','clonedom','','','',$type).
+        &Apache::lonhtmlcommon::row_closure(1).
         &Apache::lonhtmlcommon::row_title($lt{'dsh'}).'<label>'.
         '<input type="radio" name="datemode" value="delete" /> '.$lt{'ncd'}.
         '</label><br /><label>'.
@@ -2427,7 +2819,7 @@ sub clone_text {
 sub coursecode_form {
     my ($dom,$context,$codetitles,$cat_titles,$cat_order,$num) = @_;
     my $output;
-    my %rowtitle = (
+    my %rowtitle = &Apache::lonlocal::texthash (
                     instcode  => 'Course Category',
                     crosslist => 'Cross Listed Course',
                    );
@@ -2644,6 +3036,10 @@ sub print_request_outcome {
                         push(@instsections,$sec);
                     }
                     $sections{$i}{'loncapa'} = $env{'form.loncapasec_'.$i};
+                    $sections{$i}{'loncapa'} =~ s/\W//g;
+                    if ($sections{$i}{'loncapa'} eq 'none') {
+                        $sections{$i}{'loncapa'} = '';
+                    }
                 }
             }
         }
@@ -2732,10 +3128,7 @@ sub print_request_outcome {
     }
     my $autoadds = 0;
     if ($env{'form.autoadds'}) {
-        $autodrops = $env{'form.autoadds'};
-    }
-    if ($env{'form.autoadds'}) {
-        $autodrops = $env{'form.autoadds'};
+        $autoadds = $env{'form.autoadds'};
     }
     my $instcode = '';
     if (exists($env{'form.instcode'})) {
@@ -2743,7 +3136,8 @@ sub print_request_outcome {
     }
     my $clonecrs = '';
     my $clonedom = '';
-    if (($env{'form.clonecrs'} =~ /^($match_courseid)$/) && 
+    if (($env{'form.cloning'}) &&
+        ($env{'form.clonecrs'} =~ /^($match_courseid)$/) && 
         ($env{'form.clonedom'} =~ /^($match_domain)$/)) {
         my $clonehome = &Apache::lonnet::homeserver($env{'form.clonecrs'},
                                                     $env{'form.clonedom'});
@@ -2784,7 +3178,9 @@ sub print_request_outcome {
                     personnel      => \%personnel,
                   };
     my (@inststatuses,$storeresult,$creationresult);
-    my $val = &get_processtype($dom,$crstype,\@inststatuses,\%domconfig);
+    my $val = 
+        &Apache::loncoursequeueadmin::get_processtype($env{'user.name'},$env{'user.domain'},
+            $env{'user.adv'},$dom,$crstype,\@inststatuses,\%domconfig);
     if ($val eq '') {
         if ($crstype eq 'official') {
             $output = &mt('You are not permitted to request creation of official courses.');
@@ -2900,6 +3296,9 @@ sub print_request_outcome {
                                             description => $env{'form.cdescr'}, 
                                           },
                           };
+            if ($crstype eq 'official') {
+                $request->{$requestid}->{'instcode'} = $instcode;
+            }
             my $statuskey = 'status:'.$dom.':'.$cnum;
             my %userreqhash = &Apache::lonnet::get('courserequests',[$statuskey],
                                                    $env{'user.domain'},$env{'user.name'});
@@ -2934,26 +3333,9 @@ sub print_request_outcome {
                 }
             }
         }
-        my ($statusresult);
-        if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
-            $storeresult = &Apache::lonnet::store_userdata(\%reqhash,$requestkey,
-                                                           'courserequests');
-            if ($storeresult eq 'ok') {
-                my %status = (
-                                 'status:'.$dom.':'.$cnum => $reqstatus,
-                             );
-                $statusresult = &Apache::lonnet::put('courserequests',\%status);
-            }
-        } else {
-            $storeresult = 'error: invalid requestkey format'; 
-        }
-        if ($storeresult ne 'ok') {
-            $output .=  '<span class="LC_warning">'.&mt('An error occurred saving a record of the details of your request: [_1].',$storeresult).'</span><br />';
-            &Apache::lonnet::logthis("Error saving course request - $requestkey for $env{'user.name'}:$env{'user.domain'} - $storeresult");
-        } elsif ($statusresult ne 'ok') {
-            $output .= '<span class="LC_warning">'.&mt('An error occurred saving a record of the status of your request: [_1].',$statusresult).'</span><br />';
-            &Apache::lonnet::logthis("Error saving course request status for  $requestkey (for $env{'user.name'}:$env{'user.domain'}) - $statusresult");
-        }
+        ($storeresult,my $updateresult) = 
+            &Apache::loncoursequeueadmin::update_coursereq_status(\%reqhash,$dom,
+                $cnum,$reqstatus,'request');
         if ($modified && $queued && $storeresult eq 'ok') {
             if ($crstype eq 'community') {
                 $output .= '<p>'.&mt('Your community request has been updated').'</p>';
@@ -2963,7 +3345,10 @@ sub print_request_outcome {
             $output .= &notification_information($disposition,$req_notifylist,$cnum,$now);
         }
         if ($validationerror ne '') {
-            $output .= '<span class="LC_warning">'.&mt('An error occurred validating your request with institutional data sources: [_1].',$validationerror).'</p>';
+            $output .= '<p class="LC_warning">'.&mt('An error occurred validating your request with institutional data sources: [_1].',$validationerror).'</p>';
+        }
+        if ($updateresult) {
+            $output .= $updateresult;
         }
     }
     if ($creationresult ne '') {
@@ -3139,100 +3524,12 @@ sub notification_information {
                    '</div>';
     } else {
         $output .= '<div class="LC_warning">'.
-                   &mt('Your request status is: [_1].',$disposition). 
-                   '</div>'
+                   &mt('Your request status is: [_1].',$disposition).
+                   '</div>';
     }
     return $output;
 }
 
-sub get_processtype {
-    my ($dom,$crstype,$inststatuses,$domconfig) = @_;
-    return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
-    my (%userenv,%settings,$val);
-    my @options = ('autolimit','validate','approval');
-    if ($dom eq $env{'user.domain'}) {
-        %userenv = 
-            &Apache::lonnet::userenvironment($env{'user.domain'},$env{'user.name'},
-                'requestcourses.'.$crstype,'inststatus');
-        if ($userenv{'requestcourses.'.$crstype}) {
-            $val = $userenv{'requestcourses.'.$crstype};
-            @{$inststatuses} = ('_custom_');
-        } else {
-            my ($task,%alltasks);
-            if (ref($domconfig->{'requestcourses'}) eq 'HASH') {
-                %settings = %{$domconfig->{'requestcourses'}};
-                if (ref($settings{$crstype}) eq 'HASH') {
-                    if (($env{'user.adv'}) && ($settings{$crstype}{'_LC_adv'} ne '')) {
-                        $val = $settings{$crstype}{'_LC_adv'};
-                        @{$inststatuses} = ('_LC_adv_');
-                    } else {
-                        if ($userenv{'inststatus'} ne '') {
-                            @{$inststatuses} = split(',',$userenv{'inststatus'});
-                        } else {
-                            @{$inststatuses} = ('default');
-                        }
-                        foreach my $status (@{$inststatuses}) {
-                            if (exists($settings{$crstype}{$status})) {
-                                my $value = $settings{$crstype}{$status};
-                                next unless ($value); 
-                                unless (exists($alltasks{$value})) {
-                                    if (ref($alltasks{$value}) eq 'ARRAY') {
-                                        unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
-                                            push(@{$alltasks{$value}},$status); 
-                                        }
-                                    } else {
-                                        @{$alltasks{$value}} = ($status);
-                                    }
-                                }
-                            }
-                        }
-                        my $maxlimit = 0;
-                        
-                        foreach my $key (sort(keys(%alltasks))) {
-                            if ($key =~ /^autolimit=(\d*)$/) {
-                                if ($1 eq '') {
-                                    $val ='autolimit=';
-                                    last;
-                                } elsif ($1 > $maxlimit) {
-                                    $maxlimit = $1; 
-                                }
-                            }
-                        }
-                        if ($maxlimit) {
-                            $val = 'autolimit='.$maxlimit;
-                        } else {
-                            foreach my $option (@options) {
-                                if ($alltasks{$option}) {
-                                    $val = $option;
-                                    last;  
-                                }
-                            }
-                        }
-                    }
-                }
-            }
-        }
-    } else {
-        %userenv = &Apache::lonnet::userenvironment($env{'user.domain'},
-                      $env{'user.name'},'reqcrsotherdom.'.$env{'form.crstype'});
-        if ($userenv{'reqcrsotherdom.'.$crstype}) {
-            my @doms = split(',',$userenv{'reqcrsotherdom.'.$crstype});
-            my $optregex = join('|',@options);
-            foreach my $item (@doms) {
-                my ($extdom,$extopt) = split(':',$item);
-                if ($extdom eq $dom) {  
-                    if ($extopt =~ /^($optregex)(=?\d*)$/) {
-                        $val = $1.$2;
-                    }
-                    last;
-                }
-            }
-            @{$inststatuses} = ('_external_');
-        }
-    }
-    return $val;
-}
-
 sub check_autolimit {
     my ($uname,$udom,$dom,$crstype,$limit,$message) = @_;
     my %crsroles = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},
@@ -3293,18 +3590,23 @@ sub retrieve_settings {
             }
             $env{'form.clonecrs'} = $reqinfo{'clonecrs'};
             $env{'form.clonedom'} = $reqinfo{'clonedom'};
+            if (($reqinfo{'clonecrs'} ne '') && ($reqinfo{'clonedom'} ne '')) {
+                $env{'form.cloning'} = 1;
+            }
             $env{'form.datemode'} = $reqinfo{'datemode'};
             $env{'form.dateshift'} = $reqinfo{'dateshift'};
-            if (($reqinfo{'crstype'} eq 'official') && ($reqinfo{'instcode'} ne '')) { 
-                 $env{'form.sectotal'} = $reqinfo{'sectotal'};
-                 $env{'form.crosslisttotal'} = $reqinfo{'crosslisttotal'};
-                 $env{'form.autoadds'} = $reqinfo{'autoadds'};
-                 $env{'form.autdrops'} = $reqinfo{'autodrops'};
-                 $env{'form.instcode'} = $reqinfo{'instcode'};
-                 my $crscode = { 
-                                 $cnum => $reqinfo{'instcode'},
-                               };
-                 &extract_instcode($dom,'instcode',$crscode,$cnum);
+            if ($reqinfo{'crstype'} eq 'official') {
+                $env{'form.autoadds'} = $reqinfo{'autoadds'};
+                $env{'form.autodrops'} = $reqinfo{'autodrops'};
+                if ($reqinfo{'instcode'} ne '') { 
+                    $env{'form.sectotal'} = $reqinfo{'sectotal'};
+                    $env{'form.crosslisttotal'} = $reqinfo{'crosslisttotal'};
+                    $env{'form.instcode'} = $reqinfo{'instcode'};
+                    my $crscode = { 
+                                    $cnum => $reqinfo{'instcode'},
+                                  };
+                    &extract_instcode($dom,'instcode',$crscode,$cnum);
+                }
             }
             my @currsec;
             if (ref($reqinfo{'sections'}) eq 'HASH') {