Annotation of loncom/interface/selfenroll.pm, revision 1.13

1.1       raeburn     1: # The LearningOnline Network
                      2: # Allow users to self-enroll in a course
                      3: #
1.13    ! raeburn     4: # $Id: selfenroll.pm,v 1.12 2008/07/17 12:22:28 raeburn Exp $
1.1       raeburn     5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: #
                     29: package Apache::selfenroll;
                     30: 
                     31: use strict;
                     32: use Apache::Constants qw(:common);
                     33: use Apache::lonnet;
                     34: use Apache::loncommon;
                     35: use Apache::lonlocal;
1.10      raeburn    36: use Apache::createaccount;
1.1       raeburn    37: use LONCAPA qw(:DEFAULT :match);
                     38: 
                     39: sub handler {
                     40:     my $r = shift;
                     41:     &Apache::loncommon::content_type($r,'text/html');
                     42:     $r->send_http_header;
                     43:     if ($r->header_only) {
                     44:         return OK;
                     45:     }
                     46:     my $handle = &Apache::lonnet::check_for_valid_session($r);
                     47:     my $lonidsdir=$r->dir_config('lonIDsDir');
1.10      raeburn    48:     my $lonhost = $r->dir_config('lonHostID');
1.1       raeburn    49:     if ($handle ne '') {
                     50:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
                     51:     }
                     52:     &Apache::lonacc::get_posted_cgi($r);
                     53:     &Apache::lonlocal::get_language_handle($r);
1.12      raeburn    54:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['courseid']);
                     55:     my $js = &Apache::createaccount::catreturn_js();
                     56:     my $desc;
                     57:     my ($coursechk,$courseid) = &validate_course_id($env{'form.courseid'});
1.1       raeburn    58:     if ($coursechk ne 'ok') {
1.12      raeburn    59:         &page_header($r,$courseid,$js);
1.9       raeburn    60:         $r->print('<h3>'.&mt('Self-enrollment error').'</h3>'.
1.10      raeburn    61:                   '<span class="LC_error">'.
                     62:                   &mt('Invalid domain or course number').'</span>');
1.12      raeburn    63:         &page_footer($r);
1.1       raeburn    64:         return OK;
                     65:     }
1.10      raeburn    66:     my $now = time;
                     67:     if ($env{'form.phase'} eq 'login') {
1.12      raeburn    68:         $js .= "\n".&Apache::createaccount::javascript_setforms($now);
1.10      raeburn    69:     }
1.12      raeburn    70:     my %coursehash = &Apache::lonnet::coursedescription($courseid);
                     71:     my $cdom = $coursehash{'domain'};
                     72:     my $cnum = $coursehash{'num'};
                     73:     my $desc = $coursehash{'description'};
                     74:     &page_header($r,$courseid,$js,$desc);
1.10      raeburn    75:     my $include = $r->dir_config('lonIncludes');
                     76:     if ($env{'form.phase'} eq 'login') {
                     77:         my $jsh=Apache::File->new($include."/londes.js");
                     78:         $r->print(<$jsh>);
                     79:     }
1.12      raeburn    80:     my ($canenroll,$selfenroll_types,$selfenroll_registered,@cancreate,
                     81:         $knownuser,$selfenroll_access_start,$selfenroll_access_end,
                     82:         $selfenroll_section,$selfenroll_future,%curr_role,$cdomdesc);
1.1       raeburn    83:     $selfenroll_types = $coursehash{'internal.selfenroll_types'};
                     84:     $selfenroll_registered =  $coursehash{'internal.selfenroll_registered'};
                     85:     $selfenroll_section = $coursehash{'internal.selfenroll_section'};
                     86:     $selfenroll_access_start =  $coursehash{'internal.selfenroll_start_access'};
                     87:     $selfenroll_access_end =  $coursehash{'internal.selfenroll_end_access'};
                     88:     if ($selfenroll_types ne '') {
                     89:         my $start = $coursehash{'internal.selfenroll_start_date'};
                     90:         my $end = $coursehash{'internal.selfenroll_end_date'};
1.9       raeburn    91:         if (($start > 0 && $start < $now) && (($end == 0) || ($end > 0 && $end > $now))) {
1.1       raeburn    92:             $canenroll = 1;
1.9       raeburn    93:         } elsif (($end == 0) || ($end > 0 && $end > $now)) {
                     94:             if ($start > $now) {
                     95:                 $selfenroll_future = &Apache::lonlocal::locallocaltime($start);
                     96:             }
1.1       raeburn    97:         }
                     98:     }
1.9       raeburn    99:     $knownuser = &user_is_known();
1.1       raeburn   100:     if (!$canenroll) {
1.9       raeburn   101:         $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3><span class="LC_warning">'.
                    102:                   &mt('Self-enrollment is not currently available for this course.').
                    103:                   '</span><br /><br />');
                    104:         if ($selfenroll_types ne '') {
                    105:             if ($selfenroll_future ne '') {
                    106:                 if ($selfenroll_types eq '*') {
                    107:                     $r->print(&mt('Self-enrollment will become available starting [_1], and will be available to all LON-CAPA users.',$selfenroll_future).'<br />');
                    108:                 } else {
                    109:                     my ($enrolltypes,$longtypes,$alldoms);
                    110:                     if ($knownuser) {
                    111:                         &get_selfenroll_filters($selfenroll_types,$env{'user.domain'});
                    112:                         my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
                    113:                         if (ref($enrolltypes) eq 'HASH') {
                    114:                             if (ref($enrolltypes->{$env{'user.domain'}}) eq 'ARRAY') {
                    115:                                 if (grep(/^any$/,@{$enrolltypes->{$env{'user.domain'}}})) {
                    116:                                     $r->print(&mt('Self-enrollment will become available starting [_1], and will be available to all LON-CAPA users at your institution ([_2]).',
                    117:                                               $selfenroll_future,$domdesc).'<br />');
                    118: 
                    119:                                 } else {
                    120:                                     if (&user_can_selfenroll($env{'user.domain'},
                    121:                                                         $env{'user.name'},
                    122:                                                         $enrolltypes->{$env{'user.domain'}})) {
                    123:                                         $r->print(&mt('Self-enrollment will become available starting [_1]; please enroll at that time.',$selfenroll_future)); 
                    124:                                     } else {
                    125:                                         $r->print(&mt('Although self-enrollment will become available starting [_1], you are ineligible for enrollment.',$selfenroll_future).'<br />');
                    126:                                         $r->print(&print_selfenroll_types($longtypes,$env{'user.domain'}));
                    127:                                     }
                    128:                                 }
                    129:                             }
                    130:                         } 
                    131:                     } else {
                    132:                         $r->print(&mt('Self-enrollment will become available starting [_1].',
                    133:                                       $selfenroll_future).'<br />');
                    134:                         $r->print(&print_selfenroll_types($longtypes));
                    135:                     }
                    136:                 }
                    137:             } 
                    138:         }
1.1       raeburn   139:     }
                    140:     if ($knownuser) {
                    141:         foreach my $key (keys(%env)) {
                    142:             if ($key =~ m-^user\.role\.st\./$cdom/$cnum/?(\w*)$-) {
                    143:                 my $sec = $1;
                    144:                 my ($start,$end) = split(/\./,$env{$key});
                    145:                 my $status = 'active';
                    146:                 if (($end) && ($end<=$now)) {
                    147:                     $status = 'previous';
                    148:                 }
                    149:                 if (($start) && ($now<$start)) {
                    150:                     $status = 'future';
                    151:                 }
                    152:                 if ($status eq 'active' || $status eq 'future') {
                    153:                      $curr_role{'status'} = $status;
                    154:                      $curr_role{'section'} = $sec;
1.7       raeburn   155:                      if ($curr_role{'section'} eq '') {
                    156:                          $curr_role{'section'} = &mt('none'); 
                    157:                      }
1.1       raeburn   158:                      $curr_role{'start'} = &Apache::lonlocal::locallocaltime($start);
                    159:                      $curr_role{'role'} = 'st./'.$cdom.'/'.$cnum;
                    160:                      if ($sec ne '') {
                    161:                          $curr_role{'role'} .= '/'.$sec; 
                    162:                      }
                    163:                 }
                    164:             }
                    165:         }
                    166:     }
                    167:     if (!$canenroll) {
                    168:         if (keys(%curr_role)) {
                    169:             $r->print(&has_role(%curr_role));
                    170:         }
1.12      raeburn   171:         &page_footer($r);
1.1       raeburn   172:         return OK;
                    173:     }
1.3       raeburn   174:     @cancreate = &can_create($cdom);
1.12      raeburn   175:     my ($form,$login_path,$firsturl,$create_path,$sso_url,$missing_formitem);
                    176:     $form = 'logmein';
1.1       raeburn   177:     $login_path = '/adm/login';
1.12      raeburn   178:     $firsturl= '/adm/selfenroll?courseid='.$courseid;
1.1       raeburn   179:     $create_path = '/adm/createaccount';
                    180:     $sso_url = $r->dir_config('lonSSOReloginServer');
                    181:     if ($sso_url eq '') {
                    182:         $sso_url = $login_path;
                    183:     }
1.11      raeburn   184:     $missing_formitem = &mt('The link to the requested page could not be followed.')."\\n".&mt('The placeholder for the courseID is absent.');
1.1       raeburn   185:     if ($knownuser) {
                    186:         if (keys(%curr_role)) {
1.9       raeburn   187:             $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.
                    188:                       '<span class="LC_warning">'.&has_role(%curr_role).'</span>');
1.12      raeburn   189:             &page_footer($r);
1.1       raeburn   190:             return OK;
                    191:         }
                    192:         &process_self_enroll($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,
                    193:                              $selfenroll_access_start,$selfenroll_access_end,
                    194:                              $selfenroll_section,$now);
1.10      raeburn   195:     } elsif ($env{'form.phase'} eq 'login') {
                    196:         my $submit_text = &mt('Log in');
                    197:         $r->print('<h3>'.&mt('Log-in to LON-CAPA').'</h3>');
                    198:         my $udom = &Apache::lonnet::default_login_domain();
1.12      raeburn   199:         $r->print(&Apache::createaccount::login_box($now,$lonhost,$courseid,
1.10      raeburn   200:                                            $submit_text,$udom,'selfenroll'));
1.12      raeburn   201:         $r->print(&mt('You will be able to self-enroll in the course you selected ([_1]) after you have successfully logged in.','<b>'.$desc.'</b>'));
                    202:         &page_footer($r);
1.10      raeburn   203:         return OK;
1.3       raeburn   204:     } elsif (@cancreate > 0) {
1.1       raeburn   205:         $r->print(<<END);
                    206: <script type="text/javascript">
1.12      raeburn   207: function setPath(formname,formaction,item,arg) {
                    208:     var formidx = getFormByName(formname);
                    209:     if (formidx > -1) {
                    210:         if (formaction != '') {
                    211:             document.forms[formidx].action = formaction;
                    212:         }
                    213:         var itemid = getIndexByName(formidx,'setting');
                    214:         if (itemid > -1) {
                    215:             document.forms[formidx].elements[itemid].name = item;
                    216:             document.forms[formidx].elements[itemid].value = arg;
                    217:             document.forms[formidx].submit();
                    218:         } else {
                    219:             alert("$missing_formitem");
                    220:         }
1.1       raeburn   221:     }
                    222:     return;
                    223: }
                    224: 
                    225: </script>
                    226: END
1.9       raeburn   227:         $r->print('<h3>'.&mt('LON-CAPA account required').'</h3>'.
1.12      raeburn   228:                   &mt('You need to be logged into LON-CAPA to self-enroll in a course.').'<ul><li>'.&mt('If you have an account in LON-CAPA, [_1]log-in[_2].','<a href="javascript:setPath('."'$form','','phase','login'".')">','</a>').'</li>');
1.3       raeburn   229:         if (@cancreate > 1) {
                    230:             $r->print('<li>'.&mt('Otherwise:').'<br />');
                    231:             my $count = 0;
                    232:             foreach my $type ('sso','login','email') { 
1.4       raeburn   233:                 if (($count > 0) && ($count <@cancreate)) {
1.3       raeburn   234:                     $r->print('&nbsp;, '.&mt('or').'<br />');
                    235:                 }
                    236:                 if (grep(/^$type$/,@cancreate)) {
                    237:                     if ($type eq 'sso') {
1.12      raeburn   238:                         $r->print('&nbsp;&nbsp;'.&mt("if you have an institutional log-in, use your institution's [_1]single sign on page[_2] to log-in, then create an account",'<a href="javascript:setPath('."'$form','$sso_url','firsturl','$firsturl'".')">','</a>'));
1.3       raeburn   239:                     } elsif ($type eq 'login') {
1.12      raeburn   240:                         $r->print('&nbsp;&nbsp;'.&mt('if you have an institutional login, [_1]create an account[_2]','<a href="javascript:setPath('."'$form','$create_path','context','selfenroll_create'".')">','</a>'));
1.3       raeburn   241:                     } elsif ($type eq 'email') {
1.12      raeburn   242:                         $r->print('&nbsp;&nbsp;'.&mt('you can use an e-mail address to [_1]create an account[_2]','<a href="javascript:setPath('."'$form','$create_path','context','selfenroll_create'".')">','</a>'));
1.3       raeburn   243:                     }
                    244:                     $count ++;
                    245:                 }
                    246:             }
                    247:             $r->print('.<br />');
                    248:         } else {
                    249:             if ($cancreate[0] eq 'login') {
1.12      raeburn   250:                 $r->print('<li>'.&mt('Otherwise, if you have an institutional login, [_1]create an account[_2].','<a href="javascript:setPath('."'$form','$create_path','context','selfenroll_create'".')">','</a>'));
1.3       raeburn   251:             } elsif ($cancreate[0] eq 'email') {
1.12      raeburn   252:                 $r->print('<li>'.&mt('Otherwise, you will use an e-mail address to [_1]create an account[_2].','<a href="javascript:setPath('."'$form','$create_path','context','selfenroll_create'".')">','</a>'));
1.3       raeburn   253:             } elsif ($cancreate[0] eq 'sso') {
1.12      raeburn   254:                 $r->print('<li>'.&mt("Otherwise, use your institution's [_1]single sign on page[_2] to log-in, then create an account.",'<a href="javascript:setPath('."'$form','$sso_url','firsturl','$firsturl'".')">','</a>'));
1.3       raeburn   255:             }
1.1       raeburn   256:         }
                    257:         if ($selfenroll_types eq '*') {
1.9       raeburn   258:             $r->print('<br />'.&mt('You will be able to self enroll in this course once the account has been created.').'</li></ul>');
1.1       raeburn   259:         } else {
                    260:             $r->print('</li></ul>');
                    261:             my ($enrolltypes,$longtypes,$alldoms) = 
1.9       raeburn   262:                 &get_selfenroll_filters($selfenroll_types);
                    263:             $r->print(&print_selfenroll_types($longtypes));
1.1       raeburn   264:         }
                    265:     } else {
1.9       raeburn   266:         my $cdomdesc = &Apache::lonnet::domain($cdom,'description');
                    267:         $r->print('<h3>'.&mt('LON-CAPA account required').'</h3>');
                    268:         $r->print(&mt('You must [_1]log-in[_2] to LON-CAPA with an existing account to be able to enroll in this course, as account creation at this institution ([_3]) is not permitted when self-enrolling.','<a href="'.$login_path.'">','</a>',$cdomdesc));
1.1       raeburn   269:         if ($selfenroll_types ne '*') {
                    270:             my ($enrolltypes,$longtypes,$alldoms) =
                    271:                 &get_selfenroll_filters($selfenroll_types);
1.9       raeburn   272:             $r->print('<br />'.&print_selfenroll_types($longtypes));
1.1       raeburn   273:         }
1.9       raeburn   274:         my $displayurl = &escape($firsturl);
                    275:         $r->print(&mt('Submit a request to the LON-CAPA [_1]helpdesk[_2] for [_3] if you require assistance.','<a href="/adm/helpdesk?origurl='.$displayurl.'">','</a>',$cdomdesc));
1.1       raeburn   276:     }
1.12      raeburn   277:     $r->print("\n".'<form name="'.$form.'" method="post" action="">'."\n".
                    278:               '<input type="hidden" name="courseid" value="'.$courseid.'" />'."\n".
1.1       raeburn   279:               '<input type="hidden" name="setting" value=""/>'."\n".
1.12      raeburn   280:               &Apache::lonhtmlcommon::echo_form_input(['backto','courseid','context','phase'])."\n".
                    281:               '</form>'."\n");
                    282:               &page_footer($r);
1.1       raeburn   283:     return OK;
                    284: }
                    285: 
1.10      raeburn   286: sub page_header {
1.12      raeburn   287:     my ($r,$courseid,$js,$desc) = @_;
1.10      raeburn   288:     my $start_page =
                    289:         &Apache::loncommon::start_page('Self-enroll in a LON-CAPA course',$js,
                    290:                                        {'no_inline_link'   => 1,});
                    291:     $r->print($start_page);
                    292:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.12      raeburn   293:     &Apache::createaccount::selfenroll_crumbs($r,$courseid,$desc);
1.10      raeburn   294:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Self-enroll in course'));
                    295:     return;
                    296: }
                    297: 
1.12      raeburn   298: sub page_footer {
                    299:     my ($r) = @_;
                    300:     $r->print('<form name="backupcrumbs" method="post" action="/adm/coursecatalog">'."\n".
                    301:               &Apache::lonhtmlcommon::echo_form_input(['backto','courseid','phase','context']).
                    302:               '</form>'.&Apache::loncommon::end_page());
                    303:     return;
                    304: }
                    305: 
1.1       raeburn   306: sub validate_course_id {
1.12      raeburn   307:     my ($courseid) = @_;
                    308:     my ($cdom,$cnum) = ($env{'form.courseid'} =~ /^($match_domain)_($match_courseid)$/);
1.1       raeburn   309:     if ($cdom ne '' && $cnum ne '') {
                    310:         if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.12      raeburn   311:             return ('ok',$courseid);
1.1       raeburn   312:         }
                    313:     } 
                    314:     return;
                    315: }
                    316: 
                    317: sub user_is_known {
                    318:     my $known = 0;
                    319:     if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public'
                    320:         && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
                    321:         $known = 1;
                    322:     }
                    323:     return $known;
                    324: }
                    325: 
                    326: sub can_create {
                    327:     my ($cdom) = @_;
1.3       raeburn   328:     my @cancreate;
1.1       raeburn   329:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$cdom);
                    330:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
                    331:         if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
                    332:             if ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') {
1.3       raeburn   333:                 if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
                    334:                     @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
                    335:                 } else {
                    336:                     if (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') && 
                    337:                         ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
                    338:                         @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
                    339:                     }
                    340:                 }
1.1       raeburn   341:             }
                    342:         }
                    343:     }
1.3       raeburn   344:     return @cancreate;
1.1       raeburn   345: }
                    346: 
                    347: sub has_role {
                    348:     my (%curr_role) = @_;
                    349:     my $output;
                    350:     if ($curr_role{'status'} eq 'active') {
                    351:           my $rolelink = &jump_to_role($curr_role{'role'});
1.9       raeburn   352:           $output = &mt('You already have an active student role (section: "[_1]") in this course.',$curr_role{'section'}).'<br>'.$rolelink;
1.1       raeburn   353:     } elsif ($curr_role{'status'} eq 'future') {
1.9       raeburn   354:         $output = &mt('You have a student role (section: "[_1]") in this course which will become active [_2].',$curr_role{'section'},$curr_role{'start'});
1.1       raeburn   355:     }
                    356:     return $output;
                    357: }
                    358: 
                    359: sub process_self_enroll {
                    360:     my ($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,
                    361:         $selfenroll_access_start,$selfenroll_access_end,$selfenroll_section,$now) = @_;
                    362:     my $udom = $env{'user.domain'};
                    363:     my $uname = $env{'user.name'};
                    364:     my $selfenroll = 0;
1.9       raeburn   365:     my ($enrolltypes,$longtypes,$alldoms);
                    366:     if ($selfenroll_types eq '*') {
1.1       raeburn   367:         $selfenroll = 1;
1.9       raeburn   368:     } else {
                    369:         ($enrolltypes,$longtypes,$alldoms) =
                    370:             &get_selfenroll_filters($selfenroll_types,$udom);
                    371:         if ($alldoms) {
                    372:             $selfenroll = 1;
                    373:         } elsif (ref($enrolltypes) eq 'HASH') {
                    374:             if (ref($enrolltypes->{$udom}) eq 'ARRAY') {
                    375:                 if (grep(/^any$/,@{$enrolltypes->{$udom}})) {
                    376:                     $selfenroll = 1;
                    377:                 } else {
                    378:                     $selfenroll = &user_can_selfenroll($udom,$uname,$enrolltypes->{$udom});
1.1       raeburn   379:                 }
                    380:             }
                    381:         }
                    382:     }
                    383:     if ($selfenroll) {
                    384:         my $usec = $selfenroll_section;
                    385:         if ($selfenroll_section eq 'none') {
                    386:             $usec = '';
                    387:         }
                    388:         if ($selfenroll_registered) {
                    389:             my ($registered,$instsec,$message) = &check_registered($cdom,$cnum);
                    390:             $usec = $instsec;
                    391:             if (!$registered) {
1.9       raeburn   392:                 $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.&mt('Self-enrollment is restricted to students officially registered for this course.').'<br />');
1.1       raeburn   393:                 if ($message) {
                    394:                     $r->print($message);
                    395:                 } else {
                    396:                     $r->print(&mt('As you are not currently registered for this course, self-enrollment is unavailable.'));
                    397:                 }
                    398:                 return; 
                    399:             } 
                    400:         }
                    401:         my $enrollresult = 
                    402:             &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,
1.6       raeburn   403:                     undef,undef,$usec,$selfenroll_access_end,$selfenroll_access_start,
                    404:                    'manual',undef,$cdom.'_'.$cnum,$selfenroll);
1.1       raeburn   405:         if ($enrollresult eq 'ok') {
                    406:             my (%userroles,%newrole,%newgroups);
                    407:             my $role = 'st';
                    408:             my $area = '/'.$cdom.'/'.$cnum;
                    409:             my $spec = $role.'.'.$area;
                    410:             if ($usec ne '') {
                    411:                 $spec .= '/'.$usec;
                    412:                 $area .= '/'.$usec;
                    413:             }
                    414:             &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,
                    415:                                                 $area);
                    416:             &Apache::lonnet::set_userprivs(\%userroles,\%newrole,%newgroups);
                    417:             $userroles{'user.role.'.$spec} = $selfenroll_access_start.'.'.$selfenroll_access_end;
1.8       raeburn   418:             &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
1.9       raeburn   419:             $r->print('<h3>'.&mt('Enrollment process complete').'</h3>');
1.1       raeburn   420:             if ($selfenroll_access_end && $selfenroll_access_end <= $now) {
                    421:                 $r->print(&mt('The end date for access to this course for users who self-enroll has passed.').'<br />'.&mt('Consequently, although a new role was created for you in the course, it is an inactive role which does not provide access to the course.'));
                    422:             } else {
                    423:                 $r->print(&mt('Self-enrollment in this course was successful.').'<br />');
                    424:                 my $showstart = &Apache::lonlocal::locallocaltime($selfenroll_access_start);
                    425:                 my $showend = &Apache::lonlocal::locallocaltime($selfenroll_access_end);
                    426:                 if ($selfenroll_access_start && $selfenroll_access_start >$now) {
                    427:                     $r->print(&mt('The start date for access to this course for users who self-enroll has yet to be reached.').'<br />'.&mt('Consequently, although a new role was created for you in the course, you will not be able to select this role until [_1].',$showstart));
                    428:                 } else {
                    429:                     my $newrole = 'st./'.$cdom.'/'.$cnum;
                    430:                     if ($usec ne '') {
                    431:                         $newrole .= '/'.$usec;
                    432:                     }
                    433:                     my $rolelink = &jump_to_role($newrole);
                    434:                     $r->print(&mt('Your new role is available immediately, and will provide access to the course until [_1].',$showend).'<br /><br />'."\n".
                    435:                              $rolelink); 
                    436:                 }
                    437:             }
                    438:         } else {
1.9       raeburn   439:             $r->print('<h3>'.&mt('Enrollment incomplete').'</h3>'.
                    440:                       &mt('Self-enrollment in this course failed.'));
1.1       raeburn   441:             if ($enrollresult ne '') {
                    442:                 $r->print('<span class="LC_error">'.$enrollresult.'</span>');
                    443:             }
                    444:         }
                    445:     } else {
1.9       raeburn   446:         $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.
                    447:                   &mt('You are not permitted to enroll yourself in this course.').'<br />');
1.1       raeburn   448:         $r->print(&print_selfenroll_types($longtypes));
                    449:     }
                    450:     return;
                    451: }
                    452: 
1.9       raeburn   453: sub user_can_selfenroll {
                    454:     my ($udom,$uname,$domenrolltypes) = @_;
                    455:     my $selfenroll = 0;
1.13    ! raeburn   456:     my %userhash = &Apache::lonnet::userenvironment($udom,$uname,'inststatus');
1.9       raeburn   457:     my @inststatuses;
                    458:     if ($userhash{'inststatus'} eq '') {
                    459:         push(@inststatuses,'other');
                    460:     } else {
1.13    ! raeburn   461:         @inststatuses = split(':',$userhash{'inststatus'});
1.9       raeburn   462:     }
                    463:     foreach my $type (@inststatuses) {
                    464:         if (ref($domenrolltypes) eq 'ARRAY') {
                    465:             if (grep(/^\Q$type\E$/,@{$domenrolltypes})) {
                    466:                 $selfenroll = 1;
                    467:                 last;
                    468:             }
                    469:         }
                    470:     }
                    471:     return $selfenroll;
                    472: }
                    473: 
1.1       raeburn   474: sub jump_to_role {
                    475:     my ($role) = @_;
                    476:     my $output = <<"END";
                    477: <script type="text/javascript">
                    478: function SelectRole() {
                    479:     document.rolechoice.selectrole.value = '1';
                    480:     document.rolechoice.submit();
                    481: }
                    482: </script>
                    483: END
                    484:     $output .= '<a href="javascript:SelectRole()">'."\n". 
                    485:                &mt('Enter course now').'</a>'."\n".
1.9       raeburn   486:                '<form name="rolechoice" action="/adm/roles" method="post">'."\n".
1.1       raeburn   487:                '<input type="hidden" name="selectrole" value="" />'."\n".
                    488:                '<input type="hidden" name="'.$role.'" value="1" />'."\n".
                    489:                '</form>';
                    490:     return $output;
                    491: }
                    492: 
                    493: sub get_selfenroll_filters {
                    494:     my ($selfenroll_types,$domain) = @_;
                    495:     my (%enrolltypes,%longtypes,$alldoms);
                    496:     my @selfenrolldoms = split(/;/,$selfenroll_types);
                    497:     foreach my $item (@selfenrolldoms) {
                    498:         my ($selfdom,$type_str) = split(/:/,$item);
                    499:         if ($selfdom eq '*') {
                    500:             $alldoms = 1;
                    501:             last;
                    502:         }
                    503:         if ($domain ne '') {
                    504:             next if ($selfdom ne $domain);
                    505:         }
                    506:         if ($selfdom =~ /^$match_domain$/) {
                    507:             if (&Apache::lonnet::domain($selfdom) ne '') {
                    508:                 my @types = split(/,/,$type_str);
1.13    ! raeburn   509:                 my @unesc_types = map { &unescape($_); } @types;
1.1       raeburn   510:                 my ($othertitle,$usertypes,$types) =
                    511:                     &Apache::loncommon::sorted_inst_types($selfdom);
                    512:                 if (ref($usertypes) eq 'HASH') {
1.9       raeburn   513:                     my $anytype = 1;
                    514:                     foreach my $key (keys(%{$usertypes})) {
1.13    ! raeburn   515:                         if (!grep(/^\Q$key\E$/,@unesc_types)) {
1.9       raeburn   516:                             $anytype = 0;
                    517:                             last;
                    518:                         }
                    519:                     }
                    520:                     if ($anytype) {
                    521:                         if (!(grep(/^other$/,@types))) {
                    522:                             $anytype = 0;
                    523:                         }
                    524:                     }
                    525:                     $usertypes->{'any'} = &mt('Any users');
                    526:                     $usertypes->{'other'} = &mt('Others');
1.13    ! raeburn   527:                     my @showtypes;
1.9       raeburn   528:                     if ($anytype) {
                    529:                         @{$enrolltypes{$selfdom}} = ('any');
1.13    ! raeburn   530:                         @showtypes =  ('any');
1.9       raeburn   531:                     } else {
                    532:                         @{$enrolltypes{$selfdom}} = @types;
1.13    ! raeburn   533:                         @showtypes = @unesc_types;
1.9       raeburn   534:                     }
                    535:                     @{$longtypes{$selfdom}} = 
1.13    ! raeburn   536:                         map {$usertypes->{$_}} @showtypes;
1.1       raeburn   537:                 }
                    538:             }
                    539:         }
                    540:     }
                    541:     return (\%enrolltypes,\%longtypes,$alldoms);
                    542: }
                    543: 
                    544: sub print_selfenroll_types {
                    545:     my ($longtypes,$domain) = @_;
                    546:     my $output;
                    547:     if (ref($longtypes) eq 'HASH') {
1.9       raeburn   548:         if ($domain ne '') {
                    549:             my $domdesc = &Apache::lonnet::domain($domain,'description');
                    550:             if (ref($longtypes->{$domain}) eq 'ARRAY') {
                    551:                 if (grep(/^any$/,@{$longtypes->{$domain}})) {
                    552:                     $output = &mt('Self-enrollment in this course is available to any user affiliated with [_1].',$domdesc);
                    553:                 } else {
                    554:                     my $status_str = join(', ',@{$longtypes->{$domain}});
                    555:                     $output = &mt('Self-enrollment in this course is only available to users affiliated with [_1] who have the following status: "[_2]".',$domdesc,$status_str);
1.1       raeburn   556:                 }
                    557:             } else {
1.9       raeburn   558:                 $output = &mt('Self-enrollment is not currently available for this course for users affiliated with [_1].',$domdesc);
                    559:             }
                    560:         } elsif (keys(%{$longtypes}) > 0) {
1.12      raeburn   561:             $output = &mt('Self-enrollment in this course is only available to users affiliated with the following institutions, and who have the required status:').' <ul>'."\n";
1.9       raeburn   562:             foreach my $selfdom (sort(keys(%{$longtypes}))) {
                    563:                 my $selfdomdesc = &Apache::lonnet::domain($selfdom,'description');
                    564:                 if (ref($longtypes->{$selfdom}) eq 'ARRAY') {
1.12      raeburn   565:                     $output .= '<li>'.$selfdomdesc.': '.join(', ',@{$longtypes->{$selfdom}}).'</li>'."\n";
1.1       raeburn   566:                 }
                    567:             }
1.12      raeburn   568:             $output .= '</ul>'."\n";
1.1       raeburn   569:         } else {
1.5       bisitz    570:             $output = &mt('Self-enrollment is not currently available for this course.');
1.1       raeburn   571:         }
                    572:     }
                    573:     return $output;
                    574: }
                    575: 
                    576: sub check_registered {
                    577:     my ($cdom,$cnum) = @_;
                    578:     my ($registered,$instsec,$message);
                    579:     my %settings = &Apache::lonnet::get('environment',['internal.coursecode',
                    580:                                         'internal.sectionnums',
                    581:                                         'internal.crosslistings'],$cdom,$cnum);
                    582:     my (@allcourses,%LC_code,%affiliates,%reply);
                    583:     &Apache::loncommon::get_institutional_codes(\%settings,\@allcourses,\%LC_code);
                    584:     if (@allcourses > 0) {
                    585:         @{$affiliates{$cnum}} = @allcourses;
                    586:         my $outcome = &Apache::lonnet::fetch_enrollment_query('updatenow',\%affiliates,\%reply,$cdom,$cnum);
                    587:         if ($outcome eq 'ok') {
                    588:             if ($reply{$cnum} > 0) {
                    589:                 foreach my $class (@allcourses) {
                    590:                     my %enrolled;
                    591:                     my $dir = $Apache::lonnet::perlvar{'lonDaemons'};
                    592:                     my %place = &LONCAPA::Enrollment::place_hash();
                    593:                     &LONCAPA::Enrollment::parse_classlist($dir,$cdom,$cnum,$class,
                    594:                              \%place,$LC_code{$class},\%enrolled);
                    595:                     if (defined($enrolled{$env{'user.name'}})) {
                    596:                         $registered = 1;
                    597:                         $instsec = $LC_code{$class};
                    598:                         last;
                    599:                     }
                    600:                 }
                    601:             } else {
                    602:                 $message = &mt('Your registration status could not be verified.');
                    603:             }
                    604:         } else {
                    605:              $message = &mt('Your registration status could not determined, because a problem occurred retrieving data.');
                    606:         }
                    607:     } else {
                    608:         $message = &mt('As no institutional course sections are currently associated with this course, your registration status is undetermined.'); 
                    609:     }
                    610:     return ($registered,$instsec,$message);
                    611: }
                    612: 
                    613: 1;
                    614: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>