File:  [LON-CAPA] / loncom / interface / selfenroll.pm
Revision 1.14: download - view: text, annotated - select for diffs
Wed Jul 30 16:25:35 2008 UTC (15 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_8_X, version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_7_0, HEAD
- Eliminate duplicate definition.

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

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