File:  [LON-CAPA] / loncom / interface / selfenroll.pm
Revision 1.27.2.3: download - view: text, annotated - select for diffs
Sun Apr 6 14:24:40 2014 UTC (10 years ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_1, version_2_11_0_RC3, version_2_11_0
- For 2.11
  - Backport 1.31.

    1: # The LearningOnline Network
    2: # Allow users to self-enroll in a course
    3: #
    4: # $Id: selfenroll.pm,v 1.27.2.3 2014/04/06 14:24:40 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: package Apache::selfenroll;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common);
   33: use Apache::lonnet;
   34: use Apache::loncommon;
   35: use Apache::lonlocal;
   36: use Apache::createaccount;
   37: use Apache::loncoursequeueadmin;
   38: use Apache::lonuserutils;
   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');
   50:     my $lonhost = $r->dir_config('lonHostID');
   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);
   56:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['courseid']);
   57:     my $js = &Apache::createaccount::catreturn_js();
   58: 
   59:     my $courseid = Apache::lonnet::is_course($env{'form.courseid'});
   60: 
   61:     unless ($courseid) {
   62:         &page_header($r,$env{'form.courseid'},$js);
   63:         $r->print('<h3>'.&mt('Self-enrollment error').'</h3>'.
   64:                   '<span class="LC_error">'.
   65:                   &mt('Invalid domain or course number').'</span>');
   66:         &page_footer($r);
   67:         return OK;
   68:     }
   69:     my $now = time;
   70:     if ($env{'form.phase'} eq 'login') {
   71:         $js .= "\n".&Apache::createaccount::javascript_setforms($now);
   72:     }
   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);
   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:     }
   83:     my ($canenroll,$selfenroll_types,$selfenroll_registered,@cancreate,
   84:         $knownuser,$selfenroll_access_start,$selfenroll_access_end,
   85:         $selfenroll_section,$selfenroll_future,%curr_role,$cdomdesc,
   86:         $selfenroll_approval,$selfenroll_limit,$selfenroll_cap,
   87:         $selfenroll_notifylist,$owner,$crstype);
   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'};
   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'};
   98:     $crstype = $coursehash{'internal.type'};
   99:     if ($crstype eq '') {
  100:         $crstype = 'Course';
  101:     }
  102:     my $nospace;
  103:     if ($selfenroll_types ne '') {
  104:         my $start = $coursehash{'internal.selfenroll_start_date'};
  105:         my $end = $coursehash{'internal.selfenroll_end_date'};
  106:         if (($start > 0 && $start < $now) && (($end == 0) || ($end > 0 && $end > $now))) {
  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:             }
  118:         } elsif (($end == 0) || ($end > 0 && $end > $now)) {
  119:             if ($start > $now) {
  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:                 }
  128:             }
  129:         }
  130:     }
  131:     $knownuser = &user_is_known();
  132:     if (!$canenroll) {
  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 />');
  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:         } 
  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:         }
  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;
  195:                      if ($curr_role{'section'} eq '') {
  196:                          $curr_role{'section'} = &mt('none'); 
  197:                      }
  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:         }
  211:         &page_footer($r);
  212:         return OK;
  213:     }
  214:     @cancreate = &can_create($cdom);
  215:     my ($form,$login_path,$firsturl,$create_path,$sso_url,$missing_formitem);
  216:     $form = 'logmein';
  217:     $login_path = '/adm/login';
  218:     $firsturl= '/adm/selfenroll?courseid='.$courseid;
  219:     $create_path = '/adm/createaccount';
  220:     $sso_url = $r->dir_config('lonSSOReloginServer');
  221:     if ($sso_url eq '') {
  222:         $sso_url = $login_path;
  223:     }
  224:     $missing_formitem = &mt('The link to the requested page could not be followed.')."\\n".&mt('The placeholder for the courseID is absent.');
  225:     if ($knownuser) {
  226:         if (keys(%curr_role)) {
  227:             $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.
  228:                       '<span class="LC_warning">'.&has_role(%curr_role).'</span>');
  229:             &page_footer($r);
  230:             return OK;
  231:         }
  232:         &process_self_enroll($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,
  233:                              $selfenroll_access_start,$selfenroll_access_end,
  234:                              $selfenroll_section,$now,$selfenroll_approval,
  235:                              $selfenroll_notifylist,$owner,$crstype,$lonhost,$handle);
  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();
  240:         $r->print(&Apache::createaccount::login_box($now,$lonhost,$courseid,
  241:                                            $submit_text,$udom,'selfenroll'));
  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);
  244:         return OK;
  245:     } elsif (@cancreate > 0) {
  246:         $r->print(<<END);
  247: <script type="text/javascript">
  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:         }
  262:     }
  263:     return;
  264: }
  265: 
  266: </script>
  267: END
  268:         $r->print('<h3>'.&mt('LON-CAPA account required').'</h3>'.
  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>');
  270:         if (@cancreate > 1) {
  271:             $r->print('<li>'.&mt('Otherwise:').'<br />');
  272:             my $count = 0;
  273:             foreach my $type ('sso','login','email') { 
  274:                 if (($count > 0) && ($count <@cancreate)) {
  275:                     $r->print('&nbsp;, '.&mt('or').'<br />');
  276:                 }
  277:                 if (grep(/^$type$/,@cancreate)) {
  278:                     if ($type eq 'sso') {
  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>'));
  280:                     } elsif ($type eq 'login') {
  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>'));
  282:                     } elsif ($type eq 'email') {
  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>'));
  284:                     }
  285:                     $count ++;
  286:                 }
  287:             }
  288:             $r->print('.<br />');
  289:         } else {
  290:             if ($cancreate[0] eq 'login') {
  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>'));
  292:             } elsif ($cancreate[0] eq 'email') {
  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>'));
  294:             } elsif ($cancreate[0] eq 'sso') {
  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>'));
  296:             }
  297:         }
  298:         if ($selfenroll_types eq '*') {
  299:             $r->print('<br />'.&mt('You will be able to self enroll in this course once the account has been created.').'</li></ul>');
  300:         } else {
  301:             $r->print('</li></ul>');
  302:             my ($enrolltypes,$longtypes,$alldoms) = 
  303:                 &get_selfenroll_filters($selfenroll_types);
  304:             $r->print(&print_selfenroll_types($longtypes));
  305:         }
  306:     } else {
  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));
  310:         if ($selfenroll_types ne '*') {
  311:             my ($enrolltypes,$longtypes,$alldoms) =
  312:                 &get_selfenroll_filters($selfenroll_types);
  313:             $r->print('<br />'.&print_selfenroll_types($longtypes));
  314:         }
  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));
  317:     }
  318:     $r->print("\n".'<form name="'.$form.'" method="post" action="">'."\n".
  319:               '<input type="hidden" name="courseid" value="'.$courseid.'" />'."\n".
  320:               '<input type="hidden" name="setting" value=""/>'."\n".
  321:               &Apache::lonhtmlcommon::echo_form_input(['backto','courseid','context','phase'])."\n".
  322:               '</form>'."\n");
  323:               &page_footer($r);
  324:     return OK;
  325: }
  326: 
  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: 
  349: sub page_header {
  350:     my ($r,$courseid,$js,$desc) = @_;
  351:     my $start_page =
  352:         &Apache::loncommon::start_page('Self-enroll in a LON-CAPA course',$js,
  353:                                        {'no_inline_link'   => 1,});
  354:     $r->print($start_page);
  355:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  356:     &Apache::createaccount::selfenroll_crumbs($r,$courseid,$desc);
  357:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Self-enroll in course'));
  358:     return;
  359: }
  360: 
  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: 
  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) = @_;
  380:     my @cancreate;
  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') {
  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:                 }
  393:             }
  394:         }
  395:     }
  396:     return @cancreate;
  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'});
  404:           $output = &mt('You already have an active student role (section: "[_1]") in this course.',$curr_role{'section'}).'<br />'.$rolelink;
  405:     } elsif ($curr_role{'status'} eq 'future') {
  406:         $output = &mt('You have a student role (section: "[_1]") in this course which will become active [_2].',$curr_role{'section'},$curr_role{'start'});
  407:     }
  408:     return $output;
  409: }
  410: 
  411: sub process_self_enroll {
  412:     my ($r,$cdom,$cnum,$selfenroll_types,$selfenroll_registered,
  413:         $selfenroll_access_start,$selfenroll_access_end,$selfenroll_section,
  414:         $now,$selfenroll_approval,$selfenroll_notifylist,$owner,$crstype,$lonhost,$handle) = @_;
  415:     my $udom = $env{'user.domain'};
  416:     my $uname = $env{'user.name'};
  417:     my $selfenroll = 0;
  418:     my ($enrolltypes,$longtypes,$alldoms);
  419:     if ($selfenroll_types eq '*') {
  420:         $selfenroll = 1;
  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});
  432:                 }
  433:             }
  434:         }
  435:     }
  436:     if ($selfenroll) {
  437:         my $usec = $selfenroll_section;
  438:         if ($selfenroll_section eq 'none') {
  439:             $usec = '';
  440:         }
  441:         if ($selfenroll_registered) {
  442:             my ($registered,$instsec,$message) = &check_registered($cdom,$cnum);
  443:             $usec = $instsec;
  444:             if (!$registered) {
  445:                 $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.
  446:                           &mt('Self-enrollment is restricted to students officially registered for this course.').'<br />');
  447:                 if ($message) {
  448:                     $r->print($message);
  449:                 } else {
  450:                     $r->print(&mt('As you are not currently registered for this course, self-enrollment is unavailable.'));
  451:                 }
  452:                 return; 
  453:             } 
  454:         }
  455:         if ($selfenroll_approval) {
  456:             my $outcome = 
  457:                 &store_selfenroll_request($udom,$uname,$usec,$cdom,$cnum,
  458:                                           $selfenroll_notifylist,$owner,
  459:                                           $selfenroll_approval,$crstype,$lonhost,$handle);
  460:             $r->print($outcome);
  461:         } else {
  462:             my $enrollresult = 
  463:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,
  464:                         undef,undef,$usec,$selfenroll_access_end,$selfenroll_access_start,
  465:                        'selfenroll',undef,$cdom.'_'.$cnum,$selfenroll);
  466:             if ($enrollresult eq 'ok') {
  467:                 my (%userroles,%newrole,%newgroups);
  468:                 my $role = 'st';
  469:                 my $area = '/'.$cdom.'/'.$cnum;
  470:                 my $spec = $role.'.'.$area;
  471:                 if ($usec ne '') {
  472:                     $spec .= '/'.$usec;
  473:                     $area .= '/'.$usec;
  474:                 }
  475:                 &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,
  476:                                                     $area);
  477:                 &Apache::lonnet::set_userprivs(\%userroles,\%newrole,\%newgroups);
  478:                 $userroles{'user.role.'.$spec} = $selfenroll_access_start.'.'.$selfenroll_access_end;
  479:                 &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
  480:                 $r->print('<h3>'.&mt('Enrollment process complete').'</h3>');
  481:                 if ($selfenroll_access_end && $selfenroll_access_end <= $now) {
  482:                     $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.'));
  483:                 } else {
  484:                     $r->print(&mt('Self-enrollment in this course was successful.').'<br />');
  485:                     my $showstart = &Apache::lonlocal::locallocaltime($selfenroll_access_start);
  486:                     my $showend = &Apache::lonlocal::locallocaltime($selfenroll_access_end);
  487:                     if ($selfenroll_access_start && $selfenroll_access_start >$now) {
  488:                         $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));
  489:                     } else {
  490:                         my $newrole = 'st./'.$cdom.'/'.$cnum;
  491:                         if ($usec ne '') {
  492:                             $newrole .= '/'.$usec;
  493:                         }
  494:                         my $rolelink = &jump_to_role($newrole);
  495:                         $r->print(&mt('Your new role is available immediately, and will provide access to the course until [_1].',$showend).'<br /><br />'."\n".
  496:                                  $rolelink); 
  497:                     }
  498:                 }
  499:             } else {
  500:                 $r->print('<h3>'.&mt('Enrollment incomplete').'</h3>'.
  501:                           &mt('Self-enrollment in this course failed.'));
  502:                 if ($enrollresult ne '') {
  503:                     $r->print('<span class="LC_error">'.$enrollresult.'</span>');
  504:                 }
  505:             }
  506:         }
  507:     } else {
  508:         $r->print('<h3>'.&mt('Self-enrollment unavailable').'</h3>'.
  509:                   &mt('You are not permitted to enroll yourself in this course.').'<br />');
  510:         $r->print(&print_selfenroll_types($longtypes));
  511:     }
  512:     return;
  513: }
  514: 
  515: sub user_can_selfenroll {
  516:     my ($udom,$uname,$domenrolltypes) = @_;
  517:     my $selfenroll = 0;
  518:     my %userhash = &Apache::lonnet::userenvironment($udom,$uname,'inststatus');
  519:     my @inststatuses;
  520:     if ($userhash{'inststatus'} eq '') {
  521:         push(@inststatuses,'other');
  522:     } else {
  523:         @inststatuses = split(':',$userhash{'inststatus'});
  524:     }
  525:     foreach my $type (@inststatuses) {
  526:         if (ref($domenrolltypes) eq 'ARRAY') {
  527:             if (grep(/^\Q$type\E$/,@{$domenrolltypes})) {
  528:                 $selfenroll = 1;
  529:                 last;
  530:             }
  531:         }
  532:     }
  533:     return $selfenroll;
  534: }
  535: 
  536: sub store_selfenroll_request {
  537:     my ($udom,$uname,$usec,$cdom,$cnum,$selfenroll_notifylist,$owner,
  538:         $selfenroll_approval,$crstype,$lonhost,$handle) = @_;
  539:     my $namespace = 'selfenrollrequests';
  540:     my $output;
  541:     my $now = time;
  542:     my %existing = 
  543:         &Apache::lonnet::get($namespace,[$uname.':'.$udom],$cdom,$cnum);
  544:     if ($existing{$uname.':'.$udom}) {
  545:         my $status;
  546:         $output = &mt('A self-enrollment request already exists for you for this course.').'<br />';
  547:         my %info = &Apache::lonnet::get($namespace,[$cdom.'_'.$cnum],$udom,$uname);
  548:         if (ref($info{$cdom.'_'.$cnum}) eq 'HASH') {
  549:             $status = $info{$cdom.'_'.$cnum}{'status'};
  550:         }
  551:         if ($status eq 'pending') {
  552:             my $token = $info{$cdom.'_'.$cnum}{'token'};
  553:             my ($statusupdate,$pendingform) = &pending_selfenrollment_form($cdom,$cnum,$crstype,$token,$lonhost);
  554:             if ($statusupdate eq 'pending') {
  555:                 $output .= $pendingform;
  556:             }
  557:         } else {
  558:             $output .= &mt('Your earlier request is in a queue awaiting action by a Course Coordinator.').
  559:                        '<br /><br />'.&Apache::loncoursequeueadmin::queued_selfenrollment();
  560:         }
  561:     } else {
  562:         my %selfenroll = (
  563:                             $uname.':'.$udom => $now.':'.$usec,
  564:                          );
  565:         my $putresult = &Apache::lonnet::put($namespace,\%selfenroll,$cdom,$cnum);
  566:         my $status = 'request';
  567:         if ($selfenroll_approval eq '2') {
  568:             $status = 'pending';
  569:         } 
  570:         if ($putresult eq 'ok') {
  571:             my %userenroll = (
  572:                                $cdom.'_'.$cnum =>  {
  573:                                                       timestamp => $now,
  574:                                                       section   => $usec,
  575:                                                       status    => $status,
  576:                                                    });
  577:             my $token;
  578:             if ($status eq 'pending') {
  579:                 $token = &Apache::lonnet::tmpput(\%selfenroll,$lonhost);
  580:                 $userenroll{$cdom.'_'.$cnum}{'token'} = $token;
  581:                 $userenroll{$cdom.'_'.$cnum}{'lonhost'} = $lonhost;
  582:                 $userenroll{$cdom.'_'.$cnum}{'handle'} = $handle; 
  583:             } 
  584:             my $warning;
  585:             my $userresult = &Apache::lonnet::put($namespace,\%userenroll,$udom,$uname);
  586:             if ($userresult ne 'ok') {
  587:                 $warning = &mt('An error occurred saving a personal record of your request.');
  588:             }
  589:             $output = &mt('Your request for self-enrollment has been recorded.').'<br />';
  590:             if ($status eq 'pending') {
  591:                 my ($statusupdate,$pendingform) = &pending_selfenrollment_form($cdom,$cnum,$crstype,$token,$lonhost);
  592:                 if ($statusupdate eq 'request') {
  593:                     $status = $statusupdate;
  594:                 } else {
  595:                     $output .= $pendingform;
  596:                 }
  597:             }
  598:             if ($status eq 'request') {
  599:                 $output .= &mt('A message will be sent to your LON-CAPA account when the course coordinator takes action on your request.').'<br />'.
  600:                            &mt('To access your LON-CAPA message, go to the Main Menu and click on "Send and Receive Messages".').'<br />';
  601:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
  602:                 if (($emails{'permanentemail'} ne '') || ($emails{'notification'} ne '')) {
  603:                     my $address = $emails{'permanentemail'};
  604:                     if ($address eq '') {
  605:                         $address = $emails{'notification'};
  606:                     }
  607:                     $output.= &mt('An e-mail will also be sent to: [_1] when this occurs.',$address).'<br />';
  608:                 }
  609:                 if ($warning) { 
  610:                     $output .= '<span class="LC_warning">'.$warning.'</span><br />';
  611:                 }
  612:                 $output .= &Apache::loncoursequeueadmin::queued_selfenrollment();
  613: 
  614:                 if ($selfenroll_notifylist) {
  615:                     my $fullname = &Apache::loncommon::plainname($uname,$udom);
  616:                     my %courseinfo = &Apache::lonnet::coursedescription($cdom.'_'.$cnum);
  617:                     my $coursedesc = $courseinfo{'description'};
  618:                     &Apache::loncoursequeueadmin::send_selfserve_notification(
  619:                         $selfenroll_notifylist,$fullname,$cdom.'_'.$cnum,
  620:                         $coursedesc,$now,'selfenrollreq',$owner);
  621:                 }
  622:             }
  623:         } else {
  624:             $output = '<span class="LC_error">'.&mt('An error occurred when recording your request.').'</span>';
  625: 
  626:         }
  627:     }
  628:     return $output;
  629: }
  630: 
  631: sub pending_selfenrollment_form {
  632:     my ($cdom,$cnum,$crstype,$token,$lonhost) = @_;
  633:     my ($status,$output);
  634:     my $coursetype = &Apache::lonuserutils::get_extended_type($cdom,$cnum,$crstype);
  635:     my %postvalues = (
  636:                       'username'   => $env{'user.name'},
  637:                       'domain'     => $env{'user.domain'},
  638:                       'course'     => $cdom.'_'.$cnum,
  639:                       'coursetype' => $coursetype,
  640:                                  );
  641:     my %domconfig = &Apache::lonnet::get_dom('configuration',['selfenrollment'],$cdom);
  642: 
  643:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
  644:         my ($url,$buttontext,$code,@fields);
  645:         if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
  646:             my %courseinfo = &Apache::lonnet::coursedescription($cdom.'_'.$cnum,{ 'one_time' => 1});
  647:             $postvalues{'uniquecode'} = $courseinfo{'internal.uniquecode'};
  648:             $postvalues{'description'} = $courseinfo{'description'};
  649:             $url = $domconfig{'selfenrollment'}{'validation'}{'url'};
  650:             if (ref($domconfig{'selfenrollment'}{'validation'}{'fields'}) eq 'ARRAY') {
  651:                 @fields = @{$domconfig{'selfenrollment'}{'validation'}{'fields'}};
  652:             }
  653:             $buttontext = $domconfig{'selfenrollment'}{'validation'}{'button'};
  654: 
  655:             $output .= $domconfig{'selfenrollment'}{'validation'}{'markup'};
  656:             if (($url =~ m{^(https?\://|/)}) && (@fields > 0)) {
  657:                 $output .= '<form name="selfenrollvalidation" action="'.$url.'" method="post">'."\n";
  658:                 foreach my $field (@fields) {
  659:                     if ($postvalues{$field}) {
  660:                         $output .= '<input type="hidden" name="'.$field.'" value="'.$postvalues{$field}.'" />'."\n";
  661:                     }
  662:                 }
  663:                 if ($buttontext eq '') {
  664:                     $buttontext = &mt('Complete my enrollment');
  665:                 }
  666:                 my $protocol = $Apache::lonnet::protocol{$lonhost};
  667:                 $protocol = 'http' if ($protocol ne 'https');
  668:                 my $enroller = $protocol.'://'.&Apache::lonnet::hostname($lonhost).'/cgi-bin/enrollqueued.pl';
  669:                 $output .= '<input type="hidden" name="enroller" value="'.$enroller.'" />'."\n".
  670:                            '<input type="hidden" name="token" value="'.$token.'" />'."\n".
  671:                            '<input type="submit" name="validate" value="'.$buttontext.'" />'."\n".
  672:                            '</form>'."\n";
  673:                 $status = 'pending';
  674:             } else {
  675:                 $status = 'request';
  676:             }
  677:         }
  678:     } else {
  679:         $status = 'request';
  680:     }
  681:     return ($status,$output);
  682: }
  683: 
  684: sub jump_to_role {
  685:     my ($role) = @_;
  686:     my $output = <<"END";
  687: <script type="text/javascript">
  688: function SelectRole() {
  689:     document.rolechoice.selectrole.value = '1';
  690:     document.rolechoice.submit();
  691: }
  692: </script>
  693: END
  694:     $output .= '<a href="javascript:SelectRole()">'."\n". 
  695:                &mt('Enter course now').'</a>'."\n".
  696:                '<form name="rolechoice" action="/adm/roles" method="post">'."\n".
  697:                '<input type="hidden" name="selectrole" value="" />'."\n".
  698:                '<input type="hidden" name="'.$role.'" value="1" />'."\n".
  699:                '</form>';
  700:     return $output;
  701: }
  702: 
  703: sub get_selfenroll_filters {
  704:     my ($selfenroll_types,$domain) = @_;
  705:     my (%enrolltypes,%longtypes,$alldoms);
  706:     my @selfenrolldoms = split(/;/,$selfenroll_types);
  707:     foreach my $item (@selfenrolldoms) {
  708:         my ($selfdom,$type_str) = split(/:/,$item);
  709:         if ($selfdom eq '*') {
  710:             $alldoms = 1;
  711:             last;
  712:         }
  713:         if ($domain ne '') {
  714:             next if ($selfdom ne $domain);
  715:         }
  716:         if ($selfdom =~ /^$match_domain$/) {
  717:             if (&Apache::lonnet::domain($selfdom) ne '') {
  718:                 my @types = split(/,/,$type_str);
  719:                 my @unesc_types = map { &unescape($_); } @types;
  720:                 my ($othertitle,$usertypes,$types) =
  721:                     &Apache::loncommon::sorted_inst_types($selfdom);
  722:                 if (ref($usertypes) eq 'HASH') {
  723:                     my $anytype = 1;
  724:                     foreach my $key (keys(%{$usertypes})) {
  725:                         if (!grep(/^\Q$key\E$/,@unesc_types)) {
  726:                             $anytype = 0;
  727:                             last;
  728:                         }
  729:                     }
  730:                     if ($anytype) {
  731:                         if (!(grep(/^other$/,@types))) {
  732:                             $anytype = 0;
  733:                         }
  734:                     }
  735:                     $usertypes->{'any'} = &mt('Any users');
  736:                     $usertypes->{'other'} = &mt('Others');
  737:                     my @showtypes;
  738:                     if ($anytype) {
  739:                         @{$enrolltypes{$selfdom}} = ('any');
  740:                         @showtypes =  ('any');
  741:                     } else {
  742:                         @{$enrolltypes{$selfdom}} = @types;
  743:                         @showtypes = @unesc_types;
  744:                     }
  745:                     @{$longtypes{$selfdom}} = 
  746:                         map {$usertypes->{$_}} @showtypes;
  747:                 }
  748:             }
  749:         }
  750:     }
  751:     return (\%enrolltypes,\%longtypes,$alldoms);
  752: }
  753: 
  754: sub print_selfenroll_types {
  755:     my ($longtypes,$domain) = @_;
  756:     my $output;
  757:     if (ref($longtypes) eq 'HASH') {
  758:         if ($domain ne '') {
  759:             my $domdesc = &Apache::lonnet::domain($domain,'description');
  760:             if (ref($longtypes->{$domain}) eq 'ARRAY') {
  761:                 if (grep(/^any$/,@{$longtypes->{$domain}})) {
  762:                     $output = &mt('Self-enrollment in this course is available to any user affiliated with [_1].',$domdesc);
  763:                 } else {
  764:                     my $status_str = join(', ',@{$longtypes->{$domain}});
  765:                     $output = &mt('Self-enrollment in this course is only available to users affiliated with [_1] who have the following status: "[_2]".',$domdesc,$status_str);
  766:                 }
  767:             } else {
  768:                 $output = &mt('Self-enrollment is not currently available for this course for users affiliated with [_1].',$domdesc);
  769:             }
  770:         } elsif (keys(%{$longtypes}) > 0) {
  771:             $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";
  772:             foreach my $selfdom (sort(keys(%{$longtypes}))) {
  773:                 my $selfdomdesc = &Apache::lonnet::domain($selfdom,'description');
  774:                 if (ref($longtypes->{$selfdom}) eq 'ARRAY') {
  775:                     $output .= '<li>'.$selfdomdesc.': '.join(', ',@{$longtypes->{$selfdom}}).'</li>'."\n";
  776:                 }
  777:             }
  778:             $output .= '</ul>'."\n";
  779:         } else {
  780:             $output = &mt('Self-enrollment is not currently available for this course.');
  781:         }
  782:     }
  783:     return $output;
  784: }
  785: 
  786: sub check_registered {
  787:     my ($cdom,$cnum) = @_;
  788:     my ($registered,$instsec,$message);
  789:     my %settings = &Apache::lonnet::get('environment',['internal.coursecode',
  790:                                         'internal.sectionnums',
  791:                                         'internal.crosslistings'],$cdom,$cnum);
  792:     my (@allcourses,%LC_code,%affiliates,%reply);
  793:     &Apache::loncommon::get_institutional_codes(\%settings,\@allcourses,\%LC_code);
  794:     if (@allcourses > 0) {
  795:         @{$affiliates{$cnum}} = @allcourses;
  796:         my $outcome = &Apache::lonnet::fetch_enrollment_query('updatenow',\%affiliates,\%reply,$cdom,$cnum);
  797:         if ($outcome eq 'ok') {
  798:             if ($reply{$cnum} > 0) {
  799:                 foreach my $class (@allcourses) {
  800:                     my %enrolled;
  801:                     my $dir = $Apache::lonnet::perlvar{'lonDaemons'};
  802:                     my %place = &LONCAPA::Enrollment::place_hash();
  803:                     &LONCAPA::Enrollment::parse_classlist($dir,$cdom,$cnum,$class,
  804:                              \%place,$LC_code{$class},\%enrolled);
  805:                     if (defined($enrolled{$env{'user.name'}})) {
  806:                         $registered = 1;
  807:                         $instsec = $LC_code{$class};
  808:                         last;
  809:                     }
  810:                 }
  811:             } else {
  812:                 $message = &mt('Your registration status could not be verified.');
  813:             }
  814:         } else {
  815:              $message = &mt('Your registration status could not determined, because a problem occurred retrieving data.');
  816:         }
  817:     } else {
  818:         $message = &mt('As no institutional course sections are currently associated with this course, your registration status is undetermined.'); 
  819:     }
  820:     return ($registered,$instsec,$message);
  821: }
  822: 
  823: 1;
  824: 

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