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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Allow users to self-enroll in a course
                      3: #
1.25.6.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.25.6.1! raeburn    55:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['courseid','usertype']);
1.12      raeburn    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:         }
1.25.6.1! raeburn   445:         unless (($usec ne '') && ($env{'form.usertype'} eq 'new')) {
        !           446:             $usec = 'sp12';
        !           447:         }
1.1       raeburn   448:         if ($selfenroll_registered) {
                    449:             my ($registered,$instsec,$message) = &check_registered($cdom,$cnum);
                    450:             $usec = $instsec;
                    451:             if (!$registered) {
1.9       raeburn   452:                 $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.&mt('Self-enrollment is restricted to students officially registered for this course.').'<br />');
1.1       raeburn   453:                 if ($message) {
                    454:                     $r->print($message);
                    455:                 } else {
                    456:                     $r->print(&mt('As you are not currently registered for this course, self-enrollment is unavailable.'));
                    457:                 }
                    458:                 return; 
                    459:             } 
                    460:         }
1.15      raeburn   461:         if ($selfenroll_approval) {
                    462:             my $outcome = 
                    463:                 &store_selfenroll_request($udom,$uname,$usec,$cdom,$cnum,
                    464:                                           $selfenroll_notifylist,$owner);
                    465:             $r->print($outcome);
                    466:         } else {
                    467:             my $enrollresult = 
                    468:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,
                    469:                         undef,undef,$usec,$selfenroll_access_end,$selfenroll_access_start,
                    470:                        'selfenroll',undef,$cdom.'_'.$cnum,$selfenroll);
                    471:             if ($enrollresult eq 'ok') {
                    472:                 my (%userroles,%newrole,%newgroups);
                    473:                 my $role = 'st';
                    474:                 my $area = '/'.$cdom.'/'.$cnum;
                    475:                 my $spec = $role.'.'.$area;
                    476:                 if ($usec ne '') {
                    477:                     $spec .= '/'.$usec;
                    478:                     $area .= '/'.$usec;
                    479:                 }
                    480:                 &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,
                    481:                                                     $area);
1.23      raeburn   482:                 &Apache::lonnet::set_userprivs(\%userroles,\%newrole,\%newgroups);
1.15      raeburn   483:                 $userroles{'user.role.'.$spec} = $selfenroll_access_start.'.'.$selfenroll_access_end;
                    484:                 &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
                    485:                 $r->print('<h3>'.&mt('Enrollment process complete').'</h3>');
                    486:                 if ($selfenroll_access_end && $selfenroll_access_end <= $now) {
                    487:                     $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   488:                 } else {
1.15      raeburn   489:                     $r->print(&mt('Self-enrollment in this course was successful.').'<br />');
                    490:                     my $showstart = &Apache::lonlocal::locallocaltime($selfenroll_access_start);
                    491:                     my $showend = &Apache::lonlocal::locallocaltime($selfenroll_access_end);
                    492:                     if ($selfenroll_access_start && $selfenroll_access_start >$now) {
                    493:                         $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));
                    494:                     } else {
                    495:                         my $newrole = 'st./'.$cdom.'/'.$cnum;
                    496:                         if ($usec ne '') {
                    497:                             $newrole .= '/'.$usec;
                    498:                         }
                    499:                         my $rolelink = &jump_to_role($newrole);
                    500:                         $r->print(&mt('Your new role is available immediately, and will provide access to the course until [_1].',$showend).'<br /><br />'."\n".
                    501:                                  $rolelink); 
1.1       raeburn   502:                     }
                    503:                 }
1.15      raeburn   504:             } else {
                    505:                 $r->print('<h3>'.&mt('Enrollment incomplete').'</h3>'.
                    506:                           &mt('Self-enrollment in this course failed.'));
                    507:                 if ($enrollresult ne '') {
                    508:                     $r->print('<span class="LC_error">'.$enrollresult.'</span>');
                    509:                 }
1.1       raeburn   510:             }
                    511:         }
                    512:     } else {
1.9       raeburn   513:         $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.
                    514:                   &mt('You are not permitted to enroll yourself in this course.').'<br />');
1.1       raeburn   515:         $r->print(&print_selfenroll_types($longtypes));
                    516:     }
                    517:     return;
                    518: }
                    519: 
1.9       raeburn   520: sub user_can_selfenroll {
                    521:     my ($udom,$uname,$domenrolltypes) = @_;
                    522:     my $selfenroll = 0;
1.13      raeburn   523:     my %userhash = &Apache::lonnet::userenvironment($udom,$uname,'inststatus');
1.9       raeburn   524:     my @inststatuses;
                    525:     if ($userhash{'inststatus'} eq '') {
                    526:         push(@inststatuses,'other');
                    527:     } else {
1.13      raeburn   528:         @inststatuses = split(':',$userhash{'inststatus'});
1.9       raeburn   529:     }
                    530:     foreach my $type (@inststatuses) {
                    531:         if (ref($domenrolltypes) eq 'ARRAY') {
                    532:             if (grep(/^\Q$type\E$/,@{$domenrolltypes})) {
                    533:                 $selfenroll = 1;
                    534:                 last;
                    535:             }
                    536:         }
                    537:     }
                    538:     return $selfenroll;
                    539: }
                    540: 
1.15      raeburn   541: sub store_selfenroll_request {
                    542:     my ($udom,$uname,$usec,$cdom,$cnum,$selfenroll_notifylist,$owner) = @_;
                    543:     my $namespace = 'selfenrollrequests';
                    544:     my $output;
                    545:     my $now = time;
                    546:     my %existing = 
                    547:         &Apache::lonnet::get($namespace,[$uname.':'.$udom],$cdom,$cnum);
                    548:     if ($existing{$uname.':'.$udom}) {
1.24      raeburn   549:         $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.').
                    550:                   '<br /><br />'.&Apache::loncoursequeueadmin::queued_selfenrollment();
1.15      raeburn   551:     } else {
                    552:         my %selfenroll = (
                    553:                             $uname.':'.$udom => $now.':'.$usec,
                    554:                          );
                    555:         my $putresult = &Apache::lonnet::put($namespace,\%selfenroll,$cdom,$cnum);
                    556:         if ($putresult eq 'ok') {
1.16      raeburn   557:             my %userenroll = (
                    558:                                $cdom.'_'.$cnum =>  {
                    559:                                                       timestamp => $now,
                    560:                                                       section   => $usec,
                    561:                                                       status    => 'request', 
                    562:                                                    });
                    563:             my $warning;
                    564:             my $userresult = &Apache::lonnet::put($namespace,\%userenroll,$udom,$uname);
                    565:             if ($userresult ne 'ok') {
                    566:                 $warning = &mt('An error occurred saving a personal record of your request.');
                    567:             }
1.15      raeburn   568:             $output = &mt('Your request for self-enrollment has been recorded.').'<br />'.
1.19      truskell  569:                       &mt('A message will be sent to your LON-CAPA account when the course coordinator takes action on your request.').'<br />'.
1.20      bisitz    570:                       &mt('To access your LON-CAPA message, go to the Main Menu and click on "Send and Receive Messages".').'<br />';
1.15      raeburn   571:             my %emails = &Apache::loncommon::getemails($uname,$udom);
                    572:             if (($emails{'permanentemail'} ne '') || ($emails{'notification'} ne '')) {
                    573:                 my $address = $emails{'permanentemail'};
                    574:                 if ($address eq '') {
                    575:                     $address = $emails{'notification'};
                    576:                 }
1.16      raeburn   577:                 $output.= &mt('An e-mail will also be sent to: [_1] when this occurs.',$address).'<br />';
                    578:             }
                    579:             if ($warning) { 
                    580:                 $output .= '<span class="LC_warning">'.$warning.'</span><br />';
1.15      raeburn   581:             }
1.24      raeburn   582: 
1.25      raeburn   583:             $output .= &Apache::loncoursequeueadmin::queued_selfenrollment();
1.24      raeburn   584: 
1.15      raeburn   585:             if ($selfenroll_notifylist) {
                    586:                 my $fullname = &Apache::loncommon::plainname($uname,$udom);
                    587:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'_'.$cnum);
                    588:                 my $coursedesc = $courseinfo{'description'};
1.22      raeburn   589:                 &Apache::loncoursequeueadmin::send_selfserve_notification(
1.21      raeburn   590:                     $selfenroll_notifylist,$fullname,$cdom.'_'.$cnum,
                    591:                     $coursedesc,$now,'selfenrollreq',$owner);
1.15      raeburn   592:             }
                    593:         } else {
                    594:             $output = '<span class="LC_error">'.&mt('An error occurred when recording your request.').'</span>';
                    595: 
                    596:         }
                    597:     }
                    598:     return $output;
                    599: }
                    600: 
1.1       raeburn   601: sub jump_to_role {
                    602:     my ($role) = @_;
                    603:     my $output = <<"END";
                    604: <script type="text/javascript">
                    605: function SelectRole() {
                    606:     document.rolechoice.selectrole.value = '1';
                    607:     document.rolechoice.submit();
                    608: }
                    609: </script>
                    610: END
                    611:     $output .= '<a href="javascript:SelectRole()">'."\n". 
                    612:                &mt('Enter course now').'</a>'."\n".
1.9       raeburn   613:                '<form name="rolechoice" action="/adm/roles" method="post">'."\n".
1.1       raeburn   614:                '<input type="hidden" name="selectrole" value="" />'."\n".
                    615:                '<input type="hidden" name="'.$role.'" value="1" />'."\n".
                    616:                '</form>';
                    617:     return $output;
                    618: }
                    619: 
                    620: sub get_selfenroll_filters {
                    621:     my ($selfenroll_types,$domain) = @_;
                    622:     my (%enrolltypes,%longtypes,$alldoms);
                    623:     my @selfenrolldoms = split(/;/,$selfenroll_types);
                    624:     foreach my $item (@selfenrolldoms) {
                    625:         my ($selfdom,$type_str) = split(/:/,$item);
                    626:         if ($selfdom eq '*') {
                    627:             $alldoms = 1;
                    628:             last;
                    629:         }
                    630:         if ($domain ne '') {
                    631:             next if ($selfdom ne $domain);
                    632:         }
                    633:         if ($selfdom =~ /^$match_domain$/) {
                    634:             if (&Apache::lonnet::domain($selfdom) ne '') {
                    635:                 my @types = split(/,/,$type_str);
1.13      raeburn   636:                 my @unesc_types = map { &unescape($_); } @types;
1.1       raeburn   637:                 my ($othertitle,$usertypes,$types) =
                    638:                     &Apache::loncommon::sorted_inst_types($selfdom);
                    639:                 if (ref($usertypes) eq 'HASH') {
1.9       raeburn   640:                     my $anytype = 1;
                    641:                     foreach my $key (keys(%{$usertypes})) {
1.13      raeburn   642:                         if (!grep(/^\Q$key\E$/,@unesc_types)) {
1.9       raeburn   643:                             $anytype = 0;
                    644:                             last;
                    645:                         }
                    646:                     }
                    647:                     if ($anytype) {
                    648:                         if (!(grep(/^other$/,@types))) {
                    649:                             $anytype = 0;
                    650:                         }
                    651:                     }
                    652:                     $usertypes->{'any'} = &mt('Any users');
                    653:                     $usertypes->{'other'} = &mt('Others');
1.13      raeburn   654:                     my @showtypes;
1.9       raeburn   655:                     if ($anytype) {
                    656:                         @{$enrolltypes{$selfdom}} = ('any');
1.13      raeburn   657:                         @showtypes =  ('any');
1.9       raeburn   658:                     } else {
                    659:                         @{$enrolltypes{$selfdom}} = @types;
1.13      raeburn   660:                         @showtypes = @unesc_types;
1.9       raeburn   661:                     }
                    662:                     @{$longtypes{$selfdom}} = 
1.13      raeburn   663:                         map {$usertypes->{$_}} @showtypes;
1.1       raeburn   664:                 }
                    665:             }
                    666:         }
                    667:     }
                    668:     return (\%enrolltypes,\%longtypes,$alldoms);
                    669: }
                    670: 
                    671: sub print_selfenroll_types {
                    672:     my ($longtypes,$domain) = @_;
                    673:     my $output;
                    674:     if (ref($longtypes) eq 'HASH') {
1.9       raeburn   675:         if ($domain ne '') {
                    676:             my $domdesc = &Apache::lonnet::domain($domain,'description');
                    677:             if (ref($longtypes->{$domain}) eq 'ARRAY') {
                    678:                 if (grep(/^any$/,@{$longtypes->{$domain}})) {
                    679:                     $output = &mt('Self-enrollment in this course is available to any user affiliated with [_1].',$domdesc);
                    680:                 } else {
                    681:                     my $status_str = join(', ',@{$longtypes->{$domain}});
                    682:                     $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   683:                 }
                    684:             } else {
1.9       raeburn   685:                 $output = &mt('Self-enrollment is not currently available for this course for users affiliated with [_1].',$domdesc);
                    686:             }
                    687:         } elsif (keys(%{$longtypes}) > 0) {
1.12      raeburn   688:             $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   689:             foreach my $selfdom (sort(keys(%{$longtypes}))) {
                    690:                 my $selfdomdesc = &Apache::lonnet::domain($selfdom,'description');
                    691:                 if (ref($longtypes->{$selfdom}) eq 'ARRAY') {
1.12      raeburn   692:                     $output .= '<li>'.$selfdomdesc.': '.join(', ',@{$longtypes->{$selfdom}}).'</li>'."\n";
1.1       raeburn   693:                 }
                    694:             }
1.12      raeburn   695:             $output .= '</ul>'."\n";
1.1       raeburn   696:         } else {
1.5       bisitz    697:             $output = &mt('Self-enrollment is not currently available for this course.');
1.1       raeburn   698:         }
                    699:     }
                    700:     return $output;
                    701: }
                    702: 
                    703: sub check_registered {
                    704:     my ($cdom,$cnum) = @_;
                    705:     my ($registered,$instsec,$message);
                    706:     my %settings = &Apache::lonnet::get('environment',['internal.coursecode',
                    707:                                         'internal.sectionnums',
                    708:                                         'internal.crosslistings'],$cdom,$cnum);
                    709:     my (@allcourses,%LC_code,%affiliates,%reply);
                    710:     &Apache::loncommon::get_institutional_codes(\%settings,\@allcourses,\%LC_code);
                    711:     if (@allcourses > 0) {
                    712:         @{$affiliates{$cnum}} = @allcourses;
                    713:         my $outcome = &Apache::lonnet::fetch_enrollment_query('updatenow',\%affiliates,\%reply,$cdom,$cnum);
                    714:         if ($outcome eq 'ok') {
                    715:             if ($reply{$cnum} > 0) {
                    716:                 foreach my $class (@allcourses) {
                    717:                     my %enrolled;
                    718:                     my $dir = $Apache::lonnet::perlvar{'lonDaemons'};
                    719:                     my %place = &LONCAPA::Enrollment::place_hash();
                    720:                     &LONCAPA::Enrollment::parse_classlist($dir,$cdom,$cnum,$class,
                    721:                              \%place,$LC_code{$class},\%enrolled);
                    722:                     if (defined($enrolled{$env{'user.name'}})) {
                    723:                         $registered = 1;
                    724:                         $instsec = $LC_code{$class};
                    725:                         last;
                    726:                     }
                    727:                 }
                    728:             } else {
                    729:                 $message = &mt('Your registration status could not be verified.');
                    730:             }
                    731:         } else {
                    732:              $message = &mt('Your registration status could not determined, because a problem occurred retrieving data.');
                    733:         }
                    734:     } else {
                    735:         $message = &mt('As no institutional course sections are currently associated with this course, your registration status is undetermined.'); 
                    736:     }
                    737:     return ($registered,$instsec,$message);
                    738: }
                    739: 
                    740: 1;
                    741: 

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