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

1.20      harris41    1: # The LearningOnline Network with CAPA
1.1       www         2: # Create a user
                      3: #
1.471   ! raeburn     4: # $Id: loncreateuser.pm,v 1.470 2023/11/03 01:12:15 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.470     raeburn    73: use Apache::lonviewcoauthors;
1.139     albertel   74: use LONCAPA qw(:DEFAULT :match);
1.456     raeburn    75: use HTML::Entities;
1.1       www        76: 
1.20      harris41   77: my $loginscript; # piece of javascript used in two separate instances
                     78: my $authformnop;
                     79: my $authformkrb;
                     80: my $authformint;
                     81: my $authformfsys;
                     82: my $authformloc;
1.449     raeburn    83: my $authformlti;
1.20      harris41   84: 
1.94      matthew    85: sub initialize_authen_forms {
1.470     raeburn    86:     my ($dom,$formname,$curr_authtype,$mode,$readonly) = @_;
1.227     raeburn    87:     my ($krbdef,$krbdefdom) = &Apache::loncommon::get_kerberos_defaults($dom);
                     88:     my %param = ( formname => $formname,
1.187     raeburn    89:                   kerb_def_dom => $krbdefdom,
1.227     raeburn    90:                   kerb_def_auth => $krbdef,
1.187     raeburn    91:                   domain => $dom,
                     92:                 );
1.188     raeburn    93:     my %abv_auth = &auth_abbrev();
1.449     raeburn    94:     if ($curr_authtype =~ /^(krb4|krb5|internal|localauth|unix|lti):(.*)$/) {
1.188     raeburn    95:         my $long_auth = $1;
1.227     raeburn    96:         my $curr_autharg = $2;
1.188     raeburn    97:         my %abv_auth = &auth_abbrev();
                     98:         $param{'curr_authtype'} = $abv_auth{$long_auth};
                     99:         if ($long_auth =~ /^krb(4|5)$/) {
                    100:             $param{'curr_kerb_ver'} = $1;
1.227     raeburn   101:             $param{'curr_autharg'} = $curr_autharg;
1.188     raeburn   102:         }
1.205     raeburn   103:         if ($mode eq 'modifyuser') {
                    104:             $param{'mode'} = $mode;
                    105:         }
1.187     raeburn   106:     }
1.470     raeburn   107:     if ($readonly) {
                    108:         $param{'readonly'} = 1;
                    109:     }
1.227     raeburn   110:     $loginscript  = &Apache::loncommon::authform_header(%param);
                    111:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
1.31      matthew   112:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
                    113:     $authformint  = &Apache::loncommon::authform_internal(%param);
                    114:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
                    115:     $authformloc  = &Apache::loncommon::authform_local(%param);
1.449     raeburn   116:     $authformlti  = &Apache::loncommon::authform_lti(%param);
1.20      harris41  117: }
                    118: 
1.188     raeburn   119: sub auth_abbrev {
                    120:     my %abv_auth = (
1.368     raeburn   121:                      krb5      => 'krb',
                    122:                      krb4      => 'krb',
                    123:                      internal  => 'int',
                    124:                      localauth => 'loc',
                    125:                      unix      => 'fsys',
1.449     raeburn   126:                      lti       => 'lti',
1.188     raeburn   127:                    );
                    128:     return %abv_auth;
                    129: }
1.43      www       130: 
1.134     raeburn   131: # ====================================================
                    132: 
1.378     raeburn   133: sub user_quotas {
1.470     raeburn   134:     my ($ccuname,$ccdomain,$name) = @_;
1.134     raeburn   135:     my %lt = &Apache::lonlocal::texthash(
1.267     raeburn   136:                    'cust'      => "Custom quota",
                    137:                    'chqu'      => "Change quota",
1.134     raeburn   138:     );
1.470     raeburn   139:     my ($output,$longinsttype);
                    140:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
                    141:     my %titles = &Apache::lonlocal::texthash (
                    142:                     portfolio => "Disk space allocated to user's portfolio files",
                    143:                     author    => "Disk space allocated to user's Authoring Space",
                    144:                  );
                    145:     my ($currquota,$quotatype,$inststatus,$defquota) =
                    146:         &Apache::loncommon::get_user_quota($ccuname,$ccdomain,$name);
                    147:     if ($longinsttype eq '') {
                    148:         if ($inststatus ne '') {
                    149:             if ($usertypes->{$inststatus} ne '') {
                    150:                 $longinsttype = $usertypes->{$inststatus};
                    151:             }
                    152:         }
                    153:     }
                    154:     my ($showquota,$custom_on,$custom_off,$defaultinfo,$colspan);
                    155:     $custom_on = ' ';
                    156:     $custom_off = ' checked="checked" ';
                    157:     $colspan = ' colspan="2"';
                    158:     if ($quotatype eq 'custom') {
                    159:         $custom_on = $custom_off;
                    160:         $custom_off = ' ';
                    161:         $showquota = $currquota;
                    162:         if ($longinsttype eq '') {
                    163:             $defaultinfo = &mt('For this user, the default quota would be [_1]'
                    164:                               .' MB.',$defquota);
                    165:         } else {
                    166:             $defaultinfo = &mt("For this user, the default quota would be [_1]".
                    167:                                " MB,[_2]as determined by the user's institutional".
                    168:                                " affiliation ([_3]).",$defquota,'<br />',$longinsttype);
                    169:         }
                    170:     } else {
                    171:         if ($longinsttype eq '') {
                    172:             $defaultinfo = &mt('For this user, the default quota is [_1]'
                    173:                               .' MB.',$defquota);
                    174:         } else {
                    175:             $defaultinfo = &mt("For this user, the default quota of [_1]".
                    176:                                " MB,[_2]is determined by the user's institutional".
                    177:                                " affiliation ([_3]).",$defquota,'<br />'.$longinsttype);
                    178:         }
                    179:     }
                    180: 
                    181:     if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
                    182:         $output .= '<tr class="LC_info_row">'."\n".
                    183:                    '    <td'.$colspan.'>'.$titles{$name}.'</td>'."\n".
                    184:                    '  </tr>'."\n".
                    185:                    &Apache::loncommon::start_data_table_row()."\n".
                    186:                    '  <td'.$colspan.'><span class="LC_nobreak">'.
                    187:                    &mt('Current quota: [_1] MB',$currquota).'</span>&nbsp;&nbsp;'.
                    188:                    $defaultinfo.'</td>'."\n".
                    189:                    &Apache::loncommon::end_data_table_row()."\n".
                    190:                    &Apache::loncommon::start_data_table_row()."\n".
                    191:                    '<td'.$colspan.'><span class="LC_nobreak">'.$lt{'chqu'}.
                    192:                    ': <label>'.
                    193:                    '<input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_off" '.
                    194:                    'value="0" '.$custom_off.' onchange="javascript:quota_changes('."'custom','$name'".');"'.
                    195:                    ' /><span class="LC_nobreak">'.
                    196:                    &mt('Default ([_1] MB)',$defquota).'</span></label>&nbsp;'.
                    197:                    '&nbsp;<label><input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_on" '.
                    198:                    'value="1" '.$custom_on.'  onchange="javascript:quota_changes('."'custom','$name'".');"'.
                    199:                    ' />'.$lt{'cust'}.':</label>&nbsp;'.
                    200:                    '<input type="text" name="'.$name.'quota" id="'.$name.'quota" size ="5" '.
                    201:                    'value="'.$showquota.'" onfocus="javascript:quota_changes('."'quota','$name'".');"'.
                    202:                    ' />&nbsp;'.&mt('MB').'</span></td>'."\n".
                    203:                    &Apache::loncommon::end_data_table_row()."\n";
                    204:     }
                    205:     return $output;
                    206: }
                    207: 
                    208: sub user_quota_js {
                    209:     return  <<"END_SCRIPT";
1.149     raeburn   210: <script type="text/javascript">
1.301     bisitz    211: // <![CDATA[
1.378     raeburn   212: function quota_changes(caller,context) {
                    213:     var customoff = document.getElementById('custom_'+context+'quota_off');
                    214:     var customon = document.getElementById('custom_'+context+'quota_on');
                    215:     var number = document.getElementById(context+'quota');
1.149     raeburn   216:     if (caller == "custom") {
1.378     raeburn   217:         if (customoff) {
                    218:             if (customoff.checked) {
                    219:                 number.value = "";
                    220:             }
1.149     raeburn   221:         }
                    222:     }
                    223:     if (caller == "quota") {
1.378     raeburn   224:         if (customon) {
                    225:             customon.checked = true;
                    226:         }
1.149     raeburn   227:     }
1.378     raeburn   228:     return;
1.149     raeburn   229: }
1.301     bisitz    230: // ]]>
1.149     raeburn   231: </script>
                    232: END_SCRIPT
1.378     raeburn   233: 
1.470     raeburn   234: }
                    235: 
                    236: sub set_custom_js {
                    237:     return  <<"END_SCRIPT";
                    238: 
                    239: <script type="text/javascript">
                    240: // <![CDATA[
                    241: function toggleCustom(form,item,name) {
                    242:     if (document.getElementById(item)) {
                    243:         var divid = document.getElementById(item);
                    244:         var radioname = form.elements[name];
                    245:         if (radioname) {
                    246:             if (radioname.length > 0) {
                    247:                 var setvis;
                    248:                 for (var i=0; i<radioname.length; i++) {
                    249:                     if (radioname[i].checked == true) {
                    250:                         if (radioname[i].value == 1) {
                    251:                             divid.style.display = 'block';
                    252:                             setvis = 1;
                    253:                         }
                    254:                         break;
                    255:                     }
                    256:                 }
                    257:                 if (!setvis) {
                    258:                     divid.style.display = 'none';
1.378     raeburn   259:                 }
                    260:             }
                    261:         }
1.470     raeburn   262:     }
                    263:     return;
                    264: }
                    265: // ]]>
                    266: </script>
                    267: 
                    268: END_SCRIPT
1.378     raeburn   269: 
1.134     raeburn   270: }
                    271: 
1.275     raeburn   272: sub build_tools_display {
                    273:     my ($ccuname,$ccdomain,$context) = @_;
1.306     raeburn   274:     my (@usertools,%userenv,$output,@options,%validations,%reqtitles,%reqdisplay,
1.470     raeburn   275:         $colspan,$isadv,%domconfig,@defaulteditors,@customeditors,@custommanagers,
                    276:         @possmanagers,$editorsty,$customsty);
1.275     raeburn   277:     my %lt = &Apache::lonlocal::texthash (
                    278:                    'blog'       => "Personal User Blog",
                    279:                    'aboutme'    => "Personal Information Page",
1.470     raeburn   280:                    'webdav'     => "WebDAV access to Authoring Spaces (https)",
                    281:                    'editors'    => "Available Editors",
                    282:                    'managers'   => "Co-authors who can add/revoke co-authors",
1.275     raeburn   283:                    'portfolio'  => "Personal User Portfolio",
1.470     raeburn   284:                    'portaccess' => "Portfolio Shareable",
1.459     raeburn   285:                    'timezone'   => "Can set Time Zone",
1.275     raeburn   286:                    'avai'       => "Available",
                    287:                    'cusa'       => "availability",
                    288:                    'chse'       => "Change setting",
                    289:                    'usde'       => "Use default",
                    290:                    'uscu'       => "Use custom",
                    291:                    'official'   => 'Can request creation of official courses',
1.299     raeburn   292:                    'unofficial' => 'Can request creation of unofficial courses',
                    293:                    'community'  => 'Can request creation of communities',
1.384     raeburn   294:                    'textbook'   => 'Can request creation of textbook courses',
1.411     raeburn   295:                    'placement'  => 'Can request creation of placement tests',
1.449     raeburn   296:                    'lti'        => 'Can request creation of LTI courses',
1.362     raeburn   297:                    'requestauthor'  => 'Can request author space',
1.470     raeburn   298:                    'edit'       => 'Standard editor (Edit)',
                    299:                    'xml'        => 'Text editor (EditXML)',
                    300:                    'daxe'       => 'Daxe editor (Daxe)',
1.275     raeburn   301:     );
1.462     raeburn   302:     $isadv = &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
1.279     raeburn   303:     if ($context eq 'requestcourses') {
1.275     raeburn   304:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.299     raeburn   305:                       'requestcourses.official','requestcourses.unofficial',
1.411     raeburn   306:                       'requestcourses.community','requestcourses.textbook',
1.449     raeburn   307:                       'requestcourses.placement','requestcourses.lti');
                    308:         @usertools = ('official','unofficial','community','textbook','placement','lti');
1.309     raeburn   309:         @options =('norequest','approval','autolimit','validate');
1.306     raeburn   310:         %validations = &Apache::lonnet::auto_courserequest_checks($ccdomain);
                    311:         %reqtitles = &courserequest_titles();
                    312:         %reqdisplay = &courserequest_display();
1.332     raeburn   313:         %domconfig =
                    314:             &Apache::lonnet::get_dom('configuration',['requestcourses'],$ccdomain);
1.362     raeburn   315:     } elsif ($context eq 'requestauthor') {
1.470     raeburn   316:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,'requestauthor');
1.362     raeburn   317:         @usertools = ('requestauthor');
                    318:         @options =('norequest','approval','automatic');
                    319:         %reqtitles = &requestauthor_titles();
                    320:         %reqdisplay = &requestauthor_display();
                    321:         %domconfig =
                    322:             &Apache::lonnet::get_dom('configuration',['requestauthor'],$ccdomain);
1.470     raeburn   323:     } elsif ($context eq 'authordefaults') {
                    324:         %domconfig =
                    325:             &Apache::lonnet::get_dom('configuration',['quotas','authordefaults'],$ccdomain);
                    326:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,'tools.webdav',
1.471   ! raeburn   327:                                                     'authoreditors','authormanagers',
        !           328:                                                     'domcoord.author');
1.470     raeburn   329:         @usertools = ('webdav','editors','managers');
                    330:         $colspan = ' colspan="2"';
1.275     raeburn   331:     } else {
                    332:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.361     raeburn   333:                           'tools.aboutme','tools.portfolio','tools.blog',
1.470     raeburn   334:                           'tools.timezone','tools.portaccess');
                    335:         @usertools = ('aboutme','blog','portfolio','portaccess','timezone');
                    336:         $colspan = ' colspan="2"';
1.275     raeburn   337:     }
                    338:     foreach my $item (@usertools) {
1.306     raeburn   339:         my ($custom_access,$curr_access,$cust_on,$cust_off,$tool_on,$tool_off,
1.470     raeburn   340:             $currdisp,$custdisp,$custradio,$onclick);
1.275     raeburn   341:         $cust_off = 'checked="checked" ';
                    342:         $tool_on = 'checked="checked" ';
1.463     raeburn   343:         $curr_access =
1.275     raeburn   344:             &Apache::lonnet::usertools_access($ccuname,$ccdomain,$item,undef,
1.462     raeburn   345:                                               $context,\%userenv,'',
                    346:                                               {'is_adv' => $isadv});
1.362     raeburn   347:         if ($context eq 'requestauthor') {
                    348:             if ($userenv{$context} ne '') {
                    349:                 $cust_on = ' checked="checked" ';
                    350:                 $cust_off = '';
1.470     raeburn   351:             }
                    352:         } elsif ($context eq 'authordefaults') {
                    353:             if ($item eq 'editors') {
                    354:                 if ($userenv{'author'.$item} ne '') {
                    355:                     $cust_on = ' checked="checked" ';
                    356:                     $cust_off = '';
                    357:                 }
                    358:             } elsif ($item eq 'webdav') {
                    359:                 if ($userenv{'tools.'.$item} ne '') {
                    360:                     $cust_on = ' checked="checked" ';
                    361:                     $cust_off = '';
                    362:                 }
                    363:             }
1.362     raeburn   364:         } elsif ($userenv{$context.'.'.$item} ne '') {
1.306     raeburn   365:             $cust_on = ' checked="checked" ';
                    366:             $cust_off = '';
                    367:         }
                    368:         if ($context eq 'requestcourses') {
                    369:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   370:                 $custom_access = &mt('Currently from default setting.');
1.470     raeburn   371:                 $customsty = ' style="display:none;"';
1.306     raeburn   372:             } else {
                    373:                 $custom_access = &mt('Currently from custom setting.');
1.470     raeburn   374:                 $customsty = ' style="display:block;"';
1.275     raeburn   375:             }
1.362     raeburn   376:         } elsif ($context eq 'requestauthor') {
                    377:             if ($userenv{$context} eq '') {
                    378:                 $custom_access = &mt('Currently from default setting.');
1.470     raeburn   379:                 $customsty = ' style="display:none;"';
                    380:             } else {
                    381:                 $custom_access = &mt('Currently from custom setting.');
                    382:                 $customsty = ' style="display:block;"';
                    383:             }
                    384:         } elsif ($item eq 'editors') {
                    385:             if ($userenv{'author'.$item} eq '') {
                    386:                 if (ref($domconfig{'authordefaults'}{'editors'}) eq 'ARRAY') {
                    387:                     @defaulteditors = @{$domconfig{'authordefaults'}{'editors'}};
                    388:                 } else {
                    389:                     @defaulteditors = ('edit','xml');
                    390:                 }
                    391:                 $custom_access = &mt('Can use: [_1]',
                    392:                                                join(', ', map { $lt{$_} } @defaulteditors));
                    393:                 $editorsty = ' style="display:none;"';
1.362     raeburn   394:             } else {
                    395:                 $custom_access = &mt('Currently from custom setting.');
1.470     raeburn   396:                 foreach my $editor (split(/,/,$userenv{'author'.$item})) {
                    397:                     if ($editor =~ /^(edit|daxe|xml)$/) {
                    398:                         push(@customeditors,$editor);
                    399:                     }
                    400:                 }
                    401:                 if (@customeditors) {
                    402:                     if (@customeditors > 1) {
                    403:                         $custom_access .= '<br /><span>';
                    404:                     } else {
                    405:                         $custom_access .= ' <span class="LC_nobreak">';
                    406:                     }
                    407:                     $custom_access .= &mt('Can use: [_1]',
                    408:                                           join(', ', map { $lt{$_} } @customeditors)).
                    409:                                       '</span>';
                    410:                 } else {
                    411:                     $custom_access .= ' '.&mt('No available editors');
                    412:                 }
                    413:                 $editorsty = ' style="display:block;"';
                    414:             }
                    415:         } elsif ($item eq 'managers') {
                    416:             my %ca_roles = &Apache::lonnet::get_my_roles($ccuname,$ccdomain,undef,
                    417:                                                          ['active','future'],['ca']);
                    418:             if (keys(%ca_roles)) {
                    419:                 foreach my $entry (sort(keys(%ca_roles))) {
                    420:                     if ($entry =~ /^($match_username\:$match_domain):ca$/) {
                    421:                         my $user = $1;
                    422:                         unless ($user eq "$ccuname:$ccdomain") {
                    423:                             push(@possmanagers,$user);
                    424:                         }
                    425:                     }
                    426:                 }
                    427:             }
                    428:             if ($userenv{'author'.$item} eq '') {
                    429:                 $custom_access = &mt('Currently author manages co-author roles');
                    430:             } else {
                    431:                 if (keys(%ca_roles)) {
                    432:                     foreach my $user (split(/,/,$userenv{'author'.$item})) {
                    433:                         if ($user =~ /^($match_username):($match_domain)$/) {
                    434:                             if (exists($ca_roles{$user.':ca'})) {
                    435:                                 unless ($user eq "$ccuname:$ccdomain") {
                    436:                                     push(@custommanagers,$user);
                    437:                                 }
                    438:                             }
                    439:                         }
                    440:                     }
                    441:                 }
                    442:                 if (@custommanagers) {
                    443:                     $custom_access = &mt('Co-authors who manage co-author roles: [_1]',
                    444:                                          join(', ',@custommanagers));
                    445:                 } else {
                    446:                     $custom_access = &mt('Currently author manages co-author roles');
                    447:                 }
1.362     raeburn   448:             }
1.275     raeburn   449:         } else {
1.470     raeburn   450:             my $current = $userenv{$context.'.'.$item};
                    451:             if ($item eq 'webdav') {
                    452:                 $current = $userenv{'tools.webdav'};
                    453:             }
                    454:             if ($current eq '') {
1.314     raeburn   455:                 $custom_access =
1.306     raeburn   456:                     &mt('Availability determined currently from default setting.');
                    457:                 if (!$curr_access) {
                    458:                     $tool_off = 'checked="checked" ';
                    459:                     $tool_on = '';
                    460:                 }
1.470     raeburn   461:                 $customsty = ' style="display:none;"';
1.306     raeburn   462:             } else {
1.314     raeburn   463:                 $custom_access =
1.306     raeburn   464:                     &mt('Availability determined currently from custom setting.');
1.470     raeburn   465:                 if ($current == 0) {
1.306     raeburn   466:                     $tool_off = 'checked="checked" ';
                    467:                     $tool_on = '';
                    468:                 }
1.470     raeburn   469:                 $customsty = ' style="display:inline;"';
1.275     raeburn   470:             }
                    471:         }
                    472:         $output .= '  <tr class="LC_info_row">'."\n".
1.306     raeburn   473:                    '   <td'.$colspan.'>'.$lt{$item}.'</td>'."\n".
1.275     raeburn   474:                    '  </tr>'."\n".
1.306     raeburn   475:                    &Apache::loncommon::start_data_table_row()."\n";
1.362     raeburn   476:         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.470     raeburn   477:             my ($curroption,$currlimit,$customsty);
1.362     raeburn   478:             my $envkey = $context.'.'.$item;
                    479:             if ($context eq 'requestauthor') {
                    480:                 $envkey = $context;
                    481:             }
                    482:             if ($userenv{$envkey} ne '') {
                    483:                 $curroption = $userenv{$envkey};
1.470     raeburn   484:                 $customsty = ' style="display:block"';
1.332     raeburn   485:             } else {
1.470     raeburn   486:                 $customsty = ' style="display:none"';
1.332     raeburn   487:                 my (@inststatuses);
1.362     raeburn   488:                 if ($context eq 'requestcourses') {
                    489:                     $curroption =
                    490:                         &Apache::loncoursequeueadmin::get_processtype('course',$ccuname,$ccdomain,
                    491:                                                                       $isadv,$ccdomain,$item,
                    492:                                                                       \@inststatuses,\%domconfig);
                    493:                 } else {
                    494:                      $curroption = 
                    495:                          &Apache::loncoursequeueadmin::get_processtype('requestauthor',$ccuname,$ccdomain,
                    496:                                                                        $isadv,$ccdomain,undef,
                    497:                                                                        \@inststatuses,\%domconfig);
                    498:                 }
1.332     raeburn   499:             }
1.306     raeburn   500:             if (!$curroption) {
                    501:                 $curroption = 'norequest';
                    502:             }
1.470     raeburn   503:             my $name = 'crsreq_'.$item;
                    504:             if ($context eq 'requestauthor') {
                    505:                 $name = $item;
                    506:             }
                    507:             $onclick = ' onclick="javascript:toggleCustom(this.form,'."'customtext_$item','custom$item'".');"';
1.306     raeburn   508:             if ($curroption =~ /^autolimit=(\d*)$/) {
                    509:                 $currlimit = $1;
1.314     raeburn   510:                 if ($currlimit eq '') {
                    511:                     $currdisp = &mt('Yes, automatic creation');
                    512:                 } else {
                    513:                     $currdisp = &mt('Yes, up to [quant,_1,request]/user',$currlimit);
                    514:                 }
1.306     raeburn   515:             } else {
                    516:                 $currdisp = $reqdisplay{$curroption};
                    517:             }
1.470     raeburn   518:             $custdisp = '<fieldset id="customtext_'.$item.'"'.$customsty.'>';
1.306     raeburn   519:             foreach my $option (@options) {
                    520:                 my $val = $option;
                    521:                 if ($option eq 'norequest') {
                    522:                     $val = 0;
                    523:                 }
                    524:                 if ($option eq 'validate') {
                    525:                     my $canvalidate = 0;
                    526:                     if (ref($validations{$item}) eq 'HASH') {
                    527:                         if ($validations{$item}{'_custom_'}) {
                    528:                             $canvalidate = 1;
                    529:                         }
                    530:                     }
                    531:                     next if (!$canvalidate);
                    532:                 }
                    533:                 my $checked = '';
                    534:                 if ($option eq $curroption) {
                    535:                     $checked = ' checked="checked"';
                    536:                 } elsif ($option eq 'autolimit') {
                    537:                     if ($curroption =~ /^autolimit/) {
                    538:                         $checked = ' checked="checked"';
                    539:                     }
                    540:                 }
1.470     raeburn   541:                 if ($option eq 'autolimit') {
                    542:                     $custdisp .= '<br />';
1.362     raeburn   543:                 }
1.470     raeburn   544:                 $custdisp .= '<span class="LC_nobreak"><label>'.
1.362     raeburn   545:                              '<input type="radio" name="'.$name.'" '.
                    546:                              'value="'.$val.'"'.$checked.' />'.
1.306     raeburn   547:                              $reqtitles{$option}.'</label>&nbsp;';
                    548:                 if ($option eq 'autolimit') {
1.362     raeburn   549:                     $custdisp .= '<input type="text" name="'.$name.
                    550:                                  '_limit" size="1" '.
1.470     raeburn   551:                                  'value="'.$currlimit.'" />&nbsp;'.
                    552:                                  $reqtitles{'unlimited'}.'</span>';
1.362     raeburn   553:                 } else {
                    554:                     $custdisp .= '</span>';
                    555:                 }
1.470     raeburn   556:                 $custdisp .= ' ';
                    557:             }
                    558:             $custdisp .= '</fieldset>';
                    559:             $custradio = '<br />'.$custdisp;
                    560:         } elsif ($item eq 'editors') {
                    561:             $output .= '<td'.$colspan.'>'.$custom_access.'</td>'."\n".
                    562:                        &Apache::loncommon::end_data_table_row()."\n";
                    563:             unless (&Apache::lonnet::allowed('udp',$ccdomain)) {
                    564:                 $output .= &Apache::loncommon::start_data_table_row()."\n".
                    565:                           '<td'.$colspan.'><span class="LC_nobreak">'.
                    566:                           $lt{'chse'}.': <label>'.
                    567:                           '<input type="radio" name="custom'.$item.'" value="0" '.
                    568:                           $cust_off.' onclick="toggleCustom(this.form,'."'customtext_$item','custom$item'".');" />'.
                    569:                           $lt{'usde'}.'</label>'.('&nbsp;' x3).
                    570:                           '<label><input type="radio" name="custom'.$item.'" value="1" '.
                    571:                           $cust_on.' onclick="toggleCustom(this.form,'."'customtext_$item','custom$item'".');" />'.
                    572:                           $lt{'uscu'}.'</label></span><br />'.
                    573:                           '<fieldset id="customtext_'.$item.'"'.$editorsty.'>';
                    574:                 foreach my $editor ('edit','xml','daxe') {
                    575:                     my $checked;
                    576:                     if ($userenv{'author'.$item} eq '') {
                    577:                         if (grep(/^\Q$editor\E$/,@defaulteditors)) {
                    578:                             $checked = ' checked="checked"';
                    579:                         }
                    580:                     } elsif (grep(/^\Q$editor\E$/,@customeditors)) {
                    581:                         $checked = ' checked="checked"';
                    582:                     }
                    583:                     $output .= '<span style="LC_nobreak"><label>'.
                    584:                                '<input type="checkbox" name="custom_editor" '.
                    585:                                'value="'.$editor.'"'.$checked.' />'.
                    586:                                $lt{$editor}.'</label></span> ';
                    587:                 }
                    588:                 $output .= '</fieldset></td>'.
                    589:                            &Apache::loncommon::end_data_table_row()."\n";
                    590:             }
                    591:         } elsif ($item eq 'managers') {
                    592:             $output .= '<td'.$colspan.'>'.$custom_access.'</td>'."\n".
                    593:                        &Apache::loncommon::end_data_table_row()."\n";
1.471   ! raeburn   594:             unless ((&Apache::lonnet::allowed('udp',$ccdomain)) ||
        !           595:                     (($userenv{'domcoord.author'} eq 'blocked') &&
        !           596:                      (($env{'user.name'} ne $ccuname) || ($env{'user.domain'} ne $ccdomain)))) {
1.470     raeburn   597:                 $output .=
                    598:                     &Apache::loncommon::start_data_table_row()."\n".
                    599:                     '<td'.$colspan.'>';
                    600:                 if (@possmanagers) {
                    601:                     $output .= &mt('Select manager(s)').': ';
                    602:                     foreach my $user (@possmanagers) {
                    603:                         my $checked;
                    604:                         if (grep(/^\Q$user\E$/,@custommanagers)) {
                    605:                             $checked = ' checked="checked"';
                    606:                         }
                    607:                         $output .= '<span style="LC_nobreak"><label>'.
                    608:                                    '<input type="checkbox" name="custommanagers" '.
                    609:                                    'value="'.&HTML::Entities::encode($user,'\'<>"&').'"'.$checked.' />'.
                    610:                                    $user.'</label></span> ';
                    611:                     }
                    612:                 } else {
                    613:                     $output .= &mt('No co-author roles assignable as manager');
                    614:                 }
                    615:                 $output .= '</td>'.
                    616:                            &Apache::loncommon::end_data_table_row()."\n";
1.306     raeburn   617:             }
                    618:         } else {
                    619:             $currdisp = ($curr_access?&mt('Yes'):&mt('No'));
1.362     raeburn   620:             my $name = $context.'_'.$item;
1.470     raeburn   621:             $onclick = 'onclick="javascript:toggleCustom(this.form,'."'customtext_$item','custom$item'".');" ';
1.306     raeburn   622:             $custdisp = '<span class="LC_nobreak"><label>'.
1.362     raeburn   623:                         '<input type="radio" name="'.$name.'"'.
1.470     raeburn   624:                         ' value="1" '.$tool_on.$onclick.'/>'.&mt('On').'</label>&nbsp;<label>'.
1.362     raeburn   625:                         '<input type="radio" name="'.$name.'" value="0" '.
1.470     raeburn   626:                         $tool_off.$onclick.'/>'.&mt('Off').'</label></span>';
                    627:             $custradio = '<span id="customtext_'.$item.'"'.$customsty.' class="LC_nobreak">'.
                    628:                          '--'.$lt{'cusa'}.':&nbsp;'.$custdisp.'</span>';
                    629:         }
                    630:         unless (($item eq 'editors') || ($item eq 'managers')) {
                    631:             $output .= '  <td'.$colspan.'>'.$custom_access.('&nbsp;'x4).
                    632:                        $lt{'avai'}.': '.$currdisp.'</td>'."\n".
                    633:                        &Apache::loncommon::end_data_table_row()."\n";
                    634:             unless (&Apache::lonnet::allowed('udp',$ccdomain)) {
                    635:                 $output .=
1.275     raeburn   636:                    &Apache::loncommon::start_data_table_row()."\n".
1.470     raeburn   637:                    '<td><span class="LC_nobreak">'.
1.306     raeburn   638:                    $lt{'chse'}.': <label>'.
1.275     raeburn   639:                    '<input type="radio" name="custom'.$item.'" value="0" '.
1.470     raeburn   640:                    $cust_off.$onclick.'/>'.$lt{'usde'}.'</label>'.('&nbsp;' x3).
1.306     raeburn   641:                    '<label><input type="radio" name="custom'.$item.'" value="1" '.
1.470     raeburn   642:                    $cust_on.$onclick.'/>'.$lt{'uscu'}.'</label></span>';
                    643:                 if ($colspan) {
                    644:                     $output .= '</td><td>';
                    645:                 }
                    646:                 $output .= $custradio.'</td>'.
                    647:                            &Apache::loncommon::end_data_table_row()."\n";
                    648:             }
1.418     raeburn   649:         }
1.275     raeburn   650:     }
                    651:     return $output;
                    652: }
                    653: 
1.300     raeburn   654: sub coursereq_externaluser {
                    655:     my ($ccuname,$ccdomain,$cdom) = @_;
1.306     raeburn   656:     my (@usertools,@options,%validations,%userenv,$output);
1.300     raeburn   657:     my %lt = &Apache::lonlocal::texthash (
                    658:                    'official'   => 'Can request creation of official courses',
                    659:                    'unofficial' => 'Can request creation of unofficial courses',
                    660:                    'community'  => 'Can request creation of communities',
1.384     raeburn   661:                    'textbook'   => 'Can request creation of textbook courses',
1.411     raeburn   662:                    'placement'  => 'Can request creation of placement tests',
1.300     raeburn   663:     );
                    664: 
                    665:     %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    666:                       'reqcrsotherdom.official','reqcrsotherdom.unofficial',
1.411     raeburn   667:                       'reqcrsotherdom.community','reqcrsotherdom.textbook',
                    668:                       'reqcrsotherdom.placement');
                    669:     @usertools = ('official','unofficial','community','textbook','placement');
1.309     raeburn   670:     @options = ('approval','validate','autolimit');
1.306     raeburn   671:     %validations = &Apache::lonnet::auto_courserequest_checks($cdom);
                    672:     my $optregex = join('|',@options);
                    673:     my %reqtitles = &courserequest_titles();
1.300     raeburn   674:     foreach my $item (@usertools) {
1.306     raeburn   675:         my ($curroption,$currlimit,$tooloff);
1.300     raeburn   676:         if ($userenv{'reqcrsotherdom.'.$item} ne '') {
                    677:             my @curr = split(',',$userenv{'reqcrsotherdom.'.$item});
1.314     raeburn   678:             foreach my $req (@curr) {
                    679:                 if ($req =~ /^\Q$cdom\E\:($optregex)=?(\d*)$/) {
                    680:                     $curroption = $1;
                    681:                     $currlimit = $2;
                    682:                     last;
1.306     raeburn   683:                 }
                    684:             }
1.314     raeburn   685:             if (!$curroption) {
                    686:                 $curroption = 'norequest';
                    687:                 $tooloff = ' checked="checked"';
                    688:             }
1.306     raeburn   689:         } else {
                    690:             $curroption = 'norequest';
                    691:             $tooloff = ' checked="checked"';
                    692:         }
                    693:         $output.= &Apache::loncommon::start_data_table_row()."\n".
1.314     raeburn   694:                   '  <td><span class="LC_nobreak">'.$lt{$item}.': </span></td><td>'.
                    695:                   '<table><tr><td valign="top">'."\n".
1.306     raeburn   696:                   '<label><input type="radio" name="reqcrsotherdom_'.$item.
1.314     raeburn   697:                   '" value=""'.$tooloff.' />'.$reqtitles{'norequest'}.
                    698:                   '</label></td>';
1.306     raeburn   699:         foreach my $option (@options) {
                    700:             if ($option eq 'validate') {
                    701:                 my $canvalidate = 0;
                    702:                 if (ref($validations{$item}) eq 'HASH') {
                    703:                     if ($validations{$item}{'_external_'}) {
                    704:                         $canvalidate = 1;
                    705:                     }
                    706:                 }
                    707:                 next if (!$canvalidate);
                    708:             }
                    709:             my $checked = '';
                    710:             if ($option eq $curroption) {
                    711:                 $checked = ' checked="checked"';
                    712:             }
1.314     raeburn   713:             $output .= '<td valign="top"><span class="LC_nobreak"><label>'.
1.306     raeburn   714:                        '<input type="radio" name="reqcrsotherdom_'.$item.
                    715:                        '" value="'.$option.'"'.$checked.' />'.
1.314     raeburn   716:                        $reqtitles{$option}.'</label>';
1.306     raeburn   717:             if ($option eq 'autolimit') {
1.314     raeburn   718:                 $output .= '&nbsp;<input type="text" name="reqcrsotherdom_'.
1.306     raeburn   719:                            $item.'_limit" size="1" '.
1.314     raeburn   720:                            'value="'.$currlimit.'" /></span>'.
                    721:                            '<br />'.$reqtitles{'unlimited'};
                    722:             } else {
                    723:                 $output .= '</span>';
1.300     raeburn   724:             }
1.314     raeburn   725:             $output .= '</td>';
1.300     raeburn   726:         }
1.314     raeburn   727:         $output .= '</td></tr></table></td>'."\n".
1.300     raeburn   728:                    &Apache::loncommon::end_data_table_row()."\n";
                    729:     }
                    730:     return $output;
                    731: }
                    732: 
1.362     raeburn   733: sub domainrole_req {
                    734:     my ($ccuname,$ccdomain) = @_;
                    735:     return '<br /><h3>'.
1.470     raeburn   736:            &mt('Can Request Assignment of Domain Roles?').
1.362     raeburn   737:            '</h3>'."\n".
                    738:            &Apache::loncommon::start_data_table().
                    739:            &build_tools_display($ccuname,$ccdomain,
                    740:                                 'requestauthor').
                    741:            &Apache::loncommon::end_data_table();
                    742: }
                    743: 
1.470     raeburn   744: sub authoring_defaults {
                    745:     my ($ccuname,$ccdomain) = @_;
                    746:     return '<br /><h3>'.
                    747:            &mt('Authoring Space defaults (if role assigned)').
                    748:            '</h3>'."\n".
                    749:            &Apache::loncommon::start_data_table().
                    750:            &build_tools_display($ccuname,$ccdomain,
                    751:                                 'authordefaults').
                    752:            &user_quotas($ccuname,$ccdomain,'author').
                    753:            &Apache::loncommon::end_data_table();
                    754: }
                    755: 
1.306     raeburn   756: sub courserequest_titles {
                    757:     my %titles = &Apache::lonlocal::texthash (
                    758:                                    official   => 'Official',
                    759:                                    unofficial => 'Unofficial',
                    760:                                    community  => 'Communities',
1.384     raeburn   761:                                    textbook   => 'Textbook',
1.411     raeburn   762:                                    placement  => 'Placement Tests',
1.449     raeburn   763:                                    lti        => 'LTI Provider',
1.306     raeburn   764:                                    norequest  => 'Not allowed',
1.309     raeburn   765:                                    approval   => 'Approval by Dom. Coord.',
1.306     raeburn   766:                                    validate   => 'With validation',
                    767:                                    autolimit  => 'Numerical limit',
1.314     raeburn   768:                                    unlimited  => '(blank for unlimited)',
1.306     raeburn   769:                  );
                    770:     return %titles;
                    771: }
                    772: 
                    773: sub courserequest_display {
                    774:     my %titles = &Apache::lonlocal::texthash (
1.309     raeburn   775:                                    approval   => 'Yes, need approval',
1.306     raeburn   776:                                    validate   => 'Yes, with validation',
                    777:                                    norequest  => 'No',
                    778:    );
                    779:    return %titles;
                    780: }
                    781: 
1.362     raeburn   782: sub requestauthor_titles {
                    783:     my %titles = &Apache::lonlocal::texthash (
                    784:                                    norequest  => 'Not allowed',
                    785:                                    approval   => 'Approval by Dom. Coord.',
                    786:                                    automatic  => 'Automatic approval',
                    787:                  );
                    788:     return %titles;
                    789: 
                    790: }
                    791: 
                    792: sub requestauthor_display {
                    793:     my %titles = &Apache::lonlocal::texthash (
                    794:                                    approval   => 'Yes, need approval',
                    795:                                    automatic  => 'Yes, automatic approval',
                    796:                                    norequest  => 'No',
                    797:    );
                    798:    return %titles;
                    799: }
                    800: 
1.383     raeburn   801: sub requestchange_display {
                    802:     my %titles = &Apache::lonlocal::texthash (
                    803:                                    approval   => "availability set to 'on' (approval required)", 
                    804:                                    automatic  => "availability set to 'on' (automatic approval)",
                    805:                                    norequest  => "availability set to 'off'",
                    806:    );
                    807:    return %titles;
                    808: }
                    809: 
1.362     raeburn   810: sub curr_requestauthor {
                    811:     my ($uname,$udom,$isadv,$inststatuses,$domconfig) = @_;
                    812:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
                    813:     if ($uname eq '' || $udom eq '') {
                    814:         $uname = $env{'user.name'};
                    815:         $udom = $env{'user.domain'};
                    816:         $isadv = $env{'user.adv'};
                    817:     }
                    818:     my (%userenv,%settings,$val);
                    819:     my @options = ('automatic','approval');
                    820:     %userenv =
                    821:         &Apache::lonnet::userenvironment($udom,$uname,'requestauthor','inststatus');
                    822:     if ($userenv{'requestauthor'}) {
                    823:         $val = $userenv{'requestauthor'};
                    824:         @{$inststatuses} = ('_custom_');
                    825:     } else {
                    826:         my %alltasks;
                    827:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') {
                    828:             %settings = %{$domconfig->{'requestauthor'}};
                    829:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
                    830:                 $val = $settings{'_LC_adv'};
                    831:                 @{$inststatuses} = ('_LC_adv_');
                    832:             } else {
                    833:                 if ($userenv{'inststatus'} ne '') {
                    834:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
                    835:                 } else {
                    836:                     @{$inststatuses} = ('default');
                    837:                 }
                    838:                 foreach my $status (@{$inststatuses}) {
                    839:                     if (exists($settings{$status})) {
                    840:                         my $value = $settings{$status};
                    841:                         next unless ($value);
                    842:                         unless (exists($alltasks{$value})) {
                    843:                             if (ref($alltasks{$value}) eq 'ARRAY') {
                    844:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
                    845:                                     push(@{$alltasks{$value}},$status);
                    846:                                 }
                    847:                             } else {
                    848:                                 @{$alltasks{$value}} = ($status);
                    849:                             }
                    850:                         }
                    851:                     }
                    852:                 }
                    853:                 foreach my $option (@options) {
                    854:                     if ($alltasks{$option}) {
                    855:                         $val = $option;
                    856:                         last;
                    857:                     }
                    858:                 }
                    859:             }
                    860:         }
                    861:     }
                    862:     return $val;
                    863: }
                    864: 
1.2       www       865: # =================================================================== Phase one
1.1       www       866: 
1.42      matthew   867: sub print_username_entry_form {
1.439     raeburn   868:     my ($r,$context,$response,$srch,$forcenewuser,$crstype,$brcrum,
                    869:         $permission) = @_;
1.101     albertel  870:     my $defdom=$env{'request.role.domain'};
1.160     raeburn   871:     my $formtoset = 'crtuser';
                    872:     if (exists($env{'form.startrolename'})) {
                    873:         $formtoset = 'docustom';
                    874:         $env{'form.rolename'} = $env{'form.startrolename'};
1.207     raeburn   875:     } elsif ($env{'form.origform'} eq 'crtusername') {
                    876:         $formtoset =  $env{'form.origform'};
1.160     raeburn   877:     }
                    878: 
                    879:     my ($jsback,$elements) = &crumb_utilities();
                    880: 
                    881:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
1.165     albertel  882:         '<script type="text/javascript">'."\n".
1.301     bisitz    883:         '// <![CDATA['."\n".
                    884:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset})."\n".
                    885:         '// ]]>'."\n".
1.162     raeburn   886:         '</script>'."\n";
1.160     raeburn   887: 
1.324     raeburn   888:     my %existingroles=&Apache::lonuserutils::my_custom_roles($crstype);
                    889:     if (($env{'form.action'} eq 'custom') && (keys(%existingroles) > 0)
                    890:         && (&Apache::lonnet::allowed('mcr','/'))) {
                    891:         $jscript .= &customrole_javascript();
                    892:     }
1.224     raeburn   893:     my $helpitem = 'Course_Change_Privileges';
                    894:     if ($env{'form.action'} eq 'custom') {
1.439     raeburn   895:         if ($context eq 'course') {
                    896:             $helpitem = 'Course_Editing_Custom_Roles';
                    897:         } elsif ($context eq 'domain') {
                    898:             $helpitem = 'Domain_Editing_Custom_Roles';
                    899:         }
1.224     raeburn   900:     } elsif ($env{'form.action'} eq 'singlestudent') {
                    901:         $helpitem = 'Course_Add_Student';
1.416     raeburn   902:     } elsif ($env{'form.action'} eq 'accesslogs') {
                    903:         $helpitem = 'Domain_User_Access_Logs';
1.439     raeburn   904:     } elsif ($context eq 'author') {
                    905:         $helpitem = 'Author_Change_Privileges';
                    906:     } elsif ($context eq 'domain') {
                    907:         if ($permission->{'cusr'}) {
                    908:             $helpitem = 'Domain_Change_Privileges';
                    909:         } elsif ($permission->{'view'}) {
                    910:             $helpitem = 'Domain_View_Privileges';
                    911:         } else {
                    912:             undef($helpitem);
                    913:         }
1.224     raeburn   914:     }
1.422     raeburn   915:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$defdom);
1.351     raeburn   916:     if ($env{'form.action'} eq 'custom') {
                    917:         push(@{$brcrum},
                    918:                  {href=>"javascript:backPage(document.crtuser)",       
                    919:                   text=>"Pick custom role",
                    920:                   help => $helpitem,}
                    921:                  );
                    922:     } else {
                    923:         push (@{$brcrum},
                    924:                   {href => "javascript:backPage(document.crtuser)",
                    925:                    text => $breadcrumb_text{'search'},
                    926:                    help => $helpitem,
                    927:                    faq  => 282,
                    928:                    bug  => 'Instructor Interface',}
                    929:                   );
                    930:     }
                    931:     my %loaditems = (
                    932:                 'onload' => "javascript:setFormElements(document.$formtoset)",
                    933:                     );
                    934:     my $args = {bread_crumbs           => $brcrum,
                    935:                 bread_crumbs_component => 'User Management',
                    936:                 add_entries            => \%loaditems,};
                    937:     $r->print(&Apache::loncommon::start_page('User Management',$jscript,$args));
                    938: 
1.71      sakharuk  939:     my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   940:                     'srst' => 'Search for a user and enroll as a student',
1.318     raeburn   941:                     'srme' => 'Search for a user and enroll as a member',
1.229     raeburn   942:                     'srad' => 'Search for a user and modify/add user information or roles',
1.422     raeburn   943:                     'srvu' => 'Search for a user and view user information and roles',
1.416     raeburn   944:                     'srva' => 'Search for a user and view access log information',
1.71      sakharuk  945: 		    'usr'  => "Username",
                    946:                     'dom'  => "Domain",
1.324     raeburn   947:                     'ecrp' => "Define or Edit Custom Role",
                    948:                     'nr'   => "role name",
1.282     schafran  949:                     'cre'  => "Next",
1.71      sakharuk  950: 				       );
1.351     raeburn   951: 
1.214     raeburn   952:     if ($env{'form.action'} eq 'custom') {
1.190     raeburn   953:         if (&Apache::lonnet::allowed('mcr','/')) {
1.324     raeburn   954:             my $newroletext = &mt('Define new custom role:');
                    955:             $r->print('<form action="/adm/createuser" method="post" name="docustom">'.
                    956:                       '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                    957:                       '<input type="hidden" name="phase" value="selected_custom_edit" />'.
                    958:                       '<h3>'.$lt{'ecrp'}.'</h3>'.
                    959:                       &Apache::loncommon::start_data_table().
                    960:                       &Apache::loncommon::start_data_table_row().
                    961:                       '<td>');
                    962:             if (keys(%existingroles) > 0) {
                    963:                 $r->print('<br /><label><input type="radio" name="customroleaction" value="new" checked="checked" onclick="setCustomFields();" /><b>'.$newroletext.'</b></label>');
                    964:             } else {
                    965:                 $r->print('<br /><input type="hidden" name="customroleaction" value="new" /><b>'.$newroletext.'</b>');
                    966:             }
                    967:             $r->print('</td><td align="center">'.$lt{'nr'}.'<br /><input type="text" size="15" name="newrolename" onfocus="setCustomAction('."'new'".');" /></td>'.
                    968:                       &Apache::loncommon::end_data_table_row());
                    969:             if (keys(%existingroles) > 0) {
                    970:                 $r->print(&Apache::loncommon::start_data_table_row().'<td><br />'.
                    971:                           '<label><input type="radio" name="customroleaction" value="edit" onclick="setCustomFields();"/><b>'.
                    972:                           &mt('View/Modify existing role:').'</b></label></td>'.
                    973:                           '<td align="center"><br />'.
                    974:                           '<select name="rolename" onchange="setCustomAction('."'edit'".');">'.
1.326     raeburn   975:                           '<option value="" selected="selected">'.
1.324     raeburn   976:                           &mt('Select'));
                    977:                 foreach my $role (sort(keys(%existingroles))) {
1.326     raeburn   978:                     $r->print('<option value="'.$role.'">'.$role.'</option>');
1.324     raeburn   979:                 }
                    980:                 $r->print('</select>'.
                    981:                           '</td>'.
                    982:                           &Apache::loncommon::end_data_table_row());
                    983:             }
                    984:             $r->print(&Apache::loncommon::end_data_table().'<p>'.
                    985:                       '<input name="customeditor" type="submit" value="'.
                    986:                       $lt{'cre'}.'" /></p>'.
                    987:                       '</form>');
1.190     raeburn   988:         }
1.213     raeburn   989:     } else {
1.229     raeburn   990:         my $actiontext = $lt{'srad'};
1.436     raeburn   991:         my $fixeddom;
1.213     raeburn   992:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   993:             if ($crstype eq 'Community') {
                    994:                 $actiontext = $lt{'srme'};
                    995:             } else {
                    996:                 $actiontext = $lt{'srst'};
                    997:             }
1.416     raeburn   998:         } elsif ($env{'form.action'} eq 'accesslogs') {
1.417     raeburn   999:             $actiontext = $lt{'srva'};
1.436     raeburn  1000:             $fixeddom = 1;
1.422     raeburn  1001:         } elsif (($env{'form.action'} eq 'singleuser') &&
                   1002:                  ($context eq 'domain') && (!&Apache::lonnet::allowed('mau',$defdom))) {
                   1003:             $actiontext = $lt{'srvu'};
1.439     raeburn  1004:             $fixeddom = 1;
1.213     raeburn  1005:         }
1.324     raeburn  1006:         $r->print("<h3>$actiontext</h3>");
1.213     raeburn  1007:         if ($env{'form.origform'} ne 'crtusername') {
1.415     raeburn  1008:             if ($response) {
                   1009:                $r->print("\n<div>$response</div>".
                   1010:                          '<br clear="all" />');
                   1011:             }
1.213     raeburn  1012:         }
1.436     raeburn  1013:         $r->print(&entry_form($defdom,$srch,$forcenewuser,$context,$response,$crstype,$fixeddom));
1.107     www      1014:     }
1.110     albertel 1015: }
                   1016: 
1.324     raeburn  1017: sub customrole_javascript {
                   1018:     my $js = <<"END";
                   1019: <script type="text/javascript">
                   1020: // <![CDATA[
                   1021: 
                   1022: function setCustomFields() {
                   1023:     if (document.docustom.customroleaction.length > 0) {
                   1024:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                   1025:             if (document.docustom.customroleaction[i].checked) {
                   1026:                 if (document.docustom.customroleaction[i].value == 'new') {
                   1027:                     document.docustom.rolename.selectedIndex = 0;
                   1028:                 } else {
                   1029:                     document.docustom.newrolename.value = '';
                   1030:                 }
                   1031:             }
                   1032:         }
                   1033:     }
                   1034:     return;
                   1035: }
                   1036: 
                   1037: function setCustomAction(caller) {
                   1038:     if (document.docustom.customroleaction.length > 0) {
                   1039:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                   1040:             if (document.docustom.customroleaction[i].value == caller) {
                   1041:                 document.docustom.customroleaction[i].checked = true;
                   1042:             }
                   1043:         }
                   1044:     }
                   1045:     setCustomFields();
                   1046:     return;
                   1047: }
                   1048: 
                   1049: // ]]>
                   1050: </script>
                   1051: END
                   1052:     return $js;
                   1053: }
                   1054: 
1.160     raeburn  1055: sub entry_form {
1.416     raeburn  1056:     my ($dom,$srch,$forcenewuser,$context,$responsemsg,$crstype,$fixeddom) = @_;
1.229     raeburn  1057:     my ($usertype,$inexact);
1.214     raeburn  1058:     if (ref($srch) eq 'HASH') {
                   1059:         if (($srch->{'srchin'} eq 'dom') &&
                   1060:             ($srch->{'srchby'} eq 'uname') &&
                   1061:             ($srch->{'srchtype'} eq 'exact') &&
                   1062:             ($srch->{'srchdomain'} ne '') &&
                   1063:             ($srch->{'srchterm'} ne '')) {
1.353     raeburn  1064:             my (%curr_rules,%got_rules);
1.214     raeburn  1065:             my ($rules,$ruleorder) =
                   1066:                 &Apache::lonnet::inst_userrules($srch->{'srchdomain'},'username');
1.353     raeburn  1067:             $usertype = &Apache::lonuserutils::check_usertype($srch->{'srchdomain'},$srch->{'srchterm'},$rules,\%curr_rules,\%got_rules);
1.229     raeburn  1068:         } else {
                   1069:             $inexact = 1;
1.214     raeburn  1070:         }
1.207     raeburn  1071:     }
1.438     raeburn  1072:     my ($cancreate,$noinstd);
                   1073:     if ($env{'form.action'} eq 'accesslogs') {
                   1074:         $noinstd = 1;
                   1075:     } else {
                   1076:         $cancreate =
                   1077:             &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
                   1078:     }
1.412     raeburn  1079:     my ($userpicker,$cansearch) = 
1.179     raeburn  1080:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
1.438     raeburn  1081:                                        'document.crtuser',$cancreate,$usertype,$context,$fixeddom,$noinstd);
1.160     raeburn  1082:     my $srchbutton = &mt('Search');
1.229     raeburn  1083:     if ($env{'form.action'} eq 'singlestudent') {
                   1084:         $srchbutton = &mt('Search and Enroll');
1.416     raeburn  1085:     } elsif ($env{'form.action'} eq 'accesslogs') {
                   1086:         $srchbutton = &mt('Search');
1.229     raeburn  1087:     } elsif ($cancreate && $responsemsg ne '' && $inexact) {
                   1088:         $srchbutton = &mt('Search or Add New User');
                   1089:     }
1.412     raeburn  1090:     my $output;
                   1091:     if ($cansearch) {
                   1092:         $output = <<"ENDBLOCK";
1.160     raeburn  1093: <form action="/adm/createuser" method="post" name="crtuser">
1.190     raeburn  1094: <input type="hidden" name="action" value="$env{'form.action'}" />
1.160     raeburn  1095: <input type="hidden" name="phase" value="get_user_info" />
                   1096: $userpicker
1.179     raeburn  1097: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
1.160     raeburn  1098: </form>
1.207     raeburn  1099: ENDBLOCK
1.412     raeburn  1100:     } else {
                   1101:         $output = '<p>'.$userpicker.'</p>';
                   1102:     }
1.422     raeburn  1103:     if (($env{'form.phase'} eq '') && ($env{'form.action'} ne 'accesslogs') &&
1.430     raeburn  1104:         (!(($env{'form.action'} eq 'singleuser') && ($context eq 'domain') &&
1.422     raeburn  1105:         (!&Apache::lonnet::allowed('mau',$env{'request.role.domain'}))))) {
1.207     raeburn  1106:         my $defdom=$env{'request.role.domain'};
1.446     raeburn  1107:         my ($trusted,$untrusted);
1.444     raeburn  1108:         if ($context eq 'course') {
1.446     raeburn  1109:             ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('enroll',$defdom);
1.444     raeburn  1110:         } elsif ($context eq 'author') {
1.446     raeburn  1111:             ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('othcoau',$defdom);
1.444     raeburn  1112:         } elsif ($context eq 'domain') {
1.446     raeburn  1113:             ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('domroles',$defdom); 
1.444     raeburn  1114:         }
1.446     raeburn  1115:         my $domform = &Apache::loncommon::select_dom_form($defdom,'srchdomain',undef,undef,undef,$trusted,$untrusted);
1.207     raeburn  1116:         my %lt=&Apache::lonlocal::texthash(
1.229     raeburn  1117:                   'enro' => 'Enroll one student',
1.318     raeburn  1118:                   'enrm' => 'Enroll one member',
1.229     raeburn  1119:                   'admo' => 'Add/modify a single user',
                   1120:                   'crea' => 'create new user if required',
                   1121:                   'uskn' => "username is known",
1.207     raeburn  1122:                   'crnu' => 'Create a new user',
                   1123:                   'usr'  => 'Username',
                   1124:                   'dom'  => 'in domain',
1.229     raeburn  1125:                   'enrl' => 'Enroll',
                   1126:                   'cram'  => 'Create/Modify user',
1.207     raeburn  1127:         );
1.229     raeburn  1128:         my $sellink=&Apache::loncommon::selectstudent_link('crtusername','srchterm','srchdomain');
                   1129:         my ($title,$buttontext,$showresponse);
1.318     raeburn  1130:         if ($env{'form.action'} eq 'singlestudent') {
                   1131:             if ($crstype eq 'Community') {
                   1132:                 $title = $lt{'enrm'};
                   1133:             } else {
                   1134:                 $title = $lt{'enro'};
                   1135:             }
1.229     raeburn  1136:             $buttontext = $lt{'enrl'};
                   1137:         } else {
                   1138:             $title = $lt{'admo'};
                   1139:             $buttontext = $lt{'cram'};
                   1140:         }
                   1141:         if ($cancreate) {
                   1142:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'crea'}.')</span>';
                   1143:         } else {
                   1144:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'uskn'}.')</span>';
                   1145:         }
                   1146:         if ($env{'form.origform'} eq 'crtusername') {
                   1147:             $showresponse = $responsemsg;
                   1148:         }
1.207     raeburn  1149:         $output .= <<"ENDDOCUMENT";
1.229     raeburn  1150: <br />
1.207     raeburn  1151: <form action="/adm/createuser" method="post" name="crtusername">
                   1152: <input type="hidden" name="action" value="$env{'form.action'}" />
                   1153: <input type="hidden" name="phase" value="createnewuser" />
                   1154: <input type="hidden" name="srchtype" value="exact" />
1.233     raeburn  1155: <input type="hidden" name="srchby" value="uname" />
1.207     raeburn  1156: <input type="hidden" name="srchin" value="dom" />
                   1157: <input type="hidden" name="forcenewuser" value="1" />
                   1158: <input type="hidden" name="origform" value="crtusername" />
1.229     raeburn  1159: <h3>$title</h3>
                   1160: $showresponse
1.207     raeburn  1161: <table>
                   1162:  <tr>
                   1163:   <td>$lt{'usr'}:</td>
                   1164:   <td><input type="text" size="15" name="srchterm" /></td>
                   1165:   <td>&nbsp;$lt{'dom'}:</td><td>$domform</td>
1.229     raeburn  1166:   <td>&nbsp;$sellink&nbsp;</td>
                   1167:   <td>&nbsp;<input name="userrole" type="submit" value="$buttontext" /></td>
1.207     raeburn  1168:  </tr>
                   1169: </table>
                   1170: </form>
1.160     raeburn  1171: ENDDOCUMENT
1.207     raeburn  1172:     }
1.160     raeburn  1173:     return $output;
                   1174: }
1.110     albertel 1175: 
                   1176: sub user_modification_js {
1.113     raeburn  1177:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
                   1178:     
1.110     albertel 1179:     return <<END;
                   1180: <script type="text/javascript" language="Javascript">
1.301     bisitz   1181: // <![CDATA[
1.314     raeburn  1182: 
1.110     albertel 1183:     $pjump_def
                   1184:     $dc_setcourse_code
                   1185: 
                   1186:     function dateset() {
                   1187:         eval("document.cu."+document.cu.pres_marker.value+
                   1188:             ".value=document.cu.pres_value.value");
1.359     www      1189:         modalWindow.close();
1.110     albertel 1190:     }
                   1191: 
1.113     raeburn  1192:     $nondc_setsection_code
1.301     bisitz   1193: // ]]>
1.110     albertel 1194: </script>
                   1195: END
1.2       www      1196: }
                   1197: 
                   1198: # =================================================================== Phase two
1.160     raeburn  1199: sub print_user_selection_page {
1.351     raeburn  1200:     my ($r,$response,$srch,$srch_results,$srcharray,$context,$opener_elements,$crstype,$brcrum) = @_;
1.160     raeburn  1201:     my @fields = ('username','domain','lastname','firstname','permanentemail');
                   1202:     my $sortby = $env{'form.sortby'};
                   1203: 
                   1204:     if (!grep(/^\Q$sortby\E$/,@fields)) {
                   1205:         $sortby = 'lastname';
                   1206:     }
                   1207: 
                   1208:     my ($jsback,$elements) = &crumb_utilities();
                   1209: 
                   1210:     my $jscript = (<<ENDSCRIPT);
                   1211: <script type="text/javascript">
1.301     bisitz   1212: // <![CDATA[
1.160     raeburn  1213: function pickuser(uname,udom) {
                   1214:     document.usersrchform.seluname.value=uname;
                   1215:     document.usersrchform.seludom.value=udom;
                   1216:     document.usersrchform.phase.value="userpicked";
                   1217:     document.usersrchform.submit();
                   1218: }
                   1219: 
                   1220: $jsback
1.301     bisitz   1221: // ]]>
1.160     raeburn  1222: </script>
                   1223: ENDSCRIPT
                   1224: 
                   1225:     my %lt=&Apache::lonlocal::texthash(
1.179     raeburn  1226:                                        'usrch'          => "User Search to add/modify roles",
                   1227:                                        'stusrch'        => "User Search to enroll student",
1.318     raeburn  1228:                                        'memsrch'        => "User Search to enroll member",
1.416     raeburn  1229:                                        'srcva'          => "Search for a user and view access log information",
1.422     raeburn  1230:                                        'usrvu'          => "User Search to view user roles",
1.179     raeburn  1231:                                        'usel'           => "Select a user to add/modify roles",
1.422     raeburn  1232:                                        'suvr'           => "Select a user to view roles",
1.318     raeburn  1233:                                        'stusel'         => "Select a user to enroll as a student",
                   1234:                                        'memsel'         => "Select a user to enroll as a member",
1.416     raeburn  1235:                                        'vacsel'         => "Select a user to view access log",
1.160     raeburn  1236:                                        'username'       => "username",
                   1237:                                        'domain'         => "domain",
                   1238:                                        'lastname'       => "last name",
                   1239:                                        'firstname'      => "first name",
                   1240:                                        'permanentemail' => "permanent e-mail",
                   1241:                                       );
1.302     raeburn  1242:     if ($context eq 'requestcrs') {
                   1243:         $r->print('<div>');
                   1244:     } else {
1.422     raeburn  1245:         my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$srch->{'srchdomain'});
1.351     raeburn  1246:         my $helpitem;
                   1247:         if ($env{'form.action'} eq 'singleuser') {
                   1248:             $helpitem = 'Course_Change_Privileges';
                   1249:         } elsif ($env{'form.action'} eq 'singlestudent') {
                   1250:             $helpitem = 'Course_Add_Student';
1.439     raeburn  1251:         } elsif ($context eq 'author') {
                   1252:             $helpitem = 'Author_Change_Privileges';
                   1253:         } elsif ($context eq 'domain') {
                   1254:             $helpitem = 'Domain_Change_Privileges';
1.351     raeburn  1255:         }
                   1256:         push (@{$brcrum},
                   1257:                   {href => "javascript:backPage(document.usersrchform,'','')",
                   1258:                    text => $breadcrumb_text{'search'},
                   1259:                    faq  => 282,
                   1260:                    bug  => 'Instructor Interface',},
                   1261:                   {href => "javascript:backPage(document.usersrchform,'get_user_info','select')",
                   1262:                    text => $breadcrumb_text{'userpicked'},
                   1263:                    faq  => 282,
                   1264:                    bug  => 'Instructor Interface',
                   1265:                    help => $helpitem}
                   1266:                   );
                   1267:         $r->print(&Apache::loncommon::start_page('User Management',$jscript,{bread_crumbs => $brcrum}));
1.302     raeburn  1268:         if ($env{'form.action'} eq 'singleuser') {
1.422     raeburn  1269:             my $readonly;
                   1270:             if (($context eq 'domain') && (!&Apache::lonnet::allowed('mau',$srch->{'srchdomain'}))) {
                   1271:                 $readonly = 1;
                   1272:                 $r->print("<b>$lt{'usrvu'}</b><br />");
                   1273:             } else {
                   1274:                 $r->print("<b>$lt{'usrch'}</b><br />");
                   1275:             }
1.318     raeburn  1276:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
1.422     raeburn  1277:             if ($readonly) {
                   1278:                 $r->print('<h3>'.$lt{'suvr'}.'</h3>');
                   1279:             } else {
                   1280:                 $r->print('<h3>'.$lt{'usel'}.'</h3>');
                   1281:             }
1.302     raeburn  1282:         } elsif ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1283:             $r->print($jscript."<b>");
                   1284:             if ($crstype eq 'Community') {
                   1285:                 $r->print($lt{'memsrch'});
                   1286:             } else {
                   1287:                 $r->print($lt{'stusrch'});
                   1288:             }
                   1289:             $r->print("</b><br />");
                   1290:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
                   1291:             $r->print('</form><h3>');
                   1292:             if ($crstype eq 'Community') {
                   1293:                 $r->print($lt{'memsel'});
                   1294:             } else {
                   1295:                 $r->print($lt{'stusel'});
                   1296:             }
                   1297:             $r->print('</h3>');
1.416     raeburn  1298:         } elsif ($env{'form.action'} eq 'accesslogs') {
                   1299:             $r->print("<b>$lt{'srcva'}</b><br />");
1.438     raeburn  1300:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,undef,1));
1.416     raeburn  1301:             $r->print('<h3>'.$lt{'vacsel'}.'</h3>');
1.302     raeburn  1302:         }
1.179     raeburn  1303:     }
1.380     bisitz   1304:     $r->print('<form name="usersrchform" method="post" action="">'.
1.160     raeburn  1305:               &Apache::loncommon::start_data_table()."\n".
                   1306:               &Apache::loncommon::start_data_table_header_row()."\n".
                   1307:               ' <th> </th>'."\n");
                   1308:     foreach my $field (@fields) {
                   1309:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
                   1310:                   "'".$field."'".';document.usersrchform.submit();">'.
                   1311:                   $lt{$field}.'</a></th>'."\n");
                   1312:     }
                   1313:     $r->print(&Apache::loncommon::end_data_table_header_row());
                   1314: 
                   1315:     my @sorted_users = sort {
1.167     albertel 1316:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
1.160     raeburn  1317:             ||
1.167     albertel 1318:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
1.160     raeburn  1319:             ||
                   1320:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
1.167     albertel 1321: 	    ||
                   1322: 	lc($a) cmp lc($b)
1.160     raeburn  1323:         } (keys(%$srch_results));
                   1324: 
                   1325:     foreach my $user (@sorted_users) {
                   1326:         my ($uname,$udom) = split(/:/,$user);
1.302     raeburn  1327:         my $onclick;
                   1328:         if ($context eq 'requestcrs') {
1.314     raeburn  1329:             $onclick =
1.302     raeburn  1330:                 'onclick="javascript:gochoose('."'$uname','$udom',".
                   1331:                                                "'$srch_results->{$user}->{firstname}',".
                   1332:                                                "'$srch_results->{$user}->{lastname}',".
                   1333:                                                "'$srch_results->{$user}->{permanentemail}'".');"';
                   1334:         } else {
1.314     raeburn  1335:             $onclick =
1.302     raeburn  1336:                 ' onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".');"';
                   1337:         }
1.160     raeburn  1338:         $r->print(&Apache::loncommon::start_data_table_row().
1.302     raeburn  1339:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" '.
                   1340:                   $onclick.' /></td>'.
1.160     raeburn  1341:                   '<td><tt>'.$uname.'</tt></td>'.
                   1342:                   '<td><tt>'.$udom.'</tt></td>');
                   1343:         foreach my $field ('lastname','firstname','permanentemail') {
                   1344:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
                   1345:         }
                   1346:         $r->print(&Apache::loncommon::end_data_table_row());
                   1347:     }
                   1348:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
1.179     raeburn  1349:     if (ref($srcharray) eq 'ARRAY') {
                   1350:         foreach my $item (@{$srcharray}) {
                   1351:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
                   1352:         }
                   1353:     }
1.160     raeburn  1354:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
                   1355:               ' <input type="hidden" name="seluname" value="" />'."\n".
                   1356:               ' <input type="hidden" name="seludom" value="" />'."\n".
1.179     raeburn  1357:               ' <input type="hidden" name="currstate" value="select" />'."\n".
1.190     raeburn  1358:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
1.214     raeburn  1359:               ' <input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
1.302     raeburn  1360:     if ($context eq 'requestcrs') {
                   1361:         $r->print($opener_elements.'</form></div>');
                   1362:     } else {
1.351     raeburn  1363:         $r->print($response.'</form>');
1.302     raeburn  1364:     }
1.160     raeburn  1365: }
                   1366: 
                   1367: sub print_user_query_page {
1.351     raeburn  1368:     my ($r,$caller,$brcrum) = @_;
1.160     raeburn  1369: # FIXME - this is for a network-wide name search (similar to catalog search)
                   1370: # To use frames with similar behavior to catalog/portfolio search.
                   1371: # To be implemented. 
                   1372:     return;
                   1373: }
                   1374: 
1.42      matthew  1375: sub print_user_modification_page {
1.375     raeburn  1376:     my ($r,$ccuname,$ccdomain,$srch,$response,$context,$permission,$crstype,
                   1377:         $brcrum,$showcredits) = @_;
1.185     raeburn  1378:     if (($ccuname eq '') || ($ccdomain eq '')) {
1.215     raeburn  1379:         my $usermsg = &mt('No username and/or domain provided.');
                   1380:         $env{'form.phase'} = '';
1.439     raeburn  1381: 	&print_username_entry_form($r,$context,$usermsg,'','',$crstype,$brcrum,
                   1382:                                    $permission);
1.58      www      1383:         return;
                   1384:     }
1.213     raeburn  1385:     my ($form,$formname);
                   1386:     if ($env{'form.action'} eq 'singlestudent') {
                   1387:         $form = 'document.enrollstudent';
                   1388:         $formname = 'enrollstudent';
                   1389:     } else {
                   1390:         $form = 'document.cu';
                   1391:         $formname = 'cu';
                   1392:     }
1.188     raeburn  1393:     my %abv_auth = &auth_abbrev();
1.227     raeburn  1394:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
1.185     raeburn  1395:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
                   1396:     if ($uhome eq 'no_host') {
1.215     raeburn  1397:         my $usertype;
                   1398:         my ($rules,$ruleorder) =
                   1399:             &Apache::lonnet::inst_userrules($ccdomain,'username');
                   1400:             $usertype =
1.353     raeburn  1401:                 &Apache::lonuserutils::check_usertype($ccdomain,$ccuname,$rules,
1.362     raeburn  1402:                                                       \%curr_rules,\%got_rules);
1.215     raeburn  1403:         my $cancreate =
                   1404:             &Apache::lonuserutils::can_create_user($ccdomain,$context,
                   1405:                                                    $usertype);
                   1406:         if (!$cancreate) {
1.292     bisitz   1407:             my $helplink = 'javascript:helpMenu('."'display'".')';
1.215     raeburn  1408:             my %usertypetext = (
                   1409:                 official   => 'institutional',
                   1410:                 unofficial => 'non-institutional',
                   1411:             );
                   1412:             my $response;
                   1413:             if ($env{'form.origform'} eq 'crtusername') {
1.362     raeburn  1414:                 $response = '<span class="LC_warning">'.
                   1415:                             &mt('No match found for the username [_1] in LON-CAPA domain: [_2]',
                   1416:                                 '<b>'.$ccuname.'</b>',$ccdomain).
1.215     raeburn  1417:                             '</span><br />';
                   1418:             }
1.292     bisitz   1419:             $response .= '<p class="LC_warning">'
                   1420:                         .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
1.418     raeburn  1421:                         .' ';
                   1422:             if ($context eq 'domain') {
                   1423:                 $response .= &mt('Please contact a [_1] for assistance.',
                   1424:                                  &Apache::lonnet::plaintext('dc'));
                   1425:             } else {
                   1426:                 $response .= &mt('Please contact the [_1]helpdesk[_2] for assistance.'
                   1427:                                 ,'<a href="'.$helplink.'">','</a>');
                   1428:             }
                   1429:             $response .= '</p><br />';
1.215     raeburn  1430:             $env{'form.phase'} = '';
1.439     raeburn  1431:             &print_username_entry_form($r,$context,$response,undef,undef,$crstype,$brcrum,
                   1432:                                        $permission);
1.215     raeburn  1433:             return;
                   1434:         }
1.188     raeburn  1435:         $newuser = 1;
1.193     raeburn  1436:         my $checkhash;
                   1437:         my $checks = { 'username' => 1 };
1.196     raeburn  1438:         $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
1.193     raeburn  1439:         &Apache::loncommon::user_rule_check($checkhash,$checks,
1.196     raeburn  1440:             \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
                   1441:         if (ref($alerts{'username'}) eq 'HASH') {
                   1442:             if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
                   1443:                 my $domdesc =
1.193     raeburn  1444:                     &Apache::lonnet::domain($ccdomain,'description');
1.196     raeburn  1445:                 if ($alerts{'username'}{$ccdomain}{$ccuname}) {
                   1446:                     my $userchkmsg;
                   1447:                     if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
                   1448:                         $userchkmsg = 
                   1449:                             &Apache::loncommon::instrule_disallow_msg('username',
1.193     raeburn  1450:                                                                  $domdesc,1).
                   1451:                         &Apache::loncommon::user_rule_formats($ccdomain,
                   1452:                             $domdesc,$curr_rules{$ccdomain}{'username'},
                   1453:                             'username');
1.196     raeburn  1454:                     }
1.215     raeburn  1455:                     $env{'form.phase'} = '';
1.439     raeburn  1456:                     &print_username_entry_form($r,$context,$userchkmsg,undef,undef,$crstype,$brcrum,
                   1457:                                                $permission);
1.196     raeburn  1458:                     return;
1.215     raeburn  1459:                 }
1.193     raeburn  1460:             }
1.185     raeburn  1461:         }
1.187     raeburn  1462:     } else {
1.188     raeburn  1463:         $newuser = 0;
1.185     raeburn  1464:     }
1.160     raeburn  1465:     if ($response) {
1.215     raeburn  1466:         $response = '<br />'.$response;
1.160     raeburn  1467:     }
1.149     raeburn  1468: 
1.52      matthew  1469:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.88      raeburn  1470:     my $dc_setcourse_code = '';
1.119     raeburn  1471:     my $nondc_setsection_code = '';                                        
1.112     albertel 1472:     my %loaditem;
1.114     albertel 1473: 
1.216     raeburn  1474:     my $groupslist = &Apache::lonuserutils::get_groupslist();
1.88      raeburn  1475: 
1.470     raeburn  1476:     my $js = &validation_javascript($context,$ccdomain,$pjump_def,
                   1477:                                     $crstype,$groupslist,$newuser,
                   1478:                                     $formname,\%loaditem,$permission);
1.422     raeburn  1479:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$ccdomain);
1.224     raeburn  1480:     my $helpitem = 'Course_Change_Privileges';
                   1481:     if ($env{'form.action'} eq 'singlestudent') {
                   1482:         $helpitem = 'Course_Add_Student';
1.439     raeburn  1483:     } elsif ($context eq 'author') {
                   1484:         $helpitem = 'Author_Change_Privileges';
                   1485:     } elsif ($context eq 'domain') {
                   1486:         $helpitem = 'Domain_Change_Privileges';
1.470     raeburn  1487:         $js .= &set_custom_js();
1.224     raeburn  1488:     }
1.351     raeburn  1489:     push (@{$brcrum},
                   1490:         {href => "javascript:backPage($form)",
                   1491:          text => $breadcrumb_text{'search'},
                   1492:          faq  => 282,
                   1493:          bug  => 'Instructor Interface',});
                   1494:     if ($env{'form.phase'} eq 'userpicked') {
                   1495:        push(@{$brcrum},
                   1496:               {href => "javascript:backPage($form,'get_user_info','select')",
                   1497:                text => $breadcrumb_text{'userpicked'},
                   1498:                faq  => 282,
                   1499:                bug  => 'Instructor Interface',});
                   1500:     }
                   1501:     push(@{$brcrum},
                   1502:             {href => "javascript:backPage($form,'$env{'form.phase'}','modify')",
                   1503:              text => $breadcrumb_text{'modify'},
                   1504:              faq  => 282,
                   1505:              bug  => 'Instructor Interface',
                   1506:              help => $helpitem});
                   1507:     my $args = {'add_entries'           => \%loaditem,
                   1508:                 'bread_crumbs'          => $brcrum,
                   1509:                 'bread_crumbs_component' => 'User Management'};
                   1510:     if ($env{'form.popup'}) {
                   1511:         $args->{'no_nav_bar'} = 1;
                   1512:     }
1.470     raeburn  1513:     if (($context eq 'domain') && ($env{'request.role.domain'} eq $ccdomain)) {
                   1514:         my @toggles;
                   1515:         if (&Apache::lonnet::allowed('cau',$ccdomain)) {
                   1516:             my ($isadv,$isauthor) =
                   1517:                 &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
                   1518:             unless ($isauthor) {
                   1519:                 push(@toggles,'requestauthor');
                   1520:             }
                   1521:             push(@toggles,('webdav','editors'));
                   1522:         }
                   1523:         if (&Apache::lonnet::allowed('mut',$ccdomain)) {
                   1524:             push(@toggles,('aboutme','blog','portfolio','portaccess','timezone'));
                   1525:         }
                   1526:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   1527:             push(@toggles,('official','unofficial','community','textbook','placement','lti'));
                   1528:         }
                   1529:         if (@toggles) {
                   1530:             my $onload;
                   1531:             foreach my $item (@toggles) {
                   1532:                 $onload .= "toggleCustom(document.cu,'customtext_$item','custom$item');";
                   1533:             }
                   1534:             $args->{'add_entries'} = {
                   1535:                                        'onload' => $onload,
                   1536:                                      };
                   1537:         }
                   1538:     }
1.351     raeburn  1539:     my $start_page =
                   1540:         &Apache::loncommon::start_page('User Management',$js,$args);
1.3       www      1541: 
1.25      matthew  1542:     my $forminfo =<<"ENDFORMINFO";
1.216     raeburn  1543: <form action="/adm/createuser" method="post" name="$formname">
1.190     raeburn  1544: <input type="hidden" name="phase" value="update_user_data" />
1.188     raeburn  1545: <input type="hidden" name="ccuname" value="$ccuname" />
                   1546: <input type="hidden" name="ccdomain" value="$ccdomain" />
1.157     albertel 1547: <input type="hidden" name="pres_value"  value="" />
                   1548: <input type="hidden" name="pres_type"   value="" />
                   1549: <input type="hidden" name="pres_marker" value="" />
1.25      matthew  1550: ENDFORMINFO
1.375     raeburn  1551:     my (%inccourses,$roledom,$defaultcredits);
1.329     raeburn  1552:     if ($context eq 'course') {
                   1553:         $inccourses{$env{'request.course.id'}}=1;
                   1554:         $roledom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.375     raeburn  1555:         if ($showcredits) {
                   1556:             $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1557:         }
1.329     raeburn  1558:     } elsif ($context eq 'author') {
                   1559:         $roledom = $env{'request.role.domain'};
                   1560:     } elsif ($context eq 'domain') {
                   1561:         foreach my $key (keys(%env)) {
                   1562:             $roledom = $env{'request.role.domain'};
                   1563:             if ($key=~/^user\.priv\.cm\.\/($roledom)\/($match_username)/) {
                   1564:                 $inccourses{$1.'_'.$2}=1;
                   1565:             }
                   1566:         }
                   1567:     } else {
                   1568:         foreach my $key (keys(%env)) {
                   1569: 	    if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
                   1570: 	        $inccourses{$1.'_'.$2}=1;
                   1571:             }
1.2       www      1572:         }
1.24      matthew  1573:     }
1.389     bisitz   1574:     my $title = '';
1.470     raeburn  1575:     my $need_quota_js;
1.216     raeburn  1576:     if ($newuser) {
1.427     raeburn  1577:         my ($portfolioform,$domroleform);
1.267     raeburn  1578:         if ((&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) ||
                   1579:             (&Apache::lonnet::allowed('mut',$env{'request.role.domain'}))) {
                   1580:             # Current user has quota or user tools modification privileges
1.470     raeburn  1581:             $portfolioform = '<br /><h3>'.
                   1582:                              &mt('User Tools').
                   1583:                              '</h3>'."\n".
                   1584:                              &Apache::loncommon::start_data_table();
                   1585:             if (&Apache::lonnet::allowed('mut',$ccdomain)) {
                   1586:                 $portfolioform .= &build_tools_display($ccuname,$ccdomain,'tools');
                   1587:             }
                   1588:             if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
                   1589:                 $portfolioform .= &user_quotas($ccuname,$ccdomain,'portfolio');
                   1590:                 $need_quota_js = 1;
                   1591:             }
                   1592:             $portfolioform .= &Apache::loncommon::end_data_table();
1.134     raeburn  1593:         }
1.383     raeburn  1594:         if ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) &&
                   1595:             ($ccdomain eq $env{'request.role.domain'})) {
1.470     raeburn  1596:             $domroleform = &domainrole_req($ccuname,$ccdomain).
                   1597:                            &authoring_defaults($ccuname,$ccdomain);
                   1598:             $need_quota_js = 1;
                   1599:         }
                   1600:         my $readonly;
                   1601:         unless ($permission->{'cusr'}) {
                   1602:             $readonly = 1;
1.362     raeburn  1603:         }
1.470     raeburn  1604:         &initialize_authen_forms($ccdomain,$formname,'','',$readonly);
1.188     raeburn  1605:         my %lt=&Apache::lonlocal::texthash(
                   1606:                 'lg'             => 'Login Data',
1.190     raeburn  1607:                 'hs'             => "Home Server",
1.188     raeburn  1608:         );
1.185     raeburn  1609: 	$r->print(<<ENDTITLE);
1.110     albertel 1610: $start_page
1.160     raeburn  1611: $response
1.25      matthew  1612: $forminfo
1.31      matthew  1613: <script type="text/javascript" language="Javascript">
1.301     bisitz   1614: // <![CDATA[
1.20      harris41 1615: $loginscript
1.301     bisitz   1616: // ]]>
1.31      matthew  1617: </script>
1.20      harris41 1618: <input type='hidden' name='makeuser' value='1' />
1.185     raeburn  1619: ENDTITLE
1.213     raeburn  1620:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1621:             if ($crstype eq 'Community') {
1.389     bisitz   1622:                 $title = &mt('Create New User [_1] in domain [_2] as a member',
                   1623:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1624:             } else {
1.389     bisitz   1625:                 $title = &mt('Create New User [_1] in domain [_2] as a student',
                   1626:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1627:             }
1.389     bisitz   1628:         } else {
                   1629:                 $title = &mt('Create New User [_1] in domain [_2]',
                   1630:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.213     raeburn  1631:         }
1.389     bisitz   1632:         $r->print('<h2>'.$title.'</h2>'."\n");
                   1633:         $r->print('<div class="LC_left_float">');
1.393     raeburn  1634:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
1.470     raeburn  1635:                                          $inst_results{$ccuname.':'.$ccdomain},$readonly));
1.393     raeburn  1636:         # Option to disable student/employee ID conflict checking not offerred for new users.
1.187     raeburn  1637:         my ($home_server_pick,$numlib) = 
                   1638:             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
                   1639:                                                       'default','hide');
                   1640:         if ($numlib > 1) {
                   1641:             $r->print("
1.185     raeburn  1642: <br />
1.187     raeburn  1643: $lt{'hs'}: $home_server_pick
                   1644: <br />");
                   1645:         } else {
                   1646:             $r->print($home_server_pick);
                   1647:         }
1.304     raeburn  1648:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.362     raeburn  1649:             $r->print('<br /><h3>'.
1.470     raeburn  1650:                       &mt('Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.304     raeburn  1651:                       &Apache::loncommon::start_data_table().
                   1652:                       &build_tools_display($ccuname,$ccdomain,
                   1653:                                            'requestcourses').
                   1654:                       &Apache::loncommon::end_data_table());
                   1655:         }
1.188     raeburn  1656:         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                   1657:                   $lt{'lg'}.'</h3>');
1.185     raeburn  1658:         my ($fixedauth,$varauth,$authmsg); 
1.193     raeburn  1659:         if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
                   1660:             my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
                   1661:             my ($rules,$ruleorder) = 
                   1662:                 &Apache::lonnet::inst_userrules($ccdomain,'username');
1.185     raeburn  1663:             if (ref($rules) eq 'HASH') {
1.193     raeburn  1664:                 if (ref($rules->{$matchedrule}) eq 'HASH') {
                   1665:                     my $authtype = $rules->{$matchedrule}{'authtype'};
1.185     raeburn  1666:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
1.190     raeburn  1667:                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.275     raeburn  1668:                     } else { 
1.193     raeburn  1669:                         my $authparm = $rules->{$matchedrule}{'authparm'};
1.273     raeburn  1670:                         $authmsg = $rules->{$matchedrule}{'authmsg'};
1.185     raeburn  1671:                         if ($authtype =~ /^krb(4|5)$/) {
                   1672:                             my $ver = $1;
                   1673:                             if ($authparm ne '') {
                   1674:                                 $fixedauth = <<"KERB"; 
                   1675: <input type="hidden" name="login" value="krb" />
                   1676: <input type="hidden" name="krbver" value="$ver" />
                   1677: <input type="hidden" name="krbarg" value="$authparm" />
                   1678: KERB
                   1679:                             }
                   1680:                         } else {
                   1681:                             $fixedauth = 
                   1682: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
1.193     raeburn  1683:                             if ($rules->{$matchedrule}{'authparmfixed'}) {
1.185     raeburn  1684:                                 $fixedauth .=    
                   1685: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
                   1686:                             } else {
1.273     raeburn  1687:                                 if ($authtype eq 'int') {
                   1688:                                     $varauth = '<br />'.
1.301     bisitz   1689: &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  1690:                                 } elsif ($authtype eq 'loc') {
                   1691:                                     $varauth = '<br />'.
                   1692: &mt('[_1] Local Authentication with argument [_2]','','<input type="text" name="'.$authtype.'arg" value="" />')."\n";
                   1693:                                 } else {
                   1694:                                     $varauth =
1.185     raeburn  1695: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
1.273     raeburn  1696:                                 }
1.185     raeburn  1697:                             }
                   1698:                         }
                   1699:                     }
                   1700:                 } else {
1.190     raeburn  1701:                     $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.185     raeburn  1702:                 }
                   1703:             }
                   1704:             if ($authmsg) {
                   1705:                 $r->print(<<ENDAUTH);
                   1706: $fixedauth
                   1707: $authmsg
                   1708: $varauth
                   1709: ENDAUTH
                   1710:             }
                   1711:         } else {
1.190     raeburn  1712:             $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc)); 
1.187     raeburn  1713:         }
1.427     raeburn  1714:         $r->print($portfolioform.$domroleform);
1.215     raeburn  1715:         if ($env{'form.action'} eq 'singlestudent') {
                   1716:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1717:                                             $permission,$crstype,$ccuname,
                   1718:                                             $ccdomain,$showcredits));
1.215     raeburn  1719:         }
                   1720:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.216     raeburn  1721:     } else { # user already exists
1.389     bisitz   1722: 	$r->print($start_page.$forminfo);
1.213     raeburn  1723:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1724:             if ($crstype eq 'Community') {
1.389     bisitz   1725:                 $title = &mt('Enroll one member: [_1] in domain [_2]',
                   1726:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1727:             } else {
1.389     bisitz   1728:                 $title = &mt('Enroll one student: [_1] in domain [_2]',
                   1729:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1730:             }
1.213     raeburn  1731:         } else {
1.418     raeburn  1732:             if ($permission->{'cusr'}) {
                   1733:                 $title = &mt('Modify existing user: [_1] in domain [_2]',
                   1734:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
                   1735:             } else {
                   1736:                 $title = &mt('Existing user: [_1] in domain [_2]',
1.389     bisitz   1737:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
1.418     raeburn  1738:             }
1.213     raeburn  1739:         }
1.389     bisitz   1740:         $r->print('<h2>'.$title.'</h2>'."\n");
                   1741:         $r->print('<div class="LC_left_float">');
1.393     raeburn  1742:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1743:                                          $inst_results{$ccuname.':'.$ccdomain}));
1.430     raeburn  1744:         if ((&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) ||
1.418     raeburn  1745:             (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) {
1.470     raeburn  1746:             $r->print('<br /><h3>'.&mt('Can Request Creation of Courses/Communities in this Domain?').'</h3>'."\n");
1.450     raeburn  1747:             if (($env{'request.role.domain'} eq $ccdomain) ||
                   1748:                 (&Apache::lonnet::will_trust('reqcrs',$ccdomain,$env{'request.role.domain'}))) {
                   1749:                 $r->print(&Apache::loncommon::start_data_table());
                   1750:                 if ($env{'request.role.domain'} eq $ccdomain) {
                   1751:                     $r->print(&build_tools_display($ccuname,$ccdomain,'requestcourses'));
                   1752:                 } else {
1.444     raeburn  1753:                     $r->print(&coursereq_externaluser($ccuname,$ccdomain,
                   1754:                                                       $env{'request.role.domain'}));
                   1755:                 }
1.450     raeburn  1756:                 $r->print(&Apache::loncommon::end_data_table());
                   1757:             } else {
                   1758:                 $r->print(&mt('Domain configuration for this domain prohibits course creation by users from domain: "[_1]"',
                   1759:                               &Apache::lonnet::domain($ccdomain,'description')));
1.300     raeburn  1760:             }
1.275     raeburn  1761:         }
1.199     raeburn  1762:         $r->print('</div>');
1.470     raeburn  1763:         my @order = ('auth','quota','tools','requestauthor','authordefaults');
1.362     raeburn  1764:         my %user_text;
                   1765:         my ($isadv,$isauthor) = 
1.418     raeburn  1766:             &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
1.470     raeburn  1767:         if (((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) ||
1.418     raeburn  1768:              (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) &&
1.470     raeburn  1769:             ($env{'request.role.domain'} eq $ccdomain)) {
                   1770:             if (!$isauthor) {
                   1771:                 $user_text{'requestauthor'} = &domainrole_req($ccuname,$ccdomain);
                   1772:             }
                   1773:             $user_text{'authordefaults'} = &authoring_defaults($ccuname,$ccdomain);
                   1774:             if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
                   1775:                 $need_quota_js = 1;
                   1776:             }
1.362     raeburn  1777:         }
1.451     raeburn  1778:         $user_text{'auth'} =  &user_authentication($ccuname,$ccdomain,$formname,$crstype,$permission);
1.267     raeburn  1779:         if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
1.418     raeburn  1780:             (&Apache::lonnet::allowed('mut',$ccdomain)) ||
                   1781:             (&Apache::lonnet::allowed('udp',$ccdomain))) {
1.470     raeburn  1782:             $user_text{'quota'} = '<br /><h3>'.&mt('User Tools').'</h3>'."\n".
                   1783:                                   &Apache::loncommon::start_data_table();
                   1784:             if ((&Apache::lonnet::allowed('mut',$ccdomain)) ||
                   1785:                 (&Apache::lonnet::allowed('udp',$ccdomain))) {
                   1786:                 $user_text{'quota'} .= &build_tools_display($ccuname,$ccdomain,'tools');
                   1787:             }
1.188     raeburn  1788:             # Current user has quota modification privileges
1.470     raeburn  1789:             if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
                   1790:                 (&Apache::lonnet::allowed('udp',$ccdomain))) {
                   1791:                 $user_text{'quota'} .= &user_quotas($ccuname,$ccdomain,'portfolio');
                   1792:                 $need_quota_js = 1;
                   1793:             }
                   1794:             $user_text{'quota'} .= &Apache::loncommon::end_data_table();
1.267     raeburn  1795:         }
                   1796:         if (!&Apache::lonnet::allowed('mpq',$ccdomain)) {
                   1797:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
                   1798:                 my %lt=&Apache::lonlocal::texthash(
1.470     raeburn  1799:                     'dska'  => "Disk quotas for user's portfolio",
                   1800:                     'youd'  => "You do not have privileges to modify the portfolio quota for this user.",
1.267     raeburn  1801:                     'ichr'  => "If a change is required, contact a domain coordinator for the domain",
                   1802:                 );
1.362     raeburn  1803:                 $user_text{'quota'} = <<ENDNOPORTPRIV;
1.188     raeburn  1804: <h3>$lt{'dska'}</h3>
                   1805: $lt{'youd'} $lt{'ichr'}: $ccdomain
                   1806: ENDNOPORTPRIV
1.267     raeburn  1807:             }
                   1808:         }
                   1809:         if (!&Apache::lonnet::allowed('mut',$ccdomain)) {
                   1810:             if (&Apache::lonnet::allowed('mut',$env{'request.role.domain'})) {
                   1811:                 my %lt=&Apache::lonlocal::texthash(
                   1812:                     'utav'  => "User Tools Availability",
1.470     raeburn  1813:                     'yodo'  => "You do not have privileges to modify Portfolio, Blog, Personal Information Page, or Time Zone settings for this user.",
1.267     raeburn  1814:                     'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1815:                 );
1.362     raeburn  1816:                 $user_text{'tools'} = <<ENDNOTOOLSPRIV;
1.267     raeburn  1817: <h3>$lt{'utav'}</h3>
                   1818: $lt{'yodo'} $lt{'ifch'}: $ccdomain
                   1819: ENDNOTOOLSPRIV
                   1820:             }
1.188     raeburn  1821:         }
1.362     raeburn  1822:         my $gotdiv = 0; 
                   1823:         foreach my $item (@order) {
                   1824:             if ($user_text{$item} ne '') {
                   1825:                 unless ($gotdiv) {
                   1826:                     $r->print('<div class="LC_left_float">');
                   1827:                     $gotdiv = 1;
                   1828:                 }
                   1829:                 $r->print('<br />'.$user_text{$item});
                   1830:             }
                   1831:         }
                   1832:         if ($env{'form.action'} eq 'singlestudent') {
                   1833:             unless ($gotdiv) {
                   1834:                 $r->print('<div class="LC_left_float">');
1.213     raeburn  1835:             }
1.375     raeburn  1836:             my $credits;
                   1837:             if ($showcredits) {
                   1838:                 $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1839:                 if ($credits eq '') {
                   1840:                     $credits = $defaultcredits;
                   1841:                 }
                   1842:             }
1.374     raeburn  1843:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1844:                                             $permission,$crstype,$ccuname,
                   1845:                                             $ccdomain,$showcredits));
1.374     raeburn  1846:         }
1.362     raeburn  1847:         if ($gotdiv) {
                   1848:             $r->print('</div><div class="LC_clear_float_footer"></div>');
1.188     raeburn  1849:         }
1.418     raeburn  1850:         my $statuses;
                   1851:         if (($context eq 'domain') && (&Apache::lonnet::allowed('udp',$ccdomain)) &&
                   1852:             (!&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1853:             $statuses = ['active'];
                   1854:         } elsif (($context eq 'course') && ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
                   1855:                  ($env{'request.course.sec'} &&
                   1856:                   &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'})))) {
1.430     raeburn  1857:             $statuses = ['active'];
1.418     raeburn  1858:         }
1.217     raeburn  1859:         if ($env{'form.action'} ne 'singlestudent') {
1.329     raeburn  1860:             &display_existing_roles($r,$ccuname,$ccdomain,\%inccourses,$context,
1.418     raeburn  1861:                                     $roledom,$crstype,$showcredits,$statuses);
1.217     raeburn  1862:         }
1.25      matthew  1863:     } ## End of new user/old user logic
1.218     raeburn  1864:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1865:         my $btntxt;
                   1866:         if ($crstype eq 'Community') {
                   1867:             $btntxt = &mt('Enroll Member');
                   1868:         } else {
                   1869:             $btntxt = &mt('Enroll Student');
                   1870:         }
                   1871:         $r->print('<br /><input type="button" value="'.$btntxt.'" onclick="setSections(this.form)" />'."\n");
1.418     raeburn  1872:     } elsif ($permission->{'cusr'}) {
1.393     raeburn  1873:         $r->print('<div class="LC_left_float">'.
                   1874:                   '<fieldset><legend>'.&mt('Add Roles').'</legend>');
1.218     raeburn  1875:         my $addrolesdisplay = 0;
                   1876:         if ($context eq 'domain' || $context eq 'author') {
                   1877:             $addrolesdisplay = &new_coauthor_roles($r,$ccuname,$ccdomain);
                   1878:         }
                   1879:         if ($context eq 'domain') {
1.357     raeburn  1880:             my $add_domainroles = &new_domain_roles($r,$ccdomain);
1.218     raeburn  1881:             if (!$addrolesdisplay) {
                   1882:                 $addrolesdisplay = $add_domainroles;
1.2       www      1883:             }
1.375     raeburn  1884:             $r->print(&course_level_dc($env{'request.role.domain'},$showcredits));
1.393     raeburn  1885:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1886:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setCourse()" />'."\n");
1.218     raeburn  1887:         } elsif ($context eq 'author') {
                   1888:             if ($addrolesdisplay) {
1.393     raeburn  1889:                 $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1890:                           '<br /><input type="button" value="'.&mt('Save').'"');
1.218     raeburn  1891:                 if ($newuser) {
1.301     bisitz   1892:                     $r->print(' onclick="auth_check()" \>'."\n");
1.218     raeburn  1893:                 } else {
1.461     raeburn  1894:                     $r->print(' onclick="this.form.submit()" \>'."\n");
1.218     raeburn  1895:                 }
1.188     raeburn  1896:             } else {
1.393     raeburn  1897:                 $r->print('</fieldset></div>'.
                   1898:                           '<div class="LC_clear_float_footer"></div>'.
                   1899:                           '<br /><a href="javascript:backPage(document.cu)">'.
1.218     raeburn  1900:                           &mt('Back to previous page').'</a>');
1.188     raeburn  1901:             }
                   1902:         } else {
1.375     raeburn  1903:             $r->print(&course_level_table(\%inccourses,$showcredits,$defaultcredits));
1.393     raeburn  1904:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1905:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setSections(this.form)" />'."\n");
1.188     raeburn  1906:         }
1.88      raeburn  1907:     }
1.188     raeburn  1908:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
1.179     raeburn  1909:     $r->print('<input type="hidden" name="currstate" value="" />');
1.393     raeburn  1910:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" /></form><br /><br />');
1.470     raeburn  1911:     if ($need_quota_js) {
                   1912:         $r->print(&user_quota_js());
                   1913:     }
1.218     raeburn  1914:     return;
1.2       www      1915: }
1.1       www      1916: 
1.213     raeburn  1917: sub singleuser_breadcrumb {
1.422     raeburn  1918:     my ($crstype,$context,$domain) = @_;
1.213     raeburn  1919:     my %breadcrumb_text;
                   1920:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1921:         if ($crstype eq 'Community') {
                   1922:             $breadcrumb_text{'search'} = 'Enroll a member';
                   1923:         } else {
                   1924:             $breadcrumb_text{'search'} = 'Enroll a student';
                   1925:         }
1.422     raeburn  1926:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1927:         $breadcrumb_text{'modify'} = 'Set section/dates';
1.416     raeburn  1928:     } elsif ($env{'form.action'} eq 'accesslogs') {
                   1929:         $breadcrumb_text{'search'} = 'View access logs for a user';
1.422     raeburn  1930:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1931:         $breadcrumb_text{'activity'} = 'Activity';
                   1932:     } elsif (($env{'form.action'} eq 'singleuser') && ($context eq 'domain') &&
                   1933:              (!&Apache::lonnet::allowed('mau',$domain))) {
                   1934:         $breadcrumb_text{'search'} = "View user's roles";
                   1935:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1936:         $breadcrumb_text{'modify'} = 'User roles';
1.213     raeburn  1937:     } else {
1.229     raeburn  1938:         $breadcrumb_text{'search'} = 'Create/modify a user';
1.422     raeburn  1939:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1940:         $breadcrumb_text{'modify'} = 'Set user role';
1.213     raeburn  1941:     }
                   1942:     return %breadcrumb_text;
                   1943: }
                   1944: 
                   1945: sub date_sections_select {
1.375     raeburn  1946:     my ($context,$newuser,$formname,$permission,$crstype,$ccuname,$ccdomain,
                   1947:         $showcredits) = @_;
                   1948:     my $credits;
                   1949:     if ($showcredits) {
                   1950:         my $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1951:         $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1952:         if ($credits eq '') {
                   1953:             $credits = $defaultcredits;
                   1954:         }
                   1955:     }
1.213     raeburn  1956:     my $cid = $env{'request.course.id'};
                   1957:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity($cid);
                   1958:     my $date_table = '<h3>'.&mt('Starting and Ending Dates').'</h3>'."\n".
                   1959:         &Apache::lonuserutils::date_setting_table(undef,undef,$context,
                   1960:                                                   undef,$formname,$permission);
                   1961:     my $rowtitle = 'Section';
1.375     raeburn  1962:     my $secbox = '<h3>'.&mt('Section and Credits').'</h3>'."\n".
1.213     raeburn  1963:         &Apache::lonuserutils::section_picker($cdom,$cnum,'st',$rowtitle,
1.375     raeburn  1964:                                               $permission,$context,'',$crstype,
                   1965:                                               $showcredits,$credits);
1.213     raeburn  1966:     my $output = $date_table.$secbox;
                   1967:     return $output;
                   1968: }
                   1969: 
1.216     raeburn  1970: sub validation_javascript {
1.375     raeburn  1971:     my ($context,$ccdomain,$pjump_def,$crstype,$groupslist,$newuser,$formname,
1.470     raeburn  1972:         $loaditem,$permission) = @_;
1.216     raeburn  1973:     my $dc_setcourse_code = '';
                   1974:     my $nondc_setsection_code = '';
                   1975:     if ($context eq 'domain') {
1.470     raeburn  1976:         if ((ref($permission) eq 'HASH') && ($permission->{'cusr'})) {
                   1977:             my $dcdom = $env{'request.role.domain'};
                   1978:             $loaditem->{'onload'} = "document.cu.coursedesc.value='';";
                   1979:             $dc_setcourse_code =
                   1980:                 &Apache::lonuserutils::dc_setcourse_js('cu','singleuser',$context);
                   1981:         }
1.216     raeburn  1982:     } else {
1.227     raeburn  1983:         my $checkauth; 
                   1984:         if (($newuser) || (&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1985:             $checkauth = 1;
                   1986:         }
                   1987:         if ($context eq 'course') {
                   1988:             $nondc_setsection_code =
                   1989:                 &Apache::lonuserutils::setsections_javascript($formname,$groupslist,
1.375     raeburn  1990:                                                               undef,$checkauth,
                   1991:                                                               $crstype);
1.227     raeburn  1992:         }
                   1993:         if ($checkauth) {
                   1994:             $nondc_setsection_code .= 
                   1995:                 &Apache::lonuserutils::verify_authen($formname,$context);
                   1996:         }
1.216     raeburn  1997:     }
                   1998:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                   1999:                                    $nondc_setsection_code,$groupslist);
                   2000:     my ($jsback,$elements) = &crumb_utilities();
                   2001:     $js .= "\n".
1.301     bisitz   2002:            '<script type="text/javascript">'."\n".
                   2003:            '// <![CDATA['."\n".
                   2004:            $jsback."\n".
                   2005:            '// ]]>'."\n".
                   2006:            '</script>'."\n";
1.216     raeburn  2007:     return $js;
                   2008: }
                   2009: 
1.217     raeburn  2010: sub display_existing_roles {
1.375     raeburn  2011:     my ($r,$ccuname,$ccdomain,$inccourses,$context,$roledom,$crstype,
1.418     raeburn  2012:         $showcredits,$statuses) = @_;
1.329     raeburn  2013:     my $now=time;
1.418     raeburn  2014:     my $showall = 1;
                   2015:     my ($showexpired,$showactive);
                   2016:     if ((ref($statuses) eq 'ARRAY') && (@{$statuses} > 0)) {
                   2017:         $showall = 0;
                   2018:         if (grep(/^expired$/,@{$statuses})) {
                   2019:             $showexpired = 1;
                   2020:         }
                   2021:         if (grep(/^active$/,@{$statuses})) {
                   2022:             $showactive = 1;
                   2023:         }
                   2024:         if ($showexpired && $showactive) {
                   2025:             $showall = 1;
                   2026:         }
                   2027:     }
1.329     raeburn  2028:     my %lt=&Apache::lonlocal::texthash(
1.217     raeburn  2029:                     'rer'  => "Existing Roles",
                   2030:                     'rev'  => "Revoke",
                   2031:                     'del'  => "Delete",
                   2032:                     'ren'  => "Re-Enable",
                   2033:                     'rol'  => "Role",
                   2034:                     'ext'  => "Extent",
1.375     raeburn  2035:                     'crd'  => "Credits",
1.217     raeburn  2036:                     'sta'  => "Start",
                   2037:                     'end'  => "End",
                   2038:                                        );
1.329     raeburn  2039:     my (%rolesdump,%roletext,%sortrole,%roleclass,%rolepriv);
                   2040:     if ($context eq 'course' || $context eq 'author') {
                   2041:         my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
                   2042:         my %roleshash = 
                   2043:             &Apache::lonnet::get_my_roles($ccuname,$ccdomain,'userroles',
                   2044:                               ['active','previous','future'],\@roles,$roledom,1);
                   2045:         foreach my $key (keys(%roleshash)) {
                   2046:             my ($start,$end) = split(':',$roleshash{$key});
                   2047:             next if ($start eq '-1' || $end eq '-1');
                   2048:             my ($rnum,$rdom,$role,$sec) = split(':',$key);
                   2049:             if ($context eq 'course') {
                   2050:                 next unless (($rnum eq $env{'course.'.$env{'request.course.id'}.'.num'})
                   2051:                              && ($rdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}));
                   2052:             } elsif ($context eq 'author') {
1.470     raeburn  2053:                 if ($env{'request.role'} =~ m{^ca\./($match_domain)/($match_username)$}) {
                   2054:                     my ($audom,$auname) = ($1,$2);
                   2055:                     next unless (($rnum eq $auname) && ($rdom eq $audom));
                   2056:                 } else {
                   2057:                     next unless (($rnum eq $env{'user.name'}) && ($rdom eq $env{'request.role.domain'}));
                   2058:                 }
1.329     raeburn  2059:             }
                   2060:             my ($newkey,$newvalue,$newrole);
                   2061:             $newkey = '/'.$rdom.'/'.$rnum;
                   2062:             if ($sec ne '') {
                   2063:                 $newkey .= '/'.$sec;
                   2064:             }
                   2065:             $newvalue = $role;
                   2066:             if ($role =~ /^cr/) {
                   2067:                 $newrole = 'cr';
                   2068:             } else {
                   2069:                 $newrole = $role;
                   2070:             }
                   2071:             $newkey .= '_'.$newrole;
                   2072:             if ($start ne '' && $end ne '') {
                   2073:                 $newvalue .= '_'.$end.'_'.$start;
1.335     raeburn  2074:             } elsif ($end ne '') {
                   2075:                 $newvalue .= '_'.$end;
1.329     raeburn  2076:             }
                   2077:             $rolesdump{$newkey} = $newvalue;
                   2078:         }
                   2079:     } else {
1.360     raeburn  2080:         %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
1.329     raeburn  2081:     }
                   2082:     # Build up table of user roles to allow revocation and re-enabling of roles.
                   2083:     my ($tmp) = keys(%rolesdump);
                   2084:     return if ($tmp =~ /^(con_lost|error)/i);
                   2085:     foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
                   2086:                                 my $b1=join('_',(split('_',$b))[1,0]);
                   2087:                                 return $a1 cmp $b1;
                   2088:                             } keys(%rolesdump)) {
                   2089:         next if ($area =~ /^rolesdef/);
                   2090:         my $envkey=$area;
                   2091:         my $role = $rolesdump{$area};
                   2092:         my $thisrole=$area;
                   2093:         $area =~ s/\_\w\w$//;
                   2094:         my ($role_code,$role_end_time,$role_start_time) =
                   2095:             split(/_/,$role);
1.418     raeburn  2096:         my $active=1;
                   2097:         $active=0 if (($role_end_time) && ($now>$role_end_time));
                   2098:         if ($active) {
                   2099:             next unless($showall || $showactive);
                   2100:         } else {
1.430     raeburn  2101:             next unless($showall || $showexpired);
1.418     raeburn  2102:         }
1.217     raeburn  2103: # Is this a custom role? Get role owner and title.
1.329     raeburn  2104:         my ($croleudom,$croleuname,$croletitle)=
                   2105:             ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
                   2106:         my $allowed=0;
                   2107:         my $delallowed=0;
                   2108:         my $sortkey=$role_code;
                   2109:         my $class='Unknown';
1.375     raeburn  2110:         my $credits='';
1.418     raeburn  2111:         my $csec;
1.421     raeburn  2112:         if ($area =~ m{^/($match_domain)/($match_courseid)}) {
1.329     raeburn  2113:             $class='Course';
                   2114:             my ($coursedom,$coursedir) = ($1,$2);
                   2115:             my $cid = $1.'_'.$2;
                   2116:             # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
1.421     raeburn  2117:             next if ($envkey =~ m{^/$match_domain/$match_courseid/[A-Za-z0-9]+_gr$});
1.329     raeburn  2118:             my %coursedata=
                   2119:                 &Apache::lonnet::coursedescription($cid);
                   2120:             if ($coursedir =~ /^$match_community$/) {
                   2121:                 $class='Community';
                   2122:             }
                   2123:             $sortkey.="\0$coursedom";
                   2124:             my $carea;
                   2125:             if (defined($coursedata{'description'})) {
                   2126:                 $carea=$coursedata{'description'}.
                   2127:                     '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
                   2128:     &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$coursedir,$coursedom);
                   2129:                 $sortkey.="\0".$coursedata{'description'};
                   2130:             } else {
                   2131:                 if ($class eq 'Community') {
                   2132:                     $carea=&mt('Unavailable community').': '.$area;
                   2133:                     $sortkey.="\0".&mt('Unavailable community').': '.$area;
1.217     raeburn  2134:                 } else {
                   2135:                     $carea=&mt('Unavailable course').': '.$area;
                   2136:                     $sortkey.="\0".&mt('Unavailable course').': '.$area;
                   2137:                 }
1.329     raeburn  2138:             }
                   2139:             $sortkey.="\0$coursedir";
                   2140:             $inccourses->{$cid}=1;
1.375     raeburn  2141:             if (($showcredits) && ($class eq 'Course') && ($role_code eq 'st')) {
                   2142:                 my $defaultcredits = $coursedata{'internal.defaultcredits'};
                   2143:                 $credits =
                   2144:                     &get_user_credits($ccuname,$ccdomain,$defaultcredits,
                   2145:                                       $coursedom,$coursedir);
                   2146:                 if ($credits eq '') {
                   2147:                     $credits = $defaultcredits;
                   2148:                 }
                   2149:             }
1.329     raeburn  2150:             if ((&Apache::lonnet::allowed('c'.$role_code,$coursedom.'/'.$coursedir)) ||
                   2151:                 (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   2152:                 $allowed=1;
                   2153:             }
                   2154:             unless ($allowed) {
1.365     raeburn  2155:                 my $isowner = &Apache::lonuserutils::is_courseowner($cid,$coursedata{'internal.courseowner'});
1.329     raeburn  2156:                 if ($isowner) {
                   2157:                     if (($role_code eq 'co') && ($class eq 'Community')) {
                   2158:                         $allowed = 1;
                   2159:                     } elsif (($role_code eq 'cc') && ($class eq 'Course')) {
                   2160:                         $allowed = 1;
                   2161:                     }
1.217     raeburn  2162:                 }
1.329     raeburn  2163:             } 
                   2164:             if ((&Apache::lonnet::allowed('dro',$coursedom)) ||
                   2165:                 (&Apache::lonnet::allowed('dro',$ccdomain))) {
                   2166:                 $delallowed=1;
                   2167:             }
1.217     raeburn  2168: # - custom role. Needs more info, too
1.329     raeburn  2169:             if ($croletitle) {
                   2170:                 if (&Apache::lonnet::allowed('ccr',$coursedom.'/'.$coursedir)) {
                   2171:                     $allowed=1;
                   2172:                     $thisrole.='.'.$role_code;
1.217     raeburn  2173:                 }
1.329     raeburn  2174:             }
1.418     raeburn  2175:             if ($area=~m{^/($match_domain/$match_courseid/(\w+))}) {
                   2176:                 $csec = $2;
                   2177:                 $carea.='<br />'.&mt('Section: [_1]',$csec);
                   2178:                 $sortkey.="\0$csec";
1.329     raeburn  2179:                 if (!$allowed) {
1.418     raeburn  2180:                     if ($env{'request.course.sec'} eq $csec) {
                   2181:                         if (&Apache::lonnet::allowed('c'.$role_code,$1)) {
1.329     raeburn  2182:                             $allowed = 1;
1.217     raeburn  2183:                         }
                   2184:                     }
                   2185:                 }
1.329     raeburn  2186:             }
                   2187:             $area=$carea;
                   2188:         } else {
                   2189:             $sortkey.="\0".$area;
                   2190:             # Determine if current user is able to revoke privileges
                   2191:             if ($area=~m{^/($match_domain)/}) {
                   2192:                 if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
                   2193:                    (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   2194:                    $allowed=1;
1.217     raeburn  2195:                 }
1.329     raeburn  2196:                 if (((&Apache::lonnet::allowed('dro',$1))  ||
                   2197:                     (&Apache::lonnet::allowed('dro',$ccdomain))) &&
                   2198:                     ($role_code ne 'dc')) {
                   2199:                     $delallowed=1;
1.217     raeburn  2200:                 }
1.329     raeburn  2201:             } else {
                   2202:                 if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
1.217     raeburn  2203:                     $allowed=1;
                   2204:                 }
                   2205:             }
1.363     raeburn  2206:             if ($role_code eq 'ca' || $role_code eq 'au' || $role_code eq 'aa') {
1.377     raeburn  2207:                 $class='Authoring Space';
1.329     raeburn  2208:             } elsif ($role_code eq 'su') {
                   2209:                 $class='System';
1.217     raeburn  2210:             } else {
1.329     raeburn  2211:                 $class='Domain';
1.217     raeburn  2212:             }
1.329     raeburn  2213:         }
                   2214:         if (($role_code eq 'ca') || ($role_code eq 'aa')) {
                   2215:             $area=~m{/($match_domain)/($match_username)};
                   2216:             if (&Apache::lonuserutils::authorpriv($2,$1)) {
                   2217:                 $allowed=1;
1.470     raeburn  2218:             } elsif (&Apache::lonuserutils::coauthorpriv($2,$1)) {
                   2219:                 $allowed=1;
1.217     raeburn  2220:             } else {
1.329     raeburn  2221:                 $allowed=0;
1.217     raeburn  2222:             }
1.329     raeburn  2223:         }
                   2224:         my $row = '';
1.418     raeburn  2225:         if ($showall) {
                   2226:             $row.= '<td>';
                   2227:             if (($active) && ($allowed)) {
                   2228:                 $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
                   2229:             } else {
                   2230:                 if ($active) {
                   2231:                     $row.='&nbsp;';
                   2232:                 } else {
                   2233:                     $row.=&mt('expired or revoked');
                   2234:                 }
                   2235:             }
                   2236:             $row.='</td><td>';
                   2237:             if ($allowed && !$active) {
                   2238:                 $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
                   2239:             } else {
                   2240:                 $row.='&nbsp;';
                   2241:             }
                   2242:             $row.='</td><td>';
                   2243:             if ($delallowed) {
                   2244:                 $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
1.217     raeburn  2245:             } else {
1.418     raeburn  2246:                 $row.='&nbsp;';
1.217     raeburn  2247:             }
1.430     raeburn  2248:             $row.= '</td>';
1.329     raeburn  2249:         }
                   2250:         my $plaintext='';
                   2251:         if (!$croletitle) {
1.375     raeburn  2252:             $plaintext=&Apache::lonnet::plaintext($role_code,$class);
                   2253:             if (($showcredits) && ($credits ne '')) {
                   2254:                 $plaintext .= '<br/ ><span class="LC_nobreak">'.
                   2255:                               '<span class="LC_fontsize_small">'.
                   2256:                               &mt('Credits: [_1]',$credits).
                   2257:                               '</span></span>';
                   2258:             }
1.329     raeburn  2259:         } else {
                   2260:             $plaintext=
1.395     bisitz   2261:                 &mt('Custom role [_1][_2]defined by [_3]',
1.346     bisitz   2262:                         '"'.$croletitle.'"',
                   2263:                         '<br />',
                   2264:                         $croleuname.':'.$croleudom);
1.329     raeburn  2265:         }
1.418     raeburn  2266:         $row.= '<td>'.$plaintext.'</td>'.
                   2267:                '<td>'.$area.'</td>'.
                   2268:                '<td>'.($role_start_time?&Apache::lonlocal::locallocaltime($role_start_time)
                   2269:                                             : '&nbsp;' ).'</td>'.
                   2270:                '<td>'.($role_end_time  ?&Apache::lonlocal::locallocaltime($role_end_time)
                   2271:                                             : '&nbsp;' ).'</td>';
1.329     raeburn  2272:         $sortrole{$sortkey}=$envkey;
                   2273:         $roletext{$envkey}=$row;
                   2274:         $roleclass{$envkey}=$class;
1.418     raeburn  2275:         if ($allowed) {
                   2276:             $rolepriv{$envkey}='edit';
                   2277:         } else {
                   2278:             if ($context eq 'domain') {
1.420     raeburn  2279:                 if ((&Apache::lonnet::allowed('vur',$ccdomain)) &&
1.421     raeburn  2280:                     ($envkey=~m{^/$ccdomain/})) {
1.418     raeburn  2281:                     $rolepriv{$envkey}='view';
                   2282:                 }
                   2283:             } elsif ($context eq 'course') {
                   2284:                 if ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
                   2285:                     ($env{'request.course.sec'} && ($env{'request.course.sec'} eq $csec) &&
                   2286:                      &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'}))) {
                   2287:                     $rolepriv{$envkey}='view';
                   2288:                 }
                   2289:             }
                   2290:         }
1.329     raeburn  2291:     } # end of foreach        (table building loop)
                   2292: 
                   2293:     my $rolesdisplay = 0;
                   2294:     my %output = ();
1.377     raeburn  2295:     foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  2296:         $output{$type} = '';
                   2297:         foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                   2298:             if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) {
                   2299:                  $output{$type}.=
                   2300:                       &Apache::loncommon::start_data_table_row().
                   2301:                       $roletext{$sortrole{$which}}.
                   2302:                       &Apache::loncommon::end_data_table_row();
1.217     raeburn  2303:             }
1.329     raeburn  2304:         }
                   2305:         unless($output{$type} eq '') {
                   2306:             $output{$type} = '<tr class="LC_info_row">'.
                   2307:                       "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
                   2308:                       $output{$type};
                   2309:             $rolesdisplay = 1;
                   2310:         }
                   2311:     }
                   2312:     if ($rolesdisplay == 1) {
                   2313:         my $contextrole='';
                   2314:         if ($env{'request.course.id'}) {
                   2315:             if (&Apache::loncommon::course_type() eq 'Community') {
                   2316:                 $contextrole = &mt('Existing Roles in this Community');
1.290     bisitz   2317:             } else {
1.329     raeburn  2318:                 $contextrole = &mt('Existing Roles in this Course');
1.290     bisitz   2319:             }
1.329     raeburn  2320:         } elsif ($env{'request.role'} =~ /^au\./) {
1.377     raeburn  2321:             $contextrole = &mt('Existing Co-Author Roles in your Authoring Space');
1.470     raeburn  2322:         } elsif ($env{'request.role'} =~ m{^ca\./($match_domain)/($match_username)/$}) {
                   2323:             $contextrole = &mt('Existing Co-Author Roles in [_1] Authoring Space',
                   2324:                                '<i>'.$1.'_'.$2.'</i>');
1.329     raeburn  2325:         } else {
1.418     raeburn  2326:             if ($showall) {
                   2327:                 $contextrole = &mt('Existing Roles in this Domain');
                   2328:             } elsif ($showactive) {
                   2329:                 $contextrole = &mt('Unexpired Roles in this Domain');
                   2330:             } elsif ($showexpired) {
                   2331:                 $contextrole = &mt('Expired or Revoked Roles in this Domain');
                   2332:             }
1.329     raeburn  2333:         }
1.393     raeburn  2334:         $r->print('<div class="LC_left_float">'.
1.375     raeburn  2335: '<fieldset><legend>'.$contextrole.'</legend>'.
1.217     raeburn  2336: &Apache::loncommon::start_data_table("LC_createuser").
1.418     raeburn  2337: &Apache::loncommon::start_data_table_header_row());
                   2338:         if ($showall) {
                   2339:             $r->print(
1.419     raeburn  2340: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}.'</th>'
1.418     raeburn  2341:             );
                   2342:         } elsif ($showexpired) {
                   2343:             $r->print('<th>'.$lt{'rev'}.'</th>');
                   2344:         }
                   2345:         $r->print(
1.419     raeburn  2346: '<th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.'</th>'.
                   2347: '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
1.217     raeburn  2348: &Apache::loncommon::end_data_table_header_row());
1.377     raeburn  2349:         foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  2350:             if ($output{$type}) {
                   2351:                 $r->print($output{$type}."\n");
1.217     raeburn  2352:             }
                   2353:         }
1.375     raeburn  2354:         $r->print(&Apache::loncommon::end_data_table().
                   2355:                   '</fieldset></div>');
1.329     raeburn  2356:     }
1.217     raeburn  2357:     return;
                   2358: }
                   2359: 
1.218     raeburn  2360: sub new_coauthor_roles {
                   2361:     my ($r,$ccuname,$ccdomain) = @_;
                   2362:     my $addrolesdisplay = 0;
                   2363:     #
                   2364:     # Co-Author
                   2365:     #
1.470     raeburn  2366:     my ($cuname,$cudom);
                   2367:     if (($env{'request.role'} eq "au./$env{'user.domain'}/") ||
                   2368:         ($env{'request.role'} eq "dc./$env{'user.domain'}/")) {
                   2369:         $cuname=$env{'user.name'};
                   2370:         $cudom=$env{'request.role.domain'};
1.218     raeburn  2371:         # No sense in assigning co-author role to yourself
1.470     raeburn  2372:         if ((&Apache::lonuserutils::authorpriv($cuname,$cudom)) &&
                   2373:             ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
                   2374:             $addrolesdisplay = 1;
                   2375:         }
                   2376:     } elsif ($env{'request.role'} =~ m{^ca\./($match_domain)/($match_username)$}) {
                   2377:         ($cudom,$cuname) = ($1,$2);
                   2378:         if ((&Apache::lonuserutils::coauthorpriv($cuname,$cudom)) &&
                   2379:             ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain) &&
                   2380:             ($cudom ne $ccdomain || $cuname ne $ccuname)) {
                   2381:             $addrolesdisplay = 1;
                   2382:         }
                   2383:     }
                   2384:     if ($addrolesdisplay) {
1.218     raeburn  2385:         my %lt=&Apache::lonlocal::texthash(
1.377     raeburn  2386:                     'cs'   => "Authoring Space",
1.218     raeburn  2387:                     'act'  => "Activate",
                   2388:                     'rol'  => "Role",
                   2389:                     'ext'  => "Extent",
                   2390:                     'sta'  => "Start",
                   2391:                     'end'  => "End",
                   2392:                     'cau'  => "Co-Author",
                   2393:                     'caa'  => "Assistant Co-Author",
                   2394:                     'ssd'  => "Set Start Date",
                   2395:                     'sed'  => "Set End Date"
                   2396:                                        );
                   2397:         $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n".
                   2398:                   &Apache::loncommon::start_data_table()."\n".
                   2399:                   &Apache::loncommon::start_data_table_header_row()."\n".
                   2400:                   '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
                   2401:                   '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
                   2402:                   '<th>'.$lt{'end'}.'</th>'."\n".
                   2403:                   &Apache::loncommon::end_data_table_header_row()."\n".
                   2404:                   &Apache::loncommon::start_data_table_row().'
                   2405:            <td>
1.291     bisitz   2406:             <input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_ca" />
1.218     raeburn  2407:            </td>
                   2408:            <td>'.$lt{'cau'}.'</td>
                   2409:            <td>'.$cudom.'_'.$cuname.'</td>
                   2410:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
                   2411:              <a href=
                   2412: "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>
                   2413: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
                   2414: <a href=
                   2415: "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".
                   2416:               &Apache::loncommon::end_data_table_row()."\n".
                   2417:               &Apache::loncommon::start_data_table_row()."\n".
1.291     bisitz   2418: '<td><input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
1.218     raeburn  2419: <td>'.$lt{'caa'}.'</td>
                   2420: <td>'.$cudom.'_'.$cuname.'</td>
                   2421: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
                   2422: <a href=
                   2423: "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>
                   2424: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
                   2425: <a href=
                   2426: "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".
                   2427:              &Apache::loncommon::end_data_table_row()."\n".
                   2428:              &Apache::loncommon::end_data_table());
                   2429:     } elsif ($env{'request.role'} =~ /^au\./) {
                   2430:         if (!(&Apache::lonuserutils::authorpriv($env{'user.name'},
                   2431:                                                 $env{'request.role.domain'}))) {
                   2432:             $r->print('<span class="LC_error">'.
                   2433:                       &mt('You do not have privileges to assign co-author roles.').
                   2434:                       '</span>');
                   2435:         } elsif (($env{'user.name'} eq $ccuname) &&
                   2436:              ($env{'user.domain'} eq $ccdomain)) {
1.377     raeburn  2437:             $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  2438:         }
1.470     raeburn  2439:     } elsif ($env{'request.role'} =~ m{^ca\./($match_domain)/($match_username)$}) {
                   2440:         if (!(&Apache::lonuserutils::coauthorpriv($2,$1))) {
                   2441:             $r->print('<span class="LC_error">'.
                   2442:                       &mt('You do not have privileges to assign co-author roles.').
                   2443:                       '</span>');
                   2444:         } elsif (($env{'user.name'} eq $ccuname) &&
                   2445:              ($env{'user.domain'} eq $ccdomain)) {
                   2446:             $r->print(&mt('Assigning yourself a co-author or assistant co-author role in an author area in Authoring Space in which you already have a co-author role is not permitted'));
                   2447:         } elsif (($cudom eq $ccdomain) && ($cuname eq $ccuname)) {
                   2448:             $r->print(&mt("Assigning a co-author or assistant co-author role to an Authoring Space's author is not permitted"));
                   2449:         }
1.218     raeburn  2450:     }
                   2451:     return $addrolesdisplay;;
                   2452: }
                   2453: 
                   2454: sub new_domain_roles {
1.357     raeburn  2455:     my ($r,$ccdomain) = @_;
1.218     raeburn  2456:     my $addrolesdisplay = 0;
                   2457:     #
                   2458:     # Domain level
                   2459:     #
                   2460:     my $num_domain_level = 0;
                   2461:     my $domaintext =
                   2462:     '<h4>'.&mt('Domain Level').'</h4>'.
                   2463:     &Apache::loncommon::start_data_table().
                   2464:     &Apache::loncommon::start_data_table_header_row().
                   2465:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
                   2466:     &mt('Extent').'</th>'.
                   2467:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
                   2468:     &Apache::loncommon::end_data_table_header_row();
1.312     raeburn  2469:     my @allroles = &Apache::lonuserutils::roles_by_context('domain');
1.445     raeburn  2470:     my $uprimary = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
                   2471:     my $uintdom = &Apache::lonnet::internet_dom($uprimary);
1.218     raeburn  2472:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
1.312     raeburn  2473:         foreach my $role (@allroles) {
                   2474:             next if ($role eq 'ad');
1.357     raeburn  2475:             next if (($role eq 'au') && ($ccdomain ne $thisdomain));
1.218     raeburn  2476:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
1.445     raeburn  2477:                if ($role eq 'dc') {
                   2478:                    unless ($thisdomain eq $env{'request.role.domain'}) {
                   2479:                        my $domprim = &Apache::lonnet::domain($thisdomain,'primary');
                   2480:                        my $intdom = &Apache::lonnet::internet_dom($domprim);
                   2481:                        next unless ($uintdom eq $intdom);
                   2482:                    }
                   2483:                }
1.218     raeburn  2484:                my $plrole=&Apache::lonnet::plaintext($role);
                   2485:                my %lt=&Apache::lonlocal::texthash(
                   2486:                     'ssd'  => "Set Start Date",
                   2487:                     'sed'  => "Set End Date"
                   2488:                                        );
                   2489:                $num_domain_level ++;
                   2490:                $domaintext .=
                   2491: &Apache::loncommon::start_data_table_row().
1.291     bisitz   2492: '<td><input type="checkbox" name="act_'.$thisdomain.'_'.$role.'" /></td>
1.218     raeburn  2493: <td>'.$plrole.'</td>
                   2494: <td>'.$thisdomain.'</td>
                   2495: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
                   2496: <a href=
                   2497: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
                   2498: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
                   2499: <a href=
                   2500: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
                   2501: &Apache::loncommon::end_data_table_row();
                   2502:             }
                   2503:         }
                   2504:     }
                   2505:     $domaintext.= &Apache::loncommon::end_data_table();
                   2506:     if ($num_domain_level > 0) {
                   2507:         $r->print($domaintext);
                   2508:         $addrolesdisplay = 1;
                   2509:     }
                   2510:     return $addrolesdisplay;
                   2511: }
                   2512: 
1.188     raeburn  2513: sub user_authentication {
1.451     raeburn  2514:     my ($ccuname,$ccdomain,$formname,$crstype,$permission) = @_;
1.188     raeburn  2515:     my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
1.227     raeburn  2516:     my $outcome;
1.418     raeburn  2517:     my %lt=&Apache::lonlocal::texthash(
                   2518:                    'err'   => "ERROR",
                   2519:                    'uuas'  => "This user has an unrecognized authentication scheme",
                   2520:                    'adcs'  => "Please alert a domain coordinator of this situation",
                   2521:                    'sldb'  => "Please specify login data below",
                   2522:                    'ld'    => "Login Data"
                   2523:     );
1.188     raeburn  2524:     # Check for a bad authentication type
1.449     raeburn  2525:     if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth|lti):/) {
1.188     raeburn  2526:         # bad authentication scheme
                   2527:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
1.227     raeburn  2528:             &initialize_authen_forms($ccdomain,$formname);
                   2529: 
1.190     raeburn  2530:             my $choices = &Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc);
1.188     raeburn  2531:             $outcome = <<ENDBADAUTH;
                   2532: <script type="text/javascript" language="Javascript">
1.301     bisitz   2533: // <![CDATA[
1.188     raeburn  2534: $loginscript
1.301     bisitz   2535: // ]]>
1.188     raeburn  2536: </script>
                   2537: <span class="LC_error">$lt{'err'}:
                   2538: $lt{'uuas'} ($currentauth). $lt{'sldb'}.</span>
                   2539: <h3>$lt{'ld'}</h3>
                   2540: $choices
                   2541: ENDBADAUTH
                   2542:         } else {
                   2543:             # This user is not allowed to modify the user's
                   2544:             # authentication scheme, so just notify them of the problem
                   2545:             $outcome = <<ENDBADAUTH;
                   2546: <span class="LC_error"> $lt{'err'}: 
                   2547: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
                   2548: </span>
                   2549: ENDBADAUTH
                   2550:         }
                   2551:     } else { # Authentication type is valid
1.418     raeburn  2552:         
1.227     raeburn  2553:         &initialize_authen_forms($ccdomain,$formname,$currentauth,'modifyuser');
1.205     raeburn  2554:         my ($authformcurrent,$can_modify,@authform_others) =
1.188     raeburn  2555:             &modify_login_block($ccdomain,$currentauth);
                   2556:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2557:             # Current user has login modification privileges
                   2558:             $outcome =
                   2559:                        '<script type="text/javascript" language="Javascript">'."\n".
1.301     bisitz   2560:                        '// <![CDATA['."\n".
1.188     raeburn  2561:                        $loginscript."\n".
1.301     bisitz   2562:                        '// ]]>'."\n".
1.188     raeburn  2563:                        '</script>'."\n".
                   2564:                        '<h3>'.$lt{'ld'}.'</h3>'.
                   2565:                        &Apache::loncommon::start_data_table().
1.205     raeburn  2566:                        &Apache::loncommon::start_data_table_row().
1.188     raeburn  2567:                        '<td>'.$authformnop;
1.418     raeburn  2568:             if (($can_modify) && (&Apache::lonnet::allowed('mau',$ccdomain))) {
1.188     raeburn  2569:                 $outcome .= '</td>'."\n".
                   2570:                             &Apache::loncommon::end_data_table_row().
                   2571:                             &Apache::loncommon::start_data_table_row().
                   2572:                             '<td>'.$authformcurrent.'</td>'.
                   2573:                             &Apache::loncommon::end_data_table_row()."\n";
                   2574:             } else {
1.200     raeburn  2575:                 $outcome .= '&nbsp;('.$authformcurrent.')</td>'.
                   2576:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  2577:             }
1.418     raeburn  2578:             if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2579:                 foreach my $item (@authform_others) { 
                   2580:                     $outcome .= &Apache::loncommon::start_data_table_row().
                   2581:                                 '<td>'.$item.'</td>'.
                   2582:                                 &Apache::loncommon::end_data_table_row()."\n";
                   2583:                 }
1.188     raeburn  2584:             }
1.205     raeburn  2585:             $outcome .= &Apache::loncommon::end_data_table();
1.188     raeburn  2586:         } else {
1.451     raeburn  2587:             if (($currentauth =~ /^internal:/) &&
                   2588:                 (&Apache::lonuserutils::can_change_internalpass($ccuname,$ccdomain,$crstype,$permission))) {
                   2589:                 $outcome = <<"ENDJS";
                   2590: <script type="text/javascript">
                   2591: // <![CDATA[
                   2592: function togglePwd(form) {
                   2593:     if (form.newintpwd.length) {
                   2594:         if (document.getElementById('LC_ownersetpwd')) {
                   2595:             for (var i=0; i<form.newintpwd.length; i++) {
                   2596:                 if (form.newintpwd[i].checked) {
                   2597:                     if (form.newintpwd[i].value == 1) {
                   2598:                         document.getElementById('LC_ownersetpwd').style.display = 'inline-block';
                   2599:                     } else {
                   2600:                         document.getElementById('LC_ownersetpwd').style.display = 'none';
                   2601:                     }
                   2602:                 }
                   2603:             }
                   2604:         }
                   2605:     }
                   2606: }
                   2607: // ]]>
                   2608: </script>
                   2609: ENDJS
                   2610: 
                   2611:                 $outcome .= '<h3>'.$lt{'ld'}.'</h3>'.
                   2612:                             &Apache::loncommon::start_data_table().
                   2613:                             &Apache::loncommon::start_data_table_row().
                   2614:                             '<td>'.&mt('Internally authenticated').'<br />'.&mt("Change user's password?").
                   2615:                             '<label><input type="radio" name="newintpwd" value="0" checked="checked" onclick="togglePwd(this.form);" />'.
                   2616:                             &mt('No').'</label>'.('&nbsp;'x2).
                   2617:                             '<label><input type="radio" name="newintpwd" value="1" onclick="togglePwd(this.form);" />'.&mt('Yes').'</label>'.
                   2618:                             '<div id="LC_ownersetpwd" style="display:none">'.
                   2619:                             '&nbsp;&nbsp;'.&mt('Password').' <input type="password" size="15" name="intarg" value="" />'.
                   2620:                             '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.intarg.type='."'text'".' } else { this.form.intarg.type='."'password'".' }" />'.&mt('Visible input').'</label></div></td>'.
                   2621:                             &Apache::loncommon::end_data_table_row().
                   2622:                             &Apache::loncommon::end_data_table();
                   2623:             }
1.418     raeburn  2624:             if (&Apache::lonnet::allowed('udp',$ccdomain)) {
                   2625:                 # Current user has rights to view domain preferences for user's domain
                   2626:                 my $result;
                   2627:                 if ($currentauth =~ /^krb(4|5):([^:]*)$/) {
                   2628:                     my ($krbver,$krbrealm) = ($1,$2);
                   2629:                     if ($krbrealm eq '') {
                   2630:                         $result = &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
                   2631:                     } else {
                   2632:                         $result = &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
1.426     raeburn  2633:                                       $krbrealm,$krbver);
1.418     raeburn  2634:                     }
                   2635:                 } elsif ($currentauth =~ /^internal:/) {
                   2636:                     $result = &mt('Currently internally authenticated.');
                   2637:                 } elsif ($currentauth =~ /^localauth:/) {
                   2638:                     $result = &mt('Currently using local (institutional) authentication.');
                   2639:                 } elsif ($currentauth =~ /^unix:/) {
                   2640:                     $result = &mt('Currently Filesystem Authenticated.');
1.449     raeburn  2641:                 } elsif ($currentauth =~ /^lti:/) {
1.451     raeburn  2642:                     $result = &mt('Currently LTI authenticated.');
1.418     raeburn  2643:                 }
                   2644:                 $outcome = '<h3>'.$lt{'ld'}.'</h3>'.
                   2645:                            &Apache::loncommon::start_data_table().
                   2646:                            &Apache::loncommon::start_data_table_row().
                   2647:                            '<td>'.$result.'</td>'.
                   2648:                            &Apache::loncommon::end_data_table_row()."\n".
                   2649:                            &Apache::loncommon::end_data_table();
                   2650:             } elsif (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
1.188     raeburn  2651:                 my %lt=&Apache::lonlocal::texthash(
                   2652:                            'ccld'  => "Change Current Login Data",
                   2653:                            'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
                   2654:                            'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   2655:                 );
                   2656:                 $outcome .= <<ENDNOPRIV;
                   2657: <h3>$lt{'ccld'}</h3>
                   2658: $lt{'yodo'} $lt{'ifch'}: $ccdomain
1.235     raeburn  2659: <input type="hidden" name="login" value="nochange" />
1.188     raeburn  2660: ENDNOPRIV
                   2661:             }
                   2662:         }
                   2663:     }  ## End of "check for bad authentication type" logic
                   2664:     return $outcome;
                   2665: }
                   2666: 
1.187     raeburn  2667: sub modify_login_block {
                   2668:     my ($dom,$currentauth) = @_;
                   2669:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2670:     my ($authnum,%can_assign) =
                   2671:         &Apache::loncommon::get_assignable_auth($dom);
1.205     raeburn  2672:     my ($authformcurrent,@authform_others,$show_override_msg);
1.187     raeburn  2673:     if ($currentauth=~/^krb(4|5):/) {
                   2674:         $authformcurrent=$authformkrb;
                   2675:         if ($can_assign{'int'}) {
1.205     raeburn  2676:             push(@authform_others,$authformint);
1.187     raeburn  2677:         }
                   2678:         if ($can_assign{'loc'}) {
1.205     raeburn  2679:             push(@authform_others,$authformloc);
1.187     raeburn  2680:         }
1.449     raeburn  2681:         if ($can_assign{'lti'}) {
                   2682:             push(@authform_others,$authformlti);
                   2683:         }
1.187     raeburn  2684:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   2685:             $show_override_msg = 1;
                   2686:         }
                   2687:     } elsif ($currentauth=~/^internal:/) {
                   2688:         $authformcurrent=$authformint;
                   2689:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2690:             push(@authform_others,$authformkrb);
1.187     raeburn  2691:         }
                   2692:         if ($can_assign{'loc'}) {
1.205     raeburn  2693:             push(@authform_others,$authformloc);
1.187     raeburn  2694:         }
1.449     raeburn  2695:         if ($can_assign{'lti'}) {
                   2696:             push(@authform_others,$authformlti);
                   2697:         }
1.187     raeburn  2698:         if ($can_assign{'int'}) {
                   2699:             $show_override_msg = 1;
                   2700:         }
                   2701:     } elsif ($currentauth=~/^unix:/) {
                   2702:         $authformcurrent=$authformfsys;
                   2703:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2704:             push(@authform_others,$authformkrb);
1.187     raeburn  2705:         }
                   2706:         if ($can_assign{'int'}) {
1.205     raeburn  2707:             push(@authform_others,$authformint);
1.187     raeburn  2708:         }
                   2709:         if ($can_assign{'loc'}) {
1.205     raeburn  2710:             push(@authform_others,$authformloc);
1.187     raeburn  2711:         }
1.449     raeburn  2712:         if ($can_assign{'lti'}) {
                   2713:             push(@authform_others,$authformlti);
                   2714:         }
1.187     raeburn  2715:         if ($can_assign{'fsys'}) {
                   2716:             $show_override_msg = 1;
                   2717:         }
                   2718:     } elsif ($currentauth=~/^localauth:/) {
                   2719:         $authformcurrent=$authformloc;
                   2720:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2721:             push(@authform_others,$authformkrb);
1.187     raeburn  2722:         }
                   2723:         if ($can_assign{'int'}) {
1.205     raeburn  2724:             push(@authform_others,$authformint);
1.187     raeburn  2725:         }
1.449     raeburn  2726:         if ($can_assign{'lti'}) {
                   2727:             push(@authform_others,$authformlti);
                   2728:         }
1.187     raeburn  2729:         if ($can_assign{'loc'}) {
                   2730:             $show_override_msg = 1;
                   2731:         }
1.449     raeburn  2732:     } elsif ($currentauth=~/^lti:/) {
                   2733:         $authformcurrent=$authformlti;
                   2734:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   2735:             push(@authform_others,$authformkrb);
                   2736:         }
                   2737:         if ($can_assign{'int'}) {
                   2738:             push(@authform_others,$authformint);
                   2739:         }
                   2740:         if ($can_assign{'loc'}) {
                   2741:             push(@authform_others,$authformloc);
                   2742:         }
1.187     raeburn  2743:     }
                   2744:     if ($show_override_msg) {
1.205     raeburn  2745:         $authformcurrent = '<table><tr><td colspan="3">'.$authformcurrent.
                   2746:                            '</td></tr>'."\n".
                   2747:                            '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
                   2748:                            '<td><b>'.&mt('Currently in use').'</b></td>'.
                   2749:                            '<td align="right"><span class="LC_cusr_emph">'.
1.187     raeburn  2750:                             &mt('will override current values').
1.205     raeburn  2751:                             '</span></td></tr></table>';
1.187     raeburn  2752:     }
1.205     raeburn  2753:     return ($authformcurrent,$show_override_msg,@authform_others); 
1.187     raeburn  2754: }
                   2755: 
1.188     raeburn  2756: sub personal_data_display {
1.470     raeburn  2757:     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$readonly,$rolesarray,$now,
1.456     raeburn  2758:         $captchaform,$emailusername,$usertype,$usernameset,$condition,$excluded,$showsubmit) = @_;
1.470     raeburn  2759:     my ($output,%userenv,%canmodify,%canmodify_status,$disabled);
1.219     raeburn  2760:     my @userinfo = ('firstname','middlename','lastname','generation',
                   2761:                     'permanentemail','id');
1.252     raeburn  2762:     my $rowcount = 0;
                   2763:     my $editable = 0;
1.391     raeburn  2764:     my %textboxsize = (
                   2765:                        firstname      => '15',
                   2766:                        middlename     => '15',
                   2767:                        lastname       => '15',
                   2768:                        generation     => '5',
                   2769:                        permanentemail => '25',
                   2770:                        id             => '15',
                   2771:                       );
                   2772: 
                   2773:     my %lt=&Apache::lonlocal::texthash(
                   2774:                 'pd'             => "Personal Data",
                   2775:                 'firstname'      => "First Name",
                   2776:                 'middlename'     => "Middle Name",
                   2777:                 'lastname'       => "Last Name",
                   2778:                 'generation'     => "Generation",
                   2779:                 'permanentemail' => "Permanent e-mail address",
                   2780:                 'id'             => "Student/Employee ID",
                   2781:                 'lg'             => "Login Data",
                   2782:                 'inststatus'     => "Affiliation",
                   2783:                 'email'          => 'E-mail address',
                   2784:                 'valid'          => 'Validation',
1.442     raeburn  2785:                 'username'       => 'Username',
1.391     raeburn  2786:     );
                   2787: 
                   2788:     %canmodify_status =
1.286     raeburn  2789:         &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
                   2790:                                                    ['inststatus'],$rolesarray);
1.253     raeburn  2791:     if (!$newuser) {
1.188     raeburn  2792:         # Get the users information
                   2793:         %userenv = &Apache::lonnet::get('environment',
                   2794:                    ['firstname','middlename','lastname','generation',
1.286     raeburn  2795:                     'permanentemail','id','inststatus'],$ccdomain,$ccuname);
1.219     raeburn  2796:         %canmodify =
                   2797:             &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
1.252     raeburn  2798:                                                        \@userinfo,$rolesarray);
1.257     raeburn  2799:     } elsif ($context eq 'selfcreate') {
1.391     raeburn  2800:         if ($newuser eq 'email') {
1.396     raeburn  2801:             if (ref($emailusername) eq 'HASH') {
                   2802:                 if (ref($emailusername->{$usertype}) eq 'HASH') {
                   2803:                     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
1.442     raeburn  2804:                     @userinfo = ();
1.396     raeburn  2805:                     if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
                   2806:                         foreach my $field (@{$infofields}) { 
                   2807:                             if ($emailusername->{$usertype}->{$field}) {
                   2808:                                 push(@userinfo,$field);
                   2809:                                 $canmodify{$field} = 1;
                   2810:                                 unless ($textboxsize{$field}) {
                   2811:                                     $textboxsize{$field} = 25;
                   2812:                                 }
                   2813:                                 unless ($lt{$field}) {
                   2814:                                     $lt{$field} = $infotitles->{$field};
                   2815:                                 }
                   2816:                                 if ($emailusername->{$usertype}->{$field} eq 'required') {
                   2817:                                     $lt{$field} .= '<b>*</b>';
                   2818:                                 }
1.391     raeburn  2819:                             }
                   2820:                         }
                   2821:                     }
                   2822:                 }
                   2823:             }
                   2824:         } else {
                   2825:             %canmodify = &selfcreate_canmodify($context,$ccdomain,\@userinfo,
                   2826:                                                $inst_results,$rolesarray);
                   2827:         }
1.470     raeburn  2828:     } elsif ($readonly) {
                   2829:         $disabled = ' disabled="disabled"';
1.188     raeburn  2830:     }
1.391     raeburn  2831: 
1.188     raeburn  2832:     my $genhelp=&Apache::loncommon::help_open_topic('Generation');
                   2833:     $output = '<h3>'.$lt{'pd'}.'</h3>'.
                   2834:               &Apache::lonhtmlcommon::start_pick_box();
1.391     raeburn  2835:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
1.443     raeburn  2836:         my $size = 25;
1.442     raeburn  2837:         if ($condition) {
1.443     raeburn  2838:             if ($condition =~ /^\@[^\@]+$/) {
                   2839:                 $size = 10;
1.442     raeburn  2840:             } else {
                   2841:                 undef($condition);
                   2842:             }
1.443     raeburn  2843:         } 
                   2844:         if ($excluded) {
                   2845:             unless ($excluded =~ /^\@[^\@]+$/) {
                   2846:                 undef($condition);
                   2847:             }
1.442     raeburn  2848:         }
1.396     raeburn  2849:         $output .= &Apache::lonhtmlcommon::row_title($lt{'email'}.'<b>*</b>',undef,
1.391     raeburn  2850:                                                      'LC_oddrow_value')."\n".
1.443     raeburn  2851:                    '<input type="text" name="uname" size="'.$size.'" value="" autocomplete="off" />';
                   2852:         if ($condition) {
                   2853:             $output .= $condition;
                   2854:         } elsif ($excluded) {
                   2855:             $output .= '<br /><span style="font-size: smaller">'.&mt('You must use an e-mail address that does not end with [_1]',
                   2856:                                                                      $excluded).'</span>';
                   2857:         }
                   2858:         if ($usernameset eq 'first') {
                   2859:             $output .= '<br /><span style="font-size: smaller">';
                   2860:             if ($condition) {
                   2861:                 $output .= &mt('Your username in LON-CAPA will be the part of your e-mail address before [_1]',
                   2862:                                       $condition);
                   2863:             } else {
                   2864:                 $output .= &mt('Your username in LON-CAPA will be the part of your e-mail address before the @');
                   2865:             }
                   2866:             $output .= '</span>';
                   2867:         }
1.391     raeburn  2868:         $rowcount ++;
                   2869:         $output .= &Apache::lonhtmlcommon::row_closure(1);
1.460     raeburn  2870:         my $upassone = '<input type="password" name="upass'.$now.'" size="20" autocomplete="new-password" />';
                   2871:         my $upasstwo = '<input type="password" name="upasscheck'.$now.'" size="20" autocomplete="new-password" />';
1.396     raeburn  2872:         $output .= &Apache::lonhtmlcommon::row_title(&mt('Password').'<b>*</b>',
1.391     raeburn  2873:                                                     'LC_pick_box_title',
                   2874:                                                     'LC_oddrow_value')."\n".
                   2875:                    $upassone."\n".
                   2876:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
1.396     raeburn  2877:                    &Apache::lonhtmlcommon::row_title(&mt('Confirm password').'<b>*</b>',
1.391     raeburn  2878:                                                      'LC_pick_box_title',
                   2879:                                                      'LC_oddrow_value')."\n".
                   2880:                    $upasstwo.
                   2881:                    &Apache::lonhtmlcommon::row_closure()."\n";
1.443     raeburn  2882:         if ($usernameset eq 'free') {
                   2883:             my $onclick = "toggleUsernameDisp(this,'selfcreateusername');"; 
1.442     raeburn  2884:             $output .= &Apache::lonhtmlcommon::row_title($lt{'username'},undef,'LC_oddrow_value')."\n".
1.455     raeburn  2885:                        '<span class="LC_nobreak">'.&mt('Use e-mail address: ').
                   2886:                        '<label><input type="radio" name="emailused" value="1" checked="checked" onclick="'.$onclick.'" />'.
                   2887:                        &mt('Yes').'</label>'.('&nbsp;'x2).
                   2888:                        '<label><input type="radio" name="emailused" value="0" onclick="'.$onclick.'" />'.
                   2889:                        &mt('No').'</label></span>'."\n".
1.442     raeburn  2890:                        '<div id="selfcreateusername" style="display: none; font-size: smaller">'.
                   2891:                        '<br /><span class="LC_nobreak">'.&mt('Preferred username').
                   2892:                        '&nbsp;<input type="text" name="username" value="" size="20" autocomplete="off"/>'.
                   2893:                        '</span></div>'."\n".&Apache::lonhtmlcommon::row_closure(1);
                   2894:             $rowcount ++;
                   2895:         }
1.391     raeburn  2896:     }
1.188     raeburn  2897:     foreach my $item (@userinfo) {
                   2898:         my $rowtitle = $lt{$item};
1.252     raeburn  2899:         my $hiderow = 0;
1.188     raeburn  2900:         if ($item eq 'generation') {
                   2901:             $rowtitle = $genhelp.$rowtitle;
                   2902:         }
1.252     raeburn  2903:         my $row = &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
1.188     raeburn  2904:         if ($newuser) {
1.210     raeburn  2905:             if (ref($inst_results) eq 'HASH') {
                   2906:                 if ($inst_results->{$item} ne '') {
1.252     raeburn  2907:                     $row .= '<input type="hidden" name="c'.$item.'" value="'.$inst_results->{$item}.'" />'.$inst_results->{$item};
1.210     raeburn  2908:                 } else {
1.252     raeburn  2909:                     if ($context eq 'selfcreate') {
1.391     raeburn  2910:                         if ($canmodify{$item}) {
1.394     raeburn  2911:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.252     raeburn  2912:                             $editable ++;
                   2913:                         } else {
                   2914:                             $hiderow = 1;
                   2915:                         }
1.253     raeburn  2916:                     } else {
1.470     raeburn  2917:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value=""'.$disabled.' />';
1.252     raeburn  2918:                     }
1.210     raeburn  2919:                 }
1.188     raeburn  2920:             } else {
1.252     raeburn  2921:                 if ($context eq 'selfcreate') {
1.401     raeburn  2922:                     if ($canmodify{$item}) {
                   2923:                         if ($newuser eq 'email') {
                   2924:                             $row .= '<input type="text" name="'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.287     raeburn  2925:                         } else {
1.401     raeburn  2926:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.287     raeburn  2927:                         }
1.401     raeburn  2928:                         $editable ++;
                   2929:                     } else {
                   2930:                         $hiderow = 1;
1.252     raeburn  2931:                     }
1.253     raeburn  2932:                 } else {
1.470     raeburn  2933:                     $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value=""'.$disabled.' />';
1.252     raeburn  2934:                 }
1.188     raeburn  2935:             }
                   2936:         } else {
1.219     raeburn  2937:             if ($canmodify{$item}) {
1.252     raeburn  2938:                 $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
1.393     raeburn  2939:                 if (($item eq 'id') && (!$newuser)) {
                   2940:                     $row .= '<br />'.&Apache::lonuserutils::forceid_change($context);
                   2941:                 }
1.188     raeburn  2942:             } else {
1.252     raeburn  2943:                 $row .= $userenv{$item};
1.188     raeburn  2944:             }
                   2945:         }
1.252     raeburn  2946:         $row .= &Apache::lonhtmlcommon::row_closure(1);
                   2947:         if (!$hiderow) {
                   2948:             $output .= $row;
                   2949:             $rowcount ++;
                   2950:         }
1.188     raeburn  2951:     }
1.286     raeburn  2952:     if (($canmodify_status{'inststatus'}) || ($context ne 'selfcreate')) {
                   2953:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($ccdomain);
                   2954:         if (ref($types) eq 'ARRAY') {
                   2955:             if (@{$types} > 0) {
                   2956:                 my ($hiderow,$shown);
                   2957:                 if ($canmodify_status{'inststatus'}) {
                   2958:                     $shown = &pick_inst_statuses($userenv{'inststatus'},$usertypes,$types);
                   2959:                 } else {
                   2960:                     if ($userenv{'inststatus'} eq '') {
                   2961:                         $hiderow = 1;
1.334     raeburn  2962:                     } else {
                   2963:                         my @showitems;
                   2964:                         foreach my $item ( map { &unescape($_); } split(':',$userenv{'inststatus'})) {
                   2965:                             if (exists($usertypes->{$item})) {
                   2966:                                 push(@showitems,$usertypes->{$item});
                   2967:                             } else {
                   2968:                                 push(@showitems,$item);
                   2969:                             }
                   2970:                         }
                   2971:                         if (@showitems) {
                   2972:                             $shown = join(', ',@showitems);
                   2973:                         } else {
                   2974:                             $hiderow = 1;
                   2975:                         }
1.286     raeburn  2976:                     }
                   2977:                 }
                   2978:                 if (!$hiderow) {
1.389     bisitz   2979:                     my $row = &Apache::lonhtmlcommon::row_title(&mt('Affiliations'),undef,'LC_oddrow_value')."\n".
1.286     raeburn  2980:                               $shown.&Apache::lonhtmlcommon::row_closure(1); 
                   2981:                     if ($context eq 'selfcreate') {
                   2982:                         $rowcount ++;
                   2983:                     }
                   2984:                     $output .= $row;
                   2985:                 }
                   2986:             }
                   2987:         }
                   2988:     }
1.391     raeburn  2989:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
                   2990:         if ($captchaform) {
1.410     raeburn  2991:             $output .= &Apache::lonhtmlcommon::row_title($lt{'valid'}.'*',
                   2992:                                                          'LC_pick_box_title')."\n".
1.456     raeburn  2993:                        $captchaform."\n".'<br /><br />'.
1.391     raeburn  2994:                        &Apache::lonhtmlcommon::row_closure(1); 
                   2995:             $rowcount ++;
                   2996:         }
1.456     raeburn  2997:         if ($showsubmit) {
                   2998:             my $submit_text = &mt('Create account');
                   2999:             $output .= &Apache::lonhtmlcommon::row_title()."\n".
                   3000:                        '<br /><input type="submit" name="createaccount" value="'.
                   3001:                        $submit_text.'" />';
                   3002:             if ($usertype ne '') {
                   3003:                 $output .= '<input type="hidden" name="type" value="'.
                   3004:                            &HTML::Entities::encode($usertype,'\'<>"&').'" />';
                   3005:             }
                   3006:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   3007:         }
1.391     raeburn  3008:     }
1.188     raeburn  3009:     $output .= &Apache::lonhtmlcommon::end_pick_box();
1.206     raeburn  3010:     if (wantarray) {
1.252     raeburn  3011:         if ($context eq 'selfcreate') {
                   3012:             return($output,$rowcount,$editable);
                   3013:         } else {
1.388     bisitz   3014:             return $output;
1.252     raeburn  3015:         }
1.206     raeburn  3016:     } else {
                   3017:         return $output;
                   3018:     }
1.188     raeburn  3019: }
                   3020: 
1.286     raeburn  3021: sub pick_inst_statuses {
                   3022:     my ($curr,$usertypes,$types) = @_;
                   3023:     my ($output,$rem,@currtypes);
                   3024:     if ($curr ne '') {
                   3025:         @currtypes = map { &unescape($_); } split(/:/,$curr);
                   3026:     }
                   3027:     my $numinrow = 2;
                   3028:     if (ref($types) eq 'ARRAY') {
                   3029:         $output = '<table>';
                   3030:         my $lastcolspan; 
                   3031:         for (my $i=0; $i<@{$types}; $i++) {
                   3032:             if (defined($usertypes->{$types->[$i]})) {
                   3033:                 my $rem = $i%($numinrow);
                   3034:                 if ($rem == 0) {
                   3035:                     if ($i<@{$types}-1) {
                   3036:                         if ($i > 0) { 
                   3037:                             $output .= '</tr>';
                   3038:                         }
                   3039:                         $output .= '<tr>';
                   3040:                     }
                   3041:                 } elsif ($i==@{$types}-1) {
                   3042:                     my $colsleft = $numinrow - $rem;
                   3043:                     if ($colsleft > 1) {
                   3044:                         $lastcolspan = ' colspan="'.$colsleft.'"';
                   3045:                     }
                   3046:                 }
                   3047:                 my $check = ' ';
                   3048:                 if (grep(/^\Q$types->[$i]\E$/,@currtypes)) {
                   3049:                     $check = ' checked="checked" ';
                   3050:                 }
                   3051:                 $output .= '<td class="LC_left_item"'.$lastcolspan.'>'.
                   3052:                            '<span class="LC_nobreak"><label>'.
                   3053:                            '<input type="checkbox" name="inststatus" '.
                   3054:                            'value="'.$types->[$i].'"'.$check.'/>'.
                   3055:                            $usertypes->{$types->[$i]}.'</label></span></td>';
                   3056:             }
                   3057:         }
                   3058:         $output .= '</tr></table>';
                   3059:     }
                   3060:     return $output;
                   3061: }
                   3062: 
1.257     raeburn  3063: sub selfcreate_canmodify {
                   3064:     my ($context,$dom,$userinfo,$inst_results,$rolesarray) = @_;
                   3065:     if (ref($inst_results) eq 'HASH') {
                   3066:         my @inststatuses = &get_inststatuses($inst_results);
                   3067:         if (@inststatuses == 0) {
                   3068:             @inststatuses = ('default');
                   3069:         }
                   3070:         $rolesarray = \@inststatuses;
                   3071:     }
                   3072:     my %canmodify =
                   3073:         &Apache::lonuserutils::can_modify_userinfo($context,$dom,$userinfo,
                   3074:                                                    $rolesarray);
                   3075:     return %canmodify;
                   3076: }
                   3077: 
1.252     raeburn  3078: sub get_inststatuses {
                   3079:     my ($insthashref) = @_;
                   3080:     my @inststatuses = ();
                   3081:     if (ref($insthashref) eq 'HASH') {
                   3082:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
                   3083:             @inststatuses = @{$insthashref->{'inststatus'}};
                   3084:         }
                   3085:     }
                   3086:     return @inststatuses;
                   3087: }
                   3088: 
1.4       www      3089: # ================================================================= Phase Three
1.42      matthew  3090: sub update_user_data {
1.451     raeburn  3091:     my ($r,$context,$crstype,$brcrum,$showcredits,$permission) = @_; 
1.101     albertel 3092:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                   3093:                                           $env{'form.ccdomain'});
1.27      matthew  3094:     # Error messages
1.188     raeburn  3095:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
1.193     raeburn  3096:     my $end       = '</span><br /><br />';
                   3097:     my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
1.188     raeburn  3098:                     "'$env{'form.prevphase'}','modify')".'" />'.
1.219     raeburn  3099:                     &mt('Return to previous page').'</a>'.
                   3100:                     &Apache::loncommon::end_page();
                   3101:     my $now = time;
1.40      www      3102:     my $title;
1.101     albertel 3103:     if (exists($env{'form.makeuser'})) {
1.40      www      3104: 	$title='Set Privileges for New User';
                   3105:     } else {
                   3106:         $title='Modify User Privileges';
                   3107:     }
1.213     raeburn  3108:     my $newuser = 0;
1.160     raeburn  3109:     my ($jsback,$elements) = &crumb_utilities();
                   3110:     my $jscript = '<script type="text/javascript">'."\n".
1.301     bisitz   3111:                   '// <![CDATA['."\n".
                   3112:                   $jsback."\n".
                   3113:                   '// ]]>'."\n".
                   3114:                   '</script>'."\n";
1.422     raeburn  3115:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$env{'form.ccdomain'});
1.351     raeburn  3116:     push (@{$brcrum},
                   3117:              {href => "javascript:backPage(document.userupdate)",
                   3118:               text => $breadcrumb_text{'search'},
                   3119:               faq  => 282,
                   3120:               bug  => 'Instructor Interface',}
                   3121:              );
                   3122:     if ($env{'form.prevphase'} eq 'userpicked') {
                   3123:         push(@{$brcrum},
                   3124:                {href => "javascript:backPage(document.userupdate,'get_user_info','select')",
                   3125:                 text => $breadcrumb_text{'userpicked'},
                   3126:                 faq  => 282,
                   3127:                 bug  => 'Instructor Interface',});
1.233     raeburn  3128:     }
1.224     raeburn  3129:     my $helpitem = 'Course_Change_Privileges';
                   3130:     if ($env{'form.action'} eq 'singlestudent') {
                   3131:         $helpitem = 'Course_Add_Student';
1.439     raeburn  3132:     } elsif ($context eq 'author') {
                   3133:         $helpitem = 'Author_Change_Privileges';
                   3134:     } elsif ($context eq 'domain') {
                   3135:         $helpitem = 'Domain_Change_Privileges';
1.224     raeburn  3136:     }
1.351     raeburn  3137:     push(@{$brcrum}, 
                   3138:             {href => "javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
                   3139:              text => $breadcrumb_text{'modify'},
                   3140:              faq  => 282,
                   3141:              bug  => 'Instructor Interface',},
                   3142:             {href => "/adm/createuser",
                   3143:              text => "Result",
                   3144:              faq  => 282,
                   3145:              bug  => 'Instructor Interface',
                   3146:              help => $helpitem});
                   3147:     my $args = {bread_crumbs          => $brcrum,
                   3148:                 bread_crumbs_component => 'User Management'};
                   3149:     if ($env{'form.popup'}) {
                   3150:         $args->{'no_nav_bar'} = 1;
                   3151:     }
                   3152:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
1.188     raeburn  3153:     $r->print(&update_result_form($uhome));
1.27      matthew  3154:     # Check Inputs
1.101     albertel 3155:     if (! $env{'form.ccuname'} ) {
1.193     raeburn  3156: 	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
1.27      matthew  3157: 	return;
                   3158:     }
1.138     albertel 3159:     if (  $env{'form.ccuname'} ne 
                   3160: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
1.281     bisitz   3161: 	$r->print($error.&mt('Invalid login name.').'  '.
                   3162: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid.').
1.193     raeburn  3163: 		  $end.$rtnlink);
1.27      matthew  3164: 	return;
                   3165:     }
1.101     albertel 3166:     if (! $env{'form.ccdomain'}       ) {
1.193     raeburn  3167: 	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
1.27      matthew  3168: 	return;
                   3169:     }
1.138     albertel 3170:     if (  $env{'form.ccdomain'} ne
                   3171: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
1.281     bisitz   3172: 	$r->print($error.&mt('Invalid domain name.').'  '.
                   3173: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid.').
1.193     raeburn  3174: 		  $end.$rtnlink);
1.27      matthew  3175: 	return;
                   3176:     }
1.219     raeburn  3177:     if ($uhome eq 'no_host') {
                   3178:         $newuser = 1;
                   3179:     }
1.101     albertel 3180:     if (! exists($env{'form.makeuser'})) {
1.29      matthew  3181:         # Modifying an existing user, so check the validity of the name
                   3182:         if ($uhome eq 'no_host') {
1.389     bisitz   3183:             $r->print(
                   3184:                 $error
                   3185:                .'<p class="LC_error">'
                   3186:                .&mt('Unable to determine home server for [_1] in domain [_2].',
                   3187:                         '"'.$env{'form.ccuname'}.'"','"'.$env{'form.ccdomain'}.'"')
                   3188:                .'</p>');
1.29      matthew  3189:             return;
                   3190:         }
                   3191:     }
1.27      matthew  3192:     # Determine authentication method and password for the user being modified
                   3193:     my $amode='';
                   3194:     my $genpwd='';
1.101     albertel 3195:     if ($env{'form.login'} eq 'krb') {
1.41      albertel 3196: 	$amode='krb';
1.101     albertel 3197: 	$amode.=$env{'form.krbver'};
                   3198: 	$genpwd=$env{'form.krbarg'};
                   3199:     } elsif ($env{'form.login'} eq 'int') {
1.27      matthew  3200: 	$amode='internal';
1.101     albertel 3201: 	$genpwd=$env{'form.intarg'};
                   3202:     } elsif ($env{'form.login'} eq 'fsys') {
1.27      matthew  3203: 	$amode='unix';
1.101     albertel 3204: 	$genpwd=$env{'form.fsysarg'};
                   3205:     } elsif ($env{'form.login'} eq 'loc') {
1.27      matthew  3206: 	$amode='localauth';
1.101     albertel 3207: 	$genpwd=$env{'form.locarg'};
1.27      matthew  3208: 	$genpwd=" " if (!$genpwd);
1.449     raeburn  3209:     } elsif ($env{'form.login'} eq 'lti') {
                   3210:         $amode='lti';
                   3211:         $genpwd=" ";
1.101     albertel 3212:     } elsif (($env{'form.login'} eq 'nochange') ||
                   3213:              ($env{'form.login'} eq ''        )) { 
1.34      matthew  3214:         # There is no need to tell the user we did not change what they
                   3215:         # did not ask us to change.
1.35      matthew  3216:         # If they are creating a new user but have not specified login
                   3217:         # information this will be caught below.
1.30      matthew  3218:     } else {
1.367     golterma 3219:             $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);
                   3220:             return;
1.27      matthew  3221:     }
1.164     albertel 3222: 
1.188     raeburn  3223:     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
1.367     golterma 3224:                         $env{'form.ccuname'}.' ('.&Apache::loncommon::plainname($env{'form.ccuname'},
                   3225:                         $env{'form.ccdomain'}).')', $env{'form.ccdomain'}).'</h3>');
                   3226:     my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,2);
1.344     bisitz   3227: 
1.193     raeburn  3228:     my (%alerts,%rulematch,%inst_results,%curr_rules);
1.334     raeburn  3229:     my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
1.470     raeburn  3230:     my @usertools = ('aboutme','blog','portfolio','portaccess','timezone');
1.449     raeburn  3231:     my @requestcourses = ('official','unofficial','community','textbook','placement','lti');
1.362     raeburn  3232:     my @requestauthor = ('requestauthor');
1.471   ! raeburn  3233:     my @authordefaults = ('webdav','editors');
1.286     raeburn  3234:     my ($othertitle,$usertypes,$types) = 
                   3235:         &Apache::loncommon::sorted_inst_types($env{'form.ccdomain'});
1.334     raeburn  3236:     my %canmodify_status =
                   3237:         &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},
                   3238:                                                    ['inststatus']);
1.101     albertel 3239:     if ($env{'form.makeuser'}) {
1.164     albertel 3240: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
1.27      matthew  3241:         # Check for the authentication mode and password
                   3242:         if (! $amode || ! $genpwd) {
1.193     raeburn  3243: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.27      matthew  3244: 	    return;
1.18      albertel 3245: 	}
1.29      matthew  3246:         # Determine desired host
1.101     albertel 3247:         my $desiredhost = $env{'form.hserver'};
1.29      matthew  3248:         if (lc($desiredhost) eq 'default') {
                   3249:             $desiredhost = undef;
                   3250:         } else {
1.147     albertel 3251:             my %home_servers = 
                   3252: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
1.29      matthew  3253:             if (! exists($home_servers{$desiredhost})) {
1.193     raeburn  3254:                 $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
                   3255:                 return;
                   3256:             }
                   3257:         }
                   3258:         # Check ID format
                   3259:         my %checkhash;
                   3260:         my %checks = ('id' => 1);
                   3261:         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
1.219     raeburn  3262:             'newuser' => $newuser, 
1.196     raeburn  3263:             'id' => $env{'form.cid'},
1.193     raeburn  3264:         );
1.196     raeburn  3265:         if ($env{'form.cid'} ne '') {
                   3266:             &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   3267:                                           \%rulematch,\%inst_results,\%curr_rules);
                   3268:             if (ref($alerts{'id'}) eq 'HASH') {
                   3269:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
                   3270:                     my $domdesc =
                   3271:                         &Apache::lonnet::domain($env{'form.ccdomain'},'description');
                   3272:                     if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
                   3273:                         my $userchkmsg;
                   3274:                         if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
                   3275:                             $userchkmsg  = 
                   3276:                                 &Apache::loncommon::instrule_disallow_msg('id',
                   3277:                                                                     $domdesc,1).
                   3278:                                 &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
                   3279:                                     $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
                   3280:                         }
                   3281:                         $r->print($error.&mt('Invalid ID format').$end.
                   3282:                                   $userchkmsg.$rtnlink);
                   3283:                         return;
                   3284:                     }
                   3285:                 }
1.29      matthew  3286:             }
                   3287:         }
1.367     golterma 3288:         &Apache::lonhtmlcommon::Increment_PrgWin($r, \%prog_state);
1.27      matthew  3289: 	# Call modifyuser
                   3290: 	my $result = &Apache::lonnet::modifyuser
1.193     raeburn  3291: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
1.188     raeburn  3292:              $amode,$genpwd,$env{'form.cfirstname'},
                   3293:              $env{'form.cmiddlename'},$env{'form.clastname'},
                   3294:              $env{'form.cgeneration'},undef,$desiredhost,
                   3295:              $env{'form.cpermanentemail'});
1.77      www      3296: 	$r->print(&mt('Generating user').': '.$result);
1.219     raeburn  3297:         $uhome = &Apache::lonnet::homeserver($env{'form.ccuname'},
1.101     albertel 3298:                                                $env{'form.ccdomain'});
1.334     raeburn  3299:         my (%changeHash,%newcustom,%changed,%changedinfo);
1.267     raeburn  3300:         if ($uhome ne 'no_host') {
1.334     raeburn  3301:             if ($context eq 'domain') {
1.378     raeburn  3302:                 foreach my $name ('portfolio','author') {
                   3303:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   3304:                         if ($env{'form.'.$name.'quota'} eq '') {
                   3305:                             $newcustom{$name.'quota'} = 0;
                   3306:                         } else {
                   3307:                             $newcustom{$name.'quota'} = $env{'form.'.$name.'quota'};
                   3308:                             $newcustom{$name.'quota'} =~ s/[^\d\.]//g;
                   3309:                         }
                   3310:                         if (&quota_admin($newcustom{$name.'quota'},\%changeHash,$name)) {
                   3311:                             $changed{$name.'quota'} = 1;
                   3312:                         }
1.334     raeburn  3313:                     }
                   3314:                 }
                   3315:                 foreach my $item (@usertools) {
                   3316:                     if ($env{'form.custom'.$item} == 1) {
                   3317:                         $newcustom{$item} = $env{'form.tools_'.$item};
                   3318:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   3319:                                                      \%changeHash,'tools');
                   3320:                     }
1.267     raeburn  3321:                 }
1.334     raeburn  3322:                 foreach my $item (@requestcourses) {
1.341     raeburn  3323:                     if ($env{'form.custom'.$item} == 1) {
                   3324:                         $newcustom{$item} = $env{'form.crsreq_'.$item};
                   3325:                         if ($env{'form.crsreq_'.$item} eq 'autolimit') {
                   3326:                             $newcustom{$item} .= '=';
1.383     raeburn  3327:                             $env{'form.crsreq_'.$item.'_limit'} =~ s/\D+//g;
                   3328:                             if ($env{'form.crsreq_'.$item.'_limit'}) {
1.341     raeburn  3329:                                 $newcustom{$item} .= $env{'form.crsreq_'.$item.'_limit'};
                   3330:                             }
1.334     raeburn  3331:                         }
1.341     raeburn  3332:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   3333:                                                       \%changeHash,'requestcourses');
1.334     raeburn  3334:                     }
1.275     raeburn  3335:                 }
1.362     raeburn  3336:                 if ($env{'form.customrequestauthor'} == 1) {
                   3337:                     $newcustom{'requestauthor'} = $env{'form.requestauthor'};
                   3338:                     $changed{'requestauthor'} = &tool_admin('requestauthor',
                   3339:                                                     $newcustom{'requestauthor'},
                   3340:                                                     \%changeHash,'requestauthor');
                   3341:                 }
1.470     raeburn  3342:                 if ($env{'form.customeditors'} == 1) {
                   3343:                     my @editors;
                   3344:                     my @posseditors = &Apache::loncommon::get_env_multiple('form.custom_editor');
                   3345:                     if (@posseditors) {
                   3346:                         foreach my $editor (@posseditors) {
                   3347:                             if (grep(/^\Q$editor\E$/,@posseditors)) {
                   3348:                                 unless (grep(/^\Q$editor\E$/,@editors)) {
                   3349:                                     push(@editors,$editor);
                   3350:                                 }
                   3351:                             }
                   3352:                         }
                   3353:                     }
                   3354:                     if (@editors) {
                   3355:                         @editors = sort(@editors);
                   3356:                         $changed{'editors'} = &tool_admin('editors',join(',',@editors),
                   3357:                                                           \%changeHash,'authordefaults');
                   3358:                     }
                   3359:                 }
                   3360:                 if ($env{'form.customwebdav'} == 1) {
                   3361:                     $newcustom{'webdav'} = $env{'form.authordefaults_webdav'};
                   3362:                     $changed{'webdav'} = &tool_admin('webdav',$newcustom{'webdav'},
                   3363:                                                   \%changeHash,'authordefaults');
                   3364:                 }
1.275     raeburn  3365:             }
1.334     raeburn  3366:             if ($canmodify_status{'inststatus'}) {
                   3367:                 if (exists($env{'form.inststatus'})) {
                   3368:                     my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   3369:                     if (@inststatuses > 0) {
                   3370:                         $changeHash{'inststatus'} = join(',',@inststatuses);
                   3371:                         $changed{'inststatus'} = $changeHash{'inststatus'};
1.306     raeburn  3372:                     }
                   3373:                 }
1.232     raeburn  3374:             }
1.334     raeburn  3375:             if (keys(%changed)) {
                   3376:                 foreach my $item (@userinfo) {
                   3377:                     $changeHash{$item}  = $env{'form.c'.$item};
1.286     raeburn  3378:                 }
1.267     raeburn  3379:                 my $chgresult =
                   3380:                      &Apache::lonnet::put('environment',\%changeHash,
                   3381:                                           $env{'form.ccdomain'},$env{'form.ccuname'});
1.470     raeburn  3382:             }
1.232     raeburn  3383:         }
1.454     raeburn  3384:         $r->print('<br />'.&mt('Home Server').': '.$uhome.' '.
1.219     raeburn  3385:                   &Apache::lonnet::hostname($uhome));
1.101     albertel 3386:     } elsif (($env{'form.login'} ne 'nochange') &&
                   3387:              ($env{'form.login'} ne ''        )) {
1.27      matthew  3388: 	# Modify user privileges
                   3389:         if (! $amode || ! $genpwd) {
1.193     raeburn  3390: 	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
1.27      matthew  3391: 	    return;
1.20      harris41 3392: 	}
1.395     bisitz   3393: 	# Only allow authentication modification if the person has authority
1.101     albertel 3394: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.20      harris41 3395: 	    $r->print('Modifying authentication: '.
1.31      matthew  3396:                       &Apache::lonnet::modifyuserauth(
1.101     albertel 3397: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
1.21      harris41 3398:                        $amode,$genpwd));
1.454     raeburn  3399:             $r->print('<br />'.&mt('Home Server').': '.&Apache::lonnet::homeserver
1.101     albertel 3400: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
1.4       www      3401: 	} else {
1.27      matthew  3402: 	    # Okay, this is a non-fatal error.
1.452     raeburn  3403: 	    $r->print($error.&mt('You do not have privileges to modify the authentication configuration for this user.').$end);
1.27      matthew  3404: 	}
1.451     raeburn  3405:     } elsif (($env{'form.intarg'} ne '') &&
                   3406:              (&Apache::lonnet::queryauthenticate($env{'form.ccuname'},$env{'form.ccdomain'}) =~ /^internal:/) &&
                   3407:              (&Apache::lonuserutils::can_change_internalpass($env{'form.ccuname'},$env{'form.ccdomain'},$crstype,$permission))) {
                   3408:         $r->print('Modifying authentication: '.
                   3409:                   &Apache::lonnet::modifyuserauth(
                   3410:                   $env{'form.ccdomain'},$env{'form.ccuname'},
                   3411:                   'internal',$env{'form.intarg'}));
1.28      matthew  3412:     }
1.344     bisitz   3413:     $r->rflush(); # Finish display of header before time consuming actions start
1.367     golterma 3414:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state);
1.28      matthew  3415:     ##
1.375     raeburn  3416:     my (@userroles,%userupdate,$cnum,$cdom,$defaultcredits,%namechanged);
1.213     raeburn  3417:     if ($context eq 'course') {
1.375     raeburn  3418:         ($cnum,$cdom) =
                   3419:             &Apache::lonuserutils::get_course_identity();
1.318     raeburn  3420:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
1.375     raeburn  3421:         if ($showcredits) {
                   3422:            $defaultcredits = &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   3423:         }
1.213     raeburn  3424:     }
1.101     albertel 3425:     if (! $env{'form.makeuser'} ) {
1.28      matthew  3426:         # Check for need to change
                   3427:         my %userenv = &Apache::lonnet::get
1.134     raeburn  3428:             ('environment',['firstname','middlename','lastname','generation',
1.378     raeburn  3429:              'id','permanentemail','portfolioquota','authorquota','inststatus',
1.459     raeburn  3430:              'tools.aboutme','tools.blog','tools.webdav',
1.470     raeburn  3431:              'tools.portfolio','tools.timezone','tools.portaccess',
                   3432:              'authormanagers','authoreditors','requestauthor',
1.361     raeburn  3433:              'requestcourses.official','requestcourses.unofficial',
1.384     raeburn  3434:              'requestcourses.community','requestcourses.textbook',
1.470     raeburn  3435:              'requestcourses.placement','requestcourses.lti',
1.384     raeburn  3436:              'reqcrsotherdom.official','reqcrsotherdom.unofficial',
                   3437:              'reqcrsotherdom.community','reqcrsotherdom.textbook',
1.471   ! raeburn  3438:              'reqcrsotherdom.placement','domcoord.author'],
1.160     raeburn  3439:               $env{'form.ccdomain'},$env{'form.ccuname'});
1.28      matthew  3440:         my ($tmp) = keys(%userenv);
                   3441:         if ($tmp =~ /^(con_lost|error)/i) { 
                   3442:             %userenv = ();
                   3443:         }
1.471   ! raeburn  3444:         unless (($userenv{'domcoord.author'} eq 'blocked') &&
        !          3445:                 (($env{'user.name'} ne $env{'form.ccuname'}) ||
        !          3446:                  ($env{'user.domain'} ne $env{'form.ccdomain'}))) {
        !          3447:             push(@authordefaults,'managers');
        !          3448:         }
1.206     raeburn  3449:         my $no_forceid_alert;
                   3450:         # Check to see if user information can be changed
                   3451:         my %domconfig =
                   3452:             &Apache::lonnet::get_dom('configuration',['usermodification'],
                   3453:                                      $env{'form.ccdomain'});
1.213     raeburn  3454:         my @statuses = ('active','future');
                   3455:         my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@statuses,undef,$env{'request.role.domain'});
                   3456:         my ($auname,$audom);
1.220     raeburn  3457:         if ($context eq 'author') {
1.206     raeburn  3458:             $auname = $env{'user.name'};
                   3459:             $audom = $env{'user.domain'};     
                   3460:         }
                   3461:         foreach my $item (keys(%roles)) {
1.220     raeburn  3462:             my ($rolenum,$roledom,$role) = split(/:/,$item,-1);
1.206     raeburn  3463:             if ($context eq 'course') {
                   3464:                 if ($cnum ne '' && $cdom ne '') {
                   3465:                     if ($rolenum eq $cnum && $roledom eq $cdom) {
                   3466:                         if (!grep(/^\Q$role\E$/,@userroles)) {
                   3467:                             push(@userroles,$role);
                   3468:                         }
                   3469:                     }
                   3470:                 }
                   3471:             } elsif ($context eq 'author') {
                   3472:                 if ($rolenum eq $auname && $roledom eq $audom) {
1.461     raeburn  3473:                     if (!grep(/^\Q$role\E$/,@userroles)) {
1.206     raeburn  3474:                         push(@userroles,$role);
                   3475:                     }
                   3476:                 }
                   3477:             }
                   3478:         }
1.220     raeburn  3479:         if ($env{'form.action'} eq 'singlestudent') {
                   3480:             if (!grep(/^st$/,@userroles)) {
                   3481:                 push(@userroles,'st');
                   3482:             }
                   3483:         } else {
                   3484:             # Check for course or co-author roles being activated or re-enabled
                   3485:             if ($context eq 'author' || $context eq 'course') {
                   3486:                 foreach my $key (keys(%env)) {
                   3487:                     if ($context eq 'author') {
                   3488:                         if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
                   3489:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3490:                                 push(@userroles,$1);
                   3491:                             }
                   3492:                         } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
                   3493:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3494:                                 push(@userroles,$1);
                   3495:                             }
1.206     raeburn  3496:                         }
1.220     raeburn  3497:                     } elsif ($context eq 'course') {
                   3498:                         if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
                   3499:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3500:                                 push(@userroles,$1);
                   3501:                             }
                   3502:                         } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
                   3503:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3504:                                 push(@userroles,$1);
                   3505:                             }
1.206     raeburn  3506:                         }
                   3507:                     }
                   3508:                 }
                   3509:             }
                   3510:         }
                   3511:         #Check to see if we can change personal data for the user 
                   3512:         my (@mod_disallowed,@longroles);
                   3513:         foreach my $role (@userroles) {
                   3514:             if ($role eq 'cr') {
                   3515:                 push(@longroles,'Custom');
                   3516:             } else {
1.318     raeburn  3517:                 push(@longroles,&Apache::lonnet::plaintext($role,$crstype)); 
1.206     raeburn  3518:             }
                   3519:         }
1.219     raeburn  3520:         my %canmodify = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},\@userinfo,\@userroles);
                   3521:         foreach my $item (@userinfo) {
1.28      matthew  3522:             # Strip leading and trailing whitespace
1.203     raeburn  3523:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
1.219     raeburn  3524:             if (!$canmodify{$item}) {
1.207     raeburn  3525:                 if (defined($env{'form.c'.$item})) {
                   3526:                     if ($env{'form.c'.$item} ne $userenv{$item}) {
                   3527:                         push(@mod_disallowed,$item);
                   3528:                     }
1.206     raeburn  3529:                 }
                   3530:                 $env{'form.c'.$item} = $userenv{$item};
                   3531:             }
1.28      matthew  3532:         }
1.259     bisitz   3533:         # Check to see if we can change the Student/Employee ID
1.196     raeburn  3534:         my $forceid = $env{'form.forceid'};
                   3535:         my $recurseid = $env{'form.recurseid'};
                   3536:         my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
1.203     raeburn  3537:         my %uidhash = &Apache::lonnet::idrget($env{'form.ccdomain'},
                   3538:                                             $env{'form.ccuname'});
                   3539:         if (($uidhash{$env{'form.ccuname'}}) && 
                   3540:             ($uidhash{$env{'form.ccuname'}}!~/error\:/) && 
                   3541:             (!$forceid)) {
                   3542:             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
                   3543:                 $env{'form.cid'} = $userenv{'id'};
1.293     bisitz   3544:                 $no_forceid_alert = &mt('New student/employee ID does not match existing ID for this user.')
1.259     bisitz   3545:                                    .'<br />'
                   3546:                                    .&mt("Change is not permitted without checking the 'Force ID change' checkbox on the previous page.")
                   3547:                                    .'<br />'."\n";
1.203     raeburn  3548:             }
                   3549:         }
                   3550:         if ($env{'form.cid'} ne $userenv{'id'}) {
1.196     raeburn  3551:             my $checkhash;
                   3552:             my $checks = { 'id' => 1 };
                   3553:             $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
                   3554:                    { 'newuser' => $newuser,
                   3555:                      'id'  => $env{'form.cid'}, 
                   3556:                    };
                   3557:             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   3558:                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
                   3559:             if (ref($alerts{'id'}) eq 'HASH') {
                   3560:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
1.203     raeburn  3561:                    $env{'form.cid'} = $userenv{'id'};
1.196     raeburn  3562:                 }
                   3563:             }
                   3564:         }
1.378     raeburn  3565:         my (%quotachanged,%oldquota,%newquota,%olddefquota,%newdefquota, 
                   3566:             $oldinststatus,$newinststatus,%oldisdefault,%newisdefault,%oldsettings,
1.339     raeburn  3567:             %oldsettingstext,%newsettings,%newsettingstext,@disporder,
1.378     raeburn  3568:             %oldsettingstatus,%newsettingstatus);
1.334     raeburn  3569:         @disporder = ('inststatus');
                   3570:         if ($env{'request.role.domain'} eq $env{'form.ccdomain'}) {
1.470     raeburn  3571:             push(@disporder,('requestcourses','requestauthor','authordefaults'));
1.334     raeburn  3572:         } else {
                   3573:             push(@disporder,'reqcrsotherdom');
                   3574:         }
                   3575:         push(@disporder,('quota','tools'));
1.338     raeburn  3576:         $oldinststatus = $userenv{'inststatus'};
1.378     raeburn  3577:         foreach my $name ('portfolio','author') {
                   3578:             ($olddefquota{$name},$oldsettingstatus{$name}) = 
                   3579:                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$oldinststatus,$name);
                   3580:             ($newdefquota{$name},$newsettingstatus{$name}) = ($olddefquota{$name},$oldsettingstatus{$name});
                   3581:         }
1.334     raeburn  3582:         my %canshow;
1.220     raeburn  3583:         if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
1.334     raeburn  3584:             $canshow{'quota'} = 1;
1.220     raeburn  3585:         }
1.267     raeburn  3586:         if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
1.334     raeburn  3587:             $canshow{'tools'} = 1;
1.267     raeburn  3588:         }
1.275     raeburn  3589:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
1.334     raeburn  3590:             $canshow{'requestcourses'} = 1;
1.300     raeburn  3591:         } elsif (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.334     raeburn  3592:             $canshow{'reqcrsotherdom'} = 1;
1.275     raeburn  3593:         }
1.286     raeburn  3594:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.334     raeburn  3595:             $canshow{'inststatus'} = 1;
1.286     raeburn  3596:         }
1.362     raeburn  3597:         if (&Apache::lonnet::allowed('cau',$env{'form.ccdomain'})) {
                   3598:             $canshow{'requestauthor'} = 1;
1.470     raeburn  3599:             $canshow{'authordefaults'} = 1;
1.362     raeburn  3600:         }
1.267     raeburn  3601:         my (%changeHash,%changed);
1.286     raeburn  3602:         if ($oldinststatus eq '') {
1.334     raeburn  3603:             $oldsettings{'inststatus'} = $othertitle; 
1.286     raeburn  3604:         } else {
                   3605:             if (ref($usertypes) eq 'HASH') {
1.334     raeburn  3606:                 $oldsettings{'inststatus'} = join(', ',map{ $usertypes->{ &unescape($_) }; } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  3607:             } else {
1.334     raeburn  3608:                 $oldsettings{'inststatus'} = join(', ',map{ &unescape($_); } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  3609:             }
                   3610:         }
                   3611:         $changeHash{'inststatus'} = $userenv{'inststatus'};
1.334     raeburn  3612:         if ($canmodify_status{'inststatus'}) {
                   3613:             $canshow{'inststatus'} = 1;
1.286     raeburn  3614:             if (exists($env{'form.inststatus'})) {
                   3615:                 my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   3616:                 if (@inststatuses > 0) {
                   3617:                     $newinststatus = join(':',map { &escape($_); } @inststatuses);
                   3618:                     $changeHash{'inststatus'} = $newinststatus;
                   3619:                     if ($newinststatus ne $oldinststatus) {
                   3620:                         $changed{'inststatus'} = $newinststatus;
1.378     raeburn  3621:                         foreach my $name ('portfolio','author') {
                   3622:                             ($newdefquota{$name},$newsettingstatus{$name}) =
                   3623:                                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   3624:                         }
1.286     raeburn  3625:                     }
                   3626:                     if (ref($usertypes) eq 'HASH') {
1.334     raeburn  3627:                         $newsettings{'inststatus'} = join(', ',map{ $usertypes->{$_}; } (@inststatuses)); 
1.286     raeburn  3628:                     } else {
1.337     raeburn  3629:                         $newsettings{'inststatus'} = join(', ',@inststatuses);
1.286     raeburn  3630:                     }
1.334     raeburn  3631:                 }
                   3632:             } else {
                   3633:                 $newinststatus = '';
                   3634:                 $changeHash{'inststatus'} = $newinststatus;
                   3635:                 $newsettings{'inststatus'} = $othertitle;
                   3636:                 if ($newinststatus ne $oldinststatus) {
                   3637:                     $changed{'inststatus'} = $changeHash{'inststatus'};
1.378     raeburn  3638:                     foreach my $name ('portfolio','author') {
                   3639:                         ($newdefquota{$name},$newsettingstatus{$name}) =
                   3640:                             &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   3641:                     }
1.286     raeburn  3642:                 }
                   3643:             }
1.334     raeburn  3644:         } elsif ($context ne 'selfcreate') {
                   3645:             $canshow{'inststatus'} = 1;
1.337     raeburn  3646:             $newsettings{'inststatus'} = $oldsettings{'inststatus'};
1.286     raeburn  3647:         }
1.378     raeburn  3648:         foreach my $name ('portfolio','author') {
                   3649:             $changeHash{$name.'quota'} = $userenv{$name.'quota'};
                   3650:         }
1.334     raeburn  3651:         if ($context eq 'domain') {
1.378     raeburn  3652:             foreach my $name ('portfolio','author') {
                   3653:                 if ($userenv{$name.'quota'} ne '') {
                   3654:                     $oldquota{$name} = $userenv{$name.'quota'};
                   3655:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   3656:                         if ($env{'form.'.$name.'quota'} eq '') {
                   3657:                             $newquota{$name} = 0;
                   3658:                         } else {
                   3659:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   3660:                             $newquota{$name} =~ s/[^\d\.]//g;
                   3661:                         }
                   3662:                         if ($newquota{$name} != $oldquota{$name}) {
                   3663:                             if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   3664:                                 $changed{$name.'quota'} = 1;
                   3665:                             }
                   3666:                         }
1.334     raeburn  3667:                     } else {
1.378     raeburn  3668:                         if (&quota_admin('',\%changeHash,$name)) {
                   3669:                             $changed{$name.'quota'} = 1;
                   3670:                             $newquota{$name} = $newdefquota{$name};
                   3671:                             $newisdefault{$name} = 1;
                   3672:                         }
1.334     raeburn  3673:                     }
1.149     raeburn  3674:                 } else {
1.378     raeburn  3675:                     $oldisdefault{$name} = 1;
                   3676:                     $oldquota{$name} = $olddefquota{$name};
                   3677:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   3678:                         if ($env{'form.'.$name.'quota'} eq '') {
                   3679:                             $newquota{$name} = 0;
                   3680:                         } else {
                   3681:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   3682:                             $newquota{$name} =~ s/[^\d\.]//g;
                   3683:                         }
                   3684:                         if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   3685:                             $changed{$name.'quota'} = 1;
                   3686:                         }
1.334     raeburn  3687:                     } else {
1.378     raeburn  3688:                         $newquota{$name} = $newdefquota{$name};
                   3689:                         $newisdefault{$name} = 1;
1.334     raeburn  3690:                     }
1.378     raeburn  3691:                 }
                   3692:                 if ($oldisdefault{$name}) {
                   3693:                     $oldsettingstext{'quota'}{$name} = &get_defaultquota_text($oldsettingstatus{$name});
1.383     raeburn  3694:                 }  else {
                   3695:                     $oldsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$oldquota{$name});
1.378     raeburn  3696:                 }
                   3697:                 if ($newisdefault{$name}) {
                   3698:                     $newsettingstext{'quota'}{$name} = &get_defaultquota_text($newsettingstatus{$name});
1.383     raeburn  3699:                 } else {
                   3700:                     $newsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$newquota{$name});
1.134     raeburn  3701:                 }
                   3702:             }
1.334     raeburn  3703:             &tool_changes('tools',\@usertools,\%oldsettings,\%oldsettingstext,\%userenv,
                   3704:                           \%changeHash,\%changed,\%newsettings,\%newsettingstext);
                   3705:             if ($env{'form.ccdomain'} eq $env{'request.role.domain'}) {
                   3706:                 &tool_changes('requestcourses',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   3707:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.470     raeburn  3708:                 my ($isadv,$isauthor) =
                   3709:                     &Apache::lonnet::is_advanced_user($env{'form.ccdomain'},$env{'form.ccuname'});
                   3710:                 unless ($isauthor) {
                   3711:                     &tool_changes('requestauthor',\@requestauthor,\%oldsettings,\%oldsettingstext,
                   3712:                                   \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
                   3713:                 }
                   3714:                 &tool_changes('authordefaults',\@authordefaults,\%oldsettings,\%oldsettingstext,
                   3715:                                   \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  3716:             } else {
1.334     raeburn  3717:                 &tool_changes('reqcrsotherdom',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   3718:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  3719:             }
                   3720:         }
1.334     raeburn  3721:         foreach my $item (@userinfo) {
                   3722:             if ($env{'form.c'.$item} ne $userenv{$item}) {
                   3723:                 $namechanged{$item} = 1;
                   3724:             }
1.204     raeburn  3725:         }
1.378     raeburn  3726:         foreach my $name ('portfolio','author') {
1.390     bisitz   3727:             $oldsettings{'quota'}{$name} = &mt('[_1] MB',$oldquota{$name});
                   3728:             $newsettings{'quota'}{$name} = &mt('[_1] MB',$newquota{$name});
1.378     raeburn  3729:         }
1.334     raeburn  3730:         if ((keys(%namechanged) > 0) || (keys(%changed) > 0)) {
1.267     raeburn  3731:             my ($chgresult,$namechgresult);
                   3732:             if (keys(%changed) > 0) {
1.470     raeburn  3733:                 $chgresult =
1.204     raeburn  3734:                     &Apache::lonnet::put('environment',\%changeHash,
                   3735:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
1.267     raeburn  3736:                 if ($chgresult eq 'ok') {
1.470     raeburn  3737:                     my ($ca_mgr_del,%ca_mgr_add);
                   3738:                     if ($changed{'managers'}) {
                   3739:                         my (@adds,@dels);
                   3740:                         if ($changeHash{'authormanagers'} eq '') {
                   3741:                             @dels = split(/,/,$userenv{'authormanagers'});
                   3742:                         } elsif ($userenv{'authormanagers'} eq '') {
                   3743:                             @adds = split(/,/,$changeHash{'authormanagers'});
                   3744:                         } else {
                   3745:                             my @old = split(/,/,$userenv{'authormanagers'});
                   3746:                             my @new = split(/,/,$changeHash{'authormanagers'});
                   3747:                             my @diffs = &Apache::loncommon::compare_arrays(\@old,\@new);
                   3748:                             if (@diffs) {
                   3749:                                 foreach my $user (@diffs) {
                   3750:                                     if (grep(/^\Q$user\E$/,@old)) {
                   3751:                                         push(@dels,$user);
                   3752:                                     } elsif (grep(/^\Q$user\E$/,@new)) {
                   3753:                                         push(@adds,$user);
                   3754:                                     }
                   3755:                                 }
                   3756:                             }
                   3757:                         }
                   3758:                         my $key = "internal.manager./$env{'form.ccdomain'}/$env{'form.ccuname'}";
                   3759:                         if (@dels) {
                   3760:                             foreach my $user (@dels) {
                   3761:                                 if ($user =~ /^($match_username):($match_domain)$/) {
                   3762:                                     &Apache::lonnet::del('environment',[$key],$2,$1);
                   3763:                                 }
                   3764:                             }
                   3765:                             my $curruser = $env{'user.name'}.':'.$env{'user.domain'};
                   3766:                             if (grep(/^\Q$curruser\E$/,@dels)) {
                   3767:                                 $ca_mgr_del = $key;
                   3768:                             }
                   3769:                         }
                   3770:                         if (@adds) {
                   3771:                             foreach my $user (@adds) {
                   3772:                                 if ($user =~ /^($match_username):($match_domain)$/) {
                   3773:                                     &Apache::lonnet::put('environment',{$key => 1},$2,$1);
                   3774:                                 }
                   3775:                             }
                   3776:                             my $curruser = $env{'user.name'}.':'.$env{'user.domain'};
                   3777:                             if (grep(/^\Q$curruser\E$/,@adds)) {
                   3778:                                 $ca_mgr_add{$key} = 1;
                   3779:                             }
                   3780:                         }
                   3781:                     }
1.267     raeburn  3782:                     if (($env{'user.name'} eq $env{'form.ccuname'}) &&
                   3783:                         ($env{'user.domain'} eq $env{'form.ccdomain'})) {
1.270     raeburn  3784:                         my %newenvhash;
                   3785:                         foreach my $key (keys(%changed)) {
1.411     raeburn  3786:                             if (($key eq 'official') || ($key eq 'unofficial') ||
                   3787:                                 ($key eq 'community') || ($key eq 'textbook') ||
1.449     raeburn  3788:                                 ($key eq 'placement') || ($key eq 'lti')) {
1.279     raeburn  3789:                                 $newenvhash{'environment.requestcourses.'.$key} =
                   3790:                                     $changeHash{'requestcourses.'.$key};
1.362     raeburn  3791:                                 if ($changeHash{'requestcourses.'.$key}) {
1.332     raeburn  3792:                                     $newenvhash{'environment.canrequest.'.$key} = 1;
1.279     raeburn  3793:                                 } else {
                   3794:                                     $newenvhash{'environment.canrequest.'.$key} =
                   3795:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3796:                                             $key,'reload','requestcourses');
                   3797:                                 }
1.362     raeburn  3798:                             } elsif ($key eq 'requestauthor') {
                   3799:                                 $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3800:                                 if ($changeHash{$key}) {
                   3801:                                     $newenvhash{'environment.canrequest.author'} = 1;
                   3802:                                 } else {
                   3803:                                     $newenvhash{'environment.canrequest.author'} =
                   3804:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3805:                                             $key,'reload','requestauthor');
                   3806:                                 }
1.470     raeburn  3807:                             } elsif ($key eq 'editors') {
                   3808:                                 $newenvhash{'environment.author'.$key} = $changeHash{'author'.$key};
                   3809:                                 if ($key eq 'editors') {
                   3810:                                     if ($env{'form.customeditors'}) {
                   3811:                                         $newenvhash{'environment.editors'} = $changeHash{'author'.$key};
                   3812:                                     } else {
                   3813:                                         $newenvhash{'environment.editors'} =
                   3814:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3815:                                             $key,'reload','authordefaults');
                   3816:                                     }
                   3817:                                 }
1.275     raeburn  3818:                             } elsif ($key ne 'quota') {
1.270     raeburn  3819:                                 $newenvhash{'environment.tools.'.$key} = 
                   3820:                                     $changeHash{'tools.'.$key};
1.279     raeburn  3821:                                 if ($changeHash{'tools.'.$key} ne '') {
                   3822:                                     $newenvhash{'environment.availabletools.'.$key} =
                   3823:                                         $changeHash{'tools.'.$key};
                   3824:                                 } else {
                   3825:                                     $newenvhash{'environment.availabletools.'.$key} =
1.367     golterma 3826:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
1.470     raeburn  3827:                                             $key,'reload','tools');
1.279     raeburn  3828:                                 }
1.270     raeburn  3829:                             }
                   3830:                         }
1.271     raeburn  3831:                         if (keys(%newenvhash)) {
                   3832:                             &Apache::lonnet::appenv(\%newenvhash);
                   3833:                         }
1.470     raeburn  3834:                     } else {
                   3835:                         if ($ca_mgr_del) {
                   3836:                             &Apache::lonnet::delenv($ca_mgr_del);
                   3837:                         }
                   3838:                         if (keys(%ca_mgr_add)) {
                   3839:                             &Apache::lonnet::appenv(\%ca_mgr_add);
                   3840:                         }
1.267     raeburn  3841:                     }
1.463     raeburn  3842:                     if ($changed{'aboutme'}) {
                   3843:                         &Apache::loncommon::devalidate_aboutme_cache($env{'form.ccuname'},
                   3844:                                                                      $env{'form.ccdomain'});
                   3845:                     }
1.267     raeburn  3846:                 }
1.204     raeburn  3847:             }
1.334     raeburn  3848:             if (keys(%namechanged) > 0) {
1.337     raeburn  3849:                 foreach my $field (@userinfo) {
                   3850:                     $changeHash{$field}  = $env{'form.c'.$field};
                   3851:                 }
                   3852: # Make the change
1.204     raeburn  3853:                 $namechgresult =
                   3854:                     &Apache::lonnet::modifyuser($env{'form.ccdomain'},
                   3855:                         $env{'form.ccuname'},$changeHash{'id'},undef,undef,
                   3856:                         $changeHash{'firstname'},$changeHash{'middlename'},
                   3857:                         $changeHash{'lastname'},$changeHash{'generation'},
1.337     raeburn  3858:                         $changeHash{'id'},undef,$changeHash{'permanentemail'},undef,\@userinfo);
1.220     raeburn  3859:                 %userupdate = (
                   3860:                                lastname   => $env{'form.clastname'},
                   3861:                                middlename => $env{'form.cmiddlename'},
                   3862:                                firstname  => $env{'form.cfirstname'},
                   3863:                                generation => $env{'form.cgeneration'},
                   3864:                                id         => $env{'form.cid'},
                   3865:                              );
1.204     raeburn  3866:             }
1.334     raeburn  3867:             if (((keys(%namechanged) > 0) && $namechgresult eq 'ok') || 
1.267     raeburn  3868:                 ((keys(%changed) > 0) && $chgresult eq 'ok')) {
1.28      matthew  3869:             # Tell the user we changed the name
1.334     raeburn  3870:                 &display_userinfo($r,1,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3871:                                   \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,
1.334     raeburn  3872:                                   \%oldsettings, \%oldsettingstext,\%newsettings,
                   3873:                                   \%newsettingstext);
1.203     raeburn  3874:                 if ($env{'form.cid'} ne $userenv{'id'}) {
                   3875:                     &Apache::lonnet::idput($env{'form.ccdomain'},
1.407     raeburn  3876:                          {$env{'form.ccuname'} => $env{'form.cid'}},$uhome,'ids');
1.203     raeburn  3877:                     if (($recurseid) &&
                   3878:                         (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
                   3879:                         my $idresult = 
                   3880:                             &Apache::lonuserutils::propagate_id_change(
                   3881:                                 $env{'form.ccuname'},$env{'form.ccdomain'},
                   3882:                                 \%userupdate);
                   3883:                         $r->print('<br />'.$idresult.'<br />');
                   3884:                     }
1.196     raeburn  3885:                 }
1.149     raeburn  3886:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
                   3887:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
                   3888:                     my %newenvhash;
                   3889:                     foreach my $key (keys(%changeHash)) {
                   3890:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3891:                     }
1.238     raeburn  3892:                     &Apache::lonnet::appenv(\%newenvhash);
1.149     raeburn  3893:                 }
1.28      matthew  3894:             } else { # error occurred
1.389     bisitz   3895:                 $r->print(
                   3896:                     '<p class="LC_error">'
                   3897:                    .&mt('Unable to successfully change environment for [_1] in domain [_2].',
                   3898:                             '"'.$env{'form.ccuname'}.'"',
                   3899:                             '"'.$env{'form.ccdomain'}.'"')
                   3900:                    .'</p>');
1.28      matthew  3901:             }
1.334     raeburn  3902:         } else { # End of if ($env ... ) logic
1.275     raeburn  3903:             # They did not want to change the users name, quota, tool availability,
                   3904:             # or ability to request creation of courses, 
1.267     raeburn  3905:             # but we can still tell them what the name and quota and availabilities are  
1.334     raeburn  3906:             &display_userinfo($r,undef,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3907:                               \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,\%oldsettings,
1.334     raeburn  3908:                               \%oldsettingstext,\%newsettings,\%newsettingstext);
1.28      matthew  3909:         }
1.206     raeburn  3910:         if (@mod_disallowed) {
                   3911:             my ($rolestr,$contextname);
                   3912:             if (@longroles > 0) {
                   3913:                 $rolestr = join(', ',@longroles);
                   3914:             } else {
                   3915:                 $rolestr = &mt('No roles');
                   3916:             }
                   3917:             if ($context eq 'course') {
1.399     bisitz   3918:                 $contextname = 'course';
1.206     raeburn  3919:             } elsif ($context eq 'author') {
1.399     bisitz   3920:                 $contextname = 'co-author';
1.206     raeburn  3921:             }
                   3922:             $r->print(&mt('The following fields were not updated: ').'<ul>');
                   3923:             my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   3924:             foreach my $field (@mod_disallowed) {
                   3925:                 $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
                   3926:             }
1.207     raeburn  3927:             $r->print('</ul>');
                   3928:             if (@mod_disallowed == 1) {
1.399     bisitz   3929:                 $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  3930:             } else {
1.399     bisitz   3931:                 $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  3932:             }
1.292     bisitz   3933:             my $helplink = 'javascript:helpMenu('."'display'".')';
                   3934:             $r->print('<span class="LC_cusr_emph">'.$rolestr.'</span><br />'
                   3935:                      .&mt('Please contact your [_1]helpdesk[_2] for more information.'
                   3936:                          ,'<a href="'.$helplink.'">','</a>')
                   3937:                       .'<br />');
1.206     raeburn  3938:         }
1.259     bisitz   3939:         $r->print('<span class="LC_warning">'
                   3940:                   .$no_forceid_alert
                   3941:                   .&Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules)
                   3942:                   .'</span>');
1.4       www      3943:     }
1.367     golterma 3944:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.220     raeburn  3945:     if ($env{'form.action'} eq 'singlestudent') {
1.375     raeburn  3946:         &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context,
                   3947:                                $crstype,$showcredits,$defaultcredits);
1.386     bisitz   3948:         my $linktext = ($crstype eq 'Community' ?
                   3949:             &mt('Enroll Another Member') : &mt('Enroll Another Student'));
                   3950:         $r->print(
                   3951:             &Apache::lonhtmlcommon::actionbox([
                   3952:                 '<a href="javascript:backPage(document.userupdate)">'
                   3953:                .($crstype eq 'Community' ? 
                   3954:                     &mt('Enroll Another Member') : &mt('Enroll Another Student'))
                   3955:                .'</a>']));
1.220     raeburn  3956:     } else {
1.375     raeburn  3957:         my @rolechanges = &update_roles($r,$context,$showcredits);
1.334     raeburn  3958:         if (keys(%namechanged) > 0) {
1.220     raeburn  3959:             if ($context eq 'course') {
                   3960:                 if (@userroles > 0) {
1.225     raeburn  3961:                     if ((@rolechanges == 0) || 
                   3962:                         (!(grep(/^st$/,@rolechanges)))) {
                   3963:                         if (grep(/^st$/,@userroles)) {
                   3964:                             my $classlistupdated =
                   3965:                                 &Apache::lonuserutils::update_classlist($cdom,
1.220     raeburn  3966:                                               $cnum,$env{'form.ccdomain'},
                   3967:                                        $env{'form.ccuname'},\%userupdate);
1.225     raeburn  3968:                         }
1.220     raeburn  3969:                     }
                   3970:                 }
                   3971:             }
                   3972:         }
1.226     raeburn  3973:         my $userinfo = &Apache::loncommon::plainname($env{'form.ccuname'},
1.233     raeburn  3974:                                                      $env{'form.ccdomain'});
                   3975:         if ($env{'form.popup'}) {
                   3976:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   3977:         } else {
1.367     golterma 3978:             $r->print('<br />'.&Apache::lonhtmlcommon::actionbox(['<a href="javascript:backPage(document.userupdate,'."'$env{'form.prevphase'}','modify'".')">'
                   3979:                      .&mt('Modify this user: [_1]','<span class="LC_cusr_emph">'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.' ('.$userinfo.')</span>').'</a>',
                   3980:                      '<a href="javascript:backPage(document.userupdate)">'.&mt('Create/Modify Another User').'</a>']));
1.233     raeburn  3981:         }
1.220     raeburn  3982:     }
                   3983: }
                   3984: 
1.334     raeburn  3985: sub display_userinfo {
1.362     raeburn  3986:     my ($r,$changed,$order,$canshow,$requestcourses,$usertools,$requestauthor,
                   3987:         $userenv,$changedhash,$namechangedhash,$oldsetting,$oldsettingtext,
1.334     raeburn  3988:         $newsetting,$newsettingtext) = @_;
                   3989:     return unless (ref($order) eq 'ARRAY' &&
                   3990:                    ref($canshow) eq 'HASH' && 
                   3991:                    ref($requestcourses) eq 'ARRAY' && 
1.362     raeburn  3992:                    ref($requestauthor) eq 'ARRAY' &&
1.334     raeburn  3993:                    ref($usertools) eq 'ARRAY' && 
                   3994:                    ref($userenv) eq 'HASH' &&
                   3995:                    ref($changedhash) eq 'HASH' &&
                   3996:                    ref($oldsetting) eq 'HASH' &&
                   3997:                    ref($oldsettingtext) eq 'HASH' &&
                   3998:                    ref($newsetting) eq 'HASH' &&
                   3999:                    ref($newsettingtext) eq 'HASH');
                   4000:     my %lt=&Apache::lonlocal::texthash(
1.372     raeburn  4001:          'ui'             => 'User Information',
1.334     raeburn  4002:          'uic'            => 'User Information Changed',
                   4003:          'firstname'      => 'First Name',
                   4004:          'middlename'     => 'Middle Name',
                   4005:          'lastname'       => 'Last Name',
                   4006:          'generation'     => 'Generation',
                   4007:          'id'             => 'Student/Employee ID',
                   4008:          'permanentemail' => 'Permanent e-mail address',
1.378     raeburn  4009:          'portfolioquota' => 'Disk space allocated to portfolio files',
1.385     bisitz   4010:          'authorquota'    => 'Disk space allocated to Authoring Space',
1.334     raeburn  4011:          'blog'           => 'Blog Availability',
1.361     raeburn  4012:          'webdav'         => 'WebDAV Availability',
1.334     raeburn  4013:          'aboutme'        => 'Personal Information Page Availability',
                   4014:          'portfolio'      => 'Portfolio Availability',
1.470     raeburn  4015:          'portaccess'     => 'Portfolio Shareable',
1.459     raeburn  4016:          'timezone'       => 'Can set own Time Zone',
1.334     raeburn  4017:          'official'       => 'Can Request Official Courses',
                   4018:          'unofficial'     => 'Can Request Unofficial Courses',
                   4019:          'community'      => 'Can Request Communities',
1.384     raeburn  4020:          'textbook'       => 'Can Request Textbook Courses',
1.411     raeburn  4021:          'placement'      => 'Can Request Placement Tests',
1.449     raeburn  4022:          'lti'            => 'Can Request LTI Courses',
1.362     raeburn  4023:          'requestauthor'  => 'Can Request Author Role',
1.334     raeburn  4024:          'inststatus'     => "Affiliation",
                   4025:          'prvs'           => 'Previous Value:',
1.470     raeburn  4026:          'chto'           => 'Changed To:',
                   4027:          'editors'        => "Available Editors in Authoring Space",
                   4028:          'managers'       => "Co-authors who can add/revoke co-authors",
                   4029:          'edit'           => 'Standard editor (Edit)',
                   4030:          'xml'            => 'Text editor (EditXML)',
                   4031:          'daxe'           => 'Daxe editor (Daxe)',
1.334     raeburn  4032:     );
                   4033:     if ($changed) {
1.372     raeburn  4034:         $r->print('<h3>'.$lt{'uic'}.'</h3>'.
1.367     golterma 4035:                 &Apache::loncommon::start_data_table().
                   4036:                 &Apache::loncommon::start_data_table_header_row());
1.334     raeburn  4037:         $r->print("<th>&nbsp;</th>\n");
1.367     golterma 4038:         $r->print('<th><b>'.$lt{'prvs'}.'</b></th>');
                   4039:         $r->print('<th><span class="LC_nobreak"><b>'.$lt{'chto'}.'</b></span></th>');
                   4040:         $r->print(&Apache::loncommon::end_data_table_header_row());
                   4041:         my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
                   4042: 
1.334     raeburn  4043:         foreach my $item (@userinfo) {
                   4044:             my $value = $env{'form.c'.$item};
1.367     golterma 4045:             #show changes only:
1.383     raeburn  4046:             unless ($value eq $userenv->{$item}){
1.367     golterma 4047:                 $r->print(&Apache::loncommon::start_data_table_row());
                   4048:                 $r->print("<td>$lt{$item}</td>\n");
1.383     raeburn  4049:                 $r->print("<td>".$userenv->{$item}."</td>\n");
1.367     golterma 4050:                 $r->print("<td>$value </td>\n");
                   4051:                 $r->print(&Apache::loncommon::end_data_table_row());
1.334     raeburn  4052:             }
                   4053:         }
                   4054:         foreach my $entry (@{$order}) {
1.383     raeburn  4055:             if ($canshow->{$entry}) {
1.470     raeburn  4056:                 if (($entry eq 'requestcourses') || ($entry eq 'reqcrsotherdom') ||
                   4057:                     ($entry eq 'requestauthor') || ($entry eq 'authordefaults')) {
1.383     raeburn  4058:                     my @items;
                   4059:                     if ($entry eq 'requestauthor') {
                   4060:                         @items = ($entry);
1.470     raeburn  4061:                     } elsif ($entry eq 'authordefaults') {
                   4062:                         @items = ('webdav','managers','editors');
1.383     raeburn  4063:                     } else {
                   4064:                         @items = @{$requestcourses};
1.384     raeburn  4065:                     }
1.383     raeburn  4066:                     foreach my $item (@items) {
                   4067:                         if (($newsetting->{$item} ne $oldsetting->{$item}) || 
                   4068:                             ($newsettingtext->{$item} ne $oldsettingtext->{$item})) {
                   4069:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");  
1.470     raeburn  4070:                             $r->print("<td>$lt{$item}</td><td>\n");
                   4071:                             unless ($item eq 'managers') {
                   4072:                                 $r->print($oldsetting->{$item});
                   4073:                             }
1.383     raeburn  4074:                             if ($oldsettingtext->{$item}) {
                   4075:                                 if ($oldsetting->{$item}) {
1.470     raeburn  4076:                                     unless ($item eq 'managers') {
                   4077:                                         $r->print(' -- ');
                   4078:                                     }
1.383     raeburn  4079:                                 }
                   4080:                                 $r->print($oldsettingtext->{$item});
                   4081:                             }
1.470     raeburn  4082:                             $r->print("</td>\n<td>");
                   4083:                             unless ($item eq 'managers') {
                   4084:                                 $r->print($newsetting->{$item});
                   4085:                             }
1.383     raeburn  4086:                             if ($newsettingtext->{$item}) {
                   4087:                                 if ($newsetting->{$item}) {
1.470     raeburn  4088:                                     unless ($item eq 'managers') {
                   4089:                                         $r->print(' -- ');
                   4090:                                     }
1.383     raeburn  4091:                                 }
                   4092:                                 $r->print($newsettingtext->{$item});
                   4093:                             }
                   4094:                             $r->print("</td>\n");
                   4095:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  4096:                         }
                   4097:                     }
                   4098:                 } elsif ($entry eq 'tools') {
                   4099:                     foreach my $item (@{$usertools}) {
1.383     raeburn  4100:                         if ($newsetting->{$item} ne $oldsetting->{$item}) {
                   4101:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   4102:                             $r->print("<td>$lt{$item}</td>\n");
                   4103:                             $r->print("<td>".$oldsetting->{$item}.' '.$oldsettingtext->{$item}."</td>\n");
                   4104:                             $r->print("<td>".$newsetting->{$item}.' '.$newsettingtext->{$item}."</td>\n");
                   4105:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  4106:                         }
                   4107:                     }
1.378     raeburn  4108:                 } elsif ($entry eq 'quota') {
                   4109:                     if ((ref($oldsetting->{$entry}) eq 'HASH') && (ref($oldsettingtext->{$entry}) eq 'HASH') &&
                   4110:                         (ref($newsetting->{$entry}) eq 'HASH') && (ref($newsettingtext->{$entry}) eq 'HASH')) {
                   4111:                         foreach my $name ('portfolio','author') {
1.383     raeburn  4112:                             if ($newsetting->{$entry}->{$name} ne $oldsetting->{$entry}->{$name}) {
                   4113:                                 $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   4114:                                 $r->print("<td>$lt{$name.$entry}</td>\n");
                   4115:                                 $r->print("<td>".$oldsettingtext->{$entry}->{$name}."</td>\n");
                   4116:                                 $r->print("<td>".$newsettingtext->{$entry}->{$name}."</td>\n");
                   4117:                                 $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.378     raeburn  4118:                             }
                   4119:                         }
                   4120:                     }
1.334     raeburn  4121:                 } else {
1.383     raeburn  4122:                     if ($newsetting->{$entry} ne $oldsetting->{$entry}) {
                   4123:                         $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   4124:                         $r->print("<td>$lt{$entry}</td>\n");
                   4125:                         $r->print("<td>".$oldsetting->{$entry}.' '.$oldsettingtext->{$entry}."</td>\n");
                   4126:                         $r->print("<td>".$newsetting->{$entry}.' '.$newsettingtext->{$entry}."</td>\n");
                   4127:                         $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  4128:                     }
                   4129:                 }
                   4130:             }
                   4131:         }
1.367     golterma 4132:         $r->print(&Apache::loncommon::end_data_table().'<br />');
1.372     raeburn  4133:     } else {
                   4134:         $r->print('<h3>'.$lt{'ui'}.'</h3>'.
                   4135:                   '<p>'.&mt('No changes made to user information').'</p>');
1.334     raeburn  4136:     }
                   4137:     return;
                   4138: }
                   4139: 
1.275     raeburn  4140: sub tool_changes {
                   4141:     my ($context,$usertools,$oldaccess,$oldaccesstext,$userenv,$changeHash,
                   4142:         $changed,$newaccess,$newaccesstext) = @_;
                   4143:     if (!((ref($usertools) eq 'ARRAY') && (ref($oldaccess) eq 'HASH') &&
                   4144:           (ref($oldaccesstext) eq 'HASH') && (ref($userenv) eq 'HASH') &&
                   4145:           (ref($changeHash) eq 'HASH') && (ref($changed) eq 'HASH') &&
                   4146:           (ref($newaccess) eq 'HASH') && (ref($newaccesstext) eq 'HASH'))) {
                   4147:         return;
                   4148:     }
1.383     raeburn  4149:     my %reqdisplay = &requestchange_display();
1.300     raeburn  4150:     if ($context eq 'reqcrsotherdom') {
1.309     raeburn  4151:         my @options = ('approval','validate','autolimit');
1.306     raeburn  4152:         my $optregex = join('|',@options);
1.300     raeburn  4153:         my $cdom = $env{'request.role.domain'};
                   4154:         foreach my $tool (@{$usertools}) {
1.383     raeburn  4155:             $oldaccesstext->{$tool} = &mt("availability set to 'off'");
1.314     raeburn  4156:             $newaccesstext->{$tool} = $oldaccesstext->{$tool};
1.300     raeburn  4157:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
1.383     raeburn  4158:             my ($newop,$limit);
1.314     raeburn  4159:             if ($env{'form.'.$context.'_'.$tool}) {
                   4160:                 $newop = $env{'form.'.$context.'_'.$tool};
                   4161:                 if ($newop eq 'autolimit') {
1.383     raeburn  4162:                     $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
1.314     raeburn  4163:                     $limit =~ s/\D+//g;
                   4164:                     $newop .= '='.$limit;
                   4165:                 }
                   4166:             }
1.300     raeburn  4167:             if ($userenv->{$context.'.'.$tool} eq '') {
1.314     raeburn  4168:                 if ($newop) {
                   4169:                     $changed->{$tool}=&tool_admin($tool,$cdom.':'.$newop,
1.300     raeburn  4170:                                                   $changeHash,$context);
                   4171:                     if ($changed->{$tool}) {
1.383     raeburn  4172:                         if ($newop =~ /^autolimit/) {
                   4173:                             if ($limit) {
                   4174:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   4175:                             } else {
                   4176:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   4177:                             }
                   4178:                         } else {
                   4179:                             $newaccesstext->{$tool} = $reqdisplay{$newop};
                   4180:                         }
1.300     raeburn  4181:                     } else {
                   4182:                         $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   4183:                     }
                   4184:                 }
                   4185:             } else {
                   4186:                 my @curr = split(',',$userenv->{$context.'.'.$tool});
                   4187:                 my @new;
                   4188:                 my $changedoms;
1.314     raeburn  4189:                 foreach my $req (@curr) {
                   4190:                     if ($req =~ /^\Q$cdom\E\:($optregex\=?\d*)$/) {
                   4191:                         my $oldop = $1;
1.383     raeburn  4192:                         if ($oldop =~ /^autolimit=(\d*)/) {
                   4193:                             my $limit = $1;
                   4194:                             if ($limit) {
                   4195:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   4196:                             } else {
                   4197:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   4198:                             }
                   4199:                         } else {
                   4200:                             $oldaccesstext->{$tool} = $reqdisplay{$oldop};
                   4201:                         }
1.314     raeburn  4202:                         if ($oldop ne $newop) {
                   4203:                             $changedoms = 1;
                   4204:                             foreach my $item (@curr) {
                   4205:                                 my ($reqdom,$option) = split(':',$item);
                   4206:                                 unless ($reqdom eq $cdom) {
                   4207:                                     push(@new,$item);
                   4208:                                 }
                   4209:                             }
                   4210:                             if ($newop) {
                   4211:                                 push(@new,$cdom.':'.$newop);
1.300     raeburn  4212:                             }
1.314     raeburn  4213:                             @new = sort(@new);
1.300     raeburn  4214:                         }
1.314     raeburn  4215:                         last;
1.300     raeburn  4216:                     }
1.314     raeburn  4217:                 }
                   4218:                 if ((!$changedoms) && ($newop)) {
1.300     raeburn  4219:                     $changedoms = 1;
1.306     raeburn  4220:                     @new = sort(@curr,$cdom.':'.$newop);
1.300     raeburn  4221:                 }
                   4222:                 if ($changedoms) {
1.314     raeburn  4223:                     my $newdomstr;
1.300     raeburn  4224:                     if (@new) {
                   4225:                         $newdomstr = join(',',@new);
                   4226:                     }
                   4227:                     $changed->{$tool}=&tool_admin($tool,$newdomstr,$changeHash,
                   4228:                                                   $context);
                   4229:                     if ($changed->{$tool}) {
                   4230:                         if ($env{'form.'.$context.'_'.$tool}) {
1.306     raeburn  4231:                             if ($env{'form.'.$context.'_'.$tool} eq 'autolimit') {
1.314     raeburn  4232:                                 my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   4233:                                 $limit =~ s/\D+//g;
                   4234:                                 if ($limit) {
1.383     raeburn  4235:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
1.314     raeburn  4236:                                 } else {
1.383     raeburn  4237:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
1.306     raeburn  4238:                                 }
1.314     raeburn  4239:                             } else {
1.306     raeburn  4240:                                 $newaccesstext->{$tool} = $reqdisplay{$env{'form.'.$context.'_'.$tool}};
                   4241:                             }
1.300     raeburn  4242:                         } else {
1.383     raeburn  4243:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
1.300     raeburn  4244:                         }
                   4245:                     }
                   4246:                 }
                   4247:             }
                   4248:         }
                   4249:         return;
                   4250:     }
1.470     raeburn  4251:     my %tooldesc = &Apache::lonlocal::texthash(
                   4252:         'edit' => 'Standard editor (Edit)',
                   4253:         'xml'  => 'Text editor (EditXML)',
                   4254:         'daxe' => 'Daxe editor (Daxe)',
                   4255:     );
1.275     raeburn  4256:     foreach my $tool (@{$usertools}) {
1.383     raeburn  4257:         my ($newval,$limit,$envkey);
1.362     raeburn  4258:         $envkey = $context.'.'.$tool;
1.306     raeburn  4259:         if ($context eq 'requestcourses') {
                   4260:             $newval = $env{'form.crsreq_'.$tool};
                   4261:             if ($newval eq 'autolimit') {
1.383     raeburn  4262:                 $limit = $env{'form.crsreq_'.$tool.'_limit'};
                   4263:                 $limit =~ s/\D+//g;
                   4264:                 $newval .= '='.$limit;
1.306     raeburn  4265:             }
1.362     raeburn  4266:         } elsif ($context eq 'requestauthor') {
                   4267:             $newval = $env{'form.'.$context};
                   4268:             $envkey = $context;
1.470     raeburn  4269:         } elsif ($context eq 'authordefaults') {
                   4270:             if ($tool eq 'editors') {
                   4271:                 $envkey = 'authoreditors';
                   4272:                 if ($env{'form.customeditors'} == 1) {
                   4273:                     my @editors;
                   4274:                     my @posseditors = &Apache::loncommon::get_env_multiple('form.custom_editor');
                   4275:                     if (@posseditors) {
                   4276:                         foreach my $editor (@posseditors) {
                   4277:                             if (grep(/^\Q$editor\E$/,@posseditors)) {
                   4278:                                 unless (grep(/^\Q$editor\E$/,@editors)) {
                   4279:                                     push(@editors,$editor);
                   4280:                                 }
                   4281:                             }
                   4282:                         }
                   4283:                     }
                   4284:                     if (@editors) {
                   4285:                         $newval = join(',',(sort(@editors)));
                   4286:                     }
                   4287:                 }
                   4288:             } elsif ($tool eq 'managers') {
                   4289:                 $envkey = 'authormanagers';
                   4290:                 my @possibles = &Apache::loncommon::get_env_multiple('form.custommanagers');
                   4291:                 if (@possibles) {
                   4292:                     my %ca_roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},
                   4293:                                                                  undef,['active','future'],['ca']);
                   4294:                     if (keys(%ca_roles)) {
                   4295:                         my @custommanagers;
                   4296:                         foreach my $user (@possibles) {
                   4297:                             if ($user =~ /^($match_username):($match_domain)$/) {
                   4298:                                 if (exists($ca_roles{$user.':ca'})) {
                   4299:                                     unless ($user eq $env{'form.ccuname'}.':'.$env{'form.ccdomain'}) {
                   4300:                                         push(@custommanagers,$user);
                   4301:                                     }
                   4302:                                 }
                   4303:                             }
                   4304:                         }
                   4305:                         if (@custommanagers) {
                   4306:                             $newval = join(',',sort(@custommanagers));
                   4307:                         }
                   4308:                     }
                   4309:                 }
                   4310:             } elsif ($tool eq 'webdav') {
                   4311:                 $envkey = 'tools.webdav';
                   4312:                 $newval = $env{'form.'.$context.'_'.$tool};
                   4313:             }
1.314     raeburn  4314:         } else {
1.306     raeburn  4315:             $newval = $env{'form.'.$context.'_'.$tool};
                   4316:         }
1.362     raeburn  4317:         if ($userenv->{$envkey} ne '') {
1.275     raeburn  4318:             $oldaccess->{$tool} = &mt('custom');
1.383     raeburn  4319:             if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   4320:                 if ($userenv->{$envkey} =~ /^autolimit=(\d*)$/) {
                   4321:                     my $currlimit = $1;
                   4322:                     if ($currlimit eq '') {
                   4323:                         $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   4324:                     } else {
                   4325:                         $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$currlimit);
                   4326:                     }
                   4327:                 } elsif ($userenv->{$envkey}) {
                   4328:                     $oldaccesstext->{$tool} = $reqdisplay{$userenv->{$envkey}};
                   4329:                 } else {
                   4330:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   4331:                 }
1.470     raeburn  4332:             } elsif ($context eq 'authordefaults') {
                   4333:                 if ($tool eq 'managers') {
                   4334:                     if ($userenv->{$envkey} eq '') {
                   4335:                         $oldaccesstext->{$tool} = &mt('Only author may manage co-author roles');
                   4336:                     } else {
                   4337:                         my $managers = $userenv->{$envkey};
                   4338:                         $managers =~ s/,/, /g;
                   4339:                         $oldaccesstext->{$tool} = $managers;
                   4340:                     }
                   4341:                 } elsif ($tool eq 'editors') {
                   4342:                     $oldaccesstext->{$tool} = &mt('can use: [_1]',
                   4343:                                                   join(', ', map { $tooldesc{$_} } split(/,/,$userenv->{$envkey})));
                   4344:                 } elsif ($tool eq 'webdav') {
                   4345:                     if ($userenv->{$envkey}) {
                   4346:                         $oldaccesstext->{$tool} = &mt("availability set to 'on'");
                   4347:                     } else {
                   4348:                         $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   4349:                     }
                   4350:                 }
1.275     raeburn  4351:             } else {
1.383     raeburn  4352:                 if ($userenv->{$envkey}) {
                   4353:                     $oldaccesstext->{$tool} = &mt("availability set to 'on'");
                   4354:                 } else {
                   4355:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   4356:                 }
1.275     raeburn  4357:             }
1.362     raeburn  4358:             $changeHash->{$envkey} = $userenv->{$envkey};
1.470     raeburn  4359:             if (($env{'form.custom'.$tool} == 1) ||
                   4360:                 (($context eq 'authordefaults') && ($tool eq 'managers') && ($newval ne ''))) {
1.362     raeburn  4361:                 if ($newval ne $userenv->{$envkey}) {
1.306     raeburn  4362:                     $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   4363:                                                     $context);
1.275     raeburn  4364:                     if ($changed->{$tool}) {
                   4365:                         $newaccess->{$tool} = &mt('custom');
1.383     raeburn  4366:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   4367:                             if ($newval =~ /^autolimit/) {
                   4368:                                 if ($limit) {
                   4369:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   4370:                                 } else {
                   4371:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   4372:                                 }
                   4373:                             } elsif ($newval) {
                   4374:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
                   4375:                             } else {
                   4376:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   4377:                             }
1.470     raeburn  4378:                         } elsif ($context eq 'authordefaults') {
                   4379:                             if ($tool eq 'editors') {
                   4380:                                 $newaccesstext->{$tool} = &mt('can use: [_1]',
                   4381:                                                               join(', ', map { $tooldesc{$_} } split(/,/,$changeHash->{$envkey})));
                   4382:                             } elsif ($tool eq 'managers') {
                   4383:                                 if ($changeHash->{$envkey} eq '') {
                   4384:                                     $newaccesstext->{$tool} = &mt('Only author may manage co-author roles');
                   4385:                                 } else {
                   4386:                                     my $managers = $changeHash->{$envkey};
                   4387:                                     $managers =~ s/,/, /g;
                   4388:                                     $newaccesstext->{$tool} = $managers;
                   4389:                                 }
                   4390:                             } elsif ($tool eq 'webdav') {
                   4391:                                 if ($newval) {
                   4392:                                     $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   4393:                                 } else {
                   4394:                                     $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   4395:                                 }
                   4396:                             }
1.275     raeburn  4397:                         } else {
1.383     raeburn  4398:                             if ($newval) {
                   4399:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   4400:                             } else {
                   4401:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   4402:                             }
1.275     raeburn  4403:                         }
                   4404:                     } else {
                   4405:                         $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  4406:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.470     raeburn  4407:                             if ($userenv->{$envkey} =~ /^autolimit/) {
1.383     raeburn  4408:                                 if ($limit) {
                   4409:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   4410:                                 } else {
                   4411:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   4412:                                 }
1.470     raeburn  4413:                             } elsif ($userenv->{$envkey}) {
                   4414:                                 $newaccesstext->{$tool} = $reqdisplay{$userenv->{$envkey}};
1.383     raeburn  4415:                             } else {
                   4416:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   4417:                             }
1.470     raeburn  4418:                         } elsif ($context eq 'authordefaults') {
                   4419:                             if ($tool eq 'editors') {
                   4420:                                 $newaccesstext->{$tool} = &mt('can use: [_1]',
                   4421:                                                               join(', ', map { $tooldesc{$_} } split(/,/,$userenv->{$envkey})));
                   4422:                             } elsif ($tool eq 'managers') {
                   4423:                                 if ($userenv->{$envkey} eq '') {
                   4424:                                     $newaccesstext->{$tool} = &mt('Only author may manage co-author roles');
                   4425:                                 } else {
                   4426:                                     my $managers = $userenv->{$envkey};
                   4427:                                     $managers =~ s/,/, /g;
                   4428:                                     $newaccesstext->{$tool} = $managers;
                   4429:                                 }
                   4430:                             } elsif ($tool eq 'webdav') {
                   4431:                                 if ($userenv->{$envkey}) {
                   4432:                                     $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   4433:                                 } else {
                   4434:                                     $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   4435:                                 }
                   4436:                             }
1.275     raeburn  4437:                         } else {
1.383     raeburn  4438:                             if ($userenv->{$context.'.'.$tool}) {
                   4439:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   4440:                             } else {
                   4441:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   4442:                             }
1.275     raeburn  4443:                         }
                   4444:                     }
                   4445:                 } else {
                   4446:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   4447:                     $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   4448:                 }
                   4449:             } else {
                   4450:                 $changed->{$tool} = &tool_admin($tool,'',$changeHash,$context);
                   4451:                 if ($changed->{$tool}) {
                   4452:                     $newaccess->{$tool} = &mt('default');
                   4453:                 } else {
                   4454:                     $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  4455:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   4456:                         if ($newval =~ /^autolimit/) {
                   4457:                             if ($limit) {
                   4458:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   4459:                             } else {
                   4460:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   4461:                             }
                   4462:                         } elsif ($newval) {
                   4463:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   4464:                         } else {
                   4465:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   4466:                         }
1.470     raeburn  4467:                     } elsif ($context eq 'authordefaults') {
                   4468:                         if ($tool eq 'editors') {
                   4469:                             $newaccesstext->{$tool} = &mt('can use: [_1]',
                   4470:                                                           join(', ', map { $tooldesc{$_} } split(/,/,$newval)));
                   4471:                         } elsif ($tool eq 'managers') {
                   4472:                             if ($newval eq '') {
                   4473:                                 $newaccesstext->{$tool} = &mt('Only author may manage co-author roles');
                   4474:                             } else {
                   4475:                                 my $managers = $newval;
                   4476:                                 $managers =~ s/,/, /g;
                   4477:                                 $newaccesstext->{$tool} = $managers;
                   4478:                             }
                   4479:                         } elsif ($tool eq 'webdav') {
                   4480:                             if ($userenv->{$envkey}) {
                   4481:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   4482:                             } else {
                   4483:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   4484:                             }
                   4485:                         }
1.275     raeburn  4486:                     } else {
1.383     raeburn  4487:                         if ($userenv->{$context.'.'.$tool}) {
                   4488:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   4489:                         } else {
                   4490:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   4491:                         }
1.275     raeburn  4492:                     }
                   4493:                 }
                   4494:             }
                   4495:         } else {
                   4496:             $oldaccess->{$tool} = &mt('default');
1.470     raeburn  4497:             if (($env{'form.custom'.$tool} == 1) ||
                   4498:                 (($context eq 'authordefaults') && ($tool eq 'managers') && ($newval ne ''))) {
1.306     raeburn  4499:                 $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   4500:                                                 $context);
1.275     raeburn  4501:                 if ($changed->{$tool}) {
                   4502:                     $newaccess->{$tool} = &mt('custom');
1.383     raeburn  4503:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   4504:                         if ($newval =~ /^autolimit/) {
                   4505:                             if ($limit) {
                   4506:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   4507:                             } else {
                   4508:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   4509:                             }
                   4510:                         } elsif ($newval) {
                   4511:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   4512:                         } else {
                   4513:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   4514:                         }
1.470     raeburn  4515:                     } elsif ($context eq 'authordefaults') {
                   4516:                         if ($tool eq 'managers') {
                   4517:                             if ($newval eq '') {
                   4518:                                 $newaccesstext->{$tool} = &mt('Only author may manage co-author roles');
                   4519:                             } else {
                   4520:                                 my $managers = $newval;
                   4521:                                 $managers =~ s/,/, /g;
                   4522:                                 $newaccesstext->{$tool} = $managers;
                   4523:                             }
                   4524:                         } elsif ($tool eq 'editors') {
                   4525:                             $newaccesstext->{$tool} = &mt('can use: [_1]',
                   4526:                                                           join(', ', map { $tooldesc{$_} } split(/,/,$newval)));
                   4527:                         } elsif ($tool eq 'webdav') {
                   4528:                             if ($newval) {
                   4529:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   4530:                             } else {
                   4531:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   4532:                             }
                   4533:                         }
1.275     raeburn  4534:                     } else {
1.383     raeburn  4535:                         if ($newval) {
                   4536:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   4537:                         } else {
                   4538:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   4539:                         }
1.275     raeburn  4540:                     }
                   4541:                 } else {
                   4542:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   4543:                 }
                   4544:             } else {
                   4545:                 $newaccess->{$tool} = $oldaccess->{$tool};
                   4546:             }
                   4547:         }
                   4548:     }
                   4549:     return;
                   4550: }
                   4551: 
1.220     raeburn  4552: sub update_roles {
1.375     raeburn  4553:     my ($r,$context,$showcredits) = @_;
1.4       www      4554:     my $now=time;
1.225     raeburn  4555:     my @rolechanges;
1.465     raeburn  4556:     my (%disallowed,%got_role_approvals,%got_instdoms,%process_by,%instdoms,
1.466     raeburn  4557:         %pending,%reject,%notifydc,%status,%unauthorized,%currqueued);
1.465     raeburn  4558:     $got_role_approvals{$context} = '';
                   4559:     $process_by{$context} = {};
                   4560:     my @domroles = &Apache::lonuserutils::domain_roles();
                   4561:     my @cstrroles = &Apache::lonuserutils::construction_space_roles();
                   4562:     my @courseroles = &Apache::lonuserutils::roles_by_context('course',1);
1.73      sakharuk 4563:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
1.404     raeburn  4564:     foreach my $key (keys(%env)) {
1.135     raeburn  4565: 	next if (! $env{$key});
1.190     raeburn  4566:         next if ($key eq 'form.action');
1.27      matthew  4567: 	# Revoke roles
1.135     raeburn  4568: 	if ($key=~/^form\.rev/) {
                   4569: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
1.64      www      4570: # Revoke standard role
1.170     albertel 4571: 		my ($scope,$role) = ($1,$2);
                   4572: 		my $result =
                   4573: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
                   4574: 						$env{'form.ccuname'},
1.239     raeburn  4575: 						$scope,$role,'','',$context);
1.367     golterma 4576:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   4577:                             &mt('Revoking [_1] in [_2]',
                   4578:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  4579:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   4580:                                 $result ne "ok").'<br />');
                   4581:                 if ($result ne "ok") {
                   4582:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4583:                 }
1.170     albertel 4584: 		if ($role eq 'st') {
1.202     raeburn  4585: 		    my $result = 
1.198     raeburn  4586:                         &Apache::lonuserutils::classlist_drop($scope,
                   4587:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  4588: 			    $now);
1.367     golterma 4589:                     $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.53      www      4590: 		}
1.225     raeburn  4591:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   4592:                     push(@rolechanges,$role);
                   4593:                 }
1.196     raeburn  4594: 	    }
1.195     raeburn  4595: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}s) {
1.64      www      4596: # Revoke custom role
1.369     bisitz   4597:                 my $result = &Apache::lonnet::revokecustomrole(
                   4598:                     $env{'form.ccdomain'},$env{'form.ccuname'},$1,$2,$3,$4,'','',$context);
1.367     golterma 4599:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   4600:                             &mt('Revoking custom role [_1] by [_2] in [_3]',
1.372     raeburn  4601:                                 $4,$3.':'.$2,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   4602:                             $result ne 'ok').'<br />');
                   4603:                 if ($result ne "ok") {
                   4604:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4605:                 }
1.225     raeburn  4606:                 if (!grep(/^cr$/,@rolechanges)) {
                   4607:                     push(@rolechanges,'cr');
                   4608:                 }
1.64      www      4609: 	    }
1.135     raeburn  4610: 	} elsif ($key=~/^form\.del/) {
                   4611: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
1.116     raeburn  4612: # Delete standard role
1.170     albertel 4613: 		my ($scope,$role) = ($1,$2);
                   4614: 		my $result =
                   4615: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
                   4616: 						$env{'form.ccuname'},
1.239     raeburn  4617: 						$scope,$role,$now,0,1,'',
                   4618:                                                 $context);
1.367     golterma 4619:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   4620:                             &mt('Deleting [_1] in [_2]',
1.369     bisitz   4621:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  4622:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   4623:                             $result ne 'ok').'<br />');
                   4624:                 if ($result ne "ok") {
                   4625:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4626:                 }
1.367     golterma 4627: 
1.170     albertel 4628: 		if ($role eq 'st') {
1.202     raeburn  4629: 		    my $result = 
1.198     raeburn  4630:                         &Apache::lonuserutils::classlist_drop($scope,
                   4631:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  4632: 			    $now);
1.369     bisitz   4633: 		    $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.81      albertel 4634: 		}
1.225     raeburn  4635:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   4636:                     push(@rolechanges,$role);
                   4637:                 }
1.116     raeburn  4638:             }
1.139     albertel 4639: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  4640:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   4641: # Delete custom role
1.369     bisitz   4642:                 my $result =
                   4643:                     &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
                   4644:                         $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
                   4645:                         0,1,$context);
                   4646:                 $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Deleting custom role [_1] by [_2] in [_3]',
1.372     raeburn  4647:                       $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   4648:                       $result ne "ok").'<br />');
                   4649:                 if ($result ne "ok") {
                   4650:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4651:                 }
1.367     golterma 4652: 
1.225     raeburn  4653:                 if (!grep(/^cr$/,@rolechanges)) {
                   4654:                     push(@rolechanges,'cr');
                   4655:                 }
1.116     raeburn  4656:             }
1.135     raeburn  4657: 	} elsif ($key=~/^form\.ren/) {
1.101     albertel 4658:             my $udom = $env{'form.ccdomain'};
                   4659:             my $uname = $env{'form.ccuname'};
1.116     raeburn  4660: # Re-enable standard role
1.135     raeburn  4661: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
1.89      raeburn  4662:                 my $url = $1;
                   4663:                 my $role = $2;
1.465     raeburn  4664:                 my $id = $url.'_'.$role;
1.89      raeburn  4665:                 my $logmsg;
                   4666:                 my $output;
                   4667:                 if ($role eq 'st') {
1.141     albertel 4668:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
1.374     raeburn  4669:                         my ($cdom,$cnum,$csec) = ($1,$2,$3);
1.375     raeburn  4670:                         my $credits;
                   4671:                         if ($showcredits) {
1.465     raeburn  4672:                             my $defaultcredits =
1.375     raeburn  4673:                                 &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   4674:                             $credits = &get_user_credits($defaultcredits,$cdom,$cnum);
                   4675:                         }
1.465     raeburn  4676:                         unless ($udom eq $cdom) {
                   4677:                             next if (&Apache::lonuserutils::restricted_dom($context,$id,$udom,
                   4678:                                          $uname,$role,$now,0,$cdom,$cnum,$csec,$credits,
                   4679:                                          \%process_by,\%instdoms,\%got_role_approvals,
1.466     raeburn  4680:                                          \%got_instdoms,\%reject,\%pending,\%notifydc,
                   4681:                                          \%status,\%unauthorized,\%currqueued));
1.465     raeburn  4682:                         }
1.375     raeburn  4683:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$cdom,$cnum,$csec,$context,$credits);
1.220     raeburn  4684:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course') || ($result eq 'refused')) {
1.223     raeburn  4685:                             if ($result eq 'refused' && $logmsg) {
                   4686:                                 $output = $logmsg;
                   4687:                             } else { 
1.369     bisitz   4688:                                 $output = &mt('Error: [_1]',$result)."\n";
1.223     raeburn  4689:                             }
1.89      raeburn  4690:                         } else {
1.372     raeburn  4691:                             $output = &Apache::lonhtmlcommon::confirm_success(&mt('Assigning [_1] in [_2] starting [_3]',
                   4692:                                         &Apache::lonnet::plaintext($role),
                   4693:                                         &Apache::loncommon::show_role_extent($url,$context,'st'),
                   4694:                                         &Apache::lonlocal::locallocaltime($now))).'<br />'.$logmsg.'<br />';
1.89      raeburn  4695:                         }
                   4696:                     }
                   4697:                 } else {
1.465     raeburn  4698:                     my ($cdom,$cnum,$csec);
                   4699:                     if (grep(/^\Q$role\E$/,@cstrroles)) {
                   4700:                         ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_username)$});
                   4701:                     } elsif (grep(/^\Q$role\E$/,@domroles)) {
                   4702:                         ($cdom) = ($url =~ m{^/($match_domain)/$});
                   4703:                     } elsif ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
                   4704:                         ($cdom,$cnum,$csec) = ($1,$2,$3);
                   4705:                     }
                   4706:                     if ($cdom ne '') {
                   4707:                         unless ($udom eq $cdom) {
                   4708:                             next if (&Apache::lonuserutils::restricted_dom($context,$id,$udom,
                   4709:                                          $uname,$role,$now,0,$cdom,$cnum,$csec,'',\%process_by,
                   4710:                                          \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
1.466     raeburn  4711:                                          \%pending,\%notifydc,\%status,\%unauthorized,\%currqueued));
1.465     raeburn  4712:                         }
                   4713:                     }
1.101     albertel 4714: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
1.239     raeburn  4715:                                $env{'form.ccuname'},$url,$role,0,$now,'','',
                   4716:                                $context);
1.461     raeburn  4717:                     $output = &Apache::lonhtmlcommon::confirm_success(&mt('Re-enabling [_1] in [_2]',
                   4718:                                     &Apache::lonnet::plaintext($role),
                   4719:                                     &Apache::loncommon::show_role_extent($url,$context,$role)),$result ne "ok").'<br />';
1.369     bisitz   4720:                     if ($result ne "ok") {
                   4721:                         $output .= &mt('Error: [_1]',$result).'<br />';
                   4722:                     }
                   4723:                 }
1.89      raeburn  4724:                 $r->print($output);
1.225     raeburn  4725:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   4726:                     push(@rolechanges,$role);
                   4727:                 }
1.113     raeburn  4728: 	    }
1.116     raeburn  4729: # Re-enable custom role
1.139     albertel 4730: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  4731:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
1.465     raeburn  4732:                 my $id = $url.'_cr'."/$rdom/$rnam/$rolename";
                   4733:                 my $role = "cr/$rdom/$rnam/$rolename";
                   4734:                 if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
                   4735:                     my ($cdom,$cnum,$csec) = ($1,$2,$3);
                   4736:                     unless ($udom eq $cdom) {
                   4737:                         next if (&Apache::lonuserutils::restricted_dom($context,$id,$udom,
                   4738:                                      $uname,$role,$now,0,$cdom,$cnum,$csec,'',\%process_by,
                   4739:                                      \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
1.466     raeburn  4740:                                      \%pending,\%notifydc,\%status,\%unauthorized,\%currqueued));
1.465     raeburn  4741:                     }
                   4742:                 }
1.116     raeburn  4743:                 my $result = &Apache::lonnet::assigncustomrole(
                   4744:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
1.240     raeburn  4745:                                $url,$rdom,$rnam,$rolename,0,$now,undef,$context);
1.369     bisitz   4746:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   4747:                     &mt('Re-enabling custom role [_1] by [_2] in [_3]',
1.372     raeburn  4748:                         $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   4749:                     $result ne "ok").'<br />');
                   4750:                 if ($result ne "ok") {
                   4751:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4752:                 }
1.225     raeburn  4753:                 if (!grep(/^cr$/,@rolechanges)) {
                   4754:                     push(@rolechanges,'cr');
                   4755:                 }
1.116     raeburn  4756:             }
1.135     raeburn  4757: 	} elsif ($key=~/^form\.act/) {
1.101     albertel 4758:             my $udom = $env{'form.ccdomain'};
                   4759:             my $uname = $env{'form.ccuname'};
1.141     albertel 4760: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
1.65      www      4761:                 # Activate a custom role
1.83      albertel 4762: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
                   4763: 		my $url='/'.$one.'/'.$two;
1.465     raeburn  4764:                 my $id = $url.'_cr/'."$three/$four/$five";
                   4765:                 my $role = "cr/$three/$four/$five";
1.83      albertel 4766: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
1.65      www      4767: 
1.101     albertel 4768:                 my $start = ( $env{'form.start_'.$full} ?
                   4769:                               $env{'form.start_'.$full} :
1.88      raeburn  4770:                               $now );
1.101     albertel 4771:                 my $end   = ( $env{'form.end_'.$full} ?
                   4772:                               $env{'form.end_'.$full} :
1.88      raeburn  4773:                               0 );
1.465     raeburn  4774: 
1.88      raeburn  4775:                 # split multiple sections
                   4776:                 my %sections = ();
1.461     raeburn  4777:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$five);
1.88      raeburn  4778:                 if ($num_sections == 0) {
1.465     raeburn  4779:                     unless ($udom eq $one) {
                   4780:                         next if (&Apache::lonuserutils::restricted_dom($context,$id,$udom,
                   4781:                                      $uname,$role,$start,$end,$one,$two,'','',\%process_by,
                   4782:                                      \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
1.466     raeburn  4783:                                      \%pending,\%notifydc,\%status,\%unauthorized,\%currqueued));
1.465     raeburn  4784:                     }
1.240     raeburn  4785:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end,$context));
1.88      raeburn  4786:                 } else {
1.114     albertel 4787: 		    my %curr_groups =
1.117     raeburn  4788: 			&Apache::longroup::coursegroups($one,$two);
1.465     raeburn  4789:                     my ($restricted,$numchanges);
1.404     raeburn  4790:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.113     raeburn  4791:                         if (($sec eq 'none') || ($sec eq 'all') || 
                   4792:                             exists($curr_groups{$sec})) {
                   4793:                             $disallowed{$sec} = $url;
                   4794:                             next;
                   4795:                         }
                   4796:                         my $securl = $url.'/'.$sec;
1.465     raeburn  4797:                         my $secid = $securl.'_cr'."/$three/$four/$five";
                   4798:                         undef($restricted);
                   4799:                         unless ($udom eq $one) {
                   4800:                             next if (&Apache::lonuserutils::restricted_dom($context,$secid,$udom,
                   4801:                                          $uname,$role,$start,$end,$one,$two,$sec,'',\%process_by,
                   4802:                                          \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
1.466     raeburn  4803:                                          \%pending,\%notifydc,\%status,\%unauthorized,\%currqueued));
1.465     raeburn  4804:                         }
                   4805:                         $numchanges ++;
1.240     raeburn  4806: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end,$context));
1.88      raeburn  4807:                     }
1.465     raeburn  4808:                     next unless ($numchanges);
1.88      raeburn  4809:                 }
1.225     raeburn  4810:                 if (!grep(/^cr$/,@rolechanges)) {
                   4811:                     push(@rolechanges,'cr');
                   4812:                 }
1.142     raeburn  4813: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
1.27      matthew  4814: 		# Activate roles for sections with 3 id numbers
                   4815: 		# set start, end times, and the url for the class
1.83      albertel 4816: 		my ($one,$two,$three)=($1,$2,$3);
1.461     raeburn  4817: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ?
                   4818: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  4819: 			      $now );
1.461     raeburn  4820: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ?
1.101     albertel 4821: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  4822: 			      0 );
1.83      albertel 4823: 		my $url='/'.$one.'/'.$two;
1.465     raeburn  4824:                 my $id = $url.'_'.$three;
1.88      raeburn  4825:                 # split multiple sections
                   4826:                 my %sections = ();
1.101     albertel 4827:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
1.465     raeburn  4828:                 my ($credits,$numchanges);
1.375     raeburn  4829:                 if ($three eq 'st') {
1.461     raeburn  4830:                     if ($showcredits) {
1.375     raeburn  4831:                         my $defaultcredits = 
                   4832:                             &Apache::lonuserutils::get_defaultcredits($one,$two);
                   4833:                         $credits = $env{'form.credits_'.$one.'_'.$two.'_'.$three};
                   4834:                         $credits =~ s/[^\d\.]//g;
                   4835:                         if ($credits eq $defaultcredits) {
                   4836:                             undef($credits);
                   4837:                         }
                   4838:                     }
                   4839:                 }
1.88      raeburn  4840:                 if ($num_sections == 0) {
1.465     raeburn  4841:                     unless ($udom eq $one) {
                   4842:                         next if (&Apache::lonuserutils::restricted_dom($context,$id,$udom,
                   4843:                                      $uname,$three,$start,$end,$one,$two,'',$credits,\%process_by,
                   4844:                                      \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
1.466     raeburn  4845:                                      \%pending,\%notifydc,\%status,\%unauthorized,\%currqueued));
1.465     raeburn  4846:                     }
                   4847:                     $numchanges ++;
1.375     raeburn  4848:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  4849:                 } else {
1.114     albertel 4850:                     my %curr_groups = 
1.117     raeburn  4851: 			&Apache::longroup::coursegroups($one,$two);
1.88      raeburn  4852:                     my $emptysec = 0;
1.465     raeburn  4853:                     my $restricted;
1.404     raeburn  4854:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.88      raeburn  4855:                         $sec =~ s/\W//g;
1.113     raeburn  4856:                         if ($sec ne '') {
                   4857:                             if (($sec eq 'none') || ($sec eq 'all') || 
                   4858:                                 exists($curr_groups{$sec})) {
                   4859:                                 $disallowed{$sec} = $url;
                   4860:                                 next;
                   4861:                             }
1.88      raeburn  4862:                             my $securl = $url.'/'.$sec;
1.465     raeburn  4863:                             my $secid = $securl.'_'.$three;
                   4864:                             unless ($udom eq $one) {
                   4865:                                 undef($restricted);
                   4866:                                 $restricted = &Apache::lonuserutils::restricted_dom($context,$secid,$udom,
                   4867:                                                   $uname,$three,$start,$end,$one,$two,$sec,$credits,\%process_by,
                   4868:                                                   \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
1.466     raeburn  4869:                                                   \%pending,\%notifydc,\%status,\%unauthorized,\%currqueued);
1.465     raeburn  4870:                                 next if ($restricted);
                   4871:                             }
                   4872:                             $numchanges ++;
1.375     raeburn  4873:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec,$context,$credits));
1.88      raeburn  4874:                         } else {
                   4875:                             $emptysec = 1;
                   4876:                         }
                   4877:                     }
                   4878:                     if ($emptysec) {
1.465     raeburn  4879:                         unless ($udom eq $one) {
                   4880:                             undef($restricted);
                   4881:                             $restricted = &Apache::lonuserutils::restricted_dom($context,$id,$udom,
                   4882:                                               $uname,$three,$start,$end,$one,$two,'',$credits,\%process_by,
                   4883:                                               \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
1.466     raeburn  4884:                                               \%pending,\%notifydc,\%status,\%unauthorized,\%currqueued);
1.465     raeburn  4885:                             next if ($restricted);
                   4886:                         }
                   4887:                         $numchanges ++;
1.375     raeburn  4888:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  4889:                     }
1.465     raeburn  4890:                     next unless ($numchanges);
1.225     raeburn  4891:                 }
                   4892:                 if (!grep(/^\Q$three\E$/,@rolechanges)) {
                   4893:                     push(@rolechanges,$three);
                   4894:                 }
1.135     raeburn  4895: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
1.27      matthew  4896: 		# Activate roles for sections with two id numbers
                   4897: 		# set start, end times, and the url for the class
1.461     raeburn  4898: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ?
                   4899: 			      $env{'form.start_'.$1.'_'.$2} :
1.27      matthew  4900: 			      $now );
1.461     raeburn  4901: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ?
1.101     albertel 4902: 			      $env{'form.end_'.$1.'_'.$2} :
1.27      matthew  4903: 			      0 );
1.225     raeburn  4904:                 my $one = $1;
                   4905:                 my $two = $2;
                   4906: 		my $url='/'.$one.'/';
1.465     raeburn  4907:                 my $id = $url.'_'.$two;
1.468     raeburn  4908:                 my ($cdom,$cnum) = split(/\//,$one);
1.88      raeburn  4909:                 # split multiple sections
                   4910:                 my %sections = ();
1.465     raeburn  4911:                 my ($restricted,$numchanges);
1.225     raeburn  4912:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two},\%sections,$two);
1.88      raeburn  4913:                 if ($num_sections == 0) {
1.465     raeburn  4914:                     unless ($udom eq $one) {
                   4915:                         $restricted = &Apache::lonuserutils::restricted_dom($context,$id,$udom,
1.468     raeburn  4916:                                           $uname,$two,$start,$end,$cdom,$cnum,'','',\%process_by,
1.465     raeburn  4917:                                           \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
1.466     raeburn  4918:                                           \%pending,\%notifydc,\%status,\%unauthorized,\%currqueued);
1.465     raeburn  4919:                         next if ($restricted);
                   4920:                     }
                   4921:                     $numchanges ++;
1.240     raeburn  4922:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  4923:                 } else {
                   4924:                     my $emptysec = 0;
1.404     raeburn  4925:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.88      raeburn  4926:                         if ($sec ne '') {
                   4927:                             my $securl = $url.'/'.$sec;
1.465     raeburn  4928:                             my $secid = $securl.'_'.$two;
                   4929:                             unless ($udom eq $one) {
                   4930:                                 undef($restricted);
                   4931:                                 $restricted = &Apache::lonuserutils::restricted_dom($context,$secid,$udom,
1.468     raeburn  4932:                                                   $uname,$two,$start,$end,$cdom,$cnum,$sec,'',\%process_by,
1.465     raeburn  4933:                                                   \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
1.466     raeburn  4934:                                                   \%pending,\%notifydc,\%status,\%unauthorized,\%currqueued);
1.465     raeburn  4935:                                 next if ($restricted);
                   4936:                             }
                   4937:                             $numchanges ++;
1.240     raeburn  4938:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$two,$start,$end,$one,undef,$sec,$context));
1.88      raeburn  4939:                         } else {
                   4940:                             $emptysec = 1;
                   4941:                         }
                   4942:                     }
                   4943:                     if ($emptysec) {
1.465     raeburn  4944:                         unless ($udom eq $one) {
                   4945:                             undef($restricted);
                   4946:                             $restricted = &Apache::lonuserutils::restricted_dom($context,$id,$udom,
1.468     raeburn  4947:                                               $uname,$two,$start,$end,$cdom,$cnum,'','',\%process_by,
1.465     raeburn  4948:                                               \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
1.466     raeburn  4949:                                               \%pending,\%notifydc,\%status,\%unauthorized,\%currqueued);
1.465     raeburn  4950:                             next if ($restricted);
                   4951:                         }
                   4952:                         $numchanges ++;
1.240     raeburn  4953:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  4954:                     }
1.465     raeburn  4955:                     next unless ($numchanges); 
1.88      raeburn  4956:                 }
1.225     raeburn  4957:                 if (!grep(/^\Q$two\E$/,@rolechanges)) {
                   4958:                     push(@rolechanges,$two);
                   4959:                 }
1.64      www      4960: 	    } else {
1.190     raeburn  4961: 		$r->print('<p><span class="LC_error">'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></span></p><br />');
1.64      www      4962:             }
1.113     raeburn  4963:             foreach my $key (sort(keys(%disallowed))) {
1.274     bisitz   4964:                 $r->print('<p class="LC_warning">');
1.113     raeburn  4965:                 if (($key eq 'none') || ($key eq 'all')) {  
1.274     bisitz   4966:                     $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  4967:                 } else {
1.274     bisitz   4968:                     $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  4969:                 }
1.274     bisitz   4970:                 $r->print('</p><p>'
                   4971:                          .&mt('Please [_1]go back[_2] and choose a different section name.'
                   4972:                              ,'<a href="javascript:history.go(-1)'
                   4973:                              ,'</a>')
                   4974:                          .'</p><br />'
                   4975:                 );
1.113     raeburn  4976:             }
                   4977: 	}
1.101     albertel 4978:     } # End of foreach (keys(%env))
1.466     raeburn  4979:     if ((keys(%reject)) || (keys(%unauthorized))) {
                   4980:         $r->print(&Apache::lonuserutils::print_roles_rejected($context,\%reject,\%unauthorized));
1.465     raeburn  4981:     }
1.466     raeburn  4982:     if ((keys(%pending)) || (keys(%currqueued))) {
                   4983:         $r->print(&Apache::lonuserutils::print_roles_queued($context,\%pending,\%notifydc,\%currqueued));
1.465     raeburn  4984:     }
1.75      www      4985: # Flush the course logs so reverse user roles immediately updated
1.349     raeburn  4986:     $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.225     raeburn  4987:     if (@rolechanges == 0) {
1.372     raeburn  4988:         $r->print('<p>'.&mt('No roles to modify').'</p>');
1.193     raeburn  4989:     }
1.225     raeburn  4990:     return @rolechanges;
1.220     raeburn  4991: }
                   4992: 
1.375     raeburn  4993: sub get_user_credits {
                   4994:     my ($uname,$udom,$defaultcredits,$cdom,$cnum) = @_;
                   4995:     if ($cdom eq '' || $cnum eq '') {
                   4996:         return unless ($env{'request.course.id'});
                   4997:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4998:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4999:     }
                   5000:     my $credits;
                   5001:     my %currhash =
                   5002:         &Apache::lonnet::get('classlist',[$uname.':'.$udom],$cdom,$cnum);
                   5003:     if (keys(%currhash) > 0) {
                   5004:         my @items = split(/:/,$currhash{$uname.':'.$udom});
                   5005:         my $crdidx = &Apache::loncoursedata::CL_CREDITS() - 3;
                   5006:         $credits = $items[$crdidx];
                   5007:         $credits =~ s/[^\d\.]//g;
                   5008:     }
                   5009:     if ($credits eq $defaultcredits) {
                   5010:         undef($credits);
                   5011:     }
                   5012:     return $credits;
                   5013: }
                   5014: 
1.220     raeburn  5015: sub enroll_single_student {
1.375     raeburn  5016:     my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype,
                   5017:         $showcredits,$defaultcredits) = @_;
1.318     raeburn  5018:     $r->print('<h3>');
                   5019:     if ($crstype eq 'Community') {
                   5020:         $r->print(&mt('Enrolling Member'));
                   5021:     } else {
                   5022:         $r->print(&mt('Enrolling Student'));
                   5023:     }
                   5024:     $r->print('</h3>');
1.220     raeburn  5025: 
                   5026:     # Remove non alphanumeric values from section
                   5027:     $env{'form.sections'}=~s/\W//g;
                   5028: 
1.375     raeburn  5029:     my $credits;
                   5030:     if (($showcredits) && ($env{'form.credits'} ne '')) {
                   5031:         $credits = $env{'form.credits'};
                   5032:         $credits =~ s/[^\d\.]//g;
                   5033:         if ($credits ne '') {
                   5034:             if ($credits eq $defaultcredits) {
                   5035:                 undef($credits);
                   5036:             }
                   5037:         }
                   5038:     }
1.465     raeburn  5039:     my ($startdate,$enddate) = &Apache::lonuserutils::get_dates_from_form();
1.466     raeburn  5040:     my (%got_role_approvals,%got_instdoms,%process_by,%instdoms,%pending,%reject,%notifydc,
                   5041:         %status,%unauthorized,%currqueued);
1.465     raeburn  5042:     unless ($env{'form.ccdomain'} eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   5043:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5044:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   5045:         my $csec = $env{'form.sections'};
                   5046:         my $id = "/$cdom/$cnum";
                   5047:         if ($csec ne '') {
                   5048:             $id .= "/$csec";
                   5049:         }
                   5050:         $id .= '_st';
                   5051:         if (&Apache::lonuserutils::restricted_dom($context,$id,$env{'form.ccdomain'},$env{'form.ccuname'},
                   5052:                                                   'st',$startdate,$enddate,$cdom,$cnum,$csec,$credits,
                   5053:                                                   \%process_by,\%instdoms,\%got_role_approvals,\%got_instdoms,
1.466     raeburn  5054:                                                   \%reject,\%pending,\%notifydc,\%status,\%unauthorized,\%currqueued)) {
                   5055:             if ((keys(%reject)) || (keys(%unauthorized))) {
                   5056:                 $r->print(&Apache::lonuserutils::print_roles_rejected($context,\%reject,\%unauthorized));
1.465     raeburn  5057:             }
1.466     raeburn  5058:             if ((keys(%pending)) || (keys(%currqueued))) {
                   5059:                 $r->print(&Apache::lonuserutils::print_roles_queued($context,\%pending,\%notifydc,\%currqueued));
1.465     raeburn  5060:             }
                   5061:             return;
                   5062:         }
                   5063:     }
1.375     raeburn  5064: 
1.220     raeburn  5065:     # Clean out any old student roles the user has in this class.
                   5066:     &Apache::lonuserutils::modifystudent($env{'form.ccdomain'},
                   5067:          $env{'form.ccuname'},$env{'request.course.id'},undef,$uhome);
                   5068:     my $enroll_result =
                   5069:         &Apache::lonnet::modify_student_enrollment($env{'form.ccdomain'},
                   5070:             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
                   5071:             $env{'form.cmiddlename'},$env{'form.clastname'},
                   5072:             $env{'form.generation'},$env{'form.sections'},$enddate,
1.375     raeburn  5073:             $startdate,'manual',undef,$env{'request.course.id'},'',$context,
                   5074:             $credits);
1.220     raeburn  5075:     if ($enroll_result =~ /^ok/) {
1.381     bisitz   5076:         $r->print(&mt('[_1] enrolled','<b>'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.'</b>'));
1.220     raeburn  5077:         if ($env{'form.sections'} ne '') {
                   5078:             $r->print(' '.&mt('in section [_1]',$env{'form.sections'}));
                   5079:         }
                   5080:         my ($showstart,$showend);
                   5081:         if ($startdate <= $now) {
                   5082:             $showstart = &mt('Access starts immediately');
                   5083:         } else {
                   5084:             $showstart = &mt('Access starts: ').&Apache::lonlocal::locallocaltime($startdate);
                   5085:         }
                   5086:         if ($enddate == 0) {
                   5087:             $showend = &mt('ends: no ending date');
                   5088:         } else {
                   5089:             $showend = &mt('ends: ').&Apache::lonlocal::locallocaltime($enddate);
                   5090:         }
                   5091:         $r->print('.<br />'.$showstart.'; '.$showend);
                   5092:         if ($startdate <= $now && !$newuser) {
1.386     bisitz   5093:             $r->print('<p class="LC_info">');
1.318     raeburn  5094:             if ($crstype eq 'Community') {
1.392     raeburn  5095:                 $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  5096:             } else {
1.392     raeburn  5097:                 $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  5098:            }
                   5099:            $r->print('</p>');
1.220     raeburn  5100:         }
                   5101:     } else {
                   5102:         $r->print(&mt('unable to enroll').": ".$enroll_result);
                   5103:     }
                   5104:     return;
1.188     raeburn  5105: }
                   5106: 
1.204     raeburn  5107: sub get_defaultquota_text {
                   5108:     my ($settingstatus) = @_;
                   5109:     my $defquotatext; 
                   5110:     if ($settingstatus eq '') {
1.383     raeburn  5111:         $defquotatext = &mt('default');
1.204     raeburn  5112:     } else {
                   5113:         my ($usertypes,$order) =
                   5114:             &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
                   5115:         if ($usertypes->{$settingstatus} eq '') {
1.383     raeburn  5116:             $defquotatext = &mt('default');
1.204     raeburn  5117:         } else {
1.383     raeburn  5118:             $defquotatext = &mt('default for [_1]',$usertypes->{$settingstatus});
1.204     raeburn  5119:         }
                   5120:     }
                   5121:     return $defquotatext;
                   5122: }
                   5123: 
1.188     raeburn  5124: sub update_result_form {
                   5125:     my ($uhome) = @_;
                   5126:     my $outcome = 
1.367     golterma 5127:     '<form name="userupdate" method="post" action="">'."\n";
1.160     raeburn  5128:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
1.188     raeburn  5129:         $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  5130:     }
1.207     raeburn  5131:     if ($env{'form.origname'} ne '') {
                   5132:         $outcome .= '<input type="hidden" name="origname" value="'.$env{'form.origname'}.'" />'."\n";
                   5133:     }
1.160     raeburn  5134:     foreach my $item ('sortby','seluname','seludom') {
                   5135:         if (exists($env{'form.'.$item})) {
1.188     raeburn  5136:             $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  5137:         }
                   5138:     }
1.188     raeburn  5139:     if ($uhome eq 'no_host') {
                   5140:         $outcome .= '<input type="hidden" name="forcenewuser" value="1" />'."\n";
                   5141:     }
                   5142:     $outcome .= '<input type="hidden" name="phase" value="" />'."\n".
1.383     raeburn  5143:                 '<input type="hidden" name="currstate" value="" />'."\n".
                   5144:                 '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1.188     raeburn  5145:                 '</form>';
                   5146:     return $outcome;
1.4       www      5147: }
                   5148: 
1.149     raeburn  5149: sub quota_admin {
1.378     raeburn  5150:     my ($setquota,$changeHash,$name) = @_;
1.149     raeburn  5151:     my $quotachanged;
                   5152:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   5153:         # Current user has quota modification privileges
1.267     raeburn  5154:         if (ref($changeHash) eq 'HASH') {
                   5155:             $quotachanged = 1;
1.378     raeburn  5156:             $changeHash->{$name.'quota'} = $setquota;
1.267     raeburn  5157:         }
1.149     raeburn  5158:     }
                   5159:     return $quotachanged;
                   5160: }
                   5161: 
1.267     raeburn  5162: sub tool_admin {
1.275     raeburn  5163:     my ($tool,$settool,$changeHash,$context) = @_;
                   5164:     my $canchange = 0; 
1.279     raeburn  5165:     if ($context eq 'requestcourses') {
1.275     raeburn  5166:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
                   5167:             $canchange = 1;
                   5168:         }
1.300     raeburn  5169:     } elsif ($context eq 'reqcrsotherdom') {
                   5170:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   5171:             $canchange = 1;
                   5172:         }
1.362     raeburn  5173:     } elsif ($context eq 'requestauthor') {
                   5174:         if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
                   5175:             $canchange = 1;
                   5176:         }
1.470     raeburn  5177:     } elsif ($context eq 'authordefaults') {
                   5178:         if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
                   5179:             $canchange = 1;
                   5180:         }
1.275     raeburn  5181:     } elsif (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   5182:         # Current user has quota modification privileges
                   5183:         $canchange = 1;
                   5184:     }
1.267     raeburn  5185:     my $toolchanged;
1.275     raeburn  5186:     if ($canchange) {
1.267     raeburn  5187:         if (ref($changeHash) eq 'HASH') {
                   5188:             $toolchanged = 1;
1.362     raeburn  5189:             if ($tool eq 'requestauthor') {
                   5190:                 $changeHash->{$context} = $settool;
1.470     raeburn  5191:             } elsif (($tool eq 'managers') || ($tool eq 'editors')) {
                   5192:                 $changeHash->{'author'.$tool} = $settool;
                   5193:             } elsif ($tool eq 'webdav') {
                   5194:                 $changeHash->{'tools.'.$tool} = $settool;
1.362     raeburn  5195:             } else {
                   5196:                 $changeHash->{$context.'.'.$tool} = $settool;
                   5197:             }
1.267     raeburn  5198:         }
                   5199:     }
                   5200:     return $toolchanged;
                   5201: }
                   5202: 
1.88      raeburn  5203: sub build_roles {
1.89      raeburn  5204:     my ($sectionstr,$sections,$role) = @_;
1.88      raeburn  5205:     my $num_sections = 0;
                   5206:     if ($sectionstr=~ /,/) {
                   5207:         my @secnums = split/,/,$sectionstr;
1.89      raeburn  5208:         if ($role eq 'st') {
                   5209:             $secnums[0] =~ s/\W//g;
                   5210:             $$sections{$secnums[0]} = 1;
                   5211:             $num_sections = 1;
                   5212:         } else {
                   5213:             foreach my $sec (@secnums) {
                   5214:                 $sec =~ ~s/\W//g;
1.150     banghart 5215:                 if (!($sec eq "")) {
1.89      raeburn  5216:                     if (exists($$sections{$sec})) {
                   5217:                         $$sections{$sec} ++;
                   5218:                     } else {
                   5219:                         $$sections{$sec} = 1;
                   5220:                         $num_sections ++;
                   5221:                     }
1.88      raeburn  5222:                 }
                   5223:             }
                   5224:         }
                   5225:     } else {
                   5226:         $sectionstr=~s/\W//g;
                   5227:         unless ($sectionstr eq '') {
                   5228:             $$sections{$sectionstr} = 1;
                   5229:             $num_sections ++;
                   5230:         }
                   5231:     }
1.129     albertel 5232: 
1.88      raeburn  5233:     return $num_sections;
                   5234: }
                   5235: 
1.58      www      5236: # ========================================================== Custom Role Editor
                   5237: 
                   5238: sub custom_role_editor {
1.439     raeburn  5239:     my ($r,$context,$brcrum,$prefix,$permission) = @_;
1.324     raeburn  5240:     my $action = $env{'form.customroleaction'};
1.439     raeburn  5241:     my ($rolename,$helpitem);
1.324     raeburn  5242:     if ($action eq 'new') {
                   5243:         $rolename=$env{'form.newrolename'};
                   5244:     } else {
                   5245:         $rolename=$env{'form.rolename'};
1.59      www      5246:     }
                   5247: 
1.324     raeburn  5248:     my ($crstype,$context);
                   5249:     if ($env{'request.course.id'}) {
                   5250:         $crstype = &Apache::loncommon::course_type();
                   5251:         $context = 'course';
1.439     raeburn  5252:         $helpitem = 'Course_Editing_Custom_Roles';
1.324     raeburn  5253:     } else {
                   5254:         $context = 'domain';
1.414     raeburn  5255:         $crstype = 'course';
1.439     raeburn  5256:         $helpitem = 'Domain_Editing_Custom_Roles';
1.324     raeburn  5257:     }
1.351     raeburn  5258: 
                   5259:     $rolename=~s/[^A-Za-z0-9]//gs;
                   5260:     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
1.439     raeburn  5261: 	&print_username_entry_form($r,$context,undef,undef,undef,$crstype,$brcrum,
                   5262:                                    $permission);
1.351     raeburn  5263:         return;
                   5264:     }
                   5265: 
1.414     raeburn  5266:     my $formname = 'form1';
                   5267:     my %privs=();
                   5268:     my $body_top = '<h2>';
                   5269: # ------------------------------------------------------- Does this role exist?
1.59      www      5270:     my ($rdummy,$roledef)=
                   5271: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   5272:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.414     raeburn  5273:         $body_top .= &mt('Existing Role').' "';
1.61      www      5274: # ------------------------------------------------- Get current role privileges
1.414     raeburn  5275:         ($privs{'system'},$privs{'domain'},$privs{'course'})=split(/\_/,$roledef);
                   5276:         if ($privs{'system'} =~ /bre\&S/) {
                   5277:             if ($context eq 'domain') {
1.417     raeburn  5278:                 $crstype = 'Course';
1.414     raeburn  5279:             } elsif ($crstype eq 'Community') {
                   5280:                 $privs{'system'} =~ s/bre\&S//;
                   5281:             }
                   5282:         } elsif ($context eq 'domain') {
                   5283:             $crstype = 'Course';
1.324     raeburn  5284:         }
1.59      www      5285:     } else {
1.414     raeburn  5286:         $body_top .= &mt('New Role').' "';
                   5287:         $roledef='';
1.59      www      5288:     }
1.153     banghart 5289:     $body_top .= $rolename.'"</h2>';
1.414     raeburn  5290: 
                   5291: # ------------------------------------------------------- What can be assigned?
                   5292:     my %full=();
1.417     raeburn  5293:     my %levels=(
1.414     raeburn  5294:                  course => {},
                   5295:                  domain => {},
                   5296:                  system => {},
                   5297:                );
                   5298:     my %levelscurrent=(
                   5299:                         course => {},
                   5300:                         domain => {},
                   5301:                         system => {},
                   5302:                       );
                   5303:     &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
1.160     raeburn  5304:     my ($jsback,$elements) = &crumb_utilities();
1.414     raeburn  5305:     my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
1.417     raeburn  5306:     my $head_script =
1.414     raeburn  5307:         &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,
                   5308:                                                   \%full,\@templateroles,$jsback);
1.351     raeburn  5309:     push (@{$brcrum},
1.414     raeburn  5310:               {href => "javascript:backPage(document.$formname,'pickrole','')",
1.351     raeburn  5311:                text => "Pick custom role",
                   5312:                faq  => 282,bug=>'Instructor Interface',},
1.414     raeburn  5313:               {href => "javascript:backPage(document.$formname,'','')",
1.351     raeburn  5314:                text => "Edit custom role",
                   5315:                faq  => 282,
                   5316:                bug  => 'Instructor Interface',
1.439     raeburn  5317:                help => $helpitem}
1.351     raeburn  5318:               );
                   5319:     my $args = { bread_crumbs          => $brcrum,
                   5320:                  bread_crumbs_component => 'User Management'};
                   5321:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',
                   5322:                                              $head_script,$args).
                   5323:               $body_top);
1.414     raeburn  5324:     $r->print('<form name="'.$formname.'" method="post" action="">'."\n".
                   5325:               &Apache::lonuserutils::custom_role_header($context,$crstype,
                   5326:                                                         \@templateroles,$prefix));
1.264     bisitz   5327: 
1.61      www      5328:     $r->print(<<ENDCCF);
                   5329: <input type="hidden" name="phase" value="set_custom_roles" />
                   5330: <input type="hidden" name="rolename" value="$rolename" />
                   5331: ENDCCF
1.414     raeburn  5332:     $r->print(&Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
                   5333:                                                        \%levelscurrent,$prefix));
1.135     raeburn  5334:     $r->print(&Apache::loncommon::end_data_table().
1.190     raeburn  5335:    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
1.160     raeburn  5336:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
1.417     raeburn  5337:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".
1.160     raeburn  5338:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
1.351     raeburn  5339:    '<input type="submit" value="'.&mt('Save').'" /></form>');
1.61      www      5340: }
1.414     raeburn  5341: 
1.61      www      5342: # ---------------------------------------------------------- Call to definerole
                   5343: sub set_custom_role {
1.439     raeburn  5344:     my ($r,$context,$brcrum,$prefix,$permission) = @_;
1.101     albertel 5345:     my $rolename=$env{'form.rolename'};
1.63      www      5346:     $rolename=~s/[^A-Za-z0-9]//gs;
1.150     banghart 5347:     if (!$rolename) {
1.439     raeburn  5348: 	&custom_role_editor($r,$context,$brcrum,$prefix,$permission);
1.61      www      5349:         return;
                   5350:     }
1.160     raeburn  5351:     my ($jsback,$elements) = &crumb_utilities();
1.301     bisitz   5352:     my $jscript = '<script type="text/javascript">'
                   5353:                  .'// <![CDATA['."\n"
                   5354:                  .$jsback."\n"
                   5355:                  .'// ]]>'."\n"
                   5356:                  .'</script>'."\n";
1.439     raeburn  5357:     my $helpitem = 'Course_Editing_Custom_Roles';
                   5358:     if ($context eq 'domain') {
                   5359:         $helpitem = 'Domain_Editing_Custom_Roles';
                   5360:     }
1.352     raeburn  5361:     push(@{$brcrum},
                   5362:         {href => "javascript:backPage(document.customresult,'pickrole','')",
                   5363:          text => "Pick custom role",
                   5364:          faq  => 282,
                   5365:          bug  => 'Instructor Interface',},
                   5366:         {href => "javascript:backPage(document.customresult,'selected_custom_edit','')",
                   5367:          text => "Edit custom role",
                   5368:          faq  => 282,
                   5369:          bug  => 'Instructor Interface',},
                   5370:         {href => "javascript:backPage(document.customresult,'set_custom_roles','')",
                   5371:          text => "Result",
                   5372:          faq  => 282,
                   5373:          bug  => 'Instructor Interface',
1.439     raeburn  5374:          help => $helpitem,}
1.352     raeburn  5375:         );
                   5376:     my $args = { bread_crumbs           => $brcrum,
1.414     raeburn  5377:                  bread_crumbs_component => 'User Management'};
1.351     raeburn  5378:     $r->print(&Apache::loncommon::start_page('Save Custom Role',$jscript,$args));
1.160     raeburn  5379: 
1.393     raeburn  5380:     my $newrole;
1.61      www      5381:     my ($rdummy,$roledef)=
1.110     albertel 5382: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   5383: 
1.61      www      5384: # ------------------------------------------------------- Does this role exist?
1.188     raeburn  5385:     $r->print('<h3>');
1.61      www      5386:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 5387: 	$r->print(&mt('Existing Role').' "');
1.61      www      5388:     } else {
1.73      sakharuk 5389: 	$r->print(&mt('New Role').' "');
1.61      www      5390: 	$roledef='';
1.393     raeburn  5391:         $newrole = 1;
1.61      www      5392:     }
1.188     raeburn  5393:     $r->print($rolename.'"</h3>');
1.414     raeburn  5394: # ------------------------------------------------- Assign role and show result
1.61      www      5395: 
1.387     bisitz   5396:     my $errmsg;
1.414     raeburn  5397:     my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$prefix);
                   5398:     # Assign role and return result
                   5399:     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
                   5400:                                              $newprivs{'c'});
1.387     bisitz   5401:     if ($result ne 'ok') {
                   5402:         $errmsg = ': '.$result;
                   5403:     }
                   5404:     my $message =
                   5405:         &Apache::lonhtmlcommon::confirm_success(
                   5406:             &mt('Defining Role').$errmsg, ($result eq 'ok' ? 0 : 1));
1.101     albertel 5407:     if ($env{'request.course.id'}) {
                   5408:         my $url='/'.$env{'request.course.id'};
1.63      www      5409:         $url=~s/\_/\//g;
1.387     bisitz   5410:         $result =
                   5411:             &Apache::lonnet::assigncustomrole(
                   5412:                 $env{'user.domain'},$env{'user.name'},
                   5413:                 $url,
                   5414:                 $env{'user.domain'},$env{'user.name'},
                   5415:                 $rolename,undef,undef,undef,$context);
                   5416:         if ($result ne 'ok') {
                   5417:             $errmsg = ': '.$result;
                   5418:         }
                   5419:         $message .=
                   5420:             '<br />'
                   5421:            .&Apache::lonhtmlcommon::confirm_success(
                   5422:                 &mt('Assigning Role to Self').$errmsg, ($result eq 'ok' ? 0 : 1));
1.63      www      5423:     }
1.380     bisitz   5424:     $r->print(
1.387     bisitz   5425:         &Apache::loncommon::confirmwrapper($message)
                   5426:        .'<br />'
                   5427:        .&Apache::lonhtmlcommon::actionbox([
                   5428:             '<a href="javascript:backPage(document.customresult,'."'pickrole'".')">'
                   5429:            .&mt('Create or edit another custom role')
                   5430:            .'</a>'])
1.380     bisitz   5431:        .'<form name="customresult" method="post" action="">'
1.387     bisitz   5432:        .&Apache::lonhtmlcommon::echo_form_input([])
                   5433:        .'</form>'
1.380     bisitz   5434:     );
1.58      www      5435: }
                   5436: 
1.465     raeburn  5437: sub show_role_requests {
                   5438:     my ($caller,$dom) = @_;
                   5439:     my $showrolereqs;
                   5440:     my %domconfig = &Apache::lonnet::get_dom('configuration',['privacy'],$dom);
                   5441:     if (ref($domconfig{'privacy'}) eq 'HASH') {
                   5442:         if (ref($domconfig{'privacy'}{'approval'}) eq 'HASH') {
                   5443:             my %approvalconf = %{$domconfig{'privacy'}{'approval'}};
                   5444:             foreach my $key ('instdom','extdom') {
                   5445:                 if (ref($approvalconf{$key}) eq 'HASH') {
                   5446:                     if (keys(%{$approvalconf{$key}})) {
                   5447:                         foreach my $context ('domain','author','course','community') {
                   5448:                             if ($approvalconf{$key}{$context} eq $caller) {
                   5449:                                 $showrolereqs = 1;
                   5450:                                 last if ($showrolereqs);
                   5451:                             }
                   5452:                         }
                   5453:                     }
                   5454:                 }
                   5455:                 last if ($showrolereqs);
                   5456:             }
                   5457:         }
                   5458:     }
                   5459:     return $showrolereqs;
                   5460: }
                   5461: 
1.470     raeburn  5462: sub display_coauthor_managers {
                   5463:     my ($permission) = @_;
                   5464:     my $output;
                   5465:     if ((ref($permission) eq 'HASH') && ($permission->{'author'})) {
                   5466:         $output = '<form action="/adm/createuser" method="post" name="camanagers">'.
                   5467:                   '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
                   5468:                   '<p>';
                   5469:         my (@possmanagers,@custommanagers);
                   5470:         my %userenv =
                   5471:             &Apache::lonnet::userenvironment($env{'user.domain'},
                   5472:                                              $env{'user.name'},
                   5473:                                              'authormanagers');
                   5474:         my %ca_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   5475:                                                      ['active','future'],['ca']);
                   5476:         if (keys(%ca_roles)) {
                   5477:             foreach my $entry (sort(keys(%ca_roles))) {
                   5478:                 if ($entry =~ /^($match_username\:$match_domain):ca$/) {
                   5479:                     my $user = $1;
                   5480:                     unless ($user eq $env{'user.name'}.':'.$env{'user.domain'}) {
                   5481:                         push(@possmanagers,$user);
                   5482:                     }
                   5483:                 }
                   5484:             }
                   5485:         }
                   5486:         if ($userenv{'authormanagers'} eq '') {
                   5487:             $output .= &mt('Currently author manages co-author roles');
                   5488:         } else {
                   5489:             if (keys(%ca_roles)) {
                   5490:                 foreach my $user (split(/,/,$userenv{'authormanagers'})) {
                   5491:                     if ($user =~ /^($match_username)\:($match_domain)$/) {
                   5492:                         if (exists($ca_roles{$user.':ca'})) {
                   5493:                             unless ($user eq $env{'user.name'}.':'.$env{'user.domain'}) {
                   5494:                                 push(@custommanagers,$user);
                   5495:                             }
                   5496:                         }
                   5497:                     }
                   5498:                 }
                   5499:             }
                   5500:             if (@custommanagers) {
                   5501:                 $output .= &mt('Co-authors with active or future roles who currently manage co-author roles: [_1]',
                   5502:                               '<br />'.join(', ',map { &Apache::loncommon::plainname(split(':',$_))." ($_)"; } @custommanagers));
                   5503:             } else {
                   5504:                 $output .= &mt('Currently author manages co-author roles');
                   5505:             }
                   5506:         }
                   5507:         $output .= "</p>\n";
                   5508:         if (@possmanagers) {
                   5509:             $output .= '<p>'.&mt('Select manager(s)').': ';
                   5510:             foreach my $user (@possmanagers) {
                   5511:                  my $checked;
                   5512:                  if (grep(/^\Q$user\E$/,@custommanagers)) {
                   5513:                      $checked = ' checked="checked"';
                   5514:                  }
                   5515:                  $output .= '<span style="LC_nobreak"><label>'.
                   5516:                             '<input type="checkbox" name="custommanagers" '.
                   5517:                             'value="'.&HTML::Entities::encode($user,'\'<>"&').'"'.$checked.' />'.
                   5518:                             &Apache::loncommon::plainname(split(/:/,$user))." ($user)".'</label></span> '."\n";
                   5519:             }
                   5520:             $output .= '<input type="hidden" name="state" value="process" /></p>'."\n".
                   5521:                        '<p><input type="submit" value="'.&mt('Save changes').'" /></p>'."\n";
                   5522:         } else {
                   5523:             $output .= '<p>'.&mt('No co-author roles assignable as manager').'</p>';
                   5524:         }
                   5525:         $output .= '</form>';
                   5526:     } else {
                   5527:         $output = '<span class="LC_warning">'.
                   5528:                   &mt('You do not have permission to perform this action').
                   5529:                   '</span>';
                   5530:     }
                   5531:     return $output;
                   5532: }
                   5533: 
                   5534: sub update_coauthor_managers {
                   5535:     my ($permission) = @_;
                   5536:     my $output;
                   5537:     if ((ref($permission) eq 'HASH') && ($permission->{'author'})) {
                   5538:         my ($current,$newval,@possibles,@managers);
                   5539:         my %userenv =
                   5540:             &Apache::lonnet::userenvironment($env{'user.domain'},
                   5541:                                              $env{'user.name'},
                   5542:                                              'authormanagers');
                   5543:         $current = $userenv{'authormanagers'};
                   5544:         @possibles = &Apache::loncommon::get_env_multiple('form.custommanagers');
                   5545:         if (@possibles) {
                   5546:             my %ca_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   5547:                                                          ['active','future'],['ca']);
                   5548:             if (keys(%ca_roles)) {
                   5549:                 foreach my $user (@possibles) {
                   5550:                     if ($user =~ /^($match_username):($match_domain)$/) {
                   5551:                         if (exists($ca_roles{$user.':ca'})) {
                   5552:                             unless ($user eq $env{'user.name'}.':'.$env{'user.domain'}) {
                   5553:                                 push(@managers,$user);
                   5554:                             }
                   5555:                         }
                   5556:                     }
                   5557:                 }
                   5558:                 if (@managers) {
                   5559:                     $newval = join(',',sort(@managers));
                   5560:                 }
                   5561:             }
                   5562:         }
                   5563:         if ($current eq $newval) {
                   5564:             $output = &mt('No changes made to management of co-author roles');
                   5565:         } else {
                   5566:             my $chgresult =
                   5567:                 &Apache::lonnet::put('environment',{'authormanagers' => $newval},
                   5568:                                      $env{'user.domain'},$env{'user.name'});
                   5569:             if ($chgresult eq 'ok') {
                   5570:                 &Apache::lonnet::appenv({'environment.authormanagers' => $newval});
                   5571:                 my (@adds,@dels);
                   5572:                 if ($newval eq '') {
                   5573:                     @dels = split(/,/,$current);
                   5574:                 } elsif ($current eq '') {
                   5575:                     @adds = @managers;
                   5576:                 } else {
                   5577:                     my @old = split(/,/,$current);
                   5578:                     my @diffs = &Apache::loncommon::compare_arrays(\@old,\@managers);
                   5579:                     if (@diffs) {
                   5580:                         foreach my $user (@diffs) {
                   5581:                             if (grep(/^\Q$user\E$/,@old)) {
                   5582:                                 push(@dels,$user);
                   5583:                             } elsif (grep(/^\Q$user\E$/,@managers)) {
                   5584:                                 push(@adds,$user);
                   5585:                             }
                   5586:                         }
                   5587:                     }
                   5588:                 }
                   5589:                 my $key = "internal.manager./$env{'user.domain'}/$env{'user.name'}";
                   5590:                 if (@dels) {
                   5591:                     foreach my $user (@dels) {
                   5592:                         if ($user =~ /^($match_username):($match_domain)$/) {
                   5593:                             &Apache::lonnet::del('environment',[$key],$2,$1);
                   5594:                         }
                   5595:                     }
                   5596:                 }
                   5597:                 if (@adds) {
                   5598:                     foreach my $user (@adds) {
                   5599:                         if ($user =~ /^($match_username):($match_domain)$/) {
                   5600:                             &Apache::lonnet::put('environment',{$key => 1},$2,$1);
                   5601:                         }
                   5602:                     }
                   5603:                 }
                   5604:                 if ($newval eq '') {
                   5605:                     $output = &mt('Management of co-authors set to be author-only');
                   5606:                 } else {
                   5607:                     $output .= &mt('Co-authors who can manage co-author roles set to: [_1]',
                   5608:                                    '<br />'.join(', ',map { &Apache::loncommon::plainname(split(':',$_))." ($_)"; } @managers));
                   5609:                 }
                   5610:             }
                   5611:         }
                   5612:     } else {
                   5613:         $output = '<span class="LC_warning">'.
                   5614:                   &mt('You do not have permission to perform this action').
                   5615:                   '</span>';
                   5616:     }
                   5617:     return $output;
                   5618: }
                   5619: 
1.2       www      5620: # ================================================================ Main Handler
                   5621: sub handler {
                   5622:     my $r = shift;
                   5623:     if ($r->header_only) {
1.68      www      5624:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      5625:        $r->send_http_header;
                   5626:        return OK;
                   5627:     }
1.439     raeburn  5628:     my ($context,$crstype,$cid,$cnum,$cdom,$allhelpitems);
                   5629: 
1.190     raeburn  5630:     if ($env{'request.course.id'}) {
                   5631:         $context = 'course';
1.318     raeburn  5632:         $crstype = &Apache::loncommon::course_type();
1.190     raeburn  5633:     } elsif ($env{'request.role'} =~ /^au\./) {
1.206     raeburn  5634:         $context = 'author';
1.470     raeburn  5635:     } elsif ($env{'request.role'} =~ m{^(ca|aa)\./$match_domain/$match_username$}) {
                   5636:         $context = 'coauthor';
1.190     raeburn  5637:     } else {
                   5638:         $context = 'domain';
                   5639:     }
1.375     raeburn  5640: 
1.439     raeburn  5641:     my ($permission,$allowed) =
                   5642:         &Apache::lonuserutils::get_permission($context,$crstype);
1.470     raeburn  5643:     if (($context eq 'coauthor') && ($allowed)) {
                   5644:         $context = 'author';
                   5645:     }
1.439     raeburn  5646: 
                   5647:     if ($allowed) {
                   5648:         my @allhelp;
                   5649:         if ($context eq 'course') {
                   5650:             $cid = $env{'request.course.id'};
                   5651:             $cdom = $env{'course.'.$cid.'.domain'};
                   5652:             $cnum = $env{'course.'.$cid.'.num'};
                   5653: 
                   5654:             if ($permission->{'cusr'}) {
                   5655:                 push(@allhelp,'Course_Create_Class_List');
                   5656:             }
                   5657:             if ($permission->{'view'} || $permission->{'cusr'}) {
                   5658:                 push(@allhelp,('Course_Change_Privileges','Course_View_Class_List'));
                   5659:             }
                   5660:             if ($permission->{'custom'}) {
                   5661:                 push(@allhelp,'Course_Editing_Custom_Roles');
                   5662:             }
                   5663:             if ($permission->{'cusr'}) {
                   5664:                 push(@allhelp,('Course_Add_Student','Course_Drop_Student'));
                   5665:             }
                   5666:             unless ($permission->{'cusr_section'}) {
                   5667:                 if (&Apache::lonnet::auto_run($cnum,$cdom) && (($permission->{'cusr'}) || ($permission->{'view'}))) {
                   5668:                     push(@allhelp,'Course_Automated_Enrollment');
                   5669:                 }
1.469     raeburn  5670:                 if (($permission->{'selfenrolladmin'}) || ($permission->{'selfenrollview'})) {
1.439     raeburn  5671:                     push(@allhelp,'Course_Approve_Selfenroll');
                   5672:                 }
                   5673:             }
                   5674:             if ($permission->{'grp_manage'}) {
                   5675:                 push(@allhelp,'Course_Manage_Group');
                   5676:             }
                   5677:             if ($permission->{'view'} || $permission->{'cusr'}) {
                   5678:                 push(@allhelp,'Course_User_Logs');
                   5679:             }
                   5680:         } elsif ($context eq 'author') {
                   5681:             push(@allhelp,('Author_Change_Privileges','Author_Create_Coauthor_List',
                   5682:                            'Author_View_Coauthor_List','Author_User_Logs'));
1.470     raeburn  5683:         } elsif ($context eq 'coauthor') {
                   5684:             if ($permission->{'cusr'}) {
                   5685:                 push(@allhelp,('Author_Change_Privileges','Author_Create_Coauthor_List',
                   5686:                                'Author_View_Coauthor_List','Author_User_Logs'));
                   5687:             } elsif ($permission->{'view'}) {
                   5688:                 push(@allhelp,'Author_View_Coauthor_List');
                   5689:             }
1.439     raeburn  5690:         } else {
                   5691:             if ($permission->{'cusr'}) {
                   5692:                 push(@allhelp,'Domain_Change_Privileges');
                   5693:                 if ($permission->{'activity'}) {
                   5694:                     push(@allhelp,'Domain_User_Access_Logs');
                   5695:                 }
                   5696:                 push(@allhelp,('Domain_Create_Users','Domain_View_Users_List'));
                   5697:                 if ($permission->{'custom'}) {
                   5698:                     push(@allhelp,'Domain_Editing_Custom_Roles');
                   5699:                 }
                   5700:                 push(@allhelp,('Domain_Role_Approvals','Domain_Username_Approvals','Domain_Change_Logs'));
                   5701:             } elsif ($permission->{'view'}) {
                   5702:                 push(@allhelp,'Domain_View_Privileges');
                   5703:                 if ($permission->{'activity'}) {
                   5704:                     push(@allhelp,'Domain_User_Access_Logs');
                   5705:                 }
                   5706:                 push(@allhelp,('Domain_View_Users_List','Domain_Change_Logs'));
                   5707:             }
                   5708:         }
                   5709:         if (@allhelp) {
                   5710:             $allhelpitems = join(',',@allhelp);
                   5711:         }
                   5712:     }
                   5713: 
1.190     raeburn  5714:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.233     raeburn  5715:         ['action','state','callingform','roletype','showrole','bulkaction','popup','phase',
1.470     raeburn  5716:          'username','domain','srchterm','srchdomain','srchin','srchby','srchtype','queue',
                   5717:          'forceedit']);
1.190     raeburn  5718:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.351     raeburn  5719:     my $args;
                   5720:     my $brcrum = [];
                   5721:     my $bread_crumbs_component = 'User Management';
1.391     raeburn  5722:     if (($env{'form.action'} ne 'dateselect') && ($env{'form.action'} ne 'displayuserreq')) {
1.351     raeburn  5723:         $brcrum = [{href=>"/adm/createuser",
                   5724:                     text=>"User Management",
1.439     raeburn  5725:                     help=>$allhelpitems}
1.351     raeburn  5726:                   ];
1.202     raeburn  5727:     }
1.190     raeburn  5728:     if (!$allowed) {
1.358     raeburn  5729:         if ($context eq 'course') {
                   5730:             $r->internal_redirect('/adm/viewclasslist');
                   5731:             return OK;
1.470     raeburn  5732:         } elsif ($context eq 'coauthor') {
                   5733:             $r->internal_redirect('/adm/viewcoauthors');
                   5734:             return OK;
1.358     raeburn  5735:         }
1.190     raeburn  5736:         $env{'user.error.msg'}=
                   5737:             "/adm/createuser:cst:0:0:Cannot create/modify user data ".
                   5738:                                  "or view user status.";
                   5739:         return HTTP_NOT_ACCEPTABLE;
                   5740:     }
                   5741: 
                   5742:     &Apache::loncommon::content_type($r,'text/html');
                   5743:     $r->send_http_header;
                   5744: 
1.375     raeburn  5745:     my $showcredits;
                   5746:     if ((($context eq 'course') && ($crstype eq 'Course')) || 
                   5747:          ($context eq 'domain')) {
                   5748:         my %domdefaults = 
                   5749:             &Apache::lonnet::get_domain_defaults($env{'request.role.domain'});
                   5750:         if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'}) {
                   5751:             $showcredits = 1;
                   5752:         }
                   5753:     }
                   5754: 
1.190     raeburn  5755:     # Main switch on form.action and form.state, as appropriate
                   5756:     if (! exists($env{'form.action'})) {
1.351     raeburn  5757:         $args = {bread_crumbs => $brcrum,
                   5758:                  bread_crumbs_component => $bread_crumbs_component}; 
                   5759:         $r->print(&header(undef,$args));
1.318     raeburn  5760:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  5761:     } elsif ($env{'form.action'} eq 'upload' && $permission->{'cusr'}) {
1.439     raeburn  5762:         my $helpitem = 'Course_Create_Class_List';
                   5763:         if ($context eq 'author') {
                   5764:             $helpitem = 'Author_Create_Coauthor_List';
                   5765:         } elsif ($context eq 'domain') {
                   5766:             $helpitem = 'Domain_Create_Users';
                   5767:         }
1.351     raeburn  5768:         push(@{$brcrum},
                   5769:               { href => '/adm/createuser?action=upload&state=',
                   5770:                 text => 'Upload Users List',
1.439     raeburn  5771:                 help => $helpitem,
1.351     raeburn  5772:               });
                   5773:         $bread_crumbs_component = 'Upload Users List';
                   5774:         $args = {bread_crumbs           => $brcrum,
                   5775:                  bread_crumbs_component => $bread_crumbs_component};
                   5776:         $r->print(&header(undef,$args));
1.190     raeburn  5777:         $r->print('<form name="studentform" method="post" '.
                   5778:                   'enctype="multipart/form-data" '.
                   5779:                   ' action="/adm/createuser">'."\n");
                   5780:         if (! exists($env{'form.state'})) {
                   5781:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   5782:         } elsif ($env{'form.state'} eq 'got_file') {
1.448     raeburn  5783:             my $result = 
                   5784:                 &Apache::lonuserutils::print_upload_manager_form($r,$context,
                   5785:                                                                  $permission,
                   5786:                                                                  $crstype,$showcredits);
                   5787:             if ($result eq 'missingdata') {
                   5788:                 delete($env{'form.state'});
                   5789:                 &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   5790:             }
1.190     raeburn  5791:         } elsif ($env{'form.state'} eq 'enrolling') {
                   5792:             if ($env{'form.datatoken'}) {
1.448     raeburn  5793:                 my $result = &Apache::lonuserutils::upfile_drop_add($r,$context,
                   5794:                                                                     $permission,
                   5795:                                                                     $showcredits);
                   5796:                 if ($result eq 'missingdata') {
                   5797:                     delete($env{'form.state'});
                   5798:                     &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   5799:                 } elsif ($result eq 'invalidhome') {
                   5800:                     $env{'form.state'} = 'got_file';
                   5801:                     delete($env{'form.lcserver'});
                   5802:                     my $result =
                   5803:                         &Apache::lonuserutils::print_upload_manager_form($r,$context,$permission,
                   5804:                                                                          $crstype,$showcredits);
                   5805:                     if ($result eq 'missingdata') {
                   5806:                         delete($env{'form.state'});
                   5807:                         &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   5808:                     }
                   5809:                 }
                   5810:             } else {
                   5811:                 delete($env{'form.state'});
                   5812:                 &Apache::lonuserutils::print_first_users_upload_form($r,$context);
1.190     raeburn  5813:             }
                   5814:         } else {
                   5815:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   5816:         }
1.447     raeburn  5817:         $r->print('</form>');
1.416     raeburn  5818:     } elsif (((($env{'form.action'} eq 'singleuser') || ($env{'form.action'}
                   5819:               eq 'singlestudent')) && ($permission->{'cusr'})) ||
1.418     raeburn  5820:              (($env{'form.action'} eq 'singleuser') && ($permission->{'view'})) ||
1.416     raeburn  5821:              (($env{'form.action'} eq 'accesslogs') && ($permission->{'activity'}))) {
1.190     raeburn  5822:         my $phase = $env{'form.phase'};
                   5823:         my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
1.192     albertel 5824: 	&Apache::loncreateuser::restore_prev_selections();
                   5825: 	my $srch;
                   5826: 	foreach my $item (@search) {
                   5827: 	    $srch->{$item} = $env{'form.'.$item};
                   5828: 	}
1.207     raeburn  5829:         if (($phase eq 'get_user_info') || ($phase eq 'userpicked') ||
1.416     raeburn  5830:             ($phase eq 'createnewuser') || ($phase eq 'activity')) {
1.207     raeburn  5831:             if ($env{'form.phase'} eq 'createnewuser') {
                   5832:                 my $response;
                   5833:                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
1.366     bisitz   5834:                     my $response =
                   5835:                         '<span class="LC_warning">'
                   5836:                        .&mt('You must specify a valid username. Only the following are allowed:'
                   5837:                            .' letters numbers - . @')
                   5838:                        .'</span>';
1.221     raeburn  5839:                     $env{'form.phase'} = '';
1.375     raeburn  5840:                     &print_username_entry_form($r,$context,$response,$srch,undef,
1.439     raeburn  5841:                                                $crstype,$brcrum,$permission);
1.207     raeburn  5842:                 } else {
                   5843:                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
                   5844:                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
                   5845:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  5846:                                                   $srch,$response,$context,
1.375     raeburn  5847:                                                   $permission,$crstype,$brcrum,
                   5848:                                                   $showcredits);
1.207     raeburn  5849:                 }
                   5850:             } elsif ($env{'form.phase'} eq 'get_user_info') {
1.190     raeburn  5851:                 my ($currstate,$response,$forcenewuser,$results) = 
1.221     raeburn  5852:                     &user_search_result($context,$srch);
1.190     raeburn  5853:                 if ($env{'form.currstate'} eq 'modify') {
                   5854:                     $currstate = $env{'form.currstate'};
                   5855:                 }
                   5856:                 if ($currstate eq 'select') {
                   5857:                     &print_user_selection_page($r,$response,$srch,$results,
1.351     raeburn  5858:                                                \@search,$context,undef,$crstype,
                   5859:                                                $brcrum);
1.416     raeburn  5860:                 } elsif (($currstate eq 'modify') || ($env{'form.action'} eq 'accesslogs')) {
                   5861:                     my ($ccuname,$ccdomain,$uhome);
1.190     raeburn  5862:                     if (($srch->{'srchby'} eq 'uname') && 
                   5863:                         ($srch->{'srchtype'} eq 'exact')) {
                   5864:                         $ccuname = $srch->{'srchterm'};
                   5865:                         $ccdomain= $srch->{'srchdomain'};
                   5866:                     } else {
                   5867:                         my @matchedunames = keys(%{$results});
                   5868:                         ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
                   5869:                     }
                   5870:                     $ccuname =&LONCAPA::clean_username($ccuname);
                   5871:                     $ccdomain=&LONCAPA::clean_domain($ccdomain);
1.416     raeburn  5872:                     if ($env{'form.action'} eq 'accesslogs') {
                   5873:                         my $uhome;
                   5874:                         if (($ccuname ne '') && ($ccdomain ne '')) {
                   5875:                            $uhome = &Apache::lonnet::homeserver($ccuname,$ccdomain);
                   5876:                         }
                   5877:                         if (($uhome eq '') || ($uhome eq 'no_host')) {
                   5878:                             $env{'form.phase'} = '';
                   5879:                             undef($forcenewuser);
                   5880:                             #if ($response) {
                   5881:                             #    unless ($response =~ m{\Q<br /><br />\E$}) {
                   5882:                             #        $response .= '<br /><br />';
                   5883:                             #    }
                   5884:                             #}
                   5885:                             &print_username_entry_form($r,$context,$response,$srch,
1.439     raeburn  5886:                                                        $forcenewuser,$crstype,$brcrum,
                   5887:                                                        $permission);
1.416     raeburn  5888:                         } else {
                   5889:                             &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
                   5890:                         }
                   5891:                     } else {
                   5892:                         if ($env{'form.forcenewuser'}) {
                   5893:                             $response = '';
                   5894:                         }
                   5895:                         &print_user_modification_page($r,$ccuname,$ccdomain,
                   5896:                                                       $srch,$response,$context,
                   5897:                                                       $permission,$crstype,$brcrum);
1.190     raeburn  5898:                     }
                   5899:                 } elsif ($currstate eq 'query') {
1.351     raeburn  5900:                     &print_user_query_page($r,'createuser',$brcrum);
1.190     raeburn  5901:                 } else {
1.229     raeburn  5902:                     $env{'form.phase'} = '';
1.207     raeburn  5903:                     &print_username_entry_form($r,$context,$response,$srch,
1.439     raeburn  5904:                                                $forcenewuser,$crstype,$brcrum,
                   5905:                                                $permission);
1.190     raeburn  5906:                 }
                   5907:             } elsif ($env{'form.phase'} eq 'userpicked') {
                   5908:                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
                   5909:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
1.416     raeburn  5910:                 if ($env{'form.action'} eq 'accesslogs') {
                   5911:                     &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
                   5912:                 } else {
                   5913:                     &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
                   5914:                                                   $context,$permission,$crstype,
                   5915:                                                   $brcrum);
                   5916:                 }
                   5917:             } elsif ($env{'form.action'} eq 'accesslogs') {
                   5918:                 my $ccuname = &LONCAPA::clean_username($env{'form.accessuname'});
                   5919:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.accessudom'});
                   5920:                 &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
1.190     raeburn  5921:             }
                   5922:         } elsif ($env{'form.phase'} eq 'update_user_data') {
1.451     raeburn  5923:             &update_user_data($r,$context,$crstype,$brcrum,$showcredits,$permission);
1.190     raeburn  5924:         } else {
1.351     raeburn  5925:             &print_username_entry_form($r,$context,undef,$srch,undef,$crstype,
1.439     raeburn  5926:                                        $brcrum,$permission);
1.190     raeburn  5927:         }
                   5928:     } elsif ($env{'form.action'} eq 'custom' && $permission->{'custom'}) {
1.414     raeburn  5929:         my $prefix;
1.190     raeburn  5930:         if ($env{'form.phase'} eq 'set_custom_roles') {
1.439     raeburn  5931:             &set_custom_role($r,$context,$brcrum,$prefix,$permission);
1.190     raeburn  5932:         } else {
1.439     raeburn  5933:             &custom_role_editor($r,$context,$brcrum,$prefix,$permission);
1.190     raeburn  5934:         }
1.362     raeburn  5935:     } elsif (($env{'form.action'} eq 'processauthorreq') &&
                   5936:              ($permission->{'cusr'}) && 
                   5937:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   5938:         push(@{$brcrum},
                   5939:                  {href => '/adm/createuser?action=processauthorreq',
1.385     bisitz   5940:                   text => 'Authoring Space requests',
1.362     raeburn  5941:                   help => 'Domain_Role_Approvals'});
                   5942:         $bread_crumbs_component = 'Authoring requests';
                   5943:         if ($env{'form.state'} eq 'done') {
                   5944:             push(@{$brcrum},
                   5945:                      {href => '/adm/createuser?action=authorreqqueue',
                   5946:                       text => 'Result',
                   5947:                       help => 'Domain_Role_Approvals'});
                   5948:             $bread_crumbs_component = 'Authoring request result';
                   5949:         }
                   5950:         $args = { bread_crumbs           => $brcrum,
                   5951:                   bread_crumbs_component => $bread_crumbs_component};
1.391     raeburn  5952:         my $js = &usernamerequest_javascript();
                   5953:         $r->print(&header(&add_script($js),$args));
1.362     raeburn  5954:         if (!exists($env{'form.state'})) {
                   5955:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestauthor',
                   5956:                                                                             $env{'request.role.domain'}));
                   5957:         } elsif ($env{'form.state'} eq 'done') {
                   5958:             $r->print('<h3>'.&mt('Authoring request processing').'</h3>'."\n");
                   5959:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestauthor',
                   5960:                                                                          $env{'request.role.domain'}));
                   5961:         }
1.391     raeburn  5962:     } elsif (($env{'form.action'} eq 'processusernamereq') &&
                   5963:              ($permission->{'cusr'}) &&
                   5964:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   5965:         push(@{$brcrum},
                   5966:                  {href => '/adm/createuser?action=processusernamereq',
                   5967:                   text => 'LON-CAPA account requests',
                   5968:                   help => 'Domain_Username_Approvals'});
                   5969:         $bread_crumbs_component = 'Account requests';
                   5970:         if ($env{'form.state'} eq 'done') {
                   5971:             push(@{$brcrum},
                   5972:                      {href => '/adm/createuser?action=usernamereqqueue',
                   5973:                       text => 'Result',
                   5974:                       help => 'Domain_Username_Approvals'});
                   5975:             $bread_crumbs_component = 'LON-CAPA account request result';
                   5976:         }
                   5977:         $args = { bread_crumbs           => $brcrum,
                   5978:                   bread_crumbs_component => $bread_crumbs_component};
                   5979:         my $js = &usernamerequest_javascript();
                   5980:         $r->print(&header(&add_script($js),$args));
                   5981:         if (!exists($env{'form.state'})) {
                   5982:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestusername',
                   5983:                                                                             $env{'request.role.domain'}));
                   5984:         } elsif ($env{'form.state'} eq 'done') {
                   5985:             $r->print('<h3>'.&mt('LON-CAPA account request processing').'</h3>'."\n");
                   5986:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestusername',
                   5987:                                                                          $env{'request.role.domain'}));
                   5988:         }
                   5989:     } elsif (($env{'form.action'} eq 'displayuserreq') &&
                   5990:              ($permission->{'cusr'})) {
                   5991:         my $dom = $env{'form.domain'};
                   5992:         my $uname = $env{'form.username'};
                   5993:         my $warning;
                   5994:         if (($dom =~ /^$match_domain$/) && (&Apache::lonnet::domain($dom) ne '')) {
                   5995:             if (($dom eq $env{'request.role.domain'}) && (&Apache::lonnet::allowed('ccc',$dom))) {
                   5996:                 if (($uname =~ /^$match_username$/) && ($env{'form.queue'} eq 'approval')) {
                   5997:                     my $uhome = &Apache::lonnet::homeserver($uname,$dom);
                   5998:                     if ($uhome eq 'no_host') {
                   5999:                         my $queue = $env{'form.queue'};
                   6000:                         my $reqkey = &escape($uname).'_'.$queue; 
                   6001:                         my $namespace = 'usernamequeue';
                   6002:                         my $domconfig = &Apache::lonnet::get_domainconfiguser($dom);
                   6003:                         my %queued =
                   6004:                             &Apache::lonnet::get($namespace,[$reqkey],$dom,$domconfig);
                   6005:                         unless ($queued{$reqkey}) {
                   6006:                             $warning = &mt('No information was found for this LON-CAPA account request.');
                   6007:                         }
                   6008:                     } else {
                   6009:                         $warning = &mt('A LON-CAPA account already exists for the requested username and domain.');
                   6010:                     }
                   6011:                 } else {
                   6012:                     $warning = &mt('LON-CAPA account request status check is for an invalid username.');
                   6013:                 }
                   6014:             } else {
                   6015:                 $warning = &mt('You do not have rights to view LON-CAPA account requests in the domain specified.');
                   6016:             }
                   6017:         } else {
                   6018:             $warning = &mt('LON-CAPA account request status check is for an invalid domain.');
                   6019:         }
                   6020:         my $args = { only_body => 1 };
                   6021:         $r->print(&header(undef,$args).
                   6022:                   '<h3>'.&mt('LON-CAPA Account Request Details').'</h3>');
                   6023:         if ($warning ne '') {
                   6024:             $r->print('<div class="LC_warning">'.$warning.'</div>');
                   6025:         } else {
                   6026:             my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
                   6027:             my $domconfiguser = &Apache::lonnet::get_domainconfiguser($dom);
                   6028:             my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   6029:             if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   6030:                 if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
                   6031:                     if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}) eq 'HASH') {
                   6032:                         my %info =
                   6033:                             &Apache::lonnet::get('nohist_requestedusernames',[$uname],$dom,$domconfiguser);
                   6034:                         if (ref($info{$uname}) eq 'HASH') {
1.396     raeburn  6035:                             my $usertype = $info{$uname}{'inststatus'};
                   6036:                             unless ($usertype) {
                   6037:                                 $usertype = 'default';
                   6038:                             }
1.442     raeburn  6039:                             my ($showstatus,$showemail,$pickstart);
                   6040:                             my $numextras = 0;
                   6041:                             my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
1.443     raeburn  6042:                             if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
                   6043:                                 if (ref($usertypes) eq 'HASH') {
                   6044:                                     if ($usertypes->{$usertype}) {
                   6045:                                         $showstatus = $usertypes->{$usertype};
                   6046:                                     } else {
                   6047:                                         $showstatus = $othertitle;
                   6048:                                     }
                   6049:                                     if ($showstatus) {
                   6050:                                         $numextras ++;
                   6051:                                     }
1.442     raeburn  6052:                                 }
                   6053:                             }
                   6054:                             if (($info{$uname}{'email'} ne '') && ($info{$uname}{'email'} ne $uname)) {
                   6055:                                 $showemail = $info{$uname}{'email'};
                   6056:                                 $numextras ++;
                   6057:                             }
1.396     raeburn  6058:                             if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}) eq 'HASH') {
                   6059:                                 if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
1.442     raeburn  6060:                                     $pickstart = 1;
1.396     raeburn  6061:                                     $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box());
1.442     raeburn  6062:                                     my ($num,$count);
1.396     raeburn  6063:                                     $count = scalar(keys(%{$domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}}));
1.442     raeburn  6064:                                     $count += $numextras;
1.396     raeburn  6065:                                     foreach my $field (@{$infofields}) {
                   6066:                                         next unless ($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}{$field});
                   6067:                                         next unless ($infotitles->{$field});
                   6068:                                         $r->print(&Apache::lonhtmlcommon::row_title($infotitles->{$field}).
                   6069:                                                   $info{$uname}{$field});
                   6070:                                         $num ++;
1.442     raeburn  6071:                                         unless ($count == $num) {
1.396     raeburn  6072:                                             $r->print(&Apache::lonhtmlcommon::row_closure());
                   6073:                                         }
                   6074:                                     }
1.442     raeburn  6075:                                 }
                   6076:                             }
                   6077:                             if ($numextras) {
                   6078:                                 unless ($pickstart) {
                   6079:                                     $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box());
                   6080:                                     $pickstart = 1;
                   6081:                                 }
                   6082:                                 if ($showemail) {
                   6083:                                     my $closure = '';
                   6084:                                     unless ($showstatus) {
                   6085:                                         $closure = 1;
1.391     raeburn  6086:                                     }
1.442     raeburn  6087:                                     $r->print(&Apache::lonhtmlcommon::row_title(&mt('E-mail address')).
                   6088:                                               $showemail.
                   6089:                                               &Apache::lonhtmlcommon::row_closure($closure));
1.391     raeburn  6090:                                 }
1.442     raeburn  6091:                                 if ($showstatus) {
                   6092:                                     $r->print(&Apache::lonhtmlcommon::row_title(&mt('Status type[_1](self-reported)','<br />')).
                   6093:                                               $showstatus.
                   6094:                                               &Apache::lonhtmlcommon::row_closure(1));
                   6095:                                 }
                   6096:                             }
                   6097:                             if ($pickstart) { 
                   6098:                                 $r->print(&Apache::lonhtmlcommon::end_pick_box().'</div>');
                   6099:                             } else {
                   6100:                                 $r->print('<div>'.&mt('No information to display for this account request.').'</div>');
1.391     raeburn  6101:                             }
1.442     raeburn  6102:                         } else {
                   6103:                             $r->print('<div>'.&mt('No information available for this account request.').'</div>');
1.391     raeburn  6104:                         }
                   6105:                     }
                   6106:                 }
                   6107:             }
                   6108:         }
1.442     raeburn  6109:         $r->print(&close_popup_form());
1.207     raeburn  6110:     } elsif (($env{'form.action'} eq 'listusers') && 
                   6111:              ($permission->{'view'} || $permission->{'cusr'})) {
1.439     raeburn  6112:         my $helpitem = 'Course_View_Class_List';
                   6113:         if ($context eq 'author') {
                   6114:             $helpitem = 'Author_View_Coauthor_List';
                   6115:         } elsif ($context eq 'domain') {
                   6116:             $helpitem = 'Domain_View_Users_List';
                   6117:         }
1.202     raeburn  6118:         if ($env{'form.phase'} eq 'bulkchange') {
1.351     raeburn  6119:             push(@{$brcrum},
                   6120:                     {href => '/adm/createuser?action=listusers',
                   6121:                      text => "List Users"},
                   6122:                     {href => "/adm/createuser",
                   6123:                      text => "Result",
1.439     raeburn  6124:                      help => $helpitem});
1.351     raeburn  6125:             $bread_crumbs_component = 'Update Users';
                   6126:             $args = {bread_crumbs           => $brcrum,
                   6127:                      bread_crumbs_component => $bread_crumbs_component};
                   6128:             $r->print(&header(undef,$args));
1.202     raeburn  6129:             my $setting = $env{'form.roletype'};
                   6130:             my $choice = $env{'form.bulkaction'};
                   6131:             if ($permission->{'cusr'}) {
1.336     raeburn  6132:                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice,$crstype);
1.221     raeburn  6133:             } else {
                   6134:                 $r->print(&mt('You are not authorized to make bulk changes to user roles'));
1.223     raeburn  6135:                 $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
1.202     raeburn  6136:             }
                   6137:         } else {
1.351     raeburn  6138:             push(@{$brcrum},
                   6139:                     {href => '/adm/createuser?action=listusers',
                   6140:                      text => "List Users",
1.439     raeburn  6141:                      help => $helpitem});
1.351     raeburn  6142:             $bread_crumbs_component = 'List Users';
                   6143:             $args = {bread_crumbs           => $brcrum,
                   6144:                      bread_crumbs_component => $bread_crumbs_component};
1.202     raeburn  6145:             my ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles);
                   6146:             my $formname = 'studentform';
1.364     raeburn  6147:             my $hidecall = "hide_searching();";
1.321     raeburn  6148:             if (($context eq 'domain') && (($env{'form.roletype'} eq 'course') ||
                   6149:                 ($env{'form.roletype'} eq 'community'))) {
                   6150:                 if ($env{'form.roletype'} eq 'course') {
                   6151:                     ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles) = 
                   6152:                         &Apache::lonuserutils::courses_selector($env{'request.role.domain'},
                   6153:                                                                 $formname);
                   6154:                 } elsif ($env{'form.roletype'} eq 'community') {
                   6155:                     $cb_jscript = 
                   6156:                         &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
                   6157:                     my %elements = (
                   6158:                                       coursepick => 'radio',
                   6159:                                       coursetotal => 'text',
                   6160:                                       courselist => 'text',
                   6161:                                    );
                   6162:                     $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   6163:                 }
1.364     raeburn  6164:                 $jscript .= &verify_user_display($context)."\n".
                   6165:                             &Apache::loncommon::check_uncheck_jscript();
1.202     raeburn  6166:                 my $js = &add_script($jscript).$cb_jscript;
                   6167:                 my $loadcode = 
                   6168:                     &Apache::lonuserutils::course_selector_loadcode($formname);
                   6169:                 if ($loadcode ne '') {
1.364     raeburn  6170:                     $args->{add_entries} = {onload => "$loadcode;$hidecall"};
                   6171:                 } else {
                   6172:                     $args->{add_entries} = {onload => $hidecall};
1.202     raeburn  6173:                 }
1.351     raeburn  6174:                 $r->print(&header($js,$args));
1.191     raeburn  6175:             } else {
1.364     raeburn  6176:                 $args->{add_entries} = {onload => $hidecall};
                   6177:                 $jscript = &verify_user_display($context).
                   6178:                            &Apache::loncommon::check_uncheck_jscript(); 
                   6179:                 $r->print(&header(&add_script($jscript),$args));
1.191     raeburn  6180:             }
1.202     raeburn  6181:             &Apache::lonuserutils::print_userlist($r,undef,$permission,$context,
1.375     raeburn  6182:                          $formname,$totcodes,$codetitles,$idlist,$idlist_titles,
                   6183:                          $showcredits);
1.191     raeburn  6184:         }
1.213     raeburn  6185:     } elsif ($env{'form.action'} eq 'drop' && $permission->{'cusr'}) {
1.318     raeburn  6186:         my $brtext;
                   6187:         if ($crstype eq 'Community') {
                   6188:             $brtext = 'Drop Members';
                   6189:         } else {
                   6190:             $brtext = 'Drop Students';
                   6191:         }
1.351     raeburn  6192:         push(@{$brcrum},
                   6193:                 {href => '/adm/createuser?action=drop',
                   6194:                  text => $brtext,
                   6195:                  help => 'Course_Drop_Student'});
                   6196:         if ($env{'form.state'} eq 'done') {
                   6197:             push(@{$brcrum},
                   6198:                      {href=>'/adm/createuser?action=drop',
                   6199:                       text=>"Result"});
                   6200:         }
                   6201:         $bread_crumbs_component = $brtext;
                   6202:         $args = {bread_crumbs           => $brcrum,
                   6203:                  bread_crumbs_component => $bread_crumbs_component}; 
                   6204:         $r->print(&header(undef,$args));
1.213     raeburn  6205:         if (!exists($env{'form.state'})) {
1.318     raeburn  6206:             &Apache::lonuserutils::print_drop_menu($r,$context,$permission,$crstype);
1.213     raeburn  6207:         } elsif ($env{'form.state'} eq 'done') {
                   6208:             &Apache::lonuserutils::update_user_list($r,$context,undef,
                   6209:                                                     $env{'form.action'});
                   6210:         }
1.202     raeburn  6211:     } elsif ($env{'form.action'} eq 'dateselect') {
                   6212:         if ($permission->{'cusr'}) {
1.351     raeburn  6213:             $r->print(&header(undef,{'no_nav_bar' => 1}).
1.375     raeburn  6214:                       &Apache::lonuserutils::date_section_selector($context,$permission,
                   6215:                                                                    $crstype,$showcredits));
1.202     raeburn  6216:         } else {
1.351     raeburn  6217:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   6218:                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>'); 
1.202     raeburn  6219:         }
1.237     raeburn  6220:     } elsif ($env{'form.action'} eq 'selfenroll') {
1.469     raeburn  6221:         my %currsettings;
                   6222:         if ($permission->{selfenrolladmin} || $permission->{selfenrollview}) {
                   6223:             %currsettings = (
1.398     raeburn  6224:                 selfenroll_types              => $env{'course.'.$cid.'.internal.selfenroll_types'},
                   6225:                 selfenroll_registered         => $env{'course.'.$cid.'.internal.selfenroll_registered'},
                   6226:                 selfenroll_section            => $env{'course.'.$cid.'.internal.selfenroll_section'},
                   6227:                 selfenroll_notifylist         => $env{'course.'.$cid.'.internal.selfenroll_notifylist'},
                   6228:                 selfenroll_approval           => $env{'course.'.$cid.'.internal.selfenroll_approval'},
                   6229:                 selfenroll_limit              => $env{'course.'.$cid.'.internal.selfenroll_limit'},
                   6230:                 selfenroll_cap                => $env{'course.'.$cid.'.internal.selfenroll_cap'},
                   6231:                 selfenroll_start_date         => $env{'course.'.$cid.'.internal.selfenroll_start_date'},
                   6232:                 selfenroll_end_date           => $env{'course.'.$cid.'.internal.selfenroll_end_date'},
                   6233:                 selfenroll_start_access       => $env{'course.'.$cid.'.internal.selfenroll_start_access'},
                   6234:                 selfenroll_end_access         => $env{'course.'.$cid.'.internal.selfenroll_end_access'},
                   6235:                 default_enrollment_start_date => $env{'course.'.$cid.'.default_enrollment_start_date'},
                   6236:                 default_enrollment_end_date   => $env{'course.'.$cid.'.default_enrollment_end_date'},
1.400     raeburn  6237:                 uniquecode                    => $env{'course.'.$cid.'.internal.uniquecode'},
1.398     raeburn  6238:             );
1.469     raeburn  6239:         }
                   6240:         if ($permission->{selfenrolladmin}) {
1.398     raeburn  6241:             push(@{$brcrum},
                   6242:                     {href => '/adm/createuser?action=selfenroll',
                   6243:                      text => "Configure Self-enrollment",
                   6244:                      help => 'Course_Self_Enrollment'});
                   6245:             if (!exists($env{'form.state'})) {
                   6246:                 $args = { bread_crumbs           => $brcrum,
                   6247:                           bread_crumbs_component => 'Configure Self-enrollment'};
                   6248:                 $r->print(&header(undef,$args));
                   6249:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
                   6250:                 &print_selfenroll_menu($r,'course',$cid,$cdom,$cnum,\%currsettings);
                   6251:             } elsif ($env{'form.state'} eq 'done') {
                   6252:                 push (@{$brcrum},
                   6253:                           {href=>'/adm/createuser?action=selfenroll',
                   6254:                            text=>"Result"});
                   6255:                 $args = { bread_crumbs           => $brcrum,
                   6256:                           bread_crumbs_component => 'Self-enrollment result'};
                   6257:                 $r->print(&header(undef,$args));
                   6258:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
1.400     raeburn  6259:                 &update_selfenroll_config($r,$cid,$cdom,$cnum,$context,$crstype,\%currsettings);
1.398     raeburn  6260:             }
1.469     raeburn  6261:         } elsif ($permission->{selfenrollview}) {
                   6262:             push(@{$brcrum},
                   6263:                     {href => '/adm/createuser?action=selfenroll',
                   6264:                      text => "View Self-enrollment configuration",
                   6265:                      help => 'Course_Self_Enrollment'});
                   6266:             $args = { bread_crumbs           => $brcrum,
                   6267:                       bread_crumbs_component => 'Self-enrollment Settings'};
                   6268:             $r->print(&header(undef,$args));
                   6269:             $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
                   6270:             &print_selfenroll_menu($r,'course',$cid,$cdom,$cnum,\%currsettings,'',1);
1.398     raeburn  6271:         } else {
                   6272:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   6273:                      '<span class="LC_error">'.&mt('You do not have permission to configure self-enrollment').'</span>');
1.237     raeburn  6274:         }
1.277     raeburn  6275:     } elsif ($env{'form.action'} eq 'selfenrollqueue') {
1.418     raeburn  6276:         if ($permission->{selfenrolladmin}) {
1.351     raeburn  6277:             push(@{$brcrum},
                   6278:                      {href => '/adm/createuser?action=selfenrollqueue',
1.418     raeburn  6279:                       text => 'Enrollment requests',
1.439     raeburn  6280:                       help => 'Course_Approve_Selfenroll'});
1.418     raeburn  6281:             $bread_crumbs_component = 'Enrollment requests';
                   6282:             if ($env{'form.state'} eq 'done') {
                   6283:                 push(@{$brcrum},
                   6284:                          {href => '/adm/createuser?action=selfenrollqueue',
                   6285:                           text => 'Result',
1.439     raeburn  6286:                           help => 'Course_Approve_Selfenroll'});
1.418     raeburn  6287:                 $bread_crumbs_component = 'Enrollment result';
                   6288:             }
                   6289:             $args = { bread_crumbs           => $brcrum,
                   6290:                       bread_crumbs_component => $bread_crumbs_component};
                   6291:             $r->print(&header(undef,$args));
                   6292:             my $coursedesc = $env{'course.'.$cid.'.description'};
                   6293:             if (!exists($env{'form.state'})) {
                   6294:                 $r->print('<h3>'.&mt('Pending enrollment requests').'</h3>'."\n");
                   6295:                 $r->print(&Apache::loncoursequeueadmin::display_queued_requests($context,
                   6296:                                                                                 $cdom,$cnum));
                   6297:             } elsif ($env{'form.state'} eq 'done') {
                   6298:                 $r->print('<h3>'.&mt('Enrollment request processing').'</h3>'."\n");
                   6299:                 $r->print(&Apache::loncoursequeueadmin::update_request_queue($context,
1.430     raeburn  6300:                               $cdom,$cnum,$coursedesc));
1.418     raeburn  6301:             }
                   6302:         } else {
                   6303:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   6304:                      '<span class="LC_error">'.&mt('You do not have permission to manage self-enrollment').'</span>');
1.351     raeburn  6305:         }
1.418     raeburn  6306:     } elsif ($env{'form.action'} eq 'changelogs') {
                   6307:         if ($permission->{cusr} || $permission->{view}) {
                   6308:             &print_userchangelogs_display($r,$context,$permission,$brcrum);
                   6309:         } else {
                   6310:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   6311:                      '<span class="LC_error">'.&mt('You do not have permission to view change logs').'</span>');
1.277     raeburn  6312:         }
1.428     raeburn  6313:     } elsif ($env{'form.action'} eq 'helpdesk') {
1.464     raeburn  6314:         if (($permission->{'owner'} || $permission->{'co-owner'}) &&
                   6315:             ($permission->{'cusr'} || $permission->{'view'})) {
1.428     raeburn  6316:             if ($env{'form.state'} eq 'process') {
                   6317:                 if ($permission->{'owner'}) {
                   6318:                     &update_helpdeskaccess($r,$permission,$brcrum);
                   6319:                 } else {
                   6320:                     &print_helpdeskaccess_display($r,$permission,$brcrum);
1.430     raeburn  6321:                 }
1.428     raeburn  6322:             } else {
                   6323:                 &print_helpdeskaccess_display($r,$permission,$brcrum);
                   6324:             }
                   6325:         } else {
                   6326:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   6327:                       '<span class="LC_error">'.&mt('You do not have permission to view helpdesk access').'</span>');
                   6328:         }
1.465     raeburn  6329:     } elsif ($env{'form.action'} eq 'rolerequests') {
                   6330:         if ($permission->{cusr} || $permission->{view}) {
                   6331:             &print_queued_roles($r,$context,$permission,$brcrum);
                   6332:         }
                   6333:     } elsif ($env{'form.action'} eq 'queuedroles') {
                   6334:         if (($permission->{cusr}) && ($context eq 'domain')) {
                   6335:             if (&show_role_requests($context,$env{'request.role.domain'})) {
                   6336:                 if ($env{'form.state'} eq 'done') {
                   6337:                     &process_pendingroles($r,$context,$permission,$brcrum);
                   6338:                 } else {
                   6339:                     &print_pendingroles($r,$context,$permission,$brcrum);
                   6340:                 }
                   6341:             } else {
                   6342:                 $r->print(&header(undef,{'no_nav_bar' => 1}).
                   6343:                           '<span class="LC_info">'.&mt('Domain coordinator approval of requests from other domains for assignment of roles to users from this domain not in use.').'</span>');
                   6344:             }
                   6345:         } else {
                   6346:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   6347:                      '<span class="LC_error">'.&mt('You do not have permission to view queued requests from other domains for assignment of roles to users from this domain.').'</span>');
                   6348:         }
1.470     raeburn  6349:     } elsif ($env{'form.action'} eq 'camanagers') {
                   6350:         if (($permission->{cusr}) && ($context eq 'author')) {
                   6351:             push(@{$brcrum},
                   6352:                      {href => '/adm/createuser?action=camanagers',
                   6353:                       text => 'Co-authors who manage',
                   6354:                       help => 'Author_Manage_Coauthors'});
                   6355:             if ($env{'form.state'} eq 'process') {
                   6356:                 push(@{$brcrum},
                   6357:                          {href => '/adm/createuser?action=camanagers',
                   6358:                           text => 'Result',
                   6359:                           help => 'Author_Manage_Coauthors'});
                   6360:             }
                   6361:             $args = { bread_crumbs           => $brcrum };
                   6362:             $r->print(&header(undef,$args));
                   6363:             my $coursedesc = $env{'course.'.$cid.'.description'};
                   6364:             if (!exists($env{'form.state'})) {
                   6365:                 $r->print('<h3>'.&mt('Co-author Management').'</h3>'."\n".
                   6366:                           &display_coauthor_managers($permission));
                   6367:             } elsif ($env{'form.state'} eq 'process') {
                   6368:                 $r->print('<h3>'.&mt('Co-author Management Update Result').'</h3>'."\n".
                   6369:                           &update_coauthor_managers($permission));
                   6370:             }
                   6371:         }
                   6372:     } elsif (($env{'form.action'} eq 'calist') && ($context eq 'author')) {
                   6373:         if ($permission->{'cusr'}) {
                   6374:             my ($role,$audom,$auname,$canview,$canedit) =
                   6375:                 &Apache::lonviewcoauthors::get_allowable();
                   6376:             if (($canedit) && ($env{'form.forceedit'})) {
                   6377:                 &Apache::lonviewcoauthors::get_editor_crumbs($brcrum,'/adm/createuser');
                   6378:                 my $args = { 'bread_crumbs' => $brcrum };
                   6379:                 $r->print(&Apache::loncommon::start_page('Configure co-author listing',undef,
                   6380:                                                          $args).
                   6381:                           &Apache::lonviewcoauthors::edit_settings($audom,$auname,$role,
                   6382:                                                                    '/adm/createuser'));
                   6383:             } else {
                   6384:                 push(@{$brcrum},
                   6385:                        {href => '/adm/createuser?action=calist',
                   6386:                         text => 'Coauthor-viewable list',
                   6387:                         help => 'Author_List_Coauthors'});
                   6388:                 my $args = { 'bread_crumbs' => $brcrum };
                   6389:                 $r->print(&Apache::loncommon::start_page('Coauthor-viewable list',undef,
                   6390:                                                          $args));
                   6391:                 my %viewsettings =
                   6392:                     &Apache::lonviewcoauthors::retrieve_view_settings($auname,$audom,$role);
                   6393:                 if ($viewsettings{'show'} eq 'none') {
                   6394:                     $r->print('<h3>'.&mt('Coauthor-viewable listing').'</h3>'.
                   6395:                               '<p class="LC_info">'.
                   6396:                               &mt('Listing of co-authors not enabled for this Authoring Space').
                   6397:                               '</p>');
                   6398:                 } else {
                   6399:                     &Apache::lonviewcoauthors::print_coauthors($r,$auname,$audom,$role,
                   6400:                                                                '/adm/createuser',\%viewsettings);
                   6401:                 }
                   6402:             }
                   6403:         } else {
                   6404:             $r->internal_redirect('/adm/viewcoauthors');
                   6405:             return OK;
                   6406:         }
1.190     raeburn  6407:     } else {
1.351     raeburn  6408:         $bread_crumbs_component = 'User Management';
                   6409:         $args = { bread_crumbs           => $brcrum,
                   6410:                   bread_crumbs_component => $bread_crumbs_component};
                   6411:         $r->print(&header(undef,$args));
1.318     raeburn  6412:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  6413:     }
1.351     raeburn  6414:     $r->print(&Apache::loncommon::end_page());
1.190     raeburn  6415:     return OK;
                   6416: }
                   6417: 
                   6418: sub header {
1.351     raeburn  6419:     my ($jscript,$args) = @_;
1.190     raeburn  6420:     my $start_page;
1.351     raeburn  6421:     if (ref($args) eq 'HASH') {
                   6422:         $start_page=&Apache::loncommon::start_page('User Management',$jscript,$args);
1.190     raeburn  6423:     } else {
1.351     raeburn  6424:         $start_page=&Apache::loncommon::start_page('User Management',$jscript);
1.190     raeburn  6425:     }
                   6426:     return $start_page;
                   6427: }
1.2       www      6428: 
1.191     raeburn  6429: sub add_script {
                   6430:     my ($js) = @_;
1.301     bisitz   6431:     return '<script type="text/javascript">'."\n"
                   6432:           .'// <![CDATA['."\n"
                   6433:           .$js."\n"
                   6434:           .'// ]]>'."\n"
                   6435:           .'</script>'."\n";
1.191     raeburn  6436: }
                   6437: 
1.391     raeburn  6438: sub usernamerequest_javascript {
                   6439:     my $js = <<ENDJS;
                   6440: 
                   6441: function openusernamereqdisplay(dom,uname,queue) {
                   6442:     var url = '/adm/createuser?action=displayuserreq';
                   6443:     url += '&domain='+dom+'&username='+uname+'&queue='+queue;
                   6444:     var title = 'Account_Request_Browser';
                   6445:     var options = 'scrollbars=1,resizable=1,menubar=0';
                   6446:     options += ',width=700,height=600';
                   6447:     var stdeditbrowser = open(url,title,options,'1');
                   6448:     stdeditbrowser.focus();
                   6449:     return;
                   6450: }
                   6451:  
                   6452: ENDJS
                   6453: }
                   6454: 
                   6455: sub close_popup_form {
                   6456:     my $close= &mt('Close Window');
                   6457:     return << "END";
                   6458: <p><form name="displayreq" action="" method="post">
                   6459: <input type="button" name="closeme" value="$close" onclick="javascript:self.close();" />
                   6460: </form></p>
                   6461: END
                   6462: }
                   6463: 
1.202     raeburn  6464: sub verify_user_display {
1.364     raeburn  6465:     my ($context) = @_;
1.374     raeburn  6466:     my %lt = &Apache::lonlocal::texthash (
                   6467:         course    => 'course(s): description, section(s), status',
                   6468:         community => 'community(s): description, section(s), status',
                   6469:         author    => 'author',
                   6470:     );
1.364     raeburn  6471:     my $photos;
                   6472:     if (($context eq 'course') && $env{'request.course.id'}) {
                   6473:         $photos = $env{'course.'.$env{'request.course.id'}.'.internal.showphoto'};
                   6474:     }
1.202     raeburn  6475:     my $output = <<"END";
                   6476: 
1.364     raeburn  6477: function hide_searching() {
                   6478:     if (document.getElementById('searching')) {
                   6479:         document.getElementById('searching').style.display = 'none';
                   6480:     }
                   6481:     return;
                   6482: }
                   6483: 
1.202     raeburn  6484: function display_update() {
                   6485:     document.studentform.action.value = 'listusers';
                   6486:     document.studentform.phase.value = 'display';
                   6487:     document.studentform.submit();
                   6488: }
                   6489: 
1.364     raeburn  6490: function updateCols(caller) {
                   6491:     var context = '$context';
                   6492:     var photos = '$photos';
                   6493:     if (caller == 'Status') {
1.374     raeburn  6494:         if ((context == 'domain') && 
                   6495:             ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   6496:              (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community'))) {
1.364     raeburn  6497:             document.getElementById('showcolstatus').checked = false;
                   6498:             document.getElementById('showcolstatus').disabled = 'disabled';
                   6499:             document.getElementById('showcolstart').checked = false;
                   6500:             document.getElementById('showcolend').checked = false;
1.374     raeburn  6501:         } else {
                   6502:             if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   6503:                 document.getElementById('showcolstatus').checked = true;
                   6504:                 document.getElementById('showcolstatus').disabled = '';
                   6505:                 document.getElementById('showcolstart').checked = true;
                   6506:                 document.getElementById('showcolend').checked = true;
                   6507:             } else {
                   6508:                 document.getElementById('showcolstatus').checked = false;
                   6509:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   6510:                 document.getElementById('showcolstart').checked = false;
                   6511:                 document.getElementById('showcolend').checked = false;
                   6512:             }
1.364     raeburn  6513:         }
                   6514:     }
                   6515:     if (caller == 'output') {
                   6516:         if (photos == 1) {
                   6517:             if (document.getElementById('showcolphoto')) {
                   6518:                 var photoitem = document.getElementById('showcolphoto');
                   6519:                 if (document.studentform.output.options[document.studentform.output.selectedIndex].value == 'html') {
                   6520:                     photoitem.checked = true;
                   6521:                     photoitem.disabled = '';
                   6522:                 } else {
                   6523:                     photoitem.checked = false;
                   6524:                     photoitem.disabled = 'disabled';
                   6525:                 }
                   6526:             }
                   6527:         }
                   6528:     }
                   6529:     if (caller == 'showrole') {
1.371     raeburn  6530:         if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any') ||
                   6531:             (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'cr')) {
1.364     raeburn  6532:             document.getElementById('showcolrole').checked = true;
                   6533:             document.getElementById('showcolrole').disabled = '';
                   6534:         } else {
                   6535:             document.getElementById('showcolrole').checked = false;
                   6536:             document.getElementById('showcolrole').disabled = 'disabled';
                   6537:         }
1.374     raeburn  6538:         if (context == 'domain') {
1.382     raeburn  6539:             var quotausageshow = 0;
1.374     raeburn  6540:             if ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   6541:                 (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community')) {
                   6542:                 document.getElementById('showcolstatus').checked = false;
                   6543:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   6544:                 document.getElementById('showcolstart').checked = false;
                   6545:                 document.getElementById('showcolend').checked = false;
                   6546:             } else {
                   6547:                 if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   6548:                     document.getElementById('showcolstatus').checked = true;
                   6549:                     document.getElementById('showcolstatus').disabled = '';
                   6550:                     document.getElementById('showcolstart').checked = true;
                   6551:                     document.getElementById('showcolend').checked = true;
                   6552:                 }
                   6553:             }
                   6554:             if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'domain') {
                   6555:                 document.getElementById('showcolextent').disabled = 'disabled';
                   6556:                 document.getElementById('showcolextent').checked = 'false';
                   6557:                 document.getElementById('showextent').style.display='none';
                   6558:                 document.getElementById('showcoltextextent').innerHTML = '';
1.382     raeburn  6559:                 if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'au') ||
                   6560:                     (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any')) {
                   6561:                     if (document.getElementById('showcolauthorusage')) {
                   6562:                         document.getElementById('showcolauthorusage').disabled = '';
                   6563:                     }
                   6564:                     if (document.getElementById('showcolauthorquota')) {
                   6565:                         document.getElementById('showcolauthorquota').disabled = '';
                   6566:                     }
                   6567:                     quotausageshow = 1;
                   6568:                 }
1.374     raeburn  6569:             } else {
                   6570:                 document.getElementById('showextent').style.display='block';
                   6571:                 document.getElementById('showextent').style.textAlign='left';
                   6572:                 document.getElementById('showextent').style.textFace='normal';
                   6573:                 if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'author') {
                   6574:                     document.getElementById('showcolextent').disabled = '';
                   6575:                     document.getElementById('showcolextent').checked = 'true';
                   6576:                     document.getElementById('showcoltextextent').innerHTML="$lt{'author'}";
                   6577:                 } else {
                   6578:                     document.getElementById('showcolextent').disabled = '';
                   6579:                     document.getElementById('showcolextent').checked = 'true';
                   6580:                     if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community') {
                   6581:                         document.getElementById('showcoltextextent').innerHTML="$lt{'community'}";
                   6582:                     } else {
                   6583:                         document.getElementById('showcoltextextent').innerHTML="$lt{'course'}";
                   6584:                     }
                   6585:                 }
                   6586:             }
1.382     raeburn  6587:             if (quotausageshow == 0)  {
                   6588:                 if (document.getElementById('showcolauthorusage')) {
                   6589:                     document.getElementById('showcolauthorusage').checked = false;
                   6590:                     document.getElementById('showcolauthorusage').disabled = 'disabled';
                   6591:                 }
                   6592:                 if (document.getElementById('showcolauthorquota')) {
                   6593:                     document.getElementById('showcolauthorquota').checked = false;
                   6594:                     document.getElementById('showcolauthorquota').disabled = 'disabled';
                   6595:                 }
                   6596:             }
1.374     raeburn  6597:         }
1.364     raeburn  6598:     }
                   6599:     return;
                   6600: }
                   6601: 
1.202     raeburn  6602: END
                   6603:     return $output;
                   6604: 
                   6605: }
                   6606: 
1.190     raeburn  6607: ###############################################################
                   6608: ###############################################################
                   6609: #  Menu Phase One
                   6610: sub print_main_menu {
1.318     raeburn  6611:     my ($permission,$context,$crstype) = @_;
                   6612:     my $linkcontext = $context;
                   6613:     my $stuterm = lc(&Apache::lonnet::plaintext('st',$crstype));
                   6614:     if (($context eq 'course') && ($crstype eq 'Community')) {
                   6615:         $linkcontext = lc($crstype);
                   6616:         $stuterm = 'Members';
                   6617:     }
1.208     raeburn  6618:     my %links = (
1.298     droeschl 6619:                 domain => {
                   6620:                             upload     => 'Upload a File of Users',
                   6621:                             singleuser => 'Add/Modify a User',
                   6622:                             listusers  => 'Manage Users',
                   6623:                             },
                   6624:                 author => {
                   6625:                             upload     => 'Upload a File of Co-authors',
                   6626:                             singleuser => 'Add/Modify a Co-author',
                   6627:                             listusers  => 'Manage Co-authors',
                   6628:                             },
                   6629:                 course => {
                   6630:                             upload     => 'Upload a File of Course Users',
                   6631:                             singleuser => 'Add/Modify a Course User',
1.354     www      6632:                             listusers  => 'List and Modify Multiple Course Users',
1.298     droeschl 6633:                             },
1.318     raeburn  6634:                 community => {
                   6635:                             upload     => 'Upload a File of Community Users',
                   6636:                             singleuser => 'Add/Modify a Community User',
1.354     www      6637:                             listusers  => 'List and Modify Multiple Community Users',
1.318     raeburn  6638:                            },
                   6639:                 );
                   6640:      my %linktitles = (
                   6641:                 domain => {
                   6642:                             singleuser => 'Add a user to the domain, and/or a course or community in the domain.',
                   6643:                             listusers  => 'Show and manage users in this domain.',
                   6644:                             },
                   6645:                 author => {
                   6646:                             singleuser => 'Add a user with a co- or assistant author role.',
                   6647:                             listusers  => 'Show and manage co- or assistant authors.',
                   6648:                             },
                   6649:                 course => {
                   6650:                             singleuser => 'Add a user with a certain role to this course.',
                   6651:                             listusers  => 'Show and manage users in this course.',
                   6652:                             },
                   6653:                 community => {
                   6654:                             singleuser => 'Add a user with a certain role to this community.',
                   6655:                             listusers  => 'Show and manage users in this community.',
                   6656:                            },
1.298     droeschl 6657:                 );
1.465     raeburn  6658: 
1.418     raeburn  6659:   if ($linkcontext eq 'domain') {
                   6660:       unless ($permission->{'cusr'}) {
1.430     raeburn  6661:           $links{'domain'}{'singleuser'} = 'View a User';
1.418     raeburn  6662:           $linktitles{'domain'}{'singleuser'} = 'View information about a user in the domain';
                   6663:       }
                   6664:   } elsif ($linkcontext eq 'course') {
                   6665:       unless ($permission->{'cusr'}) {
                   6666:           $links{'course'}{'singleuser'} = 'View a Course User';
                   6667:           $linktitles{'course'}{'singleuser'} = 'View information about a user in this course';
                   6668:           $links{'course'}{'listusers'} = 'List Course Users';
                   6669:           $linktitles{'course'}{'listusers'} = 'Show information about users in this course';
                   6670:       }
                   6671:   } elsif ($linkcontext eq 'community') {
                   6672:       unless ($permission->{'cusr'}) {
                   6673:           $links{'community'}{'singleuser'} = 'View a Community User';
                   6674:           $linktitles{'community'}{'singleuser'} = 'View information about a user in this community';
                   6675:           $links{'community'}{'listusers'} = 'List Community Users';
                   6676:           $linktitles{'community'}{'listusers'} = 'Show information about users in this community';
                   6677:       }
                   6678:   }
1.298     droeschl 6679:   my @menu = ( {categorytitle => 'Single Users', 
                   6680:          items =>
                   6681:          [
                   6682:             {
1.318     raeburn  6683:              linktext => $links{$linkcontext}{'singleuser'},
1.298     droeschl 6684:              icon => 'edit-redo.png',
                   6685:              #help => 'Course_Change_Privileges',
                   6686:              url => '/adm/createuser?action=singleuser',
1.418     raeburn  6687:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  6688:              linktitle => $linktitles{$linkcontext}{'singleuser'},
1.298     droeschl 6689:             },
                   6690:          ]},
                   6691: 
                   6692:          {categorytitle => 'Multiple Users',
                   6693:          items => 
                   6694:          [
                   6695:             {
1.318     raeburn  6696:              linktext => $links{$linkcontext}{'upload'},
1.340     wenzelju 6697:              icon => 'uplusr.png',
1.298     droeschl 6698:              #help => 'Course_Create_Class_List',
                   6699:              url => '/adm/createuser?action=upload',
                   6700:              permission => $permission->{'cusr'},
                   6701:              linktitle => 'Upload a CSV or a text file containing users.',
                   6702:             },
                   6703:             {
1.318     raeburn  6704:              linktext => $links{$linkcontext}{'listusers'},
1.340     wenzelju 6705:              icon => 'mngcu.png',
1.298     droeschl 6706:              #help => 'Course_View_Class_List',
                   6707:              url => '/adm/createuser?action=listusers',
                   6708:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  6709:              linktitle => $linktitles{$linkcontext}{'listusers'}, 
1.298     droeschl 6710:             },
                   6711: 
                   6712:          ]},
                   6713: 
                   6714:          {categorytitle => 'Administration',
                   6715:          items => [ ]},
                   6716:        );
1.415     raeburn  6717: 
1.265     mielkec  6718:     if ($context eq 'domain'){
1.416     raeburn  6719:         push(@{  $menu[0]->{items} }, # Single Users
                   6720:             {
                   6721:              linktext => 'User Access Log',
1.417     raeburn  6722:              icon => 'document-properties.png',
1.425     raeburn  6723:              #help => 'Domain_User_Access_Logs',
1.416     raeburn  6724:              url => '/adm/createuser?action=accesslogs',
                   6725:              permission => $permission->{'activity'},
                   6726:              linktitle => 'View user access log.',
                   6727:             }
                   6728:         );
1.298     droeschl 6729:         
                   6730:         push(@{ $menu[2]->{items} }, #Category: Administration
                   6731:             {
                   6732:              linktext => 'Custom Roles',
                   6733:              icon => 'emblem-photos.png',
                   6734:              #help => 'Course_Editing_Custom_Roles',
                   6735:              url => '/adm/createuser?action=custom',
                   6736:              permission => $permission->{'custom'},
                   6737:              linktitle => 'Configure a custom role.',
                   6738:             },
1.362     raeburn  6739:             {
                   6740:              linktext => 'Authoring Space Requests',
                   6741:              icon => 'selfenrl-queue.png',
                   6742:              #help => 'Domain_Role_Approvals',
                   6743:              url => '/adm/createuser?action=processauthorreq',
                   6744:              permission => $permission->{'cusr'},
                   6745:              linktitle => 'Approve or reject author role requests',
                   6746:             },
1.363     raeburn  6747:             {
1.391     raeburn  6748:              linktext => 'LON-CAPA Account Requests',
                   6749:              icon => 'list-add.png',
                   6750:              #help => 'Domain_Username_Approvals',
                   6751:              url => '/adm/createuser?action=processusernamereq',
                   6752:              permission => $permission->{'cusr'},
                   6753:              linktitle => 'Approve or reject LON-CAPA account requests',
                   6754:             },
                   6755:             {
1.363     raeburn  6756:              linktext => 'Change Log',
                   6757:              icon => 'document-properties.png',
                   6758:              #help => 'Course_User_Logs',
                   6759:              url => '/adm/createuser?action=changelogs',
1.418     raeburn  6760:              permission => ($permission->{'cusr'} || $permission->{'view'}),
1.363     raeburn  6761:              linktitle => 'View change log.',
                   6762:             },
1.298     droeschl 6763:         );
                   6764:         
1.265     mielkec  6765:     }elsif ($context eq 'course'){
1.298     droeschl 6766:         my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
1.318     raeburn  6767: 
                   6768:         my %linktext = (
                   6769:                          'Course'    => {
                   6770:                                           single => 'Add/Modify a Student', 
                   6771:                                           drop   => 'Drop Students',
                   6772:                                           groups => 'Course Groups',
                   6773:                                         },
                   6774:                          'Community' => {
                   6775:                                           single => 'Add/Modify a Member', 
                   6776:                                           drop   => 'Drop Members',
                   6777:                                           groups => 'Community Groups',
                   6778:                                         },
                   6779:                        );
1.411     raeburn  6780:         $linktext{'Placement'} = $linktext{'Course'};
1.318     raeburn  6781: 
                   6782:         my %linktitle = (
                   6783:             'Course' => {
                   6784:                   single => 'Add a user with the role of student to this course',
                   6785:                   drop   => 'Remove a student from this course.',
                   6786:                   groups => 'Manage course groups',
                   6787:                         },
                   6788:             'Community' => {
                   6789:                   single => 'Add a user with the role of member to this community',
                   6790:                   drop   => 'Remove a member from this community.',
                   6791:                   groups => 'Manage community groups',
                   6792:                            },
                   6793:         );
                   6794: 
1.411     raeburn  6795:         $linktitle{'Placement'} = $linktitle{'Course'};
                   6796: 
1.298     droeschl 6797:         push(@{ $menu[0]->{items} }, #Category: Single Users
                   6798:             {   
1.318     raeburn  6799:              linktext => $linktext{$crstype}{'single'},
1.298     droeschl 6800:              #help => 'Course_Add_Student',
                   6801:              icon => 'list-add.png',
                   6802:              url => '/adm/createuser?action=singlestudent',
                   6803:              permission => $permission->{'cusr'},
1.318     raeburn  6804:              linktitle => $linktitle{$crstype}{'single'},
1.298     droeschl 6805:             },
                   6806:         );
                   6807:         
                   6808:         push(@{ $menu[1]->{items} }, #Category: Multiple Users 
                   6809:             {
1.318     raeburn  6810:              linktext => $linktext{$crstype}{'drop'},
1.298     droeschl 6811:              icon => 'edit-undo.png',
                   6812:              #help => 'Course_Drop_Student',
                   6813:              url => '/adm/createuser?action=drop',
                   6814:              permission => $permission->{'cusr'},
1.318     raeburn  6815:              linktitle => $linktitle{$crstype}{'drop'},
1.298     droeschl 6816:             },
                   6817:         );
                   6818:         push(@{ $menu[2]->{items} }, #Category: Administration
1.428     raeburn  6819:             {
                   6820:              linktext => 'Helpdesk Access',
                   6821:              icon => 'helpdesk-access.png',
                   6822:              #help => 'Course_Helpdesk_Access',
                   6823:              url => '/adm/createuser?action=helpdesk',
1.464     raeburn  6824:              permission => (($permission->{'owner'} || $permission->{'co-owner'}) &&
                   6825:                             ($permission->{'view'} || $permission->{'cusr'})),
1.428     raeburn  6826:              linktitle => 'Helpdesk access options',
                   6827:             },
                   6828:             {
1.298     droeschl 6829:              linktext => 'Custom Roles',
                   6830:              icon => 'emblem-photos.png',
                   6831:              #help => 'Course_Editing_Custom_Roles',
                   6832:              url => '/adm/createuser?action=custom',
                   6833:              permission => $permission->{'custom'},
                   6834:              linktitle => 'Configure a custom role.',
                   6835:             },
                   6836:             {
1.318     raeburn  6837:              linktext => $linktext{$crstype}{'groups'},
1.333     wenzelju 6838:              icon => 'grps.png',
1.298     droeschl 6839:              #help => 'Course_Manage_Group',
                   6840:              url => '/adm/coursegroups?refpage=cusr',
                   6841:              permission => $permission->{'grp_manage'},
1.318     raeburn  6842:              linktitle => $linktitle{$crstype}{'groups'},
1.298     droeschl 6843:             },
                   6844:             {
1.328     wenzelju 6845:              linktext => 'Change Log',
1.298     droeschl 6846:              icon => 'document-properties.png',
                   6847:              #help => 'Course_User_Logs',
                   6848:              url => '/adm/createuser?action=changelogs',
1.418     raeburn  6849:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.298     droeschl 6850:              linktitle => 'View change log.',
                   6851:             },
                   6852:         );
1.277     raeburn  6853:         if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'}) {
1.298     droeschl 6854:             push(@{ $menu[2]->{items} },
1.398     raeburn  6855:                     {
1.298     droeschl 6856:                      linktext => 'Enrollment Requests',
                   6857:                      icon => 'selfenrl-queue.png',
                   6858:                      #help => 'Course_Approve_Selfenroll',
                   6859:                      url => '/adm/createuser?action=selfenrollqueue',
1.469     raeburn  6860:                      permission => $permission->{'selfenrolladmin'} || $permission->{'selfenrollview'},
1.298     droeschl 6861:                      linktitle =>'Approve or reject enrollment requests.',
                   6862:                     },
                   6863:             );
1.277     raeburn  6864:         }
1.298     droeschl 6865:         
1.265     mielkec  6866:         if (!exists($permission->{'cusr_section'})){
1.320     raeburn  6867:             if ($crstype ne 'Community') {
                   6868:                 push(@{ $menu[2]->{items} },
                   6869:                     {
                   6870:                      linktext => 'Automated Enrollment',
                   6871:                      icon => 'roles.png',
                   6872:                      #help => 'Course_Automated_Enrollment',
                   6873:                      permission => (&Apache::lonnet::auto_run($cnum,$cdom)
1.418     raeburn  6874:                                          && (($permission->{'cusr'}) ||
                   6875:                                              ($permission->{'view'}))),
1.320     raeburn  6876:                      url  => '/adm/populate',
                   6877:                      linktitle => 'Automated enrollment manager.',
                   6878:                     }
                   6879:                 );
                   6880:             }
                   6881:             push(@{ $menu[2]->{items} }, 
1.298     droeschl 6882:                 {
                   6883:                  linktext => 'User Self-Enrollment',
1.342     wenzelju 6884:                  icon => 'self_enroll.png',
1.298     droeschl 6885:                  #help => 'Course_Self_Enrollment',
                   6886:                  url => '/adm/createuser?action=selfenroll',
1.469     raeburn  6887:                  permission => $permission->{'selfenrolladmin'} || $permission->{'selfenrollview'},
1.317     bisitz   6888:                  linktitle => 'Configure user self-enrollment.',
1.298     droeschl 6889:                 },
                   6890:             );
                   6891:         }
1.363     raeburn  6892:     } elsif ($context eq 'author') {
1.370     raeburn  6893:         push(@{ $menu[2]->{items} }, #Category: Administration
1.363     raeburn  6894:             {
                   6895:              linktext => 'Change Log',
                   6896:              icon => 'document-properties.png',
                   6897:              #help => 'Course_User_Logs',
                   6898:              url => '/adm/createuser?action=changelogs',
                   6899:              permission => $permission->{'cusr'},
                   6900:              linktitle => 'View change log.',
                   6901:             },
1.470     raeburn  6902:             {
                   6903:              linktext => 'Co-authors who can add/revoke co-author roles',
                   6904:              icon => 'helpdesk-access.png',
                   6905:              #help => 'Coauthor_Management',
                   6906:              url => '/adm/createuser?action=camanagers',
                   6907:              permission => $permission->{'author'},
                   6908:              linktitle => 'Assign/Revoke right to manage co-author roles',
                   6909:             },
                   6910:             {
                   6911:              linktext => 'Configure coauthor-viewable listing',
                   6912:              icon => 'helpdesk-access.png',
                   6913:              #help => 'Coauthor_Settings',
                   6914:              url => '/adm/createuser?action=calist&forceedit=1',
                   6915:              permission => ($permission->{'cusr'}),
                   6916:              linktitle => 'Set availability of coauthor-viewable user listing',
                   6917:             },
1.370     raeburn  6918:         );
1.363     raeburn  6919:     }
1.465     raeburn  6920:     push(@{ $menu[2]->{items} },
                   6921:         {
                   6922:          linktext => 'Role Requests (other domains)',
                   6923:          icon => 'edit-find.png',
                   6924:          #help => 'Role_Requests',
                   6925:          url => '/adm/createuser?action=rolerequests',
                   6926:          permission => $permission->{'cusr'},
                   6927:          linktitle => 'Role requests for users in other domains',
                   6928:         },
                   6929:     );
                   6930:     if (&show_role_requests($context,$env{'request.role.domain'})) {
                   6931:         push(@{ $menu[2]->{items} },
                   6932:             {
                   6933:              linktext => 'Queued Role Assignments (this domain)',
                   6934:              icon => 'edit-find.png',
                   6935:              #help => 'Role_Approvals',
                   6936:              url => '/adm/createuser?action=queuedroles',
                   6937:              permission => $permission->{'cusr'},
                   6938:              linktitle => "Role requests for this domain's users",
                   6939:             },
                   6940:         );
                   6941:     }
1.363     raeburn  6942:     return Apache::lonhtmlcommon::generate_menu(@menu);
1.250     raeburn  6943: #               { text => 'View Log-in History',
                   6944: #                 help => 'Course_User_Logins',
                   6945: #                 action => 'logins',
                   6946: #                 permission => $permission->{'cusr'},
                   6947: #               });
1.190     raeburn  6948: }
                   6949: 
1.189     albertel 6950: sub restore_prev_selections {
                   6951:     my %saveable_parameters = ('srchby'   => 'scalar',
                   6952: 			       'srchin'   => 'scalar',
                   6953: 			       'srchtype' => 'scalar',
                   6954: 			       );
                   6955:     &Apache::loncommon::store_settings('user','user_picker',
                   6956: 				       \%saveable_parameters);
                   6957:     &Apache::loncommon::restore_settings('user','user_picker',
                   6958: 					 \%saveable_parameters);
                   6959: }
                   6960: 
1.237     raeburn  6961: sub print_selfenroll_menu {
1.418     raeburn  6962:     my ($r,$context,$cid,$cdom,$cnum,$currsettings,$additional,$readonly) = @_;
1.322     raeburn  6963:     my $crstype = &Apache::loncommon::course_type();
1.398     raeburn  6964:     my $formname = 'selfenroll';
1.237     raeburn  6965:     my $nolink = 1;
1.398     raeburn  6966:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
1.237     raeburn  6967:     my $groupslist = &Apache::lonuserutils::get_groupslist();
                   6968:     my $setsec_js = 
                   6969:         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
1.249     raeburn  6970:     my %alerts = &Apache::lonlocal::texthash(
                   6971:         acto => 'Activation of self-enrollment was selected for the following domain(s)',
                   6972:         butn => 'but no user types have been checked.',
                   6973:         wilf => "Please uncheck 'activate' or check at least one type.",
                   6974:     );
1.418     raeburn  6975:     my $disabled;
                   6976:     if ($readonly) {
                   6977:        $disabled = ' disabled="disabled"';
                   6978:     }
1.405     damieng  6979:     &js_escape(\%alerts);
1.249     raeburn  6980:     my $selfenroll_js = <<"ENDSCRIPT";
                   6981: function update_types(caller,num) {
                   6982:     var delidx = getIndexByName('selfenroll_delete');
                   6983:     var actidx = getIndexByName('selfenroll_activate');
                   6984:     if (caller == 'selfenroll_all') {
                   6985:         var selall;
                   6986:         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   6987:             if (document.$formname.selfenroll_all[i].checked) {
                   6988:                 selall = document.$formname.selfenroll_all[i].value;
                   6989:             }
                   6990:         }
                   6991:         if (selall == 1) {
                   6992:             if (delidx != -1) {
                   6993:                 if (document.$formname.selfenroll_delete.length) {
                   6994:                     for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   6995:                         document.$formname.selfenroll_delete[j].checked = true;
                   6996:                     }
                   6997:                 } else {
                   6998:                     document.$formname.elements[delidx].checked = true;
                   6999:                 }
                   7000:             }
                   7001:             if (actidx != -1) {
                   7002:                 if (document.$formname.selfenroll_activate.length) {
                   7003:                     for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   7004:                         document.$formname.selfenroll_activate[j].checked = false;
                   7005:                     }
                   7006:                 } else {
                   7007:                     document.$formname.elements[actidx].checked = false;
                   7008:                 }
                   7009:             }
                   7010:             document.$formname.selfenroll_newdom.selectedIndex = 0; 
                   7011:         }
                   7012:     }
                   7013:     if (caller == 'selfenroll_activate') {
                   7014:         if (document.$formname.selfenroll_activate.length) {
                   7015:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   7016:                 if (document.$formname.selfenroll_activate[j].value == num) {
                   7017:                     if (document.$formname.selfenroll_activate[j].checked) {
                   7018:                         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   7019:                             if (document.$formname.selfenroll_all[i].value == '1') {
                   7020:                                 document.$formname.selfenroll_all[i].checked = false;
                   7021:                             }
                   7022:                             if (document.$formname.selfenroll_all[i].value == '0') {
                   7023:                                 document.$formname.selfenroll_all[i].checked = true;
                   7024:                             }
                   7025:                         }
                   7026:                     }
                   7027:                 }
                   7028:             }
                   7029:         } else {
                   7030:             for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   7031:                 if (document.$formname.selfenroll_all[i].value == '1') {
                   7032:                     document.$formname.selfenroll_all[i].checked = false;
                   7033:                 }
                   7034:                 if (document.$formname.selfenroll_all[i].value == '0') {
                   7035:                     document.$formname.selfenroll_all[i].checked = true;
                   7036:                 }
                   7037:             }
                   7038:         }
                   7039:     }
                   7040:     if (caller == 'selfenroll_delete') {
                   7041:         if (document.$formname.selfenroll_delete.length) {
                   7042:             for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   7043:                 if (document.$formname.selfenroll_delete[j].value == num) {
                   7044:                     if (document.$formname.selfenroll_delete[j].checked) {
                   7045:                         var delindex = getIndexByName('selfenroll_types_'+num);
                   7046:                         if (delindex != -1) { 
                   7047:                             if (document.$formname.elements[delindex].length) {
                   7048:                                 for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   7049:                                     document.$formname.elements[delindex][k].checked = false;
                   7050:                                 }
                   7051:                             } else {
                   7052:                                 document.$formname.elements[delindex].checked = false;
                   7053:                             }
                   7054:                         }
                   7055:                     }
                   7056:                 }
                   7057:             }
                   7058:         } else {
                   7059:             if (document.$formname.selfenroll_delete.checked) {
                   7060:                 var delindex = getIndexByName('selfenroll_types_'+num);
                   7061:                 if (delindex != -1) {
                   7062:                     if (document.$formname.elements[delindex].length) {
                   7063:                         for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   7064:                             document.$formname.elements[delindex][k].checked = false;
                   7065:                         }
                   7066:                     } else {
                   7067:                         document.$formname.elements[delindex].checked = false;
                   7068:                     }
                   7069:                 }
                   7070:             }
                   7071:         }
                   7072:     }
                   7073:     return;
                   7074: }
                   7075: 
                   7076: function validate_types(form) {
                   7077:     var needaction = new Array();
                   7078:     var countfail = 0;
                   7079:     var actidx = getIndexByName('selfenroll_activate');
                   7080:     if (actidx != -1) {
                   7081:         if (document.$formname.selfenroll_activate.length) {
                   7082:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   7083:                 var num = document.$formname.selfenroll_activate[j].value;
                   7084:                 if (document.$formname.selfenroll_activate[j].checked) {
                   7085:                     countfail = check_types(num,countfail,needaction)
                   7086:                 }
                   7087:             }
                   7088:         } else {
                   7089:             if (document.$formname.selfenroll_activate.checked) {
1.398     raeburn  7090:                 var num = document.$formname.selfenroll_activate.value;
1.249     raeburn  7091:                 countfail = check_types(num,countfail,needaction)
                   7092:             }
                   7093:         }
                   7094:     }
                   7095:     if (countfail > 0) {
                   7096:         var msg = "$alerts{'acto'}\\n";
                   7097:         var loopend = needaction.length -1;
                   7098:         if (loopend > 0) {
                   7099:             for (var m=0; m<loopend; m++) {
                   7100:                 msg += needaction[m]+", ";
                   7101:             }
                   7102:         }
                   7103:         msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
                   7104:         alert(msg);
                   7105:         return; 
                   7106:     }
                   7107:     setSections(form);
                   7108: }
                   7109: 
                   7110: function check_types(num,countfail,needaction) {
1.441     raeburn  7111:     var boxname = 'selfenroll_types_'+num;
                   7112:     var typeidx = getIndexByName(boxname);
1.249     raeburn  7113:     var count = 0;
                   7114:     if (typeidx != -1) {
1.441     raeburn  7115:         if (document.$formname.elements[boxname].length) {
                   7116:             for (var k=0; k<document.$formname.elements[boxname].length; k++) {
                   7117:                 if (document.$formname.elements[boxname][k].checked) {
1.249     raeburn  7118:                     count ++;
                   7119:                 }
                   7120:             }
                   7121:         } else {
                   7122:             if (document.$formname.elements[typeidx].checked) {
                   7123:                 count ++;
                   7124:             }
                   7125:         }
                   7126:         if (count == 0) {
                   7127:             var domidx = getIndexByName('selfenroll_dom_'+num);
                   7128:             if (domidx != -1) {
                   7129:                 var domname = document.$formname.elements[domidx].value;
                   7130:                 needaction[countfail] = domname;
                   7131:                 countfail ++;
                   7132:             }
                   7133:         }
                   7134:     }
                   7135:     return countfail;
                   7136: }
                   7137: 
1.398     raeburn  7138: function toggleNotify() {
                   7139:     var selfenrollApproval = 0;
                   7140:     if (document.$formname.selfenroll_approval.length) {
                   7141:         for (var i=0; i<document.$formname.selfenroll_approval.length; i++) {
                   7142:             if (document.$formname.selfenroll_approval[i].checked) {
                   7143:                 selfenrollApproval = document.$formname.selfenroll_approval[i].value;
                   7144:                 break;        
                   7145:             }
                   7146:         }
                   7147:     }
                   7148:     if (document.getElementById('notified')) {
                   7149:         if (selfenrollApproval == 0) {
                   7150:             document.getElementById('notified').style.display='none';
                   7151:         } else {
                   7152:             document.getElementById('notified').style.display='block';
                   7153:         }
                   7154:     }
                   7155:     return;
                   7156: }
                   7157: 
1.249     raeburn  7158: function getIndexByName(item) {
                   7159:     for (var i=0;i<document.$formname.elements.length;i++) {
                   7160:         if (document.$formname.elements[i].name == item) {
                   7161:             return i;
                   7162:         }
                   7163:     }
                   7164:     return -1;
                   7165: }
                   7166: ENDSCRIPT
1.256     raeburn  7167: 
1.237     raeburn  7168:     my $output = '<script type="text/javascript">'."\n".
1.301     bisitz   7169:                  '// <![CDATA['."\n".
1.249     raeburn  7170:                  $setsec_js."\n".$selfenroll_js."\n".
1.301     bisitz   7171:                  '// ]]>'."\n".
1.237     raeburn  7172:                  '</script>'."\n".
1.256     raeburn  7173:                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n";
1.469     raeburn  7174:     my $visactions = &cat_visibility($cdom);
1.400     raeburn  7175:     my ($cathash,%cattype);
                   7176:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   7177:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   7178:         $cathash = $domconfig{'coursecategories'}{'cats'};
                   7179:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
                   7180:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
1.406     raeburn  7181:         if ($cattype{'auth'} eq '') {
                   7182:             $cattype{'auth'} = 'std';
                   7183:         }
                   7184:         if ($cattype{'unauth'} eq '') {
                   7185:             $cattype{'unauth'} = 'std';
                   7186:         }
1.400     raeburn  7187:     } else {
                   7188:         $cathash = {};
                   7189:         $cattype{'auth'} = 'std';
                   7190:         $cattype{'unauth'} = 'std';
                   7191:     }
                   7192:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
                   7193:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   7194:                   '<br />'.
                   7195:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   7196:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
                   7197:                   '</ul>');
                   7198:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
                   7199:         if ($currsettings->{'uniquecode'}) {
                   7200:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
                   7201:         } else {
                   7202:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   7203:                   '<br />'.
                   7204:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   7205:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
                   7206:                   '</ul><br />');
                   7207:         }
                   7208:     } else {
                   7209:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
                   7210:         if (ref($visactions) eq 'HASH') {
                   7211:             if ($visible) {
                   7212:                 $output .= '<p class="LC_info">'.$visactions->{'vis'}.'</p>';
                   7213:            } else {
                   7214:                 $output .= '<p class="LC_warning">'.$visactions->{'miss'}.'</p>'
                   7215:                           .$visactions->{'yous'}.
                   7216:                            '<p>'.$visactions->{'gen'}.'<br />'.$visactions->{'coca'};
                   7217:                 if (ref($vismsgs) eq 'ARRAY') {
                   7218:                     $output .= '<br />'.$visactions->{'make'}.'<ul>';
                   7219:                     foreach my $item (@{$vismsgs}) {
                   7220:                         $output .= '<li>'.$visactions->{$item}.'</li>';
                   7221:                     }
                   7222:                     $output .= '</ul>';
1.256     raeburn  7223:                 }
1.400     raeburn  7224:                 $output .= '</p>';
1.256     raeburn  7225:             }
                   7226:         }
                   7227:     }
1.398     raeburn  7228:     my $actionhref = '/adm/createuser';
                   7229:     if ($context eq 'domain') {
                   7230:         $actionhref = '/adm/modifycourse';
                   7231:     }
1.400     raeburn  7232: 
                   7233:     my %noedit;
                   7234:     unless ($context eq 'domain') {
                   7235:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
                   7236:     }
1.398     raeburn  7237:     $output .= '<form name="'.$formname.'" method="post" action="'.$actionhref.'">'."\n".
1.256     raeburn  7238:                &Apache::lonhtmlcommon::start_pick_box();
1.237     raeburn  7239:     if (ref($row) eq 'ARRAY') {
                   7240:         foreach my $item (@{$row}) {
                   7241:             my $title = $item; 
                   7242:             if (ref($lt) eq 'HASH') {
                   7243:                 $title = $lt->{$item};
                   7244:             }
1.297     bisitz   7245:             $output .= &Apache::lonhtmlcommon::row_title($title);
1.237     raeburn  7246:             if ($item eq 'types') {
1.398     raeburn  7247:                 my $curr_types;
                   7248:                 if (ref($currsettings) eq 'HASH') {
                   7249:                     $curr_types = $currsettings->{'selfenroll_types'};
                   7250:                 }
1.400     raeburn  7251:                 if ($noedit{$item}) {
                   7252:                     if ($curr_types eq '*') {
                   7253:                         $output .= &mt('Any user in any domain');   
                   7254:                     } else {
                   7255:                         my @entries = split(/;/,$curr_types);
                   7256:                         if (@entries > 0) {
                   7257:                             $output .= '<ul>'; 
                   7258:                             foreach my $entry (@entries) {
                   7259:                                 my ($currdom,$typestr) = split(/:/,$entry);
                   7260:                                 next if ($typestr eq '');
                   7261:                                 my $domdesc = &Apache::lonnet::domain($currdom);
                   7262:                                 my @currinsttypes = split(',',$typestr);
                   7263:                                 my ($othertitle,$usertypes,$types) = 
                   7264:                                     &Apache::loncommon::sorted_inst_types($currdom);
                   7265:                                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   7266:                                     $usertypes->{'any'} = &mt('any user'); 
                   7267:                                     if (keys(%{$usertypes}) > 0) {
                   7268:                                         $usertypes->{'other'} = &mt('other users');
                   7269:                                     }
                   7270:                                     my @longinsttypes = map { $usertypes->{$_}; } @currinsttypes;
                   7271:                                     $output .= '<li>'.$domdesc.':'.join(', ',@longinsttypes).'</li>';
                   7272:                                  }
                   7273:                             }
                   7274:                             $output .= '</ul>';
                   7275:                         } else {
                   7276:                             $output .= &mt('None');
                   7277:                         }
                   7278:                     }
                   7279:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   7280:                     next;
                   7281:                 }
1.241     raeburn  7282:                 my $showdomdesc = 1;
                   7283:                 my $includeempty = 1;
                   7284:                 my $num = 0;
                   7285:                 $output .= &Apache::loncommon::start_data_table().
                   7286:                            &Apache::loncommon::start_data_table_row()
                   7287:                            .'<td colspan="2"><span class="LC_nobreak"><label>'
                   7288:                            .&mt('Any user in any domain:')
                   7289:                            .'&nbsp;<input type="radio" name="selfenroll_all" value="1" ';
                   7290:                 if ($curr_types eq '*') {
                   7291:                     $output .= ' checked="checked" '; 
                   7292:                 }
1.249     raeburn  7293:                 $output .= 'onchange="javascript:update_types('.
1.418     raeburn  7294:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('Yes').'</label>'.
1.249     raeburn  7295:                            '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
1.241     raeburn  7296:                 if ($curr_types ne '*') {
                   7297:                     $output .= ' checked="checked" ';
                   7298:                 }
1.249     raeburn  7299:                 $output .= ' onchange="javascript:update_types('.
1.418     raeburn  7300:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('No').'</label></td>'.
1.249     raeburn  7301:                            &Apache::loncommon::end_data_table_row().
                   7302:                            &Apache::loncommon::end_data_table().
                   7303:                            &mt('Or').'<br />'.
                   7304:                            &Apache::loncommon::start_data_table();
1.241     raeburn  7305:                 my %currdoms;
1.249     raeburn  7306:                 if ($curr_types eq '') {
1.241     raeburn  7307:                     $output .= &new_selfenroll_dom_row($cdom,'0');
                   7308:                 } elsif ($curr_types ne '*') {
                   7309:                     my @entries = split(/;/,$curr_types);
                   7310:                     if (@entries > 0) {
                   7311:                         foreach my $entry (@entries) {
                   7312:                             my ($currdom,$typestr) = split(/:/,$entry);
                   7313:                             $currdoms{$currdom} = 1;
                   7314:                             my $domdesc = &Apache::lonnet::domain($currdom);
1.249     raeburn  7315:                             my @currinsttypes = split(',',$typestr);
1.241     raeburn  7316:                             $output .= &Apache::loncommon::start_data_table_row()
                   7317:                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
                   7318:                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
                   7319:                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
                   7320:                                        .'" value="'.$currdom.'" /></span><br />'
                   7321:                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
1.418     raeburn  7322:                                        .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');"'.$disabled.' />'
1.241     raeburn  7323:                                        .&mt('Delete').'</label></span></td>';
1.249     raeburn  7324:                             $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
1.418     raeburn  7325:                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes,$readonly).'</td>'
1.241     raeburn  7326:                                        .&Apache::loncommon::end_data_table_row();
                   7327:                             $num ++;
                   7328:                         }
                   7329:                     }
                   7330:                 }
1.249     raeburn  7331:                 my $add_domtitle = &mt('Users in additional domain:');
1.241     raeburn  7332:                 if ($curr_types eq '*') { 
1.249     raeburn  7333:                     $add_domtitle = &mt('Users in specific domain:');
1.241     raeburn  7334:                 } elsif ($curr_types eq '') {
1.249     raeburn  7335:                     $add_domtitle = &mt('Users in other domain:');
1.241     raeburn  7336:                 }
1.446     raeburn  7337:                 my ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('enroll',$cdom);
1.241     raeburn  7338:                 $output .= &Apache::loncommon::start_data_table_row()
                   7339:                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
                   7340:                            .&Apache::loncommon::select_dom_form('','selfenroll_newdom',
1.446     raeburn  7341:                                                                 $includeempty,$showdomdesc,'',$trusted,$untrusted,$readonly)
1.241     raeburn  7342:                            .'<input type="hidden" name="selfenroll_types_total" value="'.$num.'" />'
                   7343:                            .'</td>'.&Apache::loncommon::end_data_table_row()
                   7344:                            .&Apache::loncommon::end_data_table();
1.237     raeburn  7345:             } elsif ($item eq 'registered') {
                   7346:                 my ($regon,$regoff);
1.398     raeburn  7347:                 my $registered;
                   7348:                 if (ref($currsettings) eq 'HASH') {
                   7349:                     $registered = $currsettings->{'selfenroll_registered'};
                   7350:                 }
1.400     raeburn  7351:                 if ($noedit{$item}) {
                   7352:                     if ($registered) {
                   7353:                         $output .= &mt('Must be registered in course');
                   7354:                     } else {
                   7355:                         $output .= &mt('No requirement');
                   7356:                     }
                   7357:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   7358:                     next;
                   7359:                 }
1.398     raeburn  7360:                 if ($registered) {
1.237     raeburn  7361:                     $regon = ' checked="checked" ';
1.419     raeburn  7362:                     $regoff = '';
1.237     raeburn  7363:                 } else {
1.419     raeburn  7364:                     $regon = '';
1.237     raeburn  7365:                     $regoff = ' checked="checked" ';
                   7366:                 }
                   7367:                 $output .= '<label>'.
1.419     raeburn  7368:                            '<input type="radio" name="selfenroll_registered" value="1"'.$regon.$disabled.' />'.
1.244     bisitz   7369:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
1.419     raeburn  7370:                            '<input type="radio" name="selfenroll_registered" value="0"'.$regoff.$disabled.' />'.
1.244     bisitz   7371:                            &mt('No').'</label>';
1.237     raeburn  7372:             } elsif ($item eq 'enroll_dates') {
1.398     raeburn  7373:                 my ($starttime,$endtime);
                   7374:                 if (ref($currsettings) eq 'HASH') {
                   7375:                     $starttime = $currsettings->{'selfenroll_start_date'};
                   7376:                     $endtime = $currsettings->{'selfenroll_end_date'};
                   7377:                     if ($starttime eq '') {
                   7378:                         $starttime = $currsettings->{'default_enrollment_start_date'};
                   7379:                     }
                   7380:                     if ($endtime eq '') {
                   7381:                         $endtime = $currsettings->{'default_enrollment_end_date'};
                   7382:                     }
1.237     raeburn  7383:                 }
1.400     raeburn  7384:                 if ($noedit{$item}) {
                   7385:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
                   7386:                                                           &Apache::lonlocal::locallocaltime($endtime));
                   7387:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   7388:                     next;
                   7389:                 }
1.237     raeburn  7390:                 my $startform =
                   7391:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
1.418     raeburn  7392:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  7393:                 my $endform =
                   7394:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
1.418     raeburn  7395:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  7396:                 $output .= &selfenroll_date_forms($startform,$endform);
                   7397:             } elsif ($item eq 'access_dates') {
1.398     raeburn  7398:                 my ($starttime,$endtime);
                   7399:                 if (ref($currsettings) eq 'HASH') {
                   7400:                     $starttime = $currsettings->{'selfenroll_start_access'};
                   7401:                     $endtime = $currsettings->{'selfenroll_end_access'};
                   7402:                     if ($starttime eq '') {
                   7403:                         $starttime = $currsettings->{'default_enrollment_start_date'};
                   7404:                     }
                   7405:                     if ($endtime eq '') {
                   7406:                         $endtime = $currsettings->{'default_enrollment_end_date'};
                   7407:                     }
1.237     raeburn  7408:                 }
1.400     raeburn  7409:                 if ($noedit{$item}) {
                   7410:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
                   7411:                                                           &Apache::lonlocal::locallocaltime($endtime));
                   7412:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   7413:                     next;
                   7414:                 }
1.237     raeburn  7415:                 my $startform =
                   7416:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
1.418     raeburn  7417:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  7418:                 my $endform =
                   7419:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
1.418     raeburn  7420:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  7421:                 $output .= &selfenroll_date_forms($startform,$endform);
                   7422:             } elsif ($item eq 'section') {
1.398     raeburn  7423:                 my $currsec;
                   7424:                 if (ref($currsettings) eq 'HASH') {
                   7425:                     $currsec = $currsettings->{'selfenroll_section'};
                   7426:                 }
1.237     raeburn  7427:                 my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   7428:                 my $newsecval;
                   7429:                 if ($currsec ne 'none' && $currsec ne '') {
                   7430:                     if (!defined($sections_count{$currsec})) {
                   7431:                         $newsecval = $currsec;
                   7432:                     }
                   7433:                 }
1.400     raeburn  7434:                 if ($noedit{$item}) {
                   7435:                     if ($currsec ne '') {
                   7436:                         $output .= $currsec;
                   7437:                     } else {
                   7438:                         $output .= &mt('No specific section');
                   7439:                     }
                   7440:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   7441:                     next;
                   7442:                 }
1.237     raeburn  7443:                 my $sections_select = 
1.418     raeburn  7444:                     &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec,$disabled);
1.237     raeburn  7445:                 $output .= '<table class="LC_createuser">'."\n".
                   7446:                            '<tr class="LC_section_row">'."\n".
                   7447:                            '<td align="center">'.&mt('Existing sections')."\n".
                   7448:                            '<br />'.$sections_select.'</td><td align="center">'.
                   7449:                            &mt('New section').'<br />'."\n".
1.418     raeburn  7450:                            '<input type="text" name="newsec" size="15" value="'.$newsecval.'"'.$disabled.' />'."\n".
1.237     raeburn  7451:                            '<input type="hidden" name="sections" value="" />'."\n".
                   7452:                            '</td></tr></table>'."\n";
1.276     raeburn  7453:             } elsif ($item eq 'approval') {
1.398     raeburn  7454:                 my ($currnotified,$currapproval,%appchecked);
                   7455:                 my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
1.430     raeburn  7456:                 if (ref($currsettings) eq 'HASH') {
1.398     raeburn  7457:                     $currnotified = $currsettings->{'selfenroll_notifylist'};
                   7458:                     $currapproval = $currsettings->{'selfenroll_approval'};
                   7459:                 }
                   7460:                 if ($currapproval !~ /^[012]$/) {
                   7461:                     $currapproval = 0;
                   7462:                 }
1.400     raeburn  7463:                 if ($noedit{$item}) {
                   7464:                     $output .=  $selfdescs{'approval'}{$currapproval}.
                   7465:                                 '<br />'.&mt('(Set by Domain Coordinator)');
                   7466:                     next;
                   7467:                 }
1.398     raeburn  7468:                 $appchecked{$currapproval} = ' checked="checked"';
                   7469:                 for my $i (0..2) {
                   7470:                     $output .= '<label>'.
                   7471:                                '<input type="radio" name="selfenroll_approval" value="'.$i.'"'.
1.418     raeburn  7472:                                $appchecked{$i}.' onclick="toggleNotify();"'.$disabled.' />'.
                   7473:                                $selfdescs{'approval'}{$i}.'</label>'.('&nbsp;'x2);
1.276     raeburn  7474:                 }
                   7475:                 my %advhash = &Apache::lonnet::get_course_adv_roles($cid,1);
                   7476:                 my (@ccs,%notified);
1.322     raeburn  7477:                 my $ccrole = 'cc';
                   7478:                 if ($crstype eq 'Community') {
                   7479:                     $ccrole = 'co';
                   7480:                 }
                   7481:                 if ($advhash{$ccrole}) {
                   7482:                     @ccs = split(/,/,$advhash{$ccrole});
1.276     raeburn  7483:                 }
                   7484:                 if ($currnotified) {
                   7485:                     foreach my $current (split(/,/,$currnotified)) {
                   7486:                         $notified{$current} = 1;
                   7487:                         if (!grep(/^\Q$current\E$/,@ccs)) {
                   7488:                             push(@ccs,$current);
                   7489:                         }
                   7490:                     }
                   7491:                 }
                   7492:                 if (@ccs) {
1.398     raeburn  7493:                     my $style;
                   7494:                     unless ($currapproval) {
                   7495:                         $style = ' style="display: none;"'; 
                   7496:                     }
                   7497:                     $output .= '<br /><div id="notified"'.$style.'>'.
                   7498:                                &mt('Personnel to be notified when an enrollment request needs approval, or has been approved:').'&nbsp;'.
                   7499:                                &Apache::loncommon::start_data_table().
1.276     raeburn  7500:                                &Apache::loncommon::start_data_table_row();
                   7501:                     my $count = 0;
                   7502:                     my $numcols = 4;
                   7503:                     foreach my $cc (sort(@ccs)) {
                   7504:                         my $notifyon;
                   7505:                         my ($ccuname,$ccudom) = split(/:/,$cc);
                   7506:                         if ($notified{$cc}) {
                   7507:                             $notifyon = ' checked="checked" ';
                   7508:                         }
                   7509:                         if ($count && !$count%$numcols) {
                   7510:                             $output .= &Apache::loncommon::end_data_table_row().
                   7511:                                        &Apache::loncommon::start_data_table_row()
                   7512:                         }
                   7513:                         $output .= '<td><span class="LC_nobreak"><label>'.
1.418     raeburn  7514:                                    '<input type="checkbox" name="selfenroll_notify"'.$notifyon.' value="'.$cc.'"'.$disabled.' />'.
1.276     raeburn  7515:                                    &Apache::loncommon::plainname($ccuname,$ccudom).
                   7516:                                    '</label></span></td>';
1.343     raeburn  7517:                         $count ++;
1.276     raeburn  7518:                     }
                   7519:                     my $rem = $count%$numcols;
                   7520:                     if ($rem) {
                   7521:                         my $emptycols = $numcols - $rem;
                   7522:                         for (my $i=0; $i<$emptycols; $i++) { 
                   7523:                             $output .= '<td>&nbsp;</td>';
                   7524:                         }
                   7525:                     }
                   7526:                     $output .= &Apache::loncommon::end_data_table_row().
1.398     raeburn  7527:                                &Apache::loncommon::end_data_table().
                   7528:                                '</div>';
1.276     raeburn  7529:                 }
                   7530:             } elsif ($item eq 'limit') {
1.398     raeburn  7531:                 my ($crslimit,$selflimit,$nolimit,$currlim,$currcap);
                   7532:                 if (ref($currsettings) eq 'HASH') {
                   7533:                     $currlim = $currsettings->{'selfenroll_limit'};
                   7534:                     $currcap = $currsettings->{'selfenroll_cap'};
                   7535:                 }
1.400     raeburn  7536:                 if ($noedit{$item}) {
                   7537:                     if (($currlim eq 'allstudents') || ($currlim eq 'selfenrolled')) {
                   7538:                         if ($currlim eq 'allstudents') {
                   7539:                             $output .= &mt('Limit by total students');
                   7540:                         } elsif ($currlim eq 'selfenrolled') {
                   7541:                             $output .= &mt('Limit by total self-enrolled students');
                   7542:                         }
                   7543:                         $output .= ' '.&mt('Maximum: [_1]',$currcap).
                   7544:                                    '<br />'.&mt('(Set by Domain Coordinator)');
                   7545:                     } else {
                   7546:                         $output .= &mt('No limit').'<br />'.&mt('(Set by Domain Coordinator)');
                   7547:                     }
                   7548:                     next;
                   7549:                 }
1.276     raeburn  7550:                 if ($currlim eq 'allstudents') {
                   7551:                     $crslimit = ' checked="checked" ';
                   7552:                     $selflimit = ' ';
                   7553:                     $nolimit = ' ';
                   7554:                 } elsif ($currlim eq 'selfenrolled') {
                   7555:                     $crslimit = ' ';
                   7556:                     $selflimit = ' checked="checked" ';
                   7557:                     $nolimit = ' '; 
                   7558:                 } else {
                   7559:                     $crslimit = ' ';
                   7560:                     $selflimit = ' ';
1.398     raeburn  7561:                     $nolimit = ' checked="checked" ';
1.276     raeburn  7562:                 }
                   7563:                 $output .= '<table><tr><td><label>'.
1.418     raeburn  7564:                            '<input type="radio" name="selfenroll_limit" value="none"'.$nolimit.$disabled.'/>'.
1.276     raeburn  7565:                            &mt('No limit').'</label></td><td><label>'.
1.418     raeburn  7566:                            '<input type="radio" name="selfenroll_limit" value="allstudents"'.$crslimit.$disabled.'/>'.
1.276     raeburn  7567:                            &mt('Limit by total students').'</label></td><td><label>'.
1.418     raeburn  7568:                            '<input type="radio" name="selfenroll_limit" value="selfenrolled"'.$selflimit.$disabled.'/>'.
1.276     raeburn  7569:                            &mt('Limit by total self-enrolled students').
                   7570:                            '</td></tr><tr>'.
                   7571:                            '<td>&nbsp;</td><td colspan="2"><span class="LC_nobreak">'.
                   7572:                            ('&nbsp;'x3).&mt('Maximum number allowed: ').
1.418     raeburn  7573:                            '<input type="text" name="selfenroll_cap" size = "5" value="'.$currcap.'"'.$disabled.' /></td></tr></table>';
1.237     raeburn  7574:             }
                   7575:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   7576:         }
                   7577:     }
1.418     raeburn  7578:     $output .= &Apache::lonhtmlcommon::end_pick_box().'<br />';
                   7579:     unless ($readonly) {
                   7580:         $output .= '<input type="button" name="selfenrollconf" value="'
                   7581:                    .&mt('Save').'" onclick="validate_types(this.form);" />';
                   7582:     }
                   7583:     $output .= '<input type="hidden" name="action" value="selfenroll" />'
                   7584:               .'<input type="hidden" name="state" value="done" />'."\n"
                   7585:               .$additional.'</form>';
1.237     raeburn  7586:     $r->print($output);
                   7587:     return;
                   7588: }
                   7589: 
1.400     raeburn  7590: sub get_noedit_fields {
                   7591:     my ($cdom,$cnum,$crstype,$row) = @_;
                   7592:     my %noedit;
                   7593:     if (ref($row) eq 'ARRAY') {
                   7594:         my %settings = &Apache::lonnet::get('environment',['internal.coursecode','internal.textbook',
                   7595:                                                            'internal.selfenrollmgrdc',
                   7596:                                                            'internal.selfenrollmgrcc'],$cdom,$cnum);
                   7597:         my $type = &Apache::lonuserutils::get_extended_type($cdom,$cnum,$crstype,\%settings);
                   7598:         my (%specific_managebydc,%specific_managebycc,%default_managebydc);
                   7599:         map { $specific_managebydc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrdc'}));
                   7600:         map { $specific_managebycc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrcc'}));
                   7601:         my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
                   7602:         map { $default_managebydc{$_} = 1; } (split(/,/,$domdefaults{$type.'selfenrolladmdc'}));
                   7603: 
                   7604:         foreach my $item (@{$row}) {
                   7605:             next if ($specific_managebycc{$item});
                   7606:             if (($specific_managebydc{$item}) || ($default_managebydc{$item})) {
                   7607:                 $noedit{$item} = 1;
                   7608:             }
                   7609:         }
                   7610:     }
                   7611:     return %noedit;
1.470     raeburn  7612: }
1.400     raeburn  7613: 
                   7614: sub visible_in_stdcat {
                   7615:     my ($cdom,$cnum,$domconf) = @_;
                   7616:     my ($cathash,%settable,@vismsgs,$cansetvis,$visible);
                   7617:     unless (ref($domconf) eq 'HASH') {
                   7618:         return ($visible,$cansetvis,\@vismsgs);
                   7619:     }
                   7620:     if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   7621:         if ($domconf->{'coursecategories'}{'togglecats'} eq 'crs') {
1.256     raeburn  7622:             $settable{'togglecats'} = 1;
                   7623:         }
1.400     raeburn  7624:         if ($domconf->{'coursecategories'}{'categorize'} eq 'crs') {
1.256     raeburn  7625:             $settable{'categorize'} = 1;
                   7626:         }
1.400     raeburn  7627:         $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  7628:     }
1.260     raeburn  7629:     if ($settable{'togglecats'} && $settable{'categorize'}) {
1.256     raeburn  7630:         $cansetvis = &mt('You are able to both assign a course category and choose to exclude this course from the catalog.');   
                   7631:     } elsif ($settable{'togglecats'}) {
                   7632:         $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  7633:     } elsif ($settable{'categorize'}) {
1.256     raeburn  7634:         $cansetvis = &mt('You may assign a course category, but only a Domain Coordinator may choose to exclude this course from the catalog.');  
                   7635:     } else {
                   7636:         $cansetvis = &mt('Only a Domain Coordinator may assign a course category or choose to exclude this course from the catalog.'); 
                   7637:     }
                   7638:      
                   7639:     my %currsettings =
                   7640:         &Apache::lonnet::get('environment',['hidefromcat','categories','internal.coursecode'],
                   7641:                              $cdom,$cnum);
1.400     raeburn  7642:     $visible = 0;
1.256     raeburn  7643:     if ($currsettings{'internal.coursecode'} ne '') {
1.400     raeburn  7644:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   7645:             $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  7646:             if (ref($cathash) eq 'HASH') {
                   7647:                 if ($cathash->{'instcode::0'} eq '') {
                   7648:                     push(@vismsgs,'dc_addinst'); 
                   7649:                 } else {
                   7650:                     $visible = 1;
                   7651:                 }
                   7652:             } else {
                   7653:                 $visible = 1;
                   7654:             }
                   7655:         } else {
                   7656:             $visible = 1;
                   7657:         }
                   7658:     } else {
                   7659:         if (ref($cathash) eq 'HASH') {
                   7660:             if ($cathash->{'instcode::0'} ne '') {
                   7661:                 push(@vismsgs,'dc_instcode');
                   7662:             }
                   7663:         } else {
                   7664:             push(@vismsgs,'dc_instcode');
                   7665:         }
                   7666:     }
                   7667:     if ($currsettings{'categories'} ne '') {
                   7668:         my $cathash;
1.400     raeburn  7669:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   7670:             $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  7671:             if (ref($cathash) eq 'HASH') {
                   7672:                 if (keys(%{$cathash}) == 0) {
                   7673:                     push(@vismsgs,'dc_catalog');
                   7674:                 } elsif ((keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} ne '')) {
                   7675:                     push(@vismsgs,'dc_categories');
                   7676:                 } else {
                   7677:                     my @currcategories = split('&',$currsettings{'categories'});
                   7678:                     my $matched = 0;
                   7679:                     foreach my $cat (@currcategories) {
                   7680:                         if ($cathash->{$cat} ne '') {
                   7681:                             $visible = 1;
                   7682:                             $matched = 1;
                   7683:                             last;
                   7684:                         }
                   7685:                     }
                   7686:                     if (!$matched) {
1.260     raeburn  7687:                         if ($settable{'categorize'}) { 
1.256     raeburn  7688:                             push(@vismsgs,'chgcat');
                   7689:                         } else {
                   7690:                             push(@vismsgs,'dc_chgcat');
                   7691:                         }
                   7692:                     }
                   7693:                 }
                   7694:             }
                   7695:         }
                   7696:     } else {
                   7697:         if (ref($cathash) eq 'HASH') {
                   7698:             if ((keys(%{$cathash}) > 1) || 
                   7699:                 (keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} eq '')) {
1.260     raeburn  7700:                 if ($settable{'categorize'}) {
1.256     raeburn  7701:                     push(@vismsgs,'addcat');
                   7702:                 } else {
                   7703:                     push(@vismsgs,'dc_addcat');
                   7704:                 }
                   7705:             }
                   7706:         }
                   7707:     }
                   7708:     if ($currsettings{'hidefromcat'} eq 'yes') {
                   7709:         $visible = 0;
                   7710:         if ($settable{'togglecats'}) {
                   7711:             unshift(@vismsgs,'unhide');
                   7712:         } else {
                   7713:             unshift(@vismsgs,'dc_unhide')
                   7714:         }
                   7715:     }
1.400     raeburn  7716:     return ($visible,$cansetvis,\@vismsgs);
                   7717: }
                   7718: 
                   7719: sub cat_visibility {
1.469     raeburn  7720:     my ($cdom) = @_;
1.400     raeburn  7721:     my %visactions = &Apache::lonlocal::texthash(
                   7722:                    vis => 'This course/community currently appears in the Course/Community Catalog for this domain.',
                   7723:                    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.',
                   7724:                    miss => 'This course/community does not currently appear in the Course/Community Catalog for this domain.',
                   7725:                    none => 'Display of a course catalog is disabled for this domain.',
                   7726:                    yous => 'You should remedy this if you plan to allow self-enrollment, otherwise students will have difficulty finding this course.',
                   7727:                    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.',
                   7728:                    make => 'Make any changes to self-enrollment settings below, click "Save", then take action to include the course in the Catalog:',
                   7729:                    take => 'Take the following action to ensure the course appears in the Catalog:',
                   7730:                    dc_chgconf => 'Ask a domain coordinator to change the Catalog type for this domain.',
                   7731:                    dc_setcode => 'Ask a domain coordinator to assign a six character code to the course',
                   7732:                    dc_unhide  => 'Ask a domain coordinator to change the "Exclude from course catalog" setting.',
1.469     raeburn  7733:                    dc_addinst => 'Ask a domain coordinator to enable catalog display of "Official courses (with institutional codes)".',
1.400     raeburn  7734:                    dc_instcode => 'Ask a domain coordinator to assign an institutional code (if this is an official course).',
                   7735:                    dc_catalog  => 'Ask a domain coordinator to enable or create at least one course category in the domain.',
                   7736:                    dc_categories => 'Ask a domain coordinator to create a hierarchy of categories and sub categories for courses in the domain.',
                   7737:                    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',
                   7738:                    dc_addcat => 'Ask a domain coordinator to assign a category to the course.',
                   7739:     );
1.469     raeburn  7740:     if ($env{'request.role'} eq "dc./$cdom/") {
                   7741:         $visactions{'dc_chgconf'} = &mt('Use: "Main menu" [_1] "Set domain configuration" [_1] "Cataloging of courses/communities" to change the Catalog type for this domain.','&raquo;');
                   7742:         $visactions{'dc_setcode'} = &mt('Use: "Main menu" [_1] "Set domain configuration" [_1] "Cataloging of courses/communities" to assign a six character code to the course.','&raquo;');
                   7743:         $visactions{'dc_unhide'} = &mt('Use: "Main menu" [_1] "Set domain configuration" [_1] "Cataloging of courses/communities" to change the "Exclude from course catalog" setting.','&raquo;');
                   7744:         $visactions{'dc_addinst'} = &mt('Use: "Main menu" [_1] "Set domain configuration" [_1] "Cataloging of courses/communities" to enable catalog display of "Official courses (with institutional codes)".','&raquo;');
                   7745:         $visactions{'dc_instcode'} = &mt('Use: "Main menu" [_1] "View or modify a course or community" [_1] "View/Modify course owner, institutional code ... " to assign an institutional code (if this is an official course).','&raquo;');
                   7746:         $visactions{'dc_catalog'} = &mt('Use: "Main menu" [_1] "Set domain configuration" [_1] "Cataloging of courses/communities" to enable or create at least one course category in the domain.','&raquo;');
                   7747:         $visactions{'dc_categories'} = &mt('Use: "Main menu" [_1] "Set domain configuration" [_1] "Cataloging of courses/communities" to create a hierarchy of categories and sub categories for courses in the domain.','&raquo;');
                   7748:         $visactions{'dc_chgcat'} = &mt('Use: "Main menu" [_1] "View or modify a course or community" [_1] "View/Modify catalog settings for course" to change the category assigned to the course, as the one currently assigned is no longer used in the domain.','&raquo;');
                   7749:         $visactions{'dc_addcat'} = &mt('Use: "Main menu" [_1] "View or modify a course or community" [_1] "View/Modify catalog settings for course" to assign a category to the course.','&raquo;');
                   7750:     }
1.400     raeburn  7751:     $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>"');
                   7752:     $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>"');
                   7753:     $visactions{'addcat'} = &mt('Use [_1]Categorize course[_2] to assign a category to the course.','"<a href="/adm/courseprefs?phase=display&actions=courseinfo">','</a>"');
                   7754:     return \%visactions;
1.256     raeburn  7755: }
                   7756: 
1.241     raeburn  7757: sub new_selfenroll_dom_row {
                   7758:     my ($newdom,$num) = @_;
                   7759:     my $domdesc = &Apache::lonnet::domain($newdom);
                   7760:     my $output;
                   7761:     if ($domdesc ne '') {
                   7762:         $output .= &Apache::loncommon::start_data_table_row()
                   7763:                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
                   7764:                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
1.249     raeburn  7765:                    .'" value="'.$newdom.'" /></span><br />'
                   7766:                    .'<span class="LC_nobreak"><label><input type="checkbox" '
                   7767:                    .'name="selfenroll_activate" value="'.$num.'" '
                   7768:                    .'onchange="javascript:update_types('
                   7769:                    ."'selfenroll_activate','$num'".');" />'
                   7770:                    .&mt('Activate').'</label></span></td>';
1.241     raeburn  7771:         my @currinsttypes;
                   7772:         $output .= '<td>'.&mt('User types:').'<br />'
                   7773:                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
                   7774:                    .&Apache::loncommon::end_data_table_row();
                   7775:     }
                   7776:     return $output;
                   7777: }
                   7778: 
                   7779: sub selfenroll_inst_types {
1.418     raeburn  7780:     my ($num,$currdom,$currinsttypes,$readonly) = @_;
1.241     raeburn  7781:     my $output;
                   7782:     my $numinrow = 4;
                   7783:     my $count = 0;
                   7784:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($currdom);
1.247     raeburn  7785:     my $othervalue = 'any';
1.418     raeburn  7786:     my $disabled;
                   7787:     if ($readonly) {
                   7788:         $disabled = ' disabled="disabled"';
                   7789:     }
1.241     raeburn  7790:     if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
1.251     raeburn  7791:         if (keys(%{$usertypes}) > 0) {
1.247     raeburn  7792:             $othervalue = 'other';
                   7793:         }
1.241     raeburn  7794:         $output .= '<table><tr>';
                   7795:         foreach my $type (@{$types}) {
                   7796:             if (($count > 0) && ($count%$numinrow == 0)) {
                   7797:                 $output .= '</tr><tr>';
                   7798:             }
                   7799:             if (defined($usertypes->{$type})) {
1.257     raeburn  7800:                 my $esc_type = &escape($type);
1.241     raeburn  7801:                 $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.
1.257     raeburn  7802:                            $esc_type.'" ';
1.241     raeburn  7803:                 if (ref($currinsttypes) eq 'ARRAY') {
                   7804:                     if (@{$currinsttypes} > 0) {
1.249     raeburn  7805:                         if (grep(/^any$/,@{$currinsttypes})) {
                   7806:                             $output .= 'checked="checked"';
1.257     raeburn  7807:                         } elsif (grep(/^\Q$esc_type\E$/,@{$currinsttypes})) {
1.241     raeburn  7808:                             $output .= 'checked="checked"';
                   7809:                         }
1.249     raeburn  7810:                     } else {
                   7811:                         $output .= 'checked="checked"';
1.241     raeburn  7812:                     }
                   7813:                 }
1.418     raeburn  7814:                 $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$usertypes->{$type}.'</label></span></td>';
1.241     raeburn  7815:             }
                   7816:             $count ++;
                   7817:         }
                   7818:         if (($count > 0) && ($count%$numinrow == 0)) {
                   7819:             $output .= '</tr><tr>';
                   7820:         }
1.249     raeburn  7821:         $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
1.241     raeburn  7822:         if (ref($currinsttypes) eq 'ARRAY') {
                   7823:             if (@{$currinsttypes} > 0) {
1.249     raeburn  7824:                 if (grep(/^any$/,@{$currinsttypes})) { 
                   7825:                     $output .= ' checked="checked"';
                   7826:                 } elsif ($othervalue eq 'other') {
                   7827:                     if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
                   7828:                         $output .= ' checked="checked"';
                   7829:                     }
1.241     raeburn  7830:                 }
1.249     raeburn  7831:             } else {
                   7832:                 $output .= ' checked="checked"';
1.241     raeburn  7833:             }
1.249     raeburn  7834:         } else {
                   7835:             $output .= ' checked="checked"';
1.241     raeburn  7836:         }
1.418     raeburn  7837:         $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$othertitle.'</label></span></td></tr></table>';
1.241     raeburn  7838:     }
                   7839:     return $output;
                   7840: }
                   7841: 
1.237     raeburn  7842: sub selfenroll_date_forms {
                   7843:     my ($startform,$endform) = @_;
                   7844:     my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
1.244     bisitz   7845:                   &Apache::lonhtmlcommon::row_title(&mt('Start date'),
1.237     raeburn  7846:                                                     'LC_oddrow_value')."\n".
                   7847:                   $startform."\n".
                   7848:                   &Apache::lonhtmlcommon::row_closure(1).
1.244     bisitz   7849:                   &Apache::lonhtmlcommon::row_title(&mt('End date'),
1.237     raeburn  7850:                                                    'LC_oddrow_value')."\n".
                   7851:                   $endform."\n".
                   7852:                   &Apache::lonhtmlcommon::row_closure(1).
                   7853:                   &Apache::lonhtmlcommon::end_pick_box();
                   7854:     return $output;
                   7855: }
                   7856: 
1.239     raeburn  7857: sub print_userchangelogs_display {
1.415     raeburn  7858:     my ($r,$context,$permission,$brcrum) = @_;
1.363     raeburn  7859:     my $formname = 'rolelog';
1.418     raeburn  7860:     my ($username,$domain,$crstype,$viewablesec,%roleslog);
1.363     raeburn  7861:     if ($context eq 'domain') {
                   7862:         $domain = $env{'request.role.domain'};
                   7863:         %roleslog=&Apache::lonnet::dump_dom('nohist_rolelog',$domain);
                   7864:     } else {
                   7865:         if ($context eq 'course') { 
                   7866:             $domain = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7867:             $username = $env{'course.'.$env{'request.course.id'}.'.num'};
                   7868:             $crstype = &Apache::loncommon::course_type();
1.418     raeburn  7869:             $viewablesec = &Apache::lonuserutils::viewable_section($permission);
1.363     raeburn  7870:             my %saveable_parameters = ('show' => 'scalar',);
                   7871:             &Apache::loncommon::store_course_settings('roles_log',
                   7872:                                                       \%saveable_parameters);
                   7873:             &Apache::loncommon::restore_course_settings('roles_log',
                   7874:                                                         \%saveable_parameters);
                   7875:         } elsif ($context eq 'author') {
1.470     raeburn  7876:             $domain = $env{'user.domain'};
1.363     raeburn  7877:             if ($env{'request.role'} =~ m{^au\./\Q$domain\E/$}) {
                   7878:                 $username = $env{'user.name'};
1.470     raeburn  7879:             } elsif ($env{'request.role'} =~ m{^ca\./($match_domain)/($match_username)$}) {
                   7880:                 ($domain,$username) = ($1,$2);
1.363     raeburn  7881:             } else {
                   7882:                 undef($domain);
                   7883:             }
                   7884:         }
                   7885:         if ($domain ne '' && $username ne '') { 
                   7886:             %roleslog=&Apache::lonnet::dump('nohist_rolelog',$domain,$username);
                   7887:         }
                   7888:     }
1.239     raeburn  7889:     if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
                   7890: 
1.415     raeburn  7891:     my $helpitem;
                   7892:     if ($context eq 'course') {
                   7893:         $helpitem = 'Course_User_Logs';
1.439     raeburn  7894:     } elsif ($context eq 'domain') {
                   7895:         $helpitem = 'Domain_Role_Logs';
                   7896:     } elsif ($context eq 'author') {
                   7897:         $helpitem = 'Author_User_Logs';
1.415     raeburn  7898:     }
                   7899:     push (@{$brcrum},
                   7900:              {href => '/adm/createuser?action=changelogs',
                   7901:               text => 'User Management Logs',
                   7902:               help => $helpitem});
                   7903:     my $bread_crumbs_component = 'User Changes';
                   7904:     my $args = { bread_crumbs           => $brcrum,
                   7905:                  bread_crumbs_component => $bread_crumbs_component};
                   7906: 
                   7907:     # Create navigation javascript
                   7908:     my $jsnav = &userlogdisplay_js($formname);
                   7909: 
                   7910:     my $jscript = (<<ENDSCRIPT);
                   7911: <script type="text/javascript">
                   7912: // <![CDATA[
                   7913: $jsnav
                   7914: // ]]>
                   7915: </script>
                   7916: ENDSCRIPT
                   7917: 
                   7918:     # print page header
                   7919:     $r->print(&header($jscript,$args));
                   7920: 
1.239     raeburn  7921:     # set defaults
                   7922:     my $now = time();
                   7923:     my $defstart = $now - (7*24*3600); #7 days ago 
                   7924:     my %defaults = (
                   7925:                      page               => '1',
                   7926:                      show               => '10',
                   7927:                      role               => 'any',
                   7928:                      chgcontext         => 'any',
                   7929:                      rolelog_start_date => $defstart,
                   7930:                      rolelog_end_date   => $now,
1.468     raeburn  7931:                      approvals          => 'any',
1.239     raeburn  7932:                    );
                   7933:     my $more_records = 0;
                   7934: 
                   7935:     # set current
                   7936:     my %curr;
1.468     raeburn  7937:     foreach my $item ('show','page','role','chgcontext','approvals') {
1.239     raeburn  7938:         $curr{$item} = $env{'form.'.$item};
                   7939:     }
                   7940:     my ($startdate,$enddate) = 
                   7941:         &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
                   7942:     $curr{'rolelog_start_date'} = $startdate;
                   7943:     $curr{'rolelog_end_date'} = $enddate;
                   7944:     foreach my $key (keys(%defaults)) {
                   7945:         if ($curr{$key} eq '') {
                   7946:             $curr{$key} = $defaults{$key};
                   7947:         }
                   7948:     }
1.248     raeburn  7949:     my (%whodunit,%changed,$version);
                   7950:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
1.239     raeburn  7951:     my ($minshown,$maxshown);
1.255     raeburn  7952:     $minshown = 1;
1.239     raeburn  7953:     my $count = 0;
1.415     raeburn  7954:     if ($curr{'show'} =~ /\D/) {
                   7955:         $curr{'page'} = 1;
                   7956:     } else {
1.239     raeburn  7957:         $maxshown = $curr{'page'} * $curr{'show'};
                   7958:         if ($curr{'page'} > 1) {
                   7959:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   7960:         }
                   7961:     }
1.301     bisitz   7962: 
1.327     raeburn  7963:     # Form Header
                   7964:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
1.363     raeburn  7965:               &role_display_filter($context,$formname,$domain,$username,\%curr,
                   7966:                                    $version,$crstype));
1.327     raeburn  7967: 
                   7968:     my $showntableheader = 0;
                   7969: 
                   7970:     # Table Header
                   7971:     my $tableheader = 
                   7972:         &Apache::loncommon::start_data_table_header_row()
                   7973:        .'<th>&nbsp;</th>'
                   7974:        .'<th>'.&mt('When').'</th>'
                   7975:        .'<th>'.&mt('Who made the change').'</th>'
                   7976:        .'<th>'.&mt('Changed User').'</th>'
1.363     raeburn  7977:        .'<th>'.&mt('Role').'</th>';
                   7978: 
                   7979:     if ($context eq 'course') {
                   7980:         $tableheader .= '<th>'.&mt('Section').'</th>';
                   7981:     }
                   7982:     $tableheader .=
                   7983:         '<th>'.&mt('Context').'</th>'
1.327     raeburn  7984:        .'<th>'.&mt('Start').'</th>'
                   7985:        .'<th>'.&mt('End').'</th>'
                   7986:        .&Apache::loncommon::end_data_table_header_row();
                   7987: 
                   7988:     # Display user change log data
1.239     raeburn  7989:     foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
                   7990:         next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
                   7991:                  ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
1.415     raeburn  7992:         if ($curr{'show'} !~ /\D/) {
1.239     raeburn  7993:             if ($count >= $curr{'page'} * $curr{'show'}) {
                   7994:                 $more_records = 1;
                   7995:                 last;
                   7996:             }
                   7997:         }
                   7998:         if ($curr{'role'} ne 'any') {
                   7999:             next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
                   8000:         }
                   8001:         if ($curr{'chgcontext'} ne 'any') {
                   8002:             if ($curr{'chgcontext'} eq 'selfenroll') {
                   8003:                 next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
                   8004:             } else {
                   8005:                 next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
                   8006:             }
                   8007:         }
1.418     raeburn  8008:         if (($context eq 'course') && ($viewablesec ne '')) {
1.430     raeburn  8009:             next if ($roleslog{$id}{'logentry'}{'section'} ne $viewablesec);
1.418     raeburn  8010:         }
1.468     raeburn  8011:         if ($curr{'approvals'} eq 'none') {
                   8012:             next if ($roleslog{$id}{'logentry'}{'approval'});
                   8013:         } elsif ($curr{'approvals'} ne 'any') { 
                   8014:             next if ($roleslog{$id}{'logentry'}{'approval'} ne $curr{'approvals'});
                   8015:         }
1.239     raeburn  8016:         $count ++;
                   8017:         next if ($count < $minshown);
1.327     raeburn  8018:         unless ($showntableheader) {
1.415     raeburn  8019:             $r->print(&Apache::loncommon::start_data_table()
1.327     raeburn  8020:                      .$tableheader);
                   8021:             $r->rflush();
                   8022:             $showntableheader = 1;
                   8023:         }
1.239     raeburn  8024:         if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
                   8025:             $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
                   8026:                 &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
                   8027:         }
                   8028:         if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
                   8029:             $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
                   8030:                 &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
                   8031:         }
                   8032:         my $sec = $roleslog{$id}{'logentry'}{'section'};
                   8033:         if ($sec eq '') {
                   8034:             $sec = &mt('None');
                   8035:         }
                   8036:         my ($rolestart,$roleend);
                   8037:         if ($roleslog{$id}{'delflag'}) {
                   8038:             $rolestart = &mt('deleted');
                   8039:             $roleend = &mt('deleted');
                   8040:         } else {
                   8041:             $rolestart = $roleslog{$id}{'logentry'}{'start'};
                   8042:             $roleend = $roleslog{$id}{'logentry'}{'end'};
                   8043:             if ($rolestart eq '' || $rolestart == 0) {
                   8044:                 $rolestart = &mt('No start date'); 
                   8045:             } else {
                   8046:                 $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
                   8047:             }
                   8048:             if ($roleend eq '' || $roleend == 0) { 
                   8049:                 $roleend = &mt('No end date');
                   8050:             } else {
                   8051:                 $roleend = &Apache::lonlocal::locallocaltime($roleend);
                   8052:             }
                   8053:         }
                   8054:         my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
                   8055:         if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
                   8056:             $chgcontext = 'selfenroll';
                   8057:         }
1.363     raeburn  8058:         my %lt = &rolechg_contexts($context,$crstype);
1.239     raeburn  8059:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
                   8060:             $chgcontext = $lt{$chgcontext};
                   8061:         }
1.468     raeburn  8062:         my ($showreqby,%reqby);
                   8063:         if (($roleslog{$id}{'logentry'}{'approval'}) &&
                   8064:             ($roleslog{$id}{'logentry'}{'requester'})) {
                   8065:             if ($reqby{$roleslog{$id}{'logentry'}{'requester'}} eq '') {
                   8066:                 my ($requname,$requdom) = split(/:/,$roleslog{$id}{'logentry'}{'requester'});
                   8067:                 $reqby{$roleslog{$id}{'logentry'}{'requester'}} =
                   8068:                     &Apache::loncommon::plainname($requname,$requdom);
                   8069:             }
                   8070:             $showreqby = &mt('Requester').': <span class="LC_nobreak">'.$reqby{$roleslog{$id}{'logentry'}{'requester'}}.'</span><br />';
                   8071:             if ($roleslog{$id}{'logentry'}{'approval'} eq 'domain') {
                   8072:                 $showreqby .= &mt('Adjudicator').': <span class="LC_nobreak">'.
                   8073:                               $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.
                   8074:                               '</span>';
                   8075:             } else {
                   8076:                 $showreqby .= '<span class="LC_nobreak">'.&mt('User approved').'</span>';
                   8077:             }
                   8078:         } else {
                   8079:             $showreqby = $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}};
                   8080:         }
1.327     raeburn  8081:         $r->print(
1.301     bisitz   8082:             &Apache::loncommon::start_data_table_row()
                   8083:            .'<td>'.$count.'</td>'
                   8084:            .'<td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td>'
1.468     raeburn  8085:            .'<td>'.$showreqby.'</td>'
1.301     bisitz   8086:            .'<td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td>'
1.363     raeburn  8087:            .'<td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'},$crstype).'</td>');
                   8088:         if ($context eq 'course') { 
                   8089:             $r->print('<td>'.$sec.'</td>');
                   8090:         }
                   8091:         $r->print(
                   8092:             '<td>'.$chgcontext.'</td>'
1.301     bisitz   8093:            .'<td>'.$rolestart.'</td>'
                   8094:            .'<td>'.$roleend.'</td>'
1.327     raeburn  8095:            .&Apache::loncommon::end_data_table_row()."\n");
1.301     bisitz   8096:     }
                   8097: 
1.327     raeburn  8098:     if ($showntableheader) { # Table footer, if content displayed above
1.415     raeburn  8099:         $r->print(&Apache::loncommon::end_data_table().
                   8100:                   &userlogdisplay_navlinks(\%curr,$more_records));
1.327     raeburn  8101:     } else { # No content displayed above
1.301     bisitz   8102:         $r->print('<p class="LC_info">'
                   8103:                  .&mt('There are no records to display.')
                   8104:                  .'</p>'
                   8105:         );
1.239     raeburn  8106:     }
1.301     bisitz   8107: 
1.327     raeburn  8108:     # Form Footer
                   8109:     $r->print( 
                   8110:         '<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   8111:        .'<input type="hidden" name="action" value="changelogs" />'
                   8112:        .'</form>');
                   8113:     return;
                   8114: }
1.301     bisitz   8115: 
1.416     raeburn  8116: sub print_useraccesslogs_display {
                   8117:     my ($r,$uname,$udom,$permission,$brcrum) = @_;
                   8118:     my $formname = 'accesslog';
                   8119:     my $form = 'document.accesslog';
                   8120: 
                   8121: # set breadcrumbs
1.422     raeburn  8122:     my %breadcrumb_text = &singleuser_breadcrumb('','domain',$udom);
1.431     raeburn  8123:     my $prevphasestr;
                   8124:     if ($env{'form.popup'}) {
                   8125:         $brcrum = [];
                   8126:     } else {
                   8127:         push (@{$brcrum},
                   8128:             {href => "javascript:backPage($form)",
                   8129:              text => $breadcrumb_text{'search'}});
                   8130:         my @prevphases;
                   8131:         if ($env{'form.prevphases'}) {
                   8132:             @prevphases = split(/,/,$env{'form.prevphases'});
                   8133:             $prevphasestr = $env{'form.prevphases'};
                   8134:         }
                   8135:         if (($env{'form.phase'} eq 'userpicked') || (grep(/^userpicked$/,@prevphases))) {
                   8136:             push(@{$brcrum},
                   8137:                   {href => "javascript:backPage($form,'get_user_info','select')",
                   8138:                    text => $breadcrumb_text{'userpicked'}});
                   8139:             if ($env{'form.phase'} eq 'userpicked') {
                   8140:                 $prevphasestr = 'userpicked';
                   8141:             }
1.416     raeburn  8142:         }
                   8143:     }
                   8144:     push(@{$brcrum},
                   8145:              {href => '/adm/createuser?action=accesslogs',
                   8146:               text => 'User access logs',
1.424     raeburn  8147:               help => 'Domain_User_Access_Logs'});
1.416     raeburn  8148:     my $bread_crumbs_component = 'User Access Logs';
                   8149:     my $args = { bread_crumbs           => $brcrum,
                   8150:                  bread_crumbs_component => 'User Management'};
1.423     raeburn  8151:     if ($env{'form.popup'}) {
                   8152:         $args->{'no_nav_bar'} = 1;
1.431     raeburn  8153:         $args->{'bread_crumbs_nomenu'} = 1;
1.423     raeburn  8154:     }
1.416     raeburn  8155: 
1.417     raeburn  8156: # set javascript
1.416     raeburn  8157:     my ($jsback,$elements) = &crumb_utilities();
                   8158:     my $jsnav = &userlogdisplay_js($formname);
                   8159: 
                   8160:     my $jscript = (<<ENDSCRIPT);
                   8161: <script type="text/javascript">
                   8162: // <![CDATA[
                   8163: 
                   8164: $jsback
                   8165: $jsnav
                   8166: 
                   8167: // ]]>
                   8168: </script>
                   8169: 
                   8170: ENDSCRIPT
                   8171: 
1.417     raeburn  8172: # print page header
1.416     raeburn  8173:     $r->print(&header($jscript,$args));
                   8174: 
                   8175: # early out unless log data can be displayed.
                   8176:     unless ($permission->{'activity'}) {
                   8177:         $r->print('<p class="LC_warning">'
                   8178:                  .&mt('You do not have rights to display user access logs.')
1.431     raeburn  8179:                  .'</p>');
                   8180:         if ($env{'form.popup'}) {
                   8181:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   8182:         } else {
                   8183:             $r->print(&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   8184:         }
1.416     raeburn  8185:         return;
                   8186:     }
                   8187: 
                   8188:     unless ($udom eq $env{'request.role.domain'}) {
                   8189:         $r->print('<p class="LC_warning">'
                   8190:                  .&mt("User's domain must match role's domain")
                   8191:                  .'</p>'
                   8192:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
1.417     raeburn  8193:         return;
1.416     raeburn  8194:     }
                   8195: 
                   8196:     if (($uname eq '') || ($udom eq '')) {
                   8197:         $r->print('<p class="LC_warning">'
                   8198:                  .&mt('Invalid username or domain')
                   8199:                  .'</p>'
                   8200:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   8201:         return;
                   8202:     }
                   8203: 
1.437     raeburn  8204:     if (&Apache::lonnet::privileged($uname,$udom,
                   8205:                                     [$env{'request.role.domain'}],['dc','su'])) {
                   8206:         unless (&Apache::lonnet::privileged($env{'user.name'},$env{'user.domain'},
                   8207:                                             [$env{'request.role.domain'}],['dc','su'])) {
                   8208:             $r->print('<p class="LC_warning">'
                   8209:                  .&mt('You need to be a privileged user to display user access logs for [_1]',
                   8210:                       &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),
                   8211:                                                          $uname,$udom))
                   8212:                  .'</p>');
                   8213:             if ($env{'form.popup'}) {
                   8214:                 $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   8215:             } else {
                   8216:                 $r->print(&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   8217:             }
                   8218:             return;
                   8219:         }
                   8220:     }
                   8221: 
1.416     raeburn  8222: # set defaults
                   8223:     my $now = time();
                   8224:     my $defstart = $now - (7*24*3600);
                   8225:     my %defaults = (
                   8226:                      page                 => '1',
                   8227:                      show                 => '10',
                   8228:                      activity             => 'any',
                   8229:                      accesslog_start_date => $defstart,
                   8230:                      accesslog_end_date   => $now,
                   8231:                    );
                   8232:     my $more_records = 0;
                   8233: 
                   8234: # set current
                   8235:     my %curr;
                   8236:     foreach my $item ('show','page','activity') {
                   8237:         $curr{$item} = $env{'form.'.$item};
                   8238:     }
                   8239:     my ($startdate,$enddate) =
                   8240:         &Apache::lonuserutils::get_dates_from_form('accesslog_start_date','accesslog_end_date');
                   8241:     $curr{'accesslog_start_date'} = $startdate;
                   8242:     $curr{'accesslog_end_date'} = $enddate;
                   8243:     foreach my $key (keys(%defaults)) {
                   8244:         if ($curr{$key} eq '') {
                   8245:             $curr{$key} = $defaults{$key};
                   8246:         }
                   8247:     }
                   8248:     my ($minshown,$maxshown);
                   8249:     $minshown = 1;
                   8250:     my $count = 0;
                   8251:     if ($curr{'show'} =~ /\D/) {
                   8252:         $curr{'page'} = 1;
                   8253:     } else {
                   8254:         $maxshown = $curr{'page'} * $curr{'show'};
                   8255:         if ($curr{'page'} > 1) {
                   8256:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   8257:         }
                   8258:     }
                   8259: 
                   8260: # form header
                   8261:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
                   8262:               &activity_display_filter($formname,\%curr));
                   8263: 
                   8264:     my $showntableheader = 0;
                   8265:     my ($nav_script,$nav_links);
                   8266: 
                   8267: # table header
1.453     raeburn  8268:     my $heading = '<h3>'.
1.431     raeburn  8269:         &mt('User access logs for: [_1]',
1.453     raeburn  8270:             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom)).'</h3>';
                   8271:     my $tableheader = $heading
1.431     raeburn  8272:        .&Apache::loncommon::start_data_table_header_row()
1.416     raeburn  8273:        .'<th>&nbsp;</th>'
                   8274:        .'<th>'.&mt('When').'</th>'
                   8275:        .'<th>'.&mt('HostID').'</th>'
                   8276:        .'<th>'.&mt('Event').'</th>'
                   8277:        .'<th>'.&mt('Other data').'</th>'
                   8278:        .&Apache::loncommon::end_data_table_header_row();
                   8279: 
                   8280:     my %filters=(
                   8281:         start  => $curr{'accesslog_start_date'},
                   8282:         end    => $curr{'accesslog_end_date'},
                   8283:         action => $curr{'activity'},
                   8284:     );
                   8285: 
                   8286:     my $reply = &Apache::lonnet::userlog_query($uname,$udom,%filters);
                   8287:     unless ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   8288:         my (%courses,%missing);
                   8289:         my @results = split(/\&/,$reply);
                   8290:         foreach my $item (reverse(@results)) {
                   8291:             my ($timestamp,$host,$event) = split(/:/,$item);
                   8292:             next unless ($event =~ /^(Log|Role)/);
                   8293:             if ($curr{'show'} !~ /\D/) {
                   8294:                 if ($count >= $curr{'page'} * $curr{'show'}) {
                   8295:                     $more_records = 1;
                   8296:                     last;
                   8297:                 }
                   8298:             }
                   8299:             $count ++;
                   8300:             next if ($count < $minshown);
                   8301:             unless ($showntableheader) {
                   8302:                 $r->print($nav_script
                   8303:                          .&Apache::loncommon::start_data_table()
                   8304:                          .$tableheader);
                   8305:                 $r->rflush();
                   8306:                 $showntableheader = 1;
                   8307:             }
1.418     raeburn  8308:             my ($shown,$extra);
1.437     raeburn  8309:             my ($event,$data) = split(/\s+/,&unescape($event),2);
1.416     raeburn  8310:             if ($event eq 'Role') {
                   8311:                 my ($rolecode,$extent) = split(/\./,$data,2);
                   8312:                 next if ($extent eq '');
                   8313:                 my ($crstype,$desc,$info);
1.418     raeburn  8314:                 if ($extent =~ m{^/($match_domain)/($match_courseid)(?:/(\w+)|)$}) {
                   8315:                     my ($cdom,$cnum,$sec) = ($1,$2,$3);
1.416     raeburn  8316:                     my $cid = $cdom.'_'.$cnum;
                   8317:                     if (exists($courses{$cid})) {
                   8318:                         $crstype = $courses{$cid}{'type'};
                   8319:                         $desc = $courses{$cid}{'description'};
                   8320:                     } elsif ($missing{$cid}) {
                   8321:                         $crstype = 'Course';
                   8322:                         $desc = 'Course/Community';
                   8323:                     } else {
                   8324:                         my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   8325:                         if (ref($crsinfo{$cdom.'_'.$cnum}) eq 'HASH') {
                   8326:                             $courses{$cid} = $crsinfo{$cid};
                   8327:                             $crstype = $crsinfo{$cid}{'type'};
                   8328:                             $desc = $crsinfo{$cid}{'description'};
                   8329:                         } else {
                   8330:                             $missing{$cid} = 1;
                   8331:                         }
                   8332:                     }
                   8333:                     $extra = &mt($crstype).': <a href="/public/'.$cdom.'/'.$cnum.'/syllabus">'.$desc.'</a>';
1.418     raeburn  8334:                     if ($sec ne '') {
                   8335:                        $extra .= ' ('.&mt('Section: [_1]',$sec).')';
                   8336:                     }
1.416     raeburn  8337:                 } elsif ($extent =~ m{^/($match_domain)/($match_username|$)}) {
                   8338:                     my ($dom,$name) = ($1,$2);
                   8339:                     if ($rolecode eq 'au') {
                   8340:                         $extra = '';
                   8341:                     } elsif ($rolecode =~ /^(ca|aa)$/) {
1.417     raeburn  8342:                         $extra = &mt('Authoring Space: [_1]',$name.':'.$dom);
1.416     raeburn  8343:                     } elsif ($rolecode =~ /^(li|dg|dh|dc|sc)$/) {
                   8344:                         $extra = &mt('Domain: [_1]',$dom);
                   8345:                     }
                   8346:                 }
                   8347:                 my $rolename;
                   8348:                 if ($rolecode =~ m{^cr/($match_domain)/($match_username)/(\w+)}) {
                   8349:                     my $role = $3;
1.417     raeburn  8350:                     my $owner = "($2:$1)";
1.416     raeburn  8351:                     if ($2 eq $1.'-domainconfig') {
                   8352:                         $owner = '(ad hoc)';
1.417     raeburn  8353:                     }
1.416     raeburn  8354:                     $rolename = &mt('Custom role: [_1]',$role.' '.$owner);
                   8355:                 } else {
                   8356:                     $rolename = &Apache::lonnet::plaintext($rolecode,$crstype);
                   8357:                 }
                   8358:                 $shown = &mt('Role selection: [_1]',$rolename);
                   8359:             } else {
                   8360:                 $shown = &mt($event);
1.437     raeburn  8361:                 if ($data =~ /^webdav/) {
                   8362:                     my ($path,$clientip) = split(/\s+/,$data,2);
                   8363:                     $path =~ s/^webdav//;
                   8364:                     if ($clientip ne '') {
                   8365:                         $extra = &mt('Client IP address: [_1]',$clientip);
                   8366:                     }
                   8367:                     if ($path ne '') {
                   8368:                         $shown .= ' '.&mt('(WebDAV access to [_1])',$path);
                   8369:                     }
                   8370:                 } elsif ($data ne '') {
                   8371:                     $extra = &mt('Client IP address: [_1]',$data);
1.416     raeburn  8372:                 }
                   8373:             }
                   8374:             $r->print(
                   8375:             &Apache::loncommon::start_data_table_row()
                   8376:            .'<td>'.$count.'</td>'
                   8377:            .'<td>'.&Apache::lonlocal::locallocaltime($timestamp).'</td>'
                   8378:            .'<td>'.$host.'</td>'
                   8379:            .'<td>'.$shown.'</td>'
                   8380:            .'<td>'.$extra.'</td>'
                   8381:            .&Apache::loncommon::end_data_table_row()."\n");
                   8382:         }
                   8383:     }
                   8384: 
                   8385:     if ($showntableheader) { # Table footer, if content displayed above
                   8386:         $r->print(&Apache::loncommon::end_data_table().
                   8387:                   &userlogdisplay_navlinks(\%curr,$more_records));
                   8388:     } else { # No content displayed above
1.453     raeburn  8389:         $r->print($heading.'<p class="LC_info">'
1.416     raeburn  8390:                  .&mt('There are no records to display.')
                   8391:                  .'</p>');
                   8392:     }
                   8393: 
1.423     raeburn  8394:     if ($env{'form.popup'} == 1) {
                   8395:         $r->print('<input type="hidden" name="popup" value="1" />'."\n");
                   8396:     }
                   8397: 
1.416     raeburn  8398:     # Form Footer
                   8399:     $r->print(
                   8400:         '<input type="hidden" name="currstate" value="" />'
                   8401:        .'<input type="hidden" name="accessuname" value="'.$uname.'" />'
                   8402:        .'<input type="hidden" name="accessudom" value="'.$udom.'" />'
                   8403:        .'<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   8404:        .'<input type="hidden" name="prevphases" value="'.$prevphasestr.'" />'
                   8405:        .'<input type="hidden" name="phase" value="activity" />'
                   8406:        .'<input type="hidden" name="action" value="accesslogs" />'
                   8407:        .'<input type="hidden" name="srchdomain" value="'.$udom.'" />'
                   8408:        .'<input type="hidden" name="srchby" value="'.$env{'form.srchby'}.'" />'
                   8409:        .'<input type="hidden" name="srchtype" value="'.$env{'form.srchtype'}.'" />'
                   8410:        .'<input type="hidden" name="srchterm" value="'.&HTML::Entities::encode($env{'form.srchterm'},'<>"&').'" />'
                   8411:        .'<input type="hidden" name="srchin" value="'.$env{'form.srchin'}.'" />'
                   8412:        .'</form>');
                   8413:     return;
                   8414: }
                   8415: 
                   8416: sub earlyout_accesslog_form {
                   8417:     my ($formname,$prevphasestr,$udom) = @_;
                   8418:     my $srchterm = &HTML::Entities::encode($env{'form.srchterm'},'<>"&');
                   8419:    return <<"END";
                   8420: <form action="/adm/createuser" method="post" name="$formname">
                   8421: <input type="hidden" name="currstate" value="" />
                   8422: <input type="hidden" name="prevphases" value="$prevphasestr" />
                   8423: <input type="hidden" name="phase" value="activity" />
                   8424: <input type="hidden" name="action" value="accesslogs" />
                   8425: <input type="hidden" name="srchdomain" value="$udom" />
                   8426: <input type="hidden" name="srchby" value="$env{'form.srchby'}" />
                   8427: <input type="hidden" name="srchtype" value="$env{'form.srchtype'}" />
                   8428: <input type="hidden" name="srchterm" value="$srchterm" />
                   8429: <input type="hidden" name="srchin" value="$env{'form.srchin'}" />
                   8430: </form>
                   8431: END
                   8432: }
                   8433: 
                   8434: sub activity_display_filter {
                   8435:     my ($formname,$curr) = @_;
                   8436:     my $nolink = 1;
                   8437:     my $output = '<table><tr><td valign="top">'.
                   8438:                  '<span class="LC_nobreak"><b>'.&mt('Actions/page:').'</b></span><br />'.
1.467     raeburn  8439:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},'',undef,
1.416     raeburn  8440:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   8441:                  '</td><td>&nbsp;&nbsp;</td>';
                   8442:     my $startform =
                   8443:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_start_date',
                   8444:                                             $curr->{'accesslog_start_date'},undef,
                   8445:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   8446:     my $endform =
                   8447:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_end_date',
                   8448:                                             $curr->{'accesslog_end_date'},undef,
                   8449:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   8450:     my %lt = &Apache::lonlocal::texthash (
                   8451:                                           activity => 'Activity',
                   8452:                                           Role     => 'Role selection',
                   8453:                                           log      => 'Log-in or Logout',
                   8454:     );
                   8455:     $output .= '<td valign="top"><b>'.&mt('Window during which actions occurred:').'</b><br />'.
                   8456:                '<table><tr><td>'.&mt('After:').
                   8457:                '</td><td>'.$startform.'</td></tr>'.
                   8458:                '<tr><td>'.&mt('Before:').'</td>'.
                   8459:                '<td>'.$endform.'</td></tr></table>'.
                   8460:                '</td>'.
                   8461:                '<td>&nbsp;&nbsp;</td>'.
                   8462:                '<td valign="top"><b>'.&mt('Activities').'</b><br />'.
                   8463:                '<select name="activity"><option value="any"';
                   8464:     if ($curr->{'activity'} eq 'any') {
                   8465:         $output .= ' selected="selected"';
                   8466:     }
                   8467:     $output .= '>'.&mt('Any').'</option>'."\n";
                   8468:     foreach my $activity ('Role','log') {
                   8469:         my $selstr = '';
                   8470:         if ($activity eq $curr->{'activity'}) {
                   8471:             $selstr = ' selected="selected"';
                   8472:         }
                   8473:         $output .= '<option value="'.$activity.'"'.$selstr.'>'.$lt{$activity}.'</option>';
                   8474:     }
                   8475:     $output .= '</select></td>'.
                   8476:                '</tr></table>';
                   8477:     # Update Display button
                   8478:     $output .= '<p>'
                   8479:               .'<input type="submit" value="'.&mt('Update Display').'" />'
1.431     raeburn  8480:               .'</p><hr />';
1.416     raeburn  8481:     return $output;
                   8482: }
                   8483: 
1.415     raeburn  8484: sub userlogdisplay_js {
                   8485:     my ($formname) = @_;
                   8486:     return <<"ENDSCRIPT";
                   8487: 
1.239     raeburn  8488: function chgPage(caller) {
                   8489:     if (caller == 'previous') {
                   8490:         document.$formname.page.value --;
                   8491:     }
                   8492:     if (caller == 'next') {
                   8493:         document.$formname.page.value ++;
                   8494:     }
1.327     raeburn  8495:     document.$formname.submit();
1.239     raeburn  8496:     return;
                   8497: }
                   8498: ENDSCRIPT
1.415     raeburn  8499: }
                   8500: 
                   8501: sub userlogdisplay_navlinks {
                   8502:     my ($curr,$more_records) = @_;
                   8503:     return unless(ref($curr) eq 'HASH');
                   8504:     # Navigation Buttons
                   8505:     my $nav_links = '<p>';
                   8506:     if (($curr->{'page'} > 1) || ($more_records)) {
                   8507:         if (($curr->{'page'} > 1) && ($curr->{'show'} !~ /\D/)) {
                   8508:             $nav_links .= '<input type="button"'
                   8509:                          .' onclick="javascript:chgPage('."'previous'".');"'
                   8510:                          .' value="'.&mt('Previous [_1] changes',$curr->{'show'})
                   8511:                          .'" /> ';
                   8512:         }
                   8513:         if ($more_records) {
                   8514:             $nav_links .= '<input type="button"'
                   8515:                          .' onclick="javascript:chgPage('."'next'".');"'
                   8516:                          .' value="'.&mt('Next [_1] changes',$curr->{'show'})
                   8517:                          .'" />';
1.301     bisitz   8518:         }
                   8519:     }
1.415     raeburn  8520:     $nav_links .= '</p>';
                   8521:     return $nav_links;
1.239     raeburn  8522: }
                   8523: 
                   8524: sub role_display_filter {
1.363     raeburn  8525:     my ($context,$formname,$cdom,$cnum,$curr,$version,$crstype) = @_;
1.239     raeburn  8526:     my $nolink = 1;
                   8527:     my $output = '<table><tr><td valign="top">'.
1.301     bisitz   8528:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b></span><br />'.
1.467     raeburn  8529:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},'',undef,
1.239     raeburn  8530:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   8531:                  '</td><td>&nbsp;&nbsp;</td>';
                   8532:     my $startform =
                   8533:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
                   8534:                                             $curr->{'rolelog_start_date'},undef,
                   8535:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   8536:     my $endform =
                   8537:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
                   8538:                                             $curr->{'rolelog_end_date'},undef,
                   8539:                                             undef,undef,undef,undef,undef,undef,$nolink);
1.363     raeburn  8540:     my %lt = &rolechg_contexts($context,$crstype);
1.301     bisitz   8541:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br />'.
                   8542:                '<table><tr><td>'.&mt('After:').
                   8543:                '</td><td>'.$startform.'</td></tr>'.
                   8544:                '<tr><td>'.&mt('Before:').'</td>'.
                   8545:                '<td>'.$endform.'</td></tr></table>'.
                   8546:                '</td>'.
                   8547:                '<td>&nbsp;&nbsp;</td>'.
1.239     raeburn  8548:                '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
                   8549:                '<select name="role"><option value="any"';
                   8550:     if ($curr->{'role'} eq 'any') {
                   8551:         $output .= ' selected="selected"';
                   8552:     }
1.466     raeburn  8553:     $output .= '>'.&mt('Any').'</option>'."\n";
1.363     raeburn  8554:     my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
1.239     raeburn  8555:     foreach my $role (@roles) {
                   8556:         my $plrole;
                   8557:         if ($role eq 'cr') {
                   8558:             $plrole = &mt('Custom Role');
                   8559:         } else {
1.318     raeburn  8560:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.239     raeburn  8561:         }
                   8562:         my $selstr = '';
                   8563:         if ($role eq $curr->{'role'}) {
                   8564:             $selstr = ' selected="selected"';
                   8565:         }
                   8566:         $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
                   8567:     }
1.301     bisitz   8568:     $output .= '</select></td>'.
                   8569:                '<td>&nbsp;&nbsp;</td>'.
                   8570:                '<td valign="top"><b>'.
1.239     raeburn  8571:                &mt('Context:').'</b><br /><select name="chgcontext">';
1.363     raeburn  8572:     my @posscontexts;
                   8573:     if ($context eq 'course') {
1.468     raeburn  8574:         @posscontexts = ('any','automated','updatenow','createcourse','course','domain','selfenroll','requestcourses','chgtype','ltienroll');
1.363     raeburn  8575:     } elsif ($context eq 'domain') {
                   8576:         @posscontexts = ('any','domain','requestauthor','domconfig','server');
                   8577:     } else {
1.470     raeburn  8578:         @posscontexts = ('any','author','coauthor','domain');
1.457     raeburn  8579:     }
1.363     raeburn  8580:     foreach my $chgtype (@posscontexts) {
1.239     raeburn  8581:         my $selstr = '';
                   8582:         if ($curr->{'chgcontext'} eq $chgtype) {
1.301     bisitz   8583:             $selstr = ' selected="selected"';
1.239     raeburn  8584:         }
1.363     raeburn  8585:         if ($context eq 'course') {
1.376     raeburn  8586:             if (($chgtype eq 'automated') || ($chgtype eq 'updatenow')) {
1.363     raeburn  8587:                 next if (!&Apache::lonnet::auto_run($cnum,$cdom));
                   8588:             }
1.239     raeburn  8589:         }
                   8590:         $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
1.248     raeburn  8591:     }
1.468     raeburn  8592:     my @possapprovals = ('any','none','domain','user');
                   8593:     my %apptxt = &approval_types();
                   8594:     $output .= '</select></td>'.
                   8595:                '<td>&nbsp;&nbsp;</td>'.
                   8596:                '<td valign="top"><b>'.
                   8597:                &mt('Approvals:').'</b><br /><select name="approvals">';
                   8598:     foreach my $approval (@possapprovals) {
                   8599:         my $selstr = '';
                   8600:         if ($curr->{'approvals'} eq $approval) {
                   8601:             $selstr = ' selected="selected"';
                   8602:         }    
                   8603:         $output .= '<option value="'.$approval.'"'.$selstr.'>'.$apptxt{$approval}.'</option>';
                   8604:     }
                   8605:     $output .= '</select></td></tr></table>';
1.303     bisitz   8606: 
                   8607:     # Update Display button
                   8608:     $output .= '<p>'
                   8609:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   8610:               .'</p>';
                   8611: 
                   8612:     # Server version info
1.363     raeburn  8613:     my $needsrev = '2.11.0';
                   8614:     if ($context eq 'course') {
                   8615:         $needsrev = '2.7.0';
                   8616:     }
                   8617:     
1.303     bisitz   8618:     $output .= '<p class="LC_info">'
                   8619:               .&mt('Only changes made from servers running LON-CAPA [_1] or later are displayed.'
1.363     raeburn  8620:                   ,$needsrev);
1.248     raeburn  8621:     if ($version) {
1.303     bisitz   8622:         $output .= ' '.&mt('This LON-CAPA server is version [_1]',$version);
                   8623:     }
                   8624:     $output .= '</p><hr />';
1.239     raeburn  8625:     return $output;
                   8626: }
                   8627: 
                   8628: sub rolechg_contexts {
1.363     raeburn  8629:     my ($context,$crstype) = @_;
                   8630:     my %lt;
                   8631:     if ($context eq 'course') {
                   8632:         %lt = &Apache::lonlocal::texthash (
1.239     raeburn  8633:                                              any          => 'Any',
1.376     raeburn  8634:                                              automated    => 'Automated Enrollment',
1.457     raeburn  8635:                                              chgtype      => 'Enrollment Type/Lock Change',
1.239     raeburn  8636:                                              updatenow    => 'Roster Update',
                   8637:                                              createcourse => 'Course Creation',
                   8638:                                              course       => 'User Management in course',
                   8639:                                              domain       => 'User Management in domain',
1.313     raeburn  8640:                                              selfenroll   => 'Self-enrolled',
1.318     raeburn  8641:                                              requestcourses => 'Course Request',
1.468     raeburn  8642:                                              ltienroll    => 'Enrollment via LTI',
1.239     raeburn  8643:                                          );
1.363     raeburn  8644:         if ($crstype eq 'Community') {
                   8645:             $lt{'createcourse'} = &mt('Community Creation');
                   8646:             $lt{'course'} = &mt('User Management in community');
                   8647:             $lt{'requestcourses'} = &mt('Community Request');
                   8648:         }
                   8649:     } elsif ($context eq 'domain') {
                   8650:         %lt = &Apache::lonlocal::texthash (
                   8651:                                              any           => 'Any',
                   8652:                                              domain        => 'User Management in domain',
                   8653:                                              requestauthor => 'Authoring Request',
                   8654:                                              server        => 'Command line script (DC role)',
                   8655:                                              domconfig     => 'Self-enrolled',
                   8656:                                          );
                   8657:     } else {
                   8658:         %lt = &Apache::lonlocal::texthash (
                   8659:                                              any    => 'Any',
                   8660:                                              domain => 'User Management in domain',
                   8661:                                              author => 'User Management by author',
1.470     raeburn  8662:                                              coauthor => 'User Management by coauthor',
1.363     raeburn  8663:                                          );
                   8664:     } 
1.239     raeburn  8665:     return %lt;
                   8666: }
                   8667: 
1.468     raeburn  8668: sub approval_types {
                   8669:     return &Apache::lonlocal::texthash (
                   8670:                                           any => 'Any',
                   8671:                                           none => 'No approval needed',
                   8672:                                           user => 'Role recipient approval',
                   8673:                                           domain => 'Domain coordinator approval',
                   8674:                                        );
                   8675: }
                   8676: 
1.428     raeburn  8677: sub print_helpdeskaccess_display {
                   8678:     my ($r,$permission,$brcrum) = @_;
                   8679:     my $formname = 'helpdeskaccess';
                   8680:     my $helpitem = 'Course_Helpdesk_Access';
                   8681:     push (@{$brcrum},
                   8682:              {href => '/adm/createuser?action=helpdesk',
                   8683:               text => 'Helpdesk Access',
                   8684:               help => $helpitem});
                   8685:     my $bread_crumbs_component = 'Helpdesk Staff Access';
                   8686:     my $args = { bread_crumbs           => $brcrum,
                   8687:                  bread_crumbs_component => $bread_crumbs_component};
                   8688: 
                   8689:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   8690:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   8691:     my $confname = $cdom.'-domainconfig';
                   8692:     my $crstype = &Apache::loncommon::course_type();
                   8693: 
1.434     raeburn  8694:     my @accesstypes = ('all','dh','da','none');
1.428     raeburn  8695:     my ($numstatustypes,@jsarray);
                   8696:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($cdom);
                   8697:     if (ref($types) eq 'ARRAY') {
1.430     raeburn  8698:         if (@{$types} > 0) {
1.428     raeburn  8699:             $numstatustypes = scalar(@{$types});
                   8700:             push(@accesstypes,'status');
                   8701:             @jsarray = ('bystatus');
                   8702:         }
                   8703:     }
                   8704:     my %customroles = &get_domain_customroles($cdom,$confname);
1.432     raeburn  8705:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($cdom,['dh','da']);
1.428     raeburn  8706:     if (keys(%domhelpdesk)) {
                   8707:        push(@accesstypes,('inc','exc'));
                   8708:        push(@jsarray,('notinc','notexc'));
                   8709:     }
                   8710:     push(@jsarray,'privs');
                   8711:     my $hiddenstr = join("','",@jsarray);
                   8712:     my $rolestr = join("','",sort(keys(%customroles)));
                   8713: 
                   8714:     my $jscript;
                   8715:     my (%settings,%overridden);
                   8716:     if (keys(%customroles)) {
                   8717:         &get_adhocrole_settings($env{'request.course.id'},\@accesstypes,
                   8718:                                 $types,\%customroles,\%settings,\%overridden);
                   8719:         my %jsfull=();
                   8720:         my %jslevels= (
                   8721:                      course => {},
                   8722:                      domain => {},
                   8723:                      system => {},
                   8724:                     );
                   8725:         my %jslevelscurrent=(
                   8726:                            course => {},
                   8727:                            domain => {},
                   8728:                            system => {},
                   8729:                           );
                   8730:         my (%privs,%jsprivs);
                   8731:         &Apache::lonuserutils::custom_role_privs(\%privs,\%jsfull,\%jslevels,\%jslevelscurrent);
                   8732:         foreach my $priv (keys(%jsfull)) {
                   8733:             if ($jslevels{'course'}{$priv}) {
                   8734:                 $jsprivs{$priv} = 1;
                   8735:             }
                   8736:         }
                   8737:         my (%elements,%stored);
                   8738:         foreach my $role (keys(%customroles)) {
                   8739:             $elements{$role.'_access'} = 'radio';
                   8740:             $elements{$role.'_incrs'} = 'radio';
                   8741:             if ($numstatustypes) {
                   8742:                 $elements{$role.'_status'} = 'checkbox';
                   8743:             }
                   8744:             if (keys(%domhelpdesk) > 0) {
                   8745:                 $elements{$role.'_staff_inc'} = 'checkbox';
                   8746:                 $elements{$role.'_staff_exc'} = 'checkbox';
                   8747:             }
1.430     raeburn  8748:             $elements{$role.'_override'} = 'checkbox';
1.428     raeburn  8749:             if (ref($settings{$role}) eq 'HASH') {
                   8750:                 if ($settings{$role}{'access'} ne '') {
                   8751:                     my $curraccess = $settings{$role}{'access'};
                   8752:                     $stored{$role.'_access'} = $curraccess;
                   8753:                     $stored{$role.'_incrs'} = 1;
                   8754:                     if ($curraccess eq 'status') {
                   8755:                         if (ref($settings{$role}{'status'}) eq 'ARRAY') {
                   8756:                             $stored{$role.'_status'} = $settings{$role}{'status'};
                   8757:                         }
                   8758:                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   8759:                         if (ref($settings{$role}{$curraccess}) eq 'ARRAY') {
                   8760:                             $stored{$role.'_staff_'.$curraccess} = $settings{$role}{$curraccess};
                   8761:                         }
                   8762:                     }
                   8763:                 } else {
                   8764:                     $stored{$role.'_incrs'} = 0;
                   8765:                 }
                   8766:                 $stored{$role.'_override'} = [];
                   8767:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.adhocpriv.'.$role}) {
                   8768:                     if (ref($settings{$role}{'off'}) eq 'ARRAY') {
                   8769:                         foreach my $priv (@{$settings{$role}{'off'}}) {
                   8770:                             push(@{$stored{$role.'_override'}},$priv);
                   8771:                         }
                   8772:                     }
                   8773:                     if (ref($settings{$role}{'on'}) eq 'ARRAY') {
                   8774:                         foreach my $priv (@{$settings{$role}{'on'}}) {
                   8775:                             unless (grep(/^$priv$/,@{$stored{$role.'_override'}})) {
                   8776:                                 push(@{$stored{$role.'_override'}},$priv);
                   8777:                             }
                   8778:                         }
                   8779:                     }
                   8780:                 }
                   8781:             } else {
                   8782:                 $stored{$role.'_incrs'} = 0;
                   8783:             }
                   8784:         }
                   8785:         $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements,\%stored);
                   8786:     }
                   8787: 
                   8788:     my $js = <<"ENDJS";
                   8789: <script type="text/javascript">
                   8790: // <![CDATA[
                   8791: $jscript;
                   8792: 
                   8793: function switchRoleTab(caller,role) {
                   8794:     if (document.getElementById(role+'_maindiv')) {
                   8795:         if (caller.id != 'LC_current_minitab') {
                   8796:             if (document.getElementById('LC_current_minitab')) {
                   8797:                 document.getElementById('LC_current_minitab').id=null;
                   8798:             }
                   8799:             var roledivs = Array('$rolestr');
                   8800:             if (roledivs.length > 0) {
                   8801:                 for (var i=0; i<roledivs.length; i++) {
                   8802:                     if (document.getElementById(roledivs[i]+'_maindiv')) {
                   8803:                         document.getElementById(roledivs[i]+'_maindiv').style.display='none';
                   8804:                     }
                   8805:                 }
                   8806:             }
                   8807:             caller.id = 'LC_current_minitab';
                   8808:             document.getElementById(role+'_maindiv').style.display='block';
                   8809:         }
                   8810:     }
                   8811:     return false;
1.430     raeburn  8812: }
1.428     raeburn  8813: 
                   8814: function helpdeskAccess(role) {
                   8815:     var curraccess = null;
                   8816:     if (document.$formname.elements[role+'_access'].length) {
                   8817:         for (var i=0; i<document.$formname.elements[role+'_access'].length; i++) {
                   8818:             if (document.$formname.elements[role+'_access'][i].checked) {
                   8819:                 curraccess = document.$formname.elements[role+'_access'][i].value;
                   8820:             }
                   8821:         }
                   8822:     }
                   8823:     var shown = Array();
                   8824:     var hidden = Array();
                   8825:     if (curraccess == 'none') {
1.430     raeburn  8826:         hidden = Array ('$hiddenstr');
1.428     raeburn  8827:     } else {
                   8828:         if (curraccess == 'status') {
1.430     raeburn  8829:             shown = Array ('bystatus','privs');
                   8830:             hidden = Array ('notinc','notexc');
1.428     raeburn  8831:         } else {
                   8832:             if (curraccess == 'exc') {
                   8833:                 shown = Array ('notexc','privs');
                   8834:                 hidden = Array ('notinc','bystatus');
                   8835:             }
                   8836:             if (curraccess == 'inc') {
                   8837:                 shown = Array ('notinc','privs');
                   8838:                 hidden = Array ('notexc','bystatus');
                   8839:             }
                   8840:             if (curraccess == 'all') {
                   8841:                 shown = Array ('privs');
                   8842:                 hidden = Array ('notinc','notexc','bystatus');
                   8843:             }
                   8844:         }
                   8845:     }
                   8846:     if (hidden.length > 0) {
                   8847:         for (var i=0; i<hidden.length; i++) {
                   8848:             if (document.getElementById(role+'_'+hidden[i])) {
1.430     raeburn  8849:                 document.getElementById(role+'_'+hidden[i]).style.display = 'none';
1.428     raeburn  8850:             }
                   8851:         }
                   8852:     }
                   8853:     if (shown.length > 0) {
                   8854:         for (var i=0; i<shown.length; i++) {
                   8855:             if (document.getElementById(role+'_'+shown[i])) {
                   8856:                 if (shown[i] == 'privs') {
                   8857:                     document.getElementById(role+'_'+shown[i]).style.display = 'block';
                   8858:                 } else {
                   8859:                     document.getElementById(role+'_'+shown[i]).style.display = 'inline';
                   8860:                 }
                   8861:             }
                   8862:         }
                   8863:     }
                   8864:     return;
                   8865: }
                   8866: 
                   8867: function toggleAccess(role) {
                   8868:     if ((document.getElementById(role+'_setincrs')) &&
                   8869:         (document.getElementById(role+'_setindom'))) {
                   8870:         for (var i=0; i<document.$formname.elements[role+'_incrs'].length; i++) {
                   8871:             if (document.$formname.elements[role+'_incrs'][i].checked) {
                   8872:                 if (document.$formname.elements[role+'_incrs'][i].value == 1) {
                   8873:                     document.getElementById(role+'_setindom').style.display = 'none';
1.430     raeburn  8874:                     document.getElementById(role+'_setincrs').style.display = 'block';
1.428     raeburn  8875:                 } else {
                   8876:                     document.getElementById(role+'_setincrs').style.display = 'none';
                   8877:                     document.getElementById(role+'_setindom').style.display = 'block';
                   8878:                 }
                   8879:                 break;
                   8880:             }
                   8881:         }
                   8882:     }
                   8883:     return;
                   8884: }
                   8885: 
                   8886: // ]]>
                   8887: </script>
                   8888: ENDJS
                   8889: 
                   8890:     $args->{add_entries} = {onload => "javascript:setFormElements(document.$formname)"};
                   8891: 
                   8892:     # print page header
                   8893:     $r->print(&header($js,$args));
                   8894:     # print form header
                   8895:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">');
                   8896: 
                   8897:     if (keys(%customroles)) {
                   8898:         my %lt = &Apache::lonlocal::texthash(
                   8899:                     'aco'    => 'As course owner you may override the defaults set in the domain for role usage and/or privileges.',
                   8900:                     'rou'    => 'Role usage',
                   8901:                     'whi'    => 'Which helpdesk personnel may use this role?',
                   8902:                     'udd'    => 'Use domain default',
1.433     raeburn  8903:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
1.434     raeburn  8904:                     'dh'     => 'All with domain helpdesk role',
                   8905:                     'da'     => 'All with domain helpdesk assistant role',
1.428     raeburn  8906:                     'none'   => 'None',
                   8907:                     'status' => 'Determined based on institutional status',
1.430     raeburn  8908:                     'inc'    => 'Include all, but exclude specific personnel',
1.428     raeburn  8909:                     'exc'    => 'Exclude all, but include specific personnel',
                   8910:                     'hel'    => 'Helpdesk',
                   8911:                     'rpr'    => 'Role privileges',
                   8912:                  );
                   8913:         $lt{'tfh'} = &mt("Custom [_1]ad hoc[_2] course roles available for use by the domain's helpdesk are as follows",'<i>','</i>');
                   8914:         my %domconfig = &Apache::lonnet::get_dom('configuration',['helpsettings'],$cdom);
                   8915:         my (%domcurrent,%ordered,%description,%domusage,$disabled);
                   8916:         if (ref($domconfig{'helpsettings'}) eq 'HASH') {
                   8917:             if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
                   8918:                 %domcurrent = %{$domconfig{'helpsettings'}{'adhoc'}};
                   8919:             }
                   8920:         }
                   8921:         my $count = 0;
                   8922:         foreach my $role (sort(keys(%customroles))) {
                   8923:             my ($order,$desc,$access_in_dom);
                   8924:             if (ref($domcurrent{$role}) eq 'HASH') {
                   8925:                 $order = $domcurrent{$role}{'order'};
                   8926:                 $desc = $domcurrent{$role}{'desc'};
                   8927:                 $access_in_dom = $domcurrent{$role}{'access'};
                   8928:             }
                   8929:             if ($order eq '') {
                   8930:                 $order = $count;
                   8931:             }
                   8932:             $ordered{$order} = $role;
                   8933:             if ($desc ne '') {
                   8934:                 $description{$role} = $desc;
                   8935:             } else {
                   8936:                 $description{$role}= $role;
                   8937:             }
                   8938:             $count++;
                   8939:         }
                   8940:         %domusage = &domain_adhoc_access(\%customroles,\%domcurrent,\@accesstypes,$usertypes,$othertitle);
                   8941:         my @roles_by_num = ();
                   8942:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
                   8943:             push(@roles_by_num,$ordered{$item});
1.430     raeburn  8944:         }
1.429     raeburn  8945:         $r->print('<p>'.$lt{'tfh'}.': <i>'.join('</i>, <i>',map { $description{$_}; } @roles_by_num).'</i>.');
1.428     raeburn  8946:         if ($permission->{'owner'}) {
                   8947:             $r->print('<br />'.$lt{'aco'}.'</p><p>');
                   8948:             $r->print('<input type="hidden" name="state" value="process" />'.
                   8949:                       '<input type="submit" value="'.&mt('Save changes').'" />');
                   8950:         } else {
                   8951:             if ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'}) {
                   8952:                 my ($ownername,$ownerdom) = split(/:/,$env{'course.'.$env{'request.course.id'}.'.internal.courseowner'});
                   8953:                 $r->print('<br />'.&mt('The course owner -- [_1] -- can override the default access and/or privileges for these ad hoc roles.',
                   8954:                                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($ownername,$ownerdom),$ownername,$ownerdom)));
                   8955:             }
                   8956:             $disabled = ' disabled="disabled"';
                   8957:         }
                   8958:         $r->print('</p>');
                   8959: 
                   8960:         $r->print('<div id="LC_minitab_header"><ul>');
                   8961:         my $count = 0;
                   8962:         my %visibility;
                   8963:         foreach my $role (@roles_by_num) {
                   8964:             my $id;
                   8965:             if ($count == 0) {
                   8966:                 $id=' id="LC_current_minitab"';
1.430     raeburn  8967:                 $visibility{$role} = ' style="display:block"';
1.428     raeburn  8968:             } else {
                   8969:                 $visibility{$role} = ' style="display:none"';
                   8970:             }
                   8971:             $count ++;
                   8972:             $r->print('<li'.$id.'><a href="#" onclick="javascript:switchRoleTab(this.parentNode,'."'$role'".');">'.$description{$role}.'</a></li>');
                   8973:         }
                   8974:         $r->print('</ul></div>');
                   8975: 
                   8976:         foreach my $role (@roles_by_num) {
                   8977:             my %usecheck = (
                   8978:                              all => ' checked="checked"',
                   8979:                            );
                   8980:             my %displaydiv = (
                   8981:                                 status => 'none',
                   8982:                                 inc    => 'none',
                   8983:                                 exc    => 'none',
                   8984:                                 priv   => 'block',
                   8985:                              );
                   8986:             my (%selected,$overridden,$incrscheck,$indomcheck,$indomvis,$incrsvis);
1.430     raeburn  8987:             if (ref($settings{$role}) eq 'HASH') {
1.428     raeburn  8988:                 if ($settings{$role}{'access'} ne '') {
                   8989:                     $indomvis = ' style="display:none"';
                   8990:                     $incrsvis = ' style="display:block"';
1.430     raeburn  8991:                     $incrscheck = ' checked="checked"';
1.428     raeburn  8992:                     if ($settings{$role}{'access'} ne 'all') {
                   8993:                         $usecheck{$settings{$role}{'access'}} = $usecheck{'all'};
                   8994:                         delete($usecheck{'all'});
                   8995:                         if ($settings{$role}{'access'} eq 'status') {
                   8996:                             my $access = 'status';
                   8997:                             $displaydiv{$access} = 'inline';
                   8998:                             if (ref($settings{$role}{$access}) eq 'ARRAY') {
                   8999:                                 $selected{$access} = $settings{$role}{$access};
                   9000:                             }
                   9001:                         } elsif ($settings{$role}{'access'} =~ /^(inc|exc)$/) {
                   9002:                             my $access = $1;
                   9003:                             $displaydiv{$access} = 'inline';
                   9004:                             if (ref($settings{$role}{$access}) eq 'ARRAY') {
                   9005:                                 $selected{$access} = $settings{$role}{$access};
                   9006:                             }
                   9007:                         } elsif ($settings{$role}{'access'} eq 'none') {
                   9008:                             $displaydiv{'priv'} = 'none';
                   9009:                         }
                   9010:                     }
                   9011:                 } else {
                   9012:                     $indomcheck = ' checked="checked"';
                   9013:                     $indomvis = ' style="display:block"';
                   9014:                     $incrsvis = ' style="display:none"';
                   9015:                 }
                   9016:             } else {
                   9017:                 $indomcheck = ' checked="checked"';
1.430     raeburn  9018:                 $indomvis = ' style="display:block"';
1.428     raeburn  9019:                 $incrsvis = ' style="display:none"';
                   9020:             }
                   9021:             $r->print('<div class="LC_left_float" id="'.$role.'_maindiv"'.$visibility{$role}.'>'.
                   9022:                       '<fieldset><legend>'.$lt{'rou'}.'</legend>'.
                   9023:                       '<p>'.$lt{'whi'}.' <span class="LC_nobreak">'.
                   9024:                       '<label><input type="radio" name="'.$role.'_incrs" value="1"'.$incrscheck.' onclick="toggleAccess('."'$role'".');"'.$disabled.'>'.
                   9025:                       &mt('Set here in [_1]',lc($crstype)).'</label>'.
                   9026:                       '<span>'.('&nbsp;'x2).
                   9027:                       '<label><input type="radio" name="'.$role.'_incrs" value="0"'.$indomcheck.' onclick="toggleAccess('."'$role'".');"'.$disabled.'>'.
                   9028:                       $lt{'udd'}.'</label><span></p>'.
                   9029:                       '<div id="'.$role.'_setindom"'.$indomvis.'>'.
                   9030:                       '<span class="LC_cusr_emph">'.$domusage{$role}.'</span></div>'.
                   9031:                       '<div id="'.$role.'_setincrs"'.$incrsvis.'>');
                   9032:             foreach my $access (@accesstypes) {
                   9033:                 $r->print('<p><label><input type="radio" name="'.$role.'_access" value="'.$access.'" '.$usecheck{$access}.
                   9034:                           ' onclick="helpdeskAccess('."'$role'".');"'.$disabled.' />'.$lt{$access}.'</label>');
                   9035:                 if ($access eq 'status') {
                   9036:                     $r->print('<div id="'.$role.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
                   9037:                               &Apache::lonuserutils::adhoc_status_types($cdom,undef,$role,$selected{$access},
                   9038:                                                                         $othertitle,$usertypes,$types,$disabled).
                   9039:                               '</div>');
                   9040:                 } elsif (($access eq 'inc') && (keys(%domhelpdesk) > 0)) {
                   9041:                     $r->print('<div id="'.$role.'_notinc" style="display:'.$displaydiv{$access}.'">'.
                   9042:                               &Apache::lonuserutils::adhoc_staff($access,undef,$role,$selected{$access},
                   9043:                                                                  \%domhelpdesk,$disabled).
                   9044:                               '</div>');
                   9045:                 } elsif (($access eq 'exc') && (keys(%domhelpdesk) > 0)) {
                   9046:                     $r->print('<div id="'.$role.'_notexc" style="display:'.$displaydiv{$access}.'">'.
                   9047:                               &Apache::lonuserutils::adhoc_staff($access,undef,$role,$selected{$access},
                   9048:                                                                  \%domhelpdesk,$disabled).
                   9049:                               '</div>');
                   9050:                 }
                   9051:                 $r->print('</p>');
                   9052:             }
                   9053:             $r->print('</div></fieldset>');
                   9054:             my %full=();
                   9055:             my %levels= (
                   9056:                          course => {},
                   9057:                          domain => {},
                   9058:                          system => {},
                   9059:                         );
                   9060:             my %levelscurrent=(
                   9061:                                course => {},
                   9062:                                domain => {},
                   9063:                                system => {},
                   9064:                               );
                   9065:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
                   9066:             $r->print('<fieldset id="'.$role.'_privs" style="display:'.$displaydiv{'priv'}.'">'.
                   9067:                       '<legend>'.$lt{'rpr'}.'</legend>'.
                   9068:                       &role_priv_table($role,$permission,$crstype,\%full,\%levels,\%levelscurrent,$overridden{$role}).
                   9069:                       '</fieldset></div><div style="padding:0;clear:both;margin:0;border:0"></div>');
                   9070:         }
1.429     raeburn  9071:         if ($permission->{'owner'}) {
                   9072:             $r->print('<p><input type="submit" value="'.&mt('Save changes').'" /></p>');
                   9073:         }
1.428     raeburn  9074:     } else {
                   9075:         $r->print(&mt('Helpdesk roles have not yet been created in this domain.'));
                   9076:     }
                   9077:     # Form Footer
                   9078:     $r->print('<input type="hidden" name="action" value="helpdesk" />'
                   9079:              .'</form>');
                   9080:     return;
                   9081: }
                   9082: 
1.465     raeburn  9083: sub print_queued_roles {
                   9084:     my ($r,$context,$permission,$brcrum) = @_;
                   9085:     push (@{$brcrum},
                   9086:              {href => '/adm/createuser?action=rolerequests',
                   9087:               text => 'Role Requests (other domains)',
                   9088:               help => ''});
                   9089:     my $bread_crumbs_component = 'Role Requests';
                   9090:     my $args = { bread_crumbs           => $brcrum,
                   9091:                  bread_crumbs_component => $bread_crumbs_component};
                   9092:     # print page header
                   9093:     $r->print(&header('',$args));
                   9094:     my ($dom,$cnum);
                   9095:     $dom = $env{'request.role.domain'};
                   9096:     if ($context eq 'course') {
                   9097:         if ($env{'request.course.id'}) {
                   9098:             if (&Apache::loncommon::course_type() eq 'Community') {
                   9099:                 $context = 'community';
                   9100:             }
                   9101:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   9102:         }
                   9103:     } elsif ($context eq 'author') {
                   9104:         $cnum = $env{'user.name'};
                   9105:     }
                   9106:     $r->print(&Apache::loncoursequeueadmin::display_queued_requests('othdomqueue',$dom,$cnum,$context));
                   9107:     return;
                   9108: }
                   9109: 
                   9110: sub print_pendingroles {
                   9111:     my ($r,$context,$permission,$brcrum) = @_;
                   9112:     push (@{$brcrum},
                   9113:              {href => '/adm/createuser?action=queuedroles',
                   9114:               text => 'Queued Role Assignments (users in this domain)',
                   9115:               help => ''});
                   9116:     my $bread_crumbs_component = 'Queued Role Assignments';
                   9117:     my $args = { bread_crumbs           => $brcrum,
                   9118:                  bread_crumbs_component => $bread_crumbs_component};
                   9119:     # print page header
                   9120:     $r->print(&header('',$args));
                   9121:     $r->print(&Apache::loncoursequeueadmin::display_queued_requests('othdomaction',$env{'request.role.domain'},'','domain'));
                   9122:     return;
                   9123: }
                   9124: 
                   9125: sub process_pendingroles {
                   9126:     my ($r,$context,$permission,$brcrum) = @_;
                   9127:     push (@{$brcrum},
                   9128:              {href => '/adm/createuser?action=queuedroles',
                   9129:               text => 'Queued Role Assignments (users in this domain)',
                   9130:               help => ''},
                   9131:              {href => '/adm/createuser?action=processrolereq',
                   9132:               text => 'Process Queue',
                   9133:               help => ''});
                   9134:     my $bread_crumbs_component = 'Queued Role Assignments';
                   9135:     my $args = { bread_crumbs           => $brcrum,
                   9136:                  bread_crumbs_component => $bread_crumbs_component};
                   9137:     # print page header
                   9138:     $r->print(&header('',$args));
                   9139:     $r->print(&Apache::loncoursequeueadmin::update_request_queue('othdombydc',
                   9140:                                                                  $env{'request.role.domain'}));
                   9141:     return;
                   9142: }
                   9143: 
1.428     raeburn  9144: sub domain_adhoc_access {
                   9145:     my ($roles,$domcurrent,$accesstypes,$usertypes,$othertitle) = @_;
                   9146:     my %domusage;
                   9147:     return unless ((ref($roles) eq 'HASH') && (ref($domcurrent) eq 'HASH') && (ref($accesstypes) eq 'ARRAY'));
                   9148:     foreach my $role (keys(%{$roles})) {
                   9149:         if (ref($domcurrent->{$role}) eq 'HASH') {
                   9150:             my $access = $domcurrent->{$role}{'access'};
                   9151:             if (($access eq '') || (!grep(/^\Q$access\E$/,@{$accesstypes}))) {
                   9152:                 $access = 'all';
1.432     raeburn  9153:                 $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role',&Apache::lonnet::plaintext('dh'),
                   9154:                                                                                           &Apache::lonnet::plaintext('da'));
1.428     raeburn  9155:             } elsif ($access eq 'status') {
                   9156:                 if (ref($domcurrent->{$role}{$access}) eq 'ARRAY') {
                   9157:                     my @shown;
                   9158:                     foreach my $type (@{$domcurrent->{$role}{$access}}) {
                   9159:                         unless ($type eq 'default') {
                   9160:                             if ($usertypes->{$type}) {
                   9161:                                 push(@shown,$usertypes->{$type});
                   9162:                             }
                   9163:                         }
                   9164:                     }
                   9165:                     if (grep(/^default$/,@{$domcurrent->{$role}{$access}})) {
                   9166:                         push(@shown,$othertitle);
                   9167:                     }
                   9168:                     if (@shown) {
                   9169:                         my $shownstatus = join(' '.&mt('or').' ',@shown);
1.432     raeburn  9170:                         $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role, and institutional status: [_3]',
                   9171:                                                &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'),$shownstatus);
1.428     raeburn  9172:                     } else {
                   9173:                         $domusage{$role} = &mt('No one in the domain');
                   9174:                     }
                   9175:                 }
                   9176:             } elsif ($access eq 'inc') {
                   9177:                 my @dominc = ();
                   9178:                 if (ref($domcurrent->{$role}{'inc'}) eq 'ARRAY') {
                   9179:                     foreach my $user (@{$domcurrent->{$role}{'inc'}}) {
                   9180:                         my ($uname,$udom) = split(/:/,$user);
                   9181:                         push(@dominc,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom));
                   9182:                     }
                   9183:                     my $showninc = join(', ',@dominc);
                   9184:                     if ($showninc ne '') {
1.432     raeburn  9185:                         $domusage{$role} = &mt('Include any user in domain with active [_1] or [_2] role, except: [_3]',
                   9186:                                                &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'),$showninc);
1.428     raeburn  9187:                     } else {
1.432     raeburn  9188:                         $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role',
                   9189:                                                &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'));
1.428     raeburn  9190:                     }
                   9191:                 }
                   9192:             } elsif ($access eq 'exc') {
                   9193:                 my @domexc = ();
                   9194:                 if (ref($domcurrent->{$role}{'exc'}) eq 'ARRAY') {
                   9195:                     foreach my $user (@{$domcurrent->{$role}{'exc'}}) {
                   9196:                         my ($uname,$udom) = split(/:/,$user);
                   9197:                         push(@domexc,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom));
                   9198:                     }
                   9199:                 }
                   9200:                 my $shownexc = join(', ',@domexc);
                   9201:                 if ($shownexc ne '') {
1.432     raeburn  9202:                     $domusage{$role} = &mt('Only the following in the domain with active [_1] or [_2] role: [_3]',
                   9203:                                            &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'),$shownexc);
1.428     raeburn  9204:                 } else {
                   9205:                     $domusage{$role} = &mt('No one in the domain');
                   9206:                 }
                   9207:             } elsif ($access eq 'none') {
                   9208:                 $domusage{$role} = &mt('No one in the domain');
1.434     raeburn  9209:             } elsif ($access eq 'dh') {
1.433     raeburn  9210:                 $domusage{$role} = &mt('Any user in domain with active [_1] role',&Apache::lonnet::plaintext('dh'));
1.434     raeburn  9211:             } elsif ($access eq 'da') {
1.433     raeburn  9212:                 $domusage{$role} = &mt('Any user in domain with active [_1] role',&Apache::lonnet::plaintext('da'));
1.428     raeburn  9213:             } elsif ($access eq 'all') {
1.432     raeburn  9214:                 $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role',
                   9215:                                        &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'));
1.428     raeburn  9216:             }
                   9217:         } else {
1.432     raeburn  9218:             $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role',
                   9219:                                    &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'));
1.428     raeburn  9220:         }
                   9221:     }
                   9222:     return %domusage;
                   9223: }
                   9224: 
                   9225: sub get_domain_customroles {
                   9226:     my ($cdom,$confname) = @_;
                   9227:     my %existing=&Apache::lonnet::dump('roles',$cdom,$confname,'rolesdef_');
                   9228:     my %customroles;
                   9229:     foreach my $key (keys(%existing)) {
                   9230:         if ($key=~/^rolesdef\_(\w+)$/) {
                   9231:             my $rolename = $1;
                   9232:             my %privs;
                   9233:             ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
                   9234:             $customroles{$rolename} = \%privs;
                   9235:         }
                   9236:     }
                   9237:     return %customroles;
                   9238: }
                   9239: 
                   9240: sub role_priv_table {
                   9241:     my ($role,$permission,$crstype,$full,$levels,$levelscurrent,$overridden) = @_;
                   9242:     return unless ((ref($full) eq 'HASH') && (ref($levels) eq 'HASH') &&
                   9243:                    (ref($levelscurrent) eq 'HASH'));
                   9244:     my %lt=&Apache::lonlocal::texthash (
                   9245:                     'crl'  => 'Course Level Privilege',
                   9246:                     'def'  => 'Domain Defaults',
                   9247:                     'ove'  => 'Override in Course',
                   9248:                     'ine'  => 'In effect',
                   9249:                     'dis'  => 'Disabled',
                   9250:                     'ena'  => 'Enabled',
                   9251:                    );
                   9252:     if ($crstype eq 'Community') {
                   9253:         $lt{'ove'} = 'Override in Community',
                   9254:     }
                   9255:     my @status = ('Disabled','Enabled');
                   9256:     my (%on,%off);
                   9257:     if (ref($overridden) eq 'HASH') {
                   9258:         if (ref($overridden->{'on'}) eq 'ARRAY') {
                   9259:             map { $on{$_} = 1; } (@{$overridden->{'on'}});
                   9260:         }
                   9261:         if (ref($overridden->{'off'}) eq 'ARRAY') {
                   9262:             map { $off{$_} = 1; } (@{$overridden->{'off'}});
                   9263:         }
                   9264:     }
                   9265:     my $output=&Apache::loncommon::start_data_table().
                   9266:                &Apache::loncommon::start_data_table_header_row().
                   9267:                '<th>'.$lt{'crl'}.'</th><th>'.$lt{'def'}.'</th><th>'.$lt{'ove'}.
                   9268:                '</th><th>'.$lt{'ine'}.'</th>'.
                   9269:                &Apache::loncommon::end_data_table_header_row();
                   9270:     foreach my $priv (sort(keys(%{$full}))) {
                   9271:         next unless ($levels->{'course'}{$priv});
                   9272:         my $privtext = &Apache::lonnet::plaintext($priv,$crstype);
                   9273:         my ($default,$ineffect);
                   9274:         if ($levelscurrent->{'course'}{$priv}) {
                   9275:             $default = '<img src="/adm/lonIcons/navmap.correct.gif" alt="'.$lt{'ena'}.'" />';
                   9276:             $ineffect = $default;
                   9277:         }
                   9278:         my ($customstatus,$checked);
                   9279:         $output .= &Apache::loncommon::start_data_table_row().
                   9280:                    '<td>'.$privtext.'</td>'.
                   9281:                    '<td>'.$default.'</td><td>';
                   9282:         if (($levelscurrent->{'course'}{$priv}) && ($off{$priv})) {
                   9283:             if ($permission->{'owner'}) {
                   9284:                 $checked = ' checked="checked"';
                   9285:             }
                   9286:             $customstatus = '<img src="/adm/lonIcons/navmap.wrong.gif" alt="'.$lt{'dis'}.'" />';
1.430     raeburn  9287:             $ineffect = $customstatus;
1.428     raeburn  9288:         } elsif ((!$levelscurrent->{'course'}{$priv}) && ($on{$priv})) {
                   9289:             if ($permission->{'owner'}) {
1.430     raeburn  9290:                 $checked = ' checked="checked"';
1.428     raeburn  9291:             }
                   9292:             $customstatus = '<img src="/adm/lonIcons/navmap.correct.gif" alt="'.$lt{'ena'}.'" />';
1.430     raeburn  9293:             $ineffect = $customstatus;
1.428     raeburn  9294:         }
                   9295:         if ($permission->{'owner'}) {
                   9296:             $output .= '<input type="checkbox" name="'.$role.'_override" value="'.$priv.'"'.$checked.' />';
                   9297:         } else {
                   9298:             $output .= $customstatus;
                   9299:         }
                   9300:         $output .= '</td><td>'.$ineffect.'</td>'.
                   9301:                    &Apache::loncommon::end_data_table_row();
                   9302:     }
                   9303:     $output .= &Apache::loncommon::end_data_table();
                   9304:     return $output;
                   9305: }
                   9306: 
                   9307: sub get_adhocrole_settings {
1.430     raeburn  9308:     my ($cid,$accesstypes,$types,$customroles,$settings,$overridden) = @_;
1.428     raeburn  9309:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($customroles) eq 'HASH') &&
                   9310:                    (ref($settings) eq 'HASH') && (ref($overridden) eq 'HASH'));
                   9311:     foreach my $role (split(/,/,$env{'course.'.$cid.'.internal.adhocaccess'})) {
                   9312:         my ($curraccess,$rest) = split(/=/,$env{'course.'.$cid.'.internal.adhoc.'.$role});
                   9313:         if (($curraccess ne '') && (grep(/^\Q$curraccess\E$/,@{$accesstypes}))) {
                   9314:             $settings->{$role}{'access'} = $curraccess;
                   9315:             if (($curraccess eq 'status') && (ref($types) eq 'ARRAY')) {
                   9316:                 my @status = split(/,/,$rest);
                   9317:                 my @currstatus;
                   9318:                 foreach my $type (@status) {
                   9319:                     if ($type eq 'default') {
                   9320:                         push(@currstatus,$type);
                   9321:                     } elsif (grep(/^\Q$type\E$/,@{$types})) {
                   9322:                         push(@currstatus,$type);
                   9323:                     }
                   9324:                 }
                   9325:                 if (@currstatus) {
                   9326:                     $settings->{$role}{$curraccess} = \@currstatus;
                   9327:                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   9328:                     my @personnel = split(/,/,$rest);
                   9329:                     $settings->{$role}{$curraccess} = \@personnel;
                   9330:                 }
                   9331:             }
                   9332:         }
                   9333:     }
                   9334:     foreach my $role (keys(%{$customroles})) {
                   9335:         if ($env{'course.'.$cid.'.internal.adhocpriv.'.$role}) {
                   9336:             my %currentprivs;
                   9337:             if (ref($customroles->{$role}) eq 'HASH') {
                   9338:                 if (exists($customroles->{$role}{'course'})) {
                   9339:                     my %full=();
                   9340:                     my %levels= (
                   9341:                                   course => {},
                   9342:                                   domain => {},
                   9343:                                   system => {},
                   9344:                                 );
                   9345:                     my %levelscurrent=(
                   9346:                                         course => {},
                   9347:                                         domain => {},
                   9348:                                         system => {},
                   9349:                                       );
                   9350:                     &Apache::lonuserutils::custom_role_privs($customroles->{$role},\%full,\%levels,\%levelscurrent);
                   9351:                     %currentprivs = %{$levelscurrent{'course'}};
                   9352:                 }
                   9353:             }
                   9354:             foreach my $item (split(/,/,$env{'course.'.$cid.'.internal.adhocpriv.'.$role})) {
                   9355:                 next if ($item eq '');
                   9356:                 my ($rule,$rest) = split(/=/,$item);
                   9357:                 next unless (($rule eq 'off') || ($rule eq 'on'));
                   9358:                 foreach my $priv (split(/:/,$rest)) {
                   9359:                     if ($priv ne '') {
                   9360:                         if ($rule eq 'off') {
                   9361:                             push(@{$overridden->{$role}{'off'}},$priv);
                   9362:                             if ($currentprivs{$priv}) {
                   9363:                                 push(@{$settings->{$role}{'off'}},$priv);
                   9364:                             }
                   9365:                         } else {
                   9366:                             push(@{$overridden->{$role}{'on'}},$priv);
                   9367:                             unless ($currentprivs{$priv}) {
                   9368:                                 push(@{$settings->{$role}{'on'}},$priv);
                   9369:                             }
                   9370:                         }
                   9371:                     }
                   9372:                 }
                   9373:             }
                   9374:         }
                   9375:     }
                   9376:     return;
                   9377: }
                   9378: 
                   9379: sub update_helpdeskaccess {
                   9380:     my ($r,$permission,$brcrum) = @_;
                   9381:     my $helpitem = 'Course_Helpdesk_Access';
                   9382:     push (@{$brcrum},
                   9383:              {href => '/adm/createuser?action=helpdesk',
                   9384:               text => 'Helpdesk Access',
                   9385:               help => $helpitem},
                   9386:              {href => '/adm/createuser?action=helpdesk',
                   9387:               text => 'Result',
                   9388:               help => $helpitem}
                   9389:          );
                   9390:     my $bread_crumbs_component = 'Helpdesk Staff Access';
                   9391:     my $args = { bread_crumbs           => $brcrum,
                   9392:                  bread_crumbs_component => $bread_crumbs_component};
                   9393: 
                   9394:     # print page header
                   9395:     $r->print(&header('',$args));
                   9396:     unless ((ref($permission) eq 'HASH') && ($permission->{'owner'})) {
                   9397:         $r->print('<p class="LC_error">'.&mt('You do not have permission to change helpdesk access.').'</p>');
                   9398:         return;
                   9399:     }
1.434     raeburn  9400:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
1.428     raeburn  9401:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   9402:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   9403:     my $confname = $cdom.'-domainconfig';
                   9404:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($cdom);
                   9405:     my $crstype = &Apache::loncommon::course_type();
                   9406:     my %customroles = &get_domain_customroles($cdom,$confname);
                   9407:     my (%settings,%overridden);
                   9408:     &get_adhocrole_settings($env{'request.course.id'},\@accesstypes,
                   9409:                             $types,\%customroles,\%settings,\%overridden);
1.432     raeburn  9410:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($cdom,['dh','da']);
1.428     raeburn  9411:     my (%changed,%storehash,@todelete);
                   9412: 
                   9413:     if (keys(%customroles)) {
                   9414:         my (%newsettings,@incrs);
                   9415:         foreach my $role (keys(%customroles)) {
                   9416:             $newsettings{$role} = {
                   9417:                                     access => '',
                   9418:                                     status => '',
                   9419:                                     exc    => '',
                   9420:                                     inc    => '',
                   9421:                                     on     => '',
                   9422:                                     off    => '',
                   9423:                                   };
                   9424:             my %current;
                   9425:             if (ref($settings{$role}) eq 'HASH') {
                   9426:                 %current = %{$settings{$role}};
                   9427:             }
                   9428:             if (ref($overridden{$role}) eq 'HASH') {
                   9429:                 $current{'overridden'} = $overridden{$role};
                   9430:             }
                   9431:             if ($env{'form.'.$role.'_incrs'}) {
                   9432:                 my $access = $env{'form.'.$role.'_access'};
                   9433:                 if (grep(/^\Q$access\E$/,@accesstypes)) {
                   9434:                     push(@incrs,$role);
                   9435:                     unless ($current{'access'} eq $access) {
                   9436:                         $changed{$role}{'access'} = 1;
1.430     raeburn  9437:                         $storehash{'internal.adhoc.'.$role} = $access;
1.428     raeburn  9438:                     }
                   9439:                     if ($access eq 'status') {
                   9440:                         my @statuses = &Apache::loncommon::get_env_multiple('form.'.$role.'_status');
                   9441:                         my @stored;
                   9442:                         my @shownstatus;
                   9443:                         if (ref($types) eq 'ARRAY') {
                   9444:                             foreach my $type (sort(@statuses)) {
                   9445:                                 if ($type eq 'default') {
                   9446:                                     push(@stored,$type);
                   9447:                                 } elsif (grep(/^\Q$type\E$/,@{$types})) {
                   9448:                                     push(@stored,$type);
                   9449:                                     push(@shownstatus,$usertypes->{$type});
                   9450:                                 }
                   9451:                             }
                   9452:                             if (grep(/^default$/,@statuses)) {
                   9453:                                 push(@shownstatus,$othertitle);
                   9454:                             }
                   9455:                             $storehash{'internal.adhoc.'.$role} .= '='.join(',',@stored);
                   9456:                         }
                   9457:                         $newsettings{$role}{'status'} = join(' '.&mt('or').' ',@shownstatus);
                   9458:                         if (ref($current{'status'}) eq 'ARRAY') {
                   9459:                             my @diffs = &Apache::loncommon::compare_arrays(\@stored,$current{'status'});
                   9460:                             if (@diffs) {
                   9461:                                 $changed{$role}{'status'} = 1;
                   9462:                             }
                   9463:                         } elsif (@stored) {
                   9464:                             $changed{$role}{'status'} = 1;
                   9465:                         }
                   9466:                     } elsif (($access eq 'inc') || ($access eq 'exc')) {
                   9467:                         my @personnel = &Apache::loncommon::get_env_multiple('form.'.$role.'_staff_'.$access);
                   9468:                         my @newspecstaff;
                   9469:                         my @stored;
                   9470:                         my @currstaff;
                   9471:                         foreach my $person (sort(@personnel)) {
                   9472:                             if ($domhelpdesk{$person}) {
1.430     raeburn  9473:                                 push(@stored,$person);
1.428     raeburn  9474:                             }
                   9475:                         }
                   9476:                         if (ref($current{$access}) eq 'ARRAY') {
                   9477:                             my @diffs = &Apache::loncommon::compare_arrays(\@stored,$current{$access});
                   9478:                             if (@diffs) {
                   9479:                                 $changed{$role}{$access} = 1;
                   9480:                             }
                   9481:                         } elsif (@stored) {
                   9482:                             $changed{$role}{$access} = 1;
                   9483:                         }
                   9484:                         $storehash{'internal.adhoc.'.$role} .= '='.join(',',@stored);
                   9485:                         foreach my $person (@stored) {
                   9486:                             my ($uname,$udom) = split(/:/,$person);
                   9487:                             push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
                   9488:                         }
                   9489:                         $newsettings{$role}{$access} = join(', ',sort(@newspecstaff));
                   9490:                     }
                   9491:                     $newsettings{$role}{'access'} = $access;
                   9492:                 }
                   9493:             } else {
                   9494:                 if (($current{'access'} ne '') && (grep(/^\Q$current{'access'}\E$/,@accesstypes))) {
                   9495:                     $changed{$role}{'access'} = 1;
                   9496:                     $newsettings{$role} = {};
                   9497:                     push(@todelete,'internal.adhoc.'.$role);
                   9498:                 }
                   9499:             }
                   9500:             if (($env{'form.'.$role.'_incrs'}) && ($env{'form.'.$role.'_access'} eq 'none')) {
                   9501:                 if (ref($current{'overridden'}) eq 'HASH') {
                   9502:                     push(@todelete,'internal.adhocpriv.'.$role);
                   9503:                 }
                   9504:             } else {
                   9505:                 my %full=();
                   9506:                 my %levels= (
                   9507:                              course => {},
                   9508:                              domain => {},
                   9509:                              system => {},
                   9510:                             );
                   9511:                 my %levelscurrent=(
                   9512:                                    course => {},
                   9513:                                    domain => {},
                   9514:                                    system => {},
                   9515:                                   );
                   9516:                 &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
                   9517:                 my (@updatedon,@updatedoff,@override);
                   9518:                 @override = &Apache::loncommon::get_env_multiple('form.'.$role.'_override');
1.430     raeburn  9519:                 if (@override) {
1.428     raeburn  9520:                     foreach my $priv (sort(keys(%full))) {
                   9521:                         next unless ($levels{'course'}{$priv});
                   9522:                         if (grep(/^\Q$priv\E$/,@override)) {
                   9523:                             if ($levelscurrent{'course'}{$priv}) {
                   9524:                                 push(@updatedoff,$priv);
                   9525:                             } else {
                   9526:                                 push(@updatedon,$priv);
                   9527:                             }
                   9528:                         }
                   9529:                     }
                   9530:                 }
                   9531:                 if (@updatedon) {
1.430     raeburn  9532:                     $newsettings{$role}{'on'} = join('</li><li>', map { &Apache::lonnet::plaintext($_,$crstype) } (@updatedon));
1.428     raeburn  9533:                 }
                   9534:                 if (@updatedoff) {
                   9535:                     $newsettings{$role}{'off'} = join('</li><li>', map { &Apache::lonnet::plaintext($_,$crstype) } (@updatedoff));
                   9536:                 }
                   9537:                 if (ref($current{'overridden'}) eq 'HASH') {
                   9538:                     if (ref($current{'overridden'}{'on'}) eq 'ARRAY') {
                   9539:                         if (@updatedon) {
                   9540:                             my @diffs = &Apache::loncommon::compare_arrays(\@updatedon,$current{'overridden'}{'on'});
                   9541:                             if (@diffs) {
                   9542:                                 $changed{$role}{'on'} = 1;
                   9543:                             }
                   9544:                         } else {
                   9545:                             $changed{$role}{'on'} = 1;
                   9546:                         }
                   9547:                     } elsif (@updatedon) {
                   9548:                         $changed{$role}{'on'} = 1;
                   9549:                     }
                   9550:                     if (ref($current{'overridden'}{'off'}) eq 'ARRAY') {
                   9551:                         if (@updatedoff) {
                   9552:                             my @diffs = &Apache::loncommon::compare_arrays(\@updatedoff,$current{'overridden'}{'off'});
                   9553:                             if (@diffs) {
                   9554:                                 $changed{$role}{'off'} = 1;
                   9555:                             }
                   9556:                         } else {
                   9557:                             $changed{$role}{'off'} = 1;
                   9558:                         }
                   9559:                     } elsif (@updatedoff) {
                   9560:                         $changed{$role}{'off'} = 1;
                   9561:                     }
                   9562:                 } else {
                   9563:                     if (@updatedon) {
                   9564:                         $changed{$role}{'on'} = 1;
                   9565:                     }
                   9566:                     if (@updatedoff) {
                   9567:                         $changed{$role}{'off'} = 1;
                   9568:                     }
                   9569:                 }
                   9570:                 if (ref($changed{$role}) eq 'HASH') {
                   9571:                     if (($changed{$role}{'on'} || $changed{$role}{'off'})) {
                   9572:                         my $newpriv;
                   9573:                         if (@updatedon) {
                   9574:                             $newpriv = 'on='.join(':',@updatedon);
                   9575:                         }
                   9576:                         if (@updatedoff) {
                   9577:                             $newpriv .= ($newpriv ? ',' : '' ).'off='.join(':',@updatedoff);
                   9578:                         }
                   9579:                         if ($newpriv eq '') {
                   9580:                             push(@todelete,'internal.adhocpriv.'.$role);
                   9581:                         } else {
                   9582:                             $storehash{'internal.adhocpriv.'.$role} = $newpriv;
                   9583:                         }
                   9584:                     }
                   9585:                 }
                   9586:             }
                   9587:         }
                   9588:         if (@incrs) {
                   9589:             $storehash{'internal.adhocaccess'} = join(',',@incrs);
                   9590:         } elsif (@todelete) {
                   9591:             push(@todelete,'internal.adhocaccess');
                   9592:         }
                   9593:         if (keys(%changed)) {
                   9594:             my ($putres,$delres);
                   9595:             if (keys(%storehash)) {
                   9596:                 $putres = &Apache::lonnet::put('environment',\%storehash,$cdom,$cnum);
                   9597:                 my %newenvhash;
                   9598:                 foreach my $key (keys(%storehash)) {
                   9599:                     $newenvhash{'course.'.$env{'request.course.id'}.'.'.$key} = $storehash{$key};
                   9600:                 }
                   9601:                 &Apache::lonnet::appenv(\%newenvhash);
                   9602:             }
                   9603:             if (@todelete) {
                   9604:                 $delres = &Apache::lonnet::del('environment',\@todelete,$cdom,$cnum);
                   9605:                 foreach my $key (@todelete) {
                   9606:                     &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.'.$key);
                   9607:                 }
                   9608:             }
                   9609:             if (($putres eq 'ok') || ($delres eq 'ok')) {
                   9610:                 my %domconfig = &Apache::lonnet::get_dom('configuration',['helpsettings'],$cdom);
                   9611:                 my (%domcurrent,%ordered,%description,%domusage);
                   9612:                 if (ref($domconfig{'helpsettings'}) eq 'HASH') {
                   9613:                     if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
                   9614:                         %domcurrent = %{$domconfig{'helpsettings'}{'adhoc'}};
                   9615:                     }
                   9616:                 }
                   9617:                 my $count = 0;
                   9618:                 foreach my $role (sort(keys(%customroles))) {
                   9619:                     my ($order,$desc);
                   9620:                     if (ref($domcurrent{$role}) eq 'HASH') {
                   9621:                         $order = $domcurrent{$role}{'order'};
                   9622:                         $desc = $domcurrent{$role}{'desc'};
                   9623:                     }
                   9624:                     if ($order eq '') {
                   9625:                         $order = $count;
                   9626:                     }
                   9627:                     $ordered{$order} = $role;
                   9628:                     if ($desc ne '') {
                   9629:                         $description{$role} = $desc;
                   9630:                     } else {
                   9631:                         $description{$role}= $role;
                   9632:                     }
                   9633:                     $count++;
                   9634:                 }
                   9635:                 my @roles_by_num = ();
                   9636:                 foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
                   9637:                     push(@roles_by_num,$ordered{$item});
                   9638:                 }
                   9639:                 %domusage = &domain_adhoc_access(\%changed,\%domcurrent,\@accesstypes,$usertypes,$othertitle);
1.430     raeburn  9640:                 $r->print(&mt('Helpdesk access settings have been changed as follows').'<br />');
1.428     raeburn  9641:                 $r->print('<ul>');
                   9642:                 foreach my $role (@roles_by_num) {
                   9643:                     next unless (ref($changed{$role}) eq 'HASH');
                   9644:                     $r->print('<li>'.&mt('Ad hoc role').': <b>'.$description{$role}.'</b>'.
                   9645:                               '<ul>');
1.430     raeburn  9646:                     if ($changed{$role}{'access'} || $changed{$role}{'status'} || $changed{$role}{'inc'} || $changed{$role}{'exc'}) {
1.428     raeburn  9647:                         $r->print('<li>');
                   9648:                         if ($env{'form.'.$role.'_incrs'}) {
                   9649:                             if ($newsettings{$role}{'access'} eq 'all') {
                   9650:                                 $r->print(&mt('All helpdesk staff can access '.lc($crstype).' with this role.'));
1.434     raeburn  9651:                             } elsif ($newsettings{$role}{'access'} eq 'dh') {
1.433     raeburn  9652:                                 $r->print(&mt('Helpdesk staff can use this role if they have an active [_1] role',
                   9653:                                               &Apache::lonnet::plaintext('dh')));
1.434     raeburn  9654:                             } elsif ($newsettings{$role}{'access'} eq 'da') {
1.433     raeburn  9655:                                 $r->print(&mt('Helpdesk staff can use this role if they have an active [_1] role',
                   9656:                                               &Apache::lonnet::plaintext('da')));
1.428     raeburn  9657:                             } elsif ($newsettings{$role}{'access'} eq 'none') {
                   9658:                                 $r->print(&mt('No helpdesk staff can access '.lc($crstype).' with this role.'));
                   9659:                             } elsif ($newsettings{$role}{'access'} eq 'status') {
                   9660:                                 if ($newsettings{$role}{'status'}) {
                   9661:                                     my ($access,$rest) = split(/=/,$storehash{'internal.adhoc.'.$role});
1.430     raeburn  9662:                                     if (split(/,/,$rest) > 1) {
1.428     raeburn  9663:                                         $r->print(&mt('Helpdesk staff can use this role if their institutional type is one of: [_1].',
                   9664:                                                       $newsettings{$role}{'status'}));
                   9665:                                     } else {
                   9666:                                         $r->print(&mt('Helpdesk staff can use this role if their institutional type is: [_1].',
                   9667:                                                       $newsettings{$role}{'status'}));
                   9668:                                     }
                   9669:                                 } else {
                   9670:                                     $r->print(&mt('No helpdesk staff can access '.lc($crstype).' with this role.'));
                   9671:                                 }
                   9672:                             } elsif ($newsettings{$role}{'access'} eq 'exc') {
                   9673:                                 if ($newsettings{$role}{'exc'}) {
                   9674:                                     $r->print(&mt('Helpdesk staff who can use this role are as follows:').' '.$newsettings{$role}{'exc'}.'.');
                   9675:                                 } else {
                   9676:                                     $r->print(&mt('No helpdesk staff can access '.lc($crstype).' with this role.'));
                   9677:                                 }
                   9678:                             } elsif ($newsettings{$role}{'access'} eq 'inc') {
                   9679:                                 if ($newsettings{$role}{'inc'}) {
                   9680:                                     $r->print(&mt('All helpdesk staff may use this role except the following:').' '.$newsettings{$role}{'inc'}.'.');
                   9681:                                 } else {
                   9682:                                     $r->print(&mt('All helpdesk staff may use this role.'));
                   9683:                                 }
                   9684:                             }
                   9685:                         } else {
                   9686:                             $r->print(&mt('Default access set in the domain now applies.').'<br />'.
                   9687:                                       '<span class="LC_cusr_emph">'.$domusage{$role}.'</span>');
                   9688:                         }
                   9689:                         $r->print('</li>');
                   9690:                     }
                   9691:                     unless ($newsettings{$role}{'access'} eq 'none') {
                   9692:                         if ($changed{$role}{'off'}) {
                   9693:                             if ($newsettings{$role}{'off'}) {
                   9694:                                 $r->print('<li>'.&mt('Privileges which are available by default for this ad hoc role, but are disabled for this specific '.lc($crstype).':').
                   9695:                                           '<ul><li>'.$newsettings{$role}{'off'}.'</li></ul></li>');
                   9696:                             } else {
1.430     raeburn  9697:                                 $r->print('<li>'.&mt('All privileges available by default for this ad hoc role are enabled.').'</li>');
1.428     raeburn  9698:                             }
                   9699:                         }
1.430     raeburn  9700:                         if ($changed{$role}{'on'}) {
1.428     raeburn  9701:                             if ($newsettings{$role}{'on'}) {
                   9702:                                 $r->print('<li>'.&mt('Privileges which are not available by default for this ad hoc role, but are enabled for this specific '.lc($crstype).':').
                   9703:                                           '<ul><li>'.$newsettings{$role}{'on'}.'</li></ul></li>');
                   9704:                             } else {
1.430     raeburn  9705:                                 $r->print('<li>'.&mt('None of the privileges unavailable by default for this ad hoc role are enabled.').'</li>');
1.428     raeburn  9706:                             }
                   9707:                         }
                   9708:                     }
                   9709:                     $r->print('</ul></li>');
                   9710:                 }
                   9711:                 $r->print('</ul>');
                   9712:             }
                   9713:         } else {
1.430     raeburn  9714:             $r->print(&mt('No changes made to helpdesk access settings.'));
1.428     raeburn  9715:         }
                   9716:     }
                   9717:     return;
                   9718: }
                   9719: 
1.27      matthew  9720: #-------------------------------------------------- functions for &phase_two
1.160     raeburn  9721: sub user_search_result {
1.221     raeburn  9722:     my ($context,$srch) = @_;
1.160     raeburn  9723:     my %allhomes;
                   9724:     my %inst_matches;
                   9725:     my %srch_results;
1.181     raeburn  9726:     my ($response,$currstate,$forcenewuser,$dirsrchres);
1.183     raeburn  9727:     $srch->{'srchterm'} =~ s/\s+/ /g;
1.176     raeburn  9728:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
1.160     raeburn  9729:         $response = &mt('Invalid search.');
                   9730:     }
                   9731:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
                   9732:         $response = &mt('Invalid search.');
                   9733:     }
1.177     raeburn  9734:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
1.160     raeburn  9735:         $response = &mt('Invalid search.');
                   9736:     }
                   9737:     if ($srch->{'srchterm'} eq '') {
                   9738:         $response = &mt('You must enter a search term.');
                   9739:     }
1.183     raeburn  9740:     if ($srch->{'srchterm'} =~ /^\s+$/) {
                   9741:         $response = &mt('Your search term must contain more than just spaces.');
                   9742:     }
1.160     raeburn  9743:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
                   9744:         if (($srch->{'srchdomain'} eq '') || 
1.163     albertel 9745: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
1.160     raeburn  9746:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
                   9747:         }
                   9748:     }
                   9749:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
                   9750:         ($srch->{'srchin'} eq 'alc')) {
1.176     raeburn  9751:         if ($srch->{'srchby'} eq 'uname') {
1.243     raeburn  9752:             my $unamecheck = $srch->{'srchterm'};
                   9753:             if ($srch->{'srchtype'} eq 'contains') {
                   9754:                 if ($unamecheck !~ /^\w/) {
                   9755:                     $unamecheck = 'a'.$unamecheck; 
                   9756:                 }
                   9757:             }
                   9758:             if ($unamecheck !~ /^$match_username$/) {
1.176     raeburn  9759:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
                   9760:             }
1.160     raeburn  9761:         }
                   9762:     }
1.180     raeburn  9763:     if ($response ne '') {
1.413     raeburn  9764:         $response = '<span class="LC_warning">'.$response.'</span><br />';
1.180     raeburn  9765:     }
1.160     raeburn  9766:     if ($srch->{'srchin'} eq 'instd') {
1.412     raeburn  9767:         my $instd_chk = &instdirectorysrch_check($srch);
1.160     raeburn  9768:         if ($instd_chk ne 'ok') {
1.412     raeburn  9769:             my $domd_chk = &domdirectorysrch_check($srch);
1.413     raeburn  9770:             $response .= '<span class="LC_warning">'.$instd_chk.'</span><br />';
1.412     raeburn  9771:             if ($domd_chk eq 'ok') {
1.435     raeburn  9772:                 $response .= &mt('You may want to search in the LON-CAPA domain instead of in the institutional directory.');
1.412     raeburn  9773:             }
1.415     raeburn  9774:             $response .= '<br />';
1.412     raeburn  9775:         }
                   9776:     } else {
1.417     raeburn  9777:         unless (($context eq 'requestcrs') && ($srch->{'srchtype'} eq 'exact')) {
1.412     raeburn  9778:             my $domd_chk = &domdirectorysrch_check($srch);
1.438     raeburn  9779:             if (($domd_chk ne 'ok') && ($env{'form.action'} ne 'accesslogs')) {
1.412     raeburn  9780:                 my $instd_chk = &instdirectorysrch_check($srch);
1.413     raeburn  9781:                 $response .= '<span class="LC_warning">'.$domd_chk.'</span><br />';
1.412     raeburn  9782:                 if ($instd_chk eq 'ok') {
1.435     raeburn  9783:                     $response .= &mt('You may want to search in the institutional directory instead of in the LON-CAPA domain.');
1.412     raeburn  9784:                 }
1.415     raeburn  9785:                 $response .= '<br />';
1.412     raeburn  9786:             }
1.160     raeburn  9787:         }
                   9788:     }
                   9789:     if ($response ne '') {
1.180     raeburn  9790:         return ($currstate,$response);
1.160     raeburn  9791:     }
                   9792:     if ($srch->{'srchby'} eq 'uname') {
                   9793:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
                   9794:             if ($env{'form.forcenew'}) {
                   9795:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
                   9796:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   9797:                     if ($uhome eq 'no_host') {
                   9798:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.180     raeburn  9799:                         my $showdom = &display_domain_info($env{'request.role.domain'});
                   9800:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
1.160     raeburn  9801:                     } else {
1.179     raeburn  9802:                         $currstate = 'modify';
1.160     raeburn  9803:                     }
                   9804:                 } else {
1.179     raeburn  9805:                     $currstate = 'modify';
1.160     raeburn  9806:                 }
                   9807:             } else {
                   9808:                 if ($srch->{'srchin'} eq 'dom') {
1.162     raeburn  9809:                     if ($srch->{'srchtype'} eq 'exact') {
                   9810:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   9811:                         if ($uhome eq 'no_host') {
1.179     raeburn  9812:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  9813:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  9814:                         } else {
1.179     raeburn  9815:                             $currstate = 'modify';
1.416     raeburn  9816:                             if ($env{'form.action'} eq 'accesslogs') {
                   9817:                                 $currstate = 'activity';
                   9818:                             }
1.310     raeburn  9819:                             my $uname = $srch->{'srchterm'};
                   9820:                             my $udom = $srch->{'srchdomain'};
                   9821:                             $srch_results{$uname.':'.$udom} =
                   9822:                                 { &Apache::lonnet::get('environment',
                   9823:                                                        ['firstname',
                   9824:                                                         'lastname',
                   9825:                                                         'permanentemail'],
                   9826:                                                          $udom,$uname)
                   9827:                                 };
1.162     raeburn  9828:                         }
                   9829:                     } else {
                   9830:                         %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  9831:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  9832:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  9833:                     }
                   9834:                 } else {
1.167     albertel 9835:                     my $courseusers = &get_courseusers();
1.162     raeburn  9836:                     if ($srch->{'srchtype'} eq 'exact') {
1.167     albertel 9837:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
1.179     raeburn  9838:                             $currstate = 'modify';
1.162     raeburn  9839:                         } else {
1.179     raeburn  9840:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  9841:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  9842:                         }
1.160     raeburn  9843:                     } else {
1.167     albertel 9844:                         foreach my $user (keys(%$courseusers)) {
1.162     raeburn  9845:                             my ($cuname,$cudomain) = split(/:/,$user);
                   9846:                             if ($cudomain eq $srch->{'srchdomain'}) {
1.177     raeburn  9847:                                 my $matched = 0;
                   9848:                                 if ($srch->{'srchtype'} eq 'begins') {
                   9849:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
                   9850:                                         $matched = 1;
                   9851:                                     }
                   9852:                                 } else {
                   9853:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
                   9854:                                         $matched = 1;
                   9855:                                     }
                   9856:                                 }
                   9857:                                 if ($matched) {
1.167     albertel 9858:                                     $srch_results{$user} = 
                   9859: 					{&Apache::lonnet::get('environment',
                   9860: 							     ['firstname',
                   9861: 							      'lastname',
1.194     albertel 9862: 							      'permanentemail'],
                   9863: 							      $cudomain,$cuname)};
1.162     raeburn  9864:                                 }
                   9865:                             }
                   9866:                         }
1.179     raeburn  9867:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  9868:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  9869:                     }
                   9870:                 }
                   9871:             }
                   9872:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  9873:             $currstate = 'query';
1.160     raeburn  9874:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  9875:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
                   9876:             if ($dirsrchres eq 'ok') {
                   9877:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  9878:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  9879:             } else {
                   9880:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   9881:                 $response = '<span class="LC_warning">'.
                   9882:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   9883:                     '</span><br />'.
1.435     raeburn  9884:                     &mt('You may want to search in the LON-CAPA domain instead of in the institutional directory.').
1.415     raeburn  9885:                     '<br />'; 
1.181     raeburn  9886:             }
1.160     raeburn  9887:         }
                   9888:     } else {
                   9889:         if ($srch->{'srchin'} eq 'dom') {
                   9890:             %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  9891:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  9892:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  9893:         } elsif ($srch->{'srchin'} eq 'crs') {
1.167     albertel 9894:             my $courseusers = &get_courseusers(); 
                   9895:             foreach my $user (keys(%$courseusers)) {
1.160     raeburn  9896:                 my ($uname,$udom) = split(/:/,$user);
                   9897:                 my %names = &Apache::loncommon::getnames($uname,$udom);
                   9898:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
                   9899:                 if ($srch->{'srchby'} eq 'lastname') {
                   9900:                     if ((($srch->{'srchtype'} eq 'exact') && 
                   9901:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
1.177     raeburn  9902:                         (($srch->{'srchtype'} eq 'begins') &&
                   9903:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
1.160     raeburn  9904:                         (($srch->{'srchtype'} eq 'contains') &&
                   9905:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
                   9906:                         $srch_results{$user} = {firstname => $names{'firstname'},
                   9907:                                             lastname => $names{'lastname'},
                   9908:                                             permanentemail => $emails{'permanentemail'},
                   9909:                                            };
                   9910:                     }
                   9911:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
                   9912:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
1.177     raeburn  9913:                     $srchlast =~ s/\s+$//;
                   9914:                     $srchfirst =~ s/^\s+//;
1.160     raeburn  9915:                     if ($srch->{'srchtype'} eq 'exact') {
                   9916:                         if (($names{'lastname'} eq $srchlast) &&
                   9917:                             ($names{'firstname'} eq $srchfirst)) {
                   9918:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   9919:                                                 lastname => $names{'lastname'},
                   9920:                                                 permanentemail => $emails{'permanentemail'},
                   9921: 
                   9922:                                            };
                   9923:                         }
1.177     raeburn  9924:                     } elsif ($srch->{'srchtype'} eq 'begins') {
                   9925:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
                   9926:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
                   9927:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   9928:                                                 lastname => $names{'lastname'},
                   9929:                                                 permanentemail => $emails{'permanentemail'},
                   9930:                                                };
                   9931:                         }
                   9932:                     } else {
1.160     raeburn  9933:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
                   9934:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
                   9935:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   9936:                                                 lastname => $names{'lastname'},
                   9937:                                                 permanentemail => $emails{'permanentemail'},
                   9938:                                                };
                   9939:                         }
                   9940:                     }
                   9941:                 }
                   9942:             }
1.179     raeburn  9943:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  9944:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  9945:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  9946:             $currstate = 'query';
1.160     raeburn  9947:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  9948:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
                   9949:             if ($dirsrchres eq 'ok') {
                   9950:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  9951:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  9952:             } else {
1.412     raeburn  9953:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   9954:                 $response = '<span class="LC_warning">'.
1.181     raeburn  9955:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   9956:                     '</span><br />'.
1.435     raeburn  9957:                     &mt('You may want to search in the LON-CAPA domain instead of in the institutional directory.').
1.415     raeburn  9958:                     '<br />';
1.181     raeburn  9959:             }
1.160     raeburn  9960:         }
                   9961:     }
1.179     raeburn  9962:     return ($currstate,$response,$forcenewuser,\%srch_results);
1.160     raeburn  9963: }
                   9964: 
1.412     raeburn  9965: sub domdirectorysrch_check {
                   9966:     my ($srch) = @_;
                   9967:     my $response;
                   9968:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   9969:                                              ['directorysrch'],$srch->{'srchdomain'});
                   9970:     my $showdom = &display_domain_info($srch->{'srchdomain'});
                   9971:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   9972:         if ($dom_inst_srch{'directorysrch'}{'lcavailable'} eq '0') {
                   9973:             return &mt('LON-CAPA directory search is not available in domain: [_1]',$showdom);
                   9974:         }
                   9975:         if ($dom_inst_srch{'directorysrch'}{'lclocalonly'}) {
                   9976:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
                   9977:                 return &mt('LON-CAPA directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom);
                   9978:             }
                   9979:         }
                   9980:     }
                   9981:     return 'ok';
                   9982: }
                   9983: 
                   9984: sub instdirectorysrch_check {
1.160     raeburn  9985:     my ($srch) = @_;
                   9986:     my $can_search = 0;
                   9987:     my $response;
                   9988:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   9989:                                              ['directorysrch'],$srch->{'srchdomain'});
1.180     raeburn  9990:     my $showdom = &display_domain_info($srch->{'srchdomain'});
1.160     raeburn  9991:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   9992:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
1.180     raeburn  9993:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
1.160     raeburn  9994:         }
                   9995:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
                   9996:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
1.180     raeburn  9997:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
1.160     raeburn  9998:             }
                   9999:             my @usertypes = split(/:/,$env{'environment.inststatus'});
                   10000:             if (!@usertypes) {
                   10001:                 push(@usertypes,'default');
                   10002:             }
                   10003:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
                   10004:                 foreach my $type (@usertypes) {
                   10005:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
                   10006:                         $can_search = 1;
                   10007:                         last;
                   10008:                     }
                   10009:                 }
                   10010:             }
                   10011:             if (!$can_search) {
                   10012:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
                   10013:                 my @longtypes; 
                   10014:                 foreach my $item (@usertypes) {
1.229     raeburn  10015:                     if (defined($insttypes->{$item})) { 
                   10016:                         push (@longtypes,$insttypes->{$item});
                   10017:                     } elsif ($item eq 'default') {
                   10018:                         push (@longtypes,&mt('other')); 
                   10019:                     }
1.160     raeburn  10020:                 }
                   10021:                 my $insttype_str = join(', ',@longtypes); 
1.180     raeburn  10022:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
1.229     raeburn  10023:             }
1.160     raeburn  10024:         } else {
                   10025:             $can_search = 1;
                   10026:         }
                   10027:     } else {
1.180     raeburn  10028:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
1.160     raeburn  10029:     }
                   10030:     my %longtext = &Apache::lonlocal::texthash (
1.167     albertel 10031:                        uname     => 'username',
1.160     raeburn  10032:                        lastfirst => 'last name, first name',
1.167     albertel 10033:                        lastname  => 'last name',
1.172     raeburn  10034:                        contains  => 'contains',
1.178     raeburn  10035:                        exact     => 'as exact match to',
                   10036:                        begins    => 'begins with',
1.160     raeburn  10037:                    );
                   10038:     if ($can_search) {
                   10039:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
                   10040:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
1.180     raeburn  10041:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
1.160     raeburn  10042:             }
                   10043:         } else {
1.180     raeburn  10044:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
1.160     raeburn  10045:         }
                   10046:     }
                   10047:     if ($can_search) {
1.178     raeburn  10048:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
                   10049:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
                   10050:                 return 'ok';
                   10051:             } else {
1.180     raeburn  10052:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  10053:             }
                   10054:         } else {
                   10055:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
                   10056:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
                   10057:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
                   10058:                 return 'ok';
                   10059:             } else {
1.180     raeburn  10060:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  10061:             }
1.160     raeburn  10062:         }
                   10063:     }
                   10064: }
                   10065: 
                   10066: sub get_courseusers {
                   10067:     my %advhash;
1.167     albertel 10068:     my $classlist = &Apache::loncoursedata::get_classlist();
1.160     raeburn  10069:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                   10070:     foreach my $role (sort(keys(%coursepersonnel))) {
                   10071:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
1.167     albertel 10072: 	    if (!exists($classlist->{$user})) {
                   10073: 		$classlist->{$user} = [];
                   10074: 	    }
1.160     raeburn  10075:         }
                   10076:     }
1.167     albertel 10077:     return $classlist;
1.160     raeburn  10078: }
                   10079: 
                   10080: sub build_search_response {
1.221     raeburn  10081:     my ($context,$srch,%srch_results) = @_;
1.179     raeburn  10082:     my ($currstate,$response,$forcenewuser);
1.160     raeburn  10083:     my %names = (
1.330     bisitz   10084:           'uname'     => 'username',
                   10085:           'lastname'  => 'last name',
1.160     raeburn  10086:           'lastfirst' => 'last name, first name',
1.330     bisitz   10087:           'crs'       => 'this course',
                   10088:           'dom'       => 'LON-CAPA domain',
                   10089:           'instd'     => 'the institutional directory for domain',
1.160     raeburn  10090:     );
                   10091: 
                   10092:     my %single = (
1.180     raeburn  10093:                    begins   => 'A match',
1.160     raeburn  10094:                    contains => 'A match',
1.180     raeburn  10095:                    exact    => 'An exact match',
1.160     raeburn  10096:                  );
                   10097:     my %nomatch = (
1.180     raeburn  10098:                    begins   => 'No match',
1.160     raeburn  10099:                    contains => 'No match',
1.180     raeburn  10100:                    exact    => 'No exact match',
1.160     raeburn  10101:                   );
                   10102:     if (keys(%srch_results) > 1) {
1.179     raeburn  10103:         $currstate = 'select';
1.160     raeburn  10104:     } else {
                   10105:         if (keys(%srch_results) == 1) {
1.416     raeburn  10106:             if ($env{'form.action'} eq 'accesslogs') {
                   10107:                 $currstate = 'activity';
                   10108:             } else {
                   10109:                 $currstate = 'modify';
                   10110:             }
1.180     raeburn  10111:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
                   10112:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   10113:                 $response .= ': '.&display_domain_info($srch->{'srchdomain'});
1.180     raeburn  10114:             }
1.330     bisitz   10115:         } else { # Search has nothing found. Prepare message to user.
                   10116:             $response = '<span class="LC_warning">';
1.180     raeburn  10117:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   10118:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}: [_2]",
                   10119:                                  '<b>'.$srch->{'srchterm'}.'</b>',
                   10120:                                  &display_domain_info($srch->{'srchdomain'}));
                   10121:             } else {
                   10122:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}.",
                   10123:                                  '<b>'.$srch->{'srchterm'}.'</b>');
1.180     raeburn  10124:             }
                   10125:             $response .= '</span>';
1.330     bisitz   10126: 
1.160     raeburn  10127:             if ($srch->{'srchin'} ne 'alc') {
                   10128:                 $forcenewuser = 1;
                   10129:                 my $cansrchinst = 0; 
1.438     raeburn  10130:                 if (($srch->{'srchdomain'}) && ($env{'form.action'} ne 'accesslogs')) {
1.160     raeburn  10131:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
                   10132:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
                   10133:                         if ($domconfig{'directorysrch'}{'available'}) {
                   10134:                             $cansrchinst = 1;
                   10135:                         } 
                   10136:                     }
                   10137:                 }
1.180     raeburn  10138:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
                   10139:                      ($srch->{'srchby'} eq 'lastname')) &&
                   10140:                     ($srch->{'srchin'} eq 'dom')) {
                   10141:                     if ($cansrchinst) {
                   10142:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
1.160     raeburn  10143:                     }
                   10144:                 }
1.180     raeburn  10145:                 if ($srch->{'srchin'} eq 'crs') {
                   10146:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
                   10147:                 }
                   10148:             }
1.305     raeburn  10149:             my $createdom = $env{'request.role.domain'};
                   10150:             if ($context eq 'requestcrs') {
                   10151:                 if ($env{'form.coursedom'} ne '') {
                   10152:                     $createdom = $env{'form.coursedom'};
                   10153:                 }
                   10154:             }
1.416     raeburn  10155:             unless (($env{'form.action'} eq 'accesslogs') || (($srch->{'srchby'} eq 'uname') && ($srch->{'srchin'} eq 'dom') &&
                   10156:                     ($srch->{'srchtype'} eq 'exact') && ($srch->{'srchdomain'} eq $createdom))) {
1.221     raeburn  10157:                 my $cancreate =
1.305     raeburn  10158:                     &Apache::lonuserutils::can_create_user($createdom,$context);
                   10159:                 my $targetdom = '<span class="LC_cusr_emph">'.$createdom.'</span>';
1.221     raeburn  10160:                 if ($cancreate) {
1.305     raeburn  10161:                     my $showdom = &display_domain_info($createdom); 
1.266     bisitz   10162:                     $response .= '<br /><br />'
                   10163:                                 .'<b>'.&mt('To add a new user:').'</b>'
1.305     raeburn  10164:                                 .'<br />';
                   10165:                     if ($context eq 'requestcrs') {
                   10166:                         $response .= &mt("(You can only define new users in the new course's domain - [_1])",$targetdom);
                   10167:                     } else {
                   10168:                         $response .= &mt("(You can only create new users in your current role's domain - [_1])",$targetdom);
                   10169:                     }
                   10170:                     $response .='<ul><li>'
1.266     bisitz   10171:                                 .&mt("Set 'Domain/institution to search' to: [_1]",'<span class="LC_cusr_emph">'.$showdom.'</span>')
                   10172:                                 .'</li><li>'
                   10173:                                 .&mt("Set 'Search criteria' to: [_1]username is ..... in selected LON-CAPA domain[_2]",'<span class="LC_cusr_emph">','</span>')
                   10174:                                 .'</li><li>'
                   10175:                                 .&mt('Provide the proposed username')
                   10176:                                 .'</li><li>'
                   10177:                                 .&mt("Click 'Search'")
                   10178:                                 .'</li></ul><br />';
1.221     raeburn  10179:                 } else {
1.422     raeburn  10180:                     unless (($context eq 'domain') && ($env{'form.action'} eq 'singleuser')) {
                   10181:                         my $helplink = ' href="javascript:helpMenu('."'display'".')"';
                   10182:                         $response .= '<br /><br />';
                   10183:                         if ($context eq 'requestcrs') {
                   10184:                             $response .= &mt("You are not authorized to define new users in the new course's domain - [_1].",$targetdom);
                   10185:                         } else {
                   10186:                             $response .= &mt("You are not authorized to create new users in your current role's domain - [_1].",$targetdom);
                   10187:                         }
                   10188:                         $response .= '<br />'
                   10189:                                      .&mt('Please contact the [_1]helpdesk[_2] if you need to create a new user.'
                   10190:                                         ,' <a'.$helplink.'>'
                   10191:                                         ,'</a>')
                   10192:                                      .'<br />';
1.305     raeburn  10193:                     }
1.221     raeburn  10194:                 }
1.160     raeburn  10195:             }
                   10196:         }
                   10197:     }
1.179     raeburn  10198:     return ($currstate,$response,$forcenewuser);
1.160     raeburn  10199: }
                   10200: 
1.180     raeburn  10201: sub display_domain_info {
                   10202:     my ($dom) = @_;
                   10203:     my $output = $dom;
                   10204:     if ($dom ne '') { 
                   10205:         my $domdesc = &Apache::lonnet::domain($dom,'description');
                   10206:         if ($domdesc ne '') {
                   10207:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
                   10208:         }
                   10209:     }
                   10210:     return $output;
                   10211: }
                   10212: 
1.160     raeburn  10213: sub crumb_utilities {
                   10214:     my %elements = (
                   10215:        crtuser => {
                   10216:            srchterm => 'text',
1.172     raeburn  10217:            srchin => 'selectbox',
1.160     raeburn  10218:            srchby => 'selectbox',
                   10219:            srchtype => 'selectbox',
                   10220:            srchdomain => 'selectbox',
                   10221:        },
1.207     raeburn  10222:        crtusername => {
                   10223:            srchterm => 'text',
                   10224:            srchdomain => 'selectbox',
                   10225:        },
1.160     raeburn  10226:        docustom => {
                   10227:            rolename => 'selectbox',
                   10228:            newrolename => 'textbox',
                   10229:        },
1.179     raeburn  10230:        studentform => {
                   10231:            srchterm => 'text',
                   10232:            srchin => 'selectbox',
                   10233:            srchby => 'selectbox',
                   10234:            srchtype => 'selectbox',
                   10235:            srchdomain => 'selectbox',
                   10236:        },
1.160     raeburn  10237:     );
                   10238: 
                   10239:     my $jsback .= qq|
                   10240: function backPage(formname,prevphase,prevstate) {
1.211     raeburn  10241:     if (typeof prevphase == 'undefined') {
                   10242:         formname.phase.value = '';
                   10243:     }
                   10244:     else {  
                   10245:         formname.phase.value = prevphase;
                   10246:     }
                   10247:     if (typeof prevstate == 'undefined') {
                   10248:         formname.currstate.value = '';
                   10249:     }
                   10250:     else {
                   10251:         formname.currstate.value = prevstate;
                   10252:     }
1.160     raeburn  10253:     formname.submit();
                   10254: }
                   10255: |;
                   10256:     return ($jsback,\%elements);
                   10257: }
                   10258: 
1.26      matthew  10259: sub course_level_table {
1.375     raeburn  10260:     my ($inccourses,$showcredits,$defaultcredits) = @_;
                   10261:     return unless (ref($inccourses) eq 'HASH');
1.26      matthew  10262:     my $table = '';
1.62      www      10263: # Custom Roles?
                   10264: 
1.190     raeburn  10265:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.89      raeburn  10266:     my %lt=&Apache::lonlocal::texthash(
                   10267:             'exs'  => "Existing sections",
                   10268:             'new'  => "Define new section",
                   10269:             'ssd'  => "Set Start Date",
                   10270:             'sed'  => "Set End Date",
1.131     raeburn  10271:             'crl'  => "Course Level",
1.89      raeburn  10272:             'act'  => "Activate",
                   10273:             'rol'  => "Role",
                   10274:             'ext'  => "Extent",
1.113     raeburn  10275:             'grs'  => "Section",
1.375     raeburn  10276:             'crd'  => "Credits",
1.89      raeburn  10277:             'sta'  => "Start",
                   10278:             'end'  => "End"
                   10279:     );
1.62      www      10280: 
1.375     raeburn  10281:     foreach my $protectedcourse (sort(keys(%{$inccourses}))) {
1.135     raeburn  10282: 	my $thiscourse=$protectedcourse;
1.26      matthew  10283: 	$thiscourse=~s:_:/:g;
                   10284: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
1.365     raeburn  10285:         my $isowner = &Apache::lonuserutils::is_courseowner($protectedcourse,$coursedata{'internal.courseowner'});
1.26      matthew  10286: 	my $area=$coursedata{'description'};
1.321     raeburn  10287:         my $crstype=$coursedata{'type'};
1.135     raeburn  10288: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
1.89      raeburn  10289: 	my ($domain,$cnum)=split(/\//,$thiscourse);
1.115     albertel 10290:         my %sections_count;
1.101     albertel 10291:         if (defined($env{'request.course.id'})) {
                   10292:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
1.115     albertel 10293:                 %sections_count = 
                   10294: 		    &Apache::loncommon::get_sections($domain,$cnum);
1.92      raeburn  10295:             }
                   10296:         }
1.321     raeburn  10297:         my @roles = &Apache::lonuserutils::roles_by_context('course','',$crstype);
1.213     raeburn  10298: 	foreach my $role (@roles) {
1.321     raeburn  10299:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.329     raeburn  10300: 	    if ((&Apache::lonnet::allowed('c'.$role,$thiscourse)) ||
                   10301:                 ((($role eq 'cc') || ($role eq 'co')) && ($isowner))) {
1.221     raeburn  10302:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  10303:                                             $plrole,\%sections_count,\%lt,
1.402     raeburn  10304:                                             $showcredits,$defaultcredits,$crstype);
1.221     raeburn  10305:             } elsif ($env{'request.course.sec'} ne '') {
                   10306:                 if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
                   10307:                                              $env{'request.course.sec'})) {
                   10308:                     $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  10309:                                                 $plrole,\%sections_count,\%lt,
1.402     raeburn  10310:                                                 $showcredits,$defaultcredits,$crstype);
1.26      matthew  10311:                 }
                   10312:             }
                   10313:         }
1.221     raeburn  10314:         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
1.324     raeburn  10315:             foreach my $cust (sort(keys(%customroles))) {
                   10316:                 next if ($crstype eq 'Community' && $customroles{$cust} =~ /bre\&S/);
1.221     raeburn  10317:                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
                   10318:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.402     raeburn  10319:                                             $cust,\%sections_count,\%lt,
                   10320:                                             $showcredits,$defaultcredits,$crstype);
1.221     raeburn  10321:             }
1.62      www      10322: 	}
1.26      matthew  10323:     }
                   10324:     return '' if ($table eq ''); # return nothing if there is nothing 
                   10325:                                  # in the table
1.188     raeburn  10326:     my $result;
                   10327:     if (!$env{'request.course.id'}) {
                   10328:         $result = '<h4>'.$lt{'crl'}.'</h4>'."\n";
                   10329:     }
                   10330:     $result .= 
1.136     raeburn  10331: &Apache::loncommon::start_data_table().
                   10332: &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  10333: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
1.402     raeburn  10334: '<th>'.$lt{'ext'}.'</th><th>'."\n";
                   10335:     if ($showcredits) {
                   10336:         $result .= $lt{'crd'}.'</th>';
                   10337:     }
                   10338:     $result .=
1.375     raeburn  10339: '<th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th>'."\n".
                   10340: '<th>'.$lt{'end'}.'</th>'.
1.136     raeburn  10341: &Apache::loncommon::end_data_table_header_row().
                   10342: $table.
                   10343: &Apache::loncommon::end_data_table();
1.26      matthew  10344:     return $result;
                   10345: }
1.88      raeburn  10346: 
1.221     raeburn  10347: sub course_level_row {
1.375     raeburn  10348:     my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,
1.402     raeburn  10349:         $lt,$showcredits,$defaultcredits,$crstype) = @_;
1.375     raeburn  10350:     my $creditem;
1.222     raeburn  10351:     my $row = &Apache::loncommon::start_data_table_row().
                   10352:               ' <td><input type="checkbox" name="act_'.
                   10353:               $protectedcourse.'_'.$role.'" /></td>'."\n".
                   10354:               ' <td>'.$plrole.'</td>'."\n".
                   10355:               ' <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
1.402     raeburn  10356:     if (($showcredits) && ($role eq 'st') && ($crstype eq 'Course')) {
1.375     raeburn  10357:         $row .= 
                   10358:             '<td><input type="text" name="credits_'.$protectedcourse.'_'.
                   10359:             $role.'" size="3" value="'.$defaultcredits.'" /></td>';
                   10360:     } else {
                   10361:         $row .= '<td>&nbsp;</td>';
                   10362:     }
1.322     raeburn  10363:     if (($role eq 'cc') || ($role eq 'co')) {
1.222     raeburn  10364:         $row .= '<td>&nbsp;</td>';
1.221     raeburn  10365:     } elsif ($env{'request.course.sec'} ne '') {
1.222     raeburn  10366:         $row .= ' <td><input type="hidden" value="'.
                   10367:                 $env{'request.course.sec'}.'" '.
                   10368:                 'name="sec_'.$protectedcourse.'_'.$role.'" />'.
                   10369:                 $env{'request.course.sec'}.'</td>';
1.221     raeburn  10370:     } else {
                   10371:         if (ref($sections_count) eq 'HASH') {
                   10372:             my $currsec = 
                   10373:                 &Apache::lonuserutils::course_sections($sections_count,
                   10374:                                                        $protectedcourse.'_'.$role);
1.222     raeburn  10375:             $row .= '<td><table class="LC_createuser">'."\n".
                   10376:                     '<tr class="LC_section_row">'."\n".
                   10377:                     ' <td valign="top">'.$lt->{'exs'}.'<br />'.
                   10378:                        $currsec.'</td>'."\n".
                   10379:                      ' <td>&nbsp;&nbsp;</td>'."\n".
                   10380:                      ' <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
1.221     raeburn  10381:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
                   10382:                      '" value="" />'.
                   10383:                      '<input type="hidden" '.
                   10384:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
1.222     raeburn  10385:                      '</tr></table></td>'."\n";
1.221     raeburn  10386:         } else {
1.222     raeburn  10387:             $row .= '<td><input type="text" size="10" '.
1.375     raeburn  10388:                     'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n";
1.221     raeburn  10389:         }
                   10390:     }
1.222     raeburn  10391:     $row .= <<ENDTIMEENTRY;
                   10392: <td><input type="hidden" name="start_$protectedcourse\_$role" value="" />
1.221     raeburn  10393: <a href=
                   10394: "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  10395: <td><input type="hidden" name="end_$protectedcourse\_$role" value="" />
1.221     raeburn  10396: <a href=
                   10397: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
                   10398: ENDTIMEENTRY
1.222     raeburn  10399:     $row .= &Apache::loncommon::end_data_table_row();
                   10400:     return $row;
1.221     raeburn  10401: }
                   10402: 
1.88      raeburn  10403: sub course_level_dc {
1.375     raeburn  10404:     my ($dcdom,$showcredits) = @_;
1.190     raeburn  10405:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.213     raeburn  10406:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.88      raeburn  10407:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
                   10408:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
1.133     raeburn  10409:                       '<input type="hidden" name="dccourse" value="" />';
1.355     www      10410:     my $courseform=&Apache::loncommon::selectcourse_link
1.356     raeburn  10411:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Select','crstype');
1.375     raeburn  10412:     my $credit_elem;
                   10413:     if ($showcredits) {
                   10414:         $credit_elem = 'credits';
                   10415:     }
                   10416:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu','role','Course/Community Browser',$credit_elem);
1.88      raeburn  10417:     my %lt=&Apache::lonlocal::texthash(
                   10418:                     'rol'  => "Role",
1.113     raeburn  10419:                     'grs'  => "Section",
1.88      raeburn  10420:                     'exs'  => "Existing sections",
                   10421:                     'new'  => "Define new section", 
                   10422:                     'sta'  => "Start",
                   10423:                     'end'  => "End",
                   10424:                     'ssd'  => "Set Start Date",
1.355     www      10425:                     'sed'  => "Set End Date",
1.375     raeburn  10426:                     'scc'  => "Course/Community",
                   10427:                     'crd'  => "Credits",
1.88      raeburn  10428:                   );
1.323     raeburn  10429:     my $header = '<h4>'.&mt('Course/Community Level').'</h4>'.
1.136     raeburn  10430:                  &Apache::loncommon::start_data_table().
                   10431:                  &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  10432:                  '<th>'.$lt{'scc'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
1.397     bisitz   10433:                  '<th>'.$lt{'grs'}.'</th>'."\n";
                   10434:     $header .=   '<th>'.$lt{'crd'}.'</th>'."\n" if ($showcredits);
                   10435:     $header .=   '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'."\n".
1.136     raeburn  10436:                  &Apache::loncommon::end_data_table_header_row();
1.143     raeburn  10437:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
1.356     raeburn  10438:                      '<td><br /><span class="LC_nobreak"><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc','','','','crstype'".')" />'.
                   10439:                      $courseform.('&nbsp;' x4).'</span></td>'."\n".
1.389     bisitz   10440:                      '<td valign="top"><br /><select name="role">'."\n";
1.213     raeburn  10441:     foreach my $role (@roles) {
1.135     raeburn  10442:         my $plrole=&Apache::lonnet::plaintext($role);
1.389     bisitz   10443:         $otheritems .= '  <option value="'.$role.'">'.$plrole.'</option>';
1.88      raeburn  10444:     }
1.404     raeburn  10445:     if ( keys(%customroles) > 0) {
                   10446:         foreach my $cust (sort(keys(%customroles))) {
1.101     albertel 10447:             my $custrole='cr_cr_'.$env{'user.domain'}.
1.135     raeburn  10448:                     '_'.$env{'user.name'}.'_'.$cust;
1.389     bisitz   10449:             $otheritems .= '  <option value="'.$custrole.'">'.$cust.'</option>';
1.88      raeburn  10450:         }
                   10451:     }
                   10452:     $otheritems .= '</select></td><td>'.
                   10453:                      '<table border="0" cellspacing="0" cellpadding="0">'.
                   10454:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
1.389     bisitz   10455:                      ' <option value="">&lt;--'.&mt('Pick course first').'</option></select></td>'.
1.88      raeburn  10456:                      '<td>&nbsp;&nbsp;</td>'.
                   10457:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
1.113     raeburn  10458:                      '<input type="text" name="newsec" value="" />'.
1.237     raeburn  10459:                      '<input type="hidden" name="section" value="" />'.
1.323     raeburn  10460:                      '<input type="hidden" name="groups" value="" />'.
                   10461:                      '<input type="hidden" name="crstype" value="" /></td>'.
1.375     raeburn  10462:                      '</tr></table></td>'."\n";
                   10463:     if ($showcredits) {
                   10464:         $otheritems .= '<td><br />'."\n".
1.397     bisitz   10465:                        '<input type="text" size="3" name="credits" value="" /></td>'."\n";
1.375     raeburn  10466:     }
1.88      raeburn  10467:     $otheritems .= <<ENDTIMEENTRY;
1.323     raeburn  10468: <td><br /><input type="hidden" name="start" value='' />
1.88      raeburn  10469: <a href=
                   10470: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.323     raeburn  10471: <td><br /><input type="hidden" name="end" value='' />
1.88      raeburn  10472: <a href=
                   10473: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
                   10474: ENDTIMEENTRY
1.136     raeburn  10475:     $otheritems .= &Apache::loncommon::end_data_table_row().
                   10476:                    &Apache::loncommon::end_data_table()."\n";
1.470     raeburn  10477:     return $cb_jscript.$hiddenitems.$header.$otheritems;
1.88      raeburn  10478: }
                   10479: 
1.237     raeburn  10480: sub update_selfenroll_config {
1.400     raeburn  10481:     my ($r,$cid,$cdom,$cnum,$context,$crstype,$currsettings) = @_;
1.398     raeburn  10482:     return unless (ref($currsettings) eq 'HASH');
                   10483:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
                   10484:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
1.237     raeburn  10485:     my (%changes,%warning);
1.241     raeburn  10486:     my $curr_types;
1.400     raeburn  10487:     my %noedit;
                   10488:     unless ($context eq 'domain') {
                   10489:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
                   10490:     }
1.237     raeburn  10491:     if (ref($row) eq 'ARRAY') {
                   10492:         foreach my $item (@{$row}) {
1.400     raeburn  10493:             next if ($noedit{$item});
1.237     raeburn  10494:             if ($item eq 'enroll_dates') {
                   10495:                 my (%currenrolldate,%newenrolldate);
                   10496:                 foreach my $type ('start','end') {
1.398     raeburn  10497:                     $currenrolldate{$type} = $currsettings->{'selfenroll_'.$type.'_date'};
1.237     raeburn  10498:                     $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
                   10499:                     if ($newenrolldate{$type} ne $currenrolldate{$type}) {
                   10500:                         $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
                   10501:                     }
                   10502:                 }
                   10503:             } elsif ($item eq 'access_dates') {
                   10504:                 my (%currdate,%newdate);
                   10505:                 foreach my $type ('start','end') {
1.398     raeburn  10506:                     $currdate{$type} = $currsettings->{'selfenroll_'.$type.'_access'};
1.237     raeburn  10507:                     $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
                   10508:                     if ($newdate{$type} ne $currdate{$type}) {
                   10509:                         $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
                   10510:                     }
                   10511:                 }
1.241     raeburn  10512:             } elsif ($item eq 'types') {
1.398     raeburn  10513:                 $curr_types = $currsettings->{'selfenroll_'.$item};
1.241     raeburn  10514:                 if ($env{'form.selfenroll_all'}) {
                   10515:                     if ($curr_types ne '*') {
                   10516:                         $changes{'internal.selfenroll_types'} = '*';
                   10517:                     } else {
                   10518:                         next;
                   10519:                     }
                   10520:                 } else {
1.249     raeburn  10521:                     my %currdoms;
1.241     raeburn  10522:                     my @entries = split(/;/,$curr_types);
                   10523:                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
1.249     raeburn  10524:                     my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
1.241     raeburn  10525:                     my $newnum = 0;
1.249     raeburn  10526:                     my @latesttypes;
                   10527:                     foreach my $num (@activations) {
                   10528:                         my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
                   10529:                         if (@types > 0) {
1.241     raeburn  10530:                             @types = sort(@types);
                   10531:                             my $typestr = join(',',@types);
1.249     raeburn  10532:                             my $typedom = $env{'form.selfenroll_dom_'.$num};
                   10533:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   10534:                             $currdoms{$typedom} = 1;
1.241     raeburn  10535:                             $newnum ++;
                   10536:                         }
                   10537:                     }
1.338     raeburn  10538:                     for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {
                   10539:                         if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
1.249     raeburn  10540:                             my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
                   10541:                             if (@types > 0) {
                   10542:                                 @types = sort(@types);
                   10543:                                 my $typestr = join(',',@types);
                   10544:                                 my $typedom = $env{'form.selfenroll_dom_'.$j};
                   10545:                                 $latesttypes[$newnum] = $typedom.':'.$typestr;
                   10546:                                 $currdoms{$typedom} = 1;
                   10547:                                 $newnum ++;
                   10548:                             }
                   10549:                         }
                   10550:                     }
                   10551:                     if ($env{'form.selfenroll_newdom'} ne '') {
                   10552:                         my $typedom = $env{'form.selfenroll_newdom'};
                   10553:                         if ((!defined($currdoms{$typedom})) && 
                   10554:                             (&Apache::lonnet::domain($typedom) ne '')) {
                   10555:                             my $typestr;
                   10556:                             my ($othertitle,$usertypes,$types) = 
                   10557:                                 &Apache::loncommon::sorted_inst_types($typedom);
                   10558:                             my $othervalue = 'any';
                   10559:                             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   10560:                                 if (@{$types} > 0) {
1.257     raeburn  10561:                                     my @esc_types = map { &escape($_); } @{$types};
1.249     raeburn  10562:                                     $othervalue = 'other';
1.258     raeburn  10563:                                     $typestr = join(',',(@esc_types,$othervalue));
1.249     raeburn  10564:                                 }
                   10565:                                 $typestr = $othervalue;
                   10566:                             } else {
                   10567:                                 $typestr = $othervalue;
                   10568:                             } 
                   10569:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   10570:                             $newnum ++ ;
                   10571:                         }
                   10572:                     }
1.241     raeburn  10573:                     my $selfenroll_types = join(';',@latesttypes);
                   10574:                     if ($selfenroll_types ne $curr_types) {
                   10575:                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
                   10576:                     }
                   10577:                 }
1.276     raeburn  10578:             } elsif ($item eq 'limit') {
                   10579:                 my $newlimit = $env{'form.selfenroll_limit'};
                   10580:                 my $newcap = $env{'form.selfenroll_cap'};
                   10581:                 $newcap =~s/\s+//g;
1.398     raeburn  10582:                 my $currlimit =  $currsettings->{'selfenroll_limit'};
1.276     raeburn  10583:                 $currlimit = 'none' if ($currlimit eq '');
1.398     raeburn  10584:                 my $currcap = $currsettings->{'selfenroll_cap'};
1.276     raeburn  10585:                 if ($newlimit ne $currlimit) {
                   10586:                     if ($newlimit ne 'none') {
                   10587:                         if ($newcap =~ /^\d+$/) {
                   10588:                             if ($newcap ne $currcap) {
                   10589:                                 $changes{'internal.selfenroll_cap'} = $newcap;
                   10590:                             }
                   10591:                             $changes{'internal.selfenroll_limit'} = $newlimit;
                   10592:                         } else {
1.398     raeburn  10593:                             $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
                   10594:                                 &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.'); 
1.276     raeburn  10595:                         }
                   10596:                     } elsif ($currcap ne '') {
                   10597:                         $changes{'internal.selfenroll_cap'} = '';
                   10598:                         $changes{'internal.selfenroll_limit'} = $newlimit; 
                   10599:                     }
                   10600:                 } elsif ($currlimit ne 'none') {
                   10601:                     if ($newcap =~ /^\d+$/) {
                   10602:                         if ($newcap ne $currcap) {
                   10603:                             $changes{'internal.selfenroll_cap'} = $newcap;
                   10604:                         }
                   10605:                     } else {
1.398     raeburn  10606:                         $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
                   10607:                             &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.');
1.276     raeburn  10608:                     }
                   10609:                 }
                   10610:             } elsif ($item eq 'approval') {
                   10611:                 my (@currnotified,@newnotified);
1.398     raeburn  10612:                 my $currapproval = $currsettings->{'selfenroll_approval'};
                   10613:                 my $currnotifylist = $currsettings->{'selfenroll_notifylist'};
1.276     raeburn  10614:                 if ($currnotifylist ne '') {
                   10615:                     @currnotified = split(/,/,$currnotifylist);
                   10616:                     @currnotified = sort(@currnotified);
                   10617:                 }
                   10618:                 my $newapproval = $env{'form.selfenroll_approval'};
                   10619:                 @newnotified = &Apache::loncommon::get_env_multiple('form.selfenroll_notify');
                   10620:                 @newnotified = sort(@newnotified);
                   10621:                 if ($newapproval ne $currapproval) {
                   10622:                     $changes{'internal.selfenroll_approval'} = $newapproval;
                   10623:                     if (!$newapproval) {
                   10624:                         if ($currnotifylist ne '') {
                   10625:                             $changes{'internal.selfenroll_notifylist'} = '';
                   10626:                         }
                   10627:                     } else {
                   10628:                         my @differences =  
1.295     raeburn  10629:                             &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  10630:                         if (@differences > 0) {
                   10631:                             if (@newnotified > 0) {
                   10632:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   10633:                             } else {
                   10634:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   10635:                             }
                   10636:                         }
                   10637:                     }
                   10638:                 } else {
1.295     raeburn  10639:                     my @differences = &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  10640:                     if (@differences > 0) {
                   10641:                         if (@newnotified > 0) {
                   10642:                             $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   10643:                         } else {
                   10644:                             $changes{'internal.selfenroll_notifylist'} = '';
                   10645:                         }
                   10646:                     }
                   10647:                 }
1.237     raeburn  10648:             } else {
1.398     raeburn  10649:                 my $curr_val = $currsettings->{'selfenroll_'.$item};
1.237     raeburn  10650:                 my $newval = $env{'form.selfenroll_'.$item};
                   10651:                 if ($item eq 'section') {
                   10652:                     $newval = $env{'form.sections'};
1.241     raeburn  10653:                     if (defined($curr_groups{$newval})) {
1.237     raeburn  10654:                         $newval = $curr_val;
1.398     raeburn  10655:                         $warning{$item} = &mt('Section for self-enrolled users unchanged as the proposed section is a group').'<br />'.
                   10656:                                           &mt('Group names and section names must be distinct');
1.237     raeburn  10657:                     } elsif ($newval eq 'all') {
                   10658:                         $newval = $curr_val;
1.274     bisitz   10659:                         $warning{$item} = &mt('Section for self-enrolled users unchanged, as "all" is a reserved section name.');
1.237     raeburn  10660:                     }
                   10661:                     if ($newval eq '') {
                   10662:                         $newval = 'none';
                   10663:                     }
                   10664:                 }
                   10665:                 if ($newval ne $curr_val) {
                   10666:                     $changes{'internal.selfenroll_'.$item} = $newval;
                   10667:                 }
1.241     raeburn  10668:             }
1.237     raeburn  10669:         }
                   10670:         if (keys(%warning) > 0) {
                   10671:             foreach my $item (@{$row}) {
                   10672:                 if (exists($warning{$item})) {
                   10673:                     $r->print($warning{$item}.'<br />');
                   10674:                 }
                   10675:             } 
                   10676:         }
                   10677:         if (keys(%changes) > 0) {
                   10678:             my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
                   10679:             if ($putresult eq 'ok') {
                   10680:                 if ((exists($changes{'internal.selfenroll_types'})) ||
                   10681:                     (exists($changes{'internal.selfenroll_start_date'}))  ||
                   10682:                     (exists($changes{'internal.selfenroll_end_date'}))) {
                   10683:                     my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
                   10684:                                                                 $cnum,undef,undef,'Course');
                   10685:                     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
1.398     raeburn  10686:                     if (ref($crsinfo{$cid}) eq 'HASH') {
1.237     raeburn  10687:                         foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
                   10688:                             if (exists($changes{'internal.'.$item})) {
1.398     raeburn  10689:                                 $crsinfo{$cid}{$item} = $changes{'internal.'.$item};
1.237     raeburn  10690:                             }
                   10691:                         }
                   10692:                         my $crsputresult =
                   10693:                             &Apache::lonnet::courseidput($cdom,\%crsinfo,
                   10694:                                                          $chome,'notime');
                   10695:                     }
                   10696:                 }
                   10697:                 $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
                   10698:                 foreach my $item (@{$row}) {
                   10699:                     my $title = $item;
                   10700:                     if (ref($lt) eq 'HASH') {
                   10701:                         $title = $lt->{$item};
                   10702:                     }
                   10703:                     if ($item eq 'enroll_dates') {
                   10704:                         foreach my $type ('start','end') {
                   10705:                             if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
                   10706:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
1.244     bisitz   10707:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  10708:                                           $title,$type,$newdate).'</li>');
                   10709:                             }
                   10710:                         }
                   10711:                     } elsif ($item eq 'access_dates') {
                   10712:                         foreach my $type ('start','end') {
                   10713:                             if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
                   10714:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
1.244     bisitz   10715:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  10716:                                           $title,$type,$newdate).'</li>');
                   10717:                             }
                   10718:                         }
1.276     raeburn  10719:                     } elsif ($item eq 'limit') {
                   10720:                         if ((exists($changes{'internal.selfenroll_limit'})) ||
                   10721:                             (exists($changes{'internal.selfenroll_cap'}))) {
                   10722:                             my ($newval,$newcap);
                   10723:                             if ($changes{'internal.selfenroll_cap'} ne '') {
                   10724:                                 $newcap = $changes{'internal.selfenroll_cap'}
                   10725:                             } else {
1.398     raeburn  10726:                                 $newcap = $currsettings->{'selfenroll_cap'};
1.276     raeburn  10727:                             }
                   10728:                             if ($changes{'internal.selfenroll_limit'} eq 'none') {
                   10729:                                 $newval = &mt('No limit');
                   10730:                             } elsif ($changes{'internal.selfenroll_limit'} eq 
                   10731:                                      'allstudents') {
                   10732:                                 $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   10733:                             } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
                   10734:                                 $newval = &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
                   10735:                             } else {
1.398     raeburn  10736:                                 my $currlimit =  $currsettings->{'selfenroll_limit'};
1.276     raeburn  10737:                                 if ($currlimit eq 'allstudents') {
                   10738:                                     $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   10739:                                 } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
1.308     raeburn  10740:                                     $newval =  &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
1.276     raeburn  10741:                                 }
                   10742:                             }
                   10743:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
                   10744:                         }
                   10745:                     } elsif ($item eq 'approval') {
                   10746:                         if ((exists($changes{'internal.selfenroll_approval'})) ||
                   10747:                             (exists($changes{'internal.selfenroll_notifylist'}))) {
1.398     raeburn  10748:                             my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
1.276     raeburn  10749:                             my ($newval,$newnotify);
                   10750:                             if (exists($changes{'internal.selfenroll_notifylist'})) {
                   10751:                                 $newnotify = $changes{'internal.selfenroll_notifylist'};
                   10752:                             } else {   
1.398     raeburn  10753:                                 $newnotify = $currsettings->{'selfenroll_notifylist'};
1.276     raeburn  10754:                             }
1.398     raeburn  10755:                             if (exists($changes{'internal.selfenroll_approval'})) {
                   10756:                                 if ($changes{'internal.selfenroll_approval'} !~ /^[012]$/) {
                   10757:                                     $changes{'internal.selfenroll_approval'} = '0';
                   10758:                                 }
                   10759:                                 $newval = $selfdescs{'approval'}{$changes{'internal.selfenroll_approval'}};
1.276     raeburn  10760:                             } else {
1.398     raeburn  10761:                                 my $currapproval = $currsettings->{'selfenroll_approval'}; 
                   10762:                                 if ($currapproval !~ /^[012]$/) {
                   10763:                                     $currapproval = 0;
1.276     raeburn  10764:                                 }
1.398     raeburn  10765:                                 $newval = $selfdescs{'approval'}{$currapproval};
1.276     raeburn  10766:                             }
                   10767:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval));
                   10768:                             if ($newnotify) {
1.277     raeburn  10769:                                 $r->print('<br />'.&mt('The following will be notified when an enrollment request needs approval, or has been approved: [_1].',$newnotify));
1.276     raeburn  10770:                             } else {
1.277     raeburn  10771:                                 $r->print('<br />'.&mt('No notifications sent when an enrollment request needs approval, or has been approved.'));
1.276     raeburn  10772:                             }
                   10773:                             $r->print('</li>'."\n");
                   10774:                         }
1.237     raeburn  10775:                     } else {
                   10776:                         if (exists($changes{'internal.selfenroll_'.$item})) {
1.241     raeburn  10777:                             my $newval = $changes{'internal.selfenroll_'.$item};
                   10778:                             if ($item eq 'types') {
                   10779:                                 if ($newval eq '') {
                   10780:                                     $newval = &mt('None');
                   10781:                                 } elsif ($newval eq '*') {
                   10782:                                     $newval = &mt('Any user in any domain');
                   10783:                                 }
1.245     raeburn  10784:                             } elsif ($item eq 'registered') {
                   10785:                                 if ($newval eq '1') {
                   10786:                                     $newval = &mt('Yes');
                   10787:                                 } elsif ($newval eq '0') {
                   10788:                                     $newval = &mt('No');
                   10789:                                 }
1.241     raeburn  10790:                             }
1.244     bisitz   10791:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
1.237     raeburn  10792:                         }
                   10793:                     }
                   10794:                 }
                   10795:                 $r->print('</ul>');
1.398     raeburn  10796:                 if ($env{'course.'.$cid.'.description'} ne '') {
                   10797:                     my %newenvhash;
                   10798:                     foreach my $key (keys(%changes)) {
                   10799:                         $newenvhash{'course.'.$cid.'.'.$key} = $changes{$key};
                   10800:                     }
                   10801:                     &Apache::lonnet::appenv(\%newenvhash);
1.237     raeburn  10802:                 }
                   10803:             } else {
1.398     raeburn  10804:                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.
                   10805:                           &mt('The error was: [_1].',$putresult));
1.237     raeburn  10806:             }
                   10807:         } else {
1.249     raeburn  10808:             $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.237     raeburn  10809:         }
                   10810:     } else {
1.249     raeburn  10811:         $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.241     raeburn  10812:     }
1.469     raeburn  10813:     my $visactions = &cat_visibility($cdom);
1.400     raeburn  10814:     my ($cathash,%cattype);
                   10815:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   10816:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   10817:         $cathash = $domconfig{'coursecategories'}{'cats'};
                   10818:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
                   10819:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
                   10820:     } else {
                   10821:         $cathash = {};
                   10822:         $cattype{'auth'} = 'std';
                   10823:         $cattype{'unauth'} = 'std';
                   10824:     }
                   10825:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
                   10826:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   10827:                   '<br />'.
                   10828:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   10829:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
                   10830:                   '</ul>');
                   10831:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
                   10832:         if ($currsettings->{'uniquecode'}) {
                   10833:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
                   10834:         } else {
1.366     bisitz   10835:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
1.400     raeburn  10836:                   '<br />'.
                   10837:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   10838:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
                   10839:                   '</ul><br />');
                   10840:         }
                   10841:     } else {
                   10842:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
                   10843:         if (ref($visactions) eq 'HASH') {
                   10844:             if (!$visible) {
                   10845:                 $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   10846:                           '<br />');
                   10847:                 if (ref($vismsgs) eq 'ARRAY') {
                   10848:                     $r->print('<br />'.$visactions->{'take'}.'<ul>');
                   10849:                     foreach my $item (@{$vismsgs}) {
                   10850:                         $r->print('<li>'.$visactions->{$item}.'</li>');
                   10851:                     }
                   10852:                     $r->print('</ul>');
1.256     raeburn  10853:                 }
1.400     raeburn  10854:                 $r->print($cansetvis);
1.256     raeburn  10855:             }
                   10856:         }
                   10857:     } 
1.237     raeburn  10858:     return;
                   10859: }
                   10860: 
1.27      matthew  10861: #---------------------------------------------- end functions for &phase_two
1.29      matthew  10862: 
                   10863: #--------------------------------- functions for &phase_two and &phase_three
                   10864: 
                   10865: #--------------------------end of functions for &phase_two and &phase_three
1.372     raeburn  10866: 
1.1       www      10867: 1;
                   10868: __END__
1.2       www      10869: 
                   10870: 

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