--- loncom/interface/lonrequestcourse.pm	2012/08/15 14:37:13	1.65
+++ loncom/interface/lonrequestcourse.pm	2013/12/25 09:52:42	1.70
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Request a course
 #
-# $Id: lonrequestcourse.pm,v 1.65 2012/08/15 14:37:13 raeburn Exp $
+# $Id: lonrequestcourse.pm,v 1.70 2013/12/25 09:52:42 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -134,7 +134,11 @@ sub handler {
     my $action = $env{'form.action'};
     my $state = $env{'form.state'};
     my (%states,%stored);
-    my ($jscript,$uname,$udom,$result,$warning);
+    my ($jscript,$uname,$udom,$result,$warning,$showcredits,$instcredits);
+    my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
+    if ($domdefs{'officialcredits'} || $domdefs{'unofficialcredits'} || $domdefs{'textbookcredits'}) {
+        $showcredits = 1;
+    }
 
     $states{'display'} = ['details'];
     $states{'view'} = ['pick_request','details','cancel','removal'];
@@ -180,7 +184,7 @@ sub handler {
         $trail{'enrollment'} = 'Enrollment';
     }
 
-    my ($page,$crumb,$newinstcode,$codechk,$checkedcode,$description) = 
+    my ($page,$crumb,$newinstcode,$codechk,$checkedcode,$description) =
         &get_breadcrumbs($dom,$action,\$state,\%states,\%trail);
     if ($action eq 'display') {
         if (($dom eq $env{'request.role.domain'}) && (&Apache::lonnet::allowed('ccc',$dom))) {
@@ -269,11 +273,11 @@ sub handler {
                 }
             }
         }
-        my %elements =  &form_elements($dom);
+        (my $elements,$instcredits) = &form_elements($dom,$showcredits);
         my $elementsref = {};
-        if (ref($elements{$action}) eq 'HASH') {
-            if (ref($elements{$action}{$state}) eq 'HASH') {
-                $elementsref = $elements{$action}{$state};
+        if ((ref($elements) eq 'HASH') && (ref($elements->{$action}) eq 'HASH')) {
+            if (ref($elements->{$action}{$state}) eq 'HASH') {
+                $elementsref = $elements->{$action}{$state};
             }
         }
         if (($state eq 'courseinfo') && ($env{'form.clonedom'} eq '')) {
@@ -307,7 +311,7 @@ sub handler {
                 &request_administration($r,$action,$state,$page,\%states,$dom,
                                         $jscript,$loaditems,$crumb,$newinstcode,
                                         $codechk,$checkedcode,$description,
-                                        \@invalidcrosslist);
+                                        $showcredits,$instcredits,\@invalidcrosslist);
             }
         } else {
             $r->print(&header('Course/Community Requests').$crumb.
@@ -320,7 +324,7 @@ sub handler {
             &print_main_menu($r,\%can_request,\%states,$dom,$jscript,$loaditems,$crumb,\%request_domains);
         } else {
             &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
-                                    $loaditems,$crumb);
+                                    $loaditems,$crumb,'','','','',$showcredits);
         }
     } elsif ($action eq 'display') {
         if ($warning ne '') {
@@ -331,7 +335,8 @@ sub handler {
                       &close_popup_form());
         } else {
             &request_administration($r,$action,$state,$page,\%states,$dom,$jscript,
-                                    $loaditems,$crumb,'','','','','',$uname,$udom);
+                                    $loaditems,$crumb,'','','','',$showcredits,'','',
+                                    $uname,$udom);
         }
     } elsif ($action eq 'log') {
         if ($state eq 'crstype') {
@@ -495,7 +500,8 @@ sub header {
 }
 
 sub form_elements {
-    my ($dom) = @_;
+    my ($dom,$showcredits) = @_;
+    my $instcredits;
     my %elements =
     (
         new => {
@@ -603,6 +609,15 @@ sub form_elements {
                     $extras{'loncapasec_'.$i} = 'text',
                 }
             }
+            (my $outcome,my $desc,$instcredits) = 
+                &Apache::lonnet::auto_validate_instcode(undef,$dom,$instcode);
+            if ($showcredits && $instcredits eq '') {
+                $extras{'coursecredits'} = 'text';
+            }
+        } elsif (($env{'form.crstype'} eq 'unofficial') || ($env{'form.crstype'} eq 'textbook')) {
+            if ($showcredits) {
+                $extras{'coursecredits'} = 'text';
+            }
         }
         my $crosslisttotal = $env{'form.crosslisttotal'};
         if ($env{'form.addcrosslist'}) {
@@ -648,7 +663,7 @@ sub form_elements {
     }
     my %personnelhash = (%{$elements{'new'}{'personnel'}},%people);
     %{$elements{'new'}{'personnel'}} = %personnelhash;
-    return %elements;
+    return (\%elements,$instcredits);;
 }
 
 sub onload_action {
@@ -706,7 +721,8 @@ $nextstate_setter
 function check_can_request(crschoice,actionchoice) {
     var official = '';
     var unofficial = '';
-    var community = '';    
+    var community = '';
+    var textbook = '';
 END
     if (ref($can_request) eq 'HASH') {
         foreach my $item (keys(%{$can_request})) {
@@ -718,8 +734,10 @@ END
     my %lt = &Apache::lonlocal::texthash(
         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.\\n\"All types\" is not allowed.',
+        community => 'You are not permitted to request creation of a community in this domain.',
+        textbook => 'You are not permitted to request creation of a textbook course in this domain',
+        all => 'You must choose a specific course type when making a new course request.',
+        allt => '"All types" is not allowed.',
     ); 
     $js .= <<END;
     if (crschoice == 'official') {
@@ -740,10 +758,17 @@ END
                     return false;
                 }
             } else {
-                if (actionchoice == 'new') {
-                    alert("$lt{'all'}");
-                    return false;
-                }               
+                if (crschoice == 'textbook') {
+                    if (textbook != 1) {
+                        alert("$lt{'community'}");
+                        return false;
+                    }
+                } else {
+                    if (actionchoice == 'new') {
+                        alert('$lt{'all'}'+'\\n'+'$lt{'allt'}');
+                        return false;
+                    }
+                }
             }
         }
     }
@@ -752,7 +777,7 @@ END
 END
     my ($pagetitle,$pageinfo,$domaintitle);
     if (ref($can_request) eq 'HASH') {
-        if (($can_request->{'official'}) || ($can_request->{'unofficial'})) {
+        if (($can_request->{'official'}) || ($can_request->{'unofficial'}) || $can_request->{'textbook'}) {
             if ($can_request->{'community'}) {
                 $pagetitle = 'Course/Community Requests';
                 $pageinfo = &mt('Request creation of a new course or community, or review your pending requests.');
@@ -854,8 +879,8 @@ END
 
 sub request_administration {
     my ($r,$action,$state,$page,$states,$dom,$jscript,$loaditems,$crumb,
-        $newinstcode,$codechk,$checkedcode,$description,$invalidcrosslist,
-        $uname,$udom) = @_;
+        $newinstcode,$codechk,$checkedcode,$description,$showcredits,
+        $instcredits,$invalidcrosslist,$uname,$udom) = @_;
     my $js;
     if (($action eq 'new') || (($action eq 'view') && ($state eq 'pick_request'))) {
         $js =  <<END;
@@ -897,7 +922,8 @@ END
         }
         $r->print(&header($title,$js.$jscript,$loaditems,$jsextra).$crumb);
         &print_request_form($r,$action,$state,$page,$states,$dom,$newinstcode,
-                            $codechk,$checkedcode,$description,$invalidcrosslist);
+                            $codechk,$checkedcode,$description,$showcredits,
+                            $instcredits,$invalidcrosslist);
     } elsif ($action eq 'view') {
         my $jsextra;
         my $formname = 'requestcrs';
@@ -928,6 +954,8 @@ END
                 $title = &mt('Pending requests for official courses');
             } elsif ($env{'form.crstype'} eq 'unofficial') {
                 $title = &mt('Pending requests for unofficial courses');
+            } elsif ($env{'form.crstype'} eq 'textbook') {
+                $title = &mt('Pending requests for textbook courses');
             } else {
                 $title = &mt('Pending course/community requests'); 
             }
@@ -951,9 +979,10 @@ END
             }
             $r->print('<h3>'.$title.'</h3><div>'."\n".$form."\n".
                       &print_review($dom,\@codetitles,\%cat_titles,\%cat_order,
-                                    \@code_order)."\n".
+                                    \@code_order,'','','','',$instcredits)."\n".
                       '<input name="origcnum" value="'.$origcnum.'" type="hidden" />'."\n");
-            my @excluded = &get_excluded_elements($dom,$states,'new','review');
+            my @excluded = &get_excluded_elements($dom,$states,'new','review',
+                                                  $showcredits);
             push(@excluded,'origcnum');
             $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</div>');
             my $other = 'modify';
@@ -975,7 +1004,8 @@ END
             my ($result,$output) = &print_cancel_request($dom,$env{'form.origcnum'});
             $r->print('<h3>'.$title.'</h3><div>'."\n".$form."\n".
                       $output);
-            my @excluded = &get_excluded_elements($dom,$states,'view','cancel');
+            my @excluded = &get_excluded_elements($dom,$states,'view','cancel',
+                                                  $showcredits);
             $r->print(&Apache::lonhtmlcommon::echo_form_input(\@excluded).'</div>');
             my %navtxt = &Apache::lonlocal::texthash (
                                                       prev => 'Back',
@@ -1052,7 +1082,8 @@ END
         $r->print(&header($title,'','','',{ 'only_body' => 1}).
                   $crumb."\n".'<h3>'.$header.'</h3>'.
                   &print_review($dom,\@codetitles,\%cat_titles,\%cat_order,
-                                \@code_order,$uname,$udom)."\n".'</div>'.
+                                \@code_order,$uname,$udom,'','',$instcredits)."\n".
+                  '</div>'.
                   &close_popup_form());
     }
     $r->print(&Apache::loncommon::end_page());
@@ -1246,7 +1277,7 @@ sub get_instcode {
 
 sub print_request_form {
     my ($r,$action,$state,$page,$states,$dom,$newinstcode,$codechk,$checkedcode,
-        $description,$invalidcrosslist) = @_;
+        $description,$showcredits,$instcredits,$invalidcrosslist) = @_;
     my $formname = 'requestcrs';
     my ($next,$prev,$message,$output,$codepicker,$crstype);
     $prev = $states->{$action}[$page-1];
@@ -1319,7 +1350,7 @@ sub print_request_form {
         }
         $r->print(&print_enrollment_menu($formname,$instcode,$dom,\@codetitles,
                                          \%cat_titles,\%cat_order,\@code_order,
-                                         $invalidcrosslist));
+                                         $showcredits,$instcredits,$invalidcrosslist));
     } elsif ($state eq 'personnel') {
         $r->print(&print_personnel_menu($dom,$formname,$crstype,$invalidcrosslist));
     } elsif ($state eq 'review') {
@@ -1435,7 +1466,7 @@ sub print_request_form {
         } else {
             $r->print('<h3>'.&mt('Review course request details before submission').'</h3>');
         }
-        $r->print(&print_review($dom,\@codetitles,\%cat_titles,\%cat_order,\@code_order,'','',\@disallowed,\%disallowmsg).
+        $r->print(&print_review($dom,\@codetitles,\%cat_titles,\%cat_order,\@code_order,'','',\@disallowed,\%disallowmsg,$instcredits).
                   '<input type="hidden" name="cnum" value="'.$cnum.'" />');
         if ($crstype eq 'community') {
             $navtxt{'next'} = &mt('Submit community request');
@@ -1448,7 +1479,7 @@ sub print_request_form {
                                                      \%cat_order,\@code_order);
         }
         my ($storeresult,$result) = &print_request_outcome($dom,\@codetitles,
-                                                           \@code_order);
+                                                           \@code_order,$instcredits);
         $r->print($result);
         if (($storeresult eq 'ok') || ($storeresult eq 'created')) {
             if ($storeresult eq 'ok') {
@@ -1518,7 +1549,7 @@ sub print_request_form {
             $r->print('<p>'.$links[0].'</p>');
         }
     }
-    my @excluded = &get_excluded_elements($dom,$states,$action,$state);
+    my @excluded = &get_excluded_elements($dom,$states,$action,$state,$showcredits);
     if ($state eq 'personnel') {
         push(@excluded,'persontotal');
     }
@@ -1615,18 +1646,19 @@ sub check_newuser_rules {
 }
 
 sub get_excluded_elements {
-    my ($dom,$states,$action,$state) = @_;
+    my ($dom,$states,$action,$state,$showcredits) = @_;
     my @excluded = ('counter');
-    my %elements = &form_elements($dom);
+    my ($elements,$instcredits) = &form_elements($dom,$showcredits);
     if (ref($states) eq 'HASH') {
         if (ref($states->{$action}) eq 'ARRAY') {
             my @items = @{$states->{$action}};
             my $numitems = scalar(@items);
             if ($numitems) {
                 for (my $i=$numitems-1; $i>=0; $i--) {
-                    if (ref($elements{$action}) eq 'HASH') {
-                        if (ref($elements{$action}{$items[$i]}) eq 'HASH') {
-                            foreach my $key (keys(%{$elements{$action}{$items[$i]}})) {
+                    if ((ref($elements) eq 'HASH') && 
+                        (ref($elements->{$action}) eq 'HASH')) {
+                        if (ref($elements->{$action}{$items[$i]}) eq 'HASH') {
+                            foreach my $key (keys(%{$elements->{$action}{$items[$i]}})) {
                                 push(@excluded,$key);
                             }
                         }
@@ -1644,8 +1676,9 @@ sub get_excluded_elements {
 
 sub print_enrollment_menu {
     my ($formname,$instcode,$dom,$codetitles,$cat_titles,$cat_order,$code_order,
-        $invalidcrosslist) =@_;
-    my ($sections,$autoenroll,$access_dates,$output,$hasauto);
+        $showcredits,$instcredits,$invalidcrosslist) =@_;
+    my ($sections,$autoenroll,$access_dates,$output,$hasauto,$hascredits,
+        $creditsrow,$domdefcredits);
     my $starttime = time;
     my $endtime = time+(6*30*24*60*60); # 6 months from now, approx
 
@@ -1657,6 +1690,12 @@ sub print_enrollment_menu {
                            'start' => 'Start auto-enrollment',
                            'end'   => 'End auto-enrollment',
                        );
+    if ($showcredits) {
+        unless ($env{'form.crstype'} eq 'community') {
+            my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
+            $domdefcredits = $domdefs{$env{'form.crstype'}.'credits'};
+        }
+    }
     if ($env{'form.crstype'} eq 'official') {
         if (&Apache::lonnet::auto_run('',$dom)) {
             $output = &show_invalid_crosslists($invalidcrosslist);
@@ -1731,12 +1770,29 @@ sub print_enrollment_menu {
                 &mt('No').'</label></span>'. 
                 &Apache::lonhtmlcommon::row_closure(1).
                 &date_setting_table($starttime,$endtime,$formname,'enroll',
-                                    $hasauto,%enrolltitles);
+                                    $hasauto,undef,%enrolltitles);
+            if ($showcredits) {
+                if ($instcredits) {
+                    $creditsrow = &mt('[quant,_1,credit]',$instcredits);
+                } else {
+                    $creditsrow = '<span class="LC_nobreak">'.
+                                  '<input type="text" size="3" name="coursecredits"'.
+                                  ' value="'.$domdefcredits.'" />';
+                }
+                $hascredits = 1;
+            }
+        }
+    } elsif (($env{'form.crstype'} eq 'unofficial') || ($env{'form.crstype'} eq 'textbook')) {
+        if ($showcredits) {
+            $creditsrow = '<span class="LC_nobreak">'.
+                          '<input type="text" size="3" name="coursecredits"'.
+                          ' value="'.$domdefcredits.'"/>';
+            $hascredits = 1;
         }
     }
     my $access_dates = 
         &date_setting_table($starttime,$endtime,$formname,'access',$hasauto,
-                            %accesstitles);
+                            $hascredits,%accesstitles);
     $output .= &Apache::lonhtmlcommon::start_pick_box();
     if ($sections) {
         $output .=  $sections;
@@ -1755,7 +1811,15 @@ sub print_enrollment_menu {
         $output .= &Apache::lonhtmlcommon::row_headline('Access').
                    '<h3>'.$header.'</h3>'.
                    &Apache::lonhtmlcommon::row_closure(1).
-                   $access_dates
+                   $access_dates;
+    }
+    if ($creditsrow) {
+        $output .= &Apache::lonhtmlcommon::row_headline('Credits').
+                   '<h3>'.&mt('Credits earned by students').'</h3>'.
+                   &Apache::lonhtmlcommon::row_closure(1).
+                   &Apache::lonhtmlcommon::row_title(&mt('Default credits')).
+                   $creditsrow.
+                   &Apache::lonhtmlcommon::row_closure(1);
     }
     return '<div>'.&Apache::lonhtmlcommon::start_pick_box().$output.
            &Apache::lonhtmlcommon::end_pick_box().'</div>';
@@ -1820,7 +1884,7 @@ sub inst_section_selector {
 }
 
 sub date_setting_table {
-    my ($starttime,$endtime,$formname,$prefix,$hasauto,%datetitles) = @_;
+    my ($starttime,$endtime,$formname,$prefix,$hasauto,$hascredits,%datetitles)=@_;
     my ($perpetual,$table);
     my $startform = &Apache::lonhtmlcommon::date_setter($formname,$prefix.'start',
                                                         $starttime,'','','',1,'','','',1);
@@ -1831,7 +1895,9 @@ sub date_setting_table {
         $perpetual = ' <span class="LC_nobreak"><label>'.
                      '<input type="checkbox" name="no_end_date" />'.
                      &mt('No end date').'</label></span>';
-        $closure = '1';
+        unless ($hascredits) {
+            $closure = '1';
+        }
     }
 
     my %help_item = (
@@ -1921,6 +1987,7 @@ sub print_personnel_menu {
             official => 'Requestor is automatically assigned Course Coordinator role.',
         );
         $lt{'unofficial'} = $lt{'official'};
+        $lt{'textbook'} = $lt{'textbook'};
         $output .= &Apache::lonhtmlcommon::row_headline().
                   '<h3>'.&Apache::loncommon::help_open_topic('Course_Request_Personnel').'&nbsp;'.$lt{$crstype}.' '.&mt('Include other personnel?').'</h3>';
     }
@@ -2251,8 +2318,8 @@ ENDJS
 }
 
 sub viewcancel_javascript {
-    my $alert = &mt('Are you sure you want to cancel this request?\\n'.
-                    'Your request will be removed.');
+    my $alert = &mt('Are you sure you want to cancel this request?').'\\n'.
+                &mt('Your request will be removed.');
     return << "ENDJS";
 function nextPage(formname,nextstate) {
     if (confirm('$alert')) {
@@ -2459,7 +2526,7 @@ sub reqstatus_names {
                         rejected  => 'Request rejected',
                         cancelled => 'Request cancelled',
             );
-    if (($crstype eq 'official') || ($crstype eq 'unofficial')) {
+    if (($crstype eq 'official') || ($crstype eq 'unofficial') || ($crstype eq 'textbook')) {
         $statusnames{'created'} = &mt('Course created');
     } elsif ($crstype eq 'community') {
         $statusnames{'created'} = &mt('Community created');
@@ -2553,7 +2620,7 @@ sub requestlog_display_filter {
 
 sub print_review {
     my ($dom,$codetitles,$cat_titles,$cat_order,$code_order,$uname,$udom,
-        $disallowed,$disallowmsg) = @_;
+        $disallowed,$disallowmsg,$instcredits) = @_;
     my ($types,$typename) = &Apache::loncommon::course_types();
     my ($owner,$ownername,$owneremail);
     if ($uname eq '' || $udom eq '') {
@@ -2601,6 +2668,12 @@ sub print_review {
                 }
             }
         }
+        $inst_headers .= '<th>'.&mt('Credits').'</th>';
+        if ($instcredits) {
+            $inst_values .= '<td>'.$instcredits.'</td>';
+        } else {
+            $inst_values .= '<td>'.$env{'form.coursecredits'}.'</td>';
+        }
         if (&Apache::lonnet::auto_run('',$dom)) {
             $enrollrow_title = &mt('Enrollment');
             $enroll_headers = '<th>'.&mt('Automatic Adds').'</th>'.
@@ -2670,6 +2743,9 @@ sub print_review {
             $section_values .= $xlistinfo;
         }
         $section_values .= '</table></td>';
+    } elsif (($env{'form.crstype'} eq 'unofficial') || ($env{'form.crstype'} eq 'textbook')) {
+        $inst_headers .= '<th>'.&mt('Credits').'</th>';
+        $inst_values .= '<td>'.$env{'form.coursecredits'}.'</td>';
     }
 
     my %ctxt = &clone_text();
@@ -2846,6 +2922,7 @@ sub courseinfo_form {
                 community => 'You must provide a (brief) community description.'
              );
     $lt{'unofficial'} = $lt{'official'};
+    $lt{'textbook'} = $lt{'official'};
     my $js_validate = <<"ENDJS";
 <script type="text/javascript">
 // <![CDATA['
@@ -3165,9 +3242,10 @@ sub display_navbuttons {
 }
 
 sub print_request_outcome {
-    my ($dom,$codetitles,$code_order) = @_;
+    my ($dom,$codetitles,$code_order,$instcredits) = @_;
     my ($output,$cnum,$now,$req_notifylist,$crstype,$enrollstart,$enrollend,
-        %sections,%crosslistings,%personnel,@baduname,@missingdom,%domconfig,);
+        %sections,%crosslistings,%personnel,@baduname,@missingdom,%domconfig,
+        $uniquecode);
     my $sectotal = $env{'form.sectotal'};
     my $crosslisttotal = 0;
     $cnum = $env{'form.cnum'};
@@ -3181,6 +3259,7 @@ sub print_request_outcome {
         if (ref($domconfig{'requestcourses'}{'notify'}) eq 'HASH') {
             $req_notifylist = $domconfig{'requestcourses'}{'notify'}{'approval'};
         }
+        $uniquecode = $domconfig{'requestcourses'}{'uniquecode'};
     }
     $now = time;
     $crstype = $env{'form.crstype'};
@@ -3356,6 +3435,12 @@ sub print_request_outcome {
     if (exists($env{'form.instcode'})) {
         $instcode = $env{'form.instcode'};
     }
+    my $credits;
+    if ($instcredits) {
+        $credits = $instcredits;
+    } elsif (exists($env{'form.coursecredits'})) {
+        $credits = $env{'form.coursecredits'};
+    }
     my $clonecrs = '';
     my $clonedom = '';
     if (($env{'form.cloning'}) &&
@@ -3383,6 +3468,8 @@ sub print_request_outcome {
                     cdescr         => $env{'form.cdescr'},
                     crstype        => $env{'form.crstype'},
                     instcode       => $instcode,
+                    defaultcredits => $credits, 
+                    uniquecode     => $uniquecode,
                     clonedom       => $clonedom,
                     clonecrs       => $clonecrs,
                     datemode       => $env{'form.datemode'},
@@ -3411,6 +3498,8 @@ sub print_request_outcome {
             $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');
+        } elsif ($crstype eq 'textbook') {
+            $output = &mt('You are not permitted to request creation of textbook courses');
         } else {
             $output = &mt('Unrecognized course type: [_1]',$crstype);
         }
@@ -3472,7 +3561,7 @@ sub print_request_outcome {
             $storeresult = 'rejected';
         } elsif ($disposition eq 'process') {
             my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
-            my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg,%longroles);
+            my ($logmsg,$newusermsg,$addresult,$enrollcount,$response,$keysmsg,%longroles,$code);
             my $type = 'Course';
             if ($crstype eq 'community') {
                 $type = 'Community';
@@ -3483,7 +3572,7 @@ sub print_request_outcome {
             }
             my $result = &Apache::loncoursequeueadmin::course_creation($dom,$cnum,
                                    'autocreate',$details,\$logmsg,\$newusermsg,\$addresult,
-                                   \$enrollcount,\$response,\$keysmsg,\%domdefs,\%longroles);
+                                   \$enrollcount,\$response,\$keysmsg,\%domdefs,\%longroles,\$code);
             if ($result eq 'created') {
                 $disposition = 'created';
                 $reqstatus = 'created';
@@ -3494,6 +3583,10 @@ sub print_request_outcome {
                 } else {
                     $output = '<p>'.&mt('Your course request has been processed and the course has been created.');
                 }
+                if ($code) {
+                    $output .= &notification_information($disposition,$env{'user.name'}.':'.$env{'user.domain'},
+                                                         $cnum,$now,$code);
+                }
                 $output .= '<br />'.$role_result.'</p>';
                 $creationresult = 'created';
             } else {
@@ -3717,7 +3810,7 @@ sub update_requestors_roles {
 }
 
 sub notification_information {
-    my ($disposition,$req_notifylist,$cnum,$now) = @_;
+    my ($disposition,$req_notifylist,$cnum,$now,$code) = @_;
     my %emails = &Apache::loncommon::getemails();
     my $address;
     if (($emails{'permanentemail'} ne '') || ($emails{'notification'} ne '')) {
@@ -3745,6 +3838,23 @@ sub notification_information {
 &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>';
+    } elsif (($disposition eq 'created') && ($code)) {
+        my $codemsg = [{
+                         mt   => 'Students can automatically select your course by entering this code: [_1]',
+                         args => [$code],
+                     }];
+        $output .= '<br />'.
+                   &mt('Students can automatically select your course by entering this code: [_1].','<b>'.$code.'</b>').
+                   '<br />'.
+                   &mt('A message has been sent to your LON-CAPA account with this information').'</br />';
+        if ($address ne '') {
+            $output.= &mt('And an e-mail has also been sent to: [_1] with this code.',$address).'<br />';
+        }
+        my $sender = $env{'user.name'}.':'.$env{'user.domain'};
+        if ($code) {
+            &Apache::loncoursequeueadmin::send_selfserve_notification($req_notifylist,$codemsg,$cnum,$env{'form.cdescr'},
+                                                                      $now,'uniquecode',$sender);
+        }
     } else {
         $output .= '<div class="LC_warning">'.
                    &mt('Your request status is: [_1].',$disposition).
@@ -3767,7 +3877,7 @@ sub check_autolimit {
             if (($crstype eq 'community') && 
                 (exists($crsroles{$cnum.':'.$cdom.':co'}))) {
                 $count ++;
-            } elsif ((($crstype eq 'official') || ($crstype eq 'unofficial')) &&
+            } elsif ((($crstype eq 'official') || ($crstype eq 'unofficial') || ($crstype eq 'textbook')) &&
                      (exists($crsroles{$cnum.':'.$cdom.':cc'}))) {
                 $count ++;
             }
@@ -3829,7 +3939,15 @@ sub retrieve_settings {
                                     $cnum => $reqinfo{'instcode'},
                                   };
                     &extract_instcode($dom,'instcode',$crscode,$cnum);
+                    (undef,undef,my $instcredits) =
+                        &Apache::lonnet::auto_validate_instcode(undef,$dom,
+                                                                $reqinfo{'instcode'});
+                    if ($instcredits ne $reqinfo{'defaultcredits'}) {
+                        $env{'form.coursecredits'} = $reqinfo{'defaultcredits'};
+                    }
                 }
+            } elsif (($reqinfo{'crstype'} eq 'unofficial') || ($reqinfo{'crstype'} eq 'textbook')) {
+                $env{'form.coursecredits'} = $reqinfo{'defaultcredits'};
             }
             my @currsec;
             if (ref($reqinfo{'sections'}) eq 'HASH') {