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

1.20      harris41    1: # The LearningOnline Network with CAPA
1.1       www         2: # Create a user
                      3: #
1.406.2.9! raeburn     4: # $Id: loncreateuser.pm,v 1.406.2.8 2016/11/29 13:22:10 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: 
1.378     raeburn   125: sub user_quotas {
1.134     raeburn   126:     my ($ccuname,$ccdomain) = @_;
                    127:     my %lt = &Apache::lonlocal::texthash(
1.267     raeburn   128:                    'usrt'      => "User Tools",
                    129:                    'cust'      => "Custom quota",
                    130:                    'chqu'      => "Change quota",
1.134     raeburn   131:     );
1.378     raeburn   132:    
1.149     raeburn   133:     my $quota_javascript = <<"END_SCRIPT";
                    134: <script type="text/javascript">
1.301     bisitz    135: // <![CDATA[
1.378     raeburn   136: function quota_changes(caller,context) {
                    137:     var customoff = document.getElementById('custom_'+context+'quota_off');
                    138:     var customon = document.getElementById('custom_'+context+'quota_on');
                    139:     var number = document.getElementById(context+'quota');
1.149     raeburn   140:     if (caller == "custom") {
1.378     raeburn   141:         if (customoff) {
                    142:             if (customoff.checked) {
                    143:                 number.value = "";
                    144:             }
1.149     raeburn   145:         }
                    146:     }
                    147:     if (caller == "quota") {
1.378     raeburn   148:         if (customon) {
                    149:             customon.checked = true;
                    150:         }
1.149     raeburn   151:     }
1.378     raeburn   152:     return;
1.149     raeburn   153: }
1.301     bisitz    154: // ]]>
1.149     raeburn   155: </script>
                    156: END_SCRIPT
1.378     raeburn   157:     my $longinsttype;
                    158:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
1.267     raeburn   159:     my $output = $quota_javascript."\n".
                    160:                  '<h3>'.$lt{'usrt'}.'</h3>'."\n".
                    161:                  &Apache::loncommon::start_data_table();
                    162: 
1.406.2.6  raeburn   163:     if ((&Apache::lonnet::allowed('mut',$ccdomain)) ||
                    164:         (&Apache::lonnet::allowed('udp',$ccdomain))) {
1.275     raeburn   165:         $output .= &build_tools_display($ccuname,$ccdomain,'tools');
1.267     raeburn   166:     }
1.378     raeburn   167: 
                    168:     my %titles = &Apache::lonlocal::texthash (
                    169:                     portfolio => "Disk space allocated to user's portfolio files",
1.385     bisitz    170:                     author    => "Disk space allocated to user's Authoring Space (if role assigned)",
1.378     raeburn   171:                  );
                    172:     foreach my $name ('portfolio','author') {
                    173:         my ($currquota,$quotatype,$inststatus,$defquota) =
                    174:             &Apache::loncommon::get_user_quota($ccuname,$ccdomain,$name);
                    175:         if ($longinsttype eq '') { 
                    176:             if ($inststatus ne '') {
                    177:                 if ($usertypes->{$inststatus} ne '') {
                    178:                     $longinsttype = $usertypes->{$inststatus};
                    179:                 }
                    180:             }
                    181:         }
                    182:         my ($showquota,$custom_on,$custom_off,$defaultinfo);
                    183:         $custom_on = ' ';
                    184:         $custom_off = ' checked="checked" ';
                    185:         if ($quotatype eq 'custom') {
                    186:             $custom_on = $custom_off;
                    187:             $custom_off = ' ';
                    188:             $showquota = $currquota;
                    189:             if ($longinsttype eq '') {
                    190:                 $defaultinfo = &mt('For this user, the default quota would be [_1]'
1.383     raeburn   191:                               .' MB.',$defquota);
1.378     raeburn   192:             } else {
                    193:                 $defaultinfo = &mt("For this user, the default quota would be [_1]".
1.383     raeburn   194:                                    " MB, as determined by the user's institutional".
1.378     raeburn   195:                                    " affiliation ([_2]).",$defquota,$longinsttype);
                    196:             }
                    197:         } else {
                    198:             if ($longinsttype eq '') {
                    199:                 $defaultinfo = &mt('For this user, the default quota is [_1]'
1.383     raeburn   200:                               .' MB.',$defquota);
1.378     raeburn   201:             } else {
                    202:                 $defaultinfo = &mt("For this user, the default quota of [_1]".
1.383     raeburn   203:                                    " MB, is determined by the user's institutional".
1.378     raeburn   204:                                    " affiliation ([_2]).",$defquota,$longinsttype);
                    205:             }
                    206:         }
                    207: 
                    208:         if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
                    209:             $output .= '<tr class="LC_info_row">'."\n".
                    210:                        '    <td>'.$titles{$name}.'</td>'."\n".
                    211:                        '  </tr>'."\n".
                    212:                        &Apache::loncommon::start_data_table_row()."\n".
1.390     bisitz    213:                        '  <td><span class="LC_nobreak">'.
                    214:                        &mt('Current quota: [_1] MB',$currquota).'</span>&nbsp;&nbsp;'.
1.378     raeburn   215:                        $defaultinfo.'</td>'."\n".
                    216:                        &Apache::loncommon::end_data_table_row()."\n".
                    217:                        &Apache::loncommon::start_data_table_row()."\n".
                    218:                        '  <td><span class="LC_nobreak">'.$lt{'chqu'}.
                    219:                        ': <label>'.
                    220:                        '<input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_off" '.
1.379     raeburn   221:                        'value="0" '.$custom_off.' onchange="javascript:quota_changes('."'custom','$name'".');"'.
1.390     bisitz    222:                        ' /><span class="LC_nobreak">'.
                    223:                        &mt('Default ([_1] MB)',$defquota).'</span></label>&nbsp;'.
1.378     raeburn   224:                        '&nbsp;<label><input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_on" '.
1.379     raeburn   225:                        'value="1" '.$custom_on.'  onchange="javascript:quota_changes('."'custom','$name'".');"'.
1.378     raeburn   226:                        ' />'.$lt{'cust'}.':</label>&nbsp;'.
1.379     raeburn   227:                        '<input type="text" name="'.$name.'quota" id="'.$name.'quota" size ="5" '.
                    228:                        'value="'.$showquota.'" onfocus="javascript:quota_changes('."'quota','$name'".');"'.
1.390     bisitz    229:                        ' />&nbsp;'.&mt('MB').'</span></td>'."\n".
1.378     raeburn   230:                        &Apache::loncommon::end_data_table_row()."\n";
                    231:         }
                    232:     }
1.267     raeburn   233:     $output .= &Apache::loncommon::end_data_table();
1.134     raeburn   234:     return $output;
                    235: }
                    236: 
1.275     raeburn   237: sub build_tools_display {
                    238:     my ($ccuname,$ccdomain,$context) = @_;
1.306     raeburn   239:     my (@usertools,%userenv,$output,@options,%validations,%reqtitles,%reqdisplay,
1.332     raeburn   240:         $colspan,$isadv,%domconfig);
1.275     raeburn   241:     my %lt = &Apache::lonlocal::texthash (
                    242:                    'blog'       => "Personal User Blog",
                    243:                    'aboutme'    => "Personal Information Page",
1.385     bisitz    244:                    'webdav'     => "WebDAV access to Authoring Spaces (if SSL and author/co-author)",
1.275     raeburn   245:                    'portfolio'  => "Personal User Portfolio",
                    246:                    'avai'       => "Available",
                    247:                    'cusa'       => "availability",
                    248:                    'chse'       => "Change setting",
                    249:                    'usde'       => "Use default",
                    250:                    'uscu'       => "Use custom",
                    251:                    'official'   => 'Can request creation of official courses',
1.299     raeburn   252:                    'unofficial' => 'Can request creation of unofficial courses',
                    253:                    'community'  => 'Can request creation of communities',
1.384     raeburn   254:                    'textbook'   => 'Can request creation of textbook courses',
1.362     raeburn   255:                    'requestauthor'  => 'Can request author space',
1.275     raeburn   256:     );
1.279     raeburn   257:     if ($context eq 'requestcourses') {
1.275     raeburn   258:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.299     raeburn   259:                       'requestcourses.official','requestcourses.unofficial',
1.384     raeburn   260:                       'requestcourses.community','requestcourses.textbook');
                    261:         @usertools = ('official','unofficial','community','textbook');
1.309     raeburn   262:         @options =('norequest','approval','autolimit','validate');
1.306     raeburn   263:         %validations = &Apache::lonnet::auto_courserequest_checks($ccdomain);
                    264:         %reqtitles = &courserequest_titles();
                    265:         %reqdisplay = &courserequest_display();
                    266:         $colspan = ' colspan="2"';
1.332     raeburn   267:         %domconfig =
                    268:             &Apache::lonnet::get_dom('configuration',['requestcourses'],$ccdomain);
1.406.2.6  raeburn   269:         $isadv = &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
1.362     raeburn   270:     } elsif ($context eq 'requestauthor') {
                    271:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    272:                                                     'requestauthor');
                    273:         @usertools = ('requestauthor');
                    274:         @options =('norequest','approval','automatic');
                    275:         %reqtitles = &requestauthor_titles();
                    276:         %reqdisplay = &requestauthor_display();
                    277:         $colspan = ' colspan="2"';
                    278:         %domconfig =
                    279:             &Apache::lonnet::get_dom('configuration',['requestauthor'],$ccdomain);
1.275     raeburn   280:     } else {
                    281:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.361     raeburn   282:                           'tools.aboutme','tools.portfolio','tools.blog',
                    283:                           'tools.webdav');
                    284:         @usertools = ('aboutme','blog','webdav','portfolio');
1.275     raeburn   285:     }
                    286:     foreach my $item (@usertools) {
1.306     raeburn   287:         my ($custom_access,$curr_access,$cust_on,$cust_off,$tool_on,$tool_off,
                    288:             $currdisp,$custdisp,$custradio);
1.275     raeburn   289:         $cust_off = 'checked="checked" ';
                    290:         $tool_on = 'checked="checked" ';
                    291:         $curr_access =  
                    292:             &Apache::lonnet::usertools_access($ccuname,$ccdomain,$item,undef,
                    293:                                               $context);
1.362     raeburn   294:         if ($context eq 'requestauthor') {
                    295:             if ($userenv{$context} ne '') {
                    296:                 $cust_on = ' checked="checked" ';
                    297:                 $cust_off = '';
                    298:             }  
                    299:         } elsif ($userenv{$context.'.'.$item} ne '') {
1.306     raeburn   300:             $cust_on = ' checked="checked" ';
                    301:             $cust_off = '';
                    302:         }
                    303:         if ($context eq 'requestcourses') {
                    304:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   305:                 $custom_access = &mt('Currently from default setting.');
1.306     raeburn   306:             } else {
                    307:                 $custom_access = &mt('Currently from custom setting.');
1.275     raeburn   308:             }
1.362     raeburn   309:         } elsif ($context eq 'requestauthor') {
                    310:             if ($userenv{$context} eq '') {
                    311:                 $custom_access = &mt('Currently from default setting.');
                    312:             } else {
                    313:                 $custom_access = &mt('Currently from custom setting.');
                    314:             }
1.275     raeburn   315:         } else {
1.306     raeburn   316:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   317:                 $custom_access =
1.306     raeburn   318:                     &mt('Availability determined currently from default setting.');
                    319:                 if (!$curr_access) {
                    320:                     $tool_off = 'checked="checked" ';
                    321:                     $tool_on = '';
                    322:                 }
                    323:             } else {
1.314     raeburn   324:                 $custom_access =
1.306     raeburn   325:                     &mt('Availability determined currently from custom setting.');
                    326:                 if ($userenv{$context.'.'.$item} == 0) {
                    327:                     $tool_off = 'checked="checked" ';
                    328:                     $tool_on = '';
                    329:                 }
1.275     raeburn   330:             }
                    331:         }
                    332:         $output .= '  <tr class="LC_info_row">'."\n".
1.306     raeburn   333:                    '   <td'.$colspan.'>'.$lt{$item}.'</td>'."\n".
1.275     raeburn   334:                    '  </tr>'."\n".
1.306     raeburn   335:                    &Apache::loncommon::start_data_table_row()."\n";
1.362     raeburn   336:         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.306     raeburn   337:             my ($curroption,$currlimit);
1.362     raeburn   338:             my $envkey = $context.'.'.$item;
                    339:             if ($context eq 'requestauthor') {
                    340:                 $envkey = $context;
                    341:             }
                    342:             if ($userenv{$envkey} ne '') {
                    343:                 $curroption = $userenv{$envkey};
1.332     raeburn   344:             } else {
                    345:                 my (@inststatuses);
1.362     raeburn   346:                 if ($context eq 'requestcourses') {
                    347:                     $curroption =
                    348:                         &Apache::loncoursequeueadmin::get_processtype('course',$ccuname,$ccdomain,
                    349:                                                                       $isadv,$ccdomain,$item,
                    350:                                                                       \@inststatuses,\%domconfig);
                    351:                 } else {
                    352:                      $curroption = 
                    353:                          &Apache::loncoursequeueadmin::get_processtype('requestauthor',$ccuname,$ccdomain,
                    354:                                                                        $isadv,$ccdomain,undef,
                    355:                                                                        \@inststatuses,\%domconfig);
                    356:                 }
1.332     raeburn   357:             }
1.306     raeburn   358:             if (!$curroption) {
                    359:                 $curroption = 'norequest';
                    360:             }
                    361:             if ($curroption =~ /^autolimit=(\d*)$/) {
                    362:                 $currlimit = $1;
1.314     raeburn   363:                 if ($currlimit eq '') {
                    364:                     $currdisp = &mt('Yes, automatic creation');
                    365:                 } else {
                    366:                     $currdisp = &mt('Yes, up to [quant,_1,request]/user',$currlimit);
                    367:                 }
1.306     raeburn   368:             } else {
                    369:                 $currdisp = $reqdisplay{$curroption};
                    370:             }
                    371:             $custdisp = '<table>';
                    372:             foreach my $option (@options) {
                    373:                 my $val = $option;
                    374:                 if ($option eq 'norequest') {
                    375:                     $val = 0;
                    376:                 }
                    377:                 if ($option eq 'validate') {
                    378:                     my $canvalidate = 0;
                    379:                     if (ref($validations{$item}) eq 'HASH') {
                    380:                         if ($validations{$item}{'_custom_'}) {
                    381:                             $canvalidate = 1;
                    382:                         }
                    383:                     }
                    384:                     next if (!$canvalidate);
                    385:                 }
                    386:                 my $checked = '';
                    387:                 if ($option eq $curroption) {
                    388:                     $checked = ' checked="checked"';
                    389:                 } elsif ($option eq 'autolimit') {
                    390:                     if ($curroption =~ /^autolimit/) {
                    391:                         $checked = ' checked="checked"';
                    392:                     }
                    393:                 }
1.362     raeburn   394:                 my $name = 'crsreq_'.$item;
                    395:                 if ($context eq 'requestauthor') {
                    396:                     $name = $item;
                    397:                 }
1.306     raeburn   398:                 $custdisp .= '<tr><td><span class="LC_nobreak"><label>'.
1.362     raeburn   399:                              '<input type="radio" name="'.$name.'" '.
                    400:                              'value="'.$val.'"'.$checked.' />'.
1.306     raeburn   401:                              $reqtitles{$option}.'</label>&nbsp;';
                    402:                 if ($option eq 'autolimit') {
1.362     raeburn   403:                     $custdisp .= '<input type="text" name="'.$name.
                    404:                                  '_limit" size="1" '.
1.314     raeburn   405:                                  'value="'.$currlimit.'" /></span><br />'.
                    406:                                  $reqtitles{'unlimited'};
1.362     raeburn   407:                 } else {
                    408:                     $custdisp .= '</span>';
                    409:                 }
                    410:                 $custdisp .= '</td></tr>';
1.306     raeburn   411:             }
                    412:             $custdisp .= '</table>';
                    413:             $custradio = '</span></td><td>'.&mt('Custom setting').'<br />'.$custdisp;
                    414:         } else {
                    415:             $currdisp = ($curr_access?&mt('Yes'):&mt('No'));
1.362     raeburn   416:             my $name = $context.'_'.$item;
                    417:             if ($context eq 'requestauthor') {
                    418:                 $name = $context;
                    419:             }
1.306     raeburn   420:             $custdisp = '<span class="LC_nobreak"><label>'.
1.362     raeburn   421:                         '<input type="radio" name="'.$name.'"'.
1.361     raeburn   422:                         ' value="1" '.$tool_on.'/>'.&mt('On').'</label>&nbsp;<label>'.
1.362     raeburn   423:                         '<input type="radio" name="'.$name.'" value="0" '.
1.306     raeburn   424:                         $tool_off.'/>'.&mt('Off').'</label></span>';
                    425:             $custradio = ('&nbsp;'x2).'--'.$lt{'cusa'}.':&nbsp;'.$custdisp.
                    426:                           '</span>';
                    427:         }
                    428:         $output .= '  <td'.$colspan.'>'.$custom_access.('&nbsp;'x4).
                    429:                    $lt{'avai'}.': '.$currdisp.'</td>'."\n".
1.406.2.6  raeburn   430:                    &Apache::loncommon::end_data_table_row()."\n";
                    431:         unless (&Apache::lonnet::allowed('udp',$ccdomain)) {
                    432:             $output .=
1.275     raeburn   433:                    &Apache::loncommon::start_data_table_row()."\n".
1.306     raeburn   434:                    '  <td style="vertical-align:top;"><span class="LC_nobreak">'.
                    435:                    $lt{'chse'}.': <label>'.
1.275     raeburn   436:                    '<input type="radio" name="custom'.$item.'" value="0" '.
1.306     raeburn   437:                    $cust_off.'/>'.$lt{'usde'}.'</label>'.('&nbsp;' x3).
                    438:                    '<label><input type="radio" name="custom'.$item.'" value="1" '.
                    439:                    $cust_on.'/>'.$lt{'uscu'}.'</label>'.$custradio.'</td>'.
1.275     raeburn   440:                    &Apache::loncommon::end_data_table_row()."\n";
1.406.2.6  raeburn   441:         }
1.275     raeburn   442:     }
                    443:     return $output;
                    444: }
                    445: 
1.300     raeburn   446: sub coursereq_externaluser {
                    447:     my ($ccuname,$ccdomain,$cdom) = @_;
1.306     raeburn   448:     my (@usertools,@options,%validations,%userenv,$output);
1.300     raeburn   449:     my %lt = &Apache::lonlocal::texthash (
                    450:                    'official'   => 'Can request creation of official courses',
                    451:                    'unofficial' => 'Can request creation of unofficial courses',
                    452:                    'community'  => 'Can request creation of communities',
1.384     raeburn   453:                    'textbook'   => 'Can request creation of textbook courses',
1.300     raeburn   454:     );
                    455: 
                    456:     %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    457:                       'reqcrsotherdom.official','reqcrsotherdom.unofficial',
1.384     raeburn   458:                       'reqcrsotherdom.community','reqcrsotherdom.textbook');
                    459:     @usertools = ('official','unofficial','community','textbook');
1.309     raeburn   460:     @options = ('approval','validate','autolimit');
1.306     raeburn   461:     %validations = &Apache::lonnet::auto_courserequest_checks($cdom);
                    462:     my $optregex = join('|',@options);
                    463:     my %reqtitles = &courserequest_titles();
1.300     raeburn   464:     foreach my $item (@usertools) {
1.306     raeburn   465:         my ($curroption,$currlimit,$tooloff);
1.300     raeburn   466:         if ($userenv{'reqcrsotherdom.'.$item} ne '') {
                    467:             my @curr = split(',',$userenv{'reqcrsotherdom.'.$item});
1.314     raeburn   468:             foreach my $req (@curr) {
                    469:                 if ($req =~ /^\Q$cdom\E\:($optregex)=?(\d*)$/) {
                    470:                     $curroption = $1;
                    471:                     $currlimit = $2;
                    472:                     last;
1.306     raeburn   473:                 }
                    474:             }
1.314     raeburn   475:             if (!$curroption) {
                    476:                 $curroption = 'norequest';
                    477:                 $tooloff = ' checked="checked"';
                    478:             }
1.306     raeburn   479:         } else {
                    480:             $curroption = 'norequest';
                    481:             $tooloff = ' checked="checked"';
                    482:         }
                    483:         $output.= &Apache::loncommon::start_data_table_row()."\n".
1.314     raeburn   484:                   '  <td><span class="LC_nobreak">'.$lt{$item}.': </span></td><td>'.
                    485:                   '<table><tr><td valign="top">'."\n".
1.306     raeburn   486:                   '<label><input type="radio" name="reqcrsotherdom_'.$item.
1.314     raeburn   487:                   '" value=""'.$tooloff.' />'.$reqtitles{'norequest'}.
                    488:                   '</label></td>';
1.306     raeburn   489:         foreach my $option (@options) {
                    490:             if ($option eq 'validate') {
                    491:                 my $canvalidate = 0;
                    492:                 if (ref($validations{$item}) eq 'HASH') {
                    493:                     if ($validations{$item}{'_external_'}) {
                    494:                         $canvalidate = 1;
                    495:                     }
                    496:                 }
                    497:                 next if (!$canvalidate);
                    498:             }
                    499:             my $checked = '';
                    500:             if ($option eq $curroption) {
                    501:                 $checked = ' checked="checked"';
                    502:             }
1.314     raeburn   503:             $output .= '<td valign="top"><span class="LC_nobreak"><label>'.
1.306     raeburn   504:                        '<input type="radio" name="reqcrsotherdom_'.$item.
                    505:                        '" value="'.$option.'"'.$checked.' />'.
1.314     raeburn   506:                        $reqtitles{$option}.'</label>';
1.306     raeburn   507:             if ($option eq 'autolimit') {
1.314     raeburn   508:                 $output .= '&nbsp;<input type="text" name="reqcrsotherdom_'.
1.306     raeburn   509:                            $item.'_limit" size="1" '.
1.314     raeburn   510:                            'value="'.$currlimit.'" /></span>'.
                    511:                            '<br />'.$reqtitles{'unlimited'};
                    512:             } else {
                    513:                 $output .= '</span>';
1.300     raeburn   514:             }
1.314     raeburn   515:             $output .= '</td>';
1.300     raeburn   516:         }
1.314     raeburn   517:         $output .= '</td></tr></table></td>'."\n".
1.300     raeburn   518:                    &Apache::loncommon::end_data_table_row()."\n";
                    519:     }
                    520:     return $output;
                    521: }
                    522: 
1.362     raeburn   523: sub domainrole_req {
                    524:     my ($ccuname,$ccdomain) = @_;
                    525:     return '<br /><h3>'.
                    526:            &mt('User Can Request Assignment of Domain Roles?').
                    527:            '</h3>'."\n".
                    528:            &Apache::loncommon::start_data_table().
                    529:            &build_tools_display($ccuname,$ccdomain,
                    530:                                 'requestauthor').
                    531:            &Apache::loncommon::end_data_table();
                    532: }
                    533: 
1.306     raeburn   534: sub courserequest_titles {
                    535:     my %titles = &Apache::lonlocal::texthash (
                    536:                                    official   => 'Official',
                    537:                                    unofficial => 'Unofficial',
                    538:                                    community  => 'Communities',
1.384     raeburn   539:                                    textbook   => 'Textbook',
1.306     raeburn   540:                                    norequest  => 'Not allowed',
1.309     raeburn   541:                                    approval   => 'Approval by Dom. Coord.',
1.306     raeburn   542:                                    validate   => 'With validation',
                    543:                                    autolimit  => 'Numerical limit',
1.314     raeburn   544:                                    unlimited  => '(blank for unlimited)',
1.306     raeburn   545:                  );
                    546:     return %titles;
                    547: }
                    548: 
                    549: sub courserequest_display {
                    550:     my %titles = &Apache::lonlocal::texthash (
1.309     raeburn   551:                                    approval   => 'Yes, need approval',
1.306     raeburn   552:                                    validate   => 'Yes, with validation',
                    553:                                    norequest  => 'No',
                    554:    );
                    555:    return %titles;
                    556: }
                    557: 
1.362     raeburn   558: sub requestauthor_titles {
                    559:     my %titles = &Apache::lonlocal::texthash (
                    560:                                    norequest  => 'Not allowed',
                    561:                                    approval   => 'Approval by Dom. Coord.',
                    562:                                    automatic  => 'Automatic approval',
                    563:                  );
                    564:     return %titles;
                    565: 
                    566: }
                    567: 
                    568: sub requestauthor_display {
                    569:     my %titles = &Apache::lonlocal::texthash (
                    570:                                    approval   => 'Yes, need approval',
                    571:                                    automatic  => 'Yes, automatic approval',
                    572:                                    norequest  => 'No',
                    573:    );
                    574:    return %titles;
                    575: }
                    576: 
1.383     raeburn   577: sub requestchange_display {
                    578:     my %titles = &Apache::lonlocal::texthash (
                    579:                                    approval   => "availability set to 'on' (approval required)", 
                    580:                                    automatic  => "availability set to 'on' (automatic approval)",
                    581:                                    norequest  => "availability set to 'off'",
                    582:    );
                    583:    return %titles;
                    584: }
                    585: 
1.362     raeburn   586: sub curr_requestauthor {
                    587:     my ($uname,$udom,$isadv,$inststatuses,$domconfig) = @_;
                    588:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
                    589:     if ($uname eq '' || $udom eq '') {
                    590:         $uname = $env{'user.name'};
                    591:         $udom = $env{'user.domain'};
                    592:         $isadv = $env{'user.adv'};
                    593:     }
                    594:     my (%userenv,%settings,$val);
                    595:     my @options = ('automatic','approval');
                    596:     %userenv =
                    597:         &Apache::lonnet::userenvironment($udom,$uname,'requestauthor','inststatus');
                    598:     if ($userenv{'requestauthor'}) {
                    599:         $val = $userenv{'requestauthor'};
                    600:         @{$inststatuses} = ('_custom_');
                    601:     } else {
                    602:         my %alltasks;
                    603:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') {
                    604:             %settings = %{$domconfig->{'requestauthor'}};
                    605:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
                    606:                 $val = $settings{'_LC_adv'};
                    607:                 @{$inststatuses} = ('_LC_adv_');
                    608:             } else {
                    609:                 if ($userenv{'inststatus'} ne '') {
                    610:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
                    611:                 } else {
                    612:                     @{$inststatuses} = ('default');
                    613:                 }
                    614:                 foreach my $status (@{$inststatuses}) {
                    615:                     if (exists($settings{$status})) {
                    616:                         my $value = $settings{$status};
                    617:                         next unless ($value);
                    618:                         unless (exists($alltasks{$value})) {
                    619:                             if (ref($alltasks{$value}) eq 'ARRAY') {
                    620:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
                    621:                                     push(@{$alltasks{$value}},$status);
                    622:                                 }
                    623:                             } else {
                    624:                                 @{$alltasks{$value}} = ($status);
                    625:                             }
                    626:                         }
                    627:                     }
                    628:                 }
                    629:                 foreach my $option (@options) {
                    630:                     if ($alltasks{$option}) {
                    631:                         $val = $option;
                    632:                         last;
                    633:                     }
                    634:                 }
                    635:             }
                    636:         }
                    637:     }
                    638:     return $val;
                    639: }
                    640: 
1.2       www       641: # =================================================================== Phase one
1.1       www       642: 
1.42      matthew   643: sub print_username_entry_form {
1.351     raeburn   644:     my ($r,$context,$response,$srch,$forcenewuser,$crstype,$brcrum) = @_;
1.101     albertel  645:     my $defdom=$env{'request.role.domain'};
1.160     raeburn   646:     my $formtoset = 'crtuser';
                    647:     if (exists($env{'form.startrolename'})) {
                    648:         $formtoset = 'docustom';
                    649:         $env{'form.rolename'} = $env{'form.startrolename'};
1.207     raeburn   650:     } elsif ($env{'form.origform'} eq 'crtusername') {
                    651:         $formtoset =  $env{'form.origform'};
1.160     raeburn   652:     }
                    653: 
                    654:     my ($jsback,$elements) = &crumb_utilities();
                    655: 
                    656:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
1.165     albertel  657:         '<script type="text/javascript">'."\n".
1.301     bisitz    658:         '// <![CDATA['."\n".
                    659:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset})."\n".
                    660:         '// ]]>'."\n".
1.162     raeburn   661:         '</script>'."\n";
1.160     raeburn   662: 
1.324     raeburn   663:     my %existingroles=&Apache::lonuserutils::my_custom_roles($crstype);
                    664:     if (($env{'form.action'} eq 'custom') && (keys(%existingroles) > 0)
                    665:         && (&Apache::lonnet::allowed('mcr','/'))) {
                    666:         $jscript .= &customrole_javascript();
                    667:     }
1.224     raeburn   668:     my $helpitem = 'Course_Change_Privileges';
                    669:     if ($env{'form.action'} eq 'custom') {
                    670:         $helpitem = 'Course_Editing_Custom_Roles';
                    671:     } elsif ($env{'form.action'} eq 'singlestudent') {
                    672:         $helpitem = 'Course_Add_Student';
1.406.2.5  raeburn   673:     } elsif ($env{'form.action'} eq 'accesslogs') {
                    674:         $helpitem = 'Domain_User_Access_Logs';
1.224     raeburn   675:     }
1.406.2.7  raeburn   676:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$defdom);
1.351     raeburn   677:     if ($env{'form.action'} eq 'custom') {
                    678:         push(@{$brcrum},
                    679:                  {href=>"javascript:backPage(document.crtuser)",       
                    680:                   text=>"Pick custom role",
                    681:                   help => $helpitem,}
                    682:                  );
                    683:     } else {
                    684:         push (@{$brcrum},
                    685:                   {href => "javascript:backPage(document.crtuser)",
                    686:                    text => $breadcrumb_text{'search'},
                    687:                    help => $helpitem,
                    688:                    faq  => 282,
                    689:                    bug  => 'Instructor Interface',}
                    690:                   );
                    691:     }
                    692:     my %loaditems = (
                    693:                 'onload' => "javascript:setFormElements(document.$formtoset)",
                    694:                     );
                    695:     my $args = {bread_crumbs           => $brcrum,
                    696:                 bread_crumbs_component => 'User Management',
                    697:                 add_entries            => \%loaditems,};
                    698:     $r->print(&Apache::loncommon::start_page('User Management',$jscript,$args));
                    699: 
1.71      sakharuk  700:     my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   701:                     'srst' => 'Search for a user and enroll as a student',
1.318     raeburn   702:                     'srme' => 'Search for a user and enroll as a member',
1.229     raeburn   703:                     'srad' => 'Search for a user and modify/add user information or roles',
1.406.2.7  raeburn   704:                     'srvu' => 'Search for a user and view user information and roles',
1.406.2.5  raeburn   705:                     'srva' => 'Search for a user and view access log information',
1.71      sakharuk  706: 		    'usr'  => "Username",
                    707:                     'dom'  => "Domain",
1.324     raeburn   708:                     'ecrp' => "Define or Edit Custom Role",
                    709:                     'nr'   => "role name",
1.282     schafran  710:                     'cre'  => "Next",
1.71      sakharuk  711: 				       );
1.351     raeburn   712: 
1.214     raeburn   713:     if ($env{'form.action'} eq 'custom') {
1.190     raeburn   714:         if (&Apache::lonnet::allowed('mcr','/')) {
1.324     raeburn   715:             my $newroletext = &mt('Define new custom role:');
                    716:             $r->print('<form action="/adm/createuser" method="post" name="docustom">'.
                    717:                       '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                    718:                       '<input type="hidden" name="phase" value="selected_custom_edit" />'.
                    719:                       '<h3>'.$lt{'ecrp'}.'</h3>'.
                    720:                       &Apache::loncommon::start_data_table().
                    721:                       &Apache::loncommon::start_data_table_row().
                    722:                       '<td>');
                    723:             if (keys(%existingroles) > 0) {
                    724:                 $r->print('<br /><label><input type="radio" name="customroleaction" value="new" checked="checked" onclick="setCustomFields();" /><b>'.$newroletext.'</b></label>');
                    725:             } else {
                    726:                 $r->print('<br /><input type="hidden" name="customroleaction" value="new" /><b>'.$newroletext.'</b>');
                    727:             }
                    728:             $r->print('</td><td align="center">'.$lt{'nr'}.'<br /><input type="text" size="15" name="newrolename" onfocus="setCustomAction('."'new'".');" /></td>'.
                    729:                       &Apache::loncommon::end_data_table_row());
                    730:             if (keys(%existingroles) > 0) {
                    731:                 $r->print(&Apache::loncommon::start_data_table_row().'<td><br />'.
                    732:                           '<label><input type="radio" name="customroleaction" value="edit" onclick="setCustomFields();"/><b>'.
                    733:                           &mt('View/Modify existing role:').'</b></label></td>'.
                    734:                           '<td align="center"><br />'.
                    735:                           '<select name="rolename" onchange="setCustomAction('."'edit'".');">'.
1.326     raeburn   736:                           '<option value="" selected="selected">'.
1.324     raeburn   737:                           &mt('Select'));
                    738:                 foreach my $role (sort(keys(%existingroles))) {
1.326     raeburn   739:                     $r->print('<option value="'.$role.'">'.$role.'</option>');
1.324     raeburn   740:                 }
                    741:                 $r->print('</select>'.
                    742:                           '</td>'.
                    743:                           &Apache::loncommon::end_data_table_row());
                    744:             }
                    745:             $r->print(&Apache::loncommon::end_data_table().'<p>'.
                    746:                       '<input name="customeditor" type="submit" value="'.
                    747:                       $lt{'cre'}.'" /></p>'.
                    748:                       '</form>');
1.190     raeburn   749:         }
1.213     raeburn   750:     } else {
1.229     raeburn   751:         my $actiontext = $lt{'srad'};
1.213     raeburn   752:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   753:             if ($crstype eq 'Community') {
                    754:                 $actiontext = $lt{'srme'};
                    755:             } else {
                    756:                 $actiontext = $lt{'srst'};
                    757:             }
1.406.2.5  raeburn   758:         } elsif ($env{'form.action'} eq 'accesslogs') {
                    759:             $actiontext = $lt{'srva'};
1.406.2.7  raeburn   760:         } elsif (($env{'form.action'} eq 'singleuser') &&
                    761:                  ($context eq 'domain') && (!&Apache::lonnet::allowed('mau',$defdom))) {
                    762:             $actiontext = $lt{'srvu'};
1.213     raeburn   763:         }
1.324     raeburn   764:         $r->print("<h3>$actiontext</h3>");
1.213     raeburn   765:         if ($env{'form.origform'} ne 'crtusername') {
1.406.2.5  raeburn   766:             if ($response) {
                    767:                $r->print("\n<div>$response</div>".
                    768:                          '<br clear="all" />');
                    769:             }
1.213     raeburn   770:         }
1.406.2.5  raeburn   771:         $r->print(&entry_form($defdom,$srch,$forcenewuser,$context,$response,$crstype,1));
1.107     www       772:     }
1.110     albertel  773: }
                    774: 
1.324     raeburn   775: sub customrole_javascript {
                    776:     my $js = <<"END";
                    777: <script type="text/javascript">
                    778: // <![CDATA[
                    779: 
                    780: function setCustomFields() {
                    781:     if (document.docustom.customroleaction.length > 0) {
                    782:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    783:             if (document.docustom.customroleaction[i].checked) {
                    784:                 if (document.docustom.customroleaction[i].value == 'new') {
                    785:                     document.docustom.rolename.selectedIndex = 0;
                    786:                 } else {
                    787:                     document.docustom.newrolename.value = '';
                    788:                 }
                    789:             }
                    790:         }
                    791:     }
                    792:     return;
                    793: }
                    794: 
                    795: function setCustomAction(caller) {
                    796:     if (document.docustom.customroleaction.length > 0) {
                    797:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    798:             if (document.docustom.customroleaction[i].value == caller) {
                    799:                 document.docustom.customroleaction[i].checked = true;
                    800:             }
                    801:         }
                    802:     }
                    803:     setCustomFields();
                    804:     return;
                    805: }
                    806: 
                    807: // ]]>
                    808: </script>
                    809: END
                    810:     return $js;
                    811: }
                    812: 
1.160     raeburn   813: sub entry_form {
1.406.2.5  raeburn   814:     my ($dom,$srch,$forcenewuser,$context,$responsemsg,$crstype,$fixeddom) = @_;
1.229     raeburn   815:     my ($usertype,$inexact);
1.214     raeburn   816:     if (ref($srch) eq 'HASH') {
                    817:         if (($srch->{'srchin'} eq 'dom') &&
                    818:             ($srch->{'srchby'} eq 'uname') &&
                    819:             ($srch->{'srchtype'} eq 'exact') &&
                    820:             ($srch->{'srchdomain'} ne '') &&
                    821:             ($srch->{'srchterm'} ne '')) {
1.353     raeburn   822:             my (%curr_rules,%got_rules);
1.214     raeburn   823:             my ($rules,$ruleorder) =
                    824:                 &Apache::lonnet::inst_userrules($srch->{'srchdomain'},'username');
1.353     raeburn   825:             $usertype = &Apache::lonuserutils::check_usertype($srch->{'srchdomain'},$srch->{'srchterm'},$rules,\%curr_rules,\%got_rules);
1.229     raeburn   826:         } else {
                    827:             $inexact = 1;
1.214     raeburn   828:         }
1.207     raeburn   829:     }
1.214     raeburn   830:     my $cancreate =
                    831:         &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
1.406.2.3  raeburn   832:     my ($userpicker,$cansearch) = 
1.179     raeburn   833:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
1.406.2.5  raeburn   834:                                        'document.crtuser',$cancreate,$usertype,$context,$fixeddom);
1.160     raeburn   835:     my $srchbutton = &mt('Search');
1.229     raeburn   836:     if ($env{'form.action'} eq 'singlestudent') {
                    837:         $srchbutton = &mt('Search and Enroll');
1.406.2.5  raeburn   838:     } elsif ($env{'form.action'} eq 'accesslogs') {
                    839:         $srchbutton = &mt('Search');
1.229     raeburn   840:     } elsif ($cancreate && $responsemsg ne '' && $inexact) {
                    841:         $srchbutton = &mt('Search or Add New User');
                    842:     }
1.406.2.3  raeburn   843:     my $output;
                    844:     if ($cansearch) {
                    845:         $output = <<"ENDBLOCK";
1.160     raeburn   846: <form action="/adm/createuser" method="post" name="crtuser">
1.190     raeburn   847: <input type="hidden" name="action" value="$env{'form.action'}" />
1.160     raeburn   848: <input type="hidden" name="phase" value="get_user_info" />
                    849: $userpicker
1.179     raeburn   850: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
1.160     raeburn   851: </form>
1.207     raeburn   852: ENDBLOCK
1.406.2.3  raeburn   853:     } else {
                    854:         $output = '<p>'.$userpicker.'</p>';
                    855:     }
1.406.2.7  raeburn   856:     if (($env{'form.phase'} eq '') && ($env{'form.action'} ne 'accesslogs') &&
                    857:         (!(($env{'form.action'} eq 'singleuser') && ($context eq 'domain') &&
                    858:         (!&Apache::lonnet::allowed('mau',$env{'request.role.domain'}))))) {
1.207     raeburn   859:         my $defdom=$env{'request.role.domain'};
                    860:         my $domform = &Apache::loncommon::select_dom_form($defdom,'srchdomain');
                    861:         my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   862:                   'enro' => 'Enroll one student',
1.318     raeburn   863:                   'enrm' => 'Enroll one member',
1.229     raeburn   864:                   'admo' => 'Add/modify a single user',
                    865:                   'crea' => 'create new user if required',
                    866:                   'uskn' => "username is known",
1.207     raeburn   867:                   'crnu' => 'Create a new user',
                    868:                   'usr'  => 'Username',
                    869:                   'dom'  => 'in domain',
1.229     raeburn   870:                   'enrl' => 'Enroll',
                    871:                   'cram'  => 'Create/Modify user',
1.207     raeburn   872:         );
1.229     raeburn   873:         my $sellink=&Apache::loncommon::selectstudent_link('crtusername','srchterm','srchdomain');
                    874:         my ($title,$buttontext,$showresponse);
1.318     raeburn   875:         if ($env{'form.action'} eq 'singlestudent') {
                    876:             if ($crstype eq 'Community') {
                    877:                 $title = $lt{'enrm'};
                    878:             } else {
                    879:                 $title = $lt{'enro'};
                    880:             }
1.229     raeburn   881:             $buttontext = $lt{'enrl'};
                    882:         } else {
                    883:             $title = $lt{'admo'};
                    884:             $buttontext = $lt{'cram'};
                    885:         }
                    886:         if ($cancreate) {
                    887:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'crea'}.')</span>';
                    888:         } else {
                    889:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'uskn'}.')</span>';
                    890:         }
                    891:         if ($env{'form.origform'} eq 'crtusername') {
                    892:             $showresponse = $responsemsg;
                    893:         }
1.207     raeburn   894:         $output .= <<"ENDDOCUMENT";
1.229     raeburn   895: <br />
1.207     raeburn   896: <form action="/adm/createuser" method="post" name="crtusername">
                    897: <input type="hidden" name="action" value="$env{'form.action'}" />
                    898: <input type="hidden" name="phase" value="createnewuser" />
                    899: <input type="hidden" name="srchtype" value="exact" />
1.233     raeburn   900: <input type="hidden" name="srchby" value="uname" />
1.207     raeburn   901: <input type="hidden" name="srchin" value="dom" />
                    902: <input type="hidden" name="forcenewuser" value="1" />
                    903: <input type="hidden" name="origform" value="crtusername" />
1.229     raeburn   904: <h3>$title</h3>
                    905: $showresponse
1.207     raeburn   906: <table>
                    907:  <tr>
                    908:   <td>$lt{'usr'}:</td>
                    909:   <td><input type="text" size="15" name="srchterm" /></td>
                    910:   <td>&nbsp;$lt{'dom'}:</td><td>$domform</td>
1.229     raeburn   911:   <td>&nbsp;$sellink&nbsp;</td>
                    912:   <td>&nbsp;<input name="userrole" type="submit" value="$buttontext" /></td>
1.207     raeburn   913:  </tr>
                    914: </table>
                    915: </form>
1.160     raeburn   916: ENDDOCUMENT
1.207     raeburn   917:     }
1.160     raeburn   918:     return $output;
                    919: }
1.110     albertel  920: 
                    921: sub user_modification_js {
1.113     raeburn   922:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
                    923:     
1.110     albertel  924:     return <<END;
                    925: <script type="text/javascript" language="Javascript">
1.301     bisitz    926: // <![CDATA[
1.314     raeburn   927: 
1.110     albertel  928:     $pjump_def
                    929:     $dc_setcourse_code
                    930: 
                    931:     function dateset() {
                    932:         eval("document.cu."+document.cu.pres_marker.value+
                    933:             ".value=document.cu.pres_value.value");
1.359     www       934:         modalWindow.close();
1.110     albertel  935:     }
                    936: 
1.113     raeburn   937:     $nondc_setsection_code
1.301     bisitz    938: // ]]>
1.110     albertel  939: </script>
                    940: END
1.2       www       941: }
                    942: 
                    943: # =================================================================== Phase two
1.160     raeburn   944: sub print_user_selection_page {
1.351     raeburn   945:     my ($r,$response,$srch,$srch_results,$srcharray,$context,$opener_elements,$crstype,$brcrum) = @_;
1.160     raeburn   946:     my @fields = ('username','domain','lastname','firstname','permanentemail');
                    947:     my $sortby = $env{'form.sortby'};
                    948: 
                    949:     if (!grep(/^\Q$sortby\E$/,@fields)) {
                    950:         $sortby = 'lastname';
                    951:     }
                    952: 
                    953:     my ($jsback,$elements) = &crumb_utilities();
                    954: 
                    955:     my $jscript = (<<ENDSCRIPT);
                    956: <script type="text/javascript">
1.301     bisitz    957: // <![CDATA[
1.160     raeburn   958: function pickuser(uname,udom) {
                    959:     document.usersrchform.seluname.value=uname;
                    960:     document.usersrchform.seludom.value=udom;
                    961:     document.usersrchform.phase.value="userpicked";
                    962:     document.usersrchform.submit();
                    963: }
                    964: 
                    965: $jsback
1.301     bisitz    966: // ]]>
1.160     raeburn   967: </script>
                    968: ENDSCRIPT
                    969: 
                    970:     my %lt=&Apache::lonlocal::texthash(
1.179     raeburn   971:                                        'usrch'          => "User Search to add/modify roles",
                    972:                                        'stusrch'        => "User Search to enroll student",
1.318     raeburn   973:                                        'memsrch'        => "User Search to enroll member",
1.406.2.5  raeburn   974:                                        'srcva'          => "Search for a user and view access log information",
1.406.2.7  raeburn   975:                                        'usrvu'          => "User Search to view user roles",
1.179     raeburn   976:                                        'usel'           => "Select a user to add/modify roles",
1.406.2.7  raeburn   977:                                        'suvr'           => "Select a user to view roles",
1.318     raeburn   978:                                        'stusel'         => "Select a user to enroll as a student",
                    979:                                        'memsel'         => "Select a user to enroll as a member",
1.406.2.5  raeburn   980:                                        'vacsel'         => "Select a user to view access log",
1.160     raeburn   981:                                        'username'       => "username",
                    982:                                        'domain'         => "domain",
                    983:                                        'lastname'       => "last name",
                    984:                                        'firstname'      => "first name",
                    985:                                        'permanentemail' => "permanent e-mail",
                    986:                                       );
1.302     raeburn   987:     if ($context eq 'requestcrs') {
                    988:         $r->print('<div>');
                    989:     } else {
1.406.2.7  raeburn   990:         my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$srch->{'srchdomain'});
1.351     raeburn   991:         my $helpitem;
                    992:         if ($env{'form.action'} eq 'singleuser') {
                    993:             $helpitem = 'Course_Change_Privileges';
                    994:         } elsif ($env{'form.action'} eq 'singlestudent') {
                    995:             $helpitem = 'Course_Add_Student';
                    996:         }
                    997:         push (@{$brcrum},
                    998:                   {href => "javascript:backPage(document.usersrchform,'','')",
                    999:                    text => $breadcrumb_text{'search'},
                   1000:                    faq  => 282,
                   1001:                    bug  => 'Instructor Interface',},
                   1002:                   {href => "javascript:backPage(document.usersrchform,'get_user_info','select')",
                   1003:                    text => $breadcrumb_text{'userpicked'},
                   1004:                    faq  => 282,
                   1005:                    bug  => 'Instructor Interface',
                   1006:                    help => $helpitem}
                   1007:                   );
                   1008:         $r->print(&Apache::loncommon::start_page('User Management',$jscript,{bread_crumbs => $brcrum}));
1.302     raeburn  1009:         if ($env{'form.action'} eq 'singleuser') {
1.406.2.7  raeburn  1010:             my $readonly;
                   1011:             if (($context eq 'domain') && (!&Apache::lonnet::allowed('mau',$srch->{'srchdomain'}))) {
                   1012:                 $readonly = 1;
                   1013:                 $r->print("<b>$lt{'usrvu'}</b><br />");
                   1014:             } else {
                   1015:                 $r->print("<b>$lt{'usrch'}</b><br />");
                   1016:             }
1.318     raeburn  1017:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
1.406.2.7  raeburn  1018:             if ($readonly) {
                   1019:                 $r->print('<h3>'.$lt{'suvr'}.'</h3>');
                   1020:             } else {
                   1021:                 $r->print('<h3>'.$lt{'usel'}.'</h3>');
                   1022:             }
1.302     raeburn  1023:         } elsif ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1024:             $r->print($jscript."<b>");
                   1025:             if ($crstype eq 'Community') {
                   1026:                 $r->print($lt{'memsrch'});
                   1027:             } else {
                   1028:                 $r->print($lt{'stusrch'});
                   1029:             }
                   1030:             $r->print("</b><br />");
                   1031:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
                   1032:             $r->print('</form><h3>');
                   1033:             if ($crstype eq 'Community') {
                   1034:                 $r->print($lt{'memsel'});
                   1035:             } else {
                   1036:                 $r->print($lt{'stusel'});
                   1037:             }
                   1038:             $r->print('</h3>');
1.406.2.5  raeburn  1039:         } elsif ($env{'form.action'} eq 'accesslogs') {
                   1040:             $r->print("<b>$lt{'srcva'}</b><br />");
                   1041:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,'accesslogs',undef,undef,1));
                   1042:             $r->print('<h3>'.$lt{'vacsel'}.'</h3>');
1.302     raeburn  1043:         }
1.179     raeburn  1044:     }
1.380     bisitz   1045:     $r->print('<form name="usersrchform" method="post" action="">'.
1.160     raeburn  1046:               &Apache::loncommon::start_data_table()."\n".
                   1047:               &Apache::loncommon::start_data_table_header_row()."\n".
                   1048:               ' <th> </th>'."\n");
                   1049:     foreach my $field (@fields) {
                   1050:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
                   1051:                   "'".$field."'".';document.usersrchform.submit();">'.
                   1052:                   $lt{$field}.'</a></th>'."\n");
                   1053:     }
                   1054:     $r->print(&Apache::loncommon::end_data_table_header_row());
                   1055: 
                   1056:     my @sorted_users = sort {
1.167     albertel 1057:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
1.160     raeburn  1058:             ||
1.167     albertel 1059:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
1.160     raeburn  1060:             ||
                   1061:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
1.167     albertel 1062: 	    ||
                   1063: 	lc($a) cmp lc($b)
1.160     raeburn  1064:         } (keys(%$srch_results));
                   1065: 
                   1066:     foreach my $user (@sorted_users) {
                   1067:         my ($uname,$udom) = split(/:/,$user);
1.302     raeburn  1068:         my $onclick;
                   1069:         if ($context eq 'requestcrs') {
1.314     raeburn  1070:             $onclick =
1.302     raeburn  1071:                 'onclick="javascript:gochoose('."'$uname','$udom',".
                   1072:                                                "'$srch_results->{$user}->{firstname}',".
                   1073:                                                "'$srch_results->{$user}->{lastname}',".
                   1074:                                                "'$srch_results->{$user}->{permanentemail}'".');"';
                   1075:         } else {
1.314     raeburn  1076:             $onclick =
1.302     raeburn  1077:                 ' onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".');"';
                   1078:         }
1.160     raeburn  1079:         $r->print(&Apache::loncommon::start_data_table_row().
1.302     raeburn  1080:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" '.
                   1081:                   $onclick.' /></td>'.
1.160     raeburn  1082:                   '<td><tt>'.$uname.'</tt></td>'.
                   1083:                   '<td><tt>'.$udom.'</tt></td>');
                   1084:         foreach my $field ('lastname','firstname','permanentemail') {
                   1085:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
                   1086:         }
                   1087:         $r->print(&Apache::loncommon::end_data_table_row());
                   1088:     }
                   1089:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
1.179     raeburn  1090:     if (ref($srcharray) eq 'ARRAY') {
                   1091:         foreach my $item (@{$srcharray}) {
                   1092:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
                   1093:         }
                   1094:     }
1.160     raeburn  1095:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
                   1096:               ' <input type="hidden" name="seluname" value="" />'."\n".
                   1097:               ' <input type="hidden" name="seludom" value="" />'."\n".
1.179     raeburn  1098:               ' <input type="hidden" name="currstate" value="select" />'."\n".
1.190     raeburn  1099:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
1.214     raeburn  1100:               ' <input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
1.302     raeburn  1101:     if ($context eq 'requestcrs') {
                   1102:         $r->print($opener_elements.'</form></div>');
                   1103:     } else {
1.351     raeburn  1104:         $r->print($response.'</form>');
1.302     raeburn  1105:     }
1.160     raeburn  1106: }
                   1107: 
                   1108: sub print_user_query_page {
1.351     raeburn  1109:     my ($r,$caller,$brcrum) = @_;
1.160     raeburn  1110: # FIXME - this is for a network-wide name search (similar to catalog search)
                   1111: # To use frames with similar behavior to catalog/portfolio search.
                   1112: # To be implemented. 
                   1113:     return;
                   1114: }
                   1115: 
1.42      matthew  1116: sub print_user_modification_page {
1.375     raeburn  1117:     my ($r,$ccuname,$ccdomain,$srch,$response,$context,$permission,$crstype,
                   1118:         $brcrum,$showcredits) = @_;
1.185     raeburn  1119:     if (($ccuname eq '') || ($ccdomain eq '')) {
1.215     raeburn  1120:         my $usermsg = &mt('No username and/or domain provided.');
                   1121:         $env{'form.phase'} = '';
1.351     raeburn  1122: 	&print_username_entry_form($r,$context,$usermsg,'','',$crstype,$brcrum);
1.58      www      1123:         return;
                   1124:     }
1.213     raeburn  1125:     my ($form,$formname);
                   1126:     if ($env{'form.action'} eq 'singlestudent') {
                   1127:         $form = 'document.enrollstudent';
                   1128:         $formname = 'enrollstudent';
                   1129:     } else {
                   1130:         $form = 'document.cu';
                   1131:         $formname = 'cu';
                   1132:     }
1.188     raeburn  1133:     my %abv_auth = &auth_abbrev();
1.227     raeburn  1134:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
1.185     raeburn  1135:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
                   1136:     if ($uhome eq 'no_host') {
1.215     raeburn  1137:         my $usertype;
                   1138:         my ($rules,$ruleorder) =
                   1139:             &Apache::lonnet::inst_userrules($ccdomain,'username');
                   1140:             $usertype =
1.353     raeburn  1141:                 &Apache::lonuserutils::check_usertype($ccdomain,$ccuname,$rules,
1.362     raeburn  1142:                                                       \%curr_rules,\%got_rules);
1.215     raeburn  1143:         my $cancreate =
                   1144:             &Apache::lonuserutils::can_create_user($ccdomain,$context,
                   1145:                                                    $usertype);
                   1146:         if (!$cancreate) {
1.292     bisitz   1147:             my $helplink = 'javascript:helpMenu('."'display'".')';
1.215     raeburn  1148:             my %usertypetext = (
                   1149:                 official   => 'institutional',
                   1150:                 unofficial => 'non-institutional',
                   1151:             );
                   1152:             my $response;
                   1153:             if ($env{'form.origform'} eq 'crtusername') {
1.362     raeburn  1154:                 $response = '<span class="LC_warning">'.
                   1155:                             &mt('No match found for the username [_1] in LON-CAPA domain: [_2]',
                   1156:                                 '<b>'.$ccuname.'</b>',$ccdomain).
1.215     raeburn  1157:                             '</span><br />';
                   1158:             }
1.292     bisitz   1159:             $response .= '<p class="LC_warning">'
                   1160:                         .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
1.406.2.6  raeburn  1161:                         .' ';
                   1162:             if ($context eq 'domain') {
                   1163:                 $response .= &mt('Please contact a [_1] for assistance.',
                   1164:                                  &Apache::lonnet::plaintext('dc'));
                   1165:             } else {
                   1166:                 $response .= &mt('Please contact the [_1]helpdesk[_2] for assistance.'
                   1167:                                 ,'<a href="'.$helplink.'">','</a>');
                   1168:             }
                   1169:             $response .= '</p><br />';
1.215     raeburn  1170:             $env{'form.phase'} = '';
1.351     raeburn  1171:             &print_username_entry_form($r,$context,$response,undef,undef,$crstype,$brcrum);
1.215     raeburn  1172:             return;
                   1173:         }
1.188     raeburn  1174:         $newuser = 1;
1.193     raeburn  1175:         my $checkhash;
                   1176:         my $checks = { 'username' => 1 };
1.196     raeburn  1177:         $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
1.193     raeburn  1178:         &Apache::loncommon::user_rule_check($checkhash,$checks,
1.196     raeburn  1179:             \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
                   1180:         if (ref($alerts{'username'}) eq 'HASH') {
                   1181:             if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
                   1182:                 my $domdesc =
1.193     raeburn  1183:                     &Apache::lonnet::domain($ccdomain,'description');
1.196     raeburn  1184:                 if ($alerts{'username'}{$ccdomain}{$ccuname}) {
                   1185:                     my $userchkmsg;
                   1186:                     if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
                   1187:                         $userchkmsg = 
                   1188:                             &Apache::loncommon::instrule_disallow_msg('username',
1.193     raeburn  1189:                                                                  $domdesc,1).
                   1190:                         &Apache::loncommon::user_rule_formats($ccdomain,
                   1191:                             $domdesc,$curr_rules{$ccdomain}{'username'},
                   1192:                             'username');
1.196     raeburn  1193:                     }
1.215     raeburn  1194:                     $env{'form.phase'} = '';
1.351     raeburn  1195:                     &print_username_entry_form($r,$context,$userchkmsg,undef,undef,$crstype,$brcrum);
1.196     raeburn  1196:                     return;
1.215     raeburn  1197:                 }
1.193     raeburn  1198:             }
1.185     raeburn  1199:         }
1.187     raeburn  1200:     } else {
1.188     raeburn  1201:         $newuser = 0;
1.185     raeburn  1202:     }
1.160     raeburn  1203:     if ($response) {
1.215     raeburn  1204:         $response = '<br />'.$response;
1.160     raeburn  1205:     }
1.149     raeburn  1206: 
1.52      matthew  1207:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.88      raeburn  1208:     my $dc_setcourse_code = '';
1.119     raeburn  1209:     my $nondc_setsection_code = '';                                        
1.112     albertel 1210:     my %loaditem;
1.114     albertel 1211: 
1.216     raeburn  1212:     my $groupslist = &Apache::lonuserutils::get_groupslist();
1.88      raeburn  1213: 
1.375     raeburn  1214:     my $js = &validation_javascript($context,$ccdomain,$pjump_def,$crstype,
1.216     raeburn  1215:                                $groupslist,$newuser,$formname,\%loaditem);
1.406.2.7  raeburn  1216:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$ccdomain);
1.224     raeburn  1217:     my $helpitem = 'Course_Change_Privileges';
                   1218:     if ($env{'form.action'} eq 'singlestudent') {
                   1219:         $helpitem = 'Course_Add_Student';
                   1220:     }
1.351     raeburn  1221:     push (@{$brcrum},
                   1222:         {href => "javascript:backPage($form)",
                   1223:          text => $breadcrumb_text{'search'},
                   1224:          faq  => 282,
                   1225:          bug  => 'Instructor Interface',});
                   1226:     if ($env{'form.phase'} eq 'userpicked') {
                   1227:        push(@{$brcrum},
                   1228:               {href => "javascript:backPage($form,'get_user_info','select')",
                   1229:                text => $breadcrumb_text{'userpicked'},
                   1230:                faq  => 282,
                   1231:                bug  => 'Instructor Interface',});
                   1232:     }
                   1233:     push(@{$brcrum},
                   1234:             {href => "javascript:backPage($form,'$env{'form.phase'}','modify')",
                   1235:              text => $breadcrumb_text{'modify'},
                   1236:              faq  => 282,
                   1237:              bug  => 'Instructor Interface',
                   1238:              help => $helpitem});
                   1239:     my $args = {'add_entries'           => \%loaditem,
                   1240:                 'bread_crumbs'          => $brcrum,
                   1241:                 'bread_crumbs_component' => 'User Management'};
                   1242:     if ($env{'form.popup'}) {
                   1243:         $args->{'no_nav_bar'} = 1;
                   1244:     }
                   1245:     my $start_page =
                   1246:         &Apache::loncommon::start_page('User Management',$js,$args);
1.3       www      1247: 
1.25      matthew  1248:     my $forminfo =<<"ENDFORMINFO";
1.216     raeburn  1249: <form action="/adm/createuser" method="post" name="$formname">
1.190     raeburn  1250: <input type="hidden" name="phase" value="update_user_data" />
1.188     raeburn  1251: <input type="hidden" name="ccuname" value="$ccuname" />
                   1252: <input type="hidden" name="ccdomain" value="$ccdomain" />
1.157     albertel 1253: <input type="hidden" name="pres_value"  value="" />
                   1254: <input type="hidden" name="pres_type"   value="" />
                   1255: <input type="hidden" name="pres_marker" value="" />
1.25      matthew  1256: ENDFORMINFO
1.375     raeburn  1257:     my (%inccourses,$roledom,$defaultcredits);
1.329     raeburn  1258:     if ($context eq 'course') {
                   1259:         $inccourses{$env{'request.course.id'}}=1;
                   1260:         $roledom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.375     raeburn  1261:         if ($showcredits) {
                   1262:             $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1263:         }
1.329     raeburn  1264:     } elsif ($context eq 'author') {
                   1265:         $roledom = $env{'request.role.domain'};
                   1266:     } elsif ($context eq 'domain') {
                   1267:         foreach my $key (keys(%env)) {
                   1268:             $roledom = $env{'request.role.domain'};
                   1269:             if ($key=~/^user\.priv\.cm\.\/($roledom)\/($match_username)/) {
                   1270:                 $inccourses{$1.'_'.$2}=1;
                   1271:             }
                   1272:         }
                   1273:     } else {
                   1274:         foreach my $key (keys(%env)) {
                   1275: 	    if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
                   1276: 	        $inccourses{$1.'_'.$2}=1;
                   1277:             }
1.2       www      1278:         }
1.24      matthew  1279:     }
1.389     bisitz   1280:     my $title = '';
1.216     raeburn  1281:     if ($newuser) {
1.406.2.9! raeburn  1282:         my ($portfolioform,$domroleform);
1.267     raeburn  1283:         if ((&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) ||
                   1284:             (&Apache::lonnet::allowed('mut',$env{'request.role.domain'}))) {
                   1285:             # Current user has quota or user tools modification privileges
1.378     raeburn  1286:             $portfolioform = '<br />'.&user_quotas($ccuname,$ccdomain);
1.134     raeburn  1287:         }
1.383     raeburn  1288:         if ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) &&
                   1289:             ($ccdomain eq $env{'request.role.domain'})) {
1.362     raeburn  1290:             $domroleform = '<br />'.&domainrole_req($ccuname,$ccdomain);
                   1291:         }
1.227     raeburn  1292:         &initialize_authen_forms($ccdomain,$formname);
1.188     raeburn  1293:         my %lt=&Apache::lonlocal::texthash(
                   1294:                 'lg'             => 'Login Data',
1.190     raeburn  1295:                 'hs'             => "Home Server",
1.188     raeburn  1296:         );
1.185     raeburn  1297: 	$r->print(<<ENDTITLE);
1.110     albertel 1298: $start_page
1.160     raeburn  1299: $response
1.25      matthew  1300: $forminfo
1.31      matthew  1301: <script type="text/javascript" language="Javascript">
1.301     bisitz   1302: // <![CDATA[
1.20      harris41 1303: $loginscript
1.301     bisitz   1304: // ]]>
1.31      matthew  1305: </script>
1.20      harris41 1306: <input type='hidden' name='makeuser' value='1' />
1.185     raeburn  1307: ENDTITLE
1.213     raeburn  1308:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1309:             if ($crstype eq 'Community') {
1.389     bisitz   1310:                 $title = &mt('Create New User [_1] in domain [_2] as a member',
                   1311:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1312:             } else {
1.389     bisitz   1313:                 $title = &mt('Create New User [_1] in domain [_2] as a student',
                   1314:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1315:             }
1.389     bisitz   1316:         } else {
                   1317:                 $title = &mt('Create New User [_1] in domain [_2]',
                   1318:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.213     raeburn  1319:         }
1.389     bisitz   1320:         $r->print('<h2>'.$title.'</h2>'."\n");
                   1321:         $r->print('<div class="LC_left_float">');
1.393     raeburn  1322:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1323:                                          $inst_results{$ccuname.':'.$ccdomain}));
                   1324:         # Option to disable student/employee ID conflict checking not offerred for new users.
1.187     raeburn  1325:         my ($home_server_pick,$numlib) = 
                   1326:             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
                   1327:                                                       'default','hide');
                   1328:         if ($numlib > 1) {
                   1329:             $r->print("
1.185     raeburn  1330: <br />
1.187     raeburn  1331: $lt{'hs'}: $home_server_pick
                   1332: <br />");
                   1333:         } else {
                   1334:             $r->print($home_server_pick);
                   1335:         }
1.304     raeburn  1336:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.362     raeburn  1337:             $r->print('<br /><h3>'.
                   1338:                       &mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.304     raeburn  1339:                       &Apache::loncommon::start_data_table().
                   1340:                       &build_tools_display($ccuname,$ccdomain,
                   1341:                                            'requestcourses').
                   1342:                       &Apache::loncommon::end_data_table());
                   1343:         }
1.188     raeburn  1344:         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                   1345:                   $lt{'lg'}.'</h3>');
1.185     raeburn  1346:         my ($fixedauth,$varauth,$authmsg); 
1.193     raeburn  1347:         if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
                   1348:             my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
                   1349:             my ($rules,$ruleorder) = 
                   1350:                 &Apache::lonnet::inst_userrules($ccdomain,'username');
1.185     raeburn  1351:             if (ref($rules) eq 'HASH') {
1.193     raeburn  1352:                 if (ref($rules->{$matchedrule}) eq 'HASH') {
                   1353:                     my $authtype = $rules->{$matchedrule}{'authtype'};
1.185     raeburn  1354:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
1.190     raeburn  1355:                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.275     raeburn  1356:                     } else { 
1.193     raeburn  1357:                         my $authparm = $rules->{$matchedrule}{'authparm'};
1.273     raeburn  1358:                         $authmsg = $rules->{$matchedrule}{'authmsg'};
1.185     raeburn  1359:                         if ($authtype =~ /^krb(4|5)$/) {
                   1360:                             my $ver = $1;
                   1361:                             if ($authparm ne '') {
                   1362:                                 $fixedauth = <<"KERB"; 
                   1363: <input type="hidden" name="login" value="krb" />
                   1364: <input type="hidden" name="krbver" value="$ver" />
                   1365: <input type="hidden" name="krbarg" value="$authparm" />
                   1366: KERB
                   1367:                             }
                   1368:                         } else {
                   1369:                             $fixedauth = 
                   1370: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
1.193     raeburn  1371:                             if ($rules->{$matchedrule}{'authparmfixed'}) {
1.185     raeburn  1372:                                 $fixedauth .=    
                   1373: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
                   1374:                             } else {
1.273     raeburn  1375:                                 if ($authtype eq 'int') {
                   1376:                                     $varauth = '<br />'.
1.301     bisitz   1377: &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  1378:                                 } elsif ($authtype eq 'loc') {
                   1379:                                     $varauth = '<br />'.
                   1380: &mt('[_1] Local Authentication with argument [_2]','','<input type="text" name="'.$authtype.'arg" value="" />')."\n";
                   1381:                                 } else {
                   1382:                                     $varauth =
1.185     raeburn  1383: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
1.273     raeburn  1384:                                 }
1.185     raeburn  1385:                             }
                   1386:                         }
                   1387:                     }
                   1388:                 } else {
1.190     raeburn  1389:                     $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.185     raeburn  1390:                 }
                   1391:             }
                   1392:             if ($authmsg) {
                   1393:                 $r->print(<<ENDAUTH);
                   1394: $fixedauth
                   1395: $authmsg
                   1396: $varauth
                   1397: ENDAUTH
                   1398:             }
                   1399:         } else {
1.190     raeburn  1400:             $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc)); 
1.187     raeburn  1401:         }
1.406.2.9! raeburn  1402:         $r->print($portfolioform.$domroleform);
1.215     raeburn  1403:         if ($env{'form.action'} eq 'singlestudent') {
                   1404:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1405:                                             $permission,$crstype,$ccuname,
                   1406:                                             $ccdomain,$showcredits));
1.215     raeburn  1407:         }
                   1408:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.216     raeburn  1409:     } else { # user already exists
1.389     bisitz   1410: 	$r->print($start_page.$forminfo);
1.213     raeburn  1411:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1412:             if ($crstype eq 'Community') {
1.389     bisitz   1413:                 $title = &mt('Enroll one member: [_1] in domain [_2]',
                   1414:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1415:             } else {
1.389     bisitz   1416:                 $title = &mt('Enroll one student: [_1] in domain [_2]',
                   1417:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1418:             }
1.213     raeburn  1419:         } else {
1.406.2.6  raeburn  1420:             if ($permission->{'cusr'}) {
                   1421:                 $title = &mt('Modify existing user: [_1] in domain [_2]',
                   1422:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
                   1423:             } else {
                   1424:                 $title = &mt('Existing user: [_1] in domain [_2]',
1.389     bisitz   1425:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
1.406.2.6  raeburn  1426:             }
1.213     raeburn  1427:         }
1.389     bisitz   1428:         $r->print('<h2>'.$title.'</h2>'."\n");
                   1429:         $r->print('<div class="LC_left_float">');
1.393     raeburn  1430:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1431:                                          $inst_results{$ccuname.':'.$ccdomain}));
1.406.2.6  raeburn  1432:         if ((&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) ||
                   1433:             (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) {
1.362     raeburn  1434:             $r->print('<br /><h3>'.&mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.300     raeburn  1435:                       &Apache::loncommon::start_data_table());
1.314     raeburn  1436:             if ($env{'request.role.domain'} eq $ccdomain) {
1.300     raeburn  1437:                 $r->print(&build_tools_display($ccuname,$ccdomain,'requestcourses'));
                   1438:             } else {
                   1439:                 $r->print(&coursereq_externaluser($ccuname,$ccdomain,
                   1440:                                                   $env{'request.role.domain'}));
                   1441:             }
                   1442:             $r->print(&Apache::loncommon::end_data_table());
1.275     raeburn  1443:         }
1.199     raeburn  1444:         $r->print('</div>');
1.406.2.9! raeburn  1445:         my @order = ('auth','quota','tools','requestauthor');
1.362     raeburn  1446:         my %user_text;
                   1447:         my ($isadv,$isauthor) = 
1.406.2.6  raeburn  1448:             &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
1.362     raeburn  1449:         if ((!$isauthor) && 
1.406.2.6  raeburn  1450:             ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) ||
                   1451:              (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) &&
                   1452:              ($env{'request.role.domain'} eq $ccdomain)) {
1.362     raeburn  1453:             $user_text{'requestauthor'} = &domainrole_req($ccuname,$ccdomain);
                   1454:         }
                   1455:         $user_text{'auth'} =  &user_authentication($ccuname,$ccdomain,$formname);
1.267     raeburn  1456:         if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
1.406.2.6  raeburn  1457:             (&Apache::lonnet::allowed('mut',$ccdomain)) ||
                   1458:             (&Apache::lonnet::allowed('udp',$ccdomain))) {
1.188     raeburn  1459:             # Current user has quota modification privileges
1.378     raeburn  1460:             $user_text{'quota'} = &user_quotas($ccuname,$ccdomain);
1.267     raeburn  1461:         }
                   1462:         if (!&Apache::lonnet::allowed('mpq',$ccdomain)) {
                   1463:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
                   1464:                 my %lt=&Apache::lonlocal::texthash(
1.385     bisitz   1465:                     'dska'  => "Disk quotas for user's portfolio and Authoring Space",
                   1466:                     'youd'  => "You do not have privileges to modify the portfolio and/or Authoring Space quotas for this user.",
1.267     raeburn  1467:                     'ichr'  => "If a change is required, contact a domain coordinator for the domain",
                   1468:                 );
1.362     raeburn  1469:                 $user_text{'quota'} = <<ENDNOPORTPRIV;
1.188     raeburn  1470: <h3>$lt{'dska'}</h3>
                   1471: $lt{'youd'} $lt{'ichr'}: $ccdomain
                   1472: ENDNOPORTPRIV
1.267     raeburn  1473:             }
                   1474:         }
                   1475:         if (!&Apache::lonnet::allowed('mut',$ccdomain)) {
                   1476:             if (&Apache::lonnet::allowed('mut',$env{'request.role.domain'})) {
                   1477:                 my %lt=&Apache::lonlocal::texthash(
                   1478:                     'utav'  => "User Tools Availability",
1.361     raeburn  1479:                     'yodo'  => "You do not have privileges to modify Portfolio, Blog, WebDAV, or Personal Information Page settings for this user.",
1.267     raeburn  1480:                     'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1481:                 );
1.362     raeburn  1482:                 $user_text{'tools'} = <<ENDNOTOOLSPRIV;
1.267     raeburn  1483: <h3>$lt{'utav'}</h3>
                   1484: $lt{'yodo'} $lt{'ifch'}: $ccdomain
                   1485: ENDNOTOOLSPRIV
                   1486:             }
1.188     raeburn  1487:         }
1.362     raeburn  1488:         my $gotdiv = 0; 
                   1489:         foreach my $item (@order) {
                   1490:             if ($user_text{$item} ne '') {
                   1491:                 unless ($gotdiv) {
                   1492:                     $r->print('<div class="LC_left_float">');
                   1493:                     $gotdiv = 1;
                   1494:                 }
                   1495:                 $r->print('<br />'.$user_text{$item});
                   1496:             }
                   1497:         }
                   1498:         if ($env{'form.action'} eq 'singlestudent') {
                   1499:             unless ($gotdiv) {
                   1500:                 $r->print('<div class="LC_left_float">');
1.213     raeburn  1501:             }
1.375     raeburn  1502:             my $credits;
                   1503:             if ($showcredits) {
                   1504:                 $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1505:                 if ($credits eq '') {
                   1506:                     $credits = $defaultcredits;
                   1507:                 }
                   1508:             }
1.374     raeburn  1509:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1510:                                             $permission,$crstype,$ccuname,
                   1511:                                             $ccdomain,$showcredits));
1.374     raeburn  1512:         }
1.362     raeburn  1513:         if ($gotdiv) {
                   1514:             $r->print('</div><div class="LC_clear_float_footer"></div>');
1.188     raeburn  1515:         }
1.406.2.6  raeburn  1516:         my $statuses;
                   1517:         if (($context eq 'domain') && (&Apache::lonnet::allowed('udp',$ccdomain)) &&
                   1518:             (!&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1519:             $statuses = ['active'];
                   1520:         } elsif (($context eq 'course') && ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
                   1521:                  ($env{'request.course.sec'} &&
                   1522:                   &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'})))) {
                   1523:             $statuses = ['active'];
                   1524:         }
1.217     raeburn  1525:         if ($env{'form.action'} ne 'singlestudent') {
1.329     raeburn  1526:             &display_existing_roles($r,$ccuname,$ccdomain,\%inccourses,$context,
1.406.2.6  raeburn  1527:                                     $roledom,$crstype,$showcredits,$statuses);
1.217     raeburn  1528:         }
1.25      matthew  1529:     } ## End of new user/old user logic
1.218     raeburn  1530:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1531:         my $btntxt;
                   1532:         if ($crstype eq 'Community') {
                   1533:             $btntxt = &mt('Enroll Member');
                   1534:         } else {
                   1535:             $btntxt = &mt('Enroll Student');
                   1536:         }
                   1537:         $r->print('<br /><input type="button" value="'.$btntxt.'" onclick="setSections(this.form)" />'."\n");
1.406.2.6  raeburn  1538:     } elsif ($permission->{'cusr'}) {
1.393     raeburn  1539:         $r->print('<div class="LC_left_float">'.
                   1540:                   '<fieldset><legend>'.&mt('Add Roles').'</legend>');
1.218     raeburn  1541:         my $addrolesdisplay = 0;
                   1542:         if ($context eq 'domain' || $context eq 'author') {
                   1543:             $addrolesdisplay = &new_coauthor_roles($r,$ccuname,$ccdomain);
                   1544:         }
                   1545:         if ($context eq 'domain') {
1.357     raeburn  1546:             my $add_domainroles = &new_domain_roles($r,$ccdomain);
1.218     raeburn  1547:             if (!$addrolesdisplay) {
                   1548:                 $addrolesdisplay = $add_domainroles;
1.2       www      1549:             }
1.375     raeburn  1550:             $r->print(&course_level_dc($env{'request.role.domain'},$showcredits));
1.393     raeburn  1551:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1552:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setCourse()" />'."\n");
1.218     raeburn  1553:         } elsif ($context eq 'author') {
                   1554:             if ($addrolesdisplay) {
1.393     raeburn  1555:                 $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1556:                           '<br /><input type="button" value="'.&mt('Save').'"');
1.218     raeburn  1557:                 if ($newuser) {
1.301     bisitz   1558:                     $r->print(' onclick="auth_check()" \>'."\n");
1.218     raeburn  1559:                 } else {
1.301     bisitz   1560:                     $r->print('onclick="this.form.submit()" \>'."\n");
1.218     raeburn  1561:                 }
1.188     raeburn  1562:             } else {
1.393     raeburn  1563:                 $r->print('</fieldset></div>'.
                   1564:                           '<div class="LC_clear_float_footer"></div>'.
                   1565:                           '<br /><a href="javascript:backPage(document.cu)">'.
1.218     raeburn  1566:                           &mt('Back to previous page').'</a>');
1.188     raeburn  1567:             }
                   1568:         } else {
1.375     raeburn  1569:             $r->print(&course_level_table(\%inccourses,$showcredits,$defaultcredits));
1.393     raeburn  1570:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1571:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setSections(this.form)" />'."\n");
1.188     raeburn  1572:         }
1.88      raeburn  1573:     }
1.188     raeburn  1574:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
1.179     raeburn  1575:     $r->print('<input type="hidden" name="currstate" value="" />');
1.393     raeburn  1576:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" /></form><br /><br />');
1.218     raeburn  1577:     return;
1.2       www      1578: }
1.1       www      1579: 
1.213     raeburn  1580: sub singleuser_breadcrumb {
1.406.2.7  raeburn  1581:     my ($crstype,$context,$domain) = @_;
1.213     raeburn  1582:     my %breadcrumb_text;
                   1583:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1584:         if ($crstype eq 'Community') {
                   1585:             $breadcrumb_text{'search'} = 'Enroll a member';
                   1586:         } else {
                   1587:             $breadcrumb_text{'search'} = 'Enroll a student';
                   1588:         }
1.406.2.7  raeburn  1589:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1590:         $breadcrumb_text{'modify'} = 'Set section/dates';
1.406.2.5  raeburn  1591:     } elsif ($env{'form.action'} eq 'accesslogs') {
                   1592:         $breadcrumb_text{'search'} = 'View access logs for a user';
1.406.2.7  raeburn  1593:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1594:         $breadcrumb_text{'activity'} = 'Activity';
                   1595:     } elsif (($env{'form.action'} eq 'singleuser') && ($context eq 'domain') &&
                   1596:              (!&Apache::lonnet::allowed('mau',$domain))) {
                   1597:         $breadcrumb_text{'search'} = "View user's roles";
                   1598:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1599:         $breadcrumb_text{'modify'} = 'User roles';
1.213     raeburn  1600:     } else {
1.229     raeburn  1601:         $breadcrumb_text{'search'} = 'Create/modify a user';
1.406.2.7  raeburn  1602:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1603:         $breadcrumb_text{'modify'} = 'Set user role';
1.213     raeburn  1604:     }
                   1605:     return %breadcrumb_text;
                   1606: }
                   1607: 
                   1608: sub date_sections_select {
1.375     raeburn  1609:     my ($context,$newuser,$formname,$permission,$crstype,$ccuname,$ccdomain,
                   1610:         $showcredits) = @_;
                   1611:     my $credits;
                   1612:     if ($showcredits) {
                   1613:         my $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1614:         $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1615:         if ($credits eq '') {
                   1616:             $credits = $defaultcredits;
                   1617:         }
                   1618:     }
1.213     raeburn  1619:     my $cid = $env{'request.course.id'};
                   1620:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity($cid);
                   1621:     my $date_table = '<h3>'.&mt('Starting and Ending Dates').'</h3>'."\n".
                   1622:         &Apache::lonuserutils::date_setting_table(undef,undef,$context,
                   1623:                                                   undef,$formname,$permission);
                   1624:     my $rowtitle = 'Section';
1.375     raeburn  1625:     my $secbox = '<h3>'.&mt('Section and Credits').'</h3>'."\n".
1.213     raeburn  1626:         &Apache::lonuserutils::section_picker($cdom,$cnum,'st',$rowtitle,
1.375     raeburn  1627:                                               $permission,$context,'',$crstype,
                   1628:                                               $showcredits,$credits);
1.213     raeburn  1629:     my $output = $date_table.$secbox;
                   1630:     return $output;
                   1631: }
                   1632: 
1.216     raeburn  1633: sub validation_javascript {
1.375     raeburn  1634:     my ($context,$ccdomain,$pjump_def,$crstype,$groupslist,$newuser,$formname,
1.216     raeburn  1635:         $loaditem) = @_;
                   1636:     my $dc_setcourse_code = '';
                   1637:     my $nondc_setsection_code = '';
                   1638:     if ($context eq 'domain') {
                   1639:         my $dcdom = $env{'request.role.domain'};
                   1640:         $loaditem->{'onload'} = "document.cu.coursedesc.value='';";
1.227     raeburn  1641:         $dc_setcourse_code = 
                   1642:             &Apache::lonuserutils::dc_setcourse_js('cu','singleuser',$context);
1.216     raeburn  1643:     } else {
1.227     raeburn  1644:         my $checkauth; 
                   1645:         if (($newuser) || (&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1646:             $checkauth = 1;
                   1647:         }
                   1648:         if ($context eq 'course') {
                   1649:             $nondc_setsection_code =
                   1650:                 &Apache::lonuserutils::setsections_javascript($formname,$groupslist,
1.375     raeburn  1651:                                                               undef,$checkauth,
                   1652:                                                               $crstype);
1.227     raeburn  1653:         }
                   1654:         if ($checkauth) {
                   1655:             $nondc_setsection_code .= 
                   1656:                 &Apache::lonuserutils::verify_authen($formname,$context);
                   1657:         }
1.216     raeburn  1658:     }
                   1659:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                   1660:                                    $nondc_setsection_code,$groupslist);
                   1661:     my ($jsback,$elements) = &crumb_utilities();
                   1662:     $js .= "\n".
1.301     bisitz   1663:            '<script type="text/javascript">'."\n".
                   1664:            '// <![CDATA['."\n".
                   1665:            $jsback."\n".
                   1666:            '// ]]>'."\n".
                   1667:            '</script>'."\n";
1.216     raeburn  1668:     return $js;
                   1669: }
                   1670: 
1.217     raeburn  1671: sub display_existing_roles {
1.375     raeburn  1672:     my ($r,$ccuname,$ccdomain,$inccourses,$context,$roledom,$crstype,
1.406.2.6  raeburn  1673:         $showcredits,$statuses) = @_;
1.329     raeburn  1674:     my $now=time;
1.406.2.6  raeburn  1675:     my $showall = 1;
                   1676:     my ($showexpired,$showactive);
                   1677:     if ((ref($statuses) eq 'ARRAY') && (@{$statuses} > 0)) {
                   1678:         $showall = 0;
                   1679:         if (grep(/^expired$/,@{$statuses})) {
                   1680:             $showexpired = 1;
                   1681:         }
                   1682:         if (grep(/^active$/,@{$statuses})) {
                   1683:             $showactive = 1;
                   1684:         }
                   1685:         if ($showexpired && $showactive) {
                   1686:             $showall = 1;
                   1687:         }
                   1688:     }
1.329     raeburn  1689:     my %lt=&Apache::lonlocal::texthash(
1.217     raeburn  1690:                     'rer'  => "Existing Roles",
                   1691:                     'rev'  => "Revoke",
                   1692:                     'del'  => "Delete",
                   1693:                     'ren'  => "Re-Enable",
                   1694:                     'rol'  => "Role",
                   1695:                     'ext'  => "Extent",
1.375     raeburn  1696:                     'crd'  => "Credits",
1.217     raeburn  1697:                     'sta'  => "Start",
                   1698:                     'end'  => "End",
                   1699:                                        );
1.329     raeburn  1700:     my (%rolesdump,%roletext,%sortrole,%roleclass,%rolepriv);
                   1701:     if ($context eq 'course' || $context eq 'author') {
                   1702:         my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
                   1703:         my %roleshash = 
                   1704:             &Apache::lonnet::get_my_roles($ccuname,$ccdomain,'userroles',
                   1705:                               ['active','previous','future'],\@roles,$roledom,1);
                   1706:         foreach my $key (keys(%roleshash)) {
                   1707:             my ($start,$end) = split(':',$roleshash{$key});
                   1708:             next if ($start eq '-1' || $end eq '-1');
                   1709:             my ($rnum,$rdom,$role,$sec) = split(':',$key);
                   1710:             if ($context eq 'course') {
                   1711:                 next unless (($rnum eq $env{'course.'.$env{'request.course.id'}.'.num'})
                   1712:                              && ($rdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}));
                   1713:             } elsif ($context eq 'author') {
                   1714:                 next unless (($rnum eq $env{'user.name'}) && ($rdom eq $env{'request.role.domain'}));
                   1715:             }
                   1716:             my ($newkey,$newvalue,$newrole);
                   1717:             $newkey = '/'.$rdom.'/'.$rnum;
                   1718:             if ($sec ne '') {
                   1719:                 $newkey .= '/'.$sec;
                   1720:             }
                   1721:             $newvalue = $role;
                   1722:             if ($role =~ /^cr/) {
                   1723:                 $newrole = 'cr';
                   1724:             } else {
                   1725:                 $newrole = $role;
                   1726:             }
                   1727:             $newkey .= '_'.$newrole;
                   1728:             if ($start ne '' && $end ne '') {
                   1729:                 $newvalue .= '_'.$end.'_'.$start;
1.335     raeburn  1730:             } elsif ($end ne '') {
                   1731:                 $newvalue .= '_'.$end;
1.329     raeburn  1732:             }
                   1733:             $rolesdump{$newkey} = $newvalue;
                   1734:         }
                   1735:     } else {
1.360     raeburn  1736:         %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
1.329     raeburn  1737:     }
                   1738:     # Build up table of user roles to allow revocation and re-enabling of roles.
                   1739:     my ($tmp) = keys(%rolesdump);
                   1740:     return if ($tmp =~ /^(con_lost|error)/i);
                   1741:     foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
                   1742:                                 my $b1=join('_',(split('_',$b))[1,0]);
                   1743:                                 return $a1 cmp $b1;
                   1744:                             } keys(%rolesdump)) {
                   1745:         next if ($area =~ /^rolesdef/);
                   1746:         my $envkey=$area;
                   1747:         my $role = $rolesdump{$area};
                   1748:         my $thisrole=$area;
                   1749:         $area =~ s/\_\w\w$//;
                   1750:         my ($role_code,$role_end_time,$role_start_time) =
                   1751:             split(/_/,$role);
1.406.2.6  raeburn  1752:         my $active=1;
                   1753:         $active=0 if (($role_end_time) && ($now>$role_end_time));
                   1754:         if ($active) {
                   1755:             next unless($showall || $showactive);
                   1756:         } else {
                   1757:             next unless($showall || $showexpired);
                   1758:         }
1.217     raeburn  1759: # Is this a custom role? Get role owner and title.
1.329     raeburn  1760:         my ($croleudom,$croleuname,$croletitle)=
                   1761:             ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
                   1762:         my $allowed=0;
                   1763:         my $delallowed=0;
                   1764:         my $sortkey=$role_code;
                   1765:         my $class='Unknown';
1.375     raeburn  1766:         my $credits='';
1.406.2.6  raeburn  1767:         my $csec;
1.406.2.7  raeburn  1768:         if ($area =~ m{^/($match_domain)/($match_courseid)}) {
1.329     raeburn  1769:             $class='Course';
                   1770:             my ($coursedom,$coursedir) = ($1,$2);
                   1771:             my $cid = $1.'_'.$2;
                   1772:             # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
1.406.2.7  raeburn  1773:             next if ($envkey =~ m{^/$match_domain/$match_courseid/[A-Za-z0-9]+_gr$});
1.329     raeburn  1774:             my %coursedata=
                   1775:                 &Apache::lonnet::coursedescription($cid);
                   1776:             if ($coursedir =~ /^$match_community$/) {
                   1777:                 $class='Community';
                   1778:             }
                   1779:             $sortkey.="\0$coursedom";
                   1780:             my $carea;
                   1781:             if (defined($coursedata{'description'})) {
                   1782:                 $carea=$coursedata{'description'}.
                   1783:                     '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
                   1784:     &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$coursedir,$coursedom);
                   1785:                 $sortkey.="\0".$coursedata{'description'};
                   1786:             } else {
                   1787:                 if ($class eq 'Community') {
                   1788:                     $carea=&mt('Unavailable community').': '.$area;
                   1789:                     $sortkey.="\0".&mt('Unavailable community').': '.$area;
1.217     raeburn  1790:                 } else {
                   1791:                     $carea=&mt('Unavailable course').': '.$area;
                   1792:                     $sortkey.="\0".&mt('Unavailable course').': '.$area;
                   1793:                 }
1.329     raeburn  1794:             }
                   1795:             $sortkey.="\0$coursedir";
                   1796:             $inccourses->{$cid}=1;
1.375     raeburn  1797:             if (($showcredits) && ($class eq 'Course') && ($role_code eq 'st')) {
                   1798:                 my $defaultcredits = $coursedata{'internal.defaultcredits'};
                   1799:                 $credits =
                   1800:                     &get_user_credits($ccuname,$ccdomain,$defaultcredits,
                   1801:                                       $coursedom,$coursedir);
                   1802:                 if ($credits eq '') {
                   1803:                     $credits = $defaultcredits;
                   1804:                 }
                   1805:             }
1.329     raeburn  1806:             if ((&Apache::lonnet::allowed('c'.$role_code,$coursedom.'/'.$coursedir)) ||
                   1807:                 (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1808:                 $allowed=1;
                   1809:             }
                   1810:             unless ($allowed) {
1.365     raeburn  1811:                 my $isowner = &Apache::lonuserutils::is_courseowner($cid,$coursedata{'internal.courseowner'});
1.329     raeburn  1812:                 if ($isowner) {
                   1813:                     if (($role_code eq 'co') && ($class eq 'Community')) {
                   1814:                         $allowed = 1;
                   1815:                     } elsif (($role_code eq 'cc') && ($class eq 'Course')) {
                   1816:                         $allowed = 1;
                   1817:                     }
1.217     raeburn  1818:                 }
1.329     raeburn  1819:             } 
                   1820:             if ((&Apache::lonnet::allowed('dro',$coursedom)) ||
                   1821:                 (&Apache::lonnet::allowed('dro',$ccdomain))) {
                   1822:                 $delallowed=1;
                   1823:             }
1.217     raeburn  1824: # - custom role. Needs more info, too
1.329     raeburn  1825:             if ($croletitle) {
                   1826:                 if (&Apache::lonnet::allowed('ccr',$coursedom.'/'.$coursedir)) {
                   1827:                     $allowed=1;
                   1828:                     $thisrole.='.'.$role_code;
1.217     raeburn  1829:                 }
1.329     raeburn  1830:             }
1.406.2.6  raeburn  1831:             if ($area=~m{^/($match_domain/$match_courseid/(\w+))}) {
                   1832:                 $csec = $2;
                   1833:                 $carea.='<br />'.&mt('Section: [_1]',$csec);
                   1834:                 $sortkey.="\0$csec";
1.329     raeburn  1835:                 if (!$allowed) {
1.406.2.6  raeburn  1836:                     if ($env{'request.course.sec'} eq $csec) {
                   1837:                         if (&Apache::lonnet::allowed('c'.$role_code,$1)) {
1.329     raeburn  1838:                             $allowed = 1;
1.217     raeburn  1839:                         }
                   1840:                     }
                   1841:                 }
1.329     raeburn  1842:             }
                   1843:             $area=$carea;
                   1844:         } else {
                   1845:             $sortkey.="\0".$area;
                   1846:             # Determine if current user is able to revoke privileges
                   1847:             if ($area=~m{^/($match_domain)/}) {
                   1848:                 if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
                   1849:                    (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1850:                    $allowed=1;
1.217     raeburn  1851:                 }
1.329     raeburn  1852:                 if (((&Apache::lonnet::allowed('dro',$1))  ||
                   1853:                     (&Apache::lonnet::allowed('dro',$ccdomain))) &&
                   1854:                     ($role_code ne 'dc')) {
                   1855:                     $delallowed=1;
1.217     raeburn  1856:                 }
1.329     raeburn  1857:             } else {
                   1858:                 if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
1.217     raeburn  1859:                     $allowed=1;
                   1860:                 }
                   1861:             }
1.363     raeburn  1862:             if ($role_code eq 'ca' || $role_code eq 'au' || $role_code eq 'aa') {
1.377     raeburn  1863:                 $class='Authoring Space';
1.329     raeburn  1864:             } elsif ($role_code eq 'su') {
                   1865:                 $class='System';
1.217     raeburn  1866:             } else {
1.329     raeburn  1867:                 $class='Domain';
1.217     raeburn  1868:             }
1.329     raeburn  1869:         }
                   1870:         if (($role_code eq 'ca') || ($role_code eq 'aa')) {
                   1871:             $area=~m{/($match_domain)/($match_username)};
                   1872:             if (&Apache::lonuserutils::authorpriv($2,$1)) {
                   1873:                 $allowed=1;
1.217     raeburn  1874:             } else {
1.329     raeburn  1875:                 $allowed=0;
1.217     raeburn  1876:             }
1.329     raeburn  1877:         }
                   1878:         my $row = '';
1.406.2.6  raeburn  1879:         if ($showall) {
                   1880:             $row.= '<td>';
                   1881:             if (($active) && ($allowed)) {
                   1882:                 $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
1.217     raeburn  1883:             } else {
1.406.2.6  raeburn  1884:                 if ($active) {
                   1885:                     $row.='&nbsp;';
                   1886:                 } else {
                   1887:                     $row.=&mt('expired or revoked');
                   1888:                 }
1.217     raeburn  1889:             }
1.406.2.6  raeburn  1890:             $row.='</td><td>';
                   1891:             if ($allowed && !$active) {
                   1892:                 $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
                   1893:             } else {
                   1894:                 $row.='&nbsp;';
                   1895:             }
                   1896:             $row.='</td><td>';
                   1897:             if ($delallowed) {
                   1898:                 $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
                   1899:             } else {
                   1900:                 $row.='&nbsp;';
                   1901:             }
                   1902:             $row.= '</td>';
1.329     raeburn  1903:         }
                   1904:         my $plaintext='';
                   1905:         if (!$croletitle) {
1.375     raeburn  1906:             $plaintext=&Apache::lonnet::plaintext($role_code,$class);
                   1907:             if (($showcredits) && ($credits ne '')) {
                   1908:                 $plaintext .= '<br/ ><span class="LC_nobreak">'.
                   1909:                               '<span class="LC_fontsize_small">'.
                   1910:                               &mt('Credits: [_1]',$credits).
                   1911:                               '</span></span>';
                   1912:             }
1.329     raeburn  1913:         } else {
                   1914:             $plaintext=
1.395     bisitz   1915:                 &mt('Custom role [_1][_2]defined by [_3]',
1.346     bisitz   1916:                         '"'.$croletitle.'"',
                   1917:                         '<br />',
                   1918:                         $croleuname.':'.$croleudom);
1.329     raeburn  1919:         }
1.406.2.6  raeburn  1920:         $row.= '<td>'.$plaintext.'</td>'.
                   1921:                '<td>'.$area.'</td>'.
                   1922:                '<td>'.($role_start_time?&Apache::lonlocal::locallocaltime($role_start_time)
                   1923:                                             : '&nbsp;' ).'</td>'.
                   1924:                '<td>'.($role_end_time  ?&Apache::lonlocal::locallocaltime($role_end_time)
                   1925:                                             : '&nbsp;' ).'</td>';
1.329     raeburn  1926:         $sortrole{$sortkey}=$envkey;
                   1927:         $roletext{$envkey}=$row;
                   1928:         $roleclass{$envkey}=$class;
1.406.2.6  raeburn  1929:         if ($allowed) {
                   1930:             $rolepriv{$envkey}='edit';
                   1931:         } else {
                   1932:             if ($context eq 'domain') {
1.406.2.7  raeburn  1933:                 if ((&Apache::lonnet::allowed('vur',$ccdomain)) &&
                   1934:                     ($envkey=~m{^/$ccdomain/})) {
1.406.2.6  raeburn  1935:                     $rolepriv{$envkey}='view';
                   1936:                 }
                   1937:             } elsif ($context eq 'course') {
                   1938:                 if ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
                   1939:                     ($env{'request.course.sec'} && ($env{'request.course.sec'} eq $csec) &&
                   1940:                      &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'}))) {
                   1941:                     $rolepriv{$envkey}='view';
                   1942:                 }
                   1943:             }
                   1944:         }
1.329     raeburn  1945:     } # end of foreach        (table building loop)
                   1946: 
                   1947:     my $rolesdisplay = 0;
                   1948:     my %output = ();
1.377     raeburn  1949:     foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  1950:         $output{$type} = '';
                   1951:         foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                   1952:             if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) {
                   1953:                  $output{$type}.=
                   1954:                       &Apache::loncommon::start_data_table_row().
                   1955:                       $roletext{$sortrole{$which}}.
                   1956:                       &Apache::loncommon::end_data_table_row();
1.217     raeburn  1957:             }
1.329     raeburn  1958:         }
                   1959:         unless($output{$type} eq '') {
                   1960:             $output{$type} = '<tr class="LC_info_row">'.
                   1961:                       "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
                   1962:                       $output{$type};
                   1963:             $rolesdisplay = 1;
                   1964:         }
                   1965:     }
                   1966:     if ($rolesdisplay == 1) {
                   1967:         my $contextrole='';
                   1968:         if ($env{'request.course.id'}) {
                   1969:             if (&Apache::loncommon::course_type() eq 'Community') {
                   1970:                 $contextrole = &mt('Existing Roles in this Community');
1.290     bisitz   1971:             } else {
1.329     raeburn  1972:                 $contextrole = &mt('Existing Roles in this Course');
1.290     bisitz   1973:             }
1.329     raeburn  1974:         } elsif ($env{'request.role'} =~ /^au\./) {
1.377     raeburn  1975:             $contextrole = &mt('Existing Co-Author Roles in your Authoring Space');
1.329     raeburn  1976:         } else {
1.406.2.6  raeburn  1977:             if ($showall) {
                   1978:                 $contextrole = &mt('Existing Roles in this Domain');
                   1979:             } elsif ($showactive) {
                   1980:                 $contextrole = &mt('Unexpired Roles in this Domain');
                   1981:             } elsif ($showexpired) {
                   1982:                 $contextrole = &mt('Expired or Revoked Roles in this Domain');
                   1983:             }
1.329     raeburn  1984:         }
1.393     raeburn  1985:         $r->print('<div class="LC_left_float">'.
1.375     raeburn  1986: '<fieldset><legend>'.$contextrole.'</legend>'.
1.217     raeburn  1987: &Apache::loncommon::start_data_table("LC_createuser").
1.406.2.6  raeburn  1988: &Apache::loncommon::start_data_table_header_row());
                   1989:         if ($showall) {
                   1990:             $r->print(
                   1991: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}.'</th>'
                   1992:             );
                   1993:         } elsif ($showexpired) {
                   1994:             $r->print('<th>'.$lt{'rev'}.'</th>');
                   1995:         }
                   1996:         $r->print(
                   1997: '<th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.'</th>'.
                   1998: '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
1.217     raeburn  1999: &Apache::loncommon::end_data_table_header_row());
1.377     raeburn  2000:         foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  2001:             if ($output{$type}) {
                   2002:                 $r->print($output{$type}."\n");
1.217     raeburn  2003:             }
                   2004:         }
1.375     raeburn  2005:         $r->print(&Apache::loncommon::end_data_table().
                   2006:                   '</fieldset></div>');
1.329     raeburn  2007:     }
1.217     raeburn  2008:     return;
                   2009: }
                   2010: 
1.218     raeburn  2011: sub new_coauthor_roles {
                   2012:     my ($r,$ccuname,$ccdomain) = @_;
                   2013:     my $addrolesdisplay = 0;
                   2014:     #
                   2015:     # Co-Author
                   2016:     #
                   2017:     if (&Apache::lonuserutils::authorpriv($env{'user.name'},
                   2018:                                           $env{'request.role.domain'}) &&
                   2019:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
                   2020:         # No sense in assigning co-author role to yourself
                   2021:         $addrolesdisplay = 1;
                   2022:         my $cuname=$env{'user.name'};
                   2023:         my $cudom=$env{'request.role.domain'};
                   2024:         my %lt=&Apache::lonlocal::texthash(
1.377     raeburn  2025:                     'cs'   => "Authoring Space",
1.218     raeburn  2026:                     'act'  => "Activate",
                   2027:                     'rol'  => "Role",
                   2028:                     'ext'  => "Extent",
                   2029:                     'sta'  => "Start",
                   2030:                     'end'  => "End",
                   2031:                     'cau'  => "Co-Author",
                   2032:                     'caa'  => "Assistant Co-Author",
                   2033:                     'ssd'  => "Set Start Date",
                   2034:                     'sed'  => "Set End Date"
                   2035:                                        );
                   2036:         $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n".
                   2037:                   &Apache::loncommon::start_data_table()."\n".
                   2038:                   &Apache::loncommon::start_data_table_header_row()."\n".
                   2039:                   '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
                   2040:                   '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
                   2041:                   '<th>'.$lt{'end'}.'</th>'."\n".
                   2042:                   &Apache::loncommon::end_data_table_header_row()."\n".
                   2043:                   &Apache::loncommon::start_data_table_row().'
                   2044:            <td>
1.291     bisitz   2045:             <input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_ca" />
1.218     raeburn  2046:            </td>
                   2047:            <td>'.$lt{'cau'}.'</td>
                   2048:            <td>'.$cudom.'_'.$cuname.'</td>
                   2049:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
                   2050:              <a href=
                   2051: "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>
                   2052: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
                   2053: <a href=
                   2054: "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".
                   2055:               &Apache::loncommon::end_data_table_row()."\n".
                   2056:               &Apache::loncommon::start_data_table_row()."\n".
1.291     bisitz   2057: '<td><input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
1.218     raeburn  2058: <td>'.$lt{'caa'}.'</td>
                   2059: <td>'.$cudom.'_'.$cuname.'</td>
                   2060: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
                   2061: <a href=
                   2062: "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>
                   2063: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
                   2064: <a href=
                   2065: "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".
                   2066:              &Apache::loncommon::end_data_table_row()."\n".
                   2067:              &Apache::loncommon::end_data_table());
                   2068:     } elsif ($env{'request.role'} =~ /^au\./) {
                   2069:         if (!(&Apache::lonuserutils::authorpriv($env{'user.name'},
                   2070:                                                 $env{'request.role.domain'}))) {
                   2071:             $r->print('<span class="LC_error">'.
                   2072:                       &mt('You do not have privileges to assign co-author roles.').
                   2073:                       '</span>');
                   2074:         } elsif (($env{'user.name'} eq $ccuname) &&
                   2075:              ($env{'user.domain'} eq $ccdomain)) {
1.377     raeburn  2076:             $r->print(&mt('Assigning yourself a co-author or assistant co-author role in your own author area in Authoring Space is not permitted'));
1.218     raeburn  2077:         }
                   2078:     }
                   2079:     return $addrolesdisplay;;
                   2080: }
                   2081: 
                   2082: sub new_domain_roles {
1.357     raeburn  2083:     my ($r,$ccdomain) = @_;
1.218     raeburn  2084:     my $addrolesdisplay = 0;
                   2085:     #
                   2086:     # Domain level
                   2087:     #
                   2088:     my $num_domain_level = 0;
                   2089:     my $domaintext =
                   2090:     '<h4>'.&mt('Domain Level').'</h4>'.
                   2091:     &Apache::loncommon::start_data_table().
                   2092:     &Apache::loncommon::start_data_table_header_row().
                   2093:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
                   2094:     &mt('Extent').'</th>'.
                   2095:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
                   2096:     &Apache::loncommon::end_data_table_header_row();
1.312     raeburn  2097:     my @allroles = &Apache::lonuserutils::roles_by_context('domain');
1.218     raeburn  2098:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
1.312     raeburn  2099:         foreach my $role (@allroles) {
                   2100:             next if ($role eq 'ad');
1.357     raeburn  2101:             next if (($role eq 'au') && ($ccdomain ne $thisdomain));
1.218     raeburn  2102:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
                   2103:                my $plrole=&Apache::lonnet::plaintext($role);
                   2104:                my %lt=&Apache::lonlocal::texthash(
                   2105:                     'ssd'  => "Set Start Date",
                   2106:                     'sed'  => "Set End Date"
                   2107:                                        );
                   2108:                $num_domain_level ++;
                   2109:                $domaintext .=
                   2110: &Apache::loncommon::start_data_table_row().
1.291     bisitz   2111: '<td><input type="checkbox" name="act_'.$thisdomain.'_'.$role.'" /></td>
1.218     raeburn  2112: <td>'.$plrole.'</td>
                   2113: <td>'.$thisdomain.'</td>
                   2114: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
                   2115: <a href=
                   2116: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
                   2117: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
                   2118: <a href=
                   2119: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
                   2120: &Apache::loncommon::end_data_table_row();
                   2121:             }
                   2122:         }
                   2123:     }
                   2124:     $domaintext.= &Apache::loncommon::end_data_table();
                   2125:     if ($num_domain_level > 0) {
                   2126:         $r->print($domaintext);
                   2127:         $addrolesdisplay = 1;
                   2128:     }
                   2129:     return $addrolesdisplay;
                   2130: }
                   2131: 
1.188     raeburn  2132: sub user_authentication {
1.227     raeburn  2133:     my ($ccuname,$ccdomain,$formname) = @_;
1.188     raeburn  2134:     my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
1.227     raeburn  2135:     my $outcome;
1.406.2.6  raeburn  2136:     my %lt=&Apache::lonlocal::texthash(
                   2137:                    'err'   => "ERROR",
                   2138:                    'uuas'  => "This user has an unrecognized authentication scheme",
                   2139:                    'adcs'  => "Please alert a domain coordinator of this situation",
                   2140:                    'sldb'  => "Please specify login data below",
                   2141:                    'ld'    => "Login Data"
                   2142:     );
1.188     raeburn  2143:     # Check for a bad authentication type
                   2144:     if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth):/) {
                   2145:         # bad authentication scheme
                   2146:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
1.227     raeburn  2147:             &initialize_authen_forms($ccdomain,$formname);
                   2148: 
1.190     raeburn  2149:             my $choices = &Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc);
1.188     raeburn  2150:             $outcome = <<ENDBADAUTH;
                   2151: <script type="text/javascript" language="Javascript">
1.301     bisitz   2152: // <![CDATA[
1.188     raeburn  2153: $loginscript
1.301     bisitz   2154: // ]]>
1.188     raeburn  2155: </script>
                   2156: <span class="LC_error">$lt{'err'}:
                   2157: $lt{'uuas'} ($currentauth). $lt{'sldb'}.</span>
                   2158: <h3>$lt{'ld'}</h3>
                   2159: $choices
                   2160: ENDBADAUTH
                   2161:         } else {
                   2162:             # This user is not allowed to modify the user's
                   2163:             # authentication scheme, so just notify them of the problem
                   2164:             $outcome = <<ENDBADAUTH;
                   2165: <span class="LC_error"> $lt{'err'}: 
                   2166: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
                   2167: </span>
                   2168: ENDBADAUTH
                   2169:         }
                   2170:     } else { # Authentication type is valid
1.227     raeburn  2171:         &initialize_authen_forms($ccdomain,$formname,$currentauth,'modifyuser');
1.205     raeburn  2172:         my ($authformcurrent,$can_modify,@authform_others) =
1.188     raeburn  2173:             &modify_login_block($ccdomain,$currentauth);
                   2174:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2175:             # Current user has login modification privileges
                   2176:             $outcome =
                   2177:                        '<script type="text/javascript" language="Javascript">'."\n".
1.301     bisitz   2178:                        '// <![CDATA['."\n".
1.188     raeburn  2179:                        $loginscript."\n".
1.301     bisitz   2180:                        '// ]]>'."\n".
1.188     raeburn  2181:                        '</script>'."\n".
                   2182:                        '<h3>'.$lt{'ld'}.'</h3>'.
                   2183:                        &Apache::loncommon::start_data_table().
1.205     raeburn  2184:                        &Apache::loncommon::start_data_table_row().
1.188     raeburn  2185:                        '<td>'.$authformnop;
1.406.2.6  raeburn  2186:             if (($can_modify) && (&Apache::lonnet::allowed('mau',$ccdomain))) {
1.188     raeburn  2187:                 $outcome .= '</td>'."\n".
                   2188:                             &Apache::loncommon::end_data_table_row().
                   2189:                             &Apache::loncommon::start_data_table_row().
                   2190:                             '<td>'.$authformcurrent.'</td>'.
                   2191:                             &Apache::loncommon::end_data_table_row()."\n";
                   2192:             } else {
1.200     raeburn  2193:                 $outcome .= '&nbsp;('.$authformcurrent.')</td>'.
                   2194:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  2195:             }
1.406.2.6  raeburn  2196:             if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2197:                 foreach my $item (@authform_others) { 
                   2198:                     $outcome .= &Apache::loncommon::start_data_table_row().
                   2199:                                 '<td>'.$item.'</td>'.
                   2200:                                 &Apache::loncommon::end_data_table_row()."\n";
                   2201:                 }
1.188     raeburn  2202:             }
1.205     raeburn  2203:             $outcome .= &Apache::loncommon::end_data_table();
1.188     raeburn  2204:         } else {
1.406.2.6  raeburn  2205:             if (&Apache::lonnet::allowed('udp',$ccdomain)) {
                   2206:                 # Current user has rights to view domain preferences for user's domain
                   2207:                 my $result;
                   2208:                 if ($currentauth =~ /^krb(4|5):([^:]*)$/) {
                   2209:                     my ($krbver,$krbrealm) = ($1,$2);
                   2210:                     if ($krbrealm eq '') {
                   2211:                         $result = &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
                   2212:                     } else {
                   2213:                         $result = &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
1.406.2.9! raeburn  2214:                                       $krbrealm,$krbver);
1.406.2.6  raeburn  2215:                     }
                   2216:                 } elsif ($currentauth =~ /^internal:/) {
                   2217:                     $result = &mt('Currently internally authenticated.');
                   2218:                 } elsif ($currentauth =~ /^localauth:/) {
                   2219:                     $result = &mt('Currently using local (institutional) authentication.');
                   2220:                 } elsif ($currentauth =~ /^unix:/) {
                   2221:                     $result = &mt('Currently Filesystem Authenticated.');
                   2222:                 }
                   2223:                 $outcome = '<h3>'.$lt{'ld'}.'</h3>'.
                   2224:                            &Apache::loncommon::start_data_table().
                   2225:                            &Apache::loncommon::start_data_table_row().
                   2226:                            '<td>'.$result.'</td>'.
                   2227:                            &Apache::loncommon::end_data_table_row()."\n".
                   2228:                            &Apache::loncommon::end_data_table();
                   2229:             } elsif (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
1.188     raeburn  2230:                 my %lt=&Apache::lonlocal::texthash(
                   2231:                            'ccld'  => "Change Current Login Data",
                   2232:                            'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
                   2233:                            'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   2234:                 );
                   2235:                 $outcome .= <<ENDNOPRIV;
                   2236: <h3>$lt{'ccld'}</h3>
                   2237: $lt{'yodo'} $lt{'ifch'}: $ccdomain
1.235     raeburn  2238: <input type="hidden" name="login" value="nochange" />
1.188     raeburn  2239: ENDNOPRIV
                   2240:             }
                   2241:         }
                   2242:     }  ## End of "check for bad authentication type" logic
                   2243:     return $outcome;
                   2244: }
                   2245: 
1.187     raeburn  2246: sub modify_login_block {
                   2247:     my ($dom,$currentauth) = @_;
                   2248:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2249:     my ($authnum,%can_assign) =
                   2250:         &Apache::loncommon::get_assignable_auth($dom);
1.205     raeburn  2251:     my ($authformcurrent,@authform_others,$show_override_msg);
1.187     raeburn  2252:     if ($currentauth=~/^krb(4|5):/) {
                   2253:         $authformcurrent=$authformkrb;
                   2254:         if ($can_assign{'int'}) {
1.205     raeburn  2255:             push(@authform_others,$authformint);
1.187     raeburn  2256:         }
                   2257:         if ($can_assign{'loc'}) {
1.205     raeburn  2258:             push(@authform_others,$authformloc);
1.187     raeburn  2259:         }
                   2260:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   2261:             $show_override_msg = 1;
                   2262:         }
                   2263:     } elsif ($currentauth=~/^internal:/) {
                   2264:         $authformcurrent=$authformint;
                   2265:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2266:             push(@authform_others,$authformkrb);
1.187     raeburn  2267:         }
                   2268:         if ($can_assign{'loc'}) {
1.205     raeburn  2269:             push(@authform_others,$authformloc);
1.187     raeburn  2270:         }
                   2271:         if ($can_assign{'int'}) {
                   2272:             $show_override_msg = 1;
                   2273:         }
                   2274:     } elsif ($currentauth=~/^unix:/) {
                   2275:         $authformcurrent=$authformfsys;
                   2276:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2277:             push(@authform_others,$authformkrb);
1.187     raeburn  2278:         }
                   2279:         if ($can_assign{'int'}) {
1.205     raeburn  2280:             push(@authform_others,$authformint);
1.187     raeburn  2281:         }
                   2282:         if ($can_assign{'loc'}) {
1.205     raeburn  2283:             push(@authform_others,$authformloc);
1.187     raeburn  2284:         }
                   2285:         if ($can_assign{'fsys'}) {
                   2286:             $show_override_msg = 1;
                   2287:         }
                   2288:     } elsif ($currentauth=~/^localauth:/) {
                   2289:         $authformcurrent=$authformloc;
                   2290:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2291:             push(@authform_others,$authformkrb);
1.187     raeburn  2292:         }
                   2293:         if ($can_assign{'int'}) {
1.205     raeburn  2294:             push(@authform_others,$authformint);
1.187     raeburn  2295:         }
                   2296:         if ($can_assign{'loc'}) {
                   2297:             $show_override_msg = 1;
                   2298:         }
                   2299:     }
                   2300:     if ($show_override_msg) {
1.205     raeburn  2301:         $authformcurrent = '<table><tr><td colspan="3">'.$authformcurrent.
                   2302:                            '</td></tr>'."\n".
                   2303:                            '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
                   2304:                            '<td><b>'.&mt('Currently in use').'</b></td>'.
                   2305:                            '<td align="right"><span class="LC_cusr_emph">'.
1.187     raeburn  2306:                             &mt('will override current values').
1.205     raeburn  2307:                             '</span></td></tr></table>';
1.187     raeburn  2308:     }
1.205     raeburn  2309:     return ($authformcurrent,$show_override_msg,@authform_others); 
1.187     raeburn  2310: }
                   2311: 
1.188     raeburn  2312: sub personal_data_display {
1.391     raeburn  2313:     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$rolesarray,
1.396     raeburn  2314:         $now,$captchaform,$emailusername,$usertype) = @_;
1.388     bisitz   2315:     my ($output,%userenv,%canmodify,%canmodify_status);
1.219     raeburn  2316:     my @userinfo = ('firstname','middlename','lastname','generation',
                   2317:                     'permanentemail','id');
1.252     raeburn  2318:     my $rowcount = 0;
                   2319:     my $editable = 0;
1.391     raeburn  2320:     my %textboxsize = (
                   2321:                        firstname      => '15',
                   2322:                        middlename     => '15',
                   2323:                        lastname       => '15',
                   2324:                        generation     => '5',
                   2325:                        permanentemail => '25',
                   2326:                        id             => '15',
                   2327:                       );
                   2328: 
                   2329:     my %lt=&Apache::lonlocal::texthash(
                   2330:                 'pd'             => "Personal Data",
                   2331:                 'firstname'      => "First Name",
                   2332:                 'middlename'     => "Middle Name",
                   2333:                 'lastname'       => "Last Name",
                   2334:                 'generation'     => "Generation",
                   2335:                 'permanentemail' => "Permanent e-mail address",
                   2336:                 'id'             => "Student/Employee ID",
                   2337:                 'lg'             => "Login Data",
                   2338:                 'inststatus'     => "Affiliation",
                   2339:                 'email'          => 'E-mail address',
                   2340:                 'valid'          => 'Validation',
                   2341:     );
                   2342: 
                   2343:     %canmodify_status =
1.286     raeburn  2344:         &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
                   2345:                                                    ['inststatus'],$rolesarray);
1.253     raeburn  2346:     if (!$newuser) {
1.188     raeburn  2347:         # Get the users information
                   2348:         %userenv = &Apache::lonnet::get('environment',
                   2349:                    ['firstname','middlename','lastname','generation',
1.286     raeburn  2350:                     'permanentemail','id','inststatus'],$ccdomain,$ccuname);
1.219     raeburn  2351:         %canmodify =
                   2352:             &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
1.252     raeburn  2353:                                                        \@userinfo,$rolesarray);
1.257     raeburn  2354:     } elsif ($context eq 'selfcreate') {
1.391     raeburn  2355:         if ($newuser eq 'email') {
1.396     raeburn  2356:             if (ref($emailusername) eq 'HASH') {
                   2357:                 if (ref($emailusername->{$usertype}) eq 'HASH') {
                   2358:                     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
                   2359:                     @userinfo = ();          
                   2360:                     if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
                   2361:                         foreach my $field (@{$infofields}) { 
                   2362:                             if ($emailusername->{$usertype}->{$field}) {
                   2363:                                 push(@userinfo,$field);
                   2364:                                 $canmodify{$field} = 1;
                   2365:                                 unless ($textboxsize{$field}) {
                   2366:                                     $textboxsize{$field} = 25;
                   2367:                                 }
                   2368:                                 unless ($lt{$field}) {
                   2369:                                     $lt{$field} = $infotitles->{$field};
                   2370:                                 }
                   2371:                                 if ($emailusername->{$usertype}->{$field} eq 'required') {
                   2372:                                     $lt{$field} .= '<b>*</b>';
                   2373:                                 }
1.391     raeburn  2374:                             }
                   2375:                         }
                   2376:                     }
                   2377:                 }
                   2378:             }
                   2379:         } else {
                   2380:             %canmodify = &selfcreate_canmodify($context,$ccdomain,\@userinfo,
                   2381:                                                $inst_results,$rolesarray);
                   2382:         }
1.188     raeburn  2383:     }
1.391     raeburn  2384: 
1.188     raeburn  2385:     my $genhelp=&Apache::loncommon::help_open_topic('Generation');
                   2386:     $output = '<h3>'.$lt{'pd'}.'</h3>'.
                   2387:               &Apache::lonhtmlcommon::start_pick_box();
1.391     raeburn  2388:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
1.396     raeburn  2389:         $output .= &Apache::lonhtmlcommon::row_title($lt{'email'}.'<b>*</b>',undef,
1.391     raeburn  2390:                                                      'LC_oddrow_value')."\n".
1.394     raeburn  2391:                    '<input type="text" name="uname" size="25" value="" autocomplete="off" />';
1.391     raeburn  2392:         $rowcount ++;
                   2393:         $output .= &Apache::lonhtmlcommon::row_closure(1);
1.406.2.1  raeburn  2394:         my $upassone = '<input type="password" name="upass'.$now.'" size="20" autocomplete="off" />';
                   2395:         my $upasstwo = '<input type="password" name="upasscheck'.$now.'" size="20" autocomplete="off" />';
1.396     raeburn  2396:         $output .= &Apache::lonhtmlcommon::row_title(&mt('Password').'<b>*</b>',
1.391     raeburn  2397:                                                     'LC_pick_box_title',
                   2398:                                                     'LC_oddrow_value')."\n".
                   2399:                    $upassone."\n".
                   2400:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
1.396     raeburn  2401:                    &Apache::lonhtmlcommon::row_title(&mt('Confirm password').'<b>*</b>',
1.391     raeburn  2402:                                                      'LC_pick_box_title',
                   2403:                                                      'LC_oddrow_value')."\n".
                   2404:                    $upasstwo.
                   2405:                    &Apache::lonhtmlcommon::row_closure()."\n";
                   2406:     }
1.188     raeburn  2407:     foreach my $item (@userinfo) {
                   2408:         my $rowtitle = $lt{$item};
1.252     raeburn  2409:         my $hiderow = 0;
1.188     raeburn  2410:         if ($item eq 'generation') {
                   2411:             $rowtitle = $genhelp.$rowtitle;
                   2412:         }
1.252     raeburn  2413:         my $row = &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
1.188     raeburn  2414:         if ($newuser) {
1.210     raeburn  2415:             if (ref($inst_results) eq 'HASH') {
                   2416:                 if ($inst_results->{$item} ne '') {
1.252     raeburn  2417:                     $row .= '<input type="hidden" name="c'.$item.'" value="'.$inst_results->{$item}.'" />'.$inst_results->{$item};
1.210     raeburn  2418:                 } else {
1.252     raeburn  2419:                     if ($context eq 'selfcreate') {
1.391     raeburn  2420:                         if ($canmodify{$item}) {
1.394     raeburn  2421:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.252     raeburn  2422:                             $editable ++;
                   2423:                         } else {
                   2424:                             $hiderow = 1;
                   2425:                         }
1.253     raeburn  2426:                     } else {
                   2427:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2428:                     }
1.210     raeburn  2429:                 }
1.188     raeburn  2430:             } else {
1.252     raeburn  2431:                 if ($context eq 'selfcreate') {
1.401     raeburn  2432:                     if ($canmodify{$item}) {
                   2433:                         if ($newuser eq 'email') {
                   2434:                             $row .= '<input type="text" name="'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.287     raeburn  2435:                         } else {
1.401     raeburn  2436:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.287     raeburn  2437:                         }
1.401     raeburn  2438:                         $editable ++;
                   2439:                     } else {
                   2440:                         $hiderow = 1;
1.252     raeburn  2441:                     }
1.253     raeburn  2442:                 } else {
                   2443:                     $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2444:                 }
1.188     raeburn  2445:             }
                   2446:         } else {
1.219     raeburn  2447:             if ($canmodify{$item}) {
1.252     raeburn  2448:                 $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
1.393     raeburn  2449:                 if (($item eq 'id') && (!$newuser)) {
                   2450:                     $row .= '<br />'.&Apache::lonuserutils::forceid_change($context);
                   2451:                 }
1.188     raeburn  2452:             } else {
1.252     raeburn  2453:                 $row .= $userenv{$item};
1.188     raeburn  2454:             }
                   2455:         }
1.252     raeburn  2456:         $row .= &Apache::lonhtmlcommon::row_closure(1);
                   2457:         if (!$hiderow) {
                   2458:             $output .= $row;
                   2459:             $rowcount ++;
                   2460:         }
1.188     raeburn  2461:     }
1.286     raeburn  2462:     if (($canmodify_status{'inststatus'}) || ($context ne 'selfcreate')) {
                   2463:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($ccdomain);
                   2464:         if (ref($types) eq 'ARRAY') {
                   2465:             if (@{$types} > 0) {
                   2466:                 my ($hiderow,$shown);
                   2467:                 if ($canmodify_status{'inststatus'}) {
                   2468:                     $shown = &pick_inst_statuses($userenv{'inststatus'},$usertypes,$types);
                   2469:                 } else {
                   2470:                     if ($userenv{'inststatus'} eq '') {
                   2471:                         $hiderow = 1;
1.334     raeburn  2472:                     } else {
                   2473:                         my @showitems;
                   2474:                         foreach my $item ( map { &unescape($_); } split(':',$userenv{'inststatus'})) {
                   2475:                             if (exists($usertypes->{$item})) {
                   2476:                                 push(@showitems,$usertypes->{$item});
                   2477:                             } else {
                   2478:                                 push(@showitems,$item);
                   2479:                             }
                   2480:                         }
                   2481:                         if (@showitems) {
                   2482:                             $shown = join(', ',@showitems);
                   2483:                         } else {
                   2484:                             $hiderow = 1;
                   2485:                         }
1.286     raeburn  2486:                     }
                   2487:                 }
                   2488:                 if (!$hiderow) {
1.389     bisitz   2489:                     my $row = &Apache::lonhtmlcommon::row_title(&mt('Affiliations'),undef,'LC_oddrow_value')."\n".
1.286     raeburn  2490:                               $shown.&Apache::lonhtmlcommon::row_closure(1); 
                   2491:                     if ($context eq 'selfcreate') {
                   2492:                         $rowcount ++;
                   2493:                     }
                   2494:                     $output .= $row;
                   2495:                 }
                   2496:             }
                   2497:         }
                   2498:     }
1.391     raeburn  2499:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
                   2500:         if ($captchaform) {
1.406.2.2  raeburn  2501:             $output .= &Apache::lonhtmlcommon::row_title($lt{'valid'}.'*',
1.391     raeburn  2502:                                                          'LC_pick_box_title')."\n".
                   2503:                        $captchaform."\n".'<br /><br />'.
                   2504:                        &Apache::lonhtmlcommon::row_closure(1); 
                   2505:             $rowcount ++;
                   2506:         }
                   2507:         my $submit_text = &mt('Create account');
                   2508:         $output .= &Apache::lonhtmlcommon::row_title()."\n".
                   2509:                    '<br /><input type="submit" name="createaccount" value="'.
                   2510:                    $submit_text.'" />'.
1.396     raeburn  2511:                    '<input type="hidden" name="type" value="'.$usertype.'" />'.
1.391     raeburn  2512:                    &Apache::lonhtmlcommon::row_closure(1);
                   2513:     }
1.188     raeburn  2514:     $output .= &Apache::lonhtmlcommon::end_pick_box();
1.206     raeburn  2515:     if (wantarray) {
1.252     raeburn  2516:         if ($context eq 'selfcreate') {
                   2517:             return($output,$rowcount,$editable);
                   2518:         } else {
1.388     bisitz   2519:             return $output;
1.252     raeburn  2520:         }
1.206     raeburn  2521:     } else {
                   2522:         return $output;
                   2523:     }
1.188     raeburn  2524: }
                   2525: 
1.286     raeburn  2526: sub pick_inst_statuses {
                   2527:     my ($curr,$usertypes,$types) = @_;
                   2528:     my ($output,$rem,@currtypes);
                   2529:     if ($curr ne '') {
                   2530:         @currtypes = map { &unescape($_); } split(/:/,$curr);
                   2531:     }
                   2532:     my $numinrow = 2;
                   2533:     if (ref($types) eq 'ARRAY') {
                   2534:         $output = '<table>';
                   2535:         my $lastcolspan; 
                   2536:         for (my $i=0; $i<@{$types}; $i++) {
                   2537:             if (defined($usertypes->{$types->[$i]})) {
                   2538:                 my $rem = $i%($numinrow);
                   2539:                 if ($rem == 0) {
                   2540:                     if ($i<@{$types}-1) {
                   2541:                         if ($i > 0) { 
                   2542:                             $output .= '</tr>';
                   2543:                         }
                   2544:                         $output .= '<tr>';
                   2545:                     }
                   2546:                 } elsif ($i==@{$types}-1) {
                   2547:                     my $colsleft = $numinrow - $rem;
                   2548:                     if ($colsleft > 1) {
                   2549:                         $lastcolspan = ' colspan="'.$colsleft.'"';
                   2550:                     }
                   2551:                 }
                   2552:                 my $check = ' ';
                   2553:                 if (grep(/^\Q$types->[$i]\E$/,@currtypes)) {
                   2554:                     $check = ' checked="checked" ';
                   2555:                 }
                   2556:                 $output .= '<td class="LC_left_item"'.$lastcolspan.'>'.
                   2557:                            '<span class="LC_nobreak"><label>'.
                   2558:                            '<input type="checkbox" name="inststatus" '.
                   2559:                            'value="'.$types->[$i].'"'.$check.'/>'.
                   2560:                            $usertypes->{$types->[$i]}.'</label></span></td>';
                   2561:             }
                   2562:         }
                   2563:         $output .= '</tr></table>';
                   2564:     }
                   2565:     return $output;
                   2566: }
                   2567: 
1.257     raeburn  2568: sub selfcreate_canmodify {
                   2569:     my ($context,$dom,$userinfo,$inst_results,$rolesarray) = @_;
                   2570:     if (ref($inst_results) eq 'HASH') {
                   2571:         my @inststatuses = &get_inststatuses($inst_results);
                   2572:         if (@inststatuses == 0) {
                   2573:             @inststatuses = ('default');
                   2574:         }
                   2575:         $rolesarray = \@inststatuses;
                   2576:     }
                   2577:     my %canmodify =
                   2578:         &Apache::lonuserutils::can_modify_userinfo($context,$dom,$userinfo,
                   2579:                                                    $rolesarray);
                   2580:     return %canmodify;
                   2581: }
                   2582: 
1.252     raeburn  2583: sub get_inststatuses {
                   2584:     my ($insthashref) = @_;
                   2585:     my @inststatuses = ();
                   2586:     if (ref($insthashref) eq 'HASH') {
                   2587:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
                   2588:             @inststatuses = @{$insthashref->{'inststatus'}};
                   2589:         }
                   2590:     }
                   2591:     return @inststatuses;
                   2592: }
                   2593: 
1.4       www      2594: # ================================================================= Phase Three
1.42      matthew  2595: sub update_user_data {
1.375     raeburn  2596:     my ($r,$context,$crstype,$brcrum,$showcredits) = @_; 
1.101     albertel 2597:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                   2598:                                           $env{'form.ccdomain'});
1.27      matthew  2599:     # Error messages
1.188     raeburn  2600:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
1.193     raeburn  2601:     my $end       = '</span><br /><br />';
                   2602:     my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
1.188     raeburn  2603:                     "'$env{'form.prevphase'}','modify')".'" />'.
1.219     raeburn  2604:                     &mt('Return to previous page').'</a>'.
                   2605:                     &Apache::loncommon::end_page();
                   2606:     my $now = time;
1.40      www      2607:     my $title;
1.101     albertel 2608:     if (exists($env{'form.makeuser'})) {
1.40      www      2609: 	$title='Set Privileges for New User';
                   2610:     } else {
                   2611:         $title='Modify User Privileges';
                   2612:     }
1.213     raeburn  2613:     my $newuser = 0;
1.160     raeburn  2614:     my ($jsback,$elements) = &crumb_utilities();
                   2615:     my $jscript = '<script type="text/javascript">'."\n".
1.301     bisitz   2616:                   '// <![CDATA['."\n".
                   2617:                   $jsback."\n".
                   2618:                   '// ]]>'."\n".
                   2619:                   '</script>'."\n";
1.406.2.7  raeburn  2620:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$env{'form.ccdomain'});
1.351     raeburn  2621:     push (@{$brcrum},
                   2622:              {href => "javascript:backPage(document.userupdate)",
                   2623:               text => $breadcrumb_text{'search'},
                   2624:               faq  => 282,
                   2625:               bug  => 'Instructor Interface',}
                   2626:              );
                   2627:     if ($env{'form.prevphase'} eq 'userpicked') {
                   2628:         push(@{$brcrum},
                   2629:                {href => "javascript:backPage(document.userupdate,'get_user_info','select')",
                   2630:                 text => $breadcrumb_text{'userpicked'},
                   2631:                 faq  => 282,
                   2632:                 bug  => 'Instructor Interface',});
1.233     raeburn  2633:     }
1.224     raeburn  2634:     my $helpitem = 'Course_Change_Privileges';
                   2635:     if ($env{'form.action'} eq 'singlestudent') {
                   2636:         $helpitem = 'Course_Add_Student';
                   2637:     }
1.351     raeburn  2638:     push(@{$brcrum}, 
                   2639:             {href => "javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
                   2640:              text => $breadcrumb_text{'modify'},
                   2641:              faq  => 282,
                   2642:              bug  => 'Instructor Interface',},
                   2643:             {href => "/adm/createuser",
                   2644:              text => "Result",
                   2645:              faq  => 282,
                   2646:              bug  => 'Instructor Interface',
                   2647:              help => $helpitem});
                   2648:     my $args = {bread_crumbs          => $brcrum,
                   2649:                 bread_crumbs_component => 'User Management'};
                   2650:     if ($env{'form.popup'}) {
                   2651:         $args->{'no_nav_bar'} = 1;
                   2652:     }
                   2653:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
1.188     raeburn  2654:     $r->print(&update_result_form($uhome));
1.27      matthew  2655:     # Check Inputs
1.101     albertel 2656:     if (! $env{'form.ccuname'} ) {
1.193     raeburn  2657: 	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
1.27      matthew  2658: 	return;
                   2659:     }
1.138     albertel 2660:     if (  $env{'form.ccuname'} ne 
                   2661: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
1.281     bisitz   2662: 	$r->print($error.&mt('Invalid login name.').'  '.
                   2663: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid.').
1.193     raeburn  2664: 		  $end.$rtnlink);
1.27      matthew  2665: 	return;
                   2666:     }
1.101     albertel 2667:     if (! $env{'form.ccdomain'}       ) {
1.193     raeburn  2668: 	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
1.27      matthew  2669: 	return;
                   2670:     }
1.138     albertel 2671:     if (  $env{'form.ccdomain'} ne
                   2672: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
1.281     bisitz   2673: 	$r->print($error.&mt('Invalid domain name.').'  '.
                   2674: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid.').
1.193     raeburn  2675: 		  $end.$rtnlink);
1.27      matthew  2676: 	return;
                   2677:     }
1.219     raeburn  2678:     if ($uhome eq 'no_host') {
                   2679:         $newuser = 1;
                   2680:     }
1.101     albertel 2681:     if (! exists($env{'form.makeuser'})) {
1.29      matthew  2682:         # Modifying an existing user, so check the validity of the name
                   2683:         if ($uhome eq 'no_host') {
1.389     bisitz   2684:             $r->print(
                   2685:                 $error
                   2686:                .'<p class="LC_error">'
                   2687:                .&mt('Unable to determine home server for [_1] in domain [_2].',
                   2688:                         '"'.$env{'form.ccuname'}.'"','"'.$env{'form.ccdomain'}.'"')
                   2689:                .'</p>');
1.29      matthew  2690:             return;
                   2691:         }
                   2692:     }
1.27      matthew  2693:     # Determine authentication method and password for the user being modified
                   2694:     my $amode='';
                   2695:     my $genpwd='';
1.101     albertel 2696:     if ($env{'form.login'} eq 'krb') {
1.41      albertel 2697: 	$amode='krb';
1.101     albertel 2698: 	$amode.=$env{'form.krbver'};
                   2699: 	$genpwd=$env{'form.krbarg'};
                   2700:     } elsif ($env{'form.login'} eq 'int') {
1.27      matthew  2701: 	$amode='internal';
1.101     albertel 2702: 	$genpwd=$env{'form.intarg'};
                   2703:     } elsif ($env{'form.login'} eq 'fsys') {
1.27      matthew  2704: 	$amode='unix';
1.101     albertel 2705: 	$genpwd=$env{'form.fsysarg'};
                   2706:     } elsif ($env{'form.login'} eq 'loc') {
1.27      matthew  2707: 	$amode='localauth';
1.101     albertel 2708: 	$genpwd=$env{'form.locarg'};
1.27      matthew  2709: 	$genpwd=" " if (!$genpwd);
1.101     albertel 2710:     } elsif (($env{'form.login'} eq 'nochange') ||
                   2711:              ($env{'form.login'} eq ''        )) { 
1.34      matthew  2712:         # There is no need to tell the user we did not change what they
                   2713:         # did not ask us to change.
1.35      matthew  2714:         # If they are creating a new user but have not specified login
                   2715:         # information this will be caught below.
1.30      matthew  2716:     } else {
1.367     golterma 2717:             $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);
                   2718:             return;
1.27      matthew  2719:     }
1.164     albertel 2720: 
1.188     raeburn  2721:     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
1.367     golterma 2722:                         $env{'form.ccuname'}.' ('.&Apache::loncommon::plainname($env{'form.ccuname'},
                   2723:                         $env{'form.ccdomain'}).')', $env{'form.ccdomain'}).'</h3>');
                   2724:     my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,2);
1.344     bisitz   2725: 
1.193     raeburn  2726:     my (%alerts,%rulematch,%inst_results,%curr_rules);
1.334     raeburn  2727:     my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
1.361     raeburn  2728:     my @usertools = ('aboutme','blog','webdav','portfolio');
1.384     raeburn  2729:     my @requestcourses = ('official','unofficial','community','textbook');
1.362     raeburn  2730:     my @requestauthor = ('requestauthor');
1.286     raeburn  2731:     my ($othertitle,$usertypes,$types) = 
                   2732:         &Apache::loncommon::sorted_inst_types($env{'form.ccdomain'});
1.334     raeburn  2733:     my %canmodify_status =
                   2734:         &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},
                   2735:                                                    ['inststatus']);
1.101     albertel 2736:     if ($env{'form.makeuser'}) {
1.164     albertel 2737: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
1.27      matthew  2738:         # Check for the authentication mode and password
                   2739:         if (! $amode || ! $genpwd) {
1.193     raeburn  2740: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.27      matthew  2741: 	    return;
1.18      albertel 2742: 	}
1.29      matthew  2743:         # Determine desired host
1.101     albertel 2744:         my $desiredhost = $env{'form.hserver'};
1.29      matthew  2745:         if (lc($desiredhost) eq 'default') {
                   2746:             $desiredhost = undef;
                   2747:         } else {
1.147     albertel 2748:             my %home_servers = 
                   2749: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
1.29      matthew  2750:             if (! exists($home_servers{$desiredhost})) {
1.193     raeburn  2751:                 $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
                   2752:                 return;
                   2753:             }
                   2754:         }
                   2755:         # Check ID format
                   2756:         my %checkhash;
                   2757:         my %checks = ('id' => 1);
                   2758:         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
1.219     raeburn  2759:             'newuser' => $newuser, 
1.196     raeburn  2760:             'id' => $env{'form.cid'},
1.193     raeburn  2761:         );
1.196     raeburn  2762:         if ($env{'form.cid'} ne '') {
                   2763:             &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   2764:                                           \%rulematch,\%inst_results,\%curr_rules);
                   2765:             if (ref($alerts{'id'}) eq 'HASH') {
                   2766:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
                   2767:                     my $domdesc =
                   2768:                         &Apache::lonnet::domain($env{'form.ccdomain'},'description');
                   2769:                     if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
                   2770:                         my $userchkmsg;
                   2771:                         if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
                   2772:                             $userchkmsg  = 
                   2773:                                 &Apache::loncommon::instrule_disallow_msg('id',
                   2774:                                                                     $domdesc,1).
                   2775:                                 &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
                   2776:                                     $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
                   2777:                         }
                   2778:                         $r->print($error.&mt('Invalid ID format').$end.
                   2779:                                   $userchkmsg.$rtnlink);
                   2780:                         return;
                   2781:                     }
                   2782:                 }
1.29      matthew  2783:             }
                   2784:         }
1.367     golterma 2785:         &Apache::lonhtmlcommon::Increment_PrgWin($r, \%prog_state);
1.27      matthew  2786: 	# Call modifyuser
                   2787: 	my $result = &Apache::lonnet::modifyuser
1.193     raeburn  2788: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
1.188     raeburn  2789:              $amode,$genpwd,$env{'form.cfirstname'},
                   2790:              $env{'form.cmiddlename'},$env{'form.clastname'},
                   2791:              $env{'form.cgeneration'},undef,$desiredhost,
                   2792:              $env{'form.cpermanentemail'});
1.77      www      2793: 	$r->print(&mt('Generating user').': '.$result);
1.219     raeburn  2794:         $uhome = &Apache::lonnet::homeserver($env{'form.ccuname'},
1.101     albertel 2795:                                                $env{'form.ccdomain'});
1.334     raeburn  2796:         my (%changeHash,%newcustom,%changed,%changedinfo);
1.267     raeburn  2797:         if ($uhome ne 'no_host') {
1.334     raeburn  2798:             if ($context eq 'domain') {
1.378     raeburn  2799:                 foreach my $name ('portfolio','author') {
                   2800:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   2801:                         if ($env{'form.'.$name.'quota'} eq '') {
                   2802:                             $newcustom{$name.'quota'} = 0;
                   2803:                         } else {
                   2804:                             $newcustom{$name.'quota'} = $env{'form.'.$name.'quota'};
                   2805:                             $newcustom{$name.'quota'} =~ s/[^\d\.]//g;
                   2806:                         }
                   2807:                         if (&quota_admin($newcustom{$name.'quota'},\%changeHash,$name)) {
                   2808:                             $changed{$name.'quota'} = 1;
                   2809:                         }
1.334     raeburn  2810:                     }
                   2811:                 }
                   2812:                 foreach my $item (@usertools) {
                   2813:                     if ($env{'form.custom'.$item} == 1) {
                   2814:                         $newcustom{$item} = $env{'form.tools_'.$item};
                   2815:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2816:                                                      \%changeHash,'tools');
                   2817:                     }
1.267     raeburn  2818:                 }
1.334     raeburn  2819:                 foreach my $item (@requestcourses) {
1.341     raeburn  2820:                     if ($env{'form.custom'.$item} == 1) {
                   2821:                         $newcustom{$item} = $env{'form.crsreq_'.$item};
                   2822:                         if ($env{'form.crsreq_'.$item} eq 'autolimit') {
                   2823:                             $newcustom{$item} .= '=';
1.383     raeburn  2824:                             $env{'form.crsreq_'.$item.'_limit'} =~ s/\D+//g;
                   2825:                             if ($env{'form.crsreq_'.$item.'_limit'}) {
1.341     raeburn  2826:                                 $newcustom{$item} .= $env{'form.crsreq_'.$item.'_limit'};
                   2827:                             }
1.334     raeburn  2828:                         }
1.341     raeburn  2829:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2830:                                                       \%changeHash,'requestcourses');
1.334     raeburn  2831:                     }
1.275     raeburn  2832:                 }
1.362     raeburn  2833:                 if ($env{'form.customrequestauthor'} == 1) {
                   2834:                     $newcustom{'requestauthor'} = $env{'form.requestauthor'};
                   2835:                     $changed{'requestauthor'} = &tool_admin('requestauthor',
                   2836:                                                     $newcustom{'requestauthor'},
                   2837:                                                     \%changeHash,'requestauthor');
                   2838:                 }
1.275     raeburn  2839:             }
1.334     raeburn  2840:             if ($canmodify_status{'inststatus'}) {
                   2841:                 if (exists($env{'form.inststatus'})) {
                   2842:                     my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2843:                     if (@inststatuses > 0) {
                   2844:                         $changeHash{'inststatus'} = join(',',@inststatuses);
                   2845:                         $changed{'inststatus'} = $changeHash{'inststatus'};
1.306     raeburn  2846:                     }
                   2847:                 }
1.232     raeburn  2848:             }
1.334     raeburn  2849:             if (keys(%changed)) {
                   2850:                 foreach my $item (@userinfo) {
                   2851:                     $changeHash{$item}  = $env{'form.c'.$item};
1.286     raeburn  2852:                 }
1.267     raeburn  2853:                 my $chgresult =
                   2854:                      &Apache::lonnet::put('environment',\%changeHash,
                   2855:                                           $env{'form.ccdomain'},$env{'form.ccuname'});
                   2856:             } 
1.232     raeburn  2857:         }
1.219     raeburn  2858:         $r->print('<br />'.&mt('Home server').': '.$uhome.' '.
                   2859:                   &Apache::lonnet::hostname($uhome));
1.101     albertel 2860:     } elsif (($env{'form.login'} ne 'nochange') &&
                   2861:              ($env{'form.login'} ne ''        )) {
1.27      matthew  2862: 	# Modify user privileges
                   2863:         if (! $amode || ! $genpwd) {
1.193     raeburn  2864: 	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
1.27      matthew  2865: 	    return;
1.20      harris41 2866: 	}
1.395     bisitz   2867: 	# Only allow authentication modification if the person has authority
1.101     albertel 2868: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.20      harris41 2869: 	    $r->print('Modifying authentication: '.
1.31      matthew  2870:                       &Apache::lonnet::modifyuserauth(
1.101     albertel 2871: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
1.21      harris41 2872:                        $amode,$genpwd));
1.102     albertel 2873:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
1.101     albertel 2874: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
1.4       www      2875: 	} else {
1.27      matthew  2876: 	    # Okay, this is a non-fatal error.
1.395     bisitz   2877: 	    $r->print($error.&mt('You do not have the authority to modify this users authentication information.').$end);    
1.27      matthew  2878: 	}
1.28      matthew  2879:     }
1.344     bisitz   2880:     $r->rflush(); # Finish display of header before time consuming actions start
1.367     golterma 2881:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state);
1.28      matthew  2882:     ##
1.375     raeburn  2883:     my (@userroles,%userupdate,$cnum,$cdom,$defaultcredits,%namechanged);
1.213     raeburn  2884:     if ($context eq 'course') {
1.375     raeburn  2885:         ($cnum,$cdom) =
                   2886:             &Apache::lonuserutils::get_course_identity();
1.318     raeburn  2887:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
1.375     raeburn  2888:         if ($showcredits) {
                   2889:            $defaultcredits = &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   2890:         }
1.213     raeburn  2891:     }
1.101     albertel 2892:     if (! $env{'form.makeuser'} ) {
1.28      matthew  2893:         # Check for need to change
                   2894:         my %userenv = &Apache::lonnet::get
1.134     raeburn  2895:             ('environment',['firstname','middlename','lastname','generation',
1.378     raeburn  2896:              'id','permanentemail','portfolioquota','authorquota','inststatus',
                   2897:              'tools.aboutme','tools.blog','tools.webdav','tools.portfolio',
1.361     raeburn  2898:              'requestcourses.official','requestcourses.unofficial',
1.384     raeburn  2899:              'requestcourses.community','requestcourses.textbook',
                   2900:              'reqcrsotherdom.official','reqcrsotherdom.unofficial',
                   2901:              'reqcrsotherdom.community','reqcrsotherdom.textbook',
1.406.2.9! raeburn  2902:              'requestauthor'],
1.160     raeburn  2903:               $env{'form.ccdomain'},$env{'form.ccuname'});
1.28      matthew  2904:         my ($tmp) = keys(%userenv);
                   2905:         if ($tmp =~ /^(con_lost|error)/i) { 
                   2906:             %userenv = ();
                   2907:         }
1.206     raeburn  2908:         my $no_forceid_alert;
                   2909:         # Check to see if user information can be changed
                   2910:         my %domconfig =
                   2911:             &Apache::lonnet::get_dom('configuration',['usermodification'],
                   2912:                                      $env{'form.ccdomain'});
1.213     raeburn  2913:         my @statuses = ('active','future');
                   2914:         my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@statuses,undef,$env{'request.role.domain'});
                   2915:         my ($auname,$audom);
1.220     raeburn  2916:         if ($context eq 'author') {
1.206     raeburn  2917:             $auname = $env{'user.name'};
                   2918:             $audom = $env{'user.domain'};     
                   2919:         }
                   2920:         foreach my $item (keys(%roles)) {
1.220     raeburn  2921:             my ($rolenum,$roledom,$role) = split(/:/,$item,-1);
1.206     raeburn  2922:             if ($context eq 'course') {
                   2923:                 if ($cnum ne '' && $cdom ne '') {
                   2924:                     if ($rolenum eq $cnum && $roledom eq $cdom) {
                   2925:                         if (!grep(/^\Q$role\E$/,@userroles)) {
                   2926:                             push(@userroles,$role);
                   2927:                         }
                   2928:                     }
                   2929:                 }
                   2930:             } elsif ($context eq 'author') {
                   2931:                 if ($rolenum eq $auname && $roledom eq $audom) {
                   2932:                     if (!grep(/^\Q$role\E$/,@userroles)) { 
                   2933:                         push(@userroles,$role);
                   2934:                     }
                   2935:                 }
                   2936:             }
                   2937:         }
1.220     raeburn  2938:         if ($env{'form.action'} eq 'singlestudent') {
                   2939:             if (!grep(/^st$/,@userroles)) {
                   2940:                 push(@userroles,'st');
                   2941:             }
                   2942:         } else {
                   2943:             # Check for course or co-author roles being activated or re-enabled
                   2944:             if ($context eq 'author' || $context eq 'course') {
                   2945:                 foreach my $key (keys(%env)) {
                   2946:                     if ($context eq 'author') {
                   2947:                         if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
                   2948:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2949:                                 push(@userroles,$1);
                   2950:                             }
                   2951:                         } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
                   2952:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2953:                                 push(@userroles,$1);
                   2954:                             }
1.206     raeburn  2955:                         }
1.220     raeburn  2956:                     } elsif ($context eq 'course') {
                   2957:                         if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
                   2958:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2959:                                 push(@userroles,$1);
                   2960:                             }
                   2961:                         } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
                   2962:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2963:                                 push(@userroles,$1);
                   2964:                             }
1.206     raeburn  2965:                         }
                   2966:                     }
                   2967:                 }
                   2968:             }
                   2969:         }
                   2970:         #Check to see if we can change personal data for the user 
                   2971:         my (@mod_disallowed,@longroles);
                   2972:         foreach my $role (@userroles) {
                   2973:             if ($role eq 'cr') {
                   2974:                 push(@longroles,'Custom');
                   2975:             } else {
1.318     raeburn  2976:                 push(@longroles,&Apache::lonnet::plaintext($role,$crstype)); 
1.206     raeburn  2977:             }
                   2978:         }
1.219     raeburn  2979:         my %canmodify = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},\@userinfo,\@userroles);
                   2980:         foreach my $item (@userinfo) {
1.28      matthew  2981:             # Strip leading and trailing whitespace
1.203     raeburn  2982:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
1.219     raeburn  2983:             if (!$canmodify{$item}) {
1.207     raeburn  2984:                 if (defined($env{'form.c'.$item})) {
                   2985:                     if ($env{'form.c'.$item} ne $userenv{$item}) {
                   2986:                         push(@mod_disallowed,$item);
                   2987:                     }
1.206     raeburn  2988:                 }
                   2989:                 $env{'form.c'.$item} = $userenv{$item};
                   2990:             }
1.28      matthew  2991:         }
1.259     bisitz   2992:         # Check to see if we can change the Student/Employee ID
1.196     raeburn  2993:         my $forceid = $env{'form.forceid'};
                   2994:         my $recurseid = $env{'form.recurseid'};
                   2995:         my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
1.203     raeburn  2996:         my %uidhash = &Apache::lonnet::idrget($env{'form.ccdomain'},
                   2997:                                             $env{'form.ccuname'});
                   2998:         if (($uidhash{$env{'form.ccuname'}}) && 
                   2999:             ($uidhash{$env{'form.ccuname'}}!~/error\:/) && 
                   3000:             (!$forceid)) {
                   3001:             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
                   3002:                 $env{'form.cid'} = $userenv{'id'};
1.293     bisitz   3003:                 $no_forceid_alert = &mt('New student/employee ID does not match existing ID for this user.')
1.259     bisitz   3004:                                    .'<br />'
                   3005:                                    .&mt("Change is not permitted without checking the 'Force ID change' checkbox on the previous page.")
                   3006:                                    .'<br />'."\n";
1.203     raeburn  3007:             }
                   3008:         }
                   3009:         if ($env{'form.cid'} ne $userenv{'id'}) {
1.196     raeburn  3010:             my $checkhash;
                   3011:             my $checks = { 'id' => 1 };
                   3012:             $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
                   3013:                    { 'newuser' => $newuser,
                   3014:                      'id'  => $env{'form.cid'}, 
                   3015:                    };
                   3016:             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   3017:                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
                   3018:             if (ref($alerts{'id'}) eq 'HASH') {
                   3019:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
1.203     raeburn  3020:                    $env{'form.cid'} = $userenv{'id'};
1.196     raeburn  3021:                 }
                   3022:             }
                   3023:         }
1.378     raeburn  3024:         my (%quotachanged,%oldquota,%newquota,%olddefquota,%newdefquota, 
                   3025:             $oldinststatus,$newinststatus,%oldisdefault,%newisdefault,%oldsettings,
1.339     raeburn  3026:             %oldsettingstext,%newsettings,%newsettingstext,@disporder,
1.378     raeburn  3027:             %oldsettingstatus,%newsettingstatus);
1.334     raeburn  3028:         @disporder = ('inststatus');
                   3029:         if ($env{'request.role.domain'} eq $env{'form.ccdomain'}) {
1.362     raeburn  3030:             push(@disporder,'requestcourses','requestauthor');
1.334     raeburn  3031:         } else {
                   3032:             push(@disporder,'reqcrsotherdom');
                   3033:         }
                   3034:         push(@disporder,('quota','tools'));
1.338     raeburn  3035:         $oldinststatus = $userenv{'inststatus'};
1.378     raeburn  3036:         foreach my $name ('portfolio','author') {
                   3037:             ($olddefquota{$name},$oldsettingstatus{$name}) = 
                   3038:                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$oldinststatus,$name);
                   3039:             ($newdefquota{$name},$newsettingstatus{$name}) = ($olddefquota{$name},$oldsettingstatus{$name});
                   3040:         }
1.334     raeburn  3041:         my %canshow;
1.220     raeburn  3042:         if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
1.334     raeburn  3043:             $canshow{'quota'} = 1;
1.220     raeburn  3044:         }
1.267     raeburn  3045:         if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
1.334     raeburn  3046:             $canshow{'tools'} = 1;
1.267     raeburn  3047:         }
1.275     raeburn  3048:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
1.334     raeburn  3049:             $canshow{'requestcourses'} = 1;
1.300     raeburn  3050:         } elsif (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.334     raeburn  3051:             $canshow{'reqcrsotherdom'} = 1;
1.275     raeburn  3052:         }
1.286     raeburn  3053:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.334     raeburn  3054:             $canshow{'inststatus'} = 1;
1.286     raeburn  3055:         }
1.362     raeburn  3056:         if (&Apache::lonnet::allowed('cau',$env{'form.ccdomain'})) {
                   3057:             $canshow{'requestauthor'} = 1;
                   3058:         }
1.267     raeburn  3059:         my (%changeHash,%changed);
1.286     raeburn  3060:         if ($oldinststatus eq '') {
1.334     raeburn  3061:             $oldsettings{'inststatus'} = $othertitle; 
1.286     raeburn  3062:         } else {
                   3063:             if (ref($usertypes) eq 'HASH') {
1.334     raeburn  3064:                 $oldsettings{'inststatus'} = join(', ',map{ $usertypes->{ &unescape($_) }; } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  3065:             } else {
1.334     raeburn  3066:                 $oldsettings{'inststatus'} = join(', ',map{ &unescape($_); } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  3067:             }
                   3068:         }
                   3069:         $changeHash{'inststatus'} = $userenv{'inststatus'};
1.334     raeburn  3070:         if ($canmodify_status{'inststatus'}) {
                   3071:             $canshow{'inststatus'} = 1;
1.286     raeburn  3072:             if (exists($env{'form.inststatus'})) {
                   3073:                 my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   3074:                 if (@inststatuses > 0) {
                   3075:                     $newinststatus = join(':',map { &escape($_); } @inststatuses);
                   3076:                     $changeHash{'inststatus'} = $newinststatus;
                   3077:                     if ($newinststatus ne $oldinststatus) {
                   3078:                         $changed{'inststatus'} = $newinststatus;
1.378     raeburn  3079:                         foreach my $name ('portfolio','author') {
                   3080:                             ($newdefquota{$name},$newsettingstatus{$name}) =
                   3081:                                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   3082:                         }
1.286     raeburn  3083:                     }
                   3084:                     if (ref($usertypes) eq 'HASH') {
1.334     raeburn  3085:                         $newsettings{'inststatus'} = join(', ',map{ $usertypes->{$_}; } (@inststatuses)); 
1.286     raeburn  3086:                     } else {
1.337     raeburn  3087:                         $newsettings{'inststatus'} = join(', ',@inststatuses);
1.286     raeburn  3088:                     }
1.334     raeburn  3089:                 }
                   3090:             } else {
                   3091:                 $newinststatus = '';
                   3092:                 $changeHash{'inststatus'} = $newinststatus;
                   3093:                 $newsettings{'inststatus'} = $othertitle;
                   3094:                 if ($newinststatus ne $oldinststatus) {
                   3095:                     $changed{'inststatus'} = $changeHash{'inststatus'};
1.378     raeburn  3096:                     foreach my $name ('portfolio','author') {
                   3097:                         ($newdefquota{$name},$newsettingstatus{$name}) =
                   3098:                             &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   3099:                     }
1.286     raeburn  3100:                 }
                   3101:             }
1.334     raeburn  3102:         } elsif ($context ne 'selfcreate') {
                   3103:             $canshow{'inststatus'} = 1;
1.337     raeburn  3104:             $newsettings{'inststatus'} = $oldsettings{'inststatus'};
1.286     raeburn  3105:         }
1.378     raeburn  3106:         foreach my $name ('portfolio','author') {
                   3107:             $changeHash{$name.'quota'} = $userenv{$name.'quota'};
                   3108:         }
1.334     raeburn  3109:         if ($context eq 'domain') {
1.378     raeburn  3110:             foreach my $name ('portfolio','author') {
                   3111:                 if ($userenv{$name.'quota'} ne '') {
                   3112:                     $oldquota{$name} = $userenv{$name.'quota'};
                   3113:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   3114:                         if ($env{'form.'.$name.'quota'} eq '') {
                   3115:                             $newquota{$name} = 0;
                   3116:                         } else {
                   3117:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   3118:                             $newquota{$name} =~ s/[^\d\.]//g;
                   3119:                         }
                   3120:                         if ($newquota{$name} != $oldquota{$name}) {
                   3121:                             if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   3122:                                 $changed{$name.'quota'} = 1;
                   3123:                             }
                   3124:                         }
1.334     raeburn  3125:                     } else {
1.378     raeburn  3126:                         if (&quota_admin('',\%changeHash,$name)) {
                   3127:                             $changed{$name.'quota'} = 1;
                   3128:                             $newquota{$name} = $newdefquota{$name};
                   3129:                             $newisdefault{$name} = 1;
                   3130:                         }
1.334     raeburn  3131:                     }
1.149     raeburn  3132:                 } else {
1.378     raeburn  3133:                     $oldisdefault{$name} = 1;
                   3134:                     $oldquota{$name} = $olddefquota{$name};
                   3135:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   3136:                         if ($env{'form.'.$name.'quota'} eq '') {
                   3137:                             $newquota{$name} = 0;
                   3138:                         } else {
                   3139:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   3140:                             $newquota{$name} =~ s/[^\d\.]//g;
                   3141:                         }
                   3142:                         if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   3143:                             $changed{$name.'quota'} = 1;
                   3144:                         }
1.334     raeburn  3145:                     } else {
1.378     raeburn  3146:                         $newquota{$name} = $newdefquota{$name};
                   3147:                         $newisdefault{$name} = 1;
1.334     raeburn  3148:                     }
1.378     raeburn  3149:                 }
                   3150:                 if ($oldisdefault{$name}) {
                   3151:                     $oldsettingstext{'quota'}{$name} = &get_defaultquota_text($oldsettingstatus{$name});
1.383     raeburn  3152:                 }  else {
                   3153:                     $oldsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$oldquota{$name});
1.378     raeburn  3154:                 }
                   3155:                 if ($newisdefault{$name}) {
                   3156:                     $newsettingstext{'quota'}{$name} = &get_defaultquota_text($newsettingstatus{$name});
1.383     raeburn  3157:                 } else {
                   3158:                     $newsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$newquota{$name});
1.134     raeburn  3159:                 }
                   3160:             }
1.334     raeburn  3161:             &tool_changes('tools',\@usertools,\%oldsettings,\%oldsettingstext,\%userenv,
                   3162:                           \%changeHash,\%changed,\%newsettings,\%newsettingstext);
                   3163:             if ($env{'form.ccdomain'} eq $env{'request.role.domain'}) {
                   3164:                 &tool_changes('requestcourses',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   3165:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.384     raeburn  3166:                 &tool_changes('requestauthor',\@requestauthor,\%oldsettings,\%oldsettingstext,
                   3167:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  3168:             } else {
1.334     raeburn  3169:                 &tool_changes('reqcrsotherdom',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   3170:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  3171:             }
                   3172:         }
1.334     raeburn  3173:         foreach my $item (@userinfo) {
                   3174:             if ($env{'form.c'.$item} ne $userenv{$item}) {
                   3175:                 $namechanged{$item} = 1;
                   3176:             }
1.204     raeburn  3177:         }
1.378     raeburn  3178:         foreach my $name ('portfolio','author') {
1.390     bisitz   3179:             $oldsettings{'quota'}{$name} = &mt('[_1] MB',$oldquota{$name});
                   3180:             $newsettings{'quota'}{$name} = &mt('[_1] MB',$newquota{$name});
1.378     raeburn  3181:         }
1.334     raeburn  3182:         if ((keys(%namechanged) > 0) || (keys(%changed) > 0)) {
1.267     raeburn  3183:             my ($chgresult,$namechgresult);
                   3184:             if (keys(%changed) > 0) {
                   3185:                 $chgresult = 
1.204     raeburn  3186:                     &Apache::lonnet::put('environment',\%changeHash,
                   3187:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
1.267     raeburn  3188:                 if ($chgresult eq 'ok') {
                   3189:                     if (($env{'user.name'} eq $env{'form.ccuname'}) &&
                   3190:                         ($env{'user.domain'} eq $env{'form.ccdomain'})) {
1.270     raeburn  3191:                         my %newenvhash;
                   3192:                         foreach my $key (keys(%changed)) {
1.299     raeburn  3193:                             if (($key eq 'official') || ($key eq 'unofficial')
1.403     raeburn  3194:                                 || ($key eq 'community') || ($key eq 'textbook')) {
1.279     raeburn  3195:                                 $newenvhash{'environment.requestcourses.'.$key} =
                   3196:                                     $changeHash{'requestcourses.'.$key};
1.362     raeburn  3197:                                 if ($changeHash{'requestcourses.'.$key}) {
1.332     raeburn  3198:                                     $newenvhash{'environment.canrequest.'.$key} = 1;
1.279     raeburn  3199:                                 } else {
                   3200:                                     $newenvhash{'environment.canrequest.'.$key} =
                   3201:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3202:                                             $key,'reload','requestcourses');
                   3203:                                 }
1.362     raeburn  3204:                             } elsif ($key eq 'requestauthor') {
                   3205:                                 $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3206:                                 if ($changeHash{$key}) {
                   3207:                                     $newenvhash{'environment.canrequest.author'} = 1;
                   3208:                                 } else {
                   3209:                                     $newenvhash{'environment.canrequest.author'} =
                   3210:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3211:                                             $key,'reload','requestauthor');
                   3212:                                 }
1.275     raeburn  3213:                             } elsif ($key ne 'quota') {
1.270     raeburn  3214:                                 $newenvhash{'environment.tools.'.$key} = 
                   3215:                                     $changeHash{'tools.'.$key};
1.279     raeburn  3216:                                 if ($changeHash{'tools.'.$key} ne '') {
                   3217:                                     $newenvhash{'environment.availabletools.'.$key} =
                   3218:                                         $changeHash{'tools.'.$key};
                   3219:                                 } else {
                   3220:                                     $newenvhash{'environment.availabletools.'.$key} =
1.367     golterma 3221:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3222:           $key,'reload','tools');
1.279     raeburn  3223:                                 }
1.270     raeburn  3224:                             }
                   3225:                         }
1.271     raeburn  3226:                         if (keys(%newenvhash)) {
                   3227:                             &Apache::lonnet::appenv(\%newenvhash);
                   3228:                         }
1.267     raeburn  3229:                     }
                   3230:                 }
1.204     raeburn  3231:             }
1.334     raeburn  3232:             if (keys(%namechanged) > 0) {
1.337     raeburn  3233:                 foreach my $field (@userinfo) {
                   3234:                     $changeHash{$field}  = $env{'form.c'.$field};
                   3235:                 }
                   3236: # Make the change
1.204     raeburn  3237:                 $namechgresult =
                   3238:                     &Apache::lonnet::modifyuser($env{'form.ccdomain'},
                   3239:                         $env{'form.ccuname'},$changeHash{'id'},undef,undef,
                   3240:                         $changeHash{'firstname'},$changeHash{'middlename'},
                   3241:                         $changeHash{'lastname'},$changeHash{'generation'},
1.337     raeburn  3242:                         $changeHash{'id'},undef,$changeHash{'permanentemail'},undef,\@userinfo);
1.220     raeburn  3243:                 %userupdate = (
                   3244:                                lastname   => $env{'form.clastname'},
                   3245:                                middlename => $env{'form.cmiddlename'},
                   3246:                                firstname  => $env{'form.cfirstname'},
                   3247:                                generation => $env{'form.cgeneration'},
                   3248:                                id         => $env{'form.cid'},
                   3249:                              );
1.204     raeburn  3250:             }
1.334     raeburn  3251:             if (((keys(%namechanged) > 0) && $namechgresult eq 'ok') || 
1.267     raeburn  3252:                 ((keys(%changed) > 0) && $chgresult eq 'ok')) {
1.28      matthew  3253:             # Tell the user we changed the name
1.334     raeburn  3254:                 &display_userinfo($r,1,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3255:                                   \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,
1.334     raeburn  3256:                                   \%oldsettings, \%oldsettingstext,\%newsettings,
                   3257:                                   \%newsettingstext);
1.203     raeburn  3258:                 if ($env{'form.cid'} ne $userenv{'id'}) {
                   3259:                     &Apache::lonnet::idput($env{'form.ccdomain'},
1.406.2.5  raeburn  3260:                          {$env{'form.ccuname'} => $env{'form.cid'}});
1.203     raeburn  3261:                     if (($recurseid) &&
                   3262:                         (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
                   3263:                         my $idresult = 
                   3264:                             &Apache::lonuserutils::propagate_id_change(
                   3265:                                 $env{'form.ccuname'},$env{'form.ccdomain'},
                   3266:                                 \%userupdate);
                   3267:                         $r->print('<br />'.$idresult.'<br />');
                   3268:                     }
1.196     raeburn  3269:                 }
1.149     raeburn  3270:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
                   3271:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
                   3272:                     my %newenvhash;
                   3273:                     foreach my $key (keys(%changeHash)) {
                   3274:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3275:                     }
1.238     raeburn  3276:                     &Apache::lonnet::appenv(\%newenvhash);
1.149     raeburn  3277:                 }
1.28      matthew  3278:             } else { # error occurred
1.389     bisitz   3279:                 $r->print(
                   3280:                     '<p class="LC_error">'
                   3281:                    .&mt('Unable to successfully change environment for [_1] in domain [_2].',
                   3282:                             '"'.$env{'form.ccuname'}.'"',
                   3283:                             '"'.$env{'form.ccdomain'}.'"')
                   3284:                    .'</p>');
1.28      matthew  3285:             }
1.334     raeburn  3286:         } else { # End of if ($env ... ) logic
1.275     raeburn  3287:             # They did not want to change the users name, quota, tool availability,
                   3288:             # or ability to request creation of courses, 
1.267     raeburn  3289:             # but we can still tell them what the name and quota and availabilities are  
1.334     raeburn  3290:             &display_userinfo($r,undef,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3291:                               \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,\%oldsettings,
1.334     raeburn  3292:                               \%oldsettingstext,\%newsettings,\%newsettingstext);
1.28      matthew  3293:         }
1.206     raeburn  3294:         if (@mod_disallowed) {
                   3295:             my ($rolestr,$contextname);
                   3296:             if (@longroles > 0) {
                   3297:                 $rolestr = join(', ',@longroles);
                   3298:             } else {
                   3299:                 $rolestr = &mt('No roles');
                   3300:             }
                   3301:             if ($context eq 'course') {
1.399     bisitz   3302:                 $contextname = 'course';
1.206     raeburn  3303:             } elsif ($context eq 'author') {
1.399     bisitz   3304:                 $contextname = 'co-author';
1.206     raeburn  3305:             }
                   3306:             $r->print(&mt('The following fields were not updated: ').'<ul>');
                   3307:             my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   3308:             foreach my $field (@mod_disallowed) {
                   3309:                 $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
                   3310:             }
1.207     raeburn  3311:             $r->print('</ul>');
                   3312:             if (@mod_disallowed == 1) {
1.399     bisitz   3313:                 $r->print(&mt("You do not have the authority to change this field given the user's current set of active/future $contextname roles:"));
1.207     raeburn  3314:             } else {
1.399     bisitz   3315:                 $r->print(&mt("You do not have the authority to change these fields given the user's current set of active/future $contextname roles:"));
1.207     raeburn  3316:             }
1.292     bisitz   3317:             my $helplink = 'javascript:helpMenu('."'display'".')';
                   3318:             $r->print('<span class="LC_cusr_emph">'.$rolestr.'</span><br />'
                   3319:                      .&mt('Please contact your [_1]helpdesk[_2] for more information.'
                   3320:                          ,'<a href="'.$helplink.'">','</a>')
                   3321:                       .'<br />');
1.206     raeburn  3322:         }
1.259     bisitz   3323:         $r->print('<span class="LC_warning">'
                   3324:                   .$no_forceid_alert
                   3325:                   .&Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules)
                   3326:                   .'</span>');
1.4       www      3327:     }
1.367     golterma 3328:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.220     raeburn  3329:     if ($env{'form.action'} eq 'singlestudent') {
1.375     raeburn  3330:         &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context,
                   3331:                                $crstype,$showcredits,$defaultcredits);
1.386     bisitz   3332:         my $linktext = ($crstype eq 'Community' ?
                   3333:             &mt('Enroll Another Member') : &mt('Enroll Another Student'));
                   3334:         $r->print(
                   3335:             &Apache::lonhtmlcommon::actionbox([
                   3336:                 '<a href="javascript:backPage(document.userupdate)">'
                   3337:                .($crstype eq 'Community' ? 
                   3338:                     &mt('Enroll Another Member') : &mt('Enroll Another Student'))
                   3339:                .'</a>']));
1.220     raeburn  3340:     } else {
1.375     raeburn  3341:         my @rolechanges = &update_roles($r,$context,$showcredits);
1.334     raeburn  3342:         if (keys(%namechanged) > 0) {
1.220     raeburn  3343:             if ($context eq 'course') {
                   3344:                 if (@userroles > 0) {
1.225     raeburn  3345:                     if ((@rolechanges == 0) || 
                   3346:                         (!(grep(/^st$/,@rolechanges)))) {
                   3347:                         if (grep(/^st$/,@userroles)) {
                   3348:                             my $classlistupdated =
                   3349:                                 &Apache::lonuserutils::update_classlist($cdom,
1.220     raeburn  3350:                                               $cnum,$env{'form.ccdomain'},
                   3351:                                        $env{'form.ccuname'},\%userupdate);
1.225     raeburn  3352:                         }
1.220     raeburn  3353:                     }
                   3354:                 }
                   3355:             }
                   3356:         }
1.226     raeburn  3357:         my $userinfo = &Apache::loncommon::plainname($env{'form.ccuname'},
1.233     raeburn  3358:                                                      $env{'form.ccdomain'});
                   3359:         if ($env{'form.popup'}) {
                   3360:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   3361:         } else {
1.367     golterma 3362:             $r->print('<br />'.&Apache::lonhtmlcommon::actionbox(['<a href="javascript:backPage(document.userupdate,'."'$env{'form.prevphase'}','modify'".')">'
                   3363:                      .&mt('Modify this user: [_1]','<span class="LC_cusr_emph">'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.' ('.$userinfo.')</span>').'</a>',
                   3364:                      '<a href="javascript:backPage(document.userupdate)">'.&mt('Create/Modify Another User').'</a>']));
1.233     raeburn  3365:         }
1.220     raeburn  3366:     }
                   3367: }
                   3368: 
1.334     raeburn  3369: sub display_userinfo {
1.362     raeburn  3370:     my ($r,$changed,$order,$canshow,$requestcourses,$usertools,$requestauthor,
                   3371:         $userenv,$changedhash,$namechangedhash,$oldsetting,$oldsettingtext,
1.334     raeburn  3372:         $newsetting,$newsettingtext) = @_;
                   3373:     return unless (ref($order) eq 'ARRAY' &&
                   3374:                    ref($canshow) eq 'HASH' && 
                   3375:                    ref($requestcourses) eq 'ARRAY' && 
1.362     raeburn  3376:                    ref($requestauthor) eq 'ARRAY' &&
1.334     raeburn  3377:                    ref($usertools) eq 'ARRAY' && 
                   3378:                    ref($userenv) eq 'HASH' &&
                   3379:                    ref($changedhash) eq 'HASH' &&
                   3380:                    ref($oldsetting) eq 'HASH' &&
                   3381:                    ref($oldsettingtext) eq 'HASH' &&
                   3382:                    ref($newsetting) eq 'HASH' &&
                   3383:                    ref($newsettingtext) eq 'HASH');
                   3384:     my %lt=&Apache::lonlocal::texthash(
1.372     raeburn  3385:          'ui'             => 'User Information',
1.334     raeburn  3386:          'uic'            => 'User Information Changed',
                   3387:          'firstname'      => 'First Name',
                   3388:          'middlename'     => 'Middle Name',
                   3389:          'lastname'       => 'Last Name',
                   3390:          'generation'     => 'Generation',
                   3391:          'id'             => 'Student/Employee ID',
                   3392:          'permanentemail' => 'Permanent e-mail address',
1.378     raeburn  3393:          'portfolioquota' => 'Disk space allocated to portfolio files',
1.385     bisitz   3394:          'authorquota'    => 'Disk space allocated to Authoring Space',
1.334     raeburn  3395:          'blog'           => 'Blog Availability',
1.361     raeburn  3396:          'webdav'         => 'WebDAV Availability',
1.334     raeburn  3397:          'aboutme'        => 'Personal Information Page Availability',
                   3398:          'portfolio'      => 'Portfolio Availability',
                   3399:          'official'       => 'Can Request Official Courses',
                   3400:          'unofficial'     => 'Can Request Unofficial Courses',
                   3401:          'community'      => 'Can Request Communities',
1.384     raeburn  3402:          'textbook'       => 'Can Request Textbook Courses',
1.362     raeburn  3403:          'requestauthor'  => 'Can Request Author Role',
1.334     raeburn  3404:          'inststatus'     => "Affiliation",
                   3405:          'prvs'           => 'Previous Value:',
                   3406:          'chto'           => 'Changed To:'
                   3407:     );
                   3408:     if ($changed) {
1.372     raeburn  3409:         $r->print('<h3>'.$lt{'uic'}.'</h3>'.
1.367     golterma 3410:                 &Apache::loncommon::start_data_table().
                   3411:                 &Apache::loncommon::start_data_table_header_row());
1.334     raeburn  3412:         $r->print("<th>&nbsp;</th>\n");
1.367     golterma 3413:         $r->print('<th><b>'.$lt{'prvs'}.'</b></th>');
                   3414:         $r->print('<th><span class="LC_nobreak"><b>'.$lt{'chto'}.'</b></span></th>');
                   3415:         $r->print(&Apache::loncommon::end_data_table_header_row());
                   3416:         my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
                   3417: 
1.334     raeburn  3418:         foreach my $item (@userinfo) {
                   3419:             my $value = $env{'form.c'.$item};
1.367     golterma 3420:             #show changes only:
1.383     raeburn  3421:             unless ($value eq $userenv->{$item}){
1.367     golterma 3422:                 $r->print(&Apache::loncommon::start_data_table_row());
                   3423:                 $r->print("<td>$lt{$item}</td>\n");
1.383     raeburn  3424:                 $r->print("<td>".$userenv->{$item}."</td>\n");
1.367     golterma 3425:                 $r->print("<td>$value </td>\n");
                   3426:                 $r->print(&Apache::loncommon::end_data_table_row());
1.334     raeburn  3427:             }
                   3428:         }
                   3429:         foreach my $entry (@{$order}) {
1.383     raeburn  3430:             if ($canshow->{$entry}) {
                   3431:                 if (($entry eq 'requestcourses') || ($entry eq 'reqcrsotherdom') || ($entry eq 'requestauthor')) {
                   3432:                     my @items;
                   3433:                     if ($entry eq 'requestauthor') {
                   3434:                         @items = ($entry);
                   3435:                     } else {
                   3436:                         @items = @{$requestcourses};
1.384     raeburn  3437:                     }
1.383     raeburn  3438:                     foreach my $item (@items) {
                   3439:                         if (($newsetting->{$item} ne $oldsetting->{$item}) || 
                   3440:                             ($newsettingtext->{$item} ne $oldsettingtext->{$item})) {
                   3441:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");  
                   3442:                             $r->print("<td>$lt{$item}</td>\n");
                   3443:                             $r->print("<td>".$oldsetting->{$item});
                   3444:                             if ($oldsettingtext->{$item}) {
                   3445:                                 if ($oldsetting->{$item}) {
                   3446:                                     $r->print(' -- ');
                   3447:                                 }
                   3448:                                 $r->print($oldsettingtext->{$item});
                   3449:                             }
                   3450:                             $r->print("</td>\n");
                   3451:                             $r->print("<td>".$newsetting->{$item});
                   3452:                             if ($newsettingtext->{$item}) {
                   3453:                                 if ($newsetting->{$item}) {
                   3454:                                     $r->print(' -- ');
                   3455:                                 }
                   3456:                                 $r->print($newsettingtext->{$item});
                   3457:                             }
                   3458:                             $r->print("</td>\n");
                   3459:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3460:                         }
                   3461:                     }
                   3462:                 } elsif ($entry eq 'tools') {
                   3463:                     foreach my $item (@{$usertools}) {
1.383     raeburn  3464:                         if ($newsetting->{$item} ne $oldsetting->{$item}) {
                   3465:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3466:                             $r->print("<td>$lt{$item}</td>\n");
                   3467:                             $r->print("<td>".$oldsetting->{$item}.' '.$oldsettingtext->{$item}."</td>\n");
                   3468:                             $r->print("<td>".$newsetting->{$item}.' '.$newsettingtext->{$item}."</td>\n");
                   3469:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3470:                         }
                   3471:                     }
1.378     raeburn  3472:                 } elsif ($entry eq 'quota') {
                   3473:                     if ((ref($oldsetting->{$entry}) eq 'HASH') && (ref($oldsettingtext->{$entry}) eq 'HASH') &&
                   3474:                         (ref($newsetting->{$entry}) eq 'HASH') && (ref($newsettingtext->{$entry}) eq 'HASH')) {
                   3475:                         foreach my $name ('portfolio','author') {
1.383     raeburn  3476:                             if ($newsetting->{$entry}->{$name} ne $oldsetting->{$entry}->{$name}) {
                   3477:                                 $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3478:                                 $r->print("<td>$lt{$name.$entry}</td>\n");
                   3479:                                 $r->print("<td>".$oldsettingtext->{$entry}->{$name}."</td>\n");
                   3480:                                 $r->print("<td>".$newsettingtext->{$entry}->{$name}."</td>\n");
                   3481:                                 $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.378     raeburn  3482:                             }
                   3483:                         }
                   3484:                     }
1.334     raeburn  3485:                 } else {
1.383     raeburn  3486:                     if ($newsetting->{$entry} ne $oldsetting->{$entry}) {
                   3487:                         $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3488:                         $r->print("<td>$lt{$entry}</td>\n");
                   3489:                         $r->print("<td>".$oldsetting->{$entry}.' '.$oldsettingtext->{$entry}."</td>\n");
                   3490:                         $r->print("<td>".$newsetting->{$entry}.' '.$newsettingtext->{$entry}."</td>\n");
                   3491:                         $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3492:                     }
                   3493:                 }
                   3494:             }
                   3495:         }
1.367     golterma 3496:         $r->print(&Apache::loncommon::end_data_table().'<br />');
1.372     raeburn  3497:     } else {
                   3498:         $r->print('<h3>'.$lt{'ui'}.'</h3>'.
                   3499:                   '<p>'.&mt('No changes made to user information').'</p>');
1.334     raeburn  3500:     }
                   3501:     return;
                   3502: }
                   3503: 
1.275     raeburn  3504: sub tool_changes {
                   3505:     my ($context,$usertools,$oldaccess,$oldaccesstext,$userenv,$changeHash,
                   3506:         $changed,$newaccess,$newaccesstext) = @_;
                   3507:     if (!((ref($usertools) eq 'ARRAY') && (ref($oldaccess) eq 'HASH') &&
                   3508:           (ref($oldaccesstext) eq 'HASH') && (ref($userenv) eq 'HASH') &&
                   3509:           (ref($changeHash) eq 'HASH') && (ref($changed) eq 'HASH') &&
                   3510:           (ref($newaccess) eq 'HASH') && (ref($newaccesstext) eq 'HASH'))) {
                   3511:         return;
                   3512:     }
1.383     raeburn  3513:     my %reqdisplay = &requestchange_display();
1.300     raeburn  3514:     if ($context eq 'reqcrsotherdom') {
1.309     raeburn  3515:         my @options = ('approval','validate','autolimit');
1.306     raeburn  3516:         my $optregex = join('|',@options);
1.300     raeburn  3517:         my $cdom = $env{'request.role.domain'};
                   3518:         foreach my $tool (@{$usertools}) {
1.383     raeburn  3519:             $oldaccesstext->{$tool} = &mt("availability set to 'off'");
1.314     raeburn  3520:             $newaccesstext->{$tool} = $oldaccesstext->{$tool};
1.300     raeburn  3521:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
1.383     raeburn  3522:             my ($newop,$limit);
1.314     raeburn  3523:             if ($env{'form.'.$context.'_'.$tool}) {
                   3524:                 $newop = $env{'form.'.$context.'_'.$tool};
                   3525:                 if ($newop eq 'autolimit') {
1.383     raeburn  3526:                     $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
1.314     raeburn  3527:                     $limit =~ s/\D+//g;
                   3528:                     $newop .= '='.$limit;
                   3529:                 }
                   3530:             }
1.300     raeburn  3531:             if ($userenv->{$context.'.'.$tool} eq '') {
1.314     raeburn  3532:                 if ($newop) {
                   3533:                     $changed->{$tool}=&tool_admin($tool,$cdom.':'.$newop,
1.300     raeburn  3534:                                                   $changeHash,$context);
                   3535:                     if ($changed->{$tool}) {
1.383     raeburn  3536:                         if ($newop =~ /^autolimit/) {
                   3537:                             if ($limit) {
                   3538:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3539:                             } else {
                   3540:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3541:                             }
                   3542:                         } else {
                   3543:                             $newaccesstext->{$tool} = $reqdisplay{$newop};
                   3544:                         }
1.300     raeburn  3545:                     } else {
                   3546:                         $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3547:                     }
                   3548:                 }
                   3549:             } else {
                   3550:                 my @curr = split(',',$userenv->{$context.'.'.$tool});
                   3551:                 my @new;
                   3552:                 my $changedoms;
1.314     raeburn  3553:                 foreach my $req (@curr) {
                   3554:                     if ($req =~ /^\Q$cdom\E\:($optregex\=?\d*)$/) {
                   3555:                         my $oldop = $1;
1.383     raeburn  3556:                         if ($oldop =~ /^autolimit=(\d*)/) {
                   3557:                             my $limit = $1;
                   3558:                             if ($limit) {
                   3559:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3560:                             } else {
                   3561:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3562:                             }
                   3563:                         } else {
                   3564:                             $oldaccesstext->{$tool} = $reqdisplay{$oldop};
                   3565:                         }
1.314     raeburn  3566:                         if ($oldop ne $newop) {
                   3567:                             $changedoms = 1;
                   3568:                             foreach my $item (@curr) {
                   3569:                                 my ($reqdom,$option) = split(':',$item);
                   3570:                                 unless ($reqdom eq $cdom) {
                   3571:                                     push(@new,$item);
                   3572:                                 }
                   3573:                             }
                   3574:                             if ($newop) {
                   3575:                                 push(@new,$cdom.':'.$newop);
1.300     raeburn  3576:                             }
1.314     raeburn  3577:                             @new = sort(@new);
1.300     raeburn  3578:                         }
1.314     raeburn  3579:                         last;
1.300     raeburn  3580:                     }
1.314     raeburn  3581:                 }
                   3582:                 if ((!$changedoms) && ($newop)) {
1.300     raeburn  3583:                     $changedoms = 1;
1.306     raeburn  3584:                     @new = sort(@curr,$cdom.':'.$newop);
1.300     raeburn  3585:                 }
                   3586:                 if ($changedoms) {
1.314     raeburn  3587:                     my $newdomstr;
1.300     raeburn  3588:                     if (@new) {
                   3589:                         $newdomstr = join(',',@new);
                   3590:                     }
                   3591:                     $changed->{$tool}=&tool_admin($tool,$newdomstr,$changeHash,
                   3592:                                                   $context);
                   3593:                     if ($changed->{$tool}) {
                   3594:                         if ($env{'form.'.$context.'_'.$tool}) {
1.306     raeburn  3595:                             if ($env{'form.'.$context.'_'.$tool} eq 'autolimit') {
1.314     raeburn  3596:                                 my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3597:                                 $limit =~ s/\D+//g;
                   3598:                                 if ($limit) {
1.383     raeburn  3599:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
1.314     raeburn  3600:                                 } else {
1.383     raeburn  3601:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
1.306     raeburn  3602:                                 }
1.314     raeburn  3603:                             } else {
1.306     raeburn  3604:                                 $newaccesstext->{$tool} = $reqdisplay{$env{'form.'.$context.'_'.$tool}};
                   3605:                             }
1.300     raeburn  3606:                         } else {
1.383     raeburn  3607:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
1.300     raeburn  3608:                         }
                   3609:                     }
                   3610:                 }
                   3611:             }
                   3612:         }
                   3613:         return;
                   3614:     }
1.275     raeburn  3615:     foreach my $tool (@{$usertools}) {
1.383     raeburn  3616:         my ($newval,$limit,$envkey);
1.362     raeburn  3617:         $envkey = $context.'.'.$tool;
1.306     raeburn  3618:         if ($context eq 'requestcourses') {
                   3619:             $newval = $env{'form.crsreq_'.$tool};
                   3620:             if ($newval eq 'autolimit') {
1.383     raeburn  3621:                 $limit = $env{'form.crsreq_'.$tool.'_limit'};
                   3622:                 $limit =~ s/\D+//g;
                   3623:                 $newval .= '='.$limit;
1.306     raeburn  3624:             }
1.362     raeburn  3625:         } elsif ($context eq 'requestauthor') {
                   3626:             $newval = $env{'form.'.$context};
                   3627:             $envkey = $context;
1.314     raeburn  3628:         } else {
1.306     raeburn  3629:             $newval = $env{'form.'.$context.'_'.$tool};
                   3630:         }
1.362     raeburn  3631:         if ($userenv->{$envkey} ne '') {
1.275     raeburn  3632:             $oldaccess->{$tool} = &mt('custom');
1.383     raeburn  3633:             if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3634:                 if ($userenv->{$envkey} =~ /^autolimit=(\d*)$/) {
                   3635:                     my $currlimit = $1;
                   3636:                     if ($currlimit eq '') {
                   3637:                         $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3638:                     } else {
                   3639:                         $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$currlimit);
                   3640:                     }
                   3641:                 } elsif ($userenv->{$envkey}) {
                   3642:                     $oldaccesstext->{$tool} = $reqdisplay{$userenv->{$envkey}};
                   3643:                 } else {
                   3644:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3645:                 }
1.275     raeburn  3646:             } else {
1.383     raeburn  3647:                 if ($userenv->{$envkey}) {
                   3648:                     $oldaccesstext->{$tool} = &mt("availability set to 'on'");
                   3649:                 } else {
                   3650:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3651:                 }
1.275     raeburn  3652:             }
1.362     raeburn  3653:             $changeHash->{$envkey} = $userenv->{$envkey};
1.275     raeburn  3654:             if ($env{'form.custom'.$tool} == 1) {
1.362     raeburn  3655:                 if ($newval ne $userenv->{$envkey}) {
1.306     raeburn  3656:                     $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3657:                                                     $context);
1.275     raeburn  3658:                     if ($changed->{$tool}) {
                   3659:                         $newaccess->{$tool} = &mt('custom');
1.383     raeburn  3660:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3661:                             if ($newval =~ /^autolimit/) {
                   3662:                                 if ($limit) {
                   3663:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3664:                                 } else {
                   3665:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3666:                                 }
                   3667:                             } elsif ($newval) {
                   3668:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3669:                             } else {
                   3670:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3671:                             }
1.275     raeburn  3672:                         } else {
1.383     raeburn  3673:                             if ($newval) {
                   3674:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3675:                             } else {
                   3676:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3677:                             }
1.275     raeburn  3678:                         }
                   3679:                     } else {
                   3680:                         $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  3681:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3682:                             if ($newval =~ /^autolimit/) {
                   3683:                                 if ($limit) {
                   3684:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3685:                                 } else {
                   3686:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3687:                                 }
                   3688:                             } elsif ($newval) {
                   3689:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3690:                             } else {
                   3691:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3692:                             }
1.275     raeburn  3693:                         } else {
1.383     raeburn  3694:                             if ($userenv->{$context.'.'.$tool}) {
                   3695:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3696:                             } else {
                   3697:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3698:                             }
1.275     raeburn  3699:                         }
                   3700:                     }
                   3701:                 } else {
                   3702:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3703:                     $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3704:                 }
                   3705:             } else {
                   3706:                 $changed->{$tool} = &tool_admin($tool,'',$changeHash,$context);
                   3707:                 if ($changed->{$tool}) {
                   3708:                     $newaccess->{$tool} = &mt('default');
                   3709:                 } else {
                   3710:                     $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  3711:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3712:                         if ($newval =~ /^autolimit/) {
                   3713:                             if ($limit) {
                   3714:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3715:                             } else {
                   3716:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3717:                             }
                   3718:                         } elsif ($newval) {
                   3719:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3720:                         } else {
                   3721:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3722:                         }
1.275     raeburn  3723:                     } else {
1.383     raeburn  3724:                         if ($userenv->{$context.'.'.$tool}) {
                   3725:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3726:                         } else {
                   3727:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3728:                         }
1.275     raeburn  3729:                     }
                   3730:                 }
                   3731:             }
                   3732:         } else {
                   3733:             $oldaccess->{$tool} = &mt('default');
                   3734:             if ($env{'form.custom'.$tool} == 1) {
1.306     raeburn  3735:                 $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3736:                                                 $context);
1.275     raeburn  3737:                 if ($changed->{$tool}) {
                   3738:                     $newaccess->{$tool} = &mt('custom');
1.383     raeburn  3739:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3740:                         if ($newval =~ /^autolimit/) {
                   3741:                             if ($limit) {
                   3742:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3743:                             } else {
                   3744:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3745:                             }
                   3746:                         } elsif ($newval) {
                   3747:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3748:                         } else {
                   3749:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3750:                         }
1.275     raeburn  3751:                     } else {
1.383     raeburn  3752:                         if ($newval) {
                   3753:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3754:                         } else {
                   3755:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3756:                         }
1.275     raeburn  3757:                     }
                   3758:                 } else {
                   3759:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3760:                 }
                   3761:             } else {
                   3762:                 $newaccess->{$tool} = $oldaccess->{$tool};
                   3763:             }
                   3764:         }
                   3765:     }
                   3766:     return;
                   3767: }
                   3768: 
1.220     raeburn  3769: sub update_roles {
1.375     raeburn  3770:     my ($r,$context,$showcredits) = @_;
1.4       www      3771:     my $now=time;
1.225     raeburn  3772:     my @rolechanges;
1.220     raeburn  3773:     my %disallowed;
1.73      sakharuk 3774:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
1.404     raeburn  3775:     foreach my $key (keys(%env)) {
1.135     raeburn  3776: 	next if (! $env{$key});
1.190     raeburn  3777:         next if ($key eq 'form.action');
1.27      matthew  3778: 	# Revoke roles
1.135     raeburn  3779: 	if ($key=~/^form\.rev/) {
                   3780: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
1.64      www      3781: # Revoke standard role
1.170     albertel 3782: 		my ($scope,$role) = ($1,$2);
                   3783: 		my $result =
                   3784: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
                   3785: 						$env{'form.ccuname'},
1.239     raeburn  3786: 						$scope,$role,'','',$context);
1.367     golterma 3787:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3788:                             &mt('Revoking [_1] in [_2]',
                   3789:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  3790:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   3791:                                 $result ne "ok").'<br />');
                   3792:                 if ($result ne "ok") {
                   3793:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3794:                 }
1.170     albertel 3795: 		if ($role eq 'st') {
1.202     raeburn  3796: 		    my $result = 
1.198     raeburn  3797:                         &Apache::lonuserutils::classlist_drop($scope,
                   3798:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3799: 			    $now);
1.367     golterma 3800:                     $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.53      www      3801: 		}
1.225     raeburn  3802:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3803:                     push(@rolechanges,$role);
                   3804:                 }
1.196     raeburn  3805: 	    }
1.195     raeburn  3806: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}s) {
1.64      www      3807: # Revoke custom role
1.369     bisitz   3808:                 my $result = &Apache::lonnet::revokecustomrole(
                   3809:                     $env{'form.ccdomain'},$env{'form.ccuname'},$1,$2,$3,$4,'','',$context);
1.367     golterma 3810:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3811:                             &mt('Revoking custom role [_1] by [_2] in [_3]',
1.372     raeburn  3812:                                 $4,$3.':'.$2,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3813:                             $result ne 'ok').'<br />');
                   3814:                 if ($result ne "ok") {
                   3815:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3816:                 }
1.225     raeburn  3817:                 if (!grep(/^cr$/,@rolechanges)) {
                   3818:                     push(@rolechanges,'cr');
                   3819:                 }
1.64      www      3820: 	    }
1.135     raeburn  3821: 	} elsif ($key=~/^form\.del/) {
                   3822: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
1.116     raeburn  3823: # Delete standard role
1.170     albertel 3824: 		my ($scope,$role) = ($1,$2);
                   3825: 		my $result =
                   3826: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
                   3827: 						$env{'form.ccuname'},
1.239     raeburn  3828: 						$scope,$role,$now,0,1,'',
                   3829:                                                 $context);
1.367     golterma 3830:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   3831:                             &mt('Deleting [_1] in [_2]',
1.369     bisitz   3832:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  3833:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   3834:                             $result ne 'ok').'<br />');
                   3835:                 if ($result ne "ok") {
                   3836:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3837:                 }
1.367     golterma 3838: 
1.170     albertel 3839: 		if ($role eq 'st') {
1.202     raeburn  3840: 		    my $result = 
1.198     raeburn  3841:                         &Apache::lonuserutils::classlist_drop($scope,
                   3842:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3843: 			    $now);
1.369     bisitz   3844: 		    $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.81      albertel 3845: 		}
1.225     raeburn  3846:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3847:                     push(@rolechanges,$role);
                   3848:                 }
1.116     raeburn  3849:             }
1.139     albertel 3850: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3851:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3852: # Delete custom role
1.369     bisitz   3853:                 my $result =
                   3854:                     &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
                   3855:                         $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
                   3856:                         0,1,$context);
                   3857:                 $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Deleting custom role [_1] by [_2] in [_3]',
1.372     raeburn  3858:                       $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3859:                       $result ne "ok").'<br />');
                   3860:                 if ($result ne "ok") {
                   3861:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3862:                 }
1.367     golterma 3863: 
1.225     raeburn  3864:                 if (!grep(/^cr$/,@rolechanges)) {
                   3865:                     push(@rolechanges,'cr');
                   3866:                 }
1.116     raeburn  3867:             }
1.135     raeburn  3868: 	} elsif ($key=~/^form\.ren/) {
1.101     albertel 3869:             my $udom = $env{'form.ccdomain'};
                   3870:             my $uname = $env{'form.ccuname'};
1.116     raeburn  3871: # Re-enable standard role
1.135     raeburn  3872: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
1.89      raeburn  3873:                 my $url = $1;
                   3874:                 my $role = $2;
                   3875:                 my $logmsg;
                   3876:                 my $output;
                   3877:                 if ($role eq 'st') {
1.141     albertel 3878:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
1.374     raeburn  3879:                         my ($cdom,$cnum,$csec) = ($1,$2,$3);
1.375     raeburn  3880:                         my $credits;
                   3881:                         if ($showcredits) {
                   3882:                             my $defaultcredits = 
                   3883:                                 &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   3884:                             $credits = &get_user_credits($defaultcredits,$cdom,$cnum);
                   3885:                         }
                   3886:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$cdom,$cnum,$csec,$context,$credits);
1.220     raeburn  3887:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course') || ($result eq 'refused')) {
1.223     raeburn  3888:                             if ($result eq 'refused' && $logmsg) {
                   3889:                                 $output = $logmsg;
                   3890:                             } else { 
1.369     bisitz   3891:                                 $output = &mt('Error: [_1]',$result)."\n";
1.223     raeburn  3892:                             }
1.89      raeburn  3893:                         } else {
1.372     raeburn  3894:                             $output = &Apache::lonhtmlcommon::confirm_success(&mt('Assigning [_1] in [_2] starting [_3]',
                   3895:                                         &Apache::lonnet::plaintext($role),
                   3896:                                         &Apache::loncommon::show_role_extent($url,$context,'st'),
                   3897:                                         &Apache::lonlocal::locallocaltime($now))).'<br />'.$logmsg.'<br />';
1.89      raeburn  3898:                         }
                   3899:                     }
                   3900:                 } else {
1.101     albertel 3901: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
1.239     raeburn  3902:                                $env{'form.ccuname'},$url,$role,0,$now,'','',
                   3903:                                $context);
1.367     golterma 3904:                         $output = &Apache::lonhtmlcommon::confirm_success(&mt('Re-enabling [_1] in [_2]',
1.372     raeburn  3905:                                         &Apache::lonnet::plaintext($role),
                   3906:                                         &Apache::loncommon::show_role_extent($url,$context,$role)),$result ne "ok").'<br />';
1.369     bisitz   3907:                     if ($result ne "ok") {
                   3908:                         $output .= &mt('Error: [_1]',$result).'<br />';
                   3909:                     }
                   3910:                 }
1.89      raeburn  3911:                 $r->print($output);
1.225     raeburn  3912:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3913:                     push(@rolechanges,$role);
                   3914:                 }
1.113     raeburn  3915: 	    }
1.116     raeburn  3916: # Re-enable custom role
1.139     albertel 3917: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3918:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3919:                 my $result = &Apache::lonnet::assigncustomrole(
                   3920:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
1.240     raeburn  3921:                                $url,$rdom,$rnam,$rolename,0,$now,undef,$context);
1.369     bisitz   3922:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   3923:                     &mt('Re-enabling custom role [_1] by [_2] in [_3]',
1.372     raeburn  3924:                         $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3925:                     $result ne "ok").'<br />');
                   3926:                 if ($result ne "ok") {
                   3927:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3928:                 }
1.225     raeburn  3929:                 if (!grep(/^cr$/,@rolechanges)) {
                   3930:                     push(@rolechanges,'cr');
                   3931:                 }
1.116     raeburn  3932:             }
1.135     raeburn  3933: 	} elsif ($key=~/^form\.act/) {
1.101     albertel 3934:             my $udom = $env{'form.ccdomain'};
                   3935:             my $uname = $env{'form.ccuname'};
1.141     albertel 3936: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
1.65      www      3937:                 # Activate a custom role
1.83      albertel 3938: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
                   3939: 		my $url='/'.$one.'/'.$two;
                   3940: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
1.65      www      3941: 
1.101     albertel 3942:                 my $start = ( $env{'form.start_'.$full} ?
                   3943:                               $env{'form.start_'.$full} :
1.88      raeburn  3944:                               $now );
1.101     albertel 3945:                 my $end   = ( $env{'form.end_'.$full} ?
                   3946:                               $env{'form.end_'.$full} :
1.88      raeburn  3947:                               0 );
                   3948:                                                                                      
                   3949:                 # split multiple sections
                   3950:                 my %sections = ();
1.101     albertel 3951:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
1.88      raeburn  3952:                 if ($num_sections == 0) {
1.240     raeburn  3953:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3954:                 } else {
1.114     albertel 3955: 		    my %curr_groups =
1.117     raeburn  3956: 			&Apache::longroup::coursegroups($one,$two);
1.404     raeburn  3957:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.113     raeburn  3958:                         if (($sec eq 'none') || ($sec eq 'all') || 
                   3959:                             exists($curr_groups{$sec})) {
                   3960:                             $disallowed{$sec} = $url;
                   3961:                             next;
                   3962:                         }
                   3963:                         my $securl = $url.'/'.$sec;
1.240     raeburn  3964: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3965:                     }
                   3966:                 }
1.225     raeburn  3967:                 if (!grep(/^cr$/,@rolechanges)) {
                   3968:                     push(@rolechanges,'cr');
                   3969:                 }
1.142     raeburn  3970: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
1.27      matthew  3971: 		# Activate roles for sections with 3 id numbers
                   3972: 		# set start, end times, and the url for the class
1.83      albertel 3973: 		my ($one,$two,$three)=($1,$2,$3);
1.101     albertel 3974: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
                   3975: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
1.27      matthew  3976: 			      $now );
1.101     albertel 3977: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
                   3978: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  3979: 			      0 );
1.83      albertel 3980: 		my $url='/'.$one.'/'.$two;
1.88      raeburn  3981:                 my $type = 'three';
                   3982:                 # split multiple sections
                   3983:                 my %sections = ();
1.101     albertel 3984:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
1.375     raeburn  3985:                 my $credits;
                   3986:                 if ($three eq 'st') {
                   3987:                     if ($showcredits) { 
                   3988:                         my $defaultcredits = 
                   3989:                             &Apache::lonuserutils::get_defaultcredits($one,$two);
                   3990:                         $credits = $env{'form.credits_'.$one.'_'.$two.'_'.$three};
                   3991:                         $credits =~ s/[^\d\.]//g;
                   3992:                         if ($credits eq $defaultcredits) {
                   3993:                             undef($credits);
                   3994:                         }
                   3995:                     }
                   3996:                 }
1.88      raeburn  3997:                 if ($num_sections == 0) {
1.375     raeburn  3998:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  3999:                 } else {
1.114     albertel 4000:                     my %curr_groups = 
1.117     raeburn  4001: 			&Apache::longroup::coursegroups($one,$two);
1.88      raeburn  4002:                     my $emptysec = 0;
1.404     raeburn  4003:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.88      raeburn  4004:                         $sec =~ s/\W//g;
1.113     raeburn  4005:                         if ($sec ne '') {
                   4006:                             if (($sec eq 'none') || ($sec eq 'all') || 
                   4007:                                 exists($curr_groups{$sec})) {
                   4008:                                 $disallowed{$sec} = $url;
                   4009:                                 next;
                   4010:                             }
1.88      raeburn  4011:                             my $securl = $url.'/'.$sec;
1.375     raeburn  4012:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec,$context,$credits));
1.88      raeburn  4013:                         } else {
                   4014:                             $emptysec = 1;
                   4015:                         }
                   4016:                     }
                   4017:                     if ($emptysec) {
1.375     raeburn  4018:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  4019:                     }
1.225     raeburn  4020:                 }
                   4021:                 if (!grep(/^\Q$three\E$/,@rolechanges)) {
                   4022:                     push(@rolechanges,$three);
                   4023:                 }
1.135     raeburn  4024: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
1.27      matthew  4025: 		# Activate roles for sections with two id numbers
                   4026: 		# set start, end times, and the url for the class
1.101     albertel 4027: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
                   4028: 			      $env{'form.start_'.$1.'_'.$2} : 
1.27      matthew  4029: 			      $now );
1.101     albertel 4030: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
                   4031: 			      $env{'form.end_'.$1.'_'.$2} :
1.27      matthew  4032: 			      0 );
1.225     raeburn  4033:                 my $one = $1;
                   4034:                 my $two = $2;
                   4035: 		my $url='/'.$one.'/';
1.88      raeburn  4036:                 # split multiple sections
                   4037:                 my %sections = ();
1.225     raeburn  4038:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two},\%sections,$two);
1.88      raeburn  4039:                 if ($num_sections == 0) {
1.240     raeburn  4040:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  4041:                 } else {
                   4042:                     my $emptysec = 0;
1.404     raeburn  4043:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.88      raeburn  4044:                         if ($sec ne '') {
                   4045:                             my $securl = $url.'/'.$sec;
1.240     raeburn  4046:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$two,$start,$end,$one,undef,$sec,$context));
1.88      raeburn  4047:                         } else {
                   4048:                             $emptysec = 1;
                   4049:                         }
                   4050:                     }
                   4051:                     if ($emptysec) {
1.240     raeburn  4052:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  4053:                     }
                   4054:                 }
1.225     raeburn  4055:                 if (!grep(/^\Q$two\E$/,@rolechanges)) {
                   4056:                     push(@rolechanges,$two);
                   4057:                 }
1.64      www      4058: 	    } else {
1.190     raeburn  4059: 		$r->print('<p><span class="LC_error">'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></span></p><br />');
1.64      www      4060:             }
1.113     raeburn  4061:             foreach my $key (sort(keys(%disallowed))) {
1.274     bisitz   4062:                 $r->print('<p class="LC_warning">');
1.113     raeburn  4063:                 if (($key eq 'none') || ($key eq 'all')) {  
1.274     bisitz   4064:                     $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  4065:                 } else {
1.274     bisitz   4066:                     $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  4067:                 }
1.274     bisitz   4068:                 $r->print('</p><p>'
                   4069:                          .&mt('Please [_1]go back[_2] and choose a different section name.'
                   4070:                              ,'<a href="javascript:history.go(-1)'
                   4071:                              ,'</a>')
                   4072:                          .'</p><br />'
                   4073:                 );
1.113     raeburn  4074:             }
                   4075: 	}
1.101     albertel 4076:     } # End of foreach (keys(%env))
1.75      www      4077: # Flush the course logs so reverse user roles immediately updated
1.349     raeburn  4078:     $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.225     raeburn  4079:     if (@rolechanges == 0) {
1.372     raeburn  4080:         $r->print('<p>'.&mt('No roles to modify').'</p>');
1.193     raeburn  4081:     }
1.225     raeburn  4082:     return @rolechanges;
1.220     raeburn  4083: }
                   4084: 
1.375     raeburn  4085: sub get_user_credits {
                   4086:     my ($uname,$udom,$defaultcredits,$cdom,$cnum) = @_;
                   4087:     if ($cdom eq '' || $cnum eq '') {
                   4088:         return unless ($env{'request.course.id'});
                   4089:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4090:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4091:     }
                   4092:     my $credits;
                   4093:     my %currhash =
                   4094:         &Apache::lonnet::get('classlist',[$uname.':'.$udom],$cdom,$cnum);
                   4095:     if (keys(%currhash) > 0) {
                   4096:         my @items = split(/:/,$currhash{$uname.':'.$udom});
                   4097:         my $crdidx = &Apache::loncoursedata::CL_CREDITS() - 3;
                   4098:         $credits = $items[$crdidx];
                   4099:         $credits =~ s/[^\d\.]//g;
                   4100:     }
                   4101:     if ($credits eq $defaultcredits) {
                   4102:         undef($credits);
                   4103:     }
                   4104:     return $credits;
                   4105: }
                   4106: 
1.220     raeburn  4107: sub enroll_single_student {
1.375     raeburn  4108:     my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype,
                   4109:         $showcredits,$defaultcredits) = @_;
1.318     raeburn  4110:     $r->print('<h3>');
                   4111:     if ($crstype eq 'Community') {
                   4112:         $r->print(&mt('Enrolling Member'));
                   4113:     } else {
                   4114:         $r->print(&mt('Enrolling Student'));
                   4115:     }
                   4116:     $r->print('</h3>');
1.220     raeburn  4117: 
                   4118:     # Remove non alphanumeric values from section
                   4119:     $env{'form.sections'}=~s/\W//g;
                   4120: 
1.375     raeburn  4121:     my $credits;
                   4122:     if (($showcredits) && ($env{'form.credits'} ne '')) {
                   4123:         $credits = $env{'form.credits'};
                   4124:         $credits =~ s/[^\d\.]//g;
                   4125:         if ($credits ne '') {
                   4126:             if ($credits eq $defaultcredits) {
                   4127:                 undef($credits);
                   4128:             }
                   4129:         }
                   4130:     }
                   4131: 
1.220     raeburn  4132:     # Clean out any old student roles the user has in this class.
                   4133:     &Apache::lonuserutils::modifystudent($env{'form.ccdomain'},
                   4134:          $env{'form.ccuname'},$env{'request.course.id'},undef,$uhome);
                   4135:     my ($startdate,$enddate) = &Apache::lonuserutils::get_dates_from_form();
                   4136:     my $enroll_result =
                   4137:         &Apache::lonnet::modify_student_enrollment($env{'form.ccdomain'},
                   4138:             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
                   4139:             $env{'form.cmiddlename'},$env{'form.clastname'},
                   4140:             $env{'form.generation'},$env{'form.sections'},$enddate,
1.375     raeburn  4141:             $startdate,'manual',undef,$env{'request.course.id'},'',$context,
                   4142:             $credits);
1.220     raeburn  4143:     if ($enroll_result =~ /^ok/) {
1.381     bisitz   4144:         $r->print(&mt('[_1] enrolled','<b>'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.'</b>'));
1.220     raeburn  4145:         if ($env{'form.sections'} ne '') {
                   4146:             $r->print(' '.&mt('in section [_1]',$env{'form.sections'}));
                   4147:         }
                   4148:         my ($showstart,$showend);
                   4149:         if ($startdate <= $now) {
                   4150:             $showstart = &mt('Access starts immediately');
                   4151:         } else {
                   4152:             $showstart = &mt('Access starts: ').&Apache::lonlocal::locallocaltime($startdate);
                   4153:         }
                   4154:         if ($enddate == 0) {
                   4155:             $showend = &mt('ends: no ending date');
                   4156:         } else {
                   4157:             $showend = &mt('ends: ').&Apache::lonlocal::locallocaltime($enddate);
                   4158:         }
                   4159:         $r->print('.<br />'.$showstart.'; '.$showend);
                   4160:         if ($startdate <= $now && !$newuser) {
1.386     bisitz   4161:             $r->print('<p class="LC_info">');
1.318     raeburn  4162:             if ($crstype eq 'Community') {
1.392     raeburn  4163:                 $r->print(&mt('If the member is currently logged-in to LON-CAPA, the new role can be displayed by using the "Check for changes" link on the Roles/Courses page.'));
1.318     raeburn  4164:             } else {
1.392     raeburn  4165:                 $r->print(&mt('If the student is currently logged-in to LON-CAPA, the new role can be displayed by using the "Check for changes" link on the Roles/Courses page.'));
1.318     raeburn  4166:            }
                   4167:            $r->print('</p>');
1.220     raeburn  4168:         }
                   4169:     } else {
                   4170:         $r->print(&mt('unable to enroll').": ".$enroll_result);
                   4171:     }
                   4172:     return;
1.188     raeburn  4173: }
                   4174: 
1.204     raeburn  4175: sub get_defaultquota_text {
                   4176:     my ($settingstatus) = @_;
                   4177:     my $defquotatext; 
                   4178:     if ($settingstatus eq '') {
1.383     raeburn  4179:         $defquotatext = &mt('default');
1.204     raeburn  4180:     } else {
                   4181:         my ($usertypes,$order) =
                   4182:             &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
                   4183:         if ($usertypes->{$settingstatus} eq '') {
1.383     raeburn  4184:             $defquotatext = &mt('default');
1.204     raeburn  4185:         } else {
1.383     raeburn  4186:             $defquotatext = &mt('default for [_1]',$usertypes->{$settingstatus});
1.204     raeburn  4187:         }
                   4188:     }
                   4189:     return $defquotatext;
                   4190: }
                   4191: 
1.188     raeburn  4192: sub update_result_form {
                   4193:     my ($uhome) = @_;
                   4194:     my $outcome = 
1.367     golterma 4195:     '<form name="userupdate" method="post" action="">'."\n";
1.160     raeburn  4196:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
1.188     raeburn  4197:         $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  4198:     }
1.207     raeburn  4199:     if ($env{'form.origname'} ne '') {
                   4200:         $outcome .= '<input type="hidden" name="origname" value="'.$env{'form.origname'}.'" />'."\n";
                   4201:     }
1.160     raeburn  4202:     foreach my $item ('sortby','seluname','seludom') {
                   4203:         if (exists($env{'form.'.$item})) {
1.188     raeburn  4204:             $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  4205:         }
                   4206:     }
1.188     raeburn  4207:     if ($uhome eq 'no_host') {
                   4208:         $outcome .= '<input type="hidden" name="forcenewuser" value="1" />'."\n";
                   4209:     }
                   4210:     $outcome .= '<input type="hidden" name="phase" value="" />'."\n".
1.383     raeburn  4211:                 '<input type="hidden" name="currstate" value="" />'."\n".
                   4212:                 '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1.188     raeburn  4213:                 '</form>';
                   4214:     return $outcome;
1.4       www      4215: }
                   4216: 
1.149     raeburn  4217: sub quota_admin {
1.378     raeburn  4218:     my ($setquota,$changeHash,$name) = @_;
1.149     raeburn  4219:     my $quotachanged;
                   4220:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   4221:         # Current user has quota modification privileges
1.267     raeburn  4222:         if (ref($changeHash) eq 'HASH') {
                   4223:             $quotachanged = 1;
1.378     raeburn  4224:             $changeHash->{$name.'quota'} = $setquota;
1.267     raeburn  4225:         }
1.149     raeburn  4226:     }
                   4227:     return $quotachanged;
                   4228: }
                   4229: 
1.267     raeburn  4230: sub tool_admin {
1.275     raeburn  4231:     my ($tool,$settool,$changeHash,$context) = @_;
                   4232:     my $canchange = 0; 
1.279     raeburn  4233:     if ($context eq 'requestcourses') {
1.275     raeburn  4234:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
                   4235:             $canchange = 1;
                   4236:         }
1.300     raeburn  4237:     } elsif ($context eq 'reqcrsotherdom') {
                   4238:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   4239:             $canchange = 1;
                   4240:         }
1.362     raeburn  4241:     } elsif ($context eq 'requestauthor') {
                   4242:         if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
                   4243:             $canchange = 1;
                   4244:         }
1.275     raeburn  4245:     } elsif (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   4246:         # Current user has quota modification privileges
                   4247:         $canchange = 1;
                   4248:     }
1.267     raeburn  4249:     my $toolchanged;
1.275     raeburn  4250:     if ($canchange) {
1.267     raeburn  4251:         if (ref($changeHash) eq 'HASH') {
                   4252:             $toolchanged = 1;
1.362     raeburn  4253:             if ($tool eq 'requestauthor') {
                   4254:                 $changeHash->{$context} = $settool;
                   4255:             } else {
                   4256:                 $changeHash->{$context.'.'.$tool} = $settool;
                   4257:             }
1.267     raeburn  4258:         }
                   4259:     }
                   4260:     return $toolchanged;
                   4261: }
                   4262: 
1.88      raeburn  4263: sub build_roles {
1.89      raeburn  4264:     my ($sectionstr,$sections,$role) = @_;
1.88      raeburn  4265:     my $num_sections = 0;
                   4266:     if ($sectionstr=~ /,/) {
                   4267:         my @secnums = split/,/,$sectionstr;
1.89      raeburn  4268:         if ($role eq 'st') {
                   4269:             $secnums[0] =~ s/\W//g;
                   4270:             $$sections{$secnums[0]} = 1;
                   4271:             $num_sections = 1;
                   4272:         } else {
                   4273:             foreach my $sec (@secnums) {
                   4274:                 $sec =~ ~s/\W//g;
1.150     banghart 4275:                 if (!($sec eq "")) {
1.89      raeburn  4276:                     if (exists($$sections{$sec})) {
                   4277:                         $$sections{$sec} ++;
                   4278:                     } else {
                   4279:                         $$sections{$sec} = 1;
                   4280:                         $num_sections ++;
                   4281:                     }
1.88      raeburn  4282:                 }
                   4283:             }
                   4284:         }
                   4285:     } else {
                   4286:         $sectionstr=~s/\W//g;
                   4287:         unless ($sectionstr eq '') {
                   4288:             $$sections{$sectionstr} = 1;
                   4289:             $num_sections ++;
                   4290:         }
                   4291:     }
1.129     albertel 4292: 
1.88      raeburn  4293:     return $num_sections;
                   4294: }
                   4295: 
1.58      www      4296: # ========================================================== Custom Role Editor
                   4297: 
                   4298: sub custom_role_editor {
1.406.2.5  raeburn  4299:     my ($r,$brcrum,$prefix) = @_;
1.324     raeburn  4300:     my $action = $env{'form.customroleaction'};
                   4301:     my $rolename; 
                   4302:     if ($action eq 'new') {
                   4303:         $rolename=$env{'form.newrolename'};
                   4304:     } else {
                   4305:         $rolename=$env{'form.rolename'};
1.59      www      4306:     }
                   4307: 
1.324     raeburn  4308:     my ($crstype,$context);
                   4309:     if ($env{'request.course.id'}) {
                   4310:         $crstype = &Apache::loncommon::course_type();
                   4311:         $context = 'course';
                   4312:     } else {
                   4313:         $context = 'domain';
1.406.2.5  raeburn  4314:         $crstype = 'course';
1.324     raeburn  4315:     }
1.351     raeburn  4316: 
                   4317:     $rolename=~s/[^A-Za-z0-9]//gs;
                   4318:     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
                   4319: 	&print_username_entry_form($r,undef,undef,undef,undef,$crstype,$brcrum);
                   4320:         return;
                   4321:     }
                   4322: 
1.406.2.5  raeburn  4323:     my $formname = 'form1';
                   4324:     my %privs=();
                   4325:     my $body_top = '<h2>';
                   4326: # ------------------------------------------------------- Does this role exist?
1.59      www      4327:     my ($rdummy,$roledef)=
                   4328: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   4329:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.406.2.5  raeburn  4330:         $body_top .= &mt('Existing Role').' "';
1.61      www      4331: # ------------------------------------------------- Get current role privileges
1.406.2.5  raeburn  4332:         ($privs{'system'},$privs{'domain'},$privs{'course'})=split(/\_/,$roledef);
                   4333:         if ($privs{'system'} =~ /bre\&S/) {
                   4334:             if ($context eq 'domain') {
                   4335:                 $crstype = 'Course';
                   4336:             } elsif ($crstype eq 'Community') {
                   4337:                 $privs{'system'} =~ s/bre\&S//;
                   4338:             }
                   4339:         } elsif ($context eq 'domain') {
                   4340:             $crstype = 'Course';
1.324     raeburn  4341:         }
1.59      www      4342:     } else {
1.406.2.5  raeburn  4343:         $body_top .= &mt('New Role').' "';
                   4344:         $roledef='';
1.59      www      4345:     }
1.153     banghart 4346:     $body_top .= $rolename.'"</h2>';
1.406.2.5  raeburn  4347: 
                   4348: # ------------------------------------------------------- What can be assigned?
                   4349:     my %full=();
                   4350:     my %levels=(
                   4351:                  course => {},
                   4352:                  domain => {},
                   4353:                  system => {},
                   4354:                );
                   4355:     my %levelscurrent=(
                   4356:                         course => {},
                   4357:                         domain => {},
                   4358:                         system => {},
                   4359:                       );
                   4360:     &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
1.160     raeburn  4361:     my ($jsback,$elements) = &crumb_utilities();
1.406.2.5  raeburn  4362:     my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
                   4363:     my $head_script =
                   4364:         &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,
                   4365:                                                   \%full,\@templateroles,$jsback);
1.351     raeburn  4366:     push (@{$brcrum},
1.406.2.5  raeburn  4367:               {href => "javascript:backPage(document.$formname,'pickrole','')",
1.351     raeburn  4368:                text => "Pick custom role",
                   4369:                faq  => 282,bug=>'Instructor Interface',},
1.406.2.5  raeburn  4370:               {href => "javascript:backPage(document.$formname,'','')",
1.351     raeburn  4371:                text => "Edit custom role",
                   4372:                faq  => 282,
                   4373:                bug  => 'Instructor Interface',
                   4374:                help => 'Course_Editing_Custom_Roles'}
                   4375:               );
                   4376:     my $args = { bread_crumbs          => $brcrum,
                   4377:                  bread_crumbs_component => 'User Management'};
1.406.2.5  raeburn  4378: 
1.351     raeburn  4379:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',
                   4380:                                              $head_script,$args).
                   4381:               $body_top);
1.406.2.5  raeburn  4382:     $r->print('<form name="'.$formname.'" method="post" action="">'."\n".
                   4383:               &Apache::lonuserutils::custom_role_header($context,$crstype,
                   4384:                                                         \@templateroles,$prefix));
1.264     bisitz   4385: 
1.61      www      4386:     $r->print(<<ENDCCF);
                   4387: <input type="hidden" name="phase" value="set_custom_roles" />
                   4388: <input type="hidden" name="rolename" value="$rolename" />
                   4389: ENDCCF
1.406.2.5  raeburn  4390:     $r->print(&Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
                   4391:                                                        \%levelscurrent,$prefix));
1.135     raeburn  4392:     $r->print(&Apache::loncommon::end_data_table().
1.190     raeburn  4393:    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
1.160     raeburn  4394:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
1.406.2.5  raeburn  4395:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".
1.160     raeburn  4396:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
1.351     raeburn  4397:    '<input type="submit" value="'.&mt('Save').'" /></form>');
1.61      www      4398: }
1.406.2.5  raeburn  4399: 
1.61      www      4400: # ---------------------------------------------------------- Call to definerole
                   4401: sub set_custom_role {
1.406.2.5  raeburn  4402:     my ($r,$context,$brcrum,$prefix) = @_;
1.101     albertel 4403:     my $rolename=$env{'form.rolename'};
1.63      www      4404:     $rolename=~s/[^A-Za-z0-9]//gs;
1.150     banghart 4405:     if (!$rolename) {
1.406.2.5  raeburn  4406: 	&custom_role_editor($r,$brcrum,$prefix);
1.61      www      4407:         return;
                   4408:     }
1.160     raeburn  4409:     my ($jsback,$elements) = &crumb_utilities();
1.301     bisitz   4410:     my $jscript = '<script type="text/javascript">'
                   4411:                  .'// <![CDATA['."\n"
                   4412:                  .$jsback."\n"
                   4413:                  .'// ]]>'."\n"
                   4414:                  .'</script>'."\n";
1.352     raeburn  4415:     push(@{$brcrum},
                   4416:         {href => "javascript:backPage(document.customresult,'pickrole','')",
                   4417:          text => "Pick custom role",
                   4418:          faq  => 282,
                   4419:          bug  => 'Instructor Interface',},
                   4420:         {href => "javascript:backPage(document.customresult,'selected_custom_edit','')",
                   4421:          text => "Edit custom role",
                   4422:          faq  => 282,
                   4423:          bug  => 'Instructor Interface',},
                   4424:         {href => "javascript:backPage(document.customresult,'set_custom_roles','')",
                   4425:          text => "Result",
                   4426:          faq  => 282,
                   4427:          bug  => 'Instructor Interface',
                   4428:          help => 'Course_Editing_Custom_Roles'},
                   4429:         );
                   4430:     my $args = { bread_crumbs           => $brcrum,
1.406.2.5  raeburn  4431:                  bread_crumbs_component => 'User Management'};
1.351     raeburn  4432:     $r->print(&Apache::loncommon::start_page('Save Custom Role',$jscript,$args));
1.160     raeburn  4433: 
1.393     raeburn  4434:     my $newrole;
1.61      www      4435:     my ($rdummy,$roledef)=
1.110     albertel 4436: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   4437: 
1.61      www      4438: # ------------------------------------------------------- Does this role exist?
1.188     raeburn  4439:     $r->print('<h3>');
1.61      www      4440:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 4441: 	$r->print(&mt('Existing Role').' "');
1.61      www      4442:     } else {
1.73      sakharuk 4443: 	$r->print(&mt('New Role').' "');
1.61      www      4444: 	$roledef='';
1.393     raeburn  4445:         $newrole = 1;
1.61      www      4446:     }
1.188     raeburn  4447:     $r->print($rolename.'"</h3>');
1.406.2.5  raeburn  4448: # ------------------------------------------------- Assign role and show result
1.61      www      4449: 
1.387     bisitz   4450:     my $errmsg;
1.406.2.5  raeburn  4451:     my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$prefix);
                   4452:     # Assign role and return result
                   4453:     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
                   4454:                                              $newprivs{'c'});
1.387     bisitz   4455:     if ($result ne 'ok') {
                   4456:         $errmsg = ': '.$result;
                   4457:     }
                   4458:     my $message =
                   4459:         &Apache::lonhtmlcommon::confirm_success(
                   4460:             &mt('Defining Role').$errmsg, ($result eq 'ok' ? 0 : 1));
1.101     albertel 4461:     if ($env{'request.course.id'}) {
                   4462:         my $url='/'.$env{'request.course.id'};
1.63      www      4463:         $url=~s/\_/\//g;
1.387     bisitz   4464:         $result =
                   4465:             &Apache::lonnet::assigncustomrole(
                   4466:                 $env{'user.domain'},$env{'user.name'},
                   4467:                 $url,
                   4468:                 $env{'user.domain'},$env{'user.name'},
                   4469:                 $rolename,undef,undef,undef,$context);
                   4470:         if ($result ne 'ok') {
                   4471:             $errmsg = ': '.$result;
                   4472:         }
                   4473:         $message .=
                   4474:             '<br />'
                   4475:            .&Apache::lonhtmlcommon::confirm_success(
                   4476:                 &mt('Assigning Role to Self').$errmsg, ($result eq 'ok' ? 0 : 1));
1.63      www      4477:     }
1.380     bisitz   4478:     $r->print(
1.387     bisitz   4479:         &Apache::loncommon::confirmwrapper($message)
                   4480:        .'<br />'
                   4481:        .&Apache::lonhtmlcommon::actionbox([
                   4482:             '<a href="javascript:backPage(document.customresult,'."'pickrole'".')">'
                   4483:            .&mt('Create or edit another custom role')
                   4484:            .'</a>'])
1.380     bisitz   4485:        .'<form name="customresult" method="post" action="">'
1.387     bisitz   4486:        .&Apache::lonhtmlcommon::echo_form_input([])
                   4487:        .'</form>'
1.380     bisitz   4488:     );
1.58      www      4489: }
                   4490: 
1.2       www      4491: # ================================================================ Main Handler
                   4492: sub handler {
                   4493:     my $r = shift;
                   4494:     if ($r->header_only) {
1.68      www      4495:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      4496:        $r->send_http_header;
                   4497:        return OK;
                   4498:     }
1.318     raeburn  4499:     my ($context,$crstype);
1.190     raeburn  4500:     if ($env{'request.course.id'}) {
                   4501:         $context = 'course';
1.318     raeburn  4502:         $crstype = &Apache::loncommon::course_type();
1.190     raeburn  4503:     } elsif ($env{'request.role'} =~ /^au\./) {
1.206     raeburn  4504:         $context = 'author';
1.190     raeburn  4505:     } else {
                   4506:         $context = 'domain';
                   4507:     }
1.375     raeburn  4508: 
1.190     raeburn  4509:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.233     raeburn  4510:         ['action','state','callingform','roletype','showrole','bulkaction','popup','phase',
1.391     raeburn  4511:          'username','domain','srchterm','srchdomain','srchin','srchby','srchtype','queue']);
1.190     raeburn  4512:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.351     raeburn  4513:     my $args;
                   4514:     my $brcrum = [];
                   4515:     my $bread_crumbs_component = 'User Management';
1.391     raeburn  4516:     if (($env{'form.action'} ne 'dateselect') && ($env{'form.action'} ne 'displayuserreq')) {
1.351     raeburn  4517:         $brcrum = [{href=>"/adm/createuser",
                   4518:                     text=>"User Management",
                   4519:                     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'}
                   4520:                   ];
1.202     raeburn  4521:     }
1.289     droeschl 4522:     #SD Following files not added to help, because the corresponding .tex-files seem to
                   4523:     #be missing: Course_Approve_Selfenroll,Course_User_Logs,
1.209     raeburn  4524:     my ($permission,$allowed) = 
1.318     raeburn  4525:         &Apache::lonuserutils::get_permission($context,$crstype);
1.190     raeburn  4526:     if (!$allowed) {
1.358     raeburn  4527:         if ($context eq 'course') {
                   4528:             $r->internal_redirect('/adm/viewclasslist');
                   4529:             return OK;
                   4530:         }
1.190     raeburn  4531:         $env{'user.error.msg'}=
                   4532:             "/adm/createuser:cst:0:0:Cannot create/modify user data ".
                   4533:                                  "or view user status.";
                   4534:         return HTTP_NOT_ACCEPTABLE;
                   4535:     }
                   4536: 
                   4537:     &Apache::loncommon::content_type($r,'text/html');
                   4538:     $r->send_http_header;
                   4539: 
1.375     raeburn  4540:     my $showcredits;
                   4541:     if ((($context eq 'course') && ($crstype eq 'Course')) || 
                   4542:          ($context eq 'domain')) {
                   4543:         my %domdefaults = 
                   4544:             &Apache::lonnet::get_domain_defaults($env{'request.role.domain'});
                   4545:         if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'}) {
                   4546:             $showcredits = 1;
                   4547:         }
                   4548:     }
                   4549: 
1.190     raeburn  4550:     # Main switch on form.action and form.state, as appropriate
                   4551:     if (! exists($env{'form.action'})) {
1.351     raeburn  4552:         $args = {bread_crumbs => $brcrum,
                   4553:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4554:         $r->print(&header(undef,$args));
1.318     raeburn  4555:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4556:     } elsif ($env{'form.action'} eq 'upload' && $permission->{'cusr'}) {
1.351     raeburn  4557:         push(@{$brcrum},
                   4558:               { href => '/adm/createuser?action=upload&state=',
                   4559:                 text => 'Upload Users List',
                   4560:                 help => 'Course_Create_Class_List',
                   4561:               });
                   4562:         $bread_crumbs_component = 'Upload Users List';
                   4563:         $args = {bread_crumbs           => $brcrum,
                   4564:                  bread_crumbs_component => $bread_crumbs_component};
                   4565:         $r->print(&header(undef,$args));
1.190     raeburn  4566:         $r->print('<form name="studentform" method="post" '.
                   4567:                   'enctype="multipart/form-data" '.
                   4568:                   ' action="/adm/createuser">'."\n");
                   4569:         if (! exists($env{'form.state'})) {
                   4570:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4571:         } elsif ($env{'form.state'} eq 'got_file') {
1.375     raeburn  4572:             &Apache::lonuserutils::print_upload_manager_form($r,$context,$permission,
                   4573:                                                              $crstype,$showcredits);
1.190     raeburn  4574:         } elsif ($env{'form.state'} eq 'enrolling') {
                   4575:             if ($env{'form.datatoken'}) {
1.375     raeburn  4576:                 &Apache::lonuserutils::upfile_drop_add($r,$context,$permission,
                   4577:                                                        $showcredits);
1.190     raeburn  4578:             }
                   4579:         } else {
                   4580:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4581:         }
1.406.2.5  raeburn  4582:     } elsif (((($env{'form.action'} eq 'singleuser') || ($env{'form.action'}
                   4583:               eq 'singlestudent')) && ($permission->{'cusr'})) ||
1.406.2.6  raeburn  4584:              (($env{'form.action'} eq 'singleuser') && ($permission->{'view'})) ||
1.406.2.5  raeburn  4585:              (($env{'form.action'} eq 'accesslogs') && ($permission->{'activity'}))) {
1.190     raeburn  4586:         my $phase = $env{'form.phase'};
                   4587:         my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
1.192     albertel 4588: 	&Apache::loncreateuser::restore_prev_selections();
                   4589: 	my $srch;
                   4590: 	foreach my $item (@search) {
                   4591: 	    $srch->{$item} = $env{'form.'.$item};
                   4592: 	}
1.207     raeburn  4593:         if (($phase eq 'get_user_info') || ($phase eq 'userpicked') ||
1.406.2.5  raeburn  4594:             ($phase eq 'createnewuser') || ($phase eq 'activity')) {
1.207     raeburn  4595:             if ($env{'form.phase'} eq 'createnewuser') {
                   4596:                 my $response;
                   4597:                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
1.366     bisitz   4598:                     my $response =
                   4599:                         '<span class="LC_warning">'
                   4600:                        .&mt('You must specify a valid username. Only the following are allowed:'
                   4601:                            .' letters numbers - . @')
                   4602:                        .'</span>';
1.221     raeburn  4603:                     $env{'form.phase'} = '';
1.375     raeburn  4604:                     &print_username_entry_form($r,$context,$response,$srch,undef,
                   4605:                                                $crstype,$brcrum,$showcredits);
1.207     raeburn  4606:                 } else {
                   4607:                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
                   4608:                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
                   4609:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4610:                                                   $srch,$response,$context,
1.375     raeburn  4611:                                                   $permission,$crstype,$brcrum,
                   4612:                                                   $showcredits);
1.207     raeburn  4613:                 }
                   4614:             } elsif ($env{'form.phase'} eq 'get_user_info') {
1.190     raeburn  4615:                 my ($currstate,$response,$forcenewuser,$results) = 
1.221     raeburn  4616:                     &user_search_result($context,$srch);
1.190     raeburn  4617:                 if ($env{'form.currstate'} eq 'modify') {
                   4618:                     $currstate = $env{'form.currstate'};
                   4619:                 }
                   4620:                 if ($currstate eq 'select') {
                   4621:                     &print_user_selection_page($r,$response,$srch,$results,
1.351     raeburn  4622:                                                \@search,$context,undef,$crstype,
                   4623:                                                $brcrum);
1.406.2.5  raeburn  4624:                 } elsif (($currstate eq 'modify') || ($env{'form.action'} eq 'accesslogs')) {
                   4625:                     my ($ccuname,$ccdomain,$uhome);
1.190     raeburn  4626:                     if (($srch->{'srchby'} eq 'uname') && 
                   4627:                         ($srch->{'srchtype'} eq 'exact')) {
                   4628:                         $ccuname = $srch->{'srchterm'};
                   4629:                         $ccdomain= $srch->{'srchdomain'};
                   4630:                     } else {
                   4631:                         my @matchedunames = keys(%{$results});
                   4632:                         ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
                   4633:                     }
                   4634:                     $ccuname =&LONCAPA::clean_username($ccuname);
                   4635:                     $ccdomain=&LONCAPA::clean_domain($ccdomain);
1.406.2.5  raeburn  4636:                     if ($env{'form.action'} eq 'accesslogs') {
                   4637:                         my $uhome;
                   4638:                         if (($ccuname ne '') && ($ccdomain ne '')) {
                   4639:                            $uhome = &Apache::lonnet::homeserver($ccuname,$ccdomain);
                   4640:                         }
                   4641:                         if (($uhome eq '') || ($uhome eq 'no_host')) {
                   4642:                             $env{'form.phase'} = '';
                   4643:                             undef($forcenewuser);
                   4644:                             #if ($response) {
                   4645:                             #    unless ($response =~ m{\Q<br /><br />\E$}) {
                   4646:                             #        $response .= '<br /><br />';
                   4647:                             #    }
                   4648:                             #}
                   4649:                             &print_username_entry_form($r,$context,$response,$srch,
                   4650:                                                        $forcenewuser,$crstype,$brcrum);
                   4651:                         } else {
                   4652:                             &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
                   4653:                         }
                   4654:                     } else {
                   4655:                         if ($env{'form.forcenewuser'}) {
                   4656:                             $response = '';
                   4657:                         }
                   4658:                         &print_user_modification_page($r,$ccuname,$ccdomain,
                   4659:                                                       $srch,$response,$context,
                   4660:                                                       $permission,$crstype,$brcrum);
1.190     raeburn  4661:                     }
                   4662:                 } elsif ($currstate eq 'query') {
1.351     raeburn  4663:                     &print_user_query_page($r,'createuser',$brcrum);
1.190     raeburn  4664:                 } else {
1.229     raeburn  4665:                     $env{'form.phase'} = '';
1.207     raeburn  4666:                     &print_username_entry_form($r,$context,$response,$srch,
1.351     raeburn  4667:                                                $forcenewuser,$crstype,$brcrum);
1.190     raeburn  4668:                 }
                   4669:             } elsif ($env{'form.phase'} eq 'userpicked') {
                   4670:                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
                   4671:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
1.406.2.5  raeburn  4672:                 if ($env{'form.action'} eq 'accesslogs') {
                   4673:                     &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
                   4674:                 } else {
                   4675:                     &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
                   4676:                                                   $context,$permission,$crstype,
                   4677:                                                   $brcrum);
                   4678:                 }
                   4679:             } elsif ($env{'form.action'} eq 'accesslogs') {
                   4680:                 my $ccuname = &LONCAPA::clean_username($env{'form.accessuname'});
                   4681:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.accessudom'});
                   4682:                 &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
1.190     raeburn  4683:             }
                   4684:         } elsif ($env{'form.phase'} eq 'update_user_data') {
1.375     raeburn  4685:             &update_user_data($r,$context,$crstype,$brcrum,$showcredits);
1.190     raeburn  4686:         } else {
1.351     raeburn  4687:             &print_username_entry_form($r,$context,undef,$srch,undef,$crstype,
                   4688:                                        $brcrum);
1.190     raeburn  4689:         }
                   4690:     } elsif ($env{'form.action'} eq 'custom' && $permission->{'custom'}) {
1.406.2.5  raeburn  4691:         my $prefix;
1.190     raeburn  4692:         if ($env{'form.phase'} eq 'set_custom_roles') {
1.406.2.5  raeburn  4693:             &set_custom_role($r,$context,$brcrum,$prefix);
1.190     raeburn  4694:         } else {
1.406.2.5  raeburn  4695:             &custom_role_editor($r,$brcrum,$prefix);
1.190     raeburn  4696:         }
1.362     raeburn  4697:     } elsif (($env{'form.action'} eq 'processauthorreq') &&
                   4698:              ($permission->{'cusr'}) && 
                   4699:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   4700:         push(@{$brcrum},
                   4701:                  {href => '/adm/createuser?action=processauthorreq',
1.385     bisitz   4702:                   text => 'Authoring Space requests',
1.362     raeburn  4703:                   help => 'Domain_Role_Approvals'});
                   4704:         $bread_crumbs_component = 'Authoring requests';
                   4705:         if ($env{'form.state'} eq 'done') {
                   4706:             push(@{$brcrum},
                   4707:                      {href => '/adm/createuser?action=authorreqqueue',
                   4708:                       text => 'Result',
                   4709:                       help => 'Domain_Role_Approvals'});
                   4710:             $bread_crumbs_component = 'Authoring request result';
                   4711:         }
                   4712:         $args = { bread_crumbs           => $brcrum,
                   4713:                   bread_crumbs_component => $bread_crumbs_component};
1.391     raeburn  4714:         my $js = &usernamerequest_javascript();
                   4715:         $r->print(&header(&add_script($js),$args));
1.362     raeburn  4716:         if (!exists($env{'form.state'})) {
                   4717:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestauthor',
                   4718:                                                                             $env{'request.role.domain'}));
                   4719:         } elsif ($env{'form.state'} eq 'done') {
                   4720:             $r->print('<h3>'.&mt('Authoring request processing').'</h3>'."\n");
                   4721:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestauthor',
                   4722:                                                                          $env{'request.role.domain'}));
                   4723:         }
1.391     raeburn  4724:     } elsif (($env{'form.action'} eq 'processusernamereq') &&
                   4725:              ($permission->{'cusr'}) &&
                   4726:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   4727:         push(@{$brcrum},
                   4728:                  {href => '/adm/createuser?action=processusernamereq',
                   4729:                   text => 'LON-CAPA account requests',
                   4730:                   help => 'Domain_Username_Approvals'});
                   4731:         $bread_crumbs_component = 'Account requests';
                   4732:         if ($env{'form.state'} eq 'done') {
                   4733:             push(@{$brcrum},
                   4734:                      {href => '/adm/createuser?action=usernamereqqueue',
                   4735:                       text => 'Result',
                   4736:                       help => 'Domain_Username_Approvals'});
                   4737:             $bread_crumbs_component = 'LON-CAPA account request result';
                   4738:         }
                   4739:         $args = { bread_crumbs           => $brcrum,
                   4740:                   bread_crumbs_component => $bread_crumbs_component};
                   4741:         my $js = &usernamerequest_javascript();
                   4742:         $r->print(&header(&add_script($js),$args));
                   4743:         if (!exists($env{'form.state'})) {
                   4744:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestusername',
                   4745:                                                                             $env{'request.role.domain'}));
                   4746:         } elsif ($env{'form.state'} eq 'done') {
                   4747:             $r->print('<h3>'.&mt('LON-CAPA account request processing').'</h3>'."\n");
                   4748:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestusername',
                   4749:                                                                          $env{'request.role.domain'}));
                   4750:         }
                   4751:     } elsif (($env{'form.action'} eq 'displayuserreq') &&
                   4752:              ($permission->{'cusr'})) {
                   4753:         my $dom = $env{'form.domain'};
                   4754:         my $uname = $env{'form.username'};
                   4755:         my $warning;
                   4756:         if (($dom =~ /^$match_domain$/) && (&Apache::lonnet::domain($dom) ne '')) {
                   4757:             if (($dom eq $env{'request.role.domain'}) && (&Apache::lonnet::allowed('ccc',$dom))) {
                   4758:                 if (($uname =~ /^$match_username$/) && ($env{'form.queue'} eq 'approval')) {
                   4759:                     my $uhome = &Apache::lonnet::homeserver($uname,$dom);
                   4760:                     if ($uhome eq 'no_host') {
                   4761:                         my $queue = $env{'form.queue'};
                   4762:                         my $reqkey = &escape($uname).'_'.$queue; 
                   4763:                         my $namespace = 'usernamequeue';
                   4764:                         my $domconfig = &Apache::lonnet::get_domainconfiguser($dom);
                   4765:                         my %queued =
                   4766:                             &Apache::lonnet::get($namespace,[$reqkey],$dom,$domconfig);
                   4767:                         unless ($queued{$reqkey}) {
                   4768:                             $warning = &mt('No information was found for this LON-CAPA account request.');
                   4769:                         }
                   4770:                     } else {
                   4771:                         $warning = &mt('A LON-CAPA account already exists for the requested username and domain.');
                   4772:                     }
                   4773:                 } else {
                   4774:                     $warning = &mt('LON-CAPA account request status check is for an invalid username.');
                   4775:                 }
                   4776:             } else {
                   4777:                 $warning = &mt('You do not have rights to view LON-CAPA account requests in the domain specified.');
                   4778:             }
                   4779:         } else {
                   4780:             $warning = &mt('LON-CAPA account request status check is for an invalid domain.');
                   4781:         }
                   4782:         my $args = { only_body => 1 };
                   4783:         $r->print(&header(undef,$args).
                   4784:                   '<h3>'.&mt('LON-CAPA Account Request Details').'</h3>');
                   4785:         if ($warning ne '') {
                   4786:             $r->print('<div class="LC_warning">'.$warning.'</div>');
                   4787:         } else {
                   4788:             my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
                   4789:             my $domconfiguser = &Apache::lonnet::get_domainconfiguser($dom);
                   4790:             my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   4791:             if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   4792:                 if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
                   4793:                     if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}) eq 'HASH') {
                   4794:                         my %info =
                   4795:                             &Apache::lonnet::get('nohist_requestedusernames',[$uname],$dom,$domconfiguser);
                   4796:                         if (ref($info{$uname}) eq 'HASH') {
1.396     raeburn  4797:                             my $usertype = $info{$uname}{'inststatus'};
                   4798:                             unless ($usertype) {
                   4799:                                 $usertype = 'default';
                   4800:                             }
                   4801:                             if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}) eq 'HASH') {
                   4802:                                 if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
                   4803:                                     $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box());
                   4804:                                     my ($num,$count,$showstatus);
                   4805:                                     $count = scalar(keys(%{$domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}}));
                   4806:                                     unless ($usertype eq 'default') {
                   4807:                                         my ($othertitle,$usertypes,$types) = 
                   4808:                                             &Apache::loncommon::sorted_inst_types($dom);
                   4809:                                         if (ref($usertypes) eq 'HASH') {
                   4810:                                             if ($usertypes->{$usertype}) {
                   4811:                                                 $showstatus = $usertypes->{$usertype};
                   4812:                                                 $count ++;
                   4813:                                             }
                   4814:                                         }
                   4815:                                     }
                   4816:                                     foreach my $field (@{$infofields}) {
                   4817:                                         next unless ($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}{$field});
                   4818:                                         next unless ($infotitles->{$field});
                   4819:                                         $r->print(&Apache::lonhtmlcommon::row_title($infotitles->{$field}).
                   4820:                                                   $info{$uname}{$field});
                   4821:                                         $num ++;
                   4822:                                         if ($count == $num) {
                   4823:                                             $r->print(&Apache::lonhtmlcommon::row_closure(1));
                   4824:                                         } else {
                   4825:                                             $r->print(&Apache::lonhtmlcommon::row_closure());
                   4826:                                         }
                   4827:                                     }
                   4828:                                     if ($showstatus) {
                   4829:                                         $r->print(&Apache::lonhtmlcommon::row_title(&mt('Status type (self-reported)')).
                   4830:                                                   $showstatus.
                   4831:                                                   &Apache::lonhtmlcommon::row_closure(1));
1.391     raeburn  4832:                                     }
1.396     raeburn  4833:                                     $r->print(&Apache::lonhtmlcommon::end_pick_box().'</div>');
1.391     raeburn  4834:                                 }
                   4835:                             }
                   4836:                         }
                   4837:                     }
                   4838:                 }
                   4839:             }
                   4840:             $r->print(&close_popup_form());
                   4841:         }
1.207     raeburn  4842:     } elsif (($env{'form.action'} eq 'listusers') && 
                   4843:              ($permission->{'view'} || $permission->{'cusr'})) {
1.202     raeburn  4844:         if ($env{'form.phase'} eq 'bulkchange') {
1.351     raeburn  4845:             push(@{$brcrum},
                   4846:                     {href => '/adm/createuser?action=listusers',
                   4847:                      text => "List Users"},
                   4848:                     {href => "/adm/createuser",
                   4849:                      text => "Result",
                   4850:                      help => 'Course_View_Class_List'});
                   4851:             $bread_crumbs_component = 'Update Users';
                   4852:             $args = {bread_crumbs           => $brcrum,
                   4853:                      bread_crumbs_component => $bread_crumbs_component};
                   4854:             $r->print(&header(undef,$args));
1.202     raeburn  4855:             my $setting = $env{'form.roletype'};
                   4856:             my $choice = $env{'form.bulkaction'};
                   4857:             if ($permission->{'cusr'}) {
1.336     raeburn  4858:                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice,$crstype);
1.221     raeburn  4859:             } else {
                   4860:                 $r->print(&mt('You are not authorized to make bulk changes to user roles'));
1.223     raeburn  4861:                 $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
1.202     raeburn  4862:             }
                   4863:         } else {
1.351     raeburn  4864:             push(@{$brcrum},
                   4865:                     {href => '/adm/createuser?action=listusers',
                   4866:                      text => "List Users",
                   4867:                      help => 'Course_View_Class_List'});
                   4868:             $bread_crumbs_component = 'List Users';
                   4869:             $args = {bread_crumbs           => $brcrum,
                   4870:                      bread_crumbs_component => $bread_crumbs_component};
1.202     raeburn  4871:             my ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles);
                   4872:             my $formname = 'studentform';
1.364     raeburn  4873:             my $hidecall = "hide_searching();";
1.321     raeburn  4874:             if (($context eq 'domain') && (($env{'form.roletype'} eq 'course') ||
                   4875:                 ($env{'form.roletype'} eq 'community'))) {
                   4876:                 if ($env{'form.roletype'} eq 'course') {
                   4877:                     ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles) = 
                   4878:                         &Apache::lonuserutils::courses_selector($env{'request.role.domain'},
                   4879:                                                                 $formname);
                   4880:                 } elsif ($env{'form.roletype'} eq 'community') {
                   4881:                     $cb_jscript = 
                   4882:                         &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
                   4883:                     my %elements = (
                   4884:                                       coursepick => 'radio',
                   4885:                                       coursetotal => 'text',
                   4886:                                       courselist => 'text',
                   4887:                                    );
                   4888:                     $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   4889:                 }
1.364     raeburn  4890:                 $jscript .= &verify_user_display($context)."\n".
                   4891:                             &Apache::loncommon::check_uncheck_jscript();
1.202     raeburn  4892:                 my $js = &add_script($jscript).$cb_jscript;
                   4893:                 my $loadcode = 
                   4894:                     &Apache::lonuserutils::course_selector_loadcode($formname);
                   4895:                 if ($loadcode ne '') {
1.364     raeburn  4896:                     $args->{add_entries} = {onload => "$loadcode;$hidecall"};
                   4897:                 } else {
                   4898:                     $args->{add_entries} = {onload => $hidecall};
1.202     raeburn  4899:                 }
1.351     raeburn  4900:                 $r->print(&header($js,$args));
1.191     raeburn  4901:             } else {
1.364     raeburn  4902:                 $args->{add_entries} = {onload => $hidecall};
                   4903:                 $jscript = &verify_user_display($context).
                   4904:                            &Apache::loncommon::check_uncheck_jscript(); 
                   4905:                 $r->print(&header(&add_script($jscript),$args));
1.191     raeburn  4906:             }
1.202     raeburn  4907:             &Apache::lonuserutils::print_userlist($r,undef,$permission,$context,
1.375     raeburn  4908:                          $formname,$totcodes,$codetitles,$idlist,$idlist_titles,
                   4909:                          $showcredits);
1.191     raeburn  4910:         }
1.213     raeburn  4911:     } elsif ($env{'form.action'} eq 'drop' && $permission->{'cusr'}) {
1.318     raeburn  4912:         my $brtext;
                   4913:         if ($crstype eq 'Community') {
                   4914:             $brtext = 'Drop Members';
                   4915:         } else {
                   4916:             $brtext = 'Drop Students';
                   4917:         }
1.351     raeburn  4918:         push(@{$brcrum},
                   4919:                 {href => '/adm/createuser?action=drop',
                   4920:                  text => $brtext,
                   4921:                  help => 'Course_Drop_Student'});
                   4922:         if ($env{'form.state'} eq 'done') {
                   4923:             push(@{$brcrum},
                   4924:                      {href=>'/adm/createuser?action=drop',
                   4925:                       text=>"Result"});
                   4926:         }
                   4927:         $bread_crumbs_component = $brtext;
                   4928:         $args = {bread_crumbs           => $brcrum,
                   4929:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4930:         $r->print(&header(undef,$args));
1.213     raeburn  4931:         if (!exists($env{'form.state'})) {
1.318     raeburn  4932:             &Apache::lonuserutils::print_drop_menu($r,$context,$permission,$crstype);
1.213     raeburn  4933:         } elsif ($env{'form.state'} eq 'done') {
                   4934:             &Apache::lonuserutils::update_user_list($r,$context,undef,
                   4935:                                                     $env{'form.action'});
                   4936:         }
1.202     raeburn  4937:     } elsif ($env{'form.action'} eq 'dateselect') {
                   4938:         if ($permission->{'cusr'}) {
1.351     raeburn  4939:             $r->print(&header(undef,{'no_nav_bar' => 1}).
1.375     raeburn  4940:                       &Apache::lonuserutils::date_section_selector($context,$permission,
                   4941:                                                                    $crstype,$showcredits));
1.202     raeburn  4942:         } else {
1.351     raeburn  4943:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   4944:                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>'); 
1.202     raeburn  4945:         }
1.237     raeburn  4946:     } elsif ($env{'form.action'} eq 'selfenroll') {
1.398     raeburn  4947:         if ($permission->{selfenrolladmin}) {
                   4948:             my $cid = $env{'request.course.id'};
                   4949:             my $cdom = $env{'course.'.$cid.'.domain'};
                   4950:             my $cnum = $env{'course.'.$cid.'.num'};
                   4951:             my %currsettings = (
                   4952:                 selfenroll_types              => $env{'course.'.$cid.'.internal.selfenroll_types'},
                   4953:                 selfenroll_registered         => $env{'course.'.$cid.'.internal.selfenroll_registered'},
                   4954:                 selfenroll_section            => $env{'course.'.$cid.'.internal.selfenroll_section'},
                   4955:                 selfenroll_notifylist         => $env{'course.'.$cid.'.internal.selfenroll_notifylist'},
                   4956:                 selfenroll_approval           => $env{'course.'.$cid.'.internal.selfenroll_approval'},
                   4957:                 selfenroll_limit              => $env{'course.'.$cid.'.internal.selfenroll_limit'},
                   4958:                 selfenroll_cap                => $env{'course.'.$cid.'.internal.selfenroll_cap'},
                   4959:                 selfenroll_start_date         => $env{'course.'.$cid.'.internal.selfenroll_start_date'},
                   4960:                 selfenroll_end_date           => $env{'course.'.$cid.'.internal.selfenroll_end_date'},
                   4961:                 selfenroll_start_access       => $env{'course.'.$cid.'.internal.selfenroll_start_access'},
                   4962:                 selfenroll_end_access         => $env{'course.'.$cid.'.internal.selfenroll_end_access'},
                   4963:                 default_enrollment_start_date => $env{'course.'.$cid.'.default_enrollment_start_date'},
                   4964:                 default_enrollment_end_date   => $env{'course.'.$cid.'.default_enrollment_end_date'},
1.400     raeburn  4965:                 uniquecode                    => $env{'course.'.$cid.'.internal.uniquecode'},
1.398     raeburn  4966:             );
                   4967:             push(@{$brcrum},
                   4968:                     {href => '/adm/createuser?action=selfenroll',
                   4969:                      text => "Configure Self-enrollment",
                   4970:                      help => 'Course_Self_Enrollment'});
                   4971:             if (!exists($env{'form.state'})) {
                   4972:                 $args = { bread_crumbs           => $brcrum,
                   4973:                           bread_crumbs_component => 'Configure Self-enrollment'};
                   4974:                 $r->print(&header(undef,$args));
                   4975:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
                   4976:                 &print_selfenroll_menu($r,'course',$cid,$cdom,$cnum,\%currsettings);
                   4977:             } elsif ($env{'form.state'} eq 'done') {
                   4978:                 push (@{$brcrum},
                   4979:                           {href=>'/adm/createuser?action=selfenroll',
                   4980:                            text=>"Result"});
                   4981:                 $args = { bread_crumbs           => $brcrum,
                   4982:                           bread_crumbs_component => 'Self-enrollment result'};
                   4983:                 $r->print(&header(undef,$args));
                   4984:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
1.400     raeburn  4985:                 &update_selfenroll_config($r,$cid,$cdom,$cnum,$context,$crstype,\%currsettings);
1.398     raeburn  4986:             }
                   4987:         } else {
                   4988:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   4989:                      '<span class="LC_error">'.&mt('You do not have permission to configure self-enrollment').'</span>');
1.237     raeburn  4990:         }
1.277     raeburn  4991:     } elsif ($env{'form.action'} eq 'selfenrollqueue') {
1.406.2.6  raeburn  4992:         if ($permission->{selfenrolladmin}) {
1.351     raeburn  4993:             push(@{$brcrum},
                   4994:                      {href => '/adm/createuser?action=selfenrollqueue',
1.406.2.6  raeburn  4995:                       text => 'Enrollment requests',
1.351     raeburn  4996:                       help => 'Course_Self_Enrollment'});
1.406.2.6  raeburn  4997:             $bread_crumbs_component = 'Enrollment requests';
                   4998:             if ($env{'form.state'} eq 'done') {
                   4999:                 push(@{$brcrum},
                   5000:                          {href => '/adm/createuser?action=selfenrollqueue',
                   5001:                           text => 'Result',
                   5002:                           help => 'Course_Self_Enrollment'});
                   5003:                 $bread_crumbs_component = 'Enrollment result';
                   5004:             }
                   5005:             $args = { bread_crumbs           => $brcrum,
                   5006:                       bread_crumbs_component => $bread_crumbs_component};
                   5007:             $r->print(&header(undef,$args));
                   5008:             my $cid = $env{'request.course.id'};
                   5009:             my $cdom = $env{'course.'.$cid.'.domain'};
                   5010:             my $cnum = $env{'course.'.$cid.'.num'};
                   5011:             my $coursedesc = $env{'course.'.$cid.'.description'};
                   5012:             if (!exists($env{'form.state'})) {
                   5013:                 $r->print('<h3>'.&mt('Pending enrollment requests').'</h3>'."\n");
                   5014:                 $r->print(&Apache::loncoursequeueadmin::display_queued_requests($context,
                   5015:                                                                                 $cdom,$cnum));
                   5016:             } elsif ($env{'form.state'} eq 'done') {
                   5017:                 $r->print('<h3>'.&mt('Enrollment request processing').'</h3>'."\n");
                   5018:                 $r->print(&Apache::loncoursequeueadmin::update_request_queue($context,
                   5019:                               $cdom,$cnum,$coursedesc));
                   5020:             }
                   5021:         } else {
                   5022:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5023:                      '<span class="LC_error">'.&mt('You do not have permission to manage self-enrollment').'</span>');
1.277     raeburn  5024:         }
1.406.2.6  raeburn  5025: 
1.239     raeburn  5026:     } elsif ($env{'form.action'} eq 'changelogs') {
1.406.2.6  raeburn  5027:         if ($permission->{cusr} || $permission->{view}) {
                   5028:             &print_userchangelogs_display($r,$context,$permission,$brcrum);
                   5029:         } else {
                   5030:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5031:                      '<span class="LC_error">'.&mt('You do not have permission to view change logs').'</span>');
                   5032:         }
1.190     raeburn  5033:     } else {
1.351     raeburn  5034:         $bread_crumbs_component = 'User Management';
                   5035:         $args = { bread_crumbs           => $brcrum,
                   5036:                   bread_crumbs_component => $bread_crumbs_component};
                   5037:         $r->print(&header(undef,$args));
1.318     raeburn  5038:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  5039:     }
1.351     raeburn  5040:     $r->print(&Apache::loncommon::end_page());
1.190     raeburn  5041:     return OK;
                   5042: }
                   5043: 
                   5044: sub header {
1.351     raeburn  5045:     my ($jscript,$args) = @_;
1.190     raeburn  5046:     my $start_page;
1.351     raeburn  5047:     if (ref($args) eq 'HASH') {
                   5048:         $start_page=&Apache::loncommon::start_page('User Management',$jscript,$args);
1.190     raeburn  5049:     } else {
1.351     raeburn  5050:         $start_page=&Apache::loncommon::start_page('User Management',$jscript);
1.190     raeburn  5051:     }
                   5052:     return $start_page;
                   5053: }
1.2       www      5054: 
1.191     raeburn  5055: sub add_script {
                   5056:     my ($js) = @_;
1.301     bisitz   5057:     return '<script type="text/javascript">'."\n"
                   5058:           .'// <![CDATA['."\n"
                   5059:           .$js."\n"
                   5060:           .'// ]]>'."\n"
                   5061:           .'</script>'."\n";
1.191     raeburn  5062: }
                   5063: 
1.391     raeburn  5064: sub usernamerequest_javascript {
                   5065:     my $js = <<ENDJS;
                   5066: 
                   5067: function openusernamereqdisplay(dom,uname,queue) {
                   5068:     var url = '/adm/createuser?action=displayuserreq';
                   5069:     url += '&domain='+dom+'&username='+uname+'&queue='+queue;
                   5070:     var title = 'Account_Request_Browser';
                   5071:     var options = 'scrollbars=1,resizable=1,menubar=0';
                   5072:     options += ',width=700,height=600';
                   5073:     var stdeditbrowser = open(url,title,options,'1');
                   5074:     stdeditbrowser.focus();
                   5075:     return;
                   5076: }
                   5077:  
                   5078: ENDJS
                   5079: }
                   5080: 
                   5081: sub close_popup_form {
                   5082:     my $close= &mt('Close Window');
                   5083:     return << "END";
                   5084: <p><form name="displayreq" action="" method="post">
                   5085: <input type="button" name="closeme" value="$close" onclick="javascript:self.close();" />
                   5086: </form></p>
                   5087: END
                   5088: }
                   5089: 
1.202     raeburn  5090: sub verify_user_display {
1.364     raeburn  5091:     my ($context) = @_;
1.374     raeburn  5092:     my %lt = &Apache::lonlocal::texthash (
                   5093:         course    => 'course(s): description, section(s), status',
                   5094:         community => 'community(s): description, section(s), status',
                   5095:         author    => 'author',
                   5096:     );
1.364     raeburn  5097:     my $photos;
                   5098:     if (($context eq 'course') && $env{'request.course.id'}) {
                   5099:         $photos = $env{'course.'.$env{'request.course.id'}.'.internal.showphoto'};
                   5100:     }
1.202     raeburn  5101:     my $output = <<"END";
                   5102: 
1.364     raeburn  5103: function hide_searching() {
                   5104:     if (document.getElementById('searching')) {
                   5105:         document.getElementById('searching').style.display = 'none';
                   5106:     }
                   5107:     return;
                   5108: }
                   5109: 
1.202     raeburn  5110: function display_update() {
                   5111:     document.studentform.action.value = 'listusers';
                   5112:     document.studentform.phase.value = 'display';
                   5113:     document.studentform.submit();
                   5114: }
                   5115: 
1.364     raeburn  5116: function updateCols(caller) {
                   5117:     var context = '$context';
                   5118:     var photos = '$photos';
                   5119:     if (caller == 'Status') {
1.374     raeburn  5120:         if ((context == 'domain') && 
                   5121:             ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   5122:              (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community'))) {
1.364     raeburn  5123:             document.getElementById('showcolstatus').checked = false;
                   5124:             document.getElementById('showcolstatus').disabled = 'disabled';
                   5125:             document.getElementById('showcolstart').checked = false;
                   5126:             document.getElementById('showcolend').checked = false;
1.374     raeburn  5127:         } else {
                   5128:             if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   5129:                 document.getElementById('showcolstatus').checked = true;
                   5130:                 document.getElementById('showcolstatus').disabled = '';
                   5131:                 document.getElementById('showcolstart').checked = true;
                   5132:                 document.getElementById('showcolend').checked = true;
                   5133:             } else {
                   5134:                 document.getElementById('showcolstatus').checked = false;
                   5135:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   5136:                 document.getElementById('showcolstart').checked = false;
                   5137:                 document.getElementById('showcolend').checked = false;
                   5138:             }
1.364     raeburn  5139:         }
                   5140:     }
                   5141:     if (caller == 'output') {
                   5142:         if (photos == 1) {
                   5143:             if (document.getElementById('showcolphoto')) {
                   5144:                 var photoitem = document.getElementById('showcolphoto');
                   5145:                 if (document.studentform.output.options[document.studentform.output.selectedIndex].value == 'html') {
                   5146:                     photoitem.checked = true;
                   5147:                     photoitem.disabled = '';
                   5148:                 } else {
                   5149:                     photoitem.checked = false;
                   5150:                     photoitem.disabled = 'disabled';
                   5151:                 }
                   5152:             }
                   5153:         }
                   5154:     }
                   5155:     if (caller == 'showrole') {
1.371     raeburn  5156:         if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any') ||
                   5157:             (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'cr')) {
1.364     raeburn  5158:             document.getElementById('showcolrole').checked = true;
                   5159:             document.getElementById('showcolrole').disabled = '';
                   5160:         } else {
                   5161:             document.getElementById('showcolrole').checked = false;
                   5162:             document.getElementById('showcolrole').disabled = 'disabled';
                   5163:         }
1.374     raeburn  5164:         if (context == 'domain') {
1.382     raeburn  5165:             var quotausageshow = 0;
1.374     raeburn  5166:             if ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   5167:                 (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community')) {
                   5168:                 document.getElementById('showcolstatus').checked = false;
                   5169:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   5170:                 document.getElementById('showcolstart').checked = false;
                   5171:                 document.getElementById('showcolend').checked = false;
                   5172:             } else {
                   5173:                 if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   5174:                     document.getElementById('showcolstatus').checked = true;
                   5175:                     document.getElementById('showcolstatus').disabled = '';
                   5176:                     document.getElementById('showcolstart').checked = true;
                   5177:                     document.getElementById('showcolend').checked = true;
                   5178:                 }
                   5179:             }
                   5180:             if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'domain') {
                   5181:                 document.getElementById('showcolextent').disabled = 'disabled';
                   5182:                 document.getElementById('showcolextent').checked = 'false';
                   5183:                 document.getElementById('showextent').style.display='none';
                   5184:                 document.getElementById('showcoltextextent').innerHTML = '';
1.382     raeburn  5185:                 if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'au') ||
                   5186:                     (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any')) {
                   5187:                     if (document.getElementById('showcolauthorusage')) {
                   5188:                         document.getElementById('showcolauthorusage').disabled = '';
                   5189:                     }
                   5190:                     if (document.getElementById('showcolauthorquota')) {
                   5191:                         document.getElementById('showcolauthorquota').disabled = '';
                   5192:                     }
                   5193:                     quotausageshow = 1;
                   5194:                 }
1.374     raeburn  5195:             } else {
                   5196:                 document.getElementById('showextent').style.display='block';
                   5197:                 document.getElementById('showextent').style.textAlign='left';
                   5198:                 document.getElementById('showextent').style.textFace='normal';
                   5199:                 if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'author') {
                   5200:                     document.getElementById('showcolextent').disabled = '';
                   5201:                     document.getElementById('showcolextent').checked = 'true';
                   5202:                     document.getElementById('showcoltextextent').innerHTML="$lt{'author'}";
                   5203:                 } else {
                   5204:                     document.getElementById('showcolextent').disabled = '';
                   5205:                     document.getElementById('showcolextent').checked = 'true';
                   5206:                     if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community') {
                   5207:                         document.getElementById('showcoltextextent').innerHTML="$lt{'community'}";
                   5208:                     } else {
                   5209:                         document.getElementById('showcoltextextent').innerHTML="$lt{'course'}";
                   5210:                     }
                   5211:                 }
                   5212:             }
1.382     raeburn  5213:             if (quotausageshow == 0)  {
                   5214:                 if (document.getElementById('showcolauthorusage')) {
                   5215:                     document.getElementById('showcolauthorusage').checked = false;
                   5216:                     document.getElementById('showcolauthorusage').disabled = 'disabled';
                   5217:                 }
                   5218:                 if (document.getElementById('showcolauthorquota')) {
                   5219:                     document.getElementById('showcolauthorquota').checked = false;
                   5220:                     document.getElementById('showcolauthorquota').disabled = 'disabled';
                   5221:                 }
                   5222:             }
1.374     raeburn  5223:         }
1.364     raeburn  5224:     }
                   5225:     return;
                   5226: }
                   5227: 
1.202     raeburn  5228: END
                   5229:     return $output;
                   5230: 
                   5231: }
                   5232: 
1.190     raeburn  5233: ###############################################################
                   5234: ###############################################################
                   5235: #  Menu Phase One
                   5236: sub print_main_menu {
1.318     raeburn  5237:     my ($permission,$context,$crstype) = @_;
                   5238:     my $linkcontext = $context;
                   5239:     my $stuterm = lc(&Apache::lonnet::plaintext('st',$crstype));
                   5240:     if (($context eq 'course') && ($crstype eq 'Community')) {
                   5241:         $linkcontext = lc($crstype);
                   5242:         $stuterm = 'Members';
                   5243:     }
1.208     raeburn  5244:     my %links = (
1.298     droeschl 5245:                 domain => {
                   5246:                             upload     => 'Upload a File of Users',
                   5247:                             singleuser => 'Add/Modify a User',
                   5248:                             listusers  => 'Manage Users',
                   5249:                             },
                   5250:                 author => {
                   5251:                             upload     => 'Upload a File of Co-authors',
                   5252:                             singleuser => 'Add/Modify a Co-author',
                   5253:                             listusers  => 'Manage Co-authors',
                   5254:                             },
                   5255:                 course => {
                   5256:                             upload     => 'Upload a File of Course Users',
                   5257:                             singleuser => 'Add/Modify a Course User',
1.354     www      5258:                             listusers  => 'List and Modify Multiple Course Users',
1.298     droeschl 5259:                             },
1.318     raeburn  5260:                 community => {
                   5261:                             upload     => 'Upload a File of Community Users',
                   5262:                             singleuser => 'Add/Modify a Community User',
1.354     www      5263:                             listusers  => 'List and Modify Multiple Community Users',
1.318     raeburn  5264:                            },
                   5265:                 );
                   5266:      my %linktitles = (
                   5267:                 domain => {
                   5268:                             singleuser => 'Add a user to the domain, and/or a course or community in the domain.',
                   5269:                             listusers  => 'Show and manage users in this domain.',
                   5270:                             },
                   5271:                 author => {
                   5272:                             singleuser => 'Add a user with a co- or assistant author role.',
                   5273:                             listusers  => 'Show and manage co- or assistant authors.',
                   5274:                             },
                   5275:                 course => {
                   5276:                             singleuser => 'Add a user with a certain role to this course.',
                   5277:                             listusers  => 'Show and manage users in this course.',
                   5278:                             },
                   5279:                 community => {
                   5280:                             singleuser => 'Add a user with a certain role to this community.',
                   5281:                             listusers  => 'Show and manage users in this community.',
                   5282:                            },
1.298     droeschl 5283:                 );
1.406.2.6  raeburn  5284:   if ($linkcontext eq 'domain') {
                   5285:       unless ($permission->{'cusr'}) {
                   5286:           $links{'domain'}{'singleuser'} = 'View a User';
                   5287:           $linktitles{'domain'}{'singleuser'} = 'View information about a user in the domain';
                   5288: 
                   5289:       }
                   5290:   } elsif ($linkcontext eq 'course') {
                   5291:       unless ($permission->{'cusr'}) {
                   5292:           $links{'course'}{'singleuser'} = 'View a Course User';
                   5293:           $linktitles{'course'}{'singleuser'} = 'View information about a user in this course';
                   5294:           $links{'course'}{'listusers'} = 'List Course Users';
                   5295:           $linktitles{'course'}{'listusers'} = 'Show information about users in this course';
                   5296:       }
                   5297:   } elsif ($linkcontext eq 'community') {
                   5298:       unless ($permission->{'cusr'}) {
                   5299:           $links{'community'}{'singleuser'} = 'View a Community User';
                   5300:           $linktitles{'community'}{'singleuser'} = 'View information about a user in this community';
                   5301:           $links{'community'}{'listusers'} = 'List Community Users';
                   5302:           $linktitles{'community'}{'listusers'} = 'Show information about users in this community';
                   5303:       }
                   5304:   }
1.298     droeschl 5305:   my @menu = ( {categorytitle => 'Single Users', 
                   5306:          items =>
                   5307:          [
                   5308:             {
1.318     raeburn  5309:              linktext => $links{$linkcontext}{'singleuser'},
1.298     droeschl 5310:              icon => 'edit-redo.png',
                   5311:              #help => 'Course_Change_Privileges',
                   5312:              url => '/adm/createuser?action=singleuser',
1.406.2.6  raeburn  5313:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  5314:              linktitle => $linktitles{$linkcontext}{'singleuser'},
1.298     droeschl 5315:             },
                   5316:          ]},
                   5317: 
                   5318:          {categorytitle => 'Multiple Users',
                   5319:          items => 
                   5320:          [
                   5321:             {
1.318     raeburn  5322:              linktext => $links{$linkcontext}{'upload'},
1.340     wenzelju 5323:              icon => 'uplusr.png',
1.298     droeschl 5324:              #help => 'Course_Create_Class_List',
                   5325:              url => '/adm/createuser?action=upload',
                   5326:              permission => $permission->{'cusr'},
                   5327:              linktitle => 'Upload a CSV or a text file containing users.',
                   5328:             },
                   5329:             {
1.318     raeburn  5330:              linktext => $links{$linkcontext}{'listusers'},
1.340     wenzelju 5331:              icon => 'mngcu.png',
1.298     droeschl 5332:              #help => 'Course_View_Class_List',
                   5333:              url => '/adm/createuser?action=listusers',
                   5334:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  5335:              linktitle => $linktitles{$linkcontext}{'listusers'}, 
1.298     droeschl 5336:             },
                   5337: 
                   5338:          ]},
                   5339: 
                   5340:          {categorytitle => 'Administration',
                   5341:          items => [ ]},
                   5342:        );
1.406.2.5  raeburn  5343: 
1.265     mielkec  5344:     if ($context eq 'domain'){
1.406.2.5  raeburn  5345:         push(@{  $menu[0]->{items} }, # Single Users
                   5346:             {
                   5347:              linktext => 'User Access Log',
                   5348:              icon => 'document-properties.png',
1.406.2.8  raeburn  5349:              #help => 'Domain_User_Access_Logs',
1.406.2.5  raeburn  5350:              url => '/adm/createuser?action=accesslogs',
                   5351:              permission => $permission->{'activity'},
                   5352:              linktitle => 'View user access log.',
                   5353:             }
                   5354:         );
1.298     droeschl 5355:         
                   5356:         push(@{ $menu[2]->{items} }, #Category: Administration
                   5357:             {
                   5358:              linktext => 'Custom Roles',
                   5359:              icon => 'emblem-photos.png',
                   5360:              #help => 'Course_Editing_Custom_Roles',
                   5361:              url => '/adm/createuser?action=custom',
                   5362:              permission => $permission->{'custom'},
                   5363:              linktitle => 'Configure a custom role.',
                   5364:             },
1.362     raeburn  5365:             {
                   5366:              linktext => 'Authoring Space Requests',
                   5367:              icon => 'selfenrl-queue.png',
                   5368:              #help => 'Domain_Role_Approvals',
                   5369:              url => '/adm/createuser?action=processauthorreq',
                   5370:              permission => $permission->{'cusr'},
                   5371:              linktitle => 'Approve or reject author role requests',
                   5372:             },
1.363     raeburn  5373:             {
1.391     raeburn  5374:              linktext => 'LON-CAPA Account Requests',
                   5375:              icon => 'list-add.png',
                   5376:              #help => 'Domain_Username_Approvals',
                   5377:              url => '/adm/createuser?action=processusernamereq',
                   5378:              permission => $permission->{'cusr'},
                   5379:              linktitle => 'Approve or reject LON-CAPA account requests',
                   5380:             },
                   5381:             {
1.363     raeburn  5382:              linktext => 'Change Log',
                   5383:              icon => 'document-properties.png',
                   5384:              #help => 'Course_User_Logs',
                   5385:              url => '/adm/createuser?action=changelogs',
1.406.2.6  raeburn  5386:              permission => ($permission->{'cusr'} || $permission->{'view'}),
1.363     raeburn  5387:              linktitle => 'View change log.',
                   5388:             },
1.298     droeschl 5389:         );
                   5390:         
1.265     mielkec  5391:     }elsif ($context eq 'course'){
1.298     droeschl 5392:         my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
1.318     raeburn  5393: 
                   5394:         my %linktext = (
                   5395:                          'Course'    => {
                   5396:                                           single => 'Add/Modify a Student', 
                   5397:                                           drop   => 'Drop Students',
                   5398:                                           groups => 'Course Groups',
                   5399:                                         },
                   5400:                          'Community' => {
                   5401:                                           single => 'Add/Modify a Member', 
                   5402:                                           drop   => 'Drop Members',
                   5403:                                           groups => 'Community Groups',
                   5404:                                         },
                   5405:                        );
                   5406: 
                   5407:         my %linktitle = (
                   5408:             'Course' => {
                   5409:                   single => 'Add a user with the role of student to this course',
                   5410:                   drop   => 'Remove a student from this course.',
                   5411:                   groups => 'Manage course groups',
                   5412:                         },
                   5413:             'Community' => {
                   5414:                   single => 'Add a user with the role of member to this community',
                   5415:                   drop   => 'Remove a member from this community.',
                   5416:                   groups => 'Manage community groups',
                   5417:                            },
                   5418:         );
                   5419: 
1.298     droeschl 5420:         push(@{ $menu[0]->{items} }, #Category: Single Users
                   5421:             {   
1.318     raeburn  5422:              linktext => $linktext{$crstype}{'single'},
1.298     droeschl 5423:              #help => 'Course_Add_Student',
                   5424:              icon => 'list-add.png',
                   5425:              url => '/adm/createuser?action=singlestudent',
                   5426:              permission => $permission->{'cusr'},
1.318     raeburn  5427:              linktitle => $linktitle{$crstype}{'single'},
1.298     droeschl 5428:             },
                   5429:         );
                   5430:         
                   5431:         push(@{ $menu[1]->{items} }, #Category: Multiple Users 
                   5432:             {
1.318     raeburn  5433:              linktext => $linktext{$crstype}{'drop'},
1.298     droeschl 5434:              icon => 'edit-undo.png',
                   5435:              #help => 'Course_Drop_Student',
                   5436:              url => '/adm/createuser?action=drop',
                   5437:              permission => $permission->{'cusr'},
1.318     raeburn  5438:              linktitle => $linktitle{$crstype}{'drop'},
1.298     droeschl 5439:             },
                   5440:         );
                   5441:         push(@{ $menu[2]->{items} }, #Category: Administration
                   5442:             {    
                   5443:              linktext => 'Custom Roles',
                   5444:              icon => 'emblem-photos.png',
                   5445:              #help => 'Course_Editing_Custom_Roles',
                   5446:              url => '/adm/createuser?action=custom',
                   5447:              permission => $permission->{'custom'},
                   5448:              linktitle => 'Configure a custom role.',
                   5449:             },
                   5450:             {
1.318     raeburn  5451:              linktext => $linktext{$crstype}{'groups'},
1.333     wenzelju 5452:              icon => 'grps.png',
1.298     droeschl 5453:              #help => 'Course_Manage_Group',
                   5454:              url => '/adm/coursegroups?refpage=cusr',
                   5455:              permission => $permission->{'grp_manage'},
1.318     raeburn  5456:              linktitle => $linktitle{$crstype}{'groups'},
1.298     droeschl 5457:             },
                   5458:             {
1.328     wenzelju 5459:              linktext => 'Change Log',
1.298     droeschl 5460:              icon => 'document-properties.png',
                   5461:              #help => 'Course_User_Logs',
                   5462:              url => '/adm/createuser?action=changelogs',
1.406.2.6  raeburn  5463:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.298     droeschl 5464:              linktitle => 'View change log.',
                   5465:             },
                   5466:         );
1.277     raeburn  5467:         if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'}) {
1.298     droeschl 5468:             push(@{ $menu[2]->{items} },
1.398     raeburn  5469:                     {
1.298     droeschl 5470:                      linktext => 'Enrollment Requests',
                   5471:                      icon => 'selfenrl-queue.png',
                   5472:                      #help => 'Course_Approve_Selfenroll',
                   5473:                      url => '/adm/createuser?action=selfenrollqueue',
1.398     raeburn  5474:                      permission => $permission->{'selfenrolladmin'},
1.298     droeschl 5475:                      linktitle =>'Approve or reject enrollment requests.',
                   5476:                     },
                   5477:             );
1.277     raeburn  5478:         }
1.298     droeschl 5479:         
1.265     mielkec  5480:         if (!exists($permission->{'cusr_section'})){
1.320     raeburn  5481:             if ($crstype ne 'Community') {
                   5482:                 push(@{ $menu[2]->{items} },
                   5483:                     {
                   5484:                      linktext => 'Automated Enrollment',
                   5485:                      icon => 'roles.png',
                   5486:                      #help => 'Course_Automated_Enrollment',
                   5487:                      permission => (&Apache::lonnet::auto_run($cnum,$cdom)
1.406.2.6  raeburn  5488:                                          && (($permission->{'cusr'}) ||
                   5489:                                              ($permission->{'view'}))),
1.320     raeburn  5490:                      url  => '/adm/populate',
                   5491:                      linktitle => 'Automated enrollment manager.',
                   5492:                     }
                   5493:                 );
                   5494:             }
                   5495:             push(@{ $menu[2]->{items} }, 
1.298     droeschl 5496:                 {
                   5497:                  linktext => 'User Self-Enrollment',
1.342     wenzelju 5498:                  icon => 'self_enroll.png',
1.298     droeschl 5499:                  #help => 'Course_Self_Enrollment',
                   5500:                  url => '/adm/createuser?action=selfenroll',
1.398     raeburn  5501:                  permission => $permission->{'selfenrolladmin'},
1.317     bisitz   5502:                  linktitle => 'Configure user self-enrollment.',
1.298     droeschl 5503:                 },
                   5504:             );
                   5505:         }
1.363     raeburn  5506:     } elsif ($context eq 'author') {
1.370     raeburn  5507:         push(@{ $menu[2]->{items} }, #Category: Administration
1.363     raeburn  5508:             {
                   5509:              linktext => 'Change Log',
                   5510:              icon => 'document-properties.png',
                   5511:              #help => 'Course_User_Logs',
                   5512:              url => '/adm/createuser?action=changelogs',
                   5513:              permission => $permission->{'cusr'},
                   5514:              linktitle => 'View change log.',
                   5515:             },
1.370     raeburn  5516:         );
1.363     raeburn  5517:     }
                   5518:     return Apache::lonhtmlcommon::generate_menu(@menu);
1.250     raeburn  5519: #               { text => 'View Log-in History',
                   5520: #                 help => 'Course_User_Logins',
                   5521: #                 action => 'logins',
                   5522: #                 permission => $permission->{'cusr'},
                   5523: #               });
1.190     raeburn  5524: }
                   5525: 
1.189     albertel 5526: sub restore_prev_selections {
                   5527:     my %saveable_parameters = ('srchby'   => 'scalar',
                   5528: 			       'srchin'   => 'scalar',
                   5529: 			       'srchtype' => 'scalar',
                   5530: 			       );
                   5531:     &Apache::loncommon::store_settings('user','user_picker',
                   5532: 				       \%saveable_parameters);
                   5533:     &Apache::loncommon::restore_settings('user','user_picker',
                   5534: 					 \%saveable_parameters);
                   5535: }
                   5536: 
1.237     raeburn  5537: sub print_selfenroll_menu {
1.406.2.6  raeburn  5538:     my ($r,$context,$cid,$cdom,$cnum,$currsettings,$additional,$readonly) = @_;
1.322     raeburn  5539:     my $crstype = &Apache::loncommon::course_type();
1.398     raeburn  5540:     my $formname = 'selfenroll';
1.237     raeburn  5541:     my $nolink = 1;
1.398     raeburn  5542:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
1.237     raeburn  5543:     my $groupslist = &Apache::lonuserutils::get_groupslist();
                   5544:     my $setsec_js = 
                   5545:         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
1.249     raeburn  5546:     my %alerts = &Apache::lonlocal::texthash(
                   5547:         acto => 'Activation of self-enrollment was selected for the following domain(s)',
                   5548:         butn => 'but no user types have been checked.',
                   5549:         wilf => "Please uncheck 'activate' or check at least one type.",
                   5550:     );
1.406.2.6  raeburn  5551:     my $disabled;
                   5552:     if ($readonly) {
                   5553:        $disabled = ' disabled="disabled"';
                   5554:     }
1.405     damieng  5555:     &js_escape(\%alerts);
1.249     raeburn  5556:     my $selfenroll_js = <<"ENDSCRIPT";
                   5557: function update_types(caller,num) {
                   5558:     var delidx = getIndexByName('selfenroll_delete');
                   5559:     var actidx = getIndexByName('selfenroll_activate');
                   5560:     if (caller == 'selfenroll_all') {
                   5561:         var selall;
                   5562:         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5563:             if (document.$formname.selfenroll_all[i].checked) {
                   5564:                 selall = document.$formname.selfenroll_all[i].value;
                   5565:             }
                   5566:         }
                   5567:         if (selall == 1) {
                   5568:             if (delidx != -1) {
                   5569:                 if (document.$formname.selfenroll_delete.length) {
                   5570:                     for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5571:                         document.$formname.selfenroll_delete[j].checked = true;
                   5572:                     }
                   5573:                 } else {
                   5574:                     document.$formname.elements[delidx].checked = true;
                   5575:                 }
                   5576:             }
                   5577:             if (actidx != -1) {
                   5578:                 if (document.$formname.selfenroll_activate.length) {
                   5579:                     for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5580:                         document.$formname.selfenroll_activate[j].checked = false;
                   5581:                     }
                   5582:                 } else {
                   5583:                     document.$formname.elements[actidx].checked = false;
                   5584:                 }
                   5585:             }
                   5586:             document.$formname.selfenroll_newdom.selectedIndex = 0; 
                   5587:         }
                   5588:     }
                   5589:     if (caller == 'selfenroll_activate') {
                   5590:         if (document.$formname.selfenroll_activate.length) {
                   5591:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5592:                 if (document.$formname.selfenroll_activate[j].value == num) {
                   5593:                     if (document.$formname.selfenroll_activate[j].checked) {
                   5594:                         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5595:                             if (document.$formname.selfenroll_all[i].value == '1') {
                   5596:                                 document.$formname.selfenroll_all[i].checked = false;
                   5597:                             }
                   5598:                             if (document.$formname.selfenroll_all[i].value == '0') {
                   5599:                                 document.$formname.selfenroll_all[i].checked = true;
                   5600:                             }
                   5601:                         }
                   5602:                     }
                   5603:                 }
                   5604:             }
                   5605:         } else {
                   5606:             for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5607:                 if (document.$formname.selfenroll_all[i].value == '1') {
                   5608:                     document.$formname.selfenroll_all[i].checked = false;
                   5609:                 }
                   5610:                 if (document.$formname.selfenroll_all[i].value == '0') {
                   5611:                     document.$formname.selfenroll_all[i].checked = true;
                   5612:                 }
                   5613:             }
                   5614:         }
                   5615:     }
                   5616:     if (caller == 'selfenroll_delete') {
                   5617:         if (document.$formname.selfenroll_delete.length) {
                   5618:             for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5619:                 if (document.$formname.selfenroll_delete[j].value == num) {
                   5620:                     if (document.$formname.selfenroll_delete[j].checked) {
                   5621:                         var delindex = getIndexByName('selfenroll_types_'+num);
                   5622:                         if (delindex != -1) { 
                   5623:                             if (document.$formname.elements[delindex].length) {
                   5624:                                 for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5625:                                     document.$formname.elements[delindex][k].checked = false;
                   5626:                                 }
                   5627:                             } else {
                   5628:                                 document.$formname.elements[delindex].checked = false;
                   5629:                             }
                   5630:                         }
                   5631:                     }
                   5632:                 }
                   5633:             }
                   5634:         } else {
                   5635:             if (document.$formname.selfenroll_delete.checked) {
                   5636:                 var delindex = getIndexByName('selfenroll_types_'+num);
                   5637:                 if (delindex != -1) {
                   5638:                     if (document.$formname.elements[delindex].length) {
                   5639:                         for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5640:                             document.$formname.elements[delindex][k].checked = false;
                   5641:                         }
                   5642:                     } else {
                   5643:                         document.$formname.elements[delindex].checked = false;
                   5644:                     }
                   5645:                 }
                   5646:             }
                   5647:         }
                   5648:     }
                   5649:     return;
                   5650: }
                   5651: 
                   5652: function validate_types(form) {
                   5653:     var needaction = new Array();
                   5654:     var countfail = 0;
                   5655:     var actidx = getIndexByName('selfenroll_activate');
                   5656:     if (actidx != -1) {
                   5657:         if (document.$formname.selfenroll_activate.length) {
                   5658:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5659:                 var num = document.$formname.selfenroll_activate[j].value;
                   5660:                 if (document.$formname.selfenroll_activate[j].checked) {
                   5661:                     countfail = check_types(num,countfail,needaction)
                   5662:                 }
                   5663:             }
                   5664:         } else {
                   5665:             if (document.$formname.selfenroll_activate.checked) {
1.398     raeburn  5666:                 var num = document.$formname.selfenroll_activate.value;
1.249     raeburn  5667:                 countfail = check_types(num,countfail,needaction)
                   5668:             }
                   5669:         }
                   5670:     }
                   5671:     if (countfail > 0) {
                   5672:         var msg = "$alerts{'acto'}\\n";
                   5673:         var loopend = needaction.length -1;
                   5674:         if (loopend > 0) {
                   5675:             for (var m=0; m<loopend; m++) {
                   5676:                 msg += needaction[m]+", ";
                   5677:             }
                   5678:         }
                   5679:         msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
                   5680:         alert(msg);
                   5681:         return; 
                   5682:     }
                   5683:     setSections(form);
                   5684: }
                   5685: 
                   5686: function check_types(num,countfail,needaction) {
                   5687:     var typeidx = getIndexByName('selfenroll_types_'+num);
                   5688:     var count = 0;
                   5689:     if (typeidx != -1) {
                   5690:         if (document.$formname.elements[typeidx].length) {
                   5691:             for (var k=0; k<document.$formname.elements[typeidx].length; k++) {
                   5692:                 if (document.$formname.elements[typeidx][k].checked) {
                   5693:                     count ++;
                   5694:                 }
                   5695:             }
                   5696:         } else {
                   5697:             if (document.$formname.elements[typeidx].checked) {
                   5698:                 count ++;
                   5699:             }
                   5700:         }
                   5701:         if (count == 0) {
                   5702:             var domidx = getIndexByName('selfenroll_dom_'+num);
                   5703:             if (domidx != -1) {
                   5704:                 var domname = document.$formname.elements[domidx].value;
                   5705:                 needaction[countfail] = domname;
                   5706:                 countfail ++;
                   5707:             }
                   5708:         }
                   5709:     }
                   5710:     return countfail;
                   5711: }
                   5712: 
1.398     raeburn  5713: function toggleNotify() {
                   5714:     var selfenrollApproval = 0;
                   5715:     if (document.$formname.selfenroll_approval.length) {
                   5716:         for (var i=0; i<document.$formname.selfenroll_approval.length; i++) {
                   5717:             if (document.$formname.selfenroll_approval[i].checked) {
                   5718:                 selfenrollApproval = document.$formname.selfenroll_approval[i].value;
                   5719:                 break;        
                   5720:             }
                   5721:         }
                   5722:     }
                   5723:     if (document.getElementById('notified')) {
                   5724:         if (selfenrollApproval == 0) {
                   5725:             document.getElementById('notified').style.display='none';
                   5726:         } else {
                   5727:             document.getElementById('notified').style.display='block';
                   5728:         }
                   5729:     }
                   5730:     return;
                   5731: }
                   5732: 
1.249     raeburn  5733: function getIndexByName(item) {
                   5734:     for (var i=0;i<document.$formname.elements.length;i++) {
                   5735:         if (document.$formname.elements[i].name == item) {
                   5736:             return i;
                   5737:         }
                   5738:     }
                   5739:     return -1;
                   5740: }
                   5741: ENDSCRIPT
1.256     raeburn  5742: 
1.237     raeburn  5743:     my $output = '<script type="text/javascript">'."\n".
1.301     bisitz   5744:                  '// <![CDATA['."\n".
1.249     raeburn  5745:                  $setsec_js."\n".$selfenroll_js."\n".
1.301     bisitz   5746:                  '// ]]>'."\n".
1.237     raeburn  5747:                  '</script>'."\n".
1.256     raeburn  5748:                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n";
1.400     raeburn  5749:  
                   5750:     my $visactions = &cat_visibility();
                   5751:     my ($cathash,%cattype);
                   5752:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   5753:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   5754:         $cathash = $domconfig{'coursecategories'}{'cats'};
                   5755:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
                   5756:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
1.406     raeburn  5757:         if ($cattype{'auth'} eq '') {
                   5758:             $cattype{'auth'} = 'std';
                   5759:         }
                   5760:         if ($cattype{'unauth'} eq '') {
                   5761:             $cattype{'unauth'} = 'std';
                   5762:         }
1.400     raeburn  5763:     } else {
                   5764:         $cathash = {};
                   5765:         $cattype{'auth'} = 'std';
                   5766:         $cattype{'unauth'} = 'std';
                   5767:     }
                   5768:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
                   5769:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   5770:                   '<br />'.
                   5771:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   5772:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
                   5773:                   '</ul>');
                   5774:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
                   5775:         if ($currsettings->{'uniquecode'}) {
                   5776:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
                   5777:         } else {
                   5778:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   5779:                   '<br />'.
                   5780:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   5781:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
                   5782:                   '</ul><br />');
                   5783:         }
                   5784:     } else {
                   5785:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
                   5786:         if (ref($visactions) eq 'HASH') {
                   5787:             if ($visible) {
                   5788:                 $output .= '<p class="LC_info">'.$visactions->{'vis'}.'</p>';
                   5789:            } else {
                   5790:                 $output .= '<p class="LC_warning">'.$visactions->{'miss'}.'</p>'
                   5791:                           .$visactions->{'yous'}.
                   5792:                            '<p>'.$visactions->{'gen'}.'<br />'.$visactions->{'coca'};
                   5793:                 if (ref($vismsgs) eq 'ARRAY') {
                   5794:                     $output .= '<br />'.$visactions->{'make'}.'<ul>';
                   5795:                     foreach my $item (@{$vismsgs}) {
                   5796:                         $output .= '<li>'.$visactions->{$item}.'</li>';
                   5797:                     }
                   5798:                     $output .= '</ul>';
1.256     raeburn  5799:                 }
1.400     raeburn  5800:                 $output .= '</p>';
1.256     raeburn  5801:             }
                   5802:         }
                   5803:     }
1.398     raeburn  5804:     my $actionhref = '/adm/createuser';
                   5805:     if ($context eq 'domain') {
                   5806:         $actionhref = '/adm/modifycourse';
                   5807:     }
1.400     raeburn  5808: 
                   5809:     my %noedit;
                   5810:     unless ($context eq 'domain') {
                   5811:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
                   5812:     }
1.398     raeburn  5813:     $output .= '<form name="'.$formname.'" method="post" action="'.$actionhref.'">'."\n".
1.256     raeburn  5814:                &Apache::lonhtmlcommon::start_pick_box();
1.237     raeburn  5815:     if (ref($row) eq 'ARRAY') {
                   5816:         foreach my $item (@{$row}) {
                   5817:             my $title = $item; 
                   5818:             if (ref($lt) eq 'HASH') {
                   5819:                 $title = $lt->{$item};
                   5820:             }
1.297     bisitz   5821:             $output .= &Apache::lonhtmlcommon::row_title($title);
1.237     raeburn  5822:             if ($item eq 'types') {
1.398     raeburn  5823:                 my $curr_types;
                   5824:                 if (ref($currsettings) eq 'HASH') {
                   5825:                     $curr_types = $currsettings->{'selfenroll_types'};
                   5826:                 }
1.400     raeburn  5827:                 if ($noedit{$item}) {
                   5828:                     if ($curr_types eq '*') {
                   5829:                         $output .= &mt('Any user in any domain');   
                   5830:                     } else {
                   5831:                         my @entries = split(/;/,$curr_types);
                   5832:                         if (@entries > 0) {
                   5833:                             $output .= '<ul>'; 
                   5834:                             foreach my $entry (@entries) {
                   5835:                                 my ($currdom,$typestr) = split(/:/,$entry);
                   5836:                                 next if ($typestr eq '');
                   5837:                                 my $domdesc = &Apache::lonnet::domain($currdom);
                   5838:                                 my @currinsttypes = split(',',$typestr);
                   5839:                                 my ($othertitle,$usertypes,$types) = 
                   5840:                                     &Apache::loncommon::sorted_inst_types($currdom);
                   5841:                                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   5842:                                     $usertypes->{'any'} = &mt('any user'); 
                   5843:                                     if (keys(%{$usertypes}) > 0) {
                   5844:                                         $usertypes->{'other'} = &mt('other users');
                   5845:                                     }
                   5846:                                     my @longinsttypes = map { $usertypes->{$_}; } @currinsttypes;
                   5847:                                     $output .= '<li>'.$domdesc.':'.join(', ',@longinsttypes).'</li>';
                   5848:                                  }
                   5849:                             }
                   5850:                             $output .= '</ul>';
                   5851:                         } else {
                   5852:                             $output .= &mt('None');
                   5853:                         }
                   5854:                     }
                   5855:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   5856:                     next;
                   5857:                 }
1.241     raeburn  5858:                 my $showdomdesc = 1;
                   5859:                 my $includeempty = 1;
                   5860:                 my $num = 0;
                   5861:                 $output .= &Apache::loncommon::start_data_table().
                   5862:                            &Apache::loncommon::start_data_table_row()
                   5863:                            .'<td colspan="2"><span class="LC_nobreak"><label>'
                   5864:                            .&mt('Any user in any domain:')
                   5865:                            .'&nbsp;<input type="radio" name="selfenroll_all" value="1" ';
                   5866:                 if ($curr_types eq '*') {
                   5867:                     $output .= ' checked="checked" '; 
                   5868:                 }
1.249     raeburn  5869:                 $output .= 'onchange="javascript:update_types('.
1.406.2.6  raeburn  5870:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('Yes').'</label>'.
1.249     raeburn  5871:                            '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
1.241     raeburn  5872:                 if ($curr_types ne '*') {
                   5873:                     $output .= ' checked="checked" ';
                   5874:                 }
1.249     raeburn  5875:                 $output .= ' onchange="javascript:update_types('.
1.406.2.6  raeburn  5876:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('No').'</label></td>'.
1.249     raeburn  5877:                            &Apache::loncommon::end_data_table_row().
                   5878:                            &Apache::loncommon::end_data_table().
                   5879:                            &mt('Or').'<br />'.
                   5880:                            &Apache::loncommon::start_data_table();
1.241     raeburn  5881:                 my %currdoms;
1.249     raeburn  5882:                 if ($curr_types eq '') {
1.241     raeburn  5883:                     $output .= &new_selfenroll_dom_row($cdom,'0');
                   5884:                 } elsif ($curr_types ne '*') {
                   5885:                     my @entries = split(/;/,$curr_types);
                   5886:                     if (@entries > 0) {
                   5887:                         foreach my $entry (@entries) {
                   5888:                             my ($currdom,$typestr) = split(/:/,$entry);
                   5889:                             $currdoms{$currdom} = 1;
                   5890:                             my $domdesc = &Apache::lonnet::domain($currdom);
1.249     raeburn  5891:                             my @currinsttypes = split(',',$typestr);
1.241     raeburn  5892:                             $output .= &Apache::loncommon::start_data_table_row()
                   5893:                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
                   5894:                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
                   5895:                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
                   5896:                                        .'" value="'.$currdom.'" /></span><br />'
                   5897:                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
1.406.2.6  raeburn  5898:                                        .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');"'.$disabled.' />'
1.241     raeburn  5899:                                        .&mt('Delete').'</label></span></td>';
1.249     raeburn  5900:                             $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
1.406.2.6  raeburn  5901:                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes,$readonly).'</td>'
1.241     raeburn  5902:                                        .&Apache::loncommon::end_data_table_row();
                   5903:                             $num ++;
                   5904:                         }
                   5905:                     }
                   5906:                 }
1.249     raeburn  5907:                 my $add_domtitle = &mt('Users in additional domain:');
1.241     raeburn  5908:                 if ($curr_types eq '*') { 
1.249     raeburn  5909:                     $add_domtitle = &mt('Users in specific domain:');
1.241     raeburn  5910:                 } elsif ($curr_types eq '') {
1.249     raeburn  5911:                     $add_domtitle = &mt('Users in other domain:');
1.241     raeburn  5912:                 }
                   5913:                 $output .= &Apache::loncommon::start_data_table_row()
                   5914:                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
                   5915:                            .&Apache::loncommon::select_dom_form('','selfenroll_newdom',
1.406.2.6  raeburn  5916:                                                                 $includeempty,$showdomdesc,'','','',$readonly)
1.241     raeburn  5917:                            .'<input type="hidden" name="selfenroll_types_total" value="'.$num.'" />'
                   5918:                            .'</td>'.&Apache::loncommon::end_data_table_row()
                   5919:                            .&Apache::loncommon::end_data_table();
1.237     raeburn  5920:             } elsif ($item eq 'registered') {
                   5921:                 my ($regon,$regoff);
1.398     raeburn  5922:                 my $registered;
                   5923:                 if (ref($currsettings) eq 'HASH') {
                   5924:                     $registered = $currsettings->{'selfenroll_registered'};
                   5925:                 }
1.400     raeburn  5926:                 if ($noedit{$item}) {
                   5927:                     if ($registered) {
                   5928:                         $output .= &mt('Must be registered in course');
                   5929:                     } else {
                   5930:                         $output .= &mt('No requirement');
                   5931:                     }
                   5932:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   5933:                     next;
                   5934:                 }
1.398     raeburn  5935:                 if ($registered) {
1.237     raeburn  5936:                     $regon = ' checked="checked" ';
1.406.2.6  raeburn  5937:                     $regoff = '';
1.237     raeburn  5938:                 } else {
1.406.2.6  raeburn  5939:                     $regon = '';
1.237     raeburn  5940:                     $regoff = ' checked="checked" ';
                   5941:                 }
                   5942:                 $output .= '<label>'.
1.406.2.6  raeburn  5943:                            '<input type="radio" name="selfenroll_registered" value="1"'.$regon.$disabled.' />'.
1.244     bisitz   5944:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
1.406.2.6  raeburn  5945:                            '<input type="radio" name="selfenroll_registered" value="0"'.$regoff.$disabled.' />'.
1.244     bisitz   5946:                            &mt('No').'</label>';
1.237     raeburn  5947:             } elsif ($item eq 'enroll_dates') {
1.398     raeburn  5948:                 my ($starttime,$endtime);
                   5949:                 if (ref($currsettings) eq 'HASH') {
                   5950:                     $starttime = $currsettings->{'selfenroll_start_date'};
                   5951:                     $endtime = $currsettings->{'selfenroll_end_date'};
                   5952:                     if ($starttime eq '') {
                   5953:                         $starttime = $currsettings->{'default_enrollment_start_date'};
                   5954:                     }
                   5955:                     if ($endtime eq '') {
                   5956:                         $endtime = $currsettings->{'default_enrollment_end_date'};
                   5957:                     }
1.237     raeburn  5958:                 }
1.400     raeburn  5959:                 if ($noedit{$item}) {
                   5960:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
                   5961:                                                           &Apache::lonlocal::locallocaltime($endtime));
                   5962:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   5963:                     next;
                   5964:                 }
1.237     raeburn  5965:                 my $startform =
                   5966:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
1.406.2.6  raeburn  5967:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  5968:                 my $endform =
                   5969:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
1.406.2.6  raeburn  5970:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  5971:                 $output .= &selfenroll_date_forms($startform,$endform);
                   5972:             } elsif ($item eq 'access_dates') {
1.398     raeburn  5973:                 my ($starttime,$endtime);
                   5974:                 if (ref($currsettings) eq 'HASH') {
                   5975:                     $starttime = $currsettings->{'selfenroll_start_access'};
                   5976:                     $endtime = $currsettings->{'selfenroll_end_access'};
                   5977:                     if ($starttime eq '') {
                   5978:                         $starttime = $currsettings->{'default_enrollment_start_date'};
                   5979:                     }
                   5980:                     if ($endtime eq '') {
                   5981:                         $endtime = $currsettings->{'default_enrollment_end_date'};
                   5982:                     }
1.237     raeburn  5983:                 }
1.400     raeburn  5984:                 if ($noedit{$item}) {
                   5985:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
                   5986:                                                           &Apache::lonlocal::locallocaltime($endtime));
                   5987:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   5988:                     next;
                   5989:                 }
1.237     raeburn  5990:                 my $startform =
                   5991:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
1.406.2.6  raeburn  5992:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  5993:                 my $endform =
                   5994:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
1.406.2.6  raeburn  5995:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  5996:                 $output .= &selfenroll_date_forms($startform,$endform);
                   5997:             } elsif ($item eq 'section') {
1.398     raeburn  5998:                 my $currsec;
                   5999:                 if (ref($currsettings) eq 'HASH') {
                   6000:                     $currsec = $currsettings->{'selfenroll_section'};
                   6001:                 }
1.237     raeburn  6002:                 my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   6003:                 my $newsecval;
                   6004:                 if ($currsec ne 'none' && $currsec ne '') {
                   6005:                     if (!defined($sections_count{$currsec})) {
                   6006:                         $newsecval = $currsec;
                   6007:                     }
                   6008:                 }
1.400     raeburn  6009:                 if ($noedit{$item}) {
                   6010:                     if ($currsec ne '') {
                   6011:                         $output .= $currsec;
                   6012:                     } else {
                   6013:                         $output .= &mt('No specific section');
                   6014:                     }
                   6015:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6016:                     next;
                   6017:                 }
1.237     raeburn  6018:                 my $sections_select = 
1.406.2.6  raeburn  6019:                     &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec,$disabled);
1.237     raeburn  6020:                 $output .= '<table class="LC_createuser">'."\n".
                   6021:                            '<tr class="LC_section_row">'."\n".
                   6022:                            '<td align="center">'.&mt('Existing sections')."\n".
                   6023:                            '<br />'.$sections_select.'</td><td align="center">'.
                   6024:                            &mt('New section').'<br />'."\n".
1.406.2.6  raeburn  6025:                            '<input type="text" name="newsec" size="15" value="'.$newsecval.'"'.$disabled.' />'."\n".
1.237     raeburn  6026:                            '<input type="hidden" name="sections" value="" />'."\n".
                   6027:                            '</td></tr></table>'."\n";
1.276     raeburn  6028:             } elsif ($item eq 'approval') {
1.398     raeburn  6029:                 my ($currnotified,$currapproval,%appchecked);
                   6030:                 my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
1.406.2.6  raeburn  6031:                 if (ref($currsettings) eq 'HASH') {
1.398     raeburn  6032:                     $currnotified = $currsettings->{'selfenroll_notifylist'};
                   6033:                     $currapproval = $currsettings->{'selfenroll_approval'};
                   6034:                 }
                   6035:                 if ($currapproval !~ /^[012]$/) {
                   6036:                     $currapproval = 0;
                   6037:                 }
1.400     raeburn  6038:                 if ($noedit{$item}) {
                   6039:                     $output .=  $selfdescs{'approval'}{$currapproval}.
                   6040:                                 '<br />'.&mt('(Set by Domain Coordinator)');
                   6041:                     next;
                   6042:                 }
1.398     raeburn  6043:                 $appchecked{$currapproval} = ' checked="checked"';
                   6044:                 for my $i (0..2) {
                   6045:                     $output .= '<label>'.
                   6046:                                '<input type="radio" name="selfenroll_approval" value="'.$i.'"'.
1.406.2.6  raeburn  6047:                                $appchecked{$i}.' onclick="toggleNotify();"'.$disabled.' />'.
                   6048:                                $selfdescs{'approval'}{$i}.'</label>'.('&nbsp;'x2);
1.276     raeburn  6049:                 }
                   6050:                 my %advhash = &Apache::lonnet::get_course_adv_roles($cid,1);
                   6051:                 my (@ccs,%notified);
1.322     raeburn  6052:                 my $ccrole = 'cc';
                   6053:                 if ($crstype eq 'Community') {
                   6054:                     $ccrole = 'co';
                   6055:                 }
                   6056:                 if ($advhash{$ccrole}) {
                   6057:                     @ccs = split(/,/,$advhash{$ccrole});
1.276     raeburn  6058:                 }
                   6059:                 if ($currnotified) {
                   6060:                     foreach my $current (split(/,/,$currnotified)) {
                   6061:                         $notified{$current} = 1;
                   6062:                         if (!grep(/^\Q$current\E$/,@ccs)) {
                   6063:                             push(@ccs,$current);
                   6064:                         }
                   6065:                     }
                   6066:                 }
                   6067:                 if (@ccs) {
1.398     raeburn  6068:                     my $style;
                   6069:                     unless ($currapproval) {
                   6070:                         $style = ' style="display: none;"'; 
                   6071:                     }
                   6072:                     $output .= '<br /><div id="notified"'.$style.'>'.
                   6073:                                &mt('Personnel to be notified when an enrollment request needs approval, or has been approved:').'&nbsp;'.
                   6074:                                &Apache::loncommon::start_data_table().
1.276     raeburn  6075:                                &Apache::loncommon::start_data_table_row();
                   6076:                     my $count = 0;
                   6077:                     my $numcols = 4;
                   6078:                     foreach my $cc (sort(@ccs)) {
                   6079:                         my $notifyon;
                   6080:                         my ($ccuname,$ccudom) = split(/:/,$cc);
                   6081:                         if ($notified{$cc}) {
                   6082:                             $notifyon = ' checked="checked" ';
                   6083:                         }
                   6084:                         if ($count && !$count%$numcols) {
                   6085:                             $output .= &Apache::loncommon::end_data_table_row().
                   6086:                                        &Apache::loncommon::start_data_table_row()
                   6087:                         }
                   6088:                         $output .= '<td><span class="LC_nobreak"><label>'.
1.406.2.6  raeburn  6089:                                    '<input type="checkbox" name="selfenroll_notify"'.$notifyon.' value="'.$cc.'"'.$disabled.' />'.
1.276     raeburn  6090:                                    &Apache::loncommon::plainname($ccuname,$ccudom).
                   6091:                                    '</label></span></td>';
1.343     raeburn  6092:                         $count ++;
1.276     raeburn  6093:                     }
                   6094:                     my $rem = $count%$numcols;
                   6095:                     if ($rem) {
                   6096:                         my $emptycols = $numcols - $rem;
                   6097:                         for (my $i=0; $i<$emptycols; $i++) { 
                   6098:                             $output .= '<td>&nbsp;</td>';
                   6099:                         }
                   6100:                     }
                   6101:                     $output .= &Apache::loncommon::end_data_table_row().
1.398     raeburn  6102:                                &Apache::loncommon::end_data_table().
                   6103:                                '</div>';
1.276     raeburn  6104:                 }
                   6105:             } elsif ($item eq 'limit') {
1.398     raeburn  6106:                 my ($crslimit,$selflimit,$nolimit,$currlim,$currcap);
                   6107:                 if (ref($currsettings) eq 'HASH') {
                   6108:                     $currlim = $currsettings->{'selfenroll_limit'};
                   6109:                     $currcap = $currsettings->{'selfenroll_cap'};
                   6110:                 }
1.400     raeburn  6111:                 if ($noedit{$item}) {
                   6112:                     if (($currlim eq 'allstudents') || ($currlim eq 'selfenrolled')) {
                   6113:                         if ($currlim eq 'allstudents') {
                   6114:                             $output .= &mt('Limit by total students');
                   6115:                         } elsif ($currlim eq 'selfenrolled') {
                   6116:                             $output .= &mt('Limit by total self-enrolled students');
                   6117:                         }
                   6118:                         $output .= ' '.&mt('Maximum: [_1]',$currcap).
                   6119:                                    '<br />'.&mt('(Set by Domain Coordinator)');
                   6120:                     } else {
                   6121:                         $output .= &mt('No limit').'<br />'.&mt('(Set by Domain Coordinator)');
                   6122:                     }
                   6123:                     next;
                   6124:                 }
1.276     raeburn  6125:                 if ($currlim eq 'allstudents') {
                   6126:                     $crslimit = ' checked="checked" ';
                   6127:                     $selflimit = ' ';
                   6128:                     $nolimit = ' ';
                   6129:                 } elsif ($currlim eq 'selfenrolled') {
                   6130:                     $crslimit = ' ';
                   6131:                     $selflimit = ' checked="checked" ';
                   6132:                     $nolimit = ' '; 
                   6133:                 } else {
                   6134:                     $crslimit = ' ';
                   6135:                     $selflimit = ' ';
1.398     raeburn  6136:                     $nolimit = ' checked="checked" ';
1.276     raeburn  6137:                 }
                   6138:                 $output .= '<table><tr><td><label>'.
1.406.2.6  raeburn  6139:                            '<input type="radio" name="selfenroll_limit" value="none"'.$nolimit.$disabled.'/>'.
1.276     raeburn  6140:                            &mt('No limit').'</label></td><td><label>'.
1.406.2.6  raeburn  6141:                            '<input type="radio" name="selfenroll_limit" value="allstudents"'.$crslimit.$disabled.'/>'.
1.276     raeburn  6142:                            &mt('Limit by total students').'</label></td><td><label>'.
1.406.2.6  raeburn  6143:                            '<input type="radio" name="selfenroll_limit" value="selfenrolled"'.$selflimit.$disabled.'/>'.
1.276     raeburn  6144:                            &mt('Limit by total self-enrolled students').
                   6145:                            '</td></tr><tr>'.
                   6146:                            '<td>&nbsp;</td><td colspan="2"><span class="LC_nobreak">'.
                   6147:                            ('&nbsp;'x3).&mt('Maximum number allowed: ').
1.406.2.6  raeburn  6148:                            '<input type="text" name="selfenroll_cap" size = "5" value="'.$currcap.'"'.$disabled.' /></td></tr></table>';
1.237     raeburn  6149:             }
                   6150:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   6151:         }
                   6152:     }
1.406.2.6  raeburn  6153:     $output .= &Apache::lonhtmlcommon::end_pick_box().'<br />';
                   6154:     unless ($readonly) {
                   6155:         $output .= '<input type="button" name="selfenrollconf" value="'
                   6156:                    .&mt('Save').'" onclick="validate_types(this.form);" />';
                   6157:     }
                   6158:     $output .= '<input type="hidden" name="action" value="selfenroll" />'
                   6159:                .'<input type="hidden" name="state" value="done" />'."\n"
                   6160:                .$additional.'</form>';
1.237     raeburn  6161:     $r->print($output);
                   6162:     return;
                   6163: }
                   6164: 
1.400     raeburn  6165: sub get_noedit_fields {
                   6166:     my ($cdom,$cnum,$crstype,$row) = @_;
                   6167:     my %noedit;
                   6168:     if (ref($row) eq 'ARRAY') {
                   6169:         my %settings = &Apache::lonnet::get('environment',['internal.coursecode','internal.textbook',
                   6170:                                                            'internal.selfenrollmgrdc',
                   6171:                                                            'internal.selfenrollmgrcc'],$cdom,$cnum);
                   6172:         my $type = &Apache::lonuserutils::get_extended_type($cdom,$cnum,$crstype,\%settings);
                   6173:         my (%specific_managebydc,%specific_managebycc,%default_managebydc);
                   6174:         map { $specific_managebydc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrdc'}));
                   6175:         map { $specific_managebycc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrcc'}));
                   6176:         my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
                   6177:         map { $default_managebydc{$_} = 1; } (split(/,/,$domdefaults{$type.'selfenrolladmdc'}));
                   6178: 
                   6179:         foreach my $item (@{$row}) {
                   6180:             next if ($specific_managebycc{$item});
                   6181:             if (($specific_managebydc{$item}) || ($default_managebydc{$item})) {
                   6182:                 $noedit{$item} = 1;
                   6183:             }
                   6184:         }
                   6185:     }
                   6186:     return %noedit;
                   6187: } 
                   6188: 
                   6189: sub visible_in_stdcat {
                   6190:     my ($cdom,$cnum,$domconf) = @_;
                   6191:     my ($cathash,%settable,@vismsgs,$cansetvis,$visible);
                   6192:     unless (ref($domconf) eq 'HASH') {
                   6193:         return ($visible,$cansetvis,\@vismsgs);
                   6194:     }
                   6195:     if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6196:         if ($domconf->{'coursecategories'}{'togglecats'} eq 'crs') {
1.256     raeburn  6197:             $settable{'togglecats'} = 1;
                   6198:         }
1.400     raeburn  6199:         if ($domconf->{'coursecategories'}{'categorize'} eq 'crs') {
1.256     raeburn  6200:             $settable{'categorize'} = 1;
                   6201:         }
1.400     raeburn  6202:         $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6203:     }
1.260     raeburn  6204:     if ($settable{'togglecats'} && $settable{'categorize'}) {
1.256     raeburn  6205:         $cansetvis = &mt('You are able to both assign a course category and choose to exclude this course from the catalog.');   
                   6206:     } elsif ($settable{'togglecats'}) {
                   6207:         $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  6208:     } elsif ($settable{'categorize'}) {
1.256     raeburn  6209:         $cansetvis = &mt('You may assign a course category, but only a Domain Coordinator may choose to exclude this course from the catalog.');  
                   6210:     } else {
                   6211:         $cansetvis = &mt('Only a Domain Coordinator may assign a course category or choose to exclude this course from the catalog.'); 
                   6212:     }
                   6213:      
                   6214:     my %currsettings =
                   6215:         &Apache::lonnet::get('environment',['hidefromcat','categories','internal.coursecode'],
                   6216:                              $cdom,$cnum);
1.400     raeburn  6217:     $visible = 0;
1.256     raeburn  6218:     if ($currsettings{'internal.coursecode'} ne '') {
1.400     raeburn  6219:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6220:             $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6221:             if (ref($cathash) eq 'HASH') {
                   6222:                 if ($cathash->{'instcode::0'} eq '') {
                   6223:                     push(@vismsgs,'dc_addinst'); 
                   6224:                 } else {
                   6225:                     $visible = 1;
                   6226:                 }
                   6227:             } else {
                   6228:                 $visible = 1;
                   6229:             }
                   6230:         } else {
                   6231:             $visible = 1;
                   6232:         }
                   6233:     } else {
                   6234:         if (ref($cathash) eq 'HASH') {
                   6235:             if ($cathash->{'instcode::0'} ne '') {
                   6236:                 push(@vismsgs,'dc_instcode');
                   6237:             }
                   6238:         } else {
                   6239:             push(@vismsgs,'dc_instcode');
                   6240:         }
                   6241:     }
                   6242:     if ($currsettings{'categories'} ne '') {
                   6243:         my $cathash;
1.400     raeburn  6244:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6245:             $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6246:             if (ref($cathash) eq 'HASH') {
                   6247:                 if (keys(%{$cathash}) == 0) {
                   6248:                     push(@vismsgs,'dc_catalog');
                   6249:                 } elsif ((keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} ne '')) {
                   6250:                     push(@vismsgs,'dc_categories');
                   6251:                 } else {
                   6252:                     my @currcategories = split('&',$currsettings{'categories'});
                   6253:                     my $matched = 0;
                   6254:                     foreach my $cat (@currcategories) {
                   6255:                         if ($cathash->{$cat} ne '') {
                   6256:                             $visible = 1;
                   6257:                             $matched = 1;
                   6258:                             last;
                   6259:                         }
                   6260:                     }
                   6261:                     if (!$matched) {
1.260     raeburn  6262:                         if ($settable{'categorize'}) { 
1.256     raeburn  6263:                             push(@vismsgs,'chgcat');
                   6264:                         } else {
                   6265:                             push(@vismsgs,'dc_chgcat');
                   6266:                         }
                   6267:                     }
                   6268:                 }
                   6269:             }
                   6270:         }
                   6271:     } else {
                   6272:         if (ref($cathash) eq 'HASH') {
                   6273:             if ((keys(%{$cathash}) > 1) || 
                   6274:                 (keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} eq '')) {
1.260     raeburn  6275:                 if ($settable{'categorize'}) {
1.256     raeburn  6276:                     push(@vismsgs,'addcat');
                   6277:                 } else {
                   6278:                     push(@vismsgs,'dc_addcat');
                   6279:                 }
                   6280:             }
                   6281:         }
                   6282:     }
                   6283:     if ($currsettings{'hidefromcat'} eq 'yes') {
                   6284:         $visible = 0;
                   6285:         if ($settable{'togglecats'}) {
                   6286:             unshift(@vismsgs,'unhide');
                   6287:         } else {
                   6288:             unshift(@vismsgs,'dc_unhide')
                   6289:         }
                   6290:     }
1.400     raeburn  6291:     return ($visible,$cansetvis,\@vismsgs);
                   6292: }
                   6293: 
                   6294: sub cat_visibility {
                   6295:     my %visactions = &Apache::lonlocal::texthash(
                   6296:                    vis => 'This course/community currently appears in the Course/Community Catalog for this domain.',
                   6297:                    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.',
                   6298:                    miss => 'This course/community does not currently appear in the Course/Community Catalog for this domain.',
                   6299:                    none => 'Display of a course catalog is disabled for this domain.',
                   6300:                    yous => 'You should remedy this if you plan to allow self-enrollment, otherwise students will have difficulty finding this course.',
                   6301:                    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.',
                   6302:                    make => 'Make any changes to self-enrollment settings below, click "Save", then take action to include the course in the Catalog:',
                   6303:                    take => 'Take the following action to ensure the course appears in the Catalog:',
                   6304:                    dc_chgconf => 'Ask a domain coordinator to change the Catalog type for this domain.',
                   6305:                    dc_setcode => 'Ask a domain coordinator to assign a six character code to the course',
                   6306:                    dc_unhide  => 'Ask a domain coordinator to change the "Exclude from course catalog" setting.',
                   6307:                    dc_addinst => 'Ask a domain coordinator to enable display the catalog of "Official courses (with institutional codes)".',
                   6308:                    dc_instcode => 'Ask a domain coordinator to assign an institutional code (if this is an official course).',
                   6309:                    dc_catalog  => 'Ask a domain coordinator to enable or create at least one course category in the domain.',
                   6310:                    dc_categories => 'Ask a domain coordinator to create a hierarchy of categories and sub categories for courses in the domain.',
                   6311:                    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',
                   6312:                    dc_addcat => 'Ask a domain coordinator to assign a category to the course.',
                   6313:     );
                   6314:     $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>"');
                   6315:     $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>"');
                   6316:     $visactions{'addcat'} = &mt('Use [_1]Categorize course[_2] to assign a category to the course.','"<a href="/adm/courseprefs?phase=display&actions=courseinfo">','</a>"');
                   6317:     return \%visactions;
1.256     raeburn  6318: }
                   6319: 
1.241     raeburn  6320: sub new_selfenroll_dom_row {
                   6321:     my ($newdom,$num) = @_;
                   6322:     my $domdesc = &Apache::lonnet::domain($newdom);
                   6323:     my $output;
                   6324:     if ($domdesc ne '') {
                   6325:         $output .= &Apache::loncommon::start_data_table_row()
                   6326:                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
                   6327:                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
1.249     raeburn  6328:                    .'" value="'.$newdom.'" /></span><br />'
                   6329:                    .'<span class="LC_nobreak"><label><input type="checkbox" '
                   6330:                    .'name="selfenroll_activate" value="'.$num.'" '
                   6331:                    .'onchange="javascript:update_types('
                   6332:                    ."'selfenroll_activate','$num'".');" />'
                   6333:                    .&mt('Activate').'</label></span></td>';
1.241     raeburn  6334:         my @currinsttypes;
                   6335:         $output .= '<td>'.&mt('User types:').'<br />'
                   6336:                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
                   6337:                    .&Apache::loncommon::end_data_table_row();
                   6338:     }
                   6339:     return $output;
                   6340: }
                   6341: 
                   6342: sub selfenroll_inst_types {
1.406.2.6  raeburn  6343:     my ($num,$currdom,$currinsttypes,$readonly) = @_;
1.241     raeburn  6344:     my $output;
                   6345:     my $numinrow = 4;
                   6346:     my $count = 0;
                   6347:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($currdom);
1.247     raeburn  6348:     my $othervalue = 'any';
1.406.2.6  raeburn  6349:     my $disabled;
                   6350:     if ($readonly) {
                   6351:         $disabled = ' disabled="disabled"';
                   6352:     }
1.241     raeburn  6353:     if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
1.251     raeburn  6354:         if (keys(%{$usertypes}) > 0) {
1.247     raeburn  6355:             $othervalue = 'other';
                   6356:         }
1.241     raeburn  6357:         $output .= '<table><tr>';
                   6358:         foreach my $type (@{$types}) {
                   6359:             if (($count > 0) && ($count%$numinrow == 0)) {
                   6360:                 $output .= '</tr><tr>';
                   6361:             }
                   6362:             if (defined($usertypes->{$type})) {
1.257     raeburn  6363:                 my $esc_type = &escape($type);
1.241     raeburn  6364:                 $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.
1.257     raeburn  6365:                            $esc_type.'" ';
1.241     raeburn  6366:                 if (ref($currinsttypes) eq 'ARRAY') {
                   6367:                     if (@{$currinsttypes} > 0) {
1.249     raeburn  6368:                         if (grep(/^any$/,@{$currinsttypes})) {
                   6369:                             $output .= 'checked="checked"';
1.257     raeburn  6370:                         } elsif (grep(/^\Q$esc_type\E$/,@{$currinsttypes})) {
1.241     raeburn  6371:                             $output .= 'checked="checked"';
                   6372:                         }
1.249     raeburn  6373:                     } else {
                   6374:                         $output .= 'checked="checked"';
1.241     raeburn  6375:                     }
                   6376:                 }
1.406.2.6  raeburn  6377:                 $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$usertypes->{$type}.'</label></span></td>';
1.241     raeburn  6378:             }
                   6379:             $count ++;
                   6380:         }
                   6381:         if (($count > 0) && ($count%$numinrow == 0)) {
                   6382:             $output .= '</tr><tr>';
                   6383:         }
1.249     raeburn  6384:         $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
1.241     raeburn  6385:         if (ref($currinsttypes) eq 'ARRAY') {
                   6386:             if (@{$currinsttypes} > 0) {
1.249     raeburn  6387:                 if (grep(/^any$/,@{$currinsttypes})) { 
                   6388:                     $output .= ' checked="checked"';
                   6389:                 } elsif ($othervalue eq 'other') {
                   6390:                     if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
                   6391:                         $output .= ' checked="checked"';
                   6392:                     }
1.241     raeburn  6393:                 }
1.249     raeburn  6394:             } else {
                   6395:                 $output .= ' checked="checked"';
1.241     raeburn  6396:             }
1.249     raeburn  6397:         } else {
                   6398:             $output .= ' checked="checked"';
1.241     raeburn  6399:         }
1.406.2.6  raeburn  6400:         $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$othertitle.'</label></span></td></tr></table>';
1.241     raeburn  6401:     }
                   6402:     return $output;
                   6403: }
                   6404: 
1.237     raeburn  6405: sub selfenroll_date_forms {
                   6406:     my ($startform,$endform) = @_;
                   6407:     my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
1.244     bisitz   6408:                   &Apache::lonhtmlcommon::row_title(&mt('Start date'),
1.237     raeburn  6409:                                                     'LC_oddrow_value')."\n".
                   6410:                   $startform."\n".
                   6411:                   &Apache::lonhtmlcommon::row_closure(1).
1.244     bisitz   6412:                   &Apache::lonhtmlcommon::row_title(&mt('End date'),
1.237     raeburn  6413:                                                    'LC_oddrow_value')."\n".
                   6414:                   $endform."\n".
                   6415:                   &Apache::lonhtmlcommon::row_closure(1).
                   6416:                   &Apache::lonhtmlcommon::end_pick_box();
                   6417:     return $output;
                   6418: }
                   6419: 
1.239     raeburn  6420: sub print_userchangelogs_display {
1.406.2.5  raeburn  6421:     my ($r,$context,$permission,$brcrum) = @_;
1.363     raeburn  6422:     my $formname = 'rolelog';
1.406.2.6  raeburn  6423:     my ($username,$domain,$crstype,$viewablesec,%roleslog);
1.363     raeburn  6424:     if ($context eq 'domain') {
                   6425:         $domain = $env{'request.role.domain'};
                   6426:         %roleslog=&Apache::lonnet::dump_dom('nohist_rolelog',$domain);
                   6427:     } else {
                   6428:         if ($context eq 'course') { 
                   6429:             $domain = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   6430:             $username = $env{'course.'.$env{'request.course.id'}.'.num'};
                   6431:             $crstype = &Apache::loncommon::course_type();
1.406.2.6  raeburn  6432:             $viewablesec = &Apache::lonuserutils::viewable_section($permission);
1.363     raeburn  6433:             my %saveable_parameters = ('show' => 'scalar',);
                   6434:             &Apache::loncommon::store_course_settings('roles_log',
                   6435:                                                       \%saveable_parameters);
                   6436:             &Apache::loncommon::restore_course_settings('roles_log',
                   6437:                                                         \%saveable_parameters);
                   6438:         } elsif ($context eq 'author') {
                   6439:             $domain = $env{'user.domain'}; 
                   6440:             if ($env{'request.role'} =~ m{^au\./\Q$domain\E/$}) {
                   6441:                 $username = $env{'user.name'};
                   6442:             } else {
                   6443:                 undef($domain);
                   6444:             }
                   6445:         }
                   6446:         if ($domain ne '' && $username ne '') { 
                   6447:             %roleslog=&Apache::lonnet::dump('nohist_rolelog',$domain,$username);
                   6448:         }
                   6449:     }
1.239     raeburn  6450:     if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
                   6451: 
1.406.2.5  raeburn  6452:     my $helpitem;
                   6453:     if ($context eq 'course') {
                   6454:         $helpitem = 'Course_User_Logs';
                   6455:     }
                   6456:     push (@{$brcrum},
                   6457:              {href => '/adm/createuser?action=changelogs',
                   6458:               text => 'User Management Logs',
                   6459:               help => $helpitem});
                   6460:     my $bread_crumbs_component = 'User Changes';
                   6461:     my $args = { bread_crumbs           => $brcrum,
                   6462:                  bread_crumbs_component => $bread_crumbs_component};
                   6463: 
                   6464:     # Create navigation javascript
                   6465:     my $jsnav = &userlogdisplay_js($formname);
                   6466: 
                   6467:     my $jscript = (<<ENDSCRIPT);
                   6468: <script type="text/javascript">
                   6469: // <![CDATA[
                   6470: $jsnav
                   6471: // ]]>
                   6472: </script>
                   6473: ENDSCRIPT
                   6474: 
                   6475:     # print page header
                   6476:     $r->print(&header($jscript,$args));
                   6477: 
1.239     raeburn  6478:     # set defaults
                   6479:     my $now = time();
                   6480:     my $defstart = $now - (7*24*3600); #7 days ago 
                   6481:     my %defaults = (
                   6482:                      page               => '1',
                   6483:                      show               => '10',
                   6484:                      role               => 'any',
                   6485:                      chgcontext         => 'any',
                   6486:                      rolelog_start_date => $defstart,
                   6487:                      rolelog_end_date   => $now,
                   6488:                    );
                   6489:     my $more_records = 0;
                   6490: 
                   6491:     # set current
                   6492:     my %curr;
                   6493:     foreach my $item ('show','page','role','chgcontext') {
                   6494:         $curr{$item} = $env{'form.'.$item};
                   6495:     }
                   6496:     my ($startdate,$enddate) = 
                   6497:         &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
                   6498:     $curr{'rolelog_start_date'} = $startdate;
                   6499:     $curr{'rolelog_end_date'} = $enddate;
                   6500:     foreach my $key (keys(%defaults)) {
                   6501:         if ($curr{$key} eq '') {
                   6502:             $curr{$key} = $defaults{$key};
                   6503:         }
                   6504:     }
1.248     raeburn  6505:     my (%whodunit,%changed,$version);
                   6506:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
1.239     raeburn  6507:     my ($minshown,$maxshown);
1.255     raeburn  6508:     $minshown = 1;
1.239     raeburn  6509:     my $count = 0;
1.406.2.5  raeburn  6510:     if ($curr{'show'} =~ /\D/) {
                   6511:         $curr{'page'} = 1;
                   6512:     } else {
1.239     raeburn  6513:         $maxshown = $curr{'page'} * $curr{'show'};
                   6514:         if ($curr{'page'} > 1) {
                   6515:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   6516:         }
                   6517:     }
1.301     bisitz   6518: 
1.327     raeburn  6519:     # Form Header
                   6520:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
1.363     raeburn  6521:               &role_display_filter($context,$formname,$domain,$username,\%curr,
                   6522:                                    $version,$crstype));
1.327     raeburn  6523: 
                   6524:     my $showntableheader = 0;
                   6525: 
                   6526:     # Table Header
                   6527:     my $tableheader = 
                   6528:         &Apache::loncommon::start_data_table_header_row()
                   6529:        .'<th>&nbsp;</th>'
                   6530:        .'<th>'.&mt('When').'</th>'
                   6531:        .'<th>'.&mt('Who made the change').'</th>'
                   6532:        .'<th>'.&mt('Changed User').'</th>'
1.363     raeburn  6533:        .'<th>'.&mt('Role').'</th>';
                   6534: 
                   6535:     if ($context eq 'course') {
                   6536:         $tableheader .= '<th>'.&mt('Section').'</th>';
                   6537:     }
                   6538:     $tableheader .=
                   6539:         '<th>'.&mt('Context').'</th>'
1.327     raeburn  6540:        .'<th>'.&mt('Start').'</th>'
                   6541:        .'<th>'.&mt('End').'</th>'
                   6542:        .&Apache::loncommon::end_data_table_header_row();
                   6543: 
                   6544:     # Display user change log data
1.239     raeburn  6545:     foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
                   6546:         next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
                   6547:                  ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
1.406.2.5  raeburn  6548:         if ($curr{'show'} !~ /\D/) {
1.239     raeburn  6549:             if ($count >= $curr{'page'} * $curr{'show'}) {
                   6550:                 $more_records = 1;
                   6551:                 last;
                   6552:             }
                   6553:         }
                   6554:         if ($curr{'role'} ne 'any') {
                   6555:             next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
                   6556:         }
                   6557:         if ($curr{'chgcontext'} ne 'any') {
                   6558:             if ($curr{'chgcontext'} eq 'selfenroll') {
                   6559:                 next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
                   6560:             } else {
                   6561:                 next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
                   6562:             }
                   6563:         }
1.406.2.6  raeburn  6564:         if (($context eq 'course') && ($viewablesec ne '')) {
                   6565:             next if ($roleslog{$id}{'logentry'}{'section'} ne $viewablesec);
                   6566:         }
1.239     raeburn  6567:         $count ++;
                   6568:         next if ($count < $minshown);
1.327     raeburn  6569:         unless ($showntableheader) {
1.406.2.5  raeburn  6570:             $r->print(&Apache::loncommon::start_data_table()
1.327     raeburn  6571:                      .$tableheader);
                   6572:             $r->rflush();
                   6573:             $showntableheader = 1;
                   6574:         }
1.239     raeburn  6575:         if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
                   6576:             $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
                   6577:                 &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
                   6578:         }
                   6579:         if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
                   6580:             $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
                   6581:                 &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
                   6582:         }
                   6583:         my $sec = $roleslog{$id}{'logentry'}{'section'};
                   6584:         if ($sec eq '') {
                   6585:             $sec = &mt('None');
                   6586:         }
                   6587:         my ($rolestart,$roleend);
                   6588:         if ($roleslog{$id}{'delflag'}) {
                   6589:             $rolestart = &mt('deleted');
                   6590:             $roleend = &mt('deleted');
                   6591:         } else {
                   6592:             $rolestart = $roleslog{$id}{'logentry'}{'start'};
                   6593:             $roleend = $roleslog{$id}{'logentry'}{'end'};
                   6594:             if ($rolestart eq '' || $rolestart == 0) {
                   6595:                 $rolestart = &mt('No start date'); 
                   6596:             } else {
                   6597:                 $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
                   6598:             }
                   6599:             if ($roleend eq '' || $roleend == 0) { 
                   6600:                 $roleend = &mt('No end date');
                   6601:             } else {
                   6602:                 $roleend = &Apache::lonlocal::locallocaltime($roleend);
                   6603:             }
                   6604:         }
                   6605:         my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
                   6606:         if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
                   6607:             $chgcontext = 'selfenroll';
                   6608:         }
1.363     raeburn  6609:         my %lt = &rolechg_contexts($context,$crstype);
1.239     raeburn  6610:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
                   6611:             $chgcontext = $lt{$chgcontext};
                   6612:         }
1.327     raeburn  6613:         $r->print(
1.301     bisitz   6614:             &Apache::loncommon::start_data_table_row()
                   6615:            .'<td>'.$count.'</td>'
                   6616:            .'<td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td>'
                   6617:            .'<td>'.$whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.'</td>'
                   6618:            .'<td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td>'
1.363     raeburn  6619:            .'<td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'},$crstype).'</td>');
                   6620:         if ($context eq 'course') { 
                   6621:             $r->print('<td>'.$sec.'</td>');
                   6622:         }
                   6623:         $r->print(
                   6624:             '<td>'.$chgcontext.'</td>'
1.301     bisitz   6625:            .'<td>'.$rolestart.'</td>'
                   6626:            .'<td>'.$roleend.'</td>'
1.327     raeburn  6627:            .&Apache::loncommon::end_data_table_row()."\n");
1.301     bisitz   6628:     }
                   6629: 
1.327     raeburn  6630:     if ($showntableheader) { # Table footer, if content displayed above
1.406.2.5  raeburn  6631:         $r->print(&Apache::loncommon::end_data_table().
                   6632:                   &userlogdisplay_navlinks(\%curr,$more_records));
1.327     raeburn  6633:     } else { # No content displayed above
1.301     bisitz   6634:         $r->print('<p class="LC_info">'
                   6635:                  .&mt('There are no records to display.')
                   6636:                  .'</p>'
                   6637:         );
1.239     raeburn  6638:     }
1.301     bisitz   6639: 
1.327     raeburn  6640:     # Form Footer
                   6641:     $r->print( 
                   6642:         '<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   6643:        .'<input type="hidden" name="action" value="changelogs" />'
                   6644:        .'</form>');
                   6645:     return;
                   6646: }
1.301     bisitz   6647: 
1.406.2.5  raeburn  6648: sub print_useraccesslogs_display {
                   6649:     my ($r,$uname,$udom,$permission,$brcrum) = @_;
                   6650:     my $formname = 'accesslog';
                   6651:     my $form = 'document.accesslog';
                   6652: 
                   6653: # set breadcrumbs
1.406.2.7  raeburn  6654:     my %breadcrumb_text = &singleuser_breadcrumb('','domain',$udom);
1.406.2.5  raeburn  6655:     push (@{$brcrum},
                   6656:         {href => "javascript:backPage($form)",
                   6657:          text => $breadcrumb_text{'search'}});
                   6658:     my (@prevphases,$prevphasestr);
                   6659:     if ($env{'form.prevphases'}) {
                   6660:         @prevphases = split(/,/,$env{'form.prevphases'});
                   6661:         $prevphasestr = $env{'form.prevphases'};
                   6662:     }
                   6663:     if (($env{'form.phase'} eq 'userpicked') || (grep(/^userpicked$/,@prevphases))) {
                   6664:         push(@{$brcrum},
                   6665:               {href => "javascript:backPage($form,'get_user_info','select')",
                   6666:                text => $breadcrumb_text{'userpicked'}});
                   6667:         if ($env{'form.phase'} eq 'userpicked') {
                   6668:             $prevphasestr = 'userpicked';
                   6669:         }
                   6670:     }
                   6671:     push(@{$brcrum},
                   6672:              {href => '/adm/createuser?action=accesslogs',
                   6673:               text => 'User access logs',
1.406.2.8  raeburn  6674:               help => 'Domain_User_Access_Logs'});
1.406.2.5  raeburn  6675:     my $bread_crumbs_component = 'User Access Logs';
                   6676:     my $args = { bread_crumbs           => $brcrum,
                   6677:                  bread_crumbs_component => 'User Management'};
1.406.2.8  raeburn  6678:     if ($env{'form.popup'}) {
                   6679:         $args->{'no_nav_bar'} = 1;
                   6680:     }
1.406.2.5  raeburn  6681: 
                   6682: # set javascript
                   6683:     my ($jsback,$elements) = &crumb_utilities();
                   6684:     my $jsnav = &userlogdisplay_js($formname);
                   6685: 
                   6686:     my $jscript = (<<ENDSCRIPT);
1.239     raeburn  6687: <script type="text/javascript">
1.301     bisitz   6688: // <![CDATA[
1.406.2.5  raeburn  6689: 
                   6690: $jsback
                   6691: $jsnav
                   6692: 
                   6693: // ]]>
                   6694: </script>
                   6695: 
                   6696: ENDSCRIPT
                   6697: 
                   6698: # print page header
                   6699:     $r->print(&header($jscript,$args));
                   6700: 
                   6701: # early out unless log data can be displayed.
                   6702:     unless ($permission->{'activity'}) {
                   6703:         $r->print('<p class="LC_warning">'
                   6704:                  .&mt('You do not have rights to display user access logs.')
                   6705:                  .'</p>'
                   6706:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   6707:         return;
                   6708:     }
                   6709: 
                   6710:     unless ($udom eq $env{'request.role.domain'}) {
                   6711:         $r->print('<p class="LC_warning">'
                   6712:                  .&mt("User's domain must match role's domain")
                   6713:                  .'</p>'
                   6714:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   6715:         return;
                   6716:     }
                   6717: 
                   6718:     if (($uname eq '') || ($udom eq '')) {
                   6719:         $r->print('<p class="LC_warning">'
                   6720:                  .&mt('Invalid username or domain')
                   6721:                  .'</p>'
                   6722:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   6723:         return;
                   6724:     }
                   6725: 
                   6726: # set defaults
                   6727:     my $now = time();
                   6728:     my $defstart = $now - (7*24*3600);
                   6729:     my %defaults = (
                   6730:                      page                 => '1',
                   6731:                      show                 => '10',
                   6732:                      activity             => 'any',
                   6733:                      accesslog_start_date => $defstart,
                   6734:                      accesslog_end_date   => $now,
                   6735:                    );
                   6736:     my $more_records = 0;
                   6737: 
                   6738: # set current
                   6739:     my %curr;
                   6740:     foreach my $item ('show','page','activity') {
                   6741:         $curr{$item} = $env{'form.'.$item};
                   6742:     }
                   6743:     my ($startdate,$enddate) =
                   6744:         &Apache::lonuserutils::get_dates_from_form('accesslog_start_date','accesslog_end_date');
                   6745:     $curr{'accesslog_start_date'} = $startdate;
                   6746:     $curr{'accesslog_end_date'} = $enddate;
                   6747:     foreach my $key (keys(%defaults)) {
                   6748:         if ($curr{$key} eq '') {
                   6749:             $curr{$key} = $defaults{$key};
                   6750:         }
                   6751:     }
                   6752:     my ($minshown,$maxshown);
                   6753:     $minshown = 1;
                   6754:     my $count = 0;
                   6755:     if ($curr{'show'} =~ /\D/) {
                   6756:         $curr{'page'} = 1;
                   6757:     } else {
                   6758:         $maxshown = $curr{'page'} * $curr{'show'};
                   6759:         if ($curr{'page'} > 1) {
                   6760:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   6761:         }
                   6762:     }
                   6763: 
                   6764: # form header
                   6765:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
                   6766:               &activity_display_filter($formname,\%curr));
                   6767: 
                   6768:     my $showntableheader = 0;
                   6769:     my ($nav_script,$nav_links);
                   6770: 
                   6771: # table header
                   6772:     my $tableheader =
                   6773:         &Apache::loncommon::start_data_table_header_row()
                   6774:        .'<th>&nbsp;</th>'
                   6775:        .'<th>'.&mt('When').'</th>'
                   6776:        .'<th>'.&mt('HostID').'</th>'
                   6777:        .'<th>'.&mt('Event').'</th>'
                   6778:        .'<th>'.&mt('Other data').'</th>'
                   6779:        .&Apache::loncommon::end_data_table_header_row();
                   6780: 
                   6781:     my %filters=(
                   6782:         start  => $curr{'accesslog_start_date'},
                   6783:         end    => $curr{'accesslog_end_date'},
                   6784:         action => $curr{'activity'},
                   6785:     );
                   6786: 
                   6787:     my $reply = &Apache::lonnet::userlog_query($uname,$udom,%filters);
                   6788:     unless ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   6789:         my (%courses,%missing);
                   6790:         my @results = split(/\&/,$reply);
                   6791:         foreach my $item (reverse(@results)) {
                   6792:             my ($timestamp,$host,$event) = split(/:/,$item);
                   6793:             next unless ($event =~ /^(Log|Role)/);
                   6794:             if ($curr{'show'} !~ /\D/) {
                   6795:                 if ($count >= $curr{'page'} * $curr{'show'}) {
                   6796:                     $more_records = 1;
                   6797:                     last;
                   6798:                 }
                   6799:             }
                   6800:             $count ++;
                   6801:             next if ($count < $minshown);
                   6802:             unless ($showntableheader) {
                   6803:                 $r->print($nav_script
                   6804:                          .&Apache::loncommon::start_data_table()
                   6805:                          .$tableheader);
                   6806:                 $r->rflush();
                   6807:                 $showntableheader = 1;
                   6808:             }
1.406.2.6  raeburn  6809:             my ($shown,$extra);
1.406.2.5  raeburn  6810:             my ($event,$data) = split(/\s+/,&unescape($event));
                   6811:             if ($event eq 'Role') {
                   6812:                 my ($rolecode,$extent) = split(/\./,$data,2);
                   6813:                 next if ($extent eq '');
                   6814:                 my ($crstype,$desc,$info);
1.406.2.6  raeburn  6815:                 if ($extent =~ m{^/($match_domain)/($match_courseid)(?:/(\w+)|)$}) {
                   6816:                     my ($cdom,$cnum,$sec) = ($1,$2,$3);
1.406.2.5  raeburn  6817:                     my $cid = $cdom.'_'.$cnum;
                   6818:                     if (exists($courses{$cid})) {
                   6819:                         $crstype = $courses{$cid}{'type'};
                   6820:                         $desc = $courses{$cid}{'description'};
                   6821:                     } elsif ($missing{$cid}) {
                   6822:                         $crstype = 'Course';
                   6823:                         $desc = 'Course/Community';
                   6824:                     } else {
                   6825:                         my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   6826:                         if (ref($crsinfo{$cdom.'_'.$cnum}) eq 'HASH') {
                   6827:                             $courses{$cid} = $crsinfo{$cid};
                   6828:                             $crstype = $crsinfo{$cid}{'type'};
                   6829:                             $desc = $crsinfo{$cid}{'description'};
                   6830:                         } else {
                   6831:                             $missing{$cid} = 1;
                   6832:                         }
                   6833:                     }
                   6834:                     $extra = &mt($crstype).': <a href="/public/'.$cdom.'/'.$cnum.'/syllabus">'.$desc.'</a>';
1.406.2.6  raeburn  6835:                     if ($sec ne '') {
                   6836:                        $extra .= ' ('.&mt('Section: [_1]',$sec).')';
                   6837:                     }
1.406.2.5  raeburn  6838:                 } elsif ($extent =~ m{^/($match_domain)/($match_username|$)}) {
                   6839:                     my ($dom,$name) = ($1,$2);
                   6840:                     if ($rolecode eq 'au') {
                   6841:                         $extra = '';
                   6842:                     } elsif ($rolecode =~ /^(ca|aa)$/) {
                   6843:                         $extra = &mt('Authoring Space: [_1]',$name.':'.$dom);
                   6844:                     } elsif ($rolecode =~ /^(li|dg|dh|dc|sc)$/) {
                   6845:                         $extra = &mt('Domain: [_1]',$dom);
                   6846:                     }
                   6847:                 }
                   6848:                 my $rolename;
                   6849:                 if ($rolecode =~ m{^cr/($match_domain)/($match_username)/(\w+)}) {
                   6850:                     my $role = $3;
                   6851:                     my $owner = "($2:$1)";
                   6852:                     if ($2 eq $1.'-domainconfig') {
                   6853:                         $owner = '(ad hoc)';
                   6854:                     }
                   6855:                     $rolename = &mt('Custom role: [_1]',$role.' '.$owner);
                   6856:                 } else {
                   6857:                     $rolename = &Apache::lonnet::plaintext($rolecode,$crstype);
                   6858:                 }
                   6859:                 $shown = &mt('Role selection: [_1]',$rolename);
                   6860:             } else {
                   6861:                 $shown = &mt($event);
                   6862:                 if ($data ne '') {
                   6863:                    $extra = &mt('Client IP address: [_1]',$data);
                   6864:                 }
                   6865:             }
                   6866:             $r->print(
                   6867:             &Apache::loncommon::start_data_table_row()
                   6868:            .'<td>'.$count.'</td>'
                   6869:            .'<td>'.&Apache::lonlocal::locallocaltime($timestamp).'</td>'
                   6870:            .'<td>'.$host.'</td>'
                   6871:            .'<td>'.$shown.'</td>'
                   6872:            .'<td>'.$extra.'</td>'
                   6873:            .&Apache::loncommon::end_data_table_row()."\n");
                   6874:         }
                   6875:     }
                   6876: 
                   6877:     if ($showntableheader) { # Table footer, if content displayed above
                   6878:         $r->print(&Apache::loncommon::end_data_table().
                   6879:                   &userlogdisplay_navlinks(\%curr,$more_records));
                   6880:     } else { # No content displayed above
                   6881:         $r->print('<p class="LC_info">'
                   6882:                  .&mt('There are no records to display.')
                   6883:                  .'</p>');
                   6884:     }
                   6885: 
1.406.2.8  raeburn  6886:     if ($env{'form.popup'} == 1) {
                   6887:         $r->print('<input type="hidden" name="popup" value="1" />'."\n");
                   6888:     }
                   6889: 
1.406.2.5  raeburn  6890:     # Form Footer
                   6891:     $r->print(
                   6892:         '<input type="hidden" name="currstate" value="" />'
                   6893:        .'<input type="hidden" name="accessuname" value="'.$uname.'" />'
                   6894:        .'<input type="hidden" name="accessudom" value="'.$udom.'" />'
                   6895:        .'<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   6896:        .'<input type="hidden" name="prevphases" value="'.$prevphasestr.'" />'
                   6897:        .'<input type="hidden" name="phase" value="activity" />'
                   6898:        .'<input type="hidden" name="action" value="accesslogs" />'
                   6899:        .'<input type="hidden" name="srchdomain" value="'.$udom.'" />'
                   6900:        .'<input type="hidden" name="srchby" value="'.$env{'form.srchby'}.'" />'
                   6901:        .'<input type="hidden" name="srchtype" value="'.$env{'form.srchtype'}.'" />'
                   6902:        .'<input type="hidden" name="srchterm" value="'.&HTML::Entities::encode($env{'form.srchterm'},'<>"&').'" />'
                   6903:        .'<input type="hidden" name="srchin" value="'.$env{'form.srchin'}.'" />'
                   6904:        .'</form>');
                   6905:     return;
                   6906: }
                   6907: 
                   6908: sub earlyout_accesslog_form {
                   6909:     my ($formname,$prevphasestr,$udom) = @_;
                   6910:     my $srchterm = &HTML::Entities::encode($env{'form.srchterm'},'<>"&');
                   6911:    return <<"END";
                   6912: <form action="/adm/createuser" method="post" name="$formname">
                   6913: <input type="hidden" name="currstate" value="" />
                   6914: <input type="hidden" name="prevphases" value="$prevphasestr" />
                   6915: <input type="hidden" name="phase" value="activity" />
                   6916: <input type="hidden" name="action" value="accesslogs" />
                   6917: <input type="hidden" name="srchdomain" value="$udom" />
                   6918: <input type="hidden" name="srchby" value="$env{'form.srchby'}" />
                   6919: <input type="hidden" name="srchtype" value="$env{'form.srchtype'}" />
                   6920: <input type="hidden" name="srchterm" value="$srchterm" />
                   6921: <input type="hidden" name="srchin" value="$env{'form.srchin'}" />
                   6922: </form>
                   6923: END
                   6924: }
                   6925: 
                   6926: sub activity_display_filter {
                   6927:     my ($formname,$curr) = @_;
                   6928:     my $nolink = 1;
                   6929:     my $output = '<table><tr><td valign="top">'.
                   6930:                  '<span class="LC_nobreak"><b>'.&mt('Actions/page:').'</b></span><br />'.
                   6931:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   6932:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   6933:                  '</td><td>&nbsp;&nbsp;</td>';
                   6934:     my $startform =
                   6935:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_start_date',
                   6936:                                             $curr->{'accesslog_start_date'},undef,
                   6937:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   6938:     my $endform =
                   6939:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_end_date',
                   6940:                                             $curr->{'accesslog_end_date'},undef,
                   6941:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   6942:     my %lt = &Apache::lonlocal::texthash (
                   6943:                                           activity => 'Activity',
                   6944:                                           Role     => 'Role selection',
                   6945:                                           log      => 'Log-in or Logout',
                   6946:     );
                   6947:     $output .= '<td valign="top"><b>'.&mt('Window during which actions occurred:').'</b><br />'.
                   6948:                '<table><tr><td>'.&mt('After:').
                   6949:                '</td><td>'.$startform.'</td></tr>'.
                   6950:                '<tr><td>'.&mt('Before:').'</td>'.
                   6951:                '<td>'.$endform.'</td></tr></table>'.
                   6952:                '</td>'.
                   6953:                '<td>&nbsp;&nbsp;</td>'.
                   6954:                '<td valign="top"><b>'.&mt('Activities').'</b><br />'.
                   6955:                '<select name="activity"><option value="any"';
                   6956:     if ($curr->{'activity'} eq 'any') {
                   6957:         $output .= ' selected="selected"';
                   6958:     }
                   6959:     $output .= '>'.&mt('Any').'</option>'."\n";
                   6960:     foreach my $activity ('Role','log') {
                   6961:         my $selstr = '';
                   6962:         if ($activity eq $curr->{'activity'}) {
                   6963:             $selstr = ' selected="selected"';
                   6964:         }
                   6965:         $output .= '<option value="'.$activity.'"'.$selstr.'>'.$lt{$activity}.'</option>';
                   6966:     }
                   6967:     $output .= '</select></td>'.
                   6968:                '</tr></table>';
                   6969:     # Update Display button
                   6970:     $output .= '<p>'
                   6971:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   6972:               .'</p>';
                   6973:     return $output;
                   6974: }
                   6975: 
                   6976: sub userlogdisplay_js {
                   6977:     my ($formname) = @_;
                   6978:     return <<"ENDSCRIPT";
                   6979: 
1.239     raeburn  6980: function chgPage(caller) {
                   6981:     if (caller == 'previous') {
                   6982:         document.$formname.page.value --;
                   6983:     }
                   6984:     if (caller == 'next') {
                   6985:         document.$formname.page.value ++;
                   6986:     }
1.327     raeburn  6987:     document.$formname.submit();
1.239     raeburn  6988:     return;
                   6989: }
                   6990: ENDSCRIPT
1.406.2.5  raeburn  6991: }
                   6992: 
                   6993: sub userlogdisplay_navlinks {
                   6994:     my ($curr,$more_records) = @_;
                   6995:     return unless(ref($curr) eq 'HASH');
                   6996:     # Navigation Buttons
                   6997:     my $nav_links = '<p>';
                   6998:     if (($curr->{'page'} > 1) || ($more_records)) {
                   6999:         if (($curr->{'page'} > 1) && ($curr->{'show'} !~ /\D/)) {
                   7000:             $nav_links .= '<input type="button"'
                   7001:                          .' onclick="javascript:chgPage('."'previous'".');"'
                   7002:                          .' value="'.&mt('Previous [_1] changes',$curr->{'show'})
                   7003:                          .'" /> ';
                   7004:         }
                   7005:         if ($more_records) {
                   7006:             $nav_links .= '<input type="button"'
                   7007:                          .' onclick="javascript:chgPage('."'next'".');"'
                   7008:                          .' value="'.&mt('Next [_1] changes',$curr->{'show'})
                   7009:                          .'" />';
1.301     bisitz   7010:         }
                   7011:     }
1.406.2.5  raeburn  7012:     $nav_links .= '</p>';
                   7013:     return $nav_links;
1.239     raeburn  7014: }
                   7015: 
                   7016: sub role_display_filter {
1.363     raeburn  7017:     my ($context,$formname,$cdom,$cnum,$curr,$version,$crstype) = @_;
                   7018:     my $lctype;
                   7019:     if ($context eq 'course') {
                   7020:         $lctype = lc($crstype);
                   7021:     }
1.239     raeburn  7022:     my $nolink = 1;
                   7023:     my $output = '<table><tr><td valign="top">'.
1.301     bisitz   7024:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b></span><br />'.
1.239     raeburn  7025:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   7026:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   7027:                  '</td><td>&nbsp;&nbsp;</td>';
                   7028:     my $startform =
                   7029:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
                   7030:                                             $curr->{'rolelog_start_date'},undef,
                   7031:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   7032:     my $endform =
                   7033:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
                   7034:                                             $curr->{'rolelog_end_date'},undef,
                   7035:                                             undef,undef,undef,undef,undef,undef,$nolink);
1.363     raeburn  7036:     my %lt = &rolechg_contexts($context,$crstype);
1.301     bisitz   7037:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br />'.
                   7038:                '<table><tr><td>'.&mt('After:').
                   7039:                '</td><td>'.$startform.'</td></tr>'.
                   7040:                '<tr><td>'.&mt('Before:').'</td>'.
                   7041:                '<td>'.$endform.'</td></tr></table>'.
                   7042:                '</td>'.
                   7043:                '<td>&nbsp;&nbsp;</td>'.
1.239     raeburn  7044:                '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
                   7045:                '<select name="role"><option value="any"';
                   7046:     if ($curr->{'role'} eq 'any') {
                   7047:         $output .= ' selected="selected"';
                   7048:     }
                   7049:     $output .=  '>'.&mt('Any').'</option>'."\n";
1.363     raeburn  7050:     my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
1.239     raeburn  7051:     foreach my $role (@roles) {
                   7052:         my $plrole;
                   7053:         if ($role eq 'cr') {
                   7054:             $plrole = &mt('Custom Role');
                   7055:         } else {
1.318     raeburn  7056:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.239     raeburn  7057:         }
                   7058:         my $selstr = '';
                   7059:         if ($role eq $curr->{'role'}) {
                   7060:             $selstr = ' selected="selected"';
                   7061:         }
                   7062:         $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
                   7063:     }
1.301     bisitz   7064:     $output .= '</select></td>'.
                   7065:                '<td>&nbsp;&nbsp;</td>'.
                   7066:                '<td valign="top"><b>'.
1.239     raeburn  7067:                &mt('Context:').'</b><br /><select name="chgcontext">';
1.363     raeburn  7068:     my @posscontexts;
                   7069:     if ($context eq 'course') {
1.376     raeburn  7070:         @posscontexts = ('any','automated','updatenow','createcourse','course','domain','selfenroll','requestcourses');
1.363     raeburn  7071:     } elsif ($context eq 'domain') {
                   7072:         @posscontexts = ('any','domain','requestauthor','domconfig','server');
                   7073:     } else {
                   7074:         @posscontexts = ('any','author','domain');
                   7075:     } 
                   7076:     foreach my $chgtype (@posscontexts) {
1.239     raeburn  7077:         my $selstr = '';
                   7078:         if ($curr->{'chgcontext'} eq $chgtype) {
1.301     bisitz   7079:             $selstr = ' selected="selected"';
1.239     raeburn  7080:         }
1.363     raeburn  7081:         if ($context eq 'course') {
1.376     raeburn  7082:             if (($chgtype eq 'automated') || ($chgtype eq 'updatenow')) {
1.363     raeburn  7083:                 next if (!&Apache::lonnet::auto_run($cnum,$cdom));
                   7084:             }
1.239     raeburn  7085:         }
                   7086:         $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
1.248     raeburn  7087:     }
1.303     bisitz   7088:     $output .= '</select></td>'
                   7089:               .'</tr></table>';
                   7090: 
                   7091:     # Update Display button
                   7092:     $output .= '<p>'
                   7093:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   7094:               .'</p>';
                   7095: 
                   7096:     # Server version info
1.363     raeburn  7097:     my $needsrev = '2.11.0';
                   7098:     if ($context eq 'course') {
                   7099:         $needsrev = '2.7.0';
                   7100:     }
                   7101:     
1.303     bisitz   7102:     $output .= '<p class="LC_info">'
                   7103:               .&mt('Only changes made from servers running LON-CAPA [_1] or later are displayed.'
1.363     raeburn  7104:                   ,$needsrev);
1.248     raeburn  7105:     if ($version) {
1.303     bisitz   7106:         $output .= ' '.&mt('This LON-CAPA server is version [_1]',$version);
                   7107:     }
                   7108:     $output .= '</p><hr />';
1.239     raeburn  7109:     return $output;
                   7110: }
                   7111: 
                   7112: sub rolechg_contexts {
1.363     raeburn  7113:     my ($context,$crstype) = @_;
                   7114:     my %lt;
                   7115:     if ($context eq 'course') {
                   7116:         %lt = &Apache::lonlocal::texthash (
1.239     raeburn  7117:                                              any          => 'Any',
1.376     raeburn  7118:                                              automated    => 'Automated Enrollment',
1.239     raeburn  7119:                                              updatenow    => 'Roster Update',
                   7120:                                              createcourse => 'Course Creation',
                   7121:                                              course       => 'User Management in course',
                   7122:                                              domain       => 'User Management in domain',
1.313     raeburn  7123:                                              selfenroll   => 'Self-enrolled',
1.318     raeburn  7124:                                              requestcourses => 'Course Request',
1.239     raeburn  7125:                                          );
1.363     raeburn  7126:         if ($crstype eq 'Community') {
                   7127:             $lt{'createcourse'} = &mt('Community Creation');
                   7128:             $lt{'course'} = &mt('User Management in community');
                   7129:             $lt{'requestcourses'} = &mt('Community Request');
                   7130:         }
                   7131:     } elsif ($context eq 'domain') {
                   7132:         %lt = &Apache::lonlocal::texthash (
                   7133:                                              any           => 'Any',
                   7134:                                              domain        => 'User Management in domain',
                   7135:                                              requestauthor => 'Authoring Request',
                   7136:                                              server        => 'Command line script (DC role)',
                   7137:                                              domconfig     => 'Self-enrolled',
                   7138:                                          );
                   7139:     } else {
                   7140:         %lt = &Apache::lonlocal::texthash (
                   7141:                                              any    => 'Any',
                   7142:                                              domain => 'User Management in domain',
                   7143:                                              author => 'User Management by author',
                   7144:                                          );
                   7145:     } 
1.239     raeburn  7146:     return %lt;
                   7147: }
                   7148: 
1.27      matthew  7149: #-------------------------------------------------- functions for &phase_two
1.160     raeburn  7150: sub user_search_result {
1.221     raeburn  7151:     my ($context,$srch) = @_;
1.160     raeburn  7152:     my %allhomes;
                   7153:     my %inst_matches;
                   7154:     my %srch_results;
1.181     raeburn  7155:     my ($response,$currstate,$forcenewuser,$dirsrchres);
1.183     raeburn  7156:     $srch->{'srchterm'} =~ s/\s+/ /g;
1.176     raeburn  7157:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
1.160     raeburn  7158:         $response = &mt('Invalid search.');
                   7159:     }
                   7160:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
                   7161:         $response = &mt('Invalid search.');
                   7162:     }
1.177     raeburn  7163:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
1.160     raeburn  7164:         $response = &mt('Invalid search.');
                   7165:     }
                   7166:     if ($srch->{'srchterm'} eq '') {
                   7167:         $response = &mt('You must enter a search term.');
                   7168:     }
1.183     raeburn  7169:     if ($srch->{'srchterm'} =~ /^\s+$/) {
                   7170:         $response = &mt('Your search term must contain more than just spaces.');
                   7171:     }
1.160     raeburn  7172:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
                   7173:         if (($srch->{'srchdomain'} eq '') || 
1.163     albertel 7174: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
1.160     raeburn  7175:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
                   7176:         }
                   7177:     }
                   7178:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
                   7179:         ($srch->{'srchin'} eq 'alc')) {
1.176     raeburn  7180:         if ($srch->{'srchby'} eq 'uname') {
1.243     raeburn  7181:             my $unamecheck = $srch->{'srchterm'};
                   7182:             if ($srch->{'srchtype'} eq 'contains') {
                   7183:                 if ($unamecheck !~ /^\w/) {
                   7184:                     $unamecheck = 'a'.$unamecheck; 
                   7185:                 }
                   7186:             }
                   7187:             if ($unamecheck !~ /^$match_username$/) {
1.176     raeburn  7188:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
                   7189:             }
1.160     raeburn  7190:         }
                   7191:     }
1.180     raeburn  7192:     if ($response ne '') {
1.406.2.4  raeburn  7193:         $response = '<span class="LC_warning">'.$response.'</span><br />';
1.180     raeburn  7194:     }
1.160     raeburn  7195:     if ($srch->{'srchin'} eq 'instd') {
1.406.2.3  raeburn  7196:         my $instd_chk = &instdirectorysrch_check($srch);
1.160     raeburn  7197:         if ($instd_chk ne 'ok') {
1.406.2.3  raeburn  7198:             my $domd_chk = &domdirectorysrch_check($srch);
1.406.2.4  raeburn  7199:             $response .= '<span class="LC_warning">'.$instd_chk.'</span><br />';
1.406.2.3  raeburn  7200:             if ($domd_chk eq 'ok') {
1.406.2.4  raeburn  7201:                 $response .= &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.');
1.406.2.3  raeburn  7202:             }
1.406.2.5  raeburn  7203:             $response .= '<br />';
1.406.2.3  raeburn  7204:         }
                   7205:     } else {
                   7206:         unless (($context eq 'requestcrs') && ($srch->{'srchtype'} eq 'exact')) {
                   7207:             my $domd_chk = &domdirectorysrch_check($srch);
                   7208:             if ($domd_chk ne 'ok') {
                   7209:                 my $instd_chk = &instdirectorysrch_check($srch);
1.406.2.4  raeburn  7210:                 $response .= '<span class="LC_warning">'.$domd_chk.'</span><br />';
1.406.2.3  raeburn  7211:                 if ($instd_chk eq 'ok') {
1.406.2.4  raeburn  7212:                     $response .= &mt('You may want to search in the institutional directory instead of the LON-CAPA domain.');
1.406.2.3  raeburn  7213:                 }
1.406.2.5  raeburn  7214:                 $response .= '<br />';
1.406.2.3  raeburn  7215:             }
1.160     raeburn  7216:         }
                   7217:     }
                   7218:     if ($response ne '') {
1.180     raeburn  7219:         return ($currstate,$response);
1.160     raeburn  7220:     }
                   7221:     if ($srch->{'srchby'} eq 'uname') {
                   7222:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
                   7223:             if ($env{'form.forcenew'}) {
                   7224:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
                   7225:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   7226:                     if ($uhome eq 'no_host') {
                   7227:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.180     raeburn  7228:                         my $showdom = &display_domain_info($env{'request.role.domain'});
                   7229:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
1.160     raeburn  7230:                     } else {
1.179     raeburn  7231:                         $currstate = 'modify';
1.160     raeburn  7232:                     }
                   7233:                 } else {
1.179     raeburn  7234:                     $currstate = 'modify';
1.160     raeburn  7235:                 }
                   7236:             } else {
                   7237:                 if ($srch->{'srchin'} eq 'dom') {
1.162     raeburn  7238:                     if ($srch->{'srchtype'} eq 'exact') {
                   7239:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   7240:                         if ($uhome eq 'no_host') {
1.179     raeburn  7241:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  7242:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  7243:                         } else {
1.179     raeburn  7244:                             $currstate = 'modify';
1.406.2.5  raeburn  7245:                             if ($env{'form.action'} eq 'accesslogs') {
                   7246:                                 $currstate = 'activity';
                   7247:                             }
1.310     raeburn  7248:                             my $uname = $srch->{'srchterm'};
                   7249:                             my $udom = $srch->{'srchdomain'};
                   7250:                             $srch_results{$uname.':'.$udom} =
                   7251:                                 { &Apache::lonnet::get('environment',
                   7252:                                                        ['firstname',
                   7253:                                                         'lastname',
                   7254:                                                         'permanentemail'],
                   7255:                                                          $udom,$uname)
                   7256:                                 };
1.162     raeburn  7257:                         }
                   7258:                     } else {
                   7259:                         %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  7260:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  7261:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  7262:                     }
                   7263:                 } else {
1.167     albertel 7264:                     my $courseusers = &get_courseusers();
1.162     raeburn  7265:                     if ($srch->{'srchtype'} eq 'exact') {
1.167     albertel 7266:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
1.179     raeburn  7267:                             $currstate = 'modify';
1.162     raeburn  7268:                         } else {
1.179     raeburn  7269:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  7270:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  7271:                         }
1.160     raeburn  7272:                     } else {
1.167     albertel 7273:                         foreach my $user (keys(%$courseusers)) {
1.162     raeburn  7274:                             my ($cuname,$cudomain) = split(/:/,$user);
                   7275:                             if ($cudomain eq $srch->{'srchdomain'}) {
1.177     raeburn  7276:                                 my $matched = 0;
                   7277:                                 if ($srch->{'srchtype'} eq 'begins') {
                   7278:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
                   7279:                                         $matched = 1;
                   7280:                                     }
                   7281:                                 } else {
                   7282:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
                   7283:                                         $matched = 1;
                   7284:                                     }
                   7285:                                 }
                   7286:                                 if ($matched) {
1.167     albertel 7287:                                     $srch_results{$user} = 
                   7288: 					{&Apache::lonnet::get('environment',
                   7289: 							     ['firstname',
                   7290: 							      'lastname',
1.194     albertel 7291: 							      'permanentemail'],
                   7292: 							      $cudomain,$cuname)};
1.162     raeburn  7293:                                 }
                   7294:                             }
                   7295:                         }
1.179     raeburn  7296:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  7297:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  7298:                     }
                   7299:                 }
                   7300:             }
                   7301:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  7302:             $currstate = 'query';
1.160     raeburn  7303:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  7304:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
                   7305:             if ($dirsrchres eq 'ok') {
                   7306:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7307:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  7308:             } else {
                   7309:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   7310:                 $response = '<span class="LC_warning">'.
                   7311:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   7312:                     '</span><br />'.
                   7313:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
1.406.2.5  raeburn  7314:                     '<br />'; 
1.181     raeburn  7315:             }
1.160     raeburn  7316:         }
                   7317:     } else {
                   7318:         if ($srch->{'srchin'} eq 'dom') {
                   7319:             %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  7320:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7321:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  7322:         } elsif ($srch->{'srchin'} eq 'crs') {
1.167     albertel 7323:             my $courseusers = &get_courseusers(); 
                   7324:             foreach my $user (keys(%$courseusers)) {
1.160     raeburn  7325:                 my ($uname,$udom) = split(/:/,$user);
                   7326:                 my %names = &Apache::loncommon::getnames($uname,$udom);
                   7327:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
                   7328:                 if ($srch->{'srchby'} eq 'lastname') {
                   7329:                     if ((($srch->{'srchtype'} eq 'exact') && 
                   7330:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
1.177     raeburn  7331:                         (($srch->{'srchtype'} eq 'begins') &&
                   7332:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
1.160     raeburn  7333:                         (($srch->{'srchtype'} eq 'contains') &&
                   7334:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
                   7335:                         $srch_results{$user} = {firstname => $names{'firstname'},
                   7336:                                             lastname => $names{'lastname'},
                   7337:                                             permanentemail => $emails{'permanentemail'},
                   7338:                                            };
                   7339:                     }
                   7340:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
                   7341:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
1.177     raeburn  7342:                     $srchlast =~ s/\s+$//;
                   7343:                     $srchfirst =~ s/^\s+//;
1.160     raeburn  7344:                     if ($srch->{'srchtype'} eq 'exact') {
                   7345:                         if (($names{'lastname'} eq $srchlast) &&
                   7346:                             ($names{'firstname'} eq $srchfirst)) {
                   7347:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   7348:                                                 lastname => $names{'lastname'},
                   7349:                                                 permanentemail => $emails{'permanentemail'},
                   7350: 
                   7351:                                            };
                   7352:                         }
1.177     raeburn  7353:                     } elsif ($srch->{'srchtype'} eq 'begins') {
                   7354:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
                   7355:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
                   7356:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   7357:                                                 lastname => $names{'lastname'},
                   7358:                                                 permanentemail => $emails{'permanentemail'},
                   7359:                                                };
                   7360:                         }
                   7361:                     } else {
1.160     raeburn  7362:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
                   7363:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
                   7364:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   7365:                                                 lastname => $names{'lastname'},
                   7366:                                                 permanentemail => $emails{'permanentemail'},
                   7367:                                                };
                   7368:                         }
                   7369:                     }
                   7370:                 }
                   7371:             }
1.179     raeburn  7372:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7373:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  7374:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  7375:             $currstate = 'query';
1.160     raeburn  7376:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  7377:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
                   7378:             if ($dirsrchres eq 'ok') {
                   7379:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7380:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  7381:             } else {
1.406.2.5  raeburn  7382:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   7383:                 $response = '<span class="LC_warning">'.
1.181     raeburn  7384:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   7385:                     '</span><br />'.
                   7386:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
1.406.2.5  raeburn  7387:                     '<br />';
1.181     raeburn  7388:             }
1.160     raeburn  7389:         }
                   7390:     }
1.179     raeburn  7391:     return ($currstate,$response,$forcenewuser,\%srch_results);
1.160     raeburn  7392: }
                   7393: 
1.406.2.3  raeburn  7394: sub domdirectorysrch_check {
                   7395:     my ($srch) = @_;
                   7396:     my $response;
                   7397:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   7398:                                              ['directorysrch'],$srch->{'srchdomain'});
                   7399:     my $showdom = &display_domain_info($srch->{'srchdomain'});
                   7400:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   7401:         if ($dom_inst_srch{'directorysrch'}{'lcavailable'} eq '0') {
                   7402:             return &mt('LON-CAPA directory search is not available in domain: [_1]',$showdom);
                   7403:         }
                   7404:         if ($dom_inst_srch{'directorysrch'}{'lclocalonly'}) {
                   7405:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
                   7406:                 return &mt('LON-CAPA directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom);
                   7407:             }
                   7408:         }
                   7409:     }
                   7410:     return 'ok';
                   7411: }
                   7412: 
                   7413: sub instdirectorysrch_check {
1.160     raeburn  7414:     my ($srch) = @_;
                   7415:     my $can_search = 0;
                   7416:     my $response;
                   7417:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   7418:                                              ['directorysrch'],$srch->{'srchdomain'});
1.180     raeburn  7419:     my $showdom = &display_domain_info($srch->{'srchdomain'});
1.160     raeburn  7420:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   7421:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
1.180     raeburn  7422:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
1.160     raeburn  7423:         }
                   7424:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
                   7425:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
1.180     raeburn  7426:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
1.160     raeburn  7427:             }
                   7428:             my @usertypes = split(/:/,$env{'environment.inststatus'});
                   7429:             if (!@usertypes) {
                   7430:                 push(@usertypes,'default');
                   7431:             }
                   7432:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
                   7433:                 foreach my $type (@usertypes) {
                   7434:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
                   7435:                         $can_search = 1;
                   7436:                         last;
                   7437:                     }
                   7438:                 }
                   7439:             }
                   7440:             if (!$can_search) {
                   7441:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
                   7442:                 my @longtypes; 
                   7443:                 foreach my $item (@usertypes) {
1.229     raeburn  7444:                     if (defined($insttypes->{$item})) { 
                   7445:                         push (@longtypes,$insttypes->{$item});
                   7446:                     } elsif ($item eq 'default') {
                   7447:                         push (@longtypes,&mt('other')); 
                   7448:                     }
1.160     raeburn  7449:                 }
                   7450:                 my $insttype_str = join(', ',@longtypes); 
1.180     raeburn  7451:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
1.229     raeburn  7452:             }
1.160     raeburn  7453:         } else {
                   7454:             $can_search = 1;
                   7455:         }
                   7456:     } else {
1.180     raeburn  7457:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
1.160     raeburn  7458:     }
                   7459:     my %longtext = &Apache::lonlocal::texthash (
1.167     albertel 7460:                        uname     => 'username',
1.160     raeburn  7461:                        lastfirst => 'last name, first name',
1.167     albertel 7462:                        lastname  => 'last name',
1.172     raeburn  7463:                        contains  => 'contains',
1.178     raeburn  7464:                        exact     => 'as exact match to',
                   7465:                        begins    => 'begins with',
1.160     raeburn  7466:                    );
                   7467:     if ($can_search) {
                   7468:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
                   7469:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
1.180     raeburn  7470:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
1.160     raeburn  7471:             }
                   7472:         } else {
1.180     raeburn  7473:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
1.160     raeburn  7474:         }
                   7475:     }
                   7476:     if ($can_search) {
1.178     raeburn  7477:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
                   7478:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
                   7479:                 return 'ok';
                   7480:             } else {
1.180     raeburn  7481:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  7482:             }
                   7483:         } else {
                   7484:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
                   7485:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
                   7486:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
                   7487:                 return 'ok';
                   7488:             } else {
1.180     raeburn  7489:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  7490:             }
1.160     raeburn  7491:         }
                   7492:     }
                   7493: }
                   7494: 
                   7495: sub get_courseusers {
                   7496:     my %advhash;
1.167     albertel 7497:     my $classlist = &Apache::loncoursedata::get_classlist();
1.160     raeburn  7498:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                   7499:     foreach my $role (sort(keys(%coursepersonnel))) {
                   7500:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
1.167     albertel 7501: 	    if (!exists($classlist->{$user})) {
                   7502: 		$classlist->{$user} = [];
                   7503: 	    }
1.160     raeburn  7504:         }
                   7505:     }
1.167     albertel 7506:     return $classlist;
1.160     raeburn  7507: }
                   7508: 
                   7509: sub build_search_response {
1.221     raeburn  7510:     my ($context,$srch,%srch_results) = @_;
1.179     raeburn  7511:     my ($currstate,$response,$forcenewuser);
1.160     raeburn  7512:     my %names = (
1.330     bisitz   7513:           'uname'     => 'username',
                   7514:           'lastname'  => 'last name',
1.160     raeburn  7515:           'lastfirst' => 'last name, first name',
1.330     bisitz   7516:           'crs'       => 'this course',
                   7517:           'dom'       => 'LON-CAPA domain',
                   7518:           'instd'     => 'the institutional directory for domain',
1.160     raeburn  7519:     );
                   7520: 
                   7521:     my %single = (
1.180     raeburn  7522:                    begins   => 'A match',
1.160     raeburn  7523:                    contains => 'A match',
1.180     raeburn  7524:                    exact    => 'An exact match',
1.160     raeburn  7525:                  );
                   7526:     my %nomatch = (
1.180     raeburn  7527:                    begins   => 'No match',
1.160     raeburn  7528:                    contains => 'No match',
1.180     raeburn  7529:                    exact    => 'No exact match',
1.160     raeburn  7530:                   );
                   7531:     if (keys(%srch_results) > 1) {
1.179     raeburn  7532:         $currstate = 'select';
1.160     raeburn  7533:     } else {
                   7534:         if (keys(%srch_results) == 1) {
1.406.2.5  raeburn  7535:             if ($env{'form.action'} eq 'accesslogs') {
                   7536:                 $currstate = 'activity';
                   7537:             } else {
                   7538:                 $currstate = 'modify';
                   7539:             }
1.180     raeburn  7540:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
                   7541:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   7542:                 $response .= ': '.&display_domain_info($srch->{'srchdomain'});
1.180     raeburn  7543:             }
1.330     bisitz   7544:         } else { # Search has nothing found. Prepare message to user.
                   7545:             $response = '<span class="LC_warning">';
1.180     raeburn  7546:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   7547:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}: [_2]",
                   7548:                                  '<b>'.$srch->{'srchterm'}.'</b>',
                   7549:                                  &display_domain_info($srch->{'srchdomain'}));
                   7550:             } else {
                   7551:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}.",
                   7552:                                  '<b>'.$srch->{'srchterm'}.'</b>');
1.180     raeburn  7553:             }
                   7554:             $response .= '</span>';
1.330     bisitz   7555: 
1.160     raeburn  7556:             if ($srch->{'srchin'} ne 'alc') {
                   7557:                 $forcenewuser = 1;
                   7558:                 my $cansrchinst = 0; 
                   7559:                 if ($srch->{'srchdomain'}) {
                   7560:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
                   7561:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
                   7562:                         if ($domconfig{'directorysrch'}{'available'}) {
                   7563:                             $cansrchinst = 1;
                   7564:                         } 
                   7565:                     }
                   7566:                 }
1.180     raeburn  7567:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
                   7568:                      ($srch->{'srchby'} eq 'lastname')) &&
                   7569:                     ($srch->{'srchin'} eq 'dom')) {
                   7570:                     if ($cansrchinst) {
                   7571:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
1.160     raeburn  7572:                     }
                   7573:                 }
1.180     raeburn  7574:                 if ($srch->{'srchin'} eq 'crs') {
                   7575:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
                   7576:                 }
                   7577:             }
1.305     raeburn  7578:             my $createdom = $env{'request.role.domain'};
                   7579:             if ($context eq 'requestcrs') {
                   7580:                 if ($env{'form.coursedom'} ne '') {
                   7581:                     $createdom = $env{'form.coursedom'};
                   7582:                 }
                   7583:             }
1.406.2.5  raeburn  7584:             unless (($env{'form.action'} eq 'accesslogs') || (($srch->{'srchby'} eq 'uname') && ($srch->{'srchin'} eq 'dom') &&
                   7585:                     ($srch->{'srchtype'} eq 'exact') && ($srch->{'srchdomain'} eq $createdom))) {
1.221     raeburn  7586:                 my $cancreate =
1.305     raeburn  7587:                     &Apache::lonuserutils::can_create_user($createdom,$context);
                   7588:                 my $targetdom = '<span class="LC_cusr_emph">'.$createdom.'</span>';
1.221     raeburn  7589:                 if ($cancreate) {
1.305     raeburn  7590:                     my $showdom = &display_domain_info($createdom); 
1.266     bisitz   7591:                     $response .= '<br /><br />'
                   7592:                                 .'<b>'.&mt('To add a new user:').'</b>'
1.305     raeburn  7593:                                 .'<br />';
                   7594:                     if ($context eq 'requestcrs') {
                   7595:                         $response .= &mt("(You can only define new users in the new course's domain - [_1])",$targetdom);
                   7596:                     } else {
                   7597:                         $response .= &mt("(You can only create new users in your current role's domain - [_1])",$targetdom);
                   7598:                     }
                   7599:                     $response .='<ul><li>'
1.266     bisitz   7600:                                 .&mt("Set 'Domain/institution to search' to: [_1]",'<span class="LC_cusr_emph">'.$showdom.'</span>')
                   7601:                                 .'</li><li>'
                   7602:                                 .&mt("Set 'Search criteria' to: [_1]username is ..... in selected LON-CAPA domain[_2]",'<span class="LC_cusr_emph">','</span>')
                   7603:                                 .'</li><li>'
                   7604:                                 .&mt('Provide the proposed username')
                   7605:                                 .'</li><li>'
                   7606:                                 .&mt("Click 'Search'")
                   7607:                                 .'</li></ul><br />';
1.221     raeburn  7608:                 } else {
1.406.2.7  raeburn  7609:                     unless (($context eq 'domain') && ($env{'form.action'} eq 'singleuser')) {
                   7610:                         my $helplink = ' href="javascript:helpMenu('."'display'".')"';
                   7611:                         $response .= '<br /><br />';
                   7612:                         if ($context eq 'requestcrs') {
                   7613:                             $response .= &mt("You are not authorized to define new users in the new course's domain - [_1].",$targetdom);
                   7614:                         } else {
                   7615:                             $response .= &mt("You are not authorized to create new users in your current role's domain - [_1].",$targetdom);
                   7616:                         }
                   7617:                         $response .= '<br />'
                   7618:                                      .&mt('Please contact the [_1]helpdesk[_2] if you need to create a new user.'
                   7619:                                         ,' <a'.$helplink.'>'
                   7620:                                         ,'</a>')
                   7621:                                      .'<br />';
1.305     raeburn  7622:                     }
1.221     raeburn  7623:                 }
1.160     raeburn  7624:             }
                   7625:         }
                   7626:     }
1.179     raeburn  7627:     return ($currstate,$response,$forcenewuser);
1.160     raeburn  7628: }
                   7629: 
1.180     raeburn  7630: sub display_domain_info {
                   7631:     my ($dom) = @_;
                   7632:     my $output = $dom;
                   7633:     if ($dom ne '') { 
                   7634:         my $domdesc = &Apache::lonnet::domain($dom,'description');
                   7635:         if ($domdesc ne '') {
                   7636:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
                   7637:         }
                   7638:     }
                   7639:     return $output;
                   7640: }
                   7641: 
1.160     raeburn  7642: sub crumb_utilities {
                   7643:     my %elements = (
                   7644:        crtuser => {
                   7645:            srchterm => 'text',
1.172     raeburn  7646:            srchin => 'selectbox',
1.160     raeburn  7647:            srchby => 'selectbox',
                   7648:            srchtype => 'selectbox',
                   7649:            srchdomain => 'selectbox',
                   7650:        },
1.207     raeburn  7651:        crtusername => {
                   7652:            srchterm => 'text',
                   7653:            srchdomain => 'selectbox',
                   7654:        },
1.160     raeburn  7655:        docustom => {
                   7656:            rolename => 'selectbox',
                   7657:            newrolename => 'textbox',
                   7658:        },
1.179     raeburn  7659:        studentform => {
                   7660:            srchterm => 'text',
                   7661:            srchin => 'selectbox',
                   7662:            srchby => 'selectbox',
                   7663:            srchtype => 'selectbox',
                   7664:            srchdomain => 'selectbox',
                   7665:        },
1.160     raeburn  7666:     );
                   7667: 
                   7668:     my $jsback .= qq|
                   7669: function backPage(formname,prevphase,prevstate) {
1.211     raeburn  7670:     if (typeof prevphase == 'undefined') {
                   7671:         formname.phase.value = '';
                   7672:     }
                   7673:     else {  
                   7674:         formname.phase.value = prevphase;
                   7675:     }
                   7676:     if (typeof prevstate == 'undefined') {
                   7677:         formname.currstate.value = '';
                   7678:     }
                   7679:     else {
                   7680:         formname.currstate.value = prevstate;
                   7681:     }
1.160     raeburn  7682:     formname.submit();
                   7683: }
                   7684: |;
                   7685:     return ($jsback,\%elements);
                   7686: }
                   7687: 
1.26      matthew  7688: sub course_level_table {
1.375     raeburn  7689:     my ($inccourses,$showcredits,$defaultcredits) = @_;
                   7690:     return unless (ref($inccourses) eq 'HASH');
1.26      matthew  7691:     my $table = '';
1.62      www      7692: # Custom Roles?
                   7693: 
1.190     raeburn  7694:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.89      raeburn  7695:     my %lt=&Apache::lonlocal::texthash(
                   7696:             'exs'  => "Existing sections",
                   7697:             'new'  => "Define new section",
                   7698:             'ssd'  => "Set Start Date",
                   7699:             'sed'  => "Set End Date",
1.131     raeburn  7700:             'crl'  => "Course Level",
1.89      raeburn  7701:             'act'  => "Activate",
                   7702:             'rol'  => "Role",
                   7703:             'ext'  => "Extent",
1.113     raeburn  7704:             'grs'  => "Section",
1.375     raeburn  7705:             'crd'  => "Credits",
1.89      raeburn  7706:             'sta'  => "Start",
                   7707:             'end'  => "End"
                   7708:     );
1.62      www      7709: 
1.375     raeburn  7710:     foreach my $protectedcourse (sort(keys(%{$inccourses}))) {
1.135     raeburn  7711: 	my $thiscourse=$protectedcourse;
1.26      matthew  7712: 	$thiscourse=~s:_:/:g;
                   7713: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
1.365     raeburn  7714:         my $isowner = &Apache::lonuserutils::is_courseowner($protectedcourse,$coursedata{'internal.courseowner'});
1.26      matthew  7715: 	my $area=$coursedata{'description'};
1.321     raeburn  7716:         my $crstype=$coursedata{'type'};
1.135     raeburn  7717: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
1.89      raeburn  7718: 	my ($domain,$cnum)=split(/\//,$thiscourse);
1.115     albertel 7719:         my %sections_count;
1.101     albertel 7720:         if (defined($env{'request.course.id'})) {
                   7721:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
1.115     albertel 7722:                 %sections_count = 
                   7723: 		    &Apache::loncommon::get_sections($domain,$cnum);
1.92      raeburn  7724:             }
                   7725:         }
1.321     raeburn  7726:         my @roles = &Apache::lonuserutils::roles_by_context('course','',$crstype);
1.213     raeburn  7727: 	foreach my $role (@roles) {
1.321     raeburn  7728:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.329     raeburn  7729: 	    if ((&Apache::lonnet::allowed('c'.$role,$thiscourse)) ||
                   7730:                 ((($role eq 'cc') || ($role eq 'co')) && ($isowner))) {
1.221     raeburn  7731:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  7732:                                             $plrole,\%sections_count,\%lt,
1.402     raeburn  7733:                                             $showcredits,$defaultcredits,$crstype);
1.221     raeburn  7734:             } elsif ($env{'request.course.sec'} ne '') {
                   7735:                 if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
                   7736:                                              $env{'request.course.sec'})) {
                   7737:                     $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  7738:                                                 $plrole,\%sections_count,\%lt,
1.402     raeburn  7739:                                                 $showcredits,$defaultcredits,$crstype);
1.26      matthew  7740:                 }
                   7741:             }
                   7742:         }
1.221     raeburn  7743:         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
1.324     raeburn  7744:             foreach my $cust (sort(keys(%customroles))) {
                   7745:                 next if ($crstype eq 'Community' && $customroles{$cust} =~ /bre\&S/);
1.221     raeburn  7746:                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
                   7747:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.402     raeburn  7748:                                             $cust,\%sections_count,\%lt,
                   7749:                                             $showcredits,$defaultcredits,$crstype);
1.221     raeburn  7750:             }
1.62      www      7751: 	}
1.26      matthew  7752:     }
                   7753:     return '' if ($table eq ''); # return nothing if there is nothing 
                   7754:                                  # in the table
1.188     raeburn  7755:     my $result;
                   7756:     if (!$env{'request.course.id'}) {
                   7757:         $result = '<h4>'.$lt{'crl'}.'</h4>'."\n";
                   7758:     }
                   7759:     $result .= 
1.136     raeburn  7760: &Apache::loncommon::start_data_table().
                   7761: &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  7762: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
1.402     raeburn  7763: '<th>'.$lt{'ext'}.'</th><th>'."\n";
                   7764:     if ($showcredits) {
                   7765:         $result .= $lt{'crd'}.'</th>';
                   7766:     }
                   7767:     $result .=
1.375     raeburn  7768: '<th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th>'."\n".
                   7769: '<th>'.$lt{'end'}.'</th>'.
1.136     raeburn  7770: &Apache::loncommon::end_data_table_header_row().
                   7771: $table.
                   7772: &Apache::loncommon::end_data_table();
1.26      matthew  7773:     return $result;
                   7774: }
1.88      raeburn  7775: 
1.221     raeburn  7776: sub course_level_row {
1.375     raeburn  7777:     my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,
1.402     raeburn  7778:         $lt,$showcredits,$defaultcredits,$crstype) = @_;
1.375     raeburn  7779:     my $creditem;
1.222     raeburn  7780:     my $row = &Apache::loncommon::start_data_table_row().
                   7781:               ' <td><input type="checkbox" name="act_'.
                   7782:               $protectedcourse.'_'.$role.'" /></td>'."\n".
                   7783:               ' <td>'.$plrole.'</td>'."\n".
                   7784:               ' <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
1.402     raeburn  7785:     if (($showcredits) && ($role eq 'st') && ($crstype eq 'Course')) {
1.375     raeburn  7786:         $row .= 
                   7787:             '<td><input type="text" name="credits_'.$protectedcourse.'_'.
                   7788:             $role.'" size="3" value="'.$defaultcredits.'" /></td>';
                   7789:     } else {
                   7790:         $row .= '<td>&nbsp;</td>';
                   7791:     }
1.322     raeburn  7792:     if (($role eq 'cc') || ($role eq 'co')) {
1.222     raeburn  7793:         $row .= '<td>&nbsp;</td>';
1.221     raeburn  7794:     } elsif ($env{'request.course.sec'} ne '') {
1.222     raeburn  7795:         $row .= ' <td><input type="hidden" value="'.
                   7796:                 $env{'request.course.sec'}.'" '.
                   7797:                 'name="sec_'.$protectedcourse.'_'.$role.'" />'.
                   7798:                 $env{'request.course.sec'}.'</td>';
1.221     raeburn  7799:     } else {
                   7800:         if (ref($sections_count) eq 'HASH') {
                   7801:             my $currsec = 
                   7802:                 &Apache::lonuserutils::course_sections($sections_count,
                   7803:                                                        $protectedcourse.'_'.$role);
1.222     raeburn  7804:             $row .= '<td><table class="LC_createuser">'."\n".
                   7805:                     '<tr class="LC_section_row">'."\n".
                   7806:                     ' <td valign="top">'.$lt->{'exs'}.'<br />'.
                   7807:                        $currsec.'</td>'."\n".
                   7808:                      ' <td>&nbsp;&nbsp;</td>'."\n".
                   7809:                      ' <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
1.221     raeburn  7810:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
                   7811:                      '" value="" />'.
                   7812:                      '<input type="hidden" '.
                   7813:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
1.222     raeburn  7814:                      '</tr></table></td>'."\n";
1.221     raeburn  7815:         } else {
1.222     raeburn  7816:             $row .= '<td><input type="text" size="10" '.
1.375     raeburn  7817:                     'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n";
1.221     raeburn  7818:         }
                   7819:     }
1.222     raeburn  7820:     $row .= <<ENDTIMEENTRY;
                   7821: <td><input type="hidden" name="start_$protectedcourse\_$role" value="" />
1.221     raeburn  7822: <a href=
                   7823: "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  7824: <td><input type="hidden" name="end_$protectedcourse\_$role" value="" />
1.221     raeburn  7825: <a href=
                   7826: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
                   7827: ENDTIMEENTRY
1.222     raeburn  7828:     $row .= &Apache::loncommon::end_data_table_row();
                   7829:     return $row;
1.221     raeburn  7830: }
                   7831: 
1.88      raeburn  7832: sub course_level_dc {
1.375     raeburn  7833:     my ($dcdom,$showcredits) = @_;
1.190     raeburn  7834:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.213     raeburn  7835:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.88      raeburn  7836:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
                   7837:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
1.133     raeburn  7838:                       '<input type="hidden" name="dccourse" value="" />';
1.355     www      7839:     my $courseform=&Apache::loncommon::selectcourse_link
1.356     raeburn  7840:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Select','crstype');
1.375     raeburn  7841:     my $credit_elem;
                   7842:     if ($showcredits) {
                   7843:         $credit_elem = 'credits';
                   7844:     }
                   7845:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu','role','Course/Community Browser',$credit_elem);
1.88      raeburn  7846:     my %lt=&Apache::lonlocal::texthash(
                   7847:                     'rol'  => "Role",
1.113     raeburn  7848:                     'grs'  => "Section",
1.88      raeburn  7849:                     'exs'  => "Existing sections",
                   7850:                     'new'  => "Define new section", 
                   7851:                     'sta'  => "Start",
                   7852:                     'end'  => "End",
                   7853:                     'ssd'  => "Set Start Date",
1.355     www      7854:                     'sed'  => "Set End Date",
1.375     raeburn  7855:                     'scc'  => "Course/Community",
                   7856:                     'crd'  => "Credits",
1.88      raeburn  7857:                   );
1.323     raeburn  7858:     my $header = '<h4>'.&mt('Course/Community Level').'</h4>'.
1.136     raeburn  7859:                  &Apache::loncommon::start_data_table().
                   7860:                  &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  7861:                  '<th>'.$lt{'scc'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
1.397     bisitz   7862:                  '<th>'.$lt{'grs'}.'</th>'."\n";
                   7863:     $header .=   '<th>'.$lt{'crd'}.'</th>'."\n" if ($showcredits);
                   7864:     $header .=   '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'."\n".
1.136     raeburn  7865:                  &Apache::loncommon::end_data_table_header_row();
1.143     raeburn  7866:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
1.356     raeburn  7867:                      '<td><br /><span class="LC_nobreak"><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc','','','','crstype'".')" />'.
                   7868:                      $courseform.('&nbsp;' x4).'</span></td>'."\n".
1.389     bisitz   7869:                      '<td valign="top"><br /><select name="role">'."\n";
1.213     raeburn  7870:     foreach my $role (@roles) {
1.135     raeburn  7871:         my $plrole=&Apache::lonnet::plaintext($role);
1.389     bisitz   7872:         $otheritems .= '  <option value="'.$role.'">'.$plrole.'</option>';
1.88      raeburn  7873:     }
1.404     raeburn  7874:     if ( keys(%customroles) > 0) {
                   7875:         foreach my $cust (sort(keys(%customroles))) {
1.101     albertel 7876:             my $custrole='cr_cr_'.$env{'user.domain'}.
1.135     raeburn  7877:                     '_'.$env{'user.name'}.'_'.$cust;
1.389     bisitz   7878:             $otheritems .= '  <option value="'.$custrole.'">'.$cust.'</option>';
1.88      raeburn  7879:         }
                   7880:     }
                   7881:     $otheritems .= '</select></td><td>'.
                   7882:                      '<table border="0" cellspacing="0" cellpadding="0">'.
                   7883:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
1.389     bisitz   7884:                      ' <option value="">&lt;--'.&mt('Pick course first').'</option></select></td>'.
1.88      raeburn  7885:                      '<td>&nbsp;&nbsp;</td>'.
                   7886:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
1.113     raeburn  7887:                      '<input type="text" name="newsec" value="" />'.
1.237     raeburn  7888:                      '<input type="hidden" name="section" value="" />'.
1.323     raeburn  7889:                      '<input type="hidden" name="groups" value="" />'.
                   7890:                      '<input type="hidden" name="crstype" value="" /></td>'.
1.375     raeburn  7891:                      '</tr></table></td>'."\n";
                   7892:     if ($showcredits) {
                   7893:         $otheritems .= '<td><br />'."\n".
1.397     bisitz   7894:                        '<input type="text" size="3" name="credits" value="" /></td>'."\n";
1.375     raeburn  7895:     }
1.88      raeburn  7896:     $otheritems .= <<ENDTIMEENTRY;
1.323     raeburn  7897: <td><br /><input type="hidden" name="start" value='' />
1.88      raeburn  7898: <a href=
                   7899: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.323     raeburn  7900: <td><br /><input type="hidden" name="end" value='' />
1.88      raeburn  7901: <a href=
                   7902: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
                   7903: ENDTIMEENTRY
1.136     raeburn  7904:     $otheritems .= &Apache::loncommon::end_data_table_row().
                   7905:                    &Apache::loncommon::end_data_table()."\n";
1.88      raeburn  7906:     return $cb_jscript.$header.$hiddenitems.$otheritems;
                   7907: }
                   7908: 
1.237     raeburn  7909: sub update_selfenroll_config {
1.400     raeburn  7910:     my ($r,$cid,$cdom,$cnum,$context,$crstype,$currsettings) = @_;
1.398     raeburn  7911:     return unless (ref($currsettings) eq 'HASH');
                   7912:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
                   7913:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
1.237     raeburn  7914:     my (%changes,%warning);
1.241     raeburn  7915:     my $curr_types;
1.400     raeburn  7916:     my %noedit;
                   7917:     unless ($context eq 'domain') {
                   7918:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
                   7919:     }
1.237     raeburn  7920:     if (ref($row) eq 'ARRAY') {
                   7921:         foreach my $item (@{$row}) {
1.400     raeburn  7922:             next if ($noedit{$item});
1.237     raeburn  7923:             if ($item eq 'enroll_dates') {
                   7924:                 my (%currenrolldate,%newenrolldate);
                   7925:                 foreach my $type ('start','end') {
1.398     raeburn  7926:                     $currenrolldate{$type} = $currsettings->{'selfenroll_'.$type.'_date'};
1.237     raeburn  7927:                     $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
                   7928:                     if ($newenrolldate{$type} ne $currenrolldate{$type}) {
                   7929:                         $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
                   7930:                     }
                   7931:                 }
                   7932:             } elsif ($item eq 'access_dates') {
                   7933:                 my (%currdate,%newdate);
                   7934:                 foreach my $type ('start','end') {
1.398     raeburn  7935:                     $currdate{$type} = $currsettings->{'selfenroll_'.$type.'_access'};
1.237     raeburn  7936:                     $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
                   7937:                     if ($newdate{$type} ne $currdate{$type}) {
                   7938:                         $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
                   7939:                     }
                   7940:                 }
1.241     raeburn  7941:             } elsif ($item eq 'types') {
1.398     raeburn  7942:                 $curr_types = $currsettings->{'selfenroll_'.$item};
1.241     raeburn  7943:                 if ($env{'form.selfenroll_all'}) {
                   7944:                     if ($curr_types ne '*') {
                   7945:                         $changes{'internal.selfenroll_types'} = '*';
                   7946:                     } else {
                   7947:                         next;
                   7948:                     }
                   7949:                 } else {
1.249     raeburn  7950:                     my %currdoms;
1.241     raeburn  7951:                     my @entries = split(/;/,$curr_types);
                   7952:                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
1.249     raeburn  7953:                     my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
1.241     raeburn  7954:                     my $newnum = 0;
1.249     raeburn  7955:                     my @latesttypes;
                   7956:                     foreach my $num (@activations) {
                   7957:                         my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
                   7958:                         if (@types > 0) {
1.241     raeburn  7959:                             @types = sort(@types);
                   7960:                             my $typestr = join(',',@types);
1.249     raeburn  7961:                             my $typedom = $env{'form.selfenroll_dom_'.$num};
                   7962:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   7963:                             $currdoms{$typedom} = 1;
1.241     raeburn  7964:                             $newnum ++;
                   7965:                         }
                   7966:                     }
1.338     raeburn  7967:                     for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {
                   7968:                         if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
1.249     raeburn  7969:                             my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
                   7970:                             if (@types > 0) {
                   7971:                                 @types = sort(@types);
                   7972:                                 my $typestr = join(',',@types);
                   7973:                                 my $typedom = $env{'form.selfenroll_dom_'.$j};
                   7974:                                 $latesttypes[$newnum] = $typedom.':'.$typestr;
                   7975:                                 $currdoms{$typedom} = 1;
                   7976:                                 $newnum ++;
                   7977:                             }
                   7978:                         }
                   7979:                     }
                   7980:                     if ($env{'form.selfenroll_newdom'} ne '') {
                   7981:                         my $typedom = $env{'form.selfenroll_newdom'};
                   7982:                         if ((!defined($currdoms{$typedom})) && 
                   7983:                             (&Apache::lonnet::domain($typedom) ne '')) {
                   7984:                             my $typestr;
                   7985:                             my ($othertitle,$usertypes,$types) = 
                   7986:                                 &Apache::loncommon::sorted_inst_types($typedom);
                   7987:                             my $othervalue = 'any';
                   7988:                             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   7989:                                 if (@{$types} > 0) {
1.257     raeburn  7990:                                     my @esc_types = map { &escape($_); } @{$types};
1.249     raeburn  7991:                                     $othervalue = 'other';
1.258     raeburn  7992:                                     $typestr = join(',',(@esc_types,$othervalue));
1.249     raeburn  7993:                                 }
                   7994:                                 $typestr = $othervalue;
                   7995:                             } else {
                   7996:                                 $typestr = $othervalue;
                   7997:                             } 
                   7998:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   7999:                             $newnum ++ ;
                   8000:                         }
                   8001:                     }
1.241     raeburn  8002:                     my $selfenroll_types = join(';',@latesttypes);
                   8003:                     if ($selfenroll_types ne $curr_types) {
                   8004:                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
                   8005:                     }
                   8006:                 }
1.276     raeburn  8007:             } elsif ($item eq 'limit') {
                   8008:                 my $newlimit = $env{'form.selfenroll_limit'};
                   8009:                 my $newcap = $env{'form.selfenroll_cap'};
                   8010:                 $newcap =~s/\s+//g;
1.398     raeburn  8011:                 my $currlimit =  $currsettings->{'selfenroll_limit'};
1.276     raeburn  8012:                 $currlimit = 'none' if ($currlimit eq '');
1.398     raeburn  8013:                 my $currcap = $currsettings->{'selfenroll_cap'};
1.276     raeburn  8014:                 if ($newlimit ne $currlimit) {
                   8015:                     if ($newlimit ne 'none') {
                   8016:                         if ($newcap =~ /^\d+$/) {
                   8017:                             if ($newcap ne $currcap) {
                   8018:                                 $changes{'internal.selfenroll_cap'} = $newcap;
                   8019:                             }
                   8020:                             $changes{'internal.selfenroll_limit'} = $newlimit;
                   8021:                         } else {
1.398     raeburn  8022:                             $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
                   8023:                                 &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.'); 
1.276     raeburn  8024:                         }
                   8025:                     } elsif ($currcap ne '') {
                   8026:                         $changes{'internal.selfenroll_cap'} = '';
                   8027:                         $changes{'internal.selfenroll_limit'} = $newlimit; 
                   8028:                     }
                   8029:                 } elsif ($currlimit ne 'none') {
                   8030:                     if ($newcap =~ /^\d+$/) {
                   8031:                         if ($newcap ne $currcap) {
                   8032:                             $changes{'internal.selfenroll_cap'} = $newcap;
                   8033:                         }
                   8034:                     } else {
1.398     raeburn  8035:                         $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
                   8036:                             &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.');
1.276     raeburn  8037:                     }
                   8038:                 }
                   8039:             } elsif ($item eq 'approval') {
                   8040:                 my (@currnotified,@newnotified);
1.398     raeburn  8041:                 my $currapproval = $currsettings->{'selfenroll_approval'};
                   8042:                 my $currnotifylist = $currsettings->{'selfenroll_notifylist'};
1.276     raeburn  8043:                 if ($currnotifylist ne '') {
                   8044:                     @currnotified = split(/,/,$currnotifylist);
                   8045:                     @currnotified = sort(@currnotified);
                   8046:                 }
                   8047:                 my $newapproval = $env{'form.selfenroll_approval'};
                   8048:                 @newnotified = &Apache::loncommon::get_env_multiple('form.selfenroll_notify');
                   8049:                 @newnotified = sort(@newnotified);
                   8050:                 if ($newapproval ne $currapproval) {
                   8051:                     $changes{'internal.selfenroll_approval'} = $newapproval;
                   8052:                     if (!$newapproval) {
                   8053:                         if ($currnotifylist ne '') {
                   8054:                             $changes{'internal.selfenroll_notifylist'} = '';
                   8055:                         }
                   8056:                     } else {
                   8057:                         my @differences =  
1.295     raeburn  8058:                             &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  8059:                         if (@differences > 0) {
                   8060:                             if (@newnotified > 0) {
                   8061:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   8062:                             } else {
                   8063:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   8064:                             }
                   8065:                         }
                   8066:                     }
                   8067:                 } else {
1.295     raeburn  8068:                     my @differences = &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  8069:                     if (@differences > 0) {
                   8070:                         if (@newnotified > 0) {
                   8071:                             $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   8072:                         } else {
                   8073:                             $changes{'internal.selfenroll_notifylist'} = '';
                   8074:                         }
                   8075:                     }
                   8076:                 }
1.237     raeburn  8077:             } else {
1.398     raeburn  8078:                 my $curr_val = $currsettings->{'selfenroll_'.$item};
1.237     raeburn  8079:                 my $newval = $env{'form.selfenroll_'.$item};
                   8080:                 if ($item eq 'section') {
                   8081:                     $newval = $env{'form.sections'};
1.241     raeburn  8082:                     if (defined($curr_groups{$newval})) {
1.237     raeburn  8083:                         $newval = $curr_val;
1.398     raeburn  8084:                         $warning{$item} = &mt('Section for self-enrolled users unchanged as the proposed section is a group').'<br />'.
                   8085:                                           &mt('Group names and section names must be distinct');
1.237     raeburn  8086:                     } elsif ($newval eq 'all') {
                   8087:                         $newval = $curr_val;
1.274     bisitz   8088:                         $warning{$item} = &mt('Section for self-enrolled users unchanged, as "all" is a reserved section name.');
1.237     raeburn  8089:                     }
                   8090:                     if ($newval eq '') {
                   8091:                         $newval = 'none';
                   8092:                     }
                   8093:                 }
                   8094:                 if ($newval ne $curr_val) {
                   8095:                     $changes{'internal.selfenroll_'.$item} = $newval;
                   8096:                 }
1.241     raeburn  8097:             }
1.237     raeburn  8098:         }
                   8099:         if (keys(%warning) > 0) {
                   8100:             foreach my $item (@{$row}) {
                   8101:                 if (exists($warning{$item})) {
                   8102:                     $r->print($warning{$item}.'<br />');
                   8103:                 }
                   8104:             } 
                   8105:         }
                   8106:         if (keys(%changes) > 0) {
                   8107:             my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
                   8108:             if ($putresult eq 'ok') {
                   8109:                 if ((exists($changes{'internal.selfenroll_types'})) ||
                   8110:                     (exists($changes{'internal.selfenroll_start_date'}))  ||
                   8111:                     (exists($changes{'internal.selfenroll_end_date'}))) {
                   8112:                     my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
                   8113:                                                                 $cnum,undef,undef,'Course');
                   8114:                     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
1.398     raeburn  8115:                     if (ref($crsinfo{$cid}) eq 'HASH') {
1.237     raeburn  8116:                         foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
                   8117:                             if (exists($changes{'internal.'.$item})) {
1.398     raeburn  8118:                                 $crsinfo{$cid}{$item} = $changes{'internal.'.$item};
1.237     raeburn  8119:                             }
                   8120:                         }
                   8121:                         my $crsputresult =
                   8122:                             &Apache::lonnet::courseidput($cdom,\%crsinfo,
                   8123:                                                          $chome,'notime');
                   8124:                     }
                   8125:                 }
                   8126:                 $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
                   8127:                 foreach my $item (@{$row}) {
                   8128:                     my $title = $item;
                   8129:                     if (ref($lt) eq 'HASH') {
                   8130:                         $title = $lt->{$item};
                   8131:                     }
                   8132:                     if ($item eq 'enroll_dates') {
                   8133:                         foreach my $type ('start','end') {
                   8134:                             if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
                   8135:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
1.244     bisitz   8136:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  8137:                                           $title,$type,$newdate).'</li>');
                   8138:                             }
                   8139:                         }
                   8140:                     } elsif ($item eq 'access_dates') {
                   8141:                         foreach my $type ('start','end') {
                   8142:                             if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
                   8143:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
1.244     bisitz   8144:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  8145:                                           $title,$type,$newdate).'</li>');
                   8146:                             }
                   8147:                         }
1.276     raeburn  8148:                     } elsif ($item eq 'limit') {
                   8149:                         if ((exists($changes{'internal.selfenroll_limit'})) ||
                   8150:                             (exists($changes{'internal.selfenroll_cap'}))) {
                   8151:                             my ($newval,$newcap);
                   8152:                             if ($changes{'internal.selfenroll_cap'} ne '') {
                   8153:                                 $newcap = $changes{'internal.selfenroll_cap'}
                   8154:                             } else {
1.398     raeburn  8155:                                 $newcap = $currsettings->{'selfenroll_cap'};
1.276     raeburn  8156:                             }
                   8157:                             if ($changes{'internal.selfenroll_limit'} eq 'none') {
                   8158:                                 $newval = &mt('No limit');
                   8159:                             } elsif ($changes{'internal.selfenroll_limit'} eq 
                   8160:                                      'allstudents') {
                   8161:                                 $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   8162:                             } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
                   8163:                                 $newval = &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
                   8164:                             } else {
1.398     raeburn  8165:                                 my $currlimit =  $currsettings->{'selfenroll_limit'};
1.276     raeburn  8166:                                 if ($currlimit eq 'allstudents') {
                   8167:                                     $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   8168:                                 } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
1.308     raeburn  8169:                                     $newval =  &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
1.276     raeburn  8170:                                 }
                   8171:                             }
                   8172:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
                   8173:                         }
                   8174:                     } elsif ($item eq 'approval') {
                   8175:                         if ((exists($changes{'internal.selfenroll_approval'})) ||
                   8176:                             (exists($changes{'internal.selfenroll_notifylist'}))) {
1.398     raeburn  8177:                             my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
1.276     raeburn  8178:                             my ($newval,$newnotify);
                   8179:                             if (exists($changes{'internal.selfenroll_notifylist'})) {
                   8180:                                 $newnotify = $changes{'internal.selfenroll_notifylist'};
                   8181:                             } else {   
1.398     raeburn  8182:                                 $newnotify = $currsettings->{'selfenroll_notifylist'};
1.276     raeburn  8183:                             }
1.398     raeburn  8184:                             if (exists($changes{'internal.selfenroll_approval'})) {
                   8185:                                 if ($changes{'internal.selfenroll_approval'} !~ /^[012]$/) {
                   8186:                                     $changes{'internal.selfenroll_approval'} = '0';
                   8187:                                 }
                   8188:                                 $newval = $selfdescs{'approval'}{$changes{'internal.selfenroll_approval'}};
1.276     raeburn  8189:                             } else {
1.398     raeburn  8190:                                 my $currapproval = $currsettings->{'selfenroll_approval'}; 
                   8191:                                 if ($currapproval !~ /^[012]$/) {
                   8192:                                     $currapproval = 0;
1.276     raeburn  8193:                                 }
1.398     raeburn  8194:                                 $newval = $selfdescs{'approval'}{$currapproval};
1.276     raeburn  8195:                             }
                   8196:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval));
                   8197:                             if ($newnotify) {
1.277     raeburn  8198:                                 $r->print('<br />'.&mt('The following will be notified when an enrollment request needs approval, or has been approved: [_1].',$newnotify));
1.276     raeburn  8199:                             } else {
1.277     raeburn  8200:                                 $r->print('<br />'.&mt('No notifications sent when an enrollment request needs approval, or has been approved.'));
1.276     raeburn  8201:                             }
                   8202:                             $r->print('</li>'."\n");
                   8203:                         }
1.237     raeburn  8204:                     } else {
                   8205:                         if (exists($changes{'internal.selfenroll_'.$item})) {
1.241     raeburn  8206:                             my $newval = $changes{'internal.selfenroll_'.$item};
                   8207:                             if ($item eq 'types') {
                   8208:                                 if ($newval eq '') {
                   8209:                                     $newval = &mt('None');
                   8210:                                 } elsif ($newval eq '*') {
                   8211:                                     $newval = &mt('Any user in any domain');
                   8212:                                 }
1.245     raeburn  8213:                             } elsif ($item eq 'registered') {
                   8214:                                 if ($newval eq '1') {
                   8215:                                     $newval = &mt('Yes');
                   8216:                                 } elsif ($newval eq '0') {
                   8217:                                     $newval = &mt('No');
                   8218:                                 }
1.241     raeburn  8219:                             }
1.244     bisitz   8220:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
1.237     raeburn  8221:                         }
                   8222:                     }
                   8223:                 }
                   8224:                 $r->print('</ul>');
1.398     raeburn  8225:                 if ($env{'course.'.$cid.'.description'} ne '') {
                   8226:                     my %newenvhash;
                   8227:                     foreach my $key (keys(%changes)) {
                   8228:                         $newenvhash{'course.'.$cid.'.'.$key} = $changes{$key};
                   8229:                     }
                   8230:                     &Apache::lonnet::appenv(\%newenvhash);
1.237     raeburn  8231:                 }
                   8232:             } else {
1.398     raeburn  8233:                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.
                   8234:                           &mt('The error was: [_1].',$putresult));
1.237     raeburn  8235:             }
                   8236:         } else {
1.249     raeburn  8237:             $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.237     raeburn  8238:         }
                   8239:     } else {
1.249     raeburn  8240:         $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.241     raeburn  8241:     }
1.400     raeburn  8242:     my $visactions = &cat_visibility();
                   8243:     my ($cathash,%cattype);
                   8244:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   8245:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   8246:         $cathash = $domconfig{'coursecategories'}{'cats'};
                   8247:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
                   8248:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
                   8249:     } else {
                   8250:         $cathash = {};
                   8251:         $cattype{'auth'} = 'std';
                   8252:         $cattype{'unauth'} = 'std';
                   8253:     }
                   8254:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
                   8255:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   8256:                   '<br />'.
                   8257:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   8258:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
                   8259:                   '</ul>');
                   8260:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
                   8261:         if ($currsettings->{'uniquecode'}) {
                   8262:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
                   8263:         } else {
1.366     bisitz   8264:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
1.400     raeburn  8265:                   '<br />'.
                   8266:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   8267:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
                   8268:                   '</ul><br />');
                   8269:         }
                   8270:     } else {
                   8271:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
                   8272:         if (ref($visactions) eq 'HASH') {
                   8273:             if (!$visible) {
                   8274:                 $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   8275:                           '<br />');
                   8276:                 if (ref($vismsgs) eq 'ARRAY') {
                   8277:                     $r->print('<br />'.$visactions->{'take'}.'<ul>');
                   8278:                     foreach my $item (@{$vismsgs}) {
                   8279:                         $r->print('<li>'.$visactions->{$item}.'</li>');
                   8280:                     }
                   8281:                     $r->print('</ul>');
1.256     raeburn  8282:                 }
1.400     raeburn  8283:                 $r->print($cansetvis);
1.256     raeburn  8284:             }
                   8285:         }
                   8286:     } 
1.237     raeburn  8287:     return;
                   8288: }
                   8289: 
1.27      matthew  8290: #---------------------------------------------- end functions for &phase_two
1.29      matthew  8291: 
                   8292: #--------------------------------- functions for &phase_two and &phase_three
                   8293: 
                   8294: #--------------------------end of functions for &phase_two and &phase_three
1.372     raeburn  8295: 
1.1       www      8296: 1;
                   8297: __END__
1.2       www      8298: 
                   8299: 

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