--- loncom/interface/lonrequestcourse.pm	2009/08/06 00:49:31	1.5
+++ loncom/interface/lonrequestcourse.pm	2009/08/12 02:04:38	1.9
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Request a course
 #
-# $Id: lonrequestcourse.pm,v 1.5 2009/08/06 00:49:31 raeburn Exp $
+# $Id: lonrequestcourse.pm,v 1.9 2009/08/12 02:04:38 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -55,6 +55,7 @@ use Apache::Constants qw(:common :http);
 use Apache::lonnet;
 use Apache::loncommon;
 use Apache::lonlocal;
+use Apache::loncoursequeueadmin;
 use LONCAPA qw(:DEFAULT :match);
 
 sub handler {
@@ -190,8 +191,9 @@ sub handler {
 sub header {
     my ($bodytitle,$jscript,$loaditems,$jsextra) = @_;
     if ($jscript) {
-        $jscript = '<script type="text/javascript">'.
-                   $jscript.'</script>'."\n";
+        $jscript = '<script type="text/javascript">'."\n".
+                   '// <![CDATA['."\n".
+                   $jscript."\n".'// ]]>'."\n".'</script>'."\n";
     }
     if ($loaditems) {
         $loaditems = {'add_entries' => $loaditems,};
@@ -468,7 +470,7 @@ END
               '<div>'.
               '<form name="domforcourse" method="post" action="/adm/requestcourse">'.
               &Apache::lonhtmlcommon::start_pick_box().
-              &Apache::lonhtmlcommon::row_title('Domain').
+              &Apache::lonhtmlcommon::row_title('Course Domain').
               &Apache::loncommon::select_dom_form($dom,'showdom','',1,$onchange));
     if (!$onchange) {
         $r->print('&nbsp;<input type="submit" name="godom" value="'.
@@ -535,11 +537,7 @@ END
             $jsextra = "\n".&Apache::loncommon::coursebrowser_javascript($dom);
         }
         $r->print(&header('Request a course',$js.$jscript,$loaditems,$jsextra).$crumb);
-        if ($state eq 'process') {
-            &print_request_outcome($r,$state,$dom);
-        } else {
-            &print_request_form($r,$action,$state,$page,$states,$dom);
-        }
+        &print_request_form($r,$action,$state,$page,$states,$dom);
     } elsif ($action eq 'view') {
         $r->print(&header('Manage course requests',$js.$jscript,$loaditems).$crumb);
     } elsif ($action eq 'log') {
@@ -631,13 +629,15 @@ sub print_request_form {
         $r->print(&print_enrollment_menu($formname,$instcode,$dom,\@codetitles,
                                          \%cat_titles,\%cat_order,\@code_order));
     } elsif ($state eq 'personnel') {
-        $r->print(&print_personnel_menu($dom,$formname));
+        $r->print(&print_personnel_menu($dom,$formname,$crstype));
     } elsif ($state eq 'review') {
         &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                                                  \%cat_order,\@code_order);
         $r->print(&print_review($formname,$dom,\@codetitles,\%cat_titles,\%cat_order,
                                 \@code_order));
         $navtxt{'next'} = &mt('Submit course request');
+    }  elsif ($state eq '') {
+        my $result = &print_request_outcome($dom);
     }
     my @excluded = ('counter');
     my %elements = &form_elements($dom);
@@ -796,7 +796,7 @@ sub date_setting_table {
 }
 
 sub print_personnel_menu {
-    my ($dom,$formname) = @_;
+    my ($dom,$formname,$crstype) = @_;
     my $output = '<div>'.&Apache::lonhtmlcommon::start_pick_box();
     my $persontotal = $env{'form.persontotal'};
     if (!defined($persontotal)) {
@@ -810,8 +810,12 @@ sub print_personnel_menu {
 
     my $roleoptions;
     my @roles = &Apache::lonuserutils::roles_by_context('course');
+    my $type = 'Course';
+    if ($crstype eq 'community') {
+        $type = 'Community';
+    }
     foreach my $role (@roles) {
-        my $plrole=&Apache::lonnet::plaintext($role);
+        my $plrole=&Apache::lonnet::plaintext($role,$type);
         $roleoptions .= '  <option value="'.$role.'">'.$plrole.'</option>'."\n";
     }
     my %customroles=&Apache::lonuserutils::my_custom_roles();
@@ -857,13 +861,13 @@ sub print_personnel_menu {
     for (my $i=0; $i<$persontotal; $i++) { 
         my @linkargs = map { 'person_'.$i.'_'.$_ } (@items);
         my $linkargstr = join("','",@linkargs);
-        my $userlink = &Apache::loncommon::selectuser_link($formname,@linkargs,$userlinktxt);
+        my $userlink = &Apache::loncommon::selectuser_link($formname,@linkargs,$dom,$userlinktxt);
         my $uname_form = '<input type="text" name="person_'.$i.'_uname" value=""'.
                          ' onFocus="this.blur();'.
-                         'openuserbrowser('."'$formname','$linkargstr'".');" />';
+                         'openuserbrowser('."'$formname','$linkargstr','$dom'".');" />';
         my $onchange = 'javascript:fix_domain('."'$formname','person_".$i."_dom',".
                        "'person_".$i."_hidedom'".');'.
-                       'openuserbrowser('."'$formname','$linkargstr'".');';
+                       'openuserbrowser('."'$formname','$linkargstr','$dom'".');';
         my $udom_form = &Apache::loncommon::select_dom_form($dom,'person_'.$i.'_dom','',
                                                             1,$onchange).
                         '<input type="hidden" name="person_'.$i.'_hidedom" value="'.$dom.'" />';
@@ -968,7 +972,9 @@ sub print_review {
                              '<td>'.$autoroster[$env{'form.autodrops'}].'</td>'.
                              '<td>'.&Apache::lonlocal::locallocaltime($startenroll).'</td>'.
                              '<td>'.&Apache::lonlocal::locallocaltime($endenroll).'</td>';
-            $section_values = '<td>';
+            $section_values = '<td><table class="LC_innerpickbox"><tr><th>'.
+                              &mt('Institutional section').'</th>'.
+                              '<th>'.&mt('LON-CAPA section').'</th></tr>';
             my $secinfo;
             if ($env{'form.sectotal'} > 0) {
                 for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
@@ -983,13 +989,13 @@ sub print_review {
                     }
                 }
             }
-            if ($secinfo) {
-                $section_values .= '<table class="LC_innerpickbox"><tr><th>'.
-                                   &mt('Institutional section').'</th>'.
-                                   '<th>'.&mt('LON-CAPA section').'</th></tr>'.$secinfo.
-                                   '</table>';
+            if ($secinfo eq '') {
+                $secinfo = '<tr><td colspan="2">'.&mt('None').'</td></tr>';
             }
-            $section_values .= '</td><td>';
+            $section_values .= $secinfo.'</table></td><td>'.
+                               '<table class="LC_innerpickbox"><tr><th>'.
+                               &mt('Institutional course/section').'</th>'.
+                               '<th>'.&mt('LON-CAPA section').'</th></tr>';
             my $xlistinfo;
             if ($env{'form.crosslisttotal'}) {
                 for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
@@ -1012,13 +1018,10 @@ sub print_review {
                     }
                 }
             }
-            if ($xlistinfo) {
-                $section_values .= '<table class="LC_innerpickbox"><tr><th>'.
-                                   &mt('Institutional course/section').'</th>'.
-                                   '<th>'.&mt('LON-CAPA section').'</th></tr>'.$xlistinfo.
-                                   '</table>';
+            if ($xlistinfo eq '') {
+                $xlistinfo = '<tr><td colspan="2">'.&mt('None').'</td></tr>';
             }
-            $section_values .= '</td>';
+            $section_values .= $xlistinfo.'</table></td>';
         }
     }
 
@@ -1092,7 +1095,7 @@ sub print_review {
     my $output =  '<p>'.&mt('Review the details of the course request before submission.').'</p>'.  
                   '<div>'.&Apache::lonhtmlcommon::start_pick_box().
                   &Apache::lonhtmlcommon::row_title(&mt('Owner')).
-                  '<table class="LC_innerpickbox">'.
+                  '<table class="LC_innerpickbox"><tr>'.
                   '<th>'.&mt('Name').'</th>'.
                   '<th>'.&mt('Username:Domain').'</th>'.
                   '<th>'.&mt('E-mail address').'</th>'.
@@ -1120,6 +1123,8 @@ sub print_review {
                $personnel_values.'</table>'."\n".
                &Apache::lonhtmlcommon::row_closure(1).
                &Apache::lonhtmlcommon::end_pick_box();
+    my $cnum = &Apache::lonnet::generate_coursenum($dom);
+    $output .= '<input type="hidden" name="cnum" value="'.$cnum.'" />';
     return $output;
 }
 
@@ -1313,9 +1318,262 @@ sub display_navbuttons {
 }
 
 sub print_request_outcome {
+    my ($dom) = @_;
+    my $output;
+    my $cnum = $env{'form.cnum'};
+    unless ($cnum =~ /^$match_courseid$/) {
+        $output = &mt('Invalid LON-CAPA course number for the new course')."\n"; 
+        return $output;
+    }
+    my $req_notifylist;
+    my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$dom);
+    if (ref($domconfig{'requestcourses'}) eq 'HASH') {
+        if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') {
+            $req_notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
+        }
+    }
+    my $now = time;
+    my $crstype = $env{'form.crstype'};
+    my ($startenroll,$endenroll,%sections,%crosslistings,%personnel);
+    if ($crstype eq 'official') {
+        if (&Apache::lonnet::auto_run('',$dom)) {
+            ($startenroll,$endenroll)=&dates_from_form('startenroll','endenroll');
+        }
+        %sections = ();
+        %crosslistings = ();  
+    }
+    my ($startaccess,$endaccess) = &dates_from_form('startaccess','endacess');
+    my $details = {
+                    owner         => $env{'user.name'},
+                    domain        => $env{'user.domain'}, 
+                    cdom          => $dom,
+                    cnum          => $cnum, 
+                    cdesc         => $env{'form.cdesc'},
+                    crstype       => $env{'form.crstype'},
+                    instcode      => $env{'form.instcode'},
+                    clonedomain   => $env{'form.clonedomain'},
+                    clonecourse   => $env{'form.clonecourse'},
+                    datemode      => $env{'form.datemode'},
+                    dateshift     => $env{'form.datshift'},
+                    sectotal      => $env{'form.sectotal'},
+                    sections      => \%sections,
+                    crosstotal    => $env{'form.crosstotal'},
+                    crosslistings => \%crosslistings,
+                    autoadds      => $env{'form.autoadds'},
+                    autodrops     => $env{'form.autodrops'},
+                    startenroll   => $startenroll,
+                    endenroll     => $endenroll,
+                    startaccess   => $startaccess,
+                    endaccess     => $endaccess,
+                    personnel     => \%personnel
+                  };
+    my @inststatuses;
+    my $val = &get_processtype($dom,$crstype,\@inststatuses,\%domconfig);
+    if ($val eq '') {
+        if ($crstype eq 'official') {
+            $output = &mt('You are not permitted to request creation of official courses');
+        } elsif ($crstype eq 'unofficial') {
+            $output = &mt('You are not permitted to request creation of unofficial courses');
+        } elsif ($crstype eq 'community') {
+            $output = &mt('You are not permitted to request creation of communities');
+        } else {
+            $output = &mt('Unrecognized course type: [_1]',$crstype);
+        }
+    } else {
+        my ($disposition,$message);
+        my %reqhash = (
+                        crstype => $crstype,
+                        details => $details,
+                      );
+        my $requestkey = $dom.'_'.$cnum;
+        if ($val =~ /^autolimit=/) {
+            $disposition = &check_autolimit($env{'user.name'},$env{'user.domain'},
+                                            $dom,$crstype,$val,\$message);
+        } elsif ($val eq 'validate') {
+            $disposition =
+                &Apache::lonnet::auto_courserequest_validation($dom,$details,
+                                                              \@inststatuses,\$message);
+        } else {
+            $disposition = 'approval';
+        }
+        $reqhash{'status'} = $disposition;
+        if ($disposition eq 'rejected') {
+            $output = &mt('Your course request was rejected.');
+            if ($message) {
+                $output .= '<div class="LC_warning">'.$message.'</div>';
+            }
+        } elsif ($disposition eq 'process') {
+            my $type = 'Course';
+            if ($crstype eq 'community') {
+                $type = 'Community';
+            }
+            my ($logmsg,$newusermsg,$addresult,$enrollcount,$output,$keysmsg,%longroles);
+            my @roles = &Apache::lonuserutils::roles_by_context('course');
+            foreach my $role (@roles) {
+                $longroles{$role}=&Apache::lonnet::plaintext($role,$type);
+            }
+            my %reqdetails = &build_batchcreatehash($details);
+            my $cid = &LONCAPA::batchcreatecourse::build_course($dom,$cnum,'request',\%reqdetails,\%longroles,\$logmsg,\$newusermsg,\$addresult,\$enrollcount,\$output,\$keysmsg,$env{'user.domain'},$env{'user.name'},$cnum);
+            $disposition = 'created';
+            if ($cid eq $cnum) {
+                $disposition = 'created';
+                $output = &mt('Your course request has been processed and the course has been created.').'<br />'.&mt('You will need to logout and log-in again to be able to select a role in the course.');
+            } else {
+                $output = &mt('An error occurred when processing your course request.').'<br />'.&mt('You may want to review the request details and submit the request again.');
+            }
+        } else {
+            my $requestid = $cnum.'_'.$disposition;
+            my $request = { 
+                            $requestid => {
+                                            timestamp   => $now,
+                                            crstype     => $crstype,
+                                            ownername   => $env{'user.name'},
+                                            ownerdom    => $env{'user.domain'},
+                                            description => $env{'form.cdesc'}, 
+                                          },
+                          };
+            my $putresult = &Apache::lonnet::newput_dom('courserequestqueue',$request,
+                                                        $dom);
+            if ($putresult eq 'ok') {
+                my %emails = &Apache::loncommon::getemails();
+                my $address;
+                if (($emails{'permanentemail'} ne '') || ($emails{'notification'} ne '')) {
+                    $address = $emails{'permanentemail'};
+                    if ($address eq '') {
+                        $address = $emails{'notification'};
+                    }
+                }
+                $output = &mt('Your course request has been recorded.').'<br />';
+                if ($disposition eq 'approval') {
+                    $output .= &mt('Your course request has been recorded.').'<br />'.
+                              &mt('A message will be sent to your LON-CAPA account when a domain coordinator takes action on your request.').'<br />'.
+                              &mt('To access your LON-CAPA message, go to the Main Menu and click on "Send and Receive Messages".').'<br />';
+                    if ($address ne '') {     
+                        $output.= &mt('An e-mail will also be sent to: [_1] when this occurs.',$address).'<br />';
+                    }
+                    if ($req_notifylist) {
+                        my $fullname = &Apache::loncommon::plainname($env{'user.name'},
+                                                                     $env{'user.domain'});
+                        &Apache::loncoursequeueadmin::send_selfserve_notification($req_notifylist,$fullname,$now,$dom,$details);
+                    }
+                } else {
+                    $output .= '<div class="LC_info">'.
+&mt('Your request has been placed in a queue pending administrative action.').'<br />'.
+&mt("Usually this means that your institution's information systems do not list you among the instructional personnel for this course.").'<br />'.
+&mt('The list of instructional personnel for the course will be automatically checked daily, and once you are listed the request will be processed.').
+                               '</div>';
+                }
+            } else {
+                $reqhash{'status'} = 'domainerror';
+                $reqhash{'disposition'} = $disposition;
+                my $warning = &mt('An error occurred saving your request in the pending requests queue.');
+                $output = '<span class"LC_warning">'.$warning.'</span><br />';
+                
+            }
+        }
+        my $storeresult = &Apache::lonnet::store_coursereq($requestkey,\%reqhash);
+        if ($storeresult ne 'ok') {
+            $output .=  '<span class="LC_warning">'.&mt('An error occurred saving a record of the details of your request.').'</span><br />';
+            &logthis("Error saving course request - $requestkey for $env{'user.name'}:$env{'user.domain'} - $storeresult");
+        }
+    }
+    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','approve');
+    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'}) && (exists($settings{$crstype}{'_LC_adv'}))) {
+                        $val = $settings{$crstype}{'_LC_adv'};
+                        @{$inststatuses} = ('_LC_adv_');
+                    } else {
+                        if ($userenv{'inststatus'} ne '') {
+                            @{$inststatuses} = split(',',$userenv{'inststatus'});
+                        } else {
+                            @{$inststatuses} = ('other');
+                        }
+                        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'}) {
+            my @doms = split(',',$userenv{'reqcrsotherdom'});
+            my $optregex = join('|',@options); 
+            if (grep(/^\Q$dom\E:($optregex=?\d*)/,@doms)) {
+                $val = $1;
+            }
+            @{$inststatuses} = ('_external_');
+        }
+    }
+    return $val;
+}
+
+sub check_autolimit {
     return;
 }
 
+sub build_batchcreatehash {
+    my ($details) = @_;
+    my %batchhash;
+    if (ref($details) eq 'HASH') {
+
+    }
+    return %batchhash;
+}
+
 sub retrieve_settings {
     my ($dom,$request_id) = @_;
     my %reqinfo = &get_request_settings($request_id,$dom);