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

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

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