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

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

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