File:  [LON-CAPA] / loncom / interface / selfenroll.pm
Revision 1.27: download - view: text, annotated - select for diffs
Wed May 16 21:19:39 2012 UTC (12 years, 1 month ago) by droeschl
Branches: MAIN
CVS tags: version_2_11_X, HEAD
Replaced calls to selfenroll::validate_course_id and createaccount::validate_course by lonnet::is_course.
Extended functionality of lonnet::is_course.
Code cleanup.

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

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