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

1.20      harris41    1: # The LearningOnline Network with CAPA
1.1       www         2: # Create a user
                      3: #
1.465   ! raeburn     4: # $Id: loncreateuser.pm,v 1.464 2022/11/17 19:07:21 raeburn Exp $
1.22      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.20      harris41   28: ###
                     29: 
1.1       www        30: package Apache::loncreateuser;
1.66      bowersj2   31: 
                     32: =pod
                     33: 
                     34: =head1 NAME
                     35: 
1.263     jms        36: Apache::loncreateuser.pm
1.66      bowersj2   37: 
                     38: =head1 SYNOPSIS
                     39: 
1.263     jms        40:     Handler to create users and custom roles
                     41: 
                     42:     Provides an Apache handler for creating users,
1.66      bowersj2   43:     editing their login parameters, roles, and removing roles, and
                     44:     also creating and assigning custom roles.
                     45: 
                     46: =head1 OVERVIEW
                     47: 
                     48: =head2 Custom Roles
                     49: 
                     50: In LON-CAPA, roles are actually collections of privileges. "Teaching
                     51: Assistant", "Course Coordinator", and other such roles are really just
                     52: collection of privileges that are useful in many circumstances.
                     53: 
1.324     raeburn    54: Custom roles can be defined by a Domain Coordinator, Course Coordinator
                     55: or Community Coordinator via the Manage User functionality.
                     56: The custom role editor screen will show all privileges which can be
                     57: assigned to users. For a complete list of privileges, please see 
                     58: C</home/httpd/lonTabs/rolesplain.tab>.
1.66      bowersj2   59: 
1.324     raeburn    60: Custom role definitions are stored in the C<roles.db> file of the creator
                     61: of the role.
1.66      bowersj2   62: 
                     63: =cut
1.1       www        64: 
                     65: use strict;
                     66: use Apache::Constants qw(:common :http);
                     67: use Apache::lonnet;
1.54      bowersj2   68: use Apache::loncommon;
1.68      www        69: use Apache::lonlocal;
1.117     raeburn    70: use Apache::longroup;
1.190     raeburn    71: use Apache::lonuserutils;
1.307     raeburn    72: use Apache::loncoursequeueadmin;
1.139     albertel   73: use LONCAPA qw(:DEFAULT :match);
1.456     raeburn    74: use HTML::Entities;
1.1       www        75: 
1.20      harris41   76: my $loginscript; # piece of javascript used in two separate instances
                     77: my $authformnop;
                     78: my $authformkrb;
                     79: my $authformint;
                     80: my $authformfsys;
                     81: my $authformloc;
1.449     raeburn    82: my $authformlti;
1.20      harris41   83: 
1.94      matthew    84: sub initialize_authen_forms {
1.227     raeburn    85:     my ($dom,$formname,$curr_authtype,$mode) = @_;
                     86:     my ($krbdef,$krbdefdom) = &Apache::loncommon::get_kerberos_defaults($dom);
                     87:     my %param = ( formname => $formname,
1.187     raeburn    88:                   kerb_def_dom => $krbdefdom,
1.227     raeburn    89:                   kerb_def_auth => $krbdef,
1.187     raeburn    90:                   domain => $dom,
                     91:                 );
1.188     raeburn    92:     my %abv_auth = &auth_abbrev();
1.449     raeburn    93:     if ($curr_authtype =~ /^(krb4|krb5|internal|localauth|unix|lti):(.*)$/) {
1.188     raeburn    94:         my $long_auth = $1;
1.227     raeburn    95:         my $curr_autharg = $2;
1.188     raeburn    96:         my %abv_auth = &auth_abbrev();
                     97:         $param{'curr_authtype'} = $abv_auth{$long_auth};
                     98:         if ($long_auth =~ /^krb(4|5)$/) {
                     99:             $param{'curr_kerb_ver'} = $1;
1.227     raeburn   100:             $param{'curr_autharg'} = $curr_autharg;
1.188     raeburn   101:         }
1.205     raeburn   102:         if ($mode eq 'modifyuser') {
                    103:             $param{'mode'} = $mode;
                    104:         }
1.187     raeburn   105:     }
1.227     raeburn   106:     $loginscript  = &Apache::loncommon::authform_header(%param);
                    107:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
1.31      matthew   108:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
                    109:     $authformint  = &Apache::loncommon::authform_internal(%param);
                    110:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
                    111:     $authformloc  = &Apache::loncommon::authform_local(%param);
1.449     raeburn   112:     $authformlti  = &Apache::loncommon::authform_lti(%param);
1.20      harris41  113: }
                    114: 
1.188     raeburn   115: sub auth_abbrev {
                    116:     my %abv_auth = (
1.368     raeburn   117:                      krb5      => 'krb',
                    118:                      krb4      => 'krb',
                    119:                      internal  => 'int',
                    120:                      localauth => 'loc',
                    121:                      unix      => 'fsys',
1.449     raeburn   122:                      lti       => 'lti',
1.188     raeburn   123:                    );
                    124:     return %abv_auth;
                    125: }
1.43      www       126: 
1.134     raeburn   127: # ====================================================
                    128: 
1.378     raeburn   129: sub user_quotas {
1.134     raeburn   130:     my ($ccuname,$ccdomain) = @_;
                    131:     my %lt = &Apache::lonlocal::texthash(
1.267     raeburn   132:                    'usrt'      => "User Tools",
                    133:                    'cust'      => "Custom quota",
                    134:                    'chqu'      => "Change quota",
1.134     raeburn   135:     );
1.378     raeburn   136:    
1.149     raeburn   137:     my $quota_javascript = <<"END_SCRIPT";
                    138: <script type="text/javascript">
1.301     bisitz    139: // <![CDATA[
1.378     raeburn   140: function quota_changes(caller,context) {
                    141:     var customoff = document.getElementById('custom_'+context+'quota_off');
                    142:     var customon = document.getElementById('custom_'+context+'quota_on');
                    143:     var number = document.getElementById(context+'quota');
1.149     raeburn   144:     if (caller == "custom") {
1.378     raeburn   145:         if (customoff) {
                    146:             if (customoff.checked) {
                    147:                 number.value = "";
                    148:             }
1.149     raeburn   149:         }
                    150:     }
                    151:     if (caller == "quota") {
1.378     raeburn   152:         if (customon) {
                    153:             customon.checked = true;
                    154:         }
1.149     raeburn   155:     }
1.378     raeburn   156:     return;
1.149     raeburn   157: }
1.301     bisitz    158: // ]]>
1.149     raeburn   159: </script>
                    160: END_SCRIPT
1.378     raeburn   161:     my $longinsttype;
                    162:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
1.267     raeburn   163:     my $output = $quota_javascript."\n".
                    164:                  '<h3>'.$lt{'usrt'}.'</h3>'."\n".
                    165:                  &Apache::loncommon::start_data_table();
                    166: 
1.418     raeburn   167:     if ((&Apache::lonnet::allowed('mut',$ccdomain)) ||
                    168:         (&Apache::lonnet::allowed('udp',$ccdomain))) {
1.275     raeburn   169:         $output .= &build_tools_display($ccuname,$ccdomain,'tools');
1.267     raeburn   170:     }
1.378     raeburn   171: 
                    172:     my %titles = &Apache::lonlocal::texthash (
                    173:                     portfolio => "Disk space allocated to user's portfolio files",
1.385     bisitz    174:                     author    => "Disk space allocated to user's Authoring Space (if role assigned)",
1.378     raeburn   175:                  );
                    176:     foreach my $name ('portfolio','author') {
                    177:         my ($currquota,$quotatype,$inststatus,$defquota) =
                    178:             &Apache::loncommon::get_user_quota($ccuname,$ccdomain,$name);
                    179:         if ($longinsttype eq '') { 
                    180:             if ($inststatus ne '') {
                    181:                 if ($usertypes->{$inststatus} ne '') {
                    182:                     $longinsttype = $usertypes->{$inststatus};
                    183:                 }
                    184:             }
                    185:         }
                    186:         my ($showquota,$custom_on,$custom_off,$defaultinfo);
                    187:         $custom_on = ' ';
                    188:         $custom_off = ' checked="checked" ';
                    189:         if ($quotatype eq 'custom') {
                    190:             $custom_on = $custom_off;
                    191:             $custom_off = ' ';
                    192:             $showquota = $currquota;
                    193:             if ($longinsttype eq '') {
                    194:                 $defaultinfo = &mt('For this user, the default quota would be [_1]'
1.383     raeburn   195:                               .' MB.',$defquota);
1.378     raeburn   196:             } else {
                    197:                 $defaultinfo = &mt("For this user, the default quota would be [_1]".
1.383     raeburn   198:                                    " MB, as determined by the user's institutional".
1.378     raeburn   199:                                    " affiliation ([_2]).",$defquota,$longinsttype);
                    200:             }
                    201:         } else {
                    202:             if ($longinsttype eq '') {
                    203:                 $defaultinfo = &mt('For this user, the default quota is [_1]'
1.383     raeburn   204:                               .' MB.',$defquota);
1.378     raeburn   205:             } else {
                    206:                 $defaultinfo = &mt("For this user, the default quota of [_1]".
1.383     raeburn   207:                                    " MB, is determined by the user's institutional".
1.378     raeburn   208:                                    " affiliation ([_2]).",$defquota,$longinsttype);
                    209:             }
                    210:         }
                    211: 
                    212:         if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
                    213:             $output .= '<tr class="LC_info_row">'."\n".
                    214:                        '    <td>'.$titles{$name}.'</td>'."\n".
                    215:                        '  </tr>'."\n".
                    216:                        &Apache::loncommon::start_data_table_row()."\n".
1.390     bisitz    217:                        '  <td><span class="LC_nobreak">'.
                    218:                        &mt('Current quota: [_1] MB',$currquota).'</span>&nbsp;&nbsp;'.
1.378     raeburn   219:                        $defaultinfo.'</td>'."\n".
                    220:                        &Apache::loncommon::end_data_table_row()."\n".
                    221:                        &Apache::loncommon::start_data_table_row()."\n".
                    222:                        '  <td><span class="LC_nobreak">'.$lt{'chqu'}.
                    223:                        ': <label>'.
                    224:                        '<input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_off" '.
1.379     raeburn   225:                        'value="0" '.$custom_off.' onchange="javascript:quota_changes('."'custom','$name'".');"'.
1.390     bisitz    226:                        ' /><span class="LC_nobreak">'.
                    227:                        &mt('Default ([_1] MB)',$defquota).'</span></label>&nbsp;'.
1.378     raeburn   228:                        '&nbsp;<label><input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_on" '.
1.379     raeburn   229:                        'value="1" '.$custom_on.'  onchange="javascript:quota_changes('."'custom','$name'".');"'.
1.378     raeburn   230:                        ' />'.$lt{'cust'}.':</label>&nbsp;'.
1.379     raeburn   231:                        '<input type="text" name="'.$name.'quota" id="'.$name.'quota" size ="5" '.
                    232:                        'value="'.$showquota.'" onfocus="javascript:quota_changes('."'quota','$name'".');"'.
1.390     bisitz    233:                        ' />&nbsp;'.&mt('MB').'</span></td>'."\n".
1.378     raeburn   234:                        &Apache::loncommon::end_data_table_row()."\n";
                    235:         }
                    236:     }
1.267     raeburn   237:     $output .= &Apache::loncommon::end_data_table();
1.134     raeburn   238:     return $output;
                    239: }
                    240: 
1.275     raeburn   241: sub build_tools_display {
                    242:     my ($ccuname,$ccdomain,$context) = @_;
1.306     raeburn   243:     my (@usertools,%userenv,$output,@options,%validations,%reqtitles,%reqdisplay,
1.332     raeburn   244:         $colspan,$isadv,%domconfig);
1.275     raeburn   245:     my %lt = &Apache::lonlocal::texthash (
                    246:                    'blog'       => "Personal User Blog",
                    247:                    'aboutme'    => "Personal Information Page",
1.385     bisitz    248:                    'webdav'     => "WebDAV access to Authoring Spaces (if SSL and author/co-author)",
1.275     raeburn   249:                    'portfolio'  => "Personal User Portfolio",
1.459     raeburn   250:                    'timezone'   => "Can set Time Zone",
1.275     raeburn   251:                    'avai'       => "Available",
                    252:                    'cusa'       => "availability",
                    253:                    'chse'       => "Change setting",
                    254:                    'usde'       => "Use default",
                    255:                    'uscu'       => "Use custom",
                    256:                    'official'   => 'Can request creation of official courses',
1.299     raeburn   257:                    'unofficial' => 'Can request creation of unofficial courses',
                    258:                    'community'  => 'Can request creation of communities',
1.384     raeburn   259:                    'textbook'   => 'Can request creation of textbook courses',
1.411     raeburn   260:                    'placement'  => 'Can request creation of placement tests',
1.449     raeburn   261:                    'lti'        => 'Can request creation of LTI courses',
1.362     raeburn   262:                    'requestauthor'  => 'Can request author space',
1.275     raeburn   263:     );
1.462     raeburn   264:     $isadv = &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
1.279     raeburn   265:     if ($context eq 'requestcourses') {
1.275     raeburn   266:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.299     raeburn   267:                       'requestcourses.official','requestcourses.unofficial',
1.411     raeburn   268:                       'requestcourses.community','requestcourses.textbook',
1.449     raeburn   269:                       'requestcourses.placement','requestcourses.lti');
                    270:         @usertools = ('official','unofficial','community','textbook','placement','lti');
1.309     raeburn   271:         @options =('norequest','approval','autolimit','validate');
1.306     raeburn   272:         %validations = &Apache::lonnet::auto_courserequest_checks($ccdomain);
                    273:         %reqtitles = &courserequest_titles();
                    274:         %reqdisplay = &courserequest_display();
                    275:         $colspan = ' colspan="2"';
1.332     raeburn   276:         %domconfig =
                    277:             &Apache::lonnet::get_dom('configuration',['requestcourses'],$ccdomain);
1.362     raeburn   278:     } elsif ($context eq 'requestauthor') {
                    279:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    280:                                                     'requestauthor');
                    281:         @usertools = ('requestauthor');
                    282:         @options =('norequest','approval','automatic');
                    283:         %reqtitles = &requestauthor_titles();
                    284:         %reqdisplay = &requestauthor_display();
                    285:         $colspan = ' colspan="2"';
                    286:         %domconfig =
                    287:             &Apache::lonnet::get_dom('configuration',['requestauthor'],$ccdomain);
1.275     raeburn   288:     } else {
                    289:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.361     raeburn   290:                           'tools.aboutme','tools.portfolio','tools.blog',
1.459     raeburn   291:                           'tools.webdav','tools.timezone');
                    292:         @usertools = ('aboutme','blog','webdav','portfolio','timezone');
1.275     raeburn   293:     }
                    294:     foreach my $item (@usertools) {
1.306     raeburn   295:         my ($custom_access,$curr_access,$cust_on,$cust_off,$tool_on,$tool_off,
                    296:             $currdisp,$custdisp,$custradio);
1.275     raeburn   297:         $cust_off = 'checked="checked" ';
                    298:         $tool_on = 'checked="checked" ';
1.463     raeburn   299:         $curr_access =
1.275     raeburn   300:             &Apache::lonnet::usertools_access($ccuname,$ccdomain,$item,undef,
1.462     raeburn   301:                                               $context,\%userenv,'',
                    302:                                               {'is_adv' => $isadv});
1.362     raeburn   303:         if ($context eq 'requestauthor') {
                    304:             if ($userenv{$context} ne '') {
                    305:                 $cust_on = ' checked="checked" ';
                    306:                 $cust_off = '';
                    307:             }  
                    308:         } elsif ($userenv{$context.'.'.$item} ne '') {
1.306     raeburn   309:             $cust_on = ' checked="checked" ';
                    310:             $cust_off = '';
                    311:         }
                    312:         if ($context eq 'requestcourses') {
                    313:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   314:                 $custom_access = &mt('Currently from default setting.');
1.306     raeburn   315:             } else {
                    316:                 $custom_access = &mt('Currently from custom setting.');
1.275     raeburn   317:             }
1.362     raeburn   318:         } elsif ($context eq 'requestauthor') {
                    319:             if ($userenv{$context} eq '') {
                    320:                 $custom_access = &mt('Currently from default setting.');
                    321:             } else {
                    322:                 $custom_access = &mt('Currently from custom setting.');
                    323:             }
1.275     raeburn   324:         } else {
1.306     raeburn   325:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   326:                 $custom_access =
1.306     raeburn   327:                     &mt('Availability determined currently from default setting.');
                    328:                 if (!$curr_access) {
                    329:                     $tool_off = 'checked="checked" ';
                    330:                     $tool_on = '';
                    331:                 }
                    332:             } else {
1.314     raeburn   333:                 $custom_access =
1.306     raeburn   334:                     &mt('Availability determined currently from custom setting.');
                    335:                 if ($userenv{$context.'.'.$item} == 0) {
                    336:                     $tool_off = 'checked="checked" ';
                    337:                     $tool_on = '';
                    338:                 }
1.275     raeburn   339:             }
                    340:         }
                    341:         $output .= '  <tr class="LC_info_row">'."\n".
1.306     raeburn   342:                    '   <td'.$colspan.'>'.$lt{$item}.'</td>'."\n".
1.275     raeburn   343:                    '  </tr>'."\n".
1.306     raeburn   344:                    &Apache::loncommon::start_data_table_row()."\n";
1.418     raeburn   345:   
1.362     raeburn   346:         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.306     raeburn   347:             my ($curroption,$currlimit);
1.362     raeburn   348:             my $envkey = $context.'.'.$item;
                    349:             if ($context eq 'requestauthor') {
                    350:                 $envkey = $context;
                    351:             }
                    352:             if ($userenv{$envkey} ne '') {
                    353:                 $curroption = $userenv{$envkey};
1.332     raeburn   354:             } else {
                    355:                 my (@inststatuses);
1.362     raeburn   356:                 if ($context eq 'requestcourses') {
                    357:                     $curroption =
                    358:                         &Apache::loncoursequeueadmin::get_processtype('course',$ccuname,$ccdomain,
                    359:                                                                       $isadv,$ccdomain,$item,
                    360:                                                                       \@inststatuses,\%domconfig);
                    361:                 } else {
                    362:                      $curroption = 
                    363:                          &Apache::loncoursequeueadmin::get_processtype('requestauthor',$ccuname,$ccdomain,
                    364:                                                                        $isadv,$ccdomain,undef,
                    365:                                                                        \@inststatuses,\%domconfig);
                    366:                 }
1.332     raeburn   367:             }
1.306     raeburn   368:             if (!$curroption) {
                    369:                 $curroption = 'norequest';
                    370:             }
                    371:             if ($curroption =~ /^autolimit=(\d*)$/) {
                    372:                 $currlimit = $1;
1.314     raeburn   373:                 if ($currlimit eq '') {
                    374:                     $currdisp = &mt('Yes, automatic creation');
                    375:                 } else {
                    376:                     $currdisp = &mt('Yes, up to [quant,_1,request]/user',$currlimit);
                    377:                 }
1.306     raeburn   378:             } else {
                    379:                 $currdisp = $reqdisplay{$curroption};
                    380:             }
                    381:             $custdisp = '<table>';
                    382:             foreach my $option (@options) {
                    383:                 my $val = $option;
                    384:                 if ($option eq 'norequest') {
                    385:                     $val = 0;
                    386:                 }
                    387:                 if ($option eq 'validate') {
                    388:                     my $canvalidate = 0;
                    389:                     if (ref($validations{$item}) eq 'HASH') {
                    390:                         if ($validations{$item}{'_custom_'}) {
                    391:                             $canvalidate = 1;
                    392:                         }
                    393:                     }
                    394:                     next if (!$canvalidate);
                    395:                 }
                    396:                 my $checked = '';
                    397:                 if ($option eq $curroption) {
                    398:                     $checked = ' checked="checked"';
                    399:                 } elsif ($option eq 'autolimit') {
                    400:                     if ($curroption =~ /^autolimit/) {
                    401:                         $checked = ' checked="checked"';
                    402:                     }
                    403:                 }
1.362     raeburn   404:                 my $name = 'crsreq_'.$item;
                    405:                 if ($context eq 'requestauthor') {
                    406:                     $name = $item;
                    407:                 }
1.306     raeburn   408:                 $custdisp .= '<tr><td><span class="LC_nobreak"><label>'.
1.362     raeburn   409:                              '<input type="radio" name="'.$name.'" '.
                    410:                              'value="'.$val.'"'.$checked.' />'.
1.306     raeburn   411:                              $reqtitles{$option}.'</label>&nbsp;';
                    412:                 if ($option eq 'autolimit') {
1.362     raeburn   413:                     $custdisp .= '<input type="text" name="'.$name.
                    414:                                  '_limit" size="1" '.
1.314     raeburn   415:                                  'value="'.$currlimit.'" /></span><br />'.
                    416:                                  $reqtitles{'unlimited'};
1.362     raeburn   417:                 } else {
                    418:                     $custdisp .= '</span>';
                    419:                 }
                    420:                 $custdisp .= '</td></tr>';
1.306     raeburn   421:             }
                    422:             $custdisp .= '</table>';
                    423:             $custradio = '</span></td><td>'.&mt('Custom setting').'<br />'.$custdisp;
                    424:         } else {
                    425:             $currdisp = ($curr_access?&mt('Yes'):&mt('No'));
1.362     raeburn   426:             my $name = $context.'_'.$item;
                    427:             if ($context eq 'requestauthor') {
                    428:                 $name = $context;
                    429:             }
1.306     raeburn   430:             $custdisp = '<span class="LC_nobreak"><label>'.
1.362     raeburn   431:                         '<input type="radio" name="'.$name.'"'.
1.361     raeburn   432:                         ' value="1" '.$tool_on.'/>'.&mt('On').'</label>&nbsp;<label>'.
1.362     raeburn   433:                         '<input type="radio" name="'.$name.'" value="0" '.
1.306     raeburn   434:                         $tool_off.'/>'.&mt('Off').'</label></span>';
                    435:             $custradio = ('&nbsp;'x2).'--'.$lt{'cusa'}.':&nbsp;'.$custdisp.
                    436:                           '</span>';
                    437:         }
                    438:         $output .= '  <td'.$colspan.'>'.$custom_access.('&nbsp;'x4).
1.419     raeburn   439:                    $lt{'avai'}.': '.$currdisp.'</td>'."\n".
1.418     raeburn   440:                    &Apache::loncommon::end_data_table_row()."\n";
                    441:         unless (&Apache::lonnet::allowed('udp',$ccdomain)) {
                    442:             $output .=
1.275     raeburn   443:                    &Apache::loncommon::start_data_table_row()."\n".
1.306     raeburn   444:                    '  <td style="vertical-align:top;"><span class="LC_nobreak">'.
                    445:                    $lt{'chse'}.': <label>'.
1.275     raeburn   446:                    '<input type="radio" name="custom'.$item.'" value="0" '.
1.306     raeburn   447:                    $cust_off.'/>'.$lt{'usde'}.'</label>'.('&nbsp;' x3).
                    448:                    '<label><input type="radio" name="custom'.$item.'" value="1" '.
                    449:                    $cust_on.'/>'.$lt{'uscu'}.'</label>'.$custradio.'</td>'.
1.275     raeburn   450:                    &Apache::loncommon::end_data_table_row()."\n";
1.418     raeburn   451:         }
1.275     raeburn   452:     }
                    453:     return $output;
                    454: }
                    455: 
1.300     raeburn   456: sub coursereq_externaluser {
                    457:     my ($ccuname,$ccdomain,$cdom) = @_;
1.306     raeburn   458:     my (@usertools,@options,%validations,%userenv,$output);
1.300     raeburn   459:     my %lt = &Apache::lonlocal::texthash (
                    460:                    'official'   => 'Can request creation of official courses',
                    461:                    'unofficial' => 'Can request creation of unofficial courses',
                    462:                    'community'  => 'Can request creation of communities',
1.384     raeburn   463:                    'textbook'   => 'Can request creation of textbook courses',
1.411     raeburn   464:                    'placement'  => 'Can request creation of placement tests',
1.300     raeburn   465:     );
                    466: 
                    467:     %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    468:                       'reqcrsotherdom.official','reqcrsotherdom.unofficial',
1.411     raeburn   469:                       'reqcrsotherdom.community','reqcrsotherdom.textbook',
                    470:                       'reqcrsotherdom.placement');
                    471:     @usertools = ('official','unofficial','community','textbook','placement');
1.309     raeburn   472:     @options = ('approval','validate','autolimit');
1.306     raeburn   473:     %validations = &Apache::lonnet::auto_courserequest_checks($cdom);
                    474:     my $optregex = join('|',@options);
                    475:     my %reqtitles = &courserequest_titles();
1.300     raeburn   476:     foreach my $item (@usertools) {
1.306     raeburn   477:         my ($curroption,$currlimit,$tooloff);
1.300     raeburn   478:         if ($userenv{'reqcrsotherdom.'.$item} ne '') {
                    479:             my @curr = split(',',$userenv{'reqcrsotherdom.'.$item});
1.314     raeburn   480:             foreach my $req (@curr) {
                    481:                 if ($req =~ /^\Q$cdom\E\:($optregex)=?(\d*)$/) {
                    482:                     $curroption = $1;
                    483:                     $currlimit = $2;
                    484:                     last;
1.306     raeburn   485:                 }
                    486:             }
1.314     raeburn   487:             if (!$curroption) {
                    488:                 $curroption = 'norequest';
                    489:                 $tooloff = ' checked="checked"';
                    490:             }
1.306     raeburn   491:         } else {
                    492:             $curroption = 'norequest';
                    493:             $tooloff = ' checked="checked"';
                    494:         }
                    495:         $output.= &Apache::loncommon::start_data_table_row()."\n".
1.314     raeburn   496:                   '  <td><span class="LC_nobreak">'.$lt{$item}.': </span></td><td>'.
                    497:                   '<table><tr><td valign="top">'."\n".
1.306     raeburn   498:                   '<label><input type="radio" name="reqcrsotherdom_'.$item.
1.314     raeburn   499:                   '" value=""'.$tooloff.' />'.$reqtitles{'norequest'}.
                    500:                   '</label></td>';
1.306     raeburn   501:         foreach my $option (@options) {
                    502:             if ($option eq 'validate') {
                    503:                 my $canvalidate = 0;
                    504:                 if (ref($validations{$item}) eq 'HASH') {
                    505:                     if ($validations{$item}{'_external_'}) {
                    506:                         $canvalidate = 1;
                    507:                     }
                    508:                 }
                    509:                 next if (!$canvalidate);
                    510:             }
                    511:             my $checked = '';
                    512:             if ($option eq $curroption) {
                    513:                 $checked = ' checked="checked"';
                    514:             }
1.314     raeburn   515:             $output .= '<td valign="top"><span class="LC_nobreak"><label>'.
1.306     raeburn   516:                        '<input type="radio" name="reqcrsotherdom_'.$item.
                    517:                        '" value="'.$option.'"'.$checked.' />'.
1.314     raeburn   518:                        $reqtitles{$option}.'</label>';
1.306     raeburn   519:             if ($option eq 'autolimit') {
1.314     raeburn   520:                 $output .= '&nbsp;<input type="text" name="reqcrsotherdom_'.
1.306     raeburn   521:                            $item.'_limit" size="1" '.
1.314     raeburn   522:                            'value="'.$currlimit.'" /></span>'.
                    523:                            '<br />'.$reqtitles{'unlimited'};
                    524:             } else {
                    525:                 $output .= '</span>';
1.300     raeburn   526:             }
1.314     raeburn   527:             $output .= '</td>';
1.300     raeburn   528:         }
1.314     raeburn   529:         $output .= '</td></tr></table></td>'."\n".
1.300     raeburn   530:                    &Apache::loncommon::end_data_table_row()."\n";
                    531:     }
                    532:     return $output;
                    533: }
                    534: 
1.362     raeburn   535: sub domainrole_req {
                    536:     my ($ccuname,$ccdomain) = @_;
                    537:     return '<br /><h3>'.
                    538:            &mt('User Can Request Assignment of Domain Roles?').
                    539:            '</h3>'."\n".
                    540:            &Apache::loncommon::start_data_table().
                    541:            &build_tools_display($ccuname,$ccdomain,
                    542:                                 'requestauthor').
                    543:            &Apache::loncommon::end_data_table();
                    544: }
                    545: 
1.306     raeburn   546: sub courserequest_titles {
                    547:     my %titles = &Apache::lonlocal::texthash (
                    548:                                    official   => 'Official',
                    549:                                    unofficial => 'Unofficial',
                    550:                                    community  => 'Communities',
1.384     raeburn   551:                                    textbook   => 'Textbook',
1.411     raeburn   552:                                    placement  => 'Placement Tests',
1.449     raeburn   553:                                    lti        => 'LTI Provider',
1.306     raeburn   554:                                    norequest  => 'Not allowed',
1.309     raeburn   555:                                    approval   => 'Approval by Dom. Coord.',
1.306     raeburn   556:                                    validate   => 'With validation',
                    557:                                    autolimit  => 'Numerical limit',
1.314     raeburn   558:                                    unlimited  => '(blank for unlimited)',
1.306     raeburn   559:                  );
                    560:     return %titles;
                    561: }
                    562: 
                    563: sub courserequest_display {
                    564:     my %titles = &Apache::lonlocal::texthash (
1.309     raeburn   565:                                    approval   => 'Yes, need approval',
1.306     raeburn   566:                                    validate   => 'Yes, with validation',
                    567:                                    norequest  => 'No',
                    568:    );
                    569:    return %titles;
                    570: }
                    571: 
1.362     raeburn   572: sub requestauthor_titles {
                    573:     my %titles = &Apache::lonlocal::texthash (
                    574:                                    norequest  => 'Not allowed',
                    575:                                    approval   => 'Approval by Dom. Coord.',
                    576:                                    automatic  => 'Automatic approval',
                    577:                  );
                    578:     return %titles;
                    579: 
                    580: }
                    581: 
                    582: sub requestauthor_display {
                    583:     my %titles = &Apache::lonlocal::texthash (
                    584:                                    approval   => 'Yes, need approval',
                    585:                                    automatic  => 'Yes, automatic approval',
                    586:                                    norequest  => 'No',
                    587:    );
                    588:    return %titles;
                    589: }
                    590: 
1.383     raeburn   591: sub requestchange_display {
                    592:     my %titles = &Apache::lonlocal::texthash (
                    593:                                    approval   => "availability set to 'on' (approval required)", 
                    594:                                    automatic  => "availability set to 'on' (automatic approval)",
                    595:                                    norequest  => "availability set to 'off'",
                    596:    );
                    597:    return %titles;
                    598: }
                    599: 
1.362     raeburn   600: sub curr_requestauthor {
                    601:     my ($uname,$udom,$isadv,$inststatuses,$domconfig) = @_;
                    602:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
                    603:     if ($uname eq '' || $udom eq '') {
                    604:         $uname = $env{'user.name'};
                    605:         $udom = $env{'user.domain'};
                    606:         $isadv = $env{'user.adv'};
                    607:     }
                    608:     my (%userenv,%settings,$val);
                    609:     my @options = ('automatic','approval');
                    610:     %userenv =
                    611:         &Apache::lonnet::userenvironment($udom,$uname,'requestauthor','inststatus');
                    612:     if ($userenv{'requestauthor'}) {
                    613:         $val = $userenv{'requestauthor'};
                    614:         @{$inststatuses} = ('_custom_');
                    615:     } else {
                    616:         my %alltasks;
                    617:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') {
                    618:             %settings = %{$domconfig->{'requestauthor'}};
                    619:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
                    620:                 $val = $settings{'_LC_adv'};
                    621:                 @{$inststatuses} = ('_LC_adv_');
                    622:             } else {
                    623:                 if ($userenv{'inststatus'} ne '') {
                    624:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
                    625:                 } else {
                    626:                     @{$inststatuses} = ('default');
                    627:                 }
                    628:                 foreach my $status (@{$inststatuses}) {
                    629:                     if (exists($settings{$status})) {
                    630:                         my $value = $settings{$status};
                    631:                         next unless ($value);
                    632:                         unless (exists($alltasks{$value})) {
                    633:                             if (ref($alltasks{$value}) eq 'ARRAY') {
                    634:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
                    635:                                     push(@{$alltasks{$value}},$status);
                    636:                                 }
                    637:                             } else {
                    638:                                 @{$alltasks{$value}} = ($status);
                    639:                             }
                    640:                         }
                    641:                     }
                    642:                 }
                    643:                 foreach my $option (@options) {
                    644:                     if ($alltasks{$option}) {
                    645:                         $val = $option;
                    646:                         last;
                    647:                     }
                    648:                 }
                    649:             }
                    650:         }
                    651:     }
                    652:     return $val;
                    653: }
                    654: 
1.2       www       655: # =================================================================== Phase one
1.1       www       656: 
1.42      matthew   657: sub print_username_entry_form {
1.439     raeburn   658:     my ($r,$context,$response,$srch,$forcenewuser,$crstype,$brcrum,
                    659:         $permission) = @_;
1.101     albertel  660:     my $defdom=$env{'request.role.domain'};
1.160     raeburn   661:     my $formtoset = 'crtuser';
                    662:     if (exists($env{'form.startrolename'})) {
                    663:         $formtoset = 'docustom';
                    664:         $env{'form.rolename'} = $env{'form.startrolename'};
1.207     raeburn   665:     } elsif ($env{'form.origform'} eq 'crtusername') {
                    666:         $formtoset =  $env{'form.origform'};
1.160     raeburn   667:     }
                    668: 
                    669:     my ($jsback,$elements) = &crumb_utilities();
                    670: 
                    671:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
1.165     albertel  672:         '<script type="text/javascript">'."\n".
1.301     bisitz    673:         '// <![CDATA['."\n".
                    674:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset})."\n".
                    675:         '// ]]>'."\n".
1.162     raeburn   676:         '</script>'."\n";
1.160     raeburn   677: 
1.324     raeburn   678:     my %existingroles=&Apache::lonuserutils::my_custom_roles($crstype);
                    679:     if (($env{'form.action'} eq 'custom') && (keys(%existingroles) > 0)
                    680:         && (&Apache::lonnet::allowed('mcr','/'))) {
                    681:         $jscript .= &customrole_javascript();
                    682:     }
1.224     raeburn   683:     my $helpitem = 'Course_Change_Privileges';
                    684:     if ($env{'form.action'} eq 'custom') {
1.439     raeburn   685:         if ($context eq 'course') {
                    686:             $helpitem = 'Course_Editing_Custom_Roles';
                    687:         } elsif ($context eq 'domain') {
                    688:             $helpitem = 'Domain_Editing_Custom_Roles';
                    689:         }
1.224     raeburn   690:     } elsif ($env{'form.action'} eq 'singlestudent') {
                    691:         $helpitem = 'Course_Add_Student';
1.416     raeburn   692:     } elsif ($env{'form.action'} eq 'accesslogs') {
                    693:         $helpitem = 'Domain_User_Access_Logs';
1.439     raeburn   694:     } elsif ($context eq 'author') {
                    695:         $helpitem = 'Author_Change_Privileges';
                    696:     } elsif ($context eq 'domain') {
                    697:         if ($permission->{'cusr'}) {
                    698:             $helpitem = 'Domain_Change_Privileges';
                    699:         } elsif ($permission->{'view'}) {
                    700:             $helpitem = 'Domain_View_Privileges';
                    701:         } else {
                    702:             undef($helpitem);
                    703:         }
1.224     raeburn   704:     }
1.422     raeburn   705:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$defdom);
1.351     raeburn   706:     if ($env{'form.action'} eq 'custom') {
                    707:         push(@{$brcrum},
                    708:                  {href=>"javascript:backPage(document.crtuser)",       
                    709:                   text=>"Pick custom role",
                    710:                   help => $helpitem,}
                    711:                  );
                    712:     } else {
                    713:         push (@{$brcrum},
                    714:                   {href => "javascript:backPage(document.crtuser)",
                    715:                    text => $breadcrumb_text{'search'},
                    716:                    help => $helpitem,
                    717:                    faq  => 282,
                    718:                    bug  => 'Instructor Interface',}
                    719:                   );
                    720:     }
                    721:     my %loaditems = (
                    722:                 'onload' => "javascript:setFormElements(document.$formtoset)",
                    723:                     );
                    724:     my $args = {bread_crumbs           => $brcrum,
                    725:                 bread_crumbs_component => 'User Management',
                    726:                 add_entries            => \%loaditems,};
                    727:     $r->print(&Apache::loncommon::start_page('User Management',$jscript,$args));
                    728: 
1.71      sakharuk  729:     my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   730:                     'srst' => 'Search for a user and enroll as a student',
1.318     raeburn   731:                     'srme' => 'Search for a user and enroll as a member',
1.229     raeburn   732:                     'srad' => 'Search for a user and modify/add user information or roles',
1.422     raeburn   733:                     'srvu' => 'Search for a user and view user information and roles',
1.416     raeburn   734:                     'srva' => 'Search for a user and view access log information',
1.71      sakharuk  735: 		    'usr'  => "Username",
                    736:                     'dom'  => "Domain",
1.324     raeburn   737:                     'ecrp' => "Define or Edit Custom Role",
                    738:                     'nr'   => "role name",
1.282     schafran  739:                     'cre'  => "Next",
1.71      sakharuk  740: 				       );
1.351     raeburn   741: 
1.214     raeburn   742:     if ($env{'form.action'} eq 'custom') {
1.190     raeburn   743:         if (&Apache::lonnet::allowed('mcr','/')) {
1.324     raeburn   744:             my $newroletext = &mt('Define new custom role:');
                    745:             $r->print('<form action="/adm/createuser" method="post" name="docustom">'.
                    746:                       '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                    747:                       '<input type="hidden" name="phase" value="selected_custom_edit" />'.
                    748:                       '<h3>'.$lt{'ecrp'}.'</h3>'.
                    749:                       &Apache::loncommon::start_data_table().
                    750:                       &Apache::loncommon::start_data_table_row().
                    751:                       '<td>');
                    752:             if (keys(%existingroles) > 0) {
                    753:                 $r->print('<br /><label><input type="radio" name="customroleaction" value="new" checked="checked" onclick="setCustomFields();" /><b>'.$newroletext.'</b></label>');
                    754:             } else {
                    755:                 $r->print('<br /><input type="hidden" name="customroleaction" value="new" /><b>'.$newroletext.'</b>');
                    756:             }
                    757:             $r->print('</td><td align="center">'.$lt{'nr'}.'<br /><input type="text" size="15" name="newrolename" onfocus="setCustomAction('."'new'".');" /></td>'.
                    758:                       &Apache::loncommon::end_data_table_row());
                    759:             if (keys(%existingroles) > 0) {
                    760:                 $r->print(&Apache::loncommon::start_data_table_row().'<td><br />'.
                    761:                           '<label><input type="radio" name="customroleaction" value="edit" onclick="setCustomFields();"/><b>'.
                    762:                           &mt('View/Modify existing role:').'</b></label></td>'.
                    763:                           '<td align="center"><br />'.
                    764:                           '<select name="rolename" onchange="setCustomAction('."'edit'".');">'.
1.326     raeburn   765:                           '<option value="" selected="selected">'.
1.324     raeburn   766:                           &mt('Select'));
                    767:                 foreach my $role (sort(keys(%existingroles))) {
1.326     raeburn   768:                     $r->print('<option value="'.$role.'">'.$role.'</option>');
1.324     raeburn   769:                 }
                    770:                 $r->print('</select>'.
                    771:                           '</td>'.
                    772:                           &Apache::loncommon::end_data_table_row());
                    773:             }
                    774:             $r->print(&Apache::loncommon::end_data_table().'<p>'.
                    775:                       '<input name="customeditor" type="submit" value="'.
                    776:                       $lt{'cre'}.'" /></p>'.
                    777:                       '</form>');
1.190     raeburn   778:         }
1.213     raeburn   779:     } else {
1.229     raeburn   780:         my $actiontext = $lt{'srad'};
1.436     raeburn   781:         my $fixeddom;
1.213     raeburn   782:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   783:             if ($crstype eq 'Community') {
                    784:                 $actiontext = $lt{'srme'};
                    785:             } else {
                    786:                 $actiontext = $lt{'srst'};
                    787:             }
1.416     raeburn   788:         } elsif ($env{'form.action'} eq 'accesslogs') {
1.417     raeburn   789:             $actiontext = $lt{'srva'};
1.436     raeburn   790:             $fixeddom = 1;
1.422     raeburn   791:         } elsif (($env{'form.action'} eq 'singleuser') &&
                    792:                  ($context eq 'domain') && (!&Apache::lonnet::allowed('mau',$defdom))) {
                    793:             $actiontext = $lt{'srvu'};
1.439     raeburn   794:             $fixeddom = 1;
1.213     raeburn   795:         }
1.324     raeburn   796:         $r->print("<h3>$actiontext</h3>");
1.213     raeburn   797:         if ($env{'form.origform'} ne 'crtusername') {
1.415     raeburn   798:             if ($response) {
                    799:                $r->print("\n<div>$response</div>".
                    800:                          '<br clear="all" />');
                    801:             }
1.213     raeburn   802:         }
1.436     raeburn   803:         $r->print(&entry_form($defdom,$srch,$forcenewuser,$context,$response,$crstype,$fixeddom));
1.107     www       804:     }
1.110     albertel  805: }
                    806: 
1.324     raeburn   807: sub customrole_javascript {
                    808:     my $js = <<"END";
                    809: <script type="text/javascript">
                    810: // <![CDATA[
                    811: 
                    812: function setCustomFields() {
                    813:     if (document.docustom.customroleaction.length > 0) {
                    814:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    815:             if (document.docustom.customroleaction[i].checked) {
                    816:                 if (document.docustom.customroleaction[i].value == 'new') {
                    817:                     document.docustom.rolename.selectedIndex = 0;
                    818:                 } else {
                    819:                     document.docustom.newrolename.value = '';
                    820:                 }
                    821:             }
                    822:         }
                    823:     }
                    824:     return;
                    825: }
                    826: 
                    827: function setCustomAction(caller) {
                    828:     if (document.docustom.customroleaction.length > 0) {
                    829:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    830:             if (document.docustom.customroleaction[i].value == caller) {
                    831:                 document.docustom.customroleaction[i].checked = true;
                    832:             }
                    833:         }
                    834:     }
                    835:     setCustomFields();
                    836:     return;
                    837: }
                    838: 
                    839: // ]]>
                    840: </script>
                    841: END
                    842:     return $js;
                    843: }
                    844: 
1.160     raeburn   845: sub entry_form {
1.416     raeburn   846:     my ($dom,$srch,$forcenewuser,$context,$responsemsg,$crstype,$fixeddom) = @_;
1.229     raeburn   847:     my ($usertype,$inexact);
1.214     raeburn   848:     if (ref($srch) eq 'HASH') {
                    849:         if (($srch->{'srchin'} eq 'dom') &&
                    850:             ($srch->{'srchby'} eq 'uname') &&
                    851:             ($srch->{'srchtype'} eq 'exact') &&
                    852:             ($srch->{'srchdomain'} ne '') &&
                    853:             ($srch->{'srchterm'} ne '')) {
1.353     raeburn   854:             my (%curr_rules,%got_rules);
1.214     raeburn   855:             my ($rules,$ruleorder) =
                    856:                 &Apache::lonnet::inst_userrules($srch->{'srchdomain'},'username');
1.353     raeburn   857:             $usertype = &Apache::lonuserutils::check_usertype($srch->{'srchdomain'},$srch->{'srchterm'},$rules,\%curr_rules,\%got_rules);
1.229     raeburn   858:         } else {
                    859:             $inexact = 1;
1.214     raeburn   860:         }
1.207     raeburn   861:     }
1.438     raeburn   862:     my ($cancreate,$noinstd);
                    863:     if ($env{'form.action'} eq 'accesslogs') {
                    864:         $noinstd = 1;
                    865:     } else {
                    866:         $cancreate =
                    867:             &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
                    868:     }
1.412     raeburn   869:     my ($userpicker,$cansearch) = 
1.179     raeburn   870:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
1.438     raeburn   871:                                        'document.crtuser',$cancreate,$usertype,$context,$fixeddom,$noinstd);
1.160     raeburn   872:     my $srchbutton = &mt('Search');
1.229     raeburn   873:     if ($env{'form.action'} eq 'singlestudent') {
                    874:         $srchbutton = &mt('Search and Enroll');
1.416     raeburn   875:     } elsif ($env{'form.action'} eq 'accesslogs') {
                    876:         $srchbutton = &mt('Search');
1.229     raeburn   877:     } elsif ($cancreate && $responsemsg ne '' && $inexact) {
                    878:         $srchbutton = &mt('Search or Add New User');
                    879:     }
1.412     raeburn   880:     my $output;
                    881:     if ($cansearch) {
                    882:         $output = <<"ENDBLOCK";
1.160     raeburn   883: <form action="/adm/createuser" method="post" name="crtuser">
1.190     raeburn   884: <input type="hidden" name="action" value="$env{'form.action'}" />
1.160     raeburn   885: <input type="hidden" name="phase" value="get_user_info" />
                    886: $userpicker
1.179     raeburn   887: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
1.160     raeburn   888: </form>
1.207     raeburn   889: ENDBLOCK
1.412     raeburn   890:     } else {
                    891:         $output = '<p>'.$userpicker.'</p>';
                    892:     }
1.422     raeburn   893:     if (($env{'form.phase'} eq '') && ($env{'form.action'} ne 'accesslogs') &&
1.430     raeburn   894:         (!(($env{'form.action'} eq 'singleuser') && ($context eq 'domain') &&
1.422     raeburn   895:         (!&Apache::lonnet::allowed('mau',$env{'request.role.domain'}))))) {
1.207     raeburn   896:         my $defdom=$env{'request.role.domain'};
1.446     raeburn   897:         my ($trusted,$untrusted);
1.444     raeburn   898:         if ($context eq 'course') {
1.446     raeburn   899:             ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('enroll',$defdom);
1.444     raeburn   900:         } elsif ($context eq 'author') {
1.446     raeburn   901:             ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('othcoau',$defdom);
1.444     raeburn   902:         } elsif ($context eq 'domain') {
1.446     raeburn   903:             ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('domroles',$defdom); 
1.444     raeburn   904:         }
1.446     raeburn   905:         my $domform = &Apache::loncommon::select_dom_form($defdom,'srchdomain',undef,undef,undef,$trusted,$untrusted);
1.207     raeburn   906:         my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   907:                   'enro' => 'Enroll one student',
1.318     raeburn   908:                   'enrm' => 'Enroll one member',
1.229     raeburn   909:                   'admo' => 'Add/modify a single user',
                    910:                   'crea' => 'create new user if required',
                    911:                   'uskn' => "username is known",
1.207     raeburn   912:                   'crnu' => 'Create a new user',
                    913:                   'usr'  => 'Username',
                    914:                   'dom'  => 'in domain',
1.229     raeburn   915:                   'enrl' => 'Enroll',
                    916:                   'cram'  => 'Create/Modify user',
1.207     raeburn   917:         );
1.229     raeburn   918:         my $sellink=&Apache::loncommon::selectstudent_link('crtusername','srchterm','srchdomain');
                    919:         my ($title,$buttontext,$showresponse);
1.318     raeburn   920:         if ($env{'form.action'} eq 'singlestudent') {
                    921:             if ($crstype eq 'Community') {
                    922:                 $title = $lt{'enrm'};
                    923:             } else {
                    924:                 $title = $lt{'enro'};
                    925:             }
1.229     raeburn   926:             $buttontext = $lt{'enrl'};
                    927:         } else {
                    928:             $title = $lt{'admo'};
                    929:             $buttontext = $lt{'cram'};
                    930:         }
                    931:         if ($cancreate) {
                    932:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'crea'}.')</span>';
                    933:         } else {
                    934:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'uskn'}.')</span>';
                    935:         }
                    936:         if ($env{'form.origform'} eq 'crtusername') {
                    937:             $showresponse = $responsemsg;
                    938:         }
1.207     raeburn   939:         $output .= <<"ENDDOCUMENT";
1.229     raeburn   940: <br />
1.207     raeburn   941: <form action="/adm/createuser" method="post" name="crtusername">
                    942: <input type="hidden" name="action" value="$env{'form.action'}" />
                    943: <input type="hidden" name="phase" value="createnewuser" />
                    944: <input type="hidden" name="srchtype" value="exact" />
1.233     raeburn   945: <input type="hidden" name="srchby" value="uname" />
1.207     raeburn   946: <input type="hidden" name="srchin" value="dom" />
                    947: <input type="hidden" name="forcenewuser" value="1" />
                    948: <input type="hidden" name="origform" value="crtusername" />
1.229     raeburn   949: <h3>$title</h3>
                    950: $showresponse
1.207     raeburn   951: <table>
                    952:  <tr>
                    953:   <td>$lt{'usr'}:</td>
                    954:   <td><input type="text" size="15" name="srchterm" /></td>
                    955:   <td>&nbsp;$lt{'dom'}:</td><td>$domform</td>
1.229     raeburn   956:   <td>&nbsp;$sellink&nbsp;</td>
                    957:   <td>&nbsp;<input name="userrole" type="submit" value="$buttontext" /></td>
1.207     raeburn   958:  </tr>
                    959: </table>
                    960: </form>
1.160     raeburn   961: ENDDOCUMENT
1.207     raeburn   962:     }
1.160     raeburn   963:     return $output;
                    964: }
1.110     albertel  965: 
                    966: sub user_modification_js {
1.113     raeburn   967:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
                    968:     
1.110     albertel  969:     return <<END;
                    970: <script type="text/javascript" language="Javascript">
1.301     bisitz    971: // <![CDATA[
1.314     raeburn   972: 
1.110     albertel  973:     $pjump_def
                    974:     $dc_setcourse_code
                    975: 
                    976:     function dateset() {
                    977:         eval("document.cu."+document.cu.pres_marker.value+
                    978:             ".value=document.cu.pres_value.value");
1.359     www       979:         modalWindow.close();
1.110     albertel  980:     }
                    981: 
1.113     raeburn   982:     $nondc_setsection_code
1.301     bisitz    983: // ]]>
1.110     albertel  984: </script>
                    985: END
1.2       www       986: }
                    987: 
                    988: # =================================================================== Phase two
1.160     raeburn   989: sub print_user_selection_page {
1.351     raeburn   990:     my ($r,$response,$srch,$srch_results,$srcharray,$context,$opener_elements,$crstype,$brcrum) = @_;
1.160     raeburn   991:     my @fields = ('username','domain','lastname','firstname','permanentemail');
                    992:     my $sortby = $env{'form.sortby'};
                    993: 
                    994:     if (!grep(/^\Q$sortby\E$/,@fields)) {
                    995:         $sortby = 'lastname';
                    996:     }
                    997: 
                    998:     my ($jsback,$elements) = &crumb_utilities();
                    999: 
                   1000:     my $jscript = (<<ENDSCRIPT);
                   1001: <script type="text/javascript">
1.301     bisitz   1002: // <![CDATA[
1.160     raeburn  1003: function pickuser(uname,udom) {
                   1004:     document.usersrchform.seluname.value=uname;
                   1005:     document.usersrchform.seludom.value=udom;
                   1006:     document.usersrchform.phase.value="userpicked";
                   1007:     document.usersrchform.submit();
                   1008: }
                   1009: 
                   1010: $jsback
1.301     bisitz   1011: // ]]>
1.160     raeburn  1012: </script>
                   1013: ENDSCRIPT
                   1014: 
                   1015:     my %lt=&Apache::lonlocal::texthash(
1.179     raeburn  1016:                                        'usrch'          => "User Search to add/modify roles",
                   1017:                                        'stusrch'        => "User Search to enroll student",
1.318     raeburn  1018:                                        'memsrch'        => "User Search to enroll member",
1.416     raeburn  1019:                                        'srcva'          => "Search for a user and view access log information",
1.422     raeburn  1020:                                        'usrvu'          => "User Search to view user roles",
1.179     raeburn  1021:                                        'usel'           => "Select a user to add/modify roles",
1.422     raeburn  1022:                                        'suvr'           => "Select a user to view roles",
1.318     raeburn  1023:                                        'stusel'         => "Select a user to enroll as a student",
                   1024:                                        'memsel'         => "Select a user to enroll as a member",
1.416     raeburn  1025:                                        'vacsel'         => "Select a user to view access log",
1.160     raeburn  1026:                                        'username'       => "username",
                   1027:                                        'domain'         => "domain",
                   1028:                                        'lastname'       => "last name",
                   1029:                                        'firstname'      => "first name",
                   1030:                                        'permanentemail' => "permanent e-mail",
                   1031:                                       );
1.302     raeburn  1032:     if ($context eq 'requestcrs') {
                   1033:         $r->print('<div>');
                   1034:     } else {
1.422     raeburn  1035:         my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$srch->{'srchdomain'});
1.351     raeburn  1036:         my $helpitem;
                   1037:         if ($env{'form.action'} eq 'singleuser') {
                   1038:             $helpitem = 'Course_Change_Privileges';
                   1039:         } elsif ($env{'form.action'} eq 'singlestudent') {
                   1040:             $helpitem = 'Course_Add_Student';
1.439     raeburn  1041:         } elsif ($context eq 'author') {
                   1042:             $helpitem = 'Author_Change_Privileges';
                   1043:         } elsif ($context eq 'domain') {
                   1044:             $helpitem = 'Domain_Change_Privileges';
1.351     raeburn  1045:         }
                   1046:         push (@{$brcrum},
                   1047:                   {href => "javascript:backPage(document.usersrchform,'','')",
                   1048:                    text => $breadcrumb_text{'search'},
                   1049:                    faq  => 282,
                   1050:                    bug  => 'Instructor Interface',},
                   1051:                   {href => "javascript:backPage(document.usersrchform,'get_user_info','select')",
                   1052:                    text => $breadcrumb_text{'userpicked'},
                   1053:                    faq  => 282,
                   1054:                    bug  => 'Instructor Interface',
                   1055:                    help => $helpitem}
                   1056:                   );
                   1057:         $r->print(&Apache::loncommon::start_page('User Management',$jscript,{bread_crumbs => $brcrum}));
1.302     raeburn  1058:         if ($env{'form.action'} eq 'singleuser') {
1.422     raeburn  1059:             my $readonly;
                   1060:             if (($context eq 'domain') && (!&Apache::lonnet::allowed('mau',$srch->{'srchdomain'}))) {
                   1061:                 $readonly = 1;
                   1062:                 $r->print("<b>$lt{'usrvu'}</b><br />");
                   1063:             } else {
                   1064:                 $r->print("<b>$lt{'usrch'}</b><br />");
                   1065:             }
1.318     raeburn  1066:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
1.422     raeburn  1067:             if ($readonly) {
                   1068:                 $r->print('<h3>'.$lt{'suvr'}.'</h3>');
                   1069:             } else {
                   1070:                 $r->print('<h3>'.$lt{'usel'}.'</h3>');
                   1071:             }
1.302     raeburn  1072:         } elsif ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1073:             $r->print($jscript."<b>");
                   1074:             if ($crstype eq 'Community') {
                   1075:                 $r->print($lt{'memsrch'});
                   1076:             } else {
                   1077:                 $r->print($lt{'stusrch'});
                   1078:             }
                   1079:             $r->print("</b><br />");
                   1080:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
                   1081:             $r->print('</form><h3>');
                   1082:             if ($crstype eq 'Community') {
                   1083:                 $r->print($lt{'memsel'});
                   1084:             } else {
                   1085:                 $r->print($lt{'stusel'});
                   1086:             }
                   1087:             $r->print('</h3>');
1.416     raeburn  1088:         } elsif ($env{'form.action'} eq 'accesslogs') {
                   1089:             $r->print("<b>$lt{'srcva'}</b><br />");
1.438     raeburn  1090:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,undef,1));
1.416     raeburn  1091:             $r->print('<h3>'.$lt{'vacsel'}.'</h3>');
1.302     raeburn  1092:         }
1.179     raeburn  1093:     }
1.380     bisitz   1094:     $r->print('<form name="usersrchform" method="post" action="">'.
1.160     raeburn  1095:               &Apache::loncommon::start_data_table()."\n".
                   1096:               &Apache::loncommon::start_data_table_header_row()."\n".
                   1097:               ' <th> </th>'."\n");
                   1098:     foreach my $field (@fields) {
                   1099:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
                   1100:                   "'".$field."'".';document.usersrchform.submit();">'.
                   1101:                   $lt{$field}.'</a></th>'."\n");
                   1102:     }
                   1103:     $r->print(&Apache::loncommon::end_data_table_header_row());
                   1104: 
                   1105:     my @sorted_users = sort {
1.167     albertel 1106:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
1.160     raeburn  1107:             ||
1.167     albertel 1108:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
1.160     raeburn  1109:             ||
                   1110:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
1.167     albertel 1111: 	    ||
                   1112: 	lc($a) cmp lc($b)
1.160     raeburn  1113:         } (keys(%$srch_results));
                   1114: 
                   1115:     foreach my $user (@sorted_users) {
                   1116:         my ($uname,$udom) = split(/:/,$user);
1.302     raeburn  1117:         my $onclick;
                   1118:         if ($context eq 'requestcrs') {
1.314     raeburn  1119:             $onclick =
1.302     raeburn  1120:                 'onclick="javascript:gochoose('."'$uname','$udom',".
                   1121:                                                "'$srch_results->{$user}->{firstname}',".
                   1122:                                                "'$srch_results->{$user}->{lastname}',".
                   1123:                                                "'$srch_results->{$user}->{permanentemail}'".');"';
                   1124:         } else {
1.314     raeburn  1125:             $onclick =
1.302     raeburn  1126:                 ' onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".');"';
                   1127:         }
1.160     raeburn  1128:         $r->print(&Apache::loncommon::start_data_table_row().
1.302     raeburn  1129:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" '.
                   1130:                   $onclick.' /></td>'.
1.160     raeburn  1131:                   '<td><tt>'.$uname.'</tt></td>'.
                   1132:                   '<td><tt>'.$udom.'</tt></td>');
                   1133:         foreach my $field ('lastname','firstname','permanentemail') {
                   1134:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
                   1135:         }
                   1136:         $r->print(&Apache::loncommon::end_data_table_row());
                   1137:     }
                   1138:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
1.179     raeburn  1139:     if (ref($srcharray) eq 'ARRAY') {
                   1140:         foreach my $item (@{$srcharray}) {
                   1141:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
                   1142:         }
                   1143:     }
1.160     raeburn  1144:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
                   1145:               ' <input type="hidden" name="seluname" value="" />'."\n".
                   1146:               ' <input type="hidden" name="seludom" value="" />'."\n".
1.179     raeburn  1147:               ' <input type="hidden" name="currstate" value="select" />'."\n".
1.190     raeburn  1148:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
1.214     raeburn  1149:               ' <input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
1.302     raeburn  1150:     if ($context eq 'requestcrs') {
                   1151:         $r->print($opener_elements.'</form></div>');
                   1152:     } else {
1.351     raeburn  1153:         $r->print($response.'</form>');
1.302     raeburn  1154:     }
1.160     raeburn  1155: }
                   1156: 
                   1157: sub print_user_query_page {
1.351     raeburn  1158:     my ($r,$caller,$brcrum) = @_;
1.160     raeburn  1159: # FIXME - this is for a network-wide name search (similar to catalog search)
                   1160: # To use frames with similar behavior to catalog/portfolio search.
                   1161: # To be implemented. 
                   1162:     return;
                   1163: }
                   1164: 
1.42      matthew  1165: sub print_user_modification_page {
1.375     raeburn  1166:     my ($r,$ccuname,$ccdomain,$srch,$response,$context,$permission,$crstype,
                   1167:         $brcrum,$showcredits) = @_;
1.185     raeburn  1168:     if (($ccuname eq '') || ($ccdomain eq '')) {
1.215     raeburn  1169:         my $usermsg = &mt('No username and/or domain provided.');
                   1170:         $env{'form.phase'} = '';
1.439     raeburn  1171: 	&print_username_entry_form($r,$context,$usermsg,'','',$crstype,$brcrum,
                   1172:                                    $permission);
1.58      www      1173:         return;
                   1174:     }
1.213     raeburn  1175:     my ($form,$formname);
                   1176:     if ($env{'form.action'} eq 'singlestudent') {
                   1177:         $form = 'document.enrollstudent';
                   1178:         $formname = 'enrollstudent';
                   1179:     } else {
                   1180:         $form = 'document.cu';
                   1181:         $formname = 'cu';
                   1182:     }
1.188     raeburn  1183:     my %abv_auth = &auth_abbrev();
1.227     raeburn  1184:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
1.185     raeburn  1185:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
                   1186:     if ($uhome eq 'no_host') {
1.215     raeburn  1187:         my $usertype;
                   1188:         my ($rules,$ruleorder) =
                   1189:             &Apache::lonnet::inst_userrules($ccdomain,'username');
                   1190:             $usertype =
1.353     raeburn  1191:                 &Apache::lonuserutils::check_usertype($ccdomain,$ccuname,$rules,
1.362     raeburn  1192:                                                       \%curr_rules,\%got_rules);
1.215     raeburn  1193:         my $cancreate =
                   1194:             &Apache::lonuserutils::can_create_user($ccdomain,$context,
                   1195:                                                    $usertype);
                   1196:         if (!$cancreate) {
1.292     bisitz   1197:             my $helplink = 'javascript:helpMenu('."'display'".')';
1.215     raeburn  1198:             my %usertypetext = (
                   1199:                 official   => 'institutional',
                   1200:                 unofficial => 'non-institutional',
                   1201:             );
                   1202:             my $response;
                   1203:             if ($env{'form.origform'} eq 'crtusername') {
1.362     raeburn  1204:                 $response = '<span class="LC_warning">'.
                   1205:                             &mt('No match found for the username [_1] in LON-CAPA domain: [_2]',
                   1206:                                 '<b>'.$ccuname.'</b>',$ccdomain).
1.215     raeburn  1207:                             '</span><br />';
                   1208:             }
1.292     bisitz   1209:             $response .= '<p class="LC_warning">'
                   1210:                         .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
1.418     raeburn  1211:                         .' ';
                   1212:             if ($context eq 'domain') {
                   1213:                 $response .= &mt('Please contact a [_1] for assistance.',
                   1214:                                  &Apache::lonnet::plaintext('dc'));
                   1215:             } else {
                   1216:                 $response .= &mt('Please contact the [_1]helpdesk[_2] for assistance.'
                   1217:                                 ,'<a href="'.$helplink.'">','</a>');
                   1218:             }
                   1219:             $response .= '</p><br />';
1.215     raeburn  1220:             $env{'form.phase'} = '';
1.439     raeburn  1221:             &print_username_entry_form($r,$context,$response,undef,undef,$crstype,$brcrum,
                   1222:                                        $permission);
1.215     raeburn  1223:             return;
                   1224:         }
1.188     raeburn  1225:         $newuser = 1;
1.193     raeburn  1226:         my $checkhash;
                   1227:         my $checks = { 'username' => 1 };
1.196     raeburn  1228:         $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
1.193     raeburn  1229:         &Apache::loncommon::user_rule_check($checkhash,$checks,
1.196     raeburn  1230:             \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
                   1231:         if (ref($alerts{'username'}) eq 'HASH') {
                   1232:             if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
                   1233:                 my $domdesc =
1.193     raeburn  1234:                     &Apache::lonnet::domain($ccdomain,'description');
1.196     raeburn  1235:                 if ($alerts{'username'}{$ccdomain}{$ccuname}) {
                   1236:                     my $userchkmsg;
                   1237:                     if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
                   1238:                         $userchkmsg = 
                   1239:                             &Apache::loncommon::instrule_disallow_msg('username',
1.193     raeburn  1240:                                                                  $domdesc,1).
                   1241:                         &Apache::loncommon::user_rule_formats($ccdomain,
                   1242:                             $domdesc,$curr_rules{$ccdomain}{'username'},
                   1243:                             'username');
1.196     raeburn  1244:                     }
1.215     raeburn  1245:                     $env{'form.phase'} = '';
1.439     raeburn  1246:                     &print_username_entry_form($r,$context,$userchkmsg,undef,undef,$crstype,$brcrum,
                   1247:                                                $permission);
1.196     raeburn  1248:                     return;
1.215     raeburn  1249:                 }
1.193     raeburn  1250:             }
1.185     raeburn  1251:         }
1.187     raeburn  1252:     } else {
1.188     raeburn  1253:         $newuser = 0;
1.185     raeburn  1254:     }
1.160     raeburn  1255:     if ($response) {
1.215     raeburn  1256:         $response = '<br />'.$response;
1.160     raeburn  1257:     }
1.149     raeburn  1258: 
1.52      matthew  1259:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.88      raeburn  1260:     my $dc_setcourse_code = '';
1.119     raeburn  1261:     my $nondc_setsection_code = '';                                        
1.112     albertel 1262:     my %loaditem;
1.114     albertel 1263: 
1.216     raeburn  1264:     my $groupslist = &Apache::lonuserutils::get_groupslist();
1.88      raeburn  1265: 
1.375     raeburn  1266:     my $js = &validation_javascript($context,$ccdomain,$pjump_def,$crstype,
1.216     raeburn  1267:                                $groupslist,$newuser,$formname,\%loaditem);
1.422     raeburn  1268:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$ccdomain);
1.224     raeburn  1269:     my $helpitem = 'Course_Change_Privileges';
                   1270:     if ($env{'form.action'} eq 'singlestudent') {
                   1271:         $helpitem = 'Course_Add_Student';
1.439     raeburn  1272:     } elsif ($context eq 'author') {
                   1273:         $helpitem = 'Author_Change_Privileges';
                   1274:     } elsif ($context eq 'domain') {
                   1275:         $helpitem = 'Domain_Change_Privileges';
1.224     raeburn  1276:     }
1.351     raeburn  1277:     push (@{$brcrum},
                   1278:         {href => "javascript:backPage($form)",
                   1279:          text => $breadcrumb_text{'search'},
                   1280:          faq  => 282,
                   1281:          bug  => 'Instructor Interface',});
                   1282:     if ($env{'form.phase'} eq 'userpicked') {
                   1283:        push(@{$brcrum},
                   1284:               {href => "javascript:backPage($form,'get_user_info','select')",
                   1285:                text => $breadcrumb_text{'userpicked'},
                   1286:                faq  => 282,
                   1287:                bug  => 'Instructor Interface',});
                   1288:     }
                   1289:     push(@{$brcrum},
                   1290:             {href => "javascript:backPage($form,'$env{'form.phase'}','modify')",
                   1291:              text => $breadcrumb_text{'modify'},
                   1292:              faq  => 282,
                   1293:              bug  => 'Instructor Interface',
                   1294:              help => $helpitem});
                   1295:     my $args = {'add_entries'           => \%loaditem,
                   1296:                 'bread_crumbs'          => $brcrum,
                   1297:                 'bread_crumbs_component' => 'User Management'};
                   1298:     if ($env{'form.popup'}) {
                   1299:         $args->{'no_nav_bar'} = 1;
                   1300:     }
                   1301:     my $start_page =
                   1302:         &Apache::loncommon::start_page('User Management',$js,$args);
1.3       www      1303: 
1.25      matthew  1304:     my $forminfo =<<"ENDFORMINFO";
1.216     raeburn  1305: <form action="/adm/createuser" method="post" name="$formname">
1.190     raeburn  1306: <input type="hidden" name="phase" value="update_user_data" />
1.188     raeburn  1307: <input type="hidden" name="ccuname" value="$ccuname" />
                   1308: <input type="hidden" name="ccdomain" value="$ccdomain" />
1.157     albertel 1309: <input type="hidden" name="pres_value"  value="" />
                   1310: <input type="hidden" name="pres_type"   value="" />
                   1311: <input type="hidden" name="pres_marker" value="" />
1.25      matthew  1312: ENDFORMINFO
1.375     raeburn  1313:     my (%inccourses,$roledom,$defaultcredits);
1.329     raeburn  1314:     if ($context eq 'course') {
                   1315:         $inccourses{$env{'request.course.id'}}=1;
                   1316:         $roledom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.375     raeburn  1317:         if ($showcredits) {
                   1318:             $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1319:         }
1.329     raeburn  1320:     } elsif ($context eq 'author') {
                   1321:         $roledom = $env{'request.role.domain'};
                   1322:     } elsif ($context eq 'domain') {
                   1323:         foreach my $key (keys(%env)) {
                   1324:             $roledom = $env{'request.role.domain'};
                   1325:             if ($key=~/^user\.priv\.cm\.\/($roledom)\/($match_username)/) {
                   1326:                 $inccourses{$1.'_'.$2}=1;
                   1327:             }
                   1328:         }
                   1329:     } else {
                   1330:         foreach my $key (keys(%env)) {
                   1331: 	    if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
                   1332: 	        $inccourses{$1.'_'.$2}=1;
                   1333:             }
1.2       www      1334:         }
1.24      matthew  1335:     }
1.389     bisitz   1336:     my $title = '';
1.216     raeburn  1337:     if ($newuser) {
1.427     raeburn  1338:         my ($portfolioform,$domroleform);
1.267     raeburn  1339:         if ((&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) ||
                   1340:             (&Apache::lonnet::allowed('mut',$env{'request.role.domain'}))) {
                   1341:             # Current user has quota or user tools modification privileges
1.378     raeburn  1342:             $portfolioform = '<br />'.&user_quotas($ccuname,$ccdomain);
1.134     raeburn  1343:         }
1.383     raeburn  1344:         if ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) &&
                   1345:             ($ccdomain eq $env{'request.role.domain'})) {
1.362     raeburn  1346:             $domroleform = '<br />'.&domainrole_req($ccuname,$ccdomain);
                   1347:         }
1.227     raeburn  1348:         &initialize_authen_forms($ccdomain,$formname);
1.188     raeburn  1349:         my %lt=&Apache::lonlocal::texthash(
                   1350:                 'lg'             => 'Login Data',
1.190     raeburn  1351:                 'hs'             => "Home Server",
1.188     raeburn  1352:         );
1.185     raeburn  1353: 	$r->print(<<ENDTITLE);
1.110     albertel 1354: $start_page
1.160     raeburn  1355: $response
1.25      matthew  1356: $forminfo
1.31      matthew  1357: <script type="text/javascript" language="Javascript">
1.301     bisitz   1358: // <![CDATA[
1.20      harris41 1359: $loginscript
1.301     bisitz   1360: // ]]>
1.31      matthew  1361: </script>
1.20      harris41 1362: <input type='hidden' name='makeuser' value='1' />
1.185     raeburn  1363: ENDTITLE
1.213     raeburn  1364:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1365:             if ($crstype eq 'Community') {
1.389     bisitz   1366:                 $title = &mt('Create New User [_1] in domain [_2] as a member',
                   1367:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1368:             } else {
1.389     bisitz   1369:                 $title = &mt('Create New User [_1] in domain [_2] as a student',
                   1370:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1371:             }
1.389     bisitz   1372:         } else {
                   1373:                 $title = &mt('Create New User [_1] in domain [_2]',
                   1374:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.213     raeburn  1375:         }
1.389     bisitz   1376:         $r->print('<h2>'.$title.'</h2>'."\n");
                   1377:         $r->print('<div class="LC_left_float">');
1.393     raeburn  1378:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1379:                                          $inst_results{$ccuname.':'.$ccdomain}));
                   1380:         # Option to disable student/employee ID conflict checking not offerred for new users.
1.187     raeburn  1381:         my ($home_server_pick,$numlib) = 
                   1382:             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
                   1383:                                                       'default','hide');
                   1384:         if ($numlib > 1) {
                   1385:             $r->print("
1.185     raeburn  1386: <br />
1.187     raeburn  1387: $lt{'hs'}: $home_server_pick
                   1388: <br />");
                   1389:         } else {
                   1390:             $r->print($home_server_pick);
                   1391:         }
1.304     raeburn  1392:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.362     raeburn  1393:             $r->print('<br /><h3>'.
                   1394:                       &mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.304     raeburn  1395:                       &Apache::loncommon::start_data_table().
                   1396:                       &build_tools_display($ccuname,$ccdomain,
                   1397:                                            'requestcourses').
                   1398:                       &Apache::loncommon::end_data_table());
                   1399:         }
1.188     raeburn  1400:         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                   1401:                   $lt{'lg'}.'</h3>');
1.185     raeburn  1402:         my ($fixedauth,$varauth,$authmsg); 
1.193     raeburn  1403:         if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
                   1404:             my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
                   1405:             my ($rules,$ruleorder) = 
                   1406:                 &Apache::lonnet::inst_userrules($ccdomain,'username');
1.185     raeburn  1407:             if (ref($rules) eq 'HASH') {
1.193     raeburn  1408:                 if (ref($rules->{$matchedrule}) eq 'HASH') {
                   1409:                     my $authtype = $rules->{$matchedrule}{'authtype'};
1.185     raeburn  1410:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
1.190     raeburn  1411:                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.275     raeburn  1412:                     } else { 
1.193     raeburn  1413:                         my $authparm = $rules->{$matchedrule}{'authparm'};
1.273     raeburn  1414:                         $authmsg = $rules->{$matchedrule}{'authmsg'};
1.185     raeburn  1415:                         if ($authtype =~ /^krb(4|5)$/) {
                   1416:                             my $ver = $1;
                   1417:                             if ($authparm ne '') {
                   1418:                                 $fixedauth = <<"KERB"; 
                   1419: <input type="hidden" name="login" value="krb" />
                   1420: <input type="hidden" name="krbver" value="$ver" />
                   1421: <input type="hidden" name="krbarg" value="$authparm" />
                   1422: KERB
                   1423:                             }
                   1424:                         } else {
                   1425:                             $fixedauth = 
                   1426: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
1.193     raeburn  1427:                             if ($rules->{$matchedrule}{'authparmfixed'}) {
1.185     raeburn  1428:                                 $fixedauth .=    
                   1429: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
                   1430:                             } else {
1.273     raeburn  1431:                                 if ($authtype eq 'int') {
                   1432:                                     $varauth = '<br />'.
1.301     bisitz   1433: &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  1434:                                 } elsif ($authtype eq 'loc') {
                   1435:                                     $varauth = '<br />'.
                   1436: &mt('[_1] Local Authentication with argument [_2]','','<input type="text" name="'.$authtype.'arg" value="" />')."\n";
                   1437:                                 } else {
                   1438:                                     $varauth =
1.185     raeburn  1439: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
1.273     raeburn  1440:                                 }
1.185     raeburn  1441:                             }
                   1442:                         }
                   1443:                     }
                   1444:                 } else {
1.190     raeburn  1445:                     $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.185     raeburn  1446:                 }
                   1447:             }
                   1448:             if ($authmsg) {
                   1449:                 $r->print(<<ENDAUTH);
                   1450: $fixedauth
                   1451: $authmsg
                   1452: $varauth
                   1453: ENDAUTH
                   1454:             }
                   1455:         } else {
1.190     raeburn  1456:             $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc)); 
1.187     raeburn  1457:         }
1.427     raeburn  1458:         $r->print($portfolioform.$domroleform);
1.215     raeburn  1459:         if ($env{'form.action'} eq 'singlestudent') {
                   1460:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1461:                                             $permission,$crstype,$ccuname,
                   1462:                                             $ccdomain,$showcredits));
1.215     raeburn  1463:         }
                   1464:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.216     raeburn  1465:     } else { # user already exists
1.389     bisitz   1466: 	$r->print($start_page.$forminfo);
1.213     raeburn  1467:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1468:             if ($crstype eq 'Community') {
1.389     bisitz   1469:                 $title = &mt('Enroll one member: [_1] in domain [_2]',
                   1470:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1471:             } else {
1.389     bisitz   1472:                 $title = &mt('Enroll one student: [_1] in domain [_2]',
                   1473:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1474:             }
1.213     raeburn  1475:         } else {
1.418     raeburn  1476:             if ($permission->{'cusr'}) {
                   1477:                 $title = &mt('Modify existing user: [_1] in domain [_2]',
                   1478:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
                   1479:             } else {
                   1480:                 $title = &mt('Existing user: [_1] in domain [_2]',
1.389     bisitz   1481:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
1.418     raeburn  1482:             }
1.213     raeburn  1483:         }
1.389     bisitz   1484:         $r->print('<h2>'.$title.'</h2>'."\n");
                   1485:         $r->print('<div class="LC_left_float">');
1.393     raeburn  1486:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1487:                                          $inst_results{$ccuname.':'.$ccdomain}));
1.430     raeburn  1488:         if ((&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) ||
1.418     raeburn  1489:             (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) {
1.450     raeburn  1490:             $r->print('<br /><h3>'.&mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'."\n");
                   1491:             if (($env{'request.role.domain'} eq $ccdomain) ||
                   1492:                 (&Apache::lonnet::will_trust('reqcrs',$ccdomain,$env{'request.role.domain'}))) {
                   1493:                 $r->print(&Apache::loncommon::start_data_table());
                   1494:                 if ($env{'request.role.domain'} eq $ccdomain) {
                   1495:                     $r->print(&build_tools_display($ccuname,$ccdomain,'requestcourses'));
                   1496:                 } else {
1.444     raeburn  1497:                     $r->print(&coursereq_externaluser($ccuname,$ccdomain,
                   1498:                                                       $env{'request.role.domain'}));
                   1499:                 }
1.450     raeburn  1500:                 $r->print(&Apache::loncommon::end_data_table());
                   1501:             } else {
                   1502:                 $r->print(&mt('Domain configuration for this domain prohibits course creation by users from domain: "[_1]"',
                   1503:                               &Apache::lonnet::domain($ccdomain,'description')));
1.300     raeburn  1504:             }
1.275     raeburn  1505:         }
1.199     raeburn  1506:         $r->print('</div>');
1.427     raeburn  1507:         my @order = ('auth','quota','tools','requestauthor');
1.362     raeburn  1508:         my %user_text;
                   1509:         my ($isadv,$isauthor) = 
1.418     raeburn  1510:             &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
1.362     raeburn  1511:         if ((!$isauthor) && 
1.418     raeburn  1512:             ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) ||
                   1513:              (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) &&
1.430     raeburn  1514:              ($env{'request.role.domain'} eq $ccdomain)) {
1.362     raeburn  1515:             $user_text{'requestauthor'} = &domainrole_req($ccuname,$ccdomain);
                   1516:         }
1.451     raeburn  1517:         $user_text{'auth'} =  &user_authentication($ccuname,$ccdomain,$formname,$crstype,$permission);
1.267     raeburn  1518:         if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
1.418     raeburn  1519:             (&Apache::lonnet::allowed('mut',$ccdomain)) ||
                   1520:             (&Apache::lonnet::allowed('udp',$ccdomain))) {
1.188     raeburn  1521:             # Current user has quota modification privileges
1.378     raeburn  1522:             $user_text{'quota'} = &user_quotas($ccuname,$ccdomain);
1.267     raeburn  1523:         }
                   1524:         if (!&Apache::lonnet::allowed('mpq',$ccdomain)) {
                   1525:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
                   1526:                 my %lt=&Apache::lonlocal::texthash(
1.385     bisitz   1527:                     'dska'  => "Disk quotas for user's portfolio and Authoring Space",
                   1528:                     'youd'  => "You do not have privileges to modify the portfolio and/or Authoring Space quotas for this user.",
1.267     raeburn  1529:                     'ichr'  => "If a change is required, contact a domain coordinator for the domain",
                   1530:                 );
1.362     raeburn  1531:                 $user_text{'quota'} = <<ENDNOPORTPRIV;
1.188     raeburn  1532: <h3>$lt{'dska'}</h3>
                   1533: $lt{'youd'} $lt{'ichr'}: $ccdomain
                   1534: ENDNOPORTPRIV
1.267     raeburn  1535:             }
                   1536:         }
                   1537:         if (!&Apache::lonnet::allowed('mut',$ccdomain)) {
                   1538:             if (&Apache::lonnet::allowed('mut',$env{'request.role.domain'})) {
                   1539:                 my %lt=&Apache::lonlocal::texthash(
                   1540:                     'utav'  => "User Tools Availability",
1.361     raeburn  1541:                     'yodo'  => "You do not have privileges to modify Portfolio, Blog, WebDAV, or Personal Information Page settings for this user.",
1.267     raeburn  1542:                     'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1543:                 );
1.362     raeburn  1544:                 $user_text{'tools'} = <<ENDNOTOOLSPRIV;
1.267     raeburn  1545: <h3>$lt{'utav'}</h3>
                   1546: $lt{'yodo'} $lt{'ifch'}: $ccdomain
                   1547: ENDNOTOOLSPRIV
                   1548:             }
1.188     raeburn  1549:         }
1.362     raeburn  1550:         my $gotdiv = 0; 
                   1551:         foreach my $item (@order) {
                   1552:             if ($user_text{$item} ne '') {
                   1553:                 unless ($gotdiv) {
                   1554:                     $r->print('<div class="LC_left_float">');
                   1555:                     $gotdiv = 1;
                   1556:                 }
                   1557:                 $r->print('<br />'.$user_text{$item});
                   1558:             }
                   1559:         }
                   1560:         if ($env{'form.action'} eq 'singlestudent') {
                   1561:             unless ($gotdiv) {
                   1562:                 $r->print('<div class="LC_left_float">');
1.213     raeburn  1563:             }
1.375     raeburn  1564:             my $credits;
                   1565:             if ($showcredits) {
                   1566:                 $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1567:                 if ($credits eq '') {
                   1568:                     $credits = $defaultcredits;
                   1569:                 }
                   1570:             }
1.374     raeburn  1571:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1572:                                             $permission,$crstype,$ccuname,
                   1573:                                             $ccdomain,$showcredits));
1.374     raeburn  1574:         }
1.362     raeburn  1575:         if ($gotdiv) {
                   1576:             $r->print('</div><div class="LC_clear_float_footer"></div>');
1.188     raeburn  1577:         }
1.418     raeburn  1578:         my $statuses;
                   1579:         if (($context eq 'domain') && (&Apache::lonnet::allowed('udp',$ccdomain)) &&
                   1580:             (!&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1581:             $statuses = ['active'];
                   1582:         } elsif (($context eq 'course') && ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
                   1583:                  ($env{'request.course.sec'} &&
                   1584:                   &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'})))) {
1.430     raeburn  1585:             $statuses = ['active'];
1.418     raeburn  1586:         }
1.217     raeburn  1587:         if ($env{'form.action'} ne 'singlestudent') {
1.329     raeburn  1588:             &display_existing_roles($r,$ccuname,$ccdomain,\%inccourses,$context,
1.418     raeburn  1589:                                     $roledom,$crstype,$showcredits,$statuses);
1.217     raeburn  1590:         }
1.25      matthew  1591:     } ## End of new user/old user logic
1.218     raeburn  1592:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1593:         my $btntxt;
                   1594:         if ($crstype eq 'Community') {
                   1595:             $btntxt = &mt('Enroll Member');
                   1596:         } else {
                   1597:             $btntxt = &mt('Enroll Student');
                   1598:         }
                   1599:         $r->print('<br /><input type="button" value="'.$btntxt.'" onclick="setSections(this.form)" />'."\n");
1.418     raeburn  1600:     } elsif ($permission->{'cusr'}) {
1.393     raeburn  1601:         $r->print('<div class="LC_left_float">'.
                   1602:                   '<fieldset><legend>'.&mt('Add Roles').'</legend>');
1.218     raeburn  1603:         my $addrolesdisplay = 0;
                   1604:         if ($context eq 'domain' || $context eq 'author') {
                   1605:             $addrolesdisplay = &new_coauthor_roles($r,$ccuname,$ccdomain);
                   1606:         }
                   1607:         if ($context eq 'domain') {
1.357     raeburn  1608:             my $add_domainroles = &new_domain_roles($r,$ccdomain);
1.218     raeburn  1609:             if (!$addrolesdisplay) {
                   1610:                 $addrolesdisplay = $add_domainroles;
1.2       www      1611:             }
1.375     raeburn  1612:             $r->print(&course_level_dc($env{'request.role.domain'},$showcredits));
1.393     raeburn  1613:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1614:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setCourse()" />'."\n");
1.218     raeburn  1615:         } elsif ($context eq 'author') {
                   1616:             if ($addrolesdisplay) {
1.393     raeburn  1617:                 $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1618:                           '<br /><input type="button" value="'.&mt('Save').'"');
1.218     raeburn  1619:                 if ($newuser) {
1.301     bisitz   1620:                     $r->print(' onclick="auth_check()" \>'."\n");
1.218     raeburn  1621:                 } else {
1.461     raeburn  1622:                     $r->print(' onclick="this.form.submit()" \>'."\n");
1.218     raeburn  1623:                 }
1.188     raeburn  1624:             } else {
1.393     raeburn  1625:                 $r->print('</fieldset></div>'.
                   1626:                           '<div class="LC_clear_float_footer"></div>'.
                   1627:                           '<br /><a href="javascript:backPage(document.cu)">'.
1.218     raeburn  1628:                           &mt('Back to previous page').'</a>');
1.188     raeburn  1629:             }
                   1630:         } else {
1.375     raeburn  1631:             $r->print(&course_level_table(\%inccourses,$showcredits,$defaultcredits));
1.393     raeburn  1632:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1633:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setSections(this.form)" />'."\n");
1.188     raeburn  1634:         }
1.88      raeburn  1635:     }
1.188     raeburn  1636:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
1.179     raeburn  1637:     $r->print('<input type="hidden" name="currstate" value="" />');
1.393     raeburn  1638:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" /></form><br /><br />');
1.218     raeburn  1639:     return;
1.2       www      1640: }
1.1       www      1641: 
1.213     raeburn  1642: sub singleuser_breadcrumb {
1.422     raeburn  1643:     my ($crstype,$context,$domain) = @_;
1.213     raeburn  1644:     my %breadcrumb_text;
                   1645:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1646:         if ($crstype eq 'Community') {
                   1647:             $breadcrumb_text{'search'} = 'Enroll a member';
                   1648:         } else {
                   1649:             $breadcrumb_text{'search'} = 'Enroll a student';
                   1650:         }
1.422     raeburn  1651:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1652:         $breadcrumb_text{'modify'} = 'Set section/dates';
1.416     raeburn  1653:     } elsif ($env{'form.action'} eq 'accesslogs') {
                   1654:         $breadcrumb_text{'search'} = 'View access logs for a user';
1.422     raeburn  1655:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1656:         $breadcrumb_text{'activity'} = 'Activity';
                   1657:     } elsif (($env{'form.action'} eq 'singleuser') && ($context eq 'domain') &&
                   1658:              (!&Apache::lonnet::allowed('mau',$domain))) {
                   1659:         $breadcrumb_text{'search'} = "View user's roles";
                   1660:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1661:         $breadcrumb_text{'modify'} = 'User roles';
1.213     raeburn  1662:     } else {
1.229     raeburn  1663:         $breadcrumb_text{'search'} = 'Create/modify a user';
1.422     raeburn  1664:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1665:         $breadcrumb_text{'modify'} = 'Set user role';
1.213     raeburn  1666:     }
                   1667:     return %breadcrumb_text;
                   1668: }
                   1669: 
                   1670: sub date_sections_select {
1.375     raeburn  1671:     my ($context,$newuser,$formname,$permission,$crstype,$ccuname,$ccdomain,
                   1672:         $showcredits) = @_;
                   1673:     my $credits;
                   1674:     if ($showcredits) {
                   1675:         my $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1676:         $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1677:         if ($credits eq '') {
                   1678:             $credits = $defaultcredits;
                   1679:         }
                   1680:     }
1.213     raeburn  1681:     my $cid = $env{'request.course.id'};
                   1682:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity($cid);
                   1683:     my $date_table = '<h3>'.&mt('Starting and Ending Dates').'</h3>'."\n".
                   1684:         &Apache::lonuserutils::date_setting_table(undef,undef,$context,
                   1685:                                                   undef,$formname,$permission);
                   1686:     my $rowtitle = 'Section';
1.375     raeburn  1687:     my $secbox = '<h3>'.&mt('Section and Credits').'</h3>'."\n".
1.213     raeburn  1688:         &Apache::lonuserutils::section_picker($cdom,$cnum,'st',$rowtitle,
1.375     raeburn  1689:                                               $permission,$context,'',$crstype,
                   1690:                                               $showcredits,$credits);
1.213     raeburn  1691:     my $output = $date_table.$secbox;
                   1692:     return $output;
                   1693: }
                   1694: 
1.216     raeburn  1695: sub validation_javascript {
1.375     raeburn  1696:     my ($context,$ccdomain,$pjump_def,$crstype,$groupslist,$newuser,$formname,
1.216     raeburn  1697:         $loaditem) = @_;
                   1698:     my $dc_setcourse_code = '';
                   1699:     my $nondc_setsection_code = '';
                   1700:     if ($context eq 'domain') {
                   1701:         my $dcdom = $env{'request.role.domain'};
                   1702:         $loaditem->{'onload'} = "document.cu.coursedesc.value='';";
1.227     raeburn  1703:         $dc_setcourse_code = 
                   1704:             &Apache::lonuserutils::dc_setcourse_js('cu','singleuser',$context);
1.216     raeburn  1705:     } else {
1.227     raeburn  1706:         my $checkauth; 
                   1707:         if (($newuser) || (&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1708:             $checkauth = 1;
                   1709:         }
                   1710:         if ($context eq 'course') {
                   1711:             $nondc_setsection_code =
                   1712:                 &Apache::lonuserutils::setsections_javascript($formname,$groupslist,
1.375     raeburn  1713:                                                               undef,$checkauth,
                   1714:                                                               $crstype);
1.227     raeburn  1715:         }
                   1716:         if ($checkauth) {
                   1717:             $nondc_setsection_code .= 
                   1718:                 &Apache::lonuserutils::verify_authen($formname,$context);
                   1719:         }
1.216     raeburn  1720:     }
                   1721:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                   1722:                                    $nondc_setsection_code,$groupslist);
                   1723:     my ($jsback,$elements) = &crumb_utilities();
                   1724:     $js .= "\n".
1.301     bisitz   1725:            '<script type="text/javascript">'."\n".
                   1726:            '// <![CDATA['."\n".
                   1727:            $jsback."\n".
                   1728:            '// ]]>'."\n".
                   1729:            '</script>'."\n";
1.216     raeburn  1730:     return $js;
                   1731: }
                   1732: 
1.217     raeburn  1733: sub display_existing_roles {
1.375     raeburn  1734:     my ($r,$ccuname,$ccdomain,$inccourses,$context,$roledom,$crstype,
1.418     raeburn  1735:         $showcredits,$statuses) = @_;
1.329     raeburn  1736:     my $now=time;
1.418     raeburn  1737:     my $showall = 1;
                   1738:     my ($showexpired,$showactive);
                   1739:     if ((ref($statuses) eq 'ARRAY') && (@{$statuses} > 0)) {
                   1740:         $showall = 0;
                   1741:         if (grep(/^expired$/,@{$statuses})) {
                   1742:             $showexpired = 1;
                   1743:         }
                   1744:         if (grep(/^active$/,@{$statuses})) {
                   1745:             $showactive = 1;
                   1746:         }
                   1747:         if ($showexpired && $showactive) {
                   1748:             $showall = 1;
                   1749:         }
                   1750:     }
1.329     raeburn  1751:     my %lt=&Apache::lonlocal::texthash(
1.217     raeburn  1752:                     'rer'  => "Existing Roles",
                   1753:                     'rev'  => "Revoke",
                   1754:                     'del'  => "Delete",
                   1755:                     'ren'  => "Re-Enable",
                   1756:                     'rol'  => "Role",
                   1757:                     'ext'  => "Extent",
1.375     raeburn  1758:                     'crd'  => "Credits",
1.217     raeburn  1759:                     'sta'  => "Start",
                   1760:                     'end'  => "End",
                   1761:                                        );
1.329     raeburn  1762:     my (%rolesdump,%roletext,%sortrole,%roleclass,%rolepriv);
                   1763:     if ($context eq 'course' || $context eq 'author') {
                   1764:         my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
                   1765:         my %roleshash = 
                   1766:             &Apache::lonnet::get_my_roles($ccuname,$ccdomain,'userroles',
                   1767:                               ['active','previous','future'],\@roles,$roledom,1);
                   1768:         foreach my $key (keys(%roleshash)) {
                   1769:             my ($start,$end) = split(':',$roleshash{$key});
                   1770:             next if ($start eq '-1' || $end eq '-1');
                   1771:             my ($rnum,$rdom,$role,$sec) = split(':',$key);
                   1772:             if ($context eq 'course') {
                   1773:                 next unless (($rnum eq $env{'course.'.$env{'request.course.id'}.'.num'})
                   1774:                              && ($rdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}));
                   1775:             } elsif ($context eq 'author') {
                   1776:                 next unless (($rnum eq $env{'user.name'}) && ($rdom eq $env{'request.role.domain'}));
                   1777:             }
                   1778:             my ($newkey,$newvalue,$newrole);
                   1779:             $newkey = '/'.$rdom.'/'.$rnum;
                   1780:             if ($sec ne '') {
                   1781:                 $newkey .= '/'.$sec;
                   1782:             }
                   1783:             $newvalue = $role;
                   1784:             if ($role =~ /^cr/) {
                   1785:                 $newrole = 'cr';
                   1786:             } else {
                   1787:                 $newrole = $role;
                   1788:             }
                   1789:             $newkey .= '_'.$newrole;
                   1790:             if ($start ne '' && $end ne '') {
                   1791:                 $newvalue .= '_'.$end.'_'.$start;
1.335     raeburn  1792:             } elsif ($end ne '') {
                   1793:                 $newvalue .= '_'.$end;
1.329     raeburn  1794:             }
                   1795:             $rolesdump{$newkey} = $newvalue;
                   1796:         }
                   1797:     } else {
1.360     raeburn  1798:         %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
1.329     raeburn  1799:     }
                   1800:     # Build up table of user roles to allow revocation and re-enabling of roles.
                   1801:     my ($tmp) = keys(%rolesdump);
                   1802:     return if ($tmp =~ /^(con_lost|error)/i);
                   1803:     foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
                   1804:                                 my $b1=join('_',(split('_',$b))[1,0]);
                   1805:                                 return $a1 cmp $b1;
                   1806:                             } keys(%rolesdump)) {
                   1807:         next if ($area =~ /^rolesdef/);
                   1808:         my $envkey=$area;
                   1809:         my $role = $rolesdump{$area};
                   1810:         my $thisrole=$area;
                   1811:         $area =~ s/\_\w\w$//;
                   1812:         my ($role_code,$role_end_time,$role_start_time) =
                   1813:             split(/_/,$role);
1.418     raeburn  1814:         my $active=1;
                   1815:         $active=0 if (($role_end_time) && ($now>$role_end_time));
                   1816:         if ($active) {
                   1817:             next unless($showall || $showactive);
                   1818:         } else {
1.430     raeburn  1819:             next unless($showall || $showexpired);
1.418     raeburn  1820:         }
1.217     raeburn  1821: # Is this a custom role? Get role owner and title.
1.329     raeburn  1822:         my ($croleudom,$croleuname,$croletitle)=
                   1823:             ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
                   1824:         my $allowed=0;
                   1825:         my $delallowed=0;
                   1826:         my $sortkey=$role_code;
                   1827:         my $class='Unknown';
1.375     raeburn  1828:         my $credits='';
1.418     raeburn  1829:         my $csec;
1.421     raeburn  1830:         if ($area =~ m{^/($match_domain)/($match_courseid)}) {
1.329     raeburn  1831:             $class='Course';
                   1832:             my ($coursedom,$coursedir) = ($1,$2);
                   1833:             my $cid = $1.'_'.$2;
                   1834:             # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
1.421     raeburn  1835:             next if ($envkey =~ m{^/$match_domain/$match_courseid/[A-Za-z0-9]+_gr$});
1.329     raeburn  1836:             my %coursedata=
                   1837:                 &Apache::lonnet::coursedescription($cid);
                   1838:             if ($coursedir =~ /^$match_community$/) {
                   1839:                 $class='Community';
                   1840:             }
                   1841:             $sortkey.="\0$coursedom";
                   1842:             my $carea;
                   1843:             if (defined($coursedata{'description'})) {
                   1844:                 $carea=$coursedata{'description'}.
                   1845:                     '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
                   1846:     &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$coursedir,$coursedom);
                   1847:                 $sortkey.="\0".$coursedata{'description'};
                   1848:             } else {
                   1849:                 if ($class eq 'Community') {
                   1850:                     $carea=&mt('Unavailable community').': '.$area;
                   1851:                     $sortkey.="\0".&mt('Unavailable community').': '.$area;
1.217     raeburn  1852:                 } else {
                   1853:                     $carea=&mt('Unavailable course').': '.$area;
                   1854:                     $sortkey.="\0".&mt('Unavailable course').': '.$area;
                   1855:                 }
1.329     raeburn  1856:             }
                   1857:             $sortkey.="\0$coursedir";
                   1858:             $inccourses->{$cid}=1;
1.375     raeburn  1859:             if (($showcredits) && ($class eq 'Course') && ($role_code eq 'st')) {
                   1860:                 my $defaultcredits = $coursedata{'internal.defaultcredits'};
                   1861:                 $credits =
                   1862:                     &get_user_credits($ccuname,$ccdomain,$defaultcredits,
                   1863:                                       $coursedom,$coursedir);
                   1864:                 if ($credits eq '') {
                   1865:                     $credits = $defaultcredits;
                   1866:                 }
                   1867:             }
1.329     raeburn  1868:             if ((&Apache::lonnet::allowed('c'.$role_code,$coursedom.'/'.$coursedir)) ||
                   1869:                 (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1870:                 $allowed=1;
                   1871:             }
                   1872:             unless ($allowed) {
1.365     raeburn  1873:                 my $isowner = &Apache::lonuserutils::is_courseowner($cid,$coursedata{'internal.courseowner'});
1.329     raeburn  1874:                 if ($isowner) {
                   1875:                     if (($role_code eq 'co') && ($class eq 'Community')) {
                   1876:                         $allowed = 1;
                   1877:                     } elsif (($role_code eq 'cc') && ($class eq 'Course')) {
                   1878:                         $allowed = 1;
                   1879:                     }
1.217     raeburn  1880:                 }
1.329     raeburn  1881:             } 
                   1882:             if ((&Apache::lonnet::allowed('dro',$coursedom)) ||
                   1883:                 (&Apache::lonnet::allowed('dro',$ccdomain))) {
                   1884:                 $delallowed=1;
                   1885:             }
1.217     raeburn  1886: # - custom role. Needs more info, too
1.329     raeburn  1887:             if ($croletitle) {
                   1888:                 if (&Apache::lonnet::allowed('ccr',$coursedom.'/'.$coursedir)) {
                   1889:                     $allowed=1;
                   1890:                     $thisrole.='.'.$role_code;
1.217     raeburn  1891:                 }
1.329     raeburn  1892:             }
1.418     raeburn  1893:             if ($area=~m{^/($match_domain/$match_courseid/(\w+))}) {
                   1894:                 $csec = $2;
                   1895:                 $carea.='<br />'.&mt('Section: [_1]',$csec);
                   1896:                 $sortkey.="\0$csec";
1.329     raeburn  1897:                 if (!$allowed) {
1.418     raeburn  1898:                     if ($env{'request.course.sec'} eq $csec) {
                   1899:                         if (&Apache::lonnet::allowed('c'.$role_code,$1)) {
1.329     raeburn  1900:                             $allowed = 1;
1.217     raeburn  1901:                         }
                   1902:                     }
                   1903:                 }
1.329     raeburn  1904:             }
                   1905:             $area=$carea;
                   1906:         } else {
                   1907:             $sortkey.="\0".$area;
                   1908:             # Determine if current user is able to revoke privileges
                   1909:             if ($area=~m{^/($match_domain)/}) {
                   1910:                 if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
                   1911:                    (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1912:                    $allowed=1;
1.217     raeburn  1913:                 }
1.329     raeburn  1914:                 if (((&Apache::lonnet::allowed('dro',$1))  ||
                   1915:                     (&Apache::lonnet::allowed('dro',$ccdomain))) &&
                   1916:                     ($role_code ne 'dc')) {
                   1917:                     $delallowed=1;
1.217     raeburn  1918:                 }
1.329     raeburn  1919:             } else {
                   1920:                 if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
1.217     raeburn  1921:                     $allowed=1;
                   1922:                 }
                   1923:             }
1.363     raeburn  1924:             if ($role_code eq 'ca' || $role_code eq 'au' || $role_code eq 'aa') {
1.377     raeburn  1925:                 $class='Authoring Space';
1.329     raeburn  1926:             } elsif ($role_code eq 'su') {
                   1927:                 $class='System';
1.217     raeburn  1928:             } else {
1.329     raeburn  1929:                 $class='Domain';
1.217     raeburn  1930:             }
1.329     raeburn  1931:         }
                   1932:         if (($role_code eq 'ca') || ($role_code eq 'aa')) {
                   1933:             $area=~m{/($match_domain)/($match_username)};
                   1934:             if (&Apache::lonuserutils::authorpriv($2,$1)) {
                   1935:                 $allowed=1;
1.217     raeburn  1936:             } else {
1.329     raeburn  1937:                 $allowed=0;
1.217     raeburn  1938:             }
1.329     raeburn  1939:         }
                   1940:         my $row = '';
1.418     raeburn  1941:         if ($showall) {
                   1942:             $row.= '<td>';
                   1943:             if (($active) && ($allowed)) {
                   1944:                 $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
                   1945:             } else {
                   1946:                 if ($active) {
                   1947:                     $row.='&nbsp;';
                   1948:                 } else {
                   1949:                     $row.=&mt('expired or revoked');
                   1950:                 }
                   1951:             }
                   1952:             $row.='</td><td>';
                   1953:             if ($allowed && !$active) {
                   1954:                 $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
                   1955:             } else {
                   1956:                 $row.='&nbsp;';
                   1957:             }
                   1958:             $row.='</td><td>';
                   1959:             if ($delallowed) {
                   1960:                 $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
1.217     raeburn  1961:             } else {
1.418     raeburn  1962:                 $row.='&nbsp;';
1.217     raeburn  1963:             }
1.430     raeburn  1964:             $row.= '</td>';
1.329     raeburn  1965:         }
                   1966:         my $plaintext='';
                   1967:         if (!$croletitle) {
1.375     raeburn  1968:             $plaintext=&Apache::lonnet::plaintext($role_code,$class);
                   1969:             if (($showcredits) && ($credits ne '')) {
                   1970:                 $plaintext .= '<br/ ><span class="LC_nobreak">'.
                   1971:                               '<span class="LC_fontsize_small">'.
                   1972:                               &mt('Credits: [_1]',$credits).
                   1973:                               '</span></span>';
                   1974:             }
1.329     raeburn  1975:         } else {
                   1976:             $plaintext=
1.395     bisitz   1977:                 &mt('Custom role [_1][_2]defined by [_3]',
1.346     bisitz   1978:                         '"'.$croletitle.'"',
                   1979:                         '<br />',
                   1980:                         $croleuname.':'.$croleudom);
1.329     raeburn  1981:         }
1.418     raeburn  1982:         $row.= '<td>'.$plaintext.'</td>'.
                   1983:                '<td>'.$area.'</td>'.
                   1984:                '<td>'.($role_start_time?&Apache::lonlocal::locallocaltime($role_start_time)
                   1985:                                             : '&nbsp;' ).'</td>'.
                   1986:                '<td>'.($role_end_time  ?&Apache::lonlocal::locallocaltime($role_end_time)
                   1987:                                             : '&nbsp;' ).'</td>';
1.329     raeburn  1988:         $sortrole{$sortkey}=$envkey;
                   1989:         $roletext{$envkey}=$row;
                   1990:         $roleclass{$envkey}=$class;
1.418     raeburn  1991:         if ($allowed) {
                   1992:             $rolepriv{$envkey}='edit';
                   1993:         } else {
                   1994:             if ($context eq 'domain') {
1.420     raeburn  1995:                 if ((&Apache::lonnet::allowed('vur',$ccdomain)) &&
1.421     raeburn  1996:                     ($envkey=~m{^/$ccdomain/})) {
1.418     raeburn  1997:                     $rolepriv{$envkey}='view';
                   1998:                 }
                   1999:             } elsif ($context eq 'course') {
                   2000:                 if ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
                   2001:                     ($env{'request.course.sec'} && ($env{'request.course.sec'} eq $csec) &&
                   2002:                      &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'}))) {
                   2003:                     $rolepriv{$envkey}='view';
                   2004:                 }
                   2005:             }
                   2006:         }
1.329     raeburn  2007:     } # end of foreach        (table building loop)
                   2008: 
                   2009:     my $rolesdisplay = 0;
                   2010:     my %output = ();
1.377     raeburn  2011:     foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  2012:         $output{$type} = '';
                   2013:         foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                   2014:             if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) {
                   2015:                  $output{$type}.=
                   2016:                       &Apache::loncommon::start_data_table_row().
                   2017:                       $roletext{$sortrole{$which}}.
                   2018:                       &Apache::loncommon::end_data_table_row();
1.217     raeburn  2019:             }
1.329     raeburn  2020:         }
                   2021:         unless($output{$type} eq '') {
                   2022:             $output{$type} = '<tr class="LC_info_row">'.
                   2023:                       "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
                   2024:                       $output{$type};
                   2025:             $rolesdisplay = 1;
                   2026:         }
                   2027:     }
                   2028:     if ($rolesdisplay == 1) {
                   2029:         my $contextrole='';
                   2030:         if ($env{'request.course.id'}) {
                   2031:             if (&Apache::loncommon::course_type() eq 'Community') {
                   2032:                 $contextrole = &mt('Existing Roles in this Community');
1.290     bisitz   2033:             } else {
1.329     raeburn  2034:                 $contextrole = &mt('Existing Roles in this Course');
1.290     bisitz   2035:             }
1.329     raeburn  2036:         } elsif ($env{'request.role'} =~ /^au\./) {
1.377     raeburn  2037:             $contextrole = &mt('Existing Co-Author Roles in your Authoring Space');
1.329     raeburn  2038:         } else {
1.418     raeburn  2039:             if ($showall) {
                   2040:                 $contextrole = &mt('Existing Roles in this Domain');
                   2041:             } elsif ($showactive) {
                   2042:                 $contextrole = &mt('Unexpired Roles in this Domain');
                   2043:             } elsif ($showexpired) {
                   2044:                 $contextrole = &mt('Expired or Revoked Roles in this Domain');
                   2045:             }
1.329     raeburn  2046:         }
1.393     raeburn  2047:         $r->print('<div class="LC_left_float">'.
1.375     raeburn  2048: '<fieldset><legend>'.$contextrole.'</legend>'.
1.217     raeburn  2049: &Apache::loncommon::start_data_table("LC_createuser").
1.418     raeburn  2050: &Apache::loncommon::start_data_table_header_row());
                   2051:         if ($showall) {
                   2052:             $r->print(
1.419     raeburn  2053: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}.'</th>'
1.418     raeburn  2054:             );
                   2055:         } elsif ($showexpired) {
                   2056:             $r->print('<th>'.$lt{'rev'}.'</th>');
                   2057:         }
                   2058:         $r->print(
1.419     raeburn  2059: '<th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.'</th>'.
                   2060: '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
1.217     raeburn  2061: &Apache::loncommon::end_data_table_header_row());
1.377     raeburn  2062:         foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  2063:             if ($output{$type}) {
                   2064:                 $r->print($output{$type}."\n");
1.217     raeburn  2065:             }
                   2066:         }
1.375     raeburn  2067:         $r->print(&Apache::loncommon::end_data_table().
                   2068:                   '</fieldset></div>');
1.329     raeburn  2069:     }
1.217     raeburn  2070:     return;
                   2071: }
                   2072: 
1.218     raeburn  2073: sub new_coauthor_roles {
                   2074:     my ($r,$ccuname,$ccdomain) = @_;
                   2075:     my $addrolesdisplay = 0;
                   2076:     #
                   2077:     # Co-Author
                   2078:     #
                   2079:     if (&Apache::lonuserutils::authorpriv($env{'user.name'},
                   2080:                                           $env{'request.role.domain'}) &&
                   2081:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
                   2082:         # No sense in assigning co-author role to yourself
                   2083:         $addrolesdisplay = 1;
                   2084:         my $cuname=$env{'user.name'};
                   2085:         my $cudom=$env{'request.role.domain'};
                   2086:         my %lt=&Apache::lonlocal::texthash(
1.377     raeburn  2087:                     'cs'   => "Authoring Space",
1.218     raeburn  2088:                     'act'  => "Activate",
                   2089:                     'rol'  => "Role",
                   2090:                     'ext'  => "Extent",
                   2091:                     'sta'  => "Start",
                   2092:                     'end'  => "End",
                   2093:                     'cau'  => "Co-Author",
                   2094:                     'caa'  => "Assistant Co-Author",
                   2095:                     'ssd'  => "Set Start Date",
                   2096:                     'sed'  => "Set End Date"
                   2097:                                        );
                   2098:         $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n".
                   2099:                   &Apache::loncommon::start_data_table()."\n".
                   2100:                   &Apache::loncommon::start_data_table_header_row()."\n".
                   2101:                   '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
                   2102:                   '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
                   2103:                   '<th>'.$lt{'end'}.'</th>'."\n".
                   2104:                   &Apache::loncommon::end_data_table_header_row()."\n".
                   2105:                   &Apache::loncommon::start_data_table_row().'
                   2106:            <td>
1.291     bisitz   2107:             <input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_ca" />
1.218     raeburn  2108:            </td>
                   2109:            <td>'.$lt{'cau'}.'</td>
                   2110:            <td>'.$cudom.'_'.$cuname.'</td>
                   2111:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
                   2112:              <a href=
                   2113: "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>
                   2114: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
                   2115: <a href=
                   2116: "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".
                   2117:               &Apache::loncommon::end_data_table_row()."\n".
                   2118:               &Apache::loncommon::start_data_table_row()."\n".
1.291     bisitz   2119: '<td><input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
1.218     raeburn  2120: <td>'.$lt{'caa'}.'</td>
                   2121: <td>'.$cudom.'_'.$cuname.'</td>
                   2122: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
                   2123: <a href=
                   2124: "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>
                   2125: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
                   2126: <a href=
                   2127: "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".
                   2128:              &Apache::loncommon::end_data_table_row()."\n".
                   2129:              &Apache::loncommon::end_data_table());
                   2130:     } elsif ($env{'request.role'} =~ /^au\./) {
                   2131:         if (!(&Apache::lonuserutils::authorpriv($env{'user.name'},
                   2132:                                                 $env{'request.role.domain'}))) {
                   2133:             $r->print('<span class="LC_error">'.
                   2134:                       &mt('You do not have privileges to assign co-author roles.').
                   2135:                       '</span>');
                   2136:         } elsif (($env{'user.name'} eq $ccuname) &&
                   2137:              ($env{'user.domain'} eq $ccdomain)) {
1.377     raeburn  2138:             $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  2139:         }
                   2140:     }
                   2141:     return $addrolesdisplay;;
                   2142: }
                   2143: 
                   2144: sub new_domain_roles {
1.357     raeburn  2145:     my ($r,$ccdomain) = @_;
1.218     raeburn  2146:     my $addrolesdisplay = 0;
                   2147:     #
                   2148:     # Domain level
                   2149:     #
                   2150:     my $num_domain_level = 0;
                   2151:     my $domaintext =
                   2152:     '<h4>'.&mt('Domain Level').'</h4>'.
                   2153:     &Apache::loncommon::start_data_table().
                   2154:     &Apache::loncommon::start_data_table_header_row().
                   2155:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
                   2156:     &mt('Extent').'</th>'.
                   2157:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
                   2158:     &Apache::loncommon::end_data_table_header_row();
1.312     raeburn  2159:     my @allroles = &Apache::lonuserutils::roles_by_context('domain');
1.445     raeburn  2160:     my $uprimary = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
                   2161:     my $uintdom = &Apache::lonnet::internet_dom($uprimary);
1.218     raeburn  2162:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
1.312     raeburn  2163:         foreach my $role (@allroles) {
                   2164:             next if ($role eq 'ad');
1.357     raeburn  2165:             next if (($role eq 'au') && ($ccdomain ne $thisdomain));
1.218     raeburn  2166:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
1.445     raeburn  2167:                if ($role eq 'dc') {
                   2168:                    unless ($thisdomain eq $env{'request.role.domain'}) {
                   2169:                        my $domprim = &Apache::lonnet::domain($thisdomain,'primary');
                   2170:                        my $intdom = &Apache::lonnet::internet_dom($domprim);
                   2171:                        next unless ($uintdom eq $intdom);
                   2172:                    }
                   2173:                }
1.218     raeburn  2174:                my $plrole=&Apache::lonnet::plaintext($role);
                   2175:                my %lt=&Apache::lonlocal::texthash(
                   2176:                     'ssd'  => "Set Start Date",
                   2177:                     'sed'  => "Set End Date"
                   2178:                                        );
                   2179:                $num_domain_level ++;
                   2180:                $domaintext .=
                   2181: &Apache::loncommon::start_data_table_row().
1.291     bisitz   2182: '<td><input type="checkbox" name="act_'.$thisdomain.'_'.$role.'" /></td>
1.218     raeburn  2183: <td>'.$plrole.'</td>
                   2184: <td>'.$thisdomain.'</td>
                   2185: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
                   2186: <a href=
                   2187: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
                   2188: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
                   2189: <a href=
                   2190: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
                   2191: &Apache::loncommon::end_data_table_row();
                   2192:             }
                   2193:         }
                   2194:     }
                   2195:     $domaintext.= &Apache::loncommon::end_data_table();
                   2196:     if ($num_domain_level > 0) {
                   2197:         $r->print($domaintext);
                   2198:         $addrolesdisplay = 1;
                   2199:     }
                   2200:     return $addrolesdisplay;
                   2201: }
                   2202: 
1.188     raeburn  2203: sub user_authentication {
1.451     raeburn  2204:     my ($ccuname,$ccdomain,$formname,$crstype,$permission) = @_;
1.188     raeburn  2205:     my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
1.227     raeburn  2206:     my $outcome;
1.418     raeburn  2207:     my %lt=&Apache::lonlocal::texthash(
                   2208:                    'err'   => "ERROR",
                   2209:                    'uuas'  => "This user has an unrecognized authentication scheme",
                   2210:                    'adcs'  => "Please alert a domain coordinator of this situation",
                   2211:                    'sldb'  => "Please specify login data below",
                   2212:                    'ld'    => "Login Data"
                   2213:     );
1.188     raeburn  2214:     # Check for a bad authentication type
1.449     raeburn  2215:     if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth|lti):/) {
1.188     raeburn  2216:         # bad authentication scheme
                   2217:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
1.227     raeburn  2218:             &initialize_authen_forms($ccdomain,$formname);
                   2219: 
1.190     raeburn  2220:             my $choices = &Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc);
1.188     raeburn  2221:             $outcome = <<ENDBADAUTH;
                   2222: <script type="text/javascript" language="Javascript">
1.301     bisitz   2223: // <![CDATA[
1.188     raeburn  2224: $loginscript
1.301     bisitz   2225: // ]]>
1.188     raeburn  2226: </script>
                   2227: <span class="LC_error">$lt{'err'}:
                   2228: $lt{'uuas'} ($currentauth). $lt{'sldb'}.</span>
                   2229: <h3>$lt{'ld'}</h3>
                   2230: $choices
                   2231: ENDBADAUTH
                   2232:         } else {
                   2233:             # This user is not allowed to modify the user's
                   2234:             # authentication scheme, so just notify them of the problem
                   2235:             $outcome = <<ENDBADAUTH;
                   2236: <span class="LC_error"> $lt{'err'}: 
                   2237: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
                   2238: </span>
                   2239: ENDBADAUTH
                   2240:         }
                   2241:     } else { # Authentication type is valid
1.418     raeburn  2242:         
1.227     raeburn  2243:         &initialize_authen_forms($ccdomain,$formname,$currentauth,'modifyuser');
1.205     raeburn  2244:         my ($authformcurrent,$can_modify,@authform_others) =
1.188     raeburn  2245:             &modify_login_block($ccdomain,$currentauth);
                   2246:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2247:             # Current user has login modification privileges
                   2248:             $outcome =
                   2249:                        '<script type="text/javascript" language="Javascript">'."\n".
1.301     bisitz   2250:                        '// <![CDATA['."\n".
1.188     raeburn  2251:                        $loginscript."\n".
1.301     bisitz   2252:                        '// ]]>'."\n".
1.188     raeburn  2253:                        '</script>'."\n".
                   2254:                        '<h3>'.$lt{'ld'}.'</h3>'.
                   2255:                        &Apache::loncommon::start_data_table().
1.205     raeburn  2256:                        &Apache::loncommon::start_data_table_row().
1.188     raeburn  2257:                        '<td>'.$authformnop;
1.418     raeburn  2258:             if (($can_modify) && (&Apache::lonnet::allowed('mau',$ccdomain))) {
1.188     raeburn  2259:                 $outcome .= '</td>'."\n".
                   2260:                             &Apache::loncommon::end_data_table_row().
                   2261:                             &Apache::loncommon::start_data_table_row().
                   2262:                             '<td>'.$authformcurrent.'</td>'.
                   2263:                             &Apache::loncommon::end_data_table_row()."\n";
                   2264:             } else {
1.200     raeburn  2265:                 $outcome .= '&nbsp;('.$authformcurrent.')</td>'.
                   2266:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  2267:             }
1.418     raeburn  2268:             if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2269:                 foreach my $item (@authform_others) { 
                   2270:                     $outcome .= &Apache::loncommon::start_data_table_row().
                   2271:                                 '<td>'.$item.'</td>'.
                   2272:                                 &Apache::loncommon::end_data_table_row()."\n";
                   2273:                 }
1.188     raeburn  2274:             }
1.205     raeburn  2275:             $outcome .= &Apache::loncommon::end_data_table();
1.188     raeburn  2276:         } else {
1.451     raeburn  2277:             if (($currentauth =~ /^internal:/) &&
                   2278:                 (&Apache::lonuserutils::can_change_internalpass($ccuname,$ccdomain,$crstype,$permission))) {
                   2279:                 $outcome = <<"ENDJS";
                   2280: <script type="text/javascript">
                   2281: // <![CDATA[
                   2282: function togglePwd(form) {
                   2283:     if (form.newintpwd.length) {
                   2284:         if (document.getElementById('LC_ownersetpwd')) {
                   2285:             for (var i=0; i<form.newintpwd.length; i++) {
                   2286:                 if (form.newintpwd[i].checked) {
                   2287:                     if (form.newintpwd[i].value == 1) {
                   2288:                         document.getElementById('LC_ownersetpwd').style.display = 'inline-block';
                   2289:                     } else {
                   2290:                         document.getElementById('LC_ownersetpwd').style.display = 'none';
                   2291:                     }
                   2292:                 }
                   2293:             }
                   2294:         }
                   2295:     }
                   2296: }
                   2297: // ]]>
                   2298: </script>
                   2299: ENDJS
                   2300: 
                   2301:                 $outcome .= '<h3>'.$lt{'ld'}.'</h3>'.
                   2302:                             &Apache::loncommon::start_data_table().
                   2303:                             &Apache::loncommon::start_data_table_row().
                   2304:                             '<td>'.&mt('Internally authenticated').'<br />'.&mt("Change user's password?").
                   2305:                             '<label><input type="radio" name="newintpwd" value="0" checked="checked" onclick="togglePwd(this.form);" />'.
                   2306:                             &mt('No').'</label>'.('&nbsp;'x2).
                   2307:                             '<label><input type="radio" name="newintpwd" value="1" onclick="togglePwd(this.form);" />'.&mt('Yes').'</label>'.
                   2308:                             '<div id="LC_ownersetpwd" style="display:none">'.
                   2309:                             '&nbsp;&nbsp;'.&mt('Password').' <input type="password" size="15" name="intarg" value="" />'.
                   2310:                             '<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>'.
                   2311:                             &Apache::loncommon::end_data_table_row().
                   2312:                             &Apache::loncommon::end_data_table();
                   2313:             }
1.418     raeburn  2314:             if (&Apache::lonnet::allowed('udp',$ccdomain)) {
                   2315:                 # Current user has rights to view domain preferences for user's domain
                   2316:                 my $result;
                   2317:                 if ($currentauth =~ /^krb(4|5):([^:]*)$/) {
                   2318:                     my ($krbver,$krbrealm) = ($1,$2);
                   2319:                     if ($krbrealm eq '') {
                   2320:                         $result = &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
                   2321:                     } else {
                   2322:                         $result = &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
1.426     raeburn  2323:                                       $krbrealm,$krbver);
1.418     raeburn  2324:                     }
                   2325:                 } elsif ($currentauth =~ /^internal:/) {
                   2326:                     $result = &mt('Currently internally authenticated.');
                   2327:                 } elsif ($currentauth =~ /^localauth:/) {
                   2328:                     $result = &mt('Currently using local (institutional) authentication.');
                   2329:                 } elsif ($currentauth =~ /^unix:/) {
                   2330:                     $result = &mt('Currently Filesystem Authenticated.');
1.449     raeburn  2331:                 } elsif ($currentauth =~ /^lti:/) {
1.451     raeburn  2332:                     $result = &mt('Currently LTI authenticated.');
1.418     raeburn  2333:                 }
                   2334:                 $outcome = '<h3>'.$lt{'ld'}.'</h3>'.
                   2335:                            &Apache::loncommon::start_data_table().
                   2336:                            &Apache::loncommon::start_data_table_row().
                   2337:                            '<td>'.$result.'</td>'.
                   2338:                            &Apache::loncommon::end_data_table_row()."\n".
                   2339:                            &Apache::loncommon::end_data_table();
                   2340:             } elsif (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
1.188     raeburn  2341:                 my %lt=&Apache::lonlocal::texthash(
                   2342:                            'ccld'  => "Change Current Login Data",
                   2343:                            'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
                   2344:                            'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   2345:                 );
                   2346:                 $outcome .= <<ENDNOPRIV;
                   2347: <h3>$lt{'ccld'}</h3>
                   2348: $lt{'yodo'} $lt{'ifch'}: $ccdomain
1.235     raeburn  2349: <input type="hidden" name="login" value="nochange" />
1.188     raeburn  2350: ENDNOPRIV
                   2351:             }
                   2352:         }
                   2353:     }  ## End of "check for bad authentication type" logic
                   2354:     return $outcome;
                   2355: }
                   2356: 
1.187     raeburn  2357: sub modify_login_block {
                   2358:     my ($dom,$currentauth) = @_;
                   2359:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2360:     my ($authnum,%can_assign) =
                   2361:         &Apache::loncommon::get_assignable_auth($dom);
1.205     raeburn  2362:     my ($authformcurrent,@authform_others,$show_override_msg);
1.187     raeburn  2363:     if ($currentauth=~/^krb(4|5):/) {
                   2364:         $authformcurrent=$authformkrb;
                   2365:         if ($can_assign{'int'}) {
1.205     raeburn  2366:             push(@authform_others,$authformint);
1.187     raeburn  2367:         }
                   2368:         if ($can_assign{'loc'}) {
1.205     raeburn  2369:             push(@authform_others,$authformloc);
1.187     raeburn  2370:         }
1.449     raeburn  2371:         if ($can_assign{'lti'}) {
                   2372:             push(@authform_others,$authformlti);
                   2373:         }
1.187     raeburn  2374:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   2375:             $show_override_msg = 1;
                   2376:         }
                   2377:     } elsif ($currentauth=~/^internal:/) {
                   2378:         $authformcurrent=$authformint;
                   2379:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2380:             push(@authform_others,$authformkrb);
1.187     raeburn  2381:         }
                   2382:         if ($can_assign{'loc'}) {
1.205     raeburn  2383:             push(@authform_others,$authformloc);
1.187     raeburn  2384:         }
1.449     raeburn  2385:         if ($can_assign{'lti'}) {
                   2386:             push(@authform_others,$authformlti);
                   2387:         }
1.187     raeburn  2388:         if ($can_assign{'int'}) {
                   2389:             $show_override_msg = 1;
                   2390:         }
                   2391:     } elsif ($currentauth=~/^unix:/) {
                   2392:         $authformcurrent=$authformfsys;
                   2393:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2394:             push(@authform_others,$authformkrb);
1.187     raeburn  2395:         }
                   2396:         if ($can_assign{'int'}) {
1.205     raeburn  2397:             push(@authform_others,$authformint);
1.187     raeburn  2398:         }
                   2399:         if ($can_assign{'loc'}) {
1.205     raeburn  2400:             push(@authform_others,$authformloc);
1.187     raeburn  2401:         }
1.449     raeburn  2402:         if ($can_assign{'lti'}) {
                   2403:             push(@authform_others,$authformlti);
                   2404:         }
1.187     raeburn  2405:         if ($can_assign{'fsys'}) {
                   2406:             $show_override_msg = 1;
                   2407:         }
                   2408:     } elsif ($currentauth=~/^localauth:/) {
                   2409:         $authformcurrent=$authformloc;
                   2410:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2411:             push(@authform_others,$authformkrb);
1.187     raeburn  2412:         }
                   2413:         if ($can_assign{'int'}) {
1.205     raeburn  2414:             push(@authform_others,$authformint);
1.187     raeburn  2415:         }
1.449     raeburn  2416:         if ($can_assign{'lti'}) {
                   2417:             push(@authform_others,$authformlti);
                   2418:         }
1.187     raeburn  2419:         if ($can_assign{'loc'}) {
                   2420:             $show_override_msg = 1;
                   2421:         }
1.449     raeburn  2422:     } elsif ($currentauth=~/^lti:/) {
                   2423:         $authformcurrent=$authformlti;
                   2424:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   2425:             push(@authform_others,$authformkrb);
                   2426:         }
                   2427:         if ($can_assign{'int'}) {
                   2428:             push(@authform_others,$authformint);
                   2429:         }
                   2430:         if ($can_assign{'loc'}) {
                   2431:             push(@authform_others,$authformloc);
                   2432:         }
1.187     raeburn  2433:     }
                   2434:     if ($show_override_msg) {
1.205     raeburn  2435:         $authformcurrent = '<table><tr><td colspan="3">'.$authformcurrent.
                   2436:                            '</td></tr>'."\n".
                   2437:                            '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
                   2438:                            '<td><b>'.&mt('Currently in use').'</b></td>'.
                   2439:                            '<td align="right"><span class="LC_cusr_emph">'.
1.187     raeburn  2440:                             &mt('will override current values').
1.205     raeburn  2441:                             '</span></td></tr></table>';
1.187     raeburn  2442:     }
1.205     raeburn  2443:     return ($authformcurrent,$show_override_msg,@authform_others); 
1.187     raeburn  2444: }
                   2445: 
1.188     raeburn  2446: sub personal_data_display {
1.456     raeburn  2447:     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$rolesarray,$now,
                   2448:         $captchaform,$emailusername,$usertype,$usernameset,$condition,$excluded,$showsubmit) = @_;
1.388     bisitz   2449:     my ($output,%userenv,%canmodify,%canmodify_status);
1.219     raeburn  2450:     my @userinfo = ('firstname','middlename','lastname','generation',
                   2451:                     'permanentemail','id');
1.252     raeburn  2452:     my $rowcount = 0;
                   2453:     my $editable = 0;
1.391     raeburn  2454:     my %textboxsize = (
                   2455:                        firstname      => '15',
                   2456:                        middlename     => '15',
                   2457:                        lastname       => '15',
                   2458:                        generation     => '5',
                   2459:                        permanentemail => '25',
                   2460:                        id             => '15',
                   2461:                       );
                   2462: 
                   2463:     my %lt=&Apache::lonlocal::texthash(
                   2464:                 'pd'             => "Personal Data",
                   2465:                 'firstname'      => "First Name",
                   2466:                 'middlename'     => "Middle Name",
                   2467:                 'lastname'       => "Last Name",
                   2468:                 'generation'     => "Generation",
                   2469:                 'permanentemail' => "Permanent e-mail address",
                   2470:                 'id'             => "Student/Employee ID",
                   2471:                 'lg'             => "Login Data",
                   2472:                 'inststatus'     => "Affiliation",
                   2473:                 'email'          => 'E-mail address',
                   2474:                 'valid'          => 'Validation',
1.442     raeburn  2475:                 'username'       => 'Username',
1.391     raeburn  2476:     );
                   2477: 
                   2478:     %canmodify_status =
1.286     raeburn  2479:         &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
                   2480:                                                    ['inststatus'],$rolesarray);
1.253     raeburn  2481:     if (!$newuser) {
1.188     raeburn  2482:         # Get the users information
                   2483:         %userenv = &Apache::lonnet::get('environment',
                   2484:                    ['firstname','middlename','lastname','generation',
1.286     raeburn  2485:                     'permanentemail','id','inststatus'],$ccdomain,$ccuname);
1.219     raeburn  2486:         %canmodify =
                   2487:             &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
1.252     raeburn  2488:                                                        \@userinfo,$rolesarray);
1.257     raeburn  2489:     } elsif ($context eq 'selfcreate') {
1.391     raeburn  2490:         if ($newuser eq 'email') {
1.396     raeburn  2491:             if (ref($emailusername) eq 'HASH') {
                   2492:                 if (ref($emailusername->{$usertype}) eq 'HASH') {
                   2493:                     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
1.442     raeburn  2494:                     @userinfo = ();
1.396     raeburn  2495:                     if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
                   2496:                         foreach my $field (@{$infofields}) { 
                   2497:                             if ($emailusername->{$usertype}->{$field}) {
                   2498:                                 push(@userinfo,$field);
                   2499:                                 $canmodify{$field} = 1;
                   2500:                                 unless ($textboxsize{$field}) {
                   2501:                                     $textboxsize{$field} = 25;
                   2502:                                 }
                   2503:                                 unless ($lt{$field}) {
                   2504:                                     $lt{$field} = $infotitles->{$field};
                   2505:                                 }
                   2506:                                 if ($emailusername->{$usertype}->{$field} eq 'required') {
                   2507:                                     $lt{$field} .= '<b>*</b>';
                   2508:                                 }
1.391     raeburn  2509:                             }
                   2510:                         }
                   2511:                     }
                   2512:                 }
                   2513:             }
                   2514:         } else {
                   2515:             %canmodify = &selfcreate_canmodify($context,$ccdomain,\@userinfo,
                   2516:                                                $inst_results,$rolesarray);
                   2517:         }
1.188     raeburn  2518:     }
1.391     raeburn  2519: 
1.188     raeburn  2520:     my $genhelp=&Apache::loncommon::help_open_topic('Generation');
                   2521:     $output = '<h3>'.$lt{'pd'}.'</h3>'.
                   2522:               &Apache::lonhtmlcommon::start_pick_box();
1.391     raeburn  2523:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
1.443     raeburn  2524:         my $size = 25;
1.442     raeburn  2525:         if ($condition) {
1.443     raeburn  2526:             if ($condition =~ /^\@[^\@]+$/) {
                   2527:                 $size = 10;
1.442     raeburn  2528:             } else {
                   2529:                 undef($condition);
                   2530:             }
1.443     raeburn  2531:         } 
                   2532:         if ($excluded) {
                   2533:             unless ($excluded =~ /^\@[^\@]+$/) {
                   2534:                 undef($condition);
                   2535:             }
1.442     raeburn  2536:         }
1.396     raeburn  2537:         $output .= &Apache::lonhtmlcommon::row_title($lt{'email'}.'<b>*</b>',undef,
1.391     raeburn  2538:                                                      'LC_oddrow_value')."\n".
1.443     raeburn  2539:                    '<input type="text" name="uname" size="'.$size.'" value="" autocomplete="off" />';
                   2540:         if ($condition) {
                   2541:             $output .= $condition;
                   2542:         } elsif ($excluded) {
                   2543:             $output .= '<br /><span style="font-size: smaller">'.&mt('You must use an e-mail address that does not end with [_1]',
                   2544:                                                                      $excluded).'</span>';
                   2545:         }
                   2546:         if ($usernameset eq 'first') {
                   2547:             $output .= '<br /><span style="font-size: smaller">';
                   2548:             if ($condition) {
                   2549:                 $output .= &mt('Your username in LON-CAPA will be the part of your e-mail address before [_1]',
                   2550:                                       $condition);
                   2551:             } else {
                   2552:                 $output .= &mt('Your username in LON-CAPA will be the part of your e-mail address before the @');
                   2553:             }
                   2554:             $output .= '</span>';
                   2555:         }
1.391     raeburn  2556:         $rowcount ++;
                   2557:         $output .= &Apache::lonhtmlcommon::row_closure(1);
1.460     raeburn  2558:         my $upassone = '<input type="password" name="upass'.$now.'" size="20" autocomplete="new-password" />';
                   2559:         my $upasstwo = '<input type="password" name="upasscheck'.$now.'" size="20" autocomplete="new-password" />';
1.396     raeburn  2560:         $output .= &Apache::lonhtmlcommon::row_title(&mt('Password').'<b>*</b>',
1.391     raeburn  2561:                                                     'LC_pick_box_title',
                   2562:                                                     'LC_oddrow_value')."\n".
                   2563:                    $upassone."\n".
                   2564:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
1.396     raeburn  2565:                    &Apache::lonhtmlcommon::row_title(&mt('Confirm password').'<b>*</b>',
1.391     raeburn  2566:                                                      'LC_pick_box_title',
                   2567:                                                      'LC_oddrow_value')."\n".
                   2568:                    $upasstwo.
                   2569:                    &Apache::lonhtmlcommon::row_closure()."\n";
1.443     raeburn  2570:         if ($usernameset eq 'free') {
                   2571:             my $onclick = "toggleUsernameDisp(this,'selfcreateusername');"; 
1.442     raeburn  2572:             $output .= &Apache::lonhtmlcommon::row_title($lt{'username'},undef,'LC_oddrow_value')."\n".
1.455     raeburn  2573:                        '<span class="LC_nobreak">'.&mt('Use e-mail address: ').
                   2574:                        '<label><input type="radio" name="emailused" value="1" checked="checked" onclick="'.$onclick.'" />'.
                   2575:                        &mt('Yes').'</label>'.('&nbsp;'x2).
                   2576:                        '<label><input type="radio" name="emailused" value="0" onclick="'.$onclick.'" />'.
                   2577:                        &mt('No').'</label></span>'."\n".
1.442     raeburn  2578:                        '<div id="selfcreateusername" style="display: none; font-size: smaller">'.
                   2579:                        '<br /><span class="LC_nobreak">'.&mt('Preferred username').
                   2580:                        '&nbsp;<input type="text" name="username" value="" size="20" autocomplete="off"/>'.
                   2581:                        '</span></div>'."\n".&Apache::lonhtmlcommon::row_closure(1);
                   2582:             $rowcount ++;
                   2583:         }
1.391     raeburn  2584:     }
1.188     raeburn  2585:     foreach my $item (@userinfo) {
                   2586:         my $rowtitle = $lt{$item};
1.252     raeburn  2587:         my $hiderow = 0;
1.188     raeburn  2588:         if ($item eq 'generation') {
                   2589:             $rowtitle = $genhelp.$rowtitle;
                   2590:         }
1.252     raeburn  2591:         my $row = &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
1.188     raeburn  2592:         if ($newuser) {
1.210     raeburn  2593:             if (ref($inst_results) eq 'HASH') {
                   2594:                 if ($inst_results->{$item} ne '') {
1.252     raeburn  2595:                     $row .= '<input type="hidden" name="c'.$item.'" value="'.$inst_results->{$item}.'" />'.$inst_results->{$item};
1.210     raeburn  2596:                 } else {
1.252     raeburn  2597:                     if ($context eq 'selfcreate') {
1.391     raeburn  2598:                         if ($canmodify{$item}) {
1.394     raeburn  2599:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.252     raeburn  2600:                             $editable ++;
                   2601:                         } else {
                   2602:                             $hiderow = 1;
                   2603:                         }
1.253     raeburn  2604:                     } else {
                   2605:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2606:                     }
1.210     raeburn  2607:                 }
1.188     raeburn  2608:             } else {
1.252     raeburn  2609:                 if ($context eq 'selfcreate') {
1.401     raeburn  2610:                     if ($canmodify{$item}) {
                   2611:                         if ($newuser eq 'email') {
                   2612:                             $row .= '<input type="text" name="'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.287     raeburn  2613:                         } else {
1.401     raeburn  2614:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.287     raeburn  2615:                         }
1.401     raeburn  2616:                         $editable ++;
                   2617:                     } else {
                   2618:                         $hiderow = 1;
1.252     raeburn  2619:                     }
1.253     raeburn  2620:                 } else {
                   2621:                     $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2622:                 }
1.188     raeburn  2623:             }
                   2624:         } else {
1.219     raeburn  2625:             if ($canmodify{$item}) {
1.252     raeburn  2626:                 $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
1.393     raeburn  2627:                 if (($item eq 'id') && (!$newuser)) {
                   2628:                     $row .= '<br />'.&Apache::lonuserutils::forceid_change($context);
                   2629:                 }
1.188     raeburn  2630:             } else {
1.252     raeburn  2631:                 $row .= $userenv{$item};
1.188     raeburn  2632:             }
                   2633:         }
1.252     raeburn  2634:         $row .= &Apache::lonhtmlcommon::row_closure(1);
                   2635:         if (!$hiderow) {
                   2636:             $output .= $row;
                   2637:             $rowcount ++;
                   2638:         }
1.188     raeburn  2639:     }
1.286     raeburn  2640:     if (($canmodify_status{'inststatus'}) || ($context ne 'selfcreate')) {
                   2641:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($ccdomain);
                   2642:         if (ref($types) eq 'ARRAY') {
                   2643:             if (@{$types} > 0) {
                   2644:                 my ($hiderow,$shown);
                   2645:                 if ($canmodify_status{'inststatus'}) {
                   2646:                     $shown = &pick_inst_statuses($userenv{'inststatus'},$usertypes,$types);
                   2647:                 } else {
                   2648:                     if ($userenv{'inststatus'} eq '') {
                   2649:                         $hiderow = 1;
1.334     raeburn  2650:                     } else {
                   2651:                         my @showitems;
                   2652:                         foreach my $item ( map { &unescape($_); } split(':',$userenv{'inststatus'})) {
                   2653:                             if (exists($usertypes->{$item})) {
                   2654:                                 push(@showitems,$usertypes->{$item});
                   2655:                             } else {
                   2656:                                 push(@showitems,$item);
                   2657:                             }
                   2658:                         }
                   2659:                         if (@showitems) {
                   2660:                             $shown = join(', ',@showitems);
                   2661:                         } else {
                   2662:                             $hiderow = 1;
                   2663:                         }
1.286     raeburn  2664:                     }
                   2665:                 }
                   2666:                 if (!$hiderow) {
1.389     bisitz   2667:                     my $row = &Apache::lonhtmlcommon::row_title(&mt('Affiliations'),undef,'LC_oddrow_value')."\n".
1.286     raeburn  2668:                               $shown.&Apache::lonhtmlcommon::row_closure(1); 
                   2669:                     if ($context eq 'selfcreate') {
                   2670:                         $rowcount ++;
                   2671:                     }
                   2672:                     $output .= $row;
                   2673:                 }
                   2674:             }
                   2675:         }
                   2676:     }
1.391     raeburn  2677:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
                   2678:         if ($captchaform) {
1.410     raeburn  2679:             $output .= &Apache::lonhtmlcommon::row_title($lt{'valid'}.'*',
                   2680:                                                          'LC_pick_box_title')."\n".
1.456     raeburn  2681:                        $captchaform."\n".'<br /><br />'.
1.391     raeburn  2682:                        &Apache::lonhtmlcommon::row_closure(1); 
                   2683:             $rowcount ++;
                   2684:         }
1.456     raeburn  2685:         if ($showsubmit) {
                   2686:             my $submit_text = &mt('Create account');
                   2687:             $output .= &Apache::lonhtmlcommon::row_title()."\n".
                   2688:                        '<br /><input type="submit" name="createaccount" value="'.
                   2689:                        $submit_text.'" />';
                   2690:             if ($usertype ne '') {
                   2691:                 $output .= '<input type="hidden" name="type" value="'.
                   2692:                            &HTML::Entities::encode($usertype,'\'<>"&').'" />';
                   2693:             }
                   2694:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   2695:         }
1.391     raeburn  2696:     }
1.188     raeburn  2697:     $output .= &Apache::lonhtmlcommon::end_pick_box();
1.206     raeburn  2698:     if (wantarray) {
1.252     raeburn  2699:         if ($context eq 'selfcreate') {
                   2700:             return($output,$rowcount,$editable);
                   2701:         } else {
1.388     bisitz   2702:             return $output;
1.252     raeburn  2703:         }
1.206     raeburn  2704:     } else {
                   2705:         return $output;
                   2706:     }
1.188     raeburn  2707: }
                   2708: 
1.286     raeburn  2709: sub pick_inst_statuses {
                   2710:     my ($curr,$usertypes,$types) = @_;
                   2711:     my ($output,$rem,@currtypes);
                   2712:     if ($curr ne '') {
                   2713:         @currtypes = map { &unescape($_); } split(/:/,$curr);
                   2714:     }
                   2715:     my $numinrow = 2;
                   2716:     if (ref($types) eq 'ARRAY') {
                   2717:         $output = '<table>';
                   2718:         my $lastcolspan; 
                   2719:         for (my $i=0; $i<@{$types}; $i++) {
                   2720:             if (defined($usertypes->{$types->[$i]})) {
                   2721:                 my $rem = $i%($numinrow);
                   2722:                 if ($rem == 0) {
                   2723:                     if ($i<@{$types}-1) {
                   2724:                         if ($i > 0) { 
                   2725:                             $output .= '</tr>';
                   2726:                         }
                   2727:                         $output .= '<tr>';
                   2728:                     }
                   2729:                 } elsif ($i==@{$types}-1) {
                   2730:                     my $colsleft = $numinrow - $rem;
                   2731:                     if ($colsleft > 1) {
                   2732:                         $lastcolspan = ' colspan="'.$colsleft.'"';
                   2733:                     }
                   2734:                 }
                   2735:                 my $check = ' ';
                   2736:                 if (grep(/^\Q$types->[$i]\E$/,@currtypes)) {
                   2737:                     $check = ' checked="checked" ';
                   2738:                 }
                   2739:                 $output .= '<td class="LC_left_item"'.$lastcolspan.'>'.
                   2740:                            '<span class="LC_nobreak"><label>'.
                   2741:                            '<input type="checkbox" name="inststatus" '.
                   2742:                            'value="'.$types->[$i].'"'.$check.'/>'.
                   2743:                            $usertypes->{$types->[$i]}.'</label></span></td>';
                   2744:             }
                   2745:         }
                   2746:         $output .= '</tr></table>';
                   2747:     }
                   2748:     return $output;
                   2749: }
                   2750: 
1.257     raeburn  2751: sub selfcreate_canmodify {
                   2752:     my ($context,$dom,$userinfo,$inst_results,$rolesarray) = @_;
                   2753:     if (ref($inst_results) eq 'HASH') {
                   2754:         my @inststatuses = &get_inststatuses($inst_results);
                   2755:         if (@inststatuses == 0) {
                   2756:             @inststatuses = ('default');
                   2757:         }
                   2758:         $rolesarray = \@inststatuses;
                   2759:     }
                   2760:     my %canmodify =
                   2761:         &Apache::lonuserutils::can_modify_userinfo($context,$dom,$userinfo,
                   2762:                                                    $rolesarray);
                   2763:     return %canmodify;
                   2764: }
                   2765: 
1.252     raeburn  2766: sub get_inststatuses {
                   2767:     my ($insthashref) = @_;
                   2768:     my @inststatuses = ();
                   2769:     if (ref($insthashref) eq 'HASH') {
                   2770:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
                   2771:             @inststatuses = @{$insthashref->{'inststatus'}};
                   2772:         }
                   2773:     }
                   2774:     return @inststatuses;
                   2775: }
                   2776: 
1.4       www      2777: # ================================================================= Phase Three
1.42      matthew  2778: sub update_user_data {
1.451     raeburn  2779:     my ($r,$context,$crstype,$brcrum,$showcredits,$permission) = @_; 
1.101     albertel 2780:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                   2781:                                           $env{'form.ccdomain'});
1.27      matthew  2782:     # Error messages
1.188     raeburn  2783:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
1.193     raeburn  2784:     my $end       = '</span><br /><br />';
                   2785:     my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
1.188     raeburn  2786:                     "'$env{'form.prevphase'}','modify')".'" />'.
1.219     raeburn  2787:                     &mt('Return to previous page').'</a>'.
                   2788:                     &Apache::loncommon::end_page();
                   2789:     my $now = time;
1.40      www      2790:     my $title;
1.101     albertel 2791:     if (exists($env{'form.makeuser'})) {
1.40      www      2792: 	$title='Set Privileges for New User';
                   2793:     } else {
                   2794:         $title='Modify User Privileges';
                   2795:     }
1.213     raeburn  2796:     my $newuser = 0;
1.160     raeburn  2797:     my ($jsback,$elements) = &crumb_utilities();
                   2798:     my $jscript = '<script type="text/javascript">'."\n".
1.301     bisitz   2799:                   '// <![CDATA['."\n".
                   2800:                   $jsback."\n".
                   2801:                   '// ]]>'."\n".
                   2802:                   '</script>'."\n";
1.422     raeburn  2803:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$env{'form.ccdomain'});
1.351     raeburn  2804:     push (@{$brcrum},
                   2805:              {href => "javascript:backPage(document.userupdate)",
                   2806:               text => $breadcrumb_text{'search'},
                   2807:               faq  => 282,
                   2808:               bug  => 'Instructor Interface',}
                   2809:              );
                   2810:     if ($env{'form.prevphase'} eq 'userpicked') {
                   2811:         push(@{$brcrum},
                   2812:                {href => "javascript:backPage(document.userupdate,'get_user_info','select')",
                   2813:                 text => $breadcrumb_text{'userpicked'},
                   2814:                 faq  => 282,
                   2815:                 bug  => 'Instructor Interface',});
1.233     raeburn  2816:     }
1.224     raeburn  2817:     my $helpitem = 'Course_Change_Privileges';
                   2818:     if ($env{'form.action'} eq 'singlestudent') {
                   2819:         $helpitem = 'Course_Add_Student';
1.439     raeburn  2820:     } elsif ($context eq 'author') {
                   2821:         $helpitem = 'Author_Change_Privileges';
                   2822:     } elsif ($context eq 'domain') {
                   2823:         $helpitem = 'Domain_Change_Privileges';
1.224     raeburn  2824:     }
1.351     raeburn  2825:     push(@{$brcrum}, 
                   2826:             {href => "javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
                   2827:              text => $breadcrumb_text{'modify'},
                   2828:              faq  => 282,
                   2829:              bug  => 'Instructor Interface',},
                   2830:             {href => "/adm/createuser",
                   2831:              text => "Result",
                   2832:              faq  => 282,
                   2833:              bug  => 'Instructor Interface',
                   2834:              help => $helpitem});
                   2835:     my $args = {bread_crumbs          => $brcrum,
                   2836:                 bread_crumbs_component => 'User Management'};
                   2837:     if ($env{'form.popup'}) {
                   2838:         $args->{'no_nav_bar'} = 1;
                   2839:     }
                   2840:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
1.188     raeburn  2841:     $r->print(&update_result_form($uhome));
1.27      matthew  2842:     # Check Inputs
1.101     albertel 2843:     if (! $env{'form.ccuname'} ) {
1.193     raeburn  2844: 	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
1.27      matthew  2845: 	return;
                   2846:     }
1.138     albertel 2847:     if (  $env{'form.ccuname'} ne 
                   2848: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
1.281     bisitz   2849: 	$r->print($error.&mt('Invalid login name.').'  '.
                   2850: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid.').
1.193     raeburn  2851: 		  $end.$rtnlink);
1.27      matthew  2852: 	return;
                   2853:     }
1.101     albertel 2854:     if (! $env{'form.ccdomain'}       ) {
1.193     raeburn  2855: 	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
1.27      matthew  2856: 	return;
                   2857:     }
1.138     albertel 2858:     if (  $env{'form.ccdomain'} ne
                   2859: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
1.281     bisitz   2860: 	$r->print($error.&mt('Invalid domain name.').'  '.
                   2861: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid.').
1.193     raeburn  2862: 		  $end.$rtnlink);
1.27      matthew  2863: 	return;
                   2864:     }
1.219     raeburn  2865:     if ($uhome eq 'no_host') {
                   2866:         $newuser = 1;
                   2867:     }
1.101     albertel 2868:     if (! exists($env{'form.makeuser'})) {
1.29      matthew  2869:         # Modifying an existing user, so check the validity of the name
                   2870:         if ($uhome eq 'no_host') {
1.389     bisitz   2871:             $r->print(
                   2872:                 $error
                   2873:                .'<p class="LC_error">'
                   2874:                .&mt('Unable to determine home server for [_1] in domain [_2].',
                   2875:                         '"'.$env{'form.ccuname'}.'"','"'.$env{'form.ccdomain'}.'"')
                   2876:                .'</p>');
1.29      matthew  2877:             return;
                   2878:         }
                   2879:     }
1.27      matthew  2880:     # Determine authentication method and password for the user being modified
                   2881:     my $amode='';
                   2882:     my $genpwd='';
1.101     albertel 2883:     if ($env{'form.login'} eq 'krb') {
1.41      albertel 2884: 	$amode='krb';
1.101     albertel 2885: 	$amode.=$env{'form.krbver'};
                   2886: 	$genpwd=$env{'form.krbarg'};
                   2887:     } elsif ($env{'form.login'} eq 'int') {
1.27      matthew  2888: 	$amode='internal';
1.101     albertel 2889: 	$genpwd=$env{'form.intarg'};
                   2890:     } elsif ($env{'form.login'} eq 'fsys') {
1.27      matthew  2891: 	$amode='unix';
1.101     albertel 2892: 	$genpwd=$env{'form.fsysarg'};
                   2893:     } elsif ($env{'form.login'} eq 'loc') {
1.27      matthew  2894: 	$amode='localauth';
1.101     albertel 2895: 	$genpwd=$env{'form.locarg'};
1.27      matthew  2896: 	$genpwd=" " if (!$genpwd);
1.449     raeburn  2897:     } elsif ($env{'form.login'} eq 'lti') {
                   2898:         $amode='lti';
                   2899:         $genpwd=" ";
1.101     albertel 2900:     } elsif (($env{'form.login'} eq 'nochange') ||
                   2901:              ($env{'form.login'} eq ''        )) { 
1.34      matthew  2902:         # There is no need to tell the user we did not change what they
                   2903:         # did not ask us to change.
1.35      matthew  2904:         # If they are creating a new user but have not specified login
                   2905:         # information this will be caught below.
1.30      matthew  2906:     } else {
1.367     golterma 2907:             $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);
                   2908:             return;
1.27      matthew  2909:     }
1.164     albertel 2910: 
1.188     raeburn  2911:     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
1.367     golterma 2912:                         $env{'form.ccuname'}.' ('.&Apache::loncommon::plainname($env{'form.ccuname'},
                   2913:                         $env{'form.ccdomain'}).')', $env{'form.ccdomain'}).'</h3>');
                   2914:     my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,2);
1.344     bisitz   2915: 
1.193     raeburn  2916:     my (%alerts,%rulematch,%inst_results,%curr_rules);
1.334     raeburn  2917:     my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
1.459     raeburn  2918:     my @usertools = ('aboutme','blog','webdav','portfolio','timezone');
1.449     raeburn  2919:     my @requestcourses = ('official','unofficial','community','textbook','placement','lti');
1.362     raeburn  2920:     my @requestauthor = ('requestauthor');
1.286     raeburn  2921:     my ($othertitle,$usertypes,$types) = 
                   2922:         &Apache::loncommon::sorted_inst_types($env{'form.ccdomain'});
1.334     raeburn  2923:     my %canmodify_status =
                   2924:         &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},
                   2925:                                                    ['inststatus']);
1.101     albertel 2926:     if ($env{'form.makeuser'}) {
1.164     albertel 2927: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
1.27      matthew  2928:         # Check for the authentication mode and password
                   2929:         if (! $amode || ! $genpwd) {
1.193     raeburn  2930: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.27      matthew  2931: 	    return;
1.18      albertel 2932: 	}
1.29      matthew  2933:         # Determine desired host
1.101     albertel 2934:         my $desiredhost = $env{'form.hserver'};
1.29      matthew  2935:         if (lc($desiredhost) eq 'default') {
                   2936:             $desiredhost = undef;
                   2937:         } else {
1.147     albertel 2938:             my %home_servers = 
                   2939: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
1.29      matthew  2940:             if (! exists($home_servers{$desiredhost})) {
1.193     raeburn  2941:                 $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
                   2942:                 return;
                   2943:             }
                   2944:         }
                   2945:         # Check ID format
                   2946:         my %checkhash;
                   2947:         my %checks = ('id' => 1);
                   2948:         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
1.219     raeburn  2949:             'newuser' => $newuser, 
1.196     raeburn  2950:             'id' => $env{'form.cid'},
1.193     raeburn  2951:         );
1.196     raeburn  2952:         if ($env{'form.cid'} ne '') {
                   2953:             &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   2954:                                           \%rulematch,\%inst_results,\%curr_rules);
                   2955:             if (ref($alerts{'id'}) eq 'HASH') {
                   2956:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
                   2957:                     my $domdesc =
                   2958:                         &Apache::lonnet::domain($env{'form.ccdomain'},'description');
                   2959:                     if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
                   2960:                         my $userchkmsg;
                   2961:                         if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
                   2962:                             $userchkmsg  = 
                   2963:                                 &Apache::loncommon::instrule_disallow_msg('id',
                   2964:                                                                     $domdesc,1).
                   2965:                                 &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
                   2966:                                     $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
                   2967:                         }
                   2968:                         $r->print($error.&mt('Invalid ID format').$end.
                   2969:                                   $userchkmsg.$rtnlink);
                   2970:                         return;
                   2971:                     }
                   2972:                 }
1.29      matthew  2973:             }
                   2974:         }
1.367     golterma 2975:         &Apache::lonhtmlcommon::Increment_PrgWin($r, \%prog_state);
1.27      matthew  2976: 	# Call modifyuser
                   2977: 	my $result = &Apache::lonnet::modifyuser
1.193     raeburn  2978: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
1.188     raeburn  2979:              $amode,$genpwd,$env{'form.cfirstname'},
                   2980:              $env{'form.cmiddlename'},$env{'form.clastname'},
                   2981:              $env{'form.cgeneration'},undef,$desiredhost,
                   2982:              $env{'form.cpermanentemail'});
1.77      www      2983: 	$r->print(&mt('Generating user').': '.$result);
1.219     raeburn  2984:         $uhome = &Apache::lonnet::homeserver($env{'form.ccuname'},
1.101     albertel 2985:                                                $env{'form.ccdomain'});
1.334     raeburn  2986:         my (%changeHash,%newcustom,%changed,%changedinfo);
1.267     raeburn  2987:         if ($uhome ne 'no_host') {
1.334     raeburn  2988:             if ($context eq 'domain') {
1.378     raeburn  2989:                 foreach my $name ('portfolio','author') {
                   2990:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   2991:                         if ($env{'form.'.$name.'quota'} eq '') {
                   2992:                             $newcustom{$name.'quota'} = 0;
                   2993:                         } else {
                   2994:                             $newcustom{$name.'quota'} = $env{'form.'.$name.'quota'};
                   2995:                             $newcustom{$name.'quota'} =~ s/[^\d\.]//g;
                   2996:                         }
                   2997:                         if (&quota_admin($newcustom{$name.'quota'},\%changeHash,$name)) {
                   2998:                             $changed{$name.'quota'} = 1;
                   2999:                         }
1.334     raeburn  3000:                     }
                   3001:                 }
                   3002:                 foreach my $item (@usertools) {
                   3003:                     if ($env{'form.custom'.$item} == 1) {
                   3004:                         $newcustom{$item} = $env{'form.tools_'.$item};
                   3005:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   3006:                                                      \%changeHash,'tools');
                   3007:                     }
1.267     raeburn  3008:                 }
1.334     raeburn  3009:                 foreach my $item (@requestcourses) {
1.341     raeburn  3010:                     if ($env{'form.custom'.$item} == 1) {
                   3011:                         $newcustom{$item} = $env{'form.crsreq_'.$item};
                   3012:                         if ($env{'form.crsreq_'.$item} eq 'autolimit') {
                   3013:                             $newcustom{$item} .= '=';
1.383     raeburn  3014:                             $env{'form.crsreq_'.$item.'_limit'} =~ s/\D+//g;
                   3015:                             if ($env{'form.crsreq_'.$item.'_limit'}) {
1.341     raeburn  3016:                                 $newcustom{$item} .= $env{'form.crsreq_'.$item.'_limit'};
                   3017:                             }
1.334     raeburn  3018:                         }
1.341     raeburn  3019:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   3020:                                                       \%changeHash,'requestcourses');
1.334     raeburn  3021:                     }
1.275     raeburn  3022:                 }
1.362     raeburn  3023:                 if ($env{'form.customrequestauthor'} == 1) {
                   3024:                     $newcustom{'requestauthor'} = $env{'form.requestauthor'};
                   3025:                     $changed{'requestauthor'} = &tool_admin('requestauthor',
                   3026:                                                     $newcustom{'requestauthor'},
                   3027:                                                     \%changeHash,'requestauthor');
                   3028:                 }
1.275     raeburn  3029:             }
1.334     raeburn  3030:             if ($canmodify_status{'inststatus'}) {
                   3031:                 if (exists($env{'form.inststatus'})) {
                   3032:                     my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   3033:                     if (@inststatuses > 0) {
                   3034:                         $changeHash{'inststatus'} = join(',',@inststatuses);
                   3035:                         $changed{'inststatus'} = $changeHash{'inststatus'};
1.306     raeburn  3036:                     }
                   3037:                 }
1.232     raeburn  3038:             }
1.334     raeburn  3039:             if (keys(%changed)) {
                   3040:                 foreach my $item (@userinfo) {
                   3041:                     $changeHash{$item}  = $env{'form.c'.$item};
1.286     raeburn  3042:                 }
1.267     raeburn  3043:                 my $chgresult =
                   3044:                      &Apache::lonnet::put('environment',\%changeHash,
                   3045:                                           $env{'form.ccdomain'},$env{'form.ccuname'});
                   3046:             } 
1.232     raeburn  3047:         }
1.454     raeburn  3048:         $r->print('<br />'.&mt('Home Server').': '.$uhome.' '.
1.219     raeburn  3049:                   &Apache::lonnet::hostname($uhome));
1.101     albertel 3050:     } elsif (($env{'form.login'} ne 'nochange') &&
                   3051:              ($env{'form.login'} ne ''        )) {
1.27      matthew  3052: 	# Modify user privileges
                   3053:         if (! $amode || ! $genpwd) {
1.193     raeburn  3054: 	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
1.27      matthew  3055: 	    return;
1.20      harris41 3056: 	}
1.395     bisitz   3057: 	# Only allow authentication modification if the person has authority
1.101     albertel 3058: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.20      harris41 3059: 	    $r->print('Modifying authentication: '.
1.31      matthew  3060:                       &Apache::lonnet::modifyuserauth(
1.101     albertel 3061: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
1.21      harris41 3062:                        $amode,$genpwd));
1.454     raeburn  3063:             $r->print('<br />'.&mt('Home Server').': '.&Apache::lonnet::homeserver
1.101     albertel 3064: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
1.4       www      3065: 	} else {
1.27      matthew  3066: 	    # Okay, this is a non-fatal error.
1.452     raeburn  3067: 	    $r->print($error.&mt('You do not have privileges to modify the authentication configuration for this user.').$end);
1.27      matthew  3068: 	}
1.451     raeburn  3069:     } elsif (($env{'form.intarg'} ne '') &&
                   3070:              (&Apache::lonnet::queryauthenticate($env{'form.ccuname'},$env{'form.ccdomain'}) =~ /^internal:/) &&
                   3071:              (&Apache::lonuserutils::can_change_internalpass($env{'form.ccuname'},$env{'form.ccdomain'},$crstype,$permission))) {
                   3072:         $r->print('Modifying authentication: '.
                   3073:                   &Apache::lonnet::modifyuserauth(
                   3074:                   $env{'form.ccdomain'},$env{'form.ccuname'},
                   3075:                   'internal',$env{'form.intarg'}));
1.28      matthew  3076:     }
1.344     bisitz   3077:     $r->rflush(); # Finish display of header before time consuming actions start
1.367     golterma 3078:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state);
1.28      matthew  3079:     ##
1.375     raeburn  3080:     my (@userroles,%userupdate,$cnum,$cdom,$defaultcredits,%namechanged);
1.213     raeburn  3081:     if ($context eq 'course') {
1.375     raeburn  3082:         ($cnum,$cdom) =
                   3083:             &Apache::lonuserutils::get_course_identity();
1.318     raeburn  3084:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
1.375     raeburn  3085:         if ($showcredits) {
                   3086:            $defaultcredits = &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   3087:         }
1.213     raeburn  3088:     }
1.101     albertel 3089:     if (! $env{'form.makeuser'} ) {
1.28      matthew  3090:         # Check for need to change
                   3091:         my %userenv = &Apache::lonnet::get
1.134     raeburn  3092:             ('environment',['firstname','middlename','lastname','generation',
1.378     raeburn  3093:              'id','permanentemail','portfolioquota','authorquota','inststatus',
1.459     raeburn  3094:              'tools.aboutme','tools.blog','tools.webdav',
                   3095:              'tools.portfolio','tools.timezone',
1.361     raeburn  3096:              'requestcourses.official','requestcourses.unofficial',
1.384     raeburn  3097:              'requestcourses.community','requestcourses.textbook',
1.458     raeburn  3098:              'requestcourses.lti','requestauthor',
1.384     raeburn  3099:              'reqcrsotherdom.official','reqcrsotherdom.unofficial',
                   3100:              'reqcrsotherdom.community','reqcrsotherdom.textbook',
1.458     raeburn  3101:              'reqcrsotherdom.placement'],
1.160     raeburn  3102:               $env{'form.ccdomain'},$env{'form.ccuname'});
1.28      matthew  3103:         my ($tmp) = keys(%userenv);
                   3104:         if ($tmp =~ /^(con_lost|error)/i) { 
                   3105:             %userenv = ();
                   3106:         }
1.206     raeburn  3107:         my $no_forceid_alert;
                   3108:         # Check to see if user information can be changed
                   3109:         my %domconfig =
                   3110:             &Apache::lonnet::get_dom('configuration',['usermodification'],
                   3111:                                      $env{'form.ccdomain'});
1.213     raeburn  3112:         my @statuses = ('active','future');
                   3113:         my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@statuses,undef,$env{'request.role.domain'});
                   3114:         my ($auname,$audom);
1.220     raeburn  3115:         if ($context eq 'author') {
1.206     raeburn  3116:             $auname = $env{'user.name'};
                   3117:             $audom = $env{'user.domain'};     
                   3118:         }
                   3119:         foreach my $item (keys(%roles)) {
1.220     raeburn  3120:             my ($rolenum,$roledom,$role) = split(/:/,$item,-1);
1.206     raeburn  3121:             if ($context eq 'course') {
                   3122:                 if ($cnum ne '' && $cdom ne '') {
                   3123:                     if ($rolenum eq $cnum && $roledom eq $cdom) {
                   3124:                         if (!grep(/^\Q$role\E$/,@userroles)) {
                   3125:                             push(@userroles,$role);
                   3126:                         }
                   3127:                     }
                   3128:                 }
                   3129:             } elsif ($context eq 'author') {
                   3130:                 if ($rolenum eq $auname && $roledom eq $audom) {
1.461     raeburn  3131:                     if (!grep(/^\Q$role\E$/,@userroles)) {
1.206     raeburn  3132:                         push(@userroles,$role);
                   3133:                     }
                   3134:                 }
                   3135:             }
                   3136:         }
1.220     raeburn  3137:         if ($env{'form.action'} eq 'singlestudent') {
                   3138:             if (!grep(/^st$/,@userroles)) {
                   3139:                 push(@userroles,'st');
                   3140:             }
                   3141:         } else {
                   3142:             # Check for course or co-author roles being activated or re-enabled
                   3143:             if ($context eq 'author' || $context eq 'course') {
                   3144:                 foreach my $key (keys(%env)) {
                   3145:                     if ($context eq 'author') {
                   3146:                         if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
                   3147:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3148:                                 push(@userroles,$1);
                   3149:                             }
                   3150:                         } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
                   3151:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3152:                                 push(@userroles,$1);
                   3153:                             }
1.206     raeburn  3154:                         }
1.220     raeburn  3155:                     } elsif ($context eq 'course') {
                   3156:                         if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
                   3157:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3158:                                 push(@userroles,$1);
                   3159:                             }
                   3160:                         } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
                   3161:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3162:                                 push(@userroles,$1);
                   3163:                             }
1.206     raeburn  3164:                         }
                   3165:                     }
                   3166:                 }
                   3167:             }
                   3168:         }
                   3169:         #Check to see if we can change personal data for the user 
                   3170:         my (@mod_disallowed,@longroles);
                   3171:         foreach my $role (@userroles) {
                   3172:             if ($role eq 'cr') {
                   3173:                 push(@longroles,'Custom');
                   3174:             } else {
1.318     raeburn  3175:                 push(@longroles,&Apache::lonnet::plaintext($role,$crstype)); 
1.206     raeburn  3176:             }
                   3177:         }
1.219     raeburn  3178:         my %canmodify = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},\@userinfo,\@userroles);
                   3179:         foreach my $item (@userinfo) {
1.28      matthew  3180:             # Strip leading and trailing whitespace
1.203     raeburn  3181:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
1.219     raeburn  3182:             if (!$canmodify{$item}) {
1.207     raeburn  3183:                 if (defined($env{'form.c'.$item})) {
                   3184:                     if ($env{'form.c'.$item} ne $userenv{$item}) {
                   3185:                         push(@mod_disallowed,$item);
                   3186:                     }
1.206     raeburn  3187:                 }
                   3188:                 $env{'form.c'.$item} = $userenv{$item};
                   3189:             }
1.28      matthew  3190:         }
1.259     bisitz   3191:         # Check to see if we can change the Student/Employee ID
1.196     raeburn  3192:         my $forceid = $env{'form.forceid'};
                   3193:         my $recurseid = $env{'form.recurseid'};
                   3194:         my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
1.203     raeburn  3195:         my %uidhash = &Apache::lonnet::idrget($env{'form.ccdomain'},
                   3196:                                             $env{'form.ccuname'});
                   3197:         if (($uidhash{$env{'form.ccuname'}}) && 
                   3198:             ($uidhash{$env{'form.ccuname'}}!~/error\:/) && 
                   3199:             (!$forceid)) {
                   3200:             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
                   3201:                 $env{'form.cid'} = $userenv{'id'};
1.293     bisitz   3202:                 $no_forceid_alert = &mt('New student/employee ID does not match existing ID for this user.')
1.259     bisitz   3203:                                    .'<br />'
                   3204:                                    .&mt("Change is not permitted without checking the 'Force ID change' checkbox on the previous page.")
                   3205:                                    .'<br />'."\n";
1.203     raeburn  3206:             }
                   3207:         }
                   3208:         if ($env{'form.cid'} ne $userenv{'id'}) {
1.196     raeburn  3209:             my $checkhash;
                   3210:             my $checks = { 'id' => 1 };
                   3211:             $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
                   3212:                    { 'newuser' => $newuser,
                   3213:                      'id'  => $env{'form.cid'}, 
                   3214:                    };
                   3215:             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   3216:                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
                   3217:             if (ref($alerts{'id'}) eq 'HASH') {
                   3218:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
1.203     raeburn  3219:                    $env{'form.cid'} = $userenv{'id'};
1.196     raeburn  3220:                 }
                   3221:             }
                   3222:         }
1.378     raeburn  3223:         my (%quotachanged,%oldquota,%newquota,%olddefquota,%newdefquota, 
                   3224:             $oldinststatus,$newinststatus,%oldisdefault,%newisdefault,%oldsettings,
1.339     raeburn  3225:             %oldsettingstext,%newsettings,%newsettingstext,@disporder,
1.378     raeburn  3226:             %oldsettingstatus,%newsettingstatus);
1.334     raeburn  3227:         @disporder = ('inststatus');
                   3228:         if ($env{'request.role.domain'} eq $env{'form.ccdomain'}) {
1.362     raeburn  3229:             push(@disporder,'requestcourses','requestauthor');
1.334     raeburn  3230:         } else {
                   3231:             push(@disporder,'reqcrsotherdom');
                   3232:         }
                   3233:         push(@disporder,('quota','tools'));
1.338     raeburn  3234:         $oldinststatus = $userenv{'inststatus'};
1.378     raeburn  3235:         foreach my $name ('portfolio','author') {
                   3236:             ($olddefquota{$name},$oldsettingstatus{$name}) = 
                   3237:                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$oldinststatus,$name);
                   3238:             ($newdefquota{$name},$newsettingstatus{$name}) = ($olddefquota{$name},$oldsettingstatus{$name});
                   3239:         }
1.334     raeburn  3240:         my %canshow;
1.220     raeburn  3241:         if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
1.334     raeburn  3242:             $canshow{'quota'} = 1;
1.220     raeburn  3243:         }
1.267     raeburn  3244:         if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
1.334     raeburn  3245:             $canshow{'tools'} = 1;
1.267     raeburn  3246:         }
1.275     raeburn  3247:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
1.334     raeburn  3248:             $canshow{'requestcourses'} = 1;
1.300     raeburn  3249:         } elsif (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.334     raeburn  3250:             $canshow{'reqcrsotherdom'} = 1;
1.275     raeburn  3251:         }
1.286     raeburn  3252:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.334     raeburn  3253:             $canshow{'inststatus'} = 1;
1.286     raeburn  3254:         }
1.362     raeburn  3255:         if (&Apache::lonnet::allowed('cau',$env{'form.ccdomain'})) {
                   3256:             $canshow{'requestauthor'} = 1;
                   3257:         }
1.267     raeburn  3258:         my (%changeHash,%changed);
1.286     raeburn  3259:         if ($oldinststatus eq '') {
1.334     raeburn  3260:             $oldsettings{'inststatus'} = $othertitle; 
1.286     raeburn  3261:         } else {
                   3262:             if (ref($usertypes) eq 'HASH') {
1.334     raeburn  3263:                 $oldsettings{'inststatus'} = join(', ',map{ $usertypes->{ &unescape($_) }; } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  3264:             } else {
1.334     raeburn  3265:                 $oldsettings{'inststatus'} = join(', ',map{ &unescape($_); } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  3266:             }
                   3267:         }
                   3268:         $changeHash{'inststatus'} = $userenv{'inststatus'};
1.334     raeburn  3269:         if ($canmodify_status{'inststatus'}) {
                   3270:             $canshow{'inststatus'} = 1;
1.286     raeburn  3271:             if (exists($env{'form.inststatus'})) {
                   3272:                 my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   3273:                 if (@inststatuses > 0) {
                   3274:                     $newinststatus = join(':',map { &escape($_); } @inststatuses);
                   3275:                     $changeHash{'inststatus'} = $newinststatus;
                   3276:                     if ($newinststatus ne $oldinststatus) {
                   3277:                         $changed{'inststatus'} = $newinststatus;
1.378     raeburn  3278:                         foreach my $name ('portfolio','author') {
                   3279:                             ($newdefquota{$name},$newsettingstatus{$name}) =
                   3280:                                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   3281:                         }
1.286     raeburn  3282:                     }
                   3283:                     if (ref($usertypes) eq 'HASH') {
1.334     raeburn  3284:                         $newsettings{'inststatus'} = join(', ',map{ $usertypes->{$_}; } (@inststatuses)); 
1.286     raeburn  3285:                     } else {
1.337     raeburn  3286:                         $newsettings{'inststatus'} = join(', ',@inststatuses);
1.286     raeburn  3287:                     }
1.334     raeburn  3288:                 }
                   3289:             } else {
                   3290:                 $newinststatus = '';
                   3291:                 $changeHash{'inststatus'} = $newinststatus;
                   3292:                 $newsettings{'inststatus'} = $othertitle;
                   3293:                 if ($newinststatus ne $oldinststatus) {
                   3294:                     $changed{'inststatus'} = $changeHash{'inststatus'};
1.378     raeburn  3295:                     foreach my $name ('portfolio','author') {
                   3296:                         ($newdefquota{$name},$newsettingstatus{$name}) =
                   3297:                             &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   3298:                     }
1.286     raeburn  3299:                 }
                   3300:             }
1.334     raeburn  3301:         } elsif ($context ne 'selfcreate') {
                   3302:             $canshow{'inststatus'} = 1;
1.337     raeburn  3303:             $newsettings{'inststatus'} = $oldsettings{'inststatus'};
1.286     raeburn  3304:         }
1.378     raeburn  3305:         foreach my $name ('portfolio','author') {
                   3306:             $changeHash{$name.'quota'} = $userenv{$name.'quota'};
                   3307:         }
1.334     raeburn  3308:         if ($context eq 'domain') {
1.378     raeburn  3309:             foreach my $name ('portfolio','author') {
                   3310:                 if ($userenv{$name.'quota'} ne '') {
                   3311:                     $oldquota{$name} = $userenv{$name.'quota'};
                   3312:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   3313:                         if ($env{'form.'.$name.'quota'} eq '') {
                   3314:                             $newquota{$name} = 0;
                   3315:                         } else {
                   3316:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   3317:                             $newquota{$name} =~ s/[^\d\.]//g;
                   3318:                         }
                   3319:                         if ($newquota{$name} != $oldquota{$name}) {
                   3320:                             if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   3321:                                 $changed{$name.'quota'} = 1;
                   3322:                             }
                   3323:                         }
1.334     raeburn  3324:                     } else {
1.378     raeburn  3325:                         if (&quota_admin('',\%changeHash,$name)) {
                   3326:                             $changed{$name.'quota'} = 1;
                   3327:                             $newquota{$name} = $newdefquota{$name};
                   3328:                             $newisdefault{$name} = 1;
                   3329:                         }
1.334     raeburn  3330:                     }
1.149     raeburn  3331:                 } else {
1.378     raeburn  3332:                     $oldisdefault{$name} = 1;
                   3333:                     $oldquota{$name} = $olddefquota{$name};
                   3334:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   3335:                         if ($env{'form.'.$name.'quota'} eq '') {
                   3336:                             $newquota{$name} = 0;
                   3337:                         } else {
                   3338:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   3339:                             $newquota{$name} =~ s/[^\d\.]//g;
                   3340:                         }
                   3341:                         if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   3342:                             $changed{$name.'quota'} = 1;
                   3343:                         }
1.334     raeburn  3344:                     } else {
1.378     raeburn  3345:                         $newquota{$name} = $newdefquota{$name};
                   3346:                         $newisdefault{$name} = 1;
1.334     raeburn  3347:                     }
1.378     raeburn  3348:                 }
                   3349:                 if ($oldisdefault{$name}) {
                   3350:                     $oldsettingstext{'quota'}{$name} = &get_defaultquota_text($oldsettingstatus{$name});
1.383     raeburn  3351:                 }  else {
                   3352:                     $oldsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$oldquota{$name});
1.378     raeburn  3353:                 }
                   3354:                 if ($newisdefault{$name}) {
                   3355:                     $newsettingstext{'quota'}{$name} = &get_defaultquota_text($newsettingstatus{$name});
1.383     raeburn  3356:                 } else {
                   3357:                     $newsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$newquota{$name});
1.134     raeburn  3358:                 }
                   3359:             }
1.334     raeburn  3360:             &tool_changes('tools',\@usertools,\%oldsettings,\%oldsettingstext,\%userenv,
                   3361:                           \%changeHash,\%changed,\%newsettings,\%newsettingstext);
                   3362:             if ($env{'form.ccdomain'} eq $env{'request.role.domain'}) {
                   3363:                 &tool_changes('requestcourses',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   3364:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.384     raeburn  3365:                 &tool_changes('requestauthor',\@requestauthor,\%oldsettings,\%oldsettingstext,
                   3366:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  3367:             } else {
1.334     raeburn  3368:                 &tool_changes('reqcrsotherdom',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   3369:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  3370:             }
                   3371:         }
1.334     raeburn  3372:         foreach my $item (@userinfo) {
                   3373:             if ($env{'form.c'.$item} ne $userenv{$item}) {
                   3374:                 $namechanged{$item} = 1;
                   3375:             }
1.204     raeburn  3376:         }
1.378     raeburn  3377:         foreach my $name ('portfolio','author') {
1.390     bisitz   3378:             $oldsettings{'quota'}{$name} = &mt('[_1] MB',$oldquota{$name});
                   3379:             $newsettings{'quota'}{$name} = &mt('[_1] MB',$newquota{$name});
1.378     raeburn  3380:         }
1.334     raeburn  3381:         if ((keys(%namechanged) > 0) || (keys(%changed) > 0)) {
1.267     raeburn  3382:             my ($chgresult,$namechgresult);
                   3383:             if (keys(%changed) > 0) {
                   3384:                 $chgresult = 
1.204     raeburn  3385:                     &Apache::lonnet::put('environment',\%changeHash,
                   3386:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
1.267     raeburn  3387:                 if ($chgresult eq 'ok') {
                   3388:                     if (($env{'user.name'} eq $env{'form.ccuname'}) &&
                   3389:                         ($env{'user.domain'} eq $env{'form.ccdomain'})) {
1.270     raeburn  3390:                         my %newenvhash;
                   3391:                         foreach my $key (keys(%changed)) {
1.411     raeburn  3392:                             if (($key eq 'official') || ($key eq 'unofficial') ||
                   3393:                                 ($key eq 'community') || ($key eq 'textbook') ||
1.449     raeburn  3394:                                 ($key eq 'placement') || ($key eq 'lti')) {
1.279     raeburn  3395:                                 $newenvhash{'environment.requestcourses.'.$key} =
                   3396:                                     $changeHash{'requestcourses.'.$key};
1.362     raeburn  3397:                                 if ($changeHash{'requestcourses.'.$key}) {
1.332     raeburn  3398:                                     $newenvhash{'environment.canrequest.'.$key} = 1;
1.279     raeburn  3399:                                 } else {
                   3400:                                     $newenvhash{'environment.canrequest.'.$key} =
                   3401:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3402:                                             $key,'reload','requestcourses');
                   3403:                                 }
1.362     raeburn  3404:                             } elsif ($key eq 'requestauthor') {
                   3405:                                 $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3406:                                 if ($changeHash{$key}) {
                   3407:                                     $newenvhash{'environment.canrequest.author'} = 1;
                   3408:                                 } else {
                   3409:                                     $newenvhash{'environment.canrequest.author'} =
                   3410:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3411:                                             $key,'reload','requestauthor');
                   3412:                                 }
1.275     raeburn  3413:                             } elsif ($key ne 'quota') {
1.270     raeburn  3414:                                 $newenvhash{'environment.tools.'.$key} = 
                   3415:                                     $changeHash{'tools.'.$key};
1.279     raeburn  3416:                                 if ($changeHash{'tools.'.$key} ne '') {
                   3417:                                     $newenvhash{'environment.availabletools.'.$key} =
                   3418:                                         $changeHash{'tools.'.$key};
                   3419:                                 } else {
                   3420:                                     $newenvhash{'environment.availabletools.'.$key} =
1.367     golterma 3421:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3422:           $key,'reload','tools');
1.279     raeburn  3423:                                 }
1.270     raeburn  3424:                             }
                   3425:                         }
1.271     raeburn  3426:                         if (keys(%newenvhash)) {
                   3427:                             &Apache::lonnet::appenv(\%newenvhash);
                   3428:                         }
1.267     raeburn  3429:                     }
1.463     raeburn  3430:                     if ($changed{'aboutme'}) {
                   3431:                         &Apache::loncommon::devalidate_aboutme_cache($env{'form.ccuname'},
                   3432:                                                                      $env{'form.ccdomain'});
                   3433:                     }
1.267     raeburn  3434:                 }
1.204     raeburn  3435:             }
1.334     raeburn  3436:             if (keys(%namechanged) > 0) {
1.337     raeburn  3437:                 foreach my $field (@userinfo) {
                   3438:                     $changeHash{$field}  = $env{'form.c'.$field};
                   3439:                 }
                   3440: # Make the change
1.204     raeburn  3441:                 $namechgresult =
                   3442:                     &Apache::lonnet::modifyuser($env{'form.ccdomain'},
                   3443:                         $env{'form.ccuname'},$changeHash{'id'},undef,undef,
                   3444:                         $changeHash{'firstname'},$changeHash{'middlename'},
                   3445:                         $changeHash{'lastname'},$changeHash{'generation'},
1.337     raeburn  3446:                         $changeHash{'id'},undef,$changeHash{'permanentemail'},undef,\@userinfo);
1.220     raeburn  3447:                 %userupdate = (
                   3448:                                lastname   => $env{'form.clastname'},
                   3449:                                middlename => $env{'form.cmiddlename'},
                   3450:                                firstname  => $env{'form.cfirstname'},
                   3451:                                generation => $env{'form.cgeneration'},
                   3452:                                id         => $env{'form.cid'},
                   3453:                              );
1.204     raeburn  3454:             }
1.334     raeburn  3455:             if (((keys(%namechanged) > 0) && $namechgresult eq 'ok') || 
1.267     raeburn  3456:                 ((keys(%changed) > 0) && $chgresult eq 'ok')) {
1.28      matthew  3457:             # Tell the user we changed the name
1.334     raeburn  3458:                 &display_userinfo($r,1,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3459:                                   \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,
1.334     raeburn  3460:                                   \%oldsettings, \%oldsettingstext,\%newsettings,
                   3461:                                   \%newsettingstext);
1.203     raeburn  3462:                 if ($env{'form.cid'} ne $userenv{'id'}) {
                   3463:                     &Apache::lonnet::idput($env{'form.ccdomain'},
1.407     raeburn  3464:                          {$env{'form.ccuname'} => $env{'form.cid'}},$uhome,'ids');
1.203     raeburn  3465:                     if (($recurseid) &&
                   3466:                         (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
                   3467:                         my $idresult = 
                   3468:                             &Apache::lonuserutils::propagate_id_change(
                   3469:                                 $env{'form.ccuname'},$env{'form.ccdomain'},
                   3470:                                 \%userupdate);
                   3471:                         $r->print('<br />'.$idresult.'<br />');
                   3472:                     }
1.196     raeburn  3473:                 }
1.149     raeburn  3474:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
                   3475:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
                   3476:                     my %newenvhash;
                   3477:                     foreach my $key (keys(%changeHash)) {
                   3478:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3479:                     }
1.238     raeburn  3480:                     &Apache::lonnet::appenv(\%newenvhash);
1.149     raeburn  3481:                 }
1.28      matthew  3482:             } else { # error occurred
1.389     bisitz   3483:                 $r->print(
                   3484:                     '<p class="LC_error">'
                   3485:                    .&mt('Unable to successfully change environment for [_1] in domain [_2].',
                   3486:                             '"'.$env{'form.ccuname'}.'"',
                   3487:                             '"'.$env{'form.ccdomain'}.'"')
                   3488:                    .'</p>');
1.28      matthew  3489:             }
1.334     raeburn  3490:         } else { # End of if ($env ... ) logic
1.275     raeburn  3491:             # They did not want to change the users name, quota, tool availability,
                   3492:             # or ability to request creation of courses, 
1.267     raeburn  3493:             # but we can still tell them what the name and quota and availabilities are  
1.334     raeburn  3494:             &display_userinfo($r,undef,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3495:                               \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,\%oldsettings,
1.334     raeburn  3496:                               \%oldsettingstext,\%newsettings,\%newsettingstext);
1.28      matthew  3497:         }
1.206     raeburn  3498:         if (@mod_disallowed) {
                   3499:             my ($rolestr,$contextname);
                   3500:             if (@longroles > 0) {
                   3501:                 $rolestr = join(', ',@longroles);
                   3502:             } else {
                   3503:                 $rolestr = &mt('No roles');
                   3504:             }
                   3505:             if ($context eq 'course') {
1.399     bisitz   3506:                 $contextname = 'course';
1.206     raeburn  3507:             } elsif ($context eq 'author') {
1.399     bisitz   3508:                 $contextname = 'co-author';
1.206     raeburn  3509:             }
                   3510:             $r->print(&mt('The following fields were not updated: ').'<ul>');
                   3511:             my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   3512:             foreach my $field (@mod_disallowed) {
                   3513:                 $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
                   3514:             }
1.207     raeburn  3515:             $r->print('</ul>');
                   3516:             if (@mod_disallowed == 1) {
1.399     bisitz   3517:                 $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  3518:             } else {
1.399     bisitz   3519:                 $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  3520:             }
1.292     bisitz   3521:             my $helplink = 'javascript:helpMenu('."'display'".')';
                   3522:             $r->print('<span class="LC_cusr_emph">'.$rolestr.'</span><br />'
                   3523:                      .&mt('Please contact your [_1]helpdesk[_2] for more information.'
                   3524:                          ,'<a href="'.$helplink.'">','</a>')
                   3525:                       .'<br />');
1.206     raeburn  3526:         }
1.259     bisitz   3527:         $r->print('<span class="LC_warning">'
                   3528:                   .$no_forceid_alert
                   3529:                   .&Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules)
                   3530:                   .'</span>');
1.4       www      3531:     }
1.367     golterma 3532:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.220     raeburn  3533:     if ($env{'form.action'} eq 'singlestudent') {
1.375     raeburn  3534:         &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context,
                   3535:                                $crstype,$showcredits,$defaultcredits);
1.386     bisitz   3536:         my $linktext = ($crstype eq 'Community' ?
                   3537:             &mt('Enroll Another Member') : &mt('Enroll Another Student'));
                   3538:         $r->print(
                   3539:             &Apache::lonhtmlcommon::actionbox([
                   3540:                 '<a href="javascript:backPage(document.userupdate)">'
                   3541:                .($crstype eq 'Community' ? 
                   3542:                     &mt('Enroll Another Member') : &mt('Enroll Another Student'))
                   3543:                .'</a>']));
1.220     raeburn  3544:     } else {
1.375     raeburn  3545:         my @rolechanges = &update_roles($r,$context,$showcredits);
1.334     raeburn  3546:         if (keys(%namechanged) > 0) {
1.220     raeburn  3547:             if ($context eq 'course') {
                   3548:                 if (@userroles > 0) {
1.225     raeburn  3549:                     if ((@rolechanges == 0) || 
                   3550:                         (!(grep(/^st$/,@rolechanges)))) {
                   3551:                         if (grep(/^st$/,@userroles)) {
                   3552:                             my $classlistupdated =
                   3553:                                 &Apache::lonuserutils::update_classlist($cdom,
1.220     raeburn  3554:                                               $cnum,$env{'form.ccdomain'},
                   3555:                                        $env{'form.ccuname'},\%userupdate);
1.225     raeburn  3556:                         }
1.220     raeburn  3557:                     }
                   3558:                 }
                   3559:             }
                   3560:         }
1.226     raeburn  3561:         my $userinfo = &Apache::loncommon::plainname($env{'form.ccuname'},
1.233     raeburn  3562:                                                      $env{'form.ccdomain'});
                   3563:         if ($env{'form.popup'}) {
                   3564:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   3565:         } else {
1.367     golterma 3566:             $r->print('<br />'.&Apache::lonhtmlcommon::actionbox(['<a href="javascript:backPage(document.userupdate,'."'$env{'form.prevphase'}','modify'".')">'
                   3567:                      .&mt('Modify this user: [_1]','<span class="LC_cusr_emph">'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.' ('.$userinfo.')</span>').'</a>',
                   3568:                      '<a href="javascript:backPage(document.userupdate)">'.&mt('Create/Modify Another User').'</a>']));
1.233     raeburn  3569:         }
1.220     raeburn  3570:     }
                   3571: }
                   3572: 
1.334     raeburn  3573: sub display_userinfo {
1.362     raeburn  3574:     my ($r,$changed,$order,$canshow,$requestcourses,$usertools,$requestauthor,
                   3575:         $userenv,$changedhash,$namechangedhash,$oldsetting,$oldsettingtext,
1.334     raeburn  3576:         $newsetting,$newsettingtext) = @_;
                   3577:     return unless (ref($order) eq 'ARRAY' &&
                   3578:                    ref($canshow) eq 'HASH' && 
                   3579:                    ref($requestcourses) eq 'ARRAY' && 
1.362     raeburn  3580:                    ref($requestauthor) eq 'ARRAY' &&
1.334     raeburn  3581:                    ref($usertools) eq 'ARRAY' && 
                   3582:                    ref($userenv) eq 'HASH' &&
                   3583:                    ref($changedhash) eq 'HASH' &&
                   3584:                    ref($oldsetting) eq 'HASH' &&
                   3585:                    ref($oldsettingtext) eq 'HASH' &&
                   3586:                    ref($newsetting) eq 'HASH' &&
                   3587:                    ref($newsettingtext) eq 'HASH');
                   3588:     my %lt=&Apache::lonlocal::texthash(
1.372     raeburn  3589:          'ui'             => 'User Information',
1.334     raeburn  3590:          'uic'            => 'User Information Changed',
                   3591:          'firstname'      => 'First Name',
                   3592:          'middlename'     => 'Middle Name',
                   3593:          'lastname'       => 'Last Name',
                   3594:          'generation'     => 'Generation',
                   3595:          'id'             => 'Student/Employee ID',
                   3596:          'permanentemail' => 'Permanent e-mail address',
1.378     raeburn  3597:          'portfolioquota' => 'Disk space allocated to portfolio files',
1.385     bisitz   3598:          'authorquota'    => 'Disk space allocated to Authoring Space',
1.334     raeburn  3599:          'blog'           => 'Blog Availability',
1.361     raeburn  3600:          'webdav'         => 'WebDAV Availability',
1.334     raeburn  3601:          'aboutme'        => 'Personal Information Page Availability',
                   3602:          'portfolio'      => 'Portfolio Availability',
1.459     raeburn  3603:          'timezone'       => 'Can set own Time Zone',
1.334     raeburn  3604:          'official'       => 'Can Request Official Courses',
                   3605:          'unofficial'     => 'Can Request Unofficial Courses',
                   3606:          'community'      => 'Can Request Communities',
1.384     raeburn  3607:          'textbook'       => 'Can Request Textbook Courses',
1.411     raeburn  3608:          'placement'      => 'Can Request Placement Tests',
1.449     raeburn  3609:          'lti'            => 'Can Request LTI Courses',
1.362     raeburn  3610:          'requestauthor'  => 'Can Request Author Role',
1.334     raeburn  3611:          'inststatus'     => "Affiliation",
                   3612:          'prvs'           => 'Previous Value:',
                   3613:          'chto'           => 'Changed To:'
                   3614:     );
                   3615:     if ($changed) {
1.372     raeburn  3616:         $r->print('<h3>'.$lt{'uic'}.'</h3>'.
1.367     golterma 3617:                 &Apache::loncommon::start_data_table().
                   3618:                 &Apache::loncommon::start_data_table_header_row());
1.334     raeburn  3619:         $r->print("<th>&nbsp;</th>\n");
1.367     golterma 3620:         $r->print('<th><b>'.$lt{'prvs'}.'</b></th>');
                   3621:         $r->print('<th><span class="LC_nobreak"><b>'.$lt{'chto'}.'</b></span></th>');
                   3622:         $r->print(&Apache::loncommon::end_data_table_header_row());
                   3623:         my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
                   3624: 
1.334     raeburn  3625:         foreach my $item (@userinfo) {
                   3626:             my $value = $env{'form.c'.$item};
1.367     golterma 3627:             #show changes only:
1.383     raeburn  3628:             unless ($value eq $userenv->{$item}){
1.367     golterma 3629:                 $r->print(&Apache::loncommon::start_data_table_row());
                   3630:                 $r->print("<td>$lt{$item}</td>\n");
1.383     raeburn  3631:                 $r->print("<td>".$userenv->{$item}."</td>\n");
1.367     golterma 3632:                 $r->print("<td>$value </td>\n");
                   3633:                 $r->print(&Apache::loncommon::end_data_table_row());
1.334     raeburn  3634:             }
                   3635:         }
                   3636:         foreach my $entry (@{$order}) {
1.383     raeburn  3637:             if ($canshow->{$entry}) {
                   3638:                 if (($entry eq 'requestcourses') || ($entry eq 'reqcrsotherdom') || ($entry eq 'requestauthor')) {
                   3639:                     my @items;
                   3640:                     if ($entry eq 'requestauthor') {
                   3641:                         @items = ($entry);
                   3642:                     } else {
                   3643:                         @items = @{$requestcourses};
1.384     raeburn  3644:                     }
1.383     raeburn  3645:                     foreach my $item (@items) {
                   3646:                         if (($newsetting->{$item} ne $oldsetting->{$item}) || 
                   3647:                             ($newsettingtext->{$item} ne $oldsettingtext->{$item})) {
                   3648:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");  
                   3649:                             $r->print("<td>$lt{$item}</td>\n");
                   3650:                             $r->print("<td>".$oldsetting->{$item});
                   3651:                             if ($oldsettingtext->{$item}) {
                   3652:                                 if ($oldsetting->{$item}) {
                   3653:                                     $r->print(' -- ');
                   3654:                                 }
                   3655:                                 $r->print($oldsettingtext->{$item});
                   3656:                             }
                   3657:                             $r->print("</td>\n");
                   3658:                             $r->print("<td>".$newsetting->{$item});
                   3659:                             if ($newsettingtext->{$item}) {
                   3660:                                 if ($newsetting->{$item}) {
                   3661:                                     $r->print(' -- ');
                   3662:                                 }
                   3663:                                 $r->print($newsettingtext->{$item});
                   3664:                             }
                   3665:                             $r->print("</td>\n");
                   3666:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3667:                         }
                   3668:                     }
                   3669:                 } elsif ($entry eq 'tools') {
                   3670:                     foreach my $item (@{$usertools}) {
1.383     raeburn  3671:                         if ($newsetting->{$item} ne $oldsetting->{$item}) {
                   3672:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3673:                             $r->print("<td>$lt{$item}</td>\n");
                   3674:                             $r->print("<td>".$oldsetting->{$item}.' '.$oldsettingtext->{$item}."</td>\n");
                   3675:                             $r->print("<td>".$newsetting->{$item}.' '.$newsettingtext->{$item}."</td>\n");
                   3676:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3677:                         }
                   3678:                     }
1.378     raeburn  3679:                 } elsif ($entry eq 'quota') {
                   3680:                     if ((ref($oldsetting->{$entry}) eq 'HASH') && (ref($oldsettingtext->{$entry}) eq 'HASH') &&
                   3681:                         (ref($newsetting->{$entry}) eq 'HASH') && (ref($newsettingtext->{$entry}) eq 'HASH')) {
                   3682:                         foreach my $name ('portfolio','author') {
1.383     raeburn  3683:                             if ($newsetting->{$entry}->{$name} ne $oldsetting->{$entry}->{$name}) {
                   3684:                                 $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3685:                                 $r->print("<td>$lt{$name.$entry}</td>\n");
                   3686:                                 $r->print("<td>".$oldsettingtext->{$entry}->{$name}."</td>\n");
                   3687:                                 $r->print("<td>".$newsettingtext->{$entry}->{$name}."</td>\n");
                   3688:                                 $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.378     raeburn  3689:                             }
                   3690:                         }
                   3691:                     }
1.334     raeburn  3692:                 } else {
1.383     raeburn  3693:                     if ($newsetting->{$entry} ne $oldsetting->{$entry}) {
                   3694:                         $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3695:                         $r->print("<td>$lt{$entry}</td>\n");
                   3696:                         $r->print("<td>".$oldsetting->{$entry}.' '.$oldsettingtext->{$entry}."</td>\n");
                   3697:                         $r->print("<td>".$newsetting->{$entry}.' '.$newsettingtext->{$entry}."</td>\n");
                   3698:                         $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3699:                     }
                   3700:                 }
                   3701:             }
                   3702:         }
1.367     golterma 3703:         $r->print(&Apache::loncommon::end_data_table().'<br />');
1.372     raeburn  3704:     } else {
                   3705:         $r->print('<h3>'.$lt{'ui'}.'</h3>'.
                   3706:                   '<p>'.&mt('No changes made to user information').'</p>');
1.334     raeburn  3707:     }
                   3708:     return;
                   3709: }
                   3710: 
1.275     raeburn  3711: sub tool_changes {
                   3712:     my ($context,$usertools,$oldaccess,$oldaccesstext,$userenv,$changeHash,
                   3713:         $changed,$newaccess,$newaccesstext) = @_;
                   3714:     if (!((ref($usertools) eq 'ARRAY') && (ref($oldaccess) eq 'HASH') &&
                   3715:           (ref($oldaccesstext) eq 'HASH') && (ref($userenv) eq 'HASH') &&
                   3716:           (ref($changeHash) eq 'HASH') && (ref($changed) eq 'HASH') &&
                   3717:           (ref($newaccess) eq 'HASH') && (ref($newaccesstext) eq 'HASH'))) {
                   3718:         return;
                   3719:     }
1.383     raeburn  3720:     my %reqdisplay = &requestchange_display();
1.300     raeburn  3721:     if ($context eq 'reqcrsotherdom') {
1.309     raeburn  3722:         my @options = ('approval','validate','autolimit');
1.306     raeburn  3723:         my $optregex = join('|',@options);
1.300     raeburn  3724:         my $cdom = $env{'request.role.domain'};
                   3725:         foreach my $tool (@{$usertools}) {
1.383     raeburn  3726:             $oldaccesstext->{$tool} = &mt("availability set to 'off'");
1.314     raeburn  3727:             $newaccesstext->{$tool} = $oldaccesstext->{$tool};
1.300     raeburn  3728:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
1.383     raeburn  3729:             my ($newop,$limit);
1.314     raeburn  3730:             if ($env{'form.'.$context.'_'.$tool}) {
                   3731:                 $newop = $env{'form.'.$context.'_'.$tool};
                   3732:                 if ($newop eq 'autolimit') {
1.383     raeburn  3733:                     $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
1.314     raeburn  3734:                     $limit =~ s/\D+//g;
                   3735:                     $newop .= '='.$limit;
                   3736:                 }
                   3737:             }
1.300     raeburn  3738:             if ($userenv->{$context.'.'.$tool} eq '') {
1.314     raeburn  3739:                 if ($newop) {
                   3740:                     $changed->{$tool}=&tool_admin($tool,$cdom.':'.$newop,
1.300     raeburn  3741:                                                   $changeHash,$context);
                   3742:                     if ($changed->{$tool}) {
1.383     raeburn  3743:                         if ($newop =~ /^autolimit/) {
                   3744:                             if ($limit) {
                   3745:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3746:                             } else {
                   3747:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3748:                             }
                   3749:                         } else {
                   3750:                             $newaccesstext->{$tool} = $reqdisplay{$newop};
                   3751:                         }
1.300     raeburn  3752:                     } else {
                   3753:                         $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3754:                     }
                   3755:                 }
                   3756:             } else {
                   3757:                 my @curr = split(',',$userenv->{$context.'.'.$tool});
                   3758:                 my @new;
                   3759:                 my $changedoms;
1.314     raeburn  3760:                 foreach my $req (@curr) {
                   3761:                     if ($req =~ /^\Q$cdom\E\:($optregex\=?\d*)$/) {
                   3762:                         my $oldop = $1;
1.383     raeburn  3763:                         if ($oldop =~ /^autolimit=(\d*)/) {
                   3764:                             my $limit = $1;
                   3765:                             if ($limit) {
                   3766:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3767:                             } else {
                   3768:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3769:                             }
                   3770:                         } else {
                   3771:                             $oldaccesstext->{$tool} = $reqdisplay{$oldop};
                   3772:                         }
1.314     raeburn  3773:                         if ($oldop ne $newop) {
                   3774:                             $changedoms = 1;
                   3775:                             foreach my $item (@curr) {
                   3776:                                 my ($reqdom,$option) = split(':',$item);
                   3777:                                 unless ($reqdom eq $cdom) {
                   3778:                                     push(@new,$item);
                   3779:                                 }
                   3780:                             }
                   3781:                             if ($newop) {
                   3782:                                 push(@new,$cdom.':'.$newop);
1.300     raeburn  3783:                             }
1.314     raeburn  3784:                             @new = sort(@new);
1.300     raeburn  3785:                         }
1.314     raeburn  3786:                         last;
1.300     raeburn  3787:                     }
1.314     raeburn  3788:                 }
                   3789:                 if ((!$changedoms) && ($newop)) {
1.300     raeburn  3790:                     $changedoms = 1;
1.306     raeburn  3791:                     @new = sort(@curr,$cdom.':'.$newop);
1.300     raeburn  3792:                 }
                   3793:                 if ($changedoms) {
1.314     raeburn  3794:                     my $newdomstr;
1.300     raeburn  3795:                     if (@new) {
                   3796:                         $newdomstr = join(',',@new);
                   3797:                     }
                   3798:                     $changed->{$tool}=&tool_admin($tool,$newdomstr,$changeHash,
                   3799:                                                   $context);
                   3800:                     if ($changed->{$tool}) {
                   3801:                         if ($env{'form.'.$context.'_'.$tool}) {
1.306     raeburn  3802:                             if ($env{'form.'.$context.'_'.$tool} eq 'autolimit') {
1.314     raeburn  3803:                                 my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3804:                                 $limit =~ s/\D+//g;
                   3805:                                 if ($limit) {
1.383     raeburn  3806:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
1.314     raeburn  3807:                                 } else {
1.383     raeburn  3808:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
1.306     raeburn  3809:                                 }
1.314     raeburn  3810:                             } else {
1.306     raeburn  3811:                                 $newaccesstext->{$tool} = $reqdisplay{$env{'form.'.$context.'_'.$tool}};
                   3812:                             }
1.300     raeburn  3813:                         } else {
1.383     raeburn  3814:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
1.300     raeburn  3815:                         }
                   3816:                     }
                   3817:                 }
                   3818:             }
                   3819:         }
                   3820:         return;
                   3821:     }
1.275     raeburn  3822:     foreach my $tool (@{$usertools}) {
1.383     raeburn  3823:         my ($newval,$limit,$envkey);
1.362     raeburn  3824:         $envkey = $context.'.'.$tool;
1.306     raeburn  3825:         if ($context eq 'requestcourses') {
                   3826:             $newval = $env{'form.crsreq_'.$tool};
                   3827:             if ($newval eq 'autolimit') {
1.383     raeburn  3828:                 $limit = $env{'form.crsreq_'.$tool.'_limit'};
                   3829:                 $limit =~ s/\D+//g;
                   3830:                 $newval .= '='.$limit;
1.306     raeburn  3831:             }
1.362     raeburn  3832:         } elsif ($context eq 'requestauthor') {
                   3833:             $newval = $env{'form.'.$context};
                   3834:             $envkey = $context;
1.314     raeburn  3835:         } else {
1.306     raeburn  3836:             $newval = $env{'form.'.$context.'_'.$tool};
                   3837:         }
1.362     raeburn  3838:         if ($userenv->{$envkey} ne '') {
1.275     raeburn  3839:             $oldaccess->{$tool} = &mt('custom');
1.383     raeburn  3840:             if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3841:                 if ($userenv->{$envkey} =~ /^autolimit=(\d*)$/) {
                   3842:                     my $currlimit = $1;
                   3843:                     if ($currlimit eq '') {
                   3844:                         $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3845:                     } else {
                   3846:                         $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$currlimit);
                   3847:                     }
                   3848:                 } elsif ($userenv->{$envkey}) {
                   3849:                     $oldaccesstext->{$tool} = $reqdisplay{$userenv->{$envkey}};
                   3850:                 } else {
                   3851:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3852:                 }
1.275     raeburn  3853:             } else {
1.383     raeburn  3854:                 if ($userenv->{$envkey}) {
                   3855:                     $oldaccesstext->{$tool} = &mt("availability set to 'on'");
                   3856:                 } else {
                   3857:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3858:                 }
1.275     raeburn  3859:             }
1.362     raeburn  3860:             $changeHash->{$envkey} = $userenv->{$envkey};
1.275     raeburn  3861:             if ($env{'form.custom'.$tool} == 1) {
1.362     raeburn  3862:                 if ($newval ne $userenv->{$envkey}) {
1.306     raeburn  3863:                     $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3864:                                                     $context);
1.275     raeburn  3865:                     if ($changed->{$tool}) {
                   3866:                         $newaccess->{$tool} = &mt('custom');
1.383     raeburn  3867:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3868:                             if ($newval =~ /^autolimit/) {
                   3869:                                 if ($limit) {
                   3870:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3871:                                 } else {
                   3872:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3873:                                 }
                   3874:                             } elsif ($newval) {
                   3875:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3876:                             } else {
                   3877:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3878:                             }
1.275     raeburn  3879:                         } else {
1.383     raeburn  3880:                             if ($newval) {
                   3881:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3882:                             } else {
                   3883:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3884:                             }
1.275     raeburn  3885:                         }
                   3886:                     } else {
                   3887:                         $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  3888:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3889:                             if ($newval =~ /^autolimit/) {
                   3890:                                 if ($limit) {
                   3891:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3892:                                 } else {
                   3893:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3894:                                 }
                   3895:                             } elsif ($newval) {
                   3896:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3897:                             } else {
                   3898:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3899:                             }
1.275     raeburn  3900:                         } else {
1.383     raeburn  3901:                             if ($userenv->{$context.'.'.$tool}) {
                   3902:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3903:                             } else {
                   3904:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3905:                             }
1.275     raeburn  3906:                         }
                   3907:                     }
                   3908:                 } else {
                   3909:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3910:                     $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3911:                 }
                   3912:             } else {
                   3913:                 $changed->{$tool} = &tool_admin($tool,'',$changeHash,$context);
                   3914:                 if ($changed->{$tool}) {
                   3915:                     $newaccess->{$tool} = &mt('default');
                   3916:                 } else {
                   3917:                     $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  3918:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3919:                         if ($newval =~ /^autolimit/) {
                   3920:                             if ($limit) {
                   3921:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3922:                             } else {
                   3923:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3924:                             }
                   3925:                         } elsif ($newval) {
                   3926:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3927:                         } else {
                   3928:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3929:                         }
1.275     raeburn  3930:                     } else {
1.383     raeburn  3931:                         if ($userenv->{$context.'.'.$tool}) {
                   3932:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3933:                         } else {
                   3934:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3935:                         }
1.275     raeburn  3936:                     }
                   3937:                 }
                   3938:             }
                   3939:         } else {
                   3940:             $oldaccess->{$tool} = &mt('default');
                   3941:             if ($env{'form.custom'.$tool} == 1) {
1.306     raeburn  3942:                 $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3943:                                                 $context);
1.275     raeburn  3944:                 if ($changed->{$tool}) {
                   3945:                     $newaccess->{$tool} = &mt('custom');
1.383     raeburn  3946:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3947:                         if ($newval =~ /^autolimit/) {
                   3948:                             if ($limit) {
                   3949:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3950:                             } else {
                   3951:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3952:                             }
                   3953:                         } elsif ($newval) {
                   3954:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3955:                         } else {
                   3956:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3957:                         }
1.275     raeburn  3958:                     } else {
1.383     raeburn  3959:                         if ($newval) {
                   3960:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3961:                         } else {
                   3962:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3963:                         }
1.275     raeburn  3964:                     }
                   3965:                 } else {
                   3966:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3967:                 }
                   3968:             } else {
                   3969:                 $newaccess->{$tool} = $oldaccess->{$tool};
                   3970:             }
                   3971:         }
                   3972:     }
                   3973:     return;
                   3974: }
                   3975: 
1.220     raeburn  3976: sub update_roles {
1.375     raeburn  3977:     my ($r,$context,$showcredits) = @_;
1.4       www      3978:     my $now=time;
1.225     raeburn  3979:     my @rolechanges;
1.465   ! raeburn  3980:     my (%disallowed,%got_role_approvals,%got_instdoms,%process_by,%instdoms,
        !          3981:         %pending,%reject,%notifydc);
        !          3982:     $got_role_approvals{$context} = '';
        !          3983:     $process_by{$context} = {};
        !          3984:     my @domroles = &Apache::lonuserutils::domain_roles();
        !          3985:     my @cstrroles = &Apache::lonuserutils::construction_space_roles();
        !          3986:     my @courseroles = &Apache::lonuserutils::roles_by_context('course',1);
1.73      sakharuk 3987:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
1.404     raeburn  3988:     foreach my $key (keys(%env)) {
1.135     raeburn  3989: 	next if (! $env{$key});
1.190     raeburn  3990:         next if ($key eq 'form.action');
1.27      matthew  3991: 	# Revoke roles
1.135     raeburn  3992: 	if ($key=~/^form\.rev/) {
                   3993: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
1.64      www      3994: # Revoke standard role
1.170     albertel 3995: 		my ($scope,$role) = ($1,$2);
                   3996: 		my $result =
                   3997: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
                   3998: 						$env{'form.ccuname'},
1.239     raeburn  3999: 						$scope,$role,'','',$context);
1.367     golterma 4000:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   4001:                             &mt('Revoking [_1] in [_2]',
                   4002:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  4003:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   4004:                                 $result ne "ok").'<br />');
                   4005:                 if ($result ne "ok") {
                   4006:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4007:                 }
1.170     albertel 4008: 		if ($role eq 'st') {
1.202     raeburn  4009: 		    my $result = 
1.198     raeburn  4010:                         &Apache::lonuserutils::classlist_drop($scope,
                   4011:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  4012: 			    $now);
1.367     golterma 4013:                     $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.53      www      4014: 		}
1.225     raeburn  4015:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   4016:                     push(@rolechanges,$role);
                   4017:                 }
1.196     raeburn  4018: 	    }
1.195     raeburn  4019: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}s) {
1.64      www      4020: # Revoke custom role
1.369     bisitz   4021:                 my $result = &Apache::lonnet::revokecustomrole(
                   4022:                     $env{'form.ccdomain'},$env{'form.ccuname'},$1,$2,$3,$4,'','',$context);
1.367     golterma 4023:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   4024:                             &mt('Revoking custom role [_1] by [_2] in [_3]',
1.372     raeburn  4025:                                 $4,$3.':'.$2,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   4026:                             $result ne 'ok').'<br />');
                   4027:                 if ($result ne "ok") {
                   4028:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4029:                 }
1.225     raeburn  4030:                 if (!grep(/^cr$/,@rolechanges)) {
                   4031:                     push(@rolechanges,'cr');
                   4032:                 }
1.64      www      4033: 	    }
1.135     raeburn  4034: 	} elsif ($key=~/^form\.del/) {
                   4035: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
1.116     raeburn  4036: # Delete standard role
1.170     albertel 4037: 		my ($scope,$role) = ($1,$2);
                   4038: 		my $result =
                   4039: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
                   4040: 						$env{'form.ccuname'},
1.239     raeburn  4041: 						$scope,$role,$now,0,1,'',
                   4042:                                                 $context);
1.367     golterma 4043:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   4044:                             &mt('Deleting [_1] in [_2]',
1.369     bisitz   4045:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  4046:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   4047:                             $result ne 'ok').'<br />');
                   4048:                 if ($result ne "ok") {
                   4049:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4050:                 }
1.367     golterma 4051: 
1.170     albertel 4052: 		if ($role eq 'st') {
1.202     raeburn  4053: 		    my $result = 
1.198     raeburn  4054:                         &Apache::lonuserutils::classlist_drop($scope,
                   4055:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  4056: 			    $now);
1.369     bisitz   4057: 		    $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.81      albertel 4058: 		}
1.225     raeburn  4059:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   4060:                     push(@rolechanges,$role);
                   4061:                 }
1.116     raeburn  4062:             }
1.139     albertel 4063: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  4064:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   4065: # Delete custom role
1.369     bisitz   4066:                 my $result =
                   4067:                     &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
                   4068:                         $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
                   4069:                         0,1,$context);
                   4070:                 $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Deleting custom role [_1] by [_2] in [_3]',
1.372     raeburn  4071:                       $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   4072:                       $result ne "ok").'<br />');
                   4073:                 if ($result ne "ok") {
                   4074:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4075:                 }
1.367     golterma 4076: 
1.225     raeburn  4077:                 if (!grep(/^cr$/,@rolechanges)) {
                   4078:                     push(@rolechanges,'cr');
                   4079:                 }
1.116     raeburn  4080:             }
1.135     raeburn  4081: 	} elsif ($key=~/^form\.ren/) {
1.101     albertel 4082:             my $udom = $env{'form.ccdomain'};
                   4083:             my $uname = $env{'form.ccuname'};
1.116     raeburn  4084: # Re-enable standard role
1.135     raeburn  4085: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
1.89      raeburn  4086:                 my $url = $1;
                   4087:                 my $role = $2;
1.465   ! raeburn  4088:                 my $id = $url.'_'.$role;
1.89      raeburn  4089:                 my $logmsg;
                   4090:                 my $output;
                   4091:                 if ($role eq 'st') {
1.141     albertel 4092:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
1.374     raeburn  4093:                         my ($cdom,$cnum,$csec) = ($1,$2,$3);
1.375     raeburn  4094:                         my $credits;
                   4095:                         if ($showcredits) {
1.465   ! raeburn  4096:                             my $defaultcredits =
1.375     raeburn  4097:                                 &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   4098:                             $credits = &get_user_credits($defaultcredits,$cdom,$cnum);
                   4099:                         }
1.465   ! raeburn  4100:                         unless ($udom eq $cdom) {
        !          4101:                             next if (&Apache::lonuserutils::restricted_dom($context,$id,$udom,
        !          4102:                                          $uname,$role,$now,0,$cdom,$cnum,$csec,$credits,
        !          4103:                                          \%process_by,\%instdoms,\%got_role_approvals,
        !          4104:                                          \%got_instdoms,\%reject,\%pending,\%notifydc));
        !          4105:                         }
1.375     raeburn  4106:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$cdom,$cnum,$csec,$context,$credits);
1.220     raeburn  4107:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course') || ($result eq 'refused')) {
1.223     raeburn  4108:                             if ($result eq 'refused' && $logmsg) {
                   4109:                                 $output = $logmsg;
                   4110:                             } else { 
1.369     bisitz   4111:                                 $output = &mt('Error: [_1]',$result)."\n";
1.223     raeburn  4112:                             }
1.89      raeburn  4113:                         } else {
1.372     raeburn  4114:                             $output = &Apache::lonhtmlcommon::confirm_success(&mt('Assigning [_1] in [_2] starting [_3]',
                   4115:                                         &Apache::lonnet::plaintext($role),
                   4116:                                         &Apache::loncommon::show_role_extent($url,$context,'st'),
                   4117:                                         &Apache::lonlocal::locallocaltime($now))).'<br />'.$logmsg.'<br />';
1.89      raeburn  4118:                         }
                   4119:                     }
                   4120:                 } else {
1.465   ! raeburn  4121:                     my ($cdom,$cnum,$csec);
        !          4122:                     if (grep(/^\Q$role\E$/,@cstrroles)) {
        !          4123:                         ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_username)$});
        !          4124:                     } elsif (grep(/^\Q$role\E$/,@domroles)) {
        !          4125:                         ($cdom) = ($url =~ m{^/($match_domain)/$});
        !          4126:                     } elsif ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
        !          4127:                         ($cdom,$cnum,$csec) = ($1,$2,$3);
        !          4128:                     }
        !          4129:                     if ($cdom ne '') {
        !          4130:                         unless ($udom eq $cdom) {
        !          4131:                             next if (&Apache::lonuserutils::restricted_dom($context,$id,$udom,
        !          4132:                                          $uname,$role,$now,0,$cdom,$cnum,$csec,'',\%process_by,
        !          4133:                                          \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
        !          4134:                                          \%pending,\%notifydc));
        !          4135:                         }
        !          4136:                     }
1.101     albertel 4137: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
1.239     raeburn  4138:                                $env{'form.ccuname'},$url,$role,0,$now,'','',
                   4139:                                $context);
1.461     raeburn  4140:                     $output = &Apache::lonhtmlcommon::confirm_success(&mt('Re-enabling [_1] in [_2]',
                   4141:                                     &Apache::lonnet::plaintext($role),
                   4142:                                     &Apache::loncommon::show_role_extent($url,$context,$role)),$result ne "ok").'<br />';
1.369     bisitz   4143:                     if ($result ne "ok") {
                   4144:                         $output .= &mt('Error: [_1]',$result).'<br />';
                   4145:                     }
                   4146:                 }
1.89      raeburn  4147:                 $r->print($output);
1.225     raeburn  4148:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   4149:                     push(@rolechanges,$role);
                   4150:                 }
1.113     raeburn  4151: 	    }
1.116     raeburn  4152: # Re-enable custom role
1.139     albertel 4153: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  4154:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
1.465   ! raeburn  4155:                 my $id = $url.'_cr'."/$rdom/$rnam/$rolename";
        !          4156:                 my $role = "cr/$rdom/$rnam/$rolename";
        !          4157:                 if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
        !          4158:                     my ($cdom,$cnum,$csec) = ($1,$2,$3);
        !          4159:                     unless ($udom eq $cdom) {
        !          4160:                         next if (&Apache::lonuserutils::restricted_dom($context,$id,$udom,
        !          4161:                                      $uname,$role,$now,0,$cdom,$cnum,$csec,'',\%process_by,
        !          4162:                                      \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
        !          4163:                                      \%pending,\%notifydc));
        !          4164:                     }
        !          4165:                 }
1.116     raeburn  4166:                 my $result = &Apache::lonnet::assigncustomrole(
                   4167:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
1.240     raeburn  4168:                                $url,$rdom,$rnam,$rolename,0,$now,undef,$context);
1.369     bisitz   4169:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   4170:                     &mt('Re-enabling custom role [_1] by [_2] in [_3]',
1.372     raeburn  4171:                         $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   4172:                     $result ne "ok").'<br />');
                   4173:                 if ($result ne "ok") {
                   4174:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4175:                 }
1.225     raeburn  4176:                 if (!grep(/^cr$/,@rolechanges)) {
                   4177:                     push(@rolechanges,'cr');
                   4178:                 }
1.116     raeburn  4179:             }
1.135     raeburn  4180: 	} elsif ($key=~/^form\.act/) {
1.101     albertel 4181:             my $udom = $env{'form.ccdomain'};
                   4182:             my $uname = $env{'form.ccuname'};
1.141     albertel 4183: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
1.65      www      4184:                 # Activate a custom role
1.83      albertel 4185: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
                   4186: 		my $url='/'.$one.'/'.$two;
1.465   ! raeburn  4187:                 my $id = $url.'_cr/'."$three/$four/$five";
        !          4188:                 my $role = "cr/$three/$four/$five";
1.83      albertel 4189: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
1.65      www      4190: 
1.101     albertel 4191:                 my $start = ( $env{'form.start_'.$full} ?
                   4192:                               $env{'form.start_'.$full} :
1.88      raeburn  4193:                               $now );
1.101     albertel 4194:                 my $end   = ( $env{'form.end_'.$full} ?
                   4195:                               $env{'form.end_'.$full} :
1.88      raeburn  4196:                               0 );
1.465   ! raeburn  4197: 
1.88      raeburn  4198:                 # split multiple sections
                   4199:                 my %sections = ();
1.461     raeburn  4200:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$five);
1.88      raeburn  4201:                 if ($num_sections == 0) {
1.465   ! raeburn  4202:                     unless ($udom eq $one) {
        !          4203:                         next if (&Apache::lonuserutils::restricted_dom($context,$id,$udom,
        !          4204:                                      $uname,$role,$start,$end,$one,$two,'','',\%process_by,
        !          4205:                                      \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
        !          4206:                                      \%pending,\%notifydc));
        !          4207:                     }
1.240     raeburn  4208:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end,$context));
1.88      raeburn  4209:                 } else {
1.114     albertel 4210: 		    my %curr_groups =
1.117     raeburn  4211: 			&Apache::longroup::coursegroups($one,$two);
1.465   ! raeburn  4212:                     my ($restricted,$numchanges);
1.404     raeburn  4213:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.113     raeburn  4214:                         if (($sec eq 'none') || ($sec eq 'all') || 
                   4215:                             exists($curr_groups{$sec})) {
                   4216:                             $disallowed{$sec} = $url;
                   4217:                             next;
                   4218:                         }
                   4219:                         my $securl = $url.'/'.$sec;
1.465   ! raeburn  4220:                         my $secid = $securl.'_cr'."/$three/$four/$five";
        !          4221:                         undef($restricted);
        !          4222:                         unless ($udom eq $one) {
        !          4223:                             next if (&Apache::lonuserutils::restricted_dom($context,$secid,$udom,
        !          4224:                                          $uname,$role,$start,$end,$one,$two,$sec,'',\%process_by,
        !          4225:                                          \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
        !          4226:                                          \%pending,\%notifydc));
        !          4227:                         }
        !          4228:                         $numchanges ++;
1.240     raeburn  4229: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end,$context));
1.88      raeburn  4230:                     }
1.465   ! raeburn  4231:                     next unless ($numchanges);
1.88      raeburn  4232:                 }
1.225     raeburn  4233:                 if (!grep(/^cr$/,@rolechanges)) {
                   4234:                     push(@rolechanges,'cr');
                   4235:                 }
1.142     raeburn  4236: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
1.27      matthew  4237: 		# Activate roles for sections with 3 id numbers
                   4238: 		# set start, end times, and the url for the class
1.83      albertel 4239: 		my ($one,$two,$three)=($1,$2,$3);
1.461     raeburn  4240: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ?
                   4241: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  4242: 			      $now );
1.461     raeburn  4243: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ?
1.101     albertel 4244: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  4245: 			      0 );
1.83      albertel 4246: 		my $url='/'.$one.'/'.$two;
1.465   ! raeburn  4247:                 my $id = $url.'_'.$three;
1.88      raeburn  4248:                 my $type = 'three';
                   4249:                 # split multiple sections
                   4250:                 my %sections = ();
1.101     albertel 4251:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
1.465   ! raeburn  4252:                 my ($credits,$numchanges);
1.375     raeburn  4253:                 if ($three eq 'st') {
1.461     raeburn  4254:                     if ($showcredits) {
1.375     raeburn  4255:                         my $defaultcredits = 
                   4256:                             &Apache::lonuserutils::get_defaultcredits($one,$two);
                   4257:                         $credits = $env{'form.credits_'.$one.'_'.$two.'_'.$three};
                   4258:                         $credits =~ s/[^\d\.]//g;
                   4259:                         if ($credits eq $defaultcredits) {
                   4260:                             undef($credits);
                   4261:                         }
                   4262:                     }
                   4263:                 }
1.88      raeburn  4264:                 if ($num_sections == 0) {
1.465   ! raeburn  4265:                     unless ($udom eq $one) {
        !          4266:                         next if (&Apache::lonuserutils::restricted_dom($context,$id,$udom,
        !          4267:                                      $uname,$three,$start,$end,$one,$two,'',$credits,\%process_by,
        !          4268:                                      \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
        !          4269:                                      \%pending,\%notifydc));
        !          4270:                     }
        !          4271:                     $numchanges ++;
1.375     raeburn  4272:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  4273:                 } else {
1.114     albertel 4274:                     my %curr_groups = 
1.117     raeburn  4275: 			&Apache::longroup::coursegroups($one,$two);
1.88      raeburn  4276:                     my $emptysec = 0;
1.465   ! raeburn  4277:                     my $restricted;
1.404     raeburn  4278:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.88      raeburn  4279:                         $sec =~ s/\W//g;
1.113     raeburn  4280:                         if ($sec ne '') {
                   4281:                             if (($sec eq 'none') || ($sec eq 'all') || 
                   4282:                                 exists($curr_groups{$sec})) {
                   4283:                                 $disallowed{$sec} = $url;
                   4284:                                 next;
                   4285:                             }
1.88      raeburn  4286:                             my $securl = $url.'/'.$sec;
1.465   ! raeburn  4287:                             my $secid = $securl.'_'.$three;
        !          4288:                             unless ($udom eq $one) {
        !          4289:                                 undef($restricted);
        !          4290:                                 $restricted = &Apache::lonuserutils::restricted_dom($context,$secid,$udom,
        !          4291:                                                   $uname,$three,$start,$end,$one,$two,$sec,$credits,\%process_by,
        !          4292:                                                   \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
        !          4293:                                                   \%pending,\%notifydc);
        !          4294:                                 next if ($restricted);
        !          4295:                             }
        !          4296:                             $numchanges ++;
1.375     raeburn  4297:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec,$context,$credits));
1.88      raeburn  4298:                         } else {
                   4299:                             $emptysec = 1;
                   4300:                         }
                   4301:                     }
                   4302:                     if ($emptysec) {
1.465   ! raeburn  4303:                         unless ($udom eq $one) {
        !          4304:                             undef($restricted);
        !          4305:                             $restricted = &Apache::lonuserutils::restricted_dom($context,$id,$udom,
        !          4306:                                               $uname,$three,$start,$end,$one,$two,'',$credits,\%process_by,
        !          4307:                                               \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
        !          4308:                                               \%pending,\%notifydc);
        !          4309:                             next if ($restricted);
        !          4310:                         }
        !          4311:                         $numchanges ++;
1.375     raeburn  4312:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  4313:                     }
1.465   ! raeburn  4314:                     next unless ($numchanges);
1.225     raeburn  4315:                 }
                   4316:                 if (!grep(/^\Q$three\E$/,@rolechanges)) {
                   4317:                     push(@rolechanges,$three);
                   4318:                 }
1.135     raeburn  4319: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
1.27      matthew  4320: 		# Activate roles for sections with two id numbers
                   4321: 		# set start, end times, and the url for the class
1.461     raeburn  4322: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ?
                   4323: 			      $env{'form.start_'.$1.'_'.$2} :
1.27      matthew  4324: 			      $now );
1.461     raeburn  4325: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ?
1.101     albertel 4326: 			      $env{'form.end_'.$1.'_'.$2} :
1.27      matthew  4327: 			      0 );
1.225     raeburn  4328:                 my $one = $1;
                   4329:                 my $two = $2;
                   4330: 		my $url='/'.$one.'/';
1.465   ! raeburn  4331:                 my $id = $url.'_'.$two;
1.88      raeburn  4332:                 # split multiple sections
                   4333:                 my %sections = ();
1.465   ! raeburn  4334:                 my ($restricted,$numchanges);
1.225     raeburn  4335:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two},\%sections,$two);
1.88      raeburn  4336:                 if ($num_sections == 0) {
1.465   ! raeburn  4337:                     unless ($udom eq $one) {
        !          4338:                         $restricted = &Apache::lonuserutils::restricted_dom($context,$id,$udom,
        !          4339:                                           $uname,$two,$start,$end,$one,$two,'','',\%process_by,
        !          4340:                                           \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
        !          4341:                                           \%pending,\%notifydc);
        !          4342:                         next if ($restricted);
        !          4343:                     }
        !          4344:                     $numchanges ++;
1.240     raeburn  4345:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  4346:                 } else {
                   4347:                     my $emptysec = 0;
1.404     raeburn  4348:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.88      raeburn  4349:                         if ($sec ne '') {
                   4350:                             my $securl = $url.'/'.$sec;
1.465   ! raeburn  4351:                             my $secid = $securl.'_'.$two;
        !          4352:                             unless ($udom eq $one) {
        !          4353:                                 undef($restricted);
        !          4354:                                 $restricted = &Apache::lonuserutils::restricted_dom($context,$secid,$udom,
        !          4355:                                                   $uname,$two,$start,$end,$one,$two,$sec,'',\%process_by,
        !          4356:                                                   \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
        !          4357:                                                   \%pending,\%notifydc);
        !          4358:                                 next if ($restricted);
        !          4359:                             }
        !          4360:                             $numchanges ++;
1.240     raeburn  4361:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$two,$start,$end,$one,undef,$sec,$context));
1.88      raeburn  4362:                         } else {
                   4363:                             $emptysec = 1;
                   4364:                         }
                   4365:                     }
                   4366:                     if ($emptysec) {
1.465   ! raeburn  4367:                         unless ($udom eq $one) {
        !          4368:                             undef($restricted);
        !          4369:                             $restricted = &Apache::lonuserutils::restricted_dom($context,$id,$udom,
        !          4370:                                               $uname,$two,$start,$end,$one,$two,'','',\%process_by,
        !          4371:                                               \%instdoms,\%got_role_approvals,\%got_instdoms,\%reject,
        !          4372:                                               \%pending,\%notifydc);
        !          4373:                             next if ($restricted);
        !          4374:                         }
        !          4375:                         $numchanges ++;
1.240     raeburn  4376:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  4377:                     }
1.465   ! raeburn  4378:                     next unless ($numchanges); 
1.88      raeburn  4379:                 }
1.225     raeburn  4380:                 if (!grep(/^\Q$two\E$/,@rolechanges)) {
                   4381:                     push(@rolechanges,$two);
                   4382:                 }
1.64      www      4383: 	    } else {
1.190     raeburn  4384: 		$r->print('<p><span class="LC_error">'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></span></p><br />');
1.64      www      4385:             }
1.113     raeburn  4386:             foreach my $key (sort(keys(%disallowed))) {
1.274     bisitz   4387:                 $r->print('<p class="LC_warning">');
1.113     raeburn  4388:                 if (($key eq 'none') || ($key eq 'all')) {  
1.274     bisitz   4389:                     $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  4390:                 } else {
1.274     bisitz   4391:                     $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  4392:                 }
1.274     bisitz   4393:                 $r->print('</p><p>'
                   4394:                          .&mt('Please [_1]go back[_2] and choose a different section name.'
                   4395:                              ,'<a href="javascript:history.go(-1)'
                   4396:                              ,'</a>')
                   4397:                          .'</p><br />'
                   4398:                 );
1.113     raeburn  4399:             }
                   4400: 	}
1.101     albertel 4401:     } # End of foreach (keys(%env))
1.465   ! raeburn  4402:     if (keys(%reject)) {
        !          4403:         $r->print(&Apache::lonuserutils::print_roles_rejected($context,\%reject));
        !          4404:     }
        !          4405:     if (keys(%pending)) {
        !          4406:         $r->print(&Apache::lonuserutils::print_roles_queued($context,\%pending,\%notifydc));
        !          4407:     }
1.75      www      4408: # Flush the course logs so reverse user roles immediately updated
1.349     raeburn  4409:     $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.225     raeburn  4410:     if (@rolechanges == 0) {
1.372     raeburn  4411:         $r->print('<p>'.&mt('No roles to modify').'</p>');
1.193     raeburn  4412:     }
1.225     raeburn  4413:     return @rolechanges;
1.220     raeburn  4414: }
                   4415: 
1.375     raeburn  4416: sub get_user_credits {
                   4417:     my ($uname,$udom,$defaultcredits,$cdom,$cnum) = @_;
                   4418:     if ($cdom eq '' || $cnum eq '') {
                   4419:         return unless ($env{'request.course.id'});
                   4420:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4421:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4422:     }
                   4423:     my $credits;
                   4424:     my %currhash =
                   4425:         &Apache::lonnet::get('classlist',[$uname.':'.$udom],$cdom,$cnum);
                   4426:     if (keys(%currhash) > 0) {
                   4427:         my @items = split(/:/,$currhash{$uname.':'.$udom});
                   4428:         my $crdidx = &Apache::loncoursedata::CL_CREDITS() - 3;
                   4429:         $credits = $items[$crdidx];
                   4430:         $credits =~ s/[^\d\.]//g;
                   4431:     }
                   4432:     if ($credits eq $defaultcredits) {
                   4433:         undef($credits);
                   4434:     }
                   4435:     return $credits;
                   4436: }
                   4437: 
1.220     raeburn  4438: sub enroll_single_student {
1.375     raeburn  4439:     my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype,
                   4440:         $showcredits,$defaultcredits) = @_;
1.318     raeburn  4441:     $r->print('<h3>');
                   4442:     if ($crstype eq 'Community') {
                   4443:         $r->print(&mt('Enrolling Member'));
                   4444:     } else {
                   4445:         $r->print(&mt('Enrolling Student'));
                   4446:     }
                   4447:     $r->print('</h3>');
1.220     raeburn  4448: 
                   4449:     # Remove non alphanumeric values from section
                   4450:     $env{'form.sections'}=~s/\W//g;
                   4451: 
1.375     raeburn  4452:     my $credits;
                   4453:     if (($showcredits) && ($env{'form.credits'} ne '')) {
                   4454:         $credits = $env{'form.credits'};
                   4455:         $credits =~ s/[^\d\.]//g;
                   4456:         if ($credits ne '') {
                   4457:             if ($credits eq $defaultcredits) {
                   4458:                 undef($credits);
                   4459:             }
                   4460:         }
                   4461:     }
1.465   ! raeburn  4462:     my ($startdate,$enddate) = &Apache::lonuserutils::get_dates_from_form();
        !          4463:     my (%got_role_approvals,%got_instdoms,%process_by,%instdoms,%pending,%reject,%notifydc);
        !          4464:     unless ($env{'form.ccdomain'} eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
        !          4465:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
        !          4466:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
        !          4467:         my $csec = $env{'form.sections'};
        !          4468:         my $id = "/$cdom/$cnum";
        !          4469:         if ($csec ne '') {
        !          4470:             $id .= "/$csec";
        !          4471:         }
        !          4472:         $id .= '_st';
        !          4473:         if (&Apache::lonuserutils::restricted_dom($context,$id,$env{'form.ccdomain'},$env{'form.ccuname'},
        !          4474:                                                   'st',$startdate,$enddate,$cdom,$cnum,$csec,$credits,
        !          4475:                                                   \%process_by,\%instdoms,\%got_role_approvals,\%got_instdoms,
        !          4476:                                                   \%reject,\%pending,\%notifydc)) {
        !          4477:             if (keys(%reject)) {
        !          4478:                 $r->print(&Apache::lonuserutils::print_roles_rejected($context,\%reject));
        !          4479:             }
        !          4480:             if (keys(%pending)) {
        !          4481:                 $r->print(&Apache::lonuserutils::print_roles_queued($context,\%pending,\%notifydc));
        !          4482:             }
        !          4483:             return;
        !          4484:         }
        !          4485:     }
1.375     raeburn  4486: 
1.220     raeburn  4487:     # Clean out any old student roles the user has in this class.
                   4488:     &Apache::lonuserutils::modifystudent($env{'form.ccdomain'},
                   4489:          $env{'form.ccuname'},$env{'request.course.id'},undef,$uhome);
                   4490:     my $enroll_result =
                   4491:         &Apache::lonnet::modify_student_enrollment($env{'form.ccdomain'},
                   4492:             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
                   4493:             $env{'form.cmiddlename'},$env{'form.clastname'},
                   4494:             $env{'form.generation'},$env{'form.sections'},$enddate,
1.375     raeburn  4495:             $startdate,'manual',undef,$env{'request.course.id'},'',$context,
                   4496:             $credits);
1.220     raeburn  4497:     if ($enroll_result =~ /^ok/) {
1.381     bisitz   4498:         $r->print(&mt('[_1] enrolled','<b>'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.'</b>'));
1.220     raeburn  4499:         if ($env{'form.sections'} ne '') {
                   4500:             $r->print(' '.&mt('in section [_1]',$env{'form.sections'}));
                   4501:         }
                   4502:         my ($showstart,$showend);
                   4503:         if ($startdate <= $now) {
                   4504:             $showstart = &mt('Access starts immediately');
                   4505:         } else {
                   4506:             $showstart = &mt('Access starts: ').&Apache::lonlocal::locallocaltime($startdate);
                   4507:         }
                   4508:         if ($enddate == 0) {
                   4509:             $showend = &mt('ends: no ending date');
                   4510:         } else {
                   4511:             $showend = &mt('ends: ').&Apache::lonlocal::locallocaltime($enddate);
                   4512:         }
                   4513:         $r->print('.<br />'.$showstart.'; '.$showend);
                   4514:         if ($startdate <= $now && !$newuser) {
1.386     bisitz   4515:             $r->print('<p class="LC_info">');
1.318     raeburn  4516:             if ($crstype eq 'Community') {
1.392     raeburn  4517:                 $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  4518:             } else {
1.392     raeburn  4519:                 $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  4520:            }
                   4521:            $r->print('</p>');
1.220     raeburn  4522:         }
                   4523:     } else {
                   4524:         $r->print(&mt('unable to enroll').": ".$enroll_result);
                   4525:     }
                   4526:     return;
1.188     raeburn  4527: }
                   4528: 
1.204     raeburn  4529: sub get_defaultquota_text {
                   4530:     my ($settingstatus) = @_;
                   4531:     my $defquotatext; 
                   4532:     if ($settingstatus eq '') {
1.383     raeburn  4533:         $defquotatext = &mt('default');
1.204     raeburn  4534:     } else {
                   4535:         my ($usertypes,$order) =
                   4536:             &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
                   4537:         if ($usertypes->{$settingstatus} eq '') {
1.383     raeburn  4538:             $defquotatext = &mt('default');
1.204     raeburn  4539:         } else {
1.383     raeburn  4540:             $defquotatext = &mt('default for [_1]',$usertypes->{$settingstatus});
1.204     raeburn  4541:         }
                   4542:     }
                   4543:     return $defquotatext;
                   4544: }
                   4545: 
1.188     raeburn  4546: sub update_result_form {
                   4547:     my ($uhome) = @_;
                   4548:     my $outcome = 
1.367     golterma 4549:     '<form name="userupdate" method="post" action="">'."\n";
1.160     raeburn  4550:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
1.188     raeburn  4551:         $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  4552:     }
1.207     raeburn  4553:     if ($env{'form.origname'} ne '') {
                   4554:         $outcome .= '<input type="hidden" name="origname" value="'.$env{'form.origname'}.'" />'."\n";
                   4555:     }
1.160     raeburn  4556:     foreach my $item ('sortby','seluname','seludom') {
                   4557:         if (exists($env{'form.'.$item})) {
1.188     raeburn  4558:             $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  4559:         }
                   4560:     }
1.188     raeburn  4561:     if ($uhome eq 'no_host') {
                   4562:         $outcome .= '<input type="hidden" name="forcenewuser" value="1" />'."\n";
                   4563:     }
                   4564:     $outcome .= '<input type="hidden" name="phase" value="" />'."\n".
1.383     raeburn  4565:                 '<input type="hidden" name="currstate" value="" />'."\n".
                   4566:                 '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1.188     raeburn  4567:                 '</form>';
                   4568:     return $outcome;
1.4       www      4569: }
                   4570: 
1.149     raeburn  4571: sub quota_admin {
1.378     raeburn  4572:     my ($setquota,$changeHash,$name) = @_;
1.149     raeburn  4573:     my $quotachanged;
                   4574:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   4575:         # Current user has quota modification privileges
1.267     raeburn  4576:         if (ref($changeHash) eq 'HASH') {
                   4577:             $quotachanged = 1;
1.378     raeburn  4578:             $changeHash->{$name.'quota'} = $setquota;
1.267     raeburn  4579:         }
1.149     raeburn  4580:     }
                   4581:     return $quotachanged;
                   4582: }
                   4583: 
1.267     raeburn  4584: sub tool_admin {
1.275     raeburn  4585:     my ($tool,$settool,$changeHash,$context) = @_;
                   4586:     my $canchange = 0; 
1.279     raeburn  4587:     if ($context eq 'requestcourses') {
1.275     raeburn  4588:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
                   4589:             $canchange = 1;
                   4590:         }
1.300     raeburn  4591:     } elsif ($context eq 'reqcrsotherdom') {
                   4592:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   4593:             $canchange = 1;
                   4594:         }
1.362     raeburn  4595:     } elsif ($context eq 'requestauthor') {
                   4596:         if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
                   4597:             $canchange = 1;
                   4598:         }
1.275     raeburn  4599:     } elsif (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   4600:         # Current user has quota modification privileges
                   4601:         $canchange = 1;
                   4602:     }
1.267     raeburn  4603:     my $toolchanged;
1.275     raeburn  4604:     if ($canchange) {
1.267     raeburn  4605:         if (ref($changeHash) eq 'HASH') {
                   4606:             $toolchanged = 1;
1.362     raeburn  4607:             if ($tool eq 'requestauthor') {
                   4608:                 $changeHash->{$context} = $settool;
                   4609:             } else {
                   4610:                 $changeHash->{$context.'.'.$tool} = $settool;
                   4611:             }
1.267     raeburn  4612:         }
                   4613:     }
                   4614:     return $toolchanged;
                   4615: }
                   4616: 
1.88      raeburn  4617: sub build_roles {
1.89      raeburn  4618:     my ($sectionstr,$sections,$role) = @_;
1.88      raeburn  4619:     my $num_sections = 0;
                   4620:     if ($sectionstr=~ /,/) {
                   4621:         my @secnums = split/,/,$sectionstr;
1.89      raeburn  4622:         if ($role eq 'st') {
                   4623:             $secnums[0] =~ s/\W//g;
                   4624:             $$sections{$secnums[0]} = 1;
                   4625:             $num_sections = 1;
                   4626:         } else {
                   4627:             foreach my $sec (@secnums) {
                   4628:                 $sec =~ ~s/\W//g;
1.150     banghart 4629:                 if (!($sec eq "")) {
1.89      raeburn  4630:                     if (exists($$sections{$sec})) {
                   4631:                         $$sections{$sec} ++;
                   4632:                     } else {
                   4633:                         $$sections{$sec} = 1;
                   4634:                         $num_sections ++;
                   4635:                     }
1.88      raeburn  4636:                 }
                   4637:             }
                   4638:         }
                   4639:     } else {
                   4640:         $sectionstr=~s/\W//g;
                   4641:         unless ($sectionstr eq '') {
                   4642:             $$sections{$sectionstr} = 1;
                   4643:             $num_sections ++;
                   4644:         }
                   4645:     }
1.129     albertel 4646: 
1.88      raeburn  4647:     return $num_sections;
                   4648: }
                   4649: 
1.58      www      4650: # ========================================================== Custom Role Editor
                   4651: 
                   4652: sub custom_role_editor {
1.439     raeburn  4653:     my ($r,$context,$brcrum,$prefix,$permission) = @_;
1.324     raeburn  4654:     my $action = $env{'form.customroleaction'};
1.439     raeburn  4655:     my ($rolename,$helpitem);
1.324     raeburn  4656:     if ($action eq 'new') {
                   4657:         $rolename=$env{'form.newrolename'};
                   4658:     } else {
                   4659:         $rolename=$env{'form.rolename'};
1.59      www      4660:     }
                   4661: 
1.324     raeburn  4662:     my ($crstype,$context);
                   4663:     if ($env{'request.course.id'}) {
                   4664:         $crstype = &Apache::loncommon::course_type();
                   4665:         $context = 'course';
1.439     raeburn  4666:         $helpitem = 'Course_Editing_Custom_Roles';
1.324     raeburn  4667:     } else {
                   4668:         $context = 'domain';
1.414     raeburn  4669:         $crstype = 'course';
1.439     raeburn  4670:         $helpitem = 'Domain_Editing_Custom_Roles';
1.324     raeburn  4671:     }
1.351     raeburn  4672: 
                   4673:     $rolename=~s/[^A-Za-z0-9]//gs;
                   4674:     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
1.439     raeburn  4675: 	&print_username_entry_form($r,$context,undef,undef,undef,$crstype,$brcrum,
                   4676:                                    $permission);
1.351     raeburn  4677:         return;
                   4678:     }
                   4679: 
1.414     raeburn  4680:     my $formname = 'form1';
                   4681:     my %privs=();
                   4682:     my $body_top = '<h2>';
                   4683: # ------------------------------------------------------- Does this role exist?
1.59      www      4684:     my ($rdummy,$roledef)=
                   4685: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   4686:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.414     raeburn  4687:         $body_top .= &mt('Existing Role').' "';
1.61      www      4688: # ------------------------------------------------- Get current role privileges
1.414     raeburn  4689:         ($privs{'system'},$privs{'domain'},$privs{'course'})=split(/\_/,$roledef);
                   4690:         if ($privs{'system'} =~ /bre\&S/) {
                   4691:             if ($context eq 'domain') {
1.417     raeburn  4692:                 $crstype = 'Course';
1.414     raeburn  4693:             } elsif ($crstype eq 'Community') {
                   4694:                 $privs{'system'} =~ s/bre\&S//;
                   4695:             }
                   4696:         } elsif ($context eq 'domain') {
                   4697:             $crstype = 'Course';
1.324     raeburn  4698:         }
1.59      www      4699:     } else {
1.414     raeburn  4700:         $body_top .= &mt('New Role').' "';
                   4701:         $roledef='';
1.59      www      4702:     }
1.153     banghart 4703:     $body_top .= $rolename.'"</h2>';
1.414     raeburn  4704: 
                   4705: # ------------------------------------------------------- What can be assigned?
                   4706:     my %full=();
1.417     raeburn  4707:     my %levels=(
1.414     raeburn  4708:                  course => {},
                   4709:                  domain => {},
                   4710:                  system => {},
                   4711:                );
                   4712:     my %levelscurrent=(
                   4713:                         course => {},
                   4714:                         domain => {},
                   4715:                         system => {},
                   4716:                       );
                   4717:     &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
1.160     raeburn  4718:     my ($jsback,$elements) = &crumb_utilities();
1.414     raeburn  4719:     my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
1.417     raeburn  4720:     my $head_script =
1.414     raeburn  4721:         &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,
                   4722:                                                   \%full,\@templateroles,$jsback);
1.351     raeburn  4723:     push (@{$brcrum},
1.414     raeburn  4724:               {href => "javascript:backPage(document.$formname,'pickrole','')",
1.351     raeburn  4725:                text => "Pick custom role",
                   4726:                faq  => 282,bug=>'Instructor Interface',},
1.414     raeburn  4727:               {href => "javascript:backPage(document.$formname,'','')",
1.351     raeburn  4728:                text => "Edit custom role",
                   4729:                faq  => 282,
                   4730:                bug  => 'Instructor Interface',
1.439     raeburn  4731:                help => $helpitem}
1.351     raeburn  4732:               );
                   4733:     my $args = { bread_crumbs          => $brcrum,
                   4734:                  bread_crumbs_component => 'User Management'};
                   4735:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',
                   4736:                                              $head_script,$args).
                   4737:               $body_top);
1.414     raeburn  4738:     $r->print('<form name="'.$formname.'" method="post" action="">'."\n".
                   4739:               &Apache::lonuserutils::custom_role_header($context,$crstype,
                   4740:                                                         \@templateroles,$prefix));
1.264     bisitz   4741: 
1.61      www      4742:     $r->print(<<ENDCCF);
                   4743: <input type="hidden" name="phase" value="set_custom_roles" />
                   4744: <input type="hidden" name="rolename" value="$rolename" />
                   4745: ENDCCF
1.414     raeburn  4746:     $r->print(&Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
                   4747:                                                        \%levelscurrent,$prefix));
1.135     raeburn  4748:     $r->print(&Apache::loncommon::end_data_table().
1.190     raeburn  4749:    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
1.160     raeburn  4750:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
1.417     raeburn  4751:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".
1.160     raeburn  4752:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
1.351     raeburn  4753:    '<input type="submit" value="'.&mt('Save').'" /></form>');
1.61      www      4754: }
1.414     raeburn  4755: 
1.61      www      4756: # ---------------------------------------------------------- Call to definerole
                   4757: sub set_custom_role {
1.439     raeburn  4758:     my ($r,$context,$brcrum,$prefix,$permission) = @_;
1.101     albertel 4759:     my $rolename=$env{'form.rolename'};
1.63      www      4760:     $rolename=~s/[^A-Za-z0-9]//gs;
1.150     banghart 4761:     if (!$rolename) {
1.439     raeburn  4762: 	&custom_role_editor($r,$context,$brcrum,$prefix,$permission);
1.61      www      4763:         return;
                   4764:     }
1.160     raeburn  4765:     my ($jsback,$elements) = &crumb_utilities();
1.301     bisitz   4766:     my $jscript = '<script type="text/javascript">'
                   4767:                  .'// <![CDATA['."\n"
                   4768:                  .$jsback."\n"
                   4769:                  .'// ]]>'."\n"
                   4770:                  .'</script>'."\n";
1.439     raeburn  4771:     my $helpitem = 'Course_Editing_Custom_Roles';
                   4772:     if ($context eq 'domain') {
                   4773:         $helpitem = 'Domain_Editing_Custom_Roles';
                   4774:     }
1.352     raeburn  4775:     push(@{$brcrum},
                   4776:         {href => "javascript:backPage(document.customresult,'pickrole','')",
                   4777:          text => "Pick custom role",
                   4778:          faq  => 282,
                   4779:          bug  => 'Instructor Interface',},
                   4780:         {href => "javascript:backPage(document.customresult,'selected_custom_edit','')",
                   4781:          text => "Edit custom role",
                   4782:          faq  => 282,
                   4783:          bug  => 'Instructor Interface',},
                   4784:         {href => "javascript:backPage(document.customresult,'set_custom_roles','')",
                   4785:          text => "Result",
                   4786:          faq  => 282,
                   4787:          bug  => 'Instructor Interface',
1.439     raeburn  4788:          help => $helpitem,}
1.352     raeburn  4789:         );
                   4790:     my $args = { bread_crumbs           => $brcrum,
1.414     raeburn  4791:                  bread_crumbs_component => 'User Management'};
1.351     raeburn  4792:     $r->print(&Apache::loncommon::start_page('Save Custom Role',$jscript,$args));
1.160     raeburn  4793: 
1.393     raeburn  4794:     my $newrole;
1.61      www      4795:     my ($rdummy,$roledef)=
1.110     albertel 4796: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   4797: 
1.61      www      4798: # ------------------------------------------------------- Does this role exist?
1.188     raeburn  4799:     $r->print('<h3>');
1.61      www      4800:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 4801: 	$r->print(&mt('Existing Role').' "');
1.61      www      4802:     } else {
1.73      sakharuk 4803: 	$r->print(&mt('New Role').' "');
1.61      www      4804: 	$roledef='';
1.393     raeburn  4805:         $newrole = 1;
1.61      www      4806:     }
1.188     raeburn  4807:     $r->print($rolename.'"</h3>');
1.414     raeburn  4808: # ------------------------------------------------- Assign role and show result
1.61      www      4809: 
1.387     bisitz   4810:     my $errmsg;
1.414     raeburn  4811:     my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$prefix);
                   4812:     # Assign role and return result
                   4813:     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
                   4814:                                              $newprivs{'c'});
1.387     bisitz   4815:     if ($result ne 'ok') {
                   4816:         $errmsg = ': '.$result;
                   4817:     }
                   4818:     my $message =
                   4819:         &Apache::lonhtmlcommon::confirm_success(
                   4820:             &mt('Defining Role').$errmsg, ($result eq 'ok' ? 0 : 1));
1.101     albertel 4821:     if ($env{'request.course.id'}) {
                   4822:         my $url='/'.$env{'request.course.id'};
1.63      www      4823:         $url=~s/\_/\//g;
1.387     bisitz   4824:         $result =
                   4825:             &Apache::lonnet::assigncustomrole(
                   4826:                 $env{'user.domain'},$env{'user.name'},
                   4827:                 $url,
                   4828:                 $env{'user.domain'},$env{'user.name'},
                   4829:                 $rolename,undef,undef,undef,$context);
                   4830:         if ($result ne 'ok') {
                   4831:             $errmsg = ': '.$result;
                   4832:         }
                   4833:         $message .=
                   4834:             '<br />'
                   4835:            .&Apache::lonhtmlcommon::confirm_success(
                   4836:                 &mt('Assigning Role to Self').$errmsg, ($result eq 'ok' ? 0 : 1));
1.63      www      4837:     }
1.380     bisitz   4838:     $r->print(
1.387     bisitz   4839:         &Apache::loncommon::confirmwrapper($message)
                   4840:        .'<br />'
                   4841:        .&Apache::lonhtmlcommon::actionbox([
                   4842:             '<a href="javascript:backPage(document.customresult,'."'pickrole'".')">'
                   4843:            .&mt('Create or edit another custom role')
                   4844:            .'</a>'])
1.380     bisitz   4845:        .'<form name="customresult" method="post" action="">'
1.387     bisitz   4846:        .&Apache::lonhtmlcommon::echo_form_input([])
                   4847:        .'</form>'
1.380     bisitz   4848:     );
1.58      www      4849: }
                   4850: 
1.465   ! raeburn  4851: sub show_role_requests {
        !          4852:     my ($caller,$dom) = @_;
        !          4853:     my $showrolereqs;
        !          4854:     my %domconfig = &Apache::lonnet::get_dom('configuration',['privacy'],$dom);
        !          4855:     if (ref($domconfig{'privacy'}) eq 'HASH') {
        !          4856:         if (ref($domconfig{'privacy'}{'approval'}) eq 'HASH') {
        !          4857:             my %approvalconf = %{$domconfig{'privacy'}{'approval'}};
        !          4858:             foreach my $key ('instdom','extdom') {
        !          4859:                 if (ref($approvalconf{$key}) eq 'HASH') {
        !          4860:                     if (keys(%{$approvalconf{$key}})) {
        !          4861:                         foreach my $context ('domain','author','course','community') {
        !          4862:                             if ($approvalconf{$key}{$context} eq $caller) {
        !          4863:                                 $showrolereqs = 1;
        !          4864:                                 last if ($showrolereqs);
        !          4865:                             }
        !          4866:                         }
        !          4867:                     }
        !          4868:                 }
        !          4869:                 last if ($showrolereqs);
        !          4870:             }
        !          4871:         }
        !          4872:     }
        !          4873:     return $showrolereqs;
        !          4874: }
        !          4875: 
1.2       www      4876: # ================================================================ Main Handler
                   4877: sub handler {
                   4878:     my $r = shift;
                   4879:     if ($r->header_only) {
1.68      www      4880:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      4881:        $r->send_http_header;
                   4882:        return OK;
                   4883:     }
1.439     raeburn  4884:     my ($context,$crstype,$cid,$cnum,$cdom,$allhelpitems);
                   4885: 
1.190     raeburn  4886:     if ($env{'request.course.id'}) {
                   4887:         $context = 'course';
1.318     raeburn  4888:         $crstype = &Apache::loncommon::course_type();
1.190     raeburn  4889:     } elsif ($env{'request.role'} =~ /^au\./) {
1.206     raeburn  4890:         $context = 'author';
1.190     raeburn  4891:     } else {
                   4892:         $context = 'domain';
                   4893:     }
1.375     raeburn  4894: 
1.439     raeburn  4895:     my ($permission,$allowed) =
                   4896:         &Apache::lonuserutils::get_permission($context,$crstype);
                   4897: 
                   4898:     if ($allowed) {
                   4899:         my @allhelp;
                   4900:         if ($context eq 'course') {
                   4901:             $cid = $env{'request.course.id'};
                   4902:             $cdom = $env{'course.'.$cid.'.domain'};
                   4903:             $cnum = $env{'course.'.$cid.'.num'};
                   4904: 
                   4905:             if ($permission->{'cusr'}) {
                   4906:                 push(@allhelp,'Course_Create_Class_List');
                   4907:             }
                   4908:             if ($permission->{'view'} || $permission->{'cusr'}) {
                   4909:                 push(@allhelp,('Course_Change_Privileges','Course_View_Class_List'));
                   4910:             }
                   4911:             if ($permission->{'custom'}) {
                   4912:                 push(@allhelp,'Course_Editing_Custom_Roles');
                   4913:             }
                   4914:             if ($permission->{'cusr'}) {
                   4915:                 push(@allhelp,('Course_Add_Student','Course_Drop_Student'));
                   4916:             }
                   4917:             unless ($permission->{'cusr_section'}) {
                   4918:                 if (&Apache::lonnet::auto_run($cnum,$cdom) && (($permission->{'cusr'}) || ($permission->{'view'}))) {
                   4919:                     push(@allhelp,'Course_Automated_Enrollment');
                   4920:                 }
                   4921:                 if ($permission->{'selfenrolladmin'}) {
                   4922:                     push(@allhelp,'Course_Approve_Selfenroll');
                   4923:                 }
                   4924:             }
                   4925:             if ($permission->{'grp_manage'}) {
                   4926:                 push(@allhelp,'Course_Manage_Group');
                   4927:             }
                   4928:             if ($permission->{'view'} || $permission->{'cusr'}) {
                   4929:                 push(@allhelp,'Course_User_Logs');
                   4930:             }
                   4931:         } elsif ($context eq 'author') {
                   4932:             push(@allhelp,('Author_Change_Privileges','Author_Create_Coauthor_List',
                   4933:                            'Author_View_Coauthor_List','Author_User_Logs'));
                   4934:         } else {
                   4935:             if ($permission->{'cusr'}) {
                   4936:                 push(@allhelp,'Domain_Change_Privileges');
                   4937:                 if ($permission->{'activity'}) {
                   4938:                     push(@allhelp,'Domain_User_Access_Logs');
                   4939:                 }
                   4940:                 push(@allhelp,('Domain_Create_Users','Domain_View_Users_List'));
                   4941:                 if ($permission->{'custom'}) {
                   4942:                     push(@allhelp,'Domain_Editing_Custom_Roles');
                   4943:                 }
                   4944:                 push(@allhelp,('Domain_Role_Approvals','Domain_Username_Approvals','Domain_Change_Logs'));
                   4945:             } elsif ($permission->{'view'}) {
                   4946:                 push(@allhelp,'Domain_View_Privileges');
                   4947:                 if ($permission->{'activity'}) {
                   4948:                     push(@allhelp,'Domain_User_Access_Logs');
                   4949:                 }
                   4950:                 push(@allhelp,('Domain_View_Users_List','Domain_Change_Logs'));
                   4951:             }
                   4952:         }
                   4953:         if (@allhelp) {
                   4954:             $allhelpitems = join(',',@allhelp);
                   4955:         }
                   4956:     }
                   4957: 
1.190     raeburn  4958:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.233     raeburn  4959:         ['action','state','callingform','roletype','showrole','bulkaction','popup','phase',
1.391     raeburn  4960:          'username','domain','srchterm','srchdomain','srchin','srchby','srchtype','queue']);
1.190     raeburn  4961:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.351     raeburn  4962:     my $args;
                   4963:     my $brcrum = [];
                   4964:     my $bread_crumbs_component = 'User Management';
1.391     raeburn  4965:     if (($env{'form.action'} ne 'dateselect') && ($env{'form.action'} ne 'displayuserreq')) {
1.351     raeburn  4966:         $brcrum = [{href=>"/adm/createuser",
                   4967:                     text=>"User Management",
1.439     raeburn  4968:                     help=>$allhelpitems}
1.351     raeburn  4969:                   ];
1.202     raeburn  4970:     }
1.190     raeburn  4971:     if (!$allowed) {
1.358     raeburn  4972:         if ($context eq 'course') {
                   4973:             $r->internal_redirect('/adm/viewclasslist');
                   4974:             return OK;
                   4975:         }
1.190     raeburn  4976:         $env{'user.error.msg'}=
                   4977:             "/adm/createuser:cst:0:0:Cannot create/modify user data ".
                   4978:                                  "or view user status.";
                   4979:         return HTTP_NOT_ACCEPTABLE;
                   4980:     }
                   4981: 
                   4982:     &Apache::loncommon::content_type($r,'text/html');
                   4983:     $r->send_http_header;
                   4984: 
1.375     raeburn  4985:     my $showcredits;
                   4986:     if ((($context eq 'course') && ($crstype eq 'Course')) || 
                   4987:          ($context eq 'domain')) {
                   4988:         my %domdefaults = 
                   4989:             &Apache::lonnet::get_domain_defaults($env{'request.role.domain'});
                   4990:         if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'}) {
                   4991:             $showcredits = 1;
                   4992:         }
                   4993:     }
                   4994: 
1.190     raeburn  4995:     # Main switch on form.action and form.state, as appropriate
                   4996:     if (! exists($env{'form.action'})) {
1.351     raeburn  4997:         $args = {bread_crumbs => $brcrum,
                   4998:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4999:         $r->print(&header(undef,$args));
1.318     raeburn  5000:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  5001:     } elsif ($env{'form.action'} eq 'upload' && $permission->{'cusr'}) {
1.439     raeburn  5002:         my $helpitem = 'Course_Create_Class_List';
                   5003:         if ($context eq 'author') {
                   5004:             $helpitem = 'Author_Create_Coauthor_List';
                   5005:         } elsif ($context eq 'domain') {
                   5006:             $helpitem = 'Domain_Create_Users';
                   5007:         }
1.351     raeburn  5008:         push(@{$brcrum},
                   5009:               { href => '/adm/createuser?action=upload&state=',
                   5010:                 text => 'Upload Users List',
1.439     raeburn  5011:                 help => $helpitem,
1.351     raeburn  5012:               });
                   5013:         $bread_crumbs_component = 'Upload Users List';
                   5014:         $args = {bread_crumbs           => $brcrum,
                   5015:                  bread_crumbs_component => $bread_crumbs_component};
                   5016:         $r->print(&header(undef,$args));
1.190     raeburn  5017:         $r->print('<form name="studentform" method="post" '.
                   5018:                   'enctype="multipart/form-data" '.
                   5019:                   ' action="/adm/createuser">'."\n");
                   5020:         if (! exists($env{'form.state'})) {
                   5021:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   5022:         } elsif ($env{'form.state'} eq 'got_file') {
1.448     raeburn  5023:             my $result = 
                   5024:                 &Apache::lonuserutils::print_upload_manager_form($r,$context,
                   5025:                                                                  $permission,
                   5026:                                                                  $crstype,$showcredits);
                   5027:             if ($result eq 'missingdata') {
                   5028:                 delete($env{'form.state'});
                   5029:                 &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   5030:             }
1.190     raeburn  5031:         } elsif ($env{'form.state'} eq 'enrolling') {
                   5032:             if ($env{'form.datatoken'}) {
1.448     raeburn  5033:                 my $result = &Apache::lonuserutils::upfile_drop_add($r,$context,
                   5034:                                                                     $permission,
                   5035:                                                                     $showcredits);
                   5036:                 if ($result eq 'missingdata') {
                   5037:                     delete($env{'form.state'});
                   5038:                     &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   5039:                 } elsif ($result eq 'invalidhome') {
                   5040:                     $env{'form.state'} = 'got_file';
                   5041:                     delete($env{'form.lcserver'});
                   5042:                     my $result =
                   5043:                         &Apache::lonuserutils::print_upload_manager_form($r,$context,$permission,
                   5044:                                                                          $crstype,$showcredits);
                   5045:                     if ($result eq 'missingdata') {
                   5046:                         delete($env{'form.state'});
                   5047:                         &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   5048:                     }
                   5049:                 }
                   5050:             } else {
                   5051:                 delete($env{'form.state'});
                   5052:                 &Apache::lonuserutils::print_first_users_upload_form($r,$context);
1.190     raeburn  5053:             }
                   5054:         } else {
                   5055:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   5056:         }
1.447     raeburn  5057:         $r->print('</form>');
1.416     raeburn  5058:     } elsif (((($env{'form.action'} eq 'singleuser') || ($env{'form.action'}
                   5059:               eq 'singlestudent')) && ($permission->{'cusr'})) ||
1.418     raeburn  5060:              (($env{'form.action'} eq 'singleuser') && ($permission->{'view'})) ||
1.416     raeburn  5061:              (($env{'form.action'} eq 'accesslogs') && ($permission->{'activity'}))) {
1.190     raeburn  5062:         my $phase = $env{'form.phase'};
                   5063:         my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
1.192     albertel 5064: 	&Apache::loncreateuser::restore_prev_selections();
                   5065: 	my $srch;
                   5066: 	foreach my $item (@search) {
                   5067: 	    $srch->{$item} = $env{'form.'.$item};
                   5068: 	}
1.207     raeburn  5069:         if (($phase eq 'get_user_info') || ($phase eq 'userpicked') ||
1.416     raeburn  5070:             ($phase eq 'createnewuser') || ($phase eq 'activity')) {
1.207     raeburn  5071:             if ($env{'form.phase'} eq 'createnewuser') {
                   5072:                 my $response;
                   5073:                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
1.366     bisitz   5074:                     my $response =
                   5075:                         '<span class="LC_warning">'
                   5076:                        .&mt('You must specify a valid username. Only the following are allowed:'
                   5077:                            .' letters numbers - . @')
                   5078:                        .'</span>';
1.221     raeburn  5079:                     $env{'form.phase'} = '';
1.375     raeburn  5080:                     &print_username_entry_form($r,$context,$response,$srch,undef,
1.439     raeburn  5081:                                                $crstype,$brcrum,$permission);
1.207     raeburn  5082:                 } else {
                   5083:                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
                   5084:                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
                   5085:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  5086:                                                   $srch,$response,$context,
1.375     raeburn  5087:                                                   $permission,$crstype,$brcrum,
                   5088:                                                   $showcredits);
1.207     raeburn  5089:                 }
                   5090:             } elsif ($env{'form.phase'} eq 'get_user_info') {
1.190     raeburn  5091:                 my ($currstate,$response,$forcenewuser,$results) = 
1.221     raeburn  5092:                     &user_search_result($context,$srch);
1.190     raeburn  5093:                 if ($env{'form.currstate'} eq 'modify') {
                   5094:                     $currstate = $env{'form.currstate'};
                   5095:                 }
                   5096:                 if ($currstate eq 'select') {
                   5097:                     &print_user_selection_page($r,$response,$srch,$results,
1.351     raeburn  5098:                                                \@search,$context,undef,$crstype,
                   5099:                                                $brcrum);
1.416     raeburn  5100:                 } elsif (($currstate eq 'modify') || ($env{'form.action'} eq 'accesslogs')) {
                   5101:                     my ($ccuname,$ccdomain,$uhome);
1.190     raeburn  5102:                     if (($srch->{'srchby'} eq 'uname') && 
                   5103:                         ($srch->{'srchtype'} eq 'exact')) {
                   5104:                         $ccuname = $srch->{'srchterm'};
                   5105:                         $ccdomain= $srch->{'srchdomain'};
                   5106:                     } else {
                   5107:                         my @matchedunames = keys(%{$results});
                   5108:                         ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
                   5109:                     }
                   5110:                     $ccuname =&LONCAPA::clean_username($ccuname);
                   5111:                     $ccdomain=&LONCAPA::clean_domain($ccdomain);
1.416     raeburn  5112:                     if ($env{'form.action'} eq 'accesslogs') {
                   5113:                         my $uhome;
                   5114:                         if (($ccuname ne '') && ($ccdomain ne '')) {
                   5115:                            $uhome = &Apache::lonnet::homeserver($ccuname,$ccdomain);
                   5116:                         }
                   5117:                         if (($uhome eq '') || ($uhome eq 'no_host')) {
                   5118:                             $env{'form.phase'} = '';
                   5119:                             undef($forcenewuser);
                   5120:                             #if ($response) {
                   5121:                             #    unless ($response =~ m{\Q<br /><br />\E$}) {
                   5122:                             #        $response .= '<br /><br />';
                   5123:                             #    }
                   5124:                             #}
                   5125:                             &print_username_entry_form($r,$context,$response,$srch,
1.439     raeburn  5126:                                                        $forcenewuser,$crstype,$brcrum,
                   5127:                                                        $permission);
1.416     raeburn  5128:                         } else {
                   5129:                             &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
                   5130:                         }
                   5131:                     } else {
                   5132:                         if ($env{'form.forcenewuser'}) {
                   5133:                             $response = '';
                   5134:                         }
                   5135:                         &print_user_modification_page($r,$ccuname,$ccdomain,
                   5136:                                                       $srch,$response,$context,
                   5137:                                                       $permission,$crstype,$brcrum);
1.190     raeburn  5138:                     }
                   5139:                 } elsif ($currstate eq 'query') {
1.351     raeburn  5140:                     &print_user_query_page($r,'createuser',$brcrum);
1.190     raeburn  5141:                 } else {
1.229     raeburn  5142:                     $env{'form.phase'} = '';
1.207     raeburn  5143:                     &print_username_entry_form($r,$context,$response,$srch,
1.439     raeburn  5144:                                                $forcenewuser,$crstype,$brcrum,
                   5145:                                                $permission);
1.190     raeburn  5146:                 }
                   5147:             } elsif ($env{'form.phase'} eq 'userpicked') {
                   5148:                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
                   5149:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
1.416     raeburn  5150:                 if ($env{'form.action'} eq 'accesslogs') {
                   5151:                     &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
                   5152:                 } else {
                   5153:                     &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
                   5154:                                                   $context,$permission,$crstype,
                   5155:                                                   $brcrum);
                   5156:                 }
                   5157:             } elsif ($env{'form.action'} eq 'accesslogs') {
                   5158:                 my $ccuname = &LONCAPA::clean_username($env{'form.accessuname'});
                   5159:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.accessudom'});
                   5160:                 &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
1.190     raeburn  5161:             }
                   5162:         } elsif ($env{'form.phase'} eq 'update_user_data') {
1.451     raeburn  5163:             &update_user_data($r,$context,$crstype,$brcrum,$showcredits,$permission);
1.190     raeburn  5164:         } else {
1.351     raeburn  5165:             &print_username_entry_form($r,$context,undef,$srch,undef,$crstype,
1.439     raeburn  5166:                                        $brcrum,$permission);
1.190     raeburn  5167:         }
                   5168:     } elsif ($env{'form.action'} eq 'custom' && $permission->{'custom'}) {
1.414     raeburn  5169:         my $prefix;
1.190     raeburn  5170:         if ($env{'form.phase'} eq 'set_custom_roles') {
1.439     raeburn  5171:             &set_custom_role($r,$context,$brcrum,$prefix,$permission);
1.190     raeburn  5172:         } else {
1.439     raeburn  5173:             &custom_role_editor($r,$context,$brcrum,$prefix,$permission);
1.190     raeburn  5174:         }
1.362     raeburn  5175:     } elsif (($env{'form.action'} eq 'processauthorreq') &&
                   5176:              ($permission->{'cusr'}) && 
                   5177:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   5178:         push(@{$brcrum},
                   5179:                  {href => '/adm/createuser?action=processauthorreq',
1.385     bisitz   5180:                   text => 'Authoring Space requests',
1.362     raeburn  5181:                   help => 'Domain_Role_Approvals'});
                   5182:         $bread_crumbs_component = 'Authoring requests';
                   5183:         if ($env{'form.state'} eq 'done') {
                   5184:             push(@{$brcrum},
                   5185:                      {href => '/adm/createuser?action=authorreqqueue',
                   5186:                       text => 'Result',
                   5187:                       help => 'Domain_Role_Approvals'});
                   5188:             $bread_crumbs_component = 'Authoring request result';
                   5189:         }
                   5190:         $args = { bread_crumbs           => $brcrum,
                   5191:                   bread_crumbs_component => $bread_crumbs_component};
1.391     raeburn  5192:         my $js = &usernamerequest_javascript();
                   5193:         $r->print(&header(&add_script($js),$args));
1.362     raeburn  5194:         if (!exists($env{'form.state'})) {
                   5195:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestauthor',
                   5196:                                                                             $env{'request.role.domain'}));
                   5197:         } elsif ($env{'form.state'} eq 'done') {
                   5198:             $r->print('<h3>'.&mt('Authoring request processing').'</h3>'."\n");
                   5199:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestauthor',
                   5200:                                                                          $env{'request.role.domain'}));
                   5201:         }
1.391     raeburn  5202:     } elsif (($env{'form.action'} eq 'processusernamereq') &&
                   5203:              ($permission->{'cusr'}) &&
                   5204:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   5205:         push(@{$brcrum},
                   5206:                  {href => '/adm/createuser?action=processusernamereq',
                   5207:                   text => 'LON-CAPA account requests',
                   5208:                   help => 'Domain_Username_Approvals'});
                   5209:         $bread_crumbs_component = 'Account requests';
                   5210:         if ($env{'form.state'} eq 'done') {
                   5211:             push(@{$brcrum},
                   5212:                      {href => '/adm/createuser?action=usernamereqqueue',
                   5213:                       text => 'Result',
                   5214:                       help => 'Domain_Username_Approvals'});
                   5215:             $bread_crumbs_component = 'LON-CAPA account request result';
                   5216:         }
                   5217:         $args = { bread_crumbs           => $brcrum,
                   5218:                   bread_crumbs_component => $bread_crumbs_component};
                   5219:         my $js = &usernamerequest_javascript();
                   5220:         $r->print(&header(&add_script($js),$args));
                   5221:         if (!exists($env{'form.state'})) {
                   5222:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestusername',
                   5223:                                                                             $env{'request.role.domain'}));
                   5224:         } elsif ($env{'form.state'} eq 'done') {
                   5225:             $r->print('<h3>'.&mt('LON-CAPA account request processing').'</h3>'."\n");
                   5226:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestusername',
                   5227:                                                                          $env{'request.role.domain'}));
                   5228:         }
                   5229:     } elsif (($env{'form.action'} eq 'displayuserreq') &&
                   5230:              ($permission->{'cusr'})) {
                   5231:         my $dom = $env{'form.domain'};
                   5232:         my $uname = $env{'form.username'};
                   5233:         my $warning;
                   5234:         if (($dom =~ /^$match_domain$/) && (&Apache::lonnet::domain($dom) ne '')) {
                   5235:             if (($dom eq $env{'request.role.domain'}) && (&Apache::lonnet::allowed('ccc',$dom))) {
                   5236:                 if (($uname =~ /^$match_username$/) && ($env{'form.queue'} eq 'approval')) {
                   5237:                     my $uhome = &Apache::lonnet::homeserver($uname,$dom);
                   5238:                     if ($uhome eq 'no_host') {
                   5239:                         my $queue = $env{'form.queue'};
                   5240:                         my $reqkey = &escape($uname).'_'.$queue; 
                   5241:                         my $namespace = 'usernamequeue';
                   5242:                         my $domconfig = &Apache::lonnet::get_domainconfiguser($dom);
                   5243:                         my %queued =
                   5244:                             &Apache::lonnet::get($namespace,[$reqkey],$dom,$domconfig);
                   5245:                         unless ($queued{$reqkey}) {
                   5246:                             $warning = &mt('No information was found for this LON-CAPA account request.');
                   5247:                         }
                   5248:                     } else {
                   5249:                         $warning = &mt('A LON-CAPA account already exists for the requested username and domain.');
                   5250:                     }
                   5251:                 } else {
                   5252:                     $warning = &mt('LON-CAPA account request status check is for an invalid username.');
                   5253:                 }
                   5254:             } else {
                   5255:                 $warning = &mt('You do not have rights to view LON-CAPA account requests in the domain specified.');
                   5256:             }
                   5257:         } else {
                   5258:             $warning = &mt('LON-CAPA account request status check is for an invalid domain.');
                   5259:         }
                   5260:         my $args = { only_body => 1 };
                   5261:         $r->print(&header(undef,$args).
                   5262:                   '<h3>'.&mt('LON-CAPA Account Request Details').'</h3>');
                   5263:         if ($warning ne '') {
                   5264:             $r->print('<div class="LC_warning">'.$warning.'</div>');
                   5265:         } else {
                   5266:             my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
                   5267:             my $domconfiguser = &Apache::lonnet::get_domainconfiguser($dom);
                   5268:             my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   5269:             if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   5270:                 if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
                   5271:                     if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}) eq 'HASH') {
                   5272:                         my %info =
                   5273:                             &Apache::lonnet::get('nohist_requestedusernames',[$uname],$dom,$domconfiguser);
                   5274:                         if (ref($info{$uname}) eq 'HASH') {
1.396     raeburn  5275:                             my $usertype = $info{$uname}{'inststatus'};
                   5276:                             unless ($usertype) {
                   5277:                                 $usertype = 'default';
                   5278:                             }
1.442     raeburn  5279:                             my ($showstatus,$showemail,$pickstart);
                   5280:                             my $numextras = 0;
                   5281:                             my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
1.443     raeburn  5282:                             if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
                   5283:                                 if (ref($usertypes) eq 'HASH') {
                   5284:                                     if ($usertypes->{$usertype}) {
                   5285:                                         $showstatus = $usertypes->{$usertype};
                   5286:                                     } else {
                   5287:                                         $showstatus = $othertitle;
                   5288:                                     }
                   5289:                                     if ($showstatus) {
                   5290:                                         $numextras ++;
                   5291:                                     }
1.442     raeburn  5292:                                 }
                   5293:                             }
                   5294:                             if (($info{$uname}{'email'} ne '') && ($info{$uname}{'email'} ne $uname)) {
                   5295:                                 $showemail = $info{$uname}{'email'};
                   5296:                                 $numextras ++;
                   5297:                             }
1.396     raeburn  5298:                             if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}) eq 'HASH') {
                   5299:                                 if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
1.442     raeburn  5300:                                     $pickstart = 1;
1.396     raeburn  5301:                                     $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box());
1.442     raeburn  5302:                                     my ($num,$count);
1.396     raeburn  5303:                                     $count = scalar(keys(%{$domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}}));
1.442     raeburn  5304:                                     $count += $numextras;
1.396     raeburn  5305:                                     foreach my $field (@{$infofields}) {
                   5306:                                         next unless ($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}{$field});
                   5307:                                         next unless ($infotitles->{$field});
                   5308:                                         $r->print(&Apache::lonhtmlcommon::row_title($infotitles->{$field}).
                   5309:                                                   $info{$uname}{$field});
                   5310:                                         $num ++;
1.442     raeburn  5311:                                         unless ($count == $num) {
1.396     raeburn  5312:                                             $r->print(&Apache::lonhtmlcommon::row_closure());
                   5313:                                         }
                   5314:                                     }
1.442     raeburn  5315:                                 }
                   5316:                             }
                   5317:                             if ($numextras) {
                   5318:                                 unless ($pickstart) {
                   5319:                                     $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box());
                   5320:                                     $pickstart = 1;
                   5321:                                 }
                   5322:                                 if ($showemail) {
                   5323:                                     my $closure = '';
                   5324:                                     unless ($showstatus) {
                   5325:                                         $closure = 1;
1.391     raeburn  5326:                                     }
1.442     raeburn  5327:                                     $r->print(&Apache::lonhtmlcommon::row_title(&mt('E-mail address')).
                   5328:                                               $showemail.
                   5329:                                               &Apache::lonhtmlcommon::row_closure($closure));
1.391     raeburn  5330:                                 }
1.442     raeburn  5331:                                 if ($showstatus) {
                   5332:                                     $r->print(&Apache::lonhtmlcommon::row_title(&mt('Status type[_1](self-reported)','<br />')).
                   5333:                                               $showstatus.
                   5334:                                               &Apache::lonhtmlcommon::row_closure(1));
                   5335:                                 }
                   5336:                             }
                   5337:                             if ($pickstart) { 
                   5338:                                 $r->print(&Apache::lonhtmlcommon::end_pick_box().'</div>');
                   5339:                             } else {
                   5340:                                 $r->print('<div>'.&mt('No information to display for this account request.').'</div>');
1.391     raeburn  5341:                             }
1.442     raeburn  5342:                         } else {
                   5343:                             $r->print('<div>'.&mt('No information available for this account request.').'</div>');
1.391     raeburn  5344:                         }
                   5345:                     }
                   5346:                 }
                   5347:             }
                   5348:         }
1.442     raeburn  5349:         $r->print(&close_popup_form());
1.207     raeburn  5350:     } elsif (($env{'form.action'} eq 'listusers') && 
                   5351:              ($permission->{'view'} || $permission->{'cusr'})) {
1.439     raeburn  5352:         my $helpitem = 'Course_View_Class_List';
                   5353:         if ($context eq 'author') {
                   5354:             $helpitem = 'Author_View_Coauthor_List';
                   5355:         } elsif ($context eq 'domain') {
                   5356:             $helpitem = 'Domain_View_Users_List';
                   5357:         }
1.202     raeburn  5358:         if ($env{'form.phase'} eq 'bulkchange') {
1.351     raeburn  5359:             push(@{$brcrum},
                   5360:                     {href => '/adm/createuser?action=listusers',
                   5361:                      text => "List Users"},
                   5362:                     {href => "/adm/createuser",
                   5363:                      text => "Result",
1.439     raeburn  5364:                      help => $helpitem});
1.351     raeburn  5365:             $bread_crumbs_component = 'Update Users';
                   5366:             $args = {bread_crumbs           => $brcrum,
                   5367:                      bread_crumbs_component => $bread_crumbs_component};
                   5368:             $r->print(&header(undef,$args));
1.202     raeburn  5369:             my $setting = $env{'form.roletype'};
                   5370:             my $choice = $env{'form.bulkaction'};
                   5371:             if ($permission->{'cusr'}) {
1.336     raeburn  5372:                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice,$crstype);
1.221     raeburn  5373:             } else {
                   5374:                 $r->print(&mt('You are not authorized to make bulk changes to user roles'));
1.223     raeburn  5375:                 $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
1.202     raeburn  5376:             }
                   5377:         } else {
1.351     raeburn  5378:             push(@{$brcrum},
                   5379:                     {href => '/adm/createuser?action=listusers',
                   5380:                      text => "List Users",
1.439     raeburn  5381:                      help => $helpitem});
1.351     raeburn  5382:             $bread_crumbs_component = 'List Users';
                   5383:             $args = {bread_crumbs           => $brcrum,
                   5384:                      bread_crumbs_component => $bread_crumbs_component};
1.202     raeburn  5385:             my ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles);
                   5386:             my $formname = 'studentform';
1.364     raeburn  5387:             my $hidecall = "hide_searching();";
1.321     raeburn  5388:             if (($context eq 'domain') && (($env{'form.roletype'} eq 'course') ||
                   5389:                 ($env{'form.roletype'} eq 'community'))) {
                   5390:                 if ($env{'form.roletype'} eq 'course') {
                   5391:                     ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles) = 
                   5392:                         &Apache::lonuserutils::courses_selector($env{'request.role.domain'},
                   5393:                                                                 $formname);
                   5394:                 } elsif ($env{'form.roletype'} eq 'community') {
                   5395:                     $cb_jscript = 
                   5396:                         &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
                   5397:                     my %elements = (
                   5398:                                       coursepick => 'radio',
                   5399:                                       coursetotal => 'text',
                   5400:                                       courselist => 'text',
                   5401:                                    );
                   5402:                     $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   5403:                 }
1.364     raeburn  5404:                 $jscript .= &verify_user_display($context)."\n".
                   5405:                             &Apache::loncommon::check_uncheck_jscript();
1.202     raeburn  5406:                 my $js = &add_script($jscript).$cb_jscript;
                   5407:                 my $loadcode = 
                   5408:                     &Apache::lonuserutils::course_selector_loadcode($formname);
                   5409:                 if ($loadcode ne '') {
1.364     raeburn  5410:                     $args->{add_entries} = {onload => "$loadcode;$hidecall"};
                   5411:                 } else {
                   5412:                     $args->{add_entries} = {onload => $hidecall};
1.202     raeburn  5413:                 }
1.351     raeburn  5414:                 $r->print(&header($js,$args));
1.191     raeburn  5415:             } else {
1.364     raeburn  5416:                 $args->{add_entries} = {onload => $hidecall};
                   5417:                 $jscript = &verify_user_display($context).
                   5418:                            &Apache::loncommon::check_uncheck_jscript(); 
                   5419:                 $r->print(&header(&add_script($jscript),$args));
1.191     raeburn  5420:             }
1.202     raeburn  5421:             &Apache::lonuserutils::print_userlist($r,undef,$permission,$context,
1.375     raeburn  5422:                          $formname,$totcodes,$codetitles,$idlist,$idlist_titles,
                   5423:                          $showcredits);
1.191     raeburn  5424:         }
1.213     raeburn  5425:     } elsif ($env{'form.action'} eq 'drop' && $permission->{'cusr'}) {
1.318     raeburn  5426:         my $brtext;
                   5427:         if ($crstype eq 'Community') {
                   5428:             $brtext = 'Drop Members';
                   5429:         } else {
                   5430:             $brtext = 'Drop Students';
                   5431:         }
1.351     raeburn  5432:         push(@{$brcrum},
                   5433:                 {href => '/adm/createuser?action=drop',
                   5434:                  text => $brtext,
                   5435:                  help => 'Course_Drop_Student'});
                   5436:         if ($env{'form.state'} eq 'done') {
                   5437:             push(@{$brcrum},
                   5438:                      {href=>'/adm/createuser?action=drop',
                   5439:                       text=>"Result"});
                   5440:         }
                   5441:         $bread_crumbs_component = $brtext;
                   5442:         $args = {bread_crumbs           => $brcrum,
                   5443:                  bread_crumbs_component => $bread_crumbs_component}; 
                   5444:         $r->print(&header(undef,$args));
1.213     raeburn  5445:         if (!exists($env{'form.state'})) {
1.318     raeburn  5446:             &Apache::lonuserutils::print_drop_menu($r,$context,$permission,$crstype);
1.213     raeburn  5447:         } elsif ($env{'form.state'} eq 'done') {
                   5448:             &Apache::lonuserutils::update_user_list($r,$context,undef,
                   5449:                                                     $env{'form.action'});
                   5450:         }
1.202     raeburn  5451:     } elsif ($env{'form.action'} eq 'dateselect') {
                   5452:         if ($permission->{'cusr'}) {
1.351     raeburn  5453:             $r->print(&header(undef,{'no_nav_bar' => 1}).
1.375     raeburn  5454:                       &Apache::lonuserutils::date_section_selector($context,$permission,
                   5455:                                                                    $crstype,$showcredits));
1.202     raeburn  5456:         } else {
1.351     raeburn  5457:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5458:                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>'); 
1.202     raeburn  5459:         }
1.237     raeburn  5460:     } elsif ($env{'form.action'} eq 'selfenroll') {
1.398     raeburn  5461:         if ($permission->{selfenrolladmin}) {
                   5462:             my %currsettings = (
                   5463:                 selfenroll_types              => $env{'course.'.$cid.'.internal.selfenroll_types'},
                   5464:                 selfenroll_registered         => $env{'course.'.$cid.'.internal.selfenroll_registered'},
                   5465:                 selfenroll_section            => $env{'course.'.$cid.'.internal.selfenroll_section'},
                   5466:                 selfenroll_notifylist         => $env{'course.'.$cid.'.internal.selfenroll_notifylist'},
                   5467:                 selfenroll_approval           => $env{'course.'.$cid.'.internal.selfenroll_approval'},
                   5468:                 selfenroll_limit              => $env{'course.'.$cid.'.internal.selfenroll_limit'},
                   5469:                 selfenroll_cap                => $env{'course.'.$cid.'.internal.selfenroll_cap'},
                   5470:                 selfenroll_start_date         => $env{'course.'.$cid.'.internal.selfenroll_start_date'},
                   5471:                 selfenroll_end_date           => $env{'course.'.$cid.'.internal.selfenroll_end_date'},
                   5472:                 selfenroll_start_access       => $env{'course.'.$cid.'.internal.selfenroll_start_access'},
                   5473:                 selfenroll_end_access         => $env{'course.'.$cid.'.internal.selfenroll_end_access'},
                   5474:                 default_enrollment_start_date => $env{'course.'.$cid.'.default_enrollment_start_date'},
                   5475:                 default_enrollment_end_date   => $env{'course.'.$cid.'.default_enrollment_end_date'},
1.400     raeburn  5476:                 uniquecode                    => $env{'course.'.$cid.'.internal.uniquecode'},
1.398     raeburn  5477:             );
                   5478:             push(@{$brcrum},
                   5479:                     {href => '/adm/createuser?action=selfenroll',
                   5480:                      text => "Configure Self-enrollment",
                   5481:                      help => 'Course_Self_Enrollment'});
                   5482:             if (!exists($env{'form.state'})) {
                   5483:                 $args = { bread_crumbs           => $brcrum,
                   5484:                           bread_crumbs_component => 'Configure Self-enrollment'};
                   5485:                 $r->print(&header(undef,$args));
                   5486:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
                   5487:                 &print_selfenroll_menu($r,'course',$cid,$cdom,$cnum,\%currsettings);
                   5488:             } elsif ($env{'form.state'} eq 'done') {
                   5489:                 push (@{$brcrum},
                   5490:                           {href=>'/adm/createuser?action=selfenroll',
                   5491:                            text=>"Result"});
                   5492:                 $args = { bread_crumbs           => $brcrum,
                   5493:                           bread_crumbs_component => 'Self-enrollment result'};
                   5494:                 $r->print(&header(undef,$args));
                   5495:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
1.400     raeburn  5496:                 &update_selfenroll_config($r,$cid,$cdom,$cnum,$context,$crstype,\%currsettings);
1.398     raeburn  5497:             }
                   5498:         } else {
                   5499:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5500:                      '<span class="LC_error">'.&mt('You do not have permission to configure self-enrollment').'</span>');
1.237     raeburn  5501:         }
1.277     raeburn  5502:     } elsif ($env{'form.action'} eq 'selfenrollqueue') {
1.418     raeburn  5503:         if ($permission->{selfenrolladmin}) {
1.351     raeburn  5504:             push(@{$brcrum},
                   5505:                      {href => '/adm/createuser?action=selfenrollqueue',
1.418     raeburn  5506:                       text => 'Enrollment requests',
1.439     raeburn  5507:                       help => 'Course_Approve_Selfenroll'});
1.418     raeburn  5508:             $bread_crumbs_component = 'Enrollment requests';
                   5509:             if ($env{'form.state'} eq 'done') {
                   5510:                 push(@{$brcrum},
                   5511:                          {href => '/adm/createuser?action=selfenrollqueue',
                   5512:                           text => 'Result',
1.439     raeburn  5513:                           help => 'Course_Approve_Selfenroll'});
1.418     raeburn  5514:                 $bread_crumbs_component = 'Enrollment result';
                   5515:             }
                   5516:             $args = { bread_crumbs           => $brcrum,
                   5517:                       bread_crumbs_component => $bread_crumbs_component};
                   5518:             $r->print(&header(undef,$args));
                   5519:             my $coursedesc = $env{'course.'.$cid.'.description'};
                   5520:             if (!exists($env{'form.state'})) {
                   5521:                 $r->print('<h3>'.&mt('Pending enrollment requests').'</h3>'."\n");
                   5522:                 $r->print(&Apache::loncoursequeueadmin::display_queued_requests($context,
                   5523:                                                                                 $cdom,$cnum));
                   5524:             } elsif ($env{'form.state'} eq 'done') {
                   5525:                 $r->print('<h3>'.&mt('Enrollment request processing').'</h3>'."\n");
                   5526:                 $r->print(&Apache::loncoursequeueadmin::update_request_queue($context,
1.430     raeburn  5527:                               $cdom,$cnum,$coursedesc));
1.418     raeburn  5528:             }
                   5529:         } else {
                   5530:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5531:                      '<span class="LC_error">'.&mt('You do not have permission to manage self-enrollment').'</span>');
1.351     raeburn  5532:         }
1.418     raeburn  5533:     } elsif ($env{'form.action'} eq 'changelogs') {
                   5534:         if ($permission->{cusr} || $permission->{view}) {
                   5535:             &print_userchangelogs_display($r,$context,$permission,$brcrum);
                   5536:         } else {
                   5537:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5538:                      '<span class="LC_error">'.&mt('You do not have permission to view change logs').'</span>');
1.277     raeburn  5539:         }
1.428     raeburn  5540:     } elsif ($env{'form.action'} eq 'helpdesk') {
1.464     raeburn  5541:         if (($permission->{'owner'} || $permission->{'co-owner'}) &&
                   5542:             ($permission->{'cusr'} || $permission->{'view'})) {
1.428     raeburn  5543:             if ($env{'form.state'} eq 'process') {
                   5544:                 if ($permission->{'owner'}) {
                   5545:                     &update_helpdeskaccess($r,$permission,$brcrum);
                   5546:                 } else {
                   5547:                     &print_helpdeskaccess_display($r,$permission,$brcrum);
1.430     raeburn  5548:                 }
1.428     raeburn  5549:             } else {
                   5550:                 &print_helpdeskaccess_display($r,$permission,$brcrum);
                   5551:             }
                   5552:         } else {
                   5553:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5554:                       '<span class="LC_error">'.&mt('You do not have permission to view helpdesk access').'</span>');
                   5555:         }
1.465   ! raeburn  5556:     } elsif ($env{'form.action'} eq 'rolerequests') {
        !          5557:         if ($permission->{cusr} || $permission->{view}) {
        !          5558:             &print_queued_roles($r,$context,$permission,$brcrum);
        !          5559:         }
        !          5560:     } elsif ($env{'form.action'} eq 'queuedroles') {
        !          5561:         if (($permission->{cusr}) && ($context eq 'domain')) {
        !          5562:             if (&show_role_requests($context,$env{'request.role.domain'})) {
        !          5563:                 if ($env{'form.state'} eq 'done') {
        !          5564:                     &process_pendingroles($r,$context,$permission,$brcrum);
        !          5565:                 } else {
        !          5566:                     &print_pendingroles($r,$context,$permission,$brcrum);
        !          5567:                 }
        !          5568:             } else {
        !          5569:                 $r->print(&header(undef,{'no_nav_bar' => 1}).
        !          5570:                           '<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>');
        !          5571:             }
        !          5572:         } else {
        !          5573:             $r->print(&header(undef,{'no_nav_bar' => 1}).
        !          5574:                      '<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>');
        !          5575:         }
1.190     raeburn  5576:     } else {
1.351     raeburn  5577:         $bread_crumbs_component = 'User Management';
                   5578:         $args = { bread_crumbs           => $brcrum,
                   5579:                   bread_crumbs_component => $bread_crumbs_component};
                   5580:         $r->print(&header(undef,$args));
1.318     raeburn  5581:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  5582:     }
1.351     raeburn  5583:     $r->print(&Apache::loncommon::end_page());
1.190     raeburn  5584:     return OK;
                   5585: }
                   5586: 
                   5587: sub header {
1.351     raeburn  5588:     my ($jscript,$args) = @_;
1.190     raeburn  5589:     my $start_page;
1.351     raeburn  5590:     if (ref($args) eq 'HASH') {
                   5591:         $start_page=&Apache::loncommon::start_page('User Management',$jscript,$args);
1.190     raeburn  5592:     } else {
1.351     raeburn  5593:         $start_page=&Apache::loncommon::start_page('User Management',$jscript);
1.190     raeburn  5594:     }
                   5595:     return $start_page;
                   5596: }
1.2       www      5597: 
1.191     raeburn  5598: sub add_script {
                   5599:     my ($js) = @_;
1.301     bisitz   5600:     return '<script type="text/javascript">'."\n"
                   5601:           .'// <![CDATA['."\n"
                   5602:           .$js."\n"
                   5603:           .'// ]]>'."\n"
                   5604:           .'</script>'."\n";
1.191     raeburn  5605: }
                   5606: 
1.391     raeburn  5607: sub usernamerequest_javascript {
                   5608:     my $js = <<ENDJS;
                   5609: 
                   5610: function openusernamereqdisplay(dom,uname,queue) {
                   5611:     var url = '/adm/createuser?action=displayuserreq';
                   5612:     url += '&domain='+dom+'&username='+uname+'&queue='+queue;
                   5613:     var title = 'Account_Request_Browser';
                   5614:     var options = 'scrollbars=1,resizable=1,menubar=0';
                   5615:     options += ',width=700,height=600';
                   5616:     var stdeditbrowser = open(url,title,options,'1');
                   5617:     stdeditbrowser.focus();
                   5618:     return;
                   5619: }
                   5620:  
                   5621: ENDJS
                   5622: }
                   5623: 
                   5624: sub close_popup_form {
                   5625:     my $close= &mt('Close Window');
                   5626:     return << "END";
                   5627: <p><form name="displayreq" action="" method="post">
                   5628: <input type="button" name="closeme" value="$close" onclick="javascript:self.close();" />
                   5629: </form></p>
                   5630: END
                   5631: }
                   5632: 
1.202     raeburn  5633: sub verify_user_display {
1.364     raeburn  5634:     my ($context) = @_;
1.374     raeburn  5635:     my %lt = &Apache::lonlocal::texthash (
                   5636:         course    => 'course(s): description, section(s), status',
                   5637:         community => 'community(s): description, section(s), status',
                   5638:         author    => 'author',
                   5639:     );
1.364     raeburn  5640:     my $photos;
                   5641:     if (($context eq 'course') && $env{'request.course.id'}) {
                   5642:         $photos = $env{'course.'.$env{'request.course.id'}.'.internal.showphoto'};
                   5643:     }
1.202     raeburn  5644:     my $output = <<"END";
                   5645: 
1.364     raeburn  5646: function hide_searching() {
                   5647:     if (document.getElementById('searching')) {
                   5648:         document.getElementById('searching').style.display = 'none';
                   5649:     }
                   5650:     return;
                   5651: }
                   5652: 
1.202     raeburn  5653: function display_update() {
                   5654:     document.studentform.action.value = 'listusers';
                   5655:     document.studentform.phase.value = 'display';
                   5656:     document.studentform.submit();
                   5657: }
                   5658: 
1.364     raeburn  5659: function updateCols(caller) {
                   5660:     var context = '$context';
                   5661:     var photos = '$photos';
                   5662:     if (caller == 'Status') {
1.374     raeburn  5663:         if ((context == 'domain') && 
                   5664:             ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   5665:              (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community'))) {
1.364     raeburn  5666:             document.getElementById('showcolstatus').checked = false;
                   5667:             document.getElementById('showcolstatus').disabled = 'disabled';
                   5668:             document.getElementById('showcolstart').checked = false;
                   5669:             document.getElementById('showcolend').checked = false;
1.374     raeburn  5670:         } else {
                   5671:             if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   5672:                 document.getElementById('showcolstatus').checked = true;
                   5673:                 document.getElementById('showcolstatus').disabled = '';
                   5674:                 document.getElementById('showcolstart').checked = true;
                   5675:                 document.getElementById('showcolend').checked = true;
                   5676:             } else {
                   5677:                 document.getElementById('showcolstatus').checked = false;
                   5678:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   5679:                 document.getElementById('showcolstart').checked = false;
                   5680:                 document.getElementById('showcolend').checked = false;
                   5681:             }
1.364     raeburn  5682:         }
                   5683:     }
                   5684:     if (caller == 'output') {
                   5685:         if (photos == 1) {
                   5686:             if (document.getElementById('showcolphoto')) {
                   5687:                 var photoitem = document.getElementById('showcolphoto');
                   5688:                 if (document.studentform.output.options[document.studentform.output.selectedIndex].value == 'html') {
                   5689:                     photoitem.checked = true;
                   5690:                     photoitem.disabled = '';
                   5691:                 } else {
                   5692:                     photoitem.checked = false;
                   5693:                     photoitem.disabled = 'disabled';
                   5694:                 }
                   5695:             }
                   5696:         }
                   5697:     }
                   5698:     if (caller == 'showrole') {
1.371     raeburn  5699:         if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any') ||
                   5700:             (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'cr')) {
1.364     raeburn  5701:             document.getElementById('showcolrole').checked = true;
                   5702:             document.getElementById('showcolrole').disabled = '';
                   5703:         } else {
                   5704:             document.getElementById('showcolrole').checked = false;
                   5705:             document.getElementById('showcolrole').disabled = 'disabled';
                   5706:         }
1.374     raeburn  5707:         if (context == 'domain') {
1.382     raeburn  5708:             var quotausageshow = 0;
1.374     raeburn  5709:             if ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   5710:                 (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community')) {
                   5711:                 document.getElementById('showcolstatus').checked = false;
                   5712:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   5713:                 document.getElementById('showcolstart').checked = false;
                   5714:                 document.getElementById('showcolend').checked = false;
                   5715:             } else {
                   5716:                 if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   5717:                     document.getElementById('showcolstatus').checked = true;
                   5718:                     document.getElementById('showcolstatus').disabled = '';
                   5719:                     document.getElementById('showcolstart').checked = true;
                   5720:                     document.getElementById('showcolend').checked = true;
                   5721:                 }
                   5722:             }
                   5723:             if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'domain') {
                   5724:                 document.getElementById('showcolextent').disabled = 'disabled';
                   5725:                 document.getElementById('showcolextent').checked = 'false';
                   5726:                 document.getElementById('showextent').style.display='none';
                   5727:                 document.getElementById('showcoltextextent').innerHTML = '';
1.382     raeburn  5728:                 if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'au') ||
                   5729:                     (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any')) {
                   5730:                     if (document.getElementById('showcolauthorusage')) {
                   5731:                         document.getElementById('showcolauthorusage').disabled = '';
                   5732:                     }
                   5733:                     if (document.getElementById('showcolauthorquota')) {
                   5734:                         document.getElementById('showcolauthorquota').disabled = '';
                   5735:                     }
                   5736:                     quotausageshow = 1;
                   5737:                 }
1.374     raeburn  5738:             } else {
                   5739:                 document.getElementById('showextent').style.display='block';
                   5740:                 document.getElementById('showextent').style.textAlign='left';
                   5741:                 document.getElementById('showextent').style.textFace='normal';
                   5742:                 if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'author') {
                   5743:                     document.getElementById('showcolextent').disabled = '';
                   5744:                     document.getElementById('showcolextent').checked = 'true';
                   5745:                     document.getElementById('showcoltextextent').innerHTML="$lt{'author'}";
                   5746:                 } else {
                   5747:                     document.getElementById('showcolextent').disabled = '';
                   5748:                     document.getElementById('showcolextent').checked = 'true';
                   5749:                     if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community') {
                   5750:                         document.getElementById('showcoltextextent').innerHTML="$lt{'community'}";
                   5751:                     } else {
                   5752:                         document.getElementById('showcoltextextent').innerHTML="$lt{'course'}";
                   5753:                     }
                   5754:                 }
                   5755:             }
1.382     raeburn  5756:             if (quotausageshow == 0)  {
                   5757:                 if (document.getElementById('showcolauthorusage')) {
                   5758:                     document.getElementById('showcolauthorusage').checked = false;
                   5759:                     document.getElementById('showcolauthorusage').disabled = 'disabled';
                   5760:                 }
                   5761:                 if (document.getElementById('showcolauthorquota')) {
                   5762:                     document.getElementById('showcolauthorquota').checked = false;
                   5763:                     document.getElementById('showcolauthorquota').disabled = 'disabled';
                   5764:                 }
                   5765:             }
1.374     raeburn  5766:         }
1.364     raeburn  5767:     }
                   5768:     return;
                   5769: }
                   5770: 
1.202     raeburn  5771: END
                   5772:     return $output;
                   5773: 
                   5774: }
                   5775: 
1.190     raeburn  5776: ###############################################################
                   5777: ###############################################################
                   5778: #  Menu Phase One
                   5779: sub print_main_menu {
1.318     raeburn  5780:     my ($permission,$context,$crstype) = @_;
                   5781:     my $linkcontext = $context;
                   5782:     my $stuterm = lc(&Apache::lonnet::plaintext('st',$crstype));
                   5783:     if (($context eq 'course') && ($crstype eq 'Community')) {
                   5784:         $linkcontext = lc($crstype);
                   5785:         $stuterm = 'Members';
                   5786:     }
1.208     raeburn  5787:     my %links = (
1.298     droeschl 5788:                 domain => {
                   5789:                             upload     => 'Upload a File of Users',
                   5790:                             singleuser => 'Add/Modify a User',
                   5791:                             listusers  => 'Manage Users',
                   5792:                             },
                   5793:                 author => {
                   5794:                             upload     => 'Upload a File of Co-authors',
                   5795:                             singleuser => 'Add/Modify a Co-author',
                   5796:                             listusers  => 'Manage Co-authors',
                   5797:                             },
                   5798:                 course => {
                   5799:                             upload     => 'Upload a File of Course Users',
                   5800:                             singleuser => 'Add/Modify a Course User',
1.354     www      5801:                             listusers  => 'List and Modify Multiple Course Users',
1.298     droeschl 5802:                             },
1.318     raeburn  5803:                 community => {
                   5804:                             upload     => 'Upload a File of Community Users',
                   5805:                             singleuser => 'Add/Modify a Community User',
1.354     www      5806:                             listusers  => 'List and Modify Multiple Community Users',
1.318     raeburn  5807:                            },
                   5808:                 );
                   5809:      my %linktitles = (
                   5810:                 domain => {
                   5811:                             singleuser => 'Add a user to the domain, and/or a course or community in the domain.',
                   5812:                             listusers  => 'Show and manage users in this domain.',
                   5813:                             },
                   5814:                 author => {
                   5815:                             singleuser => 'Add a user with a co- or assistant author role.',
                   5816:                             listusers  => 'Show and manage co- or assistant authors.',
                   5817:                             },
                   5818:                 course => {
                   5819:                             singleuser => 'Add a user with a certain role to this course.',
                   5820:                             listusers  => 'Show and manage users in this course.',
                   5821:                             },
                   5822:                 community => {
                   5823:                             singleuser => 'Add a user with a certain role to this community.',
                   5824:                             listusers  => 'Show and manage users in this community.',
                   5825:                            },
1.298     droeschl 5826:                 );
1.465   ! raeburn  5827: 
1.418     raeburn  5828:   if ($linkcontext eq 'domain') {
                   5829:       unless ($permission->{'cusr'}) {
1.430     raeburn  5830:           $links{'domain'}{'singleuser'} = 'View a User';
1.418     raeburn  5831:           $linktitles{'domain'}{'singleuser'} = 'View information about a user in the domain';
                   5832:       }
                   5833:   } elsif ($linkcontext eq 'course') {
                   5834:       unless ($permission->{'cusr'}) {
                   5835:           $links{'course'}{'singleuser'} = 'View a Course User';
                   5836:           $linktitles{'course'}{'singleuser'} = 'View information about a user in this course';
                   5837:           $links{'course'}{'listusers'} = 'List Course Users';
                   5838:           $linktitles{'course'}{'listusers'} = 'Show information about users in this course';
                   5839:       }
                   5840:   } elsif ($linkcontext eq 'community') {
                   5841:       unless ($permission->{'cusr'}) {
                   5842:           $links{'community'}{'singleuser'} = 'View a Community User';
                   5843:           $linktitles{'community'}{'singleuser'} = 'View information about a user in this community';
                   5844:           $links{'community'}{'listusers'} = 'List Community Users';
                   5845:           $linktitles{'community'}{'listusers'} = 'Show information about users in this community';
                   5846:       }
                   5847:   }
1.298     droeschl 5848:   my @menu = ( {categorytitle => 'Single Users', 
                   5849:          items =>
                   5850:          [
                   5851:             {
1.318     raeburn  5852:              linktext => $links{$linkcontext}{'singleuser'},
1.298     droeschl 5853:              icon => 'edit-redo.png',
                   5854:              #help => 'Course_Change_Privileges',
                   5855:              url => '/adm/createuser?action=singleuser',
1.418     raeburn  5856:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  5857:              linktitle => $linktitles{$linkcontext}{'singleuser'},
1.298     droeschl 5858:             },
                   5859:          ]},
                   5860: 
                   5861:          {categorytitle => 'Multiple Users',
                   5862:          items => 
                   5863:          [
                   5864:             {
1.318     raeburn  5865:              linktext => $links{$linkcontext}{'upload'},
1.340     wenzelju 5866:              icon => 'uplusr.png',
1.298     droeschl 5867:              #help => 'Course_Create_Class_List',
                   5868:              url => '/adm/createuser?action=upload',
                   5869:              permission => $permission->{'cusr'},
                   5870:              linktitle => 'Upload a CSV or a text file containing users.',
                   5871:             },
                   5872:             {
1.318     raeburn  5873:              linktext => $links{$linkcontext}{'listusers'},
1.340     wenzelju 5874:              icon => 'mngcu.png',
1.298     droeschl 5875:              #help => 'Course_View_Class_List',
                   5876:              url => '/adm/createuser?action=listusers',
                   5877:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  5878:              linktitle => $linktitles{$linkcontext}{'listusers'}, 
1.298     droeschl 5879:             },
                   5880: 
                   5881:          ]},
                   5882: 
                   5883:          {categorytitle => 'Administration',
                   5884:          items => [ ]},
                   5885:        );
1.415     raeburn  5886: 
1.265     mielkec  5887:     if ($context eq 'domain'){
1.416     raeburn  5888:         push(@{  $menu[0]->{items} }, # Single Users
                   5889:             {
                   5890:              linktext => 'User Access Log',
1.417     raeburn  5891:              icon => 'document-properties.png',
1.425     raeburn  5892:              #help => 'Domain_User_Access_Logs',
1.416     raeburn  5893:              url => '/adm/createuser?action=accesslogs',
                   5894:              permission => $permission->{'activity'},
                   5895:              linktitle => 'View user access log.',
                   5896:             }
                   5897:         );
1.298     droeschl 5898:         
                   5899:         push(@{ $menu[2]->{items} }, #Category: Administration
                   5900:             {
                   5901:              linktext => 'Custom Roles',
                   5902:              icon => 'emblem-photos.png',
                   5903:              #help => 'Course_Editing_Custom_Roles',
                   5904:              url => '/adm/createuser?action=custom',
                   5905:              permission => $permission->{'custom'},
                   5906:              linktitle => 'Configure a custom role.',
                   5907:             },
1.362     raeburn  5908:             {
                   5909:              linktext => 'Authoring Space Requests',
                   5910:              icon => 'selfenrl-queue.png',
                   5911:              #help => 'Domain_Role_Approvals',
                   5912:              url => '/adm/createuser?action=processauthorreq',
                   5913:              permission => $permission->{'cusr'},
                   5914:              linktitle => 'Approve or reject author role requests',
                   5915:             },
1.363     raeburn  5916:             {
1.391     raeburn  5917:              linktext => 'LON-CAPA Account Requests',
                   5918:              icon => 'list-add.png',
                   5919:              #help => 'Domain_Username_Approvals',
                   5920:              url => '/adm/createuser?action=processusernamereq',
                   5921:              permission => $permission->{'cusr'},
                   5922:              linktitle => 'Approve or reject LON-CAPA account requests',
                   5923:             },
                   5924:             {
1.363     raeburn  5925:              linktext => 'Change Log',
                   5926:              icon => 'document-properties.png',
                   5927:              #help => 'Course_User_Logs',
                   5928:              url => '/adm/createuser?action=changelogs',
1.418     raeburn  5929:              permission => ($permission->{'cusr'} || $permission->{'view'}),
1.363     raeburn  5930:              linktitle => 'View change log.',
                   5931:             },
1.298     droeschl 5932:         );
                   5933:         
1.265     mielkec  5934:     }elsif ($context eq 'course'){
1.298     droeschl 5935:         my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
1.318     raeburn  5936: 
                   5937:         my %linktext = (
                   5938:                          'Course'    => {
                   5939:                                           single => 'Add/Modify a Student', 
                   5940:                                           drop   => 'Drop Students',
                   5941:                                           groups => 'Course Groups',
                   5942:                                         },
                   5943:                          'Community' => {
                   5944:                                           single => 'Add/Modify a Member', 
                   5945:                                           drop   => 'Drop Members',
                   5946:                                           groups => 'Community Groups',
                   5947:                                         },
                   5948:                        );
1.411     raeburn  5949:         $linktext{'Placement'} = $linktext{'Course'};
1.318     raeburn  5950: 
                   5951:         my %linktitle = (
                   5952:             'Course' => {
                   5953:                   single => 'Add a user with the role of student to this course',
                   5954:                   drop   => 'Remove a student from this course.',
                   5955:                   groups => 'Manage course groups',
                   5956:                         },
                   5957:             'Community' => {
                   5958:                   single => 'Add a user with the role of member to this community',
                   5959:                   drop   => 'Remove a member from this community.',
                   5960:                   groups => 'Manage community groups',
                   5961:                            },
                   5962:         );
                   5963: 
1.411     raeburn  5964:         $linktitle{'Placement'} = $linktitle{'Course'};
                   5965: 
1.298     droeschl 5966:         push(@{ $menu[0]->{items} }, #Category: Single Users
                   5967:             {   
1.318     raeburn  5968:              linktext => $linktext{$crstype}{'single'},
1.298     droeschl 5969:              #help => 'Course_Add_Student',
                   5970:              icon => 'list-add.png',
                   5971:              url => '/adm/createuser?action=singlestudent',
                   5972:              permission => $permission->{'cusr'},
1.318     raeburn  5973:              linktitle => $linktitle{$crstype}{'single'},
1.298     droeschl 5974:             },
                   5975:         );
                   5976:         
                   5977:         push(@{ $menu[1]->{items} }, #Category: Multiple Users 
                   5978:             {
1.318     raeburn  5979:              linktext => $linktext{$crstype}{'drop'},
1.298     droeschl 5980:              icon => 'edit-undo.png',
                   5981:              #help => 'Course_Drop_Student',
                   5982:              url => '/adm/createuser?action=drop',
                   5983:              permission => $permission->{'cusr'},
1.318     raeburn  5984:              linktitle => $linktitle{$crstype}{'drop'},
1.298     droeschl 5985:             },
                   5986:         );
                   5987:         push(@{ $menu[2]->{items} }, #Category: Administration
1.428     raeburn  5988:             {
                   5989:              linktext => 'Helpdesk Access',
                   5990:              icon => 'helpdesk-access.png',
                   5991:              #help => 'Course_Helpdesk_Access',
                   5992:              url => '/adm/createuser?action=helpdesk',
1.464     raeburn  5993:              permission => (($permission->{'owner'} || $permission->{'co-owner'}) &&
                   5994:                             ($permission->{'view'} || $permission->{'cusr'})),
1.428     raeburn  5995:              linktitle => 'Helpdesk access options',
                   5996:             },
                   5997:             {
1.298     droeschl 5998:              linktext => 'Custom Roles',
                   5999:              icon => 'emblem-photos.png',
                   6000:              #help => 'Course_Editing_Custom_Roles',
                   6001:              url => '/adm/createuser?action=custom',
                   6002:              permission => $permission->{'custom'},
                   6003:              linktitle => 'Configure a custom role.',
                   6004:             },
                   6005:             {
1.318     raeburn  6006:              linktext => $linktext{$crstype}{'groups'},
1.333     wenzelju 6007:              icon => 'grps.png',
1.298     droeschl 6008:              #help => 'Course_Manage_Group',
                   6009:              url => '/adm/coursegroups?refpage=cusr',
                   6010:              permission => $permission->{'grp_manage'},
1.318     raeburn  6011:              linktitle => $linktitle{$crstype}{'groups'},
1.298     droeschl 6012:             },
                   6013:             {
1.328     wenzelju 6014:              linktext => 'Change Log',
1.298     droeschl 6015:              icon => 'document-properties.png',
                   6016:              #help => 'Course_User_Logs',
                   6017:              url => '/adm/createuser?action=changelogs',
1.418     raeburn  6018:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.298     droeschl 6019:              linktitle => 'View change log.',
                   6020:             },
                   6021:         );
1.277     raeburn  6022:         if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'}) {
1.298     droeschl 6023:             push(@{ $menu[2]->{items} },
1.398     raeburn  6024:                     {
1.298     droeschl 6025:                      linktext => 'Enrollment Requests',
                   6026:                      icon => 'selfenrl-queue.png',
                   6027:                      #help => 'Course_Approve_Selfenroll',
                   6028:                      url => '/adm/createuser?action=selfenrollqueue',
1.398     raeburn  6029:                      permission => $permission->{'selfenrolladmin'},
1.298     droeschl 6030:                      linktitle =>'Approve or reject enrollment requests.',
                   6031:                     },
                   6032:             );
1.277     raeburn  6033:         }
1.298     droeschl 6034:         
1.265     mielkec  6035:         if (!exists($permission->{'cusr_section'})){
1.320     raeburn  6036:             if ($crstype ne 'Community') {
                   6037:                 push(@{ $menu[2]->{items} },
                   6038:                     {
                   6039:                      linktext => 'Automated Enrollment',
                   6040:                      icon => 'roles.png',
                   6041:                      #help => 'Course_Automated_Enrollment',
                   6042:                      permission => (&Apache::lonnet::auto_run($cnum,$cdom)
1.418     raeburn  6043:                                          && (($permission->{'cusr'}) ||
                   6044:                                              ($permission->{'view'}))),
1.320     raeburn  6045:                      url  => '/adm/populate',
                   6046:                      linktitle => 'Automated enrollment manager.',
                   6047:                     }
                   6048:                 );
                   6049:             }
                   6050:             push(@{ $menu[2]->{items} }, 
1.298     droeschl 6051:                 {
                   6052:                  linktext => 'User Self-Enrollment',
1.342     wenzelju 6053:                  icon => 'self_enroll.png',
1.298     droeschl 6054:                  #help => 'Course_Self_Enrollment',
                   6055:                  url => '/adm/createuser?action=selfenroll',
1.398     raeburn  6056:                  permission => $permission->{'selfenrolladmin'},
1.317     bisitz   6057:                  linktitle => 'Configure user self-enrollment.',
1.298     droeschl 6058:                 },
                   6059:             );
                   6060:         }
1.363     raeburn  6061:     } elsif ($context eq 'author') {
1.370     raeburn  6062:         push(@{ $menu[2]->{items} }, #Category: Administration
1.363     raeburn  6063:             {
                   6064:              linktext => 'Change Log',
                   6065:              icon => 'document-properties.png',
                   6066:              #help => 'Course_User_Logs',
                   6067:              url => '/adm/createuser?action=changelogs',
                   6068:              permission => $permission->{'cusr'},
                   6069:              linktitle => 'View change log.',
                   6070:             },
1.370     raeburn  6071:         );
1.363     raeburn  6072:     }
1.465   ! raeburn  6073:     push(@{ $menu[2]->{items} },
        !          6074:         {
        !          6075:          linktext => 'Role Requests (other domains)',
        !          6076:          icon => 'edit-find.png',
        !          6077:          #help => 'Role_Requests',
        !          6078:          url => '/adm/createuser?action=rolerequests',
        !          6079:          permission => $permission->{'cusr'},
        !          6080:          linktitle => 'Role requests for users in other domains',
        !          6081:         },
        !          6082:     );
        !          6083:     if (&show_role_requests($context,$env{'request.role.domain'})) {
        !          6084:         push(@{ $menu[2]->{items} },
        !          6085:             {
        !          6086:              linktext => 'Queued Role Assignments (this domain)',
        !          6087:              icon => 'edit-find.png',
        !          6088:              #help => 'Role_Approvals',
        !          6089:              url => '/adm/createuser?action=queuedroles',
        !          6090:              permission => $permission->{'cusr'},
        !          6091:              linktitle => "Role requests for this domain's users",
        !          6092:             },
        !          6093:         );
        !          6094:     }
1.363     raeburn  6095:     return Apache::lonhtmlcommon::generate_menu(@menu);
1.250     raeburn  6096: #               { text => 'View Log-in History',
                   6097: #                 help => 'Course_User_Logins',
                   6098: #                 action => 'logins',
                   6099: #                 permission => $permission->{'cusr'},
                   6100: #               });
1.190     raeburn  6101: }
                   6102: 
1.189     albertel 6103: sub restore_prev_selections {
                   6104:     my %saveable_parameters = ('srchby'   => 'scalar',
                   6105: 			       'srchin'   => 'scalar',
                   6106: 			       'srchtype' => 'scalar',
                   6107: 			       );
                   6108:     &Apache::loncommon::store_settings('user','user_picker',
                   6109: 				       \%saveable_parameters);
                   6110:     &Apache::loncommon::restore_settings('user','user_picker',
                   6111: 					 \%saveable_parameters);
                   6112: }
                   6113: 
1.237     raeburn  6114: sub print_selfenroll_menu {
1.418     raeburn  6115:     my ($r,$context,$cid,$cdom,$cnum,$currsettings,$additional,$readonly) = @_;
1.322     raeburn  6116:     my $crstype = &Apache::loncommon::course_type();
1.398     raeburn  6117:     my $formname = 'selfenroll';
1.237     raeburn  6118:     my $nolink = 1;
1.398     raeburn  6119:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
1.237     raeburn  6120:     my $groupslist = &Apache::lonuserutils::get_groupslist();
                   6121:     my $setsec_js = 
                   6122:         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
1.249     raeburn  6123:     my %alerts = &Apache::lonlocal::texthash(
                   6124:         acto => 'Activation of self-enrollment was selected for the following domain(s)',
                   6125:         butn => 'but no user types have been checked.',
                   6126:         wilf => "Please uncheck 'activate' or check at least one type.",
                   6127:     );
1.418     raeburn  6128:     my $disabled;
                   6129:     if ($readonly) {
                   6130:        $disabled = ' disabled="disabled"';
                   6131:     }
1.405     damieng  6132:     &js_escape(\%alerts);
1.249     raeburn  6133:     my $selfenroll_js = <<"ENDSCRIPT";
                   6134: function update_types(caller,num) {
                   6135:     var delidx = getIndexByName('selfenroll_delete');
                   6136:     var actidx = getIndexByName('selfenroll_activate');
                   6137:     if (caller == 'selfenroll_all') {
                   6138:         var selall;
                   6139:         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   6140:             if (document.$formname.selfenroll_all[i].checked) {
                   6141:                 selall = document.$formname.selfenroll_all[i].value;
                   6142:             }
                   6143:         }
                   6144:         if (selall == 1) {
                   6145:             if (delidx != -1) {
                   6146:                 if (document.$formname.selfenroll_delete.length) {
                   6147:                     for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   6148:                         document.$formname.selfenroll_delete[j].checked = true;
                   6149:                     }
                   6150:                 } else {
                   6151:                     document.$formname.elements[delidx].checked = true;
                   6152:                 }
                   6153:             }
                   6154:             if (actidx != -1) {
                   6155:                 if (document.$formname.selfenroll_activate.length) {
                   6156:                     for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   6157:                         document.$formname.selfenroll_activate[j].checked = false;
                   6158:                     }
                   6159:                 } else {
                   6160:                     document.$formname.elements[actidx].checked = false;
                   6161:                 }
                   6162:             }
                   6163:             document.$formname.selfenroll_newdom.selectedIndex = 0; 
                   6164:         }
                   6165:     }
                   6166:     if (caller == 'selfenroll_activate') {
                   6167:         if (document.$formname.selfenroll_activate.length) {
                   6168:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   6169:                 if (document.$formname.selfenroll_activate[j].value == num) {
                   6170:                     if (document.$formname.selfenroll_activate[j].checked) {
                   6171:                         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   6172:                             if (document.$formname.selfenroll_all[i].value == '1') {
                   6173:                                 document.$formname.selfenroll_all[i].checked = false;
                   6174:                             }
                   6175:                             if (document.$formname.selfenroll_all[i].value == '0') {
                   6176:                                 document.$formname.selfenroll_all[i].checked = true;
                   6177:                             }
                   6178:                         }
                   6179:                     }
                   6180:                 }
                   6181:             }
                   6182:         } else {
                   6183:             for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   6184:                 if (document.$formname.selfenroll_all[i].value == '1') {
                   6185:                     document.$formname.selfenroll_all[i].checked = false;
                   6186:                 }
                   6187:                 if (document.$formname.selfenroll_all[i].value == '0') {
                   6188:                     document.$formname.selfenroll_all[i].checked = true;
                   6189:                 }
                   6190:             }
                   6191:         }
                   6192:     }
                   6193:     if (caller == 'selfenroll_delete') {
                   6194:         if (document.$formname.selfenroll_delete.length) {
                   6195:             for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   6196:                 if (document.$formname.selfenroll_delete[j].value == num) {
                   6197:                     if (document.$formname.selfenroll_delete[j].checked) {
                   6198:                         var delindex = getIndexByName('selfenroll_types_'+num);
                   6199:                         if (delindex != -1) { 
                   6200:                             if (document.$formname.elements[delindex].length) {
                   6201:                                 for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   6202:                                     document.$formname.elements[delindex][k].checked = false;
                   6203:                                 }
                   6204:                             } else {
                   6205:                                 document.$formname.elements[delindex].checked = false;
                   6206:                             }
                   6207:                         }
                   6208:                     }
                   6209:                 }
                   6210:             }
                   6211:         } else {
                   6212:             if (document.$formname.selfenroll_delete.checked) {
                   6213:                 var delindex = getIndexByName('selfenroll_types_'+num);
                   6214:                 if (delindex != -1) {
                   6215:                     if (document.$formname.elements[delindex].length) {
                   6216:                         for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   6217:                             document.$formname.elements[delindex][k].checked = false;
                   6218:                         }
                   6219:                     } else {
                   6220:                         document.$formname.elements[delindex].checked = false;
                   6221:                     }
                   6222:                 }
                   6223:             }
                   6224:         }
                   6225:     }
                   6226:     return;
                   6227: }
                   6228: 
                   6229: function validate_types(form) {
                   6230:     var needaction = new Array();
                   6231:     var countfail = 0;
                   6232:     var actidx = getIndexByName('selfenroll_activate');
                   6233:     if (actidx != -1) {
                   6234:         if (document.$formname.selfenroll_activate.length) {
                   6235:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   6236:                 var num = document.$formname.selfenroll_activate[j].value;
                   6237:                 if (document.$formname.selfenroll_activate[j].checked) {
                   6238:                     countfail = check_types(num,countfail,needaction)
                   6239:                 }
                   6240:             }
                   6241:         } else {
                   6242:             if (document.$formname.selfenroll_activate.checked) {
1.398     raeburn  6243:                 var num = document.$formname.selfenroll_activate.value;
1.249     raeburn  6244:                 countfail = check_types(num,countfail,needaction)
                   6245:             }
                   6246:         }
                   6247:     }
                   6248:     if (countfail > 0) {
                   6249:         var msg = "$alerts{'acto'}\\n";
                   6250:         var loopend = needaction.length -1;
                   6251:         if (loopend > 0) {
                   6252:             for (var m=0; m<loopend; m++) {
                   6253:                 msg += needaction[m]+", ";
                   6254:             }
                   6255:         }
                   6256:         msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
                   6257:         alert(msg);
                   6258:         return; 
                   6259:     }
                   6260:     setSections(form);
                   6261: }
                   6262: 
                   6263: function check_types(num,countfail,needaction) {
1.441     raeburn  6264:     var boxname = 'selfenroll_types_'+num;
                   6265:     var typeidx = getIndexByName(boxname);
1.249     raeburn  6266:     var count = 0;
                   6267:     if (typeidx != -1) {
1.441     raeburn  6268:         if (document.$formname.elements[boxname].length) {
                   6269:             for (var k=0; k<document.$formname.elements[boxname].length; k++) {
                   6270:                 if (document.$formname.elements[boxname][k].checked) {
1.249     raeburn  6271:                     count ++;
                   6272:                 }
                   6273:             }
                   6274:         } else {
                   6275:             if (document.$formname.elements[typeidx].checked) {
                   6276:                 count ++;
                   6277:             }
                   6278:         }
                   6279:         if (count == 0) {
                   6280:             var domidx = getIndexByName('selfenroll_dom_'+num);
                   6281:             if (domidx != -1) {
                   6282:                 var domname = document.$formname.elements[domidx].value;
                   6283:                 needaction[countfail] = domname;
                   6284:                 countfail ++;
                   6285:             }
                   6286:         }
                   6287:     }
                   6288:     return countfail;
                   6289: }
                   6290: 
1.398     raeburn  6291: function toggleNotify() {
                   6292:     var selfenrollApproval = 0;
                   6293:     if (document.$formname.selfenroll_approval.length) {
                   6294:         for (var i=0; i<document.$formname.selfenroll_approval.length; i++) {
                   6295:             if (document.$formname.selfenroll_approval[i].checked) {
                   6296:                 selfenrollApproval = document.$formname.selfenroll_approval[i].value;
                   6297:                 break;        
                   6298:             }
                   6299:         }
                   6300:     }
                   6301:     if (document.getElementById('notified')) {
                   6302:         if (selfenrollApproval == 0) {
                   6303:             document.getElementById('notified').style.display='none';
                   6304:         } else {
                   6305:             document.getElementById('notified').style.display='block';
                   6306:         }
                   6307:     }
                   6308:     return;
                   6309: }
                   6310: 
1.249     raeburn  6311: function getIndexByName(item) {
                   6312:     for (var i=0;i<document.$formname.elements.length;i++) {
                   6313:         if (document.$formname.elements[i].name == item) {
                   6314:             return i;
                   6315:         }
                   6316:     }
                   6317:     return -1;
                   6318: }
                   6319: ENDSCRIPT
1.256     raeburn  6320: 
1.237     raeburn  6321:     my $output = '<script type="text/javascript">'."\n".
1.301     bisitz   6322:                  '// <![CDATA['."\n".
1.249     raeburn  6323:                  $setsec_js."\n".$selfenroll_js."\n".
1.301     bisitz   6324:                  '// ]]>'."\n".
1.237     raeburn  6325:                  '</script>'."\n".
1.256     raeburn  6326:                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n";
1.400     raeburn  6327:  
                   6328:     my $visactions = &cat_visibility();
                   6329:     my ($cathash,%cattype);
                   6330:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   6331:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   6332:         $cathash = $domconfig{'coursecategories'}{'cats'};
                   6333:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
                   6334:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
1.406     raeburn  6335:         if ($cattype{'auth'} eq '') {
                   6336:             $cattype{'auth'} = 'std';
                   6337:         }
                   6338:         if ($cattype{'unauth'} eq '') {
                   6339:             $cattype{'unauth'} = 'std';
                   6340:         }
1.400     raeburn  6341:     } else {
                   6342:         $cathash = {};
                   6343:         $cattype{'auth'} = 'std';
                   6344:         $cattype{'unauth'} = 'std';
                   6345:     }
                   6346:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
                   6347:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   6348:                   '<br />'.
                   6349:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   6350:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
                   6351:                   '</ul>');
                   6352:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
                   6353:         if ($currsettings->{'uniquecode'}) {
                   6354:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
                   6355:         } else {
                   6356:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   6357:                   '<br />'.
                   6358:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   6359:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
                   6360:                   '</ul><br />');
                   6361:         }
                   6362:     } else {
                   6363:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
                   6364:         if (ref($visactions) eq 'HASH') {
                   6365:             if ($visible) {
                   6366:                 $output .= '<p class="LC_info">'.$visactions->{'vis'}.'</p>';
                   6367:            } else {
                   6368:                 $output .= '<p class="LC_warning">'.$visactions->{'miss'}.'</p>'
                   6369:                           .$visactions->{'yous'}.
                   6370:                            '<p>'.$visactions->{'gen'}.'<br />'.$visactions->{'coca'};
                   6371:                 if (ref($vismsgs) eq 'ARRAY') {
                   6372:                     $output .= '<br />'.$visactions->{'make'}.'<ul>';
                   6373:                     foreach my $item (@{$vismsgs}) {
                   6374:                         $output .= '<li>'.$visactions->{$item}.'</li>';
                   6375:                     }
                   6376:                     $output .= '</ul>';
1.256     raeburn  6377:                 }
1.400     raeburn  6378:                 $output .= '</p>';
1.256     raeburn  6379:             }
                   6380:         }
                   6381:     }
1.398     raeburn  6382:     my $actionhref = '/adm/createuser';
                   6383:     if ($context eq 'domain') {
                   6384:         $actionhref = '/adm/modifycourse';
                   6385:     }
1.400     raeburn  6386: 
                   6387:     my %noedit;
                   6388:     unless ($context eq 'domain') {
                   6389:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
                   6390:     }
1.398     raeburn  6391:     $output .= '<form name="'.$formname.'" method="post" action="'.$actionhref.'">'."\n".
1.256     raeburn  6392:                &Apache::lonhtmlcommon::start_pick_box();
1.237     raeburn  6393:     if (ref($row) eq 'ARRAY') {
                   6394:         foreach my $item (@{$row}) {
                   6395:             my $title = $item; 
                   6396:             if (ref($lt) eq 'HASH') {
                   6397:                 $title = $lt->{$item};
                   6398:             }
1.297     bisitz   6399:             $output .= &Apache::lonhtmlcommon::row_title($title);
1.237     raeburn  6400:             if ($item eq 'types') {
1.398     raeburn  6401:                 my $curr_types;
                   6402:                 if (ref($currsettings) eq 'HASH') {
                   6403:                     $curr_types = $currsettings->{'selfenroll_types'};
                   6404:                 }
1.400     raeburn  6405:                 if ($noedit{$item}) {
                   6406:                     if ($curr_types eq '*') {
                   6407:                         $output .= &mt('Any user in any domain');   
                   6408:                     } else {
                   6409:                         my @entries = split(/;/,$curr_types);
                   6410:                         if (@entries > 0) {
                   6411:                             $output .= '<ul>'; 
                   6412:                             foreach my $entry (@entries) {
                   6413:                                 my ($currdom,$typestr) = split(/:/,$entry);
                   6414:                                 next if ($typestr eq '');
                   6415:                                 my $domdesc = &Apache::lonnet::domain($currdom);
                   6416:                                 my @currinsttypes = split(',',$typestr);
                   6417:                                 my ($othertitle,$usertypes,$types) = 
                   6418:                                     &Apache::loncommon::sorted_inst_types($currdom);
                   6419:                                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   6420:                                     $usertypes->{'any'} = &mt('any user'); 
                   6421:                                     if (keys(%{$usertypes}) > 0) {
                   6422:                                         $usertypes->{'other'} = &mt('other users');
                   6423:                                     }
                   6424:                                     my @longinsttypes = map { $usertypes->{$_}; } @currinsttypes;
                   6425:                                     $output .= '<li>'.$domdesc.':'.join(', ',@longinsttypes).'</li>';
                   6426:                                  }
                   6427:                             }
                   6428:                             $output .= '</ul>';
                   6429:                         } else {
                   6430:                             $output .= &mt('None');
                   6431:                         }
                   6432:                     }
                   6433:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6434:                     next;
                   6435:                 }
1.241     raeburn  6436:                 my $showdomdesc = 1;
                   6437:                 my $includeempty = 1;
                   6438:                 my $num = 0;
                   6439:                 $output .= &Apache::loncommon::start_data_table().
                   6440:                            &Apache::loncommon::start_data_table_row()
                   6441:                            .'<td colspan="2"><span class="LC_nobreak"><label>'
                   6442:                            .&mt('Any user in any domain:')
                   6443:                            .'&nbsp;<input type="radio" name="selfenroll_all" value="1" ';
                   6444:                 if ($curr_types eq '*') {
                   6445:                     $output .= ' checked="checked" '; 
                   6446:                 }
1.249     raeburn  6447:                 $output .= 'onchange="javascript:update_types('.
1.418     raeburn  6448:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('Yes').'</label>'.
1.249     raeburn  6449:                            '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
1.241     raeburn  6450:                 if ($curr_types ne '*') {
                   6451:                     $output .= ' checked="checked" ';
                   6452:                 }
1.249     raeburn  6453:                 $output .= ' onchange="javascript:update_types('.
1.418     raeburn  6454:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('No').'</label></td>'.
1.249     raeburn  6455:                            &Apache::loncommon::end_data_table_row().
                   6456:                            &Apache::loncommon::end_data_table().
                   6457:                            &mt('Or').'<br />'.
                   6458:                            &Apache::loncommon::start_data_table();
1.241     raeburn  6459:                 my %currdoms;
1.249     raeburn  6460:                 if ($curr_types eq '') {
1.241     raeburn  6461:                     $output .= &new_selfenroll_dom_row($cdom,'0');
                   6462:                 } elsif ($curr_types ne '*') {
                   6463:                     my @entries = split(/;/,$curr_types);
                   6464:                     if (@entries > 0) {
                   6465:                         foreach my $entry (@entries) {
                   6466:                             my ($currdom,$typestr) = split(/:/,$entry);
                   6467:                             $currdoms{$currdom} = 1;
                   6468:                             my $domdesc = &Apache::lonnet::domain($currdom);
1.249     raeburn  6469:                             my @currinsttypes = split(',',$typestr);
1.241     raeburn  6470:                             $output .= &Apache::loncommon::start_data_table_row()
                   6471:                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
                   6472:                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
                   6473:                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
                   6474:                                        .'" value="'.$currdom.'" /></span><br />'
                   6475:                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
1.418     raeburn  6476:                                        .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');"'.$disabled.' />'
1.241     raeburn  6477:                                        .&mt('Delete').'</label></span></td>';
1.249     raeburn  6478:                             $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
1.418     raeburn  6479:                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes,$readonly).'</td>'
1.241     raeburn  6480:                                        .&Apache::loncommon::end_data_table_row();
                   6481:                             $num ++;
                   6482:                         }
                   6483:                     }
                   6484:                 }
1.249     raeburn  6485:                 my $add_domtitle = &mt('Users in additional domain:');
1.241     raeburn  6486:                 if ($curr_types eq '*') { 
1.249     raeburn  6487:                     $add_domtitle = &mt('Users in specific domain:');
1.241     raeburn  6488:                 } elsif ($curr_types eq '') {
1.249     raeburn  6489:                     $add_domtitle = &mt('Users in other domain:');
1.241     raeburn  6490:                 }
1.446     raeburn  6491:                 my ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('enroll',$cdom);
1.241     raeburn  6492:                 $output .= &Apache::loncommon::start_data_table_row()
                   6493:                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
                   6494:                            .&Apache::loncommon::select_dom_form('','selfenroll_newdom',
1.446     raeburn  6495:                                                                 $includeempty,$showdomdesc,'',$trusted,$untrusted,$readonly)
1.241     raeburn  6496:                            .'<input type="hidden" name="selfenroll_types_total" value="'.$num.'" />'
                   6497:                            .'</td>'.&Apache::loncommon::end_data_table_row()
                   6498:                            .&Apache::loncommon::end_data_table();
1.237     raeburn  6499:             } elsif ($item eq 'registered') {
                   6500:                 my ($regon,$regoff);
1.398     raeburn  6501:                 my $registered;
                   6502:                 if (ref($currsettings) eq 'HASH') {
                   6503:                     $registered = $currsettings->{'selfenroll_registered'};
                   6504:                 }
1.400     raeburn  6505:                 if ($noedit{$item}) {
                   6506:                     if ($registered) {
                   6507:                         $output .= &mt('Must be registered in course');
                   6508:                     } else {
                   6509:                         $output .= &mt('No requirement');
                   6510:                     }
                   6511:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6512:                     next;
                   6513:                 }
1.398     raeburn  6514:                 if ($registered) {
1.237     raeburn  6515:                     $regon = ' checked="checked" ';
1.419     raeburn  6516:                     $regoff = '';
1.237     raeburn  6517:                 } else {
1.419     raeburn  6518:                     $regon = '';
1.237     raeburn  6519:                     $regoff = ' checked="checked" ';
                   6520:                 }
                   6521:                 $output .= '<label>'.
1.419     raeburn  6522:                            '<input type="radio" name="selfenroll_registered" value="1"'.$regon.$disabled.' />'.
1.244     bisitz   6523:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
1.419     raeburn  6524:                            '<input type="radio" name="selfenroll_registered" value="0"'.$regoff.$disabled.' />'.
1.244     bisitz   6525:                            &mt('No').'</label>';
1.237     raeburn  6526:             } elsif ($item eq 'enroll_dates') {
1.398     raeburn  6527:                 my ($starttime,$endtime);
                   6528:                 if (ref($currsettings) eq 'HASH') {
                   6529:                     $starttime = $currsettings->{'selfenroll_start_date'};
                   6530:                     $endtime = $currsettings->{'selfenroll_end_date'};
                   6531:                     if ($starttime eq '') {
                   6532:                         $starttime = $currsettings->{'default_enrollment_start_date'};
                   6533:                     }
                   6534:                     if ($endtime eq '') {
                   6535:                         $endtime = $currsettings->{'default_enrollment_end_date'};
                   6536:                     }
1.237     raeburn  6537:                 }
1.400     raeburn  6538:                 if ($noedit{$item}) {
                   6539:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
                   6540:                                                           &Apache::lonlocal::locallocaltime($endtime));
                   6541:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6542:                     next;
                   6543:                 }
1.237     raeburn  6544:                 my $startform =
                   6545:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
1.418     raeburn  6546:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6547:                 my $endform =
                   6548:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
1.418     raeburn  6549:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6550:                 $output .= &selfenroll_date_forms($startform,$endform);
                   6551:             } elsif ($item eq 'access_dates') {
1.398     raeburn  6552:                 my ($starttime,$endtime);
                   6553:                 if (ref($currsettings) eq 'HASH') {
                   6554:                     $starttime = $currsettings->{'selfenroll_start_access'};
                   6555:                     $endtime = $currsettings->{'selfenroll_end_access'};
                   6556:                     if ($starttime eq '') {
                   6557:                         $starttime = $currsettings->{'default_enrollment_start_date'};
                   6558:                     }
                   6559:                     if ($endtime eq '') {
                   6560:                         $endtime = $currsettings->{'default_enrollment_end_date'};
                   6561:                     }
1.237     raeburn  6562:                 }
1.400     raeburn  6563:                 if ($noedit{$item}) {
                   6564:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
                   6565:                                                           &Apache::lonlocal::locallocaltime($endtime));
                   6566:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6567:                     next;
                   6568:                 }
1.237     raeburn  6569:                 my $startform =
                   6570:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
1.418     raeburn  6571:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6572:                 my $endform =
                   6573:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
1.418     raeburn  6574:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6575:                 $output .= &selfenroll_date_forms($startform,$endform);
                   6576:             } elsif ($item eq 'section') {
1.398     raeburn  6577:                 my $currsec;
                   6578:                 if (ref($currsettings) eq 'HASH') {
                   6579:                     $currsec = $currsettings->{'selfenroll_section'};
                   6580:                 }
1.237     raeburn  6581:                 my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   6582:                 my $newsecval;
                   6583:                 if ($currsec ne 'none' && $currsec ne '') {
                   6584:                     if (!defined($sections_count{$currsec})) {
                   6585:                         $newsecval = $currsec;
                   6586:                     }
                   6587:                 }
1.400     raeburn  6588:                 if ($noedit{$item}) {
                   6589:                     if ($currsec ne '') {
                   6590:                         $output .= $currsec;
                   6591:                     } else {
                   6592:                         $output .= &mt('No specific section');
                   6593:                     }
                   6594:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6595:                     next;
                   6596:                 }
1.237     raeburn  6597:                 my $sections_select = 
1.418     raeburn  6598:                     &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec,$disabled);
1.237     raeburn  6599:                 $output .= '<table class="LC_createuser">'."\n".
                   6600:                            '<tr class="LC_section_row">'."\n".
                   6601:                            '<td align="center">'.&mt('Existing sections')."\n".
                   6602:                            '<br />'.$sections_select.'</td><td align="center">'.
                   6603:                            &mt('New section').'<br />'."\n".
1.418     raeburn  6604:                            '<input type="text" name="newsec" size="15" value="'.$newsecval.'"'.$disabled.' />'."\n".
1.237     raeburn  6605:                            '<input type="hidden" name="sections" value="" />'."\n".
                   6606:                            '</td></tr></table>'."\n";
1.276     raeburn  6607:             } elsif ($item eq 'approval') {
1.398     raeburn  6608:                 my ($currnotified,$currapproval,%appchecked);
                   6609:                 my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
1.430     raeburn  6610:                 if (ref($currsettings) eq 'HASH') {
1.398     raeburn  6611:                     $currnotified = $currsettings->{'selfenroll_notifylist'};
                   6612:                     $currapproval = $currsettings->{'selfenroll_approval'};
                   6613:                 }
                   6614:                 if ($currapproval !~ /^[012]$/) {
                   6615:                     $currapproval = 0;
                   6616:                 }
1.400     raeburn  6617:                 if ($noedit{$item}) {
                   6618:                     $output .=  $selfdescs{'approval'}{$currapproval}.
                   6619:                                 '<br />'.&mt('(Set by Domain Coordinator)');
                   6620:                     next;
                   6621:                 }
1.398     raeburn  6622:                 $appchecked{$currapproval} = ' checked="checked"';
                   6623:                 for my $i (0..2) {
                   6624:                     $output .= '<label>'.
                   6625:                                '<input type="radio" name="selfenroll_approval" value="'.$i.'"'.
1.418     raeburn  6626:                                $appchecked{$i}.' onclick="toggleNotify();"'.$disabled.' />'.
                   6627:                                $selfdescs{'approval'}{$i}.'</label>'.('&nbsp;'x2);
1.276     raeburn  6628:                 }
                   6629:                 my %advhash = &Apache::lonnet::get_course_adv_roles($cid,1);
                   6630:                 my (@ccs,%notified);
1.322     raeburn  6631:                 my $ccrole = 'cc';
                   6632:                 if ($crstype eq 'Community') {
                   6633:                     $ccrole = 'co';
                   6634:                 }
                   6635:                 if ($advhash{$ccrole}) {
                   6636:                     @ccs = split(/,/,$advhash{$ccrole});
1.276     raeburn  6637:                 }
                   6638:                 if ($currnotified) {
                   6639:                     foreach my $current (split(/,/,$currnotified)) {
                   6640:                         $notified{$current} = 1;
                   6641:                         if (!grep(/^\Q$current\E$/,@ccs)) {
                   6642:                             push(@ccs,$current);
                   6643:                         }
                   6644:                     }
                   6645:                 }
                   6646:                 if (@ccs) {
1.398     raeburn  6647:                     my $style;
                   6648:                     unless ($currapproval) {
                   6649:                         $style = ' style="display: none;"'; 
                   6650:                     }
                   6651:                     $output .= '<br /><div id="notified"'.$style.'>'.
                   6652:                                &mt('Personnel to be notified when an enrollment request needs approval, or has been approved:').'&nbsp;'.
                   6653:                                &Apache::loncommon::start_data_table().
1.276     raeburn  6654:                                &Apache::loncommon::start_data_table_row();
                   6655:                     my $count = 0;
                   6656:                     my $numcols = 4;
                   6657:                     foreach my $cc (sort(@ccs)) {
                   6658:                         my $notifyon;
                   6659:                         my ($ccuname,$ccudom) = split(/:/,$cc);
                   6660:                         if ($notified{$cc}) {
                   6661:                             $notifyon = ' checked="checked" ';
                   6662:                         }
                   6663:                         if ($count && !$count%$numcols) {
                   6664:                             $output .= &Apache::loncommon::end_data_table_row().
                   6665:                                        &Apache::loncommon::start_data_table_row()
                   6666:                         }
                   6667:                         $output .= '<td><span class="LC_nobreak"><label>'.
1.418     raeburn  6668:                                    '<input type="checkbox" name="selfenroll_notify"'.$notifyon.' value="'.$cc.'"'.$disabled.' />'.
1.276     raeburn  6669:                                    &Apache::loncommon::plainname($ccuname,$ccudom).
                   6670:                                    '</label></span></td>';
1.343     raeburn  6671:                         $count ++;
1.276     raeburn  6672:                     }
                   6673:                     my $rem = $count%$numcols;
                   6674:                     if ($rem) {
                   6675:                         my $emptycols = $numcols - $rem;
                   6676:                         for (my $i=0; $i<$emptycols; $i++) { 
                   6677:                             $output .= '<td>&nbsp;</td>';
                   6678:                         }
                   6679:                     }
                   6680:                     $output .= &Apache::loncommon::end_data_table_row().
1.398     raeburn  6681:                                &Apache::loncommon::end_data_table().
                   6682:                                '</div>';
1.276     raeburn  6683:                 }
                   6684:             } elsif ($item eq 'limit') {
1.398     raeburn  6685:                 my ($crslimit,$selflimit,$nolimit,$currlim,$currcap);
                   6686:                 if (ref($currsettings) eq 'HASH') {
                   6687:                     $currlim = $currsettings->{'selfenroll_limit'};
                   6688:                     $currcap = $currsettings->{'selfenroll_cap'};
                   6689:                 }
1.400     raeburn  6690:                 if ($noedit{$item}) {
                   6691:                     if (($currlim eq 'allstudents') || ($currlim eq 'selfenrolled')) {
                   6692:                         if ($currlim eq 'allstudents') {
                   6693:                             $output .= &mt('Limit by total students');
                   6694:                         } elsif ($currlim eq 'selfenrolled') {
                   6695:                             $output .= &mt('Limit by total self-enrolled students');
                   6696:                         }
                   6697:                         $output .= ' '.&mt('Maximum: [_1]',$currcap).
                   6698:                                    '<br />'.&mt('(Set by Domain Coordinator)');
                   6699:                     } else {
                   6700:                         $output .= &mt('No limit').'<br />'.&mt('(Set by Domain Coordinator)');
                   6701:                     }
                   6702:                     next;
                   6703:                 }
1.276     raeburn  6704:                 if ($currlim eq 'allstudents') {
                   6705:                     $crslimit = ' checked="checked" ';
                   6706:                     $selflimit = ' ';
                   6707:                     $nolimit = ' ';
                   6708:                 } elsif ($currlim eq 'selfenrolled') {
                   6709:                     $crslimit = ' ';
                   6710:                     $selflimit = ' checked="checked" ';
                   6711:                     $nolimit = ' '; 
                   6712:                 } else {
                   6713:                     $crslimit = ' ';
                   6714:                     $selflimit = ' ';
1.398     raeburn  6715:                     $nolimit = ' checked="checked" ';
1.276     raeburn  6716:                 }
                   6717:                 $output .= '<table><tr><td><label>'.
1.418     raeburn  6718:                            '<input type="radio" name="selfenroll_limit" value="none"'.$nolimit.$disabled.'/>'.
1.276     raeburn  6719:                            &mt('No limit').'</label></td><td><label>'.
1.418     raeburn  6720:                            '<input type="radio" name="selfenroll_limit" value="allstudents"'.$crslimit.$disabled.'/>'.
1.276     raeburn  6721:                            &mt('Limit by total students').'</label></td><td><label>'.
1.418     raeburn  6722:                            '<input type="radio" name="selfenroll_limit" value="selfenrolled"'.$selflimit.$disabled.'/>'.
1.276     raeburn  6723:                            &mt('Limit by total self-enrolled students').
                   6724:                            '</td></tr><tr>'.
                   6725:                            '<td>&nbsp;</td><td colspan="2"><span class="LC_nobreak">'.
                   6726:                            ('&nbsp;'x3).&mt('Maximum number allowed: ').
1.418     raeburn  6727:                            '<input type="text" name="selfenroll_cap" size = "5" value="'.$currcap.'"'.$disabled.' /></td></tr></table>';
1.237     raeburn  6728:             }
                   6729:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   6730:         }
                   6731:     }
1.418     raeburn  6732:     $output .= &Apache::lonhtmlcommon::end_pick_box().'<br />';
                   6733:     unless ($readonly) {
                   6734:         $output .= '<input type="button" name="selfenrollconf" value="'
                   6735:                    .&mt('Save').'" onclick="validate_types(this.form);" />';
                   6736:     }
                   6737:     $output .= '<input type="hidden" name="action" value="selfenroll" />'
                   6738:               .'<input type="hidden" name="state" value="done" />'."\n"
                   6739:               .$additional.'</form>';
1.237     raeburn  6740:     $r->print($output);
                   6741:     return;
                   6742: }
                   6743: 
1.400     raeburn  6744: sub get_noedit_fields {
                   6745:     my ($cdom,$cnum,$crstype,$row) = @_;
                   6746:     my %noedit;
                   6747:     if (ref($row) eq 'ARRAY') {
                   6748:         my %settings = &Apache::lonnet::get('environment',['internal.coursecode','internal.textbook',
                   6749:                                                            'internal.selfenrollmgrdc',
                   6750:                                                            'internal.selfenrollmgrcc'],$cdom,$cnum);
                   6751:         my $type = &Apache::lonuserutils::get_extended_type($cdom,$cnum,$crstype,\%settings);
                   6752:         my (%specific_managebydc,%specific_managebycc,%default_managebydc);
                   6753:         map { $specific_managebydc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrdc'}));
                   6754:         map { $specific_managebycc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrcc'}));
                   6755:         my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
                   6756:         map { $default_managebydc{$_} = 1; } (split(/,/,$domdefaults{$type.'selfenrolladmdc'}));
                   6757: 
                   6758:         foreach my $item (@{$row}) {
                   6759:             next if ($specific_managebycc{$item});
                   6760:             if (($specific_managebydc{$item}) || ($default_managebydc{$item})) {
                   6761:                 $noedit{$item} = 1;
                   6762:             }
                   6763:         }
                   6764:     }
                   6765:     return %noedit;
                   6766: } 
                   6767: 
                   6768: sub visible_in_stdcat {
                   6769:     my ($cdom,$cnum,$domconf) = @_;
                   6770:     my ($cathash,%settable,@vismsgs,$cansetvis,$visible);
                   6771:     unless (ref($domconf) eq 'HASH') {
                   6772:         return ($visible,$cansetvis,\@vismsgs);
                   6773:     }
                   6774:     if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6775:         if ($domconf->{'coursecategories'}{'togglecats'} eq 'crs') {
1.256     raeburn  6776:             $settable{'togglecats'} = 1;
                   6777:         }
1.400     raeburn  6778:         if ($domconf->{'coursecategories'}{'categorize'} eq 'crs') {
1.256     raeburn  6779:             $settable{'categorize'} = 1;
                   6780:         }
1.400     raeburn  6781:         $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6782:     }
1.260     raeburn  6783:     if ($settable{'togglecats'} && $settable{'categorize'}) {
1.256     raeburn  6784:         $cansetvis = &mt('You are able to both assign a course category and choose to exclude this course from the catalog.');   
                   6785:     } elsif ($settable{'togglecats'}) {
                   6786:         $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  6787:     } elsif ($settable{'categorize'}) {
1.256     raeburn  6788:         $cansetvis = &mt('You may assign a course category, but only a Domain Coordinator may choose to exclude this course from the catalog.');  
                   6789:     } else {
                   6790:         $cansetvis = &mt('Only a Domain Coordinator may assign a course category or choose to exclude this course from the catalog.'); 
                   6791:     }
                   6792:      
                   6793:     my %currsettings =
                   6794:         &Apache::lonnet::get('environment',['hidefromcat','categories','internal.coursecode'],
                   6795:                              $cdom,$cnum);
1.400     raeburn  6796:     $visible = 0;
1.256     raeburn  6797:     if ($currsettings{'internal.coursecode'} ne '') {
1.400     raeburn  6798:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6799:             $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6800:             if (ref($cathash) eq 'HASH') {
                   6801:                 if ($cathash->{'instcode::0'} eq '') {
                   6802:                     push(@vismsgs,'dc_addinst'); 
                   6803:                 } else {
                   6804:                     $visible = 1;
                   6805:                 }
                   6806:             } else {
                   6807:                 $visible = 1;
                   6808:             }
                   6809:         } else {
                   6810:             $visible = 1;
                   6811:         }
                   6812:     } else {
                   6813:         if (ref($cathash) eq 'HASH') {
                   6814:             if ($cathash->{'instcode::0'} ne '') {
                   6815:                 push(@vismsgs,'dc_instcode');
                   6816:             }
                   6817:         } else {
                   6818:             push(@vismsgs,'dc_instcode');
                   6819:         }
                   6820:     }
                   6821:     if ($currsettings{'categories'} ne '') {
                   6822:         my $cathash;
1.400     raeburn  6823:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6824:             $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6825:             if (ref($cathash) eq 'HASH') {
                   6826:                 if (keys(%{$cathash}) == 0) {
                   6827:                     push(@vismsgs,'dc_catalog');
                   6828:                 } elsif ((keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} ne '')) {
                   6829:                     push(@vismsgs,'dc_categories');
                   6830:                 } else {
                   6831:                     my @currcategories = split('&',$currsettings{'categories'});
                   6832:                     my $matched = 0;
                   6833:                     foreach my $cat (@currcategories) {
                   6834:                         if ($cathash->{$cat} ne '') {
                   6835:                             $visible = 1;
                   6836:                             $matched = 1;
                   6837:                             last;
                   6838:                         }
                   6839:                     }
                   6840:                     if (!$matched) {
1.260     raeburn  6841:                         if ($settable{'categorize'}) { 
1.256     raeburn  6842:                             push(@vismsgs,'chgcat');
                   6843:                         } else {
                   6844:                             push(@vismsgs,'dc_chgcat');
                   6845:                         }
                   6846:                     }
                   6847:                 }
                   6848:             }
                   6849:         }
                   6850:     } else {
                   6851:         if (ref($cathash) eq 'HASH') {
                   6852:             if ((keys(%{$cathash}) > 1) || 
                   6853:                 (keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} eq '')) {
1.260     raeburn  6854:                 if ($settable{'categorize'}) {
1.256     raeburn  6855:                     push(@vismsgs,'addcat');
                   6856:                 } else {
                   6857:                     push(@vismsgs,'dc_addcat');
                   6858:                 }
                   6859:             }
                   6860:         }
                   6861:     }
                   6862:     if ($currsettings{'hidefromcat'} eq 'yes') {
                   6863:         $visible = 0;
                   6864:         if ($settable{'togglecats'}) {
                   6865:             unshift(@vismsgs,'unhide');
                   6866:         } else {
                   6867:             unshift(@vismsgs,'dc_unhide')
                   6868:         }
                   6869:     }
1.400     raeburn  6870:     return ($visible,$cansetvis,\@vismsgs);
                   6871: }
                   6872: 
                   6873: sub cat_visibility {
                   6874:     my %visactions = &Apache::lonlocal::texthash(
                   6875:                    vis => 'This course/community currently appears in the Course/Community Catalog for this domain.',
                   6876:                    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.',
                   6877:                    miss => 'This course/community does not currently appear in the Course/Community Catalog for this domain.',
                   6878:                    none => 'Display of a course catalog is disabled for this domain.',
                   6879:                    yous => 'You should remedy this if you plan to allow self-enrollment, otherwise students will have difficulty finding this course.',
                   6880:                    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.',
                   6881:                    make => 'Make any changes to self-enrollment settings below, click "Save", then take action to include the course in the Catalog:',
                   6882:                    take => 'Take the following action to ensure the course appears in the Catalog:',
                   6883:                    dc_chgconf => 'Ask a domain coordinator to change the Catalog type for this domain.',
                   6884:                    dc_setcode => 'Ask a domain coordinator to assign a six character code to the course',
                   6885:                    dc_unhide  => 'Ask a domain coordinator to change the "Exclude from course catalog" setting.',
                   6886:                    dc_addinst => 'Ask a domain coordinator to enable display the catalog of "Official courses (with institutional codes)".',
                   6887:                    dc_instcode => 'Ask a domain coordinator to assign an institutional code (if this is an official course).',
                   6888:                    dc_catalog  => 'Ask a domain coordinator to enable or create at least one course category in the domain.',
                   6889:                    dc_categories => 'Ask a domain coordinator to create a hierarchy of categories and sub categories for courses in the domain.',
                   6890:                    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',
                   6891:                    dc_addcat => 'Ask a domain coordinator to assign a category to the course.',
                   6892:     );
                   6893:     $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>"');
                   6894:     $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>"');
                   6895:     $visactions{'addcat'} = &mt('Use [_1]Categorize course[_2] to assign a category to the course.','"<a href="/adm/courseprefs?phase=display&actions=courseinfo">','</a>"');
                   6896:     return \%visactions;
1.256     raeburn  6897: }
                   6898: 
1.241     raeburn  6899: sub new_selfenroll_dom_row {
                   6900:     my ($newdom,$num) = @_;
                   6901:     my $domdesc = &Apache::lonnet::domain($newdom);
                   6902:     my $output;
                   6903:     if ($domdesc ne '') {
                   6904:         $output .= &Apache::loncommon::start_data_table_row()
                   6905:                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
                   6906:                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
1.249     raeburn  6907:                    .'" value="'.$newdom.'" /></span><br />'
                   6908:                    .'<span class="LC_nobreak"><label><input type="checkbox" '
                   6909:                    .'name="selfenroll_activate" value="'.$num.'" '
                   6910:                    .'onchange="javascript:update_types('
                   6911:                    ."'selfenroll_activate','$num'".');" />'
                   6912:                    .&mt('Activate').'</label></span></td>';
1.241     raeburn  6913:         my @currinsttypes;
                   6914:         $output .= '<td>'.&mt('User types:').'<br />'
                   6915:                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
                   6916:                    .&Apache::loncommon::end_data_table_row();
                   6917:     }
                   6918:     return $output;
                   6919: }
                   6920: 
                   6921: sub selfenroll_inst_types {
1.418     raeburn  6922:     my ($num,$currdom,$currinsttypes,$readonly) = @_;
1.241     raeburn  6923:     my $output;
                   6924:     my $numinrow = 4;
                   6925:     my $count = 0;
                   6926:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($currdom);
1.247     raeburn  6927:     my $othervalue = 'any';
1.418     raeburn  6928:     my $disabled;
                   6929:     if ($readonly) {
                   6930:         $disabled = ' disabled="disabled"';
                   6931:     }
1.241     raeburn  6932:     if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
1.251     raeburn  6933:         if (keys(%{$usertypes}) > 0) {
1.247     raeburn  6934:             $othervalue = 'other';
                   6935:         }
1.241     raeburn  6936:         $output .= '<table><tr>';
                   6937:         foreach my $type (@{$types}) {
                   6938:             if (($count > 0) && ($count%$numinrow == 0)) {
                   6939:                 $output .= '</tr><tr>';
                   6940:             }
                   6941:             if (defined($usertypes->{$type})) {
1.257     raeburn  6942:                 my $esc_type = &escape($type);
1.241     raeburn  6943:                 $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.
1.257     raeburn  6944:                            $esc_type.'" ';
1.241     raeburn  6945:                 if (ref($currinsttypes) eq 'ARRAY') {
                   6946:                     if (@{$currinsttypes} > 0) {
1.249     raeburn  6947:                         if (grep(/^any$/,@{$currinsttypes})) {
                   6948:                             $output .= 'checked="checked"';
1.257     raeburn  6949:                         } elsif (grep(/^\Q$esc_type\E$/,@{$currinsttypes})) {
1.241     raeburn  6950:                             $output .= 'checked="checked"';
                   6951:                         }
1.249     raeburn  6952:                     } else {
                   6953:                         $output .= 'checked="checked"';
1.241     raeburn  6954:                     }
                   6955:                 }
1.418     raeburn  6956:                 $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$usertypes->{$type}.'</label></span></td>';
1.241     raeburn  6957:             }
                   6958:             $count ++;
                   6959:         }
                   6960:         if (($count > 0) && ($count%$numinrow == 0)) {
                   6961:             $output .= '</tr><tr>';
                   6962:         }
1.249     raeburn  6963:         $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
1.241     raeburn  6964:         if (ref($currinsttypes) eq 'ARRAY') {
                   6965:             if (@{$currinsttypes} > 0) {
1.249     raeburn  6966:                 if (grep(/^any$/,@{$currinsttypes})) { 
                   6967:                     $output .= ' checked="checked"';
                   6968:                 } elsif ($othervalue eq 'other') {
                   6969:                     if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
                   6970:                         $output .= ' checked="checked"';
                   6971:                     }
1.241     raeburn  6972:                 }
1.249     raeburn  6973:             } else {
                   6974:                 $output .= ' checked="checked"';
1.241     raeburn  6975:             }
1.249     raeburn  6976:         } else {
                   6977:             $output .= ' checked="checked"';
1.241     raeburn  6978:         }
1.418     raeburn  6979:         $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$othertitle.'</label></span></td></tr></table>';
1.241     raeburn  6980:     }
                   6981:     return $output;
                   6982: }
                   6983: 
1.237     raeburn  6984: sub selfenroll_date_forms {
                   6985:     my ($startform,$endform) = @_;
                   6986:     my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
1.244     bisitz   6987:                   &Apache::lonhtmlcommon::row_title(&mt('Start date'),
1.237     raeburn  6988:                                                     'LC_oddrow_value')."\n".
                   6989:                   $startform."\n".
                   6990:                   &Apache::lonhtmlcommon::row_closure(1).
1.244     bisitz   6991:                   &Apache::lonhtmlcommon::row_title(&mt('End date'),
1.237     raeburn  6992:                                                    'LC_oddrow_value')."\n".
                   6993:                   $endform."\n".
                   6994:                   &Apache::lonhtmlcommon::row_closure(1).
                   6995:                   &Apache::lonhtmlcommon::end_pick_box();
                   6996:     return $output;
                   6997: }
                   6998: 
1.239     raeburn  6999: sub print_userchangelogs_display {
1.415     raeburn  7000:     my ($r,$context,$permission,$brcrum) = @_;
1.363     raeburn  7001:     my $formname = 'rolelog';
1.418     raeburn  7002:     my ($username,$domain,$crstype,$viewablesec,%roleslog);
1.363     raeburn  7003:     if ($context eq 'domain') {
                   7004:         $domain = $env{'request.role.domain'};
                   7005:         %roleslog=&Apache::lonnet::dump_dom('nohist_rolelog',$domain);
                   7006:     } else {
                   7007:         if ($context eq 'course') { 
                   7008:             $domain = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7009:             $username = $env{'course.'.$env{'request.course.id'}.'.num'};
                   7010:             $crstype = &Apache::loncommon::course_type();
1.418     raeburn  7011:             $viewablesec = &Apache::lonuserutils::viewable_section($permission);
1.363     raeburn  7012:             my %saveable_parameters = ('show' => 'scalar',);
                   7013:             &Apache::loncommon::store_course_settings('roles_log',
                   7014:                                                       \%saveable_parameters);
                   7015:             &Apache::loncommon::restore_course_settings('roles_log',
                   7016:                                                         \%saveable_parameters);
                   7017:         } elsif ($context eq 'author') {
                   7018:             $domain = $env{'user.domain'}; 
                   7019:             if ($env{'request.role'} =~ m{^au\./\Q$domain\E/$}) {
                   7020:                 $username = $env{'user.name'};
                   7021:             } else {
                   7022:                 undef($domain);
                   7023:             }
                   7024:         }
                   7025:         if ($domain ne '' && $username ne '') { 
                   7026:             %roleslog=&Apache::lonnet::dump('nohist_rolelog',$domain,$username);
                   7027:         }
                   7028:     }
1.239     raeburn  7029:     if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
                   7030: 
1.415     raeburn  7031:     my $helpitem;
                   7032:     if ($context eq 'course') {
                   7033:         $helpitem = 'Course_User_Logs';
1.439     raeburn  7034:     } elsif ($context eq 'domain') {
                   7035:         $helpitem = 'Domain_Role_Logs';
                   7036:     } elsif ($context eq 'author') {
                   7037:         $helpitem = 'Author_User_Logs';
1.415     raeburn  7038:     }
                   7039:     push (@{$brcrum},
                   7040:              {href => '/adm/createuser?action=changelogs',
                   7041:               text => 'User Management Logs',
                   7042:               help => $helpitem});
                   7043:     my $bread_crumbs_component = 'User Changes';
                   7044:     my $args = { bread_crumbs           => $brcrum,
                   7045:                  bread_crumbs_component => $bread_crumbs_component};
                   7046: 
                   7047:     # Create navigation javascript
                   7048:     my $jsnav = &userlogdisplay_js($formname);
                   7049: 
                   7050:     my $jscript = (<<ENDSCRIPT);
                   7051: <script type="text/javascript">
                   7052: // <![CDATA[
                   7053: $jsnav
                   7054: // ]]>
                   7055: </script>
                   7056: ENDSCRIPT
                   7057: 
                   7058:     # print page header
                   7059:     $r->print(&header($jscript,$args));
                   7060: 
1.239     raeburn  7061:     # set defaults
                   7062:     my $now = time();
                   7063:     my $defstart = $now - (7*24*3600); #7 days ago 
                   7064:     my %defaults = (
                   7065:                      page               => '1',
                   7066:                      show               => '10',
                   7067:                      role               => 'any',
                   7068:                      chgcontext         => 'any',
                   7069:                      rolelog_start_date => $defstart,
                   7070:                      rolelog_end_date   => $now,
                   7071:                    );
                   7072:     my $more_records = 0;
                   7073: 
                   7074:     # set current
                   7075:     my %curr;
                   7076:     foreach my $item ('show','page','role','chgcontext') {
                   7077:         $curr{$item} = $env{'form.'.$item};
                   7078:     }
                   7079:     my ($startdate,$enddate) = 
                   7080:         &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
                   7081:     $curr{'rolelog_start_date'} = $startdate;
                   7082:     $curr{'rolelog_end_date'} = $enddate;
                   7083:     foreach my $key (keys(%defaults)) {
                   7084:         if ($curr{$key} eq '') {
                   7085:             $curr{$key} = $defaults{$key};
                   7086:         }
                   7087:     }
1.248     raeburn  7088:     my (%whodunit,%changed,$version);
                   7089:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
1.239     raeburn  7090:     my ($minshown,$maxshown);
1.255     raeburn  7091:     $minshown = 1;
1.239     raeburn  7092:     my $count = 0;
1.415     raeburn  7093:     if ($curr{'show'} =~ /\D/) {
                   7094:         $curr{'page'} = 1;
                   7095:     } else {
1.239     raeburn  7096:         $maxshown = $curr{'page'} * $curr{'show'};
                   7097:         if ($curr{'page'} > 1) {
                   7098:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   7099:         }
                   7100:     }
1.301     bisitz   7101: 
1.327     raeburn  7102:     # Form Header
                   7103:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
1.363     raeburn  7104:               &role_display_filter($context,$formname,$domain,$username,\%curr,
                   7105:                                    $version,$crstype));
1.327     raeburn  7106: 
                   7107:     my $showntableheader = 0;
                   7108: 
                   7109:     # Table Header
                   7110:     my $tableheader = 
                   7111:         &Apache::loncommon::start_data_table_header_row()
                   7112:        .'<th>&nbsp;</th>'
                   7113:        .'<th>'.&mt('When').'</th>'
                   7114:        .'<th>'.&mt('Who made the change').'</th>'
                   7115:        .'<th>'.&mt('Changed User').'</th>'
1.363     raeburn  7116:        .'<th>'.&mt('Role').'</th>';
                   7117: 
                   7118:     if ($context eq 'course') {
                   7119:         $tableheader .= '<th>'.&mt('Section').'</th>';
                   7120:     }
                   7121:     $tableheader .=
                   7122:         '<th>'.&mt('Context').'</th>'
1.327     raeburn  7123:        .'<th>'.&mt('Start').'</th>'
                   7124:        .'<th>'.&mt('End').'</th>'
                   7125:        .&Apache::loncommon::end_data_table_header_row();
                   7126: 
                   7127:     # Display user change log data
1.239     raeburn  7128:     foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
                   7129:         next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
                   7130:                  ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
1.415     raeburn  7131:         if ($curr{'show'} !~ /\D/) {
1.239     raeburn  7132:             if ($count >= $curr{'page'} * $curr{'show'}) {
                   7133:                 $more_records = 1;
                   7134:                 last;
                   7135:             }
                   7136:         }
                   7137:         if ($curr{'role'} ne 'any') {
                   7138:             next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
                   7139:         }
                   7140:         if ($curr{'chgcontext'} ne 'any') {
                   7141:             if ($curr{'chgcontext'} eq 'selfenroll') {
                   7142:                 next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
                   7143:             } else {
                   7144:                 next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
                   7145:             }
                   7146:         }
1.418     raeburn  7147:         if (($context eq 'course') && ($viewablesec ne '')) {
1.430     raeburn  7148:             next if ($roleslog{$id}{'logentry'}{'section'} ne $viewablesec);
1.418     raeburn  7149:         }
1.239     raeburn  7150:         $count ++;
                   7151:         next if ($count < $minshown);
1.327     raeburn  7152:         unless ($showntableheader) {
1.415     raeburn  7153:             $r->print(&Apache::loncommon::start_data_table()
1.327     raeburn  7154:                      .$tableheader);
                   7155:             $r->rflush();
                   7156:             $showntableheader = 1;
                   7157:         }
1.239     raeburn  7158:         if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
                   7159:             $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
                   7160:                 &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
                   7161:         }
                   7162:         if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
                   7163:             $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
                   7164:                 &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
                   7165:         }
                   7166:         my $sec = $roleslog{$id}{'logentry'}{'section'};
                   7167:         if ($sec eq '') {
                   7168:             $sec = &mt('None');
                   7169:         }
                   7170:         my ($rolestart,$roleend);
                   7171:         if ($roleslog{$id}{'delflag'}) {
                   7172:             $rolestart = &mt('deleted');
                   7173:             $roleend = &mt('deleted');
                   7174:         } else {
                   7175:             $rolestart = $roleslog{$id}{'logentry'}{'start'};
                   7176:             $roleend = $roleslog{$id}{'logentry'}{'end'};
                   7177:             if ($rolestart eq '' || $rolestart == 0) {
                   7178:                 $rolestart = &mt('No start date'); 
                   7179:             } else {
                   7180:                 $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
                   7181:             }
                   7182:             if ($roleend eq '' || $roleend == 0) { 
                   7183:                 $roleend = &mt('No end date');
                   7184:             } else {
                   7185:                 $roleend = &Apache::lonlocal::locallocaltime($roleend);
                   7186:             }
                   7187:         }
                   7188:         my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
                   7189:         if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
                   7190:             $chgcontext = 'selfenroll';
                   7191:         }
1.363     raeburn  7192:         my %lt = &rolechg_contexts($context,$crstype);
1.239     raeburn  7193:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
                   7194:             $chgcontext = $lt{$chgcontext};
                   7195:         }
1.327     raeburn  7196:         $r->print(
1.301     bisitz   7197:             &Apache::loncommon::start_data_table_row()
                   7198:            .'<td>'.$count.'</td>'
                   7199:            .'<td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td>'
                   7200:            .'<td>'.$whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.'</td>'
                   7201:            .'<td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td>'
1.363     raeburn  7202:            .'<td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'},$crstype).'</td>');
                   7203:         if ($context eq 'course') { 
                   7204:             $r->print('<td>'.$sec.'</td>');
                   7205:         }
                   7206:         $r->print(
                   7207:             '<td>'.$chgcontext.'</td>'
1.301     bisitz   7208:            .'<td>'.$rolestart.'</td>'
                   7209:            .'<td>'.$roleend.'</td>'
1.327     raeburn  7210:            .&Apache::loncommon::end_data_table_row()."\n");
1.301     bisitz   7211:     }
                   7212: 
1.327     raeburn  7213:     if ($showntableheader) { # Table footer, if content displayed above
1.415     raeburn  7214:         $r->print(&Apache::loncommon::end_data_table().
                   7215:                   &userlogdisplay_navlinks(\%curr,$more_records));
1.327     raeburn  7216:     } else { # No content displayed above
1.301     bisitz   7217:         $r->print('<p class="LC_info">'
                   7218:                  .&mt('There are no records to display.')
                   7219:                  .'</p>'
                   7220:         );
1.239     raeburn  7221:     }
1.301     bisitz   7222: 
1.327     raeburn  7223:     # Form Footer
                   7224:     $r->print( 
                   7225:         '<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   7226:        .'<input type="hidden" name="action" value="changelogs" />'
                   7227:        .'</form>');
                   7228:     return;
                   7229: }
1.301     bisitz   7230: 
1.416     raeburn  7231: sub print_useraccesslogs_display {
                   7232:     my ($r,$uname,$udom,$permission,$brcrum) = @_;
                   7233:     my $formname = 'accesslog';
                   7234:     my $form = 'document.accesslog';
                   7235: 
                   7236: # set breadcrumbs
1.422     raeburn  7237:     my %breadcrumb_text = &singleuser_breadcrumb('','domain',$udom);
1.431     raeburn  7238:     my $prevphasestr;
                   7239:     if ($env{'form.popup'}) {
                   7240:         $brcrum = [];
                   7241:     } else {
                   7242:         push (@{$brcrum},
                   7243:             {href => "javascript:backPage($form)",
                   7244:              text => $breadcrumb_text{'search'}});
                   7245:         my @prevphases;
                   7246:         if ($env{'form.prevphases'}) {
                   7247:             @prevphases = split(/,/,$env{'form.prevphases'});
                   7248:             $prevphasestr = $env{'form.prevphases'};
                   7249:         }
                   7250:         if (($env{'form.phase'} eq 'userpicked') || (grep(/^userpicked$/,@prevphases))) {
                   7251:             push(@{$brcrum},
                   7252:                   {href => "javascript:backPage($form,'get_user_info','select')",
                   7253:                    text => $breadcrumb_text{'userpicked'}});
                   7254:             if ($env{'form.phase'} eq 'userpicked') {
                   7255:                 $prevphasestr = 'userpicked';
                   7256:             }
1.416     raeburn  7257:         }
                   7258:     }
                   7259:     push(@{$brcrum},
                   7260:              {href => '/adm/createuser?action=accesslogs',
                   7261:               text => 'User access logs',
1.424     raeburn  7262:               help => 'Domain_User_Access_Logs'});
1.416     raeburn  7263:     my $bread_crumbs_component = 'User Access Logs';
                   7264:     my $args = { bread_crumbs           => $brcrum,
                   7265:                  bread_crumbs_component => 'User Management'};
1.423     raeburn  7266:     if ($env{'form.popup'}) {
                   7267:         $args->{'no_nav_bar'} = 1;
1.431     raeburn  7268:         $args->{'bread_crumbs_nomenu'} = 1;
1.423     raeburn  7269:     }
1.416     raeburn  7270: 
1.417     raeburn  7271: # set javascript
1.416     raeburn  7272:     my ($jsback,$elements) = &crumb_utilities();
                   7273:     my $jsnav = &userlogdisplay_js($formname);
                   7274: 
                   7275:     my $jscript = (<<ENDSCRIPT);
                   7276: <script type="text/javascript">
                   7277: // <![CDATA[
                   7278: 
                   7279: $jsback
                   7280: $jsnav
                   7281: 
                   7282: // ]]>
                   7283: </script>
                   7284: 
                   7285: ENDSCRIPT
                   7286: 
1.417     raeburn  7287: # print page header
1.416     raeburn  7288:     $r->print(&header($jscript,$args));
                   7289: 
                   7290: # early out unless log data can be displayed.
                   7291:     unless ($permission->{'activity'}) {
                   7292:         $r->print('<p class="LC_warning">'
                   7293:                  .&mt('You do not have rights to display user access logs.')
1.431     raeburn  7294:                  .'</p>');
                   7295:         if ($env{'form.popup'}) {
                   7296:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   7297:         } else {
                   7298:             $r->print(&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   7299:         }
1.416     raeburn  7300:         return;
                   7301:     }
                   7302: 
                   7303:     unless ($udom eq $env{'request.role.domain'}) {
                   7304:         $r->print('<p class="LC_warning">'
                   7305:                  .&mt("User's domain must match role's domain")
                   7306:                  .'</p>'
                   7307:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
1.417     raeburn  7308:         return;
1.416     raeburn  7309:     }
                   7310: 
                   7311:     if (($uname eq '') || ($udom eq '')) {
                   7312:         $r->print('<p class="LC_warning">'
                   7313:                  .&mt('Invalid username or domain')
                   7314:                  .'</p>'
                   7315:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   7316:         return;
                   7317:     }
                   7318: 
1.437     raeburn  7319:     if (&Apache::lonnet::privileged($uname,$udom,
                   7320:                                     [$env{'request.role.domain'}],['dc','su'])) {
                   7321:         unless (&Apache::lonnet::privileged($env{'user.name'},$env{'user.domain'},
                   7322:                                             [$env{'request.role.domain'}],['dc','su'])) {
                   7323:             $r->print('<p class="LC_warning">'
                   7324:                  .&mt('You need to be a privileged user to display user access logs for [_1]',
                   7325:                       &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),
                   7326:                                                          $uname,$udom))
                   7327:                  .'</p>');
                   7328:             if ($env{'form.popup'}) {
                   7329:                 $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   7330:             } else {
                   7331:                 $r->print(&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   7332:             }
                   7333:             return;
                   7334:         }
                   7335:     }
                   7336: 
1.416     raeburn  7337: # set defaults
                   7338:     my $now = time();
                   7339:     my $defstart = $now - (7*24*3600);
                   7340:     my %defaults = (
                   7341:                      page                 => '1',
                   7342:                      show                 => '10',
                   7343:                      activity             => 'any',
                   7344:                      accesslog_start_date => $defstart,
                   7345:                      accesslog_end_date   => $now,
                   7346:                    );
                   7347:     my $more_records = 0;
                   7348: 
                   7349: # set current
                   7350:     my %curr;
                   7351:     foreach my $item ('show','page','activity') {
                   7352:         $curr{$item} = $env{'form.'.$item};
                   7353:     }
                   7354:     my ($startdate,$enddate) =
                   7355:         &Apache::lonuserutils::get_dates_from_form('accesslog_start_date','accesslog_end_date');
                   7356:     $curr{'accesslog_start_date'} = $startdate;
                   7357:     $curr{'accesslog_end_date'} = $enddate;
                   7358:     foreach my $key (keys(%defaults)) {
                   7359:         if ($curr{$key} eq '') {
                   7360:             $curr{$key} = $defaults{$key};
                   7361:         }
                   7362:     }
                   7363:     my ($minshown,$maxshown);
                   7364:     $minshown = 1;
                   7365:     my $count = 0;
                   7366:     if ($curr{'show'} =~ /\D/) {
                   7367:         $curr{'page'} = 1;
                   7368:     } else {
                   7369:         $maxshown = $curr{'page'} * $curr{'show'};
                   7370:         if ($curr{'page'} > 1) {
                   7371:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   7372:         }
                   7373:     }
                   7374: 
                   7375: # form header
                   7376:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
                   7377:               &activity_display_filter($formname,\%curr));
                   7378: 
                   7379:     my $showntableheader = 0;
                   7380:     my ($nav_script,$nav_links);
                   7381: 
                   7382: # table header
1.453     raeburn  7383:     my $heading = '<h3>'.
1.431     raeburn  7384:         &mt('User access logs for: [_1]',
1.453     raeburn  7385:             &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom)).'</h3>';
                   7386:     my $tableheader = $heading
1.431     raeburn  7387:        .&Apache::loncommon::start_data_table_header_row()
1.416     raeburn  7388:        .'<th>&nbsp;</th>'
                   7389:        .'<th>'.&mt('When').'</th>'
                   7390:        .'<th>'.&mt('HostID').'</th>'
                   7391:        .'<th>'.&mt('Event').'</th>'
                   7392:        .'<th>'.&mt('Other data').'</th>'
                   7393:        .&Apache::loncommon::end_data_table_header_row();
                   7394: 
                   7395:     my %filters=(
                   7396:         start  => $curr{'accesslog_start_date'},
                   7397:         end    => $curr{'accesslog_end_date'},
                   7398:         action => $curr{'activity'},
                   7399:     );
                   7400: 
                   7401:     my $reply = &Apache::lonnet::userlog_query($uname,$udom,%filters);
                   7402:     unless ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   7403:         my (%courses,%missing);
                   7404:         my @results = split(/\&/,$reply);
                   7405:         foreach my $item (reverse(@results)) {
                   7406:             my ($timestamp,$host,$event) = split(/:/,$item);
                   7407:             next unless ($event =~ /^(Log|Role)/);
                   7408:             if ($curr{'show'} !~ /\D/) {
                   7409:                 if ($count >= $curr{'page'} * $curr{'show'}) {
                   7410:                     $more_records = 1;
                   7411:                     last;
                   7412:                 }
                   7413:             }
                   7414:             $count ++;
                   7415:             next if ($count < $minshown);
                   7416:             unless ($showntableheader) {
                   7417:                 $r->print($nav_script
                   7418:                          .&Apache::loncommon::start_data_table()
                   7419:                          .$tableheader);
                   7420:                 $r->rflush();
                   7421:                 $showntableheader = 1;
                   7422:             }
1.418     raeburn  7423:             my ($shown,$extra);
1.437     raeburn  7424:             my ($event,$data) = split(/\s+/,&unescape($event),2);
1.416     raeburn  7425:             if ($event eq 'Role') {
                   7426:                 my ($rolecode,$extent) = split(/\./,$data,2);
                   7427:                 next if ($extent eq '');
                   7428:                 my ($crstype,$desc,$info);
1.418     raeburn  7429:                 if ($extent =~ m{^/($match_domain)/($match_courseid)(?:/(\w+)|)$}) {
                   7430:                     my ($cdom,$cnum,$sec) = ($1,$2,$3);
1.416     raeburn  7431:                     my $cid = $cdom.'_'.$cnum;
                   7432:                     if (exists($courses{$cid})) {
                   7433:                         $crstype = $courses{$cid}{'type'};
                   7434:                         $desc = $courses{$cid}{'description'};
                   7435:                     } elsif ($missing{$cid}) {
                   7436:                         $crstype = 'Course';
                   7437:                         $desc = 'Course/Community';
                   7438:                     } else {
                   7439:                         my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   7440:                         if (ref($crsinfo{$cdom.'_'.$cnum}) eq 'HASH') {
                   7441:                             $courses{$cid} = $crsinfo{$cid};
                   7442:                             $crstype = $crsinfo{$cid}{'type'};
                   7443:                             $desc = $crsinfo{$cid}{'description'};
                   7444:                         } else {
                   7445:                             $missing{$cid} = 1;
                   7446:                         }
                   7447:                     }
                   7448:                     $extra = &mt($crstype).': <a href="/public/'.$cdom.'/'.$cnum.'/syllabus">'.$desc.'</a>';
1.418     raeburn  7449:                     if ($sec ne '') {
                   7450:                        $extra .= ' ('.&mt('Section: [_1]',$sec).')';
                   7451:                     }
1.416     raeburn  7452:                 } elsif ($extent =~ m{^/($match_domain)/($match_username|$)}) {
                   7453:                     my ($dom,$name) = ($1,$2);
                   7454:                     if ($rolecode eq 'au') {
                   7455:                         $extra = '';
                   7456:                     } elsif ($rolecode =~ /^(ca|aa)$/) {
1.417     raeburn  7457:                         $extra = &mt('Authoring Space: [_1]',$name.':'.$dom);
1.416     raeburn  7458:                     } elsif ($rolecode =~ /^(li|dg|dh|dc|sc)$/) {
                   7459:                         $extra = &mt('Domain: [_1]',$dom);
                   7460:                     }
                   7461:                 }
                   7462:                 my $rolename;
                   7463:                 if ($rolecode =~ m{^cr/($match_domain)/($match_username)/(\w+)}) {
                   7464:                     my $role = $3;
1.417     raeburn  7465:                     my $owner = "($2:$1)";
1.416     raeburn  7466:                     if ($2 eq $1.'-domainconfig') {
                   7467:                         $owner = '(ad hoc)';
1.417     raeburn  7468:                     }
1.416     raeburn  7469:                     $rolename = &mt('Custom role: [_1]',$role.' '.$owner);
                   7470:                 } else {
                   7471:                     $rolename = &Apache::lonnet::plaintext($rolecode,$crstype);
                   7472:                 }
                   7473:                 $shown = &mt('Role selection: [_1]',$rolename);
                   7474:             } else {
                   7475:                 $shown = &mt($event);
1.437     raeburn  7476:                 if ($data =~ /^webdav/) {
                   7477:                     my ($path,$clientip) = split(/\s+/,$data,2);
                   7478:                     $path =~ s/^webdav//;
                   7479:                     if ($clientip ne '') {
                   7480:                         $extra = &mt('Client IP address: [_1]',$clientip);
                   7481:                     }
                   7482:                     if ($path ne '') {
                   7483:                         $shown .= ' '.&mt('(WebDAV access to [_1])',$path);
                   7484:                     }
                   7485:                 } elsif ($data ne '') {
                   7486:                     $extra = &mt('Client IP address: [_1]',$data);
1.416     raeburn  7487:                 }
                   7488:             }
                   7489:             $r->print(
                   7490:             &Apache::loncommon::start_data_table_row()
                   7491:            .'<td>'.$count.'</td>'
                   7492:            .'<td>'.&Apache::lonlocal::locallocaltime($timestamp).'</td>'
                   7493:            .'<td>'.$host.'</td>'
                   7494:            .'<td>'.$shown.'</td>'
                   7495:            .'<td>'.$extra.'</td>'
                   7496:            .&Apache::loncommon::end_data_table_row()."\n");
                   7497:         }
                   7498:     }
                   7499: 
                   7500:     if ($showntableheader) { # Table footer, if content displayed above
                   7501:         $r->print(&Apache::loncommon::end_data_table().
                   7502:                   &userlogdisplay_navlinks(\%curr,$more_records));
                   7503:     } else { # No content displayed above
1.453     raeburn  7504:         $r->print($heading.'<p class="LC_info">'
1.416     raeburn  7505:                  .&mt('There are no records to display.')
                   7506:                  .'</p>');
                   7507:     }
                   7508: 
1.423     raeburn  7509:     if ($env{'form.popup'} == 1) {
                   7510:         $r->print('<input type="hidden" name="popup" value="1" />'."\n");
                   7511:     }
                   7512: 
1.416     raeburn  7513:     # Form Footer
                   7514:     $r->print(
                   7515:         '<input type="hidden" name="currstate" value="" />'
                   7516:        .'<input type="hidden" name="accessuname" value="'.$uname.'" />'
                   7517:        .'<input type="hidden" name="accessudom" value="'.$udom.'" />'
                   7518:        .'<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   7519:        .'<input type="hidden" name="prevphases" value="'.$prevphasestr.'" />'
                   7520:        .'<input type="hidden" name="phase" value="activity" />'
                   7521:        .'<input type="hidden" name="action" value="accesslogs" />'
                   7522:        .'<input type="hidden" name="srchdomain" value="'.$udom.'" />'
                   7523:        .'<input type="hidden" name="srchby" value="'.$env{'form.srchby'}.'" />'
                   7524:        .'<input type="hidden" name="srchtype" value="'.$env{'form.srchtype'}.'" />'
                   7525:        .'<input type="hidden" name="srchterm" value="'.&HTML::Entities::encode($env{'form.srchterm'},'<>"&').'" />'
                   7526:        .'<input type="hidden" name="srchin" value="'.$env{'form.srchin'}.'" />'
                   7527:        .'</form>');
                   7528:     return;
                   7529: }
                   7530: 
                   7531: sub earlyout_accesslog_form {
                   7532:     my ($formname,$prevphasestr,$udom) = @_;
                   7533:     my $srchterm = &HTML::Entities::encode($env{'form.srchterm'},'<>"&');
                   7534:    return <<"END";
                   7535: <form action="/adm/createuser" method="post" name="$formname">
                   7536: <input type="hidden" name="currstate" value="" />
                   7537: <input type="hidden" name="prevphases" value="$prevphasestr" />
                   7538: <input type="hidden" name="phase" value="activity" />
                   7539: <input type="hidden" name="action" value="accesslogs" />
                   7540: <input type="hidden" name="srchdomain" value="$udom" />
                   7541: <input type="hidden" name="srchby" value="$env{'form.srchby'}" />
                   7542: <input type="hidden" name="srchtype" value="$env{'form.srchtype'}" />
                   7543: <input type="hidden" name="srchterm" value="$srchterm" />
                   7544: <input type="hidden" name="srchin" value="$env{'form.srchin'}" />
                   7545: </form>
                   7546: END
                   7547: }
                   7548: 
                   7549: sub activity_display_filter {
                   7550:     my ($formname,$curr) = @_;
                   7551:     my $nolink = 1;
                   7552:     my $output = '<table><tr><td valign="top">'.
                   7553:                  '<span class="LC_nobreak"><b>'.&mt('Actions/page:').'</b></span><br />'.
                   7554:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   7555:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   7556:                  '</td><td>&nbsp;&nbsp;</td>';
                   7557:     my $startform =
                   7558:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_start_date',
                   7559:                                             $curr->{'accesslog_start_date'},undef,
                   7560:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   7561:     my $endform =
                   7562:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_end_date',
                   7563:                                             $curr->{'accesslog_end_date'},undef,
                   7564:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   7565:     my %lt = &Apache::lonlocal::texthash (
                   7566:                                           activity => 'Activity',
                   7567:                                           Role     => 'Role selection',
                   7568:                                           log      => 'Log-in or Logout',
                   7569:     );
                   7570:     $output .= '<td valign="top"><b>'.&mt('Window during which actions occurred:').'</b><br />'.
                   7571:                '<table><tr><td>'.&mt('After:').
                   7572:                '</td><td>'.$startform.'</td></tr>'.
                   7573:                '<tr><td>'.&mt('Before:').'</td>'.
                   7574:                '<td>'.$endform.'</td></tr></table>'.
                   7575:                '</td>'.
                   7576:                '<td>&nbsp;&nbsp;</td>'.
                   7577:                '<td valign="top"><b>'.&mt('Activities').'</b><br />'.
                   7578:                '<select name="activity"><option value="any"';
                   7579:     if ($curr->{'activity'} eq 'any') {
                   7580:         $output .= ' selected="selected"';
                   7581:     }
                   7582:     $output .= '>'.&mt('Any').'</option>'."\n";
                   7583:     foreach my $activity ('Role','log') {
                   7584:         my $selstr = '';
                   7585:         if ($activity eq $curr->{'activity'}) {
                   7586:             $selstr = ' selected="selected"';
                   7587:         }
                   7588:         $output .= '<option value="'.$activity.'"'.$selstr.'>'.$lt{$activity}.'</option>';
                   7589:     }
                   7590:     $output .= '</select></td>'.
                   7591:                '</tr></table>';
                   7592:     # Update Display button
                   7593:     $output .= '<p>'
                   7594:               .'<input type="submit" value="'.&mt('Update Display').'" />'
1.431     raeburn  7595:               .'</p><hr />';
1.416     raeburn  7596:     return $output;
                   7597: }
                   7598: 
1.415     raeburn  7599: sub userlogdisplay_js {
                   7600:     my ($formname) = @_;
                   7601:     return <<"ENDSCRIPT";
                   7602: 
1.239     raeburn  7603: function chgPage(caller) {
                   7604:     if (caller == 'previous') {
                   7605:         document.$formname.page.value --;
                   7606:     }
                   7607:     if (caller == 'next') {
                   7608:         document.$formname.page.value ++;
                   7609:     }
1.327     raeburn  7610:     document.$formname.submit();
1.239     raeburn  7611:     return;
                   7612: }
                   7613: ENDSCRIPT
1.415     raeburn  7614: }
                   7615: 
                   7616: sub userlogdisplay_navlinks {
                   7617:     my ($curr,$more_records) = @_;
                   7618:     return unless(ref($curr) eq 'HASH');
                   7619:     # Navigation Buttons
                   7620:     my $nav_links = '<p>';
                   7621:     if (($curr->{'page'} > 1) || ($more_records)) {
                   7622:         if (($curr->{'page'} > 1) && ($curr->{'show'} !~ /\D/)) {
                   7623:             $nav_links .= '<input type="button"'
                   7624:                          .' onclick="javascript:chgPage('."'previous'".');"'
                   7625:                          .' value="'.&mt('Previous [_1] changes',$curr->{'show'})
                   7626:                          .'" /> ';
                   7627:         }
                   7628:         if ($more_records) {
                   7629:             $nav_links .= '<input type="button"'
                   7630:                          .' onclick="javascript:chgPage('."'next'".');"'
                   7631:                          .' value="'.&mt('Next [_1] changes',$curr->{'show'})
                   7632:                          .'" />';
1.301     bisitz   7633:         }
                   7634:     }
1.415     raeburn  7635:     $nav_links .= '</p>';
                   7636:     return $nav_links;
1.239     raeburn  7637: }
                   7638: 
                   7639: sub role_display_filter {
1.363     raeburn  7640:     my ($context,$formname,$cdom,$cnum,$curr,$version,$crstype) = @_;
                   7641:     my $lctype;
                   7642:     if ($context eq 'course') {
                   7643:         $lctype = lc($crstype);
                   7644:     }
1.239     raeburn  7645:     my $nolink = 1;
                   7646:     my $output = '<table><tr><td valign="top">'.
1.301     bisitz   7647:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b></span><br />'.
1.239     raeburn  7648:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   7649:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   7650:                  '</td><td>&nbsp;&nbsp;</td>';
                   7651:     my $startform =
                   7652:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
                   7653:                                             $curr->{'rolelog_start_date'},undef,
                   7654:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   7655:     my $endform =
                   7656:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
                   7657:                                             $curr->{'rolelog_end_date'},undef,
                   7658:                                             undef,undef,undef,undef,undef,undef,$nolink);
1.363     raeburn  7659:     my %lt = &rolechg_contexts($context,$crstype);
1.301     bisitz   7660:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br />'.
                   7661:                '<table><tr><td>'.&mt('After:').
                   7662:                '</td><td>'.$startform.'</td></tr>'.
                   7663:                '<tr><td>'.&mt('Before:').'</td>'.
                   7664:                '<td>'.$endform.'</td></tr></table>'.
                   7665:                '</td>'.
                   7666:                '<td>&nbsp;&nbsp;</td>'.
1.239     raeburn  7667:                '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
                   7668:                '<select name="role"><option value="any"';
                   7669:     if ($curr->{'role'} eq 'any') {
                   7670:         $output .= ' selected="selected"';
                   7671:     }
                   7672:     $output .=  '>'.&mt('Any').'</option>'."\n";
1.363     raeburn  7673:     my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
1.239     raeburn  7674:     foreach my $role (@roles) {
                   7675:         my $plrole;
                   7676:         if ($role eq 'cr') {
                   7677:             $plrole = &mt('Custom Role');
                   7678:         } else {
1.318     raeburn  7679:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.239     raeburn  7680:         }
                   7681:         my $selstr = '';
                   7682:         if ($role eq $curr->{'role'}) {
                   7683:             $selstr = ' selected="selected"';
                   7684:         }
                   7685:         $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
                   7686:     }
1.301     bisitz   7687:     $output .= '</select></td>'.
                   7688:                '<td>&nbsp;&nbsp;</td>'.
                   7689:                '<td valign="top"><b>'.
1.239     raeburn  7690:                &mt('Context:').'</b><br /><select name="chgcontext">';
1.363     raeburn  7691:     my @posscontexts;
                   7692:     if ($context eq 'course') {
1.457     raeburn  7693:         @posscontexts = ('any','automated','updatenow','createcourse','course','domain','selfenroll','requestcourses','chgtype');
1.363     raeburn  7694:     } elsif ($context eq 'domain') {
                   7695:         @posscontexts = ('any','domain','requestauthor','domconfig','server');
                   7696:     } else {
                   7697:         @posscontexts = ('any','author','domain');
1.457     raeburn  7698:     }
1.363     raeburn  7699:     foreach my $chgtype (@posscontexts) {
1.239     raeburn  7700:         my $selstr = '';
                   7701:         if ($curr->{'chgcontext'} eq $chgtype) {
1.301     bisitz   7702:             $selstr = ' selected="selected"';
1.239     raeburn  7703:         }
1.363     raeburn  7704:         if ($context eq 'course') {
1.376     raeburn  7705:             if (($chgtype eq 'automated') || ($chgtype eq 'updatenow')) {
1.363     raeburn  7706:                 next if (!&Apache::lonnet::auto_run($cnum,$cdom));
                   7707:             }
1.239     raeburn  7708:         }
                   7709:         $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
1.248     raeburn  7710:     }
1.303     bisitz   7711:     $output .= '</select></td>'
                   7712:               .'</tr></table>';
                   7713: 
                   7714:     # Update Display button
                   7715:     $output .= '<p>'
                   7716:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   7717:               .'</p>';
                   7718: 
                   7719:     # Server version info
1.363     raeburn  7720:     my $needsrev = '2.11.0';
                   7721:     if ($context eq 'course') {
                   7722:         $needsrev = '2.7.0';
                   7723:     }
                   7724:     
1.303     bisitz   7725:     $output .= '<p class="LC_info">'
                   7726:               .&mt('Only changes made from servers running LON-CAPA [_1] or later are displayed.'
1.363     raeburn  7727:                   ,$needsrev);
1.248     raeburn  7728:     if ($version) {
1.303     bisitz   7729:         $output .= ' '.&mt('This LON-CAPA server is version [_1]',$version);
                   7730:     }
                   7731:     $output .= '</p><hr />';
1.239     raeburn  7732:     return $output;
                   7733: }
                   7734: 
                   7735: sub rolechg_contexts {
1.363     raeburn  7736:     my ($context,$crstype) = @_;
                   7737:     my %lt;
                   7738:     if ($context eq 'course') {
                   7739:         %lt = &Apache::lonlocal::texthash (
1.239     raeburn  7740:                                              any          => 'Any',
1.376     raeburn  7741:                                              automated    => 'Automated Enrollment',
1.457     raeburn  7742:                                              chgtype      => 'Enrollment Type/Lock Change',
1.239     raeburn  7743:                                              updatenow    => 'Roster Update',
                   7744:                                              createcourse => 'Course Creation',
                   7745:                                              course       => 'User Management in course',
                   7746:                                              domain       => 'User Management in domain',
1.313     raeburn  7747:                                              selfenroll   => 'Self-enrolled',
1.318     raeburn  7748:                                              requestcourses => 'Course Request',
1.239     raeburn  7749:                                          );
1.363     raeburn  7750:         if ($crstype eq 'Community') {
                   7751:             $lt{'createcourse'} = &mt('Community Creation');
                   7752:             $lt{'course'} = &mt('User Management in community');
                   7753:             $lt{'requestcourses'} = &mt('Community Request');
                   7754:         }
                   7755:     } elsif ($context eq 'domain') {
                   7756:         %lt = &Apache::lonlocal::texthash (
                   7757:                                              any           => 'Any',
                   7758:                                              domain        => 'User Management in domain',
                   7759:                                              requestauthor => 'Authoring Request',
                   7760:                                              server        => 'Command line script (DC role)',
                   7761:                                              domconfig     => 'Self-enrolled',
                   7762:                                          );
                   7763:     } else {
                   7764:         %lt = &Apache::lonlocal::texthash (
                   7765:                                              any    => 'Any',
                   7766:                                              domain => 'User Management in domain',
                   7767:                                              author => 'User Management by author',
                   7768:                                          );
                   7769:     } 
1.239     raeburn  7770:     return %lt;
                   7771: }
                   7772: 
1.428     raeburn  7773: sub print_helpdeskaccess_display {
                   7774:     my ($r,$permission,$brcrum) = @_;
                   7775:     my $formname = 'helpdeskaccess';
                   7776:     my $helpitem = 'Course_Helpdesk_Access';
                   7777:     push (@{$brcrum},
                   7778:              {href => '/adm/createuser?action=helpdesk',
                   7779:               text => 'Helpdesk Access',
                   7780:               help => $helpitem});
                   7781:     my $bread_crumbs_component = 'Helpdesk Staff Access';
                   7782:     my $args = { bread_crumbs           => $brcrum,
                   7783:                  bread_crumbs_component => $bread_crumbs_component};
                   7784: 
                   7785:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7786:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   7787:     my $confname = $cdom.'-domainconfig';
                   7788:     my $crstype = &Apache::loncommon::course_type();
                   7789: 
1.434     raeburn  7790:     my @accesstypes = ('all','dh','da','none');
1.428     raeburn  7791:     my ($numstatustypes,@jsarray);
                   7792:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($cdom);
                   7793:     if (ref($types) eq 'ARRAY') {
1.430     raeburn  7794:         if (@{$types} > 0) {
1.428     raeburn  7795:             $numstatustypes = scalar(@{$types});
                   7796:             push(@accesstypes,'status');
                   7797:             @jsarray = ('bystatus');
                   7798:         }
                   7799:     }
                   7800:     my %customroles = &get_domain_customroles($cdom,$confname);
1.432     raeburn  7801:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($cdom,['dh','da']);
1.428     raeburn  7802:     if (keys(%domhelpdesk)) {
                   7803:        push(@accesstypes,('inc','exc'));
                   7804:        push(@jsarray,('notinc','notexc'));
                   7805:     }
                   7806:     push(@jsarray,'privs');
                   7807:     my $hiddenstr = join("','",@jsarray);
                   7808:     my $rolestr = join("','",sort(keys(%customroles)));
                   7809: 
                   7810:     my $jscript;
                   7811:     my (%settings,%overridden);
                   7812:     if (keys(%customroles)) {
                   7813:         &get_adhocrole_settings($env{'request.course.id'},\@accesstypes,
                   7814:                                 $types,\%customroles,\%settings,\%overridden);
                   7815:         my %jsfull=();
                   7816:         my %jslevels= (
                   7817:                      course => {},
                   7818:                      domain => {},
                   7819:                      system => {},
                   7820:                     );
                   7821:         my %jslevelscurrent=(
                   7822:                            course => {},
                   7823:                            domain => {},
                   7824:                            system => {},
                   7825:                           );
                   7826:         my (%privs,%jsprivs);
                   7827:         &Apache::lonuserutils::custom_role_privs(\%privs,\%jsfull,\%jslevels,\%jslevelscurrent);
                   7828:         foreach my $priv (keys(%jsfull)) {
                   7829:             if ($jslevels{'course'}{$priv}) {
                   7830:                 $jsprivs{$priv} = 1;
                   7831:             }
                   7832:         }
                   7833:         my (%elements,%stored);
                   7834:         foreach my $role (keys(%customroles)) {
                   7835:             $elements{$role.'_access'} = 'radio';
                   7836:             $elements{$role.'_incrs'} = 'radio';
                   7837:             if ($numstatustypes) {
                   7838:                 $elements{$role.'_status'} = 'checkbox';
                   7839:             }
                   7840:             if (keys(%domhelpdesk) > 0) {
                   7841:                 $elements{$role.'_staff_inc'} = 'checkbox';
                   7842:                 $elements{$role.'_staff_exc'} = 'checkbox';
                   7843:             }
1.430     raeburn  7844:             $elements{$role.'_override'} = 'checkbox';
1.428     raeburn  7845:             if (ref($settings{$role}) eq 'HASH') {
                   7846:                 if ($settings{$role}{'access'} ne '') {
                   7847:                     my $curraccess = $settings{$role}{'access'};
                   7848:                     $stored{$role.'_access'} = $curraccess;
                   7849:                     $stored{$role.'_incrs'} = 1;
                   7850:                     if ($curraccess eq 'status') {
                   7851:                         if (ref($settings{$role}{'status'}) eq 'ARRAY') {
                   7852:                             $stored{$role.'_status'} = $settings{$role}{'status'};
                   7853:                         }
                   7854:                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   7855:                         if (ref($settings{$role}{$curraccess}) eq 'ARRAY') {
                   7856:                             $stored{$role.'_staff_'.$curraccess} = $settings{$role}{$curraccess};
                   7857:                         }
                   7858:                     }
                   7859:                 } else {
                   7860:                     $stored{$role.'_incrs'} = 0;
                   7861:                 }
                   7862:                 $stored{$role.'_override'} = [];
                   7863:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.adhocpriv.'.$role}) {
                   7864:                     if (ref($settings{$role}{'off'}) eq 'ARRAY') {
                   7865:                         foreach my $priv (@{$settings{$role}{'off'}}) {
                   7866:                             push(@{$stored{$role.'_override'}},$priv);
                   7867:                         }
                   7868:                     }
                   7869:                     if (ref($settings{$role}{'on'}) eq 'ARRAY') {
                   7870:                         foreach my $priv (@{$settings{$role}{'on'}}) {
                   7871:                             unless (grep(/^$priv$/,@{$stored{$role.'_override'}})) {
                   7872:                                 push(@{$stored{$role.'_override'}},$priv);
                   7873:                             }
                   7874:                         }
                   7875:                     }
                   7876:                 }
                   7877:             } else {
                   7878:                 $stored{$role.'_incrs'} = 0;
                   7879:             }
                   7880:         }
                   7881:         $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements,\%stored);
                   7882:     }
                   7883: 
                   7884:     my $js = <<"ENDJS";
                   7885: <script type="text/javascript">
                   7886: // <![CDATA[
                   7887: $jscript;
                   7888: 
                   7889: function switchRoleTab(caller,role) {
                   7890:     if (document.getElementById(role+'_maindiv')) {
                   7891:         if (caller.id != 'LC_current_minitab') {
                   7892:             if (document.getElementById('LC_current_minitab')) {
                   7893:                 document.getElementById('LC_current_minitab').id=null;
                   7894:             }
                   7895:             var roledivs = Array('$rolestr');
                   7896:             if (roledivs.length > 0) {
                   7897:                 for (var i=0; i<roledivs.length; i++) {
                   7898:                     if (document.getElementById(roledivs[i]+'_maindiv')) {
                   7899:                         document.getElementById(roledivs[i]+'_maindiv').style.display='none';
                   7900:                     }
                   7901:                 }
                   7902:             }
                   7903:             caller.id = 'LC_current_minitab';
                   7904:             document.getElementById(role+'_maindiv').style.display='block';
                   7905:         }
                   7906:     }
                   7907:     return false;
1.430     raeburn  7908: }
1.428     raeburn  7909: 
                   7910: function helpdeskAccess(role) {
                   7911:     var curraccess = null;
                   7912:     if (document.$formname.elements[role+'_access'].length) {
                   7913:         for (var i=0; i<document.$formname.elements[role+'_access'].length; i++) {
                   7914:             if (document.$formname.elements[role+'_access'][i].checked) {
                   7915:                 curraccess = document.$formname.elements[role+'_access'][i].value;
                   7916:             }
                   7917:         }
                   7918:     }
                   7919:     var shown = Array();
                   7920:     var hidden = Array();
                   7921:     if (curraccess == 'none') {
1.430     raeburn  7922:         hidden = Array ('$hiddenstr');
1.428     raeburn  7923:     } else {
                   7924:         if (curraccess == 'status') {
1.430     raeburn  7925:             shown = Array ('bystatus','privs');
                   7926:             hidden = Array ('notinc','notexc');
1.428     raeburn  7927:         } else {
                   7928:             if (curraccess == 'exc') {
                   7929:                 shown = Array ('notexc','privs');
                   7930:                 hidden = Array ('notinc','bystatus');
                   7931:             }
                   7932:             if (curraccess == 'inc') {
                   7933:                 shown = Array ('notinc','privs');
                   7934:                 hidden = Array ('notexc','bystatus');
                   7935:             }
                   7936:             if (curraccess == 'all') {
                   7937:                 shown = Array ('privs');
                   7938:                 hidden = Array ('notinc','notexc','bystatus');
                   7939:             }
                   7940:         }
                   7941:     }
                   7942:     if (hidden.length > 0) {
                   7943:         for (var i=0; i<hidden.length; i++) {
                   7944:             if (document.getElementById(role+'_'+hidden[i])) {
1.430     raeburn  7945:                 document.getElementById(role+'_'+hidden[i]).style.display = 'none';
1.428     raeburn  7946:             }
                   7947:         }
                   7948:     }
                   7949:     if (shown.length > 0) {
                   7950:         for (var i=0; i<shown.length; i++) {
                   7951:             if (document.getElementById(role+'_'+shown[i])) {
                   7952:                 if (shown[i] == 'privs') {
                   7953:                     document.getElementById(role+'_'+shown[i]).style.display = 'block';
                   7954:                 } else {
                   7955:                     document.getElementById(role+'_'+shown[i]).style.display = 'inline';
                   7956:                 }
                   7957:             }
                   7958:         }
                   7959:     }
                   7960:     return;
                   7961: }
                   7962: 
                   7963: function toggleAccess(role) {
                   7964:     if ((document.getElementById(role+'_setincrs')) &&
                   7965:         (document.getElementById(role+'_setindom'))) {
                   7966:         for (var i=0; i<document.$formname.elements[role+'_incrs'].length; i++) {
                   7967:             if (document.$formname.elements[role+'_incrs'][i].checked) {
                   7968:                 if (document.$formname.elements[role+'_incrs'][i].value == 1) {
                   7969:                     document.getElementById(role+'_setindom').style.display = 'none';
1.430     raeburn  7970:                     document.getElementById(role+'_setincrs').style.display = 'block';
1.428     raeburn  7971:                 } else {
                   7972:                     document.getElementById(role+'_setincrs').style.display = 'none';
                   7973:                     document.getElementById(role+'_setindom').style.display = 'block';
                   7974:                 }
                   7975:                 break;
                   7976:             }
                   7977:         }
                   7978:     }
                   7979:     return;
                   7980: }
                   7981: 
                   7982: // ]]>
                   7983: </script>
                   7984: ENDJS
                   7985: 
                   7986:     $args->{add_entries} = {onload => "javascript:setFormElements(document.$formname)"};
                   7987: 
                   7988:     # print page header
                   7989:     $r->print(&header($js,$args));
                   7990:     # print form header
                   7991:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">');
                   7992: 
                   7993:     if (keys(%customroles)) {
                   7994:         my %lt = &Apache::lonlocal::texthash(
                   7995:                     'aco'    => 'As course owner you may override the defaults set in the domain for role usage and/or privileges.',
                   7996:                     'rou'    => 'Role usage',
                   7997:                     'whi'    => 'Which helpdesk personnel may use this role?',
                   7998:                     'udd'    => 'Use domain default',
1.433     raeburn  7999:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
1.434     raeburn  8000:                     'dh'     => 'All with domain helpdesk role',
                   8001:                     'da'     => 'All with domain helpdesk assistant role',
1.428     raeburn  8002:                     'none'   => 'None',
                   8003:                     'status' => 'Determined based on institutional status',
1.430     raeburn  8004:                     'inc'    => 'Include all, but exclude specific personnel',
1.428     raeburn  8005:                     'exc'    => 'Exclude all, but include specific personnel',
                   8006:                     'hel'    => 'Helpdesk',
                   8007:                     'rpr'    => 'Role privileges',
                   8008:                  );
                   8009:         $lt{'tfh'} = &mt("Custom [_1]ad hoc[_2] course roles available for use by the domain's helpdesk are as follows",'<i>','</i>');
                   8010:         my %domconfig = &Apache::lonnet::get_dom('configuration',['helpsettings'],$cdom);
                   8011:         my (%domcurrent,%ordered,%description,%domusage,$disabled);
                   8012:         if (ref($domconfig{'helpsettings'}) eq 'HASH') {
                   8013:             if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
                   8014:                 %domcurrent = %{$domconfig{'helpsettings'}{'adhoc'}};
                   8015:             }
                   8016:         }
                   8017:         my $count = 0;
                   8018:         foreach my $role (sort(keys(%customroles))) {
                   8019:             my ($order,$desc,$access_in_dom);
                   8020:             if (ref($domcurrent{$role}) eq 'HASH') {
                   8021:                 $order = $domcurrent{$role}{'order'};
                   8022:                 $desc = $domcurrent{$role}{'desc'};
                   8023:                 $access_in_dom = $domcurrent{$role}{'access'};
                   8024:             }
                   8025:             if ($order eq '') {
                   8026:                 $order = $count;
                   8027:             }
                   8028:             $ordered{$order} = $role;
                   8029:             if ($desc ne '') {
                   8030:                 $description{$role} = $desc;
                   8031:             } else {
                   8032:                 $description{$role}= $role;
                   8033:             }
                   8034:             $count++;
                   8035:         }
                   8036:         %domusage = &domain_adhoc_access(\%customroles,\%domcurrent,\@accesstypes,$usertypes,$othertitle);
                   8037:         my @roles_by_num = ();
                   8038:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
                   8039:             push(@roles_by_num,$ordered{$item});
1.430     raeburn  8040:         }
1.429     raeburn  8041:         $r->print('<p>'.$lt{'tfh'}.': <i>'.join('</i>, <i>',map { $description{$_}; } @roles_by_num).'</i>.');
1.428     raeburn  8042:         if ($permission->{'owner'}) {
                   8043:             $r->print('<br />'.$lt{'aco'}.'</p><p>');
                   8044:             $r->print('<input type="hidden" name="state" value="process" />'.
                   8045:                       '<input type="submit" value="'.&mt('Save changes').'" />');
                   8046:         } else {
                   8047:             if ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'}) {
                   8048:                 my ($ownername,$ownerdom) = split(/:/,$env{'course.'.$env{'request.course.id'}.'.internal.courseowner'});
                   8049:                 $r->print('<br />'.&mt('The course owner -- [_1] -- can override the default access and/or privileges for these ad hoc roles.',
                   8050:                                     &Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($ownername,$ownerdom),$ownername,$ownerdom)));
                   8051:             }
                   8052:             $disabled = ' disabled="disabled"';
                   8053:         }
                   8054:         $r->print('</p>');
                   8055: 
                   8056:         $r->print('<div id="LC_minitab_header"><ul>');
                   8057:         my $count = 0;
                   8058:         my %visibility;
                   8059:         foreach my $role (@roles_by_num) {
                   8060:             my $id;
                   8061:             if ($count == 0) {
                   8062:                 $id=' id="LC_current_minitab"';
1.430     raeburn  8063:                 $visibility{$role} = ' style="display:block"';
1.428     raeburn  8064:             } else {
                   8065:                 $visibility{$role} = ' style="display:none"';
                   8066:             }
                   8067:             $count ++;
                   8068:             $r->print('<li'.$id.'><a href="#" onclick="javascript:switchRoleTab(this.parentNode,'."'$role'".');">'.$description{$role}.'</a></li>');
                   8069:         }
                   8070:         $r->print('</ul></div>');
                   8071: 
                   8072:         foreach my $role (@roles_by_num) {
                   8073:             my %usecheck = (
                   8074:                              all => ' checked="checked"',
                   8075:                            );
                   8076:             my %displaydiv = (
                   8077:                                 status => 'none',
                   8078:                                 inc    => 'none',
                   8079:                                 exc    => 'none',
                   8080:                                 priv   => 'block',
                   8081:                              );
                   8082:             my (%selected,$overridden,$incrscheck,$indomcheck,$indomvis,$incrsvis);
1.430     raeburn  8083:             if (ref($settings{$role}) eq 'HASH') {
1.428     raeburn  8084:                 if ($settings{$role}{'access'} ne '') {
                   8085:                     $indomvis = ' style="display:none"';
                   8086:                     $incrsvis = ' style="display:block"';
1.430     raeburn  8087:                     $incrscheck = ' checked="checked"';
1.428     raeburn  8088:                     if ($settings{$role}{'access'} ne 'all') {
                   8089:                         $usecheck{$settings{$role}{'access'}} = $usecheck{'all'};
                   8090:                         delete($usecheck{'all'});
                   8091:                         if ($settings{$role}{'access'} eq 'status') {
                   8092:                             my $access = 'status';
                   8093:                             $displaydiv{$access} = 'inline';
                   8094:                             if (ref($settings{$role}{$access}) eq 'ARRAY') {
                   8095:                                 $selected{$access} = $settings{$role}{$access};
                   8096:                             }
                   8097:                         } elsif ($settings{$role}{'access'} =~ /^(inc|exc)$/) {
                   8098:                             my $access = $1;
                   8099:                             $displaydiv{$access} = 'inline';
                   8100:                             if (ref($settings{$role}{$access}) eq 'ARRAY') {
                   8101:                                 $selected{$access} = $settings{$role}{$access};
                   8102:                             }
                   8103:                         } elsif ($settings{$role}{'access'} eq 'none') {
                   8104:                             $displaydiv{'priv'} = 'none';
                   8105:                         }
                   8106:                     }
                   8107:                 } else {
                   8108:                     $indomcheck = ' checked="checked"';
                   8109:                     $indomvis = ' style="display:block"';
                   8110:                     $incrsvis = ' style="display:none"';
                   8111:                 }
                   8112:             } else {
                   8113:                 $indomcheck = ' checked="checked"';
1.430     raeburn  8114:                 $indomvis = ' style="display:block"';
1.428     raeburn  8115:                 $incrsvis = ' style="display:none"';
                   8116:             }
                   8117:             $r->print('<div class="LC_left_float" id="'.$role.'_maindiv"'.$visibility{$role}.'>'.
                   8118:                       '<fieldset><legend>'.$lt{'rou'}.'</legend>'.
                   8119:                       '<p>'.$lt{'whi'}.' <span class="LC_nobreak">'.
                   8120:                       '<label><input type="radio" name="'.$role.'_incrs" value="1"'.$incrscheck.' onclick="toggleAccess('."'$role'".');"'.$disabled.'>'.
                   8121:                       &mt('Set here in [_1]',lc($crstype)).'</label>'.
                   8122:                       '<span>'.('&nbsp;'x2).
                   8123:                       '<label><input type="radio" name="'.$role.'_incrs" value="0"'.$indomcheck.' onclick="toggleAccess('."'$role'".');"'.$disabled.'>'.
                   8124:                       $lt{'udd'}.'</label><span></p>'.
                   8125:                       '<div id="'.$role.'_setindom"'.$indomvis.'>'.
                   8126:                       '<span class="LC_cusr_emph">'.$domusage{$role}.'</span></div>'.
                   8127:                       '<div id="'.$role.'_setincrs"'.$incrsvis.'>');
                   8128:             foreach my $access (@accesstypes) {
                   8129:                 $r->print('<p><label><input type="radio" name="'.$role.'_access" value="'.$access.'" '.$usecheck{$access}.
                   8130:                           ' onclick="helpdeskAccess('."'$role'".');"'.$disabled.' />'.$lt{$access}.'</label>');
                   8131:                 if ($access eq 'status') {
                   8132:                     $r->print('<div id="'.$role.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
                   8133:                               &Apache::lonuserutils::adhoc_status_types($cdom,undef,$role,$selected{$access},
                   8134:                                                                         $othertitle,$usertypes,$types,$disabled).
                   8135:                               '</div>');
                   8136:                 } elsif (($access eq 'inc') && (keys(%domhelpdesk) > 0)) {
                   8137:                     $r->print('<div id="'.$role.'_notinc" style="display:'.$displaydiv{$access}.'">'.
                   8138:                               &Apache::lonuserutils::adhoc_staff($access,undef,$role,$selected{$access},
                   8139:                                                                  \%domhelpdesk,$disabled).
                   8140:                               '</div>');
                   8141:                 } elsif (($access eq 'exc') && (keys(%domhelpdesk) > 0)) {
                   8142:                     $r->print('<div id="'.$role.'_notexc" style="display:'.$displaydiv{$access}.'">'.
                   8143:                               &Apache::lonuserutils::adhoc_staff($access,undef,$role,$selected{$access},
                   8144:                                                                  \%domhelpdesk,$disabled).
                   8145:                               '</div>');
                   8146:                 }
                   8147:                 $r->print('</p>');
                   8148:             }
                   8149:             $r->print('</div></fieldset>');
                   8150:             my %full=();
                   8151:             my %levels= (
                   8152:                          course => {},
                   8153:                          domain => {},
                   8154:                          system => {},
                   8155:                         );
                   8156:             my %levelscurrent=(
                   8157:                                course => {},
                   8158:                                domain => {},
                   8159:                                system => {},
                   8160:                               );
                   8161:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
                   8162:             $r->print('<fieldset id="'.$role.'_privs" style="display:'.$displaydiv{'priv'}.'">'.
                   8163:                       '<legend>'.$lt{'rpr'}.'</legend>'.
                   8164:                       &role_priv_table($role,$permission,$crstype,\%full,\%levels,\%levelscurrent,$overridden{$role}).
                   8165:                       '</fieldset></div><div style="padding:0;clear:both;margin:0;border:0"></div>');
                   8166:         }
1.429     raeburn  8167:         if ($permission->{'owner'}) {
                   8168:             $r->print('<p><input type="submit" value="'.&mt('Save changes').'" /></p>');
                   8169:         }
1.428     raeburn  8170:     } else {
                   8171:         $r->print(&mt('Helpdesk roles have not yet been created in this domain.'));
                   8172:     }
                   8173:     # Form Footer
                   8174:     $r->print('<input type="hidden" name="action" value="helpdesk" />'
                   8175:              .'</form>');
                   8176:     return;
                   8177: }
                   8178: 
1.465   ! raeburn  8179: sub print_queued_roles {
        !          8180:     my ($r,$context,$permission,$brcrum) = @_;
        !          8181:     push (@{$brcrum},
        !          8182:              {href => '/adm/createuser?action=rolerequests',
        !          8183:               text => 'Role Requests (other domains)',
        !          8184:               help => ''});
        !          8185:     my $bread_crumbs_component = 'Role Requests';
        !          8186:     my $args = { bread_crumbs           => $brcrum,
        !          8187:                  bread_crumbs_component => $bread_crumbs_component};
        !          8188:     # print page header
        !          8189:     $r->print(&header('',$args));
        !          8190:     my ($dom,$cnum);
        !          8191:     $dom = $env{'request.role.domain'};
        !          8192:     if ($context eq 'course') {
        !          8193:         if ($env{'request.course.id'}) {
        !          8194:             if (&Apache::loncommon::course_type() eq 'Community') {
        !          8195:                 $context = 'community';
        !          8196:             }
        !          8197:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
        !          8198:         }
        !          8199:     } elsif ($context eq 'author') {
        !          8200:         $cnum = $env{'user.name'};
        !          8201:     }
        !          8202:     $r->print(&Apache::loncoursequeueadmin::display_queued_requests('othdomqueue',$dom,$cnum,$context));
        !          8203:     return;
        !          8204: }
        !          8205: 
        !          8206: sub print_pendingroles {
        !          8207:     my ($r,$context,$permission,$brcrum) = @_;
        !          8208:     push (@{$brcrum},
        !          8209:              {href => '/adm/createuser?action=queuedroles',
        !          8210:               text => 'Queued Role Assignments (users in this domain)',
        !          8211:               help => ''});
        !          8212:     my $bread_crumbs_component = 'Queued Role Assignments';
        !          8213:     my $args = { bread_crumbs           => $brcrum,
        !          8214:                  bread_crumbs_component => $bread_crumbs_component};
        !          8215:     # print page header
        !          8216:     $r->print(&header('',$args));
        !          8217:     $r->print(&Apache::loncoursequeueadmin::display_queued_requests('othdomaction',$env{'request.role.domain'},'','domain'));
        !          8218:     return;
        !          8219: }
        !          8220: 
        !          8221: sub process_pendingroles {
        !          8222:     my ($r,$context,$permission,$brcrum) = @_;
        !          8223:     push (@{$brcrum},
        !          8224:              {href => '/adm/createuser?action=queuedroles',
        !          8225:               text => 'Queued Role Assignments (users in this domain)',
        !          8226:               help => ''},
        !          8227:              {href => '/adm/createuser?action=processrolereq',
        !          8228:               text => 'Process Queue',
        !          8229:               help => ''});
        !          8230:     my $bread_crumbs_component = 'Queued Role Assignments';
        !          8231:     my $args = { bread_crumbs           => $brcrum,
        !          8232:                  bread_crumbs_component => $bread_crumbs_component};
        !          8233:     # print page header
        !          8234:     $r->print(&header('',$args));
        !          8235:     $r->print(&Apache::loncoursequeueadmin::update_request_queue('othdombydc',
        !          8236:                                                                  $env{'request.role.domain'}));
        !          8237:     return;
        !          8238: }
        !          8239: 
1.428     raeburn  8240: sub domain_adhoc_access {
                   8241:     my ($roles,$domcurrent,$accesstypes,$usertypes,$othertitle) = @_;
                   8242:     my %domusage;
                   8243:     return unless ((ref($roles) eq 'HASH') && (ref($domcurrent) eq 'HASH') && (ref($accesstypes) eq 'ARRAY'));
                   8244:     foreach my $role (keys(%{$roles})) {
                   8245:         if (ref($domcurrent->{$role}) eq 'HASH') {
                   8246:             my $access = $domcurrent->{$role}{'access'};
                   8247:             if (($access eq '') || (!grep(/^\Q$access\E$/,@{$accesstypes}))) {
                   8248:                 $access = 'all';
1.432     raeburn  8249:                 $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role',&Apache::lonnet::plaintext('dh'),
                   8250:                                                                                           &Apache::lonnet::plaintext('da'));
1.428     raeburn  8251:             } elsif ($access eq 'status') {
                   8252:                 if (ref($domcurrent->{$role}{$access}) eq 'ARRAY') {
                   8253:                     my @shown;
                   8254:                     foreach my $type (@{$domcurrent->{$role}{$access}}) {
                   8255:                         unless ($type eq 'default') {
                   8256:                             if ($usertypes->{$type}) {
                   8257:                                 push(@shown,$usertypes->{$type});
                   8258:                             }
                   8259:                         }
                   8260:                     }
                   8261:                     if (grep(/^default$/,@{$domcurrent->{$role}{$access}})) {
                   8262:                         push(@shown,$othertitle);
                   8263:                     }
                   8264:                     if (@shown) {
                   8265:                         my $shownstatus = join(' '.&mt('or').' ',@shown);
1.432     raeburn  8266:                         $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role, and institutional status: [_3]',
                   8267:                                                &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'),$shownstatus);
1.428     raeburn  8268:                     } else {
                   8269:                         $domusage{$role} = &mt('No one in the domain');
                   8270:                     }
                   8271:                 }
                   8272:             } elsif ($access eq 'inc') {
                   8273:                 my @dominc = ();
                   8274:                 if (ref($domcurrent->{$role}{'inc'}) eq 'ARRAY') {
                   8275:                     foreach my $user (@{$domcurrent->{$role}{'inc'}}) {
                   8276:                         my ($uname,$udom) = split(/:/,$user);
                   8277:                         push(@dominc,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom));
                   8278:                     }
                   8279:                     my $showninc = join(', ',@dominc);
                   8280:                     if ($showninc ne '') {
1.432     raeburn  8281:                         $domusage{$role} = &mt('Include any user in domain with active [_1] or [_2] role, except: [_3]',
                   8282:                                                &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'),$showninc);
1.428     raeburn  8283:                     } else {
1.432     raeburn  8284:                         $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role',
                   8285:                                                &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'));
1.428     raeburn  8286:                     }
                   8287:                 }
                   8288:             } elsif ($access eq 'exc') {
                   8289:                 my @domexc = ();
                   8290:                 if (ref($domcurrent->{$role}{'exc'}) eq 'ARRAY') {
                   8291:                     foreach my $user (@{$domcurrent->{$role}{'exc'}}) {
                   8292:                         my ($uname,$udom) = split(/:/,$user);
                   8293:                         push(@domexc,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom),$uname,$udom));
                   8294:                     }
                   8295:                 }
                   8296:                 my $shownexc = join(', ',@domexc);
                   8297:                 if ($shownexc ne '') {
1.432     raeburn  8298:                     $domusage{$role} = &mt('Only the following in the domain with active [_1] or [_2] role: [_3]',
                   8299:                                            &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'),$shownexc);
1.428     raeburn  8300:                 } else {
                   8301:                     $domusage{$role} = &mt('No one in the domain');
                   8302:                 }
                   8303:             } elsif ($access eq 'none') {
                   8304:                 $domusage{$role} = &mt('No one in the domain');
1.434     raeburn  8305:             } elsif ($access eq 'dh') {
1.433     raeburn  8306:                 $domusage{$role} = &mt('Any user in domain with active [_1] role',&Apache::lonnet::plaintext('dh'));
1.434     raeburn  8307:             } elsif ($access eq 'da') {
1.433     raeburn  8308:                 $domusage{$role} = &mt('Any user in domain with active [_1] role',&Apache::lonnet::plaintext('da'));
1.428     raeburn  8309:             } elsif ($access eq 'all') {
1.432     raeburn  8310:                 $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role',
                   8311:                                        &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'));
1.428     raeburn  8312:             }
                   8313:         } else {
1.432     raeburn  8314:             $domusage{$role} = &mt('Any user in domain with active [_1] or [_2] role',
                   8315:                                    &Apache::lonnet::plaintext('dh'),&Apache::lonnet::plaintext('da'));
1.428     raeburn  8316:         }
                   8317:     }
                   8318:     return %domusage;
                   8319: }
                   8320: 
                   8321: sub get_domain_customroles {
                   8322:     my ($cdom,$confname) = @_;
                   8323:     my %existing=&Apache::lonnet::dump('roles',$cdom,$confname,'rolesdef_');
                   8324:     my %customroles;
                   8325:     foreach my $key (keys(%existing)) {
                   8326:         if ($key=~/^rolesdef\_(\w+)$/) {
                   8327:             my $rolename = $1;
                   8328:             my %privs;
                   8329:             ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
                   8330:             $customroles{$rolename} = \%privs;
                   8331:         }
                   8332:     }
                   8333:     return %customroles;
                   8334: }
                   8335: 
                   8336: sub role_priv_table {
                   8337:     my ($role,$permission,$crstype,$full,$levels,$levelscurrent,$overridden) = @_;
                   8338:     return unless ((ref($full) eq 'HASH') && (ref($levels) eq 'HASH') &&
                   8339:                    (ref($levelscurrent) eq 'HASH'));
                   8340:     my %lt=&Apache::lonlocal::texthash (
                   8341:                     'crl'  => 'Course Level Privilege',
                   8342:                     'def'  => 'Domain Defaults',
                   8343:                     'ove'  => 'Override in Course',
                   8344:                     'ine'  => 'In effect',
                   8345:                     'dis'  => 'Disabled',
                   8346:                     'ena'  => 'Enabled',
                   8347:                    );
                   8348:     if ($crstype eq 'Community') {
                   8349:         $lt{'ove'} = 'Override in Community',
                   8350:     }
                   8351:     my @status = ('Disabled','Enabled');
                   8352:     my (%on,%off);
                   8353:     if (ref($overridden) eq 'HASH') {
                   8354:         if (ref($overridden->{'on'}) eq 'ARRAY') {
                   8355:             map { $on{$_} = 1; } (@{$overridden->{'on'}});
                   8356:         }
                   8357:         if (ref($overridden->{'off'}) eq 'ARRAY') {
                   8358:             map { $off{$_} = 1; } (@{$overridden->{'off'}});
                   8359:         }
                   8360:     }
                   8361:     my $output=&Apache::loncommon::start_data_table().
                   8362:                &Apache::loncommon::start_data_table_header_row().
                   8363:                '<th>'.$lt{'crl'}.'</th><th>'.$lt{'def'}.'</th><th>'.$lt{'ove'}.
                   8364:                '</th><th>'.$lt{'ine'}.'</th>'.
                   8365:                &Apache::loncommon::end_data_table_header_row();
                   8366:     foreach my $priv (sort(keys(%{$full}))) {
                   8367:         next unless ($levels->{'course'}{$priv});
                   8368:         my $privtext = &Apache::lonnet::plaintext($priv,$crstype);
                   8369:         my ($default,$ineffect);
                   8370:         if ($levelscurrent->{'course'}{$priv}) {
                   8371:             $default = '<img src="/adm/lonIcons/navmap.correct.gif" alt="'.$lt{'ena'}.'" />';
                   8372:             $ineffect = $default;
                   8373:         }
                   8374:         my ($customstatus,$checked);
                   8375:         $output .= &Apache::loncommon::start_data_table_row().
                   8376:                    '<td>'.$privtext.'</td>'.
                   8377:                    '<td>'.$default.'</td><td>';
                   8378:         if (($levelscurrent->{'course'}{$priv}) && ($off{$priv})) {
                   8379:             if ($permission->{'owner'}) {
                   8380:                 $checked = ' checked="checked"';
                   8381:             }
                   8382:             $customstatus = '<img src="/adm/lonIcons/navmap.wrong.gif" alt="'.$lt{'dis'}.'" />';
1.430     raeburn  8383:             $ineffect = $customstatus;
1.428     raeburn  8384:         } elsif ((!$levelscurrent->{'course'}{$priv}) && ($on{$priv})) {
                   8385:             if ($permission->{'owner'}) {
1.430     raeburn  8386:                 $checked = ' checked="checked"';
1.428     raeburn  8387:             }
                   8388:             $customstatus = '<img src="/adm/lonIcons/navmap.correct.gif" alt="'.$lt{'ena'}.'" />';
1.430     raeburn  8389:             $ineffect = $customstatus;
1.428     raeburn  8390:         }
                   8391:         if ($permission->{'owner'}) {
                   8392:             $output .= '<input type="checkbox" name="'.$role.'_override" value="'.$priv.'"'.$checked.' />';
                   8393:         } else {
                   8394:             $output .= $customstatus;
                   8395:         }
                   8396:         $output .= '</td><td>'.$ineffect.'</td>'.
                   8397:                    &Apache::loncommon::end_data_table_row();
                   8398:     }
                   8399:     $output .= &Apache::loncommon::end_data_table();
                   8400:     return $output;
                   8401: }
                   8402: 
                   8403: sub get_adhocrole_settings {
1.430     raeburn  8404:     my ($cid,$accesstypes,$types,$customroles,$settings,$overridden) = @_;
1.428     raeburn  8405:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($customroles) eq 'HASH') &&
                   8406:                    (ref($settings) eq 'HASH') && (ref($overridden) eq 'HASH'));
                   8407:     foreach my $role (split(/,/,$env{'course.'.$cid.'.internal.adhocaccess'})) {
                   8408:         my ($curraccess,$rest) = split(/=/,$env{'course.'.$cid.'.internal.adhoc.'.$role});
                   8409:         if (($curraccess ne '') && (grep(/^\Q$curraccess\E$/,@{$accesstypes}))) {
                   8410:             $settings->{$role}{'access'} = $curraccess;
                   8411:             if (($curraccess eq 'status') && (ref($types) eq 'ARRAY')) {
                   8412:                 my @status = split(/,/,$rest);
                   8413:                 my @currstatus;
                   8414:                 foreach my $type (@status) {
                   8415:                     if ($type eq 'default') {
                   8416:                         push(@currstatus,$type);
                   8417:                     } elsif (grep(/^\Q$type\E$/,@{$types})) {
                   8418:                         push(@currstatus,$type);
                   8419:                     }
                   8420:                 }
                   8421:                 if (@currstatus) {
                   8422:                     $settings->{$role}{$curraccess} = \@currstatus;
                   8423:                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   8424:                     my @personnel = split(/,/,$rest);
                   8425:                     $settings->{$role}{$curraccess} = \@personnel;
                   8426:                 }
                   8427:             }
                   8428:         }
                   8429:     }
                   8430:     foreach my $role (keys(%{$customroles})) {
                   8431:         if ($env{'course.'.$cid.'.internal.adhocpriv.'.$role}) {
                   8432:             my %currentprivs;
                   8433:             if (ref($customroles->{$role}) eq 'HASH') {
                   8434:                 if (exists($customroles->{$role}{'course'})) {
                   8435:                     my %full=();
                   8436:                     my %levels= (
                   8437:                                   course => {},
                   8438:                                   domain => {},
                   8439:                                   system => {},
                   8440:                                 );
                   8441:                     my %levelscurrent=(
                   8442:                                         course => {},
                   8443:                                         domain => {},
                   8444:                                         system => {},
                   8445:                                       );
                   8446:                     &Apache::lonuserutils::custom_role_privs($customroles->{$role},\%full,\%levels,\%levelscurrent);
                   8447:                     %currentprivs = %{$levelscurrent{'course'}};
                   8448:                 }
                   8449:             }
                   8450:             foreach my $item (split(/,/,$env{'course.'.$cid.'.internal.adhocpriv.'.$role})) {
                   8451:                 next if ($item eq '');
                   8452:                 my ($rule,$rest) = split(/=/,$item);
                   8453:                 next unless (($rule eq 'off') || ($rule eq 'on'));
                   8454:                 foreach my $priv (split(/:/,$rest)) {
                   8455:                     if ($priv ne '') {
                   8456:                         if ($rule eq 'off') {
                   8457:                             push(@{$overridden->{$role}{'off'}},$priv);
                   8458:                             if ($currentprivs{$priv}) {
                   8459:                                 push(@{$settings->{$role}{'off'}},$priv);
                   8460:                             }
                   8461:                         } else {
                   8462:                             push(@{$overridden->{$role}{'on'}},$priv);
                   8463:                             unless ($currentprivs{$priv}) {
                   8464:                                 push(@{$settings->{$role}{'on'}},$priv);
                   8465:                             }
                   8466:                         }
                   8467:                     }
                   8468:                 }
                   8469:             }
                   8470:         }
                   8471:     }
                   8472:     return;
                   8473: }
                   8474: 
                   8475: sub update_helpdeskaccess {
                   8476:     my ($r,$permission,$brcrum) = @_;
                   8477:     my $helpitem = 'Course_Helpdesk_Access';
                   8478:     push (@{$brcrum},
                   8479:              {href => '/adm/createuser?action=helpdesk',
                   8480:               text => 'Helpdesk Access',
                   8481:               help => $helpitem},
                   8482:              {href => '/adm/createuser?action=helpdesk',
                   8483:               text => 'Result',
                   8484:               help => $helpitem}
                   8485:          );
                   8486:     my $bread_crumbs_component = 'Helpdesk Staff Access';
                   8487:     my $args = { bread_crumbs           => $brcrum,
                   8488:                  bread_crumbs_component => $bread_crumbs_component};
                   8489: 
                   8490:     # print page header
                   8491:     $r->print(&header('',$args));
                   8492:     unless ((ref($permission) eq 'HASH') && ($permission->{'owner'})) {
                   8493:         $r->print('<p class="LC_error">'.&mt('You do not have permission to change helpdesk access.').'</p>');
                   8494:         return;
                   8495:     }
1.434     raeburn  8496:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
1.428     raeburn  8497:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   8498:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   8499:     my $confname = $cdom.'-domainconfig';
                   8500:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($cdom);
                   8501:     my $crstype = &Apache::loncommon::course_type();
                   8502:     my %customroles = &get_domain_customroles($cdom,$confname);
                   8503:     my (%settings,%overridden);
                   8504:     &get_adhocrole_settings($env{'request.course.id'},\@accesstypes,
                   8505:                             $types,\%customroles,\%settings,\%overridden);
1.432     raeburn  8506:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($cdom,['dh','da']);
1.428     raeburn  8507:     my (%changed,%storehash,@todelete);
                   8508: 
                   8509:     if (keys(%customroles)) {
                   8510:         my (%newsettings,@incrs);
                   8511:         foreach my $role (keys(%customroles)) {
                   8512:             $newsettings{$role} = {
                   8513:                                     access => '',
                   8514:                                     status => '',
                   8515:                                     exc    => '',
                   8516:                                     inc    => '',
                   8517:                                     on     => '',
                   8518:                                     off    => '',
                   8519:                                   };
                   8520:             my %current;
                   8521:             if (ref($settings{$role}) eq 'HASH') {
                   8522:                 %current = %{$settings{$role}};
                   8523:             }
                   8524:             if (ref($overridden{$role}) eq 'HASH') {
                   8525:                 $current{'overridden'} = $overridden{$role};
                   8526:             }
                   8527:             if ($env{'form.'.$role.'_incrs'}) {
                   8528:                 my $access = $env{'form.'.$role.'_access'};
                   8529:                 if (grep(/^\Q$access\E$/,@accesstypes)) {
                   8530:                     push(@incrs,$role);
                   8531:                     unless ($current{'access'} eq $access) {
                   8532:                         $changed{$role}{'access'} = 1;
1.430     raeburn  8533:                         $storehash{'internal.adhoc.'.$role} = $access;
1.428     raeburn  8534:                     }
                   8535:                     if ($access eq 'status') {
                   8536:                         my @statuses = &Apache::loncommon::get_env_multiple('form.'.$role.'_status');
                   8537:                         my @stored;
                   8538:                         my @shownstatus;
                   8539:                         if (ref($types) eq 'ARRAY') {
                   8540:                             foreach my $type (sort(@statuses)) {
                   8541:                                 if ($type eq 'default') {
                   8542:                                     push(@stored,$type);
                   8543:                                 } elsif (grep(/^\Q$type\E$/,@{$types})) {
                   8544:                                     push(@stored,$type);
                   8545:                                     push(@shownstatus,$usertypes->{$type});
                   8546:                                 }
                   8547:                             }
                   8548:                             if (grep(/^default$/,@statuses)) {
                   8549:                                 push(@shownstatus,$othertitle);
                   8550:                             }
                   8551:                             $storehash{'internal.adhoc.'.$role} .= '='.join(',',@stored);
                   8552:                         }
                   8553:                         $newsettings{$role}{'status'} = join(' '.&mt('or').' ',@shownstatus);
                   8554:                         if (ref($current{'status'}) eq 'ARRAY') {
                   8555:                             my @diffs = &Apache::loncommon::compare_arrays(\@stored,$current{'status'});
                   8556:                             if (@diffs) {
                   8557:                                 $changed{$role}{'status'} = 1;
                   8558:                             }
                   8559:                         } elsif (@stored) {
                   8560:                             $changed{$role}{'status'} = 1;
                   8561:                         }
                   8562:                     } elsif (($access eq 'inc') || ($access eq 'exc')) {
                   8563:                         my @personnel = &Apache::loncommon::get_env_multiple('form.'.$role.'_staff_'.$access);
                   8564:                         my @newspecstaff;
                   8565:                         my @stored;
                   8566:                         my @currstaff;
                   8567:                         foreach my $person (sort(@personnel)) {
                   8568:                             if ($domhelpdesk{$person}) {
1.430     raeburn  8569:                                 push(@stored,$person);
1.428     raeburn  8570:                             }
                   8571:                         }
                   8572:                         if (ref($current{$access}) eq 'ARRAY') {
                   8573:                             my @diffs = &Apache::loncommon::compare_arrays(\@stored,$current{$access});
                   8574:                             if (@diffs) {
                   8575:                                 $changed{$role}{$access} = 1;
                   8576:                             }
                   8577:                         } elsif (@stored) {
                   8578:                             $changed{$role}{$access} = 1;
                   8579:                         }
                   8580:                         $storehash{'internal.adhoc.'.$role} .= '='.join(',',@stored);
                   8581:                         foreach my $person (@stored) {
                   8582:                             my ($uname,$udom) = split(/:/,$person);
                   8583:                             push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
                   8584:                         }
                   8585:                         $newsettings{$role}{$access} = join(', ',sort(@newspecstaff));
                   8586:                     }
                   8587:                     $newsettings{$role}{'access'} = $access;
                   8588:                 }
                   8589:             } else {
                   8590:                 if (($current{'access'} ne '') && (grep(/^\Q$current{'access'}\E$/,@accesstypes))) {
                   8591:                     $changed{$role}{'access'} = 1;
                   8592:                     $newsettings{$role} = {};
                   8593:                     push(@todelete,'internal.adhoc.'.$role);
                   8594:                 }
                   8595:             }
                   8596:             if (($env{'form.'.$role.'_incrs'}) && ($env{'form.'.$role.'_access'} eq 'none')) {
                   8597:                 if (ref($current{'overridden'}) eq 'HASH') {
                   8598:                     push(@todelete,'internal.adhocpriv.'.$role);
                   8599:                 }
                   8600:             } else {
                   8601:                 my %full=();
                   8602:                 my %levels= (
                   8603:                              course => {},
                   8604:                              domain => {},
                   8605:                              system => {},
                   8606:                             );
                   8607:                 my %levelscurrent=(
                   8608:                                    course => {},
                   8609:                                    domain => {},
                   8610:                                    system => {},
                   8611:                                   );
                   8612:                 &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
                   8613:                 my (@updatedon,@updatedoff,@override);
                   8614:                 @override = &Apache::loncommon::get_env_multiple('form.'.$role.'_override');
1.430     raeburn  8615:                 if (@override) {
1.428     raeburn  8616:                     foreach my $priv (sort(keys(%full))) {
                   8617:                         next unless ($levels{'course'}{$priv});
                   8618:                         if (grep(/^\Q$priv\E$/,@override)) {
                   8619:                             if ($levelscurrent{'course'}{$priv}) {
                   8620:                                 push(@updatedoff,$priv);
                   8621:                             } else {
                   8622:                                 push(@updatedon,$priv);
                   8623:                             }
                   8624:                         }
                   8625:                     }
                   8626:                 }
                   8627:                 if (@updatedon) {
1.430     raeburn  8628:                     $newsettings{$role}{'on'} = join('</li><li>', map { &Apache::lonnet::plaintext($_,$crstype) } (@updatedon));
1.428     raeburn  8629:                 }
                   8630:                 if (@updatedoff) {
                   8631:                     $newsettings{$role}{'off'} = join('</li><li>', map { &Apache::lonnet::plaintext($_,$crstype) } (@updatedoff));
                   8632:                 }
                   8633:                 if (ref($current{'overridden'}) eq 'HASH') {
                   8634:                     if (ref($current{'overridden'}{'on'}) eq 'ARRAY') {
                   8635:                         if (@updatedon) {
                   8636:                             my @diffs = &Apache::loncommon::compare_arrays(\@updatedon,$current{'overridden'}{'on'});
                   8637:                             if (@diffs) {
                   8638:                                 $changed{$role}{'on'} = 1;
                   8639:                             }
                   8640:                         } else {
                   8641:                             $changed{$role}{'on'} = 1;
                   8642:                         }
                   8643:                     } elsif (@updatedon) {
                   8644:                         $changed{$role}{'on'} = 1;
                   8645:                     }
                   8646:                     if (ref($current{'overridden'}{'off'}) eq 'ARRAY') {
                   8647:                         if (@updatedoff) {
                   8648:                             my @diffs = &Apache::loncommon::compare_arrays(\@updatedoff,$current{'overridden'}{'off'});
                   8649:                             if (@diffs) {
                   8650:                                 $changed{$role}{'off'} = 1;
                   8651:                             }
                   8652:                         } else {
                   8653:                             $changed{$role}{'off'} = 1;
                   8654:                         }
                   8655:                     } elsif (@updatedoff) {
                   8656:                         $changed{$role}{'off'} = 1;
                   8657:                     }
                   8658:                 } else {
                   8659:                     if (@updatedon) {
                   8660:                         $changed{$role}{'on'} = 1;
                   8661:                     }
                   8662:                     if (@updatedoff) {
                   8663:                         $changed{$role}{'off'} = 1;
                   8664:                     }
                   8665:                 }
                   8666:                 if (ref($changed{$role}) eq 'HASH') {
                   8667:                     if (($changed{$role}{'on'} || $changed{$role}{'off'})) {
                   8668:                         my $newpriv;
                   8669:                         if (@updatedon) {
                   8670:                             $newpriv = 'on='.join(':',@updatedon);
                   8671:                         }
                   8672:                         if (@updatedoff) {
                   8673:                             $newpriv .= ($newpriv ? ',' : '' ).'off='.join(':',@updatedoff);
                   8674:                         }
                   8675:                         if ($newpriv eq '') {
                   8676:                             push(@todelete,'internal.adhocpriv.'.$role);
                   8677:                         } else {
                   8678:                             $storehash{'internal.adhocpriv.'.$role} = $newpriv;
                   8679:                         }
                   8680:                     }
                   8681:                 }
                   8682:             }
                   8683:         }
                   8684:         if (@incrs) {
                   8685:             $storehash{'internal.adhocaccess'} = join(',',@incrs);
                   8686:         } elsif (@todelete) {
                   8687:             push(@todelete,'internal.adhocaccess');
                   8688:         }
                   8689:         if (keys(%changed)) {
                   8690:             my ($putres,$delres);
                   8691:             if (keys(%storehash)) {
                   8692:                 $putres = &Apache::lonnet::put('environment',\%storehash,$cdom,$cnum);
                   8693:                 my %newenvhash;
                   8694:                 foreach my $key (keys(%storehash)) {
                   8695:                     $newenvhash{'course.'.$env{'request.course.id'}.'.'.$key} = $storehash{$key};
                   8696:                 }
                   8697:                 &Apache::lonnet::appenv(\%newenvhash);
                   8698:             }
                   8699:             if (@todelete) {
                   8700:                 $delres = &Apache::lonnet::del('environment',\@todelete,$cdom,$cnum);
                   8701:                 foreach my $key (@todelete) {
                   8702:                     &Apache::lonnet::delenv('course.'.$env{'request.course.id'}.'.'.$key);
                   8703:                 }
                   8704:             }
                   8705:             if (($putres eq 'ok') || ($delres eq 'ok')) {
                   8706:                 my %domconfig = &Apache::lonnet::get_dom('configuration',['helpsettings'],$cdom);
                   8707:                 my (%domcurrent,%ordered,%description,%domusage);
                   8708:                 if (ref($domconfig{'helpsettings'}) eq 'HASH') {
                   8709:                     if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
                   8710:                         %domcurrent = %{$domconfig{'helpsettings'}{'adhoc'}};
                   8711:                     }
                   8712:                 }
                   8713:                 my $count = 0;
                   8714:                 foreach my $role (sort(keys(%customroles))) {
                   8715:                     my ($order,$desc);
                   8716:                     if (ref($domcurrent{$role}) eq 'HASH') {
                   8717:                         $order = $domcurrent{$role}{'order'};
                   8718:                         $desc = $domcurrent{$role}{'desc'};
                   8719:                     }
                   8720:                     if ($order eq '') {
                   8721:                         $order = $count;
                   8722:                     }
                   8723:                     $ordered{$order} = $role;
                   8724:                     if ($desc ne '') {
                   8725:                         $description{$role} = $desc;
                   8726:                     } else {
                   8727:                         $description{$role}= $role;
                   8728:                     }
                   8729:                     $count++;
                   8730:                 }
                   8731:                 my @roles_by_num = ();
                   8732:                 foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
                   8733:                     push(@roles_by_num,$ordered{$item});
                   8734:                 }
                   8735:                 %domusage = &domain_adhoc_access(\%changed,\%domcurrent,\@accesstypes,$usertypes,$othertitle);
1.430     raeburn  8736:                 $r->print(&mt('Helpdesk access settings have been changed as follows').'<br />');
1.428     raeburn  8737:                 $r->print('<ul>');
                   8738:                 foreach my $role (@roles_by_num) {
                   8739:                     next unless (ref($changed{$role}) eq 'HASH');
                   8740:                     $r->print('<li>'.&mt('Ad hoc role').': <b>'.$description{$role}.'</b>'.
                   8741:                               '<ul>');
1.430     raeburn  8742:                     if ($changed{$role}{'access'} || $changed{$role}{'status'} || $changed{$role}{'inc'} || $changed{$role}{'exc'}) {
1.428     raeburn  8743:                         $r->print('<li>');
                   8744:                         if ($env{'form.'.$role.'_incrs'}) {
                   8745:                             if ($newsettings{$role}{'access'} eq 'all') {
                   8746:                                 $r->print(&mt('All helpdesk staff can access '.lc($crstype).' with this role.'));
1.434     raeburn  8747:                             } elsif ($newsettings{$role}{'access'} eq 'dh') {
1.433     raeburn  8748:                                 $r->print(&mt('Helpdesk staff can use this role if they have an active [_1] role',
                   8749:                                               &Apache::lonnet::plaintext('dh')));
1.434     raeburn  8750:                             } elsif ($newsettings{$role}{'access'} eq 'da') {
1.433     raeburn  8751:                                 $r->print(&mt('Helpdesk staff can use this role if they have an active [_1] role',
                   8752:                                               &Apache::lonnet::plaintext('da')));
1.428     raeburn  8753:                             } elsif ($newsettings{$role}{'access'} eq 'none') {
                   8754:                                 $r->print(&mt('No helpdesk staff can access '.lc($crstype).' with this role.'));
                   8755:                             } elsif ($newsettings{$role}{'access'} eq 'status') {
                   8756:                                 if ($newsettings{$role}{'status'}) {
                   8757:                                     my ($access,$rest) = split(/=/,$storehash{'internal.adhoc.'.$role});
1.430     raeburn  8758:                                     if (split(/,/,$rest) > 1) {
1.428     raeburn  8759:                                         $r->print(&mt('Helpdesk staff can use this role if their institutional type is one of: [_1].',
                   8760:                                                       $newsettings{$role}{'status'}));
                   8761:                                     } else {
                   8762:                                         $r->print(&mt('Helpdesk staff can use this role if their institutional type is: [_1].',
                   8763:                                                       $newsettings{$role}{'status'}));
                   8764:                                     }
                   8765:                                 } else {
                   8766:                                     $r->print(&mt('No helpdesk staff can access '.lc($crstype).' with this role.'));
                   8767:                                 }
                   8768:                             } elsif ($newsettings{$role}{'access'} eq 'exc') {
                   8769:                                 if ($newsettings{$role}{'exc'}) {
                   8770:                                     $r->print(&mt('Helpdesk staff who can use this role are as follows:').' '.$newsettings{$role}{'exc'}.'.');
                   8771:                                 } else {
                   8772:                                     $r->print(&mt('No helpdesk staff can access '.lc($crstype).' with this role.'));
                   8773:                                 }
                   8774:                             } elsif ($newsettings{$role}{'access'} eq 'inc') {
                   8775:                                 if ($newsettings{$role}{'inc'}) {
                   8776:                                     $r->print(&mt('All helpdesk staff may use this role except the following:').' '.$newsettings{$role}{'inc'}.'.');
                   8777:                                 } else {
                   8778:                                     $r->print(&mt('All helpdesk staff may use this role.'));
                   8779:                                 }
                   8780:                             }
                   8781:                         } else {
                   8782:                             $r->print(&mt('Default access set in the domain now applies.').'<br />'.
                   8783:                                       '<span class="LC_cusr_emph">'.$domusage{$role}.'</span>');
                   8784:                         }
                   8785:                         $r->print('</li>');
                   8786:                     }
                   8787:                     unless ($newsettings{$role}{'access'} eq 'none') {
                   8788:                         if ($changed{$role}{'off'}) {
                   8789:                             if ($newsettings{$role}{'off'}) {
                   8790:                                 $r->print('<li>'.&mt('Privileges which are available by default for this ad hoc role, but are disabled for this specific '.lc($crstype).':').
                   8791:                                           '<ul><li>'.$newsettings{$role}{'off'}.'</li></ul></li>');
                   8792:                             } else {
1.430     raeburn  8793:                                 $r->print('<li>'.&mt('All privileges available by default for this ad hoc role are enabled.').'</li>');
1.428     raeburn  8794:                             }
                   8795:                         }
1.430     raeburn  8796:                         if ($changed{$role}{'on'}) {
1.428     raeburn  8797:                             if ($newsettings{$role}{'on'}) {
                   8798:                                 $r->print('<li>'.&mt('Privileges which are not available by default for this ad hoc role, but are enabled for this specific '.lc($crstype).':').
                   8799:                                           '<ul><li>'.$newsettings{$role}{'on'}.'</li></ul></li>');
                   8800:                             } else {
1.430     raeburn  8801:                                 $r->print('<li>'.&mt('None of the privileges unavailable by default for this ad hoc role are enabled.').'</li>');
1.428     raeburn  8802:                             }
                   8803:                         }
                   8804:                     }
                   8805:                     $r->print('</ul></li>');
                   8806:                 }
                   8807:                 $r->print('</ul>');
                   8808:             }
                   8809:         } else {
1.430     raeburn  8810:             $r->print(&mt('No changes made to helpdesk access settings.'));
1.428     raeburn  8811:         }
                   8812:     }
                   8813:     return;
                   8814: }
                   8815: 
1.27      matthew  8816: #-------------------------------------------------- functions for &phase_two
1.160     raeburn  8817: sub user_search_result {
1.221     raeburn  8818:     my ($context,$srch) = @_;
1.160     raeburn  8819:     my %allhomes;
                   8820:     my %inst_matches;
                   8821:     my %srch_results;
1.181     raeburn  8822:     my ($response,$currstate,$forcenewuser,$dirsrchres);
1.183     raeburn  8823:     $srch->{'srchterm'} =~ s/\s+/ /g;
1.176     raeburn  8824:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
1.160     raeburn  8825:         $response = &mt('Invalid search.');
                   8826:     }
                   8827:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
                   8828:         $response = &mt('Invalid search.');
                   8829:     }
1.177     raeburn  8830:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
1.160     raeburn  8831:         $response = &mt('Invalid search.');
                   8832:     }
                   8833:     if ($srch->{'srchterm'} eq '') {
                   8834:         $response = &mt('You must enter a search term.');
                   8835:     }
1.183     raeburn  8836:     if ($srch->{'srchterm'} =~ /^\s+$/) {
                   8837:         $response = &mt('Your search term must contain more than just spaces.');
                   8838:     }
1.160     raeburn  8839:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
                   8840:         if (($srch->{'srchdomain'} eq '') || 
1.163     albertel 8841: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
1.160     raeburn  8842:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
                   8843:         }
                   8844:     }
                   8845:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
                   8846:         ($srch->{'srchin'} eq 'alc')) {
1.176     raeburn  8847:         if ($srch->{'srchby'} eq 'uname') {
1.243     raeburn  8848:             my $unamecheck = $srch->{'srchterm'};
                   8849:             if ($srch->{'srchtype'} eq 'contains') {
                   8850:                 if ($unamecheck !~ /^\w/) {
                   8851:                     $unamecheck = 'a'.$unamecheck; 
                   8852:                 }
                   8853:             }
                   8854:             if ($unamecheck !~ /^$match_username$/) {
1.176     raeburn  8855:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
                   8856:             }
1.160     raeburn  8857:         }
                   8858:     }
1.180     raeburn  8859:     if ($response ne '') {
1.413     raeburn  8860:         $response = '<span class="LC_warning">'.$response.'</span><br />';
1.180     raeburn  8861:     }
1.160     raeburn  8862:     if ($srch->{'srchin'} eq 'instd') {
1.412     raeburn  8863:         my $instd_chk = &instdirectorysrch_check($srch);
1.160     raeburn  8864:         if ($instd_chk ne 'ok') {
1.412     raeburn  8865:             my $domd_chk = &domdirectorysrch_check($srch);
1.413     raeburn  8866:             $response .= '<span class="LC_warning">'.$instd_chk.'</span><br />';
1.412     raeburn  8867:             if ($domd_chk eq 'ok') {
1.435     raeburn  8868:                 $response .= &mt('You may want to search in the LON-CAPA domain instead of in the institutional directory.');
1.412     raeburn  8869:             }
1.415     raeburn  8870:             $response .= '<br />';
1.412     raeburn  8871:         }
                   8872:     } else {
1.417     raeburn  8873:         unless (($context eq 'requestcrs') && ($srch->{'srchtype'} eq 'exact')) {
1.412     raeburn  8874:             my $domd_chk = &domdirectorysrch_check($srch);
1.438     raeburn  8875:             if (($domd_chk ne 'ok') && ($env{'form.action'} ne 'accesslogs')) {
1.412     raeburn  8876:                 my $instd_chk = &instdirectorysrch_check($srch);
1.413     raeburn  8877:                 $response .= '<span class="LC_warning">'.$domd_chk.'</span><br />';
1.412     raeburn  8878:                 if ($instd_chk eq 'ok') {
1.435     raeburn  8879:                     $response .= &mt('You may want to search in the institutional directory instead of in the LON-CAPA domain.');
1.412     raeburn  8880:                 }
1.415     raeburn  8881:                 $response .= '<br />';
1.412     raeburn  8882:             }
1.160     raeburn  8883:         }
                   8884:     }
                   8885:     if ($response ne '') {
1.180     raeburn  8886:         return ($currstate,$response);
1.160     raeburn  8887:     }
                   8888:     if ($srch->{'srchby'} eq 'uname') {
                   8889:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
                   8890:             if ($env{'form.forcenew'}) {
                   8891:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
                   8892:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   8893:                     if ($uhome eq 'no_host') {
                   8894:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.180     raeburn  8895:                         my $showdom = &display_domain_info($env{'request.role.domain'});
                   8896:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
1.160     raeburn  8897:                     } else {
1.179     raeburn  8898:                         $currstate = 'modify';
1.160     raeburn  8899:                     }
                   8900:                 } else {
1.179     raeburn  8901:                     $currstate = 'modify';
1.160     raeburn  8902:                 }
                   8903:             } else {
                   8904:                 if ($srch->{'srchin'} eq 'dom') {
1.162     raeburn  8905:                     if ($srch->{'srchtype'} eq 'exact') {
                   8906:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   8907:                         if ($uhome eq 'no_host') {
1.179     raeburn  8908:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  8909:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  8910:                         } else {
1.179     raeburn  8911:                             $currstate = 'modify';
1.416     raeburn  8912:                             if ($env{'form.action'} eq 'accesslogs') {
                   8913:                                 $currstate = 'activity';
                   8914:                             }
1.310     raeburn  8915:                             my $uname = $srch->{'srchterm'};
                   8916:                             my $udom = $srch->{'srchdomain'};
                   8917:                             $srch_results{$uname.':'.$udom} =
                   8918:                                 { &Apache::lonnet::get('environment',
                   8919:                                                        ['firstname',
                   8920:                                                         'lastname',
                   8921:                                                         'permanentemail'],
                   8922:                                                          $udom,$uname)
                   8923:                                 };
1.162     raeburn  8924:                         }
                   8925:                     } else {
                   8926:                         %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  8927:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  8928:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  8929:                     }
                   8930:                 } else {
1.167     albertel 8931:                     my $courseusers = &get_courseusers();
1.162     raeburn  8932:                     if ($srch->{'srchtype'} eq 'exact') {
1.167     albertel 8933:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
1.179     raeburn  8934:                             $currstate = 'modify';
1.162     raeburn  8935:                         } else {
1.179     raeburn  8936:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  8937:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  8938:                         }
1.160     raeburn  8939:                     } else {
1.167     albertel 8940:                         foreach my $user (keys(%$courseusers)) {
1.162     raeburn  8941:                             my ($cuname,$cudomain) = split(/:/,$user);
                   8942:                             if ($cudomain eq $srch->{'srchdomain'}) {
1.177     raeburn  8943:                                 my $matched = 0;
                   8944:                                 if ($srch->{'srchtype'} eq 'begins') {
                   8945:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
                   8946:                                         $matched = 1;
                   8947:                                     }
                   8948:                                 } else {
                   8949:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
                   8950:                                         $matched = 1;
                   8951:                                     }
                   8952:                                 }
                   8953:                                 if ($matched) {
1.167     albertel 8954:                                     $srch_results{$user} = 
                   8955: 					{&Apache::lonnet::get('environment',
                   8956: 							     ['firstname',
                   8957: 							      'lastname',
1.194     albertel 8958: 							      'permanentemail'],
                   8959: 							      $cudomain,$cuname)};
1.162     raeburn  8960:                                 }
                   8961:                             }
                   8962:                         }
1.179     raeburn  8963:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  8964:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  8965:                     }
                   8966:                 }
                   8967:             }
                   8968:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  8969:             $currstate = 'query';
1.160     raeburn  8970:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  8971:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
                   8972:             if ($dirsrchres eq 'ok') {
                   8973:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  8974:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  8975:             } else {
                   8976:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   8977:                 $response = '<span class="LC_warning">'.
                   8978:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   8979:                     '</span><br />'.
1.435     raeburn  8980:                     &mt('You may want to search in the LON-CAPA domain instead of in the institutional directory.').
1.415     raeburn  8981:                     '<br />'; 
1.181     raeburn  8982:             }
1.160     raeburn  8983:         }
                   8984:     } else {
                   8985:         if ($srch->{'srchin'} eq 'dom') {
                   8986:             %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  8987:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  8988:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  8989:         } elsif ($srch->{'srchin'} eq 'crs') {
1.167     albertel 8990:             my $courseusers = &get_courseusers(); 
                   8991:             foreach my $user (keys(%$courseusers)) {
1.160     raeburn  8992:                 my ($uname,$udom) = split(/:/,$user);
                   8993:                 my %names = &Apache::loncommon::getnames($uname,$udom);
                   8994:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
                   8995:                 if ($srch->{'srchby'} eq 'lastname') {
                   8996:                     if ((($srch->{'srchtype'} eq 'exact') && 
                   8997:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
1.177     raeburn  8998:                         (($srch->{'srchtype'} eq 'begins') &&
                   8999:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
1.160     raeburn  9000:                         (($srch->{'srchtype'} eq 'contains') &&
                   9001:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
                   9002:                         $srch_results{$user} = {firstname => $names{'firstname'},
                   9003:                                             lastname => $names{'lastname'},
                   9004:                                             permanentemail => $emails{'permanentemail'},
                   9005:                                            };
                   9006:                     }
                   9007:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
                   9008:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
1.177     raeburn  9009:                     $srchlast =~ s/\s+$//;
                   9010:                     $srchfirst =~ s/^\s+//;
1.160     raeburn  9011:                     if ($srch->{'srchtype'} eq 'exact') {
                   9012:                         if (($names{'lastname'} eq $srchlast) &&
                   9013:                             ($names{'firstname'} eq $srchfirst)) {
                   9014:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   9015:                                                 lastname => $names{'lastname'},
                   9016:                                                 permanentemail => $emails{'permanentemail'},
                   9017: 
                   9018:                                            };
                   9019:                         }
1.177     raeburn  9020:                     } elsif ($srch->{'srchtype'} eq 'begins') {
                   9021:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
                   9022:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
                   9023:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   9024:                                                 lastname => $names{'lastname'},
                   9025:                                                 permanentemail => $emails{'permanentemail'},
                   9026:                                                };
                   9027:                         }
                   9028:                     } else {
1.160     raeburn  9029:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
                   9030:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
                   9031:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   9032:                                                 lastname => $names{'lastname'},
                   9033:                                                 permanentemail => $emails{'permanentemail'},
                   9034:                                                };
                   9035:                         }
                   9036:                     }
                   9037:                 }
                   9038:             }
1.179     raeburn  9039:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  9040:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  9041:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  9042:             $currstate = 'query';
1.160     raeburn  9043:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  9044:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
                   9045:             if ($dirsrchres eq 'ok') {
                   9046:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  9047:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  9048:             } else {
1.412     raeburn  9049:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   9050:                 $response = '<span class="LC_warning">'.
1.181     raeburn  9051:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   9052:                     '</span><br />'.
1.435     raeburn  9053:                     &mt('You may want to search in the LON-CAPA domain instead of in the institutional directory.').
1.415     raeburn  9054:                     '<br />';
1.181     raeburn  9055:             }
1.160     raeburn  9056:         }
                   9057:     }
1.179     raeburn  9058:     return ($currstate,$response,$forcenewuser,\%srch_results);
1.160     raeburn  9059: }
                   9060: 
1.412     raeburn  9061: sub domdirectorysrch_check {
                   9062:     my ($srch) = @_;
                   9063:     my $response;
                   9064:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   9065:                                              ['directorysrch'],$srch->{'srchdomain'});
                   9066:     my $showdom = &display_domain_info($srch->{'srchdomain'});
                   9067:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   9068:         if ($dom_inst_srch{'directorysrch'}{'lcavailable'} eq '0') {
                   9069:             return &mt('LON-CAPA directory search is not available in domain: [_1]',$showdom);
                   9070:         }
                   9071:         if ($dom_inst_srch{'directorysrch'}{'lclocalonly'}) {
                   9072:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
                   9073:                 return &mt('LON-CAPA directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom);
                   9074:             }
                   9075:         }
                   9076:     }
                   9077:     return 'ok';
                   9078: }
                   9079: 
                   9080: sub instdirectorysrch_check {
1.160     raeburn  9081:     my ($srch) = @_;
                   9082:     my $can_search = 0;
                   9083:     my $response;
                   9084:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   9085:                                              ['directorysrch'],$srch->{'srchdomain'});
1.180     raeburn  9086:     my $showdom = &display_domain_info($srch->{'srchdomain'});
1.160     raeburn  9087:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   9088:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
1.180     raeburn  9089:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
1.160     raeburn  9090:         }
                   9091:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
                   9092:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
1.180     raeburn  9093:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
1.160     raeburn  9094:             }
                   9095:             my @usertypes = split(/:/,$env{'environment.inststatus'});
                   9096:             if (!@usertypes) {
                   9097:                 push(@usertypes,'default');
                   9098:             }
                   9099:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
                   9100:                 foreach my $type (@usertypes) {
                   9101:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
                   9102:                         $can_search = 1;
                   9103:                         last;
                   9104:                     }
                   9105:                 }
                   9106:             }
                   9107:             if (!$can_search) {
                   9108:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
                   9109:                 my @longtypes; 
                   9110:                 foreach my $item (@usertypes) {
1.229     raeburn  9111:                     if (defined($insttypes->{$item})) { 
                   9112:                         push (@longtypes,$insttypes->{$item});
                   9113:                     } elsif ($item eq 'default') {
                   9114:                         push (@longtypes,&mt('other')); 
                   9115:                     }
1.160     raeburn  9116:                 }
                   9117:                 my $insttype_str = join(', ',@longtypes); 
1.180     raeburn  9118:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
1.229     raeburn  9119:             }
1.160     raeburn  9120:         } else {
                   9121:             $can_search = 1;
                   9122:         }
                   9123:     } else {
1.180     raeburn  9124:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
1.160     raeburn  9125:     }
                   9126:     my %longtext = &Apache::lonlocal::texthash (
1.167     albertel 9127:                        uname     => 'username',
1.160     raeburn  9128:                        lastfirst => 'last name, first name',
1.167     albertel 9129:                        lastname  => 'last name',
1.172     raeburn  9130:                        contains  => 'contains',
1.178     raeburn  9131:                        exact     => 'as exact match to',
                   9132:                        begins    => 'begins with',
1.160     raeburn  9133:                    );
                   9134:     if ($can_search) {
                   9135:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
                   9136:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
1.180     raeburn  9137:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
1.160     raeburn  9138:             }
                   9139:         } else {
1.180     raeburn  9140:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
1.160     raeburn  9141:         }
                   9142:     }
                   9143:     if ($can_search) {
1.178     raeburn  9144:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
                   9145:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
                   9146:                 return 'ok';
                   9147:             } else {
1.180     raeburn  9148:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  9149:             }
                   9150:         } else {
                   9151:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
                   9152:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
                   9153:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
                   9154:                 return 'ok';
                   9155:             } else {
1.180     raeburn  9156:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  9157:             }
1.160     raeburn  9158:         }
                   9159:     }
                   9160: }
                   9161: 
                   9162: sub get_courseusers {
                   9163:     my %advhash;
1.167     albertel 9164:     my $classlist = &Apache::loncoursedata::get_classlist();
1.160     raeburn  9165:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                   9166:     foreach my $role (sort(keys(%coursepersonnel))) {
                   9167:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
1.167     albertel 9168: 	    if (!exists($classlist->{$user})) {
                   9169: 		$classlist->{$user} = [];
                   9170: 	    }
1.160     raeburn  9171:         }
                   9172:     }
1.167     albertel 9173:     return $classlist;
1.160     raeburn  9174: }
                   9175: 
                   9176: sub build_search_response {
1.221     raeburn  9177:     my ($context,$srch,%srch_results) = @_;
1.179     raeburn  9178:     my ($currstate,$response,$forcenewuser);
1.160     raeburn  9179:     my %names = (
1.330     bisitz   9180:           'uname'     => 'username',
                   9181:           'lastname'  => 'last name',
1.160     raeburn  9182:           'lastfirst' => 'last name, first name',
1.330     bisitz   9183:           'crs'       => 'this course',
                   9184:           'dom'       => 'LON-CAPA domain',
                   9185:           'instd'     => 'the institutional directory for domain',
1.160     raeburn  9186:     );
                   9187: 
                   9188:     my %single = (
1.180     raeburn  9189:                    begins   => 'A match',
1.160     raeburn  9190:                    contains => 'A match',
1.180     raeburn  9191:                    exact    => 'An exact match',
1.160     raeburn  9192:                  );
                   9193:     my %nomatch = (
1.180     raeburn  9194:                    begins   => 'No match',
1.160     raeburn  9195:                    contains => 'No match',
1.180     raeburn  9196:                    exact    => 'No exact match',
1.160     raeburn  9197:                   );
                   9198:     if (keys(%srch_results) > 1) {
1.179     raeburn  9199:         $currstate = 'select';
1.160     raeburn  9200:     } else {
                   9201:         if (keys(%srch_results) == 1) {
1.416     raeburn  9202:             if ($env{'form.action'} eq 'accesslogs') {
                   9203:                 $currstate = 'activity';
                   9204:             } else {
                   9205:                 $currstate = 'modify';
                   9206:             }
1.180     raeburn  9207:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
                   9208:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   9209:                 $response .= ': '.&display_domain_info($srch->{'srchdomain'});
1.180     raeburn  9210:             }
1.330     bisitz   9211:         } else { # Search has nothing found. Prepare message to user.
                   9212:             $response = '<span class="LC_warning">';
1.180     raeburn  9213:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   9214:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}: [_2]",
                   9215:                                  '<b>'.$srch->{'srchterm'}.'</b>',
                   9216:                                  &display_domain_info($srch->{'srchdomain'}));
                   9217:             } else {
                   9218:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}.",
                   9219:                                  '<b>'.$srch->{'srchterm'}.'</b>');
1.180     raeburn  9220:             }
                   9221:             $response .= '</span>';
1.330     bisitz   9222: 
1.160     raeburn  9223:             if ($srch->{'srchin'} ne 'alc') {
                   9224:                 $forcenewuser = 1;
                   9225:                 my $cansrchinst = 0; 
1.438     raeburn  9226:                 if (($srch->{'srchdomain'}) && ($env{'form.action'} ne 'accesslogs')) {
1.160     raeburn  9227:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
                   9228:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
                   9229:                         if ($domconfig{'directorysrch'}{'available'}) {
                   9230:                             $cansrchinst = 1;
                   9231:                         } 
                   9232:                     }
                   9233:                 }
1.180     raeburn  9234:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
                   9235:                      ($srch->{'srchby'} eq 'lastname')) &&
                   9236:                     ($srch->{'srchin'} eq 'dom')) {
                   9237:                     if ($cansrchinst) {
                   9238:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
1.160     raeburn  9239:                     }
                   9240:                 }
1.180     raeburn  9241:                 if ($srch->{'srchin'} eq 'crs') {
                   9242:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
                   9243:                 }
                   9244:             }
1.305     raeburn  9245:             my $createdom = $env{'request.role.domain'};
                   9246:             if ($context eq 'requestcrs') {
                   9247:                 if ($env{'form.coursedom'} ne '') {
                   9248:                     $createdom = $env{'form.coursedom'};
                   9249:                 }
                   9250:             }
1.416     raeburn  9251:             unless (($env{'form.action'} eq 'accesslogs') || (($srch->{'srchby'} eq 'uname') && ($srch->{'srchin'} eq 'dom') &&
                   9252:                     ($srch->{'srchtype'} eq 'exact') && ($srch->{'srchdomain'} eq $createdom))) {
1.221     raeburn  9253:                 my $cancreate =
1.305     raeburn  9254:                     &Apache::lonuserutils::can_create_user($createdom,$context);
                   9255:                 my $targetdom = '<span class="LC_cusr_emph">'.$createdom.'</span>';
1.221     raeburn  9256:                 if ($cancreate) {
1.305     raeburn  9257:                     my $showdom = &display_domain_info($createdom); 
1.266     bisitz   9258:                     $response .= '<br /><br />'
                   9259:                                 .'<b>'.&mt('To add a new user:').'</b>'
1.305     raeburn  9260:                                 .'<br />';
                   9261:                     if ($context eq 'requestcrs') {
                   9262:                         $response .= &mt("(You can only define new users in the new course's domain - [_1])",$targetdom);
                   9263:                     } else {
                   9264:                         $response .= &mt("(You can only create new users in your current role's domain - [_1])",$targetdom);
                   9265:                     }
                   9266:                     $response .='<ul><li>'
1.266     bisitz   9267:                                 .&mt("Set 'Domain/institution to search' to: [_1]",'<span class="LC_cusr_emph">'.$showdom.'</span>')
                   9268:                                 .'</li><li>'
                   9269:                                 .&mt("Set 'Search criteria' to: [_1]username is ..... in selected LON-CAPA domain[_2]",'<span class="LC_cusr_emph">','</span>')
                   9270:                                 .'</li><li>'
                   9271:                                 .&mt('Provide the proposed username')
                   9272:                                 .'</li><li>'
                   9273:                                 .&mt("Click 'Search'")
                   9274:                                 .'</li></ul><br />';
1.221     raeburn  9275:                 } else {
1.422     raeburn  9276:                     unless (($context eq 'domain') && ($env{'form.action'} eq 'singleuser')) {
                   9277:                         my $helplink = ' href="javascript:helpMenu('."'display'".')"';
                   9278:                         $response .= '<br /><br />';
                   9279:                         if ($context eq 'requestcrs') {
                   9280:                             $response .= &mt("You are not authorized to define new users in the new course's domain - [_1].",$targetdom);
                   9281:                         } else {
                   9282:                             $response .= &mt("You are not authorized to create new users in your current role's domain - [_1].",$targetdom);
                   9283:                         }
                   9284:                         $response .= '<br />'
                   9285:                                      .&mt('Please contact the [_1]helpdesk[_2] if you need to create a new user.'
                   9286:                                         ,' <a'.$helplink.'>'
                   9287:                                         ,'</a>')
                   9288:                                      .'<br />';
1.305     raeburn  9289:                     }
1.221     raeburn  9290:                 }
1.160     raeburn  9291:             }
                   9292:         }
                   9293:     }
1.179     raeburn  9294:     return ($currstate,$response,$forcenewuser);
1.160     raeburn  9295: }
                   9296: 
1.180     raeburn  9297: sub display_domain_info {
                   9298:     my ($dom) = @_;
                   9299:     my $output = $dom;
                   9300:     if ($dom ne '') { 
                   9301:         my $domdesc = &Apache::lonnet::domain($dom,'description');
                   9302:         if ($domdesc ne '') {
                   9303:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
                   9304:         }
                   9305:     }
                   9306:     return $output;
                   9307: }
                   9308: 
1.160     raeburn  9309: sub crumb_utilities {
                   9310:     my %elements = (
                   9311:        crtuser => {
                   9312:            srchterm => 'text',
1.172     raeburn  9313:            srchin => 'selectbox',
1.160     raeburn  9314:            srchby => 'selectbox',
                   9315:            srchtype => 'selectbox',
                   9316:            srchdomain => 'selectbox',
                   9317:        },
1.207     raeburn  9318:        crtusername => {
                   9319:            srchterm => 'text',
                   9320:            srchdomain => 'selectbox',
                   9321:        },
1.160     raeburn  9322:        docustom => {
                   9323:            rolename => 'selectbox',
                   9324:            newrolename => 'textbox',
                   9325:        },
1.179     raeburn  9326:        studentform => {
                   9327:            srchterm => 'text',
                   9328:            srchin => 'selectbox',
                   9329:            srchby => 'selectbox',
                   9330:            srchtype => 'selectbox',
                   9331:            srchdomain => 'selectbox',
                   9332:        },
1.160     raeburn  9333:     );
                   9334: 
                   9335:     my $jsback .= qq|
                   9336: function backPage(formname,prevphase,prevstate) {
1.211     raeburn  9337:     if (typeof prevphase == 'undefined') {
                   9338:         formname.phase.value = '';
                   9339:     }
                   9340:     else {  
                   9341:         formname.phase.value = prevphase;
                   9342:     }
                   9343:     if (typeof prevstate == 'undefined') {
                   9344:         formname.currstate.value = '';
                   9345:     }
                   9346:     else {
                   9347:         formname.currstate.value = prevstate;
                   9348:     }
1.160     raeburn  9349:     formname.submit();
                   9350: }
                   9351: |;
                   9352:     return ($jsback,\%elements);
                   9353: }
                   9354: 
1.26      matthew  9355: sub course_level_table {
1.375     raeburn  9356:     my ($inccourses,$showcredits,$defaultcredits) = @_;
                   9357:     return unless (ref($inccourses) eq 'HASH');
1.26      matthew  9358:     my $table = '';
1.62      www      9359: # Custom Roles?
                   9360: 
1.190     raeburn  9361:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.89      raeburn  9362:     my %lt=&Apache::lonlocal::texthash(
                   9363:             'exs'  => "Existing sections",
                   9364:             'new'  => "Define new section",
                   9365:             'ssd'  => "Set Start Date",
                   9366:             'sed'  => "Set End Date",
1.131     raeburn  9367:             'crl'  => "Course Level",
1.89      raeburn  9368:             'act'  => "Activate",
                   9369:             'rol'  => "Role",
                   9370:             'ext'  => "Extent",
1.113     raeburn  9371:             'grs'  => "Section",
1.375     raeburn  9372:             'crd'  => "Credits",
1.89      raeburn  9373:             'sta'  => "Start",
                   9374:             'end'  => "End"
                   9375:     );
1.62      www      9376: 
1.375     raeburn  9377:     foreach my $protectedcourse (sort(keys(%{$inccourses}))) {
1.135     raeburn  9378: 	my $thiscourse=$protectedcourse;
1.26      matthew  9379: 	$thiscourse=~s:_:/:g;
                   9380: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
1.365     raeburn  9381:         my $isowner = &Apache::lonuserutils::is_courseowner($protectedcourse,$coursedata{'internal.courseowner'});
1.26      matthew  9382: 	my $area=$coursedata{'description'};
1.321     raeburn  9383:         my $crstype=$coursedata{'type'};
1.135     raeburn  9384: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
1.89      raeburn  9385: 	my ($domain,$cnum)=split(/\//,$thiscourse);
1.115     albertel 9386:         my %sections_count;
1.101     albertel 9387:         if (defined($env{'request.course.id'})) {
                   9388:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
1.115     albertel 9389:                 %sections_count = 
                   9390: 		    &Apache::loncommon::get_sections($domain,$cnum);
1.92      raeburn  9391:             }
                   9392:         }
1.321     raeburn  9393:         my @roles = &Apache::lonuserutils::roles_by_context('course','',$crstype);
1.213     raeburn  9394: 	foreach my $role (@roles) {
1.321     raeburn  9395:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.329     raeburn  9396: 	    if ((&Apache::lonnet::allowed('c'.$role,$thiscourse)) ||
                   9397:                 ((($role eq 'cc') || ($role eq 'co')) && ($isowner))) {
1.221     raeburn  9398:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  9399:                                             $plrole,\%sections_count,\%lt,
1.402     raeburn  9400:                                             $showcredits,$defaultcredits,$crstype);
1.221     raeburn  9401:             } elsif ($env{'request.course.sec'} ne '') {
                   9402:                 if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
                   9403:                                              $env{'request.course.sec'})) {
                   9404:                     $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  9405:                                                 $plrole,\%sections_count,\%lt,
1.402     raeburn  9406:                                                 $showcredits,$defaultcredits,$crstype);
1.26      matthew  9407:                 }
                   9408:             }
                   9409:         }
1.221     raeburn  9410:         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
1.324     raeburn  9411:             foreach my $cust (sort(keys(%customroles))) {
                   9412:                 next if ($crstype eq 'Community' && $customroles{$cust} =~ /bre\&S/);
1.221     raeburn  9413:                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
                   9414:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.402     raeburn  9415:                                             $cust,\%sections_count,\%lt,
                   9416:                                             $showcredits,$defaultcredits,$crstype);
1.221     raeburn  9417:             }
1.62      www      9418: 	}
1.26      matthew  9419:     }
                   9420:     return '' if ($table eq ''); # return nothing if there is nothing 
                   9421:                                  # in the table
1.188     raeburn  9422:     my $result;
                   9423:     if (!$env{'request.course.id'}) {
                   9424:         $result = '<h4>'.$lt{'crl'}.'</h4>'."\n";
                   9425:     }
                   9426:     $result .= 
1.136     raeburn  9427: &Apache::loncommon::start_data_table().
                   9428: &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  9429: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
1.402     raeburn  9430: '<th>'.$lt{'ext'}.'</th><th>'."\n";
                   9431:     if ($showcredits) {
                   9432:         $result .= $lt{'crd'}.'</th>';
                   9433:     }
                   9434:     $result .=
1.375     raeburn  9435: '<th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th>'."\n".
                   9436: '<th>'.$lt{'end'}.'</th>'.
1.136     raeburn  9437: &Apache::loncommon::end_data_table_header_row().
                   9438: $table.
                   9439: &Apache::loncommon::end_data_table();
1.26      matthew  9440:     return $result;
                   9441: }
1.88      raeburn  9442: 
1.221     raeburn  9443: sub course_level_row {
1.375     raeburn  9444:     my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,
1.402     raeburn  9445:         $lt,$showcredits,$defaultcredits,$crstype) = @_;
1.375     raeburn  9446:     my $creditem;
1.222     raeburn  9447:     my $row = &Apache::loncommon::start_data_table_row().
                   9448:               ' <td><input type="checkbox" name="act_'.
                   9449:               $protectedcourse.'_'.$role.'" /></td>'."\n".
                   9450:               ' <td>'.$plrole.'</td>'."\n".
                   9451:               ' <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
1.402     raeburn  9452:     if (($showcredits) && ($role eq 'st') && ($crstype eq 'Course')) {
1.375     raeburn  9453:         $row .= 
                   9454:             '<td><input type="text" name="credits_'.$protectedcourse.'_'.
                   9455:             $role.'" size="3" value="'.$defaultcredits.'" /></td>';
                   9456:     } else {
                   9457:         $row .= '<td>&nbsp;</td>';
                   9458:     }
1.322     raeburn  9459:     if (($role eq 'cc') || ($role eq 'co')) {
1.222     raeburn  9460:         $row .= '<td>&nbsp;</td>';
1.221     raeburn  9461:     } elsif ($env{'request.course.sec'} ne '') {
1.222     raeburn  9462:         $row .= ' <td><input type="hidden" value="'.
                   9463:                 $env{'request.course.sec'}.'" '.
                   9464:                 'name="sec_'.$protectedcourse.'_'.$role.'" />'.
                   9465:                 $env{'request.course.sec'}.'</td>';
1.221     raeburn  9466:     } else {
                   9467:         if (ref($sections_count) eq 'HASH') {
                   9468:             my $currsec = 
                   9469:                 &Apache::lonuserutils::course_sections($sections_count,
                   9470:                                                        $protectedcourse.'_'.$role);
1.222     raeburn  9471:             $row .= '<td><table class="LC_createuser">'."\n".
                   9472:                     '<tr class="LC_section_row">'."\n".
                   9473:                     ' <td valign="top">'.$lt->{'exs'}.'<br />'.
                   9474:                        $currsec.'</td>'."\n".
                   9475:                      ' <td>&nbsp;&nbsp;</td>'."\n".
                   9476:                      ' <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
1.221     raeburn  9477:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
                   9478:                      '" value="" />'.
                   9479:                      '<input type="hidden" '.
                   9480:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
1.222     raeburn  9481:                      '</tr></table></td>'."\n";
1.221     raeburn  9482:         } else {
1.222     raeburn  9483:             $row .= '<td><input type="text" size="10" '.
1.375     raeburn  9484:                     'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n";
1.221     raeburn  9485:         }
                   9486:     }
1.222     raeburn  9487:     $row .= <<ENDTIMEENTRY;
                   9488: <td><input type="hidden" name="start_$protectedcourse\_$role" value="" />
1.221     raeburn  9489: <a href=
                   9490: "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  9491: <td><input type="hidden" name="end_$protectedcourse\_$role" value="" />
1.221     raeburn  9492: <a href=
                   9493: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
                   9494: ENDTIMEENTRY
1.222     raeburn  9495:     $row .= &Apache::loncommon::end_data_table_row();
                   9496:     return $row;
1.221     raeburn  9497: }
                   9498: 
1.88      raeburn  9499: sub course_level_dc {
1.375     raeburn  9500:     my ($dcdom,$showcredits) = @_;
1.190     raeburn  9501:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.213     raeburn  9502:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.88      raeburn  9503:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
                   9504:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
1.133     raeburn  9505:                       '<input type="hidden" name="dccourse" value="" />';
1.355     www      9506:     my $courseform=&Apache::loncommon::selectcourse_link
1.356     raeburn  9507:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Select','crstype');
1.375     raeburn  9508:     my $credit_elem;
                   9509:     if ($showcredits) {
                   9510:         $credit_elem = 'credits';
                   9511:     }
                   9512:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu','role','Course/Community Browser',$credit_elem);
1.88      raeburn  9513:     my %lt=&Apache::lonlocal::texthash(
                   9514:                     'rol'  => "Role",
1.113     raeburn  9515:                     'grs'  => "Section",
1.88      raeburn  9516:                     'exs'  => "Existing sections",
                   9517:                     'new'  => "Define new section", 
                   9518:                     'sta'  => "Start",
                   9519:                     'end'  => "End",
                   9520:                     'ssd'  => "Set Start Date",
1.355     www      9521:                     'sed'  => "Set End Date",
1.375     raeburn  9522:                     'scc'  => "Course/Community",
                   9523:                     'crd'  => "Credits",
1.88      raeburn  9524:                   );
1.323     raeburn  9525:     my $header = '<h4>'.&mt('Course/Community Level').'</h4>'.
1.136     raeburn  9526:                  &Apache::loncommon::start_data_table().
                   9527:                  &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  9528:                  '<th>'.$lt{'scc'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
1.397     bisitz   9529:                  '<th>'.$lt{'grs'}.'</th>'."\n";
                   9530:     $header .=   '<th>'.$lt{'crd'}.'</th>'."\n" if ($showcredits);
                   9531:     $header .=   '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'."\n".
1.136     raeburn  9532:                  &Apache::loncommon::end_data_table_header_row();
1.143     raeburn  9533:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
1.356     raeburn  9534:                      '<td><br /><span class="LC_nobreak"><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc','','','','crstype'".')" />'.
                   9535:                      $courseform.('&nbsp;' x4).'</span></td>'."\n".
1.389     bisitz   9536:                      '<td valign="top"><br /><select name="role">'."\n";
1.213     raeburn  9537:     foreach my $role (@roles) {
1.135     raeburn  9538:         my $plrole=&Apache::lonnet::plaintext($role);
1.389     bisitz   9539:         $otheritems .= '  <option value="'.$role.'">'.$plrole.'</option>';
1.88      raeburn  9540:     }
1.404     raeburn  9541:     if ( keys(%customroles) > 0) {
                   9542:         foreach my $cust (sort(keys(%customroles))) {
1.101     albertel 9543:             my $custrole='cr_cr_'.$env{'user.domain'}.
1.135     raeburn  9544:                     '_'.$env{'user.name'}.'_'.$cust;
1.389     bisitz   9545:             $otheritems .= '  <option value="'.$custrole.'">'.$cust.'</option>';
1.88      raeburn  9546:         }
                   9547:     }
                   9548:     $otheritems .= '</select></td><td>'.
                   9549:                      '<table border="0" cellspacing="0" cellpadding="0">'.
                   9550:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
1.389     bisitz   9551:                      ' <option value="">&lt;--'.&mt('Pick course first').'</option></select></td>'.
1.88      raeburn  9552:                      '<td>&nbsp;&nbsp;</td>'.
                   9553:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
1.113     raeburn  9554:                      '<input type="text" name="newsec" value="" />'.
1.237     raeburn  9555:                      '<input type="hidden" name="section" value="" />'.
1.323     raeburn  9556:                      '<input type="hidden" name="groups" value="" />'.
                   9557:                      '<input type="hidden" name="crstype" value="" /></td>'.
1.375     raeburn  9558:                      '</tr></table></td>'."\n";
                   9559:     if ($showcredits) {
                   9560:         $otheritems .= '<td><br />'."\n".
1.397     bisitz   9561:                        '<input type="text" size="3" name="credits" value="" /></td>'."\n";
1.375     raeburn  9562:     }
1.88      raeburn  9563:     $otheritems .= <<ENDTIMEENTRY;
1.323     raeburn  9564: <td><br /><input type="hidden" name="start" value='' />
1.88      raeburn  9565: <a href=
                   9566: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.323     raeburn  9567: <td><br /><input type="hidden" name="end" value='' />
1.88      raeburn  9568: <a href=
                   9569: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
                   9570: ENDTIMEENTRY
1.136     raeburn  9571:     $otheritems .= &Apache::loncommon::end_data_table_row().
                   9572:                    &Apache::loncommon::end_data_table()."\n";
1.88      raeburn  9573:     return $cb_jscript.$header.$hiddenitems.$otheritems;
                   9574: }
                   9575: 
1.237     raeburn  9576: sub update_selfenroll_config {
1.400     raeburn  9577:     my ($r,$cid,$cdom,$cnum,$context,$crstype,$currsettings) = @_;
1.398     raeburn  9578:     return unless (ref($currsettings) eq 'HASH');
                   9579:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
                   9580:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
1.237     raeburn  9581:     my (%changes,%warning);
1.241     raeburn  9582:     my $curr_types;
1.400     raeburn  9583:     my %noedit;
                   9584:     unless ($context eq 'domain') {
                   9585:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
                   9586:     }
1.237     raeburn  9587:     if (ref($row) eq 'ARRAY') {
                   9588:         foreach my $item (@{$row}) {
1.400     raeburn  9589:             next if ($noedit{$item});
1.237     raeburn  9590:             if ($item eq 'enroll_dates') {
                   9591:                 my (%currenrolldate,%newenrolldate);
                   9592:                 foreach my $type ('start','end') {
1.398     raeburn  9593:                     $currenrolldate{$type} = $currsettings->{'selfenroll_'.$type.'_date'};
1.237     raeburn  9594:                     $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
                   9595:                     if ($newenrolldate{$type} ne $currenrolldate{$type}) {
                   9596:                         $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
                   9597:                     }
                   9598:                 }
                   9599:             } elsif ($item eq 'access_dates') {
                   9600:                 my (%currdate,%newdate);
                   9601:                 foreach my $type ('start','end') {
1.398     raeburn  9602:                     $currdate{$type} = $currsettings->{'selfenroll_'.$type.'_access'};
1.237     raeburn  9603:                     $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
                   9604:                     if ($newdate{$type} ne $currdate{$type}) {
                   9605:                         $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
                   9606:                     }
                   9607:                 }
1.241     raeburn  9608:             } elsif ($item eq 'types') {
1.398     raeburn  9609:                 $curr_types = $currsettings->{'selfenroll_'.$item};
1.241     raeburn  9610:                 if ($env{'form.selfenroll_all'}) {
                   9611:                     if ($curr_types ne '*') {
                   9612:                         $changes{'internal.selfenroll_types'} = '*';
                   9613:                     } else {
                   9614:                         next;
                   9615:                     }
                   9616:                 } else {
1.249     raeburn  9617:                     my %currdoms;
1.241     raeburn  9618:                     my @entries = split(/;/,$curr_types);
                   9619:                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
1.249     raeburn  9620:                     my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
1.241     raeburn  9621:                     my $newnum = 0;
1.249     raeburn  9622:                     my @latesttypes;
                   9623:                     foreach my $num (@activations) {
                   9624:                         my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
                   9625:                         if (@types > 0) {
1.241     raeburn  9626:                             @types = sort(@types);
                   9627:                             my $typestr = join(',',@types);
1.249     raeburn  9628:                             my $typedom = $env{'form.selfenroll_dom_'.$num};
                   9629:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   9630:                             $currdoms{$typedom} = 1;
1.241     raeburn  9631:                             $newnum ++;
                   9632:                         }
                   9633:                     }
1.338     raeburn  9634:                     for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {
                   9635:                         if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
1.249     raeburn  9636:                             my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
                   9637:                             if (@types > 0) {
                   9638:                                 @types = sort(@types);
                   9639:                                 my $typestr = join(',',@types);
                   9640:                                 my $typedom = $env{'form.selfenroll_dom_'.$j};
                   9641:                                 $latesttypes[$newnum] = $typedom.':'.$typestr;
                   9642:                                 $currdoms{$typedom} = 1;
                   9643:                                 $newnum ++;
                   9644:                             }
                   9645:                         }
                   9646:                     }
                   9647:                     if ($env{'form.selfenroll_newdom'} ne '') {
                   9648:                         my $typedom = $env{'form.selfenroll_newdom'};
                   9649:                         if ((!defined($currdoms{$typedom})) && 
                   9650:                             (&Apache::lonnet::domain($typedom) ne '')) {
                   9651:                             my $typestr;
                   9652:                             my ($othertitle,$usertypes,$types) = 
                   9653:                                 &Apache::loncommon::sorted_inst_types($typedom);
                   9654:                             my $othervalue = 'any';
                   9655:                             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   9656:                                 if (@{$types} > 0) {
1.257     raeburn  9657:                                     my @esc_types = map { &escape($_); } @{$types};
1.249     raeburn  9658:                                     $othervalue = 'other';
1.258     raeburn  9659:                                     $typestr = join(',',(@esc_types,$othervalue));
1.249     raeburn  9660:                                 }
                   9661:                                 $typestr = $othervalue;
                   9662:                             } else {
                   9663:                                 $typestr = $othervalue;
                   9664:                             } 
                   9665:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   9666:                             $newnum ++ ;
                   9667:                         }
                   9668:                     }
1.241     raeburn  9669:                     my $selfenroll_types = join(';',@latesttypes);
                   9670:                     if ($selfenroll_types ne $curr_types) {
                   9671:                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
                   9672:                     }
                   9673:                 }
1.276     raeburn  9674:             } elsif ($item eq 'limit') {
                   9675:                 my $newlimit = $env{'form.selfenroll_limit'};
                   9676:                 my $newcap = $env{'form.selfenroll_cap'};
                   9677:                 $newcap =~s/\s+//g;
1.398     raeburn  9678:                 my $currlimit =  $currsettings->{'selfenroll_limit'};
1.276     raeburn  9679:                 $currlimit = 'none' if ($currlimit eq '');
1.398     raeburn  9680:                 my $currcap = $currsettings->{'selfenroll_cap'};
1.276     raeburn  9681:                 if ($newlimit ne $currlimit) {
                   9682:                     if ($newlimit ne 'none') {
                   9683:                         if ($newcap =~ /^\d+$/) {
                   9684:                             if ($newcap ne $currcap) {
                   9685:                                 $changes{'internal.selfenroll_cap'} = $newcap;
                   9686:                             }
                   9687:                             $changes{'internal.selfenroll_limit'} = $newlimit;
                   9688:                         } else {
1.398     raeburn  9689:                             $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
                   9690:                                 &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.'); 
1.276     raeburn  9691:                         }
                   9692:                     } elsif ($currcap ne '') {
                   9693:                         $changes{'internal.selfenroll_cap'} = '';
                   9694:                         $changes{'internal.selfenroll_limit'} = $newlimit; 
                   9695:                     }
                   9696:                 } elsif ($currlimit ne 'none') {
                   9697:                     if ($newcap =~ /^\d+$/) {
                   9698:                         if ($newcap ne $currcap) {
                   9699:                             $changes{'internal.selfenroll_cap'} = $newcap;
                   9700:                         }
                   9701:                     } else {
1.398     raeburn  9702:                         $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
                   9703:                             &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.');
1.276     raeburn  9704:                     }
                   9705:                 }
                   9706:             } elsif ($item eq 'approval') {
                   9707:                 my (@currnotified,@newnotified);
1.398     raeburn  9708:                 my $currapproval = $currsettings->{'selfenroll_approval'};
                   9709:                 my $currnotifylist = $currsettings->{'selfenroll_notifylist'};
1.276     raeburn  9710:                 if ($currnotifylist ne '') {
                   9711:                     @currnotified = split(/,/,$currnotifylist);
                   9712:                     @currnotified = sort(@currnotified);
                   9713:                 }
                   9714:                 my $newapproval = $env{'form.selfenroll_approval'};
                   9715:                 @newnotified = &Apache::loncommon::get_env_multiple('form.selfenroll_notify');
                   9716:                 @newnotified = sort(@newnotified);
                   9717:                 if ($newapproval ne $currapproval) {
                   9718:                     $changes{'internal.selfenroll_approval'} = $newapproval;
                   9719:                     if (!$newapproval) {
                   9720:                         if ($currnotifylist ne '') {
                   9721:                             $changes{'internal.selfenroll_notifylist'} = '';
                   9722:                         }
                   9723:                     } else {
                   9724:                         my @differences =  
1.295     raeburn  9725:                             &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  9726:                         if (@differences > 0) {
                   9727:                             if (@newnotified > 0) {
                   9728:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   9729:                             } else {
                   9730:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   9731:                             }
                   9732:                         }
                   9733:                     }
                   9734:                 } else {
1.295     raeburn  9735:                     my @differences = &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  9736:                     if (@differences > 0) {
                   9737:                         if (@newnotified > 0) {
                   9738:                             $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   9739:                         } else {
                   9740:                             $changes{'internal.selfenroll_notifylist'} = '';
                   9741:                         }
                   9742:                     }
                   9743:                 }
1.237     raeburn  9744:             } else {
1.398     raeburn  9745:                 my $curr_val = $currsettings->{'selfenroll_'.$item};
1.237     raeburn  9746:                 my $newval = $env{'form.selfenroll_'.$item};
                   9747:                 if ($item eq 'section') {
                   9748:                     $newval = $env{'form.sections'};
1.241     raeburn  9749:                     if (defined($curr_groups{$newval})) {
1.237     raeburn  9750:                         $newval = $curr_val;
1.398     raeburn  9751:                         $warning{$item} = &mt('Section for self-enrolled users unchanged as the proposed section is a group').'<br />'.
                   9752:                                           &mt('Group names and section names must be distinct');
1.237     raeburn  9753:                     } elsif ($newval eq 'all') {
                   9754:                         $newval = $curr_val;
1.274     bisitz   9755:                         $warning{$item} = &mt('Section for self-enrolled users unchanged, as "all" is a reserved section name.');
1.237     raeburn  9756:                     }
                   9757:                     if ($newval eq '') {
                   9758:                         $newval = 'none';
                   9759:                     }
                   9760:                 }
                   9761:                 if ($newval ne $curr_val) {
                   9762:                     $changes{'internal.selfenroll_'.$item} = $newval;
                   9763:                 }
1.241     raeburn  9764:             }
1.237     raeburn  9765:         }
                   9766:         if (keys(%warning) > 0) {
                   9767:             foreach my $item (@{$row}) {
                   9768:                 if (exists($warning{$item})) {
                   9769:                     $r->print($warning{$item}.'<br />');
                   9770:                 }
                   9771:             } 
                   9772:         }
                   9773:         if (keys(%changes) > 0) {
                   9774:             my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
                   9775:             if ($putresult eq 'ok') {
                   9776:                 if ((exists($changes{'internal.selfenroll_types'})) ||
                   9777:                     (exists($changes{'internal.selfenroll_start_date'}))  ||
                   9778:                     (exists($changes{'internal.selfenroll_end_date'}))) {
                   9779:                     my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
                   9780:                                                                 $cnum,undef,undef,'Course');
                   9781:                     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
1.398     raeburn  9782:                     if (ref($crsinfo{$cid}) eq 'HASH') {
1.237     raeburn  9783:                         foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
                   9784:                             if (exists($changes{'internal.'.$item})) {
1.398     raeburn  9785:                                 $crsinfo{$cid}{$item} = $changes{'internal.'.$item};
1.237     raeburn  9786:                             }
                   9787:                         }
                   9788:                         my $crsputresult =
                   9789:                             &Apache::lonnet::courseidput($cdom,\%crsinfo,
                   9790:                                                          $chome,'notime');
                   9791:                     }
                   9792:                 }
                   9793:                 $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
                   9794:                 foreach my $item (@{$row}) {
                   9795:                     my $title = $item;
                   9796:                     if (ref($lt) eq 'HASH') {
                   9797:                         $title = $lt->{$item};
                   9798:                     }
                   9799:                     if ($item eq 'enroll_dates') {
                   9800:                         foreach my $type ('start','end') {
                   9801:                             if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
                   9802:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
1.244     bisitz   9803:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  9804:                                           $title,$type,$newdate).'</li>');
                   9805:                             }
                   9806:                         }
                   9807:                     } elsif ($item eq 'access_dates') {
                   9808:                         foreach my $type ('start','end') {
                   9809:                             if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
                   9810:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
1.244     bisitz   9811:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  9812:                                           $title,$type,$newdate).'</li>');
                   9813:                             }
                   9814:                         }
1.276     raeburn  9815:                     } elsif ($item eq 'limit') {
                   9816:                         if ((exists($changes{'internal.selfenroll_limit'})) ||
                   9817:                             (exists($changes{'internal.selfenroll_cap'}))) {
                   9818:                             my ($newval,$newcap);
                   9819:                             if ($changes{'internal.selfenroll_cap'} ne '') {
                   9820:                                 $newcap = $changes{'internal.selfenroll_cap'}
                   9821:                             } else {
1.398     raeburn  9822:                                 $newcap = $currsettings->{'selfenroll_cap'};
1.276     raeburn  9823:                             }
                   9824:                             if ($changes{'internal.selfenroll_limit'} eq 'none') {
                   9825:                                 $newval = &mt('No limit');
                   9826:                             } elsif ($changes{'internal.selfenroll_limit'} eq 
                   9827:                                      'allstudents') {
                   9828:                                 $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   9829:                             } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
                   9830:                                 $newval = &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
                   9831:                             } else {
1.398     raeburn  9832:                                 my $currlimit =  $currsettings->{'selfenroll_limit'};
1.276     raeburn  9833:                                 if ($currlimit eq 'allstudents') {
                   9834:                                     $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   9835:                                 } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
1.308     raeburn  9836:                                     $newval =  &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
1.276     raeburn  9837:                                 }
                   9838:                             }
                   9839:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
                   9840:                         }
                   9841:                     } elsif ($item eq 'approval') {
                   9842:                         if ((exists($changes{'internal.selfenroll_approval'})) ||
                   9843:                             (exists($changes{'internal.selfenroll_notifylist'}))) {
1.398     raeburn  9844:                             my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
1.276     raeburn  9845:                             my ($newval,$newnotify);
                   9846:                             if (exists($changes{'internal.selfenroll_notifylist'})) {
                   9847:                                 $newnotify = $changes{'internal.selfenroll_notifylist'};
                   9848:                             } else {   
1.398     raeburn  9849:                                 $newnotify = $currsettings->{'selfenroll_notifylist'};
1.276     raeburn  9850:                             }
1.398     raeburn  9851:                             if (exists($changes{'internal.selfenroll_approval'})) {
                   9852:                                 if ($changes{'internal.selfenroll_approval'} !~ /^[012]$/) {
                   9853:                                     $changes{'internal.selfenroll_approval'} = '0';
                   9854:                                 }
                   9855:                                 $newval = $selfdescs{'approval'}{$changes{'internal.selfenroll_approval'}};
1.276     raeburn  9856:                             } else {
1.398     raeburn  9857:                                 my $currapproval = $currsettings->{'selfenroll_approval'}; 
                   9858:                                 if ($currapproval !~ /^[012]$/) {
                   9859:                                     $currapproval = 0;
1.276     raeburn  9860:                                 }
1.398     raeburn  9861:                                 $newval = $selfdescs{'approval'}{$currapproval};
1.276     raeburn  9862:                             }
                   9863:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval));
                   9864:                             if ($newnotify) {
1.277     raeburn  9865:                                 $r->print('<br />'.&mt('The following will be notified when an enrollment request needs approval, or has been approved: [_1].',$newnotify));
1.276     raeburn  9866:                             } else {
1.277     raeburn  9867:                                 $r->print('<br />'.&mt('No notifications sent when an enrollment request needs approval, or has been approved.'));
1.276     raeburn  9868:                             }
                   9869:                             $r->print('</li>'."\n");
                   9870:                         }
1.237     raeburn  9871:                     } else {
                   9872:                         if (exists($changes{'internal.selfenroll_'.$item})) {
1.241     raeburn  9873:                             my $newval = $changes{'internal.selfenroll_'.$item};
                   9874:                             if ($item eq 'types') {
                   9875:                                 if ($newval eq '') {
                   9876:                                     $newval = &mt('None');
                   9877:                                 } elsif ($newval eq '*') {
                   9878:                                     $newval = &mt('Any user in any domain');
                   9879:                                 }
1.245     raeburn  9880:                             } elsif ($item eq 'registered') {
                   9881:                                 if ($newval eq '1') {
                   9882:                                     $newval = &mt('Yes');
                   9883:                                 } elsif ($newval eq '0') {
                   9884:                                     $newval = &mt('No');
                   9885:                                 }
1.241     raeburn  9886:                             }
1.244     bisitz   9887:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
1.237     raeburn  9888:                         }
                   9889:                     }
                   9890:                 }
                   9891:                 $r->print('</ul>');
1.398     raeburn  9892:                 if ($env{'course.'.$cid.'.description'} ne '') {
                   9893:                     my %newenvhash;
                   9894:                     foreach my $key (keys(%changes)) {
                   9895:                         $newenvhash{'course.'.$cid.'.'.$key} = $changes{$key};
                   9896:                     }
                   9897:                     &Apache::lonnet::appenv(\%newenvhash);
1.237     raeburn  9898:                 }
                   9899:             } else {
1.398     raeburn  9900:                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.
                   9901:                           &mt('The error was: [_1].',$putresult));
1.237     raeburn  9902:             }
                   9903:         } else {
1.249     raeburn  9904:             $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.237     raeburn  9905:         }
                   9906:     } else {
1.249     raeburn  9907:         $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.241     raeburn  9908:     }
1.400     raeburn  9909:     my $visactions = &cat_visibility();
                   9910:     my ($cathash,%cattype);
                   9911:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   9912:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   9913:         $cathash = $domconfig{'coursecategories'}{'cats'};
                   9914:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
                   9915:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
                   9916:     } else {
                   9917:         $cathash = {};
                   9918:         $cattype{'auth'} = 'std';
                   9919:         $cattype{'unauth'} = 'std';
                   9920:     }
                   9921:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
                   9922:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   9923:                   '<br />'.
                   9924:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   9925:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
                   9926:                   '</ul>');
                   9927:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
                   9928:         if ($currsettings->{'uniquecode'}) {
                   9929:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
                   9930:         } else {
1.366     bisitz   9931:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
1.400     raeburn  9932:                   '<br />'.
                   9933:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   9934:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
                   9935:                   '</ul><br />');
                   9936:         }
                   9937:     } else {
                   9938:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
                   9939:         if (ref($visactions) eq 'HASH') {
                   9940:             if (!$visible) {
                   9941:                 $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   9942:                           '<br />');
                   9943:                 if (ref($vismsgs) eq 'ARRAY') {
                   9944:                     $r->print('<br />'.$visactions->{'take'}.'<ul>');
                   9945:                     foreach my $item (@{$vismsgs}) {
                   9946:                         $r->print('<li>'.$visactions->{$item}.'</li>');
                   9947:                     }
                   9948:                     $r->print('</ul>');
1.256     raeburn  9949:                 }
1.400     raeburn  9950:                 $r->print($cansetvis);
1.256     raeburn  9951:             }
                   9952:         }
                   9953:     } 
1.237     raeburn  9954:     return;
                   9955: }
                   9956: 
1.27      matthew  9957: #---------------------------------------------- end functions for &phase_two
1.29      matthew  9958: 
                   9959: #--------------------------------- functions for &phase_two and &phase_three
                   9960: 
                   9961: #--------------------------end of functions for &phase_two and &phase_three
1.372     raeburn  9962: 
1.1       www      9963: 1;
                   9964: __END__
1.2       www      9965: 
                   9966: 

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