--- loncom/interface/lonrequestcourse.pm	2010/09/10 20:25:51	1.58
+++ loncom/interface/lonrequestcourse.pm	2012/08/15 14:37:13	1.65
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Request a course
 #
-# $Id: lonrequestcourse.pm,v 1.58 2010/09/10 20:25:51 raeburn Exp $
+# $Id: lonrequestcourse.pm,v 1.65 2012/08/15 14:37:13 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -147,6 +147,12 @@ sub handler {
         }
     }
 
+    if (($action eq 'new') && (&Apache::loncoursequeueadmin::author_prompt())) {
+        if (ref($states{$action}) eq 'ARRAY') {
+            push(@{$states{$action}},'reqauthor');
+        }
+    }
+
     foreach my $key (keys(%states)) {
         if (ref($states{$key}) eq 'ARRAY') {
             unshift (@{$states{$key}},'crstype');
@@ -162,6 +168,7 @@ sub handler {
                  personnel     => 'Personnel',
                  review        => 'Review',
                  process       => 'Result',
+                 reqauthor     => 'Authoring Space Result',
                  pick_request  => 'Display Summary',
                  details       => 'Request Details',
                  cancel        => 'Cancel Request',
@@ -439,7 +446,7 @@ sub get_breadcrumbs {
                     $crumb = &Apache::lonhtmlcommon::breadcrumbs('Course/Community Requests','Course_Requests');
                     last;
                 } else {
-                    if (($$state eq 'process') || ($$state eq 'removal')) {
+                    if (($$state eq 'process') || ($$state eq 'removal') || ($$state eq 'reqauthor')) {
                         &Apache::lonhtmlcommon::add_breadcrumb(
                             { href => '/adm/requestcourse',
                               text => "$trail->{$states->{$action}[$i]}",
@@ -556,7 +563,6 @@ sub form_elements {
     }
     if (&Apache::lonnet::auto_run('',$dom)) {
         my %extras = (
-                       sectotal           => 'hidden',
                        enrollstart_month  => 'selectbox',
                        enrollstart_hour   => 'selectbox',
                        enrollend_month    => 'selectbox',
@@ -572,12 +578,30 @@ sub form_elements {
                        addcrosslist       => 'checkbox',
                        autoadds           => 'radio',
                        autodrops          => 'radio',
-                     );
-        if ($env{'form.sectotal'} > 0) {
-            for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
-                $extras{'sec_'.$i} = 'radio';
-                $extras{'secnum_'.$i} = 'text';
-                $extras{'loncapasec_'.$i} = 'text';
+        );
+        my ($instcode,$titlescount) = &get_instcode($dom);
+        if ($instcode) {
+            my @sections = &Apache::lonnet::auto_get_sections(undef,$dom,$instcode);
+            if (@sections) {
+                $extras{'sectotal'} = 'hidden';
+                if ($env{'form.sectotal'} > 0) {
+                    for (my $i=0; $i<$env{'form.sectotal'}; $i++) {
+                        $extras{'sec_'.$i} = 'radio';
+                        $extras{'secnum_'.$i} = 'text';
+                        $extras{'loncapasec_'.$i} = 'text';
+                    }
+                }
+            } else {
+                $extras{'addsection'} = 'checkbox';
+                my $sectotal = $env{'form.sectotal'};
+                if ($env{'form.addsection'}) {
+                    $sectotal ++;
+                }
+                for (my $i=0; $i<$sectotal; $i++) {
+                    $extras{'sec_'.$i} = 'checkbox';
+                    $extras{'secnum_'.$i} = 'text',
+                    $extras{'loncapasec_'.$i} = 'text',
+                }
             }
         }
         my $crosslisttotal = $env{'form.crosslisttotal'};
@@ -587,7 +611,6 @@ sub form_elements {
         if (!$crosslisttotal) {
             $crosslisttotal = 1;
         }
-
         for (my $i=0; $i<$env{'form.crosslisttotal'}; $i++) {
             if ($numtitles) {
                 $extras{'crosslist_'.$i.'_'.$lastitem} = 'text';
@@ -1322,16 +1345,38 @@ sub print_request_form {
                             }
                             if ($skipuser) {
                                 push(@disallowed,$i);
-                                $disallowmsg{$i} = &mt('[_1] was excluded because new users need be from the course domain','<tt>'.$personname.':'.$persondom.'</tt>');
+                                $disallowmsg{$i} = &mt('[_1] was excluded because new users need to be from the course domain','<tt>'.$personname.':'.$persondom.'</tt>');
                                 next;
                             }
                         }
-                        if (&get_cancreate_status($persondom,$personname,$dom)) {
-                            my ($allowed,$msg) = 
+                        my $usertype = &get_usertype($persondom,$personname,\%curr_rules,\%got_rules);
+                        if (&Apache::lonuserutils::can_create_user($dom,'requestcrs',$usertype)) {
+                            my ($allowed,$msg,$authtype,$authparam) = 
                                 &check_newuser_rules($persondom,$personname,
                                     \%alerts,\%rulematch,\%inst_results,
                                     \%curr_rules,\%got_rules);
                             if ($allowed) {
+                                my %domdefaults = &Apache::lonnet::get_domain_defaults($persondom);
+                                if ($usertype eq 'official') {
+                                    if ($authtype eq '') {
+                                        $authtype = $domdefaults{'auth_def'};
+                                        $authparam = $domdefaults{'auth_arg_def'};
+                                    }
+                                } elsif ($usertype eq 'unofficial') {
+                                    if ($authtype eq '') {
+                                        $authtype = 'internal';
+                                        $authparam = '';
+                                    }
+                                } else {
+                                    $authtype = $domdefaults{'auth_def'};
+                                    $authparam = $domdefaults{'auth_arg_def'};
+                                }
+                                if (($authtype eq '') ||
+                                    (($authtype =~/^krb/) && ($authparam eq ''))) {
+                                    push(@disallowed,$i);
+                                    $disallowmsg{$i} = &mt('[_1] was excluded because institutional information is incomplete for this new user.','<tt>'.$personname.':'.$persondom.'</tt>');
+                                    next;
+                                }
                                 if (ref($inst_results{$personname.':'.$persondom}) eq 'HASH') {
                                     if ($inst_results{$personname.':'.$persondom}{'lastname'} ne '') {
                                         $env{'form.person_'.$i.'_lastname'} = $inst_results{$personname.':'.$persondom}{'lastname'};
@@ -1406,13 +1451,71 @@ sub print_request_form {
                                                            \@code_order);
         $r->print($result);
         if (($storeresult eq 'ok') || ($storeresult eq 'created')) {
-            $r->print('<p>');
             if ($storeresult eq 'ok') {
-                $r->print('<a href="/adm/requestcourse?action=view&state=details&showdom='.$dom.'&cnum='. $env{'form.cnum'}.'">'.
-                          &mt('Modify this request').'</a>'.('&nbsp;'x4));
+                $r->print('<p><a href="/adm/requestcourse?action=view&state=details&showdom='.$dom.'&cnum='. $env{'form.cnum'}.'">'.
+                          &mt('Modify this request').'</a>'.('&nbsp;'x4).
+                          '<a href="/adm/requestcourse">'.&mt('Make another request').'</a></p>');
+            }
+            if (&Apache::loncoursequeueadmin::author_prompt()) {
+                $r->print('<h3>'.&mt('Access to authoring space').'</h3>'.
+                          '<p>'.
+                          &mt('Although assessment items can be created directly inside a course, such items only use part of the assessment capabilities of LON-CAPA.').
+                          '<br />'.
+                          &mt('By contrast, items created in authoring space, then imported into a course, can use all of the features of the assessment engine.').'</p>'.
+                          '<p>'.&mt('Request authoring space access now?').
+                          '<span class="LC_nobreak">&nbsp;'.
+                          '<label><input type="radio" name="requestauthor" value="1" />'.&mt('Yes').'</label>'.
+                          ('&nbsp;'x2).
+                          '<label><input type="radio" name="requestauthor" value="0" checked="checked"/>'.&mt('No').'</label>'.
+                          '</span></p>'.
+                          '<input type="submit" name="newauthor" value="'.&mt('Submit authoring request').'" />'.
+                          '<input type="hidden" name="state" value="reqauthor" />'.
+                          '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
+                          '<input type="hidden" name="cnum" value="'.$env{'form.cnum'}.'" />'.
+                          '<input type="hidden" name="showdom" value="'.$env{'form.showdom'}.'" />'.
+                          '<input type="hidden" name="crstype" value="'.$env{'form.crstype'}.'" />'.
+                          '<input type="hidden" name="disposition" value="'.$storeresult.'" />'.
+                          '<br />');
+            } elsif ($storeresult eq 'created') {
+                $r->print('<p><a href="/adm/requestcourse">'.&mt('Make another request').'</a></p>');
+            }
+        }
+    } elsif ($state eq 'reqauthor') {
+        my ($result,@links); 
+        if ($env{'form.requestauthor'}) {
+            $r->print(&Apache::loncoursequeueadmin::process_reqauthor(\$result));
+            if ($result eq 'created') {
+                my $role = 'au';
+                my $spec = "$role./$env{'form.showdom'}/";
+                push(@links,&mt('Enter your authoring space with role: [_1]',
+                                '<a href="/adm/roles?selectrole=1&amp;'.$spec.'=1">'.
+                                &Apache::lonnet::plaintext($role).'</a>'));
             }
-            $r->print('<a href="/adm/requestcourse">'.&mt('Make another request').'</a></p>');
-            return;
+        }
+        if (($env{'form.disposition'} eq 'created') &&
+            ($env{'form.cnum'} =~ /^$match_courseid$/) &&
+            ($env{'form.showdom'} =~ /^$match_domain$/)) {
+            my ($spec,$area,$role,$type);
+            my $role = 'cc';
+            my $spec = "$role./$env{'form.showdom'}/$env{'form.cnum'}";
+            my $type = 'Course';
+            if ($env{'form.crstype'} eq 'community') {
+                $type = 'Community';
+            }
+            my $showrole = &Apache::lonnet::plaintext($role,$type);
+            unshift(@links,&mt('Enter new course with role: [_1]',
+                               '<a href="/adm/roles?selectrole=1&amp;'.$spec.'=1">'.$showrole.'</a>'));
+        }
+        if (@links > 1) {
+            $r->print(&mt('New roles will be listed on your [_1]Roles[_2] page.',
+                          '<a href="/adm/roles">','</a>').'&nbsp'.&mt('Choose a role:').
+                       '<ul>');
+            foreach my $link (@links) {
+                $r->print('<li>'.$link.'</li>');
+            }
+            $r->print('</ul>');
+        } elsif (@links == 1) {
+            $r->print('<p>'.$links[0].'</p>');
         }
     }
     my @excluded = &get_excluded_elements($dom,$states,$action,$state);
@@ -1442,25 +1545,28 @@ sub print_request_form {
                 $count ++;
             }
             $env{'form.persontotal'} = $count;
-             
         }
     }
     if ($state eq 'enrollment') {
-        push(@excluded,'crosslisttotal');
+        push(@excluded,('sectotal','crosslisttotal'));
+    }
+    if (($state eq 'process') || ($state eq 'reqauthor')) {
+        $r->print('</form>');
+    } else {
+        $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</form>');
+        &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
+                            $navtxt{'next'},$state);
     }
-    $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</form>');
-    &display_navbuttons($r,$dom,$formname,$prev,$navtxt{'prev'},$next,
-                        $navtxt{'next'},$state);
     return;
 }
 
-sub get_cancreate_status {
-    my ($persondom,$personname,$dom) = @_;
+sub get_usertype {
+    my ($persondom,$personname,$curr_rules,$got_rules) = @_;
     my ($rules,$ruleorder) =
         &Apache::lonnet::inst_userrules($persondom,'username');
     my $usertype = &Apache::lonuserutils::check_usertype($persondom,$personname,
-                                                         $rules);
-    return &Apache::lonuserutils::can_create_user($dom,'requestcrs',$usertype);
+                                                         $rules,$curr_rules,$got_rules);
+    return $usertype;
 }
 
 sub check_newuser_rules {
@@ -1468,7 +1574,7 @@ sub check_newuser_rules {
         $got_rules) = @_;
     my $allowed = 1;
     my $newuser = 1;
-    my ($checkhash,$userchkmsg);
+    my ($checkhash,$userchkmsg,$authtype,$authparam);
     my $checks = { 'username' => 1 };
     $checkhash->{$personname.':'.$persondom} = { 'newuser' => $newuser };
     &Apache::loncommon::user_rule_check($checkhash,$checks,$alerts,$rulematch,
@@ -1490,7 +1596,22 @@ sub check_newuser_rules {
             }
         }
     }
-    return ($allowed,$userchkmsg);
+    if ($allowed) {
+        if (ref($rulematch) eq 'HASH') {
+            if (ref($rulematch->{$personname.':'.$persondom}) eq 'HASH') {
+                my $matchedrule = $rulematch->{$personname.':'.$persondom}{'username'};
+                my ($rules,$ruleorder) =
+                    &Apache::lonnet::inst_userrules($persondom,'username');
+                if (ref($rules) eq 'HASH') {
+                    if (ref($rules->{$matchedrule}) eq 'HASH') {
+                        $authtype = $rules->{$matchedrule}{'authtype'};
+                        $authparam = $rules->{$matchedrule}{'authparm'};
+                    }
+                }
+            }
+        }
+    }
+    return ($allowed,$userchkmsg,$authtype,$authparam);
 }
 
 sub get_excluded_elements {
@@ -1540,7 +1661,29 @@ sub print_enrollment_menu {
         if (&Apache::lonnet::auto_run('',$dom)) {
             $output = &show_invalid_crosslists($invalidcrosslist);
             my ($section_form,$crosslist_form);
-            $section_form = &inst_section_selector($dom,$instcode);
+            if ($instcode ne '') {
+                $section_form = &inst_section_selector($dom,$instcode);
+                if ($section_form eq '') {
+                    my $sectotal = $env{'form.sectotal'};
+                    if (!$sectotal) {
+                        $sectotal = 1;
+                    }
+                    if ($env{'form.addsection'}) {
+                        $sectotal ++;
+                    }
+                    for (my $i=0; $i<$sectotal; $i++) {
+                        $section_form .= &sections_form($dom,$instcode,$i);
+                    }
+                    if ($section_form) {
+                        $section_form .=
+                    &Apache::lonhtmlcommon::row_title(&mt('Add another')).
+                    '<input name="sectotal" type="hidden" value="'.$sectotal.'" />'.
+                    '<input name="addsection" type="checkbox" value="'.$sectotal.'"'.
+                    ' onclick="javascript:nextPage(document.'.$formname.",'".$env{'form.state'}.
+                   "'".');" />'.&mt('Add?').&Apache::lonhtmlcommon::row_closure();
+                    }
+                }
+            }
             if ($section_form) {
                 $sections = &Apache::lonhtmlcommon::row_headline().
                             '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Sections').
@@ -1567,7 +1710,7 @@ sub print_enrollment_menu {
                     ' onclick="javascript:nextPage(document.'.$formname.",'".$env{'form.state'}.
                    "'".');" />'.&mt('Add?').&Apache::lonhtmlcommon::row_closure();
                 $sections .=  &Apache::lonhtmlcommon::row_headline.
-                              '<h3>'.&mt('Crosslisted courses for auto-enrollment').'</h3>'.
+                              '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Crosslist').'&nbsp;'.&mt('Crosslisted courses for auto-enrollment').'</h3>'.
                               &Apache::lonhtmlcommon::row_closure(1).
                               $crosslist_form;
             }
@@ -2825,7 +2968,6 @@ sub coursecode_form {
                    );
     my %helpitem = ( 
                      instcode => 'Course_Request_Category',
-                     crosslist => 'Course_Request_Crosslist',
                    );
     if ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
         (ref($cat_order))) {
@@ -2842,7 +2984,11 @@ sub coursecode_form {
             my $lastitem = pop(@{$codetitles});
             my $lastinput = '<input type="text" size="5" name="'.$sel.'_'.                                            $lastitem.'" />';
             if (@{$codetitles} > 0) {
-                $output = &Apache::lonhtmlcommon::row_title(&Apache::loncommon::help_open_topic($helpitem{$context}).'&nbsp;'.$rowtitle{$context}).
+                my $helplink;
+                if (defined($helpitem{$context})) {
+                    $helplink = &Apache::loncommon::help_open_topic($helpitem{$context}).'&nbsp;';
+                }
+                $output = &Apache::lonhtmlcommon::row_title($helplink.$rowtitle{$context}).
                           '<table><tr>';
                 if ($context eq 'crosslist') {
                     $output .= '<td>'.&mt('Include?').'<br />'.
@@ -2907,6 +3053,26 @@ sub coursecode_form {
     return $output;
 }
 
+sub sections_form {
+    my ($dom,$instcode,$num) = @_;
+    my $rowtitle;
+    if ($instcode eq '') {
+        $rowtitle = &mt('Sections');
+    } else {
+        $rowtitle = &mt('Sections of [_1]',$instcode);
+    }
+    return &Apache::lonhtmlcommon::row_title($rowtitle).
+           '<table><tr><td align="center">'.
+           '<span class="LC_nobreak">'.&mt('Include?').
+           '<input type="checkbox" name="sec_'.$num.'" value="1" /></span>'.
+           '</td><td align="center">'.&mt('Institutional section').'<br />'.
+           '<input type="text" size="10" name="secnum_'.$num.'" />'.
+           '</td><td align="center">'.&mt('LON-CAPA section').'<br />'.
+           '<input type="text" size="10" name="loncapasec_'.$num.'" />'.
+           '</td></tr></table>'.
+           &Apache::lonhtmlcommon::row_closure(1);
+}
+
 sub get_course_dom {
     my $codedom = &Apache::lonnet::default_login_domain();
     if ($env{'form.showdom'} ne '') {
@@ -3065,6 +3231,7 @@ sub print_request_outcome {
         $enrollstart = '';
         $enrollend = '';
     }
+    my (%alerts,%rulematch,%inst_results,%curr_rules,%got_rules,%disallowmsg,%skipped);
     for (my $i=0; $i<$env{'form.persontotal'}; $i++) {
         my $uname = $env{'form.person_'.$i.'_uname'};
         my $udom = $env{'form.person_'.$i.'_dom'};
@@ -3076,6 +3243,56 @@ sub print_request_outcome {
                              lastname     => $env{'form.person_'.$i.'_lastname'},
                              emailaddr    => $env{'form.person_'.$i.'_emailaddr'},
                                                    };
+                    if (&Apache::lonnet::homeserver($uname,$udom) eq 'no_host') {
+                        my $usertype = &get_usertype($udom,$uname,\%curr_rules,\%got_rules);
+                        if (&Apache::lonuserutils::can_create_user($udom,'requestcrs',$usertype)) {
+                            my ($allowed,$msg,$authtype,$authparam) =
+                                &check_newuser_rules($udom,$uname,\%alerts,\%rulematch,
+                                                     \%inst_results,\%curr_rules,\%got_rules);
+                            if ($allowed) {
+                                my %domdefaults = &Apache::lonnet::get_domain_defaults($udom);
+                                if ($usertype eq 'official') {
+                                    if ($authtype eq '') {
+                                        $authtype = $domdefaults{'auth_def'};
+                                        $authparam = $domdefaults{'auth_arg_def'};
+                                    } else {
+                                        if ($authtype eq 'loc') {
+                                            $authtype = 'localauth';
+                                        } elsif ($authtype eq 'int') {
+                                            $authtype = 'internal';
+                                        }
+                                        if ($authtype !~ /^(krb4|krb5|internal|localauth)$/) {
+                                            $authtype = $domdefaults{'auth_def'};
+                                            $authparam = $domdefaults{'auth_arg_def'};
+                                        }
+                                    }
+                                } elsif ($usertype eq 'unofficial') {
+                                    if ($authtype eq '') {
+                                        $authtype = 'internal';
+                                        $authparam = '';
+                                    }
+                                } else {
+                                    $authtype = $domdefaults{'auth_def'};
+                                    $authparam = $domdefaults{'auth_arg_def'};
+                                }
+                                if (($authtype eq '') ||
+                                    (($authtype =~/^krb(4|5)$/) && ($authparam eq '')) ||
+                                    ($authtype !~ /^(krb4|krb5|internal|localauth)$/)) {
+                                    $skipped{$uname.':'.$udom} = 1;
+                                    next;
+                                } else {
+                                    $personnel{$uname.':'.$udom}{'authtype'} = $authtype;
+                                    $personnel{$uname.':'.$udom}{'autharg'} = $authparam;
+                                }
+                            } else {
+                                $skipped{$uname.':'.$udom} = 1;
+                                next;
+                            }
+                        } else {
+                            $skipped{$uname.':'.$udom} = 1;
+                            next;
+                        }
+                    }
                 }
                 my $role = $env{'form.person_'.$i.'_role'};
                 unless ($role eq '') {
@@ -3121,6 +3338,11 @@ sub print_request_outcome {
             push(@baduname,$uname.':'.$udom);
         }
     }
+    if (keys(%skipped)) {
+        foreach my $key (keys(%skipped)) {
+            delete($personnel{$key}); 
+        }
+    }
     my ($accessstart,$accessend) = &dates_from_form('accessstart','accessend');
     my $autodrops = 0;
     if ($env{'form.autodrops'}) {
@@ -3179,8 +3401,9 @@ sub print_request_outcome {
                   };
     my (@inststatuses,$storeresult,$creationresult);
     my $val = 
-        &Apache::loncoursequeueadmin::get_processtype($env{'user.name'},$env{'user.domain'},
-            $env{'user.adv'},$dom,$crstype,\@inststatuses,\%domconfig);
+        &Apache::loncoursequeueadmin::get_processtype('course',$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.');
@@ -3335,7 +3558,7 @@ sub print_request_outcome {
         }
         ($storeresult,my $updateresult) = 
             &Apache::loncoursequeueadmin::update_coursereq_status(\%reqhash,$dom,
-                $cnum,$reqstatus,'request');
+                $cnum,$reqstatus,'request',$env{'user.domain'},$env{'user.name'});
         if ($modified && $queued && $storeresult eq 'ok') {
             if ($crstype eq 'community') {
                 $output .= '<p>'.&mt('Your community request has been updated').'</p>';
@@ -3595,16 +3818,18 @@ sub retrieve_settings {
             }
             $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') {