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

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

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