File:  [LON-CAPA] / loncom / interface / loncreateuser.pm
Revision 1.451: download - view: text, annotated - select for diffs
Mon Apr 29 22:19:24 2019 UTC (5 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Support domain configuration which allows a Course Owner to change a
  student's password, if:
 (a) same domain is used by owner, course, and student
 (b) student has no active or future roles besides student role in courses
     owned by the course owner making the change
 (c) course container is not Community or Placement Test
 (d) owner is course cordinator in the course
 (e) setting to disable this action has not been set for the specific course

    1: # The LearningOnline Network with CAPA
    2: # Create a user
    3: #
    4: # $Id: loncreateuser.pm,v 1.451 2019/04/29 22:19:24 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: 
   30: package Apache::loncreateuser;
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::loncreateuser.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40:     Handler to create users and custom roles
   41: 
   42:     Provides an Apache handler for creating users,
   43:     editing their login parameters, roles, and removing roles, and
   44:     also creating and assigning custom roles.
   45: 
   46: =head1 OVERVIEW
   47: 
   48: =head2 Custom Roles
   49: 
   50: In LON-CAPA, roles are actually collections of privileges. "Teaching
   51: Assistant", "Course Coordinator", and other such roles are really just
   52: collection of privileges that are useful in many circumstances.
   53: 
   54: Custom roles can be defined by a Domain Coordinator, Course Coordinator
   55: or Community Coordinator via the Manage User functionality.
   56: The custom role editor screen will show all privileges which can be
   57: assigned to users. For a complete list of privileges, please see 
   58: C</home/httpd/lonTabs/rolesplain.tab>.
   59: 
   60: Custom role definitions are stored in the C<roles.db> file of the creator
   61: of the role.
   62: 
   63: =cut
   64: 
   65: use strict;
   66: use Apache::Constants qw(:common :http);
   67: use Apache::lonnet;
   68: use Apache::loncommon;
   69: use Apache::lonlocal;
   70: use Apache::longroup;
   71: use Apache::lonuserutils;
   72: use Apache::loncoursequeueadmin;
   73: use LONCAPA qw(:DEFAULT :match);
   74: 
   75: my $loginscript; # piece of javascript used in two separate instances
   76: my $authformnop;
   77: my $authformkrb;
   78: my $authformint;
   79: my $authformfsys;
   80: my $authformloc;
   81: my $authformlti;
   82: 
   83: sub initialize_authen_forms {
   84:     my ($dom,$formname,$curr_authtype,$mode) = @_;
   85:     my ($krbdef,$krbdefdom) = &Apache::loncommon::get_kerberos_defaults($dom);
   86:     my %param = ( formname => $formname,
   87:                   kerb_def_dom => $krbdefdom,
   88:                   kerb_def_auth => $krbdef,
   89:                   domain => $dom,
   90:                 );
   91:     my %abv_auth = &auth_abbrev();
   92:     if ($curr_authtype =~ /^(krb4|krb5|internal|localauth|unix|lti):(.*)$/) {
   93:         my $long_auth = $1;
   94:         my $curr_autharg = $2;
   95:         my %abv_auth = &auth_abbrev();
   96:         $param{'curr_authtype'} = $abv_auth{$long_auth};
   97:         if ($long_auth =~ /^krb(4|5)$/) {
   98:             $param{'curr_kerb_ver'} = $1;
   99:             $param{'curr_autharg'} = $curr_autharg;
  100:         }
  101:         if ($mode eq 'modifyuser') {
  102:             $param{'mode'} = $mode;
  103:         }
  104:     }
  105:     $loginscript  = &Apache::loncommon::authform_header(%param);
  106:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
  107:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
  108:     $authformint  = &Apache::loncommon::authform_internal(%param);
  109:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
  110:     $authformloc  = &Apache::loncommon::authform_local(%param);
  111:     $authformlti  = &Apache::loncommon::authform_lti(%param);
  112: }
  113: 
  114: sub auth_abbrev {
  115:     my %abv_auth = (
  116:                      krb5      => 'krb',
  117:                      krb4      => 'krb',
  118:                      internal  => 'int',
  119:                      localauth => 'loc',
  120:                      unix      => 'fsys',
  121:                      lti       => 'lti',
  122:                    );
  123:     return %abv_auth;
  124: }
  125: 
  126: # ====================================================
  127: 
  128: sub user_quotas {
  129:     my ($ccuname,$ccdomain) = @_;
  130:     my %lt = &Apache::lonlocal::texthash(
  131:                    'usrt'      => "User Tools",
  132:                    'cust'      => "Custom quota",
  133:                    'chqu'      => "Change quota",
  134:     );
  135:    
  136:     my $quota_javascript = <<"END_SCRIPT";
  137: <script type="text/javascript">
  138: // <![CDATA[
  139: function quota_changes(caller,context) {
  140:     var customoff = document.getElementById('custom_'+context+'quota_off');
  141:     var customon = document.getElementById('custom_'+context+'quota_on');
  142:     var number = document.getElementById(context+'quota');
  143:     if (caller == "custom") {
  144:         if (customoff) {
  145:             if (customoff.checked) {
  146:                 number.value = "";
  147:             }
  148:         }
  149:     }
  150:     if (caller == "quota") {
  151:         if (customon) {
  152:             customon.checked = true;
  153:         }
  154:     }
  155:     return;
  156: }
  157: // ]]>
  158: </script>
  159: END_SCRIPT
  160:     my $longinsttype;
  161:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
  162:     my $output = $quota_javascript."\n".
  163:                  '<h3>'.$lt{'usrt'}.'</h3>'."\n".
  164:                  &Apache::loncommon::start_data_table();
  165: 
  166:     if ((&Apache::lonnet::allowed('mut',$ccdomain)) ||
  167:         (&Apache::lonnet::allowed('udp',$ccdomain))) {
  168:         $output .= &build_tools_display($ccuname,$ccdomain,'tools');
  169:     }
  170: 
  171:     my %titles = &Apache::lonlocal::texthash (
  172:                     portfolio => "Disk space allocated to user's portfolio files",
  173:                     author    => "Disk space allocated to user's Authoring Space (if role assigned)",
  174:                  );
  175:     foreach my $name ('portfolio','author') {
  176:         my ($currquota,$quotatype,$inststatus,$defquota) =
  177:             &Apache::loncommon::get_user_quota($ccuname,$ccdomain,$name);
  178:         if ($longinsttype eq '') { 
  179:             if ($inststatus ne '') {
  180:                 if ($usertypes->{$inststatus} ne '') {
  181:                     $longinsttype = $usertypes->{$inststatus};
  182:                 }
  183:             }
  184:         }
  185:         my ($showquota,$custom_on,$custom_off,$defaultinfo);
  186:         $custom_on = ' ';
  187:         $custom_off = ' checked="checked" ';
  188:         if ($quotatype eq 'custom') {
  189:             $custom_on = $custom_off;
  190:             $custom_off = ' ';
  191:             $showquota = $currquota;
  192:             if ($longinsttype eq '') {
  193:                 $defaultinfo = &mt('For this user, the default quota would be [_1]'
  194:                               .' MB.',$defquota);
  195:             } else {
  196:                 $defaultinfo = &mt("For this user, the default quota would be [_1]".
  197:                                    " MB, as determined by the user's institutional".
  198:                                    " affiliation ([_2]).",$defquota,$longinsttype);
  199:             }
  200:         } else {
  201:             if ($longinsttype eq '') {
  202:                 $defaultinfo = &mt('For this user, the default quota is [_1]'
  203:                               .' MB.',$defquota);
  204:             } else {
  205:                 $defaultinfo = &mt("For this user, the default quota of [_1]".
  206:                                    " MB, is determined by the user's institutional".
  207:                                    " affiliation ([_2]).",$defquota,$longinsttype);
  208:             }
  209:         }
  210: 
  211:         if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
  212:             $output .= '<tr class="LC_info_row">'."\n".
  213:                        '    <td>'.$titles{$name}.'</td>'."\n".
  214:                        '  </tr>'."\n".
  215:                        &Apache::loncommon::start_data_table_row()."\n".
  216:                        '  <td><span class="LC_nobreak">'.
  217:                        &mt('Current quota: [_1] MB',$currquota).'</span>&nbsp;&nbsp;'.
  218:                        $defaultinfo.'</td>'."\n".
  219:                        &Apache::loncommon::end_data_table_row()."\n".
  220:                        &Apache::loncommon::start_data_table_row()."\n".
  221:                        '  <td><span class="LC_nobreak">'.$lt{'chqu'}.
  222:                        ': <label>'.
  223:                        '<input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_off" '.
  224:                        'value="0" '.$custom_off.' onchange="javascript:quota_changes('."'custom','$name'".');"'.
  225:                        ' /><span class="LC_nobreak">'.
  226:                        &mt('Default ([_1] MB)',$defquota).'</span></label>&nbsp;'.
  227:                        '&nbsp;<label><input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_on" '.
  228:                        'value="1" '.$custom_on.'  onchange="javascript:quota_changes('."'custom','$name'".');"'.
  229:                        ' />'.$lt{'cust'}.':</label>&nbsp;'.
  230:                        '<input type="text" name="'.$name.'quota" id="'.$name.'quota" size ="5" '.
  231:                        'value="'.$showquota.'" onfocus="javascript:quota_changes('."'quota','$name'".');"'.
  232:                        ' />&nbsp;'.&mt('MB').'</span></td>'."\n".
  233:                        &Apache::loncommon::end_data_table_row()."\n";
  234:         }
  235:     }
  236:     $output .= &Apache::loncommon::end_data_table();
  237:     return $output;
  238: }
  239: 
  240: sub build_tools_display {
  241:     my ($ccuname,$ccdomain,$context) = @_;
  242:     my (@usertools,%userenv,$output,@options,%validations,%reqtitles,%reqdisplay,
  243:         $colspan,$isadv,%domconfig);
  244:     my %lt = &Apache::lonlocal::texthash (
  245:                    'blog'       => "Personal User Blog",
  246:                    'aboutme'    => "Personal Information Page",
  247:                    'webdav'     => "WebDAV access to Authoring Spaces (if SSL and author/co-author)",
  248:                    'portfolio'  => "Personal User Portfolio",
  249:                    'avai'       => "Available",
  250:                    'cusa'       => "availability",
  251:                    'chse'       => "Change setting",
  252:                    'usde'       => "Use default",
  253:                    'uscu'       => "Use custom",
  254:                    'official'   => 'Can request creation of official courses',
  255:                    'unofficial' => 'Can request creation of unofficial courses',
  256:                    'community'  => 'Can request creation of communities',
  257:                    'textbook'   => 'Can request creation of textbook courses',
  258:                    'placement'  => 'Can request creation of placement tests',
  259:                    'lti'        => 'Can request creation of LTI courses',
  260:                    'requestauthor'  => 'Can request author space',
  261:     );
  262:     if ($context eq 'requestcourses') {
  263:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
  264:                       'requestcourses.official','requestcourses.unofficial',
  265:                       'requestcourses.community','requestcourses.textbook',
  266:                       'requestcourses.placement','requestcourses.lti');
  267:         @usertools = ('official','unofficial','community','textbook','placement','lti');
  268:         @options =('norequest','approval','autolimit','validate');
  269:         %validations = &Apache::lonnet::auto_courserequest_checks($ccdomain);
  270:         %reqtitles = &courserequest_titles();
  271:         %reqdisplay = &courserequest_display();
  272:         $colspan = ' colspan="2"';
  273:         %domconfig =
  274:             &Apache::lonnet::get_dom('configuration',['requestcourses'],$ccdomain);
  275:         $isadv = &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
  276:     } elsif ($context eq 'requestauthor') {
  277:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
  278:                                                     'requestauthor');
  279:         @usertools = ('requestauthor');
  280:         @options =('norequest','approval','automatic');
  281:         %reqtitles = &requestauthor_titles();
  282:         %reqdisplay = &requestauthor_display();
  283:         $colspan = ' colspan="2"';
  284:         %domconfig =
  285:             &Apache::lonnet::get_dom('configuration',['requestauthor'],$ccdomain);
  286:     } else {
  287:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
  288:                           'tools.aboutme','tools.portfolio','tools.blog',
  289:                           'tools.webdav');
  290:         @usertools = ('aboutme','blog','webdav','portfolio');
  291:     }
  292:     foreach my $item (@usertools) {
  293:         my ($custom_access,$curr_access,$cust_on,$cust_off,$tool_on,$tool_off,
  294:             $currdisp,$custdisp,$custradio);
  295:         $cust_off = 'checked="checked" ';
  296:         $tool_on = 'checked="checked" ';
  297:         $curr_access =  
  298:             &Apache::lonnet::usertools_access($ccuname,$ccdomain,$item,undef,
  299:                                               $context);
  300:         if ($context eq 'requestauthor') {
  301:             if ($userenv{$context} ne '') {
  302:                 $cust_on = ' checked="checked" ';
  303:                 $cust_off = '';
  304:             }  
  305:         } elsif ($userenv{$context.'.'.$item} ne '') {
  306:             $cust_on = ' checked="checked" ';
  307:             $cust_off = '';
  308:         }
  309:         if ($context eq 'requestcourses') {
  310:             if ($userenv{$context.'.'.$item} eq '') {
  311:                 $custom_access = &mt('Currently from default setting.');
  312:             } else {
  313:                 $custom_access = &mt('Currently from custom setting.');
  314:             }
  315:         } elsif ($context eq 'requestauthor') {
  316:             if ($userenv{$context} eq '') {
  317:                 $custom_access = &mt('Currently from default setting.');
  318:             } else {
  319:                 $custom_access = &mt('Currently from custom setting.');
  320:             }
  321:         } else {
  322:             if ($userenv{$context.'.'.$item} eq '') {
  323:                 $custom_access =
  324:                     &mt('Availability determined currently from default setting.');
  325:                 if (!$curr_access) {
  326:                     $tool_off = 'checked="checked" ';
  327:                     $tool_on = '';
  328:                 }
  329:             } else {
  330:                 $custom_access =
  331:                     &mt('Availability determined currently from custom setting.');
  332:                 if ($userenv{$context.'.'.$item} == 0) {
  333:                     $tool_off = 'checked="checked" ';
  334:                     $tool_on = '';
  335:                 }
  336:             }
  337:         }
  338:         $output .= '  <tr class="LC_info_row">'."\n".
  339:                    '   <td'.$colspan.'>'.$lt{$item}.'</td>'."\n".
  340:                    '  </tr>'."\n".
  341:                    &Apache::loncommon::start_data_table_row()."\n";
  342:   
  343:         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
  344:             my ($curroption,$currlimit);
  345:             my $envkey = $context.'.'.$item;
  346:             if ($context eq 'requestauthor') {
  347:                 $envkey = $context;
  348:             }
  349:             if ($userenv{$envkey} ne '') {
  350:                 $curroption = $userenv{$envkey};
  351:             } else {
  352:                 my (@inststatuses);
  353:                 if ($context eq 'requestcourses') {
  354:                     $curroption =
  355:                         &Apache::loncoursequeueadmin::get_processtype('course',$ccuname,$ccdomain,
  356:                                                                       $isadv,$ccdomain,$item,
  357:                                                                       \@inststatuses,\%domconfig);
  358:                 } else {
  359:                      $curroption = 
  360:                          &Apache::loncoursequeueadmin::get_processtype('requestauthor',$ccuname,$ccdomain,
  361:                                                                        $isadv,$ccdomain,undef,
  362:                                                                        \@inststatuses,\%domconfig);
  363:                 }
  364:             }
  365:             if (!$curroption) {
  366:                 $curroption = 'norequest';
  367:             }
  368:             if ($curroption =~ /^autolimit=(\d*)$/) {
  369:                 $currlimit = $1;
  370:                 if ($currlimit eq '') {
  371:                     $currdisp = &mt('Yes, automatic creation');
  372:                 } else {
  373:                     $currdisp = &mt('Yes, up to [quant,_1,request]/user',$currlimit);
  374:                 }
  375:             } else {
  376:                 $currdisp = $reqdisplay{$curroption};
  377:             }
  378:             $custdisp = '<table>';
  379:             foreach my $option (@options) {
  380:                 my $val = $option;
  381:                 if ($option eq 'norequest') {
  382:                     $val = 0;
  383:                 }
  384:                 if ($option eq 'validate') {
  385:                     my $canvalidate = 0;
  386:                     if (ref($validations{$item}) eq 'HASH') {
  387:                         if ($validations{$item}{'_custom_'}) {
  388:                             $canvalidate = 1;
  389:                         }
  390:                     }
  391:                     next if (!$canvalidate);
  392:                 }
  393:                 my $checked = '';
  394:                 if ($option eq $curroption) {
  395:                     $checked = ' checked="checked"';
  396:                 } elsif ($option eq 'autolimit') {
  397:                     if ($curroption =~ /^autolimit/) {
  398:                         $checked = ' checked="checked"';
  399:                     }
  400:                 }
  401:                 my $name = 'crsreq_'.$item;
  402:                 if ($context eq 'requestauthor') {
  403:                     $name = $item;
  404:                 }
  405:                 $custdisp .= '<tr><td><span class="LC_nobreak"><label>'.
  406:                              '<input type="radio" name="'.$name.'" '.
  407:                              'value="'.$val.'"'.$checked.' />'.
  408:                              $reqtitles{$option}.'</label>&nbsp;';
  409:                 if ($option eq 'autolimit') {
  410:                     $custdisp .= '<input type="text" name="'.$name.
  411:                                  '_limit" size="1" '.
  412:                                  'value="'.$currlimit.'" /></span><br />'.
  413:                                  $reqtitles{'unlimited'};
  414:                 } else {
  415:                     $custdisp .= '</span>';
  416:                 }
  417:                 $custdisp .= '</td></tr>';
  418:             }
  419:             $custdisp .= '</table>';
  420:             $custradio = '</span></td><td>'.&mt('Custom setting').'<br />'.$custdisp;
  421:         } else {
  422:             $currdisp = ($curr_access?&mt('Yes'):&mt('No'));
  423:             my $name = $context.'_'.$item;
  424:             if ($context eq 'requestauthor') {
  425:                 $name = $context;
  426:             }
  427:             $custdisp = '<span class="LC_nobreak"><label>'.
  428:                         '<input type="radio" name="'.$name.'"'.
  429:                         ' value="1" '.$tool_on.'/>'.&mt('On').'</label>&nbsp;<label>'.
  430:                         '<input type="radio" name="'.$name.'" value="0" '.
  431:                         $tool_off.'/>'.&mt('Off').'</label></span>';
  432:             $custradio = ('&nbsp;'x2).'--'.$lt{'cusa'}.':&nbsp;'.$custdisp.
  433:                           '</span>';
  434:         }
  435:         $output .= '  <td'.$colspan.'>'.$custom_access.('&nbsp;'x4).
  436:                    $lt{'avai'}.': '.$currdisp.'</td>'."\n".
  437:                    &Apache::loncommon::end_data_table_row()."\n";
  438:         unless (&Apache::lonnet::allowed('udp',$ccdomain)) {
  439:             $output .=
  440:                    &Apache::loncommon::start_data_table_row()."\n".
  441:                    '  <td style="vertical-align:top;"><span class="LC_nobreak">'.
  442:                    $lt{'chse'}.': <label>'.
  443:                    '<input type="radio" name="custom'.$item.'" value="0" '.
  444:                    $cust_off.'/>'.$lt{'usde'}.'</label>'.('&nbsp;' x3).
  445:                    '<label><input type="radio" name="custom'.$item.'" value="1" '.
  446:                    $cust_on.'/>'.$lt{'uscu'}.'</label>'.$custradio.'</td>'.
  447:                    &Apache::loncommon::end_data_table_row()."\n";
  448:         }
  449:     }
  450:     return $output;
  451: }
  452: 
  453: sub coursereq_externaluser {
  454:     my ($ccuname,$ccdomain,$cdom) = @_;
  455:     my (@usertools,@options,%validations,%userenv,$output);
  456:     my %lt = &Apache::lonlocal::texthash (
  457:                    'official'   => 'Can request creation of official courses',
  458:                    'unofficial' => 'Can request creation of unofficial courses',
  459:                    'community'  => 'Can request creation of communities',
  460:                    'textbook'   => 'Can request creation of textbook courses',
  461:                    'placement'  => 'Can request creation of placement tests',
  462:     );
  463: 
  464:     %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
  465:                       'reqcrsotherdom.official','reqcrsotherdom.unofficial',
  466:                       'reqcrsotherdom.community','reqcrsotherdom.textbook',
  467:                       'reqcrsotherdom.placement');
  468:     @usertools = ('official','unofficial','community','textbook','placement');
  469:     @options = ('approval','validate','autolimit');
  470:     %validations = &Apache::lonnet::auto_courserequest_checks($cdom);
  471:     my $optregex = join('|',@options);
  472:     my %reqtitles = &courserequest_titles();
  473:     foreach my $item (@usertools) {
  474:         my ($curroption,$currlimit,$tooloff);
  475:         if ($userenv{'reqcrsotherdom.'.$item} ne '') {
  476:             my @curr = split(',',$userenv{'reqcrsotherdom.'.$item});
  477:             foreach my $req (@curr) {
  478:                 if ($req =~ /^\Q$cdom\E\:($optregex)=?(\d*)$/) {
  479:                     $curroption = $1;
  480:                     $currlimit = $2;
  481:                     last;
  482:                 }
  483:             }
  484:             if (!$curroption) {
  485:                 $curroption = 'norequest';
  486:                 $tooloff = ' checked="checked"';
  487:             }
  488:         } else {
  489:             $curroption = 'norequest';
  490:             $tooloff = ' checked="checked"';
  491:         }
  492:         $output.= &Apache::loncommon::start_data_table_row()."\n".
  493:                   '  <td><span class="LC_nobreak">'.$lt{$item}.': </span></td><td>'.
  494:                   '<table><tr><td valign="top">'."\n".
  495:                   '<label><input type="radio" name="reqcrsotherdom_'.$item.
  496:                   '" value=""'.$tooloff.' />'.$reqtitles{'norequest'}.
  497:                   '</label></td>';
  498:         foreach my $option (@options) {
  499:             if ($option eq 'validate') {
  500:                 my $canvalidate = 0;
  501:                 if (ref($validations{$item}) eq 'HASH') {
  502:                     if ($validations{$item}{'_external_'}) {
  503:                         $canvalidate = 1;
  504:                     }
  505:                 }
  506:                 next if (!$canvalidate);
  507:             }
  508:             my $checked = '';
  509:             if ($option eq $curroption) {
  510:                 $checked = ' checked="checked"';
  511:             }
  512:             $output .= '<td valign="top"><span class="LC_nobreak"><label>'.
  513:                        '<input type="radio" name="reqcrsotherdom_'.$item.
  514:                        '" value="'.$option.'"'.$checked.' />'.
  515:                        $reqtitles{$option}.'</label>';
  516:             if ($option eq 'autolimit') {
  517:                 $output .= '&nbsp;<input type="text" name="reqcrsotherdom_'.
  518:                            $item.'_limit" size="1" '.
  519:                            'value="'.$currlimit.'" /></span>'.
  520:                            '<br />'.$reqtitles{'unlimited'};
  521:             } else {
  522:                 $output .= '</span>';
  523:             }
  524:             $output .= '</td>';
  525:         }
  526:         $output .= '</td></tr></table></td>'."\n".
  527:                    &Apache::loncommon::end_data_table_row()."\n";
  528:     }
  529:     return $output;
  530: }
  531: 
  532: sub domainrole_req {
  533:     my ($ccuname,$ccdomain) = @_;
  534:     return '<br /><h3>'.
  535:            &mt('User Can Request Assignment of Domain Roles?').
  536:            '</h3>'."\n".
  537:            &Apache::loncommon::start_data_table().
  538:            &build_tools_display($ccuname,$ccdomain,
  539:                                 'requestauthor').
  540:            &Apache::loncommon::end_data_table();
  541: }
  542: 
  543: sub courserequest_titles {
  544:     my %titles = &Apache::lonlocal::texthash (
  545:                                    official   => 'Official',
  546:                                    unofficial => 'Unofficial',
  547:                                    community  => 'Communities',
  548:                                    textbook   => 'Textbook',
  549:                                    placement  => 'Placement Tests',
  550:                                    lti        => 'LTI Provider',
  551:                                    norequest  => 'Not allowed',
  552:                                    approval   => 'Approval by Dom. Coord.',
  553:                                    validate   => 'With validation',
  554:                                    autolimit  => 'Numerical limit',
  555:                                    unlimited  => '(blank for unlimited)',
  556:                  );
  557:     return %titles;
  558: }
  559: 
  560: sub courserequest_display {
  561:     my %titles = &Apache::lonlocal::texthash (
  562:                                    approval   => 'Yes, need approval',
  563:                                    validate   => 'Yes, with validation',
  564:                                    norequest  => 'No',
  565:    );
  566:    return %titles;
  567: }
  568: 
  569: sub requestauthor_titles {
  570:     my %titles = &Apache::lonlocal::texthash (
  571:                                    norequest  => 'Not allowed',
  572:                                    approval   => 'Approval by Dom. Coord.',
  573:                                    automatic  => 'Automatic approval',
  574:                  );
  575:     return %titles;
  576: 
  577: }
  578: 
  579: sub requestauthor_display {
  580:     my %titles = &Apache::lonlocal::texthash (
  581:                                    approval   => 'Yes, need approval',
  582:                                    automatic  => 'Yes, automatic approval',
  583:                                    norequest  => 'No',
  584:    );
  585:    return %titles;
  586: }
  587: 
  588: sub requestchange_display {
  589:     my %titles = &Apache::lonlocal::texthash (
  590:                                    approval   => "availability set to 'on' (approval required)", 
  591:                                    automatic  => "availability set to 'on' (automatic approval)",
  592:                                    norequest  => "availability set to 'off'",
  593:    );
  594:    return %titles;
  595: }
  596: 
  597: sub curr_requestauthor {
  598:     my ($uname,$udom,$isadv,$inststatuses,$domconfig) = @_;
  599:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
  600:     if ($uname eq '' || $udom eq '') {
  601:         $uname = $env{'user.name'};
  602:         $udom = $env{'user.domain'};
  603:         $isadv = $env{'user.adv'};
  604:     }
  605:     my (%userenv,%settings,$val);
  606:     my @options = ('automatic','approval');
  607:     %userenv =
  608:         &Apache::lonnet::userenvironment($udom,$uname,'requestauthor','inststatus');
  609:     if ($userenv{'requestauthor'}) {
  610:         $val = $userenv{'requestauthor'};
  611:         @{$inststatuses} = ('_custom_');
  612:     } else {
  613:         my %alltasks;
  614:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') {
  615:             %settings = %{$domconfig->{'requestauthor'}};
  616:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
  617:                 $val = $settings{'_LC_adv'};
  618:                 @{$inststatuses} = ('_LC_adv_');
  619:             } else {
  620:                 if ($userenv{'inststatus'} ne '') {
  621:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
  622:                 } else {
  623:                     @{$inststatuses} = ('default');
  624:                 }
  625:                 foreach my $status (@{$inststatuses}) {
  626:                     if (exists($settings{$status})) {
  627:                         my $value = $settings{$status};
  628:                         next unless ($value);
  629:                         unless (exists($alltasks{$value})) {
  630:                             if (ref($alltasks{$value}) eq 'ARRAY') {
  631:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
  632:                                     push(@{$alltasks{$value}},$status);
  633:                                 }
  634:                             } else {
  635:                                 @{$alltasks{$value}} = ($status);
  636:                             }
  637:                         }
  638:                     }
  639:                 }
  640:                 foreach my $option (@options) {
  641:                     if ($alltasks{$option}) {
  642:                         $val = $option;
  643:                         last;
  644:                     }
  645:                 }
  646:             }
  647:         }
  648:     }
  649:     return $val;
  650: }
  651: 
  652: # =================================================================== Phase one
  653: 
  654: sub print_username_entry_form {
  655:     my ($r,$context,$response,$srch,$forcenewuser,$crstype,$brcrum,
  656:         $permission) = @_;
  657:     my $defdom=$env{'request.role.domain'};
  658:     my $formtoset = 'crtuser';
  659:     if (exists($env{'form.startrolename'})) {
  660:         $formtoset = 'docustom';
  661:         $env{'form.rolename'} = $env{'form.startrolename'};
  662:     } elsif ($env{'form.origform'} eq 'crtusername') {
  663:         $formtoset =  $env{'form.origform'};
  664:     }
  665: 
  666:     my ($jsback,$elements) = &crumb_utilities();
  667: 
  668:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
  669:         '<script type="text/javascript">'."\n".
  670:         '// <![CDATA['."\n".
  671:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset})."\n".
  672:         '// ]]>'."\n".
  673:         '</script>'."\n";
  674: 
  675:     my %existingroles=&Apache::lonuserutils::my_custom_roles($crstype);
  676:     if (($env{'form.action'} eq 'custom') && (keys(%existingroles) > 0)
  677:         && (&Apache::lonnet::allowed('mcr','/'))) {
  678:         $jscript .= &customrole_javascript();
  679:     }
  680:     my $helpitem = 'Course_Change_Privileges';
  681:     if ($env{'form.action'} eq 'custom') {
  682:         if ($context eq 'course') {
  683:             $helpitem = 'Course_Editing_Custom_Roles';
  684:         } elsif ($context eq 'domain') {
  685:             $helpitem = 'Domain_Editing_Custom_Roles';
  686:         }
  687:     } elsif ($env{'form.action'} eq 'singlestudent') {
  688:         $helpitem = 'Course_Add_Student';
  689:     } elsif ($env{'form.action'} eq 'accesslogs') {
  690:         $helpitem = 'Domain_User_Access_Logs';
  691:     } elsif ($context eq 'author') {
  692:         $helpitem = 'Author_Change_Privileges';
  693:     } elsif ($context eq 'domain') {
  694:         if ($permission->{'cusr'}) {
  695:             $helpitem = 'Domain_Change_Privileges';
  696:         } elsif ($permission->{'view'}) {
  697:             $helpitem = 'Domain_View_Privileges';
  698:         } else {
  699:             undef($helpitem);
  700:         }
  701:     }
  702:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$defdom);
  703:     if ($env{'form.action'} eq 'custom') {
  704:         push(@{$brcrum},
  705:                  {href=>"javascript:backPage(document.crtuser)",       
  706:                   text=>"Pick custom role",
  707:                   help => $helpitem,}
  708:                  );
  709:     } else {
  710:         push (@{$brcrum},
  711:                   {href => "javascript:backPage(document.crtuser)",
  712:                    text => $breadcrumb_text{'search'},
  713:                    help => $helpitem,
  714:                    faq  => 282,
  715:                    bug  => 'Instructor Interface',}
  716:                   );
  717:     }
  718:     my %loaditems = (
  719:                 'onload' => "javascript:setFormElements(document.$formtoset)",
  720:                     );
  721:     my $args = {bread_crumbs           => $brcrum,
  722:                 bread_crumbs_component => 'User Management',
  723:                 add_entries            => \%loaditems,};
  724:     $r->print(&Apache::loncommon::start_page('User Management',$jscript,$args));
  725: 
  726:     my %lt=&Apache::lonlocal::texthash(
  727:                     'srst' => 'Search for a user and enroll as a student',
  728:                     'srme' => 'Search for a user and enroll as a member',
  729:                     'srad' => 'Search for a user and modify/add user information or roles',
  730:                     'srvu' => 'Search for a user and view user information and roles',
  731:                     'srva' => 'Search for a user and view access log information',
  732: 		    'usr'  => "Username",
  733:                     'dom'  => "Domain",
  734:                     'ecrp' => "Define or Edit Custom Role",
  735:                     'nr'   => "role name",
  736:                     'cre'  => "Next",
  737: 				       );
  738: 
  739:     if ($env{'form.action'} eq 'custom') {
  740:         if (&Apache::lonnet::allowed('mcr','/')) {
  741:             my $newroletext = &mt('Define new custom role:');
  742:             $r->print('<form action="/adm/createuser" method="post" name="docustom">'.
  743:                       '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
  744:                       '<input type="hidden" name="phase" value="selected_custom_edit" />'.
  745:                       '<h3>'.$lt{'ecrp'}.'</h3>'.
  746:                       &Apache::loncommon::start_data_table().
  747:                       &Apache::loncommon::start_data_table_row().
  748:                       '<td>');
  749:             if (keys(%existingroles) > 0) {
  750:                 $r->print('<br /><label><input type="radio" name="customroleaction" value="new" checked="checked" onclick="setCustomFields();" /><b>'.$newroletext.'</b></label>');
  751:             } else {
  752:                 $r->print('<br /><input type="hidden" name="customroleaction" value="new" /><b>'.$newroletext.'</b>');
  753:             }
  754:             $r->print('</td><td align="center">'.$lt{'nr'}.'<br /><input type="text" size="15" name="newrolename" onfocus="setCustomAction('."'new'".');" /></td>'.
  755:                       &Apache::loncommon::end_data_table_row());
  756:             if (keys(%existingroles) > 0) {
  757:                 $r->print(&Apache::loncommon::start_data_table_row().'<td><br />'.
  758:                           '<label><input type="radio" name="customroleaction" value="edit" onclick="setCustomFields();"/><b>'.
  759:                           &mt('View/Modify existing role:').'</b></label></td>'.
  760:                           '<td align="center"><br />'.
  761:                           '<select name="rolename" onchange="setCustomAction('."'edit'".');">'.
  762:                           '<option value="" selected="selected">'.
  763:                           &mt('Select'));
  764:                 foreach my $role (sort(keys(%existingroles))) {
  765:                     $r->print('<option value="'.$role.'">'.$role.'</option>');
  766:                 }
  767:                 $r->print('</select>'.
  768:                           '</td>'.
  769:                           &Apache::loncommon::end_data_table_row());
  770:             }
  771:             $r->print(&Apache::loncommon::end_data_table().'<p>'.
  772:                       '<input name="customeditor" type="submit" value="'.
  773:                       $lt{'cre'}.'" /></p>'.
  774:                       '</form>');
  775:         }
  776:     } else {
  777:         my $actiontext = $lt{'srad'};
  778:         my $fixeddom;
  779:         if ($env{'form.action'} eq 'singlestudent') {
  780:             if ($crstype eq 'Community') {
  781:                 $actiontext = $lt{'srme'};
  782:             } else {
  783:                 $actiontext = $lt{'srst'};
  784:             }
  785:         } elsif ($env{'form.action'} eq 'accesslogs') {
  786:             $actiontext = $lt{'srva'};
  787:             $fixeddom = 1;
  788:         } elsif (($env{'form.action'} eq 'singleuser') &&
  789:                  ($context eq 'domain') && (!&Apache::lonnet::allowed('mau',$defdom))) {
  790:             $actiontext = $lt{'srvu'};
  791:             $fixeddom = 1;
  792:         }
  793:         $r->print("<h3>$actiontext</h3>");
  794:         if ($env{'form.origform'} ne 'crtusername') {
  795:             if ($response) {
  796:                $r->print("\n<div>$response</div>".
  797:                          '<br clear="all" />');
  798:             }
  799:         }
  800:         $r->print(&entry_form($defdom,$srch,$forcenewuser,$context,$response,$crstype,$fixeddom));
  801:     }
  802: }
  803: 
  804: sub customrole_javascript {
  805:     my $js = <<"END";
  806: <script type="text/javascript">
  807: // <![CDATA[
  808: 
  809: function setCustomFields() {
  810:     if (document.docustom.customroleaction.length > 0) {
  811:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
  812:             if (document.docustom.customroleaction[i].checked) {
  813:                 if (document.docustom.customroleaction[i].value == 'new') {
  814:                     document.docustom.rolename.selectedIndex = 0;
  815:                 } else {
  816:                     document.docustom.newrolename.value = '';
  817:                 }
  818:             }
  819:         }
  820:     }
  821:     return;
  822: }
  823: 
  824: function setCustomAction(caller) {
  825:     if (document.docustom.customroleaction.length > 0) {
  826:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
  827:             if (document.docustom.customroleaction[i].value == caller) {
  828:                 document.docustom.customroleaction[i].checked = true;
  829:             }
  830:         }
  831:     }
  832:     setCustomFields();
  833:     return;
  834: }
  835: 
  836: // ]]>
  837: </script>
  838: END
  839:     return $js;
  840: }
  841: 
  842: sub entry_form {
  843:     my ($dom,$srch,$forcenewuser,$context,$responsemsg,$crstype,$fixeddom) = @_;
  844:     my ($usertype,$inexact);
  845:     if (ref($srch) eq 'HASH') {
  846:         if (($srch->{'srchin'} eq 'dom') &&
  847:             ($srch->{'srchby'} eq 'uname') &&
  848:             ($srch->{'srchtype'} eq 'exact') &&
  849:             ($srch->{'srchdomain'} ne '') &&
  850:             ($srch->{'srchterm'} ne '')) {
  851:             my (%curr_rules,%got_rules);
  852:             my ($rules,$ruleorder) =
  853:                 &Apache::lonnet::inst_userrules($srch->{'srchdomain'},'username');
  854:             $usertype = &Apache::lonuserutils::check_usertype($srch->{'srchdomain'},$srch->{'srchterm'},$rules,\%curr_rules,\%got_rules);
  855:         } else {
  856:             $inexact = 1;
  857:         }
  858:     }
  859:     my ($cancreate,$noinstd);
  860:     if ($env{'form.action'} eq 'accesslogs') {
  861:         $noinstd = 1;
  862:     } else {
  863:         $cancreate =
  864:             &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
  865:     }
  866:     my ($userpicker,$cansearch) = 
  867:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
  868:                                        'document.crtuser',$cancreate,$usertype,$context,$fixeddom,$noinstd);
  869:     my $srchbutton = &mt('Search');
  870:     if ($env{'form.action'} eq 'singlestudent') {
  871:         $srchbutton = &mt('Search and Enroll');
  872:     } elsif ($env{'form.action'} eq 'accesslogs') {
  873:         $srchbutton = &mt('Search');
  874:     } elsif ($cancreate && $responsemsg ne '' && $inexact) {
  875:         $srchbutton = &mt('Search or Add New User');
  876:     }
  877:     my $output;
  878:     if ($cansearch) {
  879:         $output = <<"ENDBLOCK";
  880: <form action="/adm/createuser" method="post" name="crtuser">
  881: <input type="hidden" name="action" value="$env{'form.action'}" />
  882: <input type="hidden" name="phase" value="get_user_info" />
  883: $userpicker
  884: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
  885: </form>
  886: ENDBLOCK
  887:     } else {
  888:         $output = '<p>'.$userpicker.'</p>';
  889:     }
  890:     if (($env{'form.phase'} eq '') && ($env{'form.action'} ne 'accesslogs') &&
  891:         (!(($env{'form.action'} eq 'singleuser') && ($context eq 'domain') &&
  892:         (!&Apache::lonnet::allowed('mau',$env{'request.role.domain'}))))) {
  893:         my $defdom=$env{'request.role.domain'};
  894:         my ($trusted,$untrusted);
  895:         if ($context eq 'course') {
  896:             ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('enroll',$defdom);
  897:         } elsif ($context eq 'author') {
  898:             ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('othcoau',$defdom);
  899:         } elsif ($context eq 'domain') {
  900:             ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('domroles',$defdom); 
  901:         }
  902:         my $domform = &Apache::loncommon::select_dom_form($defdom,'srchdomain',undef,undef,undef,$trusted,$untrusted);
  903:         my %lt=&Apache::lonlocal::texthash(
  904:                   'enro' => 'Enroll one student',
  905:                   'enrm' => 'Enroll one member',
  906:                   'admo' => 'Add/modify a single user',
  907:                   'crea' => 'create new user if required',
  908:                   'uskn' => "username is known",
  909:                   'crnu' => 'Create a new user',
  910:                   'usr'  => 'Username',
  911:                   'dom'  => 'in domain',
  912:                   'enrl' => 'Enroll',
  913:                   'cram'  => 'Create/Modify user',
  914:         );
  915:         my $sellink=&Apache::loncommon::selectstudent_link('crtusername','srchterm','srchdomain');
  916:         my ($title,$buttontext,$showresponse);
  917:         if ($env{'form.action'} eq 'singlestudent') {
  918:             if ($crstype eq 'Community') {
  919:                 $title = $lt{'enrm'};
  920:             } else {
  921:                 $title = $lt{'enro'};
  922:             }
  923:             $buttontext = $lt{'enrl'};
  924:         } else {
  925:             $title = $lt{'admo'};
  926:             $buttontext = $lt{'cram'};
  927:         }
  928:         if ($cancreate) {
  929:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'crea'}.')</span>';
  930:         } else {
  931:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'uskn'}.')</span>';
  932:         }
  933:         if ($env{'form.origform'} eq 'crtusername') {
  934:             $showresponse = $responsemsg;
  935:         }
  936:         $output .= <<"ENDDOCUMENT";
  937: <br />
  938: <form action="/adm/createuser" method="post" name="crtusername">
  939: <input type="hidden" name="action" value="$env{'form.action'}" />
  940: <input type="hidden" name="phase" value="createnewuser" />
  941: <input type="hidden" name="srchtype" value="exact" />
  942: <input type="hidden" name="srchby" value="uname" />
  943: <input type="hidden" name="srchin" value="dom" />
  944: <input type="hidden" name="forcenewuser" value="1" />
  945: <input type="hidden" name="origform" value="crtusername" />
  946: <h3>$title</h3>
  947: $showresponse
  948: <table>
  949:  <tr>
  950:   <td>$lt{'usr'}:</td>
  951:   <td><input type="text" size="15" name="srchterm" /></td>
  952:   <td>&nbsp;$lt{'dom'}:</td><td>$domform</td>
  953:   <td>&nbsp;$sellink&nbsp;</td>
  954:   <td>&nbsp;<input name="userrole" type="submit" value="$buttontext" /></td>
  955:  </tr>
  956: </table>
  957: </form>
  958: ENDDOCUMENT
  959:     }
  960:     return $output;
  961: }
  962: 
  963: sub user_modification_js {
  964:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
  965:     
  966:     return <<END;
  967: <script type="text/javascript" language="Javascript">
  968: // <![CDATA[
  969: 
  970:     $pjump_def
  971:     $dc_setcourse_code
  972: 
  973:     function dateset() {
  974:         eval("document.cu."+document.cu.pres_marker.value+
  975:             ".value=document.cu.pres_value.value");
  976:         modalWindow.close();
  977:     }
  978: 
  979:     $nondc_setsection_code
  980: // ]]>
  981: </script>
  982: END
  983: }
  984: 
  985: # =================================================================== Phase two
  986: sub print_user_selection_page {
  987:     my ($r,$response,$srch,$srch_results,$srcharray,$context,$opener_elements,$crstype,$brcrum) = @_;
  988:     my @fields = ('username','domain','lastname','firstname','permanentemail');
  989:     my $sortby = $env{'form.sortby'};
  990: 
  991:     if (!grep(/^\Q$sortby\E$/,@fields)) {
  992:         $sortby = 'lastname';
  993:     }
  994: 
  995:     my ($jsback,$elements) = &crumb_utilities();
  996: 
  997:     my $jscript = (<<ENDSCRIPT);
  998: <script type="text/javascript">
  999: // <![CDATA[
 1000: function pickuser(uname,udom) {
 1001:     document.usersrchform.seluname.value=uname;
 1002:     document.usersrchform.seludom.value=udom;
 1003:     document.usersrchform.phase.value="userpicked";
 1004:     document.usersrchform.submit();
 1005: }
 1006: 
 1007: $jsback
 1008: // ]]>
 1009: </script>
 1010: ENDSCRIPT
 1011: 
 1012:     my %lt=&Apache::lonlocal::texthash(
 1013:                                        'usrch'          => "User Search to add/modify roles",
 1014:                                        'stusrch'        => "User Search to enroll student",
 1015:                                        'memsrch'        => "User Search to enroll member",
 1016:                                        'srcva'          => "Search for a user and view access log information",
 1017:                                        'usrvu'          => "User Search to view user roles",
 1018:                                        'usel'           => "Select a user to add/modify roles",
 1019:                                        'suvr'           => "Select a user to view roles",
 1020:                                        'stusel'         => "Select a user to enroll as a student",
 1021:                                        'memsel'         => "Select a user to enroll as a member",
 1022:                                        'vacsel'         => "Select a user to view access log",
 1023:                                        'username'       => "username",
 1024:                                        'domain'         => "domain",
 1025:                                        'lastname'       => "last name",
 1026:                                        'firstname'      => "first name",
 1027:                                        'permanentemail' => "permanent e-mail",
 1028:                                       );
 1029:     if ($context eq 'requestcrs') {
 1030:         $r->print('<div>');
 1031:     } else {
 1032:         my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$srch->{'srchdomain'});
 1033:         my $helpitem;
 1034:         if ($env{'form.action'} eq 'singleuser') {
 1035:             $helpitem = 'Course_Change_Privileges';
 1036:         } elsif ($env{'form.action'} eq 'singlestudent') {
 1037:             $helpitem = 'Course_Add_Student';
 1038:         } elsif ($context eq 'author') {
 1039:             $helpitem = 'Author_Change_Privileges';
 1040:         } elsif ($context eq 'domain') {
 1041:             $helpitem = 'Domain_Change_Privileges';
 1042:         }
 1043:         push (@{$brcrum},
 1044:                   {href => "javascript:backPage(document.usersrchform,'','')",
 1045:                    text => $breadcrumb_text{'search'},
 1046:                    faq  => 282,
 1047:                    bug  => 'Instructor Interface',},
 1048:                   {href => "javascript:backPage(document.usersrchform,'get_user_info','select')",
 1049:                    text => $breadcrumb_text{'userpicked'},
 1050:                    faq  => 282,
 1051:                    bug  => 'Instructor Interface',
 1052:                    help => $helpitem}
 1053:                   );
 1054:         $r->print(&Apache::loncommon::start_page('User Management',$jscript,{bread_crumbs => $brcrum}));
 1055:         if ($env{'form.action'} eq 'singleuser') {
 1056:             my $readonly;
 1057:             if (($context eq 'domain') && (!&Apache::lonnet::allowed('mau',$srch->{'srchdomain'}))) {
 1058:                 $readonly = 1;
 1059:                 $r->print("<b>$lt{'usrvu'}</b><br />");
 1060:             } else {
 1061:                 $r->print("<b>$lt{'usrch'}</b><br />");
 1062:             }
 1063:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
 1064:             if ($readonly) {
 1065:                 $r->print('<h3>'.$lt{'suvr'}.'</h3>');
 1066:             } else {
 1067:                 $r->print('<h3>'.$lt{'usel'}.'</h3>');
 1068:             }
 1069:         } elsif ($env{'form.action'} eq 'singlestudent') {
 1070:             $r->print($jscript."<b>");
 1071:             if ($crstype eq 'Community') {
 1072:                 $r->print($lt{'memsrch'});
 1073:             } else {
 1074:                 $r->print($lt{'stusrch'});
 1075:             }
 1076:             $r->print("</b><br />");
 1077:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
 1078:             $r->print('</form><h3>');
 1079:             if ($crstype eq 'Community') {
 1080:                 $r->print($lt{'memsel'});
 1081:             } else {
 1082:                 $r->print($lt{'stusel'});
 1083:             }
 1084:             $r->print('</h3>');
 1085:         } elsif ($env{'form.action'} eq 'accesslogs') {
 1086:             $r->print("<b>$lt{'srcva'}</b><br />");
 1087:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,undef,1));
 1088:             $r->print('<h3>'.$lt{'vacsel'}.'</h3>');
 1089:         }
 1090:     }
 1091:     $r->print('<form name="usersrchform" method="post" action="">'.
 1092:               &Apache::loncommon::start_data_table()."\n".
 1093:               &Apache::loncommon::start_data_table_header_row()."\n".
 1094:               ' <th> </th>'."\n");
 1095:     foreach my $field (@fields) {
 1096:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
 1097:                   "'".$field."'".';document.usersrchform.submit();">'.
 1098:                   $lt{$field}.'</a></th>'."\n");
 1099:     }
 1100:     $r->print(&Apache::loncommon::end_data_table_header_row());
 1101: 
 1102:     my @sorted_users = sort {
 1103:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
 1104:             ||
 1105:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
 1106:             ||
 1107:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
 1108: 	    ||
 1109: 	lc($a) cmp lc($b)
 1110:         } (keys(%$srch_results));
 1111: 
 1112:     foreach my $user (@sorted_users) {
 1113:         my ($uname,$udom) = split(/:/,$user);
 1114:         my $onclick;
 1115:         if ($context eq 'requestcrs') {
 1116:             $onclick =
 1117:                 'onclick="javascript:gochoose('."'$uname','$udom',".
 1118:                                                "'$srch_results->{$user}->{firstname}',".
 1119:                                                "'$srch_results->{$user}->{lastname}',".
 1120:                                                "'$srch_results->{$user}->{permanentemail}'".');"';
 1121:         } else {
 1122:             $onclick =
 1123:                 ' onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".');"';
 1124:         }
 1125:         $r->print(&Apache::loncommon::start_data_table_row().
 1126:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" '.
 1127:                   $onclick.' /></td>'.
 1128:                   '<td><tt>'.$uname.'</tt></td>'.
 1129:                   '<td><tt>'.$udom.'</tt></td>');
 1130:         foreach my $field ('lastname','firstname','permanentemail') {
 1131:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
 1132:         }
 1133:         $r->print(&Apache::loncommon::end_data_table_row());
 1134:     }
 1135:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
 1136:     if (ref($srcharray) eq 'ARRAY') {
 1137:         foreach my $item (@{$srcharray}) {
 1138:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
 1139:         }
 1140:     }
 1141:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
 1142:               ' <input type="hidden" name="seluname" value="" />'."\n".
 1143:               ' <input type="hidden" name="seludom" value="" />'."\n".
 1144:               ' <input type="hidden" name="currstate" value="select" />'."\n".
 1145:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
 1146:               ' <input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
 1147:     if ($context eq 'requestcrs') {
 1148:         $r->print($opener_elements.'</form></div>');
 1149:     } else {
 1150:         $r->print($response.'</form>');
 1151:     }
 1152: }
 1153: 
 1154: sub print_user_query_page {
 1155:     my ($r,$caller,$brcrum) = @_;
 1156: # FIXME - this is for a network-wide name search (similar to catalog search)
 1157: # To use frames with similar behavior to catalog/portfolio search.
 1158: # To be implemented. 
 1159:     return;
 1160: }
 1161: 
 1162: sub print_user_modification_page {
 1163:     my ($r,$ccuname,$ccdomain,$srch,$response,$context,$permission,$crstype,
 1164:         $brcrum,$showcredits) = @_;
 1165:     if (($ccuname eq '') || ($ccdomain eq '')) {
 1166:         my $usermsg = &mt('No username and/or domain provided.');
 1167:         $env{'form.phase'} = '';
 1168: 	&print_username_entry_form($r,$context,$usermsg,'','',$crstype,$brcrum,
 1169:                                    $permission);
 1170:         return;
 1171:     }
 1172:     my ($form,$formname);
 1173:     if ($env{'form.action'} eq 'singlestudent') {
 1174:         $form = 'document.enrollstudent';
 1175:         $formname = 'enrollstudent';
 1176:     } else {
 1177:         $form = 'document.cu';
 1178:         $formname = 'cu';
 1179:     }
 1180:     my %abv_auth = &auth_abbrev();
 1181:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
 1182:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
 1183:     if ($uhome eq 'no_host') {
 1184:         my $usertype;
 1185:         my ($rules,$ruleorder) =
 1186:             &Apache::lonnet::inst_userrules($ccdomain,'username');
 1187:             $usertype =
 1188:                 &Apache::lonuserutils::check_usertype($ccdomain,$ccuname,$rules,
 1189:                                                       \%curr_rules,\%got_rules);
 1190:         my $cancreate =
 1191:             &Apache::lonuserutils::can_create_user($ccdomain,$context,
 1192:                                                    $usertype);
 1193:         if (!$cancreate) {
 1194:             my $helplink = 'javascript:helpMenu('."'display'".')';
 1195:             my %usertypetext = (
 1196:                 official   => 'institutional',
 1197:                 unofficial => 'non-institutional',
 1198:             );
 1199:             my $response;
 1200:             if ($env{'form.origform'} eq 'crtusername') {
 1201:                 $response = '<span class="LC_warning">'.
 1202:                             &mt('No match found for the username [_1] in LON-CAPA domain: [_2]',
 1203:                                 '<b>'.$ccuname.'</b>',$ccdomain).
 1204:                             '</span><br />';
 1205:             }
 1206:             $response .= '<p class="LC_warning">'
 1207:                         .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
 1208:                         .' ';
 1209:             if ($context eq 'domain') {
 1210:                 $response .= &mt('Please contact a [_1] for assistance.',
 1211:                                  &Apache::lonnet::plaintext('dc'));
 1212:             } else {
 1213:                 $response .= &mt('Please contact the [_1]helpdesk[_2] for assistance.'
 1214:                                 ,'<a href="'.$helplink.'">','</a>');
 1215:             }
 1216:             $response .= '</p><br />';
 1217:             $env{'form.phase'} = '';
 1218:             &print_username_entry_form($r,$context,$response,undef,undef,$crstype,$brcrum,
 1219:                                        $permission);
 1220:             return;
 1221:         }
 1222:         $newuser = 1;
 1223:         my $checkhash;
 1224:         my $checks = { 'username' => 1 };
 1225:         $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
 1226:         &Apache::loncommon::user_rule_check($checkhash,$checks,
 1227:             \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
 1228:         if (ref($alerts{'username'}) eq 'HASH') {
 1229:             if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
 1230:                 my $domdesc =
 1231:                     &Apache::lonnet::domain($ccdomain,'description');
 1232:                 if ($alerts{'username'}{$ccdomain}{$ccuname}) {
 1233:                     my $userchkmsg;
 1234:                     if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
 1235:                         $userchkmsg = 
 1236:                             &Apache::loncommon::instrule_disallow_msg('username',
 1237:                                                                  $domdesc,1).
 1238:                         &Apache::loncommon::user_rule_formats($ccdomain,
 1239:                             $domdesc,$curr_rules{$ccdomain}{'username'},
 1240:                             'username');
 1241:                     }
 1242:                     $env{'form.phase'} = '';
 1243:                     &print_username_entry_form($r,$context,$userchkmsg,undef,undef,$crstype,$brcrum,
 1244:                                                $permission);
 1245:                     return;
 1246:                 }
 1247:             }
 1248:         }
 1249:     } else {
 1250:         $newuser = 0;
 1251:     }
 1252:     if ($response) {
 1253:         $response = '<br />'.$response;
 1254:     }
 1255: 
 1256:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
 1257:     my $dc_setcourse_code = '';
 1258:     my $nondc_setsection_code = '';                                        
 1259:     my %loaditem;
 1260: 
 1261:     my $groupslist = &Apache::lonuserutils::get_groupslist();
 1262: 
 1263:     my $js = &validation_javascript($context,$ccdomain,$pjump_def,$crstype,
 1264:                                $groupslist,$newuser,$formname,\%loaditem);
 1265:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$ccdomain);
 1266:     my $helpitem = 'Course_Change_Privileges';
 1267:     if ($env{'form.action'} eq 'singlestudent') {
 1268:         $helpitem = 'Course_Add_Student';
 1269:     } elsif ($context eq 'author') {
 1270:         $helpitem = 'Author_Change_Privileges';
 1271:     } elsif ($context eq 'domain') {
 1272:         $helpitem = 'Domain_Change_Privileges';
 1273:     }
 1274:     push (@{$brcrum},
 1275:         {href => "javascript:backPage($form)",
 1276:          text => $breadcrumb_text{'search'},
 1277:          faq  => 282,
 1278:          bug  => 'Instructor Interface',});
 1279:     if ($env{'form.phase'} eq 'userpicked') {
 1280:        push(@{$brcrum},
 1281:               {href => "javascript:backPage($form,'get_user_info','select')",
 1282:                text => $breadcrumb_text{'userpicked'},
 1283:                faq  => 282,
 1284:                bug  => 'Instructor Interface',});
 1285:     }
 1286:     push(@{$brcrum},
 1287:             {href => "javascript:backPage($form,'$env{'form.phase'}','modify')",
 1288:              text => $breadcrumb_text{'modify'},
 1289:              faq  => 282,
 1290:              bug  => 'Instructor Interface',
 1291:              help => $helpitem});
 1292:     my $args = {'add_entries'           => \%loaditem,
 1293:                 'bread_crumbs'          => $brcrum,
 1294:                 'bread_crumbs_component' => 'User Management'};
 1295:     if ($env{'form.popup'}) {
 1296:         $args->{'no_nav_bar'} = 1;
 1297:     }
 1298:     my $start_page =
 1299:         &Apache::loncommon::start_page('User Management',$js,$args);
 1300: 
 1301:     my $forminfo =<<"ENDFORMINFO";
 1302: <form action="/adm/createuser" method="post" name="$formname">
 1303: <input type="hidden" name="phase" value="update_user_data" />
 1304: <input type="hidden" name="ccuname" value="$ccuname" />
 1305: <input type="hidden" name="ccdomain" value="$ccdomain" />
 1306: <input type="hidden" name="pres_value"  value="" />
 1307: <input type="hidden" name="pres_type"   value="" />
 1308: <input type="hidden" name="pres_marker" value="" />
 1309: ENDFORMINFO
 1310:     my (%inccourses,$roledom,$defaultcredits);
 1311:     if ($context eq 'course') {
 1312:         $inccourses{$env{'request.course.id'}}=1;
 1313:         $roledom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1314:         if ($showcredits) {
 1315:             $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
 1316:         }
 1317:     } elsif ($context eq 'author') {
 1318:         $roledom = $env{'request.role.domain'};
 1319:     } elsif ($context eq 'domain') {
 1320:         foreach my $key (keys(%env)) {
 1321:             $roledom = $env{'request.role.domain'};
 1322:             if ($key=~/^user\.priv\.cm\.\/($roledom)\/($match_username)/) {
 1323:                 $inccourses{$1.'_'.$2}=1;
 1324:             }
 1325:         }
 1326:     } else {
 1327:         foreach my $key (keys(%env)) {
 1328: 	    if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
 1329: 	        $inccourses{$1.'_'.$2}=1;
 1330:             }
 1331:         }
 1332:     }
 1333:     my $title = '';
 1334:     if ($newuser) {
 1335:         my ($portfolioform,$domroleform);
 1336:         if ((&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) ||
 1337:             (&Apache::lonnet::allowed('mut',$env{'request.role.domain'}))) {
 1338:             # Current user has quota or user tools modification privileges
 1339:             $portfolioform = '<br />'.&user_quotas($ccuname,$ccdomain);
 1340:         }
 1341:         if ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) &&
 1342:             ($ccdomain eq $env{'request.role.domain'})) {
 1343:             $domroleform = '<br />'.&domainrole_req($ccuname,$ccdomain);
 1344:         }
 1345:         &initialize_authen_forms($ccdomain,$formname);
 1346:         my %lt=&Apache::lonlocal::texthash(
 1347:                 'lg'             => 'Login Data',
 1348:                 'hs'             => "Home Server",
 1349:         );
 1350: 	$r->print(<<ENDTITLE);
 1351: $start_page
 1352: $response
 1353: $forminfo
 1354: <script type="text/javascript" language="Javascript">
 1355: // <![CDATA[
 1356: $loginscript
 1357: // ]]>
 1358: </script>
 1359: <input type='hidden' name='makeuser' value='1' />
 1360: ENDTITLE
 1361:         if ($env{'form.action'} eq 'singlestudent') {
 1362:             if ($crstype eq 'Community') {
 1363:                 $title = &mt('Create New User [_1] in domain [_2] as a member',
 1364:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
 1365:             } else {
 1366:                 $title = &mt('Create New User [_1] in domain [_2] as a student',
 1367:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
 1368:             }
 1369:         } else {
 1370:                 $title = &mt('Create New User [_1] in domain [_2]',
 1371:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
 1372:         }
 1373:         $r->print('<h2>'.$title.'</h2>'."\n");
 1374:         $r->print('<div class="LC_left_float">');
 1375:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
 1376:                                          $inst_results{$ccuname.':'.$ccdomain}));
 1377:         # Option to disable student/employee ID conflict checking not offerred for new users.
 1378:         my ($home_server_pick,$numlib) = 
 1379:             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
 1380:                                                       'default','hide');
 1381:         if ($numlib > 1) {
 1382:             $r->print("
 1383: <br />
 1384: $lt{'hs'}: $home_server_pick
 1385: <br />");
 1386:         } else {
 1387:             $r->print($home_server_pick);
 1388:         }
 1389:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
 1390:             $r->print('<br /><h3>'.
 1391:                       &mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
 1392:                       &Apache::loncommon::start_data_table().
 1393:                       &build_tools_display($ccuname,$ccdomain,
 1394:                                            'requestcourses').
 1395:                       &Apache::loncommon::end_data_table());
 1396:         }
 1397:         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
 1398:                   $lt{'lg'}.'</h3>');
 1399:         my ($fixedauth,$varauth,$authmsg); 
 1400:         if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
 1401:             my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
 1402:             my ($rules,$ruleorder) = 
 1403:                 &Apache::lonnet::inst_userrules($ccdomain,'username');
 1404:             if (ref($rules) eq 'HASH') {
 1405:                 if (ref($rules->{$matchedrule}) eq 'HASH') {
 1406:                     my $authtype = $rules->{$matchedrule}{'authtype'};
 1407:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
 1408:                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
 1409:                     } else { 
 1410:                         my $authparm = $rules->{$matchedrule}{'authparm'};
 1411:                         $authmsg = $rules->{$matchedrule}{'authmsg'};
 1412:                         if ($authtype =~ /^krb(4|5)$/) {
 1413:                             my $ver = $1;
 1414:                             if ($authparm ne '') {
 1415:                                 $fixedauth = <<"KERB"; 
 1416: <input type="hidden" name="login" value="krb" />
 1417: <input type="hidden" name="krbver" value="$ver" />
 1418: <input type="hidden" name="krbarg" value="$authparm" />
 1419: KERB
 1420:                             }
 1421:                         } else {
 1422:                             $fixedauth = 
 1423: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
 1424:                             if ($rules->{$matchedrule}{'authparmfixed'}) {
 1425:                                 $fixedauth .=    
 1426: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
 1427:                             } else {
 1428:                                 if ($authtype eq 'int') {
 1429:                                     $varauth = '<br />'.
 1430: &mt('[_1] Internally authenticated (with initial password [_2])','','<input type="password" size="10" name="intarg" value="" />')."<label><input type=\"checkbox\" name=\"visible\" onclick='if (this.checked) { this.form.intarg.type=\"text\" } else { this.form.intarg.type=\"password\" }' />".&mt('Visible input').'</label>';
 1431:                                 } elsif ($authtype eq 'loc') {
 1432:                                     $varauth = '<br />'.
 1433: &mt('[_1] Local Authentication with argument [_2]','','<input type="text" name="'.$authtype.'arg" value="" />')."\n";
 1434:                                 } else {
 1435:                                     $varauth =
 1436: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
 1437:                                 }
 1438:                             }
 1439:                         }
 1440:                     }
 1441:                 } else {
 1442:                     $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
 1443:                 }
 1444:             }
 1445:             if ($authmsg) {
 1446:                 $r->print(<<ENDAUTH);
 1447: $fixedauth
 1448: $authmsg
 1449: $varauth
 1450: ENDAUTH
 1451:             }
 1452:         } else {
 1453:             $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc)); 
 1454:         }
 1455:         $r->print($portfolioform.$domroleform);
 1456:         if ($env{'form.action'} eq 'singlestudent') {
 1457:             $r->print(&date_sections_select($context,$newuser,$formname,
 1458:                                             $permission,$crstype,$ccuname,
 1459:                                             $ccdomain,$showcredits));
 1460:         }
 1461:         $r->print('</div><div class="LC_clear_float_footer"></div>');
 1462:     } else { # user already exists
 1463: 	$r->print($start_page.$forminfo);
 1464:         if ($env{'form.action'} eq 'singlestudent') {
 1465:             if ($crstype eq 'Community') {
 1466:                 $title = &mt('Enroll one member: [_1] in domain [_2]',
 1467:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
 1468:             } else {
 1469:                 $title = &mt('Enroll one student: [_1] in domain [_2]',
 1470:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
 1471:             }
 1472:         } else {
 1473:             if ($permission->{'cusr'}) {
 1474:                 $title = &mt('Modify existing user: [_1] in domain [_2]',
 1475:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
 1476:             } else {
 1477:                 $title = &mt('Existing user: [_1] in domain [_2]',
 1478:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
 1479:             }
 1480:         }
 1481:         $r->print('<h2>'.$title.'</h2>'."\n");
 1482:         $r->print('<div class="LC_left_float">');
 1483:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
 1484:                                          $inst_results{$ccuname.':'.$ccdomain}));
 1485:         if ((&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) ||
 1486:             (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) {
 1487:             $r->print('<br /><h3>'.&mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'."\n");
 1488:             if (($env{'request.role.domain'} eq $ccdomain) ||
 1489:                 (&Apache::lonnet::will_trust('reqcrs',$ccdomain,$env{'request.role.domain'}))) {
 1490:                 $r->print(&Apache::loncommon::start_data_table());
 1491:                 if ($env{'request.role.domain'} eq $ccdomain) {
 1492:                     $r->print(&build_tools_display($ccuname,$ccdomain,'requestcourses'));
 1493:                 } else {
 1494:                     $r->print(&coursereq_externaluser($ccuname,$ccdomain,
 1495:                                                       $env{'request.role.domain'}));
 1496:                 }
 1497:                 $r->print(&Apache::loncommon::end_data_table());
 1498:             } else {
 1499:                 $r->print(&mt('Domain configuration for this domain prohibits course creation by users from domain: "[_1]"',
 1500:                               &Apache::lonnet::domain($ccdomain,'description')));
 1501:             }
 1502:         }
 1503:         $r->print('</div>');
 1504:         my @order = ('auth','quota','tools','requestauthor');
 1505:         my %user_text;
 1506:         my ($isadv,$isauthor) = 
 1507:             &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
 1508:         if ((!$isauthor) && 
 1509:             ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) ||
 1510:              (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) &&
 1511:              ($env{'request.role.domain'} eq $ccdomain)) {
 1512:             $user_text{'requestauthor'} = &domainrole_req($ccuname,$ccdomain);
 1513:         }
 1514:         $user_text{'auth'} =  &user_authentication($ccuname,$ccdomain,$formname,$crstype,$permission);
 1515:         if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
 1516:             (&Apache::lonnet::allowed('mut',$ccdomain)) ||
 1517:             (&Apache::lonnet::allowed('udp',$ccdomain))) {
 1518:             # Current user has quota modification privileges
 1519:             $user_text{'quota'} = &user_quotas($ccuname,$ccdomain);
 1520:         }
 1521:         if (!&Apache::lonnet::allowed('mpq',$ccdomain)) {
 1522:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
 1523:                 my %lt=&Apache::lonlocal::texthash(
 1524:                     'dska'  => "Disk quotas for user's portfolio and Authoring Space",
 1525:                     'youd'  => "You do not have privileges to modify the portfolio and/or Authoring Space quotas for this user.",
 1526:                     'ichr'  => "If a change is required, contact a domain coordinator for the domain",
 1527:                 );
 1528:                 $user_text{'quota'} = <<ENDNOPORTPRIV;
 1529: <h3>$lt{'dska'}</h3>
 1530: $lt{'youd'} $lt{'ichr'}: $ccdomain
 1531: ENDNOPORTPRIV
 1532:             }
 1533:         }
 1534:         if (!&Apache::lonnet::allowed('mut',$ccdomain)) {
 1535:             if (&Apache::lonnet::allowed('mut',$env{'request.role.domain'})) {
 1536:                 my %lt=&Apache::lonlocal::texthash(
 1537:                     'utav'  => "User Tools Availability",
 1538:                     'yodo'  => "You do not have privileges to modify Portfolio, Blog, WebDAV, or Personal Information Page settings for this user.",
 1539:                     'ifch'  => "If a change is required, contact a domain coordinator for the domain",
 1540:                 );
 1541:                 $user_text{'tools'} = <<ENDNOTOOLSPRIV;
 1542: <h3>$lt{'utav'}</h3>
 1543: $lt{'yodo'} $lt{'ifch'}: $ccdomain
 1544: ENDNOTOOLSPRIV
 1545:             }
 1546:         }
 1547:         my $gotdiv = 0; 
 1548:         foreach my $item (@order) {
 1549:             if ($user_text{$item} ne '') {
 1550:                 unless ($gotdiv) {
 1551:                     $r->print('<div class="LC_left_float">');
 1552:                     $gotdiv = 1;
 1553:                 }
 1554:                 $r->print('<br />'.$user_text{$item});
 1555:             }
 1556:         }
 1557:         if ($env{'form.action'} eq 'singlestudent') {
 1558:             unless ($gotdiv) {
 1559:                 $r->print('<div class="LC_left_float">');
 1560:             }
 1561:             my $credits;
 1562:             if ($showcredits) {
 1563:                 $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
 1564:                 if ($credits eq '') {
 1565:                     $credits = $defaultcredits;
 1566:                 }
 1567:             }
 1568:             $r->print(&date_sections_select($context,$newuser,$formname,
 1569:                                             $permission,$crstype,$ccuname,
 1570:                                             $ccdomain,$showcredits));
 1571:         }
 1572:         if ($gotdiv) {
 1573:             $r->print('</div><div class="LC_clear_float_footer"></div>');
 1574:         }
 1575:         my $statuses;
 1576:         if (($context eq 'domain') && (&Apache::lonnet::allowed('udp',$ccdomain)) &&
 1577:             (!&Apache::lonnet::allowed('mau',$ccdomain))) {
 1578:             $statuses = ['active'];
 1579:         } elsif (($context eq 'course') && ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
 1580:                  ($env{'request.course.sec'} &&
 1581:                   &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'})))) {
 1582:             $statuses = ['active'];
 1583:         }
 1584:         if ($env{'form.action'} ne 'singlestudent') {
 1585:             &display_existing_roles($r,$ccuname,$ccdomain,\%inccourses,$context,
 1586:                                     $roledom,$crstype,$showcredits,$statuses);
 1587:         }
 1588:     } ## End of new user/old user logic
 1589:     if ($env{'form.action'} eq 'singlestudent') {
 1590:         my $btntxt;
 1591:         if ($crstype eq 'Community') {
 1592:             $btntxt = &mt('Enroll Member');
 1593:         } else {
 1594:             $btntxt = &mt('Enroll Student');
 1595:         }
 1596:         $r->print('<br /><input type="button" value="'.$btntxt.'" onclick="setSections(this.form)" />'."\n");
 1597:     } elsif ($permission->{'cusr'}) {
 1598:         $r->print('<div class="LC_left_float">'.
 1599:                   '<fieldset><legend>'.&mt('Add Roles').'</legend>');
 1600:         my $addrolesdisplay = 0;
 1601:         if ($context eq 'domain' || $context eq 'author') {
 1602:             $addrolesdisplay = &new_coauthor_roles($r,$ccuname,$ccdomain);
 1603:         }
 1604:         if ($context eq 'domain') {
 1605:             my $add_domainroles = &new_domain_roles($r,$ccdomain);
 1606:             if (!$addrolesdisplay) {
 1607:                 $addrolesdisplay = $add_domainroles;
 1608:             }
 1609:             $r->print(&course_level_dc($env{'request.role.domain'},$showcredits));
 1610:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
 1611:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setCourse()" />'."\n");
 1612:         } elsif ($context eq 'author') {
 1613:             if ($addrolesdisplay) {
 1614:                 $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
 1615:                           '<br /><input type="button" value="'.&mt('Save').'"');
 1616:                 if ($newuser) {
 1617:                     $r->print(' onclick="auth_check()" \>'."\n");
 1618:                 } else {
 1619:                     $r->print('onclick="this.form.submit()" \>'."\n");
 1620:                 }
 1621:             } else {
 1622:                 $r->print('</fieldset></div>'.
 1623:                           '<div class="LC_clear_float_footer"></div>'.
 1624:                           '<br /><a href="javascript:backPage(document.cu)">'.
 1625:                           &mt('Back to previous page').'</a>');
 1626:             }
 1627:         } else {
 1628:             $r->print(&course_level_table(\%inccourses,$showcredits,$defaultcredits));
 1629:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
 1630:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setSections(this.form)" />'."\n");
 1631:         }
 1632:     }
 1633:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
 1634:     $r->print('<input type="hidden" name="currstate" value="" />');
 1635:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" /></form><br /><br />');
 1636:     return;
 1637: }
 1638: 
 1639: sub singleuser_breadcrumb {
 1640:     my ($crstype,$context,$domain) = @_;
 1641:     my %breadcrumb_text;
 1642:     if ($env{'form.action'} eq 'singlestudent') {
 1643:         if ($crstype eq 'Community') {
 1644:             $breadcrumb_text{'search'} = 'Enroll a member';
 1645:         } else {
 1646:             $breadcrumb_text{'search'} = 'Enroll a student';
 1647:         }
 1648:         $breadcrumb_text{'userpicked'} = 'Select a user';
 1649:         $breadcrumb_text{'modify'} = 'Set section/dates';
 1650:     } elsif ($env{'form.action'} eq 'accesslogs') {
 1651:         $breadcrumb_text{'search'} = 'View access logs for a user';
 1652:         $breadcrumb_text{'userpicked'} = 'Select a user';
 1653:         $breadcrumb_text{'activity'} = 'Activity';
 1654:     } elsif (($env{'form.action'} eq 'singleuser') && ($context eq 'domain') &&
 1655:              (!&Apache::lonnet::allowed('mau',$domain))) {
 1656:         $breadcrumb_text{'search'} = "View user's roles";
 1657:         $breadcrumb_text{'userpicked'} = 'Select a user';
 1658:         $breadcrumb_text{'modify'} = 'User roles';
 1659:     } else {
 1660:         $breadcrumb_text{'search'} = 'Create/modify a user';
 1661:         $breadcrumb_text{'userpicked'} = 'Select a user';
 1662:         $breadcrumb_text{'modify'} = 'Set user role';
 1663:     }
 1664:     return %breadcrumb_text;
 1665: }
 1666: 
 1667: sub date_sections_select {
 1668:     my ($context,$newuser,$formname,$permission,$crstype,$ccuname,$ccdomain,
 1669:         $showcredits) = @_;
 1670:     my $credits;
 1671:     if ($showcredits) {
 1672:         my $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
 1673:         $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
 1674:         if ($credits eq '') {
 1675:             $credits = $defaultcredits;
 1676:         }
 1677:     }
 1678:     my $cid = $env{'request.course.id'};
 1679:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity($cid);
 1680:     my $date_table = '<h3>'.&mt('Starting and Ending Dates').'</h3>'."\n".
 1681:         &Apache::lonuserutils::date_setting_table(undef,undef,$context,
 1682:                                                   undef,$formname,$permission);
 1683:     my $rowtitle = 'Section';
 1684:     my $secbox = '<h3>'.&mt('Section and Credits').'</h3>'."\n".
 1685:         &Apache::lonuserutils::section_picker($cdom,$cnum,'st',$rowtitle,
 1686:                                               $permission,$context,'',$crstype,
 1687:                                               $showcredits,$credits);
 1688:     my $output = $date_table.$secbox;
 1689:     return $output;
 1690: }
 1691: 
 1692: sub validation_javascript {
 1693:     my ($context,$ccdomain,$pjump_def,$crstype,$groupslist,$newuser,$formname,
 1694:         $loaditem) = @_;
 1695:     my $dc_setcourse_code = '';
 1696:     my $nondc_setsection_code = '';
 1697:     if ($context eq 'domain') {
 1698:         my $dcdom = $env{'request.role.domain'};
 1699:         $loaditem->{'onload'} = "document.cu.coursedesc.value='';";
 1700:         $dc_setcourse_code = 
 1701:             &Apache::lonuserutils::dc_setcourse_js('cu','singleuser',$context);
 1702:     } else {
 1703:         my $checkauth; 
 1704:         if (($newuser) || (&Apache::lonnet::allowed('mau',$ccdomain))) {
 1705:             $checkauth = 1;
 1706:         }
 1707:         if ($context eq 'course') {
 1708:             $nondc_setsection_code =
 1709:                 &Apache::lonuserutils::setsections_javascript($formname,$groupslist,
 1710:                                                               undef,$checkauth,
 1711:                                                               $crstype);
 1712:         }
 1713:         if ($checkauth) {
 1714:             $nondc_setsection_code .= 
 1715:                 &Apache::lonuserutils::verify_authen($formname,$context);
 1716:         }
 1717:     }
 1718:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
 1719:                                    $nondc_setsection_code,$groupslist);
 1720:     my ($jsback,$elements) = &crumb_utilities();
 1721:     $js .= "\n".
 1722:            '<script type="text/javascript">'."\n".
 1723:            '// <![CDATA['."\n".
 1724:            $jsback."\n".
 1725:            '// ]]>'."\n".
 1726:            '</script>'."\n";
 1727:     return $js;
 1728: }
 1729: 
 1730: sub display_existing_roles {
 1731:     my ($r,$ccuname,$ccdomain,$inccourses,$context,$roledom,$crstype,
 1732:         $showcredits,$statuses) = @_;
 1733:     my $now=time;
 1734:     my $showall = 1;
 1735:     my ($showexpired,$showactive);
 1736:     if ((ref($statuses) eq 'ARRAY') && (@{$statuses} > 0)) {
 1737:         $showall = 0;
 1738:         if (grep(/^expired$/,@{$statuses})) {
 1739:             $showexpired = 1;
 1740:         }
 1741:         if (grep(/^active$/,@{$statuses})) {
 1742:             $showactive = 1;
 1743:         }
 1744:         if ($showexpired && $showactive) {
 1745:             $showall = 1;
 1746:         }
 1747:     }
 1748:     my %lt=&Apache::lonlocal::texthash(
 1749:                     'rer'  => "Existing Roles",
 1750:                     'rev'  => "Revoke",
 1751:                     'del'  => "Delete",
 1752:                     'ren'  => "Re-Enable",
 1753:                     'rol'  => "Role",
 1754:                     'ext'  => "Extent",
 1755:                     'crd'  => "Credits",
 1756:                     'sta'  => "Start",
 1757:                     'end'  => "End",
 1758:                                        );
 1759:     my (%rolesdump,%roletext,%sortrole,%roleclass,%rolepriv);
 1760:     if ($context eq 'course' || $context eq 'author') {
 1761:         my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
 1762:         my %roleshash = 
 1763:             &Apache::lonnet::get_my_roles($ccuname,$ccdomain,'userroles',
 1764:                               ['active','previous','future'],\@roles,$roledom,1);
 1765:         foreach my $key (keys(%roleshash)) {
 1766:             my ($start,$end) = split(':',$roleshash{$key});
 1767:             next if ($start eq '-1' || $end eq '-1');
 1768:             my ($rnum,$rdom,$role,$sec) = split(':',$key);
 1769:             if ($context eq 'course') {
 1770:                 next unless (($rnum eq $env{'course.'.$env{'request.course.id'}.'.num'})
 1771:                              && ($rdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}));
 1772:             } elsif ($context eq 'author') {
 1773:                 next unless (($rnum eq $env{'user.name'}) && ($rdom eq $env{'request.role.domain'}));
 1774:             }
 1775:             my ($newkey,$newvalue,$newrole);
 1776:             $newkey = '/'.$rdom.'/'.$rnum;
 1777:             if ($sec ne '') {
 1778:                 $newkey .= '/'.$sec;
 1779:             }
 1780:             $newvalue = $role;
 1781:             if ($role =~ /^cr/) {
 1782:                 $newrole = 'cr';
 1783:             } else {
 1784:                 $newrole = $role;
 1785:             }
 1786:             $newkey .= '_'.$newrole;
 1787:             if ($start ne '' && $end ne '') {
 1788:                 $newvalue .= '_'.$end.'_'.$start;
 1789:             } elsif ($end ne '') {
 1790:                 $newvalue .= '_'.$end;
 1791:             }
 1792:             $rolesdump{$newkey} = $newvalue;
 1793:         }
 1794:     } else {
 1795:         %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
 1796:     }
 1797:     # Build up table of user roles to allow revocation and re-enabling of roles.
 1798:     my ($tmp) = keys(%rolesdump);
 1799:     return if ($tmp =~ /^(con_lost|error)/i);
 1800:     foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
 1801:                                 my $b1=join('_',(split('_',$b))[1,0]);
 1802:                                 return $a1 cmp $b1;
 1803:                             } keys(%rolesdump)) {
 1804:         next if ($area =~ /^rolesdef/);
 1805:         my $envkey=$area;
 1806:         my $role = $rolesdump{$area};
 1807:         my $thisrole=$area;
 1808:         $area =~ s/\_\w\w$//;
 1809:         my ($role_code,$role_end_time,$role_start_time) =
 1810:             split(/_/,$role);
 1811:         my $active=1;
 1812:         $active=0 if (($role_end_time) && ($now>$role_end_time));
 1813:         if ($active) {
 1814:             next unless($showall || $showactive);
 1815:         } else {
 1816:             next unless($showall || $showexpired);
 1817:         }
 1818: # Is this a custom role? Get role owner and title.
 1819:         my ($croleudom,$croleuname,$croletitle)=
 1820:             ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
 1821:         my $allowed=0;
 1822:         my $delallowed=0;
 1823:         my $sortkey=$role_code;
 1824:         my $class='Unknown';
 1825:         my $credits='';
 1826:         my $csec;
 1827:         if ($area =~ m{^/($match_domain)/($match_courseid)}) {
 1828:             $class='Course';
 1829:             my ($coursedom,$coursedir) = ($1,$2);
 1830:             my $cid = $1.'_'.$2;
 1831:             # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
 1832:             next if ($envkey =~ m{^/$match_domain/$match_courseid/[A-Za-z0-9]+_gr$});
 1833:             my %coursedata=
 1834:                 &Apache::lonnet::coursedescription($cid);
 1835:             if ($coursedir =~ /^$match_community$/) {
 1836:                 $class='Community';
 1837:             }
 1838:             $sortkey.="\0$coursedom";
 1839:             my $carea;
 1840:             if (defined($coursedata{'description'})) {
 1841:                 $carea=$coursedata{'description'}.
 1842:                     '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
 1843:     &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$coursedir,$coursedom);
 1844:                 $sortkey.="\0".$coursedata{'description'};
 1845:             } else {
 1846:                 if ($class eq 'Community') {
 1847:                     $carea=&mt('Unavailable community').': '.$area;
 1848:                     $sortkey.="\0".&mt('Unavailable community').': '.$area;
 1849:                 } else {
 1850:                     $carea=&mt('Unavailable course').': '.$area;
 1851:                     $sortkey.="\0".&mt('Unavailable course').': '.$area;
 1852:                 }
 1853:             }
 1854:             $sortkey.="\0$coursedir";
 1855:             $inccourses->{$cid}=1;
 1856:             if (($showcredits) && ($class eq 'Course') && ($role_code eq 'st')) {
 1857:                 my $defaultcredits = $coursedata{'internal.defaultcredits'};
 1858:                 $credits =
 1859:                     &get_user_credits($ccuname,$ccdomain,$defaultcredits,
 1860:                                       $coursedom,$coursedir);
 1861:                 if ($credits eq '') {
 1862:                     $credits = $defaultcredits;
 1863:                 }
 1864:             }
 1865:             if ((&Apache::lonnet::allowed('c'.$role_code,$coursedom.'/'.$coursedir)) ||
 1866:                 (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
 1867:                 $allowed=1;
 1868:             }
 1869:             unless ($allowed) {
 1870:                 my $isowner = &Apache::lonuserutils::is_courseowner($cid,$coursedata{'internal.courseowner'});
 1871:                 if ($isowner) {
 1872:                     if (($role_code eq 'co') && ($class eq 'Community')) {
 1873:                         $allowed = 1;
 1874:                     } elsif (($role_code eq 'cc') && ($class eq 'Course')) {
 1875:                         $allowed = 1;
 1876:                     }
 1877:                 }
 1878:             } 
 1879:             if ((&Apache::lonnet::allowed('dro',$coursedom)) ||
 1880:                 (&Apache::lonnet::allowed('dro',$ccdomain))) {
 1881:                 $delallowed=1;
 1882:             }
 1883: # - custom role. Needs more info, too
 1884:             if ($croletitle) {
 1885:                 if (&Apache::lonnet::allowed('ccr',$coursedom.'/'.$coursedir)) {
 1886:                     $allowed=1;
 1887:                     $thisrole.='.'.$role_code;
 1888:                 }
 1889:             }
 1890:             if ($area=~m{^/($match_domain/$match_courseid/(\w+))}) {
 1891:                 $csec = $2;
 1892:                 $carea.='<br />'.&mt('Section: [_1]',$csec);
 1893:                 $sortkey.="\0$csec";
 1894:                 if (!$allowed) {
 1895:                     if ($env{'request.course.sec'} eq $csec) {
 1896:                         if (&Apache::lonnet::allowed('c'.$role_code,$1)) {
 1897:                             $allowed = 1;
 1898:                         }
 1899:                     }
 1900:                 }
 1901:             }
 1902:             $area=$carea;
 1903:         } else {
 1904:             $sortkey.="\0".$area;
 1905:             # Determine if current user is able to revoke privileges
 1906:             if ($area=~m{^/($match_domain)/}) {
 1907:                 if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
 1908:                    (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
 1909:                    $allowed=1;
 1910:                 }
 1911:                 if (((&Apache::lonnet::allowed('dro',$1))  ||
 1912:                     (&Apache::lonnet::allowed('dro',$ccdomain))) &&
 1913:                     ($role_code ne 'dc')) {
 1914:                     $delallowed=1;
 1915:                 }
 1916:             } else {
 1917:                 if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
 1918:                     $allowed=1;
 1919:                 }
 1920:             }
 1921:             if ($role_code eq 'ca' || $role_code eq 'au' || $role_code eq 'aa') {
 1922:                 $class='Authoring Space';
 1923:             } elsif ($role_code eq 'su') {
 1924:                 $class='System';
 1925:             } else {
 1926:                 $class='Domain';
 1927:             }
 1928:         }
 1929:         if (($role_code eq 'ca') || ($role_code eq 'aa')) {
 1930:             $area=~m{/($match_domain)/($match_username)};
 1931:             if (&Apache::lonuserutils::authorpriv($2,$1)) {
 1932:                 $allowed=1;
 1933:             } else {
 1934:                 $allowed=0;
 1935:             }
 1936:         }
 1937:         my $row = '';
 1938:         if ($showall) {
 1939:             $row.= '<td>';
 1940:             if (($active) && ($allowed)) {
 1941:                 $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
 1942:             } else {
 1943:                 if ($active) {
 1944:                     $row.='&nbsp;';
 1945:                 } else {
 1946:                     $row.=&mt('expired or revoked');
 1947:                 }
 1948:             }
 1949:             $row.='</td><td>';
 1950:             if ($allowed && !$active) {
 1951:                 $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
 1952:             } else {
 1953:                 $row.='&nbsp;';
 1954:             }
 1955:             $row.='</td><td>';
 1956:             if ($delallowed) {
 1957:                 $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
 1958:             } else {
 1959:                 $row.='&nbsp;';
 1960:             }
 1961:             $row.= '</td>';
 1962:         }
 1963:         my $plaintext='';
 1964:         if (!$croletitle) {
 1965:             $plaintext=&Apache::lonnet::plaintext($role_code,$class);
 1966:             if (($showcredits) && ($credits ne '')) {
 1967:                 $plaintext .= '<br/ ><span class="LC_nobreak">'.
 1968:                               '<span class="LC_fontsize_small">'.
 1969:                               &mt('Credits: [_1]',$credits).
 1970:                               '</span></span>';
 1971:             }
 1972:         } else {
 1973:             $plaintext=
 1974:                 &mt('Custom role [_1][_2]defined by [_3]',
 1975:                         '"'.$croletitle.'"',
 1976:                         '<br />',
 1977:                         $croleuname.':'.$croleudom);
 1978:         }
 1979:         $row.= '<td>'.$plaintext.'</td>'.
 1980:                '<td>'.$area.'</td>'.
 1981:                '<td>'.($role_start_time?&Apache::lonlocal::locallocaltime($role_start_time)
 1982:                                             : '&nbsp;' ).'</td>'.
 1983:                '<td>'.($role_end_time  ?&Apache::lonlocal::locallocaltime($role_end_time)
 1984:                                             : '&nbsp;' ).'</td>';
 1985:         $sortrole{$sortkey}=$envkey;
 1986:         $roletext{$envkey}=$row;
 1987:         $roleclass{$envkey}=$class;
 1988:         if ($allowed) {
 1989:             $rolepriv{$envkey}='edit';
 1990:         } else {
 1991:             if ($context eq 'domain') {
 1992:                 if ((&Apache::lonnet::allowed('vur',$ccdomain)) &&
 1993:                     ($envkey=~m{^/$ccdomain/})) {
 1994:                     $rolepriv{$envkey}='view';
 1995:                 }
 1996:             } elsif ($context eq 'course') {
 1997:                 if ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
 1998:                     ($env{'request.course.sec'} && ($env{'request.course.sec'} eq $csec) &&
 1999:                      &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'}))) {
 2000:                     $rolepriv{$envkey}='view';
 2001:                 }
 2002:             }
 2003:         }
 2004:     } # end of foreach        (table building loop)
 2005: 
 2006:     my $rolesdisplay = 0;
 2007:     my %output = ();
 2008:     foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
 2009:         $output{$type} = '';
 2010:         foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
 2011:             if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) {
 2012:                  $output{$type}.=
 2013:                       &Apache::loncommon::start_data_table_row().
 2014:                       $roletext{$sortrole{$which}}.
 2015:                       &Apache::loncommon::end_data_table_row();
 2016:             }
 2017:         }
 2018:         unless($output{$type} eq '') {
 2019:             $output{$type} = '<tr class="LC_info_row">'.
 2020:                       "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
 2021:                       $output{$type};
 2022:             $rolesdisplay = 1;
 2023:         }
 2024:     }
 2025:     if ($rolesdisplay == 1) {
 2026:         my $contextrole='';
 2027:         if ($env{'request.course.id'}) {
 2028:             if (&Apache::loncommon::course_type() eq 'Community') {
 2029:                 $contextrole = &mt('Existing Roles in this Community');
 2030:             } else {
 2031:                 $contextrole = &mt('Existing Roles in this Course');
 2032:             }
 2033:         } elsif ($env{'request.role'} =~ /^au\./) {
 2034:             $contextrole = &mt('Existing Co-Author Roles in your Authoring Space');
 2035:         } else {
 2036:             if ($showall) {
 2037:                 $contextrole = &mt('Existing Roles in this Domain');
 2038:             } elsif ($showactive) {
 2039:                 $contextrole = &mt('Unexpired Roles in this Domain');
 2040:             } elsif ($showexpired) {
 2041:                 $contextrole = &mt('Expired or Revoked Roles in this Domain');
 2042:             }
 2043:         }
 2044:         $r->print('<div class="LC_left_float">'.
 2045: '<fieldset><legend>'.$contextrole.'</legend>'.
 2046: &Apache::loncommon::start_data_table("LC_createuser").
 2047: &Apache::loncommon::start_data_table_header_row());
 2048:         if ($showall) {
 2049:             $r->print(
 2050: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}.'</th>'
 2051:             );
 2052:         } elsif ($showexpired) {
 2053:             $r->print('<th>'.$lt{'rev'}.'</th>');
 2054:         }
 2055:         $r->print(
 2056: '<th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.'</th>'.
 2057: '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
 2058: &Apache::loncommon::end_data_table_header_row());
 2059:         foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
 2060:             if ($output{$type}) {
 2061:                 $r->print($output{$type}."\n");
 2062:             }
 2063:         }
 2064:         $r->print(&Apache::loncommon::end_data_table().
 2065:                   '</fieldset></div>');
 2066:     }
 2067:     return;
 2068: }
 2069: 
 2070: sub new_coauthor_roles {
 2071:     my ($r,$ccuname,$ccdomain) = @_;
 2072:     my $addrolesdisplay = 0;
 2073:     #
 2074:     # Co-Author
 2075:     #
 2076:     if (&Apache::lonuserutils::authorpriv($env{'user.name'},
 2077:                                           $env{'request.role.domain'}) &&
 2078:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
 2079:         # No sense in assigning co-author role to yourself
 2080:         $addrolesdisplay = 1;
 2081:         my $cuname=$env{'user.name'};
 2082:         my $cudom=$env{'request.role.domain'};
 2083:         my %lt=&Apache::lonlocal::texthash(
 2084:                     'cs'   => "Authoring Space",
 2085:                     'act'  => "Activate",
 2086:                     'rol'  => "Role",
 2087:                     'ext'  => "Extent",
 2088:                     'sta'  => "Start",
 2089:                     'end'  => "End",
 2090:                     'cau'  => "Co-Author",
 2091:                     'caa'  => "Assistant Co-Author",
 2092:                     'ssd'  => "Set Start Date",
 2093:                     'sed'  => "Set End Date"
 2094:                                        );
 2095:         $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n".
 2096:                   &Apache::loncommon::start_data_table()."\n".
 2097:                   &Apache::loncommon::start_data_table_header_row()."\n".
 2098:                   '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
 2099:                   '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
 2100:                   '<th>'.$lt{'end'}.'</th>'."\n".
 2101:                   &Apache::loncommon::end_data_table_header_row()."\n".
 2102:                   &Apache::loncommon::start_data_table_row().'
 2103:            <td>
 2104:             <input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_ca" />
 2105:            </td>
 2106:            <td>'.$lt{'cau'}.'</td>
 2107:            <td>'.$cudom.'_'.$cuname.'</td>
 2108:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
 2109:              <a href=
 2110: "javascript:pjump('."'date_start','Start Date Co-Author',document.cu.start_$cudom\_$cuname\_ca.value,'start_$cudom\_$cuname\_ca','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
 2111: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
 2112: <a href=
 2113: "javascript:pjump('."'date_end','End Date Co-Author',document.cu.end_$cudom\_$cuname\_ca.value,'end_$cudom\_$cuname\_ca','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'."\n".
 2114:               &Apache::loncommon::end_data_table_row()."\n".
 2115:               &Apache::loncommon::start_data_table_row()."\n".
 2116: '<td><input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
 2117: <td>'.$lt{'caa'}.'</td>
 2118: <td>'.$cudom.'_'.$cuname.'</td>
 2119: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
 2120: <a href=
 2121: "javascript:pjump('."'date_start','Start Date Assistant Co-Author',document.cu.start_$cudom\_$cuname\_aa.value,'start_$cudom\_$cuname\_aa','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
 2122: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
 2123: <a href=
 2124: "javascript:pjump('."'date_end','End Date Assistant Co-Author',document.cu.end_$cudom\_$cuname\_aa.value,'end_$cudom\_$cuname\_aa','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'."\n".
 2125:              &Apache::loncommon::end_data_table_row()."\n".
 2126:              &Apache::loncommon::end_data_table());
 2127:     } elsif ($env{'request.role'} =~ /^au\./) {
 2128:         if (!(&Apache::lonuserutils::authorpriv($env{'user.name'},
 2129:                                                 $env{'request.role.domain'}))) {
 2130:             $r->print('<span class="LC_error">'.
 2131:                       &mt('You do not have privileges to assign co-author roles.').
 2132:                       '</span>');
 2133:         } elsif (($env{'user.name'} eq $ccuname) &&
 2134:              ($env{'user.domain'} eq $ccdomain)) {
 2135:             $r->print(&mt('Assigning yourself a co-author or assistant co-author role in your own author area in Authoring Space is not permitted'));
 2136:         }
 2137:     }
 2138:     return $addrolesdisplay;;
 2139: }
 2140: 
 2141: sub new_domain_roles {
 2142:     my ($r,$ccdomain) = @_;
 2143:     my $addrolesdisplay = 0;
 2144:     #
 2145:     # Domain level
 2146:     #
 2147:     my $num_domain_level = 0;
 2148:     my $domaintext =
 2149:     '<h4>'.&mt('Domain Level').'</h4>'.
 2150:     &Apache::loncommon::start_data_table().
 2151:     &Apache::loncommon::start_data_table_header_row().
 2152:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
 2153:     &mt('Extent').'</th>'.
 2154:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
 2155:     &Apache::loncommon::end_data_table_header_row();
 2156:     my @allroles = &Apache::lonuserutils::roles_by_context('domain');
 2157:     my $uprimary = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2158:     my $uintdom = &Apache::lonnet::internet_dom($uprimary);
 2159:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
 2160:         foreach my $role (@allroles) {
 2161:             next if ($role eq 'ad');
 2162:             next if (($role eq 'au') && ($ccdomain ne $thisdomain));
 2163:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
 2164:                if ($role eq 'dc') {
 2165:                    unless ($thisdomain eq $env{'request.role.domain'}) {
 2166:                        my $domprim = &Apache::lonnet::domain($thisdomain,'primary');
 2167:                        my $intdom = &Apache::lonnet::internet_dom($domprim);
 2168:                        next unless ($uintdom eq $intdom);
 2169:                    }
 2170:                }
 2171:                my $plrole=&Apache::lonnet::plaintext($role);
 2172:                my %lt=&Apache::lonlocal::texthash(
 2173:                     'ssd'  => "Set Start Date",
 2174:                     'sed'  => "Set End Date"
 2175:                                        );
 2176:                $num_domain_level ++;
 2177:                $domaintext .=
 2178: &Apache::loncommon::start_data_table_row().
 2179: '<td><input type="checkbox" name="act_'.$thisdomain.'_'.$role.'" /></td>
 2180: <td>'.$plrole.'</td>
 2181: <td>'.$thisdomain.'</td>
 2182: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
 2183: <a href=
 2184: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
 2185: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
 2186: <a href=
 2187: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
 2188: &Apache::loncommon::end_data_table_row();
 2189:             }
 2190:         }
 2191:     }
 2192:     $domaintext.= &Apache::loncommon::end_data_table();
 2193:     if ($num_domain_level > 0) {
 2194:         $r->print($domaintext);
 2195:         $addrolesdisplay = 1;
 2196:     }
 2197:     return $addrolesdisplay;
 2198: }
 2199: 
 2200: sub user_authentication {
 2201:     my ($ccuname,$ccdomain,$formname,$crstype,$permission) = @_;
 2202:     my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
 2203:     my $outcome;
 2204:     my %lt=&Apache::lonlocal::texthash(
 2205:                    'err'   => "ERROR",
 2206:                    'uuas'  => "This user has an unrecognized authentication scheme",
 2207:                    'adcs'  => "Please alert a domain coordinator of this situation",
 2208:                    'sldb'  => "Please specify login data below",
 2209:                    'ld'    => "Login Data"
 2210:     );
 2211:     # Check for a bad authentication type
 2212:     if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth|lti):/) {
 2213:         # bad authentication scheme
 2214:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
 2215:             &initialize_authen_forms($ccdomain,$formname);
 2216: 
 2217:             my $choices = &Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc);
 2218:             $outcome = <<ENDBADAUTH;
 2219: <script type="text/javascript" language="Javascript">
 2220: // <![CDATA[
 2221: $loginscript
 2222: // ]]>
 2223: </script>
 2224: <span class="LC_error">$lt{'err'}:
 2225: $lt{'uuas'} ($currentauth). $lt{'sldb'}.</span>
 2226: <h3>$lt{'ld'}</h3>
 2227: $choices
 2228: ENDBADAUTH
 2229:         } else {
 2230:             # This user is not allowed to modify the user's
 2231:             # authentication scheme, so just notify them of the problem
 2232:             $outcome = <<ENDBADAUTH;
 2233: <span class="LC_error"> $lt{'err'}: 
 2234: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
 2235: </span>
 2236: ENDBADAUTH
 2237:         }
 2238:     } else { # Authentication type is valid
 2239:         
 2240:         &initialize_authen_forms($ccdomain,$formname,$currentauth,'modifyuser');
 2241:         my ($authformcurrent,$can_modify,@authform_others) =
 2242:             &modify_login_block($ccdomain,$currentauth);
 2243:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
 2244:             # Current user has login modification privileges
 2245:             $outcome =
 2246:                        '<script type="text/javascript" language="Javascript">'."\n".
 2247:                        '// <![CDATA['."\n".
 2248:                        $loginscript."\n".
 2249:                        '// ]]>'."\n".
 2250:                        '</script>'."\n".
 2251:                        '<h3>'.$lt{'ld'}.'</h3>'.
 2252:                        &Apache::loncommon::start_data_table().
 2253:                        &Apache::loncommon::start_data_table_row().
 2254:                        '<td>'.$authformnop;
 2255:             if (($can_modify) && (&Apache::lonnet::allowed('mau',$ccdomain))) {
 2256:                 $outcome .= '</td>'."\n".
 2257:                             &Apache::loncommon::end_data_table_row().
 2258:                             &Apache::loncommon::start_data_table_row().
 2259:                             '<td>'.$authformcurrent.'</td>'.
 2260:                             &Apache::loncommon::end_data_table_row()."\n";
 2261:             } else {
 2262:                 $outcome .= '&nbsp;('.$authformcurrent.')</td>'.
 2263:                             &Apache::loncommon::end_data_table_row()."\n";
 2264:             }
 2265:             if (&Apache::lonnet::allowed('mau',$ccdomain)) {
 2266:                 foreach my $item (@authform_others) { 
 2267:                     $outcome .= &Apache::loncommon::start_data_table_row().
 2268:                                 '<td>'.$item.'</td>'.
 2269:                                 &Apache::loncommon::end_data_table_row()."\n";
 2270:                 }
 2271:             }
 2272:             $outcome .= &Apache::loncommon::end_data_table();
 2273:         } else {
 2274:             if (($currentauth =~ /^internal:/) &&
 2275:                 (&Apache::lonuserutils::can_change_internalpass($ccuname,$ccdomain,$crstype,$permission))) {
 2276:                 $outcome = <<"ENDJS";
 2277: <script type="text/javascript">
 2278: // <![CDATA[
 2279: function togglePwd(form) {
 2280:     if (form.newintpwd.length) {
 2281:         if (document.getElementById('LC_ownersetpwd')) {
 2282:             for (var i=0; i<form.newintpwd.length; i++) {
 2283:                 if (form.newintpwd[i].checked) {
 2284:                     if (form.newintpwd[i].value == 1) {
 2285:                         document.getElementById('LC_ownersetpwd').style.display = 'inline-block';
 2286:                     } else {
 2287:                         document.getElementById('LC_ownersetpwd').style.display = 'none';
 2288:                     }
 2289:                 }
 2290:             }
 2291:         }
 2292:     }
 2293: }
 2294: // ]]>
 2295: </script>
 2296: ENDJS
 2297: 
 2298:                 $outcome .= '<h3>'.$lt{'ld'}.'</h3>'.
 2299:                             &Apache::loncommon::start_data_table().
 2300:                             &Apache::loncommon::start_data_table_row().
 2301:                             '<td>'.&mt('Internally authenticated').'<br />'.&mt("Change user's password?").
 2302:                             '<label><input type="radio" name="newintpwd" value="0" checked="checked" onclick="togglePwd(this.form);" />'.
 2303:                             &mt('No').'</label>'.('&nbsp;'x2).
 2304:                             '<label><input type="radio" name="newintpwd" value="1" onclick="togglePwd(this.form);" />'.&mt('Yes').'</label>'.
 2305:                             '<div id="LC_ownersetpwd" style="display:none">'.
 2306:                             '&nbsp;&nbsp;'.&mt('Password').' <input type="password" size="15" name="intarg" value="" />'.
 2307:                             '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.intarg.type='."'text'".' } else { this.form.intarg.type='."'password'".' }" />'.&mt('Visible input').'</label></div></td>'.
 2308:                             &Apache::loncommon::end_data_table_row().
 2309:                             &Apache::loncommon::end_data_table();
 2310:             }
 2311:             if (&Apache::lonnet::allowed('udp',$ccdomain)) {
 2312:                 # Current user has rights to view domain preferences for user's domain
 2313:                 my $result;
 2314:                 if ($currentauth =~ /^krb(4|5):([^:]*)$/) {
 2315:                     my ($krbver,$krbrealm) = ($1,$2);
 2316:                     if ($krbrealm eq '') {
 2317:                         $result = &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
 2318:                     } else {
 2319:                         $result = &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
 2320:                                       $krbrealm,$krbver);
 2321:                     }
 2322:                 } elsif ($currentauth =~ /^internal:/) {
 2323:                     $result = &mt('Currently internally authenticated.');
 2324:                 } elsif ($currentauth =~ /^localauth:/) {
 2325:                     $result = &mt('Currently using local (institutional) authentication.');
 2326:                 } elsif ($currentauth =~ /^unix:/) {
 2327:                     $result = &mt('Currently Filesystem Authenticated.');
 2328:                 } elsif ($currentauth =~ /^lti:/) {
 2329:                     $result = &mt('Currently LTI authenticated.');
 2330:                 }
 2331:                 $outcome = '<h3>'.$lt{'ld'}.'</h3>'.
 2332:                            &Apache::loncommon::start_data_table().
 2333:                            &Apache::loncommon::start_data_table_row().
 2334:                            '<td>'.$result.'</td>'.
 2335:                            &Apache::loncommon::end_data_table_row()."\n".
 2336:                            &Apache::loncommon::end_data_table();
 2337:             } elsif (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
 2338:                 my %lt=&Apache::lonlocal::texthash(
 2339:                            'ccld'  => "Change Current Login Data",
 2340:                            'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
 2341:                            'ifch'  => "If a change is required, contact a domain coordinator for the domain",
 2342:                 );
 2343:                 $outcome .= <<ENDNOPRIV;
 2344: <h3>$lt{'ccld'}</h3>
 2345: $lt{'yodo'} $lt{'ifch'}: $ccdomain
 2346: <input type="hidden" name="login" value="nochange" />
 2347: ENDNOPRIV
 2348:             }
 2349:         }
 2350:     }  ## End of "check for bad authentication type" logic
 2351:     return $outcome;
 2352: }
 2353: 
 2354: sub modify_login_block {
 2355:     my ($dom,$currentauth) = @_;
 2356:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
 2357:     my ($authnum,%can_assign) =
 2358:         &Apache::loncommon::get_assignable_auth($dom);
 2359:     my ($authformcurrent,@authform_others,$show_override_msg);
 2360:     if ($currentauth=~/^krb(4|5):/) {
 2361:         $authformcurrent=$authformkrb;
 2362:         if ($can_assign{'int'}) {
 2363:             push(@authform_others,$authformint);
 2364:         }
 2365:         if ($can_assign{'loc'}) {
 2366:             push(@authform_others,$authformloc);
 2367:         }
 2368:         if ($can_assign{'lti'}) {
 2369:             push(@authform_others,$authformlti);
 2370:         }
 2371:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
 2372:             $show_override_msg = 1;
 2373:         }
 2374:     } elsif ($currentauth=~/^internal:/) {
 2375:         $authformcurrent=$authformint;
 2376:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
 2377:             push(@authform_others,$authformkrb);
 2378:         }
 2379:         if ($can_assign{'loc'}) {
 2380:             push(@authform_others,$authformloc);
 2381:         }
 2382:         if ($can_assign{'lti'}) {
 2383:             push(@authform_others,$authformlti);
 2384:         }
 2385:         if ($can_assign{'int'}) {
 2386:             $show_override_msg = 1;
 2387:         }
 2388:     } elsif ($currentauth=~/^unix:/) {
 2389:         $authformcurrent=$authformfsys;
 2390:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
 2391:             push(@authform_others,$authformkrb);
 2392:         }
 2393:         if ($can_assign{'int'}) {
 2394:             push(@authform_others,$authformint);
 2395:         }
 2396:         if ($can_assign{'loc'}) {
 2397:             push(@authform_others,$authformloc);
 2398:         }
 2399:         if ($can_assign{'lti'}) {
 2400:             push(@authform_others,$authformlti);
 2401:         }
 2402:         if ($can_assign{'fsys'}) {
 2403:             $show_override_msg = 1;
 2404:         }
 2405:     } elsif ($currentauth=~/^localauth:/) {
 2406:         $authformcurrent=$authformloc;
 2407:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
 2408:             push(@authform_others,$authformkrb);
 2409:         }
 2410:         if ($can_assign{'int'}) {
 2411:             push(@authform_others,$authformint);
 2412:         }
 2413:         if ($can_assign{'lti'}) {
 2414:             push(@authform_others,$authformlti);
 2415:         }
 2416:         if ($can_assign{'loc'}) {
 2417:             $show_override_msg = 1;
 2418:         }
 2419:     } elsif ($currentauth=~/^lti:/) {
 2420:         $authformcurrent=$authformlti;
 2421:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
 2422:             push(@authform_others,$authformkrb);
 2423:         }
 2424:         if ($can_assign{'int'}) {
 2425:             push(@authform_others,$authformint);
 2426:         }
 2427:         if ($can_assign{'loc'}) {
 2428:             push(@authform_others,$authformloc);
 2429:         }
 2430:     }
 2431:     if ($show_override_msg) {
 2432:         $authformcurrent = '<table><tr><td colspan="3">'.$authformcurrent.
 2433:                            '</td></tr>'."\n".
 2434:                            '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
 2435:                            '<td><b>'.&mt('Currently in use').'</b></td>'.
 2436:                            '<td align="right"><span class="LC_cusr_emph">'.
 2437:                             &mt('will override current values').
 2438:                             '</span></td></tr></table>';
 2439:     }
 2440:     return ($authformcurrent,$show_override_msg,@authform_others); 
 2441: }
 2442: 
 2443: sub personal_data_display {
 2444:     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$rolesarray,
 2445:         $now,$captchaform,$emailusername,$usertype,$usernameset,$condition,$excluded) = @_;
 2446:     my ($output,%userenv,%canmodify,%canmodify_status);
 2447:     my @userinfo = ('firstname','middlename','lastname','generation',
 2448:                     'permanentemail','id');
 2449:     my $rowcount = 0;
 2450:     my $editable = 0;
 2451:     my %textboxsize = (
 2452:                        firstname      => '15',
 2453:                        middlename     => '15',
 2454:                        lastname       => '15',
 2455:                        generation     => '5',
 2456:                        permanentemail => '25',
 2457:                        id             => '15',
 2458:                       );
 2459: 
 2460:     my %lt=&Apache::lonlocal::texthash(
 2461:                 'pd'             => "Personal Data",
 2462:                 'firstname'      => "First Name",
 2463:                 'middlename'     => "Middle Name",
 2464:                 'lastname'       => "Last Name",
 2465:                 'generation'     => "Generation",
 2466:                 'permanentemail' => "Permanent e-mail address",
 2467:                 'id'             => "Student/Employee ID",
 2468:                 'lg'             => "Login Data",
 2469:                 'inststatus'     => "Affiliation",
 2470:                 'email'          => 'E-mail address',
 2471:                 'valid'          => 'Validation',
 2472:                 'username'       => 'Username',
 2473:     );
 2474: 
 2475:     %canmodify_status =
 2476:         &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
 2477:                                                    ['inststatus'],$rolesarray);
 2478:     if (!$newuser) {
 2479:         # Get the users information
 2480:         %userenv = &Apache::lonnet::get('environment',
 2481:                    ['firstname','middlename','lastname','generation',
 2482:                     'permanentemail','id','inststatus'],$ccdomain,$ccuname);
 2483:         %canmodify =
 2484:             &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
 2485:                                                        \@userinfo,$rolesarray);
 2486:     } elsif ($context eq 'selfcreate') {
 2487:         if ($newuser eq 'email') {
 2488:             if (ref($emailusername) eq 'HASH') {
 2489:                 if (ref($emailusername->{$usertype}) eq 'HASH') {
 2490:                     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 2491:                     @userinfo = ();
 2492:                     if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
 2493:                         foreach my $field (@{$infofields}) { 
 2494:                             if ($emailusername->{$usertype}->{$field}) {
 2495:                                 push(@userinfo,$field);
 2496:                                 $canmodify{$field} = 1;
 2497:                                 unless ($textboxsize{$field}) {
 2498:                                     $textboxsize{$field} = 25;
 2499:                                 }
 2500:                                 unless ($lt{$field}) {
 2501:                                     $lt{$field} = $infotitles->{$field};
 2502:                                 }
 2503:                                 if ($emailusername->{$usertype}->{$field} eq 'required') {
 2504:                                     $lt{$field} .= '<b>*</b>';
 2505:                                 }
 2506:                             }
 2507:                         }
 2508:                     }
 2509:                 }
 2510:             }
 2511:         } else {
 2512:             %canmodify = &selfcreate_canmodify($context,$ccdomain,\@userinfo,
 2513:                                                $inst_results,$rolesarray);
 2514:         }
 2515:     }
 2516: 
 2517:     my $genhelp=&Apache::loncommon::help_open_topic('Generation');
 2518:     $output = '<h3>'.$lt{'pd'}.'</h3>'.
 2519:               &Apache::lonhtmlcommon::start_pick_box();
 2520:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
 2521:         my $size = 25;
 2522:         if ($condition) {
 2523:             if ($condition =~ /^\@[^\@]+$/) {
 2524:                 $size = 10;
 2525:             } else {
 2526:                 undef($condition);
 2527:             }
 2528:         } 
 2529:         if ($excluded) {
 2530:             unless ($excluded =~ /^\@[^\@]+$/) {
 2531:                 undef($condition);
 2532:             }
 2533:         }
 2534:         $output .= &Apache::lonhtmlcommon::row_title($lt{'email'}.'<b>*</b>',undef,
 2535:                                                      'LC_oddrow_value')."\n".
 2536:                    '<input type="text" name="uname" size="'.$size.'" value="" autocomplete="off" />';
 2537:         if ($condition) {
 2538:             $output .= $condition;
 2539:         } elsif ($excluded) {
 2540:             $output .= '<br /><span style="font-size: smaller">'.&mt('You must use an e-mail address that does not end with [_1]',
 2541:                                                                      $excluded).'</span>';
 2542:         }
 2543:         if ($usernameset eq 'first') {
 2544:             $output .= '<br /><span style="font-size: smaller">';
 2545:             if ($condition) {
 2546:                 $output .= &mt('Your username in LON-CAPA will be the part of your e-mail address before [_1]',
 2547:                                       $condition);
 2548:             } else {
 2549:                 $output .= &mt('Your username in LON-CAPA will be the part of your e-mail address before the @');
 2550:             }
 2551:             $output .= '</span>';
 2552:         }
 2553:         $rowcount ++;
 2554:         $output .= &Apache::lonhtmlcommon::row_closure(1);
 2555:         my $upassone = '<input type="password" name="upass'.$now.'" size="20" autocomplete="off" />';
 2556:         my $upasstwo = '<input type="password" name="upasscheck'.$now.'" size="20" autocomplete="off" />';
 2557:         $output .= &Apache::lonhtmlcommon::row_title(&mt('Password').'<b>*</b>',
 2558:                                                     'LC_pick_box_title',
 2559:                                                     'LC_oddrow_value')."\n".
 2560:                    $upassone."\n".
 2561:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
 2562:                    &Apache::lonhtmlcommon::row_title(&mt('Confirm password').'<b>*</b>',
 2563:                                                      'LC_pick_box_title',
 2564:                                                      'LC_oddrow_value')."\n".
 2565:                    $upasstwo.
 2566:                    &Apache::lonhtmlcommon::row_closure()."\n";
 2567:         if ($usernameset eq 'free') {
 2568:             my $onclick = "toggleUsernameDisp(this,'selfcreateusername');"; 
 2569:             $output .= &Apache::lonhtmlcommon::row_title($lt{'username'},undef,'LC_oddrow_value')."\n".
 2570:                        &mt('Use e-mail address: ').
 2571:                        '<label><input type="radio" name="emailused" value="1" checked="checked" onclick="'.$onclick.'" />'.&mt('Yes').'</label>'."\n".
 2572:                        ('&nbsp;'x2).
 2573:                        '<label><input type="radio" name="emailused" value="0" onclick="'.$onclick.'" />'.&mt('No').'</label>'."\n".
 2574:                        '<div id="selfcreateusername" style="display: none; font-size: smaller">'.
 2575:                        '<br /><span class="LC_nobreak">'.&mt('Preferred username').
 2576:                        '&nbsp;<input type="text" name="username" value="" size="20" autocomplete="off"/>'.
 2577:                        '</span></div>'."\n".&Apache::lonhtmlcommon::row_closure(1);
 2578:             $rowcount ++;
 2579:         }
 2580:     }
 2581:     foreach my $item (@userinfo) {
 2582:         my $rowtitle = $lt{$item};
 2583:         my $hiderow = 0;
 2584:         if ($item eq 'generation') {
 2585:             $rowtitle = $genhelp.$rowtitle;
 2586:         }
 2587:         my $row = &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
 2588:         if ($newuser) {
 2589:             if (ref($inst_results) eq 'HASH') {
 2590:                 if ($inst_results->{$item} ne '') {
 2591:                     $row .= '<input type="hidden" name="c'.$item.'" value="'.$inst_results->{$item}.'" />'.$inst_results->{$item};
 2592:                 } else {
 2593:                     if ($context eq 'selfcreate') {
 2594:                         if ($canmodify{$item}) {
 2595:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
 2596:                             $editable ++;
 2597:                         } else {
 2598:                             $hiderow = 1;
 2599:                         }
 2600:                     } else {
 2601:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
 2602:                     }
 2603:                 }
 2604:             } else {
 2605:                 if ($context eq 'selfcreate') {
 2606:                     if ($canmodify{$item}) {
 2607:                         if ($newuser eq 'email') {
 2608:                             $row .= '<input type="text" name="'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
 2609:                         } else {
 2610:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
 2611:                         }
 2612:                         $editable ++;
 2613:                     } else {
 2614:                         $hiderow = 1;
 2615:                     }
 2616:                 } else {
 2617:                     $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
 2618:                 }
 2619:             }
 2620:         } else {
 2621:             if ($canmodify{$item}) {
 2622:                 $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
 2623:                 if (($item eq 'id') && (!$newuser)) {
 2624:                     $row .= '<br />'.&Apache::lonuserutils::forceid_change($context);
 2625:                 }
 2626:             } else {
 2627:                 $row .= $userenv{$item};
 2628:             }
 2629:         }
 2630:         $row .= &Apache::lonhtmlcommon::row_closure(1);
 2631:         if (!$hiderow) {
 2632:             $output .= $row;
 2633:             $rowcount ++;
 2634:         }
 2635:     }
 2636:     if (($canmodify_status{'inststatus'}) || ($context ne 'selfcreate')) {
 2637:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($ccdomain);
 2638:         if (ref($types) eq 'ARRAY') {
 2639:             if (@{$types} > 0) {
 2640:                 my ($hiderow,$shown);
 2641:                 if ($canmodify_status{'inststatus'}) {
 2642:                     $shown = &pick_inst_statuses($userenv{'inststatus'},$usertypes,$types);
 2643:                 } else {
 2644:                     if ($userenv{'inststatus'} eq '') {
 2645:                         $hiderow = 1;
 2646:                     } else {
 2647:                         my @showitems;
 2648:                         foreach my $item ( map { &unescape($_); } split(':',$userenv{'inststatus'})) {
 2649:                             if (exists($usertypes->{$item})) {
 2650:                                 push(@showitems,$usertypes->{$item});
 2651:                             } else {
 2652:                                 push(@showitems,$item);
 2653:                             }
 2654:                         }
 2655:                         if (@showitems) {
 2656:                             $shown = join(', ',@showitems);
 2657:                         } else {
 2658:                             $hiderow = 1;
 2659:                         }
 2660:                     }
 2661:                 }
 2662:                 if (!$hiderow) {
 2663:                     my $row = &Apache::lonhtmlcommon::row_title(&mt('Affiliations'),undef,'LC_oddrow_value')."\n".
 2664:                               $shown.&Apache::lonhtmlcommon::row_closure(1); 
 2665:                     if ($context eq 'selfcreate') {
 2666:                         $rowcount ++;
 2667:                     }
 2668:                     $output .= $row;
 2669:                 }
 2670:             }
 2671:         }
 2672:     }
 2673:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
 2674:         if ($captchaform) {
 2675:             $output .= &Apache::lonhtmlcommon::row_title($lt{'valid'}.'*',
 2676:                                                          'LC_pick_box_title')."\n".
 2677:                        $captchaform."\n".'<br /><br />'.
 2678:                        &Apache::lonhtmlcommon::row_closure(1); 
 2679:             $rowcount ++;
 2680:         }
 2681:         my $submit_text = &mt('Create account');
 2682:         $output .= &Apache::lonhtmlcommon::row_title()."\n".
 2683:                    '<br /><input type="submit" name="createaccount" value="'.
 2684:                    $submit_text.'" />'.
 2685:                    '<input type="hidden" name="type" value="'.$usertype.'" />'.
 2686:                    &Apache::lonhtmlcommon::row_closure(1);
 2687:     }
 2688:     $output .= &Apache::lonhtmlcommon::end_pick_box();
 2689:     if (wantarray) {
 2690:         if ($context eq 'selfcreate') {
 2691:             return($output,$rowcount,$editable);
 2692:         } else {
 2693:             return $output;
 2694:         }
 2695:     } else {
 2696:         return $output;
 2697:     }
 2698: }
 2699: 
 2700: sub pick_inst_statuses {
 2701:     my ($curr,$usertypes,$types) = @_;
 2702:     my ($output,$rem,@currtypes);
 2703:     if ($curr ne '') {
 2704:         @currtypes = map { &unescape($_); } split(/:/,$curr);
 2705:     }
 2706:     my $numinrow = 2;
 2707:     if (ref($types) eq 'ARRAY') {
 2708:         $output = '<table>';
 2709:         my $lastcolspan; 
 2710:         for (my $i=0; $i<@{$types}; $i++) {
 2711:             if (defined($usertypes->{$types->[$i]})) {
 2712:                 my $rem = $i%($numinrow);
 2713:                 if ($rem == 0) {
 2714:                     if ($i<@{$types}-1) {
 2715:                         if ($i > 0) { 
 2716:                             $output .= '</tr>';
 2717:                         }
 2718:                         $output .= '<tr>';
 2719:                     }
 2720:                 } elsif ($i==@{$types}-1) {
 2721:                     my $colsleft = $numinrow - $rem;
 2722:                     if ($colsleft > 1) {
 2723:                         $lastcolspan = ' colspan="'.$colsleft.'"';
 2724:                     }
 2725:                 }
 2726:                 my $check = ' ';
 2727:                 if (grep(/^\Q$types->[$i]\E$/,@currtypes)) {
 2728:                     $check = ' checked="checked" ';
 2729:                 }
 2730:                 $output .= '<td class="LC_left_item"'.$lastcolspan.'>'.
 2731:                            '<span class="LC_nobreak"><label>'.
 2732:                            '<input type="checkbox" name="inststatus" '.
 2733:                            'value="'.$types->[$i].'"'.$check.'/>'.
 2734:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 2735:             }
 2736:         }
 2737:         $output .= '</tr></table>';
 2738:     }
 2739:     return $output;
 2740: }
 2741: 
 2742: sub selfcreate_canmodify {
 2743:     my ($context,$dom,$userinfo,$inst_results,$rolesarray) = @_;
 2744:     if (ref($inst_results) eq 'HASH') {
 2745:         my @inststatuses = &get_inststatuses($inst_results);
 2746:         if (@inststatuses == 0) {
 2747:             @inststatuses = ('default');
 2748:         }
 2749:         $rolesarray = \@inststatuses;
 2750:     }
 2751:     my %canmodify =
 2752:         &Apache::lonuserutils::can_modify_userinfo($context,$dom,$userinfo,
 2753:                                                    $rolesarray);
 2754:     return %canmodify;
 2755: }
 2756: 
 2757: sub get_inststatuses {
 2758:     my ($insthashref) = @_;
 2759:     my @inststatuses = ();
 2760:     if (ref($insthashref) eq 'HASH') {
 2761:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
 2762:             @inststatuses = @{$insthashref->{'inststatus'}};
 2763:         }
 2764:     }
 2765:     return @inststatuses;
 2766: }
 2767: 
 2768: # ================================================================= Phase Three
 2769: sub update_user_data {
 2770:     my ($r,$context,$crstype,$brcrum,$showcredits,$permission) = @_; 
 2771:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
 2772:                                           $env{'form.ccdomain'});
 2773:     # Error messages
 2774:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
 2775:     my $end       = '</span><br /><br />';
 2776:     my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
 2777:                     "'$env{'form.prevphase'}','modify')".'" />'.
 2778:                     &mt('Return to previous page').'</a>'.
 2779:                     &Apache::loncommon::end_page();
 2780:     my $now = time;
 2781:     my $title;
 2782:     if (exists($env{'form.makeuser'})) {
 2783: 	$title='Set Privileges for New User';
 2784:     } else {
 2785:         $title='Modify User Privileges';
 2786:     }
 2787:     my $newuser = 0;
 2788:     my ($jsback,$elements) = &crumb_utilities();
 2789:     my $jscript = '<script type="text/javascript">'."\n".
 2790:                   '// <![CDATA['."\n".
 2791:                   $jsback."\n".
 2792:                   '// ]]>'."\n".
 2793:                   '</script>'."\n";
 2794:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$env{'form.ccdomain'});
 2795:     push (@{$brcrum},
 2796:              {href => "javascript:backPage(document.userupdate)",
 2797:               text => $breadcrumb_text{'search'},
 2798:               faq  => 282,
 2799:               bug  => 'Instructor Interface',}
 2800:              );
 2801:     if ($env{'form.prevphase'} eq 'userpicked') {
 2802:         push(@{$brcrum},
 2803:                {href => "javascript:backPage(document.userupdate,'get_user_info','select')",
 2804:                 text => $breadcrumb_text{'userpicked'},
 2805:                 faq  => 282,
 2806:                 bug  => 'Instructor Interface',});
 2807:     }
 2808:     my $helpitem = 'Course_Change_Privileges';
 2809:     if ($env{'form.action'} eq 'singlestudent') {
 2810:         $helpitem = 'Course_Add_Student';
 2811:     } elsif ($context eq 'author') {
 2812:         $helpitem = 'Author_Change_Privileges';
 2813:     } elsif ($context eq 'domain') {
 2814:         $helpitem = 'Domain_Change_Privileges';
 2815:     }
 2816:     push(@{$brcrum}, 
 2817:             {href => "javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
 2818:              text => $breadcrumb_text{'modify'},
 2819:              faq  => 282,
 2820:              bug  => 'Instructor Interface',},
 2821:             {href => "/adm/createuser",
 2822:              text => "Result",
 2823:              faq  => 282,
 2824:              bug  => 'Instructor Interface',
 2825:              help => $helpitem});
 2826:     my $args = {bread_crumbs          => $brcrum,
 2827:                 bread_crumbs_component => 'User Management'};
 2828:     if ($env{'form.popup'}) {
 2829:         $args->{'no_nav_bar'} = 1;
 2830:     }
 2831:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
 2832:     $r->print(&update_result_form($uhome));
 2833:     # Check Inputs
 2834:     if (! $env{'form.ccuname'} ) {
 2835: 	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
 2836: 	return;
 2837:     }
 2838:     if (  $env{'form.ccuname'} ne 
 2839: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
 2840: 	$r->print($error.&mt('Invalid login name.').'  '.
 2841: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid.').
 2842: 		  $end.$rtnlink);
 2843: 	return;
 2844:     }
 2845:     if (! $env{'form.ccdomain'}       ) {
 2846: 	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
 2847: 	return;
 2848:     }
 2849:     if (  $env{'form.ccdomain'} ne
 2850: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
 2851: 	$r->print($error.&mt('Invalid domain name.').'  '.
 2852: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid.').
 2853: 		  $end.$rtnlink);
 2854: 	return;
 2855:     }
 2856:     if ($uhome eq 'no_host') {
 2857:         $newuser = 1;
 2858:     }
 2859:     if (! exists($env{'form.makeuser'})) {
 2860:         # Modifying an existing user, so check the validity of the name
 2861:         if ($uhome eq 'no_host') {
 2862:             $r->print(
 2863:                 $error
 2864:                .'<p class="LC_error">'
 2865:                .&mt('Unable to determine home server for [_1] in domain [_2].',
 2866:                         '"'.$env{'form.ccuname'}.'"','"'.$env{'form.ccdomain'}.'"')
 2867:                .'</p>');
 2868:             return;
 2869:         }
 2870:     }
 2871:     # Determine authentication method and password for the user being modified
 2872:     my $amode='';
 2873:     my $genpwd='';
 2874:     if ($env{'form.login'} eq 'krb') {
 2875: 	$amode='krb';
 2876: 	$amode.=$env{'form.krbver'};
 2877: 	$genpwd=$env{'form.krbarg'};
 2878:     } elsif ($env{'form.login'} eq 'int') {
 2879: 	$amode='internal';
 2880: 	$genpwd=$env{'form.intarg'};
 2881:     } elsif ($env{'form.login'} eq 'fsys') {
 2882: 	$amode='unix';
 2883: 	$genpwd=$env{'form.fsysarg'};
 2884:     } elsif ($env{'form.login'} eq 'loc') {
 2885: 	$amode='localauth';
 2886: 	$genpwd=$env{'form.locarg'};
 2887: 	$genpwd=" " if (!$genpwd);
 2888:     } elsif ($env{'form.login'} eq 'lti') {
 2889:         $amode='lti';
 2890:         $genpwd=" ";
 2891:     } elsif (($env{'form.login'} eq 'nochange') ||
 2892:              ($env{'form.login'} eq ''        )) { 
 2893:         # There is no need to tell the user we did not change what they
 2894:         # did not ask us to change.
 2895:         # If they are creating a new user but have not specified login
 2896:         # information this will be caught below.
 2897:     } else {
 2898:             $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);
 2899:             return;
 2900:     }
 2901: 
 2902:     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
 2903:                         $env{'form.ccuname'}.' ('.&Apache::loncommon::plainname($env{'form.ccuname'},
 2904:                         $env{'form.ccdomain'}).')', $env{'form.ccdomain'}).'</h3>');
 2905:     my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,2);
 2906: 
 2907:     my (%alerts,%rulematch,%inst_results,%curr_rules);
 2908:     my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
 2909:     my @usertools = ('aboutme','blog','webdav','portfolio');
 2910:     my @requestcourses = ('official','unofficial','community','textbook','placement','lti');
 2911:     my @requestauthor = ('requestauthor');
 2912:     my ($othertitle,$usertypes,$types) = 
 2913:         &Apache::loncommon::sorted_inst_types($env{'form.ccdomain'});
 2914:     my %canmodify_status =
 2915:         &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},
 2916:                                                    ['inststatus']);
 2917:     if ($env{'form.makeuser'}) {
 2918: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
 2919:         # Check for the authentication mode and password
 2920:         if (! $amode || ! $genpwd) {
 2921: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
 2922: 	    return;
 2923: 	}
 2924:         # Determine desired host
 2925:         my $desiredhost = $env{'form.hserver'};
 2926:         if (lc($desiredhost) eq 'default') {
 2927:             $desiredhost = undef;
 2928:         } else {
 2929:             my %home_servers = 
 2930: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
 2931:             if (! exists($home_servers{$desiredhost})) {
 2932:                 $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
 2933:                 return;
 2934:             }
 2935:         }
 2936:         # Check ID format
 2937:         my %checkhash;
 2938:         my %checks = ('id' => 1);
 2939:         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
 2940:             'newuser' => $newuser, 
 2941:             'id' => $env{'form.cid'},
 2942:         );
 2943:         if ($env{'form.cid'} ne '') {
 2944:             &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
 2945:                                           \%rulematch,\%inst_results,\%curr_rules);
 2946:             if (ref($alerts{'id'}) eq 'HASH') {
 2947:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
 2948:                     my $domdesc =
 2949:                         &Apache::lonnet::domain($env{'form.ccdomain'},'description');
 2950:                     if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
 2951:                         my $userchkmsg;
 2952:                         if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
 2953:                             $userchkmsg  = 
 2954:                                 &Apache::loncommon::instrule_disallow_msg('id',
 2955:                                                                     $domdesc,1).
 2956:                                 &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
 2957:                                     $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
 2958:                         }
 2959:                         $r->print($error.&mt('Invalid ID format').$end.
 2960:                                   $userchkmsg.$rtnlink);
 2961:                         return;
 2962:                     }
 2963:                 }
 2964:             }
 2965:         }
 2966:         &Apache::lonhtmlcommon::Increment_PrgWin($r, \%prog_state);
 2967: 	# Call modifyuser
 2968: 	my $result = &Apache::lonnet::modifyuser
 2969: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
 2970:              $amode,$genpwd,$env{'form.cfirstname'},
 2971:              $env{'form.cmiddlename'},$env{'form.clastname'},
 2972:              $env{'form.cgeneration'},undef,$desiredhost,
 2973:              $env{'form.cpermanentemail'});
 2974: 	$r->print(&mt('Generating user').': '.$result);
 2975:         $uhome = &Apache::lonnet::homeserver($env{'form.ccuname'},
 2976:                                                $env{'form.ccdomain'});
 2977:         my (%changeHash,%newcustom,%changed,%changedinfo);
 2978:         if ($uhome ne 'no_host') {
 2979:             if ($context eq 'domain') {
 2980:                 foreach my $name ('portfolio','author') {
 2981:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
 2982:                         if ($env{'form.'.$name.'quota'} eq '') {
 2983:                             $newcustom{$name.'quota'} = 0;
 2984:                         } else {
 2985:                             $newcustom{$name.'quota'} = $env{'form.'.$name.'quota'};
 2986:                             $newcustom{$name.'quota'} =~ s/[^\d\.]//g;
 2987:                         }
 2988:                         if (&quota_admin($newcustom{$name.'quota'},\%changeHash,$name)) {
 2989:                             $changed{$name.'quota'} = 1;
 2990:                         }
 2991:                     }
 2992:                 }
 2993:                 foreach my $item (@usertools) {
 2994:                     if ($env{'form.custom'.$item} == 1) {
 2995:                         $newcustom{$item} = $env{'form.tools_'.$item};
 2996:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
 2997:                                                      \%changeHash,'tools');
 2998:                     }
 2999:                 }
 3000:                 foreach my $item (@requestcourses) {
 3001:                     if ($env{'form.custom'.$item} == 1) {
 3002:                         $newcustom{$item} = $env{'form.crsreq_'.$item};
 3003:                         if ($env{'form.crsreq_'.$item} eq 'autolimit') {
 3004:                             $newcustom{$item} .= '=';
 3005:                             $env{'form.crsreq_'.$item.'_limit'} =~ s/\D+//g;
 3006:                             if ($env{'form.crsreq_'.$item.'_limit'}) {
 3007:                                 $newcustom{$item} .= $env{'form.crsreq_'.$item.'_limit'};
 3008:                             }
 3009:                         }
 3010:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
 3011:                                                       \%changeHash,'requestcourses');
 3012:                     }
 3013:                 }
 3014:                 if ($env{'form.customrequestauthor'} == 1) {
 3015:                     $newcustom{'requestauthor'} = $env{'form.requestauthor'};
 3016:                     $changed{'requestauthor'} = &tool_admin('requestauthor',
 3017:                                                     $newcustom{'requestauthor'},
 3018:                                                     \%changeHash,'requestauthor');
 3019:                 }
 3020:             }
 3021:             if ($canmodify_status{'inststatus'}) {
 3022:                 if (exists($env{'form.inststatus'})) {
 3023:                     my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
 3024:                     if (@inststatuses > 0) {
 3025:                         $changeHash{'inststatus'} = join(',',@inststatuses);
 3026:                         $changed{'inststatus'} = $changeHash{'inststatus'};
 3027:                     }
 3028:                 }
 3029:             }
 3030:             if (keys(%changed)) {
 3031:                 foreach my $item (@userinfo) {
 3032:                     $changeHash{$item}  = $env{'form.c'.$item};
 3033:                 }
 3034:                 my $chgresult =
 3035:                      &Apache::lonnet::put('environment',\%changeHash,
 3036:                                           $env{'form.ccdomain'},$env{'form.ccuname'});
 3037:             } 
 3038:         }
 3039:         $r->print('<br />'.&mt('Home server').': '.$uhome.' '.
 3040:                   &Apache::lonnet::hostname($uhome));
 3041:     } elsif (($env{'form.login'} ne 'nochange') &&
 3042:              ($env{'form.login'} ne ''        )) {
 3043: 	# Modify user privileges
 3044:         if (! $amode || ! $genpwd) {
 3045: 	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
 3046: 	    return;
 3047: 	}
 3048: 	# Only allow authentication modification if the person has authority
 3049: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
 3050: 	    $r->print('Modifying authentication: '.
 3051:                       &Apache::lonnet::modifyuserauth(
 3052: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
 3053:                        $amode,$genpwd));
 3054:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
 3055: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
 3056: 	} else {
 3057: 	    # Okay, this is a non-fatal error.
 3058: 	    $r->print($error.&mt('You do not have the authority to modify this users authentication information.').$end);    
 3059: 	}
 3060:     } elsif (($env{'form.intarg'} ne '') &&
 3061:              (&Apache::lonnet::queryauthenticate($env{'form.ccuname'},$env{'form.ccdomain'}) =~ /^internal:/) &&
 3062:              (&Apache::lonuserutils::can_change_internalpass($env{'form.ccuname'},$env{'form.ccdomain'},$crstype,$permission))) {
 3063:         $r->print('Modifying authentication: '.
 3064:                   &Apache::lonnet::modifyuserauth(
 3065:                   $env{'form.ccdomain'},$env{'form.ccuname'},
 3066:                   'internal',$env{'form.intarg'}));
 3067:     }
 3068:     $r->rflush(); # Finish display of header before time consuming actions start
 3069:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state);
 3070:     ##
 3071:     my (@userroles,%userupdate,$cnum,$cdom,$defaultcredits,%namechanged);
 3072:     if ($context eq 'course') {
 3073:         ($cnum,$cdom) =
 3074:             &Apache::lonuserutils::get_course_identity();
 3075:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
 3076:         if ($showcredits) {
 3077:            $defaultcredits = &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
 3078:         }
 3079:     }
 3080:     if (! $env{'form.makeuser'} ) {
 3081:         # Check for need to change
 3082:         my %userenv = &Apache::lonnet::get
 3083:             ('environment',['firstname','middlename','lastname','generation',
 3084:              'id','permanentemail','portfolioquota','authorquota','inststatus',
 3085:              'tools.aboutme','tools.blog','tools.webdav','tools.portfolio',
 3086:              'requestcourses.official','requestcourses.unofficial',
 3087:              'requestcourses.community','requestcourses.textbook',
 3088:              'reqcrsotherdom.official','reqcrsotherdom.unofficial',
 3089:              'reqcrsotherdom.community','reqcrsotherdom.textbook',
 3090:              'reqcrsotherdom.placement','requestauthor'],
 3091:               $env{'form.ccdomain'},$env{'form.ccuname'});
 3092:         my ($tmp) = keys(%userenv);
 3093:         if ($tmp =~ /^(con_lost|error)/i) { 
 3094:             %userenv = ();
 3095:         }
 3096:         my $no_forceid_alert;
 3097:         # Check to see if user information can be changed
 3098:         my %domconfig =
 3099:             &Apache::lonnet::get_dom('configuration',['usermodification'],
 3100:                                      $env{'form.ccdomain'});
 3101:         my @statuses = ('active','future');
 3102:         my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@statuses,undef,$env{'request.role.domain'});
 3103:         my ($auname,$audom);
 3104:         if ($context eq 'author') {
 3105:             $auname = $env{'user.name'};
 3106:             $audom = $env{'user.domain'};     
 3107:         }
 3108:         foreach my $item (keys(%roles)) {
 3109:             my ($rolenum,$roledom,$role) = split(/:/,$item,-1);
 3110:             if ($context eq 'course') {
 3111:                 if ($cnum ne '' && $cdom ne '') {
 3112:                     if ($rolenum eq $cnum && $roledom eq $cdom) {
 3113:                         if (!grep(/^\Q$role\E$/,@userroles)) {
 3114:                             push(@userroles,$role);
 3115:                         }
 3116:                     }
 3117:                 }
 3118:             } elsif ($context eq 'author') {
 3119:                 if ($rolenum eq $auname && $roledom eq $audom) {
 3120:                     if (!grep(/^\Q$role\E$/,@userroles)) { 
 3121:                         push(@userroles,$role);
 3122:                     }
 3123:                 }
 3124:             }
 3125:         }
 3126:         if ($env{'form.action'} eq 'singlestudent') {
 3127:             if (!grep(/^st$/,@userroles)) {
 3128:                 push(@userroles,'st');
 3129:             }
 3130:         } else {
 3131:             # Check for course or co-author roles being activated or re-enabled
 3132:             if ($context eq 'author' || $context eq 'course') {
 3133:                 foreach my $key (keys(%env)) {
 3134:                     if ($context eq 'author') {
 3135:                         if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
 3136:                             if (!grep(/^\Q$1\E$/,@userroles)) {
 3137:                                 push(@userroles,$1);
 3138:                             }
 3139:                         } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
 3140:                             if (!grep(/^\Q$1\E$/,@userroles)) {
 3141:                                 push(@userroles,$1);
 3142:                             }
 3143:                         }
 3144:                     } elsif ($context eq 'course') {
 3145:                         if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
 3146:                             if (!grep(/^\Q$1\E$/,@userroles)) {
 3147:                                 push(@userroles,$1);
 3148:                             }
 3149:                         } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
 3150:                             if (!grep(/^\Q$1\E$/,@userroles)) {
 3151:                                 push(@userroles,$1);
 3152:                             }
 3153:                         }
 3154:                     }
 3155:                 }
 3156:             }
 3157:         }
 3158:         #Check to see if we can change personal data for the user 
 3159:         my (@mod_disallowed,@longroles);
 3160:         foreach my $role (@userroles) {
 3161:             if ($role eq 'cr') {
 3162:                 push(@longroles,'Custom');
 3163:             } else {
 3164:                 push(@longroles,&Apache::lonnet::plaintext($role,$crstype)); 
 3165:             }
 3166:         }
 3167:         my %canmodify = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},\@userinfo,\@userroles);
 3168:         foreach my $item (@userinfo) {
 3169:             # Strip leading and trailing whitespace
 3170:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
 3171:             if (!$canmodify{$item}) {
 3172:                 if (defined($env{'form.c'.$item})) {
 3173:                     if ($env{'form.c'.$item} ne $userenv{$item}) {
 3174:                         push(@mod_disallowed,$item);
 3175:                     }
 3176:                 }
 3177:                 $env{'form.c'.$item} = $userenv{$item};
 3178:             }
 3179:         }
 3180:         # Check to see if we can change the Student/Employee ID
 3181:         my $forceid = $env{'form.forceid'};
 3182:         my $recurseid = $env{'form.recurseid'};
 3183:         my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
 3184:         my %uidhash = &Apache::lonnet::idrget($env{'form.ccdomain'},
 3185:                                             $env{'form.ccuname'});
 3186:         if (($uidhash{$env{'form.ccuname'}}) && 
 3187:             ($uidhash{$env{'form.ccuname'}}!~/error\:/) && 
 3188:             (!$forceid)) {
 3189:             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
 3190:                 $env{'form.cid'} = $userenv{'id'};
 3191:                 $no_forceid_alert = &mt('New student/employee ID does not match existing ID for this user.')
 3192:                                    .'<br />'
 3193:                                    .&mt("Change is not permitted without checking the 'Force ID change' checkbox on the previous page.")
 3194:                                    .'<br />'."\n";
 3195:             }
 3196:         }
 3197:         if ($env{'form.cid'} ne $userenv{'id'}) {
 3198:             my $checkhash;
 3199:             my $checks = { 'id' => 1 };
 3200:             $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
 3201:                    { 'newuser' => $newuser,
 3202:                      'id'  => $env{'form.cid'}, 
 3203:                    };
 3204:             &Apache::loncommon::user_rule_check($checkhash,$checks,
 3205:                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
 3206:             if (ref($alerts{'id'}) eq 'HASH') {
 3207:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
 3208:                    $env{'form.cid'} = $userenv{'id'};
 3209:                 }
 3210:             }
 3211:         }
 3212:         my (%quotachanged,%oldquota,%newquota,%olddefquota,%newdefquota, 
 3213:             $oldinststatus,$newinststatus,%oldisdefault,%newisdefault,%oldsettings,
 3214:             %oldsettingstext,%newsettings,%newsettingstext,@disporder,
 3215:             %oldsettingstatus,%newsettingstatus);
 3216:         @disporder = ('inststatus');
 3217:         if ($env{'request.role.domain'} eq $env{'form.ccdomain'}) {
 3218:             push(@disporder,'requestcourses','requestauthor');
 3219:         } else {
 3220:             push(@disporder,'reqcrsotherdom');
 3221:         }
 3222:         push(@disporder,('quota','tools'));
 3223:         $oldinststatus = $userenv{'inststatus'};
 3224:         foreach my $name ('portfolio','author') {
 3225:             ($olddefquota{$name},$oldsettingstatus{$name}) = 
 3226:                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$oldinststatus,$name);
 3227:             ($newdefquota{$name},$newsettingstatus{$name}) = ($olddefquota{$name},$oldsettingstatus{$name});
 3228:         }
 3229:         my %canshow;
 3230:         if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
 3231:             $canshow{'quota'} = 1;
 3232:         }
 3233:         if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
 3234:             $canshow{'tools'} = 1;
 3235:         }
 3236:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
 3237:             $canshow{'requestcourses'} = 1;
 3238:         } elsif (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
 3239:             $canshow{'reqcrsotherdom'} = 1;
 3240:         }
 3241:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
 3242:             $canshow{'inststatus'} = 1;
 3243:         }
 3244:         if (&Apache::lonnet::allowed('cau',$env{'form.ccdomain'})) {
 3245:             $canshow{'requestauthor'} = 1;
 3246:         }
 3247:         my (%changeHash,%changed);
 3248:         if ($oldinststatus eq '') {
 3249:             $oldsettings{'inststatus'} = $othertitle; 
 3250:         } else {
 3251:             if (ref($usertypes) eq 'HASH') {
 3252:                 $oldsettings{'inststatus'} = join(', ',map{ $usertypes->{ &unescape($_) }; } (split(/:/,$userenv{'inststatus'})));
 3253:             } else {
 3254:                 $oldsettings{'inststatus'} = join(', ',map{ &unescape($_); } (split(/:/,$userenv{'inststatus'})));
 3255:             }
 3256:         }
 3257:         $changeHash{'inststatus'} = $userenv{'inststatus'};
 3258:         if ($canmodify_status{'inststatus'}) {
 3259:             $canshow{'inststatus'} = 1;
 3260:             if (exists($env{'form.inststatus'})) {
 3261:                 my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
 3262:                 if (@inststatuses > 0) {
 3263:                     $newinststatus = join(':',map { &escape($_); } @inststatuses);
 3264:                     $changeHash{'inststatus'} = $newinststatus;
 3265:                     if ($newinststatus ne $oldinststatus) {
 3266:                         $changed{'inststatus'} = $newinststatus;
 3267:                         foreach my $name ('portfolio','author') {
 3268:                             ($newdefquota{$name},$newsettingstatus{$name}) =
 3269:                                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
 3270:                         }
 3271:                     }
 3272:                     if (ref($usertypes) eq 'HASH') {
 3273:                         $newsettings{'inststatus'} = join(', ',map{ $usertypes->{$_}; } (@inststatuses)); 
 3274:                     } else {
 3275:                         $newsettings{'inststatus'} = join(', ',@inststatuses);
 3276:                     }
 3277:                 }
 3278:             } else {
 3279:                 $newinststatus = '';
 3280:                 $changeHash{'inststatus'} = $newinststatus;
 3281:                 $newsettings{'inststatus'} = $othertitle;
 3282:                 if ($newinststatus ne $oldinststatus) {
 3283:                     $changed{'inststatus'} = $changeHash{'inststatus'};
 3284:                     foreach my $name ('portfolio','author') {
 3285:                         ($newdefquota{$name},$newsettingstatus{$name}) =
 3286:                             &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
 3287:                     }
 3288:                 }
 3289:             }
 3290:         } elsif ($context ne 'selfcreate') {
 3291:             $canshow{'inststatus'} = 1;
 3292:             $newsettings{'inststatus'} = $oldsettings{'inststatus'};
 3293:         }
 3294:         foreach my $name ('portfolio','author') {
 3295:             $changeHash{$name.'quota'} = $userenv{$name.'quota'};
 3296:         }
 3297:         if ($context eq 'domain') {
 3298:             foreach my $name ('portfolio','author') {
 3299:                 if ($userenv{$name.'quota'} ne '') {
 3300:                     $oldquota{$name} = $userenv{$name.'quota'};
 3301:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
 3302:                         if ($env{'form.'.$name.'quota'} eq '') {
 3303:                             $newquota{$name} = 0;
 3304:                         } else {
 3305:                             $newquota{$name} = $env{'form.'.$name.'quota'};
 3306:                             $newquota{$name} =~ s/[^\d\.]//g;
 3307:                         }
 3308:                         if ($newquota{$name} != $oldquota{$name}) {
 3309:                             if (&quota_admin($newquota{$name},\%changeHash,$name)) {
 3310:                                 $changed{$name.'quota'} = 1;
 3311:                             }
 3312:                         }
 3313:                     } else {
 3314:                         if (&quota_admin('',\%changeHash,$name)) {
 3315:                             $changed{$name.'quota'} = 1;
 3316:                             $newquota{$name} = $newdefquota{$name};
 3317:                             $newisdefault{$name} = 1;
 3318:                         }
 3319:                     }
 3320:                 } else {
 3321:                     $oldisdefault{$name} = 1;
 3322:                     $oldquota{$name} = $olddefquota{$name};
 3323:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
 3324:                         if ($env{'form.'.$name.'quota'} eq '') {
 3325:                             $newquota{$name} = 0;
 3326:                         } else {
 3327:                             $newquota{$name} = $env{'form.'.$name.'quota'};
 3328:                             $newquota{$name} =~ s/[^\d\.]//g;
 3329:                         }
 3330:                         if (&quota_admin($newquota{$name},\%changeHash,$name)) {
 3331:                             $changed{$name.'quota'} = 1;
 3332:                         }
 3333:                     } else {
 3334:                         $newquota{$name} = $newdefquota{$name};
 3335:                         $newisdefault{$name} = 1;
 3336:                     }
 3337:                 }
 3338:                 if ($oldisdefault{$name}) {
 3339:                     $oldsettingstext{'quota'}{$name} = &get_defaultquota_text($oldsettingstatus{$name});
 3340:                 }  else {
 3341:                     $oldsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$oldquota{$name});
 3342:                 }
 3343:                 if ($newisdefault{$name}) {
 3344:                     $newsettingstext{'quota'}{$name} = &get_defaultquota_text($newsettingstatus{$name});
 3345:                 } else {
 3346:                     $newsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$newquota{$name});
 3347:                 }
 3348:             }
 3349:             &tool_changes('tools',\@usertools,\%oldsettings,\%oldsettingstext,\%userenv,
 3350:                           \%changeHash,\%changed,\%newsettings,\%newsettingstext);
 3351:             if ($env{'form.ccdomain'} eq $env{'request.role.domain'}) {
 3352:                 &tool_changes('requestcourses',\@requestcourses,\%oldsettings,\%oldsettingstext,
 3353:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
 3354:                 &tool_changes('requestauthor',\@requestauthor,\%oldsettings,\%oldsettingstext,
 3355:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
 3356:             } else {
 3357:                 &tool_changes('reqcrsotherdom',\@requestcourses,\%oldsettings,\%oldsettingstext,
 3358:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
 3359:             }
 3360:         }
 3361:         foreach my $item (@userinfo) {
 3362:             if ($env{'form.c'.$item} ne $userenv{$item}) {
 3363:                 $namechanged{$item} = 1;
 3364:             }
 3365:         }
 3366:         foreach my $name ('portfolio','author') {
 3367:             $oldsettings{'quota'}{$name} = &mt('[_1] MB',$oldquota{$name});
 3368:             $newsettings{'quota'}{$name} = &mt('[_1] MB',$newquota{$name});
 3369:         }
 3370:         if ((keys(%namechanged) > 0) || (keys(%changed) > 0)) {
 3371:             my ($chgresult,$namechgresult);
 3372:             if (keys(%changed) > 0) {
 3373:                 $chgresult = 
 3374:                     &Apache::lonnet::put('environment',\%changeHash,
 3375:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
 3376:                 if ($chgresult eq 'ok') {
 3377:                     if (($env{'user.name'} eq $env{'form.ccuname'}) &&
 3378:                         ($env{'user.domain'} eq $env{'form.ccdomain'})) {
 3379:                         my %newenvhash;
 3380:                         foreach my $key (keys(%changed)) {
 3381:                             if (($key eq 'official') || ($key eq 'unofficial') ||
 3382:                                 ($key eq 'community') || ($key eq 'textbook') ||
 3383:                                 ($key eq 'placement') || ($key eq 'lti')) {
 3384:                                 $newenvhash{'environment.requestcourses.'.$key} =
 3385:                                     $changeHash{'requestcourses.'.$key};
 3386:                                 if ($changeHash{'requestcourses.'.$key}) {
 3387:                                     $newenvhash{'environment.canrequest.'.$key} = 1;
 3388:                                 } else {
 3389:                                     $newenvhash{'environment.canrequest.'.$key} =
 3390:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
 3391:                                             $key,'reload','requestcourses');
 3392:                                 }
 3393:                             } elsif ($key eq 'requestauthor') {
 3394:                                 $newenvhash{'environment.'.$key} = $changeHash{$key};
 3395:                                 if ($changeHash{$key}) {
 3396:                                     $newenvhash{'environment.canrequest.author'} = 1;
 3397:                                 } else {
 3398:                                     $newenvhash{'environment.canrequest.author'} =
 3399:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
 3400:                                             $key,'reload','requestauthor');
 3401:                                 }
 3402:                             } elsif ($key ne 'quota') {
 3403:                                 $newenvhash{'environment.tools.'.$key} = 
 3404:                                     $changeHash{'tools.'.$key};
 3405:                                 if ($changeHash{'tools.'.$key} ne '') {
 3406:                                     $newenvhash{'environment.availabletools.'.$key} =
 3407:                                         $changeHash{'tools.'.$key};
 3408:                                 } else {
 3409:                                     $newenvhash{'environment.availabletools.'.$key} =
 3410:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
 3411:           $key,'reload','tools');
 3412:                                 }
 3413:                             }
 3414:                         }
 3415:                         if (keys(%newenvhash)) {
 3416:                             &Apache::lonnet::appenv(\%newenvhash);
 3417:                         }
 3418:                     }
 3419:                 }
 3420:             }
 3421:             if (keys(%namechanged) > 0) {
 3422:                 foreach my $field (@userinfo) {
 3423:                     $changeHash{$field}  = $env{'form.c'.$field};
 3424:                 }
 3425: # Make the change
 3426:                 $namechgresult =
 3427:                     &Apache::lonnet::modifyuser($env{'form.ccdomain'},
 3428:                         $env{'form.ccuname'},$changeHash{'id'},undef,undef,
 3429:                         $changeHash{'firstname'},$changeHash{'middlename'},
 3430:                         $changeHash{'lastname'},$changeHash{'generation'},
 3431:                         $changeHash{'id'},undef,$changeHash{'permanentemail'},undef,\@userinfo);
 3432:                 %userupdate = (
 3433:                                lastname   => $env{'form.clastname'},
 3434:                                middlename => $env{'form.cmiddlename'},
 3435:                                firstname  => $env{'form.cfirstname'},
 3436:                                generation => $env{'form.cgeneration'},
 3437:                                id         => $env{'form.cid'},
 3438:                              );
 3439:             }
 3440:             if (((keys(%namechanged) > 0) && $namechgresult eq 'ok') || 
 3441:                 ((keys(%changed) > 0) && $chgresult eq 'ok')) {
 3442:             # Tell the user we changed the name
 3443:                 &display_userinfo($r,1,\@disporder,\%canshow,\@requestcourses,
 3444:                                   \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,
 3445:                                   \%oldsettings, \%oldsettingstext,\%newsettings,
 3446:                                   \%newsettingstext);
 3447:                 if ($env{'form.cid'} ne $userenv{'id'}) {
 3448:                     &Apache::lonnet::idput($env{'form.ccdomain'},
 3449:                          {$env{'form.ccuname'} => $env{'form.cid'}},$uhome,'ids');
 3450:                     if (($recurseid) &&
 3451:                         (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
 3452:                         my $idresult = 
 3453:                             &Apache::lonuserutils::propagate_id_change(
 3454:                                 $env{'form.ccuname'},$env{'form.ccdomain'},
 3455:                                 \%userupdate);
 3456:                         $r->print('<br />'.$idresult.'<br />');
 3457:                     }
 3458:                 }
 3459:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
 3460:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
 3461:                     my %newenvhash;
 3462:                     foreach my $key (keys(%changeHash)) {
 3463:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
 3464:                     }
 3465:                     &Apache::lonnet::appenv(\%newenvhash);
 3466:                 }
 3467:             } else { # error occurred
 3468:                 $r->print(
 3469:                     '<p class="LC_error">'
 3470:                    .&mt('Unable to successfully change environment for [_1] in domain [_2].',
 3471:                             '"'.$env{'form.ccuname'}.'"',
 3472:                             '"'.$env{'form.ccdomain'}.'"')
 3473:                    .'</p>');
 3474:             }
 3475:         } else { # End of if ($env ... ) logic
 3476:             # They did not want to change the users name, quota, tool availability,
 3477:             # or ability to request creation of courses, 
 3478:             # but we can still tell them what the name and quota and availabilities are  
 3479:             &display_userinfo($r,undef,\@disporder,\%canshow,\@requestcourses,
 3480:                               \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,\%oldsettings,
 3481:                               \%oldsettingstext,\%newsettings,\%newsettingstext);
 3482:         }
 3483:         if (@mod_disallowed) {
 3484:             my ($rolestr,$contextname);
 3485:             if (@longroles > 0) {
 3486:                 $rolestr = join(', ',@longroles);
 3487:             } else {
 3488:                 $rolestr = &mt('No roles');
 3489:             }
 3490:             if ($context eq 'course') {
 3491:                 $contextname = 'course';
 3492:             } elsif ($context eq 'author') {
 3493:                 $contextname = 'co-author';
 3494:             }
 3495:             $r->print(&mt('The following fields were not updated: ').'<ul>');
 3496:             my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3497:             foreach my $field (@mod_disallowed) {
 3498:                 $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
 3499:             }
 3500:             $r->print('</ul>');
 3501:             if (@mod_disallowed == 1) {
 3502:                 $r->print(&mt("You do not have the authority to change this field given the user's current set of active/future $contextname roles:"));
 3503:             } else {
 3504:                 $r->print(&mt("You do not have the authority to change these fields given the user's current set of active/future $contextname roles:"));
 3505:             }
 3506:             my $helplink = 'javascript:helpMenu('."'display'".')';
 3507:             $r->print('<span class="LC_cusr_emph">'.$rolestr.'</span><br />'
 3508:                      .&mt('Please contact your [_1]helpdesk[_2] for more information.'
 3509:                          ,'<a href="'.$helplink.'">','</a>')
 3510:                       .'<br />');
 3511:         }
 3512:         $r->print('<span class="LC_warning">'
 3513:                   .$no_forceid_alert
 3514:                   .&Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules)
 3515:                   .'</span>');
 3516:     }
 3517:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 3518:     if ($env{'form.action'} eq 'singlestudent') {
 3519:         &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context,
 3520:                                $crstype,$showcredits,$defaultcredits);
 3521:         my $linktext = ($crstype eq 'Community' ?
 3522:             &mt('Enroll Another Member') : &mt('Enroll Another Student'));
 3523:         $r->print(
 3524:             &Apache::lonhtmlcommon::actionbox([
 3525:                 '<a href="javascript:backPage(document.userupdate)">'
 3526:                .($crstype eq 'Community' ? 
 3527:                     &mt('Enroll Another Member') : &mt('Enroll Another Student'))
 3528:                .'</a>']));
 3529:     } else {
 3530:         my @rolechanges = &update_roles($r,$context,$showcredits);
 3531:         if (keys(%namechanged) > 0) {
 3532:             if ($context eq 'course') {
 3533:                 if (@userroles > 0) {
 3534:                     if ((@rolechanges == 0) || 
 3535:                         (!(grep(/^st$/,@rolechanges)))) {
 3536:                         if (grep(/^st$/,@userroles)) {
 3537:                             my $classlistupdated =
 3538:                                 &Apache::lonuserutils::update_classlist($cdom,
 3539:                                               $cnum,$env{'form.ccdomain'},
 3540:                                        $env{'form.ccuname'},\%userupdate);
 3541:                         }
 3542:                     }
 3543:                 }
 3544:             }
 3545:         }
 3546:         my $userinfo = &Apache::loncommon::plainname($env{'form.ccuname'},
 3547:                                                      $env{'form.ccdomain'});
 3548:         if ($env{'form.popup'}) {
 3549:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
 3550:         } else {
 3551:             $r->print('<br />'.&Apache::lonhtmlcommon::actionbox(['<a href="javascript:backPage(document.userupdate,'."'$env{'form.prevphase'}','modify'".')">'
 3552:                      .&mt('Modify this user: [_1]','<span class="LC_cusr_emph">'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.' ('.$userinfo.')</span>').'</a>',
 3553:                      '<a href="javascript:backPage(document.userupdate)">'.&mt('Create/Modify Another User').'</a>']));
 3554:         }
 3555:     }
 3556: }
 3557: 
 3558: sub display_userinfo {
 3559:     my ($r,$changed,$order,$canshow,$requestcourses,$usertools,$requestauthor,
 3560:         $userenv,$changedhash,$namechangedhash,$oldsetting,$oldsettingtext,
 3561:         $newsetting,$newsettingtext) = @_;
 3562:     return unless (ref($order) eq 'ARRAY' &&
 3563:                    ref($canshow) eq 'HASH' && 
 3564:                    ref($requestcourses) eq 'ARRAY' && 
 3565:                    ref($requestauthor) eq 'ARRAY' &&
 3566:                    ref($usertools) eq 'ARRAY' && 
 3567:                    ref($userenv) eq 'HASH' &&
 3568:                    ref($changedhash) eq 'HASH' &&
 3569:                    ref($oldsetting) eq 'HASH' &&
 3570:                    ref($oldsettingtext) eq 'HASH' &&
 3571:                    ref($newsetting) eq 'HASH' &&
 3572:                    ref($newsettingtext) eq 'HASH');
 3573:     my %lt=&Apache::lonlocal::texthash(
 3574:          'ui'             => 'User Information',
 3575:          'uic'            => 'User Information Changed',
 3576:          'firstname'      => 'First Name',
 3577:          'middlename'     => 'Middle Name',
 3578:          'lastname'       => 'Last Name',
 3579:          'generation'     => 'Generation',
 3580:          'id'             => 'Student/Employee ID',
 3581:          'permanentemail' => 'Permanent e-mail address',
 3582:          'portfolioquota' => 'Disk space allocated to portfolio files',
 3583:          'authorquota'    => 'Disk space allocated to Authoring Space',
 3584:          'blog'           => 'Blog Availability',
 3585:          'webdav'         => 'WebDAV Availability',
 3586:          'aboutme'        => 'Personal Information Page Availability',
 3587:          'portfolio'      => 'Portfolio Availability',
 3588:          'official'       => 'Can Request Official Courses',
 3589:          'unofficial'     => 'Can Request Unofficial Courses',
 3590:          'community'      => 'Can Request Communities',
 3591:          'textbook'       => 'Can Request Textbook Courses',
 3592:          'placement'      => 'Can Request Placement Tests',
 3593:          'lti'            => 'Can Request LTI Courses',
 3594:          'requestauthor'  => 'Can Request Author Role',
 3595:          'inststatus'     => "Affiliation",
 3596:          'prvs'           => 'Previous Value:',
 3597:          'chto'           => 'Changed To:'
 3598:     );
 3599:     if ($changed) {
 3600:         $r->print('<h3>'.$lt{'uic'}.'</h3>'.
 3601:                 &Apache::loncommon::start_data_table().
 3602:                 &Apache::loncommon::start_data_table_header_row());
 3603:         $r->print("<th>&nbsp;</th>\n");
 3604:         $r->print('<th><b>'.$lt{'prvs'}.'</b></th>');
 3605:         $r->print('<th><span class="LC_nobreak"><b>'.$lt{'chto'}.'</b></span></th>');
 3606:         $r->print(&Apache::loncommon::end_data_table_header_row());
 3607:         my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
 3608: 
 3609:         foreach my $item (@userinfo) {
 3610:             my $value = $env{'form.c'.$item};
 3611:             #show changes only:
 3612:             unless ($value eq $userenv->{$item}){
 3613:                 $r->print(&Apache::loncommon::start_data_table_row());
 3614:                 $r->print("<td>$lt{$item}</td>\n");
 3615:                 $r->print("<td>".$userenv->{$item}."</td>\n");
 3616:                 $r->print("<td>$value </td>\n");
 3617:                 $r->print(&Apache::loncommon::end_data_table_row());
 3618:             }
 3619:         }
 3620:         foreach my $entry (@{$order}) {
 3621:             if ($canshow->{$entry}) {
 3622:                 if (($entry eq 'requestcourses') || ($entry eq 'reqcrsotherdom') || ($entry eq 'requestauthor')) {
 3623:                     my @items;
 3624:                     if ($entry eq 'requestauthor') {
 3625:                         @items = ($entry);
 3626:                     } else {
 3627:                         @items = @{$requestcourses};
 3628:                     }
 3629:                     foreach my $item (@items) {
 3630:                         if (($newsetting->{$item} ne $oldsetting->{$item}) || 
 3631:                             ($newsettingtext->{$item} ne $oldsettingtext->{$item})) {
 3632:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");  
 3633:                             $r->print("<td>$lt{$item}</td>\n");
 3634:                             $r->print("<td>".$oldsetting->{$item});
 3635:                             if ($oldsettingtext->{$item}) {
 3636:                                 if ($oldsetting->{$item}) {
 3637:                                     $r->print(' -- ');
 3638:                                 }
 3639:                                 $r->print($oldsettingtext->{$item});
 3640:                             }
 3641:                             $r->print("</td>\n");
 3642:                             $r->print("<td>".$newsetting->{$item});
 3643:                             if ($newsettingtext->{$item}) {
 3644:                                 if ($newsetting->{$item}) {
 3645:                                     $r->print(' -- ');
 3646:                                 }
 3647:                                 $r->print($newsettingtext->{$item});
 3648:                             }
 3649:                             $r->print("</td>\n");
 3650:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
 3651:                         }
 3652:                     }
 3653:                 } elsif ($entry eq 'tools') {
 3654:                     foreach my $item (@{$usertools}) {
 3655:                         if ($newsetting->{$item} ne $oldsetting->{$item}) {
 3656:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");
 3657:                             $r->print("<td>$lt{$item}</td>\n");
 3658:                             $r->print("<td>".$oldsetting->{$item}.' '.$oldsettingtext->{$item}."</td>\n");
 3659:                             $r->print("<td>".$newsetting->{$item}.' '.$newsettingtext->{$item}."</td>\n");
 3660:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
 3661:                         }
 3662:                     }
 3663:                 } elsif ($entry eq 'quota') {
 3664:                     if ((ref($oldsetting->{$entry}) eq 'HASH') && (ref($oldsettingtext->{$entry}) eq 'HASH') &&
 3665:                         (ref($newsetting->{$entry}) eq 'HASH') && (ref($newsettingtext->{$entry}) eq 'HASH')) {
 3666:                         foreach my $name ('portfolio','author') {
 3667:                             if ($newsetting->{$entry}->{$name} ne $oldsetting->{$entry}->{$name}) {
 3668:                                 $r->print(&Apache::loncommon::start_data_table_row()."\n");
 3669:                                 $r->print("<td>$lt{$name.$entry}</td>\n");
 3670:                                 $r->print("<td>".$oldsettingtext->{$entry}->{$name}."</td>\n");
 3671:                                 $r->print("<td>".$newsettingtext->{$entry}->{$name}."</td>\n");
 3672:                                 $r->print(&Apache::loncommon::end_data_table_row()."\n");
 3673:                             }
 3674:                         }
 3675:                     }
 3676:                 } else {
 3677:                     if ($newsetting->{$entry} ne $oldsetting->{$entry}) {
 3678:                         $r->print(&Apache::loncommon::start_data_table_row()."\n");
 3679:                         $r->print("<td>$lt{$entry}</td>\n");
 3680:                         $r->print("<td>".$oldsetting->{$entry}.' '.$oldsettingtext->{$entry}."</td>\n");
 3681:                         $r->print("<td>".$newsetting->{$entry}.' '.$newsettingtext->{$entry}."</td>\n");
 3682:                         $r->print(&Apache::loncommon::end_data_table_row()."\n");
 3683:                     }
 3684:                 }
 3685:             }
 3686:         }
 3687:         $r->print(&Apache::loncommon::end_data_table().'<br />');
 3688:     } else {
 3689:         $r->print('<h3>'.$lt{'ui'}.'</h3>'.
 3690:                   '<p>'.&mt('No changes made to user information').'</p>');
 3691:     }
 3692:     return;
 3693: }
 3694: 
 3695: sub tool_changes {
 3696:     my ($context,$usertools,$oldaccess,$oldaccesstext,$userenv,$changeHash,
 3697:         $changed,$newaccess,$newaccesstext) = @_;
 3698:     if (!((ref($usertools) eq 'ARRAY') && (ref($oldaccess) eq 'HASH') &&
 3699:           (ref($oldaccesstext) eq 'HASH') && (ref($userenv) eq 'HASH') &&
 3700:           (ref($changeHash) eq 'HASH') && (ref($changed) eq 'HASH') &&
 3701:           (ref($newaccess) eq 'HASH') && (ref($newaccesstext) eq 'HASH'))) {
 3702:         return;
 3703:     }
 3704:     my %reqdisplay = &requestchange_display();
 3705:     if ($context eq 'reqcrsotherdom') {
 3706:         my @options = ('approval','validate','autolimit');
 3707:         my $optregex = join('|',@options);
 3708:         my $cdom = $env{'request.role.domain'};
 3709:         foreach my $tool (@{$usertools}) {
 3710:             $oldaccesstext->{$tool} = &mt("availability set to 'off'");
 3711:             $newaccesstext->{$tool} = $oldaccesstext->{$tool};
 3712:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
 3713:             my ($newop,$limit);
 3714:             if ($env{'form.'.$context.'_'.$tool}) {
 3715:                 $newop = $env{'form.'.$context.'_'.$tool};
 3716:                 if ($newop eq 'autolimit') {
 3717:                     $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
 3718:                     $limit =~ s/\D+//g;
 3719:                     $newop .= '='.$limit;
 3720:                 }
 3721:             }
 3722:             if ($userenv->{$context.'.'.$tool} eq '') {
 3723:                 if ($newop) {
 3724:                     $changed->{$tool}=&tool_admin($tool,$cdom.':'.$newop,
 3725:                                                   $changeHash,$context);
 3726:                     if ($changed->{$tool}) {
 3727:                         if ($newop =~ /^autolimit/) {
 3728:                             if ($limit) {
 3729:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
 3730:                             } else {
 3731:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
 3732:                             }
 3733:                         } else {
 3734:                             $newaccesstext->{$tool} = $reqdisplay{$newop};
 3735:                         }
 3736:                     } else {
 3737:                         $newaccesstext->{$tool} = $oldaccesstext->{$tool};
 3738:                     }
 3739:                 }
 3740:             } else {
 3741:                 my @curr = split(',',$userenv->{$context.'.'.$tool});
 3742:                 my @new;
 3743:                 my $changedoms;
 3744:                 foreach my $req (@curr) {
 3745:                     if ($req =~ /^\Q$cdom\E\:($optregex\=?\d*)$/) {
 3746:                         my $oldop = $1;
 3747:                         if ($oldop =~ /^autolimit=(\d*)/) {
 3748:                             my $limit = $1;
 3749:                             if ($limit) {
 3750:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
 3751:                             } else {
 3752:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
 3753:                             }
 3754:                         } else {
 3755:                             $oldaccesstext->{$tool} = $reqdisplay{$oldop};
 3756:                         }
 3757:                         if ($oldop ne $newop) {
 3758:                             $changedoms = 1;
 3759:                             foreach my $item (@curr) {
 3760:                                 my ($reqdom,$option) = split(':',$item);
 3761:                                 unless ($reqdom eq $cdom) {
 3762:                                     push(@new,$item);
 3763:                                 }
 3764:                             }
 3765:                             if ($newop) {
 3766:                                 push(@new,$cdom.':'.$newop);
 3767:                             }
 3768:                             @new = sort(@new);
 3769:                         }
 3770:                         last;
 3771:                     }
 3772:                 }
 3773:                 if ((!$changedoms) && ($newop)) {
 3774:                     $changedoms = 1;
 3775:                     @new = sort(@curr,$cdom.':'.$newop);
 3776:                 }
 3777:                 if ($changedoms) {
 3778:                     my $newdomstr;
 3779:                     if (@new) {
 3780:                         $newdomstr = join(',',@new);
 3781:                     }
 3782:                     $changed->{$tool}=&tool_admin($tool,$newdomstr,$changeHash,
 3783:                                                   $context);
 3784:                     if ($changed->{$tool}) {
 3785:                         if ($env{'form.'.$context.'_'.$tool}) {
 3786:                             if ($env{'form.'.$context.'_'.$tool} eq 'autolimit') {
 3787:                                 my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
 3788:                                 $limit =~ s/\D+//g;
 3789:                                 if ($limit) {
 3790:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
 3791:                                 } else {
 3792:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
 3793:                                 }
 3794:                             } else {
 3795:                                 $newaccesstext->{$tool} = $reqdisplay{$env{'form.'.$context.'_'.$tool}};
 3796:                             }
 3797:                         } else {
 3798:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
 3799:                         }
 3800:                     }
 3801:                 }
 3802:             }
 3803:         }
 3804:         return;
 3805:     }
 3806:     foreach my $tool (@{$usertools}) {
 3807:         my ($newval,$limit,$envkey);
 3808:         $envkey = $context.'.'.$tool;
 3809:         if ($context eq 'requestcourses') {
 3810:             $newval = $env{'form.crsreq_'.$tool};
 3811:             if ($newval eq 'autolimit') {
 3812:                 $limit = $env{'form.crsreq_'.$tool.'_limit'};
 3813:                 $limit =~ s/\D+//g;
 3814:                 $newval .= '='.$limit;
 3815:             }
 3816:         } elsif ($context eq 'requestauthor') {
 3817:             $newval = $env{'form.'.$context};
 3818:             $envkey = $context;
 3819:         } else {
 3820:             $newval = $env{'form.'.$context.'_'.$tool};
 3821:         }
 3822:         if ($userenv->{$envkey} ne '') {
 3823:             $oldaccess->{$tool} = &mt('custom');
 3824:             if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 3825:                 if ($userenv->{$envkey} =~ /^autolimit=(\d*)$/) {
 3826:                     my $currlimit = $1;
 3827:                     if ($currlimit eq '') {
 3828:                         $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
 3829:                     } else {
 3830:                         $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$currlimit);
 3831:                     }
 3832:                 } elsif ($userenv->{$envkey}) {
 3833:                     $oldaccesstext->{$tool} = $reqdisplay{$userenv->{$envkey}};
 3834:                 } else {
 3835:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
 3836:                 }
 3837:             } else {
 3838:                 if ($userenv->{$envkey}) {
 3839:                     $oldaccesstext->{$tool} = &mt("availability set to 'on'");
 3840:                 } else {
 3841:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
 3842:                 }
 3843:             }
 3844:             $changeHash->{$envkey} = $userenv->{$envkey};
 3845:             if ($env{'form.custom'.$tool} == 1) {
 3846:                 if ($newval ne $userenv->{$envkey}) {
 3847:                     $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
 3848:                                                     $context);
 3849:                     if ($changed->{$tool}) {
 3850:                         $newaccess->{$tool} = &mt('custom');
 3851:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 3852:                             if ($newval =~ /^autolimit/) {
 3853:                                 if ($limit) {
 3854:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
 3855:                                 } else {
 3856:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
 3857:                                 }
 3858:                             } elsif ($newval) {
 3859:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
 3860:                             } else {
 3861:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
 3862:                             }
 3863:                         } else {
 3864:                             if ($newval) {
 3865:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
 3866:                             } else {
 3867:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
 3868:                             }
 3869:                         }
 3870:                     } else {
 3871:                         $newaccess->{$tool} = $oldaccess->{$tool};
 3872:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 3873:                             if ($newval =~ /^autolimit/) {
 3874:                                 if ($limit) {
 3875:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
 3876:                                 } else {
 3877:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
 3878:                                 }
 3879:                             } elsif ($newval) {
 3880:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
 3881:                             } else {
 3882:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
 3883:                             }
 3884:                         } else {
 3885:                             if ($userenv->{$context.'.'.$tool}) {
 3886:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
 3887:                             } else {
 3888:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
 3889:                             }
 3890:                         }
 3891:                     }
 3892:                 } else {
 3893:                     $newaccess->{$tool} = $oldaccess->{$tool};
 3894:                     $newaccesstext->{$tool} = $oldaccesstext->{$tool};
 3895:                 }
 3896:             } else {
 3897:                 $changed->{$tool} = &tool_admin($tool,'',$changeHash,$context);
 3898:                 if ($changed->{$tool}) {
 3899:                     $newaccess->{$tool} = &mt('default');
 3900:                 } else {
 3901:                     $newaccess->{$tool} = $oldaccess->{$tool};
 3902:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 3903:                         if ($newval =~ /^autolimit/) {
 3904:                             if ($limit) {
 3905:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
 3906:                             } else {
 3907:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
 3908:                             }
 3909:                         } elsif ($newval) {
 3910:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
 3911:                         } else {
 3912:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
 3913:                         }
 3914:                     } else {
 3915:                         if ($userenv->{$context.'.'.$tool}) {
 3916:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
 3917:                         } else {
 3918:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
 3919:                         }
 3920:                     }
 3921:                 }
 3922:             }
 3923:         } else {
 3924:             $oldaccess->{$tool} = &mt('default');
 3925:             if ($env{'form.custom'.$tool} == 1) {
 3926:                 $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
 3927:                                                 $context);
 3928:                 if ($changed->{$tool}) {
 3929:                     $newaccess->{$tool} = &mt('custom');
 3930:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 3931:                         if ($newval =~ /^autolimit/) {
 3932:                             if ($limit) {
 3933:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
 3934:                             } else {
 3935:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
 3936:                             }
 3937:                         } elsif ($newval) {
 3938:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
 3939:                         } else {
 3940:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
 3941:                         }
 3942:                     } else {
 3943:                         if ($newval) {
 3944:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
 3945:                         } else {
 3946:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
 3947:                         }
 3948:                     }
 3949:                 } else {
 3950:                     $newaccess->{$tool} = $oldaccess->{$tool};
 3951:                 }
 3952:             } else {
 3953:                 $newaccess->{$tool} = $oldaccess->{$tool};
 3954:             }
 3955:         }
 3956:     }
 3957:     return;
 3958: }
 3959: 
 3960: sub update_roles {
 3961:     my ($r,$context,$showcredits) = @_;
 3962:     my $now=time;
 3963:     my @rolechanges;
 3964:     my %disallowed;
 3965:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
 3966:     foreach my $key (keys(%env)) {
 3967: 	next if (! $env{$key});
 3968:         next if ($key eq 'form.action');
 3969: 	# Revoke roles
 3970: 	if ($key=~/^form\.rev/) {
 3971: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
 3972: # Revoke standard role
 3973: 		my ($scope,$role) = ($1,$2);
 3974: 		my $result =
 3975: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
 3976: 						$env{'form.ccuname'},
 3977: 						$scope,$role,'','',$context);
 3978:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
 3979:                             &mt('Revoking [_1] in [_2]',
 3980:                                 &Apache::lonnet::plaintext($role),
 3981:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
 3982:                                 $result ne "ok").'<br />');
 3983:                 if ($result ne "ok") {
 3984:                     $r->print(&mt('Error: [_1]',$result).'<br />');
 3985:                 }
 3986: 		if ($role eq 'st') {
 3987: 		    my $result = 
 3988:                         &Apache::lonuserutils::classlist_drop($scope,
 3989:                             $env{'form.ccuname'},$env{'form.ccdomain'},
 3990: 			    $now);
 3991:                     $r->print(&Apache::lonhtmlcommon::confirm_success($result));
 3992: 		}
 3993:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
 3994:                     push(@rolechanges,$role);
 3995:                 }
 3996: 	    }
 3997: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}s) {
 3998: # Revoke custom role
 3999:                 my $result = &Apache::lonnet::revokecustomrole(
 4000:                     $env{'form.ccdomain'},$env{'form.ccuname'},$1,$2,$3,$4,'','',$context);
 4001:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
 4002:                             &mt('Revoking custom role [_1] by [_2] in [_3]',
 4003:                                 $4,$3.':'.$2,&Apache::loncommon::show_role_extent($1,$context,'cr')),
 4004:                             $result ne 'ok').'<br />');
 4005:                 if ($result ne "ok") {
 4006:                     $r->print(&mt('Error: [_1]',$result).'<br />');
 4007:                 }
 4008:                 if (!grep(/^cr$/,@rolechanges)) {
 4009:                     push(@rolechanges,'cr');
 4010:                 }
 4011: 	    }
 4012: 	} elsif ($key=~/^form\.del/) {
 4013: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
 4014: # Delete standard role
 4015: 		my ($scope,$role) = ($1,$2);
 4016: 		my $result =
 4017: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
 4018: 						$env{'form.ccuname'},
 4019: 						$scope,$role,$now,0,1,'',
 4020:                                                 $context);
 4021:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
 4022:                             &mt('Deleting [_1] in [_2]',
 4023:                                 &Apache::lonnet::plaintext($role),
 4024:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
 4025:                             $result ne 'ok').'<br />');
 4026:                 if ($result ne "ok") {
 4027:                     $r->print(&mt('Error: [_1]',$result).'<br />');
 4028:                 }
 4029: 
 4030: 		if ($role eq 'st') {
 4031: 		    my $result = 
 4032:                         &Apache::lonuserutils::classlist_drop($scope,
 4033:                             $env{'form.ccuname'},$env{'form.ccdomain'},
 4034: 			    $now);
 4035: 		    $r->print(&Apache::lonhtmlcommon::confirm_success($result));
 4036: 		}
 4037:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
 4038:                     push(@rolechanges,$role);
 4039:                 }
 4040:             }
 4041: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
 4042:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
 4043: # Delete custom role
 4044:                 my $result =
 4045:                     &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
 4046:                         $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
 4047:                         0,1,$context);
 4048:                 $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Deleting custom role [_1] by [_2] in [_3]',
 4049:                       $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
 4050:                       $result ne "ok").'<br />');
 4051:                 if ($result ne "ok") {
 4052:                     $r->print(&mt('Error: [_1]',$result).'<br />');
 4053:                 }
 4054: 
 4055:                 if (!grep(/^cr$/,@rolechanges)) {
 4056:                     push(@rolechanges,'cr');
 4057:                 }
 4058:             }
 4059: 	} elsif ($key=~/^form\.ren/) {
 4060:             my $udom = $env{'form.ccdomain'};
 4061:             my $uname = $env{'form.ccuname'};
 4062: # Re-enable standard role
 4063: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
 4064:                 my $url = $1;
 4065:                 my $role = $2;
 4066:                 my $logmsg;
 4067:                 my $output;
 4068:                 if ($role eq 'st') {
 4069:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
 4070:                         my ($cdom,$cnum,$csec) = ($1,$2,$3);
 4071:                         my $credits;
 4072:                         if ($showcredits) {
 4073:                             my $defaultcredits = 
 4074:                                 &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
 4075:                             $credits = &get_user_credits($defaultcredits,$cdom,$cnum);
 4076:                         }
 4077:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$cdom,$cnum,$csec,$context,$credits);
 4078:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course') || ($result eq 'refused')) {
 4079:                             if ($result eq 'refused' && $logmsg) {
 4080:                                 $output = $logmsg;
 4081:                             } else { 
 4082:                                 $output = &mt('Error: [_1]',$result)."\n";
 4083:                             }
 4084:                         } else {
 4085:                             $output = &Apache::lonhtmlcommon::confirm_success(&mt('Assigning [_1] in [_2] starting [_3]',
 4086:                                         &Apache::lonnet::plaintext($role),
 4087:                                         &Apache::loncommon::show_role_extent($url,$context,'st'),
 4088:                                         &Apache::lonlocal::locallocaltime($now))).'<br />'.$logmsg.'<br />';
 4089:                         }
 4090:                     }
 4091:                 } else {
 4092: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
 4093:                                $env{'form.ccuname'},$url,$role,0,$now,'','',
 4094:                                $context);
 4095:                         $output = &Apache::lonhtmlcommon::confirm_success(&mt('Re-enabling [_1] in [_2]',
 4096:                                         &Apache::lonnet::plaintext($role),
 4097:                                         &Apache::loncommon::show_role_extent($url,$context,$role)),$result ne "ok").'<br />';
 4098:                     if ($result ne "ok") {
 4099:                         $output .= &mt('Error: [_1]',$result).'<br />';
 4100:                     }
 4101:                 }
 4102:                 $r->print($output);
 4103:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
 4104:                     push(@rolechanges,$role);
 4105:                 }
 4106: 	    }
 4107: # Re-enable custom role
 4108: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
 4109:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
 4110:                 my $result = &Apache::lonnet::assigncustomrole(
 4111:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
 4112:                                $url,$rdom,$rnam,$rolename,0,$now,undef,$context);
 4113:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
 4114:                     &mt('Re-enabling custom role [_1] by [_2] in [_3]',
 4115:                         $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
 4116:                     $result ne "ok").'<br />');
 4117:                 if ($result ne "ok") {
 4118:                     $r->print(&mt('Error: [_1]',$result).'<br />');
 4119:                 }
 4120:                 if (!grep(/^cr$/,@rolechanges)) {
 4121:                     push(@rolechanges,'cr');
 4122:                 }
 4123:             }
 4124: 	} elsif ($key=~/^form\.act/) {
 4125:             my $udom = $env{'form.ccdomain'};
 4126:             my $uname = $env{'form.ccuname'};
 4127: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
 4128:                 # Activate a custom role
 4129: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
 4130: 		my $url='/'.$one.'/'.$two;
 4131: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
 4132: 
 4133:                 my $start = ( $env{'form.start_'.$full} ?
 4134:                               $env{'form.start_'.$full} :
 4135:                               $now );
 4136:                 my $end   = ( $env{'form.end_'.$full} ?
 4137:                               $env{'form.end_'.$full} :
 4138:                               0 );
 4139:                                                                                      
 4140:                 # split multiple sections
 4141:                 my %sections = ();
 4142:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
 4143:                 if ($num_sections == 0) {
 4144:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end,$context));
 4145:                 } else {
 4146: 		    my %curr_groups =
 4147: 			&Apache::longroup::coursegroups($one,$two);
 4148:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
 4149:                         if (($sec eq 'none') || ($sec eq 'all') || 
 4150:                             exists($curr_groups{$sec})) {
 4151:                             $disallowed{$sec} = $url;
 4152:                             next;
 4153:                         }
 4154:                         my $securl = $url.'/'.$sec;
 4155: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end,$context));
 4156:                     }
 4157:                 }
 4158:                 if (!grep(/^cr$/,@rolechanges)) {
 4159:                     push(@rolechanges,'cr');
 4160:                 }
 4161: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
 4162: 		# Activate roles for sections with 3 id numbers
 4163: 		# set start, end times, and the url for the class
 4164: 		my ($one,$two,$three)=($1,$2,$3);
 4165: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
 4166: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
 4167: 			      $now );
 4168: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
 4169: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
 4170: 			      0 );
 4171: 		my $url='/'.$one.'/'.$two;
 4172:                 my $type = 'three';
 4173:                 # split multiple sections
 4174:                 my %sections = ();
 4175:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
 4176:                 my $credits;
 4177:                 if ($three eq 'st') {
 4178:                     if ($showcredits) { 
 4179:                         my $defaultcredits = 
 4180:                             &Apache::lonuserutils::get_defaultcredits($one,$two);
 4181:                         $credits = $env{'form.credits_'.$one.'_'.$two.'_'.$three};
 4182:                         $credits =~ s/[^\d\.]//g;
 4183:                         if ($credits eq $defaultcredits) {
 4184:                             undef($credits);
 4185:                         }
 4186:                     }
 4187:                 }
 4188:                 if ($num_sections == 0) {
 4189:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
 4190:                 } else {
 4191:                     my %curr_groups = 
 4192: 			&Apache::longroup::coursegroups($one,$two);
 4193:                     my $emptysec = 0;
 4194:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
 4195:                         $sec =~ s/\W//g;
 4196:                         if ($sec ne '') {
 4197:                             if (($sec eq 'none') || ($sec eq 'all') || 
 4198:                                 exists($curr_groups{$sec})) {
 4199:                                 $disallowed{$sec} = $url;
 4200:                                 next;
 4201:                             }
 4202:                             my $securl = $url.'/'.$sec;
 4203:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec,$context,$credits));
 4204:                         } else {
 4205:                             $emptysec = 1;
 4206:                         }
 4207:                     }
 4208:                     if ($emptysec) {
 4209:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
 4210:                     }
 4211:                 }
 4212:                 if (!grep(/^\Q$three\E$/,@rolechanges)) {
 4213:                     push(@rolechanges,$three);
 4214:                 }
 4215: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
 4216: 		# Activate roles for sections with two id numbers
 4217: 		# set start, end times, and the url for the class
 4218: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
 4219: 			      $env{'form.start_'.$1.'_'.$2} : 
 4220: 			      $now );
 4221: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
 4222: 			      $env{'form.end_'.$1.'_'.$2} :
 4223: 			      0 );
 4224:                 my $one = $1;
 4225:                 my $two = $2;
 4226: 		my $url='/'.$one.'/';
 4227:                 # split multiple sections
 4228:                 my %sections = ();
 4229:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two},\%sections,$two);
 4230:                 if ($num_sections == 0) {
 4231:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
 4232:                 } else {
 4233:                     my $emptysec = 0;
 4234:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
 4235:                         if ($sec ne '') {
 4236:                             my $securl = $url.'/'.$sec;
 4237:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$two,$start,$end,$one,undef,$sec,$context));
 4238:                         } else {
 4239:                             $emptysec = 1;
 4240:                         }
 4241:                     }
 4242:                     if ($emptysec) {
 4243:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
 4244:                     }
 4245:                 }
 4246:                 if (!grep(/^\Q$two\E$/,@rolechanges)) {
 4247:                     push(@rolechanges,$two);
 4248:                 }
 4249: 	    } else {
 4250: 		$r->print('<p><span class="LC_error">'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></span></p><br />');
 4251:             }
 4252:             foreach my $key (sort(keys(%disallowed))) {
 4253:                 $r->print('<p class="LC_warning">');
 4254:                 if (($key eq 'none') || ($key eq 'all')) {  
 4255:                     $r->print(&mt('[_1] may not be used as the name for a section, as it is a reserved word.','<tt>'.$key.'</tt>'));
 4256:                 } else {
 4257:                     $r->print(&mt('[_1] may not be used as the name for a section, as it is the name of a course group.','<tt>'.$key.'</tt>'));
 4258:                 }
 4259:                 $r->print('</p><p>'
 4260:                          .&mt('Please [_1]go back[_2] and choose a different section name.'
 4261:                              ,'<a href="javascript:history.go(-1)'
 4262:                              ,'</a>')
 4263:                          .'</p><br />'
 4264:                 );
 4265:             }
 4266: 	}
 4267:     } # End of foreach (keys(%env))
 4268: # Flush the course logs so reverse user roles immediately updated
 4269:     $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
 4270:     if (@rolechanges == 0) {
 4271:         $r->print('<p>'.&mt('No roles to modify').'</p>');
 4272:     }
 4273:     return @rolechanges;
 4274: }
 4275: 
 4276: sub get_user_credits {
 4277:     my ($uname,$udom,$defaultcredits,$cdom,$cnum) = @_;
 4278:     if ($cdom eq '' || $cnum eq '') {
 4279:         return unless ($env{'request.course.id'});
 4280:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 4281:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 4282:     }
 4283:     my $credits;
 4284:     my %currhash =
 4285:         &Apache::lonnet::get('classlist',[$uname.':'.$udom],$cdom,$cnum);
 4286:     if (keys(%currhash) > 0) {
 4287:         my @items = split(/:/,$currhash{$uname.':'.$udom});
 4288:         my $crdidx = &Apache::loncoursedata::CL_CREDITS() - 3;
 4289:         $credits = $items[$crdidx];
 4290:         $credits =~ s/[^\d\.]//g;
 4291:     }
 4292:     if ($credits eq $defaultcredits) {
 4293:         undef($credits);
 4294:     }
 4295:     return $credits;
 4296: }
 4297: 
 4298: sub enroll_single_student {
 4299:     my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype,
 4300:         $showcredits,$defaultcredits) = @_;
 4301:     $r->print('<h3>');
 4302:     if ($crstype eq 'Community') {
 4303:         $r->print(&mt('Enrolling Member'));
 4304:     } else {
 4305:         $r->print(&mt('Enrolling Student'));
 4306:     }
 4307:     $r->print('</h3>');
 4308: 
 4309:     # Remove non alphanumeric values from section
 4310:     $env{'form.sections'}=~s/\W//g;
 4311: 
 4312:     my $credits;
 4313:     if (($showcredits) && ($env{'form.credits'} ne '')) {
 4314:         $credits = $env{'form.credits'};
 4315:         $credits =~ s/[^\d\.]//g;
 4316:         if ($credits ne '') {
 4317:             if ($credits eq $defaultcredits) {
 4318:                 undef($credits);
 4319:             }
 4320:         }
 4321:     }
 4322: 
 4323:     # Clean out any old student roles the user has in this class.
 4324:     &Apache::lonuserutils::modifystudent($env{'form.ccdomain'},
 4325:          $env{'form.ccuname'},$env{'request.course.id'},undef,$uhome);
 4326:     my ($startdate,$enddate) = &Apache::lonuserutils::get_dates_from_form();
 4327:     my $enroll_result =
 4328:         &Apache::lonnet::modify_student_enrollment($env{'form.ccdomain'},
 4329:             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
 4330:             $env{'form.cmiddlename'},$env{'form.clastname'},
 4331:             $env{'form.generation'},$env{'form.sections'},$enddate,
 4332:             $startdate,'manual',undef,$env{'request.course.id'},'',$context,
 4333:             $credits);
 4334:     if ($enroll_result =~ /^ok/) {
 4335:         $r->print(&mt('[_1] enrolled','<b>'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.'</b>'));
 4336:         if ($env{'form.sections'} ne '') {
 4337:             $r->print(' '.&mt('in section [_1]',$env{'form.sections'}));
 4338:         }
 4339:         my ($showstart,$showend);
 4340:         if ($startdate <= $now) {
 4341:             $showstart = &mt('Access starts immediately');
 4342:         } else {
 4343:             $showstart = &mt('Access starts: ').&Apache::lonlocal::locallocaltime($startdate);
 4344:         }
 4345:         if ($enddate == 0) {
 4346:             $showend = &mt('ends: no ending date');
 4347:         } else {
 4348:             $showend = &mt('ends: ').&Apache::lonlocal::locallocaltime($enddate);
 4349:         }
 4350:         $r->print('.<br />'.$showstart.'; '.$showend);
 4351:         if ($startdate <= $now && !$newuser) {
 4352:             $r->print('<p class="LC_info">');
 4353:             if ($crstype eq 'Community') {
 4354:                 $r->print(&mt('If the member is currently logged-in to LON-CAPA, the new role can be displayed by using the "Check for changes" link on the Roles/Courses page.'));
 4355:             } else {
 4356:                 $r->print(&mt('If the student is currently logged-in to LON-CAPA, the new role can be displayed by using the "Check for changes" link on the Roles/Courses page.'));
 4357:            }
 4358:            $r->print('</p>');
 4359:         }
 4360:     } else {
 4361:         $r->print(&mt('unable to enroll').": ".$enroll_result);
 4362:     }
 4363:     return;
 4364: }
 4365: 
 4366: sub get_defaultquota_text {
 4367:     my ($settingstatus) = @_;
 4368:     my $defquotatext; 
 4369:     if ($settingstatus eq '') {
 4370:         $defquotatext = &mt('default');
 4371:     } else {
 4372:         my ($usertypes,$order) =
 4373:             &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
 4374:         if ($usertypes->{$settingstatus} eq '') {
 4375:             $defquotatext = &mt('default');
 4376:         } else {
 4377:             $defquotatext = &mt('default for [_1]',$usertypes->{$settingstatus});
 4378:         }
 4379:     }
 4380:     return $defquotatext;
 4381: }
 4382: 
 4383: sub update_result_form {
 4384:     my ($uhome) = @_;
 4385:     my $outcome = 
 4386:     '<form name="userupdate" method="post" action="">'."\n";
 4387:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
 4388:         $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
 4389:     }
 4390:     if ($env{'form.origname'} ne '') {
 4391:         $outcome .= '<input type="hidden" name="origname" value="'.$env{'form.origname'}.'" />'."\n";
 4392:     }
 4393:     foreach my $item ('sortby','seluname','seludom') {
 4394:         if (exists($env{'form.'.$item})) {
 4395:             $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
 4396:         }
 4397:     }
 4398:     if ($uhome eq 'no_host') {
 4399:         $outcome .= '<input type="hidden" name="forcenewuser" value="1" />'."\n";
 4400:     }
 4401:     $outcome .= '<input type="hidden" name="phase" value="" />'."\n".
 4402:                 '<input type="hidden" name="currstate" value="" />'."\n".
 4403:                 '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
 4404:                 '</form>';
 4405:     return $outcome;
 4406: }
 4407: 
 4408: sub quota_admin {
 4409:     my ($setquota,$changeHash,$name) = @_;
 4410:     my $quotachanged;
 4411:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
 4412:         # Current user has quota modification privileges
 4413:         if (ref($changeHash) eq 'HASH') {
 4414:             $quotachanged = 1;
 4415:             $changeHash->{$name.'quota'} = $setquota;
 4416:         }
 4417:     }
 4418:     return $quotachanged;
 4419: }
 4420: 
 4421: sub tool_admin {
 4422:     my ($tool,$settool,$changeHash,$context) = @_;
 4423:     my $canchange = 0; 
 4424:     if ($context eq 'requestcourses') {
 4425:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
 4426:             $canchange = 1;
 4427:         }
 4428:     } elsif ($context eq 'reqcrsotherdom') {
 4429:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
 4430:             $canchange = 1;
 4431:         }
 4432:     } elsif ($context eq 'requestauthor') {
 4433:         if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
 4434:             $canchange = 1;
 4435:         }
 4436:     } elsif (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
 4437:         # Current user has quota modification privileges
 4438:         $canchange = 1;
 4439:     }
 4440:     my $toolchanged;
 4441:     if ($canchange) {
 4442:         if (ref($changeHash) eq 'HASH') {
 4443:             $toolchanged = 1;
 4444:             if ($tool eq 'requestauthor') {
 4445:                 $changeHash->{$context} = $settool;
 4446:             } else {
 4447:                 $changeHash->{$context.'.'.$tool} = $settool;
 4448:             }
 4449:         }
 4450:     }
 4451:     return $toolchanged;
 4452: }
 4453: 
 4454: sub build_roles {
 4455:     my ($sectionstr,$sections,$role) = @_;
 4456:     my $num_sections = 0;
 4457:     if ($sectionstr=~ /,/) {
 4458:         my @secnums = split/,/,$sectionstr;
 4459:         if ($role eq 'st') {
 4460:             $secnums[0] =~ s/\W//g;
 4461:             $$sections{$secnums[0]} = 1;
 4462:             $num_sections = 1;
 4463:         } else {
 4464:             foreach my $sec (@secnums) {
 4465:                 $sec =~ ~s/\W//g;
 4466:                 if (!($sec eq "")) {
 4467:                     if (exists($$sections{$sec})) {
 4468:                         $$sections{$sec} ++;
 4469:                     } else {
 4470:                         $$sections{$sec} = 1;
 4471:                         $num_sections ++;
 4472:                     }
 4473:                 }
 4474:             }
 4475:         }
 4476:     } else {
 4477:         $sectionstr=~s/\W//g;
 4478:         unless ($sectionstr eq '') {
 4479:             $$sections{$sectionstr} = 1;
 4480:             $num_sections ++;
 4481:         }
 4482:     }
 4483: 
 4484:     return $num_sections;
 4485: }
 4486: 
 4487: # ========================================================== Custom Role Editor
 4488: 
 4489: sub custom_role_editor {
 4490:     my ($r,$context,$brcrum,$prefix,$permission) = @_;
 4491:     my $action = $env{'form.customroleaction'};
 4492:     my ($rolename,$helpitem);
 4493:     if ($action eq 'new') {
 4494:         $rolename=$env{'form.newrolename'};
 4495:     } else {
 4496:         $rolename=$env{'form.rolename'};
 4497:     }
 4498: 
 4499:     my ($crstype,$context);
 4500:     if ($env{'request.course.id'}) {
 4501:         $crstype = &Apache::loncommon::course_type();
 4502:         $context = 'course';
 4503:         $helpitem = 'Course_Editing_Custom_Roles';
 4504:     } else {
 4505:         $context = 'domain';
 4506:         $crstype = 'course';
 4507:         $helpitem = 'Domain_Editing_Custom_Roles';
 4508:     }
 4509: 
 4510:     $rolename=~s/[^A-Za-z0-9]//gs;
 4511:     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
 4512: 	&print_username_entry_form($r,$context,undef,undef,undef,$crstype,$brcrum,
 4513:                                    $permission);
 4514:         return;
 4515:     }
 4516: 
 4517:     my $formname = 'form1';
 4518:     my %privs=();
 4519:     my $body_top = '<h2>';
 4520: # ------------------------------------------------------- Does this role exist?
 4521:     my ($rdummy,$roledef)=
 4522: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
 4523:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 4524:         $body_top .= &mt('Existing Role').' "';
 4525: # ------------------------------------------------- Get current role privileges
 4526:         ($privs{'system'},$privs{'domain'},$privs{'course'})=split(/\_/,$roledef);
 4527:         if ($privs{'system'} =~ /bre\&S/) {
 4528:             if ($context eq 'domain') {
 4529:                 $crstype = 'Course';
 4530:             } elsif ($crstype eq 'Community') {
 4531:                 $privs{'system'} =~ s/bre\&S//;
 4532:             }
 4533:         } elsif ($context eq 'domain') {
 4534:             $crstype = 'Course';
 4535:         }
 4536:     } else {
 4537:         $body_top .= &mt('New Role').' "';
 4538:         $roledef='';
 4539:     }
 4540:     $body_top .= $rolename.'"</h2>';
 4541: 
 4542: # ------------------------------------------------------- What can be assigned?
 4543:     my %full=();
 4544:     my %levels=(
 4545:                  course => {},
 4546:                  domain => {},
 4547:                  system => {},
 4548:                );
 4549:     my %levelscurrent=(
 4550:                         course => {},
 4551:                         domain => {},
 4552:                         system => {},
 4553:                       );
 4554:     &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4555:     my ($jsback,$elements) = &crumb_utilities();
 4556:     my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4557:     my $head_script =
 4558:         &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,
 4559:                                                   \%full,\@templateroles,$jsback);
 4560:     push (@{$brcrum},
 4561:               {href => "javascript:backPage(document.$formname,'pickrole','')",
 4562:                text => "Pick custom role",
 4563:                faq  => 282,bug=>'Instructor Interface',},
 4564:               {href => "javascript:backPage(document.$formname,'','')",
 4565:                text => "Edit custom role",
 4566:                faq  => 282,
 4567:                bug  => 'Instructor Interface',
 4568:                help => $helpitem}
 4569:               );
 4570:     my $args = { bread_crumbs          => $brcrum,
 4571:                  bread_crumbs_component => 'User Management'};
 4572:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',
 4573:                                              $head_script,$args).
 4574:               $body_top);
 4575:     $r->print('<form name="'.$formname.'" method="post" action="">'."\n".
 4576:               &Apache::lonuserutils::custom_role_header($context,$crstype,
 4577:                                                         \@templateroles,$prefix));
 4578: 
 4579:     $r->print(<<ENDCCF);
 4580: <input type="hidden" name="phase" value="set_custom_roles" />
 4581: <input type="hidden" name="rolename" value="$rolename" />
 4582: ENDCCF
 4583:     $r->print(&Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4584:                                                        \%levelscurrent,$prefix));
 4585:     $r->print(&Apache::loncommon::end_data_table().
 4586:    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
 4587:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
 4588:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".
 4589:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
 4590:    '<input type="submit" value="'.&mt('Save').'" /></form>');
 4591: }
 4592: 
 4593: # ---------------------------------------------------------- Call to definerole
 4594: sub set_custom_role {
 4595:     my ($r,$context,$brcrum,$prefix,$permission) = @_;
 4596:     my $rolename=$env{'form.rolename'};
 4597:     $rolename=~s/[^A-Za-z0-9]//gs;
 4598:     if (!$rolename) {
 4599: 	&custom_role_editor($r,$context,$brcrum,$prefix,$permission);
 4600:         return;
 4601:     }
 4602:     my ($jsback,$elements) = &crumb_utilities();
 4603:     my $jscript = '<script type="text/javascript">'
 4604:                  .'// <![CDATA['."\n"
 4605:                  .$jsback."\n"
 4606:                  .'// ]]>'."\n"
 4607:                  .'</script>'."\n";
 4608:     my $helpitem = 'Course_Editing_Custom_Roles';
 4609:     if ($context eq 'domain') {
 4610:         $helpitem = 'Domain_Editing_Custom_Roles';
 4611:     }
 4612:     push(@{$brcrum},
 4613:         {href => "javascript:backPage(document.customresult,'pickrole','')",
 4614:          text => "Pick custom role",
 4615:          faq  => 282,
 4616:          bug  => 'Instructor Interface',},
 4617:         {href => "javascript:backPage(document.customresult,'selected_custom_edit','')",
 4618:          text => "Edit custom role",
 4619:          faq  => 282,
 4620:          bug  => 'Instructor Interface',},
 4621:         {href => "javascript:backPage(document.customresult,'set_custom_roles','')",
 4622:          text => "Result",
 4623:          faq  => 282,
 4624:          bug  => 'Instructor Interface',
 4625:          help => $helpitem,}
 4626:         );
 4627:     my $args = { bread_crumbs           => $brcrum,
 4628:                  bread_crumbs_component => 'User Management'};
 4629:     $r->print(&Apache::loncommon::start_page('Save Custom Role',$jscript,$args));
 4630: 
 4631:     my $newrole;
 4632:     my ($rdummy,$roledef)=
 4633: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
 4634: 
 4635: # ------------------------------------------------------- Does this role exist?
 4636:     $r->print('<h3>');
 4637:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 4638: 	$r->print(&mt('Existing Role').' "');
 4639:     } else {
 4640: 	$r->print(&mt('New Role').' "');
 4641: 	$roledef='';
 4642:         $newrole = 1;
 4643:     }
 4644:     $r->print($rolename.'"</h3>');
 4645: # ------------------------------------------------- Assign role and show result
 4646: 
 4647:     my $errmsg;
 4648:     my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$prefix);
 4649:     # Assign role and return result
 4650:     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
 4651:                                              $newprivs{'c'});
 4652:     if ($result ne 'ok') {
 4653:         $errmsg = ': '.$result;
 4654:     }
 4655:     my $message =
 4656:         &Apache::lonhtmlcommon::confirm_success(
 4657:             &mt('Defining Role').$errmsg, ($result eq 'ok' ? 0 : 1));
 4658:     if ($env{'request.course.id'}) {
 4659:         my $url='/'.$env{'request.course.id'};
 4660:         $url=~s/\_/\//g;
 4661:         $result =
 4662:             &Apache::lonnet::assigncustomrole(
 4663:                 $env{'user.domain'},$env{'user.name'},
 4664:                 $url,
 4665:                 $env{'user.domain'},$env{'user.name'},
 4666:                 $rolename,undef,undef,undef,$context);
 4667:         if ($result ne 'ok') {
 4668:             $errmsg = ': '.$result;
 4669:         }
 4670:         $message .=
 4671:             '<br />'
 4672:            .&Apache::lonhtmlcommon::confirm_success(
 4673:                 &mt('Assigning Role to Self').$errmsg, ($result eq 'ok' ? 0 : 1));
 4674:     }
 4675:     $r->print(
 4676:         &Apache::loncommon::confirmwrapper($message)
 4677:        .'<br />'
 4678:        .&Apache::lonhtmlcommon::actionbox([
 4679:             '<a href="javascript:backPage(document.customresult,'."'pickrole'".')">'
 4680:            .&mt('Create or edit another custom role')
 4681:            .'</a>'])
 4682:        .'<form name="customresult" method="post" action="">'
 4683:        .&Apache::lonhtmlcommon::echo_form_input([])
 4684:        .'</form>'
 4685:     );
 4686: }
 4687: 
 4688: # ================================================================ Main Handler
 4689: sub handler {
 4690:     my $r = shift;
 4691:     if ($r->header_only) {
 4692:        &Apache::loncommon::content_type($r,'text/html');
 4693:        $r->send_http_header;
 4694:        return OK;
 4695:     }
 4696:     my ($context,$crstype,$cid,$cnum,$cdom,$allhelpitems);
 4697: 
 4698:     if ($env{'request.course.id'}) {
 4699:         $context = 'course';
 4700:         $crstype = &Apache::loncommon::course_type();
 4701:     } elsif ($env{'request.role'} =~ /^au\./) {
 4702:         $context = 'author';
 4703:     } else {
 4704:         $context = 'domain';
 4705:     }
 4706: 
 4707:     my ($permission,$allowed) =
 4708:         &Apache::lonuserutils::get_permission($context,$crstype);
 4709: 
 4710:     if ($allowed) {
 4711:         my @allhelp;
 4712:         if ($context eq 'course') {
 4713:             $cid = $env{'request.course.id'};
 4714:             $cdom = $env{'course.'.$cid.'.domain'};
 4715:             $cnum = $env{'course.'.$cid.'.num'};
 4716: 
 4717:             if ($permission->{'cusr'}) {
 4718:                 push(@allhelp,'Course_Create_Class_List');
 4719:             }
 4720:             if ($permission->{'view'} || $permission->{'cusr'}) {
 4721:                 push(@allhelp,('Course_Change_Privileges','Course_View_Class_List'));
 4722:             }
 4723:             if ($permission->{'custom'}) {
 4724:                 push(@allhelp,'Course_Editing_Custom_Roles');
 4725:             }
 4726:             if ($permission->{'cusr'}) {
 4727:                 push(@allhelp,('Course_Add_Student','Course_Drop_Student'));
 4728:             }
 4729:             unless ($permission->{'cusr_section'}) {
 4730:                 if (&Apache::lonnet::auto_run($cnum,$cdom) && (($permission->{'cusr'}) || ($permission->{'view'}))) {
 4731:                     push(@allhelp,'Course_Automated_Enrollment');
 4732:                 }
 4733:                 if ($permission->{'selfenrolladmin'}) {
 4734:                     push(@allhelp,'Course_Approve_Selfenroll');
 4735:                 }
 4736:             }
 4737:             if ($permission->{'grp_manage'}) {
 4738:                 push(@allhelp,'Course_Manage_Group');
 4739:             }
 4740:             if ($permission->{'view'} || $permission->{'cusr'}) {
 4741:                 push(@allhelp,'Course_User_Logs');
 4742:             }
 4743:         } elsif ($context eq 'author') {
 4744:             push(@allhelp,('Author_Change_Privileges','Author_Create_Coauthor_List',
 4745:                            'Author_View_Coauthor_List','Author_User_Logs'));
 4746:         } else {
 4747:             if ($permission->{'cusr'}) {
 4748:                 push(@allhelp,'Domain_Change_Privileges');
 4749:                 if ($permission->{'activity'}) {
 4750:                     push(@allhelp,'Domain_User_Access_Logs');
 4751:                 }
 4752:                 push(@allhelp,('Domain_Create_Users','Domain_View_Users_List'));
 4753:                 if ($permission->{'custom'}) {
 4754:                     push(@allhelp,'Domain_Editing_Custom_Roles');
 4755:                 }
 4756:                 push(@allhelp,('Domain_Role_Approvals','Domain_Username_Approvals','Domain_Change_Logs'));
 4757:             } elsif ($permission->{'view'}) {
 4758:                 push(@allhelp,'Domain_View_Privileges');
 4759:                 if ($permission->{'activity'}) {
 4760:                     push(@allhelp,'Domain_User_Access_Logs');
 4761:                 }
 4762:                 push(@allhelp,('Domain_View_Users_List','Domain_Change_Logs'));
 4763:             }
 4764:         }
 4765:         if (@allhelp) {
 4766:             $allhelpitems = join(',',@allhelp);
 4767:         }
 4768:     }
 4769: 
 4770:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 4771:         ['action','state','callingform','roletype','showrole','bulkaction','popup','phase',
 4772:          'username','domain','srchterm','srchdomain','srchin','srchby','srchtype','queue']);
 4773:     &Apache::lonhtmlcommon::clear_breadcrumbs();
 4774:     my $args;
 4775:     my $brcrum = [];
 4776:     my $bread_crumbs_component = 'User Management';
 4777:     if (($env{'form.action'} ne 'dateselect') && ($env{'form.action'} ne 'displayuserreq')) {
 4778:         $brcrum = [{href=>"/adm/createuser",
 4779:                     text=>"User Management",
 4780:                     help=>$allhelpitems}
 4781:                   ];
 4782:     }
 4783:     if (!$allowed) {
 4784:         if ($context eq 'course') {
 4785:             $r->internal_redirect('/adm/viewclasslist');
 4786:             return OK;
 4787:         }
 4788:         $env{'user.error.msg'}=
 4789:             "/adm/createuser:cst:0:0:Cannot create/modify user data ".
 4790:                                  "or view user status.";
 4791:         return HTTP_NOT_ACCEPTABLE;
 4792:     }
 4793: 
 4794:     &Apache::loncommon::content_type($r,'text/html');
 4795:     $r->send_http_header;
 4796: 
 4797:     my $showcredits;
 4798:     if ((($context eq 'course') && ($crstype eq 'Course')) || 
 4799:          ($context eq 'domain')) {
 4800:         my %domdefaults = 
 4801:             &Apache::lonnet::get_domain_defaults($env{'request.role.domain'});
 4802:         if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'}) {
 4803:             $showcredits = 1;
 4804:         }
 4805:     }
 4806: 
 4807:     # Main switch on form.action and form.state, as appropriate
 4808:     if (! exists($env{'form.action'})) {
 4809:         $args = {bread_crumbs => $brcrum,
 4810:                  bread_crumbs_component => $bread_crumbs_component}; 
 4811:         $r->print(&header(undef,$args));
 4812:         $r->print(&print_main_menu($permission,$context,$crstype));
 4813:     } elsif ($env{'form.action'} eq 'upload' && $permission->{'cusr'}) {
 4814:         my $helpitem = 'Course_Create_Class_List';
 4815:         if ($context eq 'author') {
 4816:             $helpitem = 'Author_Create_Coauthor_List';
 4817:         } elsif ($context eq 'domain') {
 4818:             $helpitem = 'Domain_Create_Users';
 4819:         }
 4820:         push(@{$brcrum},
 4821:               { href => '/adm/createuser?action=upload&state=',
 4822:                 text => 'Upload Users List',
 4823:                 help => $helpitem,
 4824:               });
 4825:         $bread_crumbs_component = 'Upload Users List';
 4826:         $args = {bread_crumbs           => $brcrum,
 4827:                  bread_crumbs_component => $bread_crumbs_component};
 4828:         $r->print(&header(undef,$args));
 4829:         $r->print('<form name="studentform" method="post" '.
 4830:                   'enctype="multipart/form-data" '.
 4831:                   ' action="/adm/createuser">'."\n");
 4832:         if (! exists($env{'form.state'})) {
 4833:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
 4834:         } elsif ($env{'form.state'} eq 'got_file') {
 4835:             my $result = 
 4836:                 &Apache::lonuserutils::print_upload_manager_form($r,$context,
 4837:                                                                  $permission,
 4838:                                                                  $crstype,$showcredits);
 4839:             if ($result eq 'missingdata') {
 4840:                 delete($env{'form.state'});
 4841:                 &Apache::lonuserutils::print_first_users_upload_form($r,$context);
 4842:             }
 4843:         } elsif ($env{'form.state'} eq 'enrolling') {
 4844:             if ($env{'form.datatoken'}) {
 4845:                 my $result = &Apache::lonuserutils::upfile_drop_add($r,$context,
 4846:                                                                     $permission,
 4847:                                                                     $showcredits);
 4848:                 if ($result eq 'missingdata') {
 4849:                     delete($env{'form.state'});
 4850:                     &Apache::lonuserutils::print_first_users_upload_form($r,$context);
 4851:                 } elsif ($result eq 'invalidhome') {
 4852:                     $env{'form.state'} = 'got_file';
 4853:                     delete($env{'form.lcserver'});
 4854:                     my $result =
 4855:                         &Apache::lonuserutils::print_upload_manager_form($r,$context,$permission,
 4856:                                                                          $crstype,$showcredits);
 4857:                     if ($result eq 'missingdata') {
 4858:                         delete($env{'form.state'});
 4859:                         &Apache::lonuserutils::print_first_users_upload_form($r,$context);
 4860:                     }
 4861:                 }
 4862:             } else {
 4863:                 delete($env{'form.state'});
 4864:                 &Apache::lonuserutils::print_first_users_upload_form($r,$context);
 4865:             }
 4866:         } else {
 4867:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
 4868:         }
 4869:         $r->print('</form>');
 4870:     } elsif (((($env{'form.action'} eq 'singleuser') || ($env{'form.action'}
 4871:               eq 'singlestudent')) && ($permission->{'cusr'})) ||
 4872:              (($env{'form.action'} eq 'singleuser') && ($permission->{'view'})) ||
 4873:              (($env{'form.action'} eq 'accesslogs') && ($permission->{'activity'}))) {
 4874:         my $phase = $env{'form.phase'};
 4875:         my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
 4876: 	&Apache::loncreateuser::restore_prev_selections();
 4877: 	my $srch;
 4878: 	foreach my $item (@search) {
 4879: 	    $srch->{$item} = $env{'form.'.$item};
 4880: 	}
 4881:         if (($phase eq 'get_user_info') || ($phase eq 'userpicked') ||
 4882:             ($phase eq 'createnewuser') || ($phase eq 'activity')) {
 4883:             if ($env{'form.phase'} eq 'createnewuser') {
 4884:                 my $response;
 4885:                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
 4886:                     my $response =
 4887:                         '<span class="LC_warning">'
 4888:                        .&mt('You must specify a valid username. Only the following are allowed:'
 4889:                            .' letters numbers - . @')
 4890:                        .'</span>';
 4891:                     $env{'form.phase'} = '';
 4892:                     &print_username_entry_form($r,$context,$response,$srch,undef,
 4893:                                                $crstype,$brcrum,$permission);
 4894:                 } else {
 4895:                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
 4896:                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
 4897:                     &print_user_modification_page($r,$ccuname,$ccdomain,
 4898:                                                   $srch,$response,$context,
 4899:                                                   $permission,$crstype,$brcrum,
 4900:                                                   $showcredits);
 4901:                 }
 4902:             } elsif ($env{'form.phase'} eq 'get_user_info') {
 4903:                 my ($currstate,$response,$forcenewuser,$results) = 
 4904:                     &user_search_result($context,$srch);
 4905:                 if ($env{'form.currstate'} eq 'modify') {
 4906:                     $currstate = $env{'form.currstate'};
 4907:                 }
 4908:                 if ($currstate eq 'select') {
 4909:                     &print_user_selection_page($r,$response,$srch,$results,
 4910:                                                \@search,$context,undef,$crstype,
 4911:                                                $brcrum);
 4912:                 } elsif (($currstate eq 'modify') || ($env{'form.action'} eq 'accesslogs')) {
 4913:                     my ($ccuname,$ccdomain,$uhome);
 4914:                     if (($srch->{'srchby'} eq 'uname') && 
 4915:                         ($srch->{'srchtype'} eq 'exact')) {
 4916:                         $ccuname = $srch->{'srchterm'};
 4917:                         $ccdomain= $srch->{'srchdomain'};
 4918:                     } else {
 4919:                         my @matchedunames = keys(%{$results});
 4920:                         ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
 4921:                     }
 4922:                     $ccuname =&LONCAPA::clean_username($ccuname);
 4923:                     $ccdomain=&LONCAPA::clean_domain($ccdomain);
 4924:                     if ($env{'form.action'} eq 'accesslogs') {
 4925:                         my $uhome;
 4926:                         if (($ccuname ne '') && ($ccdomain ne '')) {
 4927:                            $uhome = &Apache::lonnet::homeserver($ccuname,$ccdomain);
 4928:                         }
 4929:                         if (($uhome eq '') || ($uhome eq 'no_host')) {
 4930:                             $env{'form.phase'} = '';
 4931:                             undef($forcenewuser);
 4932:                             #if ($response) {
 4933:                             #    unless ($response =~ m{\Q<br /><br />\E$}) {
 4934:                             #        $response .= '<br /><br />';
 4935:                             #    }
 4936:                             #}
 4937:                             &print_username_entry_form($r,$context,$response,$srch,
 4938:                                                        $forcenewuser,$crstype,$brcrum,
 4939:                                                        $permission);
 4940:                         } else {
 4941:                             &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
 4942:                         }
 4943:                     } else {
 4944:                         if ($env{'form.forcenewuser'}) {
 4945:                             $response = '';
 4946:                         }
 4947:                         &print_user_modification_page($r,$ccuname,$ccdomain,
 4948:                                                       $srch,$response,$context,
 4949:                                                       $permission,$crstype,$brcrum);
 4950:                     }
 4951:                 } elsif ($currstate eq 'query') {
 4952:                     &print_user_query_page($r,'createuser',$brcrum);
 4953:                 } else {
 4954:                     $env{'form.phase'} = '';
 4955:                     &print_username_entry_form($r,$context,$response,$srch,
 4956:                                                $forcenewuser,$crstype,$brcrum,
 4957:                                                $permission);
 4958:                 }
 4959:             } elsif ($env{'form.phase'} eq 'userpicked') {
 4960:                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
 4961:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
 4962:                 if ($env{'form.action'} eq 'accesslogs') {
 4963:                     &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
 4964:                 } else {
 4965:                     &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
 4966:                                                   $context,$permission,$crstype,
 4967:                                                   $brcrum);
 4968:                 }
 4969:             } elsif ($env{'form.action'} eq 'accesslogs') {
 4970:                 my $ccuname = &LONCAPA::clean_username($env{'form.accessuname'});
 4971:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.accessudom'});
 4972:                 &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
 4973:             }
 4974:         } elsif ($env{'form.phase'} eq 'update_user_data') {
 4975:             &update_user_data($r,$context,$crstype,$brcrum,$showcredits,$permission);
 4976:         } else {
 4977:             &print_username_entry_form($r,$context,undef,$srch,undef,$crstype,
 4978:                                        $brcrum,$permission);
 4979:         }
 4980:     } elsif ($env{'form.action'} eq 'custom' && $permission->{'custom'}) {
 4981:         my $prefix;
 4982:         if ($env{'form.phase'} eq 'set_custom_roles') {
 4983:             &set_custom_role($r,$context,$brcrum,$prefix,$permission);
 4984:         } else {
 4985:             &custom_role_editor($r,$context,$brcrum,$prefix,$permission);
 4986:         }
 4987:     } elsif (($env{'form.action'} eq 'processauthorreq') &&
 4988:              ($permission->{'cusr'}) && 
 4989:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
 4990:         push(@{$brcrum},
 4991:                  {href => '/adm/createuser?action=processauthorreq',
 4992:                   text => 'Authoring Space requests',
 4993:                   help => 'Domain_Role_Approvals'});
 4994:         $bread_crumbs_component = 'Authoring requests';
 4995:         if ($env{'form.state'} eq 'done') {
 4996:             push(@{$brcrum},
 4997:                      {href => '/adm/createuser?action=authorreqqueue',
 4998:                       text => 'Result',
 4999:                       help => 'Domain_Role_Approvals'});
 5000:             $bread_crumbs_component = 'Authoring request result';
 5001:         }
 5002:         $args = { bread_crumbs           => $brcrum,
 5003:                   bread_crumbs_component => $bread_crumbs_component};
 5004:         my $js = &usernamerequest_javascript();
 5005:         $r->print(&header(&add_script($js),$args));
 5006:         if (!exists($env{'form.state'})) {
 5007:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestauthor',
 5008:                                                                             $env{'request.role.domain'}));
 5009:         } elsif ($env{'form.state'} eq 'done') {
 5010:             $r->print('<h3>'.&mt('Authoring request processing').'</h3>'."\n");
 5011:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestauthor',
 5012:                                                                          $env{'request.role.domain'}));
 5013:         }
 5014:     } elsif (($env{'form.action'} eq 'processusernamereq') &&
 5015:              ($permission->{'cusr'}) &&
 5016:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
 5017:         push(@{$brcrum},
 5018:                  {href => '/adm/createuser?action=processusernamereq',
 5019:                   text => 'LON-CAPA account requests',
 5020:                   help => 'Domain_Username_Approvals'});
 5021:         $bread_crumbs_component = 'Account requests';
 5022:         if ($env{'form.state'} eq 'done') {
 5023:             push(@{$brcrum},
 5024:                      {href => '/adm/createuser?action=usernamereqqueue',
 5025:                       text => 'Result',
 5026:                       help => 'Domain_Username_Approvals'});
 5027:             $bread_crumbs_component = 'LON-CAPA account request result';
 5028:         }
 5029:         $args = { bread_crumbs           => $brcrum,
 5030:                   bread_crumbs_component => $bread_crumbs_component};
 5031:         my $js = &usernamerequest_javascript();
 5032:         $r->print(&header(&add_script($js),$args));
 5033:         if (!exists($env{'form.state'})) {
 5034:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestusername',
 5035:                                                                             $env{'request.role.domain'}));
 5036:         } elsif ($env{'form.state'} eq 'done') {
 5037:             $r->print('<h3>'.&mt('LON-CAPA account request processing').'</h3>'."\n");
 5038:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestusername',
 5039:                                                                          $env{'request.role.domain'}));
 5040:         }
 5041:     } elsif (($env{'form.action'} eq 'displayuserreq') &&
 5042:              ($permission->{'cusr'})) {
 5043:         my $dom = $env{'form.domain'};
 5044:         my $uname = $env{'form.username'};
 5045:         my $warning;
 5046:         if (($dom =~ /^$match_domain$/) && (&Apache::lonnet::domain($dom) ne '')) {
 5047:             if (($dom eq $env{'request.role.domain'}) && (&Apache::lonnet::allowed('ccc',$dom))) {
 5048:                 if (($uname =~ /^$match_username$/) && ($env{'form.queue'} eq 'approval')) {
 5049:                     my $uhome = &Apache::lonnet::homeserver($uname,$dom);
 5050:                     if ($uhome eq 'no_host') {
 5051:                         my $queue = $env{'form.queue'};
 5052:                         my $reqkey = &escape($uname).'_'.$queue; 
 5053:                         my $namespace = 'usernamequeue';
 5054:                         my $domconfig = &Apache::lonnet::get_domainconfiguser($dom);
 5055:                         my %queued =
 5056:                             &Apache::lonnet::get($namespace,[$reqkey],$dom,$domconfig);
 5057:                         unless ($queued{$reqkey}) {
 5058:                             $warning = &mt('No information was found for this LON-CAPA account request.');
 5059:                         }
 5060:                     } else {
 5061:                         $warning = &mt('A LON-CAPA account already exists for the requested username and domain.');
 5062:                     }
 5063:                 } else {
 5064:                     $warning = &mt('LON-CAPA account request status check is for an invalid username.');
 5065:                 }
 5066:             } else {
 5067:                 $warning = &mt('You do not have rights to view LON-CAPA account requests in the domain specified.');
 5068:             }
 5069:         } else {
 5070:             $warning = &mt('LON-CAPA account request status check is for an invalid domain.');
 5071:         }
 5072:         my $args = { only_body => 1 };
 5073:         $r->print(&header(undef,$args).
 5074:                   '<h3>'.&mt('LON-CAPA Account Request Details').'</h3>');
 5075:         if ($warning ne '') {
 5076:             $r->print('<div class="LC_warning">'.$warning.'</div>');
 5077:         } else {
 5078:             my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5079:             my $domconfiguser = &Apache::lonnet::get_domainconfiguser($dom);
 5080:             my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
 5081:             if (ref($domconfig{'usercreation'}) eq 'HASH') {
 5082:                 if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
 5083:                     if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}) eq 'HASH') {
 5084:                         my %info =
 5085:                             &Apache::lonnet::get('nohist_requestedusernames',[$uname],$dom,$domconfiguser);
 5086:                         if (ref($info{$uname}) eq 'HASH') {
 5087:                             my $usertype = $info{$uname}{'inststatus'};
 5088:                             unless ($usertype) {
 5089:                                 $usertype = 'default';
 5090:                             }
 5091:                             my ($showstatus,$showemail,$pickstart);
 5092:                             my $numextras = 0;
 5093:                             my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5094:                             if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
 5095:                                 if (ref($usertypes) eq 'HASH') {
 5096:                                     if ($usertypes->{$usertype}) {
 5097:                                         $showstatus = $usertypes->{$usertype};
 5098:                                     } else {
 5099:                                         $showstatus = $othertitle;
 5100:                                     }
 5101:                                     if ($showstatus) {
 5102:                                         $numextras ++;
 5103:                                     }
 5104:                                 }
 5105:                             }
 5106:                             if (($info{$uname}{'email'} ne '') && ($info{$uname}{'email'} ne $uname)) {
 5107:                                 $showemail = $info{$uname}{'email'};
 5108:                                 $numextras ++;
 5109:                             }
 5110:                             if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}) eq 'HASH') {
 5111:                                 if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
 5112:                                     $pickstart = 1;
 5113:                                     $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box());
 5114:                                     my ($num,$count);
 5115:                                     $count = scalar(keys(%{$domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}}));
 5116:                                     $count += $numextras;
 5117:                                     foreach my $field (@{$infofields}) {
 5118:                                         next unless ($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}{$field});
 5119:                                         next unless ($infotitles->{$field});
 5120:                                         $r->print(&Apache::lonhtmlcommon::row_title($infotitles->{$field}).
 5121:                                                   $info{$uname}{$field});
 5122:                                         $num ++;
 5123:                                         unless ($count == $num) {
 5124:                                             $r->print(&Apache::lonhtmlcommon::row_closure());
 5125:                                         }
 5126:                                     }
 5127:                                 }
 5128:                             }
 5129:                             if ($numextras) {
 5130:                                 unless ($pickstart) {
 5131:                                     $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box());
 5132:                                     $pickstart = 1;
 5133:                                 }
 5134:                                 if ($showemail) {
 5135:                                     my $closure = '';
 5136:                                     unless ($showstatus) {
 5137:                                         $closure = 1;
 5138:                                     }
 5139:                                     $r->print(&Apache::lonhtmlcommon::row_title(&mt('E-mail address')).
 5140:                                               $showemail.
 5141:                                               &Apache::lonhtmlcommon::row_closure($closure));
 5142:                                 }
 5143:                                 if ($showstatus) {
 5144:                                     $r->print(&Apache::lonhtmlcommon::row_title(&mt('Status type[_1](self-reported)','<br />')).
 5145:                                               $showstatus.
 5146:                                               &Apache::lonhtmlcommon::row_closure(1));
 5147:                                 }
 5148:                             }
 5149:                             if ($pickstart) { 
 5150:                                 $r->print(&Apache::lonhtmlcommon::end_pick_box().'</div>');
 5151:                             } else {
 5152:                                 $r->print('<div>'.&mt('No information to display for this account request.').'</div>');
 5153:                             }
 5154:                         } else {
 5155:                             $r->print('<div>'.&mt('No information available for this account request.').'</div>');
 5156:                         }
 5157:                     }
 5158:                 }
 5159:             }
 5160:         }
 5161:         $r->print(&close_popup_form());
 5162:     } elsif (($env{'form.action'} eq 'listusers') && 
 5163:              ($permission->{'view'} || $permission->{'cusr'})) {
 5164:         my $helpitem = 'Course_View_Class_List';
 5165:         if ($context eq 'author') {
 5166:             $helpitem = 'Author_View_Coauthor_List';
 5167:         } elsif ($context eq 'domain') {
 5168:             $helpitem = 'Domain_View_Users_List';
 5169:         }
 5170:         if ($env{'form.phase'} eq 'bulkchange') {
 5171:             push(@{$brcrum},
 5172:                     {href => '/adm/createuser?action=listusers',
 5173:                      text => "List Users"},
 5174:                     {href => "/adm/createuser",
 5175:                      text => "Result",
 5176:                      help => $helpitem});
 5177:             $bread_crumbs_component = 'Update Users';
 5178:             $args = {bread_crumbs           => $brcrum,
 5179:                      bread_crumbs_component => $bread_crumbs_component};
 5180:             $r->print(&header(undef,$args));
 5181:             my $setting = $env{'form.roletype'};
 5182:             my $choice = $env{'form.bulkaction'};
 5183:             if ($permission->{'cusr'}) {
 5184:                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice,$crstype);
 5185:             } else {
 5186:                 $r->print(&mt('You are not authorized to make bulk changes to user roles'));
 5187:                 $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
 5188:             }
 5189:         } else {
 5190:             push(@{$brcrum},
 5191:                     {href => '/adm/createuser?action=listusers',
 5192:                      text => "List Users",
 5193:                      help => $helpitem});
 5194:             $bread_crumbs_component = 'List Users';
 5195:             $args = {bread_crumbs           => $brcrum,
 5196:                      bread_crumbs_component => $bread_crumbs_component};
 5197:             my ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles);
 5198:             my $formname = 'studentform';
 5199:             my $hidecall = "hide_searching();";
 5200:             if (($context eq 'domain') && (($env{'form.roletype'} eq 'course') ||
 5201:                 ($env{'form.roletype'} eq 'community'))) {
 5202:                 if ($env{'form.roletype'} eq 'course') {
 5203:                     ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles) = 
 5204:                         &Apache::lonuserutils::courses_selector($env{'request.role.domain'},
 5205:                                                                 $formname);
 5206:                 } elsif ($env{'form.roletype'} eq 'community') {
 5207:                     $cb_jscript = 
 5208:                         &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
 5209:                     my %elements = (
 5210:                                       coursepick => 'radio',
 5211:                                       coursetotal => 'text',
 5212:                                       courselist => 'text',
 5213:                                    );
 5214:                     $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
 5215:                 }
 5216:                 $jscript .= &verify_user_display($context)."\n".
 5217:                             &Apache::loncommon::check_uncheck_jscript();
 5218:                 my $js = &add_script($jscript).$cb_jscript;
 5219:                 my $loadcode = 
 5220:                     &Apache::lonuserutils::course_selector_loadcode($formname);
 5221:                 if ($loadcode ne '') {
 5222:                     $args->{add_entries} = {onload => "$loadcode;$hidecall"};
 5223:                 } else {
 5224:                     $args->{add_entries} = {onload => $hidecall};
 5225:                 }
 5226:                 $r->print(&header($js,$args));
 5227:             } else {
 5228:                 $args->{add_entries} = {onload => $hidecall};
 5229:                 $jscript = &verify_user_display($context).
 5230:                            &Apache::loncommon::check_uncheck_jscript(); 
 5231:                 $r->print(&header(&add_script($jscript),$args));
 5232:             }
 5233:             &Apache::lonuserutils::print_userlist($r,undef,$permission,$context,
 5234:                          $formname,$totcodes,$codetitles,$idlist,$idlist_titles,
 5235:                          $showcredits);
 5236:         }
 5237:     } elsif ($env{'form.action'} eq 'drop' && $permission->{'cusr'}) {
 5238:         my $brtext;
 5239:         if ($crstype eq 'Community') {
 5240:             $brtext = 'Drop Members';
 5241:         } else {
 5242:             $brtext = 'Drop Students';
 5243:         }
 5244:         push(@{$brcrum},
 5245:                 {href => '/adm/createuser?action=drop',
 5246:                  text => $brtext,
 5247:                  help => 'Course_Drop_Student'});
 5248:         if ($env{'form.state'} eq 'done') {
 5249:             push(@{$brcrum},
 5250:                      {href=>'/adm/createuser?action=drop',
 5251:                       text=>"Result"});
 5252:         }
 5253:         $bread_crumbs_component = $brtext;
 5254:         $args = {bread_crumbs           => $brcrum,
 5255:                  bread_crumbs_component => $bread_crumbs_component}; 
 5256:         $r->print(&header(undef,$args));
 5257:         if (!exists($env{'form.state'})) {
 5258:             &Apache::lonuserutils::print_drop_menu($r,$context,$permission,$crstype);
 5259:         } elsif ($env{'form.state'} eq 'done') {
 5260:             &Apache::lonuserutils::update_user_list($r,$context,undef,
 5261:                                                     $env{'form.action'});
 5262:         }
 5263:     } elsif ($env{'form.action'} eq 'dateselect') {
 5264:         if ($permission->{'cusr'}) {
 5265:             $r->print(&header(undef,{'no_nav_bar' => 1}).
 5266:                       &Apache::lonuserutils::date_section_selector($context,$permission,
 5267:                                                                    $crstype,$showcredits));
 5268:         } else {
 5269:             $r->print(&header(undef,{'no_nav_bar' => 1}).
 5270:                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>'); 
 5271:         }
 5272:     } elsif ($env{'form.action'} eq 'selfenroll') {
 5273:         if ($permission->{selfenrolladmin}) {
 5274:             my %currsettings = (
 5275:                 selfenroll_types              => $env{'course.'.$cid.'.internal.selfenroll_types'},
 5276:                 selfenroll_registered         => $env{'course.'.$cid.'.internal.selfenroll_registered'},
 5277:                 selfenroll_section            => $env{'course.'.$cid.'.internal.selfenroll_section'},
 5278:                 selfenroll_notifylist         => $env{'course.'.$cid.'.internal.selfenroll_notifylist'},
 5279:                 selfenroll_approval           => $env{'course.'.$cid.'.internal.selfenroll_approval'},
 5280:                 selfenroll_limit              => $env{'course.'.$cid.'.internal.selfenroll_limit'},
 5281:                 selfenroll_cap                => $env{'course.'.$cid.'.internal.selfenroll_cap'},
 5282:                 selfenroll_start_date         => $env{'course.'.$cid.'.internal.selfenroll_start_date'},
 5283:                 selfenroll_end_date           => $env{'course.'.$cid.'.internal.selfenroll_end_date'},
 5284:                 selfenroll_start_access       => $env{'course.'.$cid.'.internal.selfenroll_start_access'},
 5285:                 selfenroll_end_access         => $env{'course.'.$cid.'.internal.selfenroll_end_access'},
 5286:                 default_enrollment_start_date => $env{'course.'.$cid.'.default_enrollment_start_date'},
 5287:                 default_enrollment_end_date   => $env{'course.'.$cid.'.default_enrollment_end_date'},
 5288:                 uniquecode                    => $env{'course.'.$cid.'.internal.uniquecode'},
 5289:             );
 5290:             push(@{$brcrum},
 5291:                     {href => '/adm/createuser?action=selfenroll',
 5292:                      text => "Configure Self-enrollment",
 5293:                      help => 'Course_Self_Enrollment'});
 5294:             if (!exists($env{'form.state'})) {
 5295:                 $args = { bread_crumbs           => $brcrum,
 5296:                           bread_crumbs_component => 'Configure Self-enrollment'};
 5297:                 $r->print(&header(undef,$args));
 5298:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
 5299:                 &print_selfenroll_menu($r,'course',$cid,$cdom,$cnum,\%currsettings);
 5300:             } elsif ($env{'form.state'} eq 'done') {
 5301:                 push (@{$brcrum},
 5302:                           {href=>'/adm/createuser?action=selfenroll',
 5303:                            text=>"Result"});
 5304:                 $args = { bread_crumbs           => $brcrum,
 5305:                           bread_crumbs_component => 'Self-enrollment result'};
 5306:                 $r->print(&header(undef,$args));
 5307:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
 5308:                 &update_selfenroll_config($r,$cid,$cdom,$cnum,$context,$crstype,\%currsettings);
 5309:             }
 5310:         } else {
 5311:             $r->print(&header(undef,{'no_nav_bar' => 1}).
 5312:                      '<span class="LC_error">'.&mt('You do not have permission to configure self-enrollment').'</span>');
 5313:         }
 5314:     } elsif ($env{'form.action'} eq 'selfenrollqueue') {
 5315:         if ($permission->{selfenrolladmin}) {
 5316:             push(@{$brcrum},
 5317:                      {href => '/adm/createuser?action=selfenrollqueue',
 5318:                       text => 'Enrollment requests',
 5319:                       help => 'Course_Approve_Selfenroll'});
 5320:             $bread_crumbs_component = 'Enrollment requests';
 5321:             if ($env{'form.state'} eq 'done') {
 5322:                 push(@{$brcrum},
 5323:                          {href => '/adm/createuser?action=selfenrollqueue',
 5324:                           text => 'Result',
 5325:                           help => 'Course_Approve_Selfenroll'});
 5326:                 $bread_crumbs_component = 'Enrollment result';
 5327:             }
 5328:             $args = { bread_crumbs           => $brcrum,
 5329:                       bread_crumbs_component => $bread_crumbs_component};
 5330:             $r->print(&header(undef,$args));
 5331:             my $coursedesc = $env{'course.'.$cid.'.description'};
 5332:             if (!exists($env{'form.state'})) {
 5333:                 $r->print('<h3>'.&mt('Pending enrollment requests').'</h3>'."\n");
 5334:                 $r->print(&Apache::loncoursequeueadmin::display_queued_requests($context,
 5335:                                                                                 $cdom,$cnum));
 5336:             } elsif ($env{'form.state'} eq 'done') {
 5337:                 $r->print('<h3>'.&mt('Enrollment request processing').'</h3>'."\n");
 5338:                 $r->print(&Apache::loncoursequeueadmin::update_request_queue($context,
 5339:                               $cdom,$cnum,$coursedesc));
 5340:             }
 5341:         } else {
 5342:             $r->print(&header(undef,{'no_nav_bar' => 1}).
 5343:                      '<span class="LC_error">'.&mt('You do not have permission to manage self-enrollment').'</span>');
 5344:         }
 5345:     } elsif ($env{'form.action'} eq 'changelogs') {
 5346:         if ($permission->{cusr} || $permission->{view}) {
 5347:             &print_userchangelogs_display($r,$context,$permission,$brcrum);
 5348:         } else {
 5349:             $r->print(&header(undef,{'no_nav_bar' => 1}).
 5350:                      '<span class="LC_error">'.&mt('You do not have permission to view change logs').'</span>');
 5351:         }
 5352:     } elsif ($env{'form.action'} eq 'helpdesk') {
 5353:         if (($permission->{'owner'}) || ($permission->{'co-owner'})) {
 5354:             if ($env{'form.state'} eq 'process') {
 5355:                 if ($permission->{'owner'}) {
 5356:                     &update_helpdeskaccess($r,$permission,$brcrum);
 5357:                 } else {
 5358:                     &print_helpdeskaccess_display($r,$permission,$brcrum);
 5359:                 }
 5360:             } else {
 5361:                 &print_helpdeskaccess_display($r,$permission,$brcrum);
 5362:             }
 5363:         } else {
 5364:             $r->print(&header(undef,{'no_nav_bar' => 1}).
 5365:                       '<span class="LC_error">'.&mt('You do not have permission to view helpdesk access').'</span>');
 5366:         }
 5367:     } else {
 5368:         $bread_crumbs_component = 'User Management';
 5369:         $args = { bread_crumbs           => $brcrum,
 5370:                   bread_crumbs_component => $bread_crumbs_component};
 5371:         $r->print(&header(undef,$args));
 5372:         $r->print(&print_main_menu($permission,$context,$crstype));
 5373:     }
 5374:     $r->print(&Apache::loncommon::end_page());
 5375:     return OK;
 5376: }
 5377: 
 5378: sub header {
 5379:     my ($jscript,$args) = @_;
 5380:     my $start_page;
 5381:     if (ref($args) eq 'HASH') {
 5382:         $start_page=&Apache::loncommon::start_page('User Management',$jscript,$args);
 5383:     } else {
 5384:         $start_page=&Apache::loncommon::start_page('User Management',$jscript);
 5385:     }
 5386:     return $start_page;
 5387: }
 5388: 
 5389: sub add_script {
 5390:     my ($js) = @_;
 5391:     return '<script type="text/javascript">'."\n"
 5392:           .'// <![CDATA['."\n"
 5393:           .$js."\n"
 5394:           .'// ]]>'."\n"
 5395:           .'</script>'."\n";
 5396: }
 5397: 
 5398: sub usernamerequest_javascript {
 5399:     my $js = <<ENDJS;
 5400: 
 5401: function openusernamereqdisplay(dom,uname,queue) {
 5402:     var url = '/adm/createuser?action=displayuserreq';
 5403:     url += '&domain='+dom+'&username='+uname+'&queue='+queue;
 5404:     var title = 'Account_Request_Browser';
 5405:     var options = 'scrollbars=1,resizable=1,menubar=0';
 5406:     options += ',width=700,height=600';
 5407:     var stdeditbrowser = open(url,title,options,'1');
 5408:     stdeditbrowser.focus();
 5409:     return;
 5410: }
 5411:  
 5412: ENDJS
 5413: }
 5414: 
 5415: sub close_popup_form {
 5416:     my $close= &mt('Close Window');
 5417:     return << "END";
 5418: <p><form name="displayreq" action="" method="post">
 5419: <input type="button" name="closeme" value="$close" onclick="javascript:self.close();" />
 5420: </form></p>
 5421: END
 5422: }
 5423: 
 5424: sub verify_user_display {
 5425:     my ($context) = @_;
 5426:     my %lt = &Apache::lonlocal::texthash (
 5427:         course    => 'course(s): description, section(s), status',
 5428:         community => 'community(s): description, section(s), status',
 5429:         author    => 'author',
 5430:     );
 5431:     my $photos;
 5432:     if (($context eq 'course') && $env{'request.course.id'}) {
 5433:         $photos = $env{'course.'.$env{'request.course.id'}.'.internal.showphoto'};
 5434:     }
 5435:     my $output = <<"END";
 5436: 
 5437: function hide_searching() {
 5438:     if (document.getElementById('searching')) {
 5439:         document.getElementById('searching').style.display = 'none';
 5440:     }
 5441:     return;
 5442: }
 5443: 
 5444: function display_update() {
 5445:     document.studentform.action.value = 'listusers';
 5446:     document.studentform.phase.value = 'display';
 5447:     document.studentform.submit();
 5448: }
 5449: 
 5450: function updateCols(caller) {
 5451:     var context = '$context';
 5452:     var photos = '$photos';
 5453:     if (caller == 'Status') {
 5454:         if ((context == 'domain') && 
 5455:             ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
 5456:              (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community'))) {
 5457:             document.getElementById('showcolstatus').checked = false;
 5458:             document.getElementById('showcolstatus').disabled = 'disabled';
 5459:             document.getElementById('showcolstart').checked = false;
 5460:             document.getElementById('showcolend').checked = false;
 5461:         } else {
 5462:             if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
 5463:                 document.getElementById('showcolstatus').checked = true;
 5464:                 document.getElementById('showcolstatus').disabled = '';
 5465:                 document.getElementById('showcolstart').checked = true;
 5466:                 document.getElementById('showcolend').checked = true;
 5467:             } else {
 5468:                 document.getElementById('showcolstatus').checked = false;
 5469:                 document.getElementById('showcolstatus').disabled = 'disabled';
 5470:                 document.getElementById('showcolstart').checked = false;
 5471:                 document.getElementById('showcolend').checked = false;
 5472:             }
 5473:         }
 5474:     }
 5475:     if (caller == 'output') {
 5476:         if (photos == 1) {
 5477:             if (document.getElementById('showcolphoto')) {
 5478:                 var photoitem = document.getElementById('showcolphoto');
 5479:                 if (document.studentform.output.options[document.studentform.output.selectedIndex].value == 'html') {
 5480:                     photoitem.checked = true;
 5481:                     photoitem.disabled = '';
 5482:                 } else {
 5483:                     photoitem.checked = false;
 5484:                     photoitem.disabled = 'disabled';
 5485:                 }
 5486:             }
 5487:         }
 5488:     }
 5489:     if (caller == 'showrole') {
 5490:         if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any') ||
 5491:             (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'cr')) {
 5492:             document.getElementById('showcolrole').checked = true;
 5493:             document.getElementById('showcolrole').disabled = '';
 5494:         } else {
 5495:             document.getElementById('showcolrole').checked = false;
 5496:             document.getElementById('showcolrole').disabled = 'disabled';
 5497:         }
 5498:         if (context == 'domain') {
 5499:             var quotausageshow = 0;
 5500:             if ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
 5501:                 (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community')) {
 5502:                 document.getElementById('showcolstatus').checked = false;
 5503:                 document.getElementById('showcolstatus').disabled = 'disabled';
 5504:                 document.getElementById('showcolstart').checked = false;
 5505:                 document.getElementById('showcolend').checked = false;
 5506:             } else {
 5507:                 if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
 5508:                     document.getElementById('showcolstatus').checked = true;
 5509:                     document.getElementById('showcolstatus').disabled = '';
 5510:                     document.getElementById('showcolstart').checked = true;
 5511:                     document.getElementById('showcolend').checked = true;
 5512:                 }
 5513:             }
 5514:             if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'domain') {
 5515:                 document.getElementById('showcolextent').disabled = 'disabled';
 5516:                 document.getElementById('showcolextent').checked = 'false';
 5517:                 document.getElementById('showextent').style.display='none';
 5518:                 document.getElementById('showcoltextextent').innerHTML = '';
 5519:                 if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'au') ||
 5520:                     (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any')) {
 5521:                     if (document.getElementById('showcolauthorusage')) {
 5522:                         document.getElementById('showcolauthorusage').disabled = '';
 5523:                     }
 5524:                     if (document.getElementById('showcolauthorquota')) {
 5525:                         document.getElementById('showcolauthorquota').disabled = '';
 5526:                     }
 5527:                     quotausageshow = 1;
 5528:                 }
 5529:             } else {
 5530:                 document.getElementById('showextent').style.display='block';
 5531:                 document.getElementById('showextent').style.textAlign='left';
 5532:                 document.getElementById('showextent').style.textFace='normal';
 5533:                 if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'author') {
 5534:                     document.getElementById('showcolextent').disabled = '';
 5535:                     document.getElementById('showcolextent').checked = 'true';
 5536:                     document.getElementById('showcoltextextent').innerHTML="$lt{'author'}";
 5537:                 } else {
 5538:                     document.getElementById('showcolextent').disabled = '';
 5539:                     document.getElementById('showcolextent').checked = 'true';
 5540:                     if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community') {
 5541:                         document.getElementById('showcoltextextent').innerHTML="$lt{'community'}";
 5542:                     } else {
 5543:                         document.getElementById('showcoltextextent').innerHTML="$lt{'course'}";
 5544:                     }
 5545:                 }
 5546:             }
 5547:             if (quotausageshow == 0)  {
 5548:                 if (document.getElementById('showcolauthorusage')) {
 5549:                     document.getElementById('showcolauthorusage').checked = false;
 5550:                     document.getElementById('showcolauthorusage').disabled = 'disabled';
 5551:                 }
 5552:                 if (document.getElementById('showcolauthorquota')) {
 5553:                     document.getElementById('showcolauthorquota').checked = false;
 5554:                     document.getElementById('showcolauthorquota').disabled = 'disabled';
 5555:                 }
 5556:             }
 5557:         }
 5558:     }
 5559:     return;
 5560: }
 5561: 
 5562: END
 5563:     return $output;
 5564: 
 5565: }
 5566: 
 5567: ###############################################################
 5568: ###############################################################
 5569: #  Menu Phase One
 5570: sub print_main_menu {
 5571:     my ($permission,$context,$crstype) = @_;
 5572:     my $linkcontext = $context;
 5573:     my $stuterm = lc(&Apache::lonnet::plaintext('st',$crstype));
 5574:     if (($context eq 'course') && ($crstype eq 'Community')) {
 5575:         $linkcontext = lc($crstype);
 5576:         $stuterm = 'Members';
 5577:     }
 5578:     my %links = (
 5579:                 domain => {
 5580:                             upload     => 'Upload a File of Users',
 5581:                             singleuser => 'Add/Modify a User',
 5582:                             listusers  => 'Manage Users',
 5583:                             },
 5584:                 author => {
 5585:                             upload     => 'Upload a File of Co-authors',
 5586:                             singleuser => 'Add/Modify a Co-author',
 5587:                             listusers  => 'Manage Co-authors',
 5588:                             },
 5589:                 course => {
 5590:                             upload     => 'Upload a File of Course Users',
 5591:                             singleuser => 'Add/Modify a Course User',
 5592:                             listusers  => 'List and Modify Multiple Course Users',
 5593:                             },
 5594:                 community => {
 5595:                             upload     => 'Upload a File of Community Users',
 5596:                             singleuser => 'Add/Modify a Community User',
 5597:                             listusers  => 'List and Modify Multiple Community Users',
 5598:                            },
 5599:                 );
 5600:      my %linktitles = (
 5601:                 domain => {
 5602:                             singleuser => 'Add a user to the domain, and/or a course or community in the domain.',
 5603:                             listusers  => 'Show and manage users in this domain.',
 5604:                             },
 5605:                 author => {
 5606:                             singleuser => 'Add a user with a co- or assistant author role.',
 5607:                             listusers  => 'Show and manage co- or assistant authors.',
 5608:                             },
 5609:                 course => {
 5610:                             singleuser => 'Add a user with a certain role to this course.',
 5611:                             listusers  => 'Show and manage users in this course.',
 5612:                             },
 5613:                 community => {
 5614:                             singleuser => 'Add a user with a certain role to this community.',
 5615:                             listusers  => 'Show and manage users in this community.',
 5616:                            },
 5617:                 );
 5618:   if ($linkcontext eq 'domain') {
 5619:       unless ($permission->{'cusr'}) {
 5620:           $links{'domain'}{'singleuser'} = 'View a User';
 5621:           $linktitles{'domain'}{'singleuser'} = 'View information about a user in the domain';
 5622:       }
 5623:   } elsif ($linkcontext eq 'course') {
 5624:       unless ($permission->{'cusr'}) {
 5625:           $links{'course'}{'singleuser'} = 'View a Course User';
 5626:           $linktitles{'course'}{'singleuser'} = 'View information about a user in this course';
 5627:           $links{'course'}{'listusers'} = 'List Course Users';
 5628:           $linktitles{'course'}{'listusers'} = 'Show information about users in this course';
 5629:       }
 5630:   } elsif ($linkcontext eq 'community') {
 5631:       unless ($permission->{'cusr'}) {
 5632:           $links{'community'}{'singleuser'} = 'View a Community User';
 5633:           $linktitles{'community'}{'singleuser'} = 'View information about a user in this community';
 5634:           $links{'community'}{'listusers'} = 'List Community Users';
 5635:           $linktitles{'community'}{'listusers'} = 'Show information about users in this community';
 5636:       }
 5637:   }
 5638:   my @menu = ( {categorytitle => 'Single Users', 
 5639:          items =>
 5640:          [
 5641:             {
 5642:              linktext => $links{$linkcontext}{'singleuser'},
 5643:              icon => 'edit-redo.png',
 5644:              #help => 'Course_Change_Privileges',
 5645:              url => '/adm/createuser?action=singleuser',
 5646:              permission => ($permission->{'view'} || $permission->{'cusr'}),
 5647:              linktitle => $linktitles{$linkcontext}{'singleuser'},
 5648:             },
 5649:          ]},
 5650: 
 5651:          {categorytitle => 'Multiple Users',
 5652:          items => 
 5653:          [
 5654:             {
 5655:              linktext => $links{$linkcontext}{'upload'},
 5656:              icon => 'uplusr.png',
 5657:              #help => 'Course_Create_Class_List',
 5658:              url => '/adm/createuser?action=upload',
 5659:              permission => $permission->{'cusr'},
 5660:              linktitle => 'Upload a CSV or a text file containing users.',
 5661:             },
 5662:             {
 5663:              linktext => $links{$linkcontext}{'listusers'},
 5664:              icon => 'mngcu.png',
 5665:              #help => 'Course_View_Class_List',
 5666:              url => '/adm/createuser?action=listusers',
 5667:              permission => ($permission->{'view'} || $permission->{'cusr'}),
 5668:              linktitle => $linktitles{$linkcontext}{'listusers'}, 
 5669:             },
 5670: 
 5671:          ]},
 5672: 
 5673:          {categorytitle => 'Administration',
 5674:          items => [ ]},
 5675:        );
 5676: 
 5677:     if ($context eq 'domain'){
 5678:         push(@{  $menu[0]->{items} }, # Single Users
 5679:             {
 5680:              linktext => 'User Access Log',
 5681:              icon => 'document-properties.png',
 5682:              #help => 'Domain_User_Access_Logs',
 5683:              url => '/adm/createuser?action=accesslogs',
 5684:              permission => $permission->{'activity'},
 5685:              linktitle => 'View user access log.',
 5686:             }
 5687:         );
 5688:         
 5689:         push(@{ $menu[2]->{items} }, #Category: Administration
 5690:             {
 5691:              linktext => 'Custom Roles',
 5692:              icon => 'emblem-photos.png',
 5693:              #help => 'Course_Editing_Custom_Roles',
 5694:              url => '/adm/createuser?action=custom',
 5695:              permission => $permission->{'custom'},
 5696:              linktitle => 'Configure a custom role.',
 5697:             },
 5698:             {
 5699:              linktext => 'Authoring Space Requests',
 5700:              icon => 'selfenrl-queue.png',
 5701:              #help => 'Domain_Role_Approvals',
 5702:              url => '/adm/createuser?action=processauthorreq',
 5703:              permission => $permission->{'cusr'},
 5704:              linktitle => 'Approve or reject author role requests',
 5705:             },
 5706:             {
 5707:              linktext => 'LON-CAPA Account Requests',
 5708:              icon => 'list-add.png',
 5709:              #help => 'Domain_Username_Approvals',
 5710:              url => '/adm/createuser?action=processusernamereq',
 5711:              permission => $permission->{'cusr'},
 5712:              linktitle => 'Approve or reject LON-CAPA account requests',
 5713:             },
 5714:             {
 5715:              linktext => 'Change Log',
 5716:              icon => 'document-properties.png',
 5717:              #help => 'Course_User_Logs',
 5718:              url => '/adm/createuser?action=changelogs',
 5719:              permission => ($permission->{'cusr'} || $permission->{'view'}),
 5720:              linktitle => 'View change log.',
 5721:             },
 5722:         );
 5723:         
 5724:     }elsif ($context eq 'course'){
 5725:         my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
 5726: 
 5727:         my %linktext = (
 5728:                          'Course'    => {
 5729:                                           single => 'Add/Modify a Student', 
 5730:                                           drop   => 'Drop Students',
 5731:                                           groups => 'Course Groups',
 5732:                                         },
 5733:                          'Community' => {
 5734:                                           single => 'Add/Modify a Member', 
 5735:                                           drop   => 'Drop Members',
 5736:                                           groups => 'Community Groups',
 5737:                                         },
 5738:                        );
 5739:         $linktext{'Placement'} = $linktext{'Course'};
 5740: 
 5741:         my %linktitle = (
 5742:             'Course' => {
 5743:                   single => 'Add a user with the role of student to this course',
 5744:                   drop   => 'Remove a student from this course.',
 5745:                   groups => 'Manage course groups',
 5746:                         },
 5747:             'Community' => {
 5748:                   single => 'Add a user with the role of member to this community',
 5749:                   drop   => 'Remove a member from this community.',
 5750:                   groups => 'Manage community groups',
 5751:                            },
 5752:         );
 5753: 
 5754:         $linktitle{'Placement'} = $linktitle{'Course'};
 5755: 
 5756:         push(@{ $menu[0]->{items} }, #Category: Single Users
 5757:             {   
 5758:              linktext => $linktext{$crstype}{'single'},
 5759:              #help => 'Course_Add_Student',
 5760:              icon => 'list-add.png',
 5761:              url => '/adm/createuser?action=singlestudent',
 5762:              permission => $permission->{'cusr'},
 5763:              linktitle => $linktitle{$crstype}{'single'},
 5764:             },
 5765:         );
 5766:         
 5767:         push(@{ $menu[1]->{items} }, #Category: Multiple Users 
 5768:             {
 5769:              linktext => $linktext{$crstype}{'drop'},
 5770:              icon => 'edit-undo.png',
 5771:              #help => 'Course_Drop_Student',
 5772:              url => '/adm/createuser?action=drop',
 5773:              permission => $permission->{'cusr'},
 5774:              linktitle => $linktitle{$crstype}{'drop'},
 5775:             },
 5776:         );
 5777:         push(@{ $menu[2]->{items} }, #Category: Administration
 5778:             {
 5779:              linktext => 'Helpdesk Access',
 5780:              icon => 'helpdesk-access.png',
 5781:              #help => 'Course_Helpdesk_Access',
 5782:              url => '/adm/createuser?action=helpdesk',
 5783:              permission => ($permission->{'owner'} || $permission->{'co-owner'}),
 5784:              linktitle => 'Helpdesk access options',
 5785:             },
 5786:             {
 5787:              linktext => 'Custom Roles',
 5788:              icon => 'emblem-photos.png',
 5789:              #help => 'Course_Editing_Custom_Roles',
 5790:              url => '/adm/createuser?action=custom',
 5791:              permission => $permission->{'custom'},
 5792:              linktitle => 'Configure a custom role.',
 5793:             },
 5794:             {
 5795:              linktext => $linktext{$crstype}{'groups'},
 5796:              icon => 'grps.png',
 5797:              #help => 'Course_Manage_Group',
 5798:              url => '/adm/coursegroups?refpage=cusr',
 5799:              permission => $permission->{'grp_manage'},
 5800:              linktitle => $linktitle{$crstype}{'groups'},
 5801:             },
 5802:             {
 5803:              linktext => 'Change Log',
 5804:              icon => 'document-properties.png',
 5805:              #help => 'Course_User_Logs',
 5806:              url => '/adm/createuser?action=changelogs',
 5807:              permission => ($permission->{'view'} || $permission->{'cusr'}),
 5808:              linktitle => 'View change log.',
 5809:             },
 5810:         );
 5811:         if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'}) {
 5812:             push(@{ $menu[2]->{items} },
 5813:                     {
 5814:                      linktext => 'Enrollment Requests',
 5815:                      icon => 'selfenrl-queue.png',
 5816:                      #help => 'Course_Approve_Selfenroll',
 5817:                      url => '/adm/createuser?action=selfenrollqueue',
 5818:                      permission => $permission->{'selfenrolladmin'},
 5819:                      linktitle =>'Approve or reject enrollment requests.',
 5820:                     },
 5821:             );
 5822:         }
 5823:         
 5824:         if (!exists($permission->{'cusr_section'})){
 5825:             if ($crstype ne 'Community') {
 5826:                 push(@{ $menu[2]->{items} },
 5827:                     {
 5828:                      linktext => 'Automated Enrollment',
 5829:                      icon => 'roles.png',
 5830:                      #help => 'Course_Automated_Enrollment',
 5831:                      permission => (&Apache::lonnet::auto_run($cnum,$cdom)
 5832:                                          && (($permission->{'cusr'}) ||
 5833:                                              ($permission->{'view'}))),
 5834:                      url  => '/adm/populate',
 5835:                      linktitle => 'Automated enrollment manager.',
 5836:                     }
 5837:                 );
 5838:             }
 5839:             push(@{ $menu[2]->{items} }, 
 5840:                 {
 5841:                  linktext => 'User Self-Enrollment',
 5842:                  icon => 'self_enroll.png',
 5843:                  #help => 'Course_Self_Enrollment',
 5844:                  url => '/adm/createuser?action=selfenroll',
 5845:                  permission => $permission->{'selfenrolladmin'},
 5846:                  linktitle => 'Configure user self-enrollment.',
 5847:                 },
 5848:             );
 5849:         }
 5850:     } elsif ($context eq 'author') {
 5851:         push(@{ $menu[2]->{items} }, #Category: Administration
 5852:             {
 5853:              linktext => 'Change Log',
 5854:              icon => 'document-properties.png',
 5855:              #help => 'Course_User_Logs',
 5856:              url => '/adm/createuser?action=changelogs',
 5857:              permission => $permission->{'cusr'},
 5858:              linktitle => 'View change log.',
 5859:             },
 5860:         );
 5861:     }
 5862:     return Apache::lonhtmlcommon::generate_menu(@menu);
 5863: #               { text => 'View Log-in History',
 5864: #                 help => 'Course_User_Logins',
 5865: #                 action => 'logins',
 5866: #                 permission => $permission->{'cusr'},
 5867: #               });
 5868: }
 5869: 
 5870: sub restore_prev_selections {
 5871:     my %saveable_parameters = ('srchby'   => 'scalar',
 5872: 			       'srchin'   => 'scalar',
 5873: 			       'srchtype' => 'scalar',
 5874: 			       );
 5875:     &Apache::loncommon::store_settings('user','user_picker',
 5876: 				       \%saveable_parameters);
 5877:     &Apache::loncommon::restore_settings('user','user_picker',
 5878: 					 \%saveable_parameters);
 5879: }
 5880: 
 5881: sub print_selfenroll_menu {
 5882:     my ($r,$context,$cid,$cdom,$cnum,$currsettings,$additional,$readonly) = @_;
 5883:     my $crstype = &Apache::loncommon::course_type();
 5884:     my $formname = 'selfenroll';
 5885:     my $nolink = 1;
 5886:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
 5887:     my $groupslist = &Apache::lonuserutils::get_groupslist();
 5888:     my $setsec_js = 
 5889:         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
 5890:     my %alerts = &Apache::lonlocal::texthash(
 5891:         acto => 'Activation of self-enrollment was selected for the following domain(s)',
 5892:         butn => 'but no user types have been checked.',
 5893:         wilf => "Please uncheck 'activate' or check at least one type.",
 5894:     );
 5895:     my $disabled;
 5896:     if ($readonly) {
 5897:        $disabled = ' disabled="disabled"';
 5898:     }
 5899:     &js_escape(\%alerts);
 5900:     my $selfenroll_js = <<"ENDSCRIPT";
 5901: function update_types(caller,num) {
 5902:     var delidx = getIndexByName('selfenroll_delete');
 5903:     var actidx = getIndexByName('selfenroll_activate');
 5904:     if (caller == 'selfenroll_all') {
 5905:         var selall;
 5906:         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
 5907:             if (document.$formname.selfenroll_all[i].checked) {
 5908:                 selall = document.$formname.selfenroll_all[i].value;
 5909:             }
 5910:         }
 5911:         if (selall == 1) {
 5912:             if (delidx != -1) {
 5913:                 if (document.$formname.selfenroll_delete.length) {
 5914:                     for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
 5915:                         document.$formname.selfenroll_delete[j].checked = true;
 5916:                     }
 5917:                 } else {
 5918:                     document.$formname.elements[delidx].checked = true;
 5919:                 }
 5920:             }
 5921:             if (actidx != -1) {
 5922:                 if (document.$formname.selfenroll_activate.length) {
 5923:                     for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
 5924:                         document.$formname.selfenroll_activate[j].checked = false;
 5925:                     }
 5926:                 } else {
 5927:                     document.$formname.elements[actidx].checked = false;
 5928:                 }
 5929:             }
 5930:             document.$formname.selfenroll_newdom.selectedIndex = 0; 
 5931:         }
 5932:     }
 5933:     if (caller == 'selfenroll_activate') {
 5934:         if (document.$formname.selfenroll_activate.length) {
 5935:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
 5936:                 if (document.$formname.selfenroll_activate[j].value == num) {
 5937:                     if (document.$formname.selfenroll_activate[j].checked) {
 5938:                         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
 5939:                             if (document.$formname.selfenroll_all[i].value == '1') {
 5940:                                 document.$formname.selfenroll_all[i].checked = false;
 5941:                             }
 5942:                             if (document.$formname.selfenroll_all[i].value == '0') {
 5943:                                 document.$formname.selfenroll_all[i].checked = true;
 5944:                             }
 5945:                         }
 5946:                     }
 5947:                 }
 5948:             }
 5949:         } else {
 5950:             for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
 5951:                 if (document.$formname.selfenroll_all[i].value == '1') {
 5952:                     document.$formname.selfenroll_all[i].checked = false;
 5953:                 }
 5954:                 if (document.$formname.selfenroll_all[i].value == '0') {
 5955:                     document.$formname.selfenroll_all[i].checked = true;
 5956:                 }
 5957:             }
 5958:         }
 5959:     }
 5960:     if (caller == 'selfenroll_delete') {
 5961:         if (document.$formname.selfenroll_delete.length) {
 5962:             for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
 5963:                 if (document.$formname.selfenroll_delete[j].value == num) {
 5964:                     if (document.$formname.selfenroll_delete[j].checked) {
 5965:                         var delindex = getIndexByName('selfenroll_types_'+num);
 5966:                         if (delindex != -1) { 
 5967:                             if (document.$formname.elements[delindex].length) {
 5968:                                 for (var k=0; k<document.$formname.elements[delindex].length; k++) {
 5969:                                     document.$formname.elements[delindex][k].checked = false;
 5970:                                 }
 5971:                             } else {
 5972:                                 document.$formname.elements[delindex].checked = false;
 5973:                             }
 5974:                         }
 5975:                     }
 5976:                 }
 5977:             }
 5978:         } else {
 5979:             if (document.$formname.selfenroll_delete.checked) {
 5980:                 var delindex = getIndexByName('selfenroll_types_'+num);
 5981:                 if (delindex != -1) {
 5982:                     if (document.$formname.elements[delindex].length) {
 5983:                         for (var k=0; k<document.$formname.elements[delindex].length; k++) {
 5984:                             document.$formname.elements[delindex][k].checked = false;
 5985:                         }
 5986:                     } else {
 5987:                         document.$formname.elements[delindex].checked = false;
 5988:                     }
 5989:                 }
 5990:             }
 5991:         }
 5992:     }
 5993:     return;
 5994: }
 5995: 
 5996: function validate_types(form) {
 5997:     var needaction = new Array();
 5998:     var countfail = 0;
 5999:     var actidx = getIndexByName('selfenroll_activate');
 6000:     if (actidx != -1) {
 6001:         if (document.$formname.selfenroll_activate.length) {
 6002:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
 6003:                 var num = document.$formname.selfenroll_activate[j].value;
 6004:                 if (document.$formname.selfenroll_activate[j].checked) {
 6005:                     countfail = check_types(num,countfail,needaction)
 6006:                 }
 6007:             }
 6008:         } else {
 6009:             if (document.$formname.selfenroll_activate.checked) {
 6010:                 var num = document.$formname.selfenroll_activate.value;
 6011:                 countfail = check_types(num,countfail,needaction)
 6012:             }
 6013:         }
 6014:     }
 6015:     if (countfail > 0) {
 6016:         var msg = "$alerts{'acto'}\\n";
 6017:         var loopend = needaction.length -1;
 6018:         if (loopend > 0) {
 6019:             for (var m=0; m<loopend; m++) {
 6020:                 msg += needaction[m]+", ";
 6021:             }
 6022:         }
 6023:         msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
 6024:         alert(msg);
 6025:         return; 
 6026:     }
 6027:     setSections(form);
 6028: }
 6029: 
 6030: function check_types(num,countfail,needaction) {
 6031:     var boxname = 'selfenroll_types_'+num;
 6032:     var typeidx = getIndexByName(boxname);
 6033:     var count = 0;
 6034:     if (typeidx != -1) {
 6035:         if (document.$formname.elements[boxname].length) {
 6036:             for (var k=0; k<document.$formname.elements[boxname].length; k++) {
 6037:                 if (document.$formname.elements[boxname][k].checked) {
 6038:                     count ++;
 6039:                 }
 6040:             }
 6041:         } else {
 6042:             if (document.$formname.elements[typeidx].checked) {
 6043:                 count ++;
 6044:             }
 6045:         }
 6046:         if (count == 0) {
 6047:             var domidx = getIndexByName('selfenroll_dom_'+num);
 6048:             if (domidx != -1) {
 6049:                 var domname = document.$formname.elements[domidx].value;
 6050:                 needaction[countfail] = domname;
 6051:                 countfail ++;
 6052:             }
 6053:         }
 6054:     }
 6055:     return countfail;
 6056: }
 6057: 
 6058: function toggleNotify() {
 6059:     var selfenrollApproval = 0;
 6060:     if (document.$formname.selfenroll_approval.length) {
 6061:         for (var i=0; i<document.$formname.selfenroll_approval.length; i++) {
 6062:             if (document.$formname.selfenroll_approval[i].checked) {
 6063:                 selfenrollApproval = document.$formname.selfenroll_approval[i].value;
 6064:                 break;        
 6065:             }
 6066:         }
 6067:     }
 6068:     if (document.getElementById('notified')) {
 6069:         if (selfenrollApproval == 0) {
 6070:             document.getElementById('notified').style.display='none';
 6071:         } else {
 6072:             document.getElementById('notified').style.display='block';
 6073:         }
 6074:     }
 6075:     return;
 6076: }
 6077: 
 6078: function getIndexByName(item) {
 6079:     for (var i=0;i<document.$formname.elements.length;i++) {
 6080:         if (document.$formname.elements[i].name == item) {
 6081:             return i;
 6082:         }
 6083:     }
 6084:     return -1;
 6085: }
 6086: ENDSCRIPT
 6087: 
 6088:     my $output = '<script type="text/javascript">'."\n".
 6089:                  '// <![CDATA['."\n".
 6090:                  $setsec_js."\n".$selfenroll_js."\n".
 6091:                  '// ]]>'."\n".
 6092:                  '</script>'."\n".
 6093:                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n";
 6094:  
 6095:     my $visactions = &cat_visibility();
 6096:     my ($cathash,%cattype);
 6097:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
 6098:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 6099:         $cathash = $domconfig{'coursecategories'}{'cats'};
 6100:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
 6101:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
 6102:         if ($cattype{'auth'} eq '') {
 6103:             $cattype{'auth'} = 'std';
 6104:         }
 6105:         if ($cattype{'unauth'} eq '') {
 6106:             $cattype{'unauth'} = 'std';
 6107:         }
 6108:     } else {
 6109:         $cathash = {};
 6110:         $cattype{'auth'} = 'std';
 6111:         $cattype{'unauth'} = 'std';
 6112:     }
 6113:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
 6114:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
 6115:                   '<br />'.
 6116:                   '<br />'.$visactions->{'take'}.'<ul>'.
 6117:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
 6118:                   '</ul>');
 6119:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
 6120:         if ($currsettings->{'uniquecode'}) {
 6121:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
 6122:         } else {
 6123:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
 6124:                   '<br />'.
 6125:                   '<br />'.$visactions->{'take'}.'<ul>'.
 6126:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
 6127:                   '</ul><br />');
 6128:         }
 6129:     } else {
 6130:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
 6131:         if (ref($visactions) eq 'HASH') {
 6132:             if ($visible) {
 6133:                 $output .= '<p class="LC_info">'.$visactions->{'vis'}.'</p>';
 6134:            } else {
 6135:                 $output .= '<p class="LC_warning">'.$visactions->{'miss'}.'</p>'
 6136:                           .$visactions->{'yous'}.
 6137:                            '<p>'.$visactions->{'gen'}.'<br />'.$visactions->{'coca'};
 6138:                 if (ref($vismsgs) eq 'ARRAY') {
 6139:                     $output .= '<br />'.$visactions->{'make'}.'<ul>';
 6140:                     foreach my $item (@{$vismsgs}) {
 6141:                         $output .= '<li>'.$visactions->{$item}.'</li>';
 6142:                     }
 6143:                     $output .= '</ul>';
 6144:                 }
 6145:                 $output .= '</p>';
 6146:             }
 6147:         }
 6148:     }
 6149:     my $actionhref = '/adm/createuser';
 6150:     if ($context eq 'domain') {
 6151:         $actionhref = '/adm/modifycourse';
 6152:     }
 6153: 
 6154:     my %noedit;
 6155:     unless ($context eq 'domain') {
 6156:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
 6157:     }
 6158:     $output .= '<form name="'.$formname.'" method="post" action="'.$actionhref.'">'."\n".
 6159:                &Apache::lonhtmlcommon::start_pick_box();
 6160:     if (ref($row) eq 'ARRAY') {
 6161:         foreach my $item (@{$row}) {
 6162:             my $title = $item; 
 6163:             if (ref($lt) eq 'HASH') {
 6164:                 $title = $lt->{$item};
 6165:             }
 6166:             $output .= &Apache::lonhtmlcommon::row_title($title);
 6167:             if ($item eq 'types') {
 6168:                 my $curr_types;
 6169:                 if (ref($currsettings) eq 'HASH') {
 6170:                     $curr_types = $currsettings->{'selfenroll_types'};
 6171:                 }
 6172:                 if ($noedit{$item}) {
 6173:                     if ($curr_types eq '*') {
 6174:                         $output .= &mt('Any user in any domain');   
 6175:                     } else {
 6176:                         my @entries = split(/;/,$curr_types);
 6177:                         if (@entries > 0) {
 6178:                             $output .= '<ul>'; 
 6179:                             foreach my $entry (@entries) {
 6180:                                 my ($currdom,$typestr) = split(/:/,$entry);
 6181:                                 next if ($typestr eq '');
 6182:                                 my $domdesc = &Apache::lonnet::domain($currdom);
 6183:                                 my @currinsttypes = split(',',$typestr);
 6184:                                 my ($othertitle,$usertypes,$types) = 
 6185:                                     &Apache::loncommon::sorted_inst_types($currdom);
 6186:                                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6187:                                     $usertypes->{'any'} = &mt('any user'); 
 6188:                                     if (keys(%{$usertypes}) > 0) {
 6189:                                         $usertypes->{'other'} = &mt('other users');
 6190:                                     }
 6191:                                     my @longinsttypes = map { $usertypes->{$_}; } @currinsttypes;
 6192:                                     $output .= '<li>'.$domdesc.':'.join(', ',@longinsttypes).'</li>';
 6193:                                  }
 6194:                             }
 6195:                             $output .= '</ul>';
 6196:                         } else {
 6197:                             $output .= &mt('None');
 6198:                         }
 6199:                     }
 6200:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
 6201:                     next;
 6202:                 }
 6203:                 my $showdomdesc = 1;
 6204:                 my $includeempty = 1;
 6205:                 my $num = 0;
 6206:                 $output .= &Apache::loncommon::start_data_table().
 6207:                            &Apache::loncommon::start_data_table_row()
 6208:                            .'<td colspan="2"><span class="LC_nobreak"><label>'
 6209:                            .&mt('Any user in any domain:')
 6210:                            .'&nbsp;<input type="radio" name="selfenroll_all" value="1" ';
 6211:                 if ($curr_types eq '*') {
 6212:                     $output .= ' checked="checked" '; 
 6213:                 }
 6214:                 $output .= 'onchange="javascript:update_types('.
 6215:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('Yes').'</label>'.
 6216:                            '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
 6217:                 if ($curr_types ne '*') {
 6218:                     $output .= ' checked="checked" ';
 6219:                 }
 6220:                 $output .= ' onchange="javascript:update_types('.
 6221:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('No').'</label></td>'.
 6222:                            &Apache::loncommon::end_data_table_row().
 6223:                            &Apache::loncommon::end_data_table().
 6224:                            &mt('Or').'<br />'.
 6225:                            &Apache::loncommon::start_data_table();
 6226:                 my %currdoms;
 6227:                 if ($curr_types eq '') {
 6228:                     $output .= &new_selfenroll_dom_row($cdom,'0');
 6229:                 } elsif ($curr_types ne '*') {
 6230:                     my @entries = split(/;/,$curr_types);
 6231:                     if (@entries > 0) {
 6232:                         foreach my $entry (@entries) {
 6233:                             my ($currdom,$typestr) = split(/:/,$entry);
 6234:                             $currdoms{$currdom} = 1;
 6235:                             my $domdesc = &Apache::lonnet::domain($currdom);
 6236:                             my @currinsttypes = split(',',$typestr);
 6237:                             $output .= &Apache::loncommon::start_data_table_row()
 6238:                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
 6239:                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
 6240:                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
 6241:                                        .'" value="'.$currdom.'" /></span><br />'
 6242:                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
 6243:                                        .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');"'.$disabled.' />'
 6244:                                        .&mt('Delete').'</label></span></td>';
 6245:                             $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
 6246:                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes,$readonly).'</td>'
 6247:                                        .&Apache::loncommon::end_data_table_row();
 6248:                             $num ++;
 6249:                         }
 6250:                     }
 6251:                 }
 6252:                 my $add_domtitle = &mt('Users in additional domain:');
 6253:                 if ($curr_types eq '*') { 
 6254:                     $add_domtitle = &mt('Users in specific domain:');
 6255:                 } elsif ($curr_types eq '') {
 6256:                     $add_domtitle = &mt('Users in other domain:');
 6257:                 }
 6258:                 my ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('enroll',$cdom);
 6259:                 $output .= &Apache::loncommon::start_data_table_row()
 6260:                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
 6261:                            .&Apache::loncommon::select_dom_form('','selfenroll_newdom',
 6262:                                                                 $includeempty,$showdomdesc,'',$trusted,$untrusted,$readonly)
 6263:                            .'<input type="hidden" name="selfenroll_types_total" value="'.$num.'" />'
 6264:                            .'</td>'.&Apache::loncommon::end_data_table_row()
 6265:                            .&Apache::loncommon::end_data_table();
 6266:             } elsif ($item eq 'registered') {
 6267:                 my ($regon,$regoff);
 6268:                 my $registered;
 6269:                 if (ref($currsettings) eq 'HASH') {
 6270:                     $registered = $currsettings->{'selfenroll_registered'};
 6271:                 }
 6272:                 if ($noedit{$item}) {
 6273:                     if ($registered) {
 6274:                         $output .= &mt('Must be registered in course');
 6275:                     } else {
 6276:                         $output .= &mt('No requirement');
 6277:                     }
 6278:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
 6279:                     next;
 6280:                 }
 6281:                 if ($registered) {
 6282:                     $regon = ' checked="checked" ';
 6283:                     $regoff = '';
 6284:                 } else {
 6285:                     $regon = '';
 6286:                     $regoff = ' checked="checked" ';
 6287:                 }
 6288:                 $output .= '<label>'.
 6289:                            '<input type="radio" name="selfenroll_registered" value="1"'.$regon.$disabled.' />'.
 6290:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
 6291:                            '<input type="radio" name="selfenroll_registered" value="0"'.$regoff.$disabled.' />'.
 6292:                            &mt('No').'</label>';
 6293:             } elsif ($item eq 'enroll_dates') {
 6294:                 my ($starttime,$endtime);
 6295:                 if (ref($currsettings) eq 'HASH') {
 6296:                     $starttime = $currsettings->{'selfenroll_start_date'};
 6297:                     $endtime = $currsettings->{'selfenroll_end_date'};
 6298:                     if ($starttime eq '') {
 6299:                         $starttime = $currsettings->{'default_enrollment_start_date'};
 6300:                     }
 6301:                     if ($endtime eq '') {
 6302:                         $endtime = $currsettings->{'default_enrollment_end_date'};
 6303:                     }
 6304:                 }
 6305:                 if ($noedit{$item}) {
 6306:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
 6307:                                                           &Apache::lonlocal::locallocaltime($endtime));
 6308:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
 6309:                     next;
 6310:                 }
 6311:                 my $startform =
 6312:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
 6313:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
 6314:                 my $endform =
 6315:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
 6316:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
 6317:                 $output .= &selfenroll_date_forms($startform,$endform);
 6318:             } elsif ($item eq 'access_dates') {
 6319:                 my ($starttime,$endtime);
 6320:                 if (ref($currsettings) eq 'HASH') {
 6321:                     $starttime = $currsettings->{'selfenroll_start_access'};
 6322:                     $endtime = $currsettings->{'selfenroll_end_access'};
 6323:                     if ($starttime eq '') {
 6324:                         $starttime = $currsettings->{'default_enrollment_start_date'};
 6325:                     }
 6326:                     if ($endtime eq '') {
 6327:                         $endtime = $currsettings->{'default_enrollment_end_date'};
 6328:                     }
 6329:                 }
 6330:                 if ($noedit{$item}) {
 6331:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
 6332:                                                           &Apache::lonlocal::locallocaltime($endtime));
 6333:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
 6334:                     next;
 6335:                 }
 6336:                 my $startform =
 6337:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
 6338:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
 6339:                 my $endform =
 6340:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
 6341:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
 6342:                 $output .= &selfenroll_date_forms($startform,$endform);
 6343:             } elsif ($item eq 'section') {
 6344:                 my $currsec;
 6345:                 if (ref($currsettings) eq 'HASH') {
 6346:                     $currsec = $currsettings->{'selfenroll_section'};
 6347:                 }
 6348:                 my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
 6349:                 my $newsecval;
 6350:                 if ($currsec ne 'none' && $currsec ne '') {
 6351:                     if (!defined($sections_count{$currsec})) {
 6352:                         $newsecval = $currsec;
 6353:                     }
 6354:                 }
 6355:                 if ($noedit{$item}) {
 6356:                     if ($currsec ne '') {
 6357:                         $output .= $currsec;
 6358:                     } else {
 6359:                         $output .= &mt('No specific section');
 6360:                     }
 6361:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
 6362:                     next;
 6363:                 }
 6364:                 my $sections_select = 
 6365:                     &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec,$disabled);
 6366:                 $output .= '<table class="LC_createuser">'."\n".
 6367:                            '<tr class="LC_section_row">'."\n".
 6368:                            '<td align="center">'.&mt('Existing sections')."\n".
 6369:                            '<br />'.$sections_select.'</td><td align="center">'.
 6370:                            &mt('New section').'<br />'."\n".
 6371:                            '<input type="text" name="newsec" size="15" value="'.$newsecval.'"'.$disabled.' />'."\n".
 6372:                            '<input type="hidden" name="sections" value="" />'."\n".
 6373:                            '</td></tr></table>'."\n";
 6374:             } elsif ($item eq 'approval') {
 6375:                 my ($currnotified,$currapproval,%appchecked);
 6376:                 my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
 6377:                 if (ref($currsettings) eq 'HASH') {
 6378:                     $currnotified = $currsettings->{'selfenroll_notifylist'};
 6379:                     $currapproval = $currsettings->{'selfenroll_approval'};
 6380:                 }
 6381:                 if ($currapproval !~ /^[012]$/) {
 6382:                     $currapproval = 0;
 6383:                 }
 6384:                 if ($noedit{$item}) {
 6385:                     $output .=  $selfdescs{'approval'}{$currapproval}.
 6386:                                 '<br />'.&mt('(Set by Domain Coordinator)');
 6387:                     next;
 6388:                 }
 6389:                 $appchecked{$currapproval} = ' checked="checked"';
 6390:                 for my $i (0..2) {
 6391:                     $output .= '<label>'.
 6392:                                '<input type="radio" name="selfenroll_approval" value="'.$i.'"'.
 6393:                                $appchecked{$i}.' onclick="toggleNotify();"'.$disabled.' />'.
 6394:                                $selfdescs{'approval'}{$i}.'</label>'.('&nbsp;'x2);
 6395:                 }
 6396:                 my %advhash = &Apache::lonnet::get_course_adv_roles($cid,1);
 6397:                 my (@ccs,%notified);
 6398:                 my $ccrole = 'cc';
 6399:                 if ($crstype eq 'Community') {
 6400:                     $ccrole = 'co';
 6401:                 }
 6402:                 if ($advhash{$ccrole}) {
 6403:                     @ccs = split(/,/,$advhash{$ccrole});
 6404:                 }
 6405:                 if ($currnotified) {
 6406:                     foreach my $current (split(/,/,$currnotified)) {
 6407:                         $notified{$current} = 1;
 6408:                         if (!grep(/^\Q$current\E$/,@ccs)) {
 6409:                             push(@ccs,$current);
 6410:                         }
 6411:                     }
 6412:                 }
 6413:                 if (@ccs) {
 6414:                     my $style;
 6415:                     unless ($currapproval) {
 6416:                         $style = ' style="display: none;"'; 
 6417:                     }
 6418:                     $output .= '<br /><div id="notified"'.$style.'>'.
 6419:                                &mt('Personnel to be notified when an enrollment request needs approval, or has been approved:').'&nbsp;'.
 6420:                                &Apache::loncommon::start_data_table().
 6421:                                &Apache::loncommon::start_data_table_row();
 6422:                     my $count = 0;
 6423:                     my $numcols = 4;
 6424:                     foreach my $cc (sort(@ccs)) {
 6425:                         my $notifyon;
 6426:                         my ($ccuname,$ccudom) = split(/:/,$cc);
 6427:                         if ($notified{$cc}) {
 6428:                             $notifyon = ' checked="checked" ';
 6429:                         }
 6430:                         if ($count && !$count%$numcols) {
 6431:                             $output .= &Apache::loncommon::end_data_table_row().
 6432:                                        &Apache::loncommon::start_data_table_row()
 6433:                         }
 6434:                         $output .= '<td><span class="LC_nobreak"><label>'.
 6435:                                    '<input type="checkbox" name="selfenroll_notify"'.$notifyon.' value="'.$cc.'"'.$disabled.' />'.
 6436:                                    &Apache::loncommon::plainname($ccuname,$ccudom).
 6437:                                    '</label></span></td>';
 6438:                         $count ++;
 6439:                     }
 6440:                     my $rem = $count%$numcols;
 6441:                     if ($rem) {
 6442:                         my $emptycols = $numcols - $rem;
 6443:                         for (my $i=0; $i<$emptycols; $i++) { 
 6444:                             $output .= '<td>&nbsp;</td>';
 6445:                         }
 6446:                     }
 6447:                     $output .= &Apache::loncommon::end_data_table_row().
 6448:                                &Apache::loncommon::end_data_table().
 6449:                                '</div>';
 6450:                 }
 6451:             } elsif ($item eq 'limit') {
 6452:                 my ($crslimit,$selflimit,$nolimit,$currlim,$currcap);
 6453:                 if (ref($currsettings) eq 'HASH') {
 6454:                     $currlim = $currsettings->{'selfenroll_limit'};
 6455:                     $currcap = $currsettings->{'selfenroll_cap'};
 6456:                 }
 6457:                 if ($noedit{$item}) {
 6458:                     if (($currlim eq 'allstudents') || ($currlim eq 'selfenrolled')) {
 6459:                         if ($currlim eq 'allstudents') {
 6460:                             $output .= &mt('Limit by total students');
 6461:                         } elsif ($currlim eq 'selfenrolled') {
 6462:                             $output .= &mt('Limit by total self-enrolled students');
 6463:                         }
 6464:                         $output .= ' '.&mt('Maximum: [_1]',$currcap).
 6465:                                    '<br />'.&mt('(Set by Domain Coordinator)');
 6466:                     } else {
 6467:                         $output .= &mt('No limit').'<br />'.&mt('(Set by Domain Coordinator)');
 6468:                     }
 6469:                     next;
 6470:                 }
 6471:                 if ($currlim eq 'allstudents') {
 6472:                     $crslimit = ' checked="checked" ';
 6473:                     $selflimit = ' ';
 6474:                     $nolimit = ' ';
 6475:                 } elsif ($currlim eq 'selfenrolled') {
 6476:                     $crslimit = ' ';
 6477:                     $selflimit = ' checked="checked" ';
 6478:                     $nolimit = ' '; 
 6479:                 } else {
 6480:                     $crslimit = ' ';
 6481:                     $selflimit = ' ';
 6482:                     $nolimit = ' checked="checked" ';
 6483:                 }
 6484:                 $output .= '<table><tr><td><label>'.
 6485:                            '<input type="radio" name="selfenroll_limit" value="none"'.$nolimit.$disabled.'/>'.
 6486:                            &mt('No limit').'</label></td><td><label>'.
 6487:                            '<input type="radio" name="selfenroll_limit" value="allstudents"'.$crslimit.$disabled.'/>'.
 6488:                            &mt('Limit by total students').'</label></td><td><label>'.
 6489:                            '<input type="radio" name="selfenroll_limit" value="selfenrolled"'.$selflimit.$disabled.'/>'.
 6490:                            &mt('Limit by total self-enrolled students').
 6491:                            '</td></tr><tr>'.
 6492:                            '<td>&nbsp;</td><td colspan="2"><span class="LC_nobreak">'.
 6493:                            ('&nbsp;'x3).&mt('Maximum number allowed: ').
 6494:                            '<input type="text" name="selfenroll_cap" size = "5" value="'.$currcap.'"'.$disabled.' /></td></tr></table>';
 6495:             }
 6496:             $output .= &Apache::lonhtmlcommon::row_closure(1);
 6497:         }
 6498:     }
 6499:     $output .= &Apache::lonhtmlcommon::end_pick_box().'<br />';
 6500:     unless ($readonly) {
 6501:         $output .= '<input type="button" name="selfenrollconf" value="'
 6502:                    .&mt('Save').'" onclick="validate_types(this.form);" />';
 6503:     }
 6504:     $output .= '<input type="hidden" name="action" value="selfenroll" />'
 6505:               .'<input type="hidden" name="state" value="done" />'."\n"
 6506:               .$additional.'</form>';
 6507:     $r->print($output);
 6508:     return;
 6509: }
 6510: 
 6511: sub get_noedit_fields {
 6512:     my ($cdom,$cnum,$crstype,$row) = @_;
 6513:     my %noedit;
 6514:     if (ref($row) eq 'ARRAY') {
 6515:         my %settings = &Apache::lonnet::get('environment',['internal.coursecode','internal.textbook',
 6516:                                                            'internal.selfenrollmgrdc',
 6517:                                                            'internal.selfenrollmgrcc'],$cdom,$cnum);
 6518:         my $type = &Apache::lonuserutils::get_extended_type($cdom,$cnum,$crstype,\%settings);
 6519:         my (%specific_managebydc,%specific_managebycc,%default_managebydc);
 6520:         map { $specific_managebydc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrdc'}));
 6521:         map { $specific_managebycc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrcc'}));
 6522:         my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
 6523:         map { $default_managebydc{$_} = 1; } (split(/,/,$domdefaults{$type.'selfenrolladmdc'}));
 6524: 
 6525:         foreach my $item (@{$row}) {
 6526:             next if ($specific_managebycc{$item});
 6527:             if (($specific_managebydc{$item}) || ($default_managebydc{$item})) {
 6528:                 $noedit{$item} = 1;
 6529:             }
 6530:         }
 6531:     }
 6532:     return %noedit;
 6533: } 
 6534: 
 6535: sub visible_in_stdcat {
 6536:     my ($cdom,$cnum,$domconf) = @_;
 6537:     my ($cathash,%settable,@vismsgs,$cansetvis,$visible);
 6538:     unless (ref($domconf) eq 'HASH') {
 6539:         return ($visible,$cansetvis,\@vismsgs);
 6540:     }
 6541:     if (ref($domconf->{'coursecategories'}) eq 'HASH') {
 6542:         if ($domconf->{'coursecategories'}{'togglecats'} eq 'crs') {
 6543:             $settable{'togglecats'} = 1;
 6544:         }
 6545:         if ($domconf->{'coursecategories'}{'categorize'} eq 'crs') {
 6546:             $settable{'categorize'} = 1;
 6547:         }
 6548:         $cathash = $domconf->{'coursecategories'}{'cats'};
 6549:     }
 6550:     if ($settable{'togglecats'} && $settable{'categorize'}) {
 6551:         $cansetvis = &mt('You are able to both assign a course category and choose to exclude this course from the catalog.');   
 6552:     } elsif ($settable{'togglecats'}) {
 6553:         $cansetvis = &mt('You are able to choose to exclude this course from the catalog, but only a Domain Coordinator may assign a course category.'); 
 6554:     } elsif ($settable{'categorize'}) {
 6555:         $cansetvis = &mt('You may assign a course category, but only a Domain Coordinator may choose to exclude this course from the catalog.');  
 6556:     } else {
 6557:         $cansetvis = &mt('Only a Domain Coordinator may assign a course category or choose to exclude this course from the catalog.'); 
 6558:     }
 6559:      
 6560:     my %currsettings =
 6561:         &Apache::lonnet::get('environment',['hidefromcat','categories','internal.coursecode'],
 6562:                              $cdom,$cnum);
 6563:     $visible = 0;
 6564:     if ($currsettings{'internal.coursecode'} ne '') {
 6565:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
 6566:             $cathash = $domconf->{'coursecategories'}{'cats'};
 6567:             if (ref($cathash) eq 'HASH') {
 6568:                 if ($cathash->{'instcode::0'} eq '') {
 6569:                     push(@vismsgs,'dc_addinst'); 
 6570:                 } else {
 6571:                     $visible = 1;
 6572:                 }
 6573:             } else {
 6574:                 $visible = 1;
 6575:             }
 6576:         } else {
 6577:             $visible = 1;
 6578:         }
 6579:     } else {
 6580:         if (ref($cathash) eq 'HASH') {
 6581:             if ($cathash->{'instcode::0'} ne '') {
 6582:                 push(@vismsgs,'dc_instcode');
 6583:             }
 6584:         } else {
 6585:             push(@vismsgs,'dc_instcode');
 6586:         }
 6587:     }
 6588:     if ($currsettings{'categories'} ne '') {
 6589:         my $cathash;
 6590:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
 6591:             $cathash = $domconf->{'coursecategories'}{'cats'};
 6592:             if (ref($cathash) eq 'HASH') {
 6593:                 if (keys(%{$cathash}) == 0) {
 6594:                     push(@vismsgs,'dc_catalog');
 6595:                 } elsif ((keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} ne '')) {
 6596:                     push(@vismsgs,'dc_categories');
 6597:                 } else {
 6598:                     my @currcategories = split('&',$currsettings{'categories'});
 6599:                     my $matched = 0;
 6600:                     foreach my $cat (@currcategories) {
 6601:                         if ($cathash->{$cat} ne '') {
 6602:                             $visible = 1;
 6603:                             $matched = 1;
 6604:                             last;
 6605:                         }
 6606:                     }
 6607:                     if (!$matched) {
 6608:                         if ($settable{'categorize'}) { 
 6609:                             push(@vismsgs,'chgcat');
 6610:                         } else {
 6611:                             push(@vismsgs,'dc_chgcat');
 6612:                         }
 6613:                     }
 6614:                 }
 6615:             }
 6616:         }
 6617:     } else {
 6618:         if (ref($cathash) eq 'HASH') {
 6619:             if ((keys(%{$cathash}) > 1) || 
 6620:                 (keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} eq '')) {
 6621:                 if ($settable{'categorize'}) {
 6622:                     push(@vismsgs,'addcat');
 6623:                 } else {
 6624:                     push(@vismsgs,'dc_addcat');
 6625:                 }
 6626:             }
 6627:         }
 6628:     }
 6629:     if ($currsettings{'hidefromcat'} eq 'yes') {
 6630:         $visible = 0;
 6631:         if ($settable{'togglecats'}) {
 6632:             unshift(@vismsgs,'unhide');
 6633:         } else {
 6634:             unshift(@vismsgs,'dc_unhide')
 6635:         }
 6636:     }
 6637:     return ($visible,$cansetvis,\@vismsgs);
 6638: }
 6639: 
 6640: sub cat_visibility {
 6641:     my %visactions = &Apache::lonlocal::texthash(
 6642:                    vis => 'This course/community currently appears in the Course/Community Catalog for this domain.',
 6643:                    gen => 'Courses can be both self-cataloging, based on an institutional code (e.g., fs08phy231), or can be assigned categories from a hierarchy defined for the domain.',
 6644:                    miss => 'This course/community does not currently appear in the Course/Community Catalog for this domain.',
 6645:                    none => 'Display of a course catalog is disabled for this domain.',
 6646:                    yous => 'You should remedy this if you plan to allow self-enrollment, otherwise students will have difficulty finding this course.',
 6647:                    coca => 'Courses can be absent from the Catalog, because they do not have an institutional code, have no assigned category, or have been specifically excluded.',
 6648:                    make => 'Make any changes to self-enrollment settings below, click "Save", then take action to include the course in the Catalog:',
 6649:                    take => 'Take the following action to ensure the course appears in the Catalog:',
 6650:                    dc_chgconf => 'Ask a domain coordinator to change the Catalog type for this domain.',
 6651:                    dc_setcode => 'Ask a domain coordinator to assign a six character code to the course',
 6652:                    dc_unhide  => 'Ask a domain coordinator to change the "Exclude from course catalog" setting.',
 6653:                    dc_addinst => 'Ask a domain coordinator to enable display the catalog of "Official courses (with institutional codes)".',
 6654:                    dc_instcode => 'Ask a domain coordinator to assign an institutional code (if this is an official course).',
 6655:                    dc_catalog  => 'Ask a domain coordinator to enable or create at least one course category in the domain.',
 6656:                    dc_categories => 'Ask a domain coordinator to create a hierarchy of categories and sub categories for courses in the domain.',
 6657:                    dc_chgcat => 'Ask a domain coordinator to change the category assigned to the course, as the one currently assigned is no longer used in the domain',
 6658:                    dc_addcat => 'Ask a domain coordinator to assign a category to the course.',
 6659:     );
 6660:     $visactions{'unhide'} = &mt('Use [_1]Categorize course[_2] to change the "Exclude from course catalog" setting.','<a href="/adm/courseprefs?phase=display&actions=courseinfo">','</a>"');
 6661:     $visactions{'chgcat'} = &mt('Use [_1]Categorize course[_2] to change the category assigned to the course, as the one currently assigned is no longer used in the domain.','"<a href="/adm/courseprefs?phase=display&actions=courseinfo">','</a>"');
 6662:     $visactions{'addcat'} = &mt('Use [_1]Categorize course[_2] to assign a category to the course.','"<a href="/adm/courseprefs?phase=display&actions=courseinfo">','</a>"');
 6663:     return \%visactions;
 6664: }
 6665: 
 6666: sub new_selfenroll_dom_row {
 6667:     my ($newdom,$num) = @_;
 6668:     my $domdesc = &Apache::lonnet::domain($newdom);
 6669:     my $output;
 6670:     if ($domdesc ne '') {
 6671:         $output .= &Apache::loncommon::start_data_table_row()
 6672:                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
 6673:                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
 6674:                    .'" value="'.$newdom.'" /></span><br />'
 6675:                    .'<span class="LC_nobreak"><label><input type="checkbox" '
 6676:                    .'name="selfenroll_activate" value="'.$num.'" '
 6677:                    .'onchange="javascript:update_types('
 6678:                    ."'selfenroll_activate','$num'".');" />'
 6679:                    .&mt('Activate').'</label></span></td>';
 6680:         my @currinsttypes;
 6681:         $output .= '<td>'.&mt('User types:').'<br />'
 6682:                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
 6683:                    .&Apache::loncommon::end_data_table_row();
 6684:     }
 6685:     return $output;
 6686: }
 6687: 
 6688: sub selfenroll_inst_types {
 6689:     my ($num,$currdom,$currinsttypes,$readonly) = @_;
 6690:     my $output;
 6691:     my $numinrow = 4;
 6692:     my $count = 0;
 6693:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($currdom);
 6694:     my $othervalue = 'any';
 6695:     my $disabled;
 6696:     if ($readonly) {
 6697:         $disabled = ' disabled="disabled"';
 6698:     }
 6699:     if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6700:         if (keys(%{$usertypes}) > 0) {
 6701:             $othervalue = 'other';
 6702:         }
 6703:         $output .= '<table><tr>';
 6704:         foreach my $type (@{$types}) {
 6705:             if (($count > 0) && ($count%$numinrow == 0)) {
 6706:                 $output .= '</tr><tr>';
 6707:             }
 6708:             if (defined($usertypes->{$type})) {
 6709:                 my $esc_type = &escape($type);
 6710:                 $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.
 6711:                            $esc_type.'" ';
 6712:                 if (ref($currinsttypes) eq 'ARRAY') {
 6713:                     if (@{$currinsttypes} > 0) {
 6714:                         if (grep(/^any$/,@{$currinsttypes})) {
 6715:                             $output .= 'checked="checked"';
 6716:                         } elsif (grep(/^\Q$esc_type\E$/,@{$currinsttypes})) {
 6717:                             $output .= 'checked="checked"';
 6718:                         }
 6719:                     } else {
 6720:                         $output .= 'checked="checked"';
 6721:                     }
 6722:                 }
 6723:                 $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$usertypes->{$type}.'</label></span></td>';
 6724:             }
 6725:             $count ++;
 6726:         }
 6727:         if (($count > 0) && ($count%$numinrow == 0)) {
 6728:             $output .= '</tr><tr>';
 6729:         }
 6730:         $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
 6731:         if (ref($currinsttypes) eq 'ARRAY') {
 6732:             if (@{$currinsttypes} > 0) {
 6733:                 if (grep(/^any$/,@{$currinsttypes})) { 
 6734:                     $output .= ' checked="checked"';
 6735:                 } elsif ($othervalue eq 'other') {
 6736:                     if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
 6737:                         $output .= ' checked="checked"';
 6738:                     }
 6739:                 }
 6740:             } else {
 6741:                 $output .= ' checked="checked"';
 6742:             }
 6743:         } else {
 6744:             $output .= ' checked="checked"';
 6745:         }
 6746:         $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$othertitle.'</label></span></td></tr></table>';
 6747:     }
 6748:     return $output;
 6749: }
 6750: 
 6751: sub selfenroll_date_forms {
 6752:     my ($startform,$endform) = @_;
 6753:     my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
 6754:                   &Apache::lonhtmlcommon::row_title(&mt('Start date'),
 6755:                                                     'LC_oddrow_value')."\n".
 6756:                   $startform."\n".
 6757:                   &Apache::lonhtmlcommon::row_closure(1).
 6758:                   &Apache::lonhtmlcommon::row_title(&mt('End date'),
 6759:                                                    'LC_oddrow_value')."\n".
 6760:                   $endform."\n".
 6761:                   &Apache::lonhtmlcommon::row_closure(1).
 6762:                   &Apache::lonhtmlcommon::end_pick_box();
 6763:     return $output;
 6764: }
 6765: 
 6766: sub print_userchangelogs_display {
 6767:     my ($r,$context,$permission,$brcrum) = @_;
 6768:     my $formname = 'rolelog';
 6769:     my ($username,$domain,$crstype,$viewablesec,%roleslog);
 6770:     if ($context eq 'domain') {
 6771:         $domain = $env{'request.role.domain'};
 6772:         %roleslog=&Apache::lonnet::dump_dom('nohist_rolelog',$domain);
 6773:     } else {
 6774:         if ($context eq 'course') { 
 6775:             $domain = $env{'course.'.$env{'request.course.id'}.'.domain'};
 6776:             $username = $env{'course.'.$env{'request.course.id'}.'.num'};
 6777:             $crstype = &Apache::loncommon::course_type();
 6778:             $viewablesec = &Apache::lonuserutils::viewable_section($permission);
 6779:             my %saveable_parameters = ('show' => 'scalar',);
 6780:             &Apache::loncommon::store_course_settings('roles_log',
 6781:                                                       \%saveable_parameters);
 6782:             &Apache::loncommon::restore_course_settings('roles_log',
 6783:                                                         \%saveable_parameters);
 6784:         } elsif ($context eq 'author') {
 6785:             $domain = $env{'user.domain'}; 
 6786:             if ($env{'request.role'} =~ m{^au\./\Q$domain\E/$}) {
 6787:                 $username = $env{'user.name'};
 6788:             } else {
 6789:                 undef($domain);
 6790:             }
 6791:         }
 6792:         if ($domain ne '' && $username ne '') { 
 6793:             %roleslog=&Apache::lonnet::dump('nohist_rolelog',$domain,$username);
 6794:         }
 6795:     }
 6796:     if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
 6797: 
 6798:     my $helpitem;
 6799:     if ($context eq 'course') {
 6800:         $helpitem = 'Course_User_Logs';
 6801:     } elsif ($context eq 'domain') {
 6802:         $helpitem = 'Domain_Role_Logs';
 6803:     } elsif ($context eq 'author') {
 6804:         $helpitem = 'Author_User_Logs';
 6805:     }
 6806:     push (@{$brcrum},
 6807:              {href => '/adm/createuser?action=changelogs',
 6808:               text => 'User Management Logs',
 6809:               help => $helpitem});
 6810:     my $bread_crumbs_component = 'User Changes';
 6811:     my $args = { bread_crumbs           => $brcrum,
 6812:                  bread_crumbs_component => $bread_crumbs_component};
 6813: 
 6814:     # Create navigation javascript
 6815:     my $jsnav = &userlogdisplay_js($formname);
 6816: 
 6817:     my $jscript = (<<ENDSCRIPT);
 6818: <script type="text/javascript">
 6819: // <![CDATA[
 6820: $jsnav
 6821: // ]]>
 6822: </script>
 6823: ENDSCRIPT
 6824: 
 6825:     # print page header
 6826:     $r->print(&header($jscript,$args));
 6827: 
 6828:     # set defaults
 6829:     my $now = time();
 6830:     my $defstart = $now - (7*24*3600); #7 days ago 
 6831:     my %defaults = (
 6832:                      page               => '1',
 6833:                      show               => '10',
 6834:                      role               => 'any',
 6835:                      chgcontext         => 'any',
 6836:                      rolelog_start_date => $defstart,
 6837:                      rolelog_end_date   => $now,
 6838:                    );
 6839:     my $more_records = 0;
 6840: 
 6841:     # set current
 6842:     my %curr;
 6843:     foreach my $item ('show','page','role','chgcontext') {
 6844:         $curr{$item} = $env{'form.'.$item};
 6845:     }
 6846:     my ($startdate,$enddate) = 
 6847:         &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
 6848:     $curr{'rolelog_start_date'} = $startdate;
 6849:     $curr{'rolelog_end_date'} = $enddate;
 6850:     foreach my $key (keys(%defaults)) {
 6851:         if ($curr{$key} eq '') {
 6852:             $curr{$key} = $defaults{$key};
 6853:         }
 6854:     }
 6855:     my (%whodunit,%changed,$version);
 6856:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
 6857:     my ($minshown,$maxshown);
 6858:     $minshown = 1;
 6859:     my $count = 0;
 6860:     if ($curr{'show'} =~ /\D/) {
 6861:         $curr{'page'} = 1;
 6862:     } else {
 6863:         $maxshown = $curr{'page'} * $curr{'show'};
 6864:         if ($curr{'page'} > 1) {
 6865:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
 6866:         }
 6867:     }
 6868: 
 6869:     # Form Header
 6870:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
 6871:               &role_display_filter($context,$formname,$domain,$username,\%curr,
 6872:                                    $version,$crstype));
 6873: 
 6874:     my $showntableheader = 0;
 6875: 
 6876:     # Table Header
 6877:     my $tableheader = 
 6878:         &Apache::loncommon::start_data_table_header_row()
 6879:        .'<th>&nbsp;</th>'
 6880:        .'<th>'.&mt('When').'</th>'
 6881:        .'<th>'.&mt('Who made the change').'</th>'
 6882:        .'<th>'.&mt('Changed User').'</th>'
 6883:        .'<th>'.&mt('Role').'</th>';
 6884: 
 6885:     if ($context eq 'course') {
 6886:         $tableheader .= '<th>'.&mt('Section').'</th>';
 6887:     }
 6888:     $tableheader .=
 6889:         '<th>'.&mt('Context').'</th>'
 6890:        .'<th>'.&mt('Start').'</th>'
 6891:        .'<th>'.&mt('End').'</th>'
 6892:        .&Apache::loncommon::end_data_table_header_row();
 6893: 
 6894:     # Display user change log data
 6895:     foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
 6896:         next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
 6897:                  ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
 6898:         if ($curr{'show'} !~ /\D/) {
 6899:             if ($count >= $curr{'page'} * $curr{'show'}) {
 6900:                 $more_records = 1;
 6901:                 last;
 6902:             }
 6903:         }
 6904:         if ($curr{'role'} ne 'any') {
 6905:             next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
 6906:         }
 6907:         if ($curr{'chgcontext'} ne 'any') {
 6908:             if ($curr{'chgcontext'} eq 'selfenroll') {
 6909:                 next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
 6910:             } else {
 6911:                 next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
 6912:             }
 6913:         }
 6914:         if (($context eq 'course') && ($viewablesec ne '')) {
 6915:             next if ($roleslog{$id}{'logentry'}{'section'} ne $viewablesec);
 6916:         }
 6917:         $count ++;
 6918:         next if ($count < $minshown);
 6919:         unless ($showntableheader) {
 6920:             $r->print(&Apache::loncommon::start_data_table()
 6921:                      .$tableheader);
 6922:             $r->rflush();
 6923:             $showntableheader = 1;
 6924:         }
 6925:         if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
 6926:             $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
 6927:                 &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
 6928:         }
 6929:         if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
 6930:             $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
 6931:                 &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
 6932:         }
 6933:         my $sec = $roleslog{$id}{'logentry'}{'section'};
 6934:         if ($sec eq '') {
 6935:             $sec = &mt('None');
 6936:         }
 6937:         my ($rolestart,$roleend);
 6938:         if ($roleslog{$id}{'delflag'}) {
 6939:             $rolestart = &mt('deleted');
 6940:             $roleend = &mt('deleted');
 6941:         } else {
 6942:             $rolestart = $roleslog{$id}{'logentry'}{'start'};
 6943:             $roleend = $roleslog{$id}{'logentry'}{'end'};
 6944:             if ($rolestart eq '' || $rolestart == 0) {
 6945:                 $rolestart = &mt('No start date'); 
 6946:             } else {
 6947:                 $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
 6948:             }
 6949:             if ($roleend eq '' || $roleend == 0) { 
 6950:                 $roleend = &mt('No end date');
 6951:             } else {
 6952:                 $roleend = &Apache::lonlocal::locallocaltime($roleend);
 6953:             }
 6954:         }
 6955:         my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
 6956:         if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
 6957:             $chgcontext = 'selfenroll';
 6958:         }
 6959:         my %lt = &rolechg_contexts($context,$crstype);
 6960:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
 6961:             $chgcontext = $lt{$chgcontext};
 6962:         }
 6963:         $r->print(
 6964:             &Apache::loncommon::start_data_table_row()
 6965:            .'<td>'.$count.'</td>'
 6966:            .'<td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td>'
 6967:            .'<td>'.$whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.'</td>'
 6968:            .'<td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td>'
 6969:            .'<td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'},$crstype).'</td>');
 6970:         if ($context eq 'course') { 
 6971:             $r->print('<td>'.$sec.'</td>');
 6972:         }
 6973:         $r->print(
 6974:             '<td>'.$chgcontext.'</td>'
 6975:            .'<td>'.$rolestart.'</td>'
 6976:            .'<td>'.$roleend.'</td>'
 6977:            .&Apache::loncommon::end_data_table_row()."\n");
 6978:     }
 6979: 
 6980:     if ($showntableheader) { # Table footer, if content displayed above
 6981:         $r->print(&Apache::loncommon::end_data_table().
 6982:                   &userlogdisplay_navlinks(\%curr,$more_records));
 6983:     } else { # No content displayed above
 6984:         $r->print('<p class="LC_info">'
 6985:                  .&mt('There are no records to display.')
 6986:                  .'</p>'
 6987:         );
 6988:     }
 6989: 
 6990:     # Form Footer
 6991:     $r->print( 
 6992:         '<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
 6993:        .'<input type="hidden" name="action" value="changelogs" />'
 6994:        .'</form>');
 6995:     return;
 6996: }
 6997: 
 6998: sub print_useraccesslogs_display {
 6999:     my ($r,$uname,$udom,$permission,$brcrum) = @_;
 7000:     my $formname = 'accesslog';
 7001:     my $form = 'document.accesslog';
 7002: 
 7003: # set breadcrumbs
 7004:     my %breadcrumb_text = &singleuser_breadcrumb('','domain',$udom);
 7005:     my $prevphasestr;
 7006:     if ($env{'form.popup'}) {
 7007:         $brcrum = [];
 7008:     } else {
 7009:         push (@{$brcrum},
 7010:             {href => "javascript:backPage($form)",
 7011:              text => $breadcrumb_text{'search'}});
 7012:         my @prevphases;
 7013:         if ($env{'form.prevphases'}) {
 7014:             @prevphases = split(/,/,$env{'form.prevphases'});
 7015:             $prevphasestr = $env{'form.prevphases'};
 7016:         }
 7017:         if (($env{'form.phase'} eq 'userpicked') || (grep(/^userpicked$/,@prevphases))) {
 7018:             push(@{$brcrum},
 7019:                   {href => "javascript:backPage($form,'get_user_info','select')",
 7020:                    text => $breadcrumb_text{'userpicked'}});
 7021:             if ($env{'form.phase'} eq 'userpicked') {
 7022:                 $prevphasestr = 'userpicked';
 7023:             }
 7024:         }
 7025:     }
 7026:     push(@{$brcrum},
 7027:              {href => '/adm/createuser?action=accesslogs',
 7028:               text => 'User access logs',
 7029:               help => 'Domain_User_Access_Logs'});
 7030:     my $bread_crumbs_component = 'User Access Logs';
 7031:     my $args = { bread_crumbs           => $brcrum,
 7032:                  bread_crumbs_component => 'User Management'};
 7033:     if ($env{'form.popup'}) {
 7034:         $args->{'no_nav_bar'} = 1;
 7035:         $args->{'bread_crumbs_nomenu'} = 1;
 7036:     }
 7037: 
 7038: # set javascript
 7039:     my ($jsback,$elements) = &crumb_utilities();
 7040:     my $jsnav = &userlogdisplay_js($formname);
 7041: 
 7042:     my $jscript = (<<ENDSCRIPT);
 7043: <script type="text/javascript">
 7044: // <![CDATA[
 7045: 
 7046: $jsback
 7047: $jsnav
 7048: 
 7049: // ]]>
 7050: </script>
 7051: 
 7052: ENDSCRIPT
 7053: 
 7054: # print page header
 7055:     $r->print(&header($jscript,$args));
 7056: 
 7057: # early out unless log data can be displayed.
 7058:     unless ($permission->{'activity'}) {
 7059:         $r->print('<p class="LC_warning">'
 7060:                  .&mt('You do not have rights to display user access logs.')
 7061:                  .'</p>');
 7062:         if ($env{'form.popup'}) {
 7063:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
 7064:         } else {
 7065:             $r->print(&earlyout_accesslog_form($formname,$prevphasestr,$udom));
 7066:         }
 7067:         return;
 7068:     }
 7069: 
 7070:     unless ($udom eq $env{'request.role.domain'}) {
 7071:         $r->print('<p class="LC_warning">'
 7072:                  .&mt("User's domain must match role's domain")
 7073:                  .'</p>'
 7074:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
 7075:         return;
 7076:     }
 7077: 
 7078:     if (($uname eq '') || ($udom eq '')) {
 7079:         $r->print('<p class="LC_warning">'
 7080:                  .&mt('Invalid username or domain')
 7081:                  .'</p>'
 7082:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
 7083:         return;
 7084:     }
 7085: 
 7086:     if (&Apache::lonnet::privileged($uname,$udom,
 7087:                                     [$env{'request.role.domain'}],['dc','su'])) {
 7088:         unless (&Apache::lonnet::privileged($env{'user.name'},$env{'user.domain'},
 7089:                                             [$env{'request.role.domain'}],['dc','su'])) {
 7090:             $r->print('<p class="LC_warning">'
 7091:                  .&mt('You need to be a privileged user to display user access logs for [_1]',
 7092:                       &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),
 7093:                                                          $uname,$udom))
 7094:                  .'</p>');
 7095:             if ($env{'form.popup'}) {
 7096:                 $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
 7097:             } else {
 7098:                 $r->print(&earlyout_accesslog_form($formname,$prevphasestr,$udom));
 7099:             }
 7100:             return;
 7101:         }
 7102:     }
 7103: 
 7104: # set defaults
 7105:     my $now = time();
 7106:     my $defstart = $now - (7*24*3600);
 7107:     my %defaults = (
 7108:                      page                 => '1',
 7109:                      show                 => '10',
 7110:                      activity             => 'any',
 7111:                      accesslog_start_date => $defstart,
 7112:                      accesslog_end_date   => $now,
 7113:                    );
 7114:     my $more_records = 0;
 7115: 
 7116: # set current
 7117:     my %curr;
 7118:     foreach my $item ('show','page','activity') {
 7119:         $curr{$item} = $env{'form.'.$item};
 7120:     }
 7121:     my ($startdate,$enddate) =
 7122:         &Apache::lonuserutils::get_dates_from_form('accesslog_start_date','accesslog_end_date');
 7123:     $curr{'accesslog_start_date'} = $startdate;
 7124:     $curr{'accesslog_end_date'} = $enddate;
 7125:     foreach my $key (keys(%defaults)) {
 7126:         if ($curr{$key} eq '') {
 7127:             $curr{$key} = $defaults{$key};
 7128:         }
 7129:     }
 7130:     my ($minshown,$maxshown);
 7131:     $minshown = 1;
 7132:     my $count = 0;
 7133:     if ($curr{'show'} =~ /\D/) {
 7134:         $curr{'page'} = 1;
 7135:     } else {
 7136:         $maxshown = $curr{'page'} * $curr{'show'};
 7137:         if ($curr{'page'} > 1) {
 7138:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
 7139:         }
 7140:     }
 7141: 
 7142: # form header
 7143:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
 7144:               &activity_display_filter($formname,\%curr));
 7145: 
 7146:     my $showntableheader = 0;
 7147:     my ($nav_script,$nav_links);
 7148: 
 7149: # table header
 7150:     my $tableheader = '<h3>'.
 7151:         &mt('User access logs for: [_1]',
 7152:             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom)).'</h3>'
 7153:        .&Apache::loncommon::start_data_table_header_row()
 7154:        .'<th>&nbsp;</th>'
 7155:        .'<th>'.&mt('When').'</th>'
 7156:        .'<th>'.&mt('HostID').'</th>'
 7157:        .'<th>'.&mt('Event').'</th>'
 7158:        .'<th>'.&mt('Other data').'</th>'
 7159:        .&Apache::loncommon::end_data_table_header_row();
 7160: 
 7161:     my %filters=(
 7162:         start  => $curr{'accesslog_start_date'},
 7163:         end    => $curr{'accesslog_end_date'},
 7164:         action => $curr{'activity'},
 7165:     );
 7166: 
 7167:     my $reply = &Apache::lonnet::userlog_query($uname,$udom,%filters);
 7168:     unless ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 7169:         my (%courses,%missing);
 7170:         my @results = split(/\&/,$reply);
 7171:         foreach my $item (reverse(@results)) {
 7172:             my ($timestamp,$host,$event) = split(/:/,$item);
 7173:             next unless ($event =~ /^(Log|Role)/);
 7174:             if ($curr{'show'} !~ /\D/) {
 7175:                 if ($count >= $curr{'page'} * $curr{'show'}) {
 7176:                     $more_records = 1;
 7177:                     last;
 7178:                 }
 7179:             }
 7180:             $count ++;
 7181:             next if ($count < $minshown);
 7182:             unless ($showntableheader) {
 7183:                 $r->print($nav_script
 7184:                          .&Apache::loncommon::start_data_table()
 7185:                          .$tableheader);
 7186:                 $r->rflush();
 7187:                 $showntableheader = 1;
 7188:             }
 7189:             my ($shown,$extra);
 7190:             my ($event,$data) = split(/\s+/,&unescape($event),2);
 7191:             if ($event eq 'Role') {
 7192:                 my ($rolecode,$extent) = split(/\./,$data,2);
 7193:                 next if ($extent eq '');
 7194:                 my ($crstype,$desc,$info);
 7195:                 if ($extent =~ m{^/($match_domain)/($match_courseid)(?:/(\w+)|)$}) {
 7196:                     my ($cdom,$cnum,$sec) = ($1,$2,$3);
 7197:                     my $cid = $cdom.'_'.$cnum;
 7198:                     if (exists($courses{$cid})) {
 7199:                         $crstype = $courses{$cid}{'type'};
 7200:                         $desc = $courses{$cid}{'description'};
 7201:                     } elsif ($missing{$cid}) {
 7202:                         $crstype = 'Course';
 7203:                         $desc = 'Course/Community';
 7204:                     } else {
 7205:                         my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 7206:                         if (ref($crsinfo{$cdom.'_'.$cnum}) eq 'HASH') {
 7207:                             $courses{$cid} = $crsinfo{$cid};
 7208:                             $crstype = $crsinfo{$cid}{'type'};
 7209:                             $desc = $crsinfo{$cid}{'description'};
 7210:                         } else {
 7211:                             $missing{$cid} = 1;
 7212:                         }
 7213:                     }
 7214:                     $extra = &mt($crstype).': <a href="/public/'.$cdom.'/'.$cnum.'/syllabus">'.$desc.'</a>';
 7215:                     if ($sec ne '') {
 7216:                        $extra .= ' ('.&mt('Section: [_1]',$sec).')';
 7217:                     }
 7218:                 } elsif ($extent =~ m{^/($match_domain)/($match_username|$)}) {
 7219:                     my ($dom,$name) = ($1,$2);
 7220:                     if ($rolecode eq 'au') {
 7221:                         $extra = '';
 7222:                     } elsif ($rolecode =~ /^(ca|aa)$/) {
 7223:                         $extra = &mt('Authoring Space: [_1]',$name.':'.$dom);
 7224:                     } elsif ($rolecode =~ /^(li|dg|dh|dc|sc)$/) {
 7225:                         $extra = &mt('Domain: [_1]',$dom);
 7226:                     }
 7227:                 }
 7228:                 my $rolename;
 7229:                 if ($rolecode =~ m{^cr/($match_domain)/($match_username)/(\w+)}) {
 7230:                     my $role = $3;
 7231:                     my $owner = "($2:$1)";
 7232:                     if ($2 eq $1.'-domainconfig') {
 7233:                         $owner = '(ad hoc)';
 7234:                     }
 7235:                     $rolename = &mt('Custom role: [_1]',$role.' '.$owner);
 7236:                 } else {
 7237:                     $rolename = &Apache::lonnet::plaintext($rolecode,$crstype);
 7238:                 }
 7239:                 $shown = &mt('Role selection: [_1]',$rolename);
 7240:             } else {
 7241:                 $shown = &mt($event);
 7242:                 if ($data =~ /^webdav/) {
 7243:                     my ($path,$clientip) = split(/\s+/,$data,2);
 7244:                     $path =~ s/^webdav//;
 7245:                     if ($clientip ne '') {
 7246:                         $extra = &mt('Client IP address: [_1]',$clientip);
 7247:                     }
 7248:                     if ($path ne '') {
 7249:                         $shown .= ' '.&mt('(WebDAV access to [_1])',$path);
 7250:                     }
 7251:                 } elsif ($data ne '') {
 7252:                     $extra = &mt('Client IP address: [_1]',$data);
 7253:                 }
 7254:             }
 7255:             $r->print(
 7256:             &Apache::loncommon::start_data_table_row()
 7257:            .'<td>'.$count.'</td>'
 7258:            .'<td>'.&Apache::lonlocal::locallocaltime($timestamp).'</td>'
 7259:            .'<td>'.$host.'</td>'
 7260:            .'<td>'.$shown.'</td>'
 7261:            .'<td>'.$extra.'</td>'
 7262:            .&Apache::loncommon::end_data_table_row()."\n");
 7263:         }
 7264:     }
 7265: 
 7266:     if ($showntableheader) { # Table footer, if content displayed above
 7267:         $r->print(&Apache::loncommon::end_data_table().
 7268:                   &userlogdisplay_navlinks(\%curr,$more_records));
 7269:     } else { # No content displayed above
 7270:         $r->print('<p class="LC_info">'
 7271:                  .&mt('There are no records to display.')
 7272:                  .'</p>');
 7273:     }
 7274: 
 7275:     if ($env{'form.popup'} == 1) {
 7276:         $r->print('<input type="hidden" name="popup" value="1" />'."\n");
 7277:     }
 7278: 
 7279:     # Form Footer
 7280:     $r->print(
 7281:         '<input type="hidden" name="currstate" value="" />'
 7282:        .'<input type="hidden" name="accessuname" value="'.$uname.'" />'
 7283:        .'<input type="hidden" name="accessudom" value="'.$udom.'" />'
 7284:        .'<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
 7285:        .'<input type="hidden" name="prevphases" value="'.$prevphasestr.'" />'
 7286:        .'<input type="hidden" name="phase" value="activity" />'
 7287:        .'<input type="hidden" name="action" value="accesslogs" />'
 7288:        .'<input type="hidden" name="srchdomain" value="'.$udom.'" />'
 7289:        .'<input type="hidden" name="srchby" value="'.$env{'form.srchby'}.'" />'
 7290:        .'<input type="hidden" name="srchtype" value="'.$env{'form.srchtype'}.'" />'
 7291:        .'<input type="hidden" name="srchterm" value="'.&HTML::Entities::encode($env{'form.srchterm'},'<>"&').'" />'
 7292:        .'<input type="hidden" name="srchin" value="'.$env{'form.srchin'}.'" />'
 7293:        .'</form>');
 7294:     return;
 7295: }
 7296: 
 7297: sub earlyout_accesslog_form {
 7298:     my ($formname,$prevphasestr,$udom) = @_;
 7299:     my $srchterm = &HTML::Entities::encode($env{'form.srchterm'},'<>"&');
 7300:    return <<"END";
 7301: <form action="/adm/createuser" method="post" name="$formname">
 7302: <input type="hidden" name="currstate" value="" />
 7303: <input type="hidden" name="prevphases" value="$prevphasestr" />
 7304: <input type="hidden" name="phase" value="activity" />
 7305: <input type="hidden" name="action" value="accesslogs" />
 7306: <input type="hidden" name="srchdomain" value="$udom" />
 7307: <input type="hidden" name="srchby" value="$env{'form.srchby'}" />
 7308: <input type="hidden" name="srchtype" value="$env{'form.srchtype'}" />
 7309: <input type="hidden" name="srchterm" value="$srchterm" />
 7310: <input type="hidden" name="srchin" value="$env{'form.srchin'}" />
 7311: </form>
 7312: END
 7313: }
 7314: 
 7315: sub activity_display_filter {
 7316:     my ($formname,$curr) = @_;
 7317:     my $nolink = 1;
 7318:     my $output = '<table><tr><td valign="top">'.
 7319:                  '<span class="LC_nobreak"><b>'.&mt('Actions/page:').'</b></span><br />'.
 7320:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
 7321:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
 7322:                  '</td><td>&nbsp;&nbsp;</td>';
 7323:     my $startform =
 7324:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_start_date',
 7325:                                             $curr->{'accesslog_start_date'},undef,
 7326:                                             undef,undef,undef,undef,undef,undef,$nolink);
 7327:     my $endform =
 7328:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_end_date',
 7329:                                             $curr->{'accesslog_end_date'},undef,
 7330:                                             undef,undef,undef,undef,undef,undef,$nolink);
 7331:     my %lt = &Apache::lonlocal::texthash (
 7332:                                           activity => 'Activity',
 7333:                                           Role     => 'Role selection',
 7334:                                           log      => 'Log-in or Logout',
 7335:     );
 7336:     $output .= '<td valign="top"><b>'.&mt('Window during which actions occurred:').'</b><br />'.
 7337:                '<table><tr><td>'.&mt('After:').
 7338:                '</td><td>'.$startform.'</td></tr>'.
 7339:                '<tr><td>'.&mt('Before:').'</td>'.
 7340:                '<td>'.$endform.'</td></tr></table>'.
 7341:                '</td>'.
 7342:                '<td>&nbsp;&nbsp;</td>'.
 7343:                '<td valign="top"><b>'.&mt('Activities').'</b><br />'.
 7344:                '<select name="activity"><option value="any"';
 7345:     if ($curr->{'activity'} eq 'any') {
 7346:         $output .= ' selected="selected"';
 7347:     }
 7348:     $output .= '>'.&mt('Any').'</option>'."\n";
 7349:     foreach my $activity ('Role','log') {
 7350:         my $selstr = '';
 7351:         if ($activity eq $curr->{'activity'}) {
 7352:             $selstr = ' selected="selected"';
 7353:         }
 7354:         $output .= '<option value="'.$activity.'"'.$selstr.'>'.$lt{$activity}.'</option>';
 7355:     }
 7356:     $output .= '</select></td>'.
 7357:                '</tr></table>';
 7358:     # Update Display button
 7359:     $output .= '<p>'
 7360:               .'<input type="submit" value="'.&mt('Update Display').'" />'
 7361:               .'</p><hr />';
 7362:     return $output;
 7363: }
 7364: 
 7365: sub userlogdisplay_js {
 7366:     my ($formname) = @_;
 7367:     return <<"ENDSCRIPT";
 7368: 
 7369: function chgPage(caller) {
 7370:     if (caller == 'previous') {
 7371:         document.$formname.page.value --;
 7372:     }
 7373:     if (caller == 'next') {
 7374:         document.$formname.page.value ++;
 7375:     }
 7376:     document.$formname.submit();
 7377:     return;
 7378: }
 7379: ENDSCRIPT
 7380: }
 7381: 
 7382: sub userlogdisplay_navlinks {
 7383:     my ($curr,$more_records) = @_;
 7384:     return unless(ref($curr) eq 'HASH');
 7385:     # Navigation Buttons
 7386:     my $nav_links = '<p>';
 7387:     if (($curr->{'page'} > 1) || ($more_records)) {
 7388:         if (($curr->{'page'} > 1) && ($curr->{'show'} !~ /\D/)) {
 7389:             $nav_links .= '<input type="button"'
 7390:                          .' onclick="javascript:chgPage('."'previous'".');"'
 7391:                          .' value="'.&mt('Previous [_1] changes',$curr->{'show'})
 7392:                          .'" /> ';
 7393:         }
 7394:         if ($more_records) {
 7395:             $nav_links .= '<input type="button"'
 7396:                          .' onclick="javascript:chgPage('."'next'".');"'
 7397:                          .' value="'.&mt('Next [_1] changes',$curr->{'show'})
 7398:                          .'" />';
 7399:         }
 7400:     }
 7401:     $nav_links .= '</p>';
 7402:     return $nav_links;
 7403: }
 7404: 
 7405: sub role_display_filter {
 7406:     my ($context,$formname,$cdom,$cnum,$curr,$version,$crstype) = @_;
 7407:     my $lctype;
 7408:     if ($context eq 'course') {
 7409:         $lctype = lc($crstype);
 7410:     }
 7411:     my $nolink = 1;
 7412:     my $output = '<table><tr><td valign="top">'.
 7413:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b></span><br />'.
 7414:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
 7415:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
 7416:                  '</td><td>&nbsp;&nbsp;</td>';
 7417:     my $startform =
 7418:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
 7419:                                             $curr->{'rolelog_start_date'},undef,
 7420:                                             undef,undef,undef,undef,undef,undef,$nolink);
 7421:     my $endform =
 7422:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
 7423:                                             $curr->{'rolelog_end_date'},undef,
 7424:                                             undef,undef,undef,undef,undef,undef,$nolink);
 7425:     my %lt = &rolechg_contexts($context,$crstype);
 7426:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br />'.
 7427:                '<table><tr><td>'.&mt('After:').
 7428:                '</td><td>'.$startform.'</td></tr>'.
 7429:                '<tr><td>'.&mt('Before:').'</td>'.
 7430:                '<td>'.$endform.'</td></tr></table>'.
 7431:                '</td>'.
 7432:                '<td>&nbsp;&nbsp;</td>'.
 7433:                '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
 7434:                '<select name="role"><option value="any"';
 7435:     if ($curr->{'role'} eq 'any') {
 7436:         $output .= ' selected="selected"';
 7437:     }
 7438:     $output .=  '>'.&mt('Any').'</option>'."\n";
 7439:     my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
 7440:     foreach my $role (@roles) {
 7441:         my $plrole;
 7442:         if ($role eq 'cr') {
 7443:             $plrole = &mt('Custom Role');
 7444:         } else {
 7445:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
 7446:         }
 7447:         my $selstr = '';
 7448:         if ($role eq $curr->{'role'}) {
 7449:             $selstr = ' selected="selected"';
 7450:         }
 7451:         $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
 7452:     }
 7453:     $output .= '</select></td>'.
 7454:                '<td>&nbsp;&nbsp;</td>'.
 7455:                '<td valign="top"><b>'.
 7456:                &mt('Context:').'</b><br /><select name="chgcontext">';
 7457:     my @posscontexts;
 7458:     if ($context eq 'course') {
 7459:         @posscontexts = ('any','automated','updatenow','createcourse','course','domain','selfenroll','requestcourses');
 7460:     } elsif ($context eq 'domain') {
 7461:         @posscontexts = ('any','domain','requestauthor','domconfig','server');
 7462:     } else {
 7463:         @posscontexts = ('any','author','domain');
 7464:     } 
 7465:     foreach my $chgtype (@posscontexts) {
 7466:         my $selstr = '';
 7467:         if ($curr->{'chgcontext'} eq $chgtype) {
 7468:             $selstr = ' selected="selected"';
 7469:         }
 7470:         if ($context eq 'course') {
 7471:             if (($chgtype eq 'automated') || ($chgtype eq 'updatenow')) {
 7472:                 next if (!&Apache::lonnet::auto_run($cnum,$cdom));
 7473:             }
 7474:         }
 7475:         $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
 7476:     }
 7477:     $output .= '</select></td>'
 7478:               .'</tr></table>';
 7479: 
 7480:     # Update Display button
 7481:     $output .= '<p>'
 7482:               .'<input type="submit" value="'.&mt('Update Display').'" />'
 7483:               .'</p>';
 7484: 
 7485:     # Server version info
 7486:     my $needsrev = '2.11.0';
 7487:     if ($context eq 'course') {
 7488:         $needsrev = '2.7.0';
 7489:     }
 7490:     
 7491:     $output .= '<p class="LC_info">'
 7492:               .&mt('Only changes made from servers running LON-CAPA [_1] or later are displayed.'
 7493:                   ,$needsrev);
 7494:     if ($version) {
 7495:         $output .= ' '.&mt('This LON-CAPA server is version [_1]',$version);
 7496:     }
 7497:     $output .= '</p><hr />';
 7498:     return $output;
 7499: }
 7500: 
 7501: sub rolechg_contexts {
 7502:     my ($context,$crstype) = @_;
 7503:     my %lt;
 7504:     if ($context eq 'course') {
 7505:         %lt = &Apache::lonlocal::texthash (
 7506:                                              any          => 'Any',
 7507:                                              automated    => 'Automated Enrollment',
 7508:                                              updatenow    => 'Roster Update',
 7509:                                              createcourse => 'Course Creation',
 7510:                                              course       => 'User Management in course',
 7511:                                              domain       => 'User Management in domain',
 7512:                                              selfenroll   => 'Self-enrolled',
 7513:                                              requestcourses => 'Course Request',
 7514:                                          );
 7515:         if ($crstype eq 'Community') {
 7516:             $lt{'createcourse'} = &mt('Community Creation');
 7517:             $lt{'course'} = &mt('User Management in community');
 7518:             $lt{'requestcourses'} = &mt('Community Request');
 7519:         }
 7520:     } elsif ($context eq 'domain') {
 7521:         %lt = &Apache::lonlocal::texthash (
 7522:                                              any           => 'Any',
 7523:                                              domain        => 'User Management in domain',
 7524:                                              requestauthor => 'Authoring Request',
 7525:                                              server        => 'Command line script (DC role)',
 7526:                                              domconfig     => 'Self-enrolled',
 7527:                                          );
 7528:     } else {
 7529:         %lt = &Apache::lonlocal::texthash (
 7530:                                              any    => 'Any',
 7531:                                              domain => 'User Management in domain',
 7532:                                              author => 'User Management by author',
 7533:                                          );
 7534:     } 
 7535:     return %lt;
 7536: }
 7537: 
 7538: sub print_helpdeskaccess_display {
 7539:     my ($r,$permission,$brcrum) = @_;
 7540:     my $formname = 'helpdeskaccess';
 7541:     my $helpitem = 'Course_Helpdesk_Access';
 7542:     push (@{$brcrum},
 7543:              {href => '/adm/createuser?action=helpdesk',
 7544:               text => 'Helpdesk Access',
 7545:               help => $helpitem});
 7546:     my $bread_crumbs_component = 'Helpdesk Staff Access';
 7547:     my $args = { bread_crumbs           => $brcrum,
 7548:                  bread_crumbs_component => $bread_crumbs_component};
 7549: 
 7550:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7551:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7552:     my $confname = $cdom.'-domainconfig';
 7553:     my $crstype = &Apache::loncommon::course_type();
 7554: 
 7555:     my @accesstypes = ('all','dh','da','none');
 7556:     my ($numstatustypes,@jsarray);
 7557:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($cdom);
 7558:     if (ref($types) eq 'ARRAY') {
 7559:         if (@{$types} > 0) {
 7560:             $numstatustypes = scalar(@{$types});
 7561:             push(@accesstypes,'status');
 7562:             @jsarray = ('bystatus');
 7563:         }
 7564:     }
 7565:     my %customroles = &get_domain_customroles($cdom,$confname);
 7566:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($cdom,['dh','da']);
 7567:     if (keys(%domhelpdesk)) {
 7568:        push(@accesstypes,('inc','exc'));
 7569:        push(@jsarray,('notinc','notexc'));
 7570:     }
 7571:     push(@jsarray,'privs');
 7572:     my $hiddenstr = join("','",@jsarray);
 7573:     my $rolestr = join("','",sort(keys(%customroles)));
 7574: 
 7575:     my $jscript;
 7576:     my (%settings,%overridden);
 7577:     if (keys(%customroles)) {
 7578:         &get_adhocrole_settings($env{'request.course.id'},\@accesstypes,
 7579:                                 $types,\%customroles,\%settings,\%overridden);
 7580:         my %jsfull=();
 7581:         my %jslevels= (
 7582:                      course => {},
 7583:                      domain => {},
 7584:                      system => {},
 7585:                     );
 7586:         my %jslevelscurrent=(
 7587:                            course => {},
 7588:                            domain => {},
 7589:                            system => {},
 7590:                           );
 7591:         my (%privs,%jsprivs);
 7592:         &Apache::lonuserutils::custom_role_privs(\%privs,\%jsfull,\%jslevels,\%jslevelscurrent);
 7593:         foreach my $priv (keys(%jsfull)) {
 7594:             if ($jslevels{'course'}{$priv}) {
 7595:                 $jsprivs{$priv} = 1;
 7596:             }
 7597:         }
 7598:         my (%elements,%stored);
 7599:         foreach my $role (keys(%customroles)) {
 7600:             $elements{$role.'_access'} = 'radio';
 7601:             $elements{$role.'_incrs'} = 'radio';
 7602:             if ($numstatustypes) {
 7603:                 $elements{$role.'_status'} = 'checkbox';
 7604:             }
 7605:             if (keys(%domhelpdesk) > 0) {
 7606:                 $elements{$role.'_staff_inc'} = 'checkbox';
 7607:                 $elements{$role.'_staff_exc'} = 'checkbox';
 7608:             }
 7609:             $elements{$role.'_override'} = 'checkbox';
 7610:             if (ref($settings{$role}) eq 'HASH') {
 7611:                 if ($settings{$role}{'access'} ne '') {
 7612:                     my $curraccess = $settings{$role}{'access'};
 7613:                     $stored{$role.'_access'} = $curraccess;
 7614:                     $stored{$role.'_incrs'} = 1;
 7615:                     if ($curraccess eq 'status') {
 7616:                         if (ref($settings{$role}{'status'}) eq 'ARRAY') {
 7617:                             $stored{$role.'_status'} = $settings{$role}{'status'};
 7618:                         }
 7619:                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 7620:                         if (ref($settings{$role}{$curraccess}) eq 'ARRAY') {
 7621:                             $stored{$role.'_staff_'.$curraccess} = $settings{$role}{$curraccess};
 7622:                         }
 7623:                     }
 7624:                 } else {
 7625:                     $stored{$role.'_incrs'} = 0;
 7626:                 }
 7627:                 $stored{$role.'_override'} = [];
 7628:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.adhocpriv.'.$role}) {
 7629:                     if (ref($settings{$role}{'off'}) eq 'ARRAY') {
 7630:                         foreach my $priv (@{$settings{$role}{'off'}}) {
 7631:                             push(@{$stored{$role.'_override'}},$priv);
 7632:                         }
 7633:                     }
 7634:                     if (ref($settings{$role}{'on'}) eq 'ARRAY') {
 7635:                         foreach my $priv (@{$settings{$role}{'on'}}) {
 7636:                             unless (grep(/^$priv$/,@{$stored{$role.'_override'}})) {
 7637:                                 push(@{$stored{$role.'_override'}},$priv);
 7638:                             }
 7639:                         }
 7640:                     }
 7641:                 }
 7642:             } else {
 7643:                 $stored{$role.'_incrs'} = 0;
 7644:             }
 7645:         }
 7646:         $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements,\%stored);
 7647:     }
 7648: 
 7649:     my $js = <<"ENDJS";
 7650: <script type="text/javascript">
 7651: // <![CDATA[
 7652: $jscript;
 7653: 
 7654: function switchRoleTab(caller,role) {
 7655:     if (document.getElementById(role+'_maindiv')) {
 7656:         if (caller.id != 'LC_current_minitab') {
 7657:             if (document.getElementById('LC_current_minitab')) {
 7658:                 document.getElementById('LC_current_minitab').id=null;
 7659:             }
 7660:             var roledivs = Array('$rolestr');
 7661:             if (roledivs.length > 0) {
 7662:                 for (var i=0; i<roledivs.length; i++) {
 7663:                     if (document.getElementById(roledivs[i]+'_maindiv')) {
 7664:                         document.getElementById(roledivs[i]+'_maindiv').style.display='none';
 7665:                     }
 7666:                 }
 7667:             }
 7668:             caller.id = 'LC_current_minitab';
 7669:             document.getElementById(role+'_maindiv').style.display='block';
 7670:         }
 7671:     }
 7672:     return false;
 7673: }
 7674: 
 7675: function helpdeskAccess(role) {
 7676:     var curraccess = null;
 7677:     if (document.$formname.elements[role+'_access'].length) {
 7678:         for (var i=0; i<document.$formname.elements[role+'_access'].length; i++) {
 7679:             if (document.$formname.elements[role+'_access'][i].checked) {
 7680:                 curraccess = document.$formname.elements[role+'_access'][i].value;
 7681:             }
 7682:         }
 7683:     }
 7684:     var shown = Array();
 7685:     var hidden = Array();
 7686:     if (curraccess == 'none') {
 7687:         hidden = Array ('$hiddenstr');
 7688:     } else {
 7689:         if (curraccess == 'status') {
 7690:             shown = Array ('bystatus','privs');
 7691:             hidden = Array ('notinc','notexc');
 7692:         } else {
 7693:             if (curraccess == 'exc') {
 7694:                 shown = Array ('notexc','privs');
 7695:                 hidden = Array ('notinc','bystatus');
 7696:             }
 7697:             if (curraccess == 'inc') {
 7698:                 shown = Array ('notinc','privs');
 7699:                 hidden = Array ('notexc','bystatus');
 7700:             }
 7701:             if (curraccess == 'all') {
 7702:                 shown = Array ('privs');
 7703:                 hidden = Array ('notinc','notexc','bystatus');
 7704:             }
 7705:         }
 7706:     }
 7707:     if (hidden.length > 0) {
 7708:         for (var i=0; i<hidden.length; i++) {
 7709:             if (document.getElementById(role+'_'+hidden[i])) {
 7710:                 document.getElementById(role+'_'+hidden[i]).style.display = 'none';
 7711:             }
 7712:         }
 7713:     }
 7714:     if (shown.length > 0) {
 7715:         for (var i=0; i<shown.length; i++) {
 7716:             if (document.getElementById(role+'_'+shown[i])) {
 7717:                 if (shown[i] == 'privs') {
 7718:                     document.getElementById(role+'_'+shown[i]).style.display = 'block';
 7719:                 } else {
 7720:                     document.getElementById(role+'_'+shown[i]).style.display = 'inline';
 7721:                 }
 7722:             }
 7723:         }
 7724:     }
 7725:     return;
 7726: }
 7727: 
 7728: function toggleAccess(role) {
 7729:     if ((document.getElementById(role+'_setincrs')) &&
 7730:         (document.getElementById(role+'_setindom'))) {
 7731:         for (var i=0; i<document.$formname.elements[role+'_incrs'].length; i++) {
 7732:             if (document.$formname.elements[role+'_incrs'][i].checked) {
 7733:                 if (document.$formname.elements[role+'_incrs'][i].value == 1) {
 7734:                     document.getElementById(role+'_setindom').style.display = 'none';
 7735:                     document.getElementById(role+'_setincrs').style.display = 'block';
 7736:                 } else {
 7737:                     document.getElementById(role+'_setincrs').style.display = 'none';
 7738:                     document.getElementById(role+'_setindom').style.display = 'block';
 7739:                 }
 7740:                 break;
 7741:             }
 7742:         }
 7743:     }
 7744:     return;
 7745: }
 7746: 
 7747: // ]]>
 7748: </script>
 7749: ENDJS
 7750: 
 7751:     $args->{add_entries} = {onload => "javascript:setFormElements(document.$formname)"};
 7752: 
 7753:     # print page header
 7754:     $r->print(&header($js,$args));
 7755:     # print form header
 7756:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">');
 7757: 
 7758:     if (keys(%customroles)) {
 7759:         my %lt = &Apache::lonlocal::texthash(
 7760:                     'aco'    => 'As course owner you may override the defaults set in the domain for role usage and/or privileges.',
 7761:                     'rou'    => 'Role usage',
 7762:                     'whi'    => 'Which helpdesk personnel may use this role?',
 7763:                     'udd'    => 'Use domain default',
 7764:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 7765:                     'dh'     => 'All with domain helpdesk role',
 7766:                     'da'     => 'All with domain helpdesk assistant role',
 7767:                     'none'   => 'None',
 7768:                     'status' => 'Determined based on institutional status',
 7769:                     'inc'    => 'Include all, but exclude specific personnel',
 7770:                     'exc'    => 'Exclude all, but include specific personnel',
 7771:                     'hel'    => 'Helpdesk',
 7772:                     'rpr'    => 'Role privileges',
 7773:                  );
 7774:         $lt{'tfh'} = &mt("Custom [_1]ad hoc[_2] course roles available for use by the domain's helpdesk are as follows",'<i>','</i>');
 7775:         my %domconfig = &Apache::lonnet::get_dom('configuration',['helpsettings'],$cdom);
 7776:         my (%domcurrent,%ordered,%description,%domusage,$disabled);
 7777:         if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7778:             if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
 7779:                 %domcurrent = %{$domconfig{'helpsettings'}{'adhoc'}};
 7780:             }
 7781:         }
 7782:         my $count = 0;
 7783:         foreach my $role (sort(keys(%customroles))) {
 7784:             my ($order,$desc,$access_in_dom);
 7785:             if (ref($domcurrent{$role}) eq 'HASH') {
 7786:                 $order = $domcurrent{$role}{'order'};
 7787:                 $desc = $domcurrent{$role}{'desc'};
 7788:                 $access_in_dom = $domcurrent{$role}{'access'};
 7789:             }
 7790:             if ($order eq '') {
 7791:                 $order = $count;
 7792:             }
 7793:             $ordered{$order} = $role;
 7794:             if ($desc ne '') {
 7795:                 $description{$role} = $desc;
 7796:             } else {
 7797:                 $description{$role}= $role;
 7798:             }
 7799:             $count++;
 7800:         }
 7801:         %domusage = &domain_adhoc_access(\%customroles,\%domcurrent,\@accesstypes,$usertypes,$othertitle);
 7802:         my @roles_by_num = ();
 7803:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 7804:             push(@roles_by_num,$ordered{$item});
 7805:         }
 7806:         $r->print('<p>'.$lt{'tfh'}.': <i>'.join('</i>, <i>',map { $description{$_}; } @roles_by_num).'</i>.');
 7807:         if ($permission->{'owner'}) {
 7808:             $r->print('<br />'.$lt{'aco'}.'</p><p>');
 7809:             $r->print('<input type="hidden" name="state" value="process" />'.
 7810:                       '<input type="submit" value="'.&mt('Save changes').'" />');
 7811:         } else {
 7812:             if ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'}) {
 7813:                 my ($ownername,$ownerdom) = split(/:/,$env{'course.'.$env{'request.course.id'}.'.internal.courseowner'});
 7814:                 $r->print('<br />'.&mt('The course owner -- [_1] -- can override the default access and/or privileges for these ad hoc roles.',
 7815:                                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($ownername,$ownerdom),$ownername,$ownerdom)));
 7816:             }
 7817:             $disabled = ' disabled="disabled"';
 7818:         }
 7819:         $r->print('</p>');
 7820: 
 7821:         $r->print('<div id="LC_minitab_header"><ul>');
 7822:         my $count = 0;
 7823:         my %visibility;
 7824:         foreach my $role (@roles_by_num) {
 7825:             my $id;
 7826:             if ($count == 0) {
 7827:                 $id=' id="LC_current_minitab"';
 7828:                 $visibility{$role} = ' style="display:block"';
 7829:             } else {
 7830:                 $visibility{$role} = ' style="display:none"';
 7831:             }
 7832:             $count ++;
 7833:             $r->print('<li'.$id.'><a href="#" onclick="javascript:switchRoleTab(this.parentNode,'."'$role'".');">'.$description{$role}.'</a></li>');
 7834:         }
 7835:         $r->print('</ul></div>');
 7836: 
 7837:         foreach my $role (@roles_by_num) {
 7838:             my %usecheck = (
 7839:                              all => ' checked="checked"',
 7840:                            );
 7841:             my %displaydiv = (
 7842:                                 status => 'none',
 7843:                                 inc    => 'none',
 7844:                                 exc    => 'none',
 7845:                                 priv   => 'block',
 7846:                              );
 7847:             my (%selected,$overridden,$incrscheck,$indomcheck,$indomvis,$incrsvis);
 7848:             if (ref($settings{$role}) eq 'HASH') {
 7849:                 if ($settings{$role}{'access'} ne '') {
 7850:                     $indomvis = ' style="display:none"';
 7851:                     $incrsvis = ' style="display:block"';
 7852:                     $incrscheck = ' checked="checked"';
 7853:                     if ($settings{$role}{'access'} ne 'all') {
 7854:                         $usecheck{$settings{$role}{'access'}} = $usecheck{'all'};
 7855:                         delete($usecheck{'all'});
 7856:                         if ($settings{$role}{'access'} eq 'status') {
 7857:                             my $access = 'status';
 7858:                             $displaydiv{$access} = 'inline';
 7859:                             if (ref($settings{$role}{$access}) eq 'ARRAY') {
 7860:                                 $selected{$access} = $settings{$role}{$access};
 7861:                             }
 7862:                         } elsif ($settings{$role}{'access'} =~ /^(inc|exc)$/) {
 7863:                             my $access = $1;
 7864:                             $displaydiv{$access} = 'inline';
 7865:                             if (ref($settings{$role}{$access}) eq 'ARRAY') {
 7866:                                 $selected{$access} = $settings{$role}{$access};
 7867:                             }
 7868:                         } elsif ($settings{$role}{'access'} eq 'none') {
 7869:                             $displaydiv{'priv'} = 'none';
 7870:                         }
 7871:                     }
 7872:                 } else {
 7873:                     $indomcheck = ' checked="checked"';
 7874:                     $indomvis = ' style="display:block"';
 7875:                     $incrsvis = ' style="display:none"';
 7876:                 }
 7877:             } else {
 7878:                 $indomcheck = ' checked="checked"';
 7879:                 $indomvis = ' style="display:block"';
 7880:                 $incrsvis = ' style="display:none"';
 7881:             }
 7882:             $r->print('<div class="LC_left_float" id="'.$role.'_maindiv"'.$visibility{$role}.'>'.
 7883:                       '<fieldset><legend>'.$lt{'rou'}.'</legend>'.
 7884:                       '<p>'.$lt{'whi'}.' <span class="LC_nobreak">'.
 7885:                       '<label><input type="radio" name="'.$role.'_incrs" value="1"'.$incrscheck.' onclick="toggleAccess('."'$role'".');"'.$disabled.'>'.
 7886:                       &mt('Set here in [_1]',lc($crstype)).'</label>'.
 7887:                       '<span>'.('&nbsp;'x2).
 7888:                       '<label><input type="radio" name="'.$role.'_incrs" value="0"'.$indomcheck.' onclick="toggleAccess('."'$role'".');"'.$disabled.'>'.
 7889:                       $lt{'udd'}.'</label><span></p>'.
 7890:                       '<div id="'.$role.'_setindom"'.$indomvis.'>'.
 7891:                       '<span class="LC_cusr_emph">'.$domusage{$role}.'</span></div>'.
 7892:                       '<div id="'.$role.'_setincrs"'.$incrsvis.'>');
 7893:             foreach my $access (@accesstypes) {
 7894:                 $r->print('<p><label><input type="radio" name="'.$role.'_access" value="'.$access.'" '.$usecheck{$access}.
 7895:                           ' onclick="helpdeskAccess('."'$role'".');"'.$disabled.' />'.$lt{$access}.'</label>');
 7896:                 if ($access eq 'status') {
 7897:                     $r->print('<div id="'.$role.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 7898:                               &Apache::lonuserutils::adhoc_status_types($cdom,undef,$role,$selected{$access},
 7899:                                                                         $othertitle,$usertypes,$types,$disabled).
 7900:                               '</div>');
 7901:                 } elsif (($access eq 'inc') && (keys(%domhelpdesk) > 0)) {
 7902:                     $r->print('<div id="'.$role.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 7903:                               &Apache::lonuserutils::adhoc_staff($access,undef,$role,$selected{$access},
 7904:                                                                  \%domhelpdesk,$disabled).
 7905:                               '</div>');
 7906:                 } elsif (($access eq 'exc') && (keys(%domhelpdesk) > 0)) {
 7907:                     $r->print('<div id="'.$role.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 7908:                               &Apache::lonuserutils::adhoc_staff($access,undef,$role,$selected{$access},
 7909:                                                                  \%domhelpdesk,$disabled).
 7910:                               '</div>');
 7911:                 }
 7912:                 $r->print('</p>');
 7913:             }
 7914:             $r->print('</div></fieldset>');
 7915:             my %full=();
 7916:             my %levels= (
 7917:                          course => {},
 7918:                          domain => {},
 7919:                          system => {},
 7920:                         );
 7921:             my %levelscurrent=(
 7922:                                course => {},
 7923:                                domain => {},
 7924:                                system => {},
 7925:                               );
 7926:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 7927:             $r->print('<fieldset id="'.$role.'_privs" style="display:'.$displaydiv{'priv'}.'">'.
 7928:                       '<legend>'.$lt{'rpr'}.'</legend>'.
 7929:                       &role_priv_table($role,$permission,$crstype,\%full,\%levels,\%levelscurrent,$overridden{$role}).
 7930:                       '</fieldset></div><div style="padding:0;clear:both;margin:0;border:0"></div>');
 7931:         }
 7932:         if ($permission->{'owner'}) {
 7933:             $r->print('<p><input type="submit" value="'.&mt('Save changes').'" /></p>');
 7934:         }
 7935:     } else {
 7936:         $r->print(&mt('Helpdesk roles have not yet been created in this domain.'));
 7937:     }
 7938:     # Form Footer
 7939:     $r->print('<input type="hidden" name="action" value="helpdesk" />'
 7940:              .'</form>');
 7941:     return;
 7942: }
 7943: 
 7944: sub domain_adhoc_access {
 7945:     my ($roles,$domcurrent,$accesstypes,$usertypes,$othertitle) = @_;
 7946:     my %domusage;
 7947:     return unless ((ref($roles) eq 'HASH') && (ref($domcurrent) eq 'HASH') && (ref($accesstypes) eq 'ARRAY'));
 7948:     foreach my $role (keys(%{$roles})) {
 7949:         if (ref($domcurrent->{$role}) eq 'HASH') {
 7950:             my $access = $domcurrent->{$role}{'access'};
 7951:             if (($access eq '') || (!grep(/^\Q$access\E$/,@{$accesstypes}))) {
 7952:                 $access = 'all';
 7953:                 $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role',&Apache::lonnet::plaintext('dh'),
 7954:                                                                                           &Apache::lonnet::plaintext('da'));
 7955:             } elsif ($access eq 'status') {
 7956:                 if (ref($domcurrent->{$role}{$access}) eq 'ARRAY') {
 7957:                     my @shown;
 7958:                     foreach my $type (@{$domcurrent->{$role}{$access}}) {
 7959:                         unless ($type eq 'default') {
 7960:                             if ($usertypes->{$type}) {
 7961:                                 push(@shown,$usertypes->{$type});
 7962:                             }
 7963:                         }
 7964:                     }
 7965:                     if (grep(/^default$/,@{$domcurrent->{$role}{$access}})) {
 7966:                         push(@shown,$othertitle);
 7967:                     }
 7968:                     if (@shown) {
 7969:                         my $shownstatus = join(' '.&mt('or').' ',@shown);
 7970:                         $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role, and institutional status: [_3]',
 7971:                                                &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'),$shownstatus);
 7972:                     } else {
 7973:                         $domusage{$role} = &mt('No one in the domain');
 7974:                     }
 7975:                 }
 7976:             } elsif ($access eq 'inc') {
 7977:                 my @dominc = ();
 7978:                 if (ref($domcurrent->{$role}{'inc'}) eq 'ARRAY') {
 7979:                     foreach my $user (@{$domcurrent->{$role}{'inc'}}) {
 7980:                         my ($uname,$udom) = split(/:/,$user);
 7981:                         push(@dominc,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom));
 7982:                     }
 7983:                     my $showninc = join(', ',@dominc);
 7984:                     if ($showninc ne '') {
 7985:                         $domusage{$role} = &mt('Include any user in domain with active [_1] or [_2] role, except: [_3]',
 7986:                                                &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'),$showninc);
 7987:                     } else {
 7988:                         $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role',
 7989:                                                &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'));
 7990:                     }
 7991:                 }
 7992:             } elsif ($access eq 'exc') {
 7993:                 my @domexc = ();
 7994:                 if (ref($domcurrent->{$role}{'exc'}) eq 'ARRAY') {
 7995:                     foreach my $user (@{$domcurrent->{$role}{'exc'}}) {
 7996:                         my ($uname,$udom) = split(/:/,$user);
 7997:                         push(@domexc,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom));
 7998:                     }
 7999:                 }
 8000:                 my $shownexc = join(', ',@domexc);
 8001:                 if ($shownexc ne '') {
 8002:                     $domusage{$role} = &mt('Only the following in the domain with active [_1] or [_2] role: [_3]',
 8003:                                            &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'),$shownexc);
 8004:                 } else {
 8005:                     $domusage{$role} = &mt('No one in the domain');
 8006:                 }
 8007:             } elsif ($access eq 'none') {
 8008:                 $domusage{$role} = &mt('No one in the domain');
 8009:             } elsif ($access eq 'dh') {
 8010:                 $domusage{$role} = &mt('Any user in domain with active [_1] role',&Apache::lonnet::plaintext('dh'));
 8011:             } elsif ($access eq 'da') {
 8012:                 $domusage{$role} = &mt('Any user in domain with active [_1] role',&Apache::lonnet::plaintext('da'));
 8013:             } elsif ($access eq 'all') {
 8014:                 $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role',
 8015:                                        &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'));
 8016:             }
 8017:         } else {
 8018:             $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role',
 8019:                                    &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'));
 8020:         }
 8021:     }
 8022:     return %domusage;
 8023: }
 8024: 
 8025: sub get_domain_customroles {
 8026:     my ($cdom,$confname) = @_;
 8027:     my %existing=&Apache::lonnet::dump('roles',$cdom,$confname,'rolesdef_');
 8028:     my %customroles;
 8029:     foreach my $key (keys(%existing)) {
 8030:         if ($key=~/^rolesdef\_(\w+)$/) {
 8031:             my $rolename = $1;
 8032:             my %privs;
 8033:             ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 8034:             $customroles{$rolename} = \%privs;
 8035:         }
 8036:     }
 8037:     return %customroles;
 8038: }
 8039: 
 8040: sub role_priv_table {
 8041:     my ($role,$permission,$crstype,$full,$levels,$levelscurrent,$overridden) = @_;
 8042:     return unless ((ref($full) eq 'HASH') && (ref($levels) eq 'HASH') &&
 8043:                    (ref($levelscurrent) eq 'HASH'));
 8044:     my %lt=&Apache::lonlocal::texthash (
 8045:                     'crl'  => 'Course Level Privilege',
 8046:                     'def'  => 'Domain Defaults',
 8047:                     'ove'  => 'Override in Course',
 8048:                     'ine'  => 'In effect',
 8049:                     'dis'  => 'Disabled',
 8050:                     'ena'  => 'Enabled',
 8051:                    );
 8052:     if ($crstype eq 'Community') {
 8053:         $lt{'ove'} = 'Override in Community',
 8054:     }
 8055:     my @status = ('Disabled','Enabled');
 8056:     my (%on,%off);
 8057:     if (ref($overridden) eq 'HASH') {
 8058:         if (ref($overridden->{'on'}) eq 'ARRAY') {
 8059:             map { $on{$_} = 1; } (@{$overridden->{'on'}});
 8060:         }
 8061:         if (ref($overridden->{'off'}) eq 'ARRAY') {
 8062:             map { $off{$_} = 1; } (@{$overridden->{'off'}});
 8063:         }
 8064:     }
 8065:     my $output=&Apache::loncommon::start_data_table().
 8066:                &Apache::loncommon::start_data_table_header_row().
 8067:                '<th>'.$lt{'crl'}.'</th><th>'.$lt{'def'}.'</th><th>'.$lt{'ove'}.
 8068:                '</th><th>'.$lt{'ine'}.'</th>'.
 8069:                &Apache::loncommon::end_data_table_header_row();
 8070:     foreach my $priv (sort(keys(%{$full}))) {
 8071:         next unless ($levels->{'course'}{$priv});
 8072:         my $privtext = &Apache::lonnet::plaintext($priv,$crstype);
 8073:         my ($default,$ineffect);
 8074:         if ($levelscurrent->{'course'}{$priv}) {
 8075:             $default = '<img src="/adm/lonIcons/navmap.correct.gif" alt="'.$lt{'ena'}.'" />';
 8076:             $ineffect = $default;
 8077:         }
 8078:         my ($customstatus,$checked);
 8079:         $output .= &Apache::loncommon::start_data_table_row().
 8080:                    '<td>'.$privtext.'</td>'.
 8081:                    '<td>'.$default.'</td><td>';
 8082:         if (($levelscurrent->{'course'}{$priv}) && ($off{$priv})) {
 8083:             if ($permission->{'owner'}) {
 8084:                 $checked = ' checked="checked"';
 8085:             }
 8086:             $customstatus = '<img src="/adm/lonIcons/navmap.wrong.gif" alt="'.$lt{'dis'}.'" />';
 8087:             $ineffect = $customstatus;
 8088:         } elsif ((!$levelscurrent->{'course'}{$priv}) && ($on{$priv})) {
 8089:             if ($permission->{'owner'}) {
 8090:                 $checked = ' checked="checked"';
 8091:             }
 8092:             $customstatus = '<img src="/adm/lonIcons/navmap.correct.gif" alt="'.$lt{'ena'}.'" />';
 8093:             $ineffect = $customstatus;
 8094:         }
 8095:         if ($permission->{'owner'}) {
 8096:             $output .= '<input type="checkbox" name="'.$role.'_override" value="'.$priv.'"'.$checked.' />';
 8097:         } else {
 8098:             $output .= $customstatus;
 8099:         }
 8100:         $output .= '</td><td>'.$ineffect.'</td>'.
 8101:                    &Apache::loncommon::end_data_table_row();
 8102:     }
 8103:     $output .= &Apache::loncommon::end_data_table();
 8104:     return $output;
 8105: }
 8106: 
 8107: sub get_adhocrole_settings {
 8108:     my ($cid,$accesstypes,$types,$customroles,$settings,$overridden) = @_;
 8109:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($customroles) eq 'HASH') &&
 8110:                    (ref($settings) eq 'HASH') && (ref($overridden) eq 'HASH'));
 8111:     foreach my $role (split(/,/,$env{'course.'.$cid.'.internal.adhocaccess'})) {
 8112:         my ($curraccess,$rest) = split(/=/,$env{'course.'.$cid.'.internal.adhoc.'.$role});
 8113:         if (($curraccess ne '') && (grep(/^\Q$curraccess\E$/,@{$accesstypes}))) {
 8114:             $settings->{$role}{'access'} = $curraccess;
 8115:             if (($curraccess eq 'status') && (ref($types) eq 'ARRAY')) {
 8116:                 my @status = split(/,/,$rest);
 8117:                 my @currstatus;
 8118:                 foreach my $type (@status) {
 8119:                     if ($type eq 'default') {
 8120:                         push(@currstatus,$type);
 8121:                     } elsif (grep(/^\Q$type\E$/,@{$types})) {
 8122:                         push(@currstatus,$type);
 8123:                     }
 8124:                 }
 8125:                 if (@currstatus) {
 8126:                     $settings->{$role}{$curraccess} = \@currstatus;
 8127:                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 8128:                     my @personnel = split(/,/,$rest);
 8129:                     $settings->{$role}{$curraccess} = \@personnel;
 8130:                 }
 8131:             }
 8132:         }
 8133:     }
 8134:     foreach my $role (keys(%{$customroles})) {
 8135:         if ($env{'course.'.$cid.'.internal.adhocpriv.'.$role}) {
 8136:             my %currentprivs;
 8137:             if (ref($customroles->{$role}) eq 'HASH') {
 8138:                 if (exists($customroles->{$role}{'course'})) {
 8139:                     my %full=();
 8140:                     my %levels= (
 8141:                                   course => {},
 8142:                                   domain => {},
 8143:                                   system => {},
 8144:                                 );
 8145:                     my %levelscurrent=(
 8146:                                         course => {},
 8147:                                         domain => {},
 8148:                                         system => {},
 8149:                                       );
 8150:                     &Apache::lonuserutils::custom_role_privs($customroles->{$role},\%full,\%levels,\%levelscurrent);
 8151:                     %currentprivs = %{$levelscurrent{'course'}};
 8152:                 }
 8153:             }
 8154:             foreach my $item (split(/,/,$env{'course.'.$cid.'.internal.adhocpriv.'.$role})) {
 8155:                 next if ($item eq '');
 8156:                 my ($rule,$rest) = split(/=/,$item);
 8157:                 next unless (($rule eq 'off') || ($rule eq 'on'));
 8158:                 foreach my $priv (split(/:/,$rest)) {
 8159:                     if ($priv ne '') {
 8160:                         if ($rule eq 'off') {
 8161:                             push(@{$overridden->{$role}{'off'}},$priv);
 8162:                             if ($currentprivs{$priv}) {
 8163:                                 push(@{$settings->{$role}{'off'}},$priv);
 8164:                             }
 8165:                         } else {
 8166:                             push(@{$overridden->{$role}{'on'}},$priv);
 8167:                             unless ($currentprivs{$priv}) {
 8168:                                 push(@{$settings->{$role}{'on'}},$priv);
 8169:                             }
 8170:                         }
 8171:                     }
 8172:                 }
 8173:             }
 8174:         }
 8175:     }
 8176:     return;
 8177: }
 8178: 
 8179: sub update_helpdeskaccess {
 8180:     my ($r,$permission,$brcrum) = @_;
 8181:     my $helpitem = 'Course_Helpdesk_Access';
 8182:     push (@{$brcrum},
 8183:              {href => '/adm/createuser?action=helpdesk',
 8184:               text => 'Helpdesk Access',
 8185:               help => $helpitem},
 8186:              {href => '/adm/createuser?action=helpdesk',
 8187:               text => 'Result',
 8188:               help => $helpitem}
 8189:          );
 8190:     my $bread_crumbs_component = 'Helpdesk Staff Access';
 8191:     my $args = { bread_crumbs           => $brcrum,
 8192:                  bread_crumbs_component => $bread_crumbs_component};
 8193: 
 8194:     # print page header
 8195:     $r->print(&header('',$args));
 8196:     unless ((ref($permission) eq 'HASH') && ($permission->{'owner'})) {
 8197:         $r->print('<p class="LC_error">'.&mt('You do not have permission to change helpdesk access.').'</p>');
 8198:         return;
 8199:     }
 8200:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
 8201:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8202:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8203:     my $confname = $cdom.'-domainconfig';
 8204:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($cdom);
 8205:     my $crstype = &Apache::loncommon::course_type();
 8206:     my %customroles = &get_domain_customroles($cdom,$confname);
 8207:     my (%settings,%overridden);
 8208:     &get_adhocrole_settings($env{'request.course.id'},\@accesstypes,
 8209:                             $types,\%customroles,\%settings,\%overridden);
 8210:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($cdom,['dh','da']);
 8211:     my (%changed,%storehash,@todelete);
 8212: 
 8213:     if (keys(%customroles)) {
 8214:         my (%newsettings,@incrs);
 8215:         foreach my $role (keys(%customroles)) {
 8216:             $newsettings{$role} = {
 8217:                                     access => '',
 8218:                                     status => '',
 8219:                                     exc    => '',
 8220:                                     inc    => '',
 8221:                                     on     => '',
 8222:                                     off    => '',
 8223:                                   };
 8224:             my %current;
 8225:             if (ref($settings{$role}) eq 'HASH') {
 8226:                 %current = %{$settings{$role}};
 8227:             }
 8228:             if (ref($overridden{$role}) eq 'HASH') {
 8229:                 $current{'overridden'} = $overridden{$role};
 8230:             }
 8231:             if ($env{'form.'.$role.'_incrs'}) {
 8232:                 my $access = $env{'form.'.$role.'_access'};
 8233:                 if (grep(/^\Q$access\E$/,@accesstypes)) {
 8234:                     push(@incrs,$role);
 8235:                     unless ($current{'access'} eq $access) {
 8236:                         $changed{$role}{'access'} = 1;
 8237:                         $storehash{'internal.adhoc.'.$role} = $access;
 8238:                     }
 8239:                     if ($access eq 'status') {
 8240:                         my @statuses = &Apache::loncommon::get_env_multiple('form.'.$role.'_status');
 8241:                         my @stored;
 8242:                         my @shownstatus;
 8243:                         if (ref($types) eq 'ARRAY') {
 8244:                             foreach my $type (sort(@statuses)) {
 8245:                                 if ($type eq 'default') {
 8246:                                     push(@stored,$type);
 8247:                                 } elsif (grep(/^\Q$type\E$/,@{$types})) {
 8248:                                     push(@stored,$type);
 8249:                                     push(@shownstatus,$usertypes->{$type});
 8250:                                 }
 8251:                             }
 8252:                             if (grep(/^default$/,@statuses)) {
 8253:                                 push(@shownstatus,$othertitle);
 8254:                             }
 8255:                             $storehash{'internal.adhoc.'.$role} .= '='.join(',',@stored);
 8256:                         }
 8257:                         $newsettings{$role}{'status'} = join(' '.&mt('or').' ',@shownstatus);
 8258:                         if (ref($current{'status'}) eq 'ARRAY') {
 8259:                             my @diffs = &Apache::loncommon::compare_arrays(\@stored,$current{'status'});
 8260:                             if (@diffs) {
 8261:                                 $changed{$role}{'status'} = 1;
 8262:                             }
 8263:                         } elsif (@stored) {
 8264:                             $changed{$role}{'status'} = 1;
 8265:                         }
 8266:                     } elsif (($access eq 'inc') || ($access eq 'exc')) {
 8267:                         my @personnel = &Apache::loncommon::get_env_multiple('form.'.$role.'_staff_'.$access);
 8268:                         my @newspecstaff;
 8269:                         my @stored;
 8270:                         my @currstaff;
 8271:                         foreach my $person (sort(@personnel)) {
 8272:                             if ($domhelpdesk{$person}) {
 8273:                                 push(@stored,$person);
 8274:                             }
 8275:                         }
 8276:                         if (ref($current{$access}) eq 'ARRAY') {
 8277:                             my @diffs = &Apache::loncommon::compare_arrays(\@stored,$current{$access});
 8278:                             if (@diffs) {
 8279:                                 $changed{$role}{$access} = 1;
 8280:                             }
 8281:                         } elsif (@stored) {
 8282:                             $changed{$role}{$access} = 1;
 8283:                         }
 8284:                         $storehash{'internal.adhoc.'.$role} .= '='.join(',',@stored);
 8285:                         foreach my $person (@stored) {
 8286:                             my ($uname,$udom) = split(/:/,$person);
 8287:                             push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
 8288:                         }
 8289:                         $newsettings{$role}{$access} = join(', ',sort(@newspecstaff));
 8290:                     }
 8291:                     $newsettings{$role}{'access'} = $access;
 8292:                 }
 8293:             } else {
 8294:                 if (($current{'access'} ne '') && (grep(/^\Q$current{'access'}\E$/,@accesstypes))) {
 8295:                     $changed{$role}{'access'} = 1;
 8296:                     $newsettings{$role} = {};
 8297:                     push(@todelete,'internal.adhoc.'.$role);
 8298:                 }
 8299:             }
 8300:             if (($env{'form.'.$role.'_incrs'}) && ($env{'form.'.$role.'_access'} eq 'none')) {
 8301:                 if (ref($current{'overridden'}) eq 'HASH') {
 8302:                     push(@todelete,'internal.adhocpriv.'.$role);
 8303:                 }
 8304:             } else {
 8305:                 my %full=();
 8306:                 my %levels= (
 8307:                              course => {},
 8308:                              domain => {},
 8309:                              system => {},
 8310:                             );
 8311:                 my %levelscurrent=(
 8312:                                    course => {},
 8313:                                    domain => {},
 8314:                                    system => {},
 8315:                                   );
 8316:                 &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 8317:                 my (@updatedon,@updatedoff,@override);
 8318:                 @override = &Apache::loncommon::get_env_multiple('form.'.$role.'_override');
 8319:                 if (@override) {
 8320:                     foreach my $priv (sort(keys(%full))) {
 8321:                         next unless ($levels{'course'}{$priv});
 8322:                         if (grep(/^\Q$priv\E$/,@override)) {
 8323:                             if ($levelscurrent{'course'}{$priv}) {
 8324:                                 push(@updatedoff,$priv);
 8325:                             } else {
 8326:                                 push(@updatedon,$priv);
 8327:                             }
 8328:                         }
 8329:                     }
 8330:                 }
 8331:                 if (@updatedon) {
 8332:                     $newsettings{$role}{'on'} = join('</li><li>', map { &Apache::lonnet::plaintext($_,$crstype) } (@updatedon));
 8333:                 }
 8334:                 if (@updatedoff) {
 8335:                     $newsettings{$role}{'off'} = join('</li><li>', map { &Apache::lonnet::plaintext($_,$crstype) } (@updatedoff));
 8336:                 }
 8337:                 if (ref($current{'overridden'}) eq 'HASH') {
 8338:                     if (ref($current{'overridden'}{'on'}) eq 'ARRAY') {
 8339:                         if (@updatedon) {
 8340:                             my @diffs = &Apache::loncommon::compare_arrays(\@updatedon,$current{'overridden'}{'on'});
 8341:                             if (@diffs) {
 8342:                                 $changed{$role}{'on'} = 1;
 8343:                             }
 8344:                         } else {
 8345:                             $changed{$role}{'on'} = 1;
 8346:                         }
 8347:                     } elsif (@updatedon) {
 8348:                         $changed{$role}{'on'} = 1;
 8349:                     }
 8350:                     if (ref($current{'overridden'}{'off'}) eq 'ARRAY') {
 8351:                         if (@updatedoff) {
 8352:                             my @diffs = &Apache::loncommon::compare_arrays(\@updatedoff,$current{'overridden'}{'off'});
 8353:                             if (@diffs) {
 8354:                                 $changed{$role}{'off'} = 1;
 8355:                             }
 8356:                         } else {
 8357:                             $changed{$role}{'off'} = 1;
 8358:                         }
 8359:                     } elsif (@updatedoff) {
 8360:                         $changed{$role}{'off'} = 1;
 8361:                     }
 8362:                 } else {
 8363:                     if (@updatedon) {
 8364:                         $changed{$role}{'on'} = 1;
 8365:                     }
 8366:                     if (@updatedoff) {
 8367:                         $changed{$role}{'off'} = 1;
 8368:                     }
 8369:                 }
 8370:                 if (ref($changed{$role}) eq 'HASH') {
 8371:                     if (($changed{$role}{'on'} || $changed{$role}{'off'})) {
 8372:                         my $newpriv;
 8373:                         if (@updatedon) {
 8374:                             $newpriv = 'on='.join(':',@updatedon);
 8375:                         }
 8376:                         if (@updatedoff) {
 8377:                             $newpriv .= ($newpriv ? ',' : '' ).'off='.join(':',@updatedoff);
 8378:                         }
 8379:                         if ($newpriv eq '') {
 8380:                             push(@todelete,'internal.adhocpriv.'.$role);
 8381:                         } else {
 8382:                             $storehash{'internal.adhocpriv.'.$role} = $newpriv;
 8383:                         }
 8384:                     }
 8385:                 }
 8386:             }
 8387:         }
 8388:         if (@incrs) {
 8389:             $storehash{'internal.adhocaccess'} = join(',',@incrs);
 8390:         } elsif (@todelete) {
 8391:             push(@todelete,'internal.adhocaccess');
 8392:         }
 8393:         if (keys(%changed)) {
 8394:             my ($putres,$delres);
 8395:             if (keys(%storehash)) {
 8396:                 $putres = &Apache::lonnet::put('environment',\%storehash,$cdom,$cnum);
 8397:                 my %newenvhash;
 8398:                 foreach my $key (keys(%storehash)) {
 8399:                     $newenvhash{'course.'.$env{'request.course.id'}.'.'.$key} = $storehash{$key};
 8400:                 }
 8401:                 &Apache::lonnet::appenv(\%newenvhash);
 8402:             }
 8403:             if (@todelete) {
 8404:                 $delres = &Apache::lonnet::del('environment',\@todelete,$cdom,$cnum);
 8405:                 foreach my $key (@todelete) {
 8406:                     &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.'.$key);
 8407:                 }
 8408:             }
 8409:             if (($putres eq 'ok') || ($delres eq 'ok')) {
 8410:                 my %domconfig = &Apache::lonnet::get_dom('configuration',['helpsettings'],$cdom);
 8411:                 my (%domcurrent,%ordered,%description,%domusage);
 8412:                 if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 8413:                     if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
 8414:                         %domcurrent = %{$domconfig{'helpsettings'}{'adhoc'}};
 8415:                     }
 8416:                 }
 8417:                 my $count = 0;
 8418:                 foreach my $role (sort(keys(%customroles))) {
 8419:                     my ($order,$desc);
 8420:                     if (ref($domcurrent{$role}) eq 'HASH') {
 8421:                         $order = $domcurrent{$role}{'order'};
 8422:                         $desc = $domcurrent{$role}{'desc'};
 8423:                     }
 8424:                     if ($order eq '') {
 8425:                         $order = $count;
 8426:                     }
 8427:                     $ordered{$order} = $role;
 8428:                     if ($desc ne '') {
 8429:                         $description{$role} = $desc;
 8430:                     } else {
 8431:                         $description{$role}= $role;
 8432:                     }
 8433:                     $count++;
 8434:                 }
 8435:                 my @roles_by_num = ();
 8436:                 foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 8437:                     push(@roles_by_num,$ordered{$item});
 8438:                 }
 8439:                 %domusage = &domain_adhoc_access(\%changed,\%domcurrent,\@accesstypes,$usertypes,$othertitle);
 8440:                 $r->print(&mt('Helpdesk access settings have been changed as follows').'<br />');
 8441:                 $r->print('<ul>');
 8442:                 foreach my $role (@roles_by_num) {
 8443:                     next unless (ref($changed{$role}) eq 'HASH');
 8444:                     $r->print('<li>'.&mt('Ad hoc role').': <b>'.$description{$role}.'</b>'.
 8445:                               '<ul>');
 8446:                     if ($changed{$role}{'access'} || $changed{$role}{'status'} || $changed{$role}{'inc'} || $changed{$role}{'exc'}) {
 8447:                         $r->print('<li>');
 8448:                         if ($env{'form.'.$role.'_incrs'}) {
 8449:                             if ($newsettings{$role}{'access'} eq 'all') {
 8450:                                 $r->print(&mt('All helpdesk staff can access '.lc($crstype).' with this role.'));
 8451:                             } elsif ($newsettings{$role}{'access'} eq 'dh') {
 8452:                                 $r->print(&mt('Helpdesk staff can use this role if they have an active [_1] role',
 8453:                                               &Apache::lonnet::plaintext('dh')));
 8454:                             } elsif ($newsettings{$role}{'access'} eq 'da') {
 8455:                                 $r->print(&mt('Helpdesk staff can use this role if they have an active [_1] role',
 8456:                                               &Apache::lonnet::plaintext('da')));
 8457:                             } elsif ($newsettings{$role}{'access'} eq 'none') {
 8458:                                 $r->print(&mt('No helpdesk staff can access '.lc($crstype).' with this role.'));
 8459:                             } elsif ($newsettings{$role}{'access'} eq 'status') {
 8460:                                 if ($newsettings{$role}{'status'}) {
 8461:                                     my ($access,$rest) = split(/=/,$storehash{'internal.adhoc.'.$role});
 8462:                                     if (split(/,/,$rest) > 1) {
 8463:                                         $r->print(&mt('Helpdesk staff can use this role if their institutional type is one of: [_1].',
 8464:                                                       $newsettings{$role}{'status'}));
 8465:                                     } else {
 8466:                                         $r->print(&mt('Helpdesk staff can use this role if their institutional type is: [_1].',
 8467:                                                       $newsettings{$role}{'status'}));
 8468:                                     }
 8469:                                 } else {
 8470:                                     $r->print(&mt('No helpdesk staff can access '.lc($crstype).' with this role.'));
 8471:                                 }
 8472:                             } elsif ($newsettings{$role}{'access'} eq 'exc') {
 8473:                                 if ($newsettings{$role}{'exc'}) {
 8474:                                     $r->print(&mt('Helpdesk staff who can use this role are as follows:').' '.$newsettings{$role}{'exc'}.'.');
 8475:                                 } else {
 8476:                                     $r->print(&mt('No helpdesk staff can access '.lc($crstype).' with this role.'));
 8477:                                 }
 8478:                             } elsif ($newsettings{$role}{'access'} eq 'inc') {
 8479:                                 if ($newsettings{$role}{'inc'}) {
 8480:                                     $r->print(&mt('All helpdesk staff may use this role except the following:').' '.$newsettings{$role}{'inc'}.'.');
 8481:                                 } else {
 8482:                                     $r->print(&mt('All helpdesk staff may use this role.'));
 8483:                                 }
 8484:                             }
 8485:                         } else {
 8486:                             $r->print(&mt('Default access set in the domain now applies.').'<br />'.
 8487:                                       '<span class="LC_cusr_emph">'.$domusage{$role}.'</span>');
 8488:                         }
 8489:                         $r->print('</li>');
 8490:                     }
 8491:                     unless ($newsettings{$role}{'access'} eq 'none') {
 8492:                         if ($changed{$role}{'off'}) {
 8493:                             if ($newsettings{$role}{'off'}) {
 8494:                                 $r->print('<li>'.&mt('Privileges which are available by default for this ad hoc role, but are disabled for this specific '.lc($crstype).':').
 8495:                                           '<ul><li>'.$newsettings{$role}{'off'}.'</li></ul></li>');
 8496:                             } else {
 8497:                                 $r->print('<li>'.&mt('All privileges available by default for this ad hoc role are enabled.').'</li>');
 8498:                             }
 8499:                         }
 8500:                         if ($changed{$role}{'on'}) {
 8501:                             if ($newsettings{$role}{'on'}) {
 8502:                                 $r->print('<li>'.&mt('Privileges which are not available by default for this ad hoc role, but are enabled for this specific '.lc($crstype).':').
 8503:                                           '<ul><li>'.$newsettings{$role}{'on'}.'</li></ul></li>');
 8504:                             } else {
 8505:                                 $r->print('<li>'.&mt('None of the privileges unavailable by default for this ad hoc role are enabled.').'</li>');
 8506:                             }
 8507:                         }
 8508:                     }
 8509:                     $r->print('</ul></li>');
 8510:                 }
 8511:                 $r->print('</ul>');
 8512:             }
 8513:         } else {
 8514:             $r->print(&mt('No changes made to helpdesk access settings.'));
 8515:         }
 8516:     }
 8517:     return;
 8518: }
 8519: 
 8520: #-------------------------------------------------- functions for &phase_two
 8521: sub user_search_result {
 8522:     my ($context,$srch) = @_;
 8523:     my %allhomes;
 8524:     my %inst_matches;
 8525:     my %srch_results;
 8526:     my ($response,$currstate,$forcenewuser,$dirsrchres);
 8527:     $srch->{'srchterm'} =~ s/\s+/ /g;
 8528:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
 8529:         $response = &mt('Invalid search.');
 8530:     }
 8531:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
 8532:         $response = &mt('Invalid search.');
 8533:     }
 8534:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
 8535:         $response = &mt('Invalid search.');
 8536:     }
 8537:     if ($srch->{'srchterm'} eq '') {
 8538:         $response = &mt('You must enter a search term.');
 8539:     }
 8540:     if ($srch->{'srchterm'} =~ /^\s+$/) {
 8541:         $response = &mt('Your search term must contain more than just spaces.');
 8542:     }
 8543:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
 8544:         if (($srch->{'srchdomain'} eq '') || 
 8545: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
 8546:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
 8547:         }
 8548:     }
 8549:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
 8550:         ($srch->{'srchin'} eq 'alc')) {
 8551:         if ($srch->{'srchby'} eq 'uname') {
 8552:             my $unamecheck = $srch->{'srchterm'};
 8553:             if ($srch->{'srchtype'} eq 'contains') {
 8554:                 if ($unamecheck !~ /^\w/) {
 8555:                     $unamecheck = 'a'.$unamecheck; 
 8556:                 }
 8557:             }
 8558:             if ($unamecheck !~ /^$match_username$/) {
 8559:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
 8560:             }
 8561:         }
 8562:     }
 8563:     if ($response ne '') {
 8564:         $response = '<span class="LC_warning">'.$response.'</span><br />';
 8565:     }
 8566:     if ($srch->{'srchin'} eq 'instd') {
 8567:         my $instd_chk = &instdirectorysrch_check($srch);
 8568:         if ($instd_chk ne 'ok') {
 8569:             my $domd_chk = &domdirectorysrch_check($srch);
 8570:             $response .= '<span class="LC_warning">'.$instd_chk.'</span><br />';
 8571:             if ($domd_chk eq 'ok') {
 8572:                 $response .= &mt('You may want to search in the LON-CAPA domain instead of in the institutional directory.');
 8573:             }
 8574:             $response .= '<br />';
 8575:         }
 8576:     } else {
 8577:         unless (($context eq 'requestcrs') && ($srch->{'srchtype'} eq 'exact')) {
 8578:             my $domd_chk = &domdirectorysrch_check($srch);
 8579:             if (($domd_chk ne 'ok') && ($env{'form.action'} ne 'accesslogs')) {
 8580:                 my $instd_chk = &instdirectorysrch_check($srch);
 8581:                 $response .= '<span class="LC_warning">'.$domd_chk.'</span><br />';
 8582:                 if ($instd_chk eq 'ok') {
 8583:                     $response .= &mt('You may want to search in the institutional directory instead of in the LON-CAPA domain.');
 8584:                 }
 8585:                 $response .= '<br />';
 8586:             }
 8587:         }
 8588:     }
 8589:     if ($response ne '') {
 8590:         return ($currstate,$response);
 8591:     }
 8592:     if ($srch->{'srchby'} eq 'uname') {
 8593:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
 8594:             if ($env{'form.forcenew'}) {
 8595:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
 8596:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
 8597:                     if ($uhome eq 'no_host') {
 8598:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
 8599:                         my $showdom = &display_domain_info($env{'request.role.domain'});
 8600:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
 8601:                     } else {
 8602:                         $currstate = 'modify';
 8603:                     }
 8604:                 } else {
 8605:                     $currstate = 'modify';
 8606:                 }
 8607:             } else {
 8608:                 if ($srch->{'srchin'} eq 'dom') {
 8609:                     if ($srch->{'srchtype'} eq 'exact') {
 8610:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
 8611:                         if ($uhome eq 'no_host') {
 8612:                             ($currstate,$response,$forcenewuser) =
 8613:                                 &build_search_response($context,$srch,%srch_results);
 8614:                         } else {
 8615:                             $currstate = 'modify';
 8616:                             if ($env{'form.action'} eq 'accesslogs') {
 8617:                                 $currstate = 'activity';
 8618:                             }
 8619:                             my $uname = $srch->{'srchterm'};
 8620:                             my $udom = $srch->{'srchdomain'};
 8621:                             $srch_results{$uname.':'.$udom} =
 8622:                                 { &Apache::lonnet::get('environment',
 8623:                                                        ['firstname',
 8624:                                                         'lastname',
 8625:                                                         'permanentemail'],
 8626:                                                          $udom,$uname)
 8627:                                 };
 8628:                         }
 8629:                     } else {
 8630:                         %srch_results = &Apache::lonnet::usersearch($srch);
 8631:                         ($currstate,$response,$forcenewuser) =
 8632:                             &build_search_response($context,$srch,%srch_results);
 8633:                     }
 8634:                 } else {
 8635:                     my $courseusers = &get_courseusers();
 8636:                     if ($srch->{'srchtype'} eq 'exact') {
 8637:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
 8638:                             $currstate = 'modify';
 8639:                         } else {
 8640:                             ($currstate,$response,$forcenewuser) =
 8641:                                 &build_search_response($context,$srch,%srch_results);
 8642:                         }
 8643:                     } else {
 8644:                         foreach my $user (keys(%$courseusers)) {
 8645:                             my ($cuname,$cudomain) = split(/:/,$user);
 8646:                             if ($cudomain eq $srch->{'srchdomain'}) {
 8647:                                 my $matched = 0;
 8648:                                 if ($srch->{'srchtype'} eq 'begins') {
 8649:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
 8650:                                         $matched = 1;
 8651:                                     }
 8652:                                 } else {
 8653:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
 8654:                                         $matched = 1;
 8655:                                     }
 8656:                                 }
 8657:                                 if ($matched) {
 8658:                                     $srch_results{$user} = 
 8659: 					{&Apache::lonnet::get('environment',
 8660: 							     ['firstname',
 8661: 							      'lastname',
 8662: 							      'permanentemail'],
 8663: 							      $cudomain,$cuname)};
 8664:                                 }
 8665:                             }
 8666:                         }
 8667:                         ($currstate,$response,$forcenewuser) =
 8668:                             &build_search_response($context,$srch,%srch_results);
 8669:                     }
 8670:                 }
 8671:             }
 8672:         } elsif ($srch->{'srchin'} eq 'alc') {
 8673:             $currstate = 'query';
 8674:         } elsif ($srch->{'srchin'} eq 'instd') {
 8675:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
 8676:             if ($dirsrchres eq 'ok') {
 8677:                 ($currstate,$response,$forcenewuser) = 
 8678:                     &build_search_response($context,$srch,%srch_results);
 8679:             } else {
 8680:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
 8681:                 $response = '<span class="LC_warning">'.
 8682:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
 8683:                     '</span><br />'.
 8684:                     &mt('You may want to search in the LON-CAPA domain instead of in the institutional directory.').
 8685:                     '<br />'; 
 8686:             }
 8687:         }
 8688:     } else {
 8689:         if ($srch->{'srchin'} eq 'dom') {
 8690:             %srch_results = &Apache::lonnet::usersearch($srch);
 8691:             ($currstate,$response,$forcenewuser) = 
 8692:                 &build_search_response($context,$srch,%srch_results); 
 8693:         } elsif ($srch->{'srchin'} eq 'crs') {
 8694:             my $courseusers = &get_courseusers(); 
 8695:             foreach my $user (keys(%$courseusers)) {
 8696:                 my ($uname,$udom) = split(/:/,$user);
 8697:                 my %names = &Apache::loncommon::getnames($uname,$udom);
 8698:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
 8699:                 if ($srch->{'srchby'} eq 'lastname') {
 8700:                     if ((($srch->{'srchtype'} eq 'exact') && 
 8701:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
 8702:                         (($srch->{'srchtype'} eq 'begins') &&
 8703:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
 8704:                         (($srch->{'srchtype'} eq 'contains') &&
 8705:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
 8706:                         $srch_results{$user} = {firstname => $names{'firstname'},
 8707:                                             lastname => $names{'lastname'},
 8708:                                             permanentemail => $emails{'permanentemail'},
 8709:                                            };
 8710:                     }
 8711:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
 8712:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
 8713:                     $srchlast =~ s/\s+$//;
 8714:                     $srchfirst =~ s/^\s+//;
 8715:                     if ($srch->{'srchtype'} eq 'exact') {
 8716:                         if (($names{'lastname'} eq $srchlast) &&
 8717:                             ($names{'firstname'} eq $srchfirst)) {
 8718:                             $srch_results{$user} = {firstname => $names{'firstname'},
 8719:                                                 lastname => $names{'lastname'},
 8720:                                                 permanentemail => $emails{'permanentemail'},
 8721: 
 8722:                                            };
 8723:                         }
 8724:                     } elsif ($srch->{'srchtype'} eq 'begins') {
 8725:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
 8726:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
 8727:                             $srch_results{$user} = {firstname => $names{'firstname'},
 8728:                                                 lastname => $names{'lastname'},
 8729:                                                 permanentemail => $emails{'permanentemail'},
 8730:                                                };
 8731:                         }
 8732:                     } else {
 8733:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
 8734:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
 8735:                             $srch_results{$user} = {firstname => $names{'firstname'},
 8736:                                                 lastname => $names{'lastname'},
 8737:                                                 permanentemail => $emails{'permanentemail'},
 8738:                                                };
 8739:                         }
 8740:                     }
 8741:                 }
 8742:             }
 8743:             ($currstate,$response,$forcenewuser) = 
 8744:                 &build_search_response($context,$srch,%srch_results); 
 8745:         } elsif ($srch->{'srchin'} eq 'alc') {
 8746:             $currstate = 'query';
 8747:         } elsif ($srch->{'srchin'} eq 'instd') {
 8748:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
 8749:             if ($dirsrchres eq 'ok') {
 8750:                 ($currstate,$response,$forcenewuser) = 
 8751:                     &build_search_response($context,$srch,%srch_results);
 8752:             } else {
 8753:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
 8754:                 $response = '<span class="LC_warning">'.
 8755:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
 8756:                     '</span><br />'.
 8757:                     &mt('You may want to search in the LON-CAPA domain instead of in the institutional directory.').
 8758:                     '<br />';
 8759:             }
 8760:         }
 8761:     }
 8762:     return ($currstate,$response,$forcenewuser,\%srch_results);
 8763: }
 8764: 
 8765: sub domdirectorysrch_check {
 8766:     my ($srch) = @_;
 8767:     my $response;
 8768:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
 8769:                                              ['directorysrch'],$srch->{'srchdomain'});
 8770:     my $showdom = &display_domain_info($srch->{'srchdomain'});
 8771:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
 8772:         if ($dom_inst_srch{'directorysrch'}{'lcavailable'} eq '0') {
 8773:             return &mt('LON-CAPA directory search is not available in domain: [_1]',$showdom);
 8774:         }
 8775:         if ($dom_inst_srch{'directorysrch'}{'lclocalonly'}) {
 8776:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
 8777:                 return &mt('LON-CAPA directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom);
 8778:             }
 8779:         }
 8780:     }
 8781:     return 'ok';
 8782: }
 8783: 
 8784: sub instdirectorysrch_check {
 8785:     my ($srch) = @_;
 8786:     my $can_search = 0;
 8787:     my $response;
 8788:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
 8789:                                              ['directorysrch'],$srch->{'srchdomain'});
 8790:     my $showdom = &display_domain_info($srch->{'srchdomain'});
 8791:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
 8792:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
 8793:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
 8794:         }
 8795:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
 8796:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
 8797:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
 8798:             }
 8799:             my @usertypes = split(/:/,$env{'environment.inststatus'});
 8800:             if (!@usertypes) {
 8801:                 push(@usertypes,'default');
 8802:             }
 8803:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
 8804:                 foreach my $type (@usertypes) {
 8805:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
 8806:                         $can_search = 1;
 8807:                         last;
 8808:                     }
 8809:                 }
 8810:             }
 8811:             if (!$can_search) {
 8812:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
 8813:                 my @longtypes; 
 8814:                 foreach my $item (@usertypes) {
 8815:                     if (defined($insttypes->{$item})) { 
 8816:                         push (@longtypes,$insttypes->{$item});
 8817:                     } elsif ($item eq 'default') {
 8818:                         push (@longtypes,&mt('other')); 
 8819:                     }
 8820:                 }
 8821:                 my $insttype_str = join(', ',@longtypes); 
 8822:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
 8823:             }
 8824:         } else {
 8825:             $can_search = 1;
 8826:         }
 8827:     } else {
 8828:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
 8829:     }
 8830:     my %longtext = &Apache::lonlocal::texthash (
 8831:                        uname     => 'username',
 8832:                        lastfirst => 'last name, first name',
 8833:                        lastname  => 'last name',
 8834:                        contains  => 'contains',
 8835:                        exact     => 'as exact match to',
 8836:                        begins    => 'begins with',
 8837:                    );
 8838:     if ($can_search) {
 8839:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
 8840:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
 8841:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
 8842:             }
 8843:         } else {
 8844:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
 8845:         }
 8846:     }
 8847:     if ($can_search) {
 8848:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
 8849:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
 8850:                 return 'ok';
 8851:             } else {
 8852:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
 8853:             }
 8854:         } else {
 8855:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
 8856:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
 8857:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
 8858:                 return 'ok';
 8859:             } else {
 8860:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
 8861:             }
 8862:         }
 8863:     }
 8864: }
 8865: 
 8866: sub get_courseusers {
 8867:     my %advhash;
 8868:     my $classlist = &Apache::loncoursedata::get_classlist();
 8869:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
 8870:     foreach my $role (sort(keys(%coursepersonnel))) {
 8871:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
 8872: 	    if (!exists($classlist->{$user})) {
 8873: 		$classlist->{$user} = [];
 8874: 	    }
 8875:         }
 8876:     }
 8877:     return $classlist;
 8878: }
 8879: 
 8880: sub build_search_response {
 8881:     my ($context,$srch,%srch_results) = @_;
 8882:     my ($currstate,$response,$forcenewuser);
 8883:     my %names = (
 8884:           'uname'     => 'username',
 8885:           'lastname'  => 'last name',
 8886:           'lastfirst' => 'last name, first name',
 8887:           'crs'       => 'this course',
 8888:           'dom'       => 'LON-CAPA domain',
 8889:           'instd'     => 'the institutional directory for domain',
 8890:     );
 8891: 
 8892:     my %single = (
 8893:                    begins   => 'A match',
 8894:                    contains => 'A match',
 8895:                    exact    => 'An exact match',
 8896:                  );
 8897:     my %nomatch = (
 8898:                    begins   => 'No match',
 8899:                    contains => 'No match',
 8900:                    exact    => 'No exact match',
 8901:                   );
 8902:     if (keys(%srch_results) > 1) {
 8903:         $currstate = 'select';
 8904:     } else {
 8905:         if (keys(%srch_results) == 1) {
 8906:             if ($env{'form.action'} eq 'accesslogs') {
 8907:                 $currstate = 'activity';
 8908:             } else {
 8909:                 $currstate = 'modify';
 8910:             }
 8911:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
 8912:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
 8913:                 $response .= ': '.&display_domain_info($srch->{'srchdomain'});
 8914:             }
 8915:         } else { # Search has nothing found. Prepare message to user.
 8916:             $response = '<span class="LC_warning">';
 8917:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
 8918:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}: [_2]",
 8919:                                  '<b>'.$srch->{'srchterm'}.'</b>',
 8920:                                  &display_domain_info($srch->{'srchdomain'}));
 8921:             } else {
 8922:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}.",
 8923:                                  '<b>'.$srch->{'srchterm'}.'</b>');
 8924:             }
 8925:             $response .= '</span>';
 8926: 
 8927:             if ($srch->{'srchin'} ne 'alc') {
 8928:                 $forcenewuser = 1;
 8929:                 my $cansrchinst = 0; 
 8930:                 if (($srch->{'srchdomain'}) && ($env{'form.action'} ne 'accesslogs')) {
 8931:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
 8932:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 8933:                         if ($domconfig{'directorysrch'}{'available'}) {
 8934:                             $cansrchinst = 1;
 8935:                         } 
 8936:                     }
 8937:                 }
 8938:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
 8939:                      ($srch->{'srchby'} eq 'lastname')) &&
 8940:                     ($srch->{'srchin'} eq 'dom')) {
 8941:                     if ($cansrchinst) {
 8942:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
 8943:                     }
 8944:                 }
 8945:                 if ($srch->{'srchin'} eq 'crs') {
 8946:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
 8947:                 }
 8948:             }
 8949:             my $createdom = $env{'request.role.domain'};
 8950:             if ($context eq 'requestcrs') {
 8951:                 if ($env{'form.coursedom'} ne '') {
 8952:                     $createdom = $env{'form.coursedom'};
 8953:                 }
 8954:             }
 8955:             unless (($env{'form.action'} eq 'accesslogs') || (($srch->{'srchby'} eq 'uname') && ($srch->{'srchin'} eq 'dom') &&
 8956:                     ($srch->{'srchtype'} eq 'exact') && ($srch->{'srchdomain'} eq $createdom))) {
 8957:                 my $cancreate =
 8958:                     &Apache::lonuserutils::can_create_user($createdom,$context);
 8959:                 my $targetdom = '<span class="LC_cusr_emph">'.$createdom.'</span>';
 8960:                 if ($cancreate) {
 8961:                     my $showdom = &display_domain_info($createdom); 
 8962:                     $response .= '<br /><br />'
 8963:                                 .'<b>'.&mt('To add a new user:').'</b>'
 8964:                                 .'<br />';
 8965:                     if ($context eq 'requestcrs') {
 8966:                         $response .= &mt("(You can only define new users in the new course's domain - [_1])",$targetdom);
 8967:                     } else {
 8968:                         $response .= &mt("(You can only create new users in your current role's domain - [_1])",$targetdom);
 8969:                     }
 8970:                     $response .='<ul><li>'
 8971:                                 .&mt("Set 'Domain/institution to search' to: [_1]",'<span class="LC_cusr_emph">'.$showdom.'</span>')
 8972:                                 .'</li><li>'
 8973:                                 .&mt("Set 'Search criteria' to: [_1]username is ..... in selected LON-CAPA domain[_2]",'<span class="LC_cusr_emph">','</span>')
 8974:                                 .'</li><li>'
 8975:                                 .&mt('Provide the proposed username')
 8976:                                 .'</li><li>'
 8977:                                 .&mt("Click 'Search'")
 8978:                                 .'</li></ul><br />';
 8979:                 } else {
 8980:                     unless (($context eq 'domain') && ($env{'form.action'} eq 'singleuser')) {
 8981:                         my $helplink = ' href="javascript:helpMenu('."'display'".')"';
 8982:                         $response .= '<br /><br />';
 8983:                         if ($context eq 'requestcrs') {
 8984:                             $response .= &mt("You are not authorized to define new users in the new course's domain - [_1].",$targetdom);
 8985:                         } else {
 8986:                             $response .= &mt("You are not authorized to create new users in your current role's domain - [_1].",$targetdom);
 8987:                         }
 8988:                         $response .= '<br />'
 8989:                                      .&mt('Please contact the [_1]helpdesk[_2] if you need to create a new user.'
 8990:                                         ,' <a'.$helplink.'>'
 8991:                                         ,'</a>')
 8992:                                      .'<br />';
 8993:                     }
 8994:                 }
 8995:             }
 8996:         }
 8997:     }
 8998:     return ($currstate,$response,$forcenewuser);
 8999: }
 9000: 
 9001: sub display_domain_info {
 9002:     my ($dom) = @_;
 9003:     my $output = $dom;
 9004:     if ($dom ne '') { 
 9005:         my $domdesc = &Apache::lonnet::domain($dom,'description');
 9006:         if ($domdesc ne '') {
 9007:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
 9008:         }
 9009:     }
 9010:     return $output;
 9011: }
 9012: 
 9013: sub crumb_utilities {
 9014:     my %elements = (
 9015:        crtuser => {
 9016:            srchterm => 'text',
 9017:            srchin => 'selectbox',
 9018:            srchby => 'selectbox',
 9019:            srchtype => 'selectbox',
 9020:            srchdomain => 'selectbox',
 9021:        },
 9022:        crtusername => {
 9023:            srchterm => 'text',
 9024:            srchdomain => 'selectbox',
 9025:        },
 9026:        docustom => {
 9027:            rolename => 'selectbox',
 9028:            newrolename => 'textbox',
 9029:        },
 9030:        studentform => {
 9031:            srchterm => 'text',
 9032:            srchin => 'selectbox',
 9033:            srchby => 'selectbox',
 9034:            srchtype => 'selectbox',
 9035:            srchdomain => 'selectbox',
 9036:        },
 9037:     );
 9038: 
 9039:     my $jsback .= qq|
 9040: function backPage(formname,prevphase,prevstate) {
 9041:     if (typeof prevphase == 'undefined') {
 9042:         formname.phase.value = '';
 9043:     }
 9044:     else {  
 9045:         formname.phase.value = prevphase;
 9046:     }
 9047:     if (typeof prevstate == 'undefined') {
 9048:         formname.currstate.value = '';
 9049:     }
 9050:     else {
 9051:         formname.currstate.value = prevstate;
 9052:     }
 9053:     formname.submit();
 9054: }
 9055: |;
 9056:     return ($jsback,\%elements);
 9057: }
 9058: 
 9059: sub course_level_table {
 9060:     my ($inccourses,$showcredits,$defaultcredits) = @_;
 9061:     return unless (ref($inccourses) eq 'HASH');
 9062:     my $table = '';
 9063: # Custom Roles?
 9064: 
 9065:     my %customroles=&Apache::lonuserutils::my_custom_roles();
 9066:     my %lt=&Apache::lonlocal::texthash(
 9067:             'exs'  => "Existing sections",
 9068:             'new'  => "Define new section",
 9069:             'ssd'  => "Set Start Date",
 9070:             'sed'  => "Set End Date",
 9071:             'crl'  => "Course Level",
 9072:             'act'  => "Activate",
 9073:             'rol'  => "Role",
 9074:             'ext'  => "Extent",
 9075:             'grs'  => "Section",
 9076:             'crd'  => "Credits",
 9077:             'sta'  => "Start",
 9078:             'end'  => "End"
 9079:     );
 9080: 
 9081:     foreach my $protectedcourse (sort(keys(%{$inccourses}))) {
 9082: 	my $thiscourse=$protectedcourse;
 9083: 	$thiscourse=~s:_:/:g;
 9084: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
 9085:         my $isowner = &Apache::lonuserutils::is_courseowner($protectedcourse,$coursedata{'internal.courseowner'});
 9086: 	my $area=$coursedata{'description'};
 9087:         my $crstype=$coursedata{'type'};
 9088: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
 9089: 	my ($domain,$cnum)=split(/\//,$thiscourse);
 9090:         my %sections_count;
 9091:         if (defined($env{'request.course.id'})) {
 9092:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
 9093:                 %sections_count = 
 9094: 		    &Apache::loncommon::get_sections($domain,$cnum);
 9095:             }
 9096:         }
 9097:         my @roles = &Apache::lonuserutils::roles_by_context('course','',$crstype);
 9098: 	foreach my $role (@roles) {
 9099:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
 9100: 	    if ((&Apache::lonnet::allowed('c'.$role,$thiscourse)) ||
 9101:                 ((($role eq 'cc') || ($role eq 'co')) && ($isowner))) {
 9102:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
 9103:                                             $plrole,\%sections_count,\%lt,
 9104:                                             $showcredits,$defaultcredits,$crstype);
 9105:             } elsif ($env{'request.course.sec'} ne '') {
 9106:                 if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
 9107:                                              $env{'request.course.sec'})) {
 9108:                     $table .= &course_level_row($protectedcourse,$role,$area,$domain,
 9109:                                                 $plrole,\%sections_count,\%lt,
 9110:                                                 $showcredits,$defaultcredits,$crstype);
 9111:                 }
 9112:             }
 9113:         }
 9114:         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
 9115:             foreach my $cust (sort(keys(%customroles))) {
 9116:                 next if ($crstype eq 'Community' && $customroles{$cust} =~ /bre\&S/);
 9117:                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
 9118:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
 9119:                                             $cust,\%sections_count,\%lt,
 9120:                                             $showcredits,$defaultcredits,$crstype);
 9121:             }
 9122: 	}
 9123:     }
 9124:     return '' if ($table eq ''); # return nothing if there is nothing 
 9125:                                  # in the table
 9126:     my $result;
 9127:     if (!$env{'request.course.id'}) {
 9128:         $result = '<h4>'.$lt{'crl'}.'</h4>'."\n";
 9129:     }
 9130:     $result .= 
 9131: &Apache::loncommon::start_data_table().
 9132: &Apache::loncommon::start_data_table_header_row().
 9133: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
 9134: '<th>'.$lt{'ext'}.'</th><th>'."\n";
 9135:     if ($showcredits) {
 9136:         $result .= $lt{'crd'}.'</th>';
 9137:     }
 9138:     $result .=
 9139: '<th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th>'."\n".
 9140: '<th>'.$lt{'end'}.'</th>'.
 9141: &Apache::loncommon::end_data_table_header_row().
 9142: $table.
 9143: &Apache::loncommon::end_data_table();
 9144:     return $result;
 9145: }
 9146: 
 9147: sub course_level_row {
 9148:     my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,
 9149:         $lt,$showcredits,$defaultcredits,$crstype) = @_;
 9150:     my $creditem;
 9151:     my $row = &Apache::loncommon::start_data_table_row().
 9152:               ' <td><input type="checkbox" name="act_'.
 9153:               $protectedcourse.'_'.$role.'" /></td>'."\n".
 9154:               ' <td>'.$plrole.'</td>'."\n".
 9155:               ' <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
 9156:     if (($showcredits) && ($role eq 'st') && ($crstype eq 'Course')) {
 9157:         $row .= 
 9158:             '<td><input type="text" name="credits_'.$protectedcourse.'_'.
 9159:             $role.'" size="3" value="'.$defaultcredits.'" /></td>';
 9160:     } else {
 9161:         $row .= '<td>&nbsp;</td>';
 9162:     }
 9163:     if (($role eq 'cc') || ($role eq 'co')) {
 9164:         $row .= '<td>&nbsp;</td>';
 9165:     } elsif ($env{'request.course.sec'} ne '') {
 9166:         $row .= ' <td><input type="hidden" value="'.
 9167:                 $env{'request.course.sec'}.'" '.
 9168:                 'name="sec_'.$protectedcourse.'_'.$role.'" />'.
 9169:                 $env{'request.course.sec'}.'</td>';
 9170:     } else {
 9171:         if (ref($sections_count) eq 'HASH') {
 9172:             my $currsec = 
 9173:                 &Apache::lonuserutils::course_sections($sections_count,
 9174:                                                        $protectedcourse.'_'.$role);
 9175:             $row .= '<td><table class="LC_createuser">'."\n".
 9176:                     '<tr class="LC_section_row">'."\n".
 9177:                     ' <td valign="top">'.$lt->{'exs'}.'<br />'.
 9178:                        $currsec.'</td>'."\n".
 9179:                      ' <td>&nbsp;&nbsp;</td>'."\n".
 9180:                      ' <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
 9181:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
 9182:                      '" value="" />'.
 9183:                      '<input type="hidden" '.
 9184:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
 9185:                      '</tr></table></td>'."\n";
 9186:         } else {
 9187:             $row .= '<td><input type="text" size="10" '.
 9188:                     'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n";
 9189:         }
 9190:     }
 9191:     $row .= <<ENDTIMEENTRY;
 9192: <td><input type="hidden" name="start_$protectedcourse\_$role" value="" />
 9193: <a href=
 9194: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$role.value,'start_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'ssd'}</a></td>
 9195: <td><input type="hidden" name="end_$protectedcourse\_$role" value="" />
 9196: <a href=
 9197: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
 9198: ENDTIMEENTRY
 9199:     $row .= &Apache::loncommon::end_data_table_row();
 9200:     return $row;
 9201: }
 9202: 
 9203: sub course_level_dc {
 9204:     my ($dcdom,$showcredits) = @_;
 9205:     my %customroles=&Apache::lonuserutils::my_custom_roles();
 9206:     my @roles = &Apache::lonuserutils::roles_by_context('course');
 9207:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
 9208:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
 9209:                       '<input type="hidden" name="dccourse" value="" />';
 9210:     my $courseform=&Apache::loncommon::selectcourse_link
 9211:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Select','crstype');
 9212:     my $credit_elem;
 9213:     if ($showcredits) {
 9214:         $credit_elem = 'credits';
 9215:     }
 9216:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu','role','Course/Community Browser',$credit_elem);
 9217:     my %lt=&Apache::lonlocal::texthash(
 9218:                     'rol'  => "Role",
 9219:                     'grs'  => "Section",
 9220:                     'exs'  => "Existing sections",
 9221:                     'new'  => "Define new section", 
 9222:                     'sta'  => "Start",
 9223:                     'end'  => "End",
 9224:                     'ssd'  => "Set Start Date",
 9225:                     'sed'  => "Set End Date",
 9226:                     'scc'  => "Course/Community",
 9227:                     'crd'  => "Credits",
 9228:                   );
 9229:     my $header = '<h4>'.&mt('Course/Community Level').'</h4>'.
 9230:                  &Apache::loncommon::start_data_table().
 9231:                  &Apache::loncommon::start_data_table_header_row().
 9232:                  '<th>'.$lt{'scc'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
 9233:                  '<th>'.$lt{'grs'}.'</th>'."\n";
 9234:     $header .=   '<th>'.$lt{'crd'}.'</th>'."\n" if ($showcredits);
 9235:     $header .=   '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'."\n".
 9236:                  &Apache::loncommon::end_data_table_header_row();
 9237:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
 9238:                      '<td><br /><span class="LC_nobreak"><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc','','','','crstype'".')" />'.
 9239:                      $courseform.('&nbsp;' x4).'</span></td>'."\n".
 9240:                      '<td valign="top"><br /><select name="role">'."\n";
 9241:     foreach my $role (@roles) {
 9242:         my $plrole=&Apache::lonnet::plaintext($role);
 9243:         $otheritems .= '  <option value="'.$role.'">'.$plrole.'</option>';
 9244:     }
 9245:     if ( keys(%customroles) > 0) {
 9246:         foreach my $cust (sort(keys(%customroles))) {
 9247:             my $custrole='cr_cr_'.$env{'user.domain'}.
 9248:                     '_'.$env{'user.name'}.'_'.$cust;
 9249:             $otheritems .= '  <option value="'.$custrole.'">'.$cust.'</option>';
 9250:         }
 9251:     }
 9252:     $otheritems .= '</select></td><td>'.
 9253:                      '<table border="0" cellspacing="0" cellpadding="0">'.
 9254:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
 9255:                      ' <option value="">&lt;--'.&mt('Pick course first').'</option></select></td>'.
 9256:                      '<td>&nbsp;&nbsp;</td>'.
 9257:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
 9258:                      '<input type="text" name="newsec" value="" />'.
 9259:                      '<input type="hidden" name="section" value="" />'.
 9260:                      '<input type="hidden" name="groups" value="" />'.
 9261:                      '<input type="hidden" name="crstype" value="" /></td>'.
 9262:                      '</tr></table></td>'."\n";
 9263:     if ($showcredits) {
 9264:         $otheritems .= '<td><br />'."\n".
 9265:                        '<input type="text" size="3" name="credits" value="" /></td>'."\n";
 9266:     }
 9267:     $otheritems .= <<ENDTIMEENTRY;
 9268: <td><br /><input type="hidden" name="start" value='' />
 9269: <a href=
 9270: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
 9271: <td><br /><input type="hidden" name="end" value='' />
 9272: <a href=
 9273: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
 9274: ENDTIMEENTRY
 9275:     $otheritems .= &Apache::loncommon::end_data_table_row().
 9276:                    &Apache::loncommon::end_data_table()."\n";
 9277:     return $cb_jscript.$header.$hiddenitems.$otheritems;
 9278: }
 9279: 
 9280: sub update_selfenroll_config {
 9281:     my ($r,$cid,$cdom,$cnum,$context,$crstype,$currsettings) = @_;
 9282:     return unless (ref($currsettings) eq 'HASH');
 9283:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
 9284:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
 9285:     my (%changes,%warning);
 9286:     my $curr_types;
 9287:     my %noedit;
 9288:     unless ($context eq 'domain') {
 9289:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
 9290:     }
 9291:     if (ref($row) eq 'ARRAY') {
 9292:         foreach my $item (@{$row}) {
 9293:             next if ($noedit{$item});
 9294:             if ($item eq 'enroll_dates') {
 9295:                 my (%currenrolldate,%newenrolldate);
 9296:                 foreach my $type ('start','end') {
 9297:                     $currenrolldate{$type} = $currsettings->{'selfenroll_'.$type.'_date'};
 9298:                     $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
 9299:                     if ($newenrolldate{$type} ne $currenrolldate{$type}) {
 9300:                         $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
 9301:                     }
 9302:                 }
 9303:             } elsif ($item eq 'access_dates') {
 9304:                 my (%currdate,%newdate);
 9305:                 foreach my $type ('start','end') {
 9306:                     $currdate{$type} = $currsettings->{'selfenroll_'.$type.'_access'};
 9307:                     $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
 9308:                     if ($newdate{$type} ne $currdate{$type}) {
 9309:                         $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
 9310:                     }
 9311:                 }
 9312:             } elsif ($item eq 'types') {
 9313:                 $curr_types = $currsettings->{'selfenroll_'.$item};
 9314:                 if ($env{'form.selfenroll_all'}) {
 9315:                     if ($curr_types ne '*') {
 9316:                         $changes{'internal.selfenroll_types'} = '*';
 9317:                     } else {
 9318:                         next;
 9319:                     }
 9320:                 } else {
 9321:                     my %currdoms;
 9322:                     my @entries = split(/;/,$curr_types);
 9323:                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
 9324:                     my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
 9325:                     my $newnum = 0;
 9326:                     my @latesttypes;
 9327:                     foreach my $num (@activations) {
 9328:                         my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
 9329:                         if (@types > 0) {
 9330:                             @types = sort(@types);
 9331:                             my $typestr = join(',',@types);
 9332:                             my $typedom = $env{'form.selfenroll_dom_'.$num};
 9333:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
 9334:                             $currdoms{$typedom} = 1;
 9335:                             $newnum ++;
 9336:                         }
 9337:                     }
 9338:                     for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {
 9339:                         if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
 9340:                             my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
 9341:                             if (@types > 0) {
 9342:                                 @types = sort(@types);
 9343:                                 my $typestr = join(',',@types);
 9344:                                 my $typedom = $env{'form.selfenroll_dom_'.$j};
 9345:                                 $latesttypes[$newnum] = $typedom.':'.$typestr;
 9346:                                 $currdoms{$typedom} = 1;
 9347:                                 $newnum ++;
 9348:                             }
 9349:                         }
 9350:                     }
 9351:                     if ($env{'form.selfenroll_newdom'} ne '') {
 9352:                         my $typedom = $env{'form.selfenroll_newdom'};
 9353:                         if ((!defined($currdoms{$typedom})) && 
 9354:                             (&Apache::lonnet::domain($typedom) ne '')) {
 9355:                             my $typestr;
 9356:                             my ($othertitle,$usertypes,$types) = 
 9357:                                 &Apache::loncommon::sorted_inst_types($typedom);
 9358:                             my $othervalue = 'any';
 9359:                             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 9360:                                 if (@{$types} > 0) {
 9361:                                     my @esc_types = map { &escape($_); } @{$types};
 9362:                                     $othervalue = 'other';
 9363:                                     $typestr = join(',',(@esc_types,$othervalue));
 9364:                                 }
 9365:                                 $typestr = $othervalue;
 9366:                             } else {
 9367:                                 $typestr = $othervalue;
 9368:                             } 
 9369:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
 9370:                             $newnum ++ ;
 9371:                         }
 9372:                     }
 9373:                     my $selfenroll_types = join(';',@latesttypes);
 9374:                     if ($selfenroll_types ne $curr_types) {
 9375:                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
 9376:                     }
 9377:                 }
 9378:             } elsif ($item eq 'limit') {
 9379:                 my $newlimit = $env{'form.selfenroll_limit'};
 9380:                 my $newcap = $env{'form.selfenroll_cap'};
 9381:                 $newcap =~s/\s+//g;
 9382:                 my $currlimit =  $currsettings->{'selfenroll_limit'};
 9383:                 $currlimit = 'none' if ($currlimit eq '');
 9384:                 my $currcap = $currsettings->{'selfenroll_cap'};
 9385:                 if ($newlimit ne $currlimit) {
 9386:                     if ($newlimit ne 'none') {
 9387:                         if ($newcap =~ /^\d+$/) {
 9388:                             if ($newcap ne $currcap) {
 9389:                                 $changes{'internal.selfenroll_cap'} = $newcap;
 9390:                             }
 9391:                             $changes{'internal.selfenroll_limit'} = $newlimit;
 9392:                         } else {
 9393:                             $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
 9394:                                 &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.'); 
 9395:                         }
 9396:                     } elsif ($currcap ne '') {
 9397:                         $changes{'internal.selfenroll_cap'} = '';
 9398:                         $changes{'internal.selfenroll_limit'} = $newlimit; 
 9399:                     }
 9400:                 } elsif ($currlimit ne 'none') {
 9401:                     if ($newcap =~ /^\d+$/) {
 9402:                         if ($newcap ne $currcap) {
 9403:                             $changes{'internal.selfenroll_cap'} = $newcap;
 9404:                         }
 9405:                     } else {
 9406:                         $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
 9407:                             &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.');
 9408:                     }
 9409:                 }
 9410:             } elsif ($item eq 'approval') {
 9411:                 my (@currnotified,@newnotified);
 9412:                 my $currapproval = $currsettings->{'selfenroll_approval'};
 9413:                 my $currnotifylist = $currsettings->{'selfenroll_notifylist'};
 9414:                 if ($currnotifylist ne '') {
 9415:                     @currnotified = split(/,/,$currnotifylist);
 9416:                     @currnotified = sort(@currnotified);
 9417:                 }
 9418:                 my $newapproval = $env{'form.selfenroll_approval'};
 9419:                 @newnotified = &Apache::loncommon::get_env_multiple('form.selfenroll_notify');
 9420:                 @newnotified = sort(@newnotified);
 9421:                 if ($newapproval ne $currapproval) {
 9422:                     $changes{'internal.selfenroll_approval'} = $newapproval;
 9423:                     if (!$newapproval) {
 9424:                         if ($currnotifylist ne '') {
 9425:                             $changes{'internal.selfenroll_notifylist'} = '';
 9426:                         }
 9427:                     } else {
 9428:                         my @differences =  
 9429:                             &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
 9430:                         if (@differences > 0) {
 9431:                             if (@newnotified > 0) {
 9432:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
 9433:                             } else {
 9434:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
 9435:                             }
 9436:                         }
 9437:                     }
 9438:                 } else {
 9439:                     my @differences = &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
 9440:                     if (@differences > 0) {
 9441:                         if (@newnotified > 0) {
 9442:                             $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
 9443:                         } else {
 9444:                             $changes{'internal.selfenroll_notifylist'} = '';
 9445:                         }
 9446:                     }
 9447:                 }
 9448:             } else {
 9449:                 my $curr_val = $currsettings->{'selfenroll_'.$item};
 9450:                 my $newval = $env{'form.selfenroll_'.$item};
 9451:                 if ($item eq 'section') {
 9452:                     $newval = $env{'form.sections'};
 9453:                     if (defined($curr_groups{$newval})) {
 9454:                         $newval = $curr_val;
 9455:                         $warning{$item} = &mt('Section for self-enrolled users unchanged as the proposed section is a group').'<br />'.
 9456:                                           &mt('Group names and section names must be distinct');
 9457:                     } elsif ($newval eq 'all') {
 9458:                         $newval = $curr_val;
 9459:                         $warning{$item} = &mt('Section for self-enrolled users unchanged, as "all" is a reserved section name.');
 9460:                     }
 9461:                     if ($newval eq '') {
 9462:                         $newval = 'none';
 9463:                     }
 9464:                 }
 9465:                 if ($newval ne $curr_val) {
 9466:                     $changes{'internal.selfenroll_'.$item} = $newval;
 9467:                 }
 9468:             }
 9469:         }
 9470:         if (keys(%warning) > 0) {
 9471:             foreach my $item (@{$row}) {
 9472:                 if (exists($warning{$item})) {
 9473:                     $r->print($warning{$item}.'<br />');
 9474:                 }
 9475:             } 
 9476:         }
 9477:         if (keys(%changes) > 0) {
 9478:             my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
 9479:             if ($putresult eq 'ok') {
 9480:                 if ((exists($changes{'internal.selfenroll_types'})) ||
 9481:                     (exists($changes{'internal.selfenroll_start_date'}))  ||
 9482:                     (exists($changes{'internal.selfenroll_end_date'}))) {
 9483:                     my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
 9484:                                                                 $cnum,undef,undef,'Course');
 9485:                     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
 9486:                     if (ref($crsinfo{$cid}) eq 'HASH') {
 9487:                         foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
 9488:                             if (exists($changes{'internal.'.$item})) {
 9489:                                 $crsinfo{$cid}{$item} = $changes{'internal.'.$item};
 9490:                             }
 9491:                         }
 9492:                         my $crsputresult =
 9493:                             &Apache::lonnet::courseidput($cdom,\%crsinfo,
 9494:                                                          $chome,'notime');
 9495:                     }
 9496:                 }
 9497:                 $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
 9498:                 foreach my $item (@{$row}) {
 9499:                     my $title = $item;
 9500:                     if (ref($lt) eq 'HASH') {
 9501:                         $title = $lt->{$item};
 9502:                     }
 9503:                     if ($item eq 'enroll_dates') {
 9504:                         foreach my $type ('start','end') {
 9505:                             if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
 9506:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
 9507:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
 9508:                                           $title,$type,$newdate).'</li>');
 9509:                             }
 9510:                         }
 9511:                     } elsif ($item eq 'access_dates') {
 9512:                         foreach my $type ('start','end') {
 9513:                             if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
 9514:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
 9515:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
 9516:                                           $title,$type,$newdate).'</li>');
 9517:                             }
 9518:                         }
 9519:                     } elsif ($item eq 'limit') {
 9520:                         if ((exists($changes{'internal.selfenroll_limit'})) ||
 9521:                             (exists($changes{'internal.selfenroll_cap'}))) {
 9522:                             my ($newval,$newcap);
 9523:                             if ($changes{'internal.selfenroll_cap'} ne '') {
 9524:                                 $newcap = $changes{'internal.selfenroll_cap'}
 9525:                             } else {
 9526:                                 $newcap = $currsettings->{'selfenroll_cap'};
 9527:                             }
 9528:                             if ($changes{'internal.selfenroll_limit'} eq 'none') {
 9529:                                 $newval = &mt('No limit');
 9530:                             } elsif ($changes{'internal.selfenroll_limit'} eq 
 9531:                                      'allstudents') {
 9532:                                 $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
 9533:                             } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
 9534:                                 $newval = &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
 9535:                             } else {
 9536:                                 my $currlimit =  $currsettings->{'selfenroll_limit'};
 9537:                                 if ($currlimit eq 'allstudents') {
 9538:                                     $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
 9539:                                 } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
 9540:                                     $newval =  &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
 9541:                                 }
 9542:                             }
 9543:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
 9544:                         }
 9545:                     } elsif ($item eq 'approval') {
 9546:                         if ((exists($changes{'internal.selfenroll_approval'})) ||
 9547:                             (exists($changes{'internal.selfenroll_notifylist'}))) {
 9548:                             my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
 9549:                             my ($newval,$newnotify);
 9550:                             if (exists($changes{'internal.selfenroll_notifylist'})) {
 9551:                                 $newnotify = $changes{'internal.selfenroll_notifylist'};
 9552:                             } else {   
 9553:                                 $newnotify = $currsettings->{'selfenroll_notifylist'};
 9554:                             }
 9555:                             if (exists($changes{'internal.selfenroll_approval'})) {
 9556:                                 if ($changes{'internal.selfenroll_approval'} !~ /^[012]$/) {
 9557:                                     $changes{'internal.selfenroll_approval'} = '0';
 9558:                                 }
 9559:                                 $newval = $selfdescs{'approval'}{$changes{'internal.selfenroll_approval'}};
 9560:                             } else {
 9561:                                 my $currapproval = $currsettings->{'selfenroll_approval'}; 
 9562:                                 if ($currapproval !~ /^[012]$/) {
 9563:                                     $currapproval = 0;
 9564:                                 }
 9565:                                 $newval = $selfdescs{'approval'}{$currapproval};
 9566:                             }
 9567:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval));
 9568:                             if ($newnotify) {
 9569:                                 $r->print('<br />'.&mt('The following will be notified when an enrollment request needs approval, or has been approved: [_1].',$newnotify));
 9570:                             } else {
 9571:                                 $r->print('<br />'.&mt('No notifications sent when an enrollment request needs approval, or has been approved.'));
 9572:                             }
 9573:                             $r->print('</li>'."\n");
 9574:                         }
 9575:                     } else {
 9576:                         if (exists($changes{'internal.selfenroll_'.$item})) {
 9577:                             my $newval = $changes{'internal.selfenroll_'.$item};
 9578:                             if ($item eq 'types') {
 9579:                                 if ($newval eq '') {
 9580:                                     $newval = &mt('None');
 9581:                                 } elsif ($newval eq '*') {
 9582:                                     $newval = &mt('Any user in any domain');
 9583:                                 }
 9584:                             } elsif ($item eq 'registered') {
 9585:                                 if ($newval eq '1') {
 9586:                                     $newval = &mt('Yes');
 9587:                                 } elsif ($newval eq '0') {
 9588:                                     $newval = &mt('No');
 9589:                                 }
 9590:                             }
 9591:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
 9592:                         }
 9593:                     }
 9594:                 }
 9595:                 $r->print('</ul>');
 9596:                 if ($env{'course.'.$cid.'.description'} ne '') {
 9597:                     my %newenvhash;
 9598:                     foreach my $key (keys(%changes)) {
 9599:                         $newenvhash{'course.'.$cid.'.'.$key} = $changes{$key};
 9600:                     }
 9601:                     &Apache::lonnet::appenv(\%newenvhash);
 9602:                 }
 9603:             } else {
 9604:                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.
 9605:                           &mt('The error was: [_1].',$putresult));
 9606:             }
 9607:         } else {
 9608:             $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
 9609:         }
 9610:     } else {
 9611:         $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
 9612:     }
 9613:     my $visactions = &cat_visibility();
 9614:     my ($cathash,%cattype);
 9615:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
 9616:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9617:         $cathash = $domconfig{'coursecategories'}{'cats'};
 9618:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
 9619:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
 9620:     } else {
 9621:         $cathash = {};
 9622:         $cattype{'auth'} = 'std';
 9623:         $cattype{'unauth'} = 'std';
 9624:     }
 9625:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
 9626:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
 9627:                   '<br />'.
 9628:                   '<br />'.$visactions->{'take'}.'<ul>'.
 9629:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
 9630:                   '</ul>');
 9631:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
 9632:         if ($currsettings->{'uniquecode'}) {
 9633:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
 9634:         } else {
 9635:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
 9636:                   '<br />'.
 9637:                   '<br />'.$visactions->{'take'}.'<ul>'.
 9638:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
 9639:                   '</ul><br />');
 9640:         }
 9641:     } else {
 9642:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
 9643:         if (ref($visactions) eq 'HASH') {
 9644:             if (!$visible) {
 9645:                 $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
 9646:                           '<br />');
 9647:                 if (ref($vismsgs) eq 'ARRAY') {
 9648:                     $r->print('<br />'.$visactions->{'take'}.'<ul>');
 9649:                     foreach my $item (@{$vismsgs}) {
 9650:                         $r->print('<li>'.$visactions->{$item}.'</li>');
 9651:                     }
 9652:                     $r->print('</ul>');
 9653:                 }
 9654:                 $r->print($cansetvis);
 9655:             }
 9656:         }
 9657:     } 
 9658:     return;
 9659: }
 9660: 
 9661: #---------------------------------------------- end functions for &phase_two
 9662: 
 9663: #--------------------------------- functions for &phase_two and &phase_three
 9664: 
 9665: #--------------------------end of functions for &phase_two and &phase_three
 9666: 
 9667: 1;
 9668: __END__
 9669: 
 9670: 

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