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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Allow users to self-enroll in a course
                      3: #
1.30    ! raeburn     4: # $Id: selfenroll.pm,v 1.29 2014/04/02 16:51:11 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.11      raeburn   224:     $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   225:     if ($knownuser) {
                    226:         if (keys(%curr_role)) {
1.9       raeburn   227:             $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.
                    228:                       '<span class="LC_warning">'.&has_role(%curr_role).'</span>');
1.12      raeburn   229:             &page_footer($r);
1.1       raeburn   230:             return OK;
                    231:         }
                    232:         &process_self_enroll($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,
                    233:                              $selfenroll_access_start,$selfenroll_access_end,
1.15      raeburn   234:                              $selfenroll_section,$now,$selfenroll_approval,
1.29      raeburn   235:                              $selfenroll_notifylist,$owner,$crstype,$lonhost,$handle);
1.10      raeburn   236:     } elsif ($env{'form.phase'} eq 'login') {
                    237:         my $submit_text = &mt('Log in');
                    238:         $r->print('<h3>'.&mt('Log-in to LON-CAPA').'</h3>');
                    239:         my $udom = &Apache::lonnet::default_login_domain();
1.12      raeburn   240:         $r->print(&Apache::createaccount::login_box($now,$lonhost,$courseid,
1.10      raeburn   241:                                            $submit_text,$udom,'selfenroll'));
1.12      raeburn   242:         $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>'));
                    243:         &page_footer($r);
1.10      raeburn   244:         return OK;
1.3       raeburn   245:     } elsif (@cancreate > 0) {
1.1       raeburn   246:         $r->print(<<END);
                    247: <script type="text/javascript">
1.12      raeburn   248: function setPath(formname,formaction,item,arg) {
                    249:     var formidx = getFormByName(formname);
                    250:     if (formidx > -1) {
                    251:         if (formaction != '') {
                    252:             document.forms[formidx].action = formaction;
                    253:         }
                    254:         var itemid = getIndexByName(formidx,'setting');
                    255:         if (itemid > -1) {
                    256:             document.forms[formidx].elements[itemid].name = item;
                    257:             document.forms[formidx].elements[itemid].value = arg;
                    258:             document.forms[formidx].submit();
                    259:         } else {
                    260:             alert("$missing_formitem");
                    261:         }
1.1       raeburn   262:     }
                    263:     return;
                    264: }
                    265: 
                    266: </script>
                    267: END
1.9       raeburn   268:         $r->print('<h3>'.&mt('LON-CAPA account required').'</h3>'.
1.12      raeburn   269:                   &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   270:         if (@cancreate > 1) {
                    271:             $r->print('<li>'.&mt('Otherwise:').'<br />');
                    272:             my $count = 0;
                    273:             foreach my $type ('sso','login','email') { 
1.4       raeburn   274:                 if (($count > 0) && ($count <@cancreate)) {
1.3       raeburn   275:                     $r->print('&nbsp;, '.&mt('or').'<br />');
                    276:                 }
                    277:                 if (grep(/^$type$/,@cancreate)) {
                    278:                     if ($type eq 'sso') {
1.12      raeburn   279:                         $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   280:                     } elsif ($type eq 'login') {
1.12      raeburn   281:                         $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   282:                     } elsif ($type eq 'email') {
1.12      raeburn   283:                         $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   284:                     }
                    285:                     $count ++;
                    286:                 }
                    287:             }
                    288:             $r->print('.<br />');
                    289:         } else {
                    290:             if ($cancreate[0] eq 'login') {
1.12      raeburn   291:                 $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   292:             } elsif ($cancreate[0] eq 'email') {
1.12      raeburn   293:                 $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   294:             } elsif ($cancreate[0] eq 'sso') {
1.12      raeburn   295:                 $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   296:             }
1.1       raeburn   297:         }
                    298:         if ($selfenroll_types eq '*') {
1.9       raeburn   299:             $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   300:         } else {
                    301:             $r->print('</li></ul>');
                    302:             my ($enrolltypes,$longtypes,$alldoms) = 
1.9       raeburn   303:                 &get_selfenroll_filters($selfenroll_types);
                    304:             $r->print(&print_selfenroll_types($longtypes));
1.1       raeburn   305:         }
                    306:     } else {
1.9       raeburn   307:         my $cdomdesc = &Apache::lonnet::domain($cdom,'description');
                    308:         $r->print('<h3>'.&mt('LON-CAPA account required').'</h3>');
                    309:         $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   310:         if ($selfenroll_types ne '*') {
                    311:             my ($enrolltypes,$longtypes,$alldoms) =
                    312:                 &get_selfenroll_filters($selfenroll_types);
1.9       raeburn   313:             $r->print('<br />'.&print_selfenroll_types($longtypes));
1.1       raeburn   314:         }
1.9       raeburn   315:         my $displayurl = &escape($firsturl);
                    316:         $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   317:     }
1.12      raeburn   318:     $r->print("\n".'<form name="'.$form.'" method="post" action="">'."\n".
                    319:               '<input type="hidden" name="courseid" value="'.$courseid.'" />'."\n".
1.1       raeburn   320:               '<input type="hidden" name="setting" value=""/>'."\n".
1.12      raeburn   321:               &Apache::lonhtmlcommon::echo_form_input(['backto','courseid','context','phase'])."\n".
                    322:               '</form>'."\n");
                    323:               &page_footer($r);
1.1       raeburn   324:     return OK;
                    325: }
                    326: 
1.15      raeburn   327: sub enrollment_limit_check {
                    328:     my ($selfenroll_limit,$selfenroll_cap,$cdom,$cnum) = @_;
                    329:     my $nospace = 0;
                    330:     my (%idx,%stucount);
                    331:     my $classlist = &Apache::loncoursedata::get_classlist($cdom,$cnum);
                    332:     $idx{'type'} = &Apache::loncoursedata::CL_TYPE();
                    333:     $idx{'status'} = &Apache::loncoursedata::CL_STATUS();
                    334:     while (my ($student,$data) = each(%$classlist)) {
                    335:         if (($data->[$idx{'status'}] eq 'Active') ||
                    336:             ($data->[$idx{'status'}] eq 'Future')) {
                    337:             if ($data->[$idx{'type'}] eq 'selfenroll') {
                    338:                 $stucount{'selfenroll'} ++;
                    339:             }
                    340:             $stucount{'allstudents'} ++;
                    341:         }
                    342:     }
                    343:     if ($stucount{$selfenroll_limit} >= $selfenroll_cap) {
                    344:         $nospace = 1;
                    345:     }
                    346:     return $nospace;
                    347: }
                    348: 
1.10      raeburn   349: sub page_header {
1.12      raeburn   350:     my ($r,$courseid,$js,$desc) = @_;
1.10      raeburn   351:     my $start_page =
1.26      droeschl  352:         &Apache::loncommon::start_page('Self-enroll in a LON-CAPA course',$js);
1.10      raeburn   353:     $r->print($start_page);
                    354:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.12      raeburn   355:     &Apache::createaccount::selfenroll_crumbs($r,$courseid,$desc);
1.10      raeburn   356:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Self-enroll in course'));
                    357:     return;
                    358: }
                    359: 
1.12      raeburn   360: sub page_footer {
                    361:     my ($r) = @_;
                    362:     $r->print('<form name="backupcrumbs" method="post" action="/adm/coursecatalog">'."\n".
                    363:               &Apache::lonhtmlcommon::echo_form_input(['backto','courseid','phase','context']).
                    364:               '</form>'.&Apache::loncommon::end_page());
                    365:     return;
                    366: }
                    367: 
1.1       raeburn   368: sub user_is_known {
                    369:     my $known = 0;
                    370:     if ($env{'user.name'} ne '' && $env{'user.name'} ne 'public'
                    371:         && $env{'user.domain'} ne '' && $env{'user.domain'} ne 'public') {
                    372:         $known = 1;
                    373:     }
                    374:     return $known;
                    375: }
                    376: 
                    377: sub can_create {
                    378:     my ($cdom) = @_;
1.3       raeburn   379:     my @cancreate;
1.1       raeburn   380:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$cdom);
                    381:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
                    382:         if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
                    383:             if ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') {
1.3       raeburn   384:                 if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
                    385:                     @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
                    386:                 } else {
                    387:                     if (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') && 
                    388:                         ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
                    389:                         @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
                    390:                     }
                    391:                 }
1.1       raeburn   392:             }
                    393:         }
                    394:     }
1.3       raeburn   395:     return @cancreate;
1.1       raeburn   396: }
                    397: 
                    398: sub has_role {
                    399:     my (%curr_role) = @_;
                    400:     my $output;
                    401:     if ($curr_role{'status'} eq 'active') {
                    402:           my $rolelink = &jump_to_role($curr_role{'role'});
1.18      bisitz    403:           $output = &mt('You already have an active student role (section: "[_1]") in this course.',$curr_role{'section'}).'<br />'.$rolelink;
1.1       raeburn   404:     } elsif ($curr_role{'status'} eq 'future') {
1.9       raeburn   405:         $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   406:     }
                    407:     return $output;
                    408: }
                    409: 
                    410: sub process_self_enroll {
                    411:     my ($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,
1.15      raeburn   412:         $selfenroll_access_start,$selfenroll_access_end,$selfenroll_section,
1.29      raeburn   413:         $now,$selfenroll_approval,$selfenroll_notifylist,$owner,$crstype,$lonhost,$handle) = @_;
1.1       raeburn   414:     my $udom = $env{'user.domain'};
                    415:     my $uname = $env{'user.name'};
                    416:     my $selfenroll = 0;
1.9       raeburn   417:     my ($enrolltypes,$longtypes,$alldoms);
                    418:     if ($selfenroll_types eq '*') {
1.1       raeburn   419:         $selfenroll = 1;
1.9       raeburn   420:     } else {
                    421:         ($enrolltypes,$longtypes,$alldoms) =
                    422:             &get_selfenroll_filters($selfenroll_types,$udom);
                    423:         if ($alldoms) {
                    424:             $selfenroll = 1;
                    425:         } elsif (ref($enrolltypes) eq 'HASH') {
                    426:             if (ref($enrolltypes->{$udom}) eq 'ARRAY') {
                    427:                 if (grep(/^any$/,@{$enrolltypes->{$udom}})) {
                    428:                     $selfenroll = 1;
                    429:                 } else {
                    430:                     $selfenroll = &user_can_selfenroll($udom,$uname,$enrolltypes->{$udom});
1.1       raeburn   431:                 }
                    432:             }
                    433:         }
                    434:     }
                    435:     if ($selfenroll) {
                    436:         my $usec = $selfenroll_section;
                    437:         if ($selfenroll_section eq 'none') {
                    438:             $usec = '';
                    439:         }
                    440:         if ($selfenroll_registered) {
                    441:             my ($registered,$instsec,$message) = &check_registered($cdom,$cnum);
                    442:             $usec = $instsec;
                    443:             if (!$registered) {
1.28      raeburn   444:                 $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.
                    445:                           &mt('Self-enrollment is restricted to students officially registered for this course.').'<br />');
1.1       raeburn   446:                 if ($message) {
                    447:                     $r->print($message);
                    448:                 } else {
                    449:                     $r->print(&mt('As you are not currently registered for this course, self-enrollment is unavailable.'));
                    450:                 }
                    451:                 return; 
                    452:             } 
                    453:         }
1.15      raeburn   454:         if ($selfenroll_approval) {
                    455:             my $outcome = 
                    456:                 &store_selfenroll_request($udom,$uname,$usec,$cdom,$cnum,
1.28      raeburn   457:                                           $selfenroll_notifylist,$owner,
1.29      raeburn   458:                                           $selfenroll_approval,$crstype,$lonhost,$handle);
1.15      raeburn   459:             $r->print($outcome);
                    460:         } else {
                    461:             my $enrollresult = 
                    462:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,
                    463:                         undef,undef,$usec,$selfenroll_access_end,$selfenroll_access_start,
                    464:                        'selfenroll',undef,$cdom.'_'.$cnum,$selfenroll);
                    465:             if ($enrollresult eq 'ok') {
                    466:                 my (%userroles,%newrole,%newgroups);
                    467:                 my $role = 'st';
                    468:                 my $area = '/'.$cdom.'/'.$cnum;
                    469:                 my $spec = $role.'.'.$area;
                    470:                 if ($usec ne '') {
                    471:                     $spec .= '/'.$usec;
                    472:                     $area .= '/'.$usec;
                    473:                 }
                    474:                 &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,
                    475:                                                     $area);
1.23      raeburn   476:                 &Apache::lonnet::set_userprivs(\%userroles,\%newrole,\%newgroups);
1.15      raeburn   477:                 $userroles{'user.role.'.$spec} = $selfenroll_access_start.'.'.$selfenroll_access_end;
                    478:                 &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
                    479:                 $r->print('<h3>'.&mt('Enrollment process complete').'</h3>');
                    480:                 if ($selfenroll_access_end && $selfenroll_access_end <= $now) {
                    481:                     $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   482:                 } else {
1.15      raeburn   483:                     $r->print(&mt('Self-enrollment in this course was successful.').'<br />');
                    484:                     my $showstart = &Apache::lonlocal::locallocaltime($selfenroll_access_start);
                    485:                     my $showend = &Apache::lonlocal::locallocaltime($selfenroll_access_end);
                    486:                     if ($selfenroll_access_start && $selfenroll_access_start >$now) {
                    487:                         $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));
                    488:                     } else {
                    489:                         my $newrole = 'st./'.$cdom.'/'.$cnum;
                    490:                         if ($usec ne '') {
                    491:                             $newrole .= '/'.$usec;
                    492:                         }
                    493:                         my $rolelink = &jump_to_role($newrole);
                    494:                         $r->print(&mt('Your new role is available immediately, and will provide access to the course until [_1].',$showend).'<br /><br />'."\n".
                    495:                                  $rolelink); 
1.1       raeburn   496:                     }
                    497:                 }
1.15      raeburn   498:             } else {
                    499:                 $r->print('<h3>'.&mt('Enrollment incomplete').'</h3>'.
                    500:                           &mt('Self-enrollment in this course failed.'));
                    501:                 if ($enrollresult ne '') {
                    502:                     $r->print('<span class="LC_error">'.$enrollresult.'</span>');
                    503:                 }
1.1       raeburn   504:             }
                    505:         }
                    506:     } else {
1.9       raeburn   507:         $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.
                    508:                   &mt('You are not permitted to enroll yourself in this course.').'<br />');
1.1       raeburn   509:         $r->print(&print_selfenroll_types($longtypes));
                    510:     }
                    511:     return;
                    512: }
                    513: 
1.9       raeburn   514: sub user_can_selfenroll {
                    515:     my ($udom,$uname,$domenrolltypes) = @_;
                    516:     my $selfenroll = 0;
1.13      raeburn   517:     my %userhash = &Apache::lonnet::userenvironment($udom,$uname,'inststatus');
1.9       raeburn   518:     my @inststatuses;
                    519:     if ($userhash{'inststatus'} eq '') {
                    520:         push(@inststatuses,'other');
                    521:     } else {
1.13      raeburn   522:         @inststatuses = split(':',$userhash{'inststatus'});
1.9       raeburn   523:     }
                    524:     foreach my $type (@inststatuses) {
                    525:         if (ref($domenrolltypes) eq 'ARRAY') {
                    526:             if (grep(/^\Q$type\E$/,@{$domenrolltypes})) {
                    527:                 $selfenroll = 1;
                    528:                 last;
                    529:             }
                    530:         }
                    531:     }
                    532:     return $selfenroll;
                    533: }
                    534: 
1.15      raeburn   535: sub store_selfenroll_request {
1.28      raeburn   536:     my ($udom,$uname,$usec,$cdom,$cnum,$selfenroll_notifylist,$owner,
1.29      raeburn   537:         $selfenroll_approval,$crstype,$lonhost,$handle) = @_;
1.15      raeburn   538:     my $namespace = 'selfenrollrequests';
                    539:     my $output;
                    540:     my $now = time;
                    541:     my %existing = 
                    542:         &Apache::lonnet::get($namespace,[$uname.':'.$udom],$cdom,$cnum);
                    543:     if ($existing{$uname.':'.$udom}) {
1.28      raeburn   544:         $output = &mt('A self-enrollment request already exists for you for this course.').'<br />'.
                    545:                   &mt('Your earlier request is in a queue awaiting action by a Course Coordinator.').
1.24      raeburn   546:                   '<br /><br />'.&Apache::loncoursequeueadmin::queued_selfenrollment();
1.15      raeburn   547:     } else {
                    548:         my %selfenroll = (
                    549:                             $uname.':'.$udom => $now.':'.$usec,
                    550:                          );
                    551:         my $putresult = &Apache::lonnet::put($namespace,\%selfenroll,$cdom,$cnum);
1.28      raeburn   552:         my $status = 'request';
                    553:         if ($selfenroll_approval eq '2') {
                    554:             $status = 'pending';
                    555:         } 
1.15      raeburn   556:         if ($putresult eq 'ok') {
1.16      raeburn   557:             my %userenroll = (
                    558:                                $cdom.'_'.$cnum =>  {
                    559:                                                       timestamp => $now,
                    560:                                                       section   => $usec,
1.28      raeburn   561:                                                       status    => $status,
1.16      raeburn   562:                                                    });
1.28      raeburn   563:             my $token;
                    564:             if ($status eq 'pending') {
1.30    ! raeburn   565:                 $token = &Apache::lonnet::tmpput(\%selfenroll,$lonhost);
1.28      raeburn   566:                 $userenroll{$cdom.'_'.$cnum}{'token'} = $token;
1.29      raeburn   567:                 $userenroll{$cdom.'_'.$cnum}{'lonhost'} = $lonhost;
                    568:                 $userenroll{$cdom.'_'.$cnum}{'handle'} = $handle; 
1.28      raeburn   569:             } 
1.16      raeburn   570:             my $warning;
                    571:             my $userresult = &Apache::lonnet::put($namespace,\%userenroll,$udom,$uname);
                    572:             if ($userresult ne 'ok') {
                    573:                 $warning = &mt('An error occurred saving a personal record of your request.');
                    574:             }
1.28      raeburn   575:             $output = &mt('Your request for self-enrollment has been recorded.').'<br />';
                    576:             if ($status eq 'pending') {
                    577:                 my $coursetype = &Apache::lonuserutils::get_extended_type($cdom,$cnum,$crstype); 
                    578:                 my %postvalues = (
                    579:                                    'username'   => $env{'user.name'},
                    580:                                    'domain'     => $env{'user.domain'},
                    581:                                    'course'     => $cdom.'_'.$cnum,
                    582:                                    'coursetype' => $coursetype,
                    583:                                    'token'      => $token,
                    584:                                  );
                    585:                 my %domconfig = &Apache::lonnet::get_dom('configuration',['selfenrollment'],$cdom);
1.30    ! raeburn   586: 
1.28      raeburn   587:                 if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
                    588:                     my ($url,$buttontext,$code,@fields);
                    589:                     if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
                    590:                         my %courseinfo = &Apache::lonnet::coursedescription($cdom.'_'.$cnum,{ 'one_time' => 1});
                    591:                         $postvalues{'uniquecode'} = $courseinfo{'internal.uniquecode'};
1.30    ! raeburn   592:                         $postvalues{'description'} = $courseinfo{'description'};
1.28      raeburn   593:                         $url = $domconfig{'selfenrollment'}{'validation'}{'url'};
                    594:                         if (ref($domconfig{'selfenrollment'}{'validation'}{'fields'}) eq 'ARRAY') {
                    595:                             @fields = @{$domconfig{'selfenrollment'}{'validation'}{'fields'}};
                    596:                         }
                    597:                         $buttontext = $domconfig{'selfenrollment'}{'validation'}{'button'};
                    598: 
                    599:                         $output .= $domconfig{'selfenrollment'}{'validation'}{'markup'};
                    600:                         if (($url =~ m{^(https?\://|/)}) && (@fields > 0)) {
                    601:                             $output .= '<form name="selfenrollvalidation" action="'.$url.'" method="post">'."\n";
                    602:                             foreach my $field (@fields) {
                    603:                                 if ($postvalues{$field}) {
                    604:                                     $output .= '<input type="hidden" name="'.$field.'" value="'.$postvalues{$field}.'" />'."\n";
                    605:                                 }
                    606:                             }
1.30    ! raeburn   607:                             if ($buttontext eq '') {
        !           608:                                 $buttontext = &mt('Complete my enrollment');
        !           609:                             }
        !           610:                             my $protocol = $Apache::lonnet::protocol{$lonhost};
        !           611:                             $protocol = 'http' if ($protocol ne 'https');
        !           612:                             my $enroller = $protocol.'://'.&Apache::lonnet::hostname($lonhost).'/cgi-bin/enrollqueued.pl';
        !           613:                             $output .= '<input type="hidden" name="enroller" value="'.$enroller.'" />'."\n".
        !           614:                                        '<input type="submit" name="validate" value="'.$buttontext.'" />'."\n".
        !           615:                                        '</form>'."\n";
        !           616:                         } else {
        !           617:                             $status = 'request';
        !           618:                         } 
1.28      raeburn   619:                     }
                    620:                 } else {
1.30    ! raeburn   621:                     $status = 'request';
1.28      raeburn   622:                 }
                    623:             }
                    624:             if ($status eq 'request') {
                    625:                 $output .= &mt('A message will be sent to your LON-CAPA account when the course coordinator takes action on your request.').'<br />'.
                    626:                            &mt('To access your LON-CAPA message, go to the Main Menu and click on "Send and Receive Messages".').'<br />';
                    627:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
                    628:                 if (($emails{'permanentemail'} ne '') || ($emails{'notification'} ne '')) {
                    629:                     my $address = $emails{'permanentemail'};
                    630:                     if ($address eq '') {
                    631:                         $address = $emails{'notification'};
                    632:                     }
                    633:                     $output.= &mt('An e-mail will also be sent to: [_1] when this occurs.',$address).'<br />';
                    634:                 }
                    635:                 if ($warning) { 
                    636:                     $output .= '<span class="LC_warning">'.$warning.'</span><br />';
                    637:                 }
                    638:                 $output .= &Apache::loncoursequeueadmin::queued_selfenrollment();
                    639: 
                    640:                 if ($selfenroll_notifylist) {
                    641:                     my $fullname = &Apache::loncommon::plainname($uname,$udom);
                    642:                     my %courseinfo = &Apache::lonnet::coursedescription($cdom.'_'.$cnum);
                    643:                     my $coursedesc = $courseinfo{'description'};
                    644:                     &Apache::loncoursequeueadmin::send_selfserve_notification(
                    645:                         $selfenroll_notifylist,$fullname,$cdom.'_'.$cnum,
                    646:                         $coursedesc,$now,'selfenrollreq',$owner);
                    647:                 }
1.15      raeburn   648:             }
                    649:         } else {
                    650:             $output = '<span class="LC_error">'.&mt('An error occurred when recording your request.').'</span>';
                    651: 
                    652:         }
                    653:     }
                    654:     return $output;
                    655: }
                    656: 
1.1       raeburn   657: sub jump_to_role {
                    658:     my ($role) = @_;
                    659:     my $output = <<"END";
                    660: <script type="text/javascript">
                    661: function SelectRole() {
                    662:     document.rolechoice.selectrole.value = '1';
                    663:     document.rolechoice.submit();
                    664: }
                    665: </script>
                    666: END
                    667:     $output .= '<a href="javascript:SelectRole()">'."\n". 
                    668:                &mt('Enter course now').'</a>'."\n".
1.9       raeburn   669:                '<form name="rolechoice" action="/adm/roles" method="post">'."\n".
1.1       raeburn   670:                '<input type="hidden" name="selectrole" value="" />'."\n".
                    671:                '<input type="hidden" name="'.$role.'" value="1" />'."\n".
                    672:                '</form>';
                    673:     return $output;
                    674: }
                    675: 
                    676: sub get_selfenroll_filters {
                    677:     my ($selfenroll_types,$domain) = @_;
                    678:     my (%enrolltypes,%longtypes,$alldoms);
                    679:     my @selfenrolldoms = split(/;/,$selfenroll_types);
                    680:     foreach my $item (@selfenrolldoms) {
                    681:         my ($selfdom,$type_str) = split(/:/,$item);
                    682:         if ($selfdom eq '*') {
                    683:             $alldoms = 1;
                    684:             last;
                    685:         }
                    686:         if ($domain ne '') {
                    687:             next if ($selfdom ne $domain);
                    688:         }
                    689:         if ($selfdom =~ /^$match_domain$/) {
                    690:             if (&Apache::lonnet::domain($selfdom) ne '') {
                    691:                 my @types = split(/,/,$type_str);
1.13      raeburn   692:                 my @unesc_types = map { &unescape($_); } @types;
1.1       raeburn   693:                 my ($othertitle,$usertypes,$types) =
                    694:                     &Apache::loncommon::sorted_inst_types($selfdom);
                    695:                 if (ref($usertypes) eq 'HASH') {
1.9       raeburn   696:                     my $anytype = 1;
                    697:                     foreach my $key (keys(%{$usertypes})) {
1.13      raeburn   698:                         if (!grep(/^\Q$key\E$/,@unesc_types)) {
1.9       raeburn   699:                             $anytype = 0;
                    700:                             last;
                    701:                         }
                    702:                     }
                    703:                     if ($anytype) {
                    704:                         if (!(grep(/^other$/,@types))) {
                    705:                             $anytype = 0;
                    706:                         }
                    707:                     }
                    708:                     $usertypes->{'any'} = &mt('Any users');
                    709:                     $usertypes->{'other'} = &mt('Others');
1.13      raeburn   710:                     my @showtypes;
1.9       raeburn   711:                     if ($anytype) {
                    712:                         @{$enrolltypes{$selfdom}} = ('any');
1.13      raeburn   713:                         @showtypes =  ('any');
1.9       raeburn   714:                     } else {
                    715:                         @{$enrolltypes{$selfdom}} = @types;
1.13      raeburn   716:                         @showtypes = @unesc_types;
1.9       raeburn   717:                     }
                    718:                     @{$longtypes{$selfdom}} = 
1.13      raeburn   719:                         map {$usertypes->{$_}} @showtypes;
1.1       raeburn   720:                 }
                    721:             }
                    722:         }
                    723:     }
                    724:     return (\%enrolltypes,\%longtypes,$alldoms);
                    725: }
                    726: 
                    727: sub print_selfenroll_types {
                    728:     my ($longtypes,$domain) = @_;
                    729:     my $output;
                    730:     if (ref($longtypes) eq 'HASH') {
1.9       raeburn   731:         if ($domain ne '') {
                    732:             my $domdesc = &Apache::lonnet::domain($domain,'description');
                    733:             if (ref($longtypes->{$domain}) eq 'ARRAY') {
                    734:                 if (grep(/^any$/,@{$longtypes->{$domain}})) {
                    735:                     $output = &mt('Self-enrollment in this course is available to any user affiliated with [_1].',$domdesc);
                    736:                 } else {
                    737:                     my $status_str = join(', ',@{$longtypes->{$domain}});
                    738:                     $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   739:                 }
                    740:             } else {
1.9       raeburn   741:                 $output = &mt('Self-enrollment is not currently available for this course for users affiliated with [_1].',$domdesc);
                    742:             }
                    743:         } elsif (keys(%{$longtypes}) > 0) {
1.12      raeburn   744:             $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   745:             foreach my $selfdom (sort(keys(%{$longtypes}))) {
                    746:                 my $selfdomdesc = &Apache::lonnet::domain($selfdom,'description');
                    747:                 if (ref($longtypes->{$selfdom}) eq 'ARRAY') {
1.12      raeburn   748:                     $output .= '<li>'.$selfdomdesc.': '.join(', ',@{$longtypes->{$selfdom}}).'</li>'."\n";
1.1       raeburn   749:                 }
                    750:             }
1.12      raeburn   751:             $output .= '</ul>'."\n";
1.1       raeburn   752:         } else {
1.5       bisitz    753:             $output = &mt('Self-enrollment is not currently available for this course.');
1.1       raeburn   754:         }
                    755:     }
                    756:     return $output;
                    757: }
                    758: 
                    759: sub check_registered {
                    760:     my ($cdom,$cnum) = @_;
                    761:     my ($registered,$instsec,$message);
                    762:     my %settings = &Apache::lonnet::get('environment',['internal.coursecode',
                    763:                                         'internal.sectionnums',
                    764:                                         'internal.crosslistings'],$cdom,$cnum);
                    765:     my (@allcourses,%LC_code,%affiliates,%reply);
                    766:     &Apache::loncommon::get_institutional_codes(\%settings,\@allcourses,\%LC_code);
                    767:     if (@allcourses > 0) {
                    768:         @{$affiliates{$cnum}} = @allcourses;
                    769:         my $outcome = &Apache::lonnet::fetch_enrollment_query('updatenow',\%affiliates,\%reply,$cdom,$cnum);
                    770:         if ($outcome eq 'ok') {
                    771:             if ($reply{$cnum} > 0) {
                    772:                 foreach my $class (@allcourses) {
                    773:                     my %enrolled;
                    774:                     my $dir = $Apache::lonnet::perlvar{'lonDaemons'};
                    775:                     my %place = &LONCAPA::Enrollment::place_hash();
                    776:                     &LONCAPA::Enrollment::parse_classlist($dir,$cdom,$cnum,$class,
                    777:                              \%place,$LC_code{$class},\%enrolled);
                    778:                     if (defined($enrolled{$env{'user.name'}})) {
                    779:                         $registered = 1;
                    780:                         $instsec = $LC_code{$class};
                    781:                         last;
                    782:                     }
                    783:                 }
                    784:             } else {
                    785:                 $message = &mt('Your registration status could not be verified.');
                    786:             }
                    787:         } else {
                    788:              $message = &mt('Your registration status could not determined, because a problem occurred retrieving data.');
                    789:         }
                    790:     } else {
                    791:         $message = &mt('As no institutional course sections are currently associated with this course, your registration status is undetermined.'); 
                    792:     }
                    793:     return ($registered,$instsec,$message);
                    794: }
                    795: 
                    796: 1;
                    797: 

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