File:  [LON-CAPA] / loncom / interface / selfenroll.pm
Revision 1.26: download - view: text, annotated - select for diffs
Mon Mar 22 20:11:22 2010 UTC (14 years, 1 month ago) by droeschl
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
In process of removing remote control, remote navmap and different icon modes code
- removed no_inline_link argument from loncommon::start_page, loncommon::bodytag
- removed subs
    o loncommon::head: it wasn't used
    o loncommon::endheadtag: functionality now covered by loncommon::headtag
    o lonmenu::registerurl: functionality moved to loncommon::headtag, because it was the only function that called registerurl
- removed remote control related translations in de.pm
- refactoring and cleanup

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

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