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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Allow users to self-enroll in a course
                      3: #
1.25.2.1! raeburn     4: # $Id: selfenroll.pm,v 1.25 2010/01/16 16:13:42 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.21      raeburn    37: use Apache::loncoursequeueadmin;
1.1       raeburn    38: use LONCAPA qw(:DEFAULT :match);
                     39: 
                     40: sub handler {
                     41:     my $r = shift;
                     42:     &Apache::loncommon::content_type($r,'text/html');
                     43:     $r->send_http_header;
                     44:     if ($r->header_only) {
                     45:         return OK;
                     46:     }
                     47:     my $handle = &Apache::lonnet::check_for_valid_session($r);
                     48:     my $lonidsdir=$r->dir_config('lonIDsDir');
1.10      raeburn    49:     my $lonhost = $r->dir_config('lonHostID');
1.1       raeburn    50:     if ($handle ne '') {
                     51:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
                     52:     }
                     53:     &Apache::lonacc::get_posted_cgi($r);
                     54:     &Apache::lonlocal::get_language_handle($r);
1.12      raeburn    55:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['courseid']);
                     56:     my $js = &Apache::createaccount::catreturn_js();
                     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,
1.15      raeburn    82:         $selfenroll_section,$selfenroll_future,%curr_role,$cdomdesc,
                     83:         $selfenroll_approval,$selfenroll_limit,$selfenroll_cap,
                     84:         $selfenroll_notifylist,$owner);
1.1       raeburn    85:     $selfenroll_types = $coursehash{'internal.selfenroll_types'};
                     86:     $selfenroll_registered =  $coursehash{'internal.selfenroll_registered'};
                     87:     $selfenroll_section = $coursehash{'internal.selfenroll_section'};
                     88:     $selfenroll_access_start =  $coursehash{'internal.selfenroll_start_access'};
                     89:     $selfenroll_access_end =  $coursehash{'internal.selfenroll_end_access'};
1.15      raeburn    90:     $selfenroll_limit =  $coursehash{'internal.selfenroll_limit'};
                     91:     $selfenroll_cap = $coursehash{'internal.selfenroll_cap'};
                     92:     $selfenroll_approval = $coursehash{'internal.selfenroll_approval'};
                     93:     $selfenroll_notifylist = $coursehash{'internal.selfenroll_notifylist'};
                     94:     $owner = $coursehash{'internal.courseowner'};
                     95:     my $nospace;
1.1       raeburn    96:     if ($selfenroll_types ne '') {
                     97:         my $start = $coursehash{'internal.selfenroll_start_date'};
                     98:         my $end = $coursehash{'internal.selfenroll_end_date'};
1.9       raeburn    99:         if (($start > 0 && $start < $now) && (($end == 0) || ($end > 0 && $end > $now))) {
1.15      raeburn   100:             if (($selfenroll_limit eq 'allstudents') || 
                    101:                 ($selfenroll_limit eq 'selfenroll')) {
                    102:                 $nospace = 
                    103:                     &enrollment_limit_check($selfenroll_limit,$selfenroll_cap,
                    104:                                             $cdom,$cnum);
                    105:                 if (!$nospace) {
                    106:                     $canenroll = 1; 
                    107:                 }
                    108:             } else {
                    109:                 $canenroll = 1;
                    110:             }
1.9       raeburn   111:         } elsif (($end == 0) || ($end > 0 && $end > $now)) {
                    112:             if ($start > $now) {
1.15      raeburn   113:                 if (($selfenroll_limit eq 'allstudents') ||
                    114:                     ($selfenroll_limit eq 'selfenroll')) {
                    115:                     $nospace = 
                    116:                         &enrollment_limit_check($selfenroll_limit,$cdom,$cnum);
                    117:                 }
                    118:                 if (!$nospace) {
                    119:                     $selfenroll_future = &Apache::lonlocal::locallocaltime($start);
                    120:                 }
1.9       raeburn   121:             }
1.1       raeburn   122:         }
                    123:     }
1.9       raeburn   124:     $knownuser = &user_is_known();
1.1       raeburn   125:     if (!$canenroll) {
1.9       raeburn   126:         $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3><span class="LC_warning">'.
                    127:                   &mt('Self-enrollment is not currently available for this course.').
                    128:                   '</span><br /><br />');
1.15      raeburn   129:         if ($nospace) {
                    130:             if ($selfenroll_limit eq 'allstudents') {
                    131:                 $r->print(&mt('The enrollment limit of [quant,_1,student] has been reached.',$selfenroll_cap));
                    132:             } else {
                    133:                 $r->print(&mt('The enrollment limit of [quant,_1,self-enrolled student] has been reached.',$selfenroll_cap));
                    134: 
                    135:             }
                    136:         } 
1.9       raeburn   137:         if ($selfenroll_types ne '') {
                    138:             if ($selfenroll_future ne '') {
                    139:                 if ($selfenroll_types eq '*') {
                    140:                     $r->print(&mt('Self-enrollment will become available starting [_1], and will be available to all LON-CAPA users.',$selfenroll_future).'<br />');
                    141:                 } else {
                    142:                     my ($enrolltypes,$longtypes,$alldoms);
                    143:                     if ($knownuser) {
                    144:                         &get_selfenroll_filters($selfenroll_types,$env{'user.domain'});
                    145:                         my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
                    146:                         if (ref($enrolltypes) eq 'HASH') {
                    147:                             if (ref($enrolltypes->{$env{'user.domain'}}) eq 'ARRAY') {
                    148:                                 if (grep(/^any$/,@{$enrolltypes->{$env{'user.domain'}}})) {
                    149:                                     $r->print(&mt('Self-enrollment will become available starting [_1], and will be available to all LON-CAPA users at your institution ([_2]).',
                    150:                                               $selfenroll_future,$domdesc).'<br />');
                    151: 
                    152:                                 } else {
                    153:                                     if (&user_can_selfenroll($env{'user.domain'},
                    154:                                                         $env{'user.name'},
                    155:                                                         $enrolltypes->{$env{'user.domain'}})) {
                    156:                                         $r->print(&mt('Self-enrollment will become available starting [_1]; please enroll at that time.',$selfenroll_future)); 
                    157:                                     } else {
                    158:                                         $r->print(&mt('Although self-enrollment will become available starting [_1], you are ineligible for enrollment.',$selfenroll_future).'<br />');
                    159:                                         $r->print(&print_selfenroll_types($longtypes,$env{'user.domain'}));
                    160:                                     }
                    161:                                 }
                    162:                             }
                    163:                         } 
                    164:                     } else {
                    165:                         $r->print(&mt('Self-enrollment will become available starting [_1].',
                    166:                                       $selfenroll_future).'<br />');
                    167:                         $r->print(&print_selfenroll_types($longtypes));
                    168:                     }
                    169:                 }
                    170:             } 
                    171:         }
1.1       raeburn   172:     }
                    173:     if ($knownuser) {
                    174:         foreach my $key (keys(%env)) {
                    175:             if ($key =~ m-^user\.role\.st\./$cdom/$cnum/?(\w*)$-) {
                    176:                 my $sec = $1;
                    177:                 my ($start,$end) = split(/\./,$env{$key});
                    178:                 my $status = 'active';
                    179:                 if (($end) && ($end<=$now)) {
                    180:                     $status = 'previous';
                    181:                 }
                    182:                 if (($start) && ($now<$start)) {
                    183:                     $status = 'future';
                    184:                 }
                    185:                 if ($status eq 'active' || $status eq 'future') {
                    186:                      $curr_role{'status'} = $status;
                    187:                      $curr_role{'section'} = $sec;
1.7       raeburn   188:                      if ($curr_role{'section'} eq '') {
                    189:                          $curr_role{'section'} = &mt('none'); 
                    190:                      }
1.1       raeburn   191:                      $curr_role{'start'} = &Apache::lonlocal::locallocaltime($start);
                    192:                      $curr_role{'role'} = 'st./'.$cdom.'/'.$cnum;
                    193:                      if ($sec ne '') {
                    194:                          $curr_role{'role'} .= '/'.$sec; 
                    195:                      }
                    196:                 }
                    197:             }
                    198:         }
                    199:     }
                    200:     if (!$canenroll) {
                    201:         if (keys(%curr_role)) {
                    202:             $r->print(&has_role(%curr_role));
                    203:         }
1.12      raeburn   204:         &page_footer($r);
1.1       raeburn   205:         return OK;
                    206:     }
1.3       raeburn   207:     @cancreate = &can_create($cdom);
1.12      raeburn   208:     my ($form,$login_path,$firsturl,$create_path,$sso_url,$missing_formitem);
                    209:     $form = 'logmein';
1.1       raeburn   210:     $login_path = '/adm/login';
1.12      raeburn   211:     $firsturl= '/adm/selfenroll?courseid='.$courseid;
1.1       raeburn   212:     $create_path = '/adm/createaccount';
                    213:     $sso_url = $r->dir_config('lonSSOReloginServer');
                    214:     if ($sso_url eq '') {
                    215:         $sso_url = $login_path;
                    216:     }
1.11      raeburn   217:     $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   218:     if ($knownuser) {
                    219:         if (keys(%curr_role)) {
1.9       raeburn   220:             $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.
                    221:                       '<span class="LC_warning">'.&has_role(%curr_role).'</span>');
1.12      raeburn   222:             &page_footer($r);
1.1       raeburn   223:             return OK;
                    224:         }
                    225:         &process_self_enroll($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,
                    226:                              $selfenroll_access_start,$selfenroll_access_end,
1.15      raeburn   227:                              $selfenroll_section,$now,$selfenroll_approval,
                    228:                              $selfenroll_notifylist,$owner);
1.10      raeburn   229:     } elsif ($env{'form.phase'} eq 'login') {
                    230:         my $submit_text = &mt('Log in');
                    231:         $r->print('<h3>'.&mt('Log-in to LON-CAPA').'</h3>');
                    232:         my $udom = &Apache::lonnet::default_login_domain();
1.12      raeburn   233:         $r->print(&Apache::createaccount::login_box($now,$lonhost,$courseid,
1.10      raeburn   234:                                            $submit_text,$udom,'selfenroll'));
1.12      raeburn   235:         $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>'));
                    236:         &page_footer($r);
1.10      raeburn   237:         return OK;
1.3       raeburn   238:     } elsif (@cancreate > 0) {
1.1       raeburn   239:         $r->print(<<END);
                    240: <script type="text/javascript">
1.12      raeburn   241: function setPath(formname,formaction,item,arg) {
                    242:     var formidx = getFormByName(formname);
                    243:     if (formidx > -1) {
                    244:         if (formaction != '') {
                    245:             document.forms[formidx].action = formaction;
                    246:         }
                    247:         var itemid = getIndexByName(formidx,'setting');
                    248:         if (itemid > -1) {
                    249:             document.forms[formidx].elements[itemid].name = item;
                    250:             document.forms[formidx].elements[itemid].value = arg;
                    251:             document.forms[formidx].submit();
                    252:         } else {
                    253:             alert("$missing_formitem");
                    254:         }
1.1       raeburn   255:     }
                    256:     return;
                    257: }
                    258: 
                    259: </script>
                    260: END
1.9       raeburn   261:         $r->print('<h3>'.&mt('LON-CAPA account required').'</h3>'.
1.12      raeburn   262:                   &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   263:         if (@cancreate > 1) {
                    264:             $r->print('<li>'.&mt('Otherwise:').'<br />');
                    265:             my $count = 0;
                    266:             foreach my $type ('sso','login','email') { 
1.4       raeburn   267:                 if (($count > 0) && ($count <@cancreate)) {
1.3       raeburn   268:                     $r->print('&nbsp;, '.&mt('or').'<br />');
                    269:                 }
                    270:                 if (grep(/^$type$/,@cancreate)) {
                    271:                     if ($type eq 'sso') {
1.12      raeburn   272:                         $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   273:                     } elsif ($type eq 'login') {
1.12      raeburn   274:                         $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   275:                     } elsif ($type eq 'email') {
1.12      raeburn   276:                         $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   277:                     }
                    278:                     $count ++;
                    279:                 }
                    280:             }
                    281:             $r->print('.<br />');
                    282:         } else {
                    283:             if ($cancreate[0] eq 'login') {
1.12      raeburn   284:                 $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   285:             } elsif ($cancreate[0] eq 'email') {
1.12      raeburn   286:                 $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   287:             } elsif ($cancreate[0] eq 'sso') {
1.12      raeburn   288:                 $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   289:             }
1.1       raeburn   290:         }
                    291:         if ($selfenroll_types eq '*') {
1.9       raeburn   292:             $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   293:         } else {
                    294:             $r->print('</li></ul>');
                    295:             my ($enrolltypes,$longtypes,$alldoms) = 
1.9       raeburn   296:                 &get_selfenroll_filters($selfenroll_types);
                    297:             $r->print(&print_selfenroll_types($longtypes));
1.1       raeburn   298:         }
                    299:     } else {
1.9       raeburn   300:         my $cdomdesc = &Apache::lonnet::domain($cdom,'description');
                    301:         $r->print('<h3>'.&mt('LON-CAPA account required').'</h3>');
                    302:         $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   303:         if ($selfenroll_types ne '*') {
                    304:             my ($enrolltypes,$longtypes,$alldoms) =
                    305:                 &get_selfenroll_filters($selfenroll_types);
1.9       raeburn   306:             $r->print('<br />'.&print_selfenroll_types($longtypes));
1.1       raeburn   307:         }
1.9       raeburn   308:         my $displayurl = &escape($firsturl);
                    309:         $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   310:     }
1.12      raeburn   311:     $r->print("\n".'<form name="'.$form.'" method="post" action="">'."\n".
                    312:               '<input type="hidden" name="courseid" value="'.$courseid.'" />'."\n".
1.1       raeburn   313:               '<input type="hidden" name="setting" value=""/>'."\n".
1.12      raeburn   314:               &Apache::lonhtmlcommon::echo_form_input(['backto','courseid','context','phase'])."\n".
                    315:               '</form>'."\n");
                    316:               &page_footer($r);
1.1       raeburn   317:     return OK;
                    318: }
                    319: 
1.15      raeburn   320: sub enrollment_limit_check {
                    321:     my ($selfenroll_limit,$selfenroll_cap,$cdom,$cnum) = @_;
                    322:     my $nospace = 0;
                    323:     my (%idx,%stucount);
                    324:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
                    325:     $idx{'type'} = &Apache::loncoursedata::CL_TYPE();
                    326:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
                    327:     while (my ($student,$data) = each(%$classlist)) {
                    328:         if (($data->[$idx{'status'}] eq 'Active') ||
                    329:             ($data->[$idx{'status'}] eq 'Future')) {
                    330:             if ($data->[$idx{'type'}] eq 'selfenroll') {
                    331:                 $stucount{'selfenroll'} ++;
                    332:             }
                    333:             $stucount{'allstudents'} ++;
                    334:         }
                    335:     }
                    336:     if ($stucount{$selfenroll_limit} >= $selfenroll_cap) {
                    337:         $nospace = 1;
                    338:     }
                    339:     return $nospace;
                    340: }
                    341: 
1.10      raeburn   342: sub page_header {
1.12      raeburn   343:     my ($r,$courseid,$js,$desc) = @_;
1.10      raeburn   344:     my $start_page =
                    345:         &Apache::loncommon::start_page('Self-enroll in a LON-CAPA course',$js,
                    346:                                        {'no_inline_link'   => 1,});
                    347:     $r->print($start_page);
                    348:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.12      raeburn   349:     &Apache::createaccount::selfenroll_crumbs($r,$courseid,$desc);
1.10      raeburn   350:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Self-enroll in course'));
                    351:     return;
                    352: }
                    353: 
1.12      raeburn   354: sub page_footer {
                    355:     my ($r) = @_;
                    356:     $r->print('<form name="backupcrumbs" method="post" action="/adm/coursecatalog">'."\n".
                    357:               &Apache::lonhtmlcommon::echo_form_input(['backto','courseid','phase','context']).
                    358:               '</form>'.&Apache::loncommon::end_page());
                    359:     return;
                    360: }
                    361: 
1.1       raeburn   362: sub validate_course_id {
1.12      raeburn   363:     my ($courseid) = @_;
                    364:     my ($cdom,$cnum) = ($env{'form.courseid'} =~ /^($match_domain)_($match_courseid)$/);
1.1       raeburn   365:     if ($cdom ne '' && $cnum ne '') {
                    366:         if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.12      raeburn   367:             return ('ok',$courseid);
1.1       raeburn   368:         }
                    369:     } 
                    370:     return;
                    371: }
                    372: 
                    373: sub user_is_known {
                    374:     my $known = 0;
                    375:     if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public'
                    376:         && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
                    377:         $known = 1;
                    378:     }
                    379:     return $known;
                    380: }
                    381: 
                    382: sub can_create {
                    383:     my ($cdom) = @_;
1.3       raeburn   384:     my @cancreate;
1.1       raeburn   385:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$cdom);
                    386:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
                    387:         if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
                    388:             if ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') {
1.3       raeburn   389:                 if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
                    390:                     @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
                    391:                 } else {
                    392:                     if (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') && 
                    393:                         ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
                    394:                         @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
                    395:                     }
                    396:                 }
1.1       raeburn   397:             }
                    398:         }
                    399:     }
1.3       raeburn   400:     return @cancreate;
1.1       raeburn   401: }
                    402: 
                    403: sub has_role {
                    404:     my (%curr_role) = @_;
                    405:     my $output;
                    406:     if ($curr_role{'status'} eq 'active') {
                    407:           my $rolelink = &jump_to_role($curr_role{'role'});
1.18      bisitz    408:           $output = &mt('You already have an active student role (section: "[_1]") in this course.',$curr_role{'section'}).'<br />'.$rolelink;
1.1       raeburn   409:     } elsif ($curr_role{'status'} eq 'future') {
1.9       raeburn   410:         $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   411:     }
                    412:     return $output;
                    413: }
                    414: 
                    415: sub process_self_enroll {
                    416:     my ($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,
1.15      raeburn   417:         $selfenroll_access_start,$selfenroll_access_end,$selfenroll_section,
                    418:         $now,$selfenroll_approval,$selfenroll_notifylist,$owner) = @_;
1.1       raeburn   419:     my $udom = $env{'user.domain'};
                    420:     my $uname = $env{'user.name'};
                    421:     my $selfenroll = 0;
1.9       raeburn   422:     my ($enrolltypes,$longtypes,$alldoms);
                    423:     if ($selfenroll_types eq '*') {
1.1       raeburn   424:         $selfenroll = 1;
1.9       raeburn   425:     } else {
                    426:         ($enrolltypes,$longtypes,$alldoms) =
                    427:             &get_selfenroll_filters($selfenroll_types,$udom);
                    428:         if ($alldoms) {
                    429:             $selfenroll = 1;
                    430:         } elsif (ref($enrolltypes) eq 'HASH') {
                    431:             if (ref($enrolltypes->{$udom}) eq 'ARRAY') {
                    432:                 if (grep(/^any$/,@{$enrolltypes->{$udom}})) {
                    433:                     $selfenroll = 1;
                    434:                 } else {
                    435:                     $selfenroll = &user_can_selfenroll($udom,$uname,$enrolltypes->{$udom});
1.1       raeburn   436:                 }
                    437:             }
                    438:         }
                    439:     }
                    440:     if ($selfenroll) {
                    441:         my $usec = $selfenroll_section;
                    442:         if ($selfenroll_section eq 'none') {
                    443:             $usec = '';
                    444:         }
                    445:         if ($selfenroll_registered) {
                    446:             my ($registered,$instsec,$message) = &check_registered($cdom,$cnum);
                    447:             $usec = $instsec;
                    448:             if (!$registered) {
1.9       raeburn   449:                 $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.&mt('Self-enrollment is restricted to students officially registered for this course.').'<br />');
1.1       raeburn   450:                 if ($message) {
                    451:                     $r->print($message);
                    452:                 } else {
                    453:                     $r->print(&mt('As you are not currently registered for this course, self-enrollment is unavailable.'));
                    454:                 }
                    455:                 return; 
                    456:             } 
                    457:         }
1.15      raeburn   458:         if ($selfenroll_approval) {
                    459:             my $outcome = 
                    460:                 &store_selfenroll_request($udom,$uname,$usec,$cdom,$cnum,
                    461:                                           $selfenroll_notifylist,$owner);
                    462:             $r->print($outcome);
                    463:         } else {
                    464:             my $enrollresult = 
                    465:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,
                    466:                         undef,undef,$usec,$selfenroll_access_end,$selfenroll_access_start,
                    467:                        'selfenroll',undef,$cdom.'_'.$cnum,$selfenroll);
                    468:             if ($enrollresult eq 'ok') {
                    469:                 my (%userroles,%newrole,%newgroups);
                    470:                 my $role = 'st';
                    471:                 my $area = '/'.$cdom.'/'.$cnum;
                    472:                 my $spec = $role.'.'.$area;
                    473:                 if ($usec ne '') {
                    474:                     $spec .= '/'.$usec;
                    475:                     $area .= '/'.$usec;
                    476:                 }
                    477:                 &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,
                    478:                                                     $area);
1.23      raeburn   479:                 &Apache::lonnet::set_userprivs(\%userroles,\%newrole,\%newgroups);
1.15      raeburn   480:                 $userroles{'user.role.'.$spec} = $selfenroll_access_start.'.'.$selfenroll_access_end;
                    481:                 &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
                    482:                 $r->print('<h3>'.&mt('Enrollment process complete').'</h3>');
                    483:                 if ($selfenroll_access_end && $selfenroll_access_end <= $now) {
                    484:                     $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.'));
1.1       raeburn   485:                 } else {
1.15      raeburn   486:                     $r->print(&mt('Self-enrollment in this course was successful.').'<br />');
                    487:                     my $showstart = &Apache::lonlocal::locallocaltime($selfenroll_access_start);
                    488:                     my $showend = &Apache::lonlocal::locallocaltime($selfenroll_access_end);
                    489:                     if ($selfenroll_access_start && $selfenroll_access_start >$now) {
                    490:                         $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));
                    491:                     } else {
                    492:                         my $newrole = 'st./'.$cdom.'/'.$cnum;
                    493:                         if ($usec ne '') {
                    494:                             $newrole .= '/'.$usec;
                    495:                         }
                    496:                         my $rolelink = &jump_to_role($newrole);
                    497:                         $r->print(&mt('Your new role is available immediately, and will provide access to the course until [_1].',$showend).'<br /><br />'."\n".
                    498:                                  $rolelink); 
1.1       raeburn   499:                     }
                    500:                 }
1.15      raeburn   501:             } else {
                    502:                 $r->print('<h3>'.&mt('Enrollment incomplete').'</h3>'.
                    503:                           &mt('Self-enrollment in this course failed.'));
                    504:                 if ($enrollresult ne '') {
                    505:                     $r->print('<span class="LC_error">'.$enrollresult.'</span>');
                    506:                 }
1.1       raeburn   507:             }
                    508:         }
                    509:     } else {
1.9       raeburn   510:         $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.
                    511:                   &mt('You are not permitted to enroll yourself in this course.').'<br />');
1.1       raeburn   512:         $r->print(&print_selfenroll_types($longtypes));
                    513:     }
                    514:     return;
                    515: }
                    516: 
1.9       raeburn   517: sub user_can_selfenroll {
                    518:     my ($udom,$uname,$domenrolltypes) = @_;
                    519:     my $selfenroll = 0;
1.13      raeburn   520:     my %userhash = &Apache::lonnet::userenvironment($udom,$uname,'inststatus');
1.9       raeburn   521:     my @inststatuses;
                    522:     if ($userhash{'inststatus'} eq '') {
                    523:         push(@inststatuses,'other');
                    524:     } else {
1.13      raeburn   525:         @inststatuses = split(':',$userhash{'inststatus'});
1.9       raeburn   526:     }
                    527:     foreach my $type (@inststatuses) {
                    528:         if (ref($domenrolltypes) eq 'ARRAY') {
                    529:             if (grep(/^\Q$type\E$/,@{$domenrolltypes})) {
                    530:                 $selfenroll = 1;
                    531:                 last;
                    532:             }
                    533:         }
                    534:     }
                    535:     return $selfenroll;
                    536: }
                    537: 
1.15      raeburn   538: sub store_selfenroll_request {
                    539:     my ($udom,$uname,$usec,$cdom,$cnum,$selfenroll_notifylist,$owner) = @_;
                    540:     my $namespace = 'selfenrollrequests';
                    541:     my $output;
                    542:     my $now = time;
                    543:     my %existing = 
                    544:         &Apache::lonnet::get($namespace,[$uname.':'.$udom],$cdom,$cnum);
                    545:     if ($existing{$uname.':'.$udom}) {
1.24      raeburn   546:         $output = &mt('A self-enrollment request already exists for you for this course.').'<br />'.&mt('Your earlier request is in a queue awaiting action by a Course Coordinator.').
                    547:                   '<br /><br />'.&Apache::loncoursequeueadmin::queued_selfenrollment();
1.15      raeburn   548:     } else {
                    549:         my %selfenroll = (
                    550:                             $uname.':'.$udom => $now.':'.$usec,
                    551:                          );
                    552:         my $putresult = &Apache::lonnet::put($namespace,\%selfenroll,$cdom,$cnum);
                    553:         if ($putresult eq 'ok') {
1.16      raeburn   554:             my %userenroll = (
                    555:                                $cdom.'_'.$cnum =>  {
                    556:                                                       timestamp => $now,
                    557:                                                       section   => $usec,
                    558:                                                       status    => 'request', 
                    559:                                                    });
                    560:             my $warning;
                    561:             my $userresult = &Apache::lonnet::put($namespace,\%userenroll,$udom,$uname);
                    562:             if ($userresult ne 'ok') {
                    563:                 $warning = &mt('An error occurred saving a personal record of your request.');
                    564:             }
1.15      raeburn   565:             $output = &mt('Your request for self-enrollment has been recorded.').'<br />'.
1.19      truskell  566:                       &mt('A message will be sent to your LON-CAPA account when the course coordinator takes action on your request.').'<br />'.
1.25.2.1! raeburn   567:                       &mt('To access your LON-CAPA message, go to the Main Menu and click on "Send and Receive Messages".');
1.15      raeburn   568:             my %emails = &Apache::loncommon::getemails($uname,$udom);
                    569:             if (($emails{'permanentemail'} ne '') || ($emails{'notification'} ne '')) {
                    570:                 my $address = $emails{'permanentemail'};
                    571:                 if ($address eq '') {
                    572:                     $address = $emails{'notification'};
                    573:                 }
1.16      raeburn   574:                 $output.= &mt('An e-mail will also be sent to: [_1] when this occurs.',$address).'<br />';
                    575:             }
                    576:             if ($warning) { 
                    577:                 $output .= '<span class="LC_warning">'.$warning.'</span><br />';
1.15      raeburn   578:             }
1.24      raeburn   579: 
1.25.2.1! raeburn   580:             $output .= '<br />'.&Apache::loncoursequeueadmin::queued_selfenrollment();
1.24      raeburn   581: 
1.15      raeburn   582:             if ($selfenroll_notifylist) {
                    583:                 my $fullname = &Apache::loncommon::plainname($uname,$udom);
                    584:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'_'.$cnum);
                    585:                 my $coursedesc = $courseinfo{'description'};
1.22      raeburn   586:                 &Apache::loncoursequeueadmin::send_selfserve_notification(
1.21      raeburn   587:                     $selfenroll_notifylist,$fullname,$cdom.'_'.$cnum,
                    588:                     $coursedesc,$now,'selfenrollreq',$owner);
1.15      raeburn   589:             }
                    590:         } else {
                    591:             $output = '<span class="LC_error">'.&mt('An error occurred when recording your request.').'</span>';
                    592: 
                    593:         }
                    594:     }
                    595:     return $output;
                    596: }
                    597: 
1.1       raeburn   598: sub jump_to_role {
                    599:     my ($role) = @_;
                    600:     my $output = <<"END";
                    601: <script type="text/javascript">
                    602: function SelectRole() {
                    603:     document.rolechoice.selectrole.value = '1';
                    604:     document.rolechoice.submit();
                    605: }
                    606: </script>
                    607: END
                    608:     $output .= '<a href="javascript:SelectRole()">'."\n". 
                    609:                &mt('Enter course now').'</a>'."\n".
1.9       raeburn   610:                '<form name="rolechoice" action="/adm/roles" method="post">'."\n".
1.1       raeburn   611:                '<input type="hidden" name="selectrole" value="" />'."\n".
                    612:                '<input type="hidden" name="'.$role.'" value="1" />'."\n".
                    613:                '</form>';
                    614:     return $output;
                    615: }
                    616: 
                    617: sub get_selfenroll_filters {
                    618:     my ($selfenroll_types,$domain) = @_;
                    619:     my (%enrolltypes,%longtypes,$alldoms);
                    620:     my @selfenrolldoms = split(/;/,$selfenroll_types);
                    621:     foreach my $item (@selfenrolldoms) {
                    622:         my ($selfdom,$type_str) = split(/:/,$item);
                    623:         if ($selfdom eq '*') {
                    624:             $alldoms = 1;
                    625:             last;
                    626:         }
                    627:         if ($domain ne '') {
                    628:             next if ($selfdom ne $domain);
                    629:         }
                    630:         if ($selfdom =~ /^$match_domain$/) {
                    631:             if (&Apache::lonnet::domain($selfdom) ne '') {
                    632:                 my @types = split(/,/,$type_str);
1.13      raeburn   633:                 my @unesc_types = map { &unescape($_); } @types;
1.1       raeburn   634:                 my ($othertitle,$usertypes,$types) =
                    635:                     &Apache::loncommon::sorted_inst_types($selfdom);
                    636:                 if (ref($usertypes) eq 'HASH') {
1.9       raeburn   637:                     my $anytype = 1;
                    638:                     foreach my $key (keys(%{$usertypes})) {
1.13      raeburn   639:                         if (!grep(/^\Q$key\E$/,@unesc_types)) {
1.9       raeburn   640:                             $anytype = 0;
                    641:                             last;
                    642:                         }
                    643:                     }
                    644:                     if ($anytype) {
                    645:                         if (!(grep(/^other$/,@types))) {
                    646:                             $anytype = 0;
                    647:                         }
                    648:                     }
                    649:                     $usertypes->{'any'} = &mt('Any users');
                    650:                     $usertypes->{'other'} = &mt('Others');
1.13      raeburn   651:                     my @showtypes;
1.9       raeburn   652:                     if ($anytype) {
                    653:                         @{$enrolltypes{$selfdom}} = ('any');
1.13      raeburn   654:                         @showtypes =  ('any');
1.9       raeburn   655:                     } else {
                    656:                         @{$enrolltypes{$selfdom}} = @types;
1.13      raeburn   657:                         @showtypes = @unesc_types;
1.9       raeburn   658:                     }
                    659:                     @{$longtypes{$selfdom}} = 
1.13      raeburn   660:                         map {$usertypes->{$_}} @showtypes;
1.1       raeburn   661:                 }
                    662:             }
                    663:         }
                    664:     }
                    665:     return (\%enrolltypes,\%longtypes,$alldoms);
                    666: }
                    667: 
                    668: sub print_selfenroll_types {
                    669:     my ($longtypes,$domain) = @_;
                    670:     my $output;
                    671:     if (ref($longtypes) eq 'HASH') {
1.9       raeburn   672:         if ($domain ne '') {
                    673:             my $domdesc = &Apache::lonnet::domain($domain,'description');
                    674:             if (ref($longtypes->{$domain}) eq 'ARRAY') {
                    675:                 if (grep(/^any$/,@{$longtypes->{$domain}})) {
                    676:                     $output = &mt('Self-enrollment in this course is available to any user affiliated with [_1].',$domdesc);
                    677:                 } else {
                    678:                     my $status_str = join(', ',@{$longtypes->{$domain}});
                    679:                     $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   680:                 }
                    681:             } else {
1.9       raeburn   682:                 $output = &mt('Self-enrollment is not currently available for this course for users affiliated with [_1].',$domdesc);
                    683:             }
                    684:         } elsif (keys(%{$longtypes}) > 0) {
1.12      raeburn   685:             $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   686:             foreach my $selfdom (sort(keys(%{$longtypes}))) {
                    687:                 my $selfdomdesc = &Apache::lonnet::domain($selfdom,'description');
                    688:                 if (ref($longtypes->{$selfdom}) eq 'ARRAY') {
1.12      raeburn   689:                     $output .= '<li>'.$selfdomdesc.': '.join(', ',@{$longtypes->{$selfdom}}).'</li>'."\n";
1.1       raeburn   690:                 }
                    691:             }
1.12      raeburn   692:             $output .= '</ul>'."\n";
1.1       raeburn   693:         } else {
1.5       bisitz    694:             $output = &mt('Self-enrollment is not currently available for this course.');
1.1       raeburn   695:         }
                    696:     }
                    697:     return $output;
                    698: }
                    699: 
                    700: sub check_registered {
                    701:     my ($cdom,$cnum) = @_;
                    702:     my ($registered,$instsec,$message);
                    703:     my %settings = &Apache::lonnet::get('environment',['internal.coursecode',
                    704:                                         'internal.sectionnums',
                    705:                                         'internal.crosslistings'],$cdom,$cnum);
                    706:     my (@allcourses,%LC_code,%affiliates,%reply);
                    707:     &Apache::loncommon::get_institutional_codes(\%settings,\@allcourses,\%LC_code);
                    708:     if (@allcourses > 0) {
                    709:         @{$affiliates{$cnum}} = @allcourses;
                    710:         my $outcome = &Apache::lonnet::fetch_enrollment_query('updatenow',\%affiliates,\%reply,$cdom,$cnum);
                    711:         if ($outcome eq 'ok') {
                    712:             if ($reply{$cnum} > 0) {
                    713:                 foreach my $class (@allcourses) {
                    714:                     my %enrolled;
                    715:                     my $dir = $Apache::lonnet::perlvar{'lonDaemons'};
                    716:                     my %place = &LONCAPA::Enrollment::place_hash();
                    717:                     &LONCAPA::Enrollment::parse_classlist($dir,$cdom,$cnum,$class,
                    718:                              \%place,$LC_code{$class},\%enrolled);
                    719:                     if (defined($enrolled{$env{'user.name'}})) {
                    720:                         $registered = 1;
                    721:                         $instsec = $LC_code{$class};
                    722:                         last;
                    723:                     }
                    724:                 }
                    725:             } else {
                    726:                 $message = &mt('Your registration status could not be verified.');
                    727:             }
                    728:         } else {
                    729:              $message = &mt('Your registration status could not determined, because a problem occurred retrieving data.');
                    730:         }
                    731:     } else {
                    732:         $message = &mt('As no institutional course sections are currently associated with this course, your registration status is undetermined.'); 
                    733:     }
                    734:     return ($registered,$instsec,$message);
                    735: }
                    736: 
                    737: 1;
                    738: 

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