File:  [LON-CAPA] / loncom / interface / selfenroll.pm
Revision 1.13: download - view: text, annotated - select for diffs
Thu Jul 17 20:08:17 2008 UTC (15 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_6_99_1, HEAD
- lonnet::userenvironment() takes items to be retrieved (last arg) as an array, not a ref to an array.
- institutional status types allowed to self-enroll stored in an escape form, need to unescape before
   creating string to display which types are permitted to self-enroll.

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

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