Annotation of loncom/interface/loncreateuser.pm, revision 1.376

1.20      harris41    1: # The LearningOnline Network with CAPA
1.1       www         2: # Create a user
                      3: #
1.376   ! raeburn     4: # $Id: loncreateuser.pm,v 1.375 2013/03/01 04:55:34 raeburn Exp $
1.22      albertel    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: #
1.20      harris41   28: ###
                     29: 
1.1       www        30: package Apache::loncreateuser;
1.66      bowersj2   31: 
                     32: =pod
                     33: 
                     34: =head1 NAME
                     35: 
1.263     jms        36: Apache::loncreateuser.pm
1.66      bowersj2   37: 
                     38: =head1 SYNOPSIS
                     39: 
1.263     jms        40:     Handler to create users and custom roles
                     41: 
                     42:     Provides an Apache handler for creating users,
1.66      bowersj2   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: 
1.324     raeburn    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>.
1.66      bowersj2   59: 
1.324     raeburn    60: Custom role definitions are stored in the C<roles.db> file of the creator
                     61: of the role.
1.66      bowersj2   62: 
                     63: =cut
1.1       www        64: 
                     65: use strict;
                     66: use Apache::Constants qw(:common :http);
                     67: use Apache::lonnet;
1.54      bowersj2   68: use Apache::loncommon;
1.68      www        69: use Apache::lonlocal;
1.117     raeburn    70: use Apache::longroup;
1.190     raeburn    71: use Apache::lonuserutils;
1.307     raeburn    72: use Apache::loncoursequeueadmin;
1.139     albertel   73: use LONCAPA qw(:DEFAULT :match);
1.1       www        74: 
1.20      harris41   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: 
1.94      matthew    82: sub initialize_authen_forms {
1.227     raeburn    83:     my ($dom,$formname,$curr_authtype,$mode) = @_;
                     84:     my ($krbdef,$krbdefdom) = &Apache::loncommon::get_kerberos_defaults($dom);
                     85:     my %param = ( formname => $formname,
1.187     raeburn    86:                   kerb_def_dom => $krbdefdom,
1.227     raeburn    87:                   kerb_def_auth => $krbdef,
1.187     raeburn    88:                   domain => $dom,
                     89:                 );
1.188     raeburn    90:     my %abv_auth = &auth_abbrev();
1.227     raeburn    91:     if ($curr_authtype =~ /^(krb4|krb5|internal|localauth|unix):(.*)$/) {
1.188     raeburn    92:         my $long_auth = $1;
1.227     raeburn    93:         my $curr_autharg = $2;
1.188     raeburn    94:         my %abv_auth = &auth_abbrev();
                     95:         $param{'curr_authtype'} = $abv_auth{$long_auth};
                     96:         if ($long_auth =~ /^krb(4|5)$/) {
                     97:             $param{'curr_kerb_ver'} = $1;
1.227     raeburn    98:             $param{'curr_autharg'} = $curr_autharg;
1.188     raeburn    99:         }
1.205     raeburn   100:         if ($mode eq 'modifyuser') {
                    101:             $param{'mode'} = $mode;
                    102:         }
1.187     raeburn   103:     }
1.227     raeburn   104:     $loginscript  = &Apache::loncommon::authform_header(%param);
                    105:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
1.31      matthew   106:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
                    107:     $authformint  = &Apache::loncommon::authform_internal(%param);
                    108:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
                    109:     $authformloc  = &Apache::loncommon::authform_local(%param);
1.20      harris41  110: }
                    111: 
1.188     raeburn   112: sub auth_abbrev {
                    113:     my %abv_auth = (
1.368     raeburn   114:                      krb5      => 'krb',
                    115:                      krb4      => 'krb',
                    116:                      internal  => 'int',
                    117:                      localauth => 'loc',
                    118:                      unix      => 'fsys',
1.188     raeburn   119:                    );
                    120:     return %abv_auth;
                    121: }
1.43      www       122: 
1.134     raeburn   123: # ====================================================
                    124: 
                    125: sub portfolio_quota {
                    126:     my ($ccuname,$ccdomain) = @_;
                    127:     my %lt = &Apache::lonlocal::texthash(
1.267     raeburn   128:                    'usrt'      => "User Tools",
                    129:                    'disk'      => "Disk space allocated to user's portfolio files",
                    130:                    'cuqu'      => "Current quota",
                    131:                    'cust'      => "Custom quota",
                    132:                    'defa'      => "Default",
                    133:                    'chqu'      => "Change quota",
1.134     raeburn   134:     );
1.149     raeburn   135:     my ($currquota,$quotatype,$inststatus,$defquota) = 
                    136:         &Apache::loncommon::get_user_quota($ccuname,$ccdomain);
                    137:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
                    138:     my ($longinsttype,$showquota,$custom_on,$custom_off,$defaultinfo);
                    139:     if ($inststatus ne '') {
                    140:         if ($usertypes->{$inststatus} ne '') {
                    141:             $longinsttype = $usertypes->{$inststatus};
                    142:         }
                    143:     }
                    144:     $custom_on = ' ';
                    145:     $custom_off = ' checked="checked" ';
                    146:     my $quota_javascript = <<"END_SCRIPT";
                    147: <script type="text/javascript">
1.301     bisitz    148: // <![CDATA[
1.149     raeburn   149: function quota_changes(caller) {
                    150:     if (caller == "custom") {
                    151:         if (document.cu.customquota[0].checked) {
                    152:             document.cu.portfolioquota.value = "";
                    153:         }
                    154:     }
                    155:     if (caller == "quota") {
                    156:         document.cu.customquota[1].checked = true;
                    157:     }
                    158: }
1.301     bisitz    159: // ]]>
1.149     raeburn   160: </script>
                    161: END_SCRIPT
                    162:     if ($quotatype eq 'custom') {
                    163:         $custom_on = $custom_off;
                    164:         $custom_off = ' ';
                    165:         $showquota = $currquota;
                    166:         if ($longinsttype eq '') {
1.230     bisitz    167:             $defaultinfo = &mt('For this user, the default quota would be [_1]'
                    168:                             .' Mb.',$defquota);
1.149     raeburn   169:         } else {
1.231     raeburn   170:             $defaultinfo = &mt("For this user, the default quota would be [_1]".
                    171:                                " Mb, as determined by the user's institutional".
                    172:                                " affiliation ([_2]).",$defquota,$longinsttype);
1.149     raeburn   173:         }
                    174:     } else {
                    175:         if ($longinsttype eq '') {
1.230     bisitz    176:             $defaultinfo = &mt('For this user, the default quota is [_1]'
                    177:                             .' Mb.',$defquota);
1.149     raeburn   178:         } else {
1.231     raeburn   179:             $defaultinfo = &mt("For this user, the default quota of [_1]".
                    180:                                " Mb, is determined by the user's institutional".
                    181:                                " affiliation ([_2]).",$defquota,$longinsttype);
1.149     raeburn   182:         }
                    183:     }
1.267     raeburn   184: 
                    185:     my $output = $quota_javascript."\n".
                    186:                  '<h3>'.$lt{'usrt'}.'</h3>'."\n".
                    187:                  &Apache::loncommon::start_data_table();
                    188: 
                    189:     if (&Apache::lonnet::allowed('mut',$ccdomain)) {
1.275     raeburn   190:         $output .= &build_tools_display($ccuname,$ccdomain,'tools');
1.267     raeburn   191:     }
                    192:     if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
                    193:         $output .= '<tr class="LC_info_row">'."\n".
                    194:                    '    <td>'.$lt{'disk'}.'</td>'."\n".
                    195:                    '  </tr>'."\n".
                    196:                    &Apache::loncommon::start_data_table_row()."\n".
                    197:                    '  <td>'.$lt{'cuqu'}.': '.
                    198:                    $currquota.'&nbsp;Mb.&nbsp;&nbsp;'.
                    199:                    $defaultinfo.'</td>'."\n".
                    200:                    &Apache::loncommon::end_data_table_row()."\n".
                    201:                    &Apache::loncommon::start_data_table_row()."\n".
                    202:                    '  <td><span class="LC_nobreak">'.$lt{'chqu'}.
                    203:                    ': <label>'.
                    204:                    '<input type="radio" name="customquota" value="0" '.
                    205:                    $custom_off.' onchange="javascript:quota_changes('."'custom'".')"'.
                    206:                    ' />'.$lt{'defa'}.'&nbsp;('.$defquota.' Mb).</label>&nbsp;'.
                    207:                    '&nbsp;<label><input type="radio" name="customquota" value="1" '. 
                    208:                    $custom_on.'  onchange="javascript:quota_changes('."'custom'".')" />'.
                    209:                    $lt{'cust'}.':</label>&nbsp;'.
                    210:                    '<input type="text" name="portfolioquota" size ="5" value="'.
                    211:                    $showquota.'" onfocus="javascript:quota_changes('."'quota'".')" '.
                    212:                    '/>&nbsp;Mb</span></td>'."\n".
                    213:                    &Apache::loncommon::end_data_table_row()."\n";
                    214:     }  
                    215:     $output .= &Apache::loncommon::end_data_table();
1.134     raeburn   216:     return $output;
                    217: }
                    218: 
1.275     raeburn   219: sub build_tools_display {
                    220:     my ($ccuname,$ccdomain,$context) = @_;
1.306     raeburn   221:     my (@usertools,%userenv,$output,@options,%validations,%reqtitles,%reqdisplay,
1.332     raeburn   222:         $colspan,$isadv,%domconfig);
1.275     raeburn   223:     my %lt = &Apache::lonlocal::texthash (
                    224:                    'blog'       => "Personal User Blog",
                    225:                    'aboutme'    => "Personal Information Page",
1.361     raeburn   226:                    'webdav'     => "WebDAV access to authoring spaces (if SSL and author/co-author)",
1.275     raeburn   227:                    'portfolio'  => "Personal User Portfolio",
                    228:                    'avai'       => "Available",
                    229:                    'cusa'       => "availability",
                    230:                    'chse'       => "Change setting",
                    231:                    'usde'       => "Use default",
                    232:                    'uscu'       => "Use custom",
                    233:                    'official'   => 'Can request creation of official courses',
1.299     raeburn   234:                    'unofficial' => 'Can request creation of unofficial courses',
                    235:                    'community'  => 'Can request creation of communities',
1.362     raeburn   236:                    'requestauthor'  => 'Can request author space',
1.275     raeburn   237:     );
1.279     raeburn   238:     if ($context eq 'requestcourses') {
1.275     raeburn   239:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.299     raeburn   240:                       'requestcourses.official','requestcourses.unofficial',
                    241:                       'requestcourses.community');
                    242:         @usertools = ('official','unofficial','community');
1.309     raeburn   243:         @options =('norequest','approval','autolimit','validate');
1.306     raeburn   244:         %validations = &Apache::lonnet::auto_courserequest_checks($ccdomain);
                    245:         %reqtitles = &courserequest_titles();
                    246:         %reqdisplay = &courserequest_display();
                    247:         $colspan = ' colspan="2"';
1.332     raeburn   248:         %domconfig =
                    249:             &Apache::lonnet::get_dom('configuration',['requestcourses'],$ccdomain);
                    250:         $isadv = &Apache::lonnet::is_advanced_user($ccuname,$ccdomain);
1.362     raeburn   251:     } elsif ($context eq 'requestauthor') {
                    252:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    253:                                                     'requestauthor');
                    254:         @usertools = ('requestauthor');
                    255:         @options =('norequest','approval','automatic');
                    256:         %reqtitles = &requestauthor_titles();
                    257:         %reqdisplay = &requestauthor_display();
                    258:         $colspan = ' colspan="2"';
                    259:         %domconfig =
                    260:             &Apache::lonnet::get_dom('configuration',['requestauthor'],$ccdomain);
1.275     raeburn   261:     } else {
                    262:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.361     raeburn   263:                           'tools.aboutme','tools.portfolio','tools.blog',
                    264:                           'tools.webdav');
                    265:         @usertools = ('aboutme','blog','webdav','portfolio');
1.275     raeburn   266:     }
                    267:     foreach my $item (@usertools) {
1.306     raeburn   268:         my ($custom_access,$curr_access,$cust_on,$cust_off,$tool_on,$tool_off,
                    269:             $currdisp,$custdisp,$custradio);
1.275     raeburn   270:         $cust_off = 'checked="checked" ';
                    271:         $tool_on = 'checked="checked" ';
                    272:         $curr_access =  
                    273:             &Apache::lonnet::usertools_access($ccuname,$ccdomain,$item,undef,
                    274:                                               $context);
1.362     raeburn   275:         if ($context eq 'requestauthor') {
                    276:             if ($userenv{$context} ne '') {
                    277:                 $cust_on = ' checked="checked" ';
                    278:                 $cust_off = '';
                    279:             }  
                    280:         } elsif ($userenv{$context.'.'.$item} ne '') {
1.306     raeburn   281:             $cust_on = ' checked="checked" ';
                    282:             $cust_off = '';
                    283:         }
                    284:         if ($context eq 'requestcourses') {
                    285:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   286:                 $custom_access = &mt('Currently from default setting.');
1.306     raeburn   287:             } else {
                    288:                 $custom_access = &mt('Currently from custom setting.');
1.275     raeburn   289:             }
1.362     raeburn   290:         } elsif ($context eq 'requestauthor') {
                    291:             if ($userenv{$context} eq '') {
                    292:                 $custom_access = &mt('Currently from default setting.');
                    293:             } else {
                    294:                 $custom_access = &mt('Currently from custom setting.');
                    295:             }
1.275     raeburn   296:         } else {
1.306     raeburn   297:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   298:                 $custom_access =
1.306     raeburn   299:                     &mt('Availability determined currently from default setting.');
                    300:                 if (!$curr_access) {
                    301:                     $tool_off = 'checked="checked" ';
                    302:                     $tool_on = '';
                    303:                 }
                    304:             } else {
1.314     raeburn   305:                 $custom_access =
1.306     raeburn   306:                     &mt('Availability determined currently from custom setting.');
                    307:                 if ($userenv{$context.'.'.$item} == 0) {
                    308:                     $tool_off = 'checked="checked" ';
                    309:                     $tool_on = '';
                    310:                 }
1.275     raeburn   311:             }
                    312:         }
                    313:         $output .= '  <tr class="LC_info_row">'."\n".
1.306     raeburn   314:                    '   <td'.$colspan.'>'.$lt{$item}.'</td>'."\n".
1.275     raeburn   315:                    '  </tr>'."\n".
1.306     raeburn   316:                    &Apache::loncommon::start_data_table_row()."\n";
1.362     raeburn   317:         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.306     raeburn   318:             my ($curroption,$currlimit);
1.362     raeburn   319:             my $envkey = $context.'.'.$item;
                    320:             if ($context eq 'requestauthor') {
                    321:                 $envkey = $context;
                    322:             }
                    323:             if ($userenv{$envkey} ne '') {
                    324:                 $curroption = $userenv{$envkey};
1.332     raeburn   325:             } else {
                    326:                 my (@inststatuses);
1.362     raeburn   327:                 if ($context eq 'requestcourses') {
                    328:                     $curroption =
                    329:                         &Apache::loncoursequeueadmin::get_processtype('course',$ccuname,$ccdomain,
                    330:                                                                       $isadv,$ccdomain,$item,
                    331:                                                                       \@inststatuses,\%domconfig);
                    332:                 } else {
                    333:                      $curroption = 
                    334:                          &Apache::loncoursequeueadmin::get_processtype('requestauthor',$ccuname,$ccdomain,
                    335:                                                                        $isadv,$ccdomain,undef,
                    336:                                                                        \@inststatuses,\%domconfig);
                    337:                 }
1.332     raeburn   338:             }
1.306     raeburn   339:             if (!$curroption) {
                    340:                 $curroption = 'norequest';
                    341:             }
                    342:             if ($curroption =~ /^autolimit=(\d*)$/) {
                    343:                 $currlimit = $1;
1.314     raeburn   344:                 if ($currlimit eq '') {
                    345:                     $currdisp = &mt('Yes, automatic creation');
                    346:                 } else {
                    347:                     $currdisp = &mt('Yes, up to [quant,_1,request]/user',$currlimit);
                    348:                 }
1.306     raeburn   349:             } else {
                    350:                 $currdisp = $reqdisplay{$curroption};
                    351:             }
                    352:             $custdisp = '<table>';
                    353:             foreach my $option (@options) {
                    354:                 my $val = $option;
                    355:                 if ($option eq 'norequest') {
                    356:                     $val = 0;
                    357:                 }
                    358:                 if ($option eq 'validate') {
                    359:                     my $canvalidate = 0;
                    360:                     if (ref($validations{$item}) eq 'HASH') {
                    361:                         if ($validations{$item}{'_custom_'}) {
                    362:                             $canvalidate = 1;
                    363:                         }
                    364:                     }
                    365:                     next if (!$canvalidate);
                    366:                 }
                    367:                 my $checked = '';
                    368:                 if ($option eq $curroption) {
                    369:                     $checked = ' checked="checked"';
                    370:                 } elsif ($option eq 'autolimit') {
                    371:                     if ($curroption =~ /^autolimit/) {
                    372:                         $checked = ' checked="checked"';
                    373:                     }
                    374:                 }
1.362     raeburn   375:                 my $name = 'crsreq_'.$item;
                    376:                 if ($context eq 'requestauthor') {
                    377:                     $name = $item;
                    378:                 }
1.306     raeburn   379:                 $custdisp .= '<tr><td><span class="LC_nobreak"><label>'.
1.362     raeburn   380:                              '<input type="radio" name="'.$name.'" '.
                    381:                              'value="'.$val.'"'.$checked.' />'.
1.306     raeburn   382:                              $reqtitles{$option}.'</label>&nbsp;';
                    383:                 if ($option eq 'autolimit') {
1.362     raeburn   384:                     $custdisp .= '<input type="text" name="'.$name.
                    385:                                  '_limit" size="1" '.
1.314     raeburn   386:                                  'value="'.$currlimit.'" /></span><br />'.
                    387:                                  $reqtitles{'unlimited'};
1.362     raeburn   388:                 } else {
                    389:                     $custdisp .= '</span>';
                    390:                 }
                    391:                 $custdisp .= '</td></tr>';
1.306     raeburn   392:             }
                    393:             $custdisp .= '</table>';
                    394:             $custradio = '</span></td><td>'.&mt('Custom setting').'<br />'.$custdisp;
                    395:         } else {
                    396:             $currdisp = ($curr_access?&mt('Yes'):&mt('No'));
1.362     raeburn   397:             my $name = $context.'_'.$item;
                    398:             if ($context eq 'requestauthor') {
                    399:                 $name = $context;
                    400:             }
1.306     raeburn   401:             $custdisp = '<span class="LC_nobreak"><label>'.
1.362     raeburn   402:                         '<input type="radio" name="'.$name.'"'.
1.361     raeburn   403:                         ' value="1" '.$tool_on.'/>'.&mt('On').'</label>&nbsp;<label>'.
1.362     raeburn   404:                         '<input type="radio" name="'.$name.'" value="0" '.
1.306     raeburn   405:                         $tool_off.'/>'.&mt('Off').'</label></span>';
                    406:             $custradio = ('&nbsp;'x2).'--'.$lt{'cusa'}.':&nbsp;'.$custdisp.
                    407:                           '</span>';
                    408:         }
                    409:         $output .= '  <td'.$colspan.'>'.$custom_access.('&nbsp;'x4).
                    410:                    $lt{'avai'}.': '.$currdisp.'</td>'."\n".
1.275     raeburn   411:                    &Apache::loncommon::end_data_table_row()."\n".
                    412:                    &Apache::loncommon::start_data_table_row()."\n".
1.306     raeburn   413:                    '  <td style="vertical-align:top;"><span class="LC_nobreak">'.
                    414:                    $lt{'chse'}.': <label>'.
1.275     raeburn   415:                    '<input type="radio" name="custom'.$item.'" value="0" '.
1.306     raeburn   416:                    $cust_off.'/>'.$lt{'usde'}.'</label>'.('&nbsp;' x3).
                    417:                    '<label><input type="radio" name="custom'.$item.'" value="1" '.
                    418:                    $cust_on.'/>'.$lt{'uscu'}.'</label>'.$custradio.'</td>'.
1.275     raeburn   419:                    &Apache::loncommon::end_data_table_row()."\n";
                    420:     }
                    421:     return $output;
                    422: }
                    423: 
1.300     raeburn   424: sub coursereq_externaluser {
                    425:     my ($ccuname,$ccdomain,$cdom) = @_;
1.306     raeburn   426:     my (@usertools,@options,%validations,%userenv,$output);
1.300     raeburn   427:     my %lt = &Apache::lonlocal::texthash (
                    428:                    'official'   => 'Can request creation of official courses',
                    429:                    'unofficial' => 'Can request creation of unofficial courses',
                    430:                    'community'  => 'Can request creation of communities',
                    431:     );
                    432: 
                    433:     %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    434:                       'reqcrsotherdom.official','reqcrsotherdom.unofficial',
                    435:                       'reqcrsotherdom.community');
                    436:     @usertools = ('official','unofficial','community');
1.309     raeburn   437:     @options = ('approval','validate','autolimit');
1.306     raeburn   438:     %validations = &Apache::lonnet::auto_courserequest_checks($cdom);
                    439:     my $optregex = join('|',@options);
                    440:     my %reqtitles = &courserequest_titles();
1.300     raeburn   441:     foreach my $item (@usertools) {
1.306     raeburn   442:         my ($curroption,$currlimit,$tooloff);
1.300     raeburn   443:         if ($userenv{'reqcrsotherdom.'.$item} ne '') {
                    444:             my @curr = split(',',$userenv{'reqcrsotherdom.'.$item});
1.314     raeburn   445:             foreach my $req (@curr) {
                    446:                 if ($req =~ /^\Q$cdom\E\:($optregex)=?(\d*)$/) {
                    447:                     $curroption = $1;
                    448:                     $currlimit = $2;
                    449:                     last;
1.306     raeburn   450:                 }
                    451:             }
1.314     raeburn   452:             if (!$curroption) {
                    453:                 $curroption = 'norequest';
                    454:                 $tooloff = ' checked="checked"';
                    455:             }
1.306     raeburn   456:         } else {
                    457:             $curroption = 'norequest';
                    458:             $tooloff = ' checked="checked"';
                    459:         }
                    460:         $output.= &Apache::loncommon::start_data_table_row()."\n".
1.314     raeburn   461:                   '  <td><span class="LC_nobreak">'.$lt{$item}.': </span></td><td>'.
                    462:                   '<table><tr><td valign="top">'."\n".
1.306     raeburn   463:                   '<label><input type="radio" name="reqcrsotherdom_'.$item.
1.314     raeburn   464:                   '" value=""'.$tooloff.' />'.$reqtitles{'norequest'}.
                    465:                   '</label></td>';
1.306     raeburn   466:         foreach my $option (@options) {
                    467:             if ($option eq 'validate') {
                    468:                 my $canvalidate = 0;
                    469:                 if (ref($validations{$item}) eq 'HASH') {
                    470:                     if ($validations{$item}{'_external_'}) {
                    471:                         $canvalidate = 1;
                    472:                     }
                    473:                 }
                    474:                 next if (!$canvalidate);
                    475:             }
                    476:             my $checked = '';
                    477:             if ($option eq $curroption) {
                    478:                 $checked = ' checked="checked"';
                    479:             }
1.314     raeburn   480:             $output .= '<td valign="top"><span class="LC_nobreak"><label>'.
1.306     raeburn   481:                        '<input type="radio" name="reqcrsotherdom_'.$item.
                    482:                        '" value="'.$option.'"'.$checked.' />'.
1.314     raeburn   483:                        $reqtitles{$option}.'</label>';
1.306     raeburn   484:             if ($option eq 'autolimit') {
1.314     raeburn   485:                 $output .= '&nbsp;<input type="text" name="reqcrsotherdom_'.
1.306     raeburn   486:                            $item.'_limit" size="1" '.
1.314     raeburn   487:                            'value="'.$currlimit.'" /></span>'.
                    488:                            '<br />'.$reqtitles{'unlimited'};
                    489:             } else {
                    490:                 $output .= '</span>';
1.300     raeburn   491:             }
1.314     raeburn   492:             $output .= '</td>';
1.300     raeburn   493:         }
1.314     raeburn   494:         $output .= '</td></tr></table></td>'."\n".
1.300     raeburn   495:                    &Apache::loncommon::end_data_table_row()."\n";
                    496:     }
                    497:     return $output;
                    498: }
                    499: 
1.362     raeburn   500: sub domainrole_req {
                    501:     my ($ccuname,$ccdomain) = @_;
                    502:     return '<br /><h3>'.
                    503:            &mt('User Can Request Assignment of Domain Roles?').
                    504:            '</h3>'."\n".
                    505:            &Apache::loncommon::start_data_table().
                    506:            &build_tools_display($ccuname,$ccdomain,
                    507:                                 'requestauthor').
                    508:            &Apache::loncommon::end_data_table();
                    509: }
                    510: 
1.306     raeburn   511: sub courserequest_titles {
                    512:     my %titles = &Apache::lonlocal::texthash (
                    513:                                    official   => 'Official',
                    514:                                    unofficial => 'Unofficial',
                    515:                                    community  => 'Communities',
                    516:                                    norequest  => 'Not allowed',
1.309     raeburn   517:                                    approval   => 'Approval by Dom. Coord.',
1.306     raeburn   518:                                    validate   => 'With validation',
                    519:                                    autolimit  => 'Numerical limit',
1.314     raeburn   520:                                    unlimited  => '(blank for unlimited)',
1.306     raeburn   521:                  );
                    522:     return %titles;
                    523: }
                    524: 
                    525: sub courserequest_display {
                    526:     my %titles = &Apache::lonlocal::texthash (
1.309     raeburn   527:                                    approval   => 'Yes, need approval',
1.306     raeburn   528:                                    validate   => 'Yes, with validation',
                    529:                                    norequest  => 'No',
                    530:    );
                    531:    return %titles;
                    532: }
                    533: 
1.362     raeburn   534: sub requestauthor_titles {
                    535:     my %titles = &Apache::lonlocal::texthash (
                    536:                                    norequest  => 'Not allowed',
                    537:                                    approval   => 'Approval by Dom. Coord.',
                    538:                                    automatic  => 'Automatic approval',
                    539:                  );
                    540:     return %titles;
                    541: 
                    542: }
                    543: 
                    544: sub requestauthor_display {
                    545:     my %titles = &Apache::lonlocal::texthash (
                    546:                                    approval   => 'Yes, need approval',
                    547:                                    automatic  => 'Yes, automatic approval',
                    548:                                    norequest  => 'No',
                    549:    );
                    550:    return %titles;
                    551: }
                    552: 
                    553: sub curr_requestauthor {
                    554:     my ($uname,$udom,$isadv,$inststatuses,$domconfig) = @_;
                    555:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
                    556:     if ($uname eq '' || $udom eq '') {
                    557:         $uname = $env{'user.name'};
                    558:         $udom = $env{'user.domain'};
                    559:         $isadv = $env{'user.adv'};
                    560:     }
                    561:     my (%userenv,%settings,$val);
                    562:     my @options = ('automatic','approval');
                    563:     %userenv =
                    564:         &Apache::lonnet::userenvironment($udom,$uname,'requestauthor','inststatus');
                    565:     if ($userenv{'requestauthor'}) {
                    566:         $val = $userenv{'requestauthor'};
                    567:         @{$inststatuses} = ('_custom_');
                    568:     } else {
                    569:         my %alltasks;
                    570:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') {
                    571:             %settings = %{$domconfig->{'requestauthor'}};
                    572:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
                    573:                 $val = $settings{'_LC_adv'};
                    574:                 @{$inststatuses} = ('_LC_adv_');
                    575:             } else {
                    576:                 if ($userenv{'inststatus'} ne '') {
                    577:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
                    578:                 } else {
                    579:                     @{$inststatuses} = ('default');
                    580:                 }
                    581:                 foreach my $status (@{$inststatuses}) {
                    582:                     if (exists($settings{$status})) {
                    583:                         my $value = $settings{$status};
                    584:                         next unless ($value);
                    585:                         unless (exists($alltasks{$value})) {
                    586:                             if (ref($alltasks{$value}) eq 'ARRAY') {
                    587:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
                    588:                                     push(@{$alltasks{$value}},$status);
                    589:                                 }
                    590:                             } else {
                    591:                                 @{$alltasks{$value}} = ($status);
                    592:                             }
                    593:                         }
                    594:                     }
                    595:                 }
                    596:                 foreach my $option (@options) {
                    597:                     if ($alltasks{$option}) {
                    598:                         $val = $option;
                    599:                         last;
                    600:                     }
                    601:                 }
                    602:             }
                    603:         }
                    604:     }
                    605:     return $val;
                    606: }
                    607: 
1.2       www       608: # =================================================================== Phase one
1.1       www       609: 
1.42      matthew   610: sub print_username_entry_form {
1.351     raeburn   611:     my ($r,$context,$response,$srch,$forcenewuser,$crstype,$brcrum) = @_;
1.101     albertel  612:     my $defdom=$env{'request.role.domain'};
1.160     raeburn   613:     my $formtoset = 'crtuser';
                    614:     if (exists($env{'form.startrolename'})) {
                    615:         $formtoset = 'docustom';
                    616:         $env{'form.rolename'} = $env{'form.startrolename'};
1.207     raeburn   617:     } elsif ($env{'form.origform'} eq 'crtusername') {
                    618:         $formtoset =  $env{'form.origform'};
1.160     raeburn   619:     }
                    620: 
                    621:     my ($jsback,$elements) = &crumb_utilities();
                    622: 
                    623:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
1.165     albertel  624:         '<script type="text/javascript">'."\n".
1.301     bisitz    625:         '// <![CDATA['."\n".
                    626:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset})."\n".
                    627:         '// ]]>'."\n".
1.162     raeburn   628:         '</script>'."\n";
1.160     raeburn   629: 
1.324     raeburn   630:     my %existingroles=&Apache::lonuserutils::my_custom_roles($crstype);
                    631:     if (($env{'form.action'} eq 'custom') && (keys(%existingroles) > 0)
                    632:         && (&Apache::lonnet::allowed('mcr','/'))) {
                    633:         $jscript .= &customrole_javascript();
                    634:     }
1.224     raeburn   635:     my $helpitem = 'Course_Change_Privileges';
                    636:     if ($env{'form.action'} eq 'custom') {
                    637:         $helpitem = 'Course_Editing_Custom_Roles';
                    638:     } elsif ($env{'form.action'} eq 'singlestudent') {
                    639:         $helpitem = 'Course_Add_Student';
                    640:     }
1.351     raeburn   641:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
                    642:     if ($env{'form.action'} eq 'custom') {
                    643:         push(@{$brcrum},
                    644:                  {href=>"javascript:backPage(document.crtuser)",       
                    645:                   text=>"Pick custom role",
                    646:                   help => $helpitem,}
                    647:                  );
                    648:     } else {
                    649:         push (@{$brcrum},
                    650:                   {href => "javascript:backPage(document.crtuser)",
                    651:                    text => $breadcrumb_text{'search'},
                    652:                    help => $helpitem,
                    653:                    faq  => 282,
                    654:                    bug  => 'Instructor Interface',}
                    655:                   );
                    656:     }
                    657:     my %loaditems = (
                    658:                 'onload' => "javascript:setFormElements(document.$formtoset)",
                    659:                     );
                    660:     my $args = {bread_crumbs           => $brcrum,
                    661:                 bread_crumbs_component => 'User Management',
                    662:                 add_entries            => \%loaditems,};
                    663:     $r->print(&Apache::loncommon::start_page('User Management',$jscript,$args));
                    664: 
1.71      sakharuk  665:     my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   666:                     'srst' => 'Search for a user and enroll as a student',
1.318     raeburn   667:                     'srme' => 'Search for a user and enroll as a member',
1.229     raeburn   668:                     'srad' => 'Search for a user and modify/add user information or roles',
1.71      sakharuk  669: 		    'usr'  => "Username",
                    670:                     'dom'  => "Domain",
1.324     raeburn   671:                     'ecrp' => "Define or Edit Custom Role",
                    672:                     'nr'   => "role name",
1.282     schafran  673:                     'cre'  => "Next",
1.71      sakharuk  674: 				       );
1.351     raeburn   675: 
1.214     raeburn   676:     if ($env{'form.action'} eq 'custom') {
1.190     raeburn   677:         if (&Apache::lonnet::allowed('mcr','/')) {
1.324     raeburn   678:             my $newroletext = &mt('Define new custom role:');
                    679:             $r->print('<form action="/adm/createuser" method="post" name="docustom">'.
                    680:                       '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                    681:                       '<input type="hidden" name="phase" value="selected_custom_edit" />'.
                    682:                       '<h3>'.$lt{'ecrp'}.'</h3>'.
                    683:                       &Apache::loncommon::start_data_table().
                    684:                       &Apache::loncommon::start_data_table_row().
                    685:                       '<td>');
                    686:             if (keys(%existingroles) > 0) {
                    687:                 $r->print('<br /><label><input type="radio" name="customroleaction" value="new" checked="checked" onclick="setCustomFields();" /><b>'.$newroletext.'</b></label>');
                    688:             } else {
                    689:                 $r->print('<br /><input type="hidden" name="customroleaction" value="new" /><b>'.$newroletext.'</b>');
                    690:             }
                    691:             $r->print('</td><td align="center">'.$lt{'nr'}.'<br /><input type="text" size="15" name="newrolename" onfocus="setCustomAction('."'new'".');" /></td>'.
                    692:                       &Apache::loncommon::end_data_table_row());
                    693:             if (keys(%existingroles) > 0) {
                    694:                 $r->print(&Apache::loncommon::start_data_table_row().'<td><br />'.
                    695:                           '<label><input type="radio" name="customroleaction" value="edit" onclick="setCustomFields();"/><b>'.
                    696:                           &mt('View/Modify existing role:').'</b></label></td>'.
                    697:                           '<td align="center"><br />'.
                    698:                           '<select name="rolename" onchange="setCustomAction('."'edit'".');">'.
1.326     raeburn   699:                           '<option value="" selected="selected">'.
1.324     raeburn   700:                           &mt('Select'));
                    701:                 foreach my $role (sort(keys(%existingroles))) {
1.326     raeburn   702:                     $r->print('<option value="'.$role.'">'.$role.'</option>');
1.324     raeburn   703:                 }
                    704:                 $r->print('</select>'.
                    705:                           '</td>'.
                    706:                           &Apache::loncommon::end_data_table_row());
                    707:             }
                    708:             $r->print(&Apache::loncommon::end_data_table().'<p>'.
                    709:                       '<input name="customeditor" type="submit" value="'.
                    710:                       $lt{'cre'}.'" /></p>'.
                    711:                       '</form>');
1.190     raeburn   712:         }
1.213     raeburn   713:     } else {
1.229     raeburn   714:         my $actiontext = $lt{'srad'};
1.213     raeburn   715:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   716:             if ($crstype eq 'Community') {
                    717:                 $actiontext = $lt{'srme'};
                    718:             } else {
                    719:                 $actiontext = $lt{'srst'};
                    720:             }
1.213     raeburn   721:         }
1.324     raeburn   722:         $r->print("<h3>$actiontext</h3>");
1.213     raeburn   723:         if ($env{'form.origform'} ne 'crtusername') {
                    724:             $r->print("\n".$response);
                    725:         }
1.318     raeburn   726:         $r->print(&entry_form($defdom,$srch,$forcenewuser,$context,$response,$crstype));
1.107     www       727:     }
1.110     albertel  728: }
                    729: 
1.324     raeburn   730: sub customrole_javascript {
                    731:     my $js = <<"END";
                    732: <script type="text/javascript">
                    733: // <![CDATA[
                    734: 
                    735: function setCustomFields() {
                    736:     if (document.docustom.customroleaction.length > 0) {
                    737:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    738:             if (document.docustom.customroleaction[i].checked) {
                    739:                 if (document.docustom.customroleaction[i].value == 'new') {
                    740:                     document.docustom.rolename.selectedIndex = 0;
                    741:                 } else {
                    742:                     document.docustom.newrolename.value = '';
                    743:                 }
                    744:             }
                    745:         }
                    746:     }
                    747:     return;
                    748: }
                    749: 
                    750: function setCustomAction(caller) {
                    751:     if (document.docustom.customroleaction.length > 0) {
                    752:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    753:             if (document.docustom.customroleaction[i].value == caller) {
                    754:                 document.docustom.customroleaction[i].checked = true;
                    755:             }
                    756:         }
                    757:     }
                    758:     setCustomFields();
                    759:     return;
                    760: }
                    761: 
                    762: // ]]>
                    763: </script>
                    764: END
                    765:     return $js;
                    766: }
                    767: 
1.160     raeburn   768: sub entry_form {
1.318     raeburn   769:     my ($dom,$srch,$forcenewuser,$context,$responsemsg,$crstype) = @_;
1.229     raeburn   770:     my ($usertype,$inexact);
1.214     raeburn   771:     if (ref($srch) eq 'HASH') {
                    772:         if (($srch->{'srchin'} eq 'dom') &&
                    773:             ($srch->{'srchby'} eq 'uname') &&
                    774:             ($srch->{'srchtype'} eq 'exact') &&
                    775:             ($srch->{'srchdomain'} ne '') &&
                    776:             ($srch->{'srchterm'} ne '')) {
1.353     raeburn   777:             my (%curr_rules,%got_rules);
1.214     raeburn   778:             my ($rules,$ruleorder) =
                    779:                 &Apache::lonnet::inst_userrules($srch->{'srchdomain'},'username');
1.353     raeburn   780:             $usertype = &Apache::lonuserutils::check_usertype($srch->{'srchdomain'},$srch->{'srchterm'},$rules,\%curr_rules,\%got_rules);
1.229     raeburn   781:         } else {
                    782:             $inexact = 1;
1.214     raeburn   783:         }
1.207     raeburn   784:     }
1.214     raeburn   785:     my $cancreate =
                    786:         &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
1.160     raeburn   787:     my $userpicker = 
1.179     raeburn   788:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
1.214     raeburn   789:                                        'document.crtuser',$cancreate,$usertype);
1.160     raeburn   790:     my $srchbutton = &mt('Search');
1.229     raeburn   791:     if ($env{'form.action'} eq 'singlestudent') {
                    792:         $srchbutton = &mt('Search and Enroll');
                    793:     } elsif ($cancreate && $responsemsg ne '' && $inexact) {
                    794:         $srchbutton = &mt('Search or Add New User');
                    795:     }
1.207     raeburn   796:     my $output = <<"ENDBLOCK";
1.160     raeburn   797: <form action="/adm/createuser" method="post" name="crtuser">
1.190     raeburn   798: <input type="hidden" name="action" value="$env{'form.action'}" />
1.160     raeburn   799: <input type="hidden" name="phase" value="get_user_info" />
                    800: $userpicker
1.179     raeburn   801: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
1.160     raeburn   802: </form>
1.207     raeburn   803: ENDBLOCK
1.229     raeburn   804:     if ($env{'form.phase'} eq '') {
1.207     raeburn   805:         my $defdom=$env{'request.role.domain'};
                    806:         my $domform = &Apache::loncommon::select_dom_form($defdom,'srchdomain');
                    807:         my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   808:                   'enro' => 'Enroll one student',
1.318     raeburn   809:                   'enrm' => 'Enroll one member',
1.229     raeburn   810:                   'admo' => 'Add/modify a single user',
                    811:                   'crea' => 'create new user if required',
                    812:                   'uskn' => "username is known",
1.207     raeburn   813:                   'crnu' => 'Create a new user',
                    814:                   'usr'  => 'Username',
                    815:                   'dom'  => 'in domain',
1.229     raeburn   816:                   'enrl' => 'Enroll',
                    817:                   'cram'  => 'Create/Modify user',
1.207     raeburn   818:         );
1.229     raeburn   819:         my $sellink=&Apache::loncommon::selectstudent_link('crtusername','srchterm','srchdomain');
                    820:         my ($title,$buttontext,$showresponse);
1.318     raeburn   821:         if ($env{'form.action'} eq 'singlestudent') {
                    822:             if ($crstype eq 'Community') {
                    823:                 $title = $lt{'enrm'};
                    824:             } else {
                    825:                 $title = $lt{'enro'};
                    826:             }
1.229     raeburn   827:             $buttontext = $lt{'enrl'};
                    828:         } else {
                    829:             $title = $lt{'admo'};
                    830:             $buttontext = $lt{'cram'};
                    831:         }
                    832:         if ($cancreate) {
                    833:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'crea'}.')</span>';
                    834:         } else {
                    835:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'uskn'}.')</span>';
                    836:         }
                    837:         if ($env{'form.origform'} eq 'crtusername') {
                    838:             $showresponse = $responsemsg;
                    839:         }
1.207     raeburn   840:         $output .= <<"ENDDOCUMENT";
1.229     raeburn   841: <br />
1.207     raeburn   842: <form action="/adm/createuser" method="post" name="crtusername">
                    843: <input type="hidden" name="action" value="$env{'form.action'}" />
                    844: <input type="hidden" name="phase" value="createnewuser" />
                    845: <input type="hidden" name="srchtype" value="exact" />
1.233     raeburn   846: <input type="hidden" name="srchby" value="uname" />
1.207     raeburn   847: <input type="hidden" name="srchin" value="dom" />
                    848: <input type="hidden" name="forcenewuser" value="1" />
                    849: <input type="hidden" name="origform" value="crtusername" />
1.229     raeburn   850: <h3>$title</h3>
                    851: $showresponse
1.207     raeburn   852: <table>
                    853:  <tr>
                    854:   <td>$lt{'usr'}:</td>
                    855:   <td><input type="text" size="15" name="srchterm" /></td>
                    856:   <td>&nbsp;$lt{'dom'}:</td><td>$domform</td>
1.229     raeburn   857:   <td>&nbsp;$sellink&nbsp;</td>
                    858:   <td>&nbsp;<input name="userrole" type="submit" value="$buttontext" /></td>
1.207     raeburn   859:  </tr>
                    860: </table>
                    861: </form>
1.160     raeburn   862: ENDDOCUMENT
1.207     raeburn   863:     }
1.160     raeburn   864:     return $output;
                    865: }
1.110     albertel  866: 
                    867: sub user_modification_js {
1.113     raeburn   868:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
                    869:     
1.110     albertel  870:     return <<END;
                    871: <script type="text/javascript" language="Javascript">
1.301     bisitz    872: // <![CDATA[
1.314     raeburn   873: 
1.110     albertel  874:     $pjump_def
                    875:     $dc_setcourse_code
                    876: 
                    877:     function dateset() {
                    878:         eval("document.cu."+document.cu.pres_marker.value+
                    879:             ".value=document.cu.pres_value.value");
1.359     www       880:         modalWindow.close();
1.110     albertel  881:     }
                    882: 
1.113     raeburn   883:     $nondc_setsection_code
1.301     bisitz    884: // ]]>
1.110     albertel  885: </script>
                    886: END
1.2       www       887: }
                    888: 
                    889: # =================================================================== Phase two
1.160     raeburn   890: sub print_user_selection_page {
1.351     raeburn   891:     my ($r,$response,$srch,$srch_results,$srcharray,$context,$opener_elements,$crstype,$brcrum) = @_;
1.160     raeburn   892:     my @fields = ('username','domain','lastname','firstname','permanentemail');
                    893:     my $sortby = $env{'form.sortby'};
                    894: 
                    895:     if (!grep(/^\Q$sortby\E$/,@fields)) {
                    896:         $sortby = 'lastname';
                    897:     }
                    898: 
                    899:     my ($jsback,$elements) = &crumb_utilities();
                    900: 
                    901:     my $jscript = (<<ENDSCRIPT);
                    902: <script type="text/javascript">
1.301     bisitz    903: // <![CDATA[
1.160     raeburn   904: function pickuser(uname,udom) {
                    905:     document.usersrchform.seluname.value=uname;
                    906:     document.usersrchform.seludom.value=udom;
                    907:     document.usersrchform.phase.value="userpicked";
                    908:     document.usersrchform.submit();
                    909: }
                    910: 
                    911: $jsback
1.301     bisitz    912: // ]]>
1.160     raeburn   913: </script>
                    914: ENDSCRIPT
                    915: 
                    916:     my %lt=&Apache::lonlocal::texthash(
1.179     raeburn   917:                                        'usrch'          => "User Search to add/modify roles",
                    918:                                        'stusrch'        => "User Search to enroll student",
1.318     raeburn   919:                                        'memsrch'        => "User Search to enroll member",
1.179     raeburn   920:                                        'usel'           => "Select a user to add/modify roles",
1.318     raeburn   921:                                        'stusel'         => "Select a user to enroll as a student",
                    922:                                        'memsel'         => "Select a user to enroll as a member",
1.160     raeburn   923:                                        'username'       => "username",
                    924:                                        'domain'         => "domain",
                    925:                                        'lastname'       => "last name",
                    926:                                        'firstname'      => "first name",
                    927:                                        'permanentemail' => "permanent e-mail",
                    928:                                       );
1.302     raeburn   929:     if ($context eq 'requestcrs') {
                    930:         $r->print('<div>');
                    931:     } else {
1.318     raeburn   932:         my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.351     raeburn   933:         my $helpitem;
                    934:         if ($env{'form.action'} eq 'singleuser') {
                    935:             $helpitem = 'Course_Change_Privileges';
                    936:         } elsif ($env{'form.action'} eq 'singlestudent') {
                    937:             $helpitem = 'Course_Add_Student';
                    938:         }
                    939:         push (@{$brcrum},
                    940:                   {href => "javascript:backPage(document.usersrchform,'','')",
                    941:                    text => $breadcrumb_text{'search'},
                    942:                    faq  => 282,
                    943:                    bug  => 'Instructor Interface',},
                    944:                   {href => "javascript:backPage(document.usersrchform,'get_user_info','select')",
                    945:                    text => $breadcrumb_text{'userpicked'},
                    946:                    faq  => 282,
                    947:                    bug  => 'Instructor Interface',
                    948:                    help => $helpitem}
                    949:                   );
                    950:         $r->print(&Apache::loncommon::start_page('User Management',$jscript,{bread_crumbs => $brcrum}));
1.302     raeburn   951:         if ($env{'form.action'} eq 'singleuser') {
                    952:             $r->print("<b>$lt{'usrch'}</b><br />");
1.318     raeburn   953:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
1.302     raeburn   954:             $r->print('<h3>'.$lt{'usel'}.'</h3>');
                    955:         } elsif ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   956:             $r->print($jscript."<b>");
                    957:             if ($crstype eq 'Community') {
                    958:                 $r->print($lt{'memsrch'});
                    959:             } else {
                    960:                 $r->print($lt{'stusrch'});
                    961:             }
                    962:             $r->print("</b><br />");
                    963:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
                    964:             $r->print('</form><h3>');
                    965:             if ($crstype eq 'Community') {
                    966:                 $r->print($lt{'memsel'});
                    967:             } else {
                    968:                 $r->print($lt{'stusel'});
                    969:             }
                    970:             $r->print('</h3>');
1.302     raeburn   971:         }
1.179     raeburn   972:     }
1.160     raeburn   973:     $r->print('<form name="usersrchform" method="post">'.
                    974:               &Apache::loncommon::start_data_table()."\n".
                    975:               &Apache::loncommon::start_data_table_header_row()."\n".
                    976:               ' <th> </th>'."\n");
                    977:     foreach my $field (@fields) {
                    978:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
                    979:                   "'".$field."'".';document.usersrchform.submit();">'.
                    980:                   $lt{$field}.'</a></th>'."\n");
                    981:     }
                    982:     $r->print(&Apache::loncommon::end_data_table_header_row());
                    983: 
                    984:     my @sorted_users = sort {
1.167     albertel  985:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
1.160     raeburn   986:             ||
1.167     albertel  987:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
1.160     raeburn   988:             ||
                    989:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
1.167     albertel  990: 	    ||
                    991: 	lc($a) cmp lc($b)
1.160     raeburn   992:         } (keys(%$srch_results));
                    993: 
                    994:     foreach my $user (@sorted_users) {
                    995:         my ($uname,$udom) = split(/:/,$user);
1.302     raeburn   996:         my $onclick;
                    997:         if ($context eq 'requestcrs') {
1.314     raeburn   998:             $onclick =
1.302     raeburn   999:                 'onclick="javascript:gochoose('."'$uname','$udom',".
                   1000:                                                "'$srch_results->{$user}->{firstname}',".
                   1001:                                                "'$srch_results->{$user}->{lastname}',".
                   1002:                                                "'$srch_results->{$user}->{permanentemail}'".');"';
                   1003:         } else {
1.314     raeburn  1004:             $onclick =
1.302     raeburn  1005:                 ' onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".');"';
                   1006:         }
1.160     raeburn  1007:         $r->print(&Apache::loncommon::start_data_table_row().
1.302     raeburn  1008:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" '.
                   1009:                   $onclick.' /></td>'.
1.160     raeburn  1010:                   '<td><tt>'.$uname.'</tt></td>'.
                   1011:                   '<td><tt>'.$udom.'</tt></td>');
                   1012:         foreach my $field ('lastname','firstname','permanentemail') {
                   1013:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
                   1014:         }
                   1015:         $r->print(&Apache::loncommon::end_data_table_row());
                   1016:     }
                   1017:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
1.179     raeburn  1018:     if (ref($srcharray) eq 'ARRAY') {
                   1019:         foreach my $item (@{$srcharray}) {
                   1020:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
                   1021:         }
                   1022:     }
1.160     raeburn  1023:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
                   1024:               ' <input type="hidden" name="seluname" value="" />'."\n".
                   1025:               ' <input type="hidden" name="seludom" value="" />'."\n".
1.179     raeburn  1026:               ' <input type="hidden" name="currstate" value="select" />'."\n".
1.190     raeburn  1027:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
1.214     raeburn  1028:               ' <input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
1.302     raeburn  1029:     if ($context eq 'requestcrs') {
                   1030:         $r->print($opener_elements.'</form></div>');
                   1031:     } else {
1.351     raeburn  1032:         $r->print($response.'</form>');
1.302     raeburn  1033:     }
1.160     raeburn  1034: }
                   1035: 
                   1036: sub print_user_query_page {
1.351     raeburn  1037:     my ($r,$caller,$brcrum) = @_;
1.160     raeburn  1038: # FIXME - this is for a network-wide name search (similar to catalog search)
                   1039: # To use frames with similar behavior to catalog/portfolio search.
                   1040: # To be implemented. 
                   1041:     return;
                   1042: }
                   1043: 
1.42      matthew  1044: sub print_user_modification_page {
1.375     raeburn  1045:     my ($r,$ccuname,$ccdomain,$srch,$response,$context,$permission,$crstype,
                   1046:         $brcrum,$showcredits) = @_;
1.185     raeburn  1047:     if (($ccuname eq '') || ($ccdomain eq '')) {
1.215     raeburn  1048:         my $usermsg = &mt('No username and/or domain provided.');
                   1049:         $env{'form.phase'} = '';
1.351     raeburn  1050: 	&print_username_entry_form($r,$context,$usermsg,'','',$crstype,$brcrum);
1.58      www      1051:         return;
                   1052:     }
1.213     raeburn  1053:     my ($form,$formname);
                   1054:     if ($env{'form.action'} eq 'singlestudent') {
                   1055:         $form = 'document.enrollstudent';
                   1056:         $formname = 'enrollstudent';
                   1057:     } else {
                   1058:         $form = 'document.cu';
                   1059:         $formname = 'cu';
                   1060:     }
1.188     raeburn  1061:     my %abv_auth = &auth_abbrev();
1.227     raeburn  1062:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
1.185     raeburn  1063:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
                   1064:     if ($uhome eq 'no_host') {
1.215     raeburn  1065:         my $usertype;
                   1066:         my ($rules,$ruleorder) =
                   1067:             &Apache::lonnet::inst_userrules($ccdomain,'username');
                   1068:             $usertype =
1.353     raeburn  1069:                 &Apache::lonuserutils::check_usertype($ccdomain,$ccuname,$rules,
1.362     raeburn  1070:                                                       \%curr_rules,\%got_rules);
1.215     raeburn  1071:         my $cancreate =
                   1072:             &Apache::lonuserutils::can_create_user($ccdomain,$context,
                   1073:                                                    $usertype);
                   1074:         if (!$cancreate) {
1.292     bisitz   1075:             my $helplink = 'javascript:helpMenu('."'display'".')';
1.215     raeburn  1076:             my %usertypetext = (
                   1077:                 official   => 'institutional',
                   1078:                 unofficial => 'non-institutional',
                   1079:             );
                   1080:             my $response;
                   1081:             if ($env{'form.origform'} eq 'crtusername') {
1.362     raeburn  1082:                 $response = '<span class="LC_warning">'.
                   1083:                             &mt('No match found for the username [_1] in LON-CAPA domain: [_2]',
                   1084:                                 '<b>'.$ccuname.'</b>',$ccdomain).
1.215     raeburn  1085:                             '</span><br />';
                   1086:             }
1.292     bisitz   1087:             $response .= '<p class="LC_warning">'
                   1088:                         .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
                   1089:                         .' '
                   1090:                         .&mt('Please contact the [_1]helpdesk[_2] for assistance.'
                   1091:                             ,'<a href="'.$helplink.'">','</a>')
                   1092:                         .'</p><br />';
1.215     raeburn  1093:             $env{'form.phase'} = '';
1.351     raeburn  1094:             &print_username_entry_form($r,$context,$response,undef,undef,$crstype,$brcrum);
1.215     raeburn  1095:             return;
                   1096:         }
1.188     raeburn  1097:         $newuser = 1;
1.193     raeburn  1098:         my $checkhash;
                   1099:         my $checks = { 'username' => 1 };
1.196     raeburn  1100:         $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
1.193     raeburn  1101:         &Apache::loncommon::user_rule_check($checkhash,$checks,
1.196     raeburn  1102:             \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
                   1103:         if (ref($alerts{'username'}) eq 'HASH') {
                   1104:             if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
                   1105:                 my $domdesc =
1.193     raeburn  1106:                     &Apache::lonnet::domain($ccdomain,'description');
1.196     raeburn  1107:                 if ($alerts{'username'}{$ccdomain}{$ccuname}) {
                   1108:                     my $userchkmsg;
                   1109:                     if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
                   1110:                         $userchkmsg = 
                   1111:                             &Apache::loncommon::instrule_disallow_msg('username',
1.193     raeburn  1112:                                                                  $domdesc,1).
                   1113:                         &Apache::loncommon::user_rule_formats($ccdomain,
                   1114:                             $domdesc,$curr_rules{$ccdomain}{'username'},
                   1115:                             'username');
1.196     raeburn  1116:                     }
1.215     raeburn  1117:                     $env{'form.phase'} = '';
1.351     raeburn  1118:                     &print_username_entry_form($r,$context,$userchkmsg,undef,undef,$crstype,$brcrum);
1.196     raeburn  1119:                     return;
1.215     raeburn  1120:                 }
1.193     raeburn  1121:             }
1.185     raeburn  1122:         }
1.187     raeburn  1123:     } else {
1.188     raeburn  1124:         $newuser = 0;
1.185     raeburn  1125:     }
1.160     raeburn  1126:     if ($response) {
1.215     raeburn  1127:         $response = '<br />'.$response;
1.160     raeburn  1128:     }
1.149     raeburn  1129: 
1.52      matthew  1130:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.88      raeburn  1131:     my $dc_setcourse_code = '';
1.119     raeburn  1132:     my $nondc_setsection_code = '';                                        
1.112     albertel 1133:     my %loaditem;
1.114     albertel 1134: 
1.216     raeburn  1135:     my $groupslist = &Apache::lonuserutils::get_groupslist();
1.88      raeburn  1136: 
1.375     raeburn  1137:     my $js = &validation_javascript($context,$ccdomain,$pjump_def,$crstype,
1.216     raeburn  1138:                                $groupslist,$newuser,$formname,\%loaditem);
1.318     raeburn  1139:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.224     raeburn  1140:     my $helpitem = 'Course_Change_Privileges';
                   1141:     if ($env{'form.action'} eq 'singlestudent') {
                   1142:         $helpitem = 'Course_Add_Student';
                   1143:     }
1.351     raeburn  1144:     push (@{$brcrum},
                   1145:         {href => "javascript:backPage($form)",
                   1146:          text => $breadcrumb_text{'search'},
                   1147:          faq  => 282,
                   1148:          bug  => 'Instructor Interface',});
                   1149:     if ($env{'form.phase'} eq 'userpicked') {
                   1150:        push(@{$brcrum},
                   1151:               {href => "javascript:backPage($form,'get_user_info','select')",
                   1152:                text => $breadcrumb_text{'userpicked'},
                   1153:                faq  => 282,
                   1154:                bug  => 'Instructor Interface',});
                   1155:     }
                   1156:     push(@{$brcrum},
                   1157:             {href => "javascript:backPage($form,'$env{'form.phase'}','modify')",
                   1158:              text => $breadcrumb_text{'modify'},
                   1159:              faq  => 282,
                   1160:              bug  => 'Instructor Interface',
                   1161:              help => $helpitem});
                   1162:     my $args = {'add_entries'           => \%loaditem,
                   1163:                 'bread_crumbs'          => $brcrum,
                   1164:                 'bread_crumbs_component' => 'User Management'};
                   1165:     if ($env{'form.popup'}) {
                   1166:         $args->{'no_nav_bar'} = 1;
                   1167:     }
                   1168:     my $start_page =
                   1169:         &Apache::loncommon::start_page('User Management',$js,$args);
1.3       www      1170: 
1.25      matthew  1171:     my $forminfo =<<"ENDFORMINFO";
1.216     raeburn  1172: <form action="/adm/createuser" method="post" name="$formname">
1.190     raeburn  1173: <input type="hidden" name="phase" value="update_user_data" />
1.188     raeburn  1174: <input type="hidden" name="ccuname" value="$ccuname" />
                   1175: <input type="hidden" name="ccdomain" value="$ccdomain" />
1.157     albertel 1176: <input type="hidden" name="pres_value"  value="" />
                   1177: <input type="hidden" name="pres_type"   value="" />
                   1178: <input type="hidden" name="pres_marker" value="" />
1.25      matthew  1179: ENDFORMINFO
1.375     raeburn  1180:     my (%inccourses,$roledom,$defaultcredits);
1.329     raeburn  1181:     if ($context eq 'course') {
                   1182:         $inccourses{$env{'request.course.id'}}=1;
                   1183:         $roledom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.375     raeburn  1184:         if ($showcredits) {
                   1185:             $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1186:         }
1.329     raeburn  1187:     } elsif ($context eq 'author') {
                   1188:         $roledom = $env{'request.role.domain'};
                   1189:     } elsif ($context eq 'domain') {
                   1190:         foreach my $key (keys(%env)) {
                   1191:             $roledom = $env{'request.role.domain'};
                   1192:             if ($key=~/^user\.priv\.cm\.\/($roledom)\/($match_username)/) {
                   1193:                 $inccourses{$1.'_'.$2}=1;
                   1194:             }
                   1195:         }
                   1196:     } else {
                   1197:         foreach my $key (keys(%env)) {
                   1198: 	    if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
                   1199: 	        $inccourses{$1.'_'.$2}=1;
                   1200:             }
1.2       www      1201:         }
1.24      matthew  1202:     }
1.216     raeburn  1203:     if ($newuser) {
1.362     raeburn  1204:         my ($portfolioform,$domroleform);
1.267     raeburn  1205:         if ((&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) ||
                   1206:             (&Apache::lonnet::allowed('mut',$env{'request.role.domain'}))) {
                   1207:             # Current user has quota or user tools modification privileges
1.188     raeburn  1208:             $portfolioform = '<br />'.&portfolio_quota($ccuname,$ccdomain);
1.134     raeburn  1209:         }
1.362     raeburn  1210:         if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
                   1211:             $domroleform = '<br />'.&domainrole_req($ccuname,$ccdomain);
                   1212:         }
1.227     raeburn  1213:         &initialize_authen_forms($ccdomain,$formname);
1.188     raeburn  1214:         my %lt=&Apache::lonlocal::texthash(
                   1215:                 'cnu'            => 'Create New User',
1.213     raeburn  1216:                 'ast'            => 'as a student',
1.318     raeburn  1217:                 'ame'            => 'as a member',
1.188     raeburn  1218:                 'ind'            => 'in domain',
                   1219:                 'lg'             => 'Login Data',
1.190     raeburn  1220:                 'hs'             => "Home Server",
1.188     raeburn  1221:         );
1.185     raeburn  1222: 	$r->print(<<ENDTITLE);
1.110     albertel 1223: $start_page
1.160     raeburn  1224: $response
1.25      matthew  1225: $forminfo
1.31      matthew  1226: <script type="text/javascript" language="Javascript">
1.301     bisitz   1227: // <![CDATA[
1.20      harris41 1228: $loginscript
1.301     bisitz   1229: // ]]>
1.31      matthew  1230: </script>
1.20      harris41 1231: <input type='hidden' name='makeuser' value='1' />
1.216     raeburn  1232: <h2>$lt{'cnu'} "$ccuname" $lt{'ind'} $ccdomain
1.185     raeburn  1233: ENDTITLE
1.213     raeburn  1234:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1235:             if ($crstype eq 'Community') {
                   1236:                 $r->print(' ('.$lt{'ame'}.')');
                   1237:             } else {
                   1238:                 $r->print(' ('.$lt{'ast'}.')');
                   1239:             }
1.213     raeburn  1240:         }
                   1241:         $r->print('</h2>'."\n".'<div class="LC_left_float">');
1.206     raeburn  1242:         my $personal_table = 
1.210     raeburn  1243:             &personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1244:                                    $inst_results{$ccuname.':'.$ccdomain});
1.206     raeburn  1245:         $r->print($personal_table);
1.187     raeburn  1246:         my ($home_server_pick,$numlib) = 
                   1247:             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
                   1248:                                                       'default','hide');
                   1249:         if ($numlib > 1) {
                   1250:             $r->print("
1.185     raeburn  1251: <br />
1.187     raeburn  1252: $lt{'hs'}: $home_server_pick
                   1253: <br />");
                   1254:         } else {
                   1255:             $r->print($home_server_pick);
                   1256:         }
1.304     raeburn  1257:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.362     raeburn  1258:             $r->print('<br /><h3>'.
                   1259:                       &mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.304     raeburn  1260:                       &Apache::loncommon::start_data_table().
                   1261:                       &build_tools_display($ccuname,$ccdomain,
                   1262:                                            'requestcourses').
                   1263:                       &Apache::loncommon::end_data_table());
                   1264:         }
1.188     raeburn  1265:         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                   1266:                   $lt{'lg'}.'</h3>');
1.185     raeburn  1267:         my ($fixedauth,$varauth,$authmsg); 
1.193     raeburn  1268:         if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
                   1269:             my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
                   1270:             my ($rules,$ruleorder) = 
                   1271:                 &Apache::lonnet::inst_userrules($ccdomain,'username');
1.185     raeburn  1272:             if (ref($rules) eq 'HASH') {
1.193     raeburn  1273:                 if (ref($rules->{$matchedrule}) eq 'HASH') {
                   1274:                     my $authtype = $rules->{$matchedrule}{'authtype'};
1.185     raeburn  1275:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
1.190     raeburn  1276:                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.275     raeburn  1277:                     } else { 
1.193     raeburn  1278:                         my $authparm = $rules->{$matchedrule}{'authparm'};
1.273     raeburn  1279:                         $authmsg = $rules->{$matchedrule}{'authmsg'};
1.185     raeburn  1280:                         if ($authtype =~ /^krb(4|5)$/) {
                   1281:                             my $ver = $1;
                   1282:                             if ($authparm ne '') {
                   1283:                                 $fixedauth = <<"KERB"; 
                   1284: <input type="hidden" name="login" value="krb" />
                   1285: <input type="hidden" name="krbver" value="$ver" />
                   1286: <input type="hidden" name="krbarg" value="$authparm" />
                   1287: KERB
                   1288:                             }
                   1289:                         } else {
                   1290:                             $fixedauth = 
                   1291: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
1.193     raeburn  1292:                             if ($rules->{$matchedrule}{'authparmfixed'}) {
1.185     raeburn  1293:                                 $fixedauth .=    
                   1294: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
                   1295:                             } else {
1.273     raeburn  1296:                                 if ($authtype eq 'int') {
                   1297:                                     $varauth = '<br />'.
1.301     bisitz   1298: &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>';
1.273     raeburn  1299:                                 } elsif ($authtype eq 'loc') {
                   1300:                                     $varauth = '<br />'.
                   1301: &mt('[_1] Local Authentication with argument [_2]','','<input type="text" name="'.$authtype.'arg" value="" />')."\n";
                   1302:                                 } else {
                   1303:                                     $varauth =
1.185     raeburn  1304: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
1.273     raeburn  1305:                                 }
1.185     raeburn  1306:                             }
                   1307:                         }
                   1308:                     }
                   1309:                 } else {
1.190     raeburn  1310:                     $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.185     raeburn  1311:                 }
                   1312:             }
                   1313:             if ($authmsg) {
                   1314:                 $r->print(<<ENDAUTH);
                   1315: $fixedauth
                   1316: $authmsg
                   1317: $varauth
                   1318: ENDAUTH
                   1319:             }
                   1320:         } else {
1.190     raeburn  1321:             $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc)); 
1.187     raeburn  1322:         }
1.362     raeburn  1323:         $r->print($portfolioform.$domroleform);
1.215     raeburn  1324:         if ($env{'form.action'} eq 'singlestudent') {
                   1325:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1326:                                             $permission,$crstype,$ccuname,
                   1327:                                             $ccdomain,$showcredits));
1.215     raeburn  1328:         }
                   1329:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.216     raeburn  1330:     } else { # user already exists
1.79      albertel 1331: 	my %lt=&Apache::lonlocal::texthash(
1.191     raeburn  1332:                     'cup'  => "Modify existing user: ",
1.213     raeburn  1333:                     'ens'  => "Enroll one student: ",
1.318     raeburn  1334:                     'enm'  => "Enroll one member: ",
1.72      sakharuk 1335:                     'id'   => "in domain",
                   1336: 				       );
1.26      matthew  1337: 	$r->print(<<ENDCHANGEUSER);
1.110     albertel 1338: $start_page
1.25      matthew  1339: $forminfo
1.213     raeburn  1340: <h2>
1.26      matthew  1341: ENDCHANGEUSER
1.213     raeburn  1342:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1343:             if ($crstype eq 'Community') {
                   1344:                 $r->print($lt{'enm'});
                   1345:             } else {
                   1346:                 $r->print($lt{'ens'});
                   1347:             }
1.213     raeburn  1348:         } else {
                   1349:             $r->print($lt{'cup'});
                   1350:         }
                   1351:         $r->print(' "'.$ccuname.'" '.$lt{'id'}.' "'.$ccdomain.'"</h2>'.
                   1352:                   "\n".'<div class="LC_left_float">');
1.206     raeburn  1353:         my ($personal_table,$showforceid) = 
1.210     raeburn  1354:             &personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1355:                                    $inst_results{$ccuname.':'.$ccdomain});
1.206     raeburn  1356:         $r->print($personal_table);
                   1357:         if ($showforceid) {
1.362     raeburn  1358:             $r->print('<table>'.&Apache::lonuserutils::forceid_change($context).'</table>');
1.199     raeburn  1359:         }
1.275     raeburn  1360:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.362     raeburn  1361:             $r->print('<br /><h3>'.&mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.300     raeburn  1362:                       &Apache::loncommon::start_data_table());
1.314     raeburn  1363:             if ($env{'request.role.domain'} eq $ccdomain) {
1.300     raeburn  1364:                 $r->print(&build_tools_display($ccuname,$ccdomain,'requestcourses'));
                   1365:             } else {
                   1366:                 $r->print(&coursereq_externaluser($ccuname,$ccdomain,
                   1367:                                                   $env{'request.role.domain'}));
                   1368:             }
                   1369:             $r->print(&Apache::loncommon::end_data_table());
1.275     raeburn  1370:         }
1.199     raeburn  1371:         $r->print('</div>');
1.362     raeburn  1372:         my @order = ('auth','quota','tools','requestauthor');
                   1373:         my %user_text;
                   1374:         my ($isadv,$isauthor) = 
                   1375:             &Apache::lonnet::is_advanced_user($ccuname,$ccdomain);
                   1376:         if ((!$isauthor) && 
                   1377:             (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   1378:             $user_text{'requestauthor'} = &domainrole_req($ccuname,$ccdomain);
                   1379:         }
                   1380:         $user_text{'auth'} =  &user_authentication($ccuname,$ccdomain,$formname);
1.267     raeburn  1381:         if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
                   1382:             (&Apache::lonnet::allowed('mut',$ccdomain))) {
1.188     raeburn  1383:             # Current user has quota modification privileges
1.362     raeburn  1384:             $user_text{'quota'} = &portfolio_quota($ccuname,$ccdomain);
1.267     raeburn  1385:         }
                   1386:         if (!&Apache::lonnet::allowed('mpq',$ccdomain)) {
                   1387:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
                   1388:                 # Get the user's portfolio information
                   1389:                 my %portq = &Apache::lonnet::get('environment',['portfolioquota'],
                   1390:                                                  $ccdomain,$ccuname);
                   1391:                 my %lt=&Apache::lonlocal::texthash(
                   1392:                     'dska'  => "Disk space allocated to user's portfolio files",
                   1393:                     'youd'  => "You do not have privileges to modify the portfolio quota for this user.",
                   1394:                     'ichr'  => "If a change is required, contact a domain coordinator for the domain",
                   1395:                 );
1.362     raeburn  1396:                 $user_text{'quota'} = <<ENDNOPORTPRIV;
1.188     raeburn  1397: <h3>$lt{'dska'}</h3>
                   1398: $lt{'youd'} $lt{'ichr'}: $ccdomain
                   1399: ENDNOPORTPRIV
1.267     raeburn  1400:             }
                   1401:         }
                   1402:         if (!&Apache::lonnet::allowed('mut',$ccdomain)) {
                   1403:             if (&Apache::lonnet::allowed('mut',$env{'request.role.domain'})) {
                   1404:                 my %lt=&Apache::lonlocal::texthash(
                   1405:                     'utav'  => "User Tools Availability",
1.361     raeburn  1406:                     'yodo'  => "You do not have privileges to modify Portfolio, Blog, WebDAV, or Personal Information Page settings for this user.",
1.267     raeburn  1407:                     'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1408:                 );
1.362     raeburn  1409:                 $user_text{'tools'} = <<ENDNOTOOLSPRIV;
1.267     raeburn  1410: <h3>$lt{'utav'}</h3>
                   1411: $lt{'yodo'} $lt{'ifch'}: $ccdomain
                   1412: ENDNOTOOLSPRIV
                   1413:             }
1.188     raeburn  1414:         }
1.362     raeburn  1415:         my $gotdiv = 0; 
                   1416:         foreach my $item (@order) {
                   1417:             if ($user_text{$item} ne '') {
                   1418:                 unless ($gotdiv) {
                   1419:                     $r->print('<div class="LC_left_float">');
                   1420:                     $gotdiv = 1;
                   1421:                 }
                   1422:                 $r->print('<br />'.$user_text{$item});
                   1423:             }
                   1424:         }
                   1425:         if ($env{'form.action'} eq 'singlestudent') {
                   1426:             unless ($gotdiv) {
                   1427:                 $r->print('<div class="LC_left_float">');
1.213     raeburn  1428:             }
1.375     raeburn  1429:             my $credits;
                   1430:             if ($showcredits) {
                   1431:                 $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1432:                 if ($credits eq '') {
                   1433:                     $credits = $defaultcredits;
                   1434:                 }
                   1435:             }
1.374     raeburn  1436:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1437:                                             $permission,$crstype,$ccuname,
                   1438:                                             $ccdomain,$showcredits));
1.374     raeburn  1439:         }
1.362     raeburn  1440:         if ($gotdiv) {
                   1441:             $r->print('</div><div class="LC_clear_float_footer"></div>');
1.188     raeburn  1442:         }
1.217     raeburn  1443:         if ($env{'form.action'} ne 'singlestudent') {
1.329     raeburn  1444:             &display_existing_roles($r,$ccuname,$ccdomain,\%inccourses,$context,
                   1445:                                     $roledom,$crstype);
1.217     raeburn  1446:         }
1.25      matthew  1447:     } ## End of new user/old user logic
1.218     raeburn  1448:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1449:         my $btntxt;
                   1450:         if ($crstype eq 'Community') {
                   1451:             $btntxt = &mt('Enroll Member');
                   1452:         } else {
                   1453:             $btntxt = &mt('Enroll Student');
                   1454:         }
                   1455:         $r->print('<br /><input type="button" value="'.$btntxt.'" onclick="setSections(this.form)" />'."\n");
1.218     raeburn  1456:     } else {
1.375     raeburn  1457:         $r->print('<fieldset><legend>'.&mt('Add Roles').'</legend>');
1.218     raeburn  1458:         my $addrolesdisplay = 0;
                   1459:         if ($context eq 'domain' || $context eq 'author') {
                   1460:             $addrolesdisplay = &new_coauthor_roles($r,$ccuname,$ccdomain);
                   1461:         }
                   1462:         if ($context eq 'domain') {
1.357     raeburn  1463:             my $add_domainroles = &new_domain_roles($r,$ccdomain);
1.218     raeburn  1464:             if (!$addrolesdisplay) {
                   1465:                 $addrolesdisplay = $add_domainroles;
1.2       www      1466:             }
1.375     raeburn  1467:             $r->print(&course_level_dc($env{'request.role.domain'},$showcredits));
                   1468:             $r->print('</fieldset><br /><input type="button" value="'.&mt('Save').'" onclick="setCourse()" />'."\n");
1.218     raeburn  1469:         } elsif ($context eq 'author') {
                   1470:             if ($addrolesdisplay) {
1.375     raeburn  1471:                 $r->print('</fieldset><br /><input type="button" value="'.&mt('Save').'"');
1.218     raeburn  1472:                 if ($newuser) {
1.301     bisitz   1473:                     $r->print(' onclick="auth_check()" \>'."\n");
1.218     raeburn  1474:                 } else {
1.301     bisitz   1475:                     $r->print('onclick="this.form.submit()" \>'."\n");
1.218     raeburn  1476:                 }
1.188     raeburn  1477:             } else {
1.375     raeburn  1478:                 $r->print('</fieldset><br /><a href="javascript:backPage(document.cu)">'.
1.218     raeburn  1479:                           &mt('Back to previous page').'</a>');
1.188     raeburn  1480:             }
                   1481:         } else {
1.375     raeburn  1482:             $r->print(&course_level_table(\%inccourses,$showcredits,$defaultcredits));
                   1483:             $r->print('</fieldset><br /><input type="button" value="'.&mt('Save').'" onclick="setSections(this.form)" />'."\n");
1.188     raeburn  1484:         }
1.88      raeburn  1485:     }
1.188     raeburn  1486:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
1.179     raeburn  1487:     $r->print('<input type="hidden" name="currstate" value="" />');
1.352     raeburn  1488:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" /></form>');
1.218     raeburn  1489:     return;
1.2       www      1490: }
1.1       www      1491: 
1.213     raeburn  1492: sub singleuser_breadcrumb {
1.318     raeburn  1493:     my ($crstype) = @_;
1.213     raeburn  1494:     my %breadcrumb_text;
                   1495:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1496:         if ($crstype eq 'Community') {
                   1497:             $breadcrumb_text{'search'} = 'Enroll a member';
                   1498:         } else {
                   1499:             $breadcrumb_text{'search'} = 'Enroll a student';
                   1500:         }
1.213     raeburn  1501:         $breadcrumb_text{'userpicked'} = 'Select a user',
                   1502:         $breadcrumb_text{'modify'} = 'Set section/dates',
                   1503:     } else {
1.229     raeburn  1504:         $breadcrumb_text{'search'} = 'Create/modify a user';
1.213     raeburn  1505:         $breadcrumb_text{'userpicked'} = 'Select a user',
                   1506:         $breadcrumb_text{'modify'} = 'Set user role',
                   1507:     }
                   1508:     return %breadcrumb_text;
                   1509: }
                   1510: 
                   1511: sub date_sections_select {
1.375     raeburn  1512:     my ($context,$newuser,$formname,$permission,$crstype,$ccuname,$ccdomain,
                   1513:         $showcredits) = @_;
                   1514:     my $credits;
                   1515:     if ($showcredits) {
                   1516:         my $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1517:         $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1518:         if ($credits eq '') {
                   1519:             $credits = $defaultcredits;
                   1520:         }
                   1521:     }
1.213     raeburn  1522:     my $cid = $env{'request.course.id'};
                   1523:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity($cid);
                   1524:     my $date_table = '<h3>'.&mt('Starting and Ending Dates').'</h3>'."\n".
                   1525:         &Apache::lonuserutils::date_setting_table(undef,undef,$context,
                   1526:                                                   undef,$formname,$permission);
                   1527:     my $rowtitle = 'Section';
1.375     raeburn  1528:     my $secbox = '<h3>'.&mt('Section and Credits').'</h3>'."\n".
1.213     raeburn  1529:         &Apache::lonuserutils::section_picker($cdom,$cnum,'st',$rowtitle,
1.375     raeburn  1530:                                               $permission,$context,'',$crstype,
                   1531:                                               $showcredits,$credits);
1.213     raeburn  1532:     my $output = $date_table.$secbox;
                   1533:     return $output;
                   1534: }
                   1535: 
1.216     raeburn  1536: sub validation_javascript {
1.375     raeburn  1537:     my ($context,$ccdomain,$pjump_def,$crstype,$groupslist,$newuser,$formname,
1.216     raeburn  1538:         $loaditem) = @_;
                   1539:     my $dc_setcourse_code = '';
                   1540:     my $nondc_setsection_code = '';
                   1541:     if ($context eq 'domain') {
                   1542:         my $dcdom = $env{'request.role.domain'};
                   1543:         $loaditem->{'onload'} = "document.cu.coursedesc.value='';";
1.227     raeburn  1544:         $dc_setcourse_code = 
                   1545:             &Apache::lonuserutils::dc_setcourse_js('cu','singleuser',$context);
1.216     raeburn  1546:     } else {
1.227     raeburn  1547:         my $checkauth; 
                   1548:         if (($newuser) || (&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1549:             $checkauth = 1;
                   1550:         }
                   1551:         if ($context eq 'course') {
                   1552:             $nondc_setsection_code =
                   1553:                 &Apache::lonuserutils::setsections_javascript($formname,$groupslist,
1.375     raeburn  1554:                                                               undef,$checkauth,
                   1555:                                                               $crstype);
1.227     raeburn  1556:         }
                   1557:         if ($checkauth) {
                   1558:             $nondc_setsection_code .= 
                   1559:                 &Apache::lonuserutils::verify_authen($formname,$context);
                   1560:         }
1.216     raeburn  1561:     }
                   1562:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                   1563:                                    $nondc_setsection_code,$groupslist);
                   1564:     my ($jsback,$elements) = &crumb_utilities();
                   1565:     $js .= "\n".
1.301     bisitz   1566:            '<script type="text/javascript">'."\n".
                   1567:            '// <![CDATA['."\n".
                   1568:            $jsback."\n".
                   1569:            '// ]]>'."\n".
                   1570:            '</script>'."\n";
1.216     raeburn  1571:     return $js;
                   1572: }
                   1573: 
1.217     raeburn  1574: sub display_existing_roles {
1.375     raeburn  1575:     my ($r,$ccuname,$ccdomain,$inccourses,$context,$roledom,$crstype,
                   1576:         $showcredits) = @_;
1.329     raeburn  1577:     my $now=time;
                   1578:     my %lt=&Apache::lonlocal::texthash(
1.217     raeburn  1579:                     'rer'  => "Existing Roles",
                   1580:                     'rev'  => "Revoke",
                   1581:                     'del'  => "Delete",
                   1582:                     'ren'  => "Re-Enable",
                   1583:                     'rol'  => "Role",
                   1584:                     'ext'  => "Extent",
1.375     raeburn  1585:                     'crd'  => "Credits",
1.217     raeburn  1586:                     'sta'  => "Start",
                   1587:                     'end'  => "End",
                   1588:                                        );
1.329     raeburn  1589:     my (%rolesdump,%roletext,%sortrole,%roleclass,%rolepriv);
                   1590:     if ($context eq 'course' || $context eq 'author') {
                   1591:         my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
                   1592:         my %roleshash = 
                   1593:             &Apache::lonnet::get_my_roles($ccuname,$ccdomain,'userroles',
                   1594:                               ['active','previous','future'],\@roles,$roledom,1);
                   1595:         foreach my $key (keys(%roleshash)) {
                   1596:             my ($start,$end) = split(':',$roleshash{$key});
                   1597:             next if ($start eq '-1' || $end eq '-1');
                   1598:             my ($rnum,$rdom,$role,$sec) = split(':',$key);
                   1599:             if ($context eq 'course') {
                   1600:                 next unless (($rnum eq $env{'course.'.$env{'request.course.id'}.'.num'})
                   1601:                              && ($rdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}));
                   1602:             } elsif ($context eq 'author') {
                   1603:                 next unless (($rnum eq $env{'user.name'}) && ($rdom eq $env{'request.role.domain'}));
                   1604:             }
                   1605:             my ($newkey,$newvalue,$newrole);
                   1606:             $newkey = '/'.$rdom.'/'.$rnum;
                   1607:             if ($sec ne '') {
                   1608:                 $newkey .= '/'.$sec;
                   1609:             }
                   1610:             $newvalue = $role;
                   1611:             if ($role =~ /^cr/) {
                   1612:                 $newrole = 'cr';
                   1613:             } else {
                   1614:                 $newrole = $role;
                   1615:             }
                   1616:             $newkey .= '_'.$newrole;
                   1617:             if ($start ne '' && $end ne '') {
                   1618:                 $newvalue .= '_'.$end.'_'.$start;
1.335     raeburn  1619:             } elsif ($end ne '') {
                   1620:                 $newvalue .= '_'.$end;
1.329     raeburn  1621:             }
                   1622:             $rolesdump{$newkey} = $newvalue;
                   1623:         }
                   1624:     } else {
1.360     raeburn  1625:         %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
1.329     raeburn  1626:     }
                   1627:     # Build up table of user roles to allow revocation and re-enabling of roles.
                   1628:     my ($tmp) = keys(%rolesdump);
                   1629:     return if ($tmp =~ /^(con_lost|error)/i);
                   1630:     foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
                   1631:                                 my $b1=join('_',(split('_',$b))[1,0]);
                   1632:                                 return $a1 cmp $b1;
                   1633:                             } keys(%rolesdump)) {
                   1634:         next if ($area =~ /^rolesdef/);
                   1635:         my $envkey=$area;
                   1636:         my $role = $rolesdump{$area};
                   1637:         my $thisrole=$area;
                   1638:         $area =~ s/\_\w\w$//;
                   1639:         my ($role_code,$role_end_time,$role_start_time) =
                   1640:             split(/_/,$role);
1.217     raeburn  1641: # Is this a custom role? Get role owner and title.
1.329     raeburn  1642:         my ($croleudom,$croleuname,$croletitle)=
                   1643:             ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
                   1644:         my $allowed=0;
                   1645:         my $delallowed=0;
                   1646:         my $sortkey=$role_code;
                   1647:         my $class='Unknown';
1.375     raeburn  1648:         my $credits='';
1.329     raeburn  1649:         if ($area =~ m{^/($match_domain)/($match_courseid)} ) {
                   1650:             $class='Course';
                   1651:             my ($coursedom,$coursedir) = ($1,$2);
                   1652:             my $cid = $1.'_'.$2;
                   1653:             # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
                   1654:             my %coursedata=
                   1655:                 &Apache::lonnet::coursedescription($cid);
                   1656:             if ($coursedir =~ /^$match_community$/) {
                   1657:                 $class='Community';
                   1658:             }
                   1659:             $sortkey.="\0$coursedom";
                   1660:             my $carea;
                   1661:             if (defined($coursedata{'description'})) {
                   1662:                 $carea=$coursedata{'description'}.
                   1663:                     '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
                   1664:     &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$coursedir,$coursedom);
                   1665:                 $sortkey.="\0".$coursedata{'description'};
                   1666:             } else {
                   1667:                 if ($class eq 'Community') {
                   1668:                     $carea=&mt('Unavailable community').': '.$area;
                   1669:                     $sortkey.="\0".&mt('Unavailable community').': '.$area;
1.217     raeburn  1670:                 } else {
                   1671:                     $carea=&mt('Unavailable course').': '.$area;
                   1672:                     $sortkey.="\0".&mt('Unavailable course').': '.$area;
                   1673:                 }
1.329     raeburn  1674:             }
                   1675:             $sortkey.="\0$coursedir";
                   1676:             $inccourses->{$cid}=1;
1.375     raeburn  1677:             if (($showcredits) && ($class eq 'Course') && ($role_code eq 'st')) {
                   1678:                 my $defaultcredits = $coursedata{'internal.defaultcredits'};
                   1679:                 $credits =
                   1680:                     &get_user_credits($ccuname,$ccdomain,$defaultcredits,
                   1681:                                       $coursedom,$coursedir);
                   1682:                 if ($credits eq '') {
                   1683:                     $credits = $defaultcredits;
                   1684:                 }
                   1685:             }
1.329     raeburn  1686:             if ((&Apache::lonnet::allowed('c'.$role_code,$coursedom.'/'.$coursedir)) ||
                   1687:                 (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1688:                 $allowed=1;
                   1689:             }
                   1690:             unless ($allowed) {
1.365     raeburn  1691:                 my $isowner = &Apache::lonuserutils::is_courseowner($cid,$coursedata{'internal.courseowner'});
1.329     raeburn  1692:                 if ($isowner) {
                   1693:                     if (($role_code eq 'co') && ($class eq 'Community')) {
                   1694:                         $allowed = 1;
                   1695:                     } elsif (($role_code eq 'cc') && ($class eq 'Course')) {
                   1696:                         $allowed = 1;
                   1697:                     }
1.217     raeburn  1698:                 }
1.329     raeburn  1699:             } 
                   1700:             if ((&Apache::lonnet::allowed('dro',$coursedom)) ||
                   1701:                 (&Apache::lonnet::allowed('dro',$ccdomain))) {
                   1702:                 $delallowed=1;
                   1703:             }
1.217     raeburn  1704: # - custom role. Needs more info, too
1.329     raeburn  1705:             if ($croletitle) {
                   1706:                 if (&Apache::lonnet::allowed('ccr',$coursedom.'/'.$coursedir)) {
                   1707:                     $allowed=1;
                   1708:                     $thisrole.='.'.$role_code;
1.217     raeburn  1709:                 }
1.329     raeburn  1710:             }
                   1711:             if ($area=~m{^/($match_domain)/($match_courseid)/(\w+)}) {
1.373     bisitz   1712:                 $carea.='<br />'.&mt('Section: [_1]',$3);
1.329     raeburn  1713:                 $sortkey.="\0$3";
                   1714:                 if (!$allowed) {
                   1715:                     if ($env{'request.course.sec'} eq $3) {
                   1716:                         if (&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2.'/'.$3)) {
                   1717:                             $allowed = 1;
1.217     raeburn  1718:                         }
                   1719:                     }
                   1720:                 }
1.329     raeburn  1721:             }
                   1722:             $area=$carea;
                   1723:         } else {
                   1724:             $sortkey.="\0".$area;
                   1725:             # Determine if current user is able to revoke privileges
                   1726:             if ($area=~m{^/($match_domain)/}) {
                   1727:                 if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
                   1728:                    (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1729:                    $allowed=1;
1.217     raeburn  1730:                 }
1.329     raeburn  1731:                 if (((&Apache::lonnet::allowed('dro',$1))  ||
                   1732:                     (&Apache::lonnet::allowed('dro',$ccdomain))) &&
                   1733:                     ($role_code ne 'dc')) {
                   1734:                     $delallowed=1;
1.217     raeburn  1735:                 }
1.329     raeburn  1736:             } else {
                   1737:                 if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
1.217     raeburn  1738:                     $allowed=1;
                   1739:                 }
                   1740:             }
1.363     raeburn  1741:             if ($role_code eq 'ca' || $role_code eq 'au' || $role_code eq 'aa') {
1.329     raeburn  1742:                 $class='Construction Space';
                   1743:             } elsif ($role_code eq 'su') {
                   1744:                 $class='System';
1.217     raeburn  1745:             } else {
1.329     raeburn  1746:                 $class='Domain';
1.217     raeburn  1747:             }
1.329     raeburn  1748:         }
                   1749:         if (($role_code eq 'ca') || ($role_code eq 'aa')) {
                   1750:             $area=~m{/($match_domain)/($match_username)};
                   1751:             if (&Apache::lonuserutils::authorpriv($2,$1)) {
                   1752:                 $allowed=1;
1.217     raeburn  1753:             } else {
1.329     raeburn  1754:                 $allowed=0;
1.217     raeburn  1755:             }
1.329     raeburn  1756:         }
                   1757:         my $row = '';
                   1758:         $row.= '<td>';
                   1759:         my $active=1;
                   1760:         $active=0 if (($role_end_time) && ($now>$role_end_time));
                   1761:         if (($active) && ($allowed)) {
                   1762:             $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
                   1763:         } else {
                   1764:             if ($active) {
                   1765:                $row.='&nbsp;';
1.217     raeburn  1766:             } else {
1.329     raeburn  1767:                $row.=&mt('expired or revoked');
1.217     raeburn  1768:             }
1.329     raeburn  1769:         }
                   1770:         $row.='</td><td>';
                   1771:         if ($allowed && !$active) {
                   1772:             $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
                   1773:         } else {
                   1774:             $row.='&nbsp;';
                   1775:         }
                   1776:         $row.='</td><td>';
                   1777:         if ($delallowed) {
                   1778:             $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
                   1779:         } else {
                   1780:             $row.='&nbsp;';
                   1781:         }
                   1782:         my $plaintext='';
                   1783:         if (!$croletitle) {
1.375     raeburn  1784:             $plaintext=&Apache::lonnet::plaintext($role_code,$class);
                   1785:             if (($showcredits) && ($credits ne '')) {
                   1786:                 $plaintext .= '<br/ ><span class="LC_nobreak">'.
                   1787:                               '<span class="LC_fontsize_small">'.
                   1788:                               &mt('Credits: [_1]',$credits).
                   1789:                               '</span></span>';
                   1790:             }
1.329     raeburn  1791:         } else {
                   1792:             $plaintext=
1.346     bisitz   1793:                 &mt('Customrole [_1][_2]defined by [_3]',
                   1794:                         '"'.$croletitle.'"',
                   1795:                         '<br />',
                   1796:                         $croleuname.':'.$croleudom);
1.329     raeburn  1797:         }
                   1798:         $row.= '</td><td>'.$plaintext.
                   1799:                '</td><td>'.$area.
                   1800:                '</td><td>'.($role_start_time?&Apache::lonlocal::locallocaltime($role_start_time)
                   1801:                                             : '&nbsp;' ).
                   1802:                '</td><td>'.($role_end_time  ?&Apache::lonlocal::locallocaltime($role_end_time)
                   1803:                                             : '&nbsp;' )
                   1804:                ."</td>";
                   1805:         $sortrole{$sortkey}=$envkey;
                   1806:         $roletext{$envkey}=$row;
                   1807:         $roleclass{$envkey}=$class;
                   1808:         $rolepriv{$envkey}=$allowed;
                   1809:     } # end of foreach        (table building loop)
                   1810: 
                   1811:     my $rolesdisplay = 0;
                   1812:     my %output = ();
                   1813:     foreach my $type ('Construction Space','Course','Community','Domain','System','Unknown') {
                   1814:         $output{$type} = '';
                   1815:         foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                   1816:             if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) {
                   1817:                  $output{$type}.=
                   1818:                       &Apache::loncommon::start_data_table_row().
                   1819:                       $roletext{$sortrole{$which}}.
                   1820:                       &Apache::loncommon::end_data_table_row();
1.217     raeburn  1821:             }
1.329     raeburn  1822:         }
                   1823:         unless($output{$type} eq '') {
                   1824:             $output{$type} = '<tr class="LC_info_row">'.
                   1825:                       "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
                   1826:                       $output{$type};
                   1827:             $rolesdisplay = 1;
                   1828:         }
                   1829:     }
                   1830:     if ($rolesdisplay == 1) {
                   1831:         my $contextrole='';
                   1832:         if ($env{'request.course.id'}) {
                   1833:             if (&Apache::loncommon::course_type() eq 'Community') {
                   1834:                 $contextrole = &mt('Existing Roles in this Community');
1.290     bisitz   1835:             } else {
1.329     raeburn  1836:                 $contextrole = &mt('Existing Roles in this Course');
1.290     bisitz   1837:             }
1.329     raeburn  1838:         } elsif ($env{'request.role'} =~ /^au\./) {
                   1839:             $contextrole = &mt('Existing Co-Author Roles in your Construction Space');
                   1840:         } else {
                   1841:             $contextrole = &mt('Existing Roles in this Domain');
                   1842:         }
1.375     raeburn  1843:         $r->print('<div>'.
                   1844: '<fieldset><legend>'.$contextrole.'</legend>'.
1.217     raeburn  1845: &Apache::loncommon::start_data_table("LC_createuser").
                   1846: &Apache::loncommon::start_data_table_header_row().
                   1847: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}.
                   1848: '</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.
                   1849: '</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
                   1850: &Apache::loncommon::end_data_table_header_row());
1.329     raeburn  1851:         foreach my $type ('Construction Space','Course','Community','Domain','System','Unknown') {
                   1852:             if ($output{$type}) {
                   1853:                 $r->print($output{$type}."\n");
1.217     raeburn  1854:             }
                   1855:         }
1.375     raeburn  1856:         $r->print(&Apache::loncommon::end_data_table().
                   1857:                   '</fieldset></div>');
1.329     raeburn  1858:     }
1.217     raeburn  1859:     return;
                   1860: }
                   1861: 
1.218     raeburn  1862: sub new_coauthor_roles {
                   1863:     my ($r,$ccuname,$ccdomain) = @_;
                   1864:     my $addrolesdisplay = 0;
                   1865:     #
                   1866:     # Co-Author
                   1867:     #
                   1868:     if (&Apache::lonuserutils::authorpriv($env{'user.name'},
                   1869:                                           $env{'request.role.domain'}) &&
                   1870:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
                   1871:         # No sense in assigning co-author role to yourself
                   1872:         $addrolesdisplay = 1;
                   1873:         my $cuname=$env{'user.name'};
                   1874:         my $cudom=$env{'request.role.domain'};
                   1875:         my %lt=&Apache::lonlocal::texthash(
                   1876:                     'cs'   => "Construction Space",
                   1877:                     'act'  => "Activate",
                   1878:                     'rol'  => "Role",
                   1879:                     'ext'  => "Extent",
                   1880:                     'sta'  => "Start",
                   1881:                     'end'  => "End",
                   1882:                     'cau'  => "Co-Author",
                   1883:                     'caa'  => "Assistant Co-Author",
                   1884:                     'ssd'  => "Set Start Date",
                   1885:                     'sed'  => "Set End Date"
                   1886:                                        );
                   1887:         $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n".
                   1888:                   &Apache::loncommon::start_data_table()."\n".
                   1889:                   &Apache::loncommon::start_data_table_header_row()."\n".
                   1890:                   '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
                   1891:                   '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
                   1892:                   '<th>'.$lt{'end'}.'</th>'."\n".
                   1893:                   &Apache::loncommon::end_data_table_header_row()."\n".
                   1894:                   &Apache::loncommon::start_data_table_row().'
                   1895:            <td>
1.291     bisitz   1896:             <input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_ca" />
1.218     raeburn  1897:            </td>
                   1898:            <td>'.$lt{'cau'}.'</td>
                   1899:            <td>'.$cudom.'_'.$cuname.'</td>
                   1900:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
                   1901:              <a href=
                   1902: "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>
                   1903: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
                   1904: <a href=
                   1905: "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".
                   1906:               &Apache::loncommon::end_data_table_row()."\n".
                   1907:               &Apache::loncommon::start_data_table_row()."\n".
1.291     bisitz   1908: '<td><input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
1.218     raeburn  1909: <td>'.$lt{'caa'}.'</td>
                   1910: <td>'.$cudom.'_'.$cuname.'</td>
                   1911: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
                   1912: <a href=
                   1913: "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>
                   1914: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
                   1915: <a href=
                   1916: "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".
                   1917:              &Apache::loncommon::end_data_table_row()."\n".
                   1918:              &Apache::loncommon::end_data_table());
                   1919:     } elsif ($env{'request.role'} =~ /^au\./) {
                   1920:         if (!(&Apache::lonuserutils::authorpriv($env{'user.name'},
                   1921:                                                 $env{'request.role.domain'}))) {
                   1922:             $r->print('<span class="LC_error">'.
                   1923:                       &mt('You do not have privileges to assign co-author roles.').
                   1924:                       '</span>');
                   1925:         } elsif (($env{'user.name'} eq $ccuname) &&
                   1926:              ($env{'user.domain'} eq $ccdomain)) {
                   1927:             $r->print(&mt('Assigning yourself a co-author or assistant co-author role in your own author area in Construction Space is not permitted'));
                   1928:         }
                   1929:     }
                   1930:     return $addrolesdisplay;;
                   1931: }
                   1932: 
                   1933: sub new_domain_roles {
1.357     raeburn  1934:     my ($r,$ccdomain) = @_;
1.218     raeburn  1935:     my $addrolesdisplay = 0;
                   1936:     #
                   1937:     # Domain level
                   1938:     #
                   1939:     my $num_domain_level = 0;
                   1940:     my $domaintext =
                   1941:     '<h4>'.&mt('Domain Level').'</h4>'.
                   1942:     &Apache::loncommon::start_data_table().
                   1943:     &Apache::loncommon::start_data_table_header_row().
                   1944:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
                   1945:     &mt('Extent').'</th>'.
                   1946:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
                   1947:     &Apache::loncommon::end_data_table_header_row();
1.312     raeburn  1948:     my @allroles = &Apache::lonuserutils::roles_by_context('domain');
1.218     raeburn  1949:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
1.312     raeburn  1950:         foreach my $role (@allroles) {
                   1951:             next if ($role eq 'ad');
1.357     raeburn  1952:             next if (($role eq 'au') && ($ccdomain ne $thisdomain));
1.218     raeburn  1953:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
                   1954:                my $plrole=&Apache::lonnet::plaintext($role);
                   1955:                my %lt=&Apache::lonlocal::texthash(
                   1956:                     'ssd'  => "Set Start Date",
                   1957:                     'sed'  => "Set End Date"
                   1958:                                        );
                   1959:                $num_domain_level ++;
                   1960:                $domaintext .=
                   1961: &Apache::loncommon::start_data_table_row().
1.291     bisitz   1962: '<td><input type="checkbox" name="act_'.$thisdomain.'_'.$role.'" /></td>
1.218     raeburn  1963: <td>'.$plrole.'</td>
                   1964: <td>'.$thisdomain.'</td>
                   1965: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
                   1966: <a href=
                   1967: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
                   1968: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
                   1969: <a href=
                   1970: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
                   1971: &Apache::loncommon::end_data_table_row();
                   1972:             }
                   1973:         }
                   1974:     }
                   1975:     $domaintext.= &Apache::loncommon::end_data_table();
                   1976:     if ($num_domain_level > 0) {
                   1977:         $r->print($domaintext);
                   1978:         $addrolesdisplay = 1;
                   1979:     }
                   1980:     return $addrolesdisplay;
                   1981: }
                   1982: 
1.188     raeburn  1983: sub user_authentication {
1.227     raeburn  1984:     my ($ccuname,$ccdomain,$formname) = @_;
1.188     raeburn  1985:     my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
1.227     raeburn  1986:     my $outcome;
1.188     raeburn  1987:     # Check for a bad authentication type
                   1988:     if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth):/) {
                   1989:         # bad authentication scheme
                   1990:         my %lt=&Apache::lonlocal::texthash(
                   1991:                        'err'   => "ERROR",
                   1992:                        'uuas'  => "This user has an unrecognized authentication scheme",
                   1993:                        'adcs'  => "Please alert a domain coordinator of this situation",
                   1994:                        'sldb'  => "Please specify login data below",
                   1995:                        'ld'    => "Login Data"
                   1996:         );
                   1997:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
1.227     raeburn  1998:             &initialize_authen_forms($ccdomain,$formname);
                   1999: 
1.190     raeburn  2000:             my $choices = &Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc);
1.188     raeburn  2001:             $outcome = <<ENDBADAUTH;
                   2002: <script type="text/javascript" language="Javascript">
1.301     bisitz   2003: // <![CDATA[
1.188     raeburn  2004: $loginscript
1.301     bisitz   2005: // ]]>
1.188     raeburn  2006: </script>
                   2007: <span class="LC_error">$lt{'err'}:
                   2008: $lt{'uuas'} ($currentauth). $lt{'sldb'}.</span>
                   2009: <h3>$lt{'ld'}</h3>
                   2010: $choices
                   2011: ENDBADAUTH
                   2012:         } else {
                   2013:             # This user is not allowed to modify the user's
                   2014:             # authentication scheme, so just notify them of the problem
                   2015:             $outcome = <<ENDBADAUTH;
                   2016: <span class="LC_error"> $lt{'err'}: 
                   2017: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
                   2018: </span>
                   2019: ENDBADAUTH
                   2020:         }
                   2021:     } else { # Authentication type is valid
1.227     raeburn  2022:         &initialize_authen_forms($ccdomain,$formname,$currentauth,'modifyuser');
1.205     raeburn  2023:         my ($authformcurrent,$can_modify,@authform_others) =
1.188     raeburn  2024:             &modify_login_block($ccdomain,$currentauth);
                   2025:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2026:             # Current user has login modification privileges
                   2027:             my %lt=&Apache::lonlocal::texthash (
                   2028:                            'ld'    => "Login Data",
                   2029:                            'ccld'  => "Change Current Login Data",
                   2030:                            'enld'  => "Enter New Login Data"
                   2031:                                                );
                   2032:             $outcome =
                   2033:                        '<script type="text/javascript" language="Javascript">'."\n".
1.301     bisitz   2034:                        '// <![CDATA['."\n".
1.188     raeburn  2035:                        $loginscript."\n".
1.301     bisitz   2036:                        '// ]]>'."\n".
1.188     raeburn  2037:                        '</script>'."\n".
                   2038:                        '<h3>'.$lt{'ld'}.'</h3>'.
                   2039:                        &Apache::loncommon::start_data_table().
1.205     raeburn  2040:                        &Apache::loncommon::start_data_table_row().
1.188     raeburn  2041:                        '<td>'.$authformnop;
                   2042:             if ($can_modify) {
                   2043:                 $outcome .= '</td>'."\n".
                   2044:                             &Apache::loncommon::end_data_table_row().
                   2045:                             &Apache::loncommon::start_data_table_row().
                   2046:                             '<td>'.$authformcurrent.'</td>'.
                   2047:                             &Apache::loncommon::end_data_table_row()."\n";
                   2048:             } else {
1.200     raeburn  2049:                 $outcome .= '&nbsp;('.$authformcurrent.')</td>'.
                   2050:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  2051:             }
1.205     raeburn  2052:             foreach my $item (@authform_others) { 
                   2053:                 $outcome .= &Apache::loncommon::start_data_table_row().
                   2054:                             '<td>'.$item.'</td>'.
                   2055:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  2056:             }
1.205     raeburn  2057:             $outcome .= &Apache::loncommon::end_data_table();
1.188     raeburn  2058:         } else {
                   2059:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
                   2060:                 my %lt=&Apache::lonlocal::texthash(
                   2061:                            'ccld'  => "Change Current Login Data",
                   2062:                            'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
                   2063:                            'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   2064:                 );
                   2065:                 $outcome .= <<ENDNOPRIV;
                   2066: <h3>$lt{'ccld'}</h3>
                   2067: $lt{'yodo'} $lt{'ifch'}: $ccdomain
1.235     raeburn  2068: <input type="hidden" name="login" value="nochange" />
1.188     raeburn  2069: ENDNOPRIV
                   2070:             }
                   2071:         }
                   2072:     }  ## End of "check for bad authentication type" logic
                   2073:     return $outcome;
                   2074: }
                   2075: 
1.187     raeburn  2076: sub modify_login_block {
                   2077:     my ($dom,$currentauth) = @_;
                   2078:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2079:     my ($authnum,%can_assign) =
                   2080:         &Apache::loncommon::get_assignable_auth($dom);
1.205     raeburn  2081:     my ($authformcurrent,@authform_others,$show_override_msg);
1.187     raeburn  2082:     if ($currentauth=~/^krb(4|5):/) {
                   2083:         $authformcurrent=$authformkrb;
                   2084:         if ($can_assign{'int'}) {
1.205     raeburn  2085:             push(@authform_others,$authformint);
1.187     raeburn  2086:         }
                   2087:         if ($can_assign{'loc'}) {
1.205     raeburn  2088:             push(@authform_others,$authformloc);
1.187     raeburn  2089:         }
                   2090:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   2091:             $show_override_msg = 1;
                   2092:         }
                   2093:     } elsif ($currentauth=~/^internal:/) {
                   2094:         $authformcurrent=$authformint;
                   2095:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2096:             push(@authform_others,$authformkrb);
1.187     raeburn  2097:         }
                   2098:         if ($can_assign{'loc'}) {
1.205     raeburn  2099:             push(@authform_others,$authformloc);
1.187     raeburn  2100:         }
                   2101:         if ($can_assign{'int'}) {
                   2102:             $show_override_msg = 1;
                   2103:         }
                   2104:     } elsif ($currentauth=~/^unix:/) {
                   2105:         $authformcurrent=$authformfsys;
                   2106:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2107:             push(@authform_others,$authformkrb);
1.187     raeburn  2108:         }
                   2109:         if ($can_assign{'int'}) {
1.205     raeburn  2110:             push(@authform_others,$authformint);
1.187     raeburn  2111:         }
                   2112:         if ($can_assign{'loc'}) {
1.205     raeburn  2113:             push(@authform_others,$authformloc);
1.187     raeburn  2114:         }
                   2115:         if ($can_assign{'fsys'}) {
                   2116:             $show_override_msg = 1;
                   2117:         }
                   2118:     } elsif ($currentauth=~/^localauth:/) {
                   2119:         $authformcurrent=$authformloc;
                   2120:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2121:             push(@authform_others,$authformkrb);
1.187     raeburn  2122:         }
                   2123:         if ($can_assign{'int'}) {
1.205     raeburn  2124:             push(@authform_others,$authformint);
1.187     raeburn  2125:         }
                   2126:         if ($can_assign{'loc'}) {
                   2127:             $show_override_msg = 1;
                   2128:         }
                   2129:     }
                   2130:     if ($show_override_msg) {
1.205     raeburn  2131:         $authformcurrent = '<table><tr><td colspan="3">'.$authformcurrent.
                   2132:                            '</td></tr>'."\n".
                   2133:                            '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
                   2134:                            '<td><b>'.&mt('Currently in use').'</b></td>'.
                   2135:                            '<td align="right"><span class="LC_cusr_emph">'.
1.187     raeburn  2136:                             &mt('will override current values').
1.205     raeburn  2137:                             '</span></td></tr></table>';
1.187     raeburn  2138:     }
1.205     raeburn  2139:     return ($authformcurrent,$show_override_msg,@authform_others); 
1.187     raeburn  2140: }
                   2141: 
1.188     raeburn  2142: sub personal_data_display {
1.252     raeburn  2143:     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$rolesarray) = @_;
1.286     raeburn  2144:     my ($output,$showforceid,%userenv,%canmodify,%canmodify_status);
1.219     raeburn  2145:     my @userinfo = ('firstname','middlename','lastname','generation',
                   2146:                     'permanentemail','id');
1.252     raeburn  2147:     my $rowcount = 0;
                   2148:     my $editable = 0;
1.286     raeburn  2149:     %canmodify_status = 
                   2150:         &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
                   2151:                                                    ['inststatus'],$rolesarray);
1.253     raeburn  2152:     if (!$newuser) {
1.188     raeburn  2153:         # Get the users information
                   2154:         %userenv = &Apache::lonnet::get('environment',
                   2155:                    ['firstname','middlename','lastname','generation',
1.286     raeburn  2156:                     'permanentemail','id','inststatus'],$ccdomain,$ccuname);
1.219     raeburn  2157:         %canmodify =
                   2158:             &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
1.252     raeburn  2159:                                                        \@userinfo,$rolesarray);
1.257     raeburn  2160:     } elsif ($context eq 'selfcreate') {
                   2161:         %canmodify = &selfcreate_canmodify($context,$ccdomain,\@userinfo,
                   2162:                                            $inst_results,$rolesarray);
1.188     raeburn  2163:     }
                   2164:     my %lt=&Apache::lonlocal::texthash(
                   2165:                 'pd'             => "Personal Data",
                   2166:                 'firstname'      => "First Name",
                   2167:                 'middlename'     => "Middle Name",
                   2168:                 'lastname'       => "Last Name",
                   2169:                 'generation'     => "Generation",
                   2170:                 'permanentemail' => "Permanent e-mail address",
1.259     bisitz   2171:                 'id'             => "Student/Employee ID",
1.286     raeburn  2172:                 'lg'             => "Login Data",
                   2173:                 'inststatus'     => "Affiliation",
1.188     raeburn  2174:     );
                   2175:     my %textboxsize = (
                   2176:                        firstname      => '15',
                   2177:                        middlename     => '15',
                   2178:                        lastname       => '15',
                   2179:                        generation     => '5',
                   2180:                        permanentemail => '25',
                   2181:                        id             => '15',
                   2182:                       );
                   2183:     my $genhelp=&Apache::loncommon::help_open_topic('Generation');
                   2184:     $output = '<h3>'.$lt{'pd'}.'</h3>'.
                   2185:               &Apache::lonhtmlcommon::start_pick_box();
                   2186:     foreach my $item (@userinfo) {
                   2187:         my $rowtitle = $lt{$item};
1.252     raeburn  2188:         my $hiderow = 0;
1.188     raeburn  2189:         if ($item eq 'generation') {
                   2190:             $rowtitle = $genhelp.$rowtitle;
                   2191:         }
1.252     raeburn  2192:         my $row = &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
1.188     raeburn  2193:         if ($newuser) {
1.210     raeburn  2194:             if (ref($inst_results) eq 'HASH') {
                   2195:                 if ($inst_results->{$item} ne '') {
1.252     raeburn  2196:                     $row .= '<input type="hidden" name="c'.$item.'" value="'.$inst_results->{$item}.'" />'.$inst_results->{$item};
1.210     raeburn  2197:                 } else {
1.252     raeburn  2198:                     if ($context eq 'selfcreate') {
                   2199:                         if ($canmodify{$item}) { 
                   2200:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                   2201:                             $editable ++;
                   2202:                         } else {
                   2203:                             $hiderow = 1;
                   2204:                         }
1.253     raeburn  2205:                     } else {
                   2206:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2207:                     }
1.210     raeburn  2208:                 }
1.188     raeburn  2209:             } else {
1.252     raeburn  2210:                 if ($context eq 'selfcreate') {
1.287     raeburn  2211:                     if (($item eq 'permanentemail') && ($newuser eq 'email')) {
                   2212:                         $row .= $ccuname;
1.252     raeburn  2213:                     } else {
1.287     raeburn  2214:                         if ($canmodify{$item}) {
                   2215:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                   2216:                             $editable ++;
                   2217:                         } else {
                   2218:                             $hiderow = 1;
                   2219:                         }
1.252     raeburn  2220:                     }
1.253     raeburn  2221:                 } else {
                   2222:                     $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2223:                 }
1.188     raeburn  2224:             }
                   2225:         } else {
1.219     raeburn  2226:             if ($canmodify{$item}) {
1.252     raeburn  2227:                 $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
1.188     raeburn  2228:             } else {
1.252     raeburn  2229:                 $row .= $userenv{$item};
1.188     raeburn  2230:             }
1.206     raeburn  2231:             if ($item eq 'id') {
1.219     raeburn  2232:                 $showforceid = $canmodify{$item};
                   2233:             }
1.188     raeburn  2234:         }
1.252     raeburn  2235:         $row .= &Apache::lonhtmlcommon::row_closure(1);
                   2236:         if (!$hiderow) {
                   2237:             $output .= $row;
                   2238:             $rowcount ++;
                   2239:         }
1.188     raeburn  2240:     }
1.286     raeburn  2241:     if (($canmodify_status{'inststatus'}) || ($context ne 'selfcreate')) {
                   2242:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($ccdomain);
                   2243:         if (ref($types) eq 'ARRAY') {
                   2244:             if (@{$types} > 0) {
                   2245:                 my ($hiderow,$shown);
                   2246:                 if ($canmodify_status{'inststatus'}) {
                   2247:                     $shown = &pick_inst_statuses($userenv{'inststatus'},$usertypes,$types);
                   2248:                 } else {
                   2249:                     if ($userenv{'inststatus'} eq '') {
                   2250:                         $hiderow = 1;
1.334     raeburn  2251:                     } else {
                   2252:                         my @showitems;
                   2253:                         foreach my $item ( map { &unescape($_); } split(':',$userenv{'inststatus'})) {
                   2254:                             if (exists($usertypes->{$item})) {
                   2255:                                 push(@showitems,$usertypes->{$item});
                   2256:                             } else {
                   2257:                                 push(@showitems,$item);
                   2258:                             }
                   2259:                         }
                   2260:                         if (@showitems) {
                   2261:                             $shown = join(', ',@showitems);
                   2262:                         } else {
                   2263:                             $hiderow = 1;
                   2264:                         }
1.286     raeburn  2265:                     }
                   2266:                 }
                   2267:                 if (!$hiderow) {
                   2268:                     my $row = &Apache::lonhtmlcommon::row_title(&mt('Affliations'),undef,'LC_oddrow_value')."\n".
                   2269:                               $shown.&Apache::lonhtmlcommon::row_closure(1); 
                   2270:                     if ($context eq 'selfcreate') {
                   2271:                         $rowcount ++;
                   2272:                     }
                   2273:                     $output .= $row;
                   2274:                 }
                   2275:             }
                   2276:         }
                   2277:     }
1.188     raeburn  2278:     $output .= &Apache::lonhtmlcommon::end_pick_box();
1.206     raeburn  2279:     if (wantarray) {
1.252     raeburn  2280:         if ($context eq 'selfcreate') {
                   2281:             return($output,$rowcount,$editable);
                   2282:         } else {
                   2283:             return ($output,$showforceid);
                   2284:         }
1.206     raeburn  2285:     } else {
                   2286:         return $output;
                   2287:     }
1.188     raeburn  2288: }
                   2289: 
1.286     raeburn  2290: sub pick_inst_statuses {
                   2291:     my ($curr,$usertypes,$types) = @_;
                   2292:     my ($output,$rem,@currtypes);
                   2293:     if ($curr ne '') {
                   2294:         @currtypes = map { &unescape($_); } split(/:/,$curr);
                   2295:     }
                   2296:     my $numinrow = 2;
                   2297:     if (ref($types) eq 'ARRAY') {
                   2298:         $output = '<table>';
                   2299:         my $lastcolspan; 
                   2300:         for (my $i=0; $i<@{$types}; $i++) {
                   2301:             if (defined($usertypes->{$types->[$i]})) {
                   2302:                 my $rem = $i%($numinrow);
                   2303:                 if ($rem == 0) {
                   2304:                     if ($i<@{$types}-1) {
                   2305:                         if ($i > 0) { 
                   2306:                             $output .= '</tr>';
                   2307:                         }
                   2308:                         $output .= '<tr>';
                   2309:                     }
                   2310:                 } elsif ($i==@{$types}-1) {
                   2311:                     my $colsleft = $numinrow - $rem;
                   2312:                     if ($colsleft > 1) {
                   2313:                         $lastcolspan = ' colspan="'.$colsleft.'"';
                   2314:                     }
                   2315:                 }
                   2316:                 my $check = ' ';
                   2317:                 if (grep(/^\Q$types->[$i]\E$/,@currtypes)) {
                   2318:                     $check = ' checked="checked" ';
                   2319:                 }
                   2320:                 $output .= '<td class="LC_left_item"'.$lastcolspan.'>'.
                   2321:                            '<span class="LC_nobreak"><label>'.
                   2322:                            '<input type="checkbox" name="inststatus" '.
                   2323:                            'value="'.$types->[$i].'"'.$check.'/>'.
                   2324:                            $usertypes->{$types->[$i]}.'</label></span></td>';
                   2325:             }
                   2326:         }
                   2327:         $output .= '</tr></table>';
                   2328:     }
                   2329:     return $output;
                   2330: }
                   2331: 
1.257     raeburn  2332: sub selfcreate_canmodify {
                   2333:     my ($context,$dom,$userinfo,$inst_results,$rolesarray) = @_;
                   2334:     if (ref($inst_results) eq 'HASH') {
                   2335:         my @inststatuses = &get_inststatuses($inst_results);
                   2336:         if (@inststatuses == 0) {
                   2337:             @inststatuses = ('default');
                   2338:         }
                   2339:         $rolesarray = \@inststatuses;
                   2340:     }
                   2341:     my %canmodify =
                   2342:         &Apache::lonuserutils::can_modify_userinfo($context,$dom,$userinfo,
                   2343:                                                    $rolesarray);
                   2344:     return %canmodify;
                   2345: }
                   2346: 
1.252     raeburn  2347: sub get_inststatuses {
                   2348:     my ($insthashref) = @_;
                   2349:     my @inststatuses = ();
                   2350:     if (ref($insthashref) eq 'HASH') {
                   2351:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
                   2352:             @inststatuses = @{$insthashref->{'inststatus'}};
                   2353:         }
                   2354:     }
                   2355:     return @inststatuses;
                   2356: }
                   2357: 
1.4       www      2358: # ================================================================= Phase Three
1.42      matthew  2359: sub update_user_data {
1.375     raeburn  2360:     my ($r,$context,$crstype,$brcrum,$showcredits) = @_; 
1.101     albertel 2361:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                   2362:                                           $env{'form.ccdomain'});
1.27      matthew  2363:     # Error messages
1.188     raeburn  2364:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
1.193     raeburn  2365:     my $end       = '</span><br /><br />';
                   2366:     my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
1.188     raeburn  2367:                     "'$env{'form.prevphase'}','modify')".'" />'.
1.219     raeburn  2368:                     &mt('Return to previous page').'</a>'.
                   2369:                     &Apache::loncommon::end_page();
                   2370:     my $now = time;
1.40      www      2371:     my $title;
1.101     albertel 2372:     if (exists($env{'form.makeuser'})) {
1.40      www      2373: 	$title='Set Privileges for New User';
                   2374:     } else {
                   2375:         $title='Modify User Privileges';
                   2376:     }
1.213     raeburn  2377:     my $newuser = 0;
1.160     raeburn  2378:     my ($jsback,$elements) = &crumb_utilities();
                   2379:     my $jscript = '<script type="text/javascript">'."\n".
1.301     bisitz   2380:                   '// <![CDATA['."\n".
                   2381:                   $jsback."\n".
                   2382:                   '// ]]>'."\n".
                   2383:                   '</script>'."\n";
1.318     raeburn  2384:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.351     raeburn  2385:     push (@{$brcrum},
                   2386:              {href => "javascript:backPage(document.userupdate)",
                   2387:               text => $breadcrumb_text{'search'},
                   2388:               faq  => 282,
                   2389:               bug  => 'Instructor Interface',}
                   2390:              );
                   2391:     if ($env{'form.prevphase'} eq 'userpicked') {
                   2392:         push(@{$brcrum},
                   2393:                {href => "javascript:backPage(document.userupdate,'get_user_info','select')",
                   2394:                 text => $breadcrumb_text{'userpicked'},
                   2395:                 faq  => 282,
                   2396:                 bug  => 'Instructor Interface',});
1.233     raeburn  2397:     }
1.224     raeburn  2398:     my $helpitem = 'Course_Change_Privileges';
                   2399:     if ($env{'form.action'} eq 'singlestudent') {
                   2400:         $helpitem = 'Course_Add_Student';
                   2401:     }
1.351     raeburn  2402:     push(@{$brcrum}, 
                   2403:             {href => "javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
                   2404:              text => $breadcrumb_text{'modify'},
                   2405:              faq  => 282,
                   2406:              bug  => 'Instructor Interface',},
                   2407:             {href => "/adm/createuser",
                   2408:              text => "Result",
                   2409:              faq  => 282,
                   2410:              bug  => 'Instructor Interface',
                   2411:              help => $helpitem});
                   2412:     my $args = {bread_crumbs          => $brcrum,
                   2413:                 bread_crumbs_component => 'User Management'};
                   2414:     if ($env{'form.popup'}) {
                   2415:         $args->{'no_nav_bar'} = 1;
                   2416:     }
                   2417:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
1.188     raeburn  2418:     $r->print(&update_result_form($uhome));
1.27      matthew  2419:     # Check Inputs
1.101     albertel 2420:     if (! $env{'form.ccuname'} ) {
1.193     raeburn  2421: 	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
1.27      matthew  2422: 	return;
                   2423:     }
1.138     albertel 2424:     if (  $env{'form.ccuname'} ne 
                   2425: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
1.281     bisitz   2426: 	$r->print($error.&mt('Invalid login name.').'  '.
                   2427: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid.').
1.193     raeburn  2428: 		  $end.$rtnlink);
1.27      matthew  2429: 	return;
                   2430:     }
1.101     albertel 2431:     if (! $env{'form.ccdomain'}       ) {
1.193     raeburn  2432: 	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
1.27      matthew  2433: 	return;
                   2434:     }
1.138     albertel 2435:     if (  $env{'form.ccdomain'} ne
                   2436: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
1.281     bisitz   2437: 	$r->print($error.&mt('Invalid domain name.').'  '.
                   2438: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid.').
1.193     raeburn  2439: 		  $end.$rtnlink);
1.27      matthew  2440: 	return;
                   2441:     }
1.219     raeburn  2442:     if ($uhome eq 'no_host') {
                   2443:         $newuser = 1;
                   2444:     }
1.101     albertel 2445:     if (! exists($env{'form.makeuser'})) {
1.29      matthew  2446:         # Modifying an existing user, so check the validity of the name
                   2447:         if ($uhome eq 'no_host') {
1.73      sakharuk 2448:             $r->print($error.&mt('Unable to determine home server for ').
1.101     albertel 2449:                       $env{'form.ccuname'}.&mt(' in domain ').
                   2450:                       $env{'form.ccdomain'}.'.');
1.29      matthew  2451:             return;
                   2452:         }
                   2453:     }
1.27      matthew  2454:     # Determine authentication method and password for the user being modified
                   2455:     my $amode='';
                   2456:     my $genpwd='';
1.101     albertel 2457:     if ($env{'form.login'} eq 'krb') {
1.41      albertel 2458: 	$amode='krb';
1.101     albertel 2459: 	$amode.=$env{'form.krbver'};
                   2460: 	$genpwd=$env{'form.krbarg'};
                   2461:     } elsif ($env{'form.login'} eq 'int') {
1.27      matthew  2462: 	$amode='internal';
1.101     albertel 2463: 	$genpwd=$env{'form.intarg'};
                   2464:     } elsif ($env{'form.login'} eq 'fsys') {
1.27      matthew  2465: 	$amode='unix';
1.101     albertel 2466: 	$genpwd=$env{'form.fsysarg'};
                   2467:     } elsif ($env{'form.login'} eq 'loc') {
1.27      matthew  2468: 	$amode='localauth';
1.101     albertel 2469: 	$genpwd=$env{'form.locarg'};
1.27      matthew  2470: 	$genpwd=" " if (!$genpwd);
1.101     albertel 2471:     } elsif (($env{'form.login'} eq 'nochange') ||
                   2472:              ($env{'form.login'} eq ''        )) { 
1.34      matthew  2473:         # There is no need to tell the user we did not change what they
                   2474:         # did not ask us to change.
1.35      matthew  2475:         # If they are creating a new user but have not specified login
                   2476:         # information this will be caught below.
1.30      matthew  2477:     } else {
1.367     golterma 2478:             $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);
                   2479:             return;
1.27      matthew  2480:     }
1.164     albertel 2481: 
1.188     raeburn  2482:     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
1.367     golterma 2483:                         $env{'form.ccuname'}.' ('.&Apache::loncommon::plainname($env{'form.ccuname'},
                   2484:                         $env{'form.ccdomain'}).')', $env{'form.ccdomain'}).'</h3>');
                   2485:     my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,2);
1.344     bisitz   2486: 
1.193     raeburn  2487:     my (%alerts,%rulematch,%inst_results,%curr_rules);
1.334     raeburn  2488:     my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
1.361     raeburn  2489:     my @usertools = ('aboutme','blog','webdav','portfolio');
1.299     raeburn  2490:     my @requestcourses = ('official','unofficial','community');
1.362     raeburn  2491:     my @requestauthor = ('requestauthor');
1.286     raeburn  2492:     my ($othertitle,$usertypes,$types) = 
                   2493:         &Apache::loncommon::sorted_inst_types($env{'form.ccdomain'});
1.334     raeburn  2494:     my %canmodify_status =
                   2495:         &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},
                   2496:                                                    ['inststatus']);
1.101     albertel 2497:     if ($env{'form.makeuser'}) {
1.164     albertel 2498: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
1.27      matthew  2499:         # Check for the authentication mode and password
                   2500:         if (! $amode || ! $genpwd) {
1.193     raeburn  2501: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.27      matthew  2502: 	    return;
1.18      albertel 2503: 	}
1.29      matthew  2504:         # Determine desired host
1.101     albertel 2505:         my $desiredhost = $env{'form.hserver'};
1.29      matthew  2506:         if (lc($desiredhost) eq 'default') {
                   2507:             $desiredhost = undef;
                   2508:         } else {
1.147     albertel 2509:             my %home_servers = 
                   2510: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
1.29      matthew  2511:             if (! exists($home_servers{$desiredhost})) {
1.193     raeburn  2512:                 $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
                   2513:                 return;
                   2514:             }
                   2515:         }
                   2516:         # Check ID format
                   2517:         my %checkhash;
                   2518:         my %checks = ('id' => 1);
                   2519:         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
1.219     raeburn  2520:             'newuser' => $newuser, 
1.196     raeburn  2521:             'id' => $env{'form.cid'},
1.193     raeburn  2522:         );
1.196     raeburn  2523:         if ($env{'form.cid'} ne '') {
                   2524:             &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   2525:                                           \%rulematch,\%inst_results,\%curr_rules);
                   2526:             if (ref($alerts{'id'}) eq 'HASH') {
                   2527:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
                   2528:                     my $domdesc =
                   2529:                         &Apache::lonnet::domain($env{'form.ccdomain'},'description');
                   2530:                     if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
                   2531:                         my $userchkmsg;
                   2532:                         if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
                   2533:                             $userchkmsg  = 
                   2534:                                 &Apache::loncommon::instrule_disallow_msg('id',
                   2535:                                                                     $domdesc,1).
                   2536:                                 &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
                   2537:                                     $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
                   2538:                         }
                   2539:                         $r->print($error.&mt('Invalid ID format').$end.
                   2540:                                   $userchkmsg.$rtnlink);
                   2541:                         return;
                   2542:                     }
                   2543:                 }
1.29      matthew  2544:             }
                   2545:         }
1.367     golterma 2546:         &Apache::lonhtmlcommon::Increment_PrgWin($r, \%prog_state);
1.27      matthew  2547: 	# Call modifyuser
                   2548: 	my $result = &Apache::lonnet::modifyuser
1.193     raeburn  2549: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
1.188     raeburn  2550:              $amode,$genpwd,$env{'form.cfirstname'},
                   2551:              $env{'form.cmiddlename'},$env{'form.clastname'},
                   2552:              $env{'form.cgeneration'},undef,$desiredhost,
                   2553:              $env{'form.cpermanentemail'});
1.77      www      2554: 	$r->print(&mt('Generating user').': '.$result);
1.219     raeburn  2555:         $uhome = &Apache::lonnet::homeserver($env{'form.ccuname'},
1.101     albertel 2556:                                                $env{'form.ccdomain'});
1.334     raeburn  2557:         my (%changeHash,%newcustom,%changed,%changedinfo);
1.267     raeburn  2558:         if ($uhome ne 'no_host') {
1.334     raeburn  2559:             if ($context eq 'domain') {
                   2560:                 if ($env{'form.customquota'} == 1) {
                   2561:                     if ($env{'form.portfolioquota'} eq '') {
                   2562:                         $newcustom{'quota'} = 0;
                   2563:                     } else {
                   2564:                         $newcustom{'quota'} = $env{'form.portfolioquota'};
                   2565:                         $newcustom{'quota'} =~ s/[^\d\.]//g;
                   2566:                     }
                   2567:                     $changed{'quota'} = &quota_admin($newcustom{'quota'},\%changeHash);
                   2568:                 }
                   2569:                 foreach my $item (@usertools) {
                   2570:                     if ($env{'form.custom'.$item} == 1) {
                   2571:                         $newcustom{$item} = $env{'form.tools_'.$item};
                   2572:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2573:                                                      \%changeHash,'tools');
                   2574:                     }
1.267     raeburn  2575:                 }
1.334     raeburn  2576:                 foreach my $item (@requestcourses) {
1.341     raeburn  2577:                     if ($env{'form.custom'.$item} == 1) {
                   2578:                         $newcustom{$item} = $env{'form.crsreq_'.$item};
                   2579:                         if ($env{'form.crsreq_'.$item} eq 'autolimit') {
                   2580:                             $newcustom{$item} .= '=';
                   2581:                             unless ($env{'form.crsreq_'.$item.'_limit'} =~ /\D/) {
                   2582:                                 $newcustom{$item} .= $env{'form.crsreq_'.$item.'_limit'};
                   2583:                             }
1.334     raeburn  2584:                         }
1.341     raeburn  2585:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2586:                                                       \%changeHash,'requestcourses');
1.334     raeburn  2587:                     }
1.275     raeburn  2588:                 }
1.362     raeburn  2589:                 if ($env{'form.customrequestauthor'} == 1) {
                   2590:                     $newcustom{'requestauthor'} = $env{'form.requestauthor'};
                   2591:                     $changed{'requestauthor'} = &tool_admin('requestauthor',
                   2592:                                                     $newcustom{'requestauthor'},
                   2593:                                                     \%changeHash,'requestauthor');
                   2594:                 }
1.275     raeburn  2595:             }
1.334     raeburn  2596:             if ($canmodify_status{'inststatus'}) {
                   2597:                 if (exists($env{'form.inststatus'})) {
                   2598:                     my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2599:                     if (@inststatuses > 0) {
                   2600:                         $changeHash{'inststatus'} = join(',',@inststatuses);
                   2601:                         $changed{'inststatus'} = $changeHash{'inststatus'};
1.306     raeburn  2602:                     }
                   2603:                 }
1.232     raeburn  2604:             }
1.334     raeburn  2605:             if (keys(%changed)) {
                   2606:                 foreach my $item (@userinfo) {
                   2607:                     $changeHash{$item}  = $env{'form.c'.$item};
1.286     raeburn  2608:                 }
1.267     raeburn  2609:                 my $chgresult =
                   2610:                      &Apache::lonnet::put('environment',\%changeHash,
                   2611:                                           $env{'form.ccdomain'},$env{'form.ccuname'});
                   2612:             } 
1.232     raeburn  2613:         }
1.219     raeburn  2614:         $r->print('<br />'.&mt('Home server').': '.$uhome.' '.
                   2615:                   &Apache::lonnet::hostname($uhome));
1.101     albertel 2616:     } elsif (($env{'form.login'} ne 'nochange') &&
                   2617:              ($env{'form.login'} ne ''        )) {
1.27      matthew  2618: 	# Modify user privileges
                   2619:         if (! $amode || ! $genpwd) {
1.193     raeburn  2620: 	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
1.27      matthew  2621: 	    return;
1.20      harris41 2622: 	}
1.27      matthew  2623: 	# Only allow authentification modification if the person has authority
1.101     albertel 2624: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.20      harris41 2625: 	    $r->print('Modifying authentication: '.
1.31      matthew  2626:                       &Apache::lonnet::modifyuserauth(
1.101     albertel 2627: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
1.21      harris41 2628:                        $amode,$genpwd));
1.102     albertel 2629:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
1.101     albertel 2630: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
1.4       www      2631: 	} else {
1.27      matthew  2632: 	    # Okay, this is a non-fatal error.
1.193     raeburn  2633: 	    $r->print($error.&mt('You do not have the authority to modify this users authentification information').'.'.$end);    
1.27      matthew  2634: 	}
1.28      matthew  2635:     }
1.344     bisitz   2636:     $r->rflush(); # Finish display of header before time consuming actions start
1.367     golterma 2637:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state);
1.28      matthew  2638:     ##
1.375     raeburn  2639:     my (@userroles,%userupdate,$cnum,$cdom,$defaultcredits,%namechanged);
1.213     raeburn  2640:     if ($context eq 'course') {
1.375     raeburn  2641:         ($cnum,$cdom) =
                   2642:             &Apache::lonuserutils::get_course_identity();
1.318     raeburn  2643:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
1.375     raeburn  2644:         if ($showcredits) {
                   2645:            $defaultcredits = &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   2646:         }
1.213     raeburn  2647:     }
1.101     albertel 2648:     if (! $env{'form.makeuser'} ) {
1.28      matthew  2649:         # Check for need to change
                   2650:         my %userenv = &Apache::lonnet::get
1.134     raeburn  2651:             ('environment',['firstname','middlename','lastname','generation',
1.267     raeburn  2652:              'id','permanentemail','portfolioquota','inststatus','tools.aboutme',
1.361     raeburn  2653:              'tools.blog','tools.webdav','tools.portfolio',
                   2654:              'requestcourses.official','requestcourses.unofficial',
                   2655:              'requestcourses.community','reqcrsotherdom.official',
1.362     raeburn  2656:              'reqcrsotherdom.unofficial','reqcrsotherdom.community',
                   2657:              'requestauthor'],
1.160     raeburn  2658:               $env{'form.ccdomain'},$env{'form.ccuname'});
1.28      matthew  2659:         my ($tmp) = keys(%userenv);
                   2660:         if ($tmp =~ /^(con_lost|error)/i) { 
                   2661:             %userenv = ();
                   2662:         }
1.206     raeburn  2663:         my $no_forceid_alert;
                   2664:         # Check to see if user information can be changed
                   2665:         my %domconfig =
                   2666:             &Apache::lonnet::get_dom('configuration',['usermodification'],
                   2667:                                      $env{'form.ccdomain'});
1.213     raeburn  2668:         my @statuses = ('active','future');
                   2669:         my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@statuses,undef,$env{'request.role.domain'});
                   2670:         my ($auname,$audom);
1.220     raeburn  2671:         if ($context eq 'author') {
1.206     raeburn  2672:             $auname = $env{'user.name'};
                   2673:             $audom = $env{'user.domain'};     
                   2674:         }
                   2675:         foreach my $item (keys(%roles)) {
1.220     raeburn  2676:             my ($rolenum,$roledom,$role) = split(/:/,$item,-1);
1.206     raeburn  2677:             if ($context eq 'course') {
                   2678:                 if ($cnum ne '' && $cdom ne '') {
                   2679:                     if ($rolenum eq $cnum && $roledom eq $cdom) {
                   2680:                         if (!grep(/^\Q$role\E$/,@userroles)) {
                   2681:                             push(@userroles,$role);
                   2682:                         }
                   2683:                     }
                   2684:                 }
                   2685:             } elsif ($context eq 'author') {
                   2686:                 if ($rolenum eq $auname && $roledom eq $audom) {
                   2687:                     if (!grep(/^\Q$role\E$/,@userroles)) { 
                   2688:                         push(@userroles,$role);
                   2689:                     }
                   2690:                 }
                   2691:             }
                   2692:         }
1.220     raeburn  2693:         if ($env{'form.action'} eq 'singlestudent') {
                   2694:             if (!grep(/^st$/,@userroles)) {
                   2695:                 push(@userroles,'st');
                   2696:             }
                   2697:         } else {
                   2698:             # Check for course or co-author roles being activated or re-enabled
                   2699:             if ($context eq 'author' || $context eq 'course') {
                   2700:                 foreach my $key (keys(%env)) {
                   2701:                     if ($context eq 'author') {
                   2702:                         if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
                   2703:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2704:                                 push(@userroles,$1);
                   2705:                             }
                   2706:                         } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
                   2707:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2708:                                 push(@userroles,$1);
                   2709:                             }
1.206     raeburn  2710:                         }
1.220     raeburn  2711:                     } elsif ($context eq 'course') {
                   2712:                         if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
                   2713:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2714:                                 push(@userroles,$1);
                   2715:                             }
                   2716:                         } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
                   2717:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2718:                                 push(@userroles,$1);
                   2719:                             }
1.206     raeburn  2720:                         }
                   2721:                     }
                   2722:                 }
                   2723:             }
                   2724:         }
                   2725:         #Check to see if we can change personal data for the user 
                   2726:         my (@mod_disallowed,@longroles);
                   2727:         foreach my $role (@userroles) {
                   2728:             if ($role eq 'cr') {
                   2729:                 push(@longroles,'Custom');
                   2730:             } else {
1.318     raeburn  2731:                 push(@longroles,&Apache::lonnet::plaintext($role,$crstype)); 
1.206     raeburn  2732:             }
                   2733:         }
1.219     raeburn  2734:         my %canmodify = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},\@userinfo,\@userroles);
                   2735:         foreach my $item (@userinfo) {
1.28      matthew  2736:             # Strip leading and trailing whitespace
1.203     raeburn  2737:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
1.219     raeburn  2738:             if (!$canmodify{$item}) {
1.207     raeburn  2739:                 if (defined($env{'form.c'.$item})) {
                   2740:                     if ($env{'form.c'.$item} ne $userenv{$item}) {
                   2741:                         push(@mod_disallowed,$item);
                   2742:                     }
1.206     raeburn  2743:                 }
                   2744:                 $env{'form.c'.$item} = $userenv{$item};
                   2745:             }
1.28      matthew  2746:         }
1.259     bisitz   2747:         # Check to see if we can change the Student/Employee ID
1.196     raeburn  2748:         my $forceid = $env{'form.forceid'};
                   2749:         my $recurseid = $env{'form.recurseid'};
                   2750:         my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
1.203     raeburn  2751:         my %uidhash = &Apache::lonnet::idrget($env{'form.ccdomain'},
                   2752:                                             $env{'form.ccuname'});
                   2753:         if (($uidhash{$env{'form.ccuname'}}) && 
                   2754:             ($uidhash{$env{'form.ccuname'}}!~/error\:/) && 
                   2755:             (!$forceid)) {
                   2756:             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
                   2757:                 $env{'form.cid'} = $userenv{'id'};
1.293     bisitz   2758:                 $no_forceid_alert = &mt('New student/employee ID does not match existing ID for this user.')
1.259     bisitz   2759:                                    .'<br />'
                   2760:                                    .&mt("Change is not permitted without checking the 'Force ID change' checkbox on the previous page.")
                   2761:                                    .'<br />'."\n";
1.203     raeburn  2762:             }
                   2763:         }
                   2764:         if ($env{'form.cid'} ne $userenv{'id'}) {
1.196     raeburn  2765:             my $checkhash;
                   2766:             my $checks = { 'id' => 1 };
                   2767:             $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
                   2768:                    { 'newuser' => $newuser,
                   2769:                      'id'  => $env{'form.cid'}, 
                   2770:                    };
                   2771:             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   2772:                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
                   2773:             if (ref($alerts{'id'}) eq 'HASH') {
                   2774:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
1.203     raeburn  2775:                    $env{'form.cid'} = $userenv{'id'};
1.196     raeburn  2776:                 }
                   2777:             }
                   2778:         }
1.286     raeburn  2779:         my ($quotachanged,$oldportfolioquota,$newportfolioquota,$oldinststatus,
1.334     raeburn  2780:             $newinststatus,$oldisdefault,$newisdefault,%oldsettings,
1.339     raeburn  2781:             %oldsettingstext,%newsettings,%newsettingstext,@disporder,
                   2782:             $olddefquota,$oldsettingstatus,$newdefquota,$newsettingstatus);
1.334     raeburn  2783:         @disporder = ('inststatus');
                   2784:         if ($env{'request.role.domain'} eq $env{'form.ccdomain'}) {
1.362     raeburn  2785:             push(@disporder,'requestcourses','requestauthor');
1.334     raeburn  2786:         } else {
                   2787:             push(@disporder,'reqcrsotherdom');
                   2788:         }
                   2789:         push(@disporder,('quota','tools'));
1.338     raeburn  2790:         $oldinststatus = $userenv{'inststatus'};
1.339     raeburn  2791:         ($olddefquota,$oldsettingstatus) = 
1.334     raeburn  2792:             &Apache::loncommon::default_quota($env{'form.ccdomain'},$oldinststatus);
1.339     raeburn  2793:         ($newdefquota,$newsettingstatus) = ($olddefquota,$oldsettingstatus);
1.334     raeburn  2794:         my %canshow;
1.220     raeburn  2795:         if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
1.334     raeburn  2796:             $canshow{'quota'} = 1;
1.220     raeburn  2797:         }
1.267     raeburn  2798:         if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
1.334     raeburn  2799:             $canshow{'tools'} = 1;
1.267     raeburn  2800:         }
1.275     raeburn  2801:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
1.334     raeburn  2802:             $canshow{'requestcourses'} = 1;
1.300     raeburn  2803:         } elsif (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.334     raeburn  2804:             $canshow{'reqcrsotherdom'} = 1;
1.275     raeburn  2805:         }
1.286     raeburn  2806:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.334     raeburn  2807:             $canshow{'inststatus'} = 1;
1.286     raeburn  2808:         }
1.362     raeburn  2809:         if (&Apache::lonnet::allowed('cau',$env{'form.ccdomain'})) {
                   2810:             $canshow{'requestauthor'} = 1;
                   2811:         }
1.267     raeburn  2812:         my (%changeHash,%changed);
1.286     raeburn  2813:         if ($oldinststatus eq '') {
1.334     raeburn  2814:             $oldsettings{'inststatus'} = $othertitle; 
1.286     raeburn  2815:         } else {
                   2816:             if (ref($usertypes) eq 'HASH') {
1.334     raeburn  2817:                 $oldsettings{'inststatus'} = join(', ',map{ $usertypes->{ &unescape($_) }; } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  2818:             } else {
1.334     raeburn  2819:                 $oldsettings{'inststatus'} = join(', ',map{ &unescape($_); } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  2820:             }
                   2821:         }
                   2822:         $changeHash{'inststatus'} = $userenv{'inststatus'};
1.334     raeburn  2823:         if ($canmodify_status{'inststatus'}) {
                   2824:             $canshow{'inststatus'} = 1;
1.286     raeburn  2825:             if (exists($env{'form.inststatus'})) {
                   2826:                 my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2827:                 if (@inststatuses > 0) {
                   2828:                     $newinststatus = join(':',map { &escape($_); } @inststatuses);
                   2829:                     $changeHash{'inststatus'} = $newinststatus;
                   2830:                     if ($newinststatus ne $oldinststatus) {
                   2831:                         $changed{'inststatus'} = $newinststatus;
1.339     raeburn  2832:                         ($newdefquota,$newsettingstatus) =
                   2833:                             &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus);
1.286     raeburn  2834:                     }
                   2835:                     if (ref($usertypes) eq 'HASH') {
1.334     raeburn  2836:                         $newsettings{'inststatus'} = join(', ',map{ $usertypes->{$_}; } (@inststatuses)); 
1.286     raeburn  2837:                     } else {
1.337     raeburn  2838:                         $newsettings{'inststatus'} = join(', ',@inststatuses);
1.286     raeburn  2839:                     }
1.334     raeburn  2840:                 }
                   2841:             } else {
                   2842:                 $newinststatus = '';
                   2843:                 $changeHash{'inststatus'} = $newinststatus;
                   2844:                 $newsettings{'inststatus'} = $othertitle;
                   2845:                 if ($newinststatus ne $oldinststatus) {
                   2846:                     $changed{'inststatus'} = $changeHash{'inststatus'};
1.339     raeburn  2847:                     ($newdefquota,$newsettingstatus) =
                   2848:                         &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus);
1.286     raeburn  2849:                 }
                   2850:             }
1.334     raeburn  2851:         } elsif ($context ne 'selfcreate') {
                   2852:             $canshow{'inststatus'} = 1;
1.337     raeburn  2853:             $newsettings{'inststatus'} = $oldsettings{'inststatus'};
1.286     raeburn  2854:         }
1.204     raeburn  2855:         $changeHash{'portfolioquota'} = $userenv{'portfolioquota'};
1.334     raeburn  2856:         if ($context eq 'domain') {
                   2857:             if ($userenv{'portfolioquota'} ne '') {
                   2858:                 $oldportfolioquota = $userenv{'portfolioquota'};
                   2859:                 if ($env{'form.customquota'} == 1) {
                   2860:                     if ($env{'form.portfolioquota'} eq '') {
                   2861:                         $newportfolioquota = 0;
                   2862:                     } else {
                   2863:                         $newportfolioquota = $env{'form.portfolioquota'};
                   2864:                         $newportfolioquota =~ s/[^\d\.]//g;
                   2865:                     }
                   2866:                     if ($newportfolioquota != $oldportfolioquota) {
                   2867:                         $changed{'quota'} = &quota_admin($newportfolioquota,\%changeHash);
                   2868:                     }
1.149     raeburn  2869:                 } else {
1.334     raeburn  2870:                     $changed{'quota'} = &quota_admin('',\%changeHash);
1.339     raeburn  2871:                     $newportfolioquota = $newdefquota;
1.334     raeburn  2872:                     $newisdefault = 1;
1.149     raeburn  2873:                 }
1.334     raeburn  2874:             } else {
                   2875:                 $oldisdefault = 1;
1.339     raeburn  2876:                 $oldportfolioquota = $olddefquota;
1.334     raeburn  2877:                 if ($env{'form.customquota'} == 1) {
                   2878:                     if ($env{'form.portfolioquota'} eq '') {
                   2879:                         $newportfolioquota = 0;
                   2880:                     } else {
                   2881:                         $newportfolioquota = $env{'form.portfolioquota'};
                   2882:                         $newportfolioquota =~ s/[^\d\.]//g;
                   2883:                     }
1.267     raeburn  2884:                     $changed{'quota'} = &quota_admin($newportfolioquota,\%changeHash);
1.334     raeburn  2885:                 } else {
1.339     raeburn  2886:                     $newportfolioquota = $newdefquota;
1.334     raeburn  2887:                     $newisdefault = 1;
1.134     raeburn  2888:                 }
                   2889:             }
1.334     raeburn  2890:             if ($oldisdefault) {
1.339     raeburn  2891:                 $oldsettingstext{'quota'} = &get_defaultquota_text($oldsettingstatus);
1.334     raeburn  2892:             }
                   2893:             if ($newisdefault) {
1.339     raeburn  2894:                 $newsettingstext{'quota'} = &get_defaultquota_text($newsettingstatus);
1.334     raeburn  2895:             }
                   2896:             &tool_changes('tools',\@usertools,\%oldsettings,\%oldsettingstext,\%userenv,
                   2897:                           \%changeHash,\%changed,\%newsettings,\%newsettingstext);
                   2898:             if ($env{'form.ccdomain'} eq $env{'request.role.domain'}) {
                   2899:                 &tool_changes('requestcourses',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   2900:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.362     raeburn  2901:                 &tool_changes('requestauthor',\@requestauthor,\%oldsettings,\%oldsettingstext,\%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  2902:             } else {
1.334     raeburn  2903:                 &tool_changes('reqcrsotherdom',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   2904:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  2905:             }
                   2906:         }
1.334     raeburn  2907:         foreach my $item (@userinfo) {
                   2908:             if ($env{'form.c'.$item} ne $userenv{$item}) {
                   2909:                 $namechanged{$item} = 1;
                   2910:             }
1.204     raeburn  2911:         }
1.334     raeburn  2912:         $oldsettings{'quota'} = $oldportfolioquota.' Mb';
                   2913:         $newsettings{'quota'} = $newportfolioquota.' Mb';
                   2914:         if ((keys(%namechanged) > 0) || (keys(%changed) > 0)) {
1.267     raeburn  2915:             my ($chgresult,$namechgresult);
                   2916:             if (keys(%changed) > 0) {
                   2917:                 $chgresult = 
1.204     raeburn  2918:                     &Apache::lonnet::put('environment',\%changeHash,
                   2919:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
1.267     raeburn  2920:                 if ($chgresult eq 'ok') {
                   2921:                     if (($env{'user.name'} eq $env{'form.ccuname'}) &&
                   2922:                         ($env{'user.domain'} eq $env{'form.ccdomain'})) {
1.270     raeburn  2923:                         my %newenvhash;
                   2924:                         foreach my $key (keys(%changed)) {
1.299     raeburn  2925:                             if (($key eq 'official') || ($key eq 'unofficial')
                   2926:                                 || ($key eq 'community')) {
1.279     raeburn  2927:                                 $newenvhash{'environment.requestcourses.'.$key} =
                   2928:                                     $changeHash{'requestcourses.'.$key};
1.362     raeburn  2929:                                 if ($changeHash{'requestcourses.'.$key}) {
1.332     raeburn  2930:                                     $newenvhash{'environment.canrequest.'.$key} = 1;
1.279     raeburn  2931:                                 } else {
                   2932:                                     $newenvhash{'environment.canrequest.'.$key} =
                   2933:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   2934:                                             $key,'reload','requestcourses');
                   2935:                                 }
1.362     raeburn  2936:                             } elsif ($key eq 'requestauthor') {
                   2937:                                 $newenvhash{'environment.'.$key} = $changeHash{$key};
                   2938:                                 if ($changeHash{$key}) {
                   2939:                                     $newenvhash{'environment.canrequest.author'} = 1;
                   2940:                                 } else {
                   2941:                                     $newenvhash{'environment.canrequest.author'} =
                   2942:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   2943:                                             $key,'reload','requestauthor');
                   2944:                                 }
1.275     raeburn  2945:                             } elsif ($key ne 'quota') {
1.270     raeburn  2946:                                 $newenvhash{'environment.tools.'.$key} = 
                   2947:                                     $changeHash{'tools.'.$key};
1.279     raeburn  2948:                                 if ($changeHash{'tools.'.$key} ne '') {
                   2949:                                     $newenvhash{'environment.availabletools.'.$key} =
                   2950:                                         $changeHash{'tools.'.$key};
                   2951:                                 } else {
                   2952:                                     $newenvhash{'environment.availabletools.'.$key} =
1.367     golterma 2953:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   2954:           $key,'reload','tools');
1.279     raeburn  2955:                                 }
1.270     raeburn  2956:                             }
                   2957:                         }
1.271     raeburn  2958:                         if (keys(%newenvhash)) {
                   2959:                             &Apache::lonnet::appenv(\%newenvhash);
                   2960:                         }
1.267     raeburn  2961:                     }
                   2962:                 }
1.204     raeburn  2963:             }
1.334     raeburn  2964:             if (keys(%namechanged) > 0) {
1.337     raeburn  2965:                 foreach my $field (@userinfo) {
                   2966:                     $changeHash{$field}  = $env{'form.c'.$field};
                   2967:                 }
                   2968: # Make the change
1.204     raeburn  2969:                 $namechgresult =
                   2970:                     &Apache::lonnet::modifyuser($env{'form.ccdomain'},
                   2971:                         $env{'form.ccuname'},$changeHash{'id'},undef,undef,
                   2972:                         $changeHash{'firstname'},$changeHash{'middlename'},
                   2973:                         $changeHash{'lastname'},$changeHash{'generation'},
1.337     raeburn  2974:                         $changeHash{'id'},undef,$changeHash{'permanentemail'},undef,\@userinfo);
1.220     raeburn  2975:                 %userupdate = (
                   2976:                                lastname   => $env{'form.clastname'},
                   2977:                                middlename => $env{'form.cmiddlename'},
                   2978:                                firstname  => $env{'form.cfirstname'},
                   2979:                                generation => $env{'form.cgeneration'},
                   2980:                                id         => $env{'form.cid'},
                   2981:                              );
1.204     raeburn  2982:             }
1.334     raeburn  2983:             if (((keys(%namechanged) > 0) && $namechgresult eq 'ok') || 
1.267     raeburn  2984:                 ((keys(%changed) > 0) && $chgresult eq 'ok')) {
1.28      matthew  2985:             # Tell the user we changed the name
1.334     raeburn  2986:                 &display_userinfo($r,1,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  2987:                                   \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,
1.334     raeburn  2988:                                   \%oldsettings, \%oldsettingstext,\%newsettings,
                   2989:                                   \%newsettingstext);
1.203     raeburn  2990:                 if ($env{'form.cid'} ne $userenv{'id'}) {
                   2991:                     &Apache::lonnet::idput($env{'form.ccdomain'},
                   2992:                          ($env{'form.ccuname'} => $env{'form.cid'}));
                   2993:                     if (($recurseid) &&
                   2994:                         (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
                   2995:                         my $idresult = 
                   2996:                             &Apache::lonuserutils::propagate_id_change(
                   2997:                                 $env{'form.ccuname'},$env{'form.ccdomain'},
                   2998:                                 \%userupdate);
                   2999:                         $r->print('<br />'.$idresult.'<br />');
                   3000:                     }
1.196     raeburn  3001:                 }
1.149     raeburn  3002:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
                   3003:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
                   3004:                     my %newenvhash;
                   3005:                     foreach my $key (keys(%changeHash)) {
                   3006:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3007:                     }
1.238     raeburn  3008:                     &Apache::lonnet::appenv(\%newenvhash);
1.149     raeburn  3009:                 }
1.28      matthew  3010:             } else { # error occurred
1.188     raeburn  3011:                 $r->print('<span class="LC_error">'.&mt('Unable to successfully change environment for').' '.
                   3012:                       $env{'form.ccuname'}.' '.&mt('in domain').' '.
1.206     raeburn  3013:                       $env{'form.ccdomain'}.'</span><br />');
1.28      matthew  3014:             }
1.334     raeburn  3015:         } else { # End of if ($env ... ) logic
1.275     raeburn  3016:             # They did not want to change the users name, quota, tool availability,
                   3017:             # or ability to request creation of courses, 
1.267     raeburn  3018:             # but we can still tell them what the name and quota and availabilities are  
1.334     raeburn  3019:             &display_userinfo($r,undef,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3020:                               \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,\%oldsettings,
1.334     raeburn  3021:                               \%oldsettingstext,\%newsettings,\%newsettingstext);
1.28      matthew  3022:         }
1.206     raeburn  3023:         if (@mod_disallowed) {
                   3024:             my ($rolestr,$contextname);
                   3025:             if (@longroles > 0) {
                   3026:                 $rolestr = join(', ',@longroles);
                   3027:             } else {
                   3028:                 $rolestr = &mt('No roles');
                   3029:             }
                   3030:             if ($context eq 'course') {
                   3031:                 $contextname = &mt('course');
                   3032:             } elsif ($context eq 'author') {
                   3033:                 $contextname = &mt('co-author');
                   3034:             }
                   3035:             $r->print(&mt('The following fields were not updated: ').'<ul>');
                   3036:             my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   3037:             foreach my $field (@mod_disallowed) {
                   3038:                 $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
                   3039:             }
1.207     raeburn  3040:             $r->print('</ul>');
                   3041:             if (@mod_disallowed == 1) {
                   3042:                 $r->print(&mt("You do not have the authority to change this field given the user's current set of active/future [_1] roles:",$contextname));
                   3043:             } else {
                   3044:                 $r->print(&mt("You do not have the authority to change these fields given the user's current set of active/future [_1] roles:",$contextname));
                   3045:             }
1.292     bisitz   3046:             my $helplink = 'javascript:helpMenu('."'display'".')';
                   3047:             $r->print('<span class="LC_cusr_emph">'.$rolestr.'</span><br />'
                   3048:                      .&mt('Please contact your [_1]helpdesk[_2] for more information.'
                   3049:                          ,'<a href="'.$helplink.'">','</a>')
                   3050:                       .'<br />');
1.206     raeburn  3051:         }
1.259     bisitz   3052:         $r->print('<span class="LC_warning">'
                   3053:                   .$no_forceid_alert
                   3054:                   .&Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules)
                   3055:                   .'</span>');
1.4       www      3056:     }
1.367     golterma 3057:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.220     raeburn  3058:     if ($env{'form.action'} eq 'singlestudent') {
1.375     raeburn  3059:         &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context,
                   3060:                                $crstype,$showcredits,$defaultcredits);
1.318     raeburn  3061:         $r->print('<p><a href="javascript:backPage(document.userupdate)">');
                   3062:         if ($crstype eq 'Community') {
                   3063:             $r->print(&mt('Enroll Another Member'));
                   3064:         } else {
                   3065:             $r->print(&mt('Enroll Another Student'));
                   3066:         }
                   3067:         $r->print('</a></p>');
1.220     raeburn  3068:     } else {
1.375     raeburn  3069:         my @rolechanges = &update_roles($r,$context,$showcredits);
1.334     raeburn  3070:         if (keys(%namechanged) > 0) {
1.220     raeburn  3071:             if ($context eq 'course') {
                   3072:                 if (@userroles > 0) {
1.225     raeburn  3073:                     if ((@rolechanges == 0) || 
                   3074:                         (!(grep(/^st$/,@rolechanges)))) {
                   3075:                         if (grep(/^st$/,@userroles)) {
                   3076:                             my $classlistupdated =
                   3077:                                 &Apache::lonuserutils::update_classlist($cdom,
1.220     raeburn  3078:                                               $cnum,$env{'form.ccdomain'},
                   3079:                                        $env{'form.ccuname'},\%userupdate);
1.225     raeburn  3080:                         }
1.220     raeburn  3081:                     }
                   3082:                 }
                   3083:             }
                   3084:         }
1.226     raeburn  3085:         my $userinfo = &Apache::loncommon::plainname($env{'form.ccuname'},
1.233     raeburn  3086:                                                      $env{'form.ccdomain'});
                   3087:         if ($env{'form.popup'}) {
                   3088:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   3089:         } else {
1.367     golterma 3090:             $r->print('<br />'.&Apache::lonhtmlcommon::actionbox(['<a href="javascript:backPage(document.userupdate,'."'$env{'form.prevphase'}','modify'".')">'
                   3091:                      .&mt('Modify this user: [_1]','<span class="LC_cusr_emph">'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.' ('.$userinfo.')</span>').'</a>',
                   3092:                      '<a href="javascript:backPage(document.userupdate)">'.&mt('Create/Modify Another User').'</a>']));
1.233     raeburn  3093:         }
1.220     raeburn  3094:     }
                   3095: }
                   3096: 
1.334     raeburn  3097: sub display_userinfo {
1.362     raeburn  3098:     my ($r,$changed,$order,$canshow,$requestcourses,$usertools,$requestauthor,
                   3099:         $userenv,$changedhash,$namechangedhash,$oldsetting,$oldsettingtext,
1.334     raeburn  3100:         $newsetting,$newsettingtext) = @_;
                   3101:     return unless (ref($order) eq 'ARRAY' &&
                   3102:                    ref($canshow) eq 'HASH' && 
                   3103:                    ref($requestcourses) eq 'ARRAY' && 
1.362     raeburn  3104:                    ref($requestauthor) eq 'ARRAY' &&
1.334     raeburn  3105:                    ref($usertools) eq 'ARRAY' && 
                   3106:                    ref($userenv) eq 'HASH' &&
                   3107:                    ref($changedhash) eq 'HASH' &&
                   3108:                    ref($oldsetting) eq 'HASH' &&
                   3109:                    ref($oldsettingtext) eq 'HASH' &&
                   3110:                    ref($newsetting) eq 'HASH' &&
                   3111:                    ref($newsettingtext) eq 'HASH');
                   3112:     my %lt=&Apache::lonlocal::texthash(
1.372     raeburn  3113:          'ui'             => 'User Information',
1.334     raeburn  3114:          'uic'            => 'User Information Changed',
                   3115:          'firstname'      => 'First Name',
                   3116:          'middlename'     => 'Middle Name',
                   3117:          'lastname'       => 'Last Name',
                   3118:          'generation'     => 'Generation',
                   3119:          'id'             => 'Student/Employee ID',
                   3120:          'permanentemail' => 'Permanent e-mail address',
                   3121:          'quota'          => 'Disk space allocated to portfolio files',
                   3122:          'blog'           => 'Blog Availability',
1.361     raeburn  3123:          'webdav'         => 'WebDAV Availability',
1.334     raeburn  3124:          'aboutme'        => 'Personal Information Page Availability',
                   3125:          'portfolio'      => 'Portfolio Availability',
                   3126:          'official'       => 'Can Request Official Courses',
                   3127:          'unofficial'     => 'Can Request Unofficial Courses',
                   3128:          'community'      => 'Can Request Communities',
1.362     raeburn  3129:          'requestauthor'  => 'Can Request Author Role',
1.334     raeburn  3130:          'inststatus'     => "Affiliation",
                   3131:          'prvs'           => 'Previous Value:',
                   3132:          'chto'           => 'Changed To:'
                   3133:     );
                   3134:     if ($changed) {
1.372     raeburn  3135:         $r->print('<h3>'.$lt{'uic'}.'</h3>'.
1.367     golterma 3136:                 &Apache::loncommon::start_data_table().
                   3137:                 &Apache::loncommon::start_data_table_header_row());
1.334     raeburn  3138:         $r->print("<th>&nbsp;</th>\n");
1.367     golterma 3139:         $r->print('<th><b>'.$lt{'prvs'}.'</b></th>');
                   3140:         $r->print('<th><span class="LC_nobreak"><b>'.$lt{'chto'}.'</b></span></th>');
                   3141:         $r->print(&Apache::loncommon::end_data_table_header_row());
                   3142:         my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
                   3143:         
                   3144: 
1.334     raeburn  3145:         foreach my $item (@userinfo) {
                   3146:             my $value = $env{'form.c'.$item};
1.367     golterma 3147:             #show changes only:
                   3148:             unless($value eq $userenv->{$item}){
                   3149:                 $r->print(&Apache::loncommon::start_data_table_row());
                   3150: 
                   3151:                 $r->print("<td>$lt{$item}</td>\n");
                   3152:                 $r->print('<td>'.$userenv->{$item}.' </td>');
                   3153:                 $r->print("<td>$value </td>\n");
                   3154: 
                   3155:                 $r->print(&Apache::loncommon::end_data_table_row());
1.334     raeburn  3156:             }
                   3157:         }
                   3158:         foreach my $entry (@{$order}) {
1.367     golterma 3159:             if ($canshow->{$entry} && ($newsetting->{$entry} ne $newsetting->{$entry})) {
                   3160:                 $r->print(&Apache::loncommon::start_data_table_row());
1.334     raeburn  3161:                 if (($entry eq 'requestcourses') || ($entry eq 'reqcrsotherdom')) {
                   3162:                     foreach my $item (@{$requestcourses}) {
1.367     golterma 3163:                         $r->print("<td>$lt{$item}</td>\n");
                   3164:                         $r->print("<td>$oldsetting->{$item} $oldsettingtext->{$item}</td>\n");
1.334     raeburn  3165:                         my $value = $newsetting->{$item}.' '.$newsettingtext->{$item};
                   3166:                         if ($changedhash->{$item}) {
                   3167:                             $value = '<span class="LC_cusr_emph">'.$value.'</span>';
                   3168:                         }
                   3169:                         $r->print("<td>$value </td>\n");
                   3170:                     }
                   3171:                 } elsif ($entry eq 'tools') {
                   3172:                     foreach my $item (@{$usertools}) {
1.367     golterma 3173:                         $r->print("<td>$lt{$item}</td>\n");
                   3174:                         $r->print("<td>$oldsetting->{$item} $oldsettingtext->{$item}</td>\n");
1.334     raeburn  3175:                         my $value = $newsetting->{$item}.' '.$newsettingtext->{$item};
                   3176:                         if ($changedhash->{$item}) {
                   3177:                             $value = '<span class="LC_cusr_emph">'.$value.'</span>';
                   3178:                         }
                   3179:                         $r->print("<td>$value </td>\n");
                   3180:                     }
                   3181:                 } else {
1.367     golterma 3182:                     $r->print("<td>$lt{$entry}</td>\n");
                   3183:                     $r->print("<td>$oldsetting->{$entry} $oldsettingtext->{$entry} </td>\n");
1.334     raeburn  3184:                     my $value = $newsetting->{$entry}.' '.$newsettingtext->{$entry};
                   3185:                     if ($changedhash->{$entry}) {
                   3186:                         $value = '<span class="LC_cusr_emph">'.$value.'</span>';
                   3187:                     }
                   3188:                     $r->print("<td>$value </td>\n");
                   3189:                 }
1.367     golterma 3190:                 $r->print(&Apache::loncommon::end_data_table_row());
1.334     raeburn  3191:             }
                   3192:         }
1.367     golterma 3193:         $r->print(&Apache::loncommon::end_data_table().'<br />');
1.372     raeburn  3194:     } else {
                   3195:         $r->print('<h3>'.$lt{'ui'}.'</h3>'.
                   3196:                   '<p>'.&mt('No changes made to user information').'</p>');
1.334     raeburn  3197:     }
                   3198:     return;
                   3199: }
                   3200: 
1.275     raeburn  3201: sub tool_changes {
                   3202:     my ($context,$usertools,$oldaccess,$oldaccesstext,$userenv,$changeHash,
                   3203:         $changed,$newaccess,$newaccesstext) = @_;
                   3204:     if (!((ref($usertools) eq 'ARRAY') && (ref($oldaccess) eq 'HASH') &&
                   3205:           (ref($oldaccesstext) eq 'HASH') && (ref($userenv) eq 'HASH') &&
                   3206:           (ref($changeHash) eq 'HASH') && (ref($changed) eq 'HASH') &&
                   3207:           (ref($newaccess) eq 'HASH') && (ref($newaccesstext) eq 'HASH'))) {
                   3208:         return;
                   3209:     }
1.300     raeburn  3210:     if ($context eq 'reqcrsotherdom') {
1.309     raeburn  3211:         my @options = ('approval','validate','autolimit');
1.306     raeburn  3212:         my $optregex = join('|',@options);
                   3213:         my %reqdisplay = &courserequest_display();
1.300     raeburn  3214:         my $cdom = $env{'request.role.domain'};
                   3215:         foreach my $tool (@{$usertools}) {
1.314     raeburn  3216:             $oldaccesstext->{$tool} = &mt('No');
                   3217:             $newaccesstext->{$tool} = $oldaccesstext->{$tool};
1.300     raeburn  3218:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
1.314     raeburn  3219:             my $newop;
                   3220:             if ($env{'form.'.$context.'_'.$tool}) {
                   3221:                 $newop = $env{'form.'.$context.'_'.$tool};
                   3222:                 if ($newop eq 'autolimit') {
                   3223:                     my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3224:                     $limit =~ s/\D+//g;
                   3225:                     $newop .= '='.$limit;
                   3226:                 }
                   3227:             }
1.300     raeburn  3228:             if ($userenv->{$context.'.'.$tool} eq '') {
1.314     raeburn  3229:                 if ($newop) {
                   3230:                     $changed->{$tool}=&tool_admin($tool,$cdom.':'.$newop,
1.300     raeburn  3231:                                                   $changeHash,$context);
                   3232:                     if ($changed->{$tool}) {
1.314     raeburn  3233:                         $newaccesstext->{$tool} = &mt('Yes');
1.300     raeburn  3234:                     } else {
                   3235:                         $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3236:                     }
                   3237:                 }
                   3238:             } else {
                   3239:                 my @curr = split(',',$userenv->{$context.'.'.$tool});
                   3240:                 my @new;
                   3241:                 my $changedoms;
1.314     raeburn  3242:                 foreach my $req (@curr) {
                   3243:                     if ($req =~ /^\Q$cdom\E\:($optregex\=?\d*)$/) {
                   3244:                         $oldaccesstext->{$tool} = &mt('Yes');
                   3245:                         my $oldop = $1;
                   3246:                         if ($oldop ne $newop) {
                   3247:                             $changedoms = 1;
                   3248:                             foreach my $item (@curr) {
                   3249:                                 my ($reqdom,$option) = split(':',$item);
                   3250:                                 unless ($reqdom eq $cdom) {
                   3251:                                     push(@new,$item);
                   3252:                                 }
                   3253:                             }
                   3254:                             if ($newop) {
                   3255:                                 push(@new,$cdom.':'.$newop);
1.300     raeburn  3256:                             }
1.314     raeburn  3257:                             @new = sort(@new);
1.300     raeburn  3258:                         }
1.314     raeburn  3259:                         last;
1.300     raeburn  3260:                     }
1.314     raeburn  3261:                 }
                   3262:                 if ((!$changedoms) && ($newop)) {
1.300     raeburn  3263:                     $changedoms = 1;
1.306     raeburn  3264:                     @new = sort(@curr,$cdom.':'.$newop);
1.300     raeburn  3265:                 }
                   3266:                 if ($changedoms) {
1.314     raeburn  3267:                     my $newdomstr;
1.300     raeburn  3268:                     if (@new) {
                   3269:                         $newdomstr = join(',',@new);
                   3270:                     }
                   3271:                     $changed->{$tool}=&tool_admin($tool,$newdomstr,$changeHash,
                   3272:                                                   $context);
                   3273:                     if ($changed->{$tool}) {
                   3274:                         if ($env{'form.'.$context.'_'.$tool}) {
1.306     raeburn  3275:                             if ($env{'form.'.$context.'_'.$tool} eq 'autolimit') {
1.314     raeburn  3276:                                 my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3277:                                 $limit =~ s/\D+//g;
                   3278:                                 if ($limit) {
                   3279:                                     $newaccesstext->{$tool} = &mt('Yes, up to limit of [quant,_1,request] per user.',$limit);
                   3280:                                 } else {
1.306     raeburn  3281:                                     $newaccesstext->{$tool} = &mt('Yes, processed automatically');
                   3282:                                 }
1.314     raeburn  3283:                             } else {
1.306     raeburn  3284:                                 $newaccesstext->{$tool} = $reqdisplay{$env{'form.'.$context.'_'.$tool}};
                   3285:                             }
1.300     raeburn  3286:                         } else {
1.306     raeburn  3287:                             $newaccesstext->{$tool} = &mt('No');
1.300     raeburn  3288:                         }
                   3289:                     }
                   3290:                 }
                   3291:             }
                   3292:         }
                   3293:         return;
                   3294:     }
1.275     raeburn  3295:     foreach my $tool (@{$usertools}) {
1.362     raeburn  3296:         my ($newval,$envkey);
                   3297:         $envkey = $context.'.'.$tool;
1.306     raeburn  3298:         if ($context eq 'requestcourses') {
                   3299:             $newval = $env{'form.crsreq_'.$tool};
                   3300:             if ($newval eq 'autolimit') {
                   3301:                 $newval .= '='.$env{'form.crsreq_'.$tool.'_limit'};
                   3302:             }
1.362     raeburn  3303:         } elsif ($context eq 'requestauthor') {
                   3304:             $newval = $env{'form.'.$context};
                   3305:             $envkey = $context;
1.314     raeburn  3306:         } else {
1.306     raeburn  3307:             $newval = $env{'form.'.$context.'_'.$tool};
                   3308:         }
1.362     raeburn  3309:         if ($userenv->{$envkey} ne '') {
1.275     raeburn  3310:             $oldaccess->{$tool} = &mt('custom');
1.362     raeburn  3311:             if ($userenv->{$envkey}) {
1.275     raeburn  3312:                 $oldaccesstext->{$tool} = &mt("availability set to 'on'");
                   3313:             } else {
                   3314:                 $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3315:             }
1.362     raeburn  3316:             $changeHash->{$envkey} = $userenv->{$envkey};
1.275     raeburn  3317:             if ($env{'form.custom'.$tool} == 1) {
1.362     raeburn  3318:                 if ($newval ne $userenv->{$envkey}) {
1.306     raeburn  3319:                     $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3320:                                                     $context);
1.275     raeburn  3321:                     if ($changed->{$tool}) {
                   3322:                         $newaccess->{$tool} = &mt('custom');
1.306     raeburn  3323:                         if ($newval) {
1.275     raeburn  3324:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3325:                         } else {
                   3326:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3327:                         }
                   3328:                     } else {
                   3329:                         $newaccess->{$tool} = $oldaccess->{$tool};
                   3330:                         if ($userenv->{$context.'.'.$tool}) {
                   3331:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3332:                         } else {
                   3333:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3334:                         }
                   3335:                     }
                   3336:                 } else {
                   3337:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3338:                     $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3339:                 }
                   3340:             } else {
                   3341:                 $changed->{$tool} = &tool_admin($tool,'',$changeHash,$context);
                   3342:                 if ($changed->{$tool}) {
                   3343:                     $newaccess->{$tool} = &mt('default');
                   3344:                 } else {
                   3345:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3346:                     if ($userenv->{$context.'.'.$tool}) {
1.300     raeburn  3347:                         $newaccesstext->{$tool} = &mt("availability set to 'on'");
1.275     raeburn  3348:                     } else {
1.300     raeburn  3349:                         $newaccesstext->{$tool} = &mt("availability set to 'off'");
1.275     raeburn  3350:                     }
                   3351:                 }
                   3352:             }
                   3353:         } else {
                   3354:             $oldaccess->{$tool} = &mt('default');
                   3355:             if ($env{'form.custom'.$tool} == 1) {
1.306     raeburn  3356:                 $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3357:                                                 $context);
1.275     raeburn  3358:                 if ($changed->{$tool}) {
                   3359:                     $newaccess->{$tool} = &mt('custom');
1.306     raeburn  3360:                     if ($newval) {
1.275     raeburn  3361:                         $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3362:                     } else {
                   3363:                         $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3364:                     }
                   3365:                 } else {
                   3366:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3367:                 }
                   3368:             } else {
                   3369:                 $newaccess->{$tool} = $oldaccess->{$tool};
                   3370:             }
                   3371:         }
                   3372:     }
                   3373:     return;
                   3374: }
                   3375: 
1.220     raeburn  3376: sub update_roles {
1.375     raeburn  3377:     my ($r,$context,$showcredits) = @_;
1.4       www      3378:     my $now=time;
1.225     raeburn  3379:     my @rolechanges;
1.220     raeburn  3380:     my %disallowed;
1.73      sakharuk 3381:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
1.135     raeburn  3382:     foreach my $key (keys (%env)) {
                   3383: 	next if (! $env{$key});
1.190     raeburn  3384:         next if ($key eq 'form.action');
1.27      matthew  3385: 	# Revoke roles
1.135     raeburn  3386: 	if ($key=~/^form\.rev/) {
                   3387: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
1.64      www      3388: # Revoke standard role
1.170     albertel 3389: 		my ($scope,$role) = ($1,$2);
                   3390: 		my $result =
                   3391: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
                   3392: 						$env{'form.ccuname'},
1.239     raeburn  3393: 						$scope,$role,'','',$context);
1.367     golterma 3394:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3395:                             &mt('Revoking [_1] in [_2]',
                   3396:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  3397:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   3398:                                 $result ne "ok").'<br />');
                   3399:                 if ($result ne "ok") {
                   3400:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3401:                 }
1.170     albertel 3402: 		if ($role eq 'st') {
1.202     raeburn  3403: 		    my $result = 
1.198     raeburn  3404:                         &Apache::lonuserutils::classlist_drop($scope,
                   3405:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3406: 			    $now);
1.367     golterma 3407:                     $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.53      www      3408: 		}
1.225     raeburn  3409:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3410:                     push(@rolechanges,$role);
                   3411:                 }
1.196     raeburn  3412: 	    }
1.195     raeburn  3413: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}s) {
1.64      www      3414: # Revoke custom role
1.369     bisitz   3415:                 my $result = &Apache::lonnet::revokecustomrole(
                   3416:                     $env{'form.ccdomain'},$env{'form.ccuname'},$1,$2,$3,$4,'','',$context);
1.367     golterma 3417:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3418:                             &mt('Revoking custom role [_1] by [_2] in [_3]',
1.372     raeburn  3419:                                 $4,$3.':'.$2,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3420:                             $result ne 'ok').'<br />');
                   3421:                 if ($result ne "ok") {
                   3422:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3423:                 }
1.225     raeburn  3424:                 if (!grep(/^cr$/,@rolechanges)) {
                   3425:                     push(@rolechanges,'cr');
                   3426:                 }
1.64      www      3427: 	    }
1.135     raeburn  3428: 	} elsif ($key=~/^form\.del/) {
                   3429: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
1.116     raeburn  3430: # Delete standard role
1.170     albertel 3431: 		my ($scope,$role) = ($1,$2);
                   3432: 		my $result =
                   3433: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
                   3434: 						$env{'form.ccuname'},
1.239     raeburn  3435: 						$scope,$role,$now,0,1,'',
                   3436:                                                 $context);
1.367     golterma 3437:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   3438:                             &mt('Deleting [_1] in [_2]',
1.369     bisitz   3439:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  3440:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   3441:                             $result ne 'ok').'<br />');
                   3442:                 if ($result ne "ok") {
                   3443:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3444:                 }
1.367     golterma 3445: 
1.170     albertel 3446: 		if ($role eq 'st') {
1.202     raeburn  3447: 		    my $result = 
1.198     raeburn  3448:                         &Apache::lonuserutils::classlist_drop($scope,
                   3449:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3450: 			    $now);
1.369     bisitz   3451: 		    $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.81      albertel 3452: 		}
1.225     raeburn  3453:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3454:                     push(@rolechanges,$role);
                   3455:                 }
1.116     raeburn  3456:             }
1.139     albertel 3457: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3458:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3459: # Delete custom role
1.369     bisitz   3460:                 my $result =
                   3461:                     &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
                   3462:                         $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
                   3463:                         0,1,$context);
                   3464:                 $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Deleting custom role [_1] by [_2] in [_3]',
1.372     raeburn  3465:                       $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3466:                       $result ne "ok").'<br />');
                   3467:                 if ($result ne "ok") {
                   3468:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3469:                 }
1.367     golterma 3470: 
1.225     raeburn  3471:                 if (!grep(/^cr$/,@rolechanges)) {
                   3472:                     push(@rolechanges,'cr');
                   3473:                 }
1.116     raeburn  3474:             }
1.135     raeburn  3475: 	} elsif ($key=~/^form\.ren/) {
1.101     albertel 3476:             my $udom = $env{'form.ccdomain'};
                   3477:             my $uname = $env{'form.ccuname'};
1.116     raeburn  3478: # Re-enable standard role
1.135     raeburn  3479: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
1.89      raeburn  3480:                 my $url = $1;
                   3481:                 my $role = $2;
                   3482:                 my $logmsg;
                   3483:                 my $output;
                   3484:                 if ($role eq 'st') {
1.141     albertel 3485:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
1.374     raeburn  3486:                         my ($cdom,$cnum,$csec) = ($1,$2,$3);
1.375     raeburn  3487:                         my $credits;
                   3488:                         if ($showcredits) {
                   3489:                             my $defaultcredits = 
                   3490:                                 &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   3491:                             $credits = &get_user_credits($defaultcredits,$cdom,$cnum);
                   3492:                         }
                   3493:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$cdom,$cnum,$csec,$context,$credits);
1.220     raeburn  3494:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course') || ($result eq 'refused')) {
1.223     raeburn  3495:                             if ($result eq 'refused' && $logmsg) {
                   3496:                                 $output = $logmsg;
                   3497:                             } else { 
1.369     bisitz   3498:                                 $output = &mt('Error: [_1]',$result)."\n";
1.223     raeburn  3499:                             }
1.89      raeburn  3500:                         } else {
1.372     raeburn  3501:                             $output = &Apache::lonhtmlcommon::confirm_success(&mt('Assigning [_1] in [_2] starting [_3]',
                   3502:                                         &Apache::lonnet::plaintext($role),
                   3503:                                         &Apache::loncommon::show_role_extent($url,$context,'st'),
                   3504:                                         &Apache::lonlocal::locallocaltime($now))).'<br />'.$logmsg.'<br />';
1.89      raeburn  3505:                         }
                   3506:                     }
                   3507:                 } else {
1.101     albertel 3508: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
1.239     raeburn  3509:                                $env{'form.ccuname'},$url,$role,0,$now,'','',
                   3510:                                $context);
1.367     golterma 3511:                         $output = &Apache::lonhtmlcommon::confirm_success(&mt('Re-enabling [_1] in [_2]',
1.372     raeburn  3512:                                         &Apache::lonnet::plaintext($role),
                   3513:                                         &Apache::loncommon::show_role_extent($url,$context,$role)),$result ne "ok").'<br />';
1.369     bisitz   3514:                     if ($result ne "ok") {
                   3515:                         $output .= &mt('Error: [_1]',$result).'<br />';
                   3516:                     }
                   3517:                 }
1.89      raeburn  3518:                 $r->print($output);
1.225     raeburn  3519:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3520:                     push(@rolechanges,$role);
                   3521:                 }
1.113     raeburn  3522: 	    }
1.116     raeburn  3523: # Re-enable custom role
1.139     albertel 3524: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3525:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3526:                 my $result = &Apache::lonnet::assigncustomrole(
                   3527:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
1.240     raeburn  3528:                                $url,$rdom,$rnam,$rolename,0,$now,undef,$context);
1.369     bisitz   3529:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   3530:                     &mt('Re-enabling custom role [_1] by [_2] in [_3]',
1.372     raeburn  3531:                         $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3532:                     $result ne "ok").'<br />');
                   3533:                 if ($result ne "ok") {
                   3534:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3535:                 }
1.225     raeburn  3536:                 if (!grep(/^cr$/,@rolechanges)) {
                   3537:                     push(@rolechanges,'cr');
                   3538:                 }
1.116     raeburn  3539:             }
1.135     raeburn  3540: 	} elsif ($key=~/^form\.act/) {
1.101     albertel 3541:             my $udom = $env{'form.ccdomain'};
                   3542:             my $uname = $env{'form.ccuname'};
1.141     albertel 3543: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
1.65      www      3544:                 # Activate a custom role
1.83      albertel 3545: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
                   3546: 		my $url='/'.$one.'/'.$two;
                   3547: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
1.65      www      3548: 
1.101     albertel 3549:                 my $start = ( $env{'form.start_'.$full} ?
                   3550:                               $env{'form.start_'.$full} :
1.88      raeburn  3551:                               $now );
1.101     albertel 3552:                 my $end   = ( $env{'form.end_'.$full} ?
                   3553:                               $env{'form.end_'.$full} :
1.88      raeburn  3554:                               0 );
                   3555:                                                                                      
                   3556:                 # split multiple sections
                   3557:                 my %sections = ();
1.101     albertel 3558:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
1.88      raeburn  3559:                 if ($num_sections == 0) {
1.240     raeburn  3560:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3561:                 } else {
1.114     albertel 3562: 		    my %curr_groups =
1.117     raeburn  3563: 			&Apache::longroup::coursegroups($one,$two);
1.113     raeburn  3564:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3565:                         if (($sec eq 'none') || ($sec eq 'all') || 
                   3566:                             exists($curr_groups{$sec})) {
                   3567:                             $disallowed{$sec} = $url;
                   3568:                             next;
                   3569:                         }
                   3570:                         my $securl = $url.'/'.$sec;
1.240     raeburn  3571: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3572:                     }
                   3573:                 }
1.225     raeburn  3574:                 if (!grep(/^cr$/,@rolechanges)) {
                   3575:                     push(@rolechanges,'cr');
                   3576:                 }
1.142     raeburn  3577: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
1.27      matthew  3578: 		# Activate roles for sections with 3 id numbers
                   3579: 		# set start, end times, and the url for the class
1.83      albertel 3580: 		my ($one,$two,$three)=($1,$2,$3);
1.101     albertel 3581: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
                   3582: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
1.27      matthew  3583: 			      $now );
1.101     albertel 3584: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
                   3585: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  3586: 			      0 );
1.83      albertel 3587: 		my $url='/'.$one.'/'.$two;
1.88      raeburn  3588:                 my $type = 'three';
                   3589:                 # split multiple sections
                   3590:                 my %sections = ();
1.101     albertel 3591:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
1.375     raeburn  3592:                 my $credits;
                   3593:                 if ($three eq 'st') {
                   3594:                     if ($showcredits) { 
                   3595:                         my $defaultcredits = 
                   3596:                             &Apache::lonuserutils::get_defaultcredits($one,$two);
                   3597:                         $credits = $env{'form.credits_'.$one.'_'.$two.'_'.$three};
                   3598:                         $credits =~ s/[^\d\.]//g;
                   3599:                         if ($credits eq $defaultcredits) {
                   3600:                             undef($credits);
                   3601:                         }
                   3602:                     }
                   3603:                 }
1.88      raeburn  3604:                 if ($num_sections == 0) {
1.375     raeburn  3605:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  3606:                 } else {
1.114     albertel 3607:                     my %curr_groups = 
1.117     raeburn  3608: 			&Apache::longroup::coursegroups($one,$two);
1.88      raeburn  3609:                     my $emptysec = 0;
                   3610:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3611:                         $sec =~ s/\W//g;
1.113     raeburn  3612:                         if ($sec ne '') {
                   3613:                             if (($sec eq 'none') || ($sec eq 'all') || 
                   3614:                                 exists($curr_groups{$sec})) {
                   3615:                                 $disallowed{$sec} = $url;
                   3616:                                 next;
                   3617:                             }
1.88      raeburn  3618:                             my $securl = $url.'/'.$sec;
1.375     raeburn  3619:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec,$context,$credits));
1.88      raeburn  3620:                         } else {
                   3621:                             $emptysec = 1;
                   3622:                         }
                   3623:                     }
                   3624:                     if ($emptysec) {
1.375     raeburn  3625:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  3626:                     }
1.225     raeburn  3627:                 }
                   3628:                 if (!grep(/^\Q$three\E$/,@rolechanges)) {
                   3629:                     push(@rolechanges,$three);
                   3630:                 }
1.135     raeburn  3631: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
1.27      matthew  3632: 		# Activate roles for sections with two id numbers
                   3633: 		# set start, end times, and the url for the class
1.101     albertel 3634: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
                   3635: 			      $env{'form.start_'.$1.'_'.$2} : 
1.27      matthew  3636: 			      $now );
1.101     albertel 3637: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
                   3638: 			      $env{'form.end_'.$1.'_'.$2} :
1.27      matthew  3639: 			      0 );
1.225     raeburn  3640:                 my $one = $1;
                   3641:                 my $two = $2;
                   3642: 		my $url='/'.$one.'/';
1.88      raeburn  3643:                 # split multiple sections
                   3644:                 my %sections = ();
1.225     raeburn  3645:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two},\%sections,$two);
1.88      raeburn  3646:                 if ($num_sections == 0) {
1.240     raeburn  3647:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  3648:                 } else {
                   3649:                     my $emptysec = 0;
                   3650:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3651:                         if ($sec ne '') {
                   3652:                             my $securl = $url.'/'.$sec;
1.240     raeburn  3653:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$two,$start,$end,$one,undef,$sec,$context));
1.88      raeburn  3654:                         } else {
                   3655:                             $emptysec = 1;
                   3656:                         }
                   3657:                     }
                   3658:                     if ($emptysec) {
1.240     raeburn  3659:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  3660:                     }
                   3661:                 }
1.225     raeburn  3662:                 if (!grep(/^\Q$two\E$/,@rolechanges)) {
                   3663:                     push(@rolechanges,$two);
                   3664:                 }
1.64      www      3665: 	    } else {
1.190     raeburn  3666: 		$r->print('<p><span class="LC_error">'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></span></p><br />');
1.64      www      3667:             }
1.113     raeburn  3668:             foreach my $key (sort(keys(%disallowed))) {
1.274     bisitz   3669:                 $r->print('<p class="LC_warning">');
1.113     raeburn  3670:                 if (($key eq 'none') || ($key eq 'all')) {  
1.274     bisitz   3671:                     $r->print(&mt('[_1] may not be used as the name for a section, as it is a reserved word.','<tt>'.$key.'</tt>'));
1.113     raeburn  3672:                 } else {
1.274     bisitz   3673:                     $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>'));
1.113     raeburn  3674:                 }
1.274     bisitz   3675:                 $r->print('</p><p>'
                   3676:                          .&mt('Please [_1]go back[_2] and choose a different section name.'
                   3677:                              ,'<a href="javascript:history.go(-1)'
                   3678:                              ,'</a>')
                   3679:                          .'</p><br />'
                   3680:                 );
1.113     raeburn  3681:             }
                   3682: 	}
1.101     albertel 3683:     } # End of foreach (keys(%env))
1.75      www      3684: # Flush the course logs so reverse user roles immediately updated
1.349     raeburn  3685:     $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.225     raeburn  3686:     if (@rolechanges == 0) {
1.372     raeburn  3687:         $r->print('<p>'.&mt('No roles to modify').'</p>');
1.193     raeburn  3688:     }
1.225     raeburn  3689:     return @rolechanges;
1.220     raeburn  3690: }
                   3691: 
1.375     raeburn  3692: sub get_user_credits {
                   3693:     my ($uname,$udom,$defaultcredits,$cdom,$cnum) = @_;
                   3694:     if ($cdom eq '' || $cnum eq '') {
                   3695:         return unless ($env{'request.course.id'});
                   3696:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   3697:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   3698:     }
                   3699:     my $credits;
                   3700:     my %currhash =
                   3701:         &Apache::lonnet::get('classlist',[$uname.':'.$udom],$cdom,$cnum);
                   3702:     if (keys(%currhash) > 0) {
                   3703:         my @items = split(/:/,$currhash{$uname.':'.$udom});
                   3704:         my $crdidx = &Apache::loncoursedata::CL_CREDITS() - 3;
                   3705:         $credits = $items[$crdidx];
                   3706:         $credits =~ s/[^\d\.]//g;
                   3707:     }
                   3708:     if ($credits eq $defaultcredits) {
                   3709:         undef($credits);
                   3710:     }
                   3711:     return $credits;
                   3712: }
                   3713: 
1.220     raeburn  3714: sub enroll_single_student {
1.375     raeburn  3715:     my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype,
                   3716:         $showcredits,$defaultcredits) = @_;
1.318     raeburn  3717:     $r->print('<h3>');
                   3718:     if ($crstype eq 'Community') {
                   3719:         $r->print(&mt('Enrolling Member'));
                   3720:     } else {
                   3721:         $r->print(&mt('Enrolling Student'));
                   3722:     }
                   3723:     $r->print('</h3>');
1.220     raeburn  3724: 
                   3725:     # Remove non alphanumeric values from section
                   3726:     $env{'form.sections'}=~s/\W//g;
                   3727: 
1.375     raeburn  3728:     my $credits;
                   3729:     if (($showcredits) && ($env{'form.credits'} ne '')) {
                   3730:         $credits = $env{'form.credits'};
                   3731:         $credits =~ s/[^\d\.]//g;
                   3732:         if ($credits ne '') {
                   3733:             if ($credits eq $defaultcredits) {
                   3734:                 undef($credits);
                   3735:             }
                   3736:         }
                   3737:     }
                   3738: 
1.220     raeburn  3739:     # Clean out any old student roles the user has in this class.
                   3740:     &Apache::lonuserutils::modifystudent($env{'form.ccdomain'},
                   3741:          $env{'form.ccuname'},$env{'request.course.id'},undef,$uhome);
                   3742:     my ($startdate,$enddate) = &Apache::lonuserutils::get_dates_from_form();
                   3743:     my $enroll_result =
                   3744:         &Apache::lonnet::modify_student_enrollment($env{'form.ccdomain'},
                   3745:             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
                   3746:             $env{'form.cmiddlename'},$env{'form.clastname'},
                   3747:             $env{'form.generation'},$env{'form.sections'},$enddate,
1.375     raeburn  3748:             $startdate,'manual',undef,$env{'request.course.id'},'',$context,
                   3749:             $credits);
1.220     raeburn  3750:     if ($enroll_result =~ /^ok/) {
                   3751:         $r->print(&mt('<b>[_1]</b> enrolled',$env{'form.ccuname'}.':'.$env{'form.ccdomain'}));
                   3752:         if ($env{'form.sections'} ne '') {
                   3753:             $r->print(' '.&mt('in section [_1]',$env{'form.sections'}));
                   3754:         }
                   3755:         my ($showstart,$showend);
                   3756:         if ($startdate <= $now) {
                   3757:             $showstart = &mt('Access starts immediately');
                   3758:         } else {
                   3759:             $showstart = &mt('Access starts: ').&Apache::lonlocal::locallocaltime($startdate);
                   3760:         }
                   3761:         if ($enddate == 0) {
                   3762:             $showend = &mt('ends: no ending date');
                   3763:         } else {
                   3764:             $showend = &mt('ends: ').&Apache::lonlocal::locallocaltime($enddate);
                   3765:         }
                   3766:         $r->print('.<br />'.$showstart.'; '.$showend);
                   3767:         if ($startdate <= $now && !$newuser) {
1.318     raeburn  3768:             $r->print('<p> ');
                   3769:             if ($crstype eq 'Community') {
                   3770:                 $r->print(&mt('If the member is currently logged-in to LON-CAPA, the new role will be available when the member next logs in.'));
                   3771:             } else {
                   3772:                 $r->print(&mt('If the student is currently logged-in to LON-CAPA, the new role will be available when the student next logs in.'));
                   3773:            }
                   3774:            $r->print('</p>');
1.220     raeburn  3775:         }
                   3776:     } else {
                   3777:         $r->print(&mt('unable to enroll').": ".$enroll_result);
                   3778:     }
                   3779:     return;
1.188     raeburn  3780: }
                   3781: 
1.204     raeburn  3782: sub get_defaultquota_text {
                   3783:     my ($settingstatus) = @_;
                   3784:     my $defquotatext; 
                   3785:     if ($settingstatus eq '') {
                   3786:         $defquotatext = &mt('(default)');
                   3787:     } else {
                   3788:         my ($usertypes,$order) =
                   3789:             &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
                   3790:         if ($usertypes->{$settingstatus} eq '') {
                   3791:             $defquotatext = &mt('(default)');
                   3792:         } else {
                   3793:             $defquotatext = &mt('(default for [_1])',$usertypes->{$settingstatus});
                   3794:         }
                   3795:     }
                   3796:     return $defquotatext;
                   3797: }
                   3798: 
1.188     raeburn  3799: sub update_result_form {
                   3800:     my ($uhome) = @_;
                   3801:     my $outcome = 
1.367     golterma 3802:     '<form name="userupdate" method="post" action="">'."\n";
1.160     raeburn  3803:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
1.188     raeburn  3804:         $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  3805:     }
1.207     raeburn  3806:     if ($env{'form.origname'} ne '') {
                   3807:         $outcome .= '<input type="hidden" name="origname" value="'.$env{'form.origname'}.'" />'."\n";
                   3808:     }
1.160     raeburn  3809:     foreach my $item ('sortby','seluname','seludom') {
                   3810:         if (exists($env{'form.'.$item})) {
1.188     raeburn  3811:             $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  3812:         }
                   3813:     }
1.188     raeburn  3814:     if ($uhome eq 'no_host') {
                   3815:         $outcome .= '<input type="hidden" name="forcenewuser" value="1" />'."\n";
                   3816:     }
                   3817:     $outcome .= '<input type="hidden" name="phase" value="" />'."\n".
                   3818:                 '<input type ="hidden" name="currstate" value="" />'."\n".
1.220     raeburn  3819:                 '<input type ="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1.188     raeburn  3820:                 '</form>';
                   3821:     return $outcome;
1.4       www      3822: }
                   3823: 
1.149     raeburn  3824: sub quota_admin {
                   3825:     my ($setquota,$changeHash) = @_;
                   3826:     my $quotachanged;
                   3827:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   3828:         # Current user has quota modification privileges
1.267     raeburn  3829:         if (ref($changeHash) eq 'HASH') {
                   3830:             $quotachanged = 1;
                   3831:             $changeHash->{'portfolioquota'} = $setquota;
                   3832:         }
1.149     raeburn  3833:     }
                   3834:     return $quotachanged;
                   3835: }
                   3836: 
1.267     raeburn  3837: sub tool_admin {
1.275     raeburn  3838:     my ($tool,$settool,$changeHash,$context) = @_;
                   3839:     my $canchange = 0; 
1.279     raeburn  3840:     if ($context eq 'requestcourses') {
1.275     raeburn  3841:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
                   3842:             $canchange = 1;
                   3843:         }
1.300     raeburn  3844:     } elsif ($context eq 'reqcrsotherdom') {
                   3845:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   3846:             $canchange = 1;
                   3847:         }
1.362     raeburn  3848:     } elsif ($context eq 'requestauthor') {
                   3849:         if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
                   3850:             $canchange = 1;
                   3851:         }
1.275     raeburn  3852:     } elsif (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   3853:         # Current user has quota modification privileges
                   3854:         $canchange = 1;
                   3855:     }
1.267     raeburn  3856:     my $toolchanged;
1.275     raeburn  3857:     if ($canchange) {
1.267     raeburn  3858:         if (ref($changeHash) eq 'HASH') {
                   3859:             $toolchanged = 1;
1.362     raeburn  3860:             if ($tool eq 'requestauthor') {
                   3861:                 $changeHash->{$context} = $settool;
                   3862:             } else {
                   3863:                 $changeHash->{$context.'.'.$tool} = $settool;
                   3864:             }
1.267     raeburn  3865:         }
                   3866:     }
                   3867:     return $toolchanged;
                   3868: }
                   3869: 
1.88      raeburn  3870: sub build_roles {
1.89      raeburn  3871:     my ($sectionstr,$sections,$role) = @_;
1.88      raeburn  3872:     my $num_sections = 0;
                   3873:     if ($sectionstr=~ /,/) {
                   3874:         my @secnums = split/,/,$sectionstr;
1.89      raeburn  3875:         if ($role eq 'st') {
                   3876:             $secnums[0] =~ s/\W//g;
                   3877:             $$sections{$secnums[0]} = 1;
                   3878:             $num_sections = 1;
                   3879:         } else {
                   3880:             foreach my $sec (@secnums) {
                   3881:                 $sec =~ ~s/\W//g;
1.150     banghart 3882:                 if (!($sec eq "")) {
1.89      raeburn  3883:                     if (exists($$sections{$sec})) {
                   3884:                         $$sections{$sec} ++;
                   3885:                     } else {
                   3886:                         $$sections{$sec} = 1;
                   3887:                         $num_sections ++;
                   3888:                     }
1.88      raeburn  3889:                 }
                   3890:             }
                   3891:         }
                   3892:     } else {
                   3893:         $sectionstr=~s/\W//g;
                   3894:         unless ($sectionstr eq '') {
                   3895:             $$sections{$sectionstr} = 1;
                   3896:             $num_sections ++;
                   3897:         }
                   3898:     }
1.129     albertel 3899: 
1.88      raeburn  3900:     return $num_sections;
                   3901: }
                   3902: 
1.58      www      3903: # ========================================================== Custom Role Editor
                   3904: 
                   3905: sub custom_role_editor {
1.351     raeburn  3906:     my ($r,$brcrum) = @_;
1.324     raeburn  3907:     my $action = $env{'form.customroleaction'};
                   3908:     my $rolename; 
                   3909:     if ($action eq 'new') {
                   3910:         $rolename=$env{'form.newrolename'};
                   3911:     } else {
                   3912:         $rolename=$env{'form.rolename'};
1.59      www      3913:     }
                   3914: 
1.324     raeburn  3915:     my ($crstype,$context);
                   3916:     if ($env{'request.course.id'}) {
                   3917:         $crstype = &Apache::loncommon::course_type();
                   3918:         $context = 'course';
                   3919:     } else {
                   3920:         $context = 'domain';
                   3921:         $crstype = $env{'form.templatecrstype'};
                   3922:     }
1.351     raeburn  3923: 
                   3924:     $rolename=~s/[^A-Za-z0-9]//gs;
                   3925:     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
                   3926: 	&print_username_entry_form($r,undef,undef,undef,undef,$crstype,$brcrum);
                   3927:         return;
                   3928:     }
                   3929: 
1.153     banghart 3930: # ------------------------------------------------------- What can be assigned?
                   3931:     my %full=();
                   3932:     my %courselevel=();
                   3933:     my %courselevelcurrent=();
1.61      www      3934:     my $syspriv='';
                   3935:     my $dompriv='';
                   3936:     my $coursepriv='';
1.153     banghart 3937:     my $body_top;
1.59      www      3938:     my ($rdummy,$roledef)=
                   3939: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
1.60      www      3940: # ------------------------------------------------------- Does this role exist?
1.153     banghart 3941:     $body_top .= '<h2>';
1.59      www      3942:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.153     banghart 3943: 	$body_top .= &mt('Existing Role').' "';
1.61      www      3944: # ------------------------------------------------- Get current role privileges
                   3945: 	($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
1.324     raeburn  3946:         if ($crstype eq 'Community') {
                   3947:             $syspriv =~ s/bre\&S//;   
                   3948:         }
1.59      www      3949:     } else {
1.153     banghart 3950: 	$body_top .= &mt('New Role').' "';
1.59      www      3951: 	$roledef='';
                   3952:     }
1.153     banghart 3953:     $body_top .= $rolename.'"</h2>';
1.135     raeburn  3954:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   3955: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3956:         if (!$restrict) { $restrict='F'; }
1.60      www      3957:         $courselevel{$priv}=$restrict;
1.61      www      3958:         if ($coursepriv=~/\:$priv/) {
                   3959: 	    $courselevelcurrent{$priv}=1;
                   3960: 	}
1.60      www      3961: 	$full{$priv}=1;
                   3962:     }
                   3963:     my %domainlevel=();
1.61      www      3964:     my %domainlevelcurrent=();
1.135     raeburn  3965:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   3966: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3967:         if (!$restrict) { $restrict='F'; }
1.60      www      3968:         $domainlevel{$priv}=$restrict;
1.61      www      3969:         if ($dompriv=~/\:$priv/) {
                   3970: 	    $domainlevelcurrent{$priv}=1;
                   3971: 	}
1.60      www      3972: 	$full{$priv}=1;
                   3973:     }
1.61      www      3974:     my %systemlevel=();
                   3975:     my %systemlevelcurrent=();
1.135     raeburn  3976:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   3977: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3978:         if (!$restrict) { $restrict='F'; }
1.61      www      3979:         $systemlevel{$priv}=$restrict;
                   3980:         if ($syspriv=~/\:$priv/) {
                   3981: 	    $systemlevelcurrent{$priv}=1;
                   3982: 	}
                   3983: 	$full{$priv}=1;
                   3984:     }
1.160     raeburn  3985:     my ($jsback,$elements) = &crumb_utilities();
1.154     banghart 3986:     my $button_code = "\n";
1.153     banghart 3987:     my $head_script = "\n";
1.301     bisitz   3988:     $head_script .= '<script type="text/javascript">'."\n"
                   3989:                    .'// <![CDATA['."\n";
1.324     raeburn  3990:     my @template_roles = ("in","ta","ep");
                   3991:     if ($context eq 'domain') {
                   3992:         push(@template_roles,"ad");
1.318     raeburn  3993:     }
1.324     raeburn  3994:     push(@template_roles,"st");
1.318     raeburn  3995:     if ($crstype eq 'Community') {
                   3996:         unshift(@template_roles,'co');
                   3997:     } else {
                   3998:         unshift(@template_roles,'cc');
                   3999:     }
1.154     banghart 4000:     foreach my $role (@template_roles) {
1.324     raeburn  4001:         $head_script .= &make_script_template($role,$crstype);
1.318     raeburn  4002:         $button_code .= &make_button_code($role,$crstype).' ';
1.154     banghart 4003:     }
1.324     raeburn  4004:     my $context_code;
                   4005:     if ($context eq 'domain') {
                   4006:         my $checkedCommunity = '';
                   4007:         my $checkedCourse = ' checked="checked"';
                   4008:         if ($env{'form.templatecrstype'} eq 'Community') {
                   4009:             $checkedCommunity = $checkedCourse;
                   4010:             $checkedCourse = '';
                   4011:         }
                   4012:         $context_code = '<label>'.
                   4013:                         '<input type="radio" name="templatecrstype" value="Course"'.$checkedCourse.' onclick="this.form.submit();">'.
                   4014:                         &mt('Course').
                   4015:                         '</label>'.('&nbsp;' x2).
                   4016:                         '<label>'.
                   4017:                         '<input type="radio" name="templatecrstype" value="Community"'.$checkedCommunity.' onclick="this.form.submit();">'.
                   4018:                         &mt('Community').
                   4019:                         '</label>'.
                   4020:                         '</fieldset>'.
                   4021:                         '<input type="hidden" name="customroleaction" value="'.
                   4022:                         $action.'" />';
                   4023:         if ($env{'form.customroleaction'} eq 'new') {
                   4024:             $context_code .= '<input type="hidden" name="newrolename" value="'.
                   4025:                              $rolename.'" />';
                   4026:         } else {
                   4027:             $context_code .= '<input type="hidden" name="rolename" value="'.
                   4028:                              $rolename.'" />';
                   4029:         }
                   4030:         $context_code .= '<input type="hidden" name="action" value="custom" />'.
                   4031:                          '<input type="hidden" name="phase" value="selected_custom_edit" />';
                   4032:     }
                   4033: 
1.301     bisitz   4034:     $head_script .= "\n".$jsback."\n"
                   4035:                    .'// ]]>'."\n"
                   4036:                    .'</script>'."\n";
1.351     raeburn  4037:     push (@{$brcrum},
                   4038:               {href => "javascript:backPage(document.form1,'pickrole','')",
                   4039:                text => "Pick custom role",
                   4040:                faq  => 282,bug=>'Instructor Interface',},
                   4041:               {href => "javascript:backPage(document.form1,'','')",
                   4042:                text => "Edit custom role",
                   4043:                faq  => 282,
                   4044:                bug  => 'Instructor Interface',
                   4045:                help => 'Course_Editing_Custom_Roles'}
                   4046:               );
                   4047:     my $args = { bread_crumbs          => $brcrum,
                   4048:                  bread_crumbs_component => 'User Management'};
                   4049:  
                   4050:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',
                   4051:                                              $head_script,$args).
                   4052:               $body_top);
1.73      sakharuk 4053:     my %lt=&Apache::lonlocal::texthash(
                   4054: 		    'prv'  => "Privilege",
1.131     raeburn  4055: 		    'crl'  => "Course Level",
1.73      sakharuk 4056:                     'dml'  => "Domain Level",
1.150     banghart 4057:                     'ssl'  => "System Level");
1.264     bisitz   4058: 
1.324     raeburn  4059:     $r->print('<div class="LC_left_float">'
1.264     bisitz   4060:              .'<form action=""><fieldset>'
                   4061:              .'<legend>'.&mt('Select a Template').'</legend>'
                   4062:              .$button_code
1.324     raeburn  4063:              .'</fieldset></form></div>');
                   4064:     if ($context_code) {
                   4065:         $r->print('<div class="LC_left_float">'
                   4066:                  .'<form action="/adm/createuser" method="post"><fieldset>'
                   4067:                  .'<legend>'.&mt('Context').'</legend>'
                   4068:                  .$context_code
                   4069:                  .'</form>'
                   4070:                  .'</div>'
                   4071:         );
                   4072:     }
                   4073:     $r->print('<br clear="all" />');
1.264     bisitz   4074: 
1.61      www      4075:     $r->print(<<ENDCCF);
1.160     raeburn  4076: <form name="form1" method="post">
1.61      www      4077: <input type="hidden" name="phase" value="set_custom_roles" />
                   4078: <input type="hidden" name="rolename" value="$rolename" />
                   4079: ENDCCF
1.135     raeburn  4080:     $r->print(&Apache::loncommon::start_data_table().
                   4081:               &Apache::loncommon::start_data_table_header_row(). 
                   4082: '<th>'.$lt{'prv'}.'</th><th>'.$lt{'crl'}.'</th><th>'.$lt{'dml'}.
                   4083: '</th><th>'.$lt{'ssl'}.'</th>'.
                   4084:               &Apache::loncommon::end_data_table_header_row());
1.324     raeburn  4085:     foreach my $priv (sort(keys(%full))) {
1.318     raeburn  4086:         my $privtext = &Apache::lonnet::plaintext($priv,$crstype);
1.135     raeburn  4087:         $r->print(&Apache::loncommon::start_data_table_row().
                   4088: 	          '<td>'.$privtext.'</td><td>'.
1.288     bisitz   4089:     ($courselevel{$priv}?'<input type="checkbox" name="'.$priv.'_c"'.
                   4090:     ($courselevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;').
1.61      www      4091:     '</td><td>'.
1.288     bisitz   4092:     ($domainlevel{$priv}?'<input type="checkbox" name="'.$priv.'_d"'.
                   4093:     ($domainlevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;').
1.324     raeburn  4094:     '</td><td>');
                   4095:         if ($priv eq 'bre' && $crstype eq 'Community') {
                   4096:             $r->print('&nbsp;');  
                   4097:         } else {
                   4098:             $r->print($systemlevel{$priv}?'<input type="checkbox" name="'.$priv.'_s"'.
                   4099:                       ($systemlevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;');
                   4100:         }
                   4101:         $r->print('</td>'.
                   4102:                   &Apache::loncommon::end_data_table_row());
1.60      www      4103:     }
1.135     raeburn  4104:     $r->print(&Apache::loncommon::end_data_table().
1.190     raeburn  4105:    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
1.160     raeburn  4106:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
1.179     raeburn  4107:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".   
1.160     raeburn  4108:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
1.351     raeburn  4109:    '<input type="submit" value="'.&mt('Save').'" /></form>');
1.61      www      4110: }
1.153     banghart 4111: # --------------------------------------------------------
                   4112: sub make_script_template {
1.324     raeburn  4113:     my ($role,$crstype) = @_;
1.153     banghart 4114:     my %full_c=();
                   4115:     my %full_d=();
                   4116:     my %full_s=();
                   4117:     my $return_script;
                   4118:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   4119:         my ($priv,$restrict)=split(/\&/,$item);
                   4120:         $full_c{$priv}=1;
                   4121:     }
                   4122:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   4123:         my ($priv,$restrict)=split(/\&/,$item);
                   4124:         $full_d{$priv}=1;
                   4125:     }
1.154     banghart 4126:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
1.324     raeburn  4127:         next if (($crstype eq 'Community') && ($item eq 'bre&S'));
1.153     banghart 4128:         my ($priv,$restrict)=split(/\&/,$item);
                   4129:         $full_s{$priv}=1;
                   4130:     }
                   4131:     $return_script .= 'function set_'.$role.'() {'."\n";
                   4132:     my @temp = split(/:/,$Apache::lonnet::pr{$role.':c'});
                   4133:     my %role_c;
1.155     banghart 4134:     foreach my $priv (@temp) {
1.153     banghart 4135:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   4136:         $role_c{$priv_item} = 1;
                   4137:     }
1.269     raeburn  4138:     my %role_d;
                   4139:     @temp = split(/:/,$Apache::lonnet::pr{$role.':d'});
                   4140:     foreach my $priv(@temp) {
                   4141:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   4142:         $role_d{$priv_item} = 1;
                   4143:     }
                   4144:     my %role_s;
                   4145:     @temp = split(/:/,$Apache::lonnet::pr{$role.':s'});
                   4146:     foreach my $priv(@temp) {
                   4147:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   4148:         $role_s{$priv_item} = 1;
                   4149:     }
1.153     banghart 4150:     foreach my $priv_item (keys(%full_c)) {
                   4151:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.269     raeburn  4152:         if ((exists($role_c{$priv})) || (exists($role_d{$priv})) || 
                   4153:             (exists($role_s{$priv}))) {
1.153     banghart 4154:             $return_script .= "document.form1.$priv"."_c.checked = true;\n";
                   4155:         } else {
                   4156:             $return_script .= "document.form1.$priv"."_c.checked = false;\n";
                   4157:         }
                   4158:     }
1.154     banghart 4159:     foreach my $priv_item (keys(%full_d)) {
                   4160:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.269     raeburn  4161:         if ((exists($role_d{$priv})) || (exists($role_s{$priv}))) {
1.154     banghart 4162:             $return_script .= "document.form1.$priv"."_d.checked = true;\n";
                   4163:         } else {
                   4164:             $return_script .= "document.form1.$priv"."_d.checked = false;\n";
                   4165:         }
                   4166:     }
                   4167:     foreach my $priv_item (keys(%full_s)) {
1.153     banghart 4168:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.154     banghart 4169:         if (exists($role_s{$priv})) {
                   4170:             $return_script .= "document.form1.$priv"."_s.checked = true;\n";
                   4171:         } else {
                   4172:             $return_script .= "document.form1.$priv"."_s.checked = false;\n";
                   4173:         }
1.153     banghart 4174:     }
                   4175:     $return_script .= '}'."\n";
1.154     banghart 4176:     return ($return_script);
                   4177: }
                   4178: # ----------------------------------------------------------
                   4179: sub make_button_code {
1.318     raeburn  4180:     my ($role,$crstype) = @_;
                   4181:     my $label = &Apache::lonnet::plaintext($role,$crstype);
1.301     bisitz   4182:     my $button_code = '<input type="button" onclick="set_'.$role.'()" value="'.$label.'" />';
1.154     banghart 4183:     return ($button_code);
1.153     banghart 4184: }
1.61      www      4185: # ---------------------------------------------------------- Call to definerole
                   4186: sub set_custom_role {
1.351     raeburn  4187:     my ($r,$context,$brcrum) = @_;
1.101     albertel 4188:     my $rolename=$env{'form.rolename'};
1.63      www      4189:     $rolename=~s/[^A-Za-z0-9]//gs;
1.150     banghart 4190:     if (!$rolename) {
1.351     raeburn  4191: 	&custom_role_editor($r,$brcrum);
1.61      www      4192:         return;
                   4193:     }
1.160     raeburn  4194:     my ($jsback,$elements) = &crumb_utilities();
1.301     bisitz   4195:     my $jscript = '<script type="text/javascript">'
                   4196:                  .'// <![CDATA['."\n"
                   4197:                  .$jsback."\n"
                   4198:                  .'// ]]>'."\n"
                   4199:                  .'</script>'."\n";
1.352     raeburn  4200:     push(@{$brcrum},
                   4201:         {href => "javascript:backPage(document.customresult,'pickrole','')",
                   4202:          text => "Pick custom role",
                   4203:          faq  => 282,
                   4204:          bug  => 'Instructor Interface',},
                   4205:         {href => "javascript:backPage(document.customresult,'selected_custom_edit','')",
                   4206:          text => "Edit custom role",
                   4207:          faq  => 282,
                   4208:          bug  => 'Instructor Interface',},
                   4209:         {href => "javascript:backPage(document.customresult,'set_custom_roles','')",
                   4210:          text => "Result",
                   4211:          faq  => 282,
                   4212:          bug  => 'Instructor Interface',
                   4213:          help => 'Course_Editing_Custom_Roles'},
                   4214:         );
                   4215:     my $args = { bread_crumbs           => $brcrum,
1.351     raeburn  4216:                  bread_crumbs_component => 'User Management'}; 
                   4217:     $r->print(&Apache::loncommon::start_page('Save Custom Role',$jscript,$args));
1.160     raeburn  4218: 
1.61      www      4219:     my ($rdummy,$roledef)=
1.110     albertel 4220: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   4221: 
1.61      www      4222: # ------------------------------------------------------- Does this role exist?
1.188     raeburn  4223:     $r->print('<h3>');
1.61      www      4224:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 4225: 	$r->print(&mt('Existing Role').' "');
1.61      www      4226:     } else {
1.73      sakharuk 4227: 	$r->print(&mt('New Role').' "');
1.61      www      4228: 	$roledef='';
                   4229:     }
1.188     raeburn  4230:     $r->print($rolename.'"</h3>');
1.61      www      4231: # ------------------------------------------------------- What can be assigned?
                   4232:     my $sysrole='';
                   4233:     my $domrole='';
                   4234:     my $courole='';
                   4235: 
1.135     raeburn  4236:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   4237: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4238:         if (!$restrict) { $restrict=''; }
                   4239:         if ($env{'form.'.$priv.'_c'}) {
1.135     raeburn  4240: 	    $courole.=':'.$item;
1.61      www      4241: 	}
                   4242:     }
                   4243: 
1.135     raeburn  4244:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   4245: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4246:         if (!$restrict) { $restrict=''; }
                   4247:         if ($env{'form.'.$priv.'_d'}) {
1.135     raeburn  4248: 	    $domrole.=':'.$item;
1.61      www      4249: 	}
                   4250:     }
                   4251: 
1.135     raeburn  4252:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   4253: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4254:         if (!$restrict) { $restrict=''; }
                   4255:         if ($env{'form.'.$priv.'_s'}) {
1.135     raeburn  4256: 	    $sysrole.=':'.$item;
1.61      www      4257: 	}
                   4258:     }
1.63      www      4259:     $r->print('<br />Defining Role: '.
1.61      www      4260: 	   &Apache::lonnet::definerole($rolename,$sysrole,$domrole,$courole));
1.101     albertel 4261:     if ($env{'request.course.id'}) {
                   4262:         my $url='/'.$env{'request.course.id'};
1.63      www      4263:         $url=~s/\_/\//g;
1.73      sakharuk 4264: 	$r->print('<br />'.&mt('Assigning Role to Self').': '.
1.101     albertel 4265: 	      &Apache::lonnet::assigncustomrole($env{'user.domain'},
                   4266: 						$env{'user.name'},
1.63      www      4267: 						$url,
1.101     albertel 4268: 						$env{'user.domain'},
                   4269: 						$env{'user.name'},
1.240     raeburn  4270: 						$rolename,undef,undef,undef,$context));
1.63      www      4271:     }
1.190     raeburn  4272:     $r->print('<p><a href="javascript:backPage(document.customresult,'."'pickrole'".')">'.&mt('Create or edit another custom role').'</a></p><form name="customresult" method="post">');
1.160     raeburn  4273:     $r->print(&Apache::lonhtmlcommon::echo_form_input([]).'</form>');
1.58      www      4274: }
                   4275: 
1.2       www      4276: # ================================================================ Main Handler
                   4277: sub handler {
                   4278:     my $r = shift;
                   4279:     if ($r->header_only) {
1.68      www      4280:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      4281:        $r->send_http_header;
                   4282:        return OK;
                   4283:     }
1.318     raeburn  4284:     my ($context,$crstype);
1.190     raeburn  4285:     if ($env{'request.course.id'}) {
                   4286:         $context = 'course';
1.318     raeburn  4287:         $crstype = &Apache::loncommon::course_type();
1.190     raeburn  4288:     } elsif ($env{'request.role'} =~ /^au\./) {
1.206     raeburn  4289:         $context = 'author';
1.190     raeburn  4290:     } else {
                   4291:         $context = 'domain';
                   4292:     }
1.375     raeburn  4293: 
1.190     raeburn  4294:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.233     raeburn  4295:         ['action','state','callingform','roletype','showrole','bulkaction','popup','phase',
                   4296:          'username','domain','srchterm','srchdomain','srchin','srchby','srchtype']);
1.190     raeburn  4297:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.351     raeburn  4298:     my $args;
                   4299:     my $brcrum = [];
                   4300:     my $bread_crumbs_component = 'User Management';
1.202     raeburn  4301:     if ($env{'form.action'} ne 'dateselect') {
1.351     raeburn  4302:         $brcrum = [{href=>"/adm/createuser",
                   4303:                     text=>"User Management",
                   4304:                     help=>'Course_Create_Class_List,Course_Change_Privileges,Course_View_Class_List,Course_Editing_Custom_Roles,Course_Add_Student,Course_Drop_Student,Course_Automated_Enrollment,Course_Self_Enrollment,Course_Manage_Group'}
                   4305:                   ];
1.202     raeburn  4306:     }
1.289     droeschl 4307:     #SD Following files not added to help, because the corresponding .tex-files seem to
                   4308:     #be missing: Course_Approve_Selfenroll,Course_User_Logs,
1.209     raeburn  4309:     my ($permission,$allowed) = 
1.318     raeburn  4310:         &Apache::lonuserutils::get_permission($context,$crstype);
1.190     raeburn  4311:     if (!$allowed) {
1.358     raeburn  4312:         if ($context eq 'course') {
                   4313:             $r->internal_redirect('/adm/viewclasslist');
                   4314:             return OK;
                   4315:         }
1.190     raeburn  4316:         $env{'user.error.msg'}=
                   4317:             "/adm/createuser:cst:0:0:Cannot create/modify user data ".
                   4318:                                  "or view user status.";
                   4319:         return HTTP_NOT_ACCEPTABLE;
                   4320:     }
                   4321: 
                   4322:     &Apache::loncommon::content_type($r,'text/html');
                   4323:     $r->send_http_header;
                   4324: 
1.375     raeburn  4325:     my $showcredits;
                   4326:     if ((($context eq 'course') && ($crstype eq 'Course')) || 
                   4327:          ($context eq 'domain')) {
                   4328:         my %domdefaults = 
                   4329:             &Apache::lonnet::get_domain_defaults($env{'request.role.domain'});
                   4330:         if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'}) {
                   4331:             $showcredits = 1;
                   4332:         }
                   4333:     }
                   4334: 
1.190     raeburn  4335:     # Main switch on form.action and form.state, as appropriate
                   4336:     if (! exists($env{'form.action'})) {
1.351     raeburn  4337:         $args = {bread_crumbs => $brcrum,
                   4338:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4339:         $r->print(&header(undef,$args));
1.318     raeburn  4340:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4341:     } elsif ($env{'form.action'} eq 'upload' && $permission->{'cusr'}) {
1.351     raeburn  4342:         push(@{$brcrum},
                   4343:               { href => '/adm/createuser?action=upload&state=',
                   4344:                 text => 'Upload Users List',
                   4345:                 help => 'Course_Create_Class_List',
                   4346:               });
                   4347:         $bread_crumbs_component = 'Upload Users List';
                   4348:         $args = {bread_crumbs           => $brcrum,
                   4349:                  bread_crumbs_component => $bread_crumbs_component};
                   4350:         $r->print(&header(undef,$args));
1.190     raeburn  4351:         $r->print('<form name="studentform" method="post" '.
                   4352:                   'enctype="multipart/form-data" '.
                   4353:                   ' action="/adm/createuser">'."\n");
                   4354:         if (! exists($env{'form.state'})) {
                   4355:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4356:         } elsif ($env{'form.state'} eq 'got_file') {
1.375     raeburn  4357:             &Apache::lonuserutils::print_upload_manager_form($r,$context,$permission,
                   4358:                                                              $crstype,$showcredits);
1.190     raeburn  4359:         } elsif ($env{'form.state'} eq 'enrolling') {
                   4360:             if ($env{'form.datatoken'}) {
1.375     raeburn  4361:                 &Apache::lonuserutils::upfile_drop_add($r,$context,$permission,
                   4362:                                                        $showcredits);
1.190     raeburn  4363:             }
                   4364:         } else {
                   4365:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4366:         }
1.213     raeburn  4367:     } elsif ((($env{'form.action'} eq 'singleuser') || ($env{'form.action'}
                   4368:              eq 'singlestudent')) && ($permission->{'cusr'})) {
1.190     raeburn  4369:         my $phase = $env{'form.phase'};
                   4370:         my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
1.192     albertel 4371: 	&Apache::loncreateuser::restore_prev_selections();
                   4372: 	my $srch;
                   4373: 	foreach my $item (@search) {
                   4374: 	    $srch->{$item} = $env{'form.'.$item};
                   4375: 	}
1.207     raeburn  4376:         if (($phase eq 'get_user_info') || ($phase eq 'userpicked') ||
                   4377:             ($phase eq 'createnewuser')) {
                   4378:             if ($env{'form.phase'} eq 'createnewuser') {
                   4379:                 my $response;
                   4380:                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
1.366     bisitz   4381:                     my $response =
                   4382:                         '<span class="LC_warning">'
                   4383:                        .&mt('You must specify a valid username. Only the following are allowed:'
                   4384:                            .' letters numbers - . @')
                   4385:                        .'</span>';
1.221     raeburn  4386:                     $env{'form.phase'} = '';
1.375     raeburn  4387:                     &print_username_entry_form($r,$context,$response,$srch,undef,
                   4388:                                                $crstype,$brcrum,$showcredits);
1.207     raeburn  4389:                 } else {
                   4390:                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
                   4391:                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
                   4392:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4393:                                                   $srch,$response,$context,
1.375     raeburn  4394:                                                   $permission,$crstype,$brcrum,
                   4395:                                                   $showcredits);
1.207     raeburn  4396:                 }
                   4397:             } elsif ($env{'form.phase'} eq 'get_user_info') {
1.190     raeburn  4398:                 my ($currstate,$response,$forcenewuser,$results) = 
1.221     raeburn  4399:                     &user_search_result($context,$srch);
1.190     raeburn  4400:                 if ($env{'form.currstate'} eq 'modify') {
                   4401:                     $currstate = $env{'form.currstate'};
                   4402:                 }
                   4403:                 if ($currstate eq 'select') {
                   4404:                     &print_user_selection_page($r,$response,$srch,$results,
1.351     raeburn  4405:                                                \@search,$context,undef,$crstype,
                   4406:                                                $brcrum);
1.190     raeburn  4407:                 } elsif ($currstate eq 'modify') {
                   4408:                     my ($ccuname,$ccdomain);
                   4409:                     if (($srch->{'srchby'} eq 'uname') && 
                   4410:                         ($srch->{'srchtype'} eq 'exact')) {
                   4411:                         $ccuname = $srch->{'srchterm'};
                   4412:                         $ccdomain= $srch->{'srchdomain'};
                   4413:                     } else {
                   4414:                         my @matchedunames = keys(%{$results});
                   4415:                         ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
                   4416:                     }
                   4417:                     $ccuname =&LONCAPA::clean_username($ccuname);
                   4418:                     $ccdomain=&LONCAPA::clean_domain($ccdomain);
                   4419:                     if ($env{'form.forcenewuser'}) {
                   4420:                         $response = '';
                   4421:                     }
                   4422:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4423:                                                   $srch,$response,$context,
1.351     raeburn  4424:                                                   $permission,$crstype,$brcrum);
1.190     raeburn  4425:                 } elsif ($currstate eq 'query') {
1.351     raeburn  4426:                     &print_user_query_page($r,'createuser',$brcrum);
1.190     raeburn  4427:                 } else {
1.229     raeburn  4428:                     $env{'form.phase'} = '';
1.207     raeburn  4429:                     &print_username_entry_form($r,$context,$response,$srch,
1.351     raeburn  4430:                                                $forcenewuser,$crstype,$brcrum);
1.190     raeburn  4431:                 }
                   4432:             } elsif ($env{'form.phase'} eq 'userpicked') {
                   4433:                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
                   4434:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
1.196     raeburn  4435:                 &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
1.351     raeburn  4436:                                               $context,$permission,$crstype,
                   4437:                                               $brcrum);
1.190     raeburn  4438:             }
                   4439:         } elsif ($env{'form.phase'} eq 'update_user_data') {
1.375     raeburn  4440:             &update_user_data($r,$context,$crstype,$brcrum,$showcredits);
1.190     raeburn  4441:         } else {
1.351     raeburn  4442:             &print_username_entry_form($r,$context,undef,$srch,undef,$crstype,
                   4443:                                        $brcrum);
1.190     raeburn  4444:         }
                   4445:     } elsif ($env{'form.action'} eq 'custom' && $permission->{'custom'}) {
                   4446:         if ($env{'form.phase'} eq 'set_custom_roles') {
1.351     raeburn  4447:             &set_custom_role($r,$context,$brcrum);
1.190     raeburn  4448:         } else {
1.351     raeburn  4449:             &custom_role_editor($r,$brcrum);
1.190     raeburn  4450:         }
1.362     raeburn  4451:     } elsif (($env{'form.action'} eq 'processauthorreq') &&
                   4452:              ($permission->{'cusr'}) && 
                   4453:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   4454:         push(@{$brcrum},
                   4455:                  {href => '/adm/createuser?action=processauthorreq',
                   4456:                   text => 'Authoring space requests',
                   4457:                   help => 'Domain_Role_Approvals'});
                   4458:         $bread_crumbs_component = 'Authoring requests';
                   4459:         if ($env{'form.state'} eq 'done') {
                   4460:             push(@{$brcrum},
                   4461:                      {href => '/adm/createuser?action=authorreqqueue',
                   4462:                       text => 'Result',
                   4463:                       help => 'Domain_Role_Approvals'});
                   4464:             $bread_crumbs_component = 'Authoring request result';
                   4465:         }
                   4466:         $args = { bread_crumbs           => $brcrum,
                   4467:                   bread_crumbs_component => $bread_crumbs_component};
                   4468:         $r->print(&header(undef,$args));
                   4469:         if (!exists($env{'form.state'})) {
                   4470:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestauthor',
                   4471:                                                                             $env{'request.role.domain'}));
                   4472:         } elsif ($env{'form.state'} eq 'done') {
                   4473:             $r->print('<h3>'.&mt('Authoring request processing').'</h3>'."\n");
                   4474:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestauthor',
                   4475:                                                                          $env{'request.role.domain'}));
                   4476:         }
1.207     raeburn  4477:     } elsif (($env{'form.action'} eq 'listusers') && 
                   4478:              ($permission->{'view'} || $permission->{'cusr'})) {
1.202     raeburn  4479:         if ($env{'form.phase'} eq 'bulkchange') {
1.351     raeburn  4480:             push(@{$brcrum},
                   4481:                     {href => '/adm/createuser?action=listusers',
                   4482:                      text => "List Users"},
                   4483:                     {href => "/adm/createuser",
                   4484:                      text => "Result",
                   4485:                      help => 'Course_View_Class_List'});
                   4486:             $bread_crumbs_component = 'Update Users';
                   4487:             $args = {bread_crumbs           => $brcrum,
                   4488:                      bread_crumbs_component => $bread_crumbs_component};
                   4489:             $r->print(&header(undef,$args));
1.202     raeburn  4490:             my $setting = $env{'form.roletype'};
                   4491:             my $choice = $env{'form.bulkaction'};
                   4492:             if ($permission->{'cusr'}) {
1.336     raeburn  4493:                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice,$crstype);
1.221     raeburn  4494:             } else {
                   4495:                 $r->print(&mt('You are not authorized to make bulk changes to user roles'));
1.223     raeburn  4496:                 $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
1.202     raeburn  4497:             }
                   4498:         } else {
1.351     raeburn  4499:             push(@{$brcrum},
                   4500:                     {href => '/adm/createuser?action=listusers',
                   4501:                      text => "List Users",
                   4502:                      help => 'Course_View_Class_List'});
                   4503:             $bread_crumbs_component = 'List Users';
                   4504:             $args = {bread_crumbs           => $brcrum,
                   4505:                      bread_crumbs_component => $bread_crumbs_component};
1.202     raeburn  4506:             my ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles);
                   4507:             my $formname = 'studentform';
1.364     raeburn  4508:             my $hidecall = "hide_searching();";
1.321     raeburn  4509:             if (($context eq 'domain') && (($env{'form.roletype'} eq 'course') ||
                   4510:                 ($env{'form.roletype'} eq 'community'))) {
                   4511:                 if ($env{'form.roletype'} eq 'course') {
                   4512:                     ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles) = 
                   4513:                         &Apache::lonuserutils::courses_selector($env{'request.role.domain'},
                   4514:                                                                 $formname);
                   4515:                 } elsif ($env{'form.roletype'} eq 'community') {
                   4516:                     $cb_jscript = 
                   4517:                         &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
                   4518:                     my %elements = (
                   4519:                                       coursepick => 'radio',
                   4520:                                       coursetotal => 'text',
                   4521:                                       courselist => 'text',
                   4522:                                    );
                   4523:                     $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   4524:                 }
1.364     raeburn  4525:                 $jscript .= &verify_user_display($context)."\n".
                   4526:                             &Apache::loncommon::check_uncheck_jscript();
1.202     raeburn  4527:                 my $js = &add_script($jscript).$cb_jscript;
                   4528:                 my $loadcode = 
                   4529:                     &Apache::lonuserutils::course_selector_loadcode($formname);
                   4530:                 if ($loadcode ne '') {
1.364     raeburn  4531:                     $args->{add_entries} = {onload => "$loadcode;$hidecall"};
                   4532:                 } else {
                   4533:                     $args->{add_entries} = {onload => $hidecall};
1.202     raeburn  4534:                 }
1.351     raeburn  4535:                 $r->print(&header($js,$args));
1.191     raeburn  4536:             } else {
1.364     raeburn  4537:                 $args->{add_entries} = {onload => $hidecall};
                   4538:                 $jscript = &verify_user_display($context).
                   4539:                            &Apache::loncommon::check_uncheck_jscript(); 
                   4540:                 $r->print(&header(&add_script($jscript),$args));
1.191     raeburn  4541:             }
1.202     raeburn  4542:             &Apache::lonuserutils::print_userlist($r,undef,$permission,$context,
1.375     raeburn  4543:                          $formname,$totcodes,$codetitles,$idlist,$idlist_titles,
                   4544:                          $showcredits);
1.191     raeburn  4545:         }
1.213     raeburn  4546:     } elsif ($env{'form.action'} eq 'drop' && $permission->{'cusr'}) {
1.318     raeburn  4547:         my $brtext;
                   4548:         if ($crstype eq 'Community') {
                   4549:             $brtext = 'Drop Members';
                   4550:         } else {
                   4551:             $brtext = 'Drop Students';
                   4552:         }
1.351     raeburn  4553:         push(@{$brcrum},
                   4554:                 {href => '/adm/createuser?action=drop',
                   4555:                  text => $brtext,
                   4556:                  help => 'Course_Drop_Student'});
                   4557:         if ($env{'form.state'} eq 'done') {
                   4558:             push(@{$brcrum},
                   4559:                      {href=>'/adm/createuser?action=drop',
                   4560:                       text=>"Result"});
                   4561:         }
                   4562:         $bread_crumbs_component = $brtext;
                   4563:         $args = {bread_crumbs           => $brcrum,
                   4564:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4565:         $r->print(&header(undef,$args));
1.213     raeburn  4566:         if (!exists($env{'form.state'})) {
1.318     raeburn  4567:             &Apache::lonuserutils::print_drop_menu($r,$context,$permission,$crstype);
1.213     raeburn  4568:         } elsif ($env{'form.state'} eq 'done') {
                   4569:             &Apache::lonuserutils::update_user_list($r,$context,undef,
                   4570:                                                     $env{'form.action'});
                   4571:         }
1.202     raeburn  4572:     } elsif ($env{'form.action'} eq 'dateselect') {
                   4573:         if ($permission->{'cusr'}) {
1.351     raeburn  4574:             $r->print(&header(undef,{'no_nav_bar' => 1}).
1.375     raeburn  4575:                       &Apache::lonuserutils::date_section_selector($context,$permission,
                   4576:                                                                    $crstype,$showcredits));
1.202     raeburn  4577:         } else {
1.351     raeburn  4578:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   4579:                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>'); 
1.202     raeburn  4580:         }
1.237     raeburn  4581:     } elsif ($env{'form.action'} eq 'selfenroll') {
1.351     raeburn  4582:         push(@{$brcrum},
                   4583:                 {href => '/adm/createuser?action=selfenroll',
                   4584:                  text => "Configure Self-enrollment",
                   4585:                  help => 'Course_Self_Enrollment'});
1.237     raeburn  4586:         if (!exists($env{'form.state'})) {
1.351     raeburn  4587:             $args = { bread_crumbs           => $brcrum,
                   4588:                       bread_crumbs_component => 'Configure Self-enrollment'};
                   4589:             $r->print(&header(undef,$args));
1.241     raeburn  4590:             $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
1.237     raeburn  4591:             &print_selfenroll_menu($r,$context,$permission);
                   4592:         } elsif ($env{'form.state'} eq 'done') {
1.351     raeburn  4593:             push (@{$brcrum},
                   4594:                       {href=>'/adm/createuser?action=selfenroll',
                   4595:                        text=>"Result"});
                   4596:             $args = { bread_crumbs           => $brcrum,
                   4597:                       bread_crumbs_component => 'Self-enrollment result'};
                   4598:             $r->print(&header(undef,$args));
1.241     raeburn  4599:             $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
                   4600:             &update_selfenroll_config($r,$context,$permission);
1.237     raeburn  4601:         }
1.277     raeburn  4602:     } elsif ($env{'form.action'} eq 'selfenrollqueue') {
1.351     raeburn  4603:         push(@{$brcrum},
                   4604:                  {href => '/adm/createuser?action=selfenrollqueue',
                   4605:                   text => 'Enrollment requests',
                   4606:                   help => 'Course_Self_Enrollment'});
                   4607:         $bread_crumbs_component = 'Enrollment requests';
                   4608:         if ($env{'form.state'} eq 'done') {
                   4609:             push(@{$brcrum},
                   4610:                      {href => '/adm/createuser?action=selfenrollqueue',
                   4611:                       text => 'Result',
                   4612:                       help => 'Course_Self_Enrollment'});
                   4613:             $bread_crumbs_component = 'Enrollment result';
                   4614:         }
                   4615:         $args = { bread_crumbs           => $brcrum,
                   4616:                   bread_crumbs_component => $bread_crumbs_component};
                   4617:         $r->print(&header(undef,$args));
1.277     raeburn  4618:         my $cid = $env{'request.course.id'};
                   4619:         my $cdom = $env{'course.'.$cid.'.domain'};
                   4620:         my $cnum = $env{'course.'.$cid.'.num'};
1.307     raeburn  4621:         my $coursedesc = $env{'course.'.$cid.'.description'};
1.277     raeburn  4622:         if (!exists($env{'form.state'})) {
                   4623:             $r->print('<h3>'.&mt('Pending enrollment requests').'</h3>'."\n");
1.307     raeburn  4624:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests($context,
                   4625:                                                                        $cdom,$cnum));
1.277     raeburn  4626:         } elsif ($env{'form.state'} eq 'done') {
                   4627:             $r->print('<h3>'.&mt('Enrollment request processing').'</h3>'."\n");
1.307     raeburn  4628:             $r->print(&Apache::loncoursequeueadmin::update_request_queue($context,
                   4629:                           $cdom,$cnum,$coursedesc));
1.277     raeburn  4630:         }
1.239     raeburn  4631:     } elsif ($env{'form.action'} eq 'changelogs') {
1.363     raeburn  4632:         my $helpitem;
                   4633:         if ($context eq 'course') {
                   4634:             $helpitem = 'Course_User_Logs';
                   4635:         }
1.351     raeburn  4636:         push (@{$brcrum},
                   4637:                  {href => '/adm/createuser?action=changelogs',
                   4638:                   text => 'User Management Logs',
1.363     raeburn  4639:                   help => $helpitem});
1.351     raeburn  4640:         $bread_crumbs_component = 'User Changes';
                   4641:         $args = { bread_crumbs           => $brcrum,
                   4642:                   bread_crumbs_component => $bread_crumbs_component};
                   4643:         $r->print(&header(undef,$args));
                   4644:         &print_userchangelogs_display($r,$context,$permission);
1.190     raeburn  4645:     } else {
1.351     raeburn  4646:         $bread_crumbs_component = 'User Management';
                   4647:         $args = { bread_crumbs           => $brcrum,
                   4648:                   bread_crumbs_component => $bread_crumbs_component};
                   4649:         $r->print(&header(undef,$args));
1.318     raeburn  4650:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4651:     }
1.351     raeburn  4652:     $r->print(&Apache::loncommon::end_page());
1.190     raeburn  4653:     return OK;
                   4654: }
                   4655: 
                   4656: sub header {
1.351     raeburn  4657:     my ($jscript,$args) = @_;
1.190     raeburn  4658:     my $start_page;
1.351     raeburn  4659:     if (ref($args) eq 'HASH') {
                   4660:         $start_page=&Apache::loncommon::start_page('User Management',$jscript,$args);
1.190     raeburn  4661:     } else {
1.351     raeburn  4662:         $start_page=&Apache::loncommon::start_page('User Management',$jscript);
1.190     raeburn  4663:     }
                   4664:     return $start_page;
                   4665: }
1.2       www      4666: 
1.191     raeburn  4667: sub add_script {
                   4668:     my ($js) = @_;
1.301     bisitz   4669:     return '<script type="text/javascript">'."\n"
                   4670:           .'// <![CDATA['."\n"
                   4671:           .$js."\n"
                   4672:           .'// ]]>'."\n"
                   4673:           .'</script>'."\n";
1.191     raeburn  4674: }
                   4675: 
1.202     raeburn  4676: sub verify_user_display {
1.364     raeburn  4677:     my ($context) = @_;
1.374     raeburn  4678:     my %lt = &Apache::lonlocal::texthash (
                   4679:         course    => 'course(s): description, section(s), status',
                   4680:         community => 'community(s): description, section(s), status',
                   4681:         author    => 'author',
                   4682:     );
1.364     raeburn  4683:     my $photos;
                   4684:     if (($context eq 'course') && $env{'request.course.id'}) {
                   4685:         $photos = $env{'course.'.$env{'request.course.id'}.'.internal.showphoto'};
                   4686:     }
1.202     raeburn  4687:     my $output = <<"END";
                   4688: 
1.364     raeburn  4689: function hide_searching() {
                   4690:     if (document.getElementById('searching')) {
                   4691:         document.getElementById('searching').style.display = 'none';
                   4692:     }
                   4693:     return;
                   4694: }
                   4695: 
1.202     raeburn  4696: function display_update() {
                   4697:     document.studentform.action.value = 'listusers';
                   4698:     document.studentform.phase.value = 'display';
                   4699:     document.studentform.submit();
                   4700: }
                   4701: 
1.364     raeburn  4702: function updateCols(caller) {
                   4703:     var context = '$context';
                   4704:     var photos = '$photos';
                   4705:     if (caller == 'Status') {
1.374     raeburn  4706:         if ((context == 'domain') && 
                   4707:             ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   4708:              (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community'))) {
1.364     raeburn  4709:             document.getElementById('showcolstatus').checked = false;
                   4710:             document.getElementById('showcolstatus').disabled = 'disabled';
                   4711:             document.getElementById('showcolstart').checked = false;
                   4712:             document.getElementById('showcolend').checked = false;
1.374     raeburn  4713:         } else {
                   4714:             if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   4715:                 document.getElementById('showcolstatus').checked = true;
                   4716:                 document.getElementById('showcolstatus').disabled = '';
                   4717:                 document.getElementById('showcolstart').checked = true;
                   4718:                 document.getElementById('showcolend').checked = true;
                   4719:             } else {
                   4720:                 document.getElementById('showcolstatus').checked = false;
                   4721:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   4722:                 document.getElementById('showcolstart').checked = false;
                   4723:                 document.getElementById('showcolend').checked = false;
                   4724:             }
1.364     raeburn  4725:         }
                   4726:     }
                   4727:     if (caller == 'output') {
                   4728:         if (photos == 1) {
                   4729:             if (document.getElementById('showcolphoto')) {
                   4730:                 var photoitem = document.getElementById('showcolphoto');
                   4731:                 if (document.studentform.output.options[document.studentform.output.selectedIndex].value == 'html') {
                   4732:                     photoitem.checked = true;
                   4733:                     photoitem.disabled = '';
                   4734:                 } else {
                   4735:                     photoitem.checked = false;
                   4736:                     photoitem.disabled = 'disabled';
                   4737:                 }
                   4738:             }
                   4739:         }
                   4740:     }
                   4741:     if (caller == 'showrole') {
1.371     raeburn  4742:         if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any') ||
                   4743:             (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'cr')) {
1.364     raeburn  4744:             document.getElementById('showcolrole').checked = true;
                   4745:             document.getElementById('showcolrole').disabled = '';
                   4746:         } else {
                   4747:             document.getElementById('showcolrole').checked = false;
                   4748:             document.getElementById('showcolrole').disabled = 'disabled';
                   4749:         }
1.374     raeburn  4750:         if (context == 'domain') {
                   4751:             if ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   4752:                 (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community')) {
                   4753:                 document.getElementById('showcolstatus').checked = false;
                   4754:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   4755:                 document.getElementById('showcolstart').checked = false;
                   4756:                 document.getElementById('showcolend').checked = false;
                   4757:             } else {
                   4758:                 if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   4759:                     document.getElementById('showcolstatus').checked = true;
                   4760:                     document.getElementById('showcolstatus').disabled = '';
                   4761:                     document.getElementById('showcolstart').checked = true;
                   4762:                     document.getElementById('showcolend').checked = true;
                   4763:                 }
                   4764:             }
                   4765:             if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'domain') {
                   4766:                 document.getElementById('showcolextent').disabled = 'disabled';
                   4767:                 document.getElementById('showcolextent').checked = 'false';
                   4768:                 document.getElementById('showextent').style.display='none';
                   4769:                 document.getElementById('showcoltextextent').innerHTML = '';
                   4770:             } else {
                   4771:                 document.getElementById('showextent').style.display='block';
                   4772:                 document.getElementById('showextent').style.textAlign='left';
                   4773:                 document.getElementById('showextent').style.textFace='normal';
                   4774:                 if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'author') {
                   4775:                     document.getElementById('showcolextent').disabled = '';
                   4776:                     document.getElementById('showcolextent').checked = 'true';
                   4777:                     document.getElementById('showcoltextextent').innerHTML="$lt{'author'}";
                   4778:                 } else {
                   4779:                     document.getElementById('showcolextent').disabled = '';
                   4780:                     document.getElementById('showcolextent').checked = 'true';
                   4781:                     if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community') {
                   4782:                         document.getElementById('showcoltextextent').innerHTML="$lt{'community'}";
                   4783:                     } else {
                   4784:                         document.getElementById('showcoltextextent').innerHTML="$lt{'course'}";
                   4785:                     }
                   4786:                 }
                   4787:             }
                   4788:         }
1.364     raeburn  4789:     }
                   4790:     return;
                   4791: }
                   4792: 
1.202     raeburn  4793: END
                   4794:     return $output;
                   4795: 
                   4796: }
                   4797: 
1.190     raeburn  4798: ###############################################################
                   4799: ###############################################################
                   4800: #  Menu Phase One
                   4801: sub print_main_menu {
1.318     raeburn  4802:     my ($permission,$context,$crstype) = @_;
                   4803:     my $linkcontext = $context;
                   4804:     my $stuterm = lc(&Apache::lonnet::plaintext('st',$crstype));
                   4805:     if (($context eq 'course') && ($crstype eq 'Community')) {
                   4806:         $linkcontext = lc($crstype);
                   4807:         $stuterm = 'Members';
                   4808:     }
1.208     raeburn  4809:     my %links = (
1.298     droeschl 4810:                 domain => {
                   4811:                             upload     => 'Upload a File of Users',
                   4812:                             singleuser => 'Add/Modify a User',
                   4813:                             listusers  => 'Manage Users',
                   4814:                             },
                   4815:                 author => {
                   4816:                             upload     => 'Upload a File of Co-authors',
                   4817:                             singleuser => 'Add/Modify a Co-author',
                   4818:                             listusers  => 'Manage Co-authors',
                   4819:                             },
                   4820:                 course => {
                   4821:                             upload     => 'Upload a File of Course Users',
                   4822:                             singleuser => 'Add/Modify a Course User',
1.354     www      4823:                             listusers  => 'List and Modify Multiple Course Users',
1.298     droeschl 4824:                             },
1.318     raeburn  4825:                 community => {
                   4826:                             upload     => 'Upload a File of Community Users',
                   4827:                             singleuser => 'Add/Modify a Community User',
1.354     www      4828:                             listusers  => 'List and Modify Multiple Community Users',
1.318     raeburn  4829:                            },
                   4830:                 );
                   4831:      my %linktitles = (
                   4832:                 domain => {
                   4833:                             singleuser => 'Add a user to the domain, and/or a course or community in the domain.',
                   4834:                             listusers  => 'Show and manage users in this domain.',
                   4835:                             },
                   4836:                 author => {
                   4837:                             singleuser => 'Add a user with a co- or assistant author role.',
                   4838:                             listusers  => 'Show and manage co- or assistant authors.',
                   4839:                             },
                   4840:                 course => {
                   4841:                             singleuser => 'Add a user with a certain role to this course.',
                   4842:                             listusers  => 'Show and manage users in this course.',
                   4843:                             },
                   4844:                 community => {
                   4845:                             singleuser => 'Add a user with a certain role to this community.',
                   4846:                             listusers  => 'Show and manage users in this community.',
                   4847:                            },
1.298     droeschl 4848:                 );
                   4849:   my @menu = ( {categorytitle => 'Single Users', 
                   4850:          items =>
                   4851:          [
                   4852:             {
1.318     raeburn  4853:              linktext => $links{$linkcontext}{'singleuser'},
1.298     droeschl 4854:              icon => 'edit-redo.png',
                   4855:              #help => 'Course_Change_Privileges',
                   4856:              url => '/adm/createuser?action=singleuser',
                   4857:              permission => $permission->{'cusr'},
1.318     raeburn  4858:              linktitle => $linktitles{$linkcontext}{'singleuser'},
1.298     droeschl 4859:             },
                   4860:          ]},
                   4861: 
                   4862:          {categorytitle => 'Multiple Users',
                   4863:          items => 
                   4864:          [
                   4865:             {
1.318     raeburn  4866:              linktext => $links{$linkcontext}{'upload'},
1.340     wenzelju 4867:              icon => 'uplusr.png',
1.298     droeschl 4868:              #help => 'Course_Create_Class_List',
                   4869:              url => '/adm/createuser?action=upload',
                   4870:              permission => $permission->{'cusr'},
                   4871:              linktitle => 'Upload a CSV or a text file containing users.',
                   4872:             },
                   4873:             {
1.318     raeburn  4874:              linktext => $links{$linkcontext}{'listusers'},
1.340     wenzelju 4875:              icon => 'mngcu.png',
1.298     droeschl 4876:              #help => 'Course_View_Class_List',
                   4877:              url => '/adm/createuser?action=listusers',
                   4878:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  4879:              linktitle => $linktitles{$linkcontext}{'listusers'}, 
1.298     droeschl 4880:             },
                   4881: 
                   4882:          ]},
                   4883: 
                   4884:          {categorytitle => 'Administration',
                   4885:          items => [ ]},
                   4886:        );
                   4887:             
1.265     mielkec  4888:     if ($context eq 'domain'){
1.298     droeschl 4889:         
                   4890:         push(@{ $menu[2]->{items} }, #Category: Administration
                   4891:             {
                   4892:              linktext => 'Custom Roles',
                   4893:              icon => 'emblem-photos.png',
                   4894:              #help => 'Course_Editing_Custom_Roles',
                   4895:              url => '/adm/createuser?action=custom',
                   4896:              permission => $permission->{'custom'},
                   4897:              linktitle => 'Configure a custom role.',
                   4898:             },
1.362     raeburn  4899:             {
                   4900:              linktext => 'Authoring Space Requests',
                   4901:              icon => 'selfenrl-queue.png',
                   4902:              #help => 'Domain_Role_Approvals',
                   4903:              url => '/adm/createuser?action=processauthorreq',
                   4904:              permission => $permission->{'cusr'},
                   4905:              linktitle => 'Approve or reject author role requests',
                   4906:             },
1.363     raeburn  4907:             {
                   4908:              linktext => 'Change Log',
                   4909:              icon => 'document-properties.png',
                   4910:              #help => 'Course_User_Logs',
                   4911:              url => '/adm/createuser?action=changelogs',
                   4912:              permission => $permission->{'cusr'},
                   4913:              linktitle => 'View change log.',
                   4914:             },
1.298     droeschl 4915:         );
                   4916:         
1.265     mielkec  4917:     }elsif ($context eq 'course'){
1.298     droeschl 4918:         my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
1.318     raeburn  4919: 
                   4920:         my %linktext = (
                   4921:                          'Course'    => {
                   4922:                                           single => 'Add/Modify a Student', 
                   4923:                                           drop   => 'Drop Students',
                   4924:                                           groups => 'Course Groups',
                   4925:                                         },
                   4926:                          'Community' => {
                   4927:                                           single => 'Add/Modify a Member', 
                   4928:                                           drop   => 'Drop Members',
                   4929:                                           groups => 'Community Groups',
                   4930:                                         },
                   4931:                        );
                   4932: 
                   4933:         my %linktitle = (
                   4934:             'Course' => {
                   4935:                   single => 'Add a user with the role of student to this course',
                   4936:                   drop   => 'Remove a student from this course.',
                   4937:                   groups => 'Manage course groups',
                   4938:                         },
                   4939:             'Community' => {
                   4940:                   single => 'Add a user with the role of member to this community',
                   4941:                   drop   => 'Remove a member from this community.',
                   4942:                   groups => 'Manage community groups',
                   4943:                            },
                   4944:         );
                   4945: 
1.298     droeschl 4946:         push(@{ $menu[0]->{items} }, #Category: Single Users
                   4947:             {   
1.318     raeburn  4948:              linktext => $linktext{$crstype}{'single'},
1.298     droeschl 4949:              #help => 'Course_Add_Student',
                   4950:              icon => 'list-add.png',
                   4951:              url => '/adm/createuser?action=singlestudent',
                   4952:              permission => $permission->{'cusr'},
1.318     raeburn  4953:              linktitle => $linktitle{$crstype}{'single'},
1.298     droeschl 4954:             },
                   4955:         );
                   4956:         
                   4957:         push(@{ $menu[1]->{items} }, #Category: Multiple Users 
                   4958:             {
1.318     raeburn  4959:              linktext => $linktext{$crstype}{'drop'},
1.298     droeschl 4960:              icon => 'edit-undo.png',
                   4961:              #help => 'Course_Drop_Student',
                   4962:              url => '/adm/createuser?action=drop',
                   4963:              permission => $permission->{'cusr'},
1.318     raeburn  4964:              linktitle => $linktitle{$crstype}{'drop'},
1.298     droeschl 4965:             },
                   4966:         );
                   4967:         push(@{ $menu[2]->{items} }, #Category: Administration
                   4968:             {    
                   4969:              linktext => 'Custom Roles',
                   4970:              icon => 'emblem-photos.png',
                   4971:              #help => 'Course_Editing_Custom_Roles',
                   4972:              url => '/adm/createuser?action=custom',
                   4973:              permission => $permission->{'custom'},
                   4974:              linktitle => 'Configure a custom role.',
                   4975:             },
                   4976:             {
1.318     raeburn  4977:              linktext => $linktext{$crstype}{'groups'},
1.333     wenzelju 4978:              icon => 'grps.png',
1.298     droeschl 4979:              #help => 'Course_Manage_Group',
                   4980:              url => '/adm/coursegroups?refpage=cusr',
                   4981:              permission => $permission->{'grp_manage'},
1.318     raeburn  4982:              linktitle => $linktitle{$crstype}{'groups'},
1.298     droeschl 4983:             },
                   4984:             {
1.328     wenzelju 4985:              linktext => 'Change Log',
1.298     droeschl 4986:              icon => 'document-properties.png',
                   4987:              #help => 'Course_User_Logs',
                   4988:              url => '/adm/createuser?action=changelogs',
                   4989:              permission => $permission->{'cusr'},
                   4990:              linktitle => 'View change log.',
                   4991:             },
                   4992:         );
1.277     raeburn  4993:         if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'}) {
1.298     droeschl 4994:             push(@{ $menu[2]->{items} },
                   4995:                     {   
                   4996:                      linktext => 'Enrollment Requests',
                   4997:                      icon => 'selfenrl-queue.png',
                   4998:                      #help => 'Course_Approve_Selfenroll',
                   4999:                      url => '/adm/createuser?action=selfenrollqueue',
                   5000:                      permission => $permission->{'cusr'},
                   5001:                      linktitle =>'Approve or reject enrollment requests.',
                   5002:                     },
                   5003:             );
1.277     raeburn  5004:         }
1.298     droeschl 5005:         
1.265     mielkec  5006:         if (!exists($permission->{'cusr_section'})){
1.320     raeburn  5007:             if ($crstype ne 'Community') {
                   5008:                 push(@{ $menu[2]->{items} },
                   5009:                     {
                   5010:                      linktext => 'Automated Enrollment',
                   5011:                      icon => 'roles.png',
                   5012:                      #help => 'Course_Automated_Enrollment',
                   5013:                      permission => (&Apache::lonnet::auto_run($cnum,$cdom)
                   5014:                                          && $permission->{'cusr'}),
                   5015:                      url  => '/adm/populate',
                   5016:                      linktitle => 'Automated enrollment manager.',
                   5017:                     }
                   5018:                 );
                   5019:             }
                   5020:             push(@{ $menu[2]->{items} }, 
1.298     droeschl 5021:                 {
                   5022:                  linktext => 'User Self-Enrollment',
1.342     wenzelju 5023:                  icon => 'self_enroll.png',
1.298     droeschl 5024:                  #help => 'Course_Self_Enrollment',
                   5025:                  url => '/adm/createuser?action=selfenroll',
                   5026:                  permission => $permission->{'cusr'},
1.317     bisitz   5027:                  linktitle => 'Configure user self-enrollment.',
1.298     droeschl 5028:                 },
                   5029:             );
                   5030:         }
1.363     raeburn  5031:     } elsif ($context eq 'author') {
1.370     raeburn  5032:         push(@{ $menu[2]->{items} }, #Category: Administration
1.363     raeburn  5033:             {
                   5034:              linktext => 'Change Log',
                   5035:              icon => 'document-properties.png',
                   5036:              #help => 'Course_User_Logs',
                   5037:              url => '/adm/createuser?action=changelogs',
                   5038:              permission => $permission->{'cusr'},
                   5039:              linktitle => 'View change log.',
                   5040:             },
1.370     raeburn  5041:         );
1.363     raeburn  5042:     }
                   5043:     return Apache::lonhtmlcommon::generate_menu(@menu);
1.250     raeburn  5044: #               { text => 'View Log-in History',
                   5045: #                 help => 'Course_User_Logins',
                   5046: #                 action => 'logins',
                   5047: #                 permission => $permission->{'cusr'},
                   5048: #               });
1.190     raeburn  5049: }
                   5050: 
1.189     albertel 5051: sub restore_prev_selections {
                   5052:     my %saveable_parameters = ('srchby'   => 'scalar',
                   5053: 			       'srchin'   => 'scalar',
                   5054: 			       'srchtype' => 'scalar',
                   5055: 			       );
                   5056:     &Apache::loncommon::store_settings('user','user_picker',
                   5057: 				       \%saveable_parameters);
                   5058:     &Apache::loncommon::restore_settings('user','user_picker',
                   5059: 					 \%saveable_parameters);
                   5060: }
                   5061: 
1.237     raeburn  5062: sub print_selfenroll_menu {
                   5063:     my ($r,$context,$permission) = @_;
1.322     raeburn  5064:     my $crstype = &Apache::loncommon::course_type();
1.237     raeburn  5065:     my $formname = 'enrollstudent';
                   5066:     my $nolink = 1;
                   5067:     my ($row,$lt) = &get_selfenroll_titles();
                   5068:     my $groupslist = &Apache::lonuserutils::get_groupslist();
                   5069:     my $setsec_js = 
                   5070:         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
1.249     raeburn  5071:     my %alerts = &Apache::lonlocal::texthash(
                   5072:         acto => 'Activation of self-enrollment was selected for the following domain(s)',
                   5073:         butn => 'but no user types have been checked.',
                   5074:         wilf => "Please uncheck 'activate' or check at least one type.",
                   5075:     );
                   5076:     my $selfenroll_js = <<"ENDSCRIPT";
                   5077: function update_types(caller,num) {
                   5078:     var delidx = getIndexByName('selfenroll_delete');
                   5079:     var actidx = getIndexByName('selfenroll_activate');
                   5080:     if (caller == 'selfenroll_all') {
                   5081:         var selall;
                   5082:         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5083:             if (document.$formname.selfenroll_all[i].checked) {
                   5084:                 selall = document.$formname.selfenroll_all[i].value;
                   5085:             }
                   5086:         }
                   5087:         if (selall == 1) {
                   5088:             if (delidx != -1) {
                   5089:                 if (document.$formname.selfenroll_delete.length) {
                   5090:                     for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5091:                         document.$formname.selfenroll_delete[j].checked = true;
                   5092:                     }
                   5093:                 } else {
                   5094:                     document.$formname.elements[delidx].checked = true;
                   5095:                 }
                   5096:             }
                   5097:             if (actidx != -1) {
                   5098:                 if (document.$formname.selfenroll_activate.length) {
                   5099:                     for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5100:                         document.$formname.selfenroll_activate[j].checked = false;
                   5101:                     }
                   5102:                 } else {
                   5103:                     document.$formname.elements[actidx].checked = false;
                   5104:                 }
                   5105:             }
                   5106:             document.$formname.selfenroll_newdom.selectedIndex = 0; 
                   5107:         }
                   5108:     }
                   5109:     if (caller == 'selfenroll_activate') {
                   5110:         if (document.$formname.selfenroll_activate.length) {
                   5111:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5112:                 if (document.$formname.selfenroll_activate[j].value == num) {
                   5113:                     if (document.$formname.selfenroll_activate[j].checked) {
                   5114:                         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5115:                             if (document.$formname.selfenroll_all[i].value == '1') {
                   5116:                                 document.$formname.selfenroll_all[i].checked = false;
                   5117:                             }
                   5118:                             if (document.$formname.selfenroll_all[i].value == '0') {
                   5119:                                 document.$formname.selfenroll_all[i].checked = true;
                   5120:                             }
                   5121:                         }
                   5122:                     }
                   5123:                 }
                   5124:             }
                   5125:         } else {
                   5126:             for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5127:                 if (document.$formname.selfenroll_all[i].value == '1') {
                   5128:                     document.$formname.selfenroll_all[i].checked = false;
                   5129:                 }
                   5130:                 if (document.$formname.selfenroll_all[i].value == '0') {
                   5131:                     document.$formname.selfenroll_all[i].checked = true;
                   5132:                 }
                   5133:             }
                   5134:         }
                   5135:     }
                   5136:     if (caller == 'selfenroll_delete') {
                   5137:         if (document.$formname.selfenroll_delete.length) {
                   5138:             for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5139:                 if (document.$formname.selfenroll_delete[j].value == num) {
                   5140:                     if (document.$formname.selfenroll_delete[j].checked) {
                   5141:                         var delindex = getIndexByName('selfenroll_types_'+num);
                   5142:                         if (delindex != -1) { 
                   5143:                             if (document.$formname.elements[delindex].length) {
                   5144:                                 for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5145:                                     document.$formname.elements[delindex][k].checked = false;
                   5146:                                 }
                   5147:                             } else {
                   5148:                                 document.$formname.elements[delindex].checked = false;
                   5149:                             }
                   5150:                         }
                   5151:                     }
                   5152:                 }
                   5153:             }
                   5154:         } else {
                   5155:             if (document.$formname.selfenroll_delete.checked) {
                   5156:                 var delindex = getIndexByName('selfenroll_types_'+num);
                   5157:                 if (delindex != -1) {
                   5158:                     if (document.$formname.elements[delindex].length) {
                   5159:                         for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5160:                             document.$formname.elements[delindex][k].checked = false;
                   5161:                         }
                   5162:                     } else {
                   5163:                         document.$formname.elements[delindex].checked = false;
                   5164:                     }
                   5165:                 }
                   5166:             }
                   5167:         }
                   5168:     }
                   5169:     return;
                   5170: }
                   5171: 
                   5172: function validate_types(form) {
                   5173:     var needaction = new Array();
                   5174:     var countfail = 0;
                   5175:     var actidx = getIndexByName('selfenroll_activate');
                   5176:     if (actidx != -1) {
                   5177:         if (document.$formname.selfenroll_activate.length) {
                   5178:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5179:                 var num = document.$formname.selfenroll_activate[j].value;
                   5180:                 if (document.$formname.selfenroll_activate[j].checked) {
                   5181:                     countfail = check_types(num,countfail,needaction)
                   5182:                 }
                   5183:             }
                   5184:         } else {
                   5185:             if (document.$formname.selfenroll_activate.checked) {
                   5186:                 var num = document.enrollstudent.selfenroll_activate.value;
                   5187:                 countfail = check_types(num,countfail,needaction)
                   5188:             }
                   5189:         }
                   5190:     }
                   5191:     if (countfail > 0) {
                   5192:         var msg = "$alerts{'acto'}\\n";
                   5193:         var loopend = needaction.length -1;
                   5194:         if (loopend > 0) {
                   5195:             for (var m=0; m<loopend; m++) {
                   5196:                 msg += needaction[m]+", ";
                   5197:             }
                   5198:         }
                   5199:         msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
                   5200:         alert(msg);
                   5201:         return; 
                   5202:     }
                   5203:     setSections(form);
                   5204: }
                   5205: 
                   5206: function check_types(num,countfail,needaction) {
                   5207:     var typeidx = getIndexByName('selfenroll_types_'+num);
                   5208:     var count = 0;
                   5209:     if (typeidx != -1) {
                   5210:         if (document.$formname.elements[typeidx].length) {
                   5211:             for (var k=0; k<document.$formname.elements[typeidx].length; k++) {
                   5212:                 if (document.$formname.elements[typeidx][k].checked) {
                   5213:                     count ++;
                   5214:                 }
                   5215:             }
                   5216:         } else {
                   5217:             if (document.$formname.elements[typeidx].checked) {
                   5218:                 count ++;
                   5219:             }
                   5220:         }
                   5221:         if (count == 0) {
                   5222:             var domidx = getIndexByName('selfenroll_dom_'+num);
                   5223:             if (domidx != -1) {
                   5224:                 var domname = document.$formname.elements[domidx].value;
                   5225:                 needaction[countfail] = domname;
                   5226:                 countfail ++;
                   5227:             }
                   5228:         }
                   5229:     }
                   5230:     return countfail;
                   5231: }
                   5232: 
                   5233: function getIndexByName(item) {
                   5234:     for (var i=0;i<document.$formname.elements.length;i++) {
                   5235:         if (document.$formname.elements[i].name == item) {
                   5236:             return i;
                   5237:         }
                   5238:     }
                   5239:     return -1;
                   5240: }
                   5241: ENDSCRIPT
1.256     raeburn  5242:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5243:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   5244: 
1.237     raeburn  5245:     my $output = '<script type="text/javascript">'."\n".
1.301     bisitz   5246:                  '// <![CDATA['."\n".
1.249     raeburn  5247:                  $setsec_js."\n".$selfenroll_js."\n".
1.301     bisitz   5248:                  '// ]]>'."\n".
1.237     raeburn  5249:                  '</script>'."\n".
1.256     raeburn  5250:                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n";
                   5251:     my ($visible,$cansetvis,$vismsgs,$visactions) = &visible_in_cat($cdom,$cnum);
                   5252:     if (ref($visactions) eq 'HASH') {
                   5253:         if ($visible) {
1.283     bisitz   5254:             $output .= '<p class="LC_info">'.$visactions->{'vis'}.'</p>';
1.256     raeburn  5255:         } else {
1.283     bisitz   5256:             $output .= '<p class="LC_warning">'.$visactions->{'miss'}.'</p>'
                   5257:                       .$visactions->{'yous'}.
1.256     raeburn  5258:                        '<p>'.$visactions->{'gen'}.'<br />'.$visactions->{'coca'};
                   5259:             if (ref($vismsgs) eq 'ARRAY') {
                   5260:                 $output .= '<br />'.$visactions->{'make'}.'<ul>';
                   5261:                 foreach my $item (@{$vismsgs}) {
                   5262:                     $output .= '<li>'.$visactions->{$item}.'</li>';
                   5263:                 }
                   5264:                 $output .= '</ul>';
                   5265:             }
                   5266:             $output .= '</p>';
                   5267:         }
                   5268:     }
                   5269:     $output .= '<form name="'.$formname.'" method="post" action="/adm/createuser">'."\n".
                   5270:                &Apache::lonhtmlcommon::start_pick_box();
1.237     raeburn  5271:     if (ref($row) eq 'ARRAY') {
                   5272:         foreach my $item (@{$row}) {
                   5273:             my $title = $item; 
                   5274:             if (ref($lt) eq 'HASH') {
                   5275:                 $title = $lt->{$item};
                   5276:             }
1.297     bisitz   5277:             $output .= &Apache::lonhtmlcommon::row_title($title);
1.237     raeburn  5278:             if ($item eq 'types') {
                   5279:                 my $curr_types = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_types'};
1.241     raeburn  5280:                 my $showdomdesc = 1;
                   5281:                 my $includeempty = 1;
                   5282:                 my $num = 0;
                   5283:                 $output .= &Apache::loncommon::start_data_table().
                   5284:                            &Apache::loncommon::start_data_table_row()
                   5285:                            .'<td colspan="2"><span class="LC_nobreak"><label>'
                   5286:                            .&mt('Any user in any domain:')
                   5287:                            .'&nbsp;<input type="radio" name="selfenroll_all" value="1" ';
                   5288:                 if ($curr_types eq '*') {
                   5289:                     $output .= ' checked="checked" '; 
                   5290:                 }
1.249     raeburn  5291:                 $output .= 'onchange="javascript:update_types('.
                   5292:                            "'selfenroll_all'".');" />'.&mt('Yes').'</label>'.
                   5293:                            '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
1.241     raeburn  5294:                 if ($curr_types ne '*') {
                   5295:                     $output .= ' checked="checked" ';
                   5296:                 }
1.249     raeburn  5297:                 $output .= ' onchange="javascript:update_types('.
                   5298:                            "'selfenroll_all'".');"/>'.&mt('No').'</label></td>'.
                   5299:                            &Apache::loncommon::end_data_table_row().
                   5300:                            &Apache::loncommon::end_data_table().
                   5301:                            &mt('Or').'<br />'.
                   5302:                            &Apache::loncommon::start_data_table();
1.241     raeburn  5303:                 my %currdoms;
1.249     raeburn  5304:                 if ($curr_types eq '') {
1.241     raeburn  5305:                     $output .= &new_selfenroll_dom_row($cdom,'0');
                   5306:                 } elsif ($curr_types ne '*') {
                   5307:                     my @entries = split(/;/,$curr_types);
                   5308:                     if (@entries > 0) {
                   5309:                         foreach my $entry (@entries) {
                   5310:                             my ($currdom,$typestr) = split(/:/,$entry);
                   5311:                             $currdoms{$currdom} = 1;
                   5312:                             my $domdesc = &Apache::lonnet::domain($currdom);
1.249     raeburn  5313:                             my @currinsttypes = split(',',$typestr);
1.241     raeburn  5314:                             $output .= &Apache::loncommon::start_data_table_row()
                   5315:                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
                   5316:                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
                   5317:                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
                   5318:                                        .'" value="'.$currdom.'" /></span><br />'
                   5319:                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
1.249     raeburn  5320:                                        .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');" />'
1.241     raeburn  5321:                                        .&mt('Delete').'</label></span></td>';
1.249     raeburn  5322:                             $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
1.241     raeburn  5323:                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes).'</td>'
                   5324:                                        .&Apache::loncommon::end_data_table_row();
                   5325:                             $num ++;
                   5326:                         }
                   5327:                     }
                   5328:                 }
1.249     raeburn  5329:                 my $add_domtitle = &mt('Users in additional domain:');
1.241     raeburn  5330:                 if ($curr_types eq '*') { 
1.249     raeburn  5331:                     $add_domtitle = &mt('Users in specific domain:');
1.241     raeburn  5332:                 } elsif ($curr_types eq '') {
1.249     raeburn  5333:                     $add_domtitle = &mt('Users in other domain:');
1.241     raeburn  5334:                 }
                   5335:                 $output .= &Apache::loncommon::start_data_table_row()
                   5336:                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
                   5337:                            .&Apache::loncommon::select_dom_form('','selfenroll_newdom',
                   5338:                                                                 $includeempty,$showdomdesc)
                   5339:                            .'<input type="hidden" name="selfenroll_types_total" value="'.$num.'" />'
                   5340:                            .'</td>'.&Apache::loncommon::end_data_table_row()
                   5341:                            .&Apache::loncommon::end_data_table();
1.237     raeburn  5342:             } elsif ($item eq 'registered') {
                   5343:                 my ($regon,$regoff);
                   5344:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_registered'}) {
                   5345:                     $regon = ' checked="checked" ';
                   5346:                     $regoff = ' ';
                   5347:                 } else {
                   5348:                     $regon = ' ';
                   5349:                     $regoff = ' checked="checked" ';
                   5350:                 }
                   5351:                 $output .= '<label>'.
1.245     raeburn  5352:                            '<input type="radio" name="selfenroll_registered" value="1"'.$regon.'/>'.
1.244     bisitz   5353:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
1.245     raeburn  5354:                            '<input type="radio" name="selfenroll_registered" value="0"'.$regoff.'/>'.
1.244     bisitz   5355:                            &mt('No').'</label>';
1.237     raeburn  5356:             } elsif ($item eq 'enroll_dates') {
                   5357:                 my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_date'};
                   5358:                 my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_date'};
                   5359:                 if ($starttime eq '') {
                   5360:                     $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   5361:                 }
                   5362:                 if ($endtime eq '') {
                   5363:                     $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   5364:                 }
                   5365:                 my $startform =
                   5366:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
                   5367:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5368:                 my $endform =
                   5369:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
                   5370:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5371:                 $output .= &selfenroll_date_forms($startform,$endform);
                   5372:             } elsif ($item eq 'access_dates') {
                   5373:                 my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_access'};
                   5374:                 my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_access'};
                   5375:                 if ($starttime eq '') {
                   5376:                     $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   5377:                 }
                   5378:                 if ($endtime eq '') {
                   5379:                     $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   5380:                 }
                   5381:                 my $startform =
                   5382:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
                   5383:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5384:                 my $endform =
                   5385:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
                   5386:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5387:                 $output .= &selfenroll_date_forms($startform,$endform);
                   5388:             } elsif ($item eq 'section') {
                   5389:                 my $currsec = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_section'}; 
                   5390:                 my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   5391:                 my $newsecval;
                   5392:                 if ($currsec ne 'none' && $currsec ne '') {
                   5393:                     if (!defined($sections_count{$currsec})) {
                   5394:                         $newsecval = $currsec;
                   5395:                     }
                   5396:                 }
                   5397:                 my $sections_select = 
                   5398:                     &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec);
                   5399:                 $output .= '<table class="LC_createuser">'."\n".
                   5400:                            '<tr class="LC_section_row">'."\n".
                   5401:                            '<td align="center">'.&mt('Existing sections')."\n".
                   5402:                            '<br />'.$sections_select.'</td><td align="center">'.
                   5403:                            &mt('New section').'<br />'."\n".
                   5404:                            '<input type="text" name="newsec" size="15" value="'.$newsecval.'" />'."\n".
                   5405:                            '<input type="hidden" name="sections" value="" />'."\n".
                   5406:                            '<input type="hidden" name="state" value="done" />'."\n".
                   5407:                            '</td></tr></table>'."\n";
1.276     raeburn  5408:             } elsif ($item eq 'approval') {
                   5409:                 my ($appon,$appoff);
                   5410:                 my $cid = $env{'request.course.id'};
                   5411:                 my $currnotified = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
                   5412:                 if ($env{'course.'.$cid.'.internal.selfenroll_approval'}) {
                   5413:                     $appon = ' checked="checked" ';
                   5414:                     $appoff = ' ';
                   5415:                 } else {
                   5416:                     $appon = ' ';
                   5417:                     $appoff = ' checked="checked" ';
                   5418:                 }
                   5419:                 $output .= '<label>'.
                   5420:                            '<input type="radio" name="selfenroll_approval" value="1"'.$appon.'/>'.
                   5421:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
                   5422:                            '<input type="radio" name="selfenroll_approval" value="0"'.$appoff.'/>'.
                   5423:                            &mt('No').'</label>';
                   5424:                 my %advhash = &Apache::lonnet::get_course_adv_roles($cid,1);
                   5425:                 my (@ccs,%notified);
1.322     raeburn  5426:                 my $ccrole = 'cc';
                   5427:                 if ($crstype eq 'Community') {
                   5428:                     $ccrole = 'co';
                   5429:                 }
                   5430:                 if ($advhash{$ccrole}) {
                   5431:                     @ccs = split(/,/,$advhash{$ccrole});
1.276     raeburn  5432:                 }
                   5433:                 if ($currnotified) {
                   5434:                     foreach my $current (split(/,/,$currnotified)) {
                   5435:                         $notified{$current} = 1;
                   5436:                         if (!grep(/^\Q$current\E$/,@ccs)) {
                   5437:                             push(@ccs,$current);
                   5438:                         }
                   5439:                     }
                   5440:                 }
                   5441:                 if (@ccs) {
1.277     raeburn  5442:                     $output .= '<br />'.&mt('Personnel to be notified when an enrollment request needs approval, or has been approved:').'&nbsp;'.&Apache::loncommon::start_data_table().
1.276     raeburn  5443:                                &Apache::loncommon::start_data_table_row();
                   5444:                     my $count = 0;
                   5445:                     my $numcols = 4;
                   5446:                     foreach my $cc (sort(@ccs)) {
                   5447:                         my $notifyon;
                   5448:                         my ($ccuname,$ccudom) = split(/:/,$cc);
                   5449:                         if ($notified{$cc}) {
                   5450:                             $notifyon = ' checked="checked" ';
                   5451:                         }
                   5452:                         if ($count && !$count%$numcols) {
                   5453:                             $output .= &Apache::loncommon::end_data_table_row().
                   5454:                                        &Apache::loncommon::start_data_table_row()
                   5455:                         }
                   5456:                         $output .= '<td><span class="LC_nobreak"><label>'.
                   5457:                                    '<input type="checkbox" name="selfenroll_notify"'.$notifyon.' value="'.$cc.'" />'.
                   5458:                                    &Apache::loncommon::plainname($ccuname,$ccudom).
                   5459:                                    '</label></span></td>';
1.343     raeburn  5460:                         $count ++;
1.276     raeburn  5461:                     }
                   5462:                     my $rem = $count%$numcols;
                   5463:                     if ($rem) {
                   5464:                         my $emptycols = $numcols - $rem;
                   5465:                         for (my $i=0; $i<$emptycols; $i++) { 
                   5466:                             $output .= '<td>&nbsp;</td>';
                   5467:                         }
                   5468:                     }
                   5469:                     $output .= &Apache::loncommon::end_data_table_row().
                   5470:                                &Apache::loncommon::end_data_table();
                   5471:                 }
                   5472:             } elsif ($item eq 'limit') {
                   5473:                 my ($crslimit,$selflimit,$nolimit);
                   5474:                 my $cid = $env{'request.course.id'};
                   5475:                 my $currlim = $env{'course.'.$cid.'.internal.selfenroll_limit'};
                   5476:                 my $currcap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
1.343     raeburn  5477:                 $nolimit = ' checked="checked" ';
1.276     raeburn  5478:                 if ($currlim eq 'allstudents') {
                   5479:                     $crslimit = ' checked="checked" ';
                   5480:                     $selflimit = ' ';
                   5481:                     $nolimit = ' ';
                   5482:                 } elsif ($currlim eq 'selfenrolled') {
                   5483:                     $crslimit = ' ';
                   5484:                     $selflimit = ' checked="checked" ';
                   5485:                     $nolimit = ' '; 
                   5486:                 } else {
                   5487:                     $crslimit = ' ';
                   5488:                     $selflimit = ' ';
                   5489:                 }
                   5490:                 $output .= '<table><tr><td><label>'.
1.278     raeburn  5491:                            '<input type="radio" name="selfenroll_limit" value="none"'.$nolimit.'/>'.
1.276     raeburn  5492:                            &mt('No limit').'</label></td><td><label>'.
                   5493:                            '<input type="radio" name="selfenroll_limit" value="allstudents"'.$crslimit.'/>'.
                   5494:                            &mt('Limit by total students').'</label></td><td><label>'.
                   5495:                            '<input type="radio" name="selfenroll_limit" value="selfenrolled"'.$selflimit.'/>'.
                   5496:                            &mt('Limit by total self-enrolled students').
                   5497:                            '</td></tr><tr>'.
                   5498:                            '<td>&nbsp;</td><td colspan="2"><span class="LC_nobreak">'.
                   5499:                            ('&nbsp;'x3).&mt('Maximum number allowed: ').
                   5500:                            '<input type="text" name="selfenroll_cap" size = "5" value="'.$currcap.'" /></td></tr></table>';
1.237     raeburn  5501:             }
                   5502:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   5503:         }
                   5504:     }
                   5505:     $output .= &Apache::lonhtmlcommon::end_pick_box().
1.241     raeburn  5506:                '<br /><input type="button" name="selfenrollconf" value="'
1.282     schafran 5507:                .&mt('Save').'" onclick="validate_types(this.form);" />'
1.241     raeburn  5508:                .'<input type="hidden" name="action" value="selfenroll" /></form>';
1.237     raeburn  5509:     $r->print($output);
                   5510:     return;
                   5511: }
                   5512: 
1.256     raeburn  5513: sub visible_in_cat {
                   5514:     my ($cdom,$cnum) = @_;
                   5515:     my %domconf = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   5516:     my ($cathash,%settable,@vismsgs,$cansetvis);
                   5517:     my %visactions = &Apache::lonlocal::texthash(
1.316     bisitz   5518:                    vis => 'Your course/community currently appears in the Course/Community Catalog for this domain.',
1.256     raeburn  5519:                    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.',
1.316     bisitz   5520:                    miss => 'Your course/community does not currently appear in the Course/Community Catalog for this domain.',
1.256     raeburn  5521:                    yous => 'You should remedy this if you plan to allow self-enrollment, otherwise students will have difficulty finding your course.',
                   5522:                    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.',
1.282     schafran 5523:                    make => 'Make any changes to self-enrollment settings below, click "Save", then take action to include the course in the Catalog:',
1.256     raeburn  5524:                    take => 'Take the following action to ensure the course appears in the Catalog:',
                   5525:                    dc_unhide  => 'Ask a domain coordinator to change the "Exclude from course catalog" setting.',
                   5526:                    dc_addinst => 'Ask a domain coordinator to enable display the catalog of "Official courses (with institutional codes)".',
                   5527:                    dc_instcode => 'Ask a domain coordinator to assign an institutional code (if this is an official course).',
                   5528:                    dc_catalog  => 'Ask a domain coordinator to enable or create at least one course category in the domain.',
                   5529:                    dc_categories => 'Ask a domain coordinator to create a hierarchy of categories and sub categories for courses in the domain.',
                   5530:                    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',
                   5531:                    dc_addcat => 'Ask a domain coordinator to assign a category to the course.',
                   5532:     );
1.347     raeburn  5533:     $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>"');
                   5534:     $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>"');
                   5535:     $visactions{'addcat'} = &mt('Use [_1]Categorize course[_2] to assign a category to the course.','"<a href="/adm/courseprefs?phase=display&actions=courseinfo">','</a>"');
1.256     raeburn  5536:     if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5537:         if ($domconf{'coursecategories'}{'togglecats'} eq 'crs') {
                   5538:             $settable{'togglecats'} = 1;
                   5539:         }
                   5540:         if ($domconf{'coursecategories'}{'categorize'} eq 'crs') {
                   5541:             $settable{'categorize'} = 1;
                   5542:         }
                   5543:         $cathash = $domconf{'coursecategories'}{'cats'};
                   5544:     }
1.260     raeburn  5545:     if ($settable{'togglecats'} && $settable{'categorize'}) {
1.256     raeburn  5546:         $cansetvis = &mt('You are able to both assign a course category and choose to exclude this course from the catalog.');   
                   5547:     } elsif ($settable{'togglecats'}) {
                   5548:         $cansetvis = &mt('You are able to choose to exclude this course from the catalog, but only a Domain Coordinator may assign a course category.'); 
1.260     raeburn  5549:     } elsif ($settable{'categorize'}) {
1.256     raeburn  5550:         $cansetvis = &mt('You may assign a course category, but only a Domain Coordinator may choose to exclude this course from the catalog.');  
                   5551:     } else {
                   5552:         $cansetvis = &mt('Only a Domain Coordinator may assign a course category or choose to exclude this course from the catalog.'); 
                   5553:     }
                   5554:      
                   5555:     my %currsettings =
                   5556:         &Apache::lonnet::get('environment',['hidefromcat','categories','internal.coursecode'],
                   5557:                              $cdom,$cnum);
                   5558:     my $visible = 0;
                   5559:     if ($currsettings{'internal.coursecode'} ne '') {
                   5560:         if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5561:             $cathash = $domconf{'coursecategories'}{'cats'};
                   5562:             if (ref($cathash) eq 'HASH') {
                   5563:                 if ($cathash->{'instcode::0'} eq '') {
                   5564:                     push(@vismsgs,'dc_addinst'); 
                   5565:                 } else {
                   5566:                     $visible = 1;
                   5567:                 }
                   5568:             } else {
                   5569:                 $visible = 1;
                   5570:             }
                   5571:         } else {
                   5572:             $visible = 1;
                   5573:         }
                   5574:     } else {
                   5575:         if (ref($cathash) eq 'HASH') {
                   5576:             if ($cathash->{'instcode::0'} ne '') {
                   5577:                 push(@vismsgs,'dc_instcode');
                   5578:             }
                   5579:         } else {
                   5580:             push(@vismsgs,'dc_instcode');
                   5581:         }
                   5582:     }
                   5583:     if ($currsettings{'categories'} ne '') {
                   5584:         my $cathash;
                   5585:         if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5586:             $cathash = $domconf{'coursecategories'}{'cats'};
                   5587:             if (ref($cathash) eq 'HASH') {
                   5588:                 if (keys(%{$cathash}) == 0) {
                   5589:                     push(@vismsgs,'dc_catalog');
                   5590:                 } elsif ((keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} ne '')) {
                   5591:                     push(@vismsgs,'dc_categories');
                   5592:                 } else {
                   5593:                     my @currcategories = split('&',$currsettings{'categories'});
                   5594:                     my $matched = 0;
                   5595:                     foreach my $cat (@currcategories) {
                   5596:                         if ($cathash->{$cat} ne '') {
                   5597:                             $visible = 1;
                   5598:                             $matched = 1;
                   5599:                             last;
                   5600:                         }
                   5601:                     }
                   5602:                     if (!$matched) {
1.260     raeburn  5603:                         if ($settable{'categorize'}) { 
1.256     raeburn  5604:                             push(@vismsgs,'chgcat');
                   5605:                         } else {
                   5606:                             push(@vismsgs,'dc_chgcat');
                   5607:                         }
                   5608:                     }
                   5609:                 }
                   5610:             }
                   5611:         }
                   5612:     } else {
                   5613:         if (ref($cathash) eq 'HASH') {
                   5614:             if ((keys(%{$cathash}) > 1) || 
                   5615:                 (keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} eq '')) {
1.260     raeburn  5616:                 if ($settable{'categorize'}) {
1.256     raeburn  5617:                     push(@vismsgs,'addcat');
                   5618:                 } else {
                   5619:                     push(@vismsgs,'dc_addcat');
                   5620:                 }
                   5621:             }
                   5622:         }
                   5623:     }
                   5624:     if ($currsettings{'hidefromcat'} eq 'yes') {
                   5625:         $visible = 0;
                   5626:         if ($settable{'togglecats'}) {
                   5627:             unshift(@vismsgs,'unhide');
                   5628:         } else {
                   5629:             unshift(@vismsgs,'dc_unhide')
                   5630:         }
                   5631:     }
                   5632:     return ($visible,$cansetvis,\@vismsgs,\%visactions);
                   5633: }
                   5634: 
1.241     raeburn  5635: sub new_selfenroll_dom_row {
                   5636:     my ($newdom,$num) = @_;
                   5637:     my $domdesc = &Apache::lonnet::domain($newdom);
                   5638:     my $output;
                   5639:     if ($domdesc ne '') {
                   5640:         $output .= &Apache::loncommon::start_data_table_row()
                   5641:                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
                   5642:                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
1.249     raeburn  5643:                    .'" value="'.$newdom.'" /></span><br />'
                   5644:                    .'<span class="LC_nobreak"><label><input type="checkbox" '
                   5645:                    .'name="selfenroll_activate" value="'.$num.'" '
                   5646:                    .'onchange="javascript:update_types('
                   5647:                    ."'selfenroll_activate','$num'".');" />'
                   5648:                    .&mt('Activate').'</label></span></td>';
1.241     raeburn  5649:         my @currinsttypes;
                   5650:         $output .= '<td>'.&mt('User types:').'<br />'
                   5651:                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
                   5652:                    .&Apache::loncommon::end_data_table_row();
                   5653:     }
                   5654:     return $output;
                   5655: }
                   5656: 
                   5657: sub selfenroll_inst_types {
                   5658:     my ($num,$currdom,$currinsttypes) = @_;
                   5659:     my $output;
                   5660:     my $numinrow = 4;
                   5661:     my $count = 0;
                   5662:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($currdom);
1.247     raeburn  5663:     my $othervalue = 'any';
1.241     raeburn  5664:     if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
1.251     raeburn  5665:         if (keys(%{$usertypes}) > 0) {
1.247     raeburn  5666:             $othervalue = 'other';
                   5667:         }
1.241     raeburn  5668:         $output .= '<table><tr>';
                   5669:         foreach my $type (@{$types}) {
                   5670:             if (($count > 0) && ($count%$numinrow == 0)) {
                   5671:                 $output .= '</tr><tr>';
                   5672:             }
                   5673:             if (defined($usertypes->{$type})) {
1.257     raeburn  5674:                 my $esc_type = &escape($type);
1.241     raeburn  5675:                 $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.
1.257     raeburn  5676:                            $esc_type.'" ';
1.241     raeburn  5677:                 if (ref($currinsttypes) eq 'ARRAY') {
                   5678:                     if (@{$currinsttypes} > 0) {
1.249     raeburn  5679:                         if (grep(/^any$/,@{$currinsttypes})) {
                   5680:                             $output .= 'checked="checked"';
1.257     raeburn  5681:                         } elsif (grep(/^\Q$esc_type\E$/,@{$currinsttypes})) {
1.241     raeburn  5682:                             $output .= 'checked="checked"';
                   5683:                         }
1.249     raeburn  5684:                     } else {
                   5685:                         $output .= 'checked="checked"';
1.241     raeburn  5686:                     }
                   5687:                 }
                   5688:                 $output .= ' name="selfenroll_types_'.$num.'" />'.$usertypes->{$type}.'</label></span></td>';
                   5689:             }
                   5690:             $count ++;
                   5691:         }
                   5692:         if (($count > 0) && ($count%$numinrow == 0)) {
                   5693:             $output .= '</tr><tr>';
                   5694:         }
1.249     raeburn  5695:         $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
1.241     raeburn  5696:         if (ref($currinsttypes) eq 'ARRAY') {
                   5697:             if (@{$currinsttypes} > 0) {
1.249     raeburn  5698:                 if (grep(/^any$/,@{$currinsttypes})) { 
                   5699:                     $output .= ' checked="checked"';
                   5700:                 } elsif ($othervalue eq 'other') {
                   5701:                     if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
                   5702:                         $output .= ' checked="checked"';
                   5703:                     }
1.241     raeburn  5704:                 }
1.249     raeburn  5705:             } else {
                   5706:                 $output .= ' checked="checked"';
1.241     raeburn  5707:             }
1.249     raeburn  5708:         } else {
                   5709:             $output .= ' checked="checked"';
1.241     raeburn  5710:         }
                   5711:         $output .= ' name="selfenroll_types_'.$num.'" />'.$othertitle.'</label></span></td></tr></table>';
                   5712:     }
                   5713:     return $output;
                   5714: }
                   5715: 
1.237     raeburn  5716: sub selfenroll_date_forms {
                   5717:     my ($startform,$endform) = @_;
                   5718:     my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
1.244     bisitz   5719:                   &Apache::lonhtmlcommon::row_title(&mt('Start date'),
1.237     raeburn  5720:                                                     'LC_oddrow_value')."\n".
                   5721:                   $startform."\n".
                   5722:                   &Apache::lonhtmlcommon::row_closure(1).
1.244     bisitz   5723:                   &Apache::lonhtmlcommon::row_title(&mt('End date'),
1.237     raeburn  5724:                                                    'LC_oddrow_value')."\n".
                   5725:                   $endform."\n".
                   5726:                   &Apache::lonhtmlcommon::row_closure(1).
                   5727:                   &Apache::lonhtmlcommon::end_pick_box();
                   5728:     return $output;
                   5729: }
                   5730: 
1.239     raeburn  5731: sub print_userchangelogs_display {
                   5732:     my ($r,$context,$permission) = @_;
1.363     raeburn  5733:     my $formname = 'rolelog';
                   5734:     my ($username,$domain,$crstype,%roleslog);
                   5735:     if ($context eq 'domain') {
                   5736:         $domain = $env{'request.role.domain'};
                   5737:         %roleslog=&Apache::lonnet::dump_dom('nohist_rolelog',$domain);
                   5738:     } else {
                   5739:         if ($context eq 'course') { 
                   5740:             $domain = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5741:             $username = $env{'course.'.$env{'request.course.id'}.'.num'};
                   5742:             $crstype = &Apache::loncommon::course_type();
                   5743:             my %saveable_parameters = ('show' => 'scalar',);
                   5744:             &Apache::loncommon::store_course_settings('roles_log',
                   5745:                                                       \%saveable_parameters);
                   5746:             &Apache::loncommon::restore_course_settings('roles_log',
                   5747:                                                         \%saveable_parameters);
                   5748:         } elsif ($context eq 'author') {
                   5749:             $domain = $env{'user.domain'}; 
                   5750:             if ($env{'request.role'} =~ m{^au\./\Q$domain\E/$}) {
                   5751:                 $username = $env{'user.name'};
                   5752:             } else {
                   5753:                 undef($domain);
                   5754:             }
                   5755:         }
                   5756:         if ($domain ne '' && $username ne '') { 
                   5757:             %roleslog=&Apache::lonnet::dump('nohist_rolelog',$domain,$username);
                   5758:         }
                   5759:     }
1.239     raeburn  5760:     if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
                   5761: 
                   5762:     # set defaults
                   5763:     my $now = time();
                   5764:     my $defstart = $now - (7*24*3600); #7 days ago 
                   5765:     my %defaults = (
                   5766:                      page               => '1',
                   5767:                      show               => '10',
                   5768:                      role               => 'any',
                   5769:                      chgcontext         => 'any',
                   5770:                      rolelog_start_date => $defstart,
                   5771:                      rolelog_end_date   => $now,
                   5772:                    );
                   5773:     my $more_records = 0;
                   5774: 
                   5775:     # set current
                   5776:     my %curr;
                   5777:     foreach my $item ('show','page','role','chgcontext') {
                   5778:         $curr{$item} = $env{'form.'.$item};
                   5779:     }
                   5780:     my ($startdate,$enddate) = 
                   5781:         &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
                   5782:     $curr{'rolelog_start_date'} = $startdate;
                   5783:     $curr{'rolelog_end_date'} = $enddate;
                   5784:     foreach my $key (keys(%defaults)) {
                   5785:         if ($curr{$key} eq '') {
                   5786:             $curr{$key} = $defaults{$key};
                   5787:         }
                   5788:     }
1.248     raeburn  5789:     my (%whodunit,%changed,$version);
                   5790:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
1.239     raeburn  5791:     my ($minshown,$maxshown);
1.255     raeburn  5792:     $minshown = 1;
1.239     raeburn  5793:     my $count = 0;
                   5794:     if ($curr{'show'} ne &mt('all')) { 
                   5795:         $maxshown = $curr{'page'} * $curr{'show'};
                   5796:         if ($curr{'page'} > 1) {
                   5797:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   5798:         }
                   5799:     }
1.301     bisitz   5800: 
1.327     raeburn  5801:     # Form Header
                   5802:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
1.363     raeburn  5803:               &role_display_filter($context,$formname,$domain,$username,\%curr,
                   5804:                                    $version,$crstype));
1.327     raeburn  5805: 
                   5806:     # Create navigation
                   5807:     my ($nav_script,$nav_links) = &userlogdisplay_nav($formname,\%curr,$more_records);
                   5808:     my $showntableheader = 0;
                   5809: 
                   5810:     # Table Header
                   5811:     my $tableheader = 
                   5812:         &Apache::loncommon::start_data_table_header_row()
                   5813:        .'<th>&nbsp;</th>'
                   5814:        .'<th>'.&mt('When').'</th>'
                   5815:        .'<th>'.&mt('Who made the change').'</th>'
                   5816:        .'<th>'.&mt('Changed User').'</th>'
1.363     raeburn  5817:        .'<th>'.&mt('Role').'</th>';
                   5818: 
                   5819:     if ($context eq 'course') {
                   5820:         $tableheader .= '<th>'.&mt('Section').'</th>';
                   5821:     }
                   5822:     $tableheader .=
                   5823:         '<th>'.&mt('Context').'</th>'
1.327     raeburn  5824:        .'<th>'.&mt('Start').'</th>'
                   5825:        .'<th>'.&mt('End').'</th>'
                   5826:        .&Apache::loncommon::end_data_table_header_row();
                   5827: 
                   5828:     # Display user change log data
1.239     raeburn  5829:     foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
                   5830:         next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
                   5831:                  ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
                   5832:         if ($curr{'show'} ne &mt('all')) {
                   5833:             if ($count >= $curr{'page'} * $curr{'show'}) {
                   5834:                 $more_records = 1;
                   5835:                 last;
                   5836:             }
                   5837:         }
                   5838:         if ($curr{'role'} ne 'any') {
                   5839:             next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
                   5840:         }
                   5841:         if ($curr{'chgcontext'} ne 'any') {
                   5842:             if ($curr{'chgcontext'} eq 'selfenroll') {
                   5843:                 next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
                   5844:             } else {
                   5845:                 next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
                   5846:             }
                   5847:         }
                   5848:         $count ++;
                   5849:         next if ($count < $minshown);
1.327     raeburn  5850:         unless ($showntableheader) {
                   5851:             $r->print($nav_script
                   5852:                      .$nav_links
                   5853:                      .&Apache::loncommon::start_data_table()
                   5854:                      .$tableheader);
                   5855:             $r->rflush();
                   5856:             $showntableheader = 1;
                   5857:         }
1.239     raeburn  5858:         if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
                   5859:             $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
                   5860:                 &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
                   5861:         }
                   5862:         if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
                   5863:             $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
                   5864:                 &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
                   5865:         }
                   5866:         my $sec = $roleslog{$id}{'logentry'}{'section'};
                   5867:         if ($sec eq '') {
                   5868:             $sec = &mt('None');
                   5869:         }
                   5870:         my ($rolestart,$roleend);
                   5871:         if ($roleslog{$id}{'delflag'}) {
                   5872:             $rolestart = &mt('deleted');
                   5873:             $roleend = &mt('deleted');
                   5874:         } else {
                   5875:             $rolestart = $roleslog{$id}{'logentry'}{'start'};
                   5876:             $roleend = $roleslog{$id}{'logentry'}{'end'};
                   5877:             if ($rolestart eq '' || $rolestart == 0) {
                   5878:                 $rolestart = &mt('No start date'); 
                   5879:             } else {
                   5880:                 $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
                   5881:             }
                   5882:             if ($roleend eq '' || $roleend == 0) { 
                   5883:                 $roleend = &mt('No end date');
                   5884:             } else {
                   5885:                 $roleend = &Apache::lonlocal::locallocaltime($roleend);
                   5886:             }
                   5887:         }
                   5888:         my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
                   5889:         if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
                   5890:             $chgcontext = 'selfenroll';
                   5891:         }
1.363     raeburn  5892:         my %lt = &rolechg_contexts($context,$crstype);
1.239     raeburn  5893:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
                   5894:             $chgcontext = $lt{$chgcontext};
                   5895:         }
1.327     raeburn  5896:         $r->print(
1.301     bisitz   5897:             &Apache::loncommon::start_data_table_row()
                   5898:            .'<td>'.$count.'</td>'
                   5899:            .'<td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td>'
                   5900:            .'<td>'.$whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.'</td>'
                   5901:            .'<td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td>'
1.363     raeburn  5902:            .'<td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'},$crstype).'</td>');
                   5903:         if ($context eq 'course') { 
                   5904:             $r->print('<td>'.$sec.'</td>');
                   5905:         }
                   5906:         $r->print(
                   5907:             '<td>'.$chgcontext.'</td>'
1.301     bisitz   5908:            .'<td>'.$rolestart.'</td>'
                   5909:            .'<td>'.$roleend.'</td>'
1.327     raeburn  5910:            .&Apache::loncommon::end_data_table_row()."\n");
1.301     bisitz   5911:     }
                   5912: 
1.327     raeburn  5913:     if ($showntableheader) { # Table footer, if content displayed above
                   5914:         $r->print(&Apache::loncommon::end_data_table()
                   5915:                  .$nav_links);
                   5916:     } else { # No content displayed above
1.301     bisitz   5917:         $r->print('<p class="LC_info">'
                   5918:                  .&mt('There are no records to display.')
                   5919:                  .'</p>'
                   5920:         );
1.239     raeburn  5921:     }
1.301     bisitz   5922: 
1.327     raeburn  5923:     # Form Footer
                   5924:     $r->print( 
                   5925:         '<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   5926:        .'<input type="hidden" name="action" value="changelogs" />'
                   5927:        .'</form>');
                   5928:     return;
                   5929: }
1.301     bisitz   5930: 
1.327     raeburn  5931: sub userlogdisplay_nav {
                   5932:     my ($formname,$curr,$more_records) = @_;
                   5933:     my ($nav_script,$nav_links);
                   5934:     if (ref($curr) eq 'HASH') {
                   5935:         # Create Navigation:
                   5936:         # Navigation Script
                   5937:         $nav_script = <<"ENDSCRIPT";
1.239     raeburn  5938: <script type="text/javascript">
1.301     bisitz   5939: // <![CDATA[
1.239     raeburn  5940: function chgPage(caller) {
                   5941:     if (caller == 'previous') {
                   5942:         document.$formname.page.value --;
                   5943:     }
                   5944:     if (caller == 'next') {
                   5945:         document.$formname.page.value ++;
                   5946:     }
1.327     raeburn  5947:     document.$formname.submit();
1.239     raeburn  5948:     return;
                   5949: }
1.301     bisitz   5950: // ]]>
1.239     raeburn  5951: </script>
                   5952: ENDSCRIPT
1.327     raeburn  5953:         # Navigation Buttons
                   5954:         $nav_links = '<p>';
                   5955:         if (($curr->{'page'} > 1) || ($more_records)) {
                   5956:             if ($curr->{'page'} > 1) {
                   5957:                 $nav_links .= '<input type="button"'
                   5958:                              .' onclick="javascript:chgPage('."'previous'".');"'
                   5959:                              .' value="'.&mt('Previous [_1] changes',$curr->{'show'})
                   5960:                              .'" /> ';
                   5961:             }
                   5962:             if ($more_records) {
                   5963:                 $nav_links .= '<input type="button"'
                   5964:                              .' onclick="javascript:chgPage('."'next'".');"'
                   5965:                              .' value="'.&mt('Next [_1] changes',$curr->{'show'})
                   5966:                              .'" />';
                   5967:             }
1.301     bisitz   5968:         }
1.327     raeburn  5969:         $nav_links .= '</p>';
1.301     bisitz   5970:     }
1.327     raeburn  5971:     return ($nav_script,$nav_links);
1.239     raeburn  5972: }
                   5973: 
                   5974: sub role_display_filter {
1.363     raeburn  5975:     my ($context,$formname,$cdom,$cnum,$curr,$version,$crstype) = @_;
                   5976:     my $lctype;
                   5977:     if ($context eq 'course') {
                   5978:         $lctype = lc($crstype);
                   5979:     }
1.239     raeburn  5980:     my $nolink = 1;
                   5981:     my $output = '<table><tr><td valign="top">'.
1.301     bisitz   5982:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b></span><br />'.
1.239     raeburn  5983:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   5984:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   5985:                  '</td><td>&nbsp;&nbsp;</td>';
                   5986:     my $startform =
                   5987:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
                   5988:                                             $curr->{'rolelog_start_date'},undef,
                   5989:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   5990:     my $endform =
                   5991:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
                   5992:                                             $curr->{'rolelog_end_date'},undef,
                   5993:                                             undef,undef,undef,undef,undef,undef,$nolink);
1.363     raeburn  5994:     my %lt = &rolechg_contexts($context,$crstype);
1.301     bisitz   5995:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br />'.
                   5996:                '<table><tr><td>'.&mt('After:').
                   5997:                '</td><td>'.$startform.'</td></tr>'.
                   5998:                '<tr><td>'.&mt('Before:').'</td>'.
                   5999:                '<td>'.$endform.'</td></tr></table>'.
                   6000:                '</td>'.
                   6001:                '<td>&nbsp;&nbsp;</td>'.
1.239     raeburn  6002:                '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
                   6003:                '<select name="role"><option value="any"';
                   6004:     if ($curr->{'role'} eq 'any') {
                   6005:         $output .= ' selected="selected"';
                   6006:     }
                   6007:     $output .=  '>'.&mt('Any').'</option>'."\n";
1.363     raeburn  6008:     my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
1.239     raeburn  6009:     foreach my $role (@roles) {
                   6010:         my $plrole;
                   6011:         if ($role eq 'cr') {
                   6012:             $plrole = &mt('Custom Role');
                   6013:         } else {
1.318     raeburn  6014:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.239     raeburn  6015:         }
                   6016:         my $selstr = '';
                   6017:         if ($role eq $curr->{'role'}) {
                   6018:             $selstr = ' selected="selected"';
                   6019:         }
                   6020:         $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
                   6021:     }
1.301     bisitz   6022:     $output .= '</select></td>'.
                   6023:                '<td>&nbsp;&nbsp;</td>'.
                   6024:                '<td valign="top"><b>'.
1.239     raeburn  6025:                &mt('Context:').'</b><br /><select name="chgcontext">';
1.363     raeburn  6026:     my @posscontexts;
                   6027:     if ($context eq 'course') {
1.376   ! raeburn  6028:         @posscontexts = ('any','automated','updatenow','createcourse','course','domain','selfenroll','requestcourses');
1.363     raeburn  6029:     } elsif ($context eq 'domain') {
                   6030:         @posscontexts = ('any','domain','requestauthor','domconfig','server');
                   6031:     } else {
                   6032:         @posscontexts = ('any','author','domain');
                   6033:     } 
                   6034:     foreach my $chgtype (@posscontexts) {
1.239     raeburn  6035:         my $selstr = '';
                   6036:         if ($curr->{'chgcontext'} eq $chgtype) {
1.301     bisitz   6037:             $selstr = ' selected="selected"';
1.239     raeburn  6038:         }
1.363     raeburn  6039:         if ($context eq 'course') {
1.376   ! raeburn  6040:             if (($chgtype eq 'automated') || ($chgtype eq 'updatenow')) {
1.363     raeburn  6041:                 next if (!&Apache::lonnet::auto_run($cnum,$cdom));
                   6042:             }
1.239     raeburn  6043:         }
                   6044:         $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
1.248     raeburn  6045:     }
1.303     bisitz   6046:     $output .= '</select></td>'
                   6047:               .'</tr></table>';
                   6048: 
                   6049:     # Update Display button
                   6050:     $output .= '<p>'
                   6051:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   6052:               .'</p>';
                   6053: 
                   6054:     # Server version info
1.363     raeburn  6055:     my $needsrev = '2.11.0';
                   6056:     if ($context eq 'course') {
                   6057:         $needsrev = '2.7.0';
                   6058:     }
                   6059:     
1.303     bisitz   6060:     $output .= '<p class="LC_info">'
                   6061:               .&mt('Only changes made from servers running LON-CAPA [_1] or later are displayed.'
1.363     raeburn  6062:                   ,$needsrev);
1.248     raeburn  6063:     if ($version) {
1.303     bisitz   6064:         $output .= ' '.&mt('This LON-CAPA server is version [_1]',$version);
                   6065:     }
                   6066:     $output .= '</p><hr />';
1.239     raeburn  6067:     return $output;
                   6068: }
                   6069: 
                   6070: sub rolechg_contexts {
1.363     raeburn  6071:     my ($context,$crstype) = @_;
                   6072:     my %lt;
                   6073:     if ($context eq 'course') {
                   6074:         %lt = &Apache::lonlocal::texthash (
1.239     raeburn  6075:                                              any          => 'Any',
1.376   ! raeburn  6076:                                              automated    => 'Automated Enrollment',
1.239     raeburn  6077:                                              updatenow    => 'Roster Update',
                   6078:                                              createcourse => 'Course Creation',
                   6079:                                              course       => 'User Management in course',
                   6080:                                              domain       => 'User Management in domain',
1.313     raeburn  6081:                                              selfenroll   => 'Self-enrolled',
1.318     raeburn  6082:                                              requestcourses => 'Course Request',
1.239     raeburn  6083:                                          );
1.363     raeburn  6084:         if ($crstype eq 'Community') {
                   6085:             $lt{'createcourse'} = &mt('Community Creation');
                   6086:             $lt{'course'} = &mt('User Management in community');
                   6087:             $lt{'requestcourses'} = &mt('Community Request');
                   6088:         }
                   6089:     } elsif ($context eq 'domain') {
                   6090:         %lt = &Apache::lonlocal::texthash (
                   6091:                                              any           => 'Any',
                   6092:                                              domain        => 'User Management in domain',
                   6093:                                              requestauthor => 'Authoring Request',
                   6094:                                              server        => 'Command line script (DC role)',
                   6095:                                              domconfig     => 'Self-enrolled',
                   6096:                                          );
                   6097:     } else {
                   6098:         %lt = &Apache::lonlocal::texthash (
                   6099:                                              any    => 'Any',
                   6100:                                              domain => 'User Management in domain',
                   6101:                                              author => 'User Management by author',
                   6102:                                          );
                   6103:     } 
1.239     raeburn  6104:     return %lt;
                   6105: }
                   6106: 
1.27      matthew  6107: #-------------------------------------------------- functions for &phase_two
1.160     raeburn  6108: sub user_search_result {
1.221     raeburn  6109:     my ($context,$srch) = @_;
1.160     raeburn  6110:     my %allhomes;
                   6111:     my %inst_matches;
                   6112:     my %srch_results;
1.181     raeburn  6113:     my ($response,$currstate,$forcenewuser,$dirsrchres);
1.183     raeburn  6114:     $srch->{'srchterm'} =~ s/\s+/ /g;
1.176     raeburn  6115:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
1.160     raeburn  6116:         $response = &mt('Invalid search.');
                   6117:     }
                   6118:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
                   6119:         $response = &mt('Invalid search.');
                   6120:     }
1.177     raeburn  6121:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
1.160     raeburn  6122:         $response = &mt('Invalid search.');
                   6123:     }
                   6124:     if ($srch->{'srchterm'} eq '') {
                   6125:         $response = &mt('You must enter a search term.');
                   6126:     }
1.183     raeburn  6127:     if ($srch->{'srchterm'} =~ /^\s+$/) {
                   6128:         $response = &mt('Your search term must contain more than just spaces.');
                   6129:     }
1.160     raeburn  6130:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
                   6131:         if (($srch->{'srchdomain'} eq '') || 
1.163     albertel 6132: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
1.160     raeburn  6133:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
                   6134:         }
                   6135:     }
                   6136:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
                   6137:         ($srch->{'srchin'} eq 'alc')) {
1.176     raeburn  6138:         if ($srch->{'srchby'} eq 'uname') {
1.243     raeburn  6139:             my $unamecheck = $srch->{'srchterm'};
                   6140:             if ($srch->{'srchtype'} eq 'contains') {
                   6141:                 if ($unamecheck !~ /^\w/) {
                   6142:                     $unamecheck = 'a'.$unamecheck; 
                   6143:                 }
                   6144:             }
                   6145:             if ($unamecheck !~ /^$match_username$/) {
1.176     raeburn  6146:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
                   6147:             }
1.160     raeburn  6148:         }
                   6149:     }
1.180     raeburn  6150:     if ($response ne '') {
                   6151:         $response = '<span class="LC_warning">'.$response.'</span>';
                   6152:     }
1.160     raeburn  6153:     if ($srch->{'srchin'} eq 'instd') {
                   6154:         my $instd_chk = &directorysrch_check($srch);
                   6155:         if ($instd_chk ne 'ok') {
1.180     raeburn  6156:             $response = '<span class="LC_warning">'.$instd_chk.'</span>'.
                   6157:                         '<br />'.&mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').'<br /><br />';
1.160     raeburn  6158:         }
                   6159:     }
                   6160:     if ($response ne '') {
1.180     raeburn  6161:         return ($currstate,$response);
1.160     raeburn  6162:     }
                   6163:     if ($srch->{'srchby'} eq 'uname') {
                   6164:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
                   6165:             if ($env{'form.forcenew'}) {
                   6166:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
                   6167:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   6168:                     if ($uhome eq 'no_host') {
                   6169:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.180     raeburn  6170:                         my $showdom = &display_domain_info($env{'request.role.domain'});
                   6171:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
1.160     raeburn  6172:                     } else {
1.179     raeburn  6173:                         $currstate = 'modify';
1.160     raeburn  6174:                     }
                   6175:                 } else {
1.179     raeburn  6176:                     $currstate = 'modify';
1.160     raeburn  6177:                 }
                   6178:             } else {
                   6179:                 if ($srch->{'srchin'} eq 'dom') {
1.162     raeburn  6180:                     if ($srch->{'srchtype'} eq 'exact') {
                   6181:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   6182:                         if ($uhome eq 'no_host') {
1.179     raeburn  6183:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  6184:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  6185:                         } else {
1.179     raeburn  6186:                             $currstate = 'modify';
1.310     raeburn  6187:                             my $uname = $srch->{'srchterm'};
                   6188:                             my $udom = $srch->{'srchdomain'};
                   6189:                             $srch_results{$uname.':'.$udom} =
                   6190:                                 { &Apache::lonnet::get('environment',
                   6191:                                                        ['firstname',
                   6192:                                                         'lastname',
                   6193:                                                         'permanentemail'],
                   6194:                                                          $udom,$uname)
                   6195:                                 };
1.162     raeburn  6196:                         }
                   6197:                     } else {
                   6198:                         %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  6199:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  6200:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  6201:                     }
                   6202:                 } else {
1.167     albertel 6203:                     my $courseusers = &get_courseusers();
1.162     raeburn  6204:                     if ($srch->{'srchtype'} eq 'exact') {
1.167     albertel 6205:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
1.179     raeburn  6206:                             $currstate = 'modify';
1.162     raeburn  6207:                         } else {
1.179     raeburn  6208:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  6209:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  6210:                         }
1.160     raeburn  6211:                     } else {
1.167     albertel 6212:                         foreach my $user (keys(%$courseusers)) {
1.162     raeburn  6213:                             my ($cuname,$cudomain) = split(/:/,$user);
                   6214:                             if ($cudomain eq $srch->{'srchdomain'}) {
1.177     raeburn  6215:                                 my $matched = 0;
                   6216:                                 if ($srch->{'srchtype'} eq 'begins') {
                   6217:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
                   6218:                                         $matched = 1;
                   6219:                                     }
                   6220:                                 } else {
                   6221:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
                   6222:                                         $matched = 1;
                   6223:                                     }
                   6224:                                 }
                   6225:                                 if ($matched) {
1.167     albertel 6226:                                     $srch_results{$user} = 
                   6227: 					{&Apache::lonnet::get('environment',
                   6228: 							     ['firstname',
                   6229: 							      'lastname',
1.194     albertel 6230: 							      'permanentemail'],
                   6231: 							      $cudomain,$cuname)};
1.162     raeburn  6232:                                 }
                   6233:                             }
                   6234:                         }
1.179     raeburn  6235:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  6236:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  6237:                     }
                   6238:                 }
                   6239:             }
                   6240:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  6241:             $currstate = 'query';
1.160     raeburn  6242:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  6243:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
                   6244:             if ($dirsrchres eq 'ok') {
                   6245:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  6246:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  6247:             } else {
                   6248:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   6249:                 $response = '<span class="LC_warning">'.
                   6250:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   6251:                     '</span><br />'.
                   6252:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
                   6253:                     '<br /><br />'; 
                   6254:             }
1.160     raeburn  6255:         }
                   6256:     } else {
                   6257:         if ($srch->{'srchin'} eq 'dom') {
                   6258:             %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  6259:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  6260:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  6261:         } elsif ($srch->{'srchin'} eq 'crs') {
1.167     albertel 6262:             my $courseusers = &get_courseusers(); 
                   6263:             foreach my $user (keys(%$courseusers)) {
1.160     raeburn  6264:                 my ($uname,$udom) = split(/:/,$user);
                   6265:                 my %names = &Apache::loncommon::getnames($uname,$udom);
                   6266:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
                   6267:                 if ($srch->{'srchby'} eq 'lastname') {
                   6268:                     if ((($srch->{'srchtype'} eq 'exact') && 
                   6269:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
1.177     raeburn  6270:                         (($srch->{'srchtype'} eq 'begins') &&
                   6271:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
1.160     raeburn  6272:                         (($srch->{'srchtype'} eq 'contains') &&
                   6273:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
                   6274:                         $srch_results{$user} = {firstname => $names{'firstname'},
                   6275:                                             lastname => $names{'lastname'},
                   6276:                                             permanentemail => $emails{'permanentemail'},
                   6277:                                            };
                   6278:                     }
                   6279:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
                   6280:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
1.177     raeburn  6281:                     $srchlast =~ s/\s+$//;
                   6282:                     $srchfirst =~ s/^\s+//;
1.160     raeburn  6283:                     if ($srch->{'srchtype'} eq 'exact') {
                   6284:                         if (($names{'lastname'} eq $srchlast) &&
                   6285:                             ($names{'firstname'} eq $srchfirst)) {
                   6286:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   6287:                                                 lastname => $names{'lastname'},
                   6288:                                                 permanentemail => $emails{'permanentemail'},
                   6289: 
                   6290:                                            };
                   6291:                         }
1.177     raeburn  6292:                     } elsif ($srch->{'srchtype'} eq 'begins') {
                   6293:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
                   6294:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
                   6295:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   6296:                                                 lastname => $names{'lastname'},
                   6297:                                                 permanentemail => $emails{'permanentemail'},
                   6298:                                                };
                   6299:                         }
                   6300:                     } else {
1.160     raeburn  6301:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
                   6302:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
                   6303:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   6304:                                                 lastname => $names{'lastname'},
                   6305:                                                 permanentemail => $emails{'permanentemail'},
                   6306:                                                };
                   6307:                         }
                   6308:                     }
                   6309:                 }
                   6310:             }
1.179     raeburn  6311:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  6312:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  6313:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  6314:             $currstate = 'query';
1.160     raeburn  6315:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  6316:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
                   6317:             if ($dirsrchres eq 'ok') {
                   6318:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  6319:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  6320:             } else {
                   6321:                 my $showdom = &display_domain_info($srch->{'srchdomain'});                $response = '<span class="LC_warning">'.
                   6322:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   6323:                     '</span><br />'.
                   6324:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
                   6325:                     '<br /><br />';
                   6326:             }
1.160     raeburn  6327:         }
                   6328:     }
1.179     raeburn  6329:     return ($currstate,$response,$forcenewuser,\%srch_results);
1.160     raeburn  6330: }
                   6331: 
                   6332: sub directorysrch_check {
                   6333:     my ($srch) = @_;
                   6334:     my $can_search = 0;
                   6335:     my $response;
                   6336:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   6337:                                              ['directorysrch'],$srch->{'srchdomain'});
1.180     raeburn  6338:     my $showdom = &display_domain_info($srch->{'srchdomain'});
1.160     raeburn  6339:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   6340:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
1.180     raeburn  6341:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
1.160     raeburn  6342:         }
                   6343:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
                   6344:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
1.180     raeburn  6345:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
1.160     raeburn  6346:             }
                   6347:             my @usertypes = split(/:/,$env{'environment.inststatus'});
                   6348:             if (!@usertypes) {
                   6349:                 push(@usertypes,'default');
                   6350:             }
                   6351:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
                   6352:                 foreach my $type (@usertypes) {
                   6353:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
                   6354:                         $can_search = 1;
                   6355:                         last;
                   6356:                     }
                   6357:                 }
                   6358:             }
                   6359:             if (!$can_search) {
                   6360:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
                   6361:                 my @longtypes; 
                   6362:                 foreach my $item (@usertypes) {
1.229     raeburn  6363:                     if (defined($insttypes->{$item})) { 
                   6364:                         push (@longtypes,$insttypes->{$item});
                   6365:                     } elsif ($item eq 'default') {
                   6366:                         push (@longtypes,&mt('other')); 
                   6367:                     }
1.160     raeburn  6368:                 }
                   6369:                 my $insttype_str = join(', ',@longtypes); 
1.180     raeburn  6370:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
1.229     raeburn  6371:             }
1.160     raeburn  6372:         } else {
                   6373:             $can_search = 1;
                   6374:         }
                   6375:     } else {
1.180     raeburn  6376:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
1.160     raeburn  6377:     }
                   6378:     my %longtext = &Apache::lonlocal::texthash (
1.167     albertel 6379:                        uname     => 'username',
1.160     raeburn  6380:                        lastfirst => 'last name, first name',
1.167     albertel 6381:                        lastname  => 'last name',
1.172     raeburn  6382:                        contains  => 'contains',
1.178     raeburn  6383:                        exact     => 'as exact match to',
                   6384:                        begins    => 'begins with',
1.160     raeburn  6385:                    );
                   6386:     if ($can_search) {
                   6387:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
                   6388:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
1.180     raeburn  6389:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
1.160     raeburn  6390:             }
                   6391:         } else {
1.180     raeburn  6392:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
1.160     raeburn  6393:         }
                   6394:     }
                   6395:     if ($can_search) {
1.178     raeburn  6396:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
                   6397:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
                   6398:                 return 'ok';
                   6399:             } else {
1.180     raeburn  6400:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  6401:             }
                   6402:         } else {
                   6403:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
                   6404:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
                   6405:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
                   6406:                 return 'ok';
                   6407:             } else {
1.180     raeburn  6408:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  6409:             }
1.160     raeburn  6410:         }
                   6411:     }
                   6412: }
                   6413: 
                   6414: sub get_courseusers {
                   6415:     my %advhash;
1.167     albertel 6416:     my $classlist = &Apache::loncoursedata::get_classlist();
1.160     raeburn  6417:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                   6418:     foreach my $role (sort(keys(%coursepersonnel))) {
                   6419:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
1.167     albertel 6420: 	    if (!exists($classlist->{$user})) {
                   6421: 		$classlist->{$user} = [];
                   6422: 	    }
1.160     raeburn  6423:         }
                   6424:     }
1.167     albertel 6425:     return $classlist;
1.160     raeburn  6426: }
                   6427: 
                   6428: sub build_search_response {
1.221     raeburn  6429:     my ($context,$srch,%srch_results) = @_;
1.179     raeburn  6430:     my ($currstate,$response,$forcenewuser);
1.160     raeburn  6431:     my %names = (
1.330     bisitz   6432:           'uname'     => 'username',
                   6433:           'lastname'  => 'last name',
1.160     raeburn  6434:           'lastfirst' => 'last name, first name',
1.330     bisitz   6435:           'crs'       => 'this course',
                   6436:           'dom'       => 'LON-CAPA domain',
                   6437:           'instd'     => 'the institutional directory for domain',
1.160     raeburn  6438:     );
                   6439: 
                   6440:     my %single = (
1.180     raeburn  6441:                    begins   => 'A match',
1.160     raeburn  6442:                    contains => 'A match',
1.180     raeburn  6443:                    exact    => 'An exact match',
1.160     raeburn  6444:                  );
                   6445:     my %nomatch = (
1.180     raeburn  6446:                    begins   => 'No match',
1.160     raeburn  6447:                    contains => 'No match',
1.180     raeburn  6448:                    exact    => 'No exact match',
1.160     raeburn  6449:                   );
                   6450:     if (keys(%srch_results) > 1) {
1.179     raeburn  6451:         $currstate = 'select';
1.160     raeburn  6452:     } else {
                   6453:         if (keys(%srch_results) == 1) {
1.179     raeburn  6454:             $currstate = 'modify';
1.180     raeburn  6455:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
                   6456:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   6457:                 $response .= ': '.&display_domain_info($srch->{'srchdomain'});
1.180     raeburn  6458:             }
1.330     bisitz   6459:         } else { # Search has nothing found. Prepare message to user.
                   6460:             $response = '<span class="LC_warning">';
1.180     raeburn  6461:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   6462:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}: [_2]",
                   6463:                                  '<b>'.$srch->{'srchterm'}.'</b>',
                   6464:                                  &display_domain_info($srch->{'srchdomain'}));
                   6465:             } else {
                   6466:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}.",
                   6467:                                  '<b>'.$srch->{'srchterm'}.'</b>');
1.180     raeburn  6468:             }
                   6469:             $response .= '</span>';
1.330     bisitz   6470: 
1.160     raeburn  6471:             if ($srch->{'srchin'} ne 'alc') {
                   6472:                 $forcenewuser = 1;
                   6473:                 my $cansrchinst = 0; 
                   6474:                 if ($srch->{'srchdomain'}) {
                   6475:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
                   6476:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
                   6477:                         if ($domconfig{'directorysrch'}{'available'}) {
                   6478:                             $cansrchinst = 1;
                   6479:                         } 
                   6480:                     }
                   6481:                 }
1.180     raeburn  6482:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
                   6483:                      ($srch->{'srchby'} eq 'lastname')) &&
                   6484:                     ($srch->{'srchin'} eq 'dom')) {
                   6485:                     if ($cansrchinst) {
                   6486:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
1.160     raeburn  6487:                     }
                   6488:                 }
1.180     raeburn  6489:                 if ($srch->{'srchin'} eq 'crs') {
                   6490:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
                   6491:                 }
                   6492:             }
1.305     raeburn  6493:             my $createdom = $env{'request.role.domain'};
                   6494:             if ($context eq 'requestcrs') {
                   6495:                 if ($env{'form.coursedom'} ne '') {
                   6496:                     $createdom = $env{'form.coursedom'};
                   6497:                 }
                   6498:             }
                   6499:             if (!($srch->{'srchby'} eq 'uname' && $srch->{'srchin'} eq 'dom' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchdomain'} eq $createdom)) {
1.221     raeburn  6500:                 my $cancreate =
1.305     raeburn  6501:                     &Apache::lonuserutils::can_create_user($createdom,$context);
                   6502:                 my $targetdom = '<span class="LC_cusr_emph">'.$createdom.'</span>';
1.221     raeburn  6503:                 if ($cancreate) {
1.305     raeburn  6504:                     my $showdom = &display_domain_info($createdom); 
1.266     bisitz   6505:                     $response .= '<br /><br />'
                   6506:                                 .'<b>'.&mt('To add a new user:').'</b>'
1.305     raeburn  6507:                                 .'<br />';
                   6508:                     if ($context eq 'requestcrs') {
                   6509:                         $response .= &mt("(You can only define new users in the new course's domain - [_1])",$targetdom);
                   6510:                     } else {
                   6511:                         $response .= &mt("(You can only create new users in your current role's domain - [_1])",$targetdom);
                   6512:                     }
                   6513:                     $response .='<ul><li>'
1.266     bisitz   6514:                                 .&mt("Set 'Domain/institution to search' to: [_1]",'<span class="LC_cusr_emph">'.$showdom.'</span>')
                   6515:                                 .'</li><li>'
                   6516:                                 .&mt("Set 'Search criteria' to: [_1]username is ..... in selected LON-CAPA domain[_2]",'<span class="LC_cusr_emph">','</span>')
                   6517:                                 .'</li><li>'
                   6518:                                 .&mt('Provide the proposed username')
                   6519:                                 .'</li><li>'
                   6520:                                 .&mt("Click 'Search'")
                   6521:                                 .'</li></ul><br />';
1.221     raeburn  6522:                 } else {
                   6523:                     my $helplink = ' href="javascript:helpMenu('."'display'".')"';
1.305     raeburn  6524:                     $response .= '<br /><br />';
                   6525:                     if ($context eq 'requestcrs') {
1.314     raeburn  6526:                         $response .= &mt("You are not authorized to define new users in the new course's domain - [_1].",$targetdom);
1.305     raeburn  6527:                     } else {
                   6528:                         $response .= &mt("You are not authorized to create new users in your current role's domain - [_1].",$targetdom);
                   6529:                     }
                   6530:                     $response .= '<br />'
                   6531:                                  .&mt('Please contact the [_1]helpdesk[_2] if you need to create a new user.'
1.266     bisitz   6532:                                     ,' <a'.$helplink.'>'
                   6533:                                     ,'</a>')
1.305     raeburn  6534:                                  .'<br /><br />';
1.221     raeburn  6535:                 }
1.160     raeburn  6536:             }
                   6537:         }
                   6538:     }
1.179     raeburn  6539:     return ($currstate,$response,$forcenewuser);
1.160     raeburn  6540: }
                   6541: 
1.180     raeburn  6542: sub display_domain_info {
                   6543:     my ($dom) = @_;
                   6544:     my $output = $dom;
                   6545:     if ($dom ne '') { 
                   6546:         my $domdesc = &Apache::lonnet::domain($dom,'description');
                   6547:         if ($domdesc ne '') {
                   6548:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
                   6549:         }
                   6550:     }
                   6551:     return $output;
                   6552: }
                   6553: 
1.160     raeburn  6554: sub crumb_utilities {
                   6555:     my %elements = (
                   6556:        crtuser => {
                   6557:            srchterm => 'text',
1.172     raeburn  6558:            srchin => 'selectbox',
1.160     raeburn  6559:            srchby => 'selectbox',
                   6560:            srchtype => 'selectbox',
                   6561:            srchdomain => 'selectbox',
                   6562:        },
1.207     raeburn  6563:        crtusername => {
                   6564:            srchterm => 'text',
                   6565:            srchdomain => 'selectbox',
                   6566:        },
1.160     raeburn  6567:        docustom => {
                   6568:            rolename => 'selectbox',
                   6569:            newrolename => 'textbox',
                   6570:        },
1.179     raeburn  6571:        studentform => {
                   6572:            srchterm => 'text',
                   6573:            srchin => 'selectbox',
                   6574:            srchby => 'selectbox',
                   6575:            srchtype => 'selectbox',
                   6576:            srchdomain => 'selectbox',
                   6577:        },
1.160     raeburn  6578:     );
                   6579: 
                   6580:     my $jsback .= qq|
                   6581: function backPage(formname,prevphase,prevstate) {
1.211     raeburn  6582:     if (typeof prevphase == 'undefined') {
                   6583:         formname.phase.value = '';
                   6584:     }
                   6585:     else {  
                   6586:         formname.phase.value = prevphase;
                   6587:     }
                   6588:     if (typeof prevstate == 'undefined') {
                   6589:         formname.currstate.value = '';
                   6590:     }
                   6591:     else {
                   6592:         formname.currstate.value = prevstate;
                   6593:     }
1.160     raeburn  6594:     formname.submit();
                   6595: }
                   6596: |;
                   6597:     return ($jsback,\%elements);
                   6598: }
                   6599: 
1.26      matthew  6600: sub course_level_table {
1.375     raeburn  6601:     my ($inccourses,$showcredits,$defaultcredits) = @_;
                   6602:     return unless (ref($inccourses) eq 'HASH');
1.26      matthew  6603:     my $table = '';
1.62      www      6604: # Custom Roles?
                   6605: 
1.190     raeburn  6606:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.89      raeburn  6607:     my %lt=&Apache::lonlocal::texthash(
                   6608:             'exs'  => "Existing sections",
                   6609:             'new'  => "Define new section",
                   6610:             'ssd'  => "Set Start Date",
                   6611:             'sed'  => "Set End Date",
1.131     raeburn  6612:             'crl'  => "Course Level",
1.89      raeburn  6613:             'act'  => "Activate",
                   6614:             'rol'  => "Role",
                   6615:             'ext'  => "Extent",
1.113     raeburn  6616:             'grs'  => "Section",
1.375     raeburn  6617:             'crd'  => "Credits",
1.89      raeburn  6618:             'sta'  => "Start",
                   6619:             'end'  => "End"
                   6620:     );
1.62      www      6621: 
1.375     raeburn  6622:     foreach my $protectedcourse (sort(keys(%{$inccourses}))) {
1.135     raeburn  6623: 	my $thiscourse=$protectedcourse;
1.26      matthew  6624: 	$thiscourse=~s:_:/:g;
                   6625: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
1.365     raeburn  6626:         my $isowner = &Apache::lonuserutils::is_courseowner($protectedcourse,$coursedata{'internal.courseowner'});
1.26      matthew  6627: 	my $area=$coursedata{'description'};
1.321     raeburn  6628:         my $crstype=$coursedata{'type'};
1.135     raeburn  6629: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
1.89      raeburn  6630: 	my ($domain,$cnum)=split(/\//,$thiscourse);
1.115     albertel 6631:         my %sections_count;
1.101     albertel 6632:         if (defined($env{'request.course.id'})) {
                   6633:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
1.115     albertel 6634:                 %sections_count = 
                   6635: 		    &Apache::loncommon::get_sections($domain,$cnum);
1.92      raeburn  6636:             }
                   6637:         }
1.321     raeburn  6638:         my @roles = &Apache::lonuserutils::roles_by_context('course','',$crstype);
1.213     raeburn  6639: 	foreach my $role (@roles) {
1.321     raeburn  6640:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.329     raeburn  6641: 	    if ((&Apache::lonnet::allowed('c'.$role,$thiscourse)) ||
                   6642:                 ((($role eq 'cc') || ($role eq 'co')) && ($isowner))) {
1.221     raeburn  6643:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  6644:                                             $plrole,\%sections_count,\%lt,
                   6645:                                             $defaultcredits,$crstype);
1.221     raeburn  6646:             } elsif ($env{'request.course.sec'} ne '') {
                   6647:                 if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
                   6648:                                              $env{'request.course.sec'})) {
                   6649:                     $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  6650:                                                 $plrole,\%sections_count,\%lt,
                   6651:                                                 $defaultcredits,$crstype);
1.26      matthew  6652:                 }
                   6653:             }
                   6654:         }
1.221     raeburn  6655:         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
1.324     raeburn  6656:             foreach my $cust (sort(keys(%customroles))) {
                   6657:                 next if ($crstype eq 'Community' && $customroles{$cust} =~ /bre\&S/);
1.221     raeburn  6658:                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
                   6659:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
                   6660:                                             $cust,\%sections_count,\%lt);
                   6661:             }
1.62      www      6662: 	}
1.26      matthew  6663:     }
                   6664:     return '' if ($table eq ''); # return nothing if there is nothing 
                   6665:                                  # in the table
1.188     raeburn  6666:     my $result;
                   6667:     if (!$env{'request.course.id'}) {
                   6668:         $result = '<h4>'.$lt{'crl'}.'</h4>'."\n";
                   6669:     }
                   6670:     $result .= 
1.136     raeburn  6671: &Apache::loncommon::start_data_table().
                   6672: &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  6673: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
                   6674: '<th>'.$lt{'ext'}.'</th><th>'.$lt{'crd'}.'</th>'."\n".
                   6675: '<th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th>'."\n".
                   6676: '<th>'.$lt{'end'}.'</th>'.
1.136     raeburn  6677: &Apache::loncommon::end_data_table_header_row().
                   6678: $table.
                   6679: &Apache::loncommon::end_data_table();
1.26      matthew  6680:     return $result;
                   6681: }
1.88      raeburn  6682: 
1.221     raeburn  6683: sub course_level_row {
1.375     raeburn  6684:     my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,
                   6685:         $lt,$defaultcredits,$crstype) = @_;
                   6686:     my $creditem;
1.222     raeburn  6687:     my $row = &Apache::loncommon::start_data_table_row().
                   6688:               ' <td><input type="checkbox" name="act_'.
                   6689:               $protectedcourse.'_'.$role.'" /></td>'."\n".
                   6690:               ' <td>'.$plrole.'</td>'."\n".
                   6691:               ' <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
1.375     raeburn  6692:     if (($role eq 'st') && ($crstype eq 'Course')) {
                   6693:         $row .= 
                   6694:             '<td><input type="text" name="credits_'.$protectedcourse.'_'.
                   6695:             $role.'" size="3" value="'.$defaultcredits.'" /></td>';
                   6696:     } else {
                   6697:         $row .= '<td>&nbsp;</td>';
                   6698:     }
1.322     raeburn  6699:     if (($role eq 'cc') || ($role eq 'co')) {
1.222     raeburn  6700:         $row .= '<td>&nbsp;</td>';
1.221     raeburn  6701:     } elsif ($env{'request.course.sec'} ne '') {
1.222     raeburn  6702:         $row .= ' <td><input type="hidden" value="'.
                   6703:                 $env{'request.course.sec'}.'" '.
                   6704:                 'name="sec_'.$protectedcourse.'_'.$role.'" />'.
                   6705:                 $env{'request.course.sec'}.'</td>';
1.221     raeburn  6706:     } else {
                   6707:         if (ref($sections_count) eq 'HASH') {
                   6708:             my $currsec = 
                   6709:                 &Apache::lonuserutils::course_sections($sections_count,
                   6710:                                                        $protectedcourse.'_'.$role);
1.222     raeburn  6711:             $row .= '<td><table class="LC_createuser">'."\n".
                   6712:                     '<tr class="LC_section_row">'."\n".
                   6713:                     ' <td valign="top">'.$lt->{'exs'}.'<br />'.
                   6714:                        $currsec.'</td>'."\n".
                   6715:                      ' <td>&nbsp;&nbsp;</td>'."\n".
                   6716:                      ' <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
1.221     raeburn  6717:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
                   6718:                      '" value="" />'.
                   6719:                      '<input type="hidden" '.
                   6720:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
1.222     raeburn  6721:                      '</tr></table></td>'."\n";
1.221     raeburn  6722:         } else {
1.222     raeburn  6723:             $row .= '<td><input type="text" size="10" '.
1.375     raeburn  6724:                     'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n";
1.221     raeburn  6725:         }
                   6726:     }
1.222     raeburn  6727:     $row .= <<ENDTIMEENTRY;
                   6728: <td><input type="hidden" name="start_$protectedcourse\_$role" value="" />
1.221     raeburn  6729: <a href=
                   6730: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$role.value,'start_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'ssd'}</a></td>
1.222     raeburn  6731: <td><input type="hidden" name="end_$protectedcourse\_$role" value="" />
1.221     raeburn  6732: <a href=
                   6733: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
                   6734: ENDTIMEENTRY
1.222     raeburn  6735:     $row .= &Apache::loncommon::end_data_table_row();
                   6736:     return $row;
1.221     raeburn  6737: }
                   6738: 
1.88      raeburn  6739: sub course_level_dc {
1.375     raeburn  6740:     my ($dcdom,$showcredits) = @_;
1.190     raeburn  6741:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.213     raeburn  6742:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.88      raeburn  6743:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
                   6744:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
1.133     raeburn  6745:                       '<input type="hidden" name="dccourse" value="" />';
1.355     www      6746:     my $courseform=&Apache::loncommon::selectcourse_link
1.356     raeburn  6747:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Select','crstype');
1.375     raeburn  6748:     my $credit_elem;
                   6749:     if ($showcredits) {
                   6750:         $credit_elem = 'credits';
                   6751:     }
                   6752:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu','role','Course/Community Browser',$credit_elem);
1.88      raeburn  6753:     my %lt=&Apache::lonlocal::texthash(
                   6754:                     'rol'  => "Role",
1.113     raeburn  6755:                     'grs'  => "Section",
1.88      raeburn  6756:                     'exs'  => "Existing sections",
                   6757:                     'new'  => "Define new section", 
                   6758:                     'sta'  => "Start",
                   6759:                     'end'  => "End",
                   6760:                     'ssd'  => "Set Start Date",
1.355     www      6761:                     'sed'  => "Set End Date",
1.375     raeburn  6762:                     'scc'  => "Course/Community",
                   6763:                     'crd'  => "Credits",
1.88      raeburn  6764:                   );
1.323     raeburn  6765:     my $header = '<h4>'.&mt('Course/Community Level').'</h4>'.
1.136     raeburn  6766:                  &Apache::loncommon::start_data_table().
                   6767:                  &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  6768:                  '<th>'.$lt{'scc'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
                   6769:                  '<th>'.$lt{'grs'}.'</th><th>'.$lt{'crd'}.'</th>'."\n".
                   6770:                  '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'."\n".
1.136     raeburn  6771:                  &Apache::loncommon::end_data_table_header_row();
1.143     raeburn  6772:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
1.356     raeburn  6773:                      '<td><br /><span class="LC_nobreak"><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc','','','','crstype'".')" />'.
                   6774:                      $courseform.('&nbsp;' x4).'</span></td>'."\n".
1.323     raeburn  6775:                      '<td valign><br /><select name="role">'."\n";
1.213     raeburn  6776:     foreach my $role (@roles) {
1.135     raeburn  6777:         my $plrole=&Apache::lonnet::plaintext($role);
                   6778:         $otheritems .= '  <option value="'.$role.'">'.$plrole;
1.88      raeburn  6779:     }
                   6780:     if ( keys %customroles > 0) {
1.135     raeburn  6781:         foreach my $cust (sort keys %customroles) {
1.101     albertel 6782:             my $custrole='cr_cr_'.$env{'user.domain'}.
1.135     raeburn  6783:                     '_'.$env{'user.name'}.'_'.$cust;
                   6784:             $otheritems .= '  <option value="'.$custrole.'">'.$cust;
1.88      raeburn  6785:         }
                   6786:     }
                   6787:     $otheritems .= '</select></td><td>'.
                   6788:                      '<table border="0" cellspacing="0" cellpadding="0">'.
                   6789:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
                   6790:                      ' <option value=""><--'.&mt('Pick course first').'</select></td>'.
                   6791:                      '<td>&nbsp;&nbsp;</td>'.
                   6792:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
1.113     raeburn  6793:                      '<input type="text" name="newsec" value="" />'.
1.237     raeburn  6794:                      '<input type="hidden" name="section" value="" />'.
1.323     raeburn  6795:                      '<input type="hidden" name="groups" value="" />'.
                   6796:                      '<input type="hidden" name="crstype" value="" /></td>'.
1.375     raeburn  6797:                      '</tr></table></td>'."\n";
                   6798:     if ($showcredits) {
                   6799:         $otheritems .= '<td><br />'."\n".
                   6800:                        '<input type="text" size="3" name="credits" value="" />'."\n";
                   6801:     }
1.88      raeburn  6802:     $otheritems .= <<ENDTIMEENTRY;
1.323     raeburn  6803: <td><br /><input type="hidden" name="start" value='' />
1.88      raeburn  6804: <a href=
                   6805: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.323     raeburn  6806: <td><br /><input type="hidden" name="end" value='' />
1.88      raeburn  6807: <a href=
                   6808: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
                   6809: ENDTIMEENTRY
1.136     raeburn  6810:     $otheritems .= &Apache::loncommon::end_data_table_row().
                   6811:                    &Apache::loncommon::end_data_table()."\n";
1.88      raeburn  6812:     return $cb_jscript.$header.$hiddenitems.$otheritems;
                   6813: }
                   6814: 
1.237     raeburn  6815: sub update_selfenroll_config {
1.241     raeburn  6816:     my ($r,$context,$permission) = @_;
1.237     raeburn  6817:     my ($row,$lt) = &get_selfenroll_titles();
1.241     raeburn  6818:     my %curr_groups = &Apache::longroup::coursegroups();
1.237     raeburn  6819:     my (%changes,%warning);
                   6820:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   6821:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.241     raeburn  6822:     my $curr_types;
1.237     raeburn  6823:     if (ref($row) eq 'ARRAY') {
                   6824:         foreach my $item (@{$row}) {
                   6825:             if ($item eq 'enroll_dates') {
                   6826:                 my (%currenrolldate,%newenrolldate);
                   6827:                 foreach my $type ('start','end') {
                   6828:                     $currenrolldate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_date'};
                   6829:                     $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
                   6830:                     if ($newenrolldate{$type} ne $currenrolldate{$type}) {
                   6831:                         $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
                   6832:                     }
                   6833:                 }
                   6834:             } elsif ($item eq 'access_dates') {
                   6835:                 my (%currdate,%newdate);
                   6836:                 foreach my $type ('start','end') {
                   6837:                     $currdate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_access'};
                   6838:                     $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
                   6839:                     if ($newdate{$type} ne $currdate{$type}) {
                   6840:                         $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
                   6841:                     }
                   6842:                 }
1.241     raeburn  6843:             } elsif ($item eq 'types') {
                   6844:                 $curr_types =
                   6845:                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$item};
                   6846:                 if ($env{'form.selfenroll_all'}) {
                   6847:                     if ($curr_types ne '*') {
                   6848:                         $changes{'internal.selfenroll_types'} = '*';
                   6849:                     } else {
                   6850:                         next;
                   6851:                     }
                   6852:                 } else {
1.249     raeburn  6853:                     my %currdoms;
1.241     raeburn  6854:                     my @entries = split(/;/,$curr_types);
                   6855:                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
1.249     raeburn  6856:                     my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
1.241     raeburn  6857:                     my $newnum = 0;
1.249     raeburn  6858:                     my @latesttypes;
                   6859:                     foreach my $num (@activations) {
                   6860:                         my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
                   6861:                         if (@types > 0) {
1.241     raeburn  6862:                             @types = sort(@types);
                   6863:                             my $typestr = join(',',@types);
1.249     raeburn  6864:                             my $typedom = $env{'form.selfenroll_dom_'.$num};
                   6865:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   6866:                             $currdoms{$typedom} = 1;
1.241     raeburn  6867:                             $newnum ++;
                   6868:                         }
                   6869:                     }
1.338     raeburn  6870:                     for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {
                   6871:                         if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
1.249     raeburn  6872:                             my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
                   6873:                             if (@types > 0) {
                   6874:                                 @types = sort(@types);
                   6875:                                 my $typestr = join(',',@types);
                   6876:                                 my $typedom = $env{'form.selfenroll_dom_'.$j};
                   6877:                                 $latesttypes[$newnum] = $typedom.':'.$typestr;
                   6878:                                 $currdoms{$typedom} = 1;
                   6879:                                 $newnum ++;
                   6880:                             }
                   6881:                         }
                   6882:                     }
                   6883:                     if ($env{'form.selfenroll_newdom'} ne '') {
                   6884:                         my $typedom = $env{'form.selfenroll_newdom'};
                   6885:                         if ((!defined($currdoms{$typedom})) && 
                   6886:                             (&Apache::lonnet::domain($typedom) ne '')) {
                   6887:                             my $typestr;
                   6888:                             my ($othertitle,$usertypes,$types) = 
                   6889:                                 &Apache::loncommon::sorted_inst_types($typedom);
                   6890:                             my $othervalue = 'any';
                   6891:                             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   6892:                                 if (@{$types} > 0) {
1.257     raeburn  6893:                                     my @esc_types = map { &escape($_); } @{$types};
1.249     raeburn  6894:                                     $othervalue = 'other';
1.258     raeburn  6895:                                     $typestr = join(',',(@esc_types,$othervalue));
1.249     raeburn  6896:                                 }
                   6897:                                 $typestr = $othervalue;
                   6898:                             } else {
                   6899:                                 $typestr = $othervalue;
                   6900:                             } 
                   6901:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   6902:                             $newnum ++ ;
                   6903:                         }
                   6904:                     }
1.241     raeburn  6905:                     my $selfenroll_types = join(';',@latesttypes);
                   6906:                     if ($selfenroll_types ne $curr_types) {
                   6907:                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
                   6908:                     }
                   6909:                 }
1.276     raeburn  6910:             } elsif ($item eq 'limit') {
                   6911:                 my $newlimit = $env{'form.selfenroll_limit'};
                   6912:                 my $newcap = $env{'form.selfenroll_cap'};
                   6913:                 $newcap =~s/\s+//g;
                   6914:                 my $currlimit =  $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_limit'};
                   6915:                 $currlimit = 'none' if ($currlimit eq '');
                   6916:                 my $currcap = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_cap'};
                   6917:                 if ($newlimit ne $currlimit) {
                   6918:                     if ($newlimit ne 'none') {
                   6919:                         if ($newcap =~ /^\d+$/) {
                   6920:                             if ($newcap ne $currcap) {
                   6921:                                 $changes{'internal.selfenroll_cap'} = $newcap;
                   6922:                             }
                   6923:                             $changes{'internal.selfenroll_limit'} = $newlimit;
                   6924:                         } else {
                   6925:                             $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.&mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.'); 
                   6926:                         }
                   6927:                     } elsif ($currcap ne '') {
                   6928:                         $changes{'internal.selfenroll_cap'} = '';
                   6929:                         $changes{'internal.selfenroll_limit'} = $newlimit; 
                   6930:                     }
                   6931:                 } elsif ($currlimit ne 'none') {
                   6932:                     if ($newcap =~ /^\d+$/) {
                   6933:                         if ($newcap ne $currcap) {
                   6934:                             $changes{'internal.selfenroll_cap'} = $newcap;
                   6935:                         }
                   6936:                     } else {
                   6937:                         $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.&mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.');
                   6938:                     }
                   6939:                 }
                   6940:             } elsif ($item eq 'approval') {
                   6941:                 my (@currnotified,@newnotified);
                   6942:                 my $currapproval = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'};
                   6943:                 my $currnotifylist = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_notifylist'};
                   6944:                 if ($currnotifylist ne '') {
                   6945:                     @currnotified = split(/,/,$currnotifylist);
                   6946:                     @currnotified = sort(@currnotified);
                   6947:                 }
                   6948:                 my $newapproval = $env{'form.selfenroll_approval'};
                   6949:                 @newnotified = &Apache::loncommon::get_env_multiple('form.selfenroll_notify');
                   6950:                 @newnotified = sort(@newnotified);
                   6951:                 if ($newapproval ne $currapproval) {
                   6952:                     $changes{'internal.selfenroll_approval'} = $newapproval;
                   6953:                     if (!$newapproval) {
                   6954:                         if ($currnotifylist ne '') {
                   6955:                             $changes{'internal.selfenroll_notifylist'} = '';
                   6956:                         }
                   6957:                     } else {
                   6958:                         my @differences =  
1.295     raeburn  6959:                             &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  6960:                         if (@differences > 0) {
                   6961:                             if (@newnotified > 0) {
                   6962:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   6963:                             } else {
                   6964:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   6965:                             }
                   6966:                         }
                   6967:                     }
                   6968:                 } else {
1.295     raeburn  6969:                     my @differences = &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  6970:                     if (@differences > 0) {
                   6971:                         if (@newnotified > 0) {
                   6972:                             $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   6973:                         } else {
                   6974:                             $changes{'internal.selfenroll_notifylist'} = '';
                   6975:                         }
                   6976:                     }
                   6977:                 }
1.237     raeburn  6978:             } else {
                   6979:                 my $curr_val = 
                   6980:                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$item};
                   6981:                 my $newval = $env{'form.selfenroll_'.$item};
                   6982:                 if ($item eq 'section') {
                   6983:                     $newval = $env{'form.sections'};
1.241     raeburn  6984:                     if (defined($curr_groups{$newval})) {
1.237     raeburn  6985:                         $newval = $curr_val;
                   6986:                         $warning{$item} = &mt('Section for self-enrolled users unchanged as the proposed section is a group').'<br />'.&mt('Group names and section names must be distinct');
                   6987:                     } elsif ($newval eq 'all') {
                   6988:                         $newval = $curr_val;
1.274     bisitz   6989:                         $warning{$item} = &mt('Section for self-enrolled users unchanged, as "all" is a reserved section name.');
1.237     raeburn  6990:                     }
                   6991:                     if ($newval eq '') {
                   6992:                         $newval = 'none';
                   6993:                     }
                   6994:                 }
                   6995:                 if ($newval ne $curr_val) {
                   6996:                     $changes{'internal.selfenroll_'.$item} = $newval;
                   6997:                 }
1.241     raeburn  6998:             }
1.237     raeburn  6999:         }
                   7000:         if (keys(%warning) > 0) {
                   7001:             foreach my $item (@{$row}) {
                   7002:                 if (exists($warning{$item})) {
                   7003:                     $r->print($warning{$item}.'<br />');
                   7004:                 }
                   7005:             } 
                   7006:         }
                   7007:         if (keys(%changes) > 0) {
                   7008:             my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
                   7009:             if ($putresult eq 'ok') {
                   7010:                 if ((exists($changes{'internal.selfenroll_types'})) ||
                   7011:                     (exists($changes{'internal.selfenroll_start_date'}))  ||
                   7012:                     (exists($changes{'internal.selfenroll_end_date'}))) {
                   7013:                     my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
                   7014:                                                                 $cnum,undef,undef,'Course');
                   7015:                     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
                   7016:                     if (ref($crsinfo{$env{'request.course.id'}}) eq 'HASH') {
                   7017:                         foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
                   7018:                             if (exists($changes{'internal.'.$item})) {
                   7019:                                 $crsinfo{$env{'request.course.id'}}{$item} = 
                   7020:                                     $changes{'internal.'.$item};
                   7021:                             }
                   7022:                         }
                   7023:                         my $crsputresult =
                   7024:                             &Apache::lonnet::courseidput($cdom,\%crsinfo,
                   7025:                                                          $chome,'notime');
                   7026:                     }
                   7027:                 }
                   7028:                 $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
                   7029:                 foreach my $item (@{$row}) {
                   7030:                     my $title = $item;
                   7031:                     if (ref($lt) eq 'HASH') {
                   7032:                         $title = $lt->{$item};
                   7033:                     }
                   7034:                     if ($item eq 'enroll_dates') {
                   7035:                         foreach my $type ('start','end') {
                   7036:                             if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
                   7037:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
1.244     bisitz   7038:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  7039:                                           $title,$type,$newdate).'</li>');
                   7040:                             }
                   7041:                         }
                   7042:                     } elsif ($item eq 'access_dates') {
                   7043:                         foreach my $type ('start','end') {
                   7044:                             if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
                   7045:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
1.244     bisitz   7046:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  7047:                                           $title,$type,$newdate).'</li>');
                   7048:                             }
                   7049:                         }
1.276     raeburn  7050:                     } elsif ($item eq 'limit') {
                   7051:                         if ((exists($changes{'internal.selfenroll_limit'})) ||
                   7052:                             (exists($changes{'internal.selfenroll_cap'}))) {
                   7053:                             my ($newval,$newcap);
                   7054:                             if ($changes{'internal.selfenroll_cap'} ne '') {
                   7055:                                 $newcap = $changes{'internal.selfenroll_cap'}
                   7056:                             } else {
                   7057:                                 $newcap = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_cap'};
                   7058:                             }
                   7059:                             if ($changes{'internal.selfenroll_limit'} eq 'none') {
                   7060:                                 $newval = &mt('No limit');
                   7061:                             } elsif ($changes{'internal.selfenroll_limit'} eq 
                   7062:                                      'allstudents') {
                   7063:                                 $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   7064:                             } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
                   7065:                                 $newval = &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
                   7066:                             } else {
                   7067:                                 my $currlimit =  $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_limit'};
                   7068:                                 if ($currlimit eq 'allstudents') {
                   7069:                                     $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   7070:                                 } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
1.308     raeburn  7071:                                     $newval =  &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
1.276     raeburn  7072:                                 }
                   7073:                             }
                   7074:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
                   7075:                         }
                   7076:                     } elsif ($item eq 'approval') {
                   7077:                         if ((exists($changes{'internal.selfenroll_approval'})) ||
                   7078:                             (exists($changes{'internal.selfenroll_notifylist'}))) {
                   7079:                             my ($newval,$newnotify);
                   7080:                             if (exists($changes{'internal.selfenroll_notifylist'})) {
                   7081:                                 $newnotify = $changes{'internal.selfenroll_notifylist'};
                   7082:                             } else {   
                   7083:                                 $newnotify = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_notifylist'};
                   7084:                             }
                   7085:                             if ($changes{'internal.selfenroll_approval'}) {
                   7086:                                 $newval = &mt('Yes');
                   7087:                             } elsif ($changes{'internal.selfenroll_approval'} eq '0') {
                   7088:                                 $newval = &mt('No');
                   7089:                             } else {
                   7090:                                 my $currapproval = 
                   7091:                                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'};
                   7092:                                 if ($currapproval) {
                   7093:                                     $newval = &mt('Yes');
                   7094:                                 } else {
                   7095:                                     $newval = &mt('No');
                   7096:                                 }
                   7097:                             }
                   7098:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval));
                   7099:                             if ($newnotify) {
1.277     raeburn  7100:                                 $r->print('<br />'.&mt('The following will be notified when an enrollment request needs approval, or has been approved: [_1].',$newnotify));
1.276     raeburn  7101:                             } else {
1.277     raeburn  7102:                                 $r->print('<br />'.&mt('No notifications sent when an enrollment request needs approval, or has been approved.'));
1.276     raeburn  7103:                             }
                   7104:                             $r->print('</li>'."\n");
                   7105:                         }
1.237     raeburn  7106:                     } else {
                   7107:                         if (exists($changes{'internal.selfenroll_'.$item})) {
1.241     raeburn  7108:                             my $newval = $changes{'internal.selfenroll_'.$item};
                   7109:                             if ($item eq 'types') {
                   7110:                                 if ($newval eq '') {
                   7111:                                     $newval = &mt('None');
                   7112:                                 } elsif ($newval eq '*') {
                   7113:                                     $newval = &mt('Any user in any domain');
                   7114:                                 }
1.245     raeburn  7115:                             } elsif ($item eq 'registered') {
                   7116:                                 if ($newval eq '1') {
                   7117:                                     $newval = &mt('Yes');
                   7118:                                 } elsif ($newval eq '0') {
                   7119:                                     $newval = &mt('No');
                   7120:                                 }
1.241     raeburn  7121:                             }
1.244     bisitz   7122:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
1.237     raeburn  7123:                         }
                   7124:                     }
                   7125:                 }
                   7126:                 $r->print('</ul>');
                   7127:                 my %newenvhash;
                   7128:                 foreach my $key (keys(%changes)) {
                   7129:                     $newenvhash{'course.'.$env{'request.course.id'}.'.'.$key} = $changes{$key};
                   7130:                 }
1.238     raeburn  7131:                 &Apache::lonnet::appenv(\%newenvhash);
1.237     raeburn  7132:             } else {
                   7133:                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.&mt('The error was: [_1].',$putresult));
                   7134:             }
                   7135:         } else {
1.249     raeburn  7136:             $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.237     raeburn  7137:         }
                   7138:     } else {
1.249     raeburn  7139:         $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.241     raeburn  7140:     }
1.256     raeburn  7141:     my ($visible,$cansetvis,$vismsgs,$visactions) = &visible_in_cat($cdom,$cnum);
                   7142:     if (ref($visactions) eq 'HASH') {
                   7143:         if (!$visible) {
1.366     bisitz   7144:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
1.256     raeburn  7145:                       '<br />');
                   7146:             if (ref($vismsgs) eq 'ARRAY') {
                   7147:                 $r->print('<br />'.$visactions->{'take'}.'<ul>');
                   7148:                 foreach my $item (@{$vismsgs}) {
                   7149:                     $r->print('<li>'.$visactions->{$item}.'</li>');
                   7150:                 }
                   7151:                 $r->print('</ul>');
                   7152:             }
                   7153:             $r->print($cansetvis);
                   7154:         }
                   7155:     } 
1.237     raeburn  7156:     return;
                   7157: }
                   7158: 
                   7159: sub get_selfenroll_titles {
1.276     raeburn  7160:     my @row = ('types','registered','enroll_dates','access_dates','section',
                   7161:                'approval','limit');
1.237     raeburn  7162:     my %lt = &Apache::lonlocal::texthash (
                   7163:                 types        => 'Users allowed to self-enroll in this course',
1.245     raeburn  7164:                 registered   => 'Restrict self-enrollment to students officially registered for the course',
1.237     raeburn  7165:                 enroll_dates => 'Dates self-enrollment available',
1.256     raeburn  7166:                 access_dates => 'Course access dates assigned to self-enrolling users',
                   7167:                 section      => 'Section assigned to self-enrolling users',
1.276     raeburn  7168:                 approval     => 'Self-enrollment requests need approval?',
                   7169:                 limit        => 'Enrollment limit',
1.237     raeburn  7170:              );
                   7171:     return (\@row,\%lt);
                   7172: }
                   7173: 
1.27      matthew  7174: #---------------------------------------------- end functions for &phase_two
1.29      matthew  7175: 
                   7176: #--------------------------------- functions for &phase_two and &phase_three
                   7177: 
                   7178: #--------------------------end of functions for &phase_two and &phase_three
1.372     raeburn  7179: 
1.1       www      7180: 1;
                   7181: __END__
1.2       www      7182: 
                   7183: 

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