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

1.20      harris41    1: # The LearningOnline Network with CAPA
1.1       www         2: # Create a user
                      3: #
1.427   ! raeburn     4: # $Id: loncreateuser.pm,v 1.426 2016/12/26 18:43:52 raeburn Exp $
1.22      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.20      harris41   28: ###
                     29: 
1.1       www        30: package Apache::loncreateuser;
1.66      bowersj2   31: 
                     32: =pod
                     33: 
                     34: =head1 NAME
                     35: 
1.263     jms        36: Apache::loncreateuser.pm
1.66      bowersj2   37: 
                     38: =head1 SYNOPSIS
                     39: 
1.263     jms        40:     Handler to create users and custom roles
                     41: 
                     42:     Provides an Apache handler for creating users,
1.66      bowersj2   43:     editing their login parameters, roles, and removing roles, and
                     44:     also creating and assigning custom roles.
                     45: 
                     46: =head1 OVERVIEW
                     47: 
                     48: =head2 Custom Roles
                     49: 
                     50: In LON-CAPA, roles are actually collections of privileges. "Teaching
                     51: Assistant", "Course Coordinator", and other such roles are really just
                     52: collection of privileges that are useful in many circumstances.
                     53: 
1.324     raeburn    54: Custom roles can be defined by a Domain Coordinator, Course Coordinator
                     55: or Community Coordinator via the Manage User functionality.
                     56: The custom role editor screen will show all privileges which can be
                     57: assigned to users. For a complete list of privileges, please see 
                     58: C</home/httpd/lonTabs/rolesplain.tab>.
1.66      bowersj2   59: 
1.324     raeburn    60: Custom role definitions are stored in the C<roles.db> file of the creator
                     61: of the role.
1.66      bowersj2   62: 
                     63: =cut
1.1       www        64: 
                     65: use strict;
                     66: use Apache::Constants qw(:common :http);
                     67: use Apache::lonnet;
1.54      bowersj2   68: use Apache::loncommon;
1.68      www        69: use Apache::lonlocal;
1.117     raeburn    70: use Apache::longroup;
1.190     raeburn    71: use Apache::lonuserutils;
1.307     raeburn    72: use Apache::loncoursequeueadmin;
1.139     albertel   73: use LONCAPA qw(:DEFAULT :match);
1.1       www        74: 
1.20      harris41   75: my $loginscript; # piece of javascript used in two separate instances
                     76: my $authformnop;
                     77: my $authformkrb;
                     78: my $authformint;
                     79: my $authformfsys;
                     80: my $authformloc;
                     81: 
1.94      matthew    82: sub initialize_authen_forms {
1.227     raeburn    83:     my ($dom,$formname,$curr_authtype,$mode) = @_;
                     84:     my ($krbdef,$krbdefdom) = &Apache::loncommon::get_kerberos_defaults($dom);
                     85:     my %param = ( formname => $formname,
1.187     raeburn    86:                   kerb_def_dom => $krbdefdom,
1.227     raeburn    87:                   kerb_def_auth => $krbdef,
1.187     raeburn    88:                   domain => $dom,
                     89:                 );
1.188     raeburn    90:     my %abv_auth = &auth_abbrev();
1.227     raeburn    91:     if ($curr_authtype =~ /^(krb4|krb5|internal|localauth|unix):(.*)$/) {
1.188     raeburn    92:         my $long_auth = $1;
1.227     raeburn    93:         my $curr_autharg = $2;
1.188     raeburn    94:         my %abv_auth = &auth_abbrev();
                     95:         $param{'curr_authtype'} = $abv_auth{$long_auth};
                     96:         if ($long_auth =~ /^krb(4|5)$/) {
                     97:             $param{'curr_kerb_ver'} = $1;
1.227     raeburn    98:             $param{'curr_autharg'} = $curr_autharg;
1.188     raeburn    99:         }
1.205     raeburn   100:         if ($mode eq 'modifyuser') {
                    101:             $param{'mode'} = $mode;
                    102:         }
1.187     raeburn   103:     }
1.227     raeburn   104:     $loginscript  = &Apache::loncommon::authform_header(%param);
                    105:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
1.31      matthew   106:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
                    107:     $authformint  = &Apache::loncommon::authform_internal(%param);
                    108:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
                    109:     $authformloc  = &Apache::loncommon::authform_local(%param);
1.20      harris41  110: }
                    111: 
1.188     raeburn   112: sub auth_abbrev {
                    113:     my %abv_auth = (
1.368     raeburn   114:                      krb5      => 'krb',
                    115:                      krb4      => 'krb',
                    116:                      internal  => 'int',
                    117:                      localauth => 'loc',
                    118:                      unix      => 'fsys',
1.188     raeburn   119:                    );
                    120:     return %abv_auth;
                    121: }
1.43      www       122: 
1.134     raeburn   123: # ====================================================
                    124: 
1.378     raeburn   125: sub user_quotas {
1.134     raeburn   126:     my ($ccuname,$ccdomain) = @_;
                    127:     my %lt = &Apache::lonlocal::texthash(
1.267     raeburn   128:                    'usrt'      => "User Tools",
                    129:                    'cust'      => "Custom quota",
                    130:                    'chqu'      => "Change quota",
1.134     raeburn   131:     );
1.378     raeburn   132:    
1.149     raeburn   133:     my $quota_javascript = <<"END_SCRIPT";
                    134: <script type="text/javascript">
1.301     bisitz    135: // <![CDATA[
1.378     raeburn   136: function quota_changes(caller,context) {
                    137:     var customoff = document.getElementById('custom_'+context+'quota_off');
                    138:     var customon = document.getElementById('custom_'+context+'quota_on');
                    139:     var number = document.getElementById(context+'quota');
1.149     raeburn   140:     if (caller == "custom") {
1.378     raeburn   141:         if (customoff) {
                    142:             if (customoff.checked) {
                    143:                 number.value = "";
                    144:             }
1.149     raeburn   145:         }
                    146:     }
                    147:     if (caller == "quota") {
1.378     raeburn   148:         if (customon) {
                    149:             customon.checked = true;
                    150:         }
1.149     raeburn   151:     }
1.378     raeburn   152:     return;
1.149     raeburn   153: }
1.301     bisitz    154: // ]]>
1.149     raeburn   155: </script>
                    156: END_SCRIPT
1.378     raeburn   157:     my $longinsttype;
                    158:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
1.267     raeburn   159:     my $output = $quota_javascript."\n".
                    160:                  '<h3>'.$lt{'usrt'}.'</h3>'."\n".
                    161:                  &Apache::loncommon::start_data_table();
                    162: 
1.418     raeburn   163:     if ((&Apache::lonnet::allowed('mut',$ccdomain)) ||
                    164:         (&Apache::lonnet::allowed('udp',$ccdomain))) {
1.275     raeburn   165:         $output .= &build_tools_display($ccuname,$ccdomain,'tools');
1.267     raeburn   166:     }
1.378     raeburn   167: 
                    168:     my %titles = &Apache::lonlocal::texthash (
                    169:                     portfolio => "Disk space allocated to user's portfolio files",
1.385     bisitz    170:                     author    => "Disk space allocated to user's Authoring Space (if role assigned)",
1.378     raeburn   171:                  );
                    172:     foreach my $name ('portfolio','author') {
                    173:         my ($currquota,$quotatype,$inststatus,$defquota) =
                    174:             &Apache::loncommon::get_user_quota($ccuname,$ccdomain,$name);
                    175:         if ($longinsttype eq '') { 
                    176:             if ($inststatus ne '') {
                    177:                 if ($usertypes->{$inststatus} ne '') {
                    178:                     $longinsttype = $usertypes->{$inststatus};
                    179:                 }
                    180:             }
                    181:         }
                    182:         my ($showquota,$custom_on,$custom_off,$defaultinfo);
                    183:         $custom_on = ' ';
                    184:         $custom_off = ' checked="checked" ';
                    185:         if ($quotatype eq 'custom') {
                    186:             $custom_on = $custom_off;
                    187:             $custom_off = ' ';
                    188:             $showquota = $currquota;
                    189:             if ($longinsttype eq '') {
                    190:                 $defaultinfo = &mt('For this user, the default quota would be [_1]'
1.383     raeburn   191:                               .' MB.',$defquota);
1.378     raeburn   192:             } else {
                    193:                 $defaultinfo = &mt("For this user, the default quota would be [_1]".
1.383     raeburn   194:                                    " MB, as determined by the user's institutional".
1.378     raeburn   195:                                    " affiliation ([_2]).",$defquota,$longinsttype);
                    196:             }
                    197:         } else {
                    198:             if ($longinsttype eq '') {
                    199:                 $defaultinfo = &mt('For this user, the default quota is [_1]'
1.383     raeburn   200:                               .' MB.',$defquota);
1.378     raeburn   201:             } else {
                    202:                 $defaultinfo = &mt("For this user, the default quota of [_1]".
1.383     raeburn   203:                                    " MB, is determined by the user's institutional".
1.378     raeburn   204:                                    " affiliation ([_2]).",$defquota,$longinsttype);
                    205:             }
                    206:         }
                    207: 
                    208:         if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
                    209:             $output .= '<tr class="LC_info_row">'."\n".
                    210:                        '    <td>'.$titles{$name}.'</td>'."\n".
                    211:                        '  </tr>'."\n".
                    212:                        &Apache::loncommon::start_data_table_row()."\n".
1.390     bisitz    213:                        '  <td><span class="LC_nobreak">'.
                    214:                        &mt('Current quota: [_1] MB',$currquota).'</span>&nbsp;&nbsp;'.
1.378     raeburn   215:                        $defaultinfo.'</td>'."\n".
                    216:                        &Apache::loncommon::end_data_table_row()."\n".
                    217:                        &Apache::loncommon::start_data_table_row()."\n".
                    218:                        '  <td><span class="LC_nobreak">'.$lt{'chqu'}.
                    219:                        ': <label>'.
                    220:                        '<input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_off" '.
1.379     raeburn   221:                        'value="0" '.$custom_off.' onchange="javascript:quota_changes('."'custom','$name'".');"'.
1.390     bisitz    222:                        ' /><span class="LC_nobreak">'.
                    223:                        &mt('Default ([_1] MB)',$defquota).'</span></label>&nbsp;'.
1.378     raeburn   224:                        '&nbsp;<label><input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_on" '.
1.379     raeburn   225:                        'value="1" '.$custom_on.'  onchange="javascript:quota_changes('."'custom','$name'".');"'.
1.378     raeburn   226:                        ' />'.$lt{'cust'}.':</label>&nbsp;'.
1.379     raeburn   227:                        '<input type="text" name="'.$name.'quota" id="'.$name.'quota" size ="5" '.
                    228:                        'value="'.$showquota.'" onfocus="javascript:quota_changes('."'quota','$name'".');"'.
1.390     bisitz    229:                        ' />&nbsp;'.&mt('MB').'</span></td>'."\n".
1.378     raeburn   230:                        &Apache::loncommon::end_data_table_row()."\n";
                    231:         }
                    232:     }
1.267     raeburn   233:     $output .= &Apache::loncommon::end_data_table();
1.134     raeburn   234:     return $output;
                    235: }
                    236: 
1.275     raeburn   237: sub build_tools_display {
                    238:     my ($ccuname,$ccdomain,$context) = @_;
1.306     raeburn   239:     my (@usertools,%userenv,$output,@options,%validations,%reqtitles,%reqdisplay,
1.332     raeburn   240:         $colspan,$isadv,%domconfig);
1.275     raeburn   241:     my %lt = &Apache::lonlocal::texthash (
                    242:                    'blog'       => "Personal User Blog",
                    243:                    'aboutme'    => "Personal Information Page",
1.385     bisitz    244:                    'webdav'     => "WebDAV access to Authoring Spaces (if SSL and author/co-author)",
1.275     raeburn   245:                    'portfolio'  => "Personal User Portfolio",
                    246:                    'avai'       => "Available",
                    247:                    'cusa'       => "availability",
                    248:                    'chse'       => "Change setting",
                    249:                    'usde'       => "Use default",
                    250:                    'uscu'       => "Use custom",
                    251:                    'official'   => 'Can request creation of official courses',
1.299     raeburn   252:                    'unofficial' => 'Can request creation of unofficial courses',
                    253:                    'community'  => 'Can request creation of communities',
1.384     raeburn   254:                    'textbook'   => 'Can request creation of textbook courses',
1.411     raeburn   255:                    'placement'  => 'Can request creation of placement tests',
1.362     raeburn   256:                    'requestauthor'  => 'Can request author space',
1.275     raeburn   257:     );
1.279     raeburn   258:     if ($context eq 'requestcourses') {
1.275     raeburn   259:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.299     raeburn   260:                       'requestcourses.official','requestcourses.unofficial',
1.411     raeburn   261:                       'requestcourses.community','requestcourses.textbook',
                    262:                       'requestcourses.placement');
                    263:         @usertools = ('official','unofficial','community','textbook','placement');
1.309     raeburn   264:         @options =('norequest','approval','autolimit','validate');
1.306     raeburn   265:         %validations = &Apache::lonnet::auto_courserequest_checks($ccdomain);
                    266:         %reqtitles = &courserequest_titles();
                    267:         %reqdisplay = &courserequest_display();
                    268:         $colspan = ' colspan="2"';
1.332     raeburn   269:         %domconfig =
                    270:             &Apache::lonnet::get_dom('configuration',['requestcourses'],$ccdomain);
1.418     raeburn   271:         $isadv = &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
1.362     raeburn   272:     } elsif ($context eq 'requestauthor') {
                    273:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    274:                                                     'requestauthor');
                    275:         @usertools = ('requestauthor');
                    276:         @options =('norequest','approval','automatic');
                    277:         %reqtitles = &requestauthor_titles();
                    278:         %reqdisplay = &requestauthor_display();
                    279:         $colspan = ' colspan="2"';
                    280:         %domconfig =
                    281:             &Apache::lonnet::get_dom('configuration',['requestauthor'],$ccdomain);
1.275     raeburn   282:     } else {
                    283:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.361     raeburn   284:                           'tools.aboutme','tools.portfolio','tools.blog',
                    285:                           'tools.webdav');
                    286:         @usertools = ('aboutme','blog','webdav','portfolio');
1.275     raeburn   287:     }
                    288:     foreach my $item (@usertools) {
1.306     raeburn   289:         my ($custom_access,$curr_access,$cust_on,$cust_off,$tool_on,$tool_off,
                    290:             $currdisp,$custdisp,$custradio);
1.275     raeburn   291:         $cust_off = 'checked="checked" ';
                    292:         $tool_on = 'checked="checked" ';
                    293:         $curr_access =  
                    294:             &Apache::lonnet::usertools_access($ccuname,$ccdomain,$item,undef,
                    295:                                               $context);
1.362     raeburn   296:         if ($context eq 'requestauthor') {
                    297:             if ($userenv{$context} ne '') {
                    298:                 $cust_on = ' checked="checked" ';
                    299:                 $cust_off = '';
                    300:             }  
                    301:         } elsif ($userenv{$context.'.'.$item} ne '') {
1.306     raeburn   302:             $cust_on = ' checked="checked" ';
                    303:             $cust_off = '';
                    304:         }
                    305:         if ($context eq 'requestcourses') {
                    306:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   307:                 $custom_access = &mt('Currently from default setting.');
1.306     raeburn   308:             } else {
                    309:                 $custom_access = &mt('Currently from custom setting.');
1.275     raeburn   310:             }
1.362     raeburn   311:         } elsif ($context eq 'requestauthor') {
                    312:             if ($userenv{$context} eq '') {
                    313:                 $custom_access = &mt('Currently from default setting.');
                    314:             } else {
                    315:                 $custom_access = &mt('Currently from custom setting.');
                    316:             }
1.275     raeburn   317:         } else {
1.306     raeburn   318:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   319:                 $custom_access =
1.306     raeburn   320:                     &mt('Availability determined currently from default setting.');
                    321:                 if (!$curr_access) {
                    322:                     $tool_off = 'checked="checked" ';
                    323:                     $tool_on = '';
                    324:                 }
                    325:             } else {
1.314     raeburn   326:                 $custom_access =
1.306     raeburn   327:                     &mt('Availability determined currently from custom setting.');
                    328:                 if ($userenv{$context.'.'.$item} == 0) {
                    329:                     $tool_off = 'checked="checked" ';
                    330:                     $tool_on = '';
                    331:                 }
1.275     raeburn   332:             }
                    333:         }
                    334:         $output .= '  <tr class="LC_info_row">'."\n".
1.306     raeburn   335:                    '   <td'.$colspan.'>'.$lt{$item}.'</td>'."\n".
1.275     raeburn   336:                    '  </tr>'."\n".
1.306     raeburn   337:                    &Apache::loncommon::start_data_table_row()."\n";
1.418     raeburn   338:   
1.362     raeburn   339:         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.306     raeburn   340:             my ($curroption,$currlimit);
1.362     raeburn   341:             my $envkey = $context.'.'.$item;
                    342:             if ($context eq 'requestauthor') {
                    343:                 $envkey = $context;
                    344:             }
                    345:             if ($userenv{$envkey} ne '') {
                    346:                 $curroption = $userenv{$envkey};
1.332     raeburn   347:             } else {
                    348:                 my (@inststatuses);
1.362     raeburn   349:                 if ($context eq 'requestcourses') {
                    350:                     $curroption =
                    351:                         &Apache::loncoursequeueadmin::get_processtype('course',$ccuname,$ccdomain,
                    352:                                                                       $isadv,$ccdomain,$item,
                    353:                                                                       \@inststatuses,\%domconfig);
                    354:                 } else {
                    355:                      $curroption = 
                    356:                          &Apache::loncoursequeueadmin::get_processtype('requestauthor',$ccuname,$ccdomain,
                    357:                                                                        $isadv,$ccdomain,undef,
                    358:                                                                        \@inststatuses,\%domconfig);
                    359:                 }
1.332     raeburn   360:             }
1.306     raeburn   361:             if (!$curroption) {
                    362:                 $curroption = 'norequest';
                    363:             }
                    364:             if ($curroption =~ /^autolimit=(\d*)$/) {
                    365:                 $currlimit = $1;
1.314     raeburn   366:                 if ($currlimit eq '') {
                    367:                     $currdisp = &mt('Yes, automatic creation');
                    368:                 } else {
                    369:                     $currdisp = &mt('Yes, up to [quant,_1,request]/user',$currlimit);
                    370:                 }
1.306     raeburn   371:             } else {
                    372:                 $currdisp = $reqdisplay{$curroption};
                    373:             }
                    374:             $custdisp = '<table>';
                    375:             foreach my $option (@options) {
                    376:                 my $val = $option;
                    377:                 if ($option eq 'norequest') {
                    378:                     $val = 0;
                    379:                 }
                    380:                 if ($option eq 'validate') {
                    381:                     my $canvalidate = 0;
                    382:                     if (ref($validations{$item}) eq 'HASH') {
                    383:                         if ($validations{$item}{'_custom_'}) {
                    384:                             $canvalidate = 1;
                    385:                         }
                    386:                     }
                    387:                     next if (!$canvalidate);
                    388:                 }
                    389:                 my $checked = '';
                    390:                 if ($option eq $curroption) {
                    391:                     $checked = ' checked="checked"';
                    392:                 } elsif ($option eq 'autolimit') {
                    393:                     if ($curroption =~ /^autolimit/) {
                    394:                         $checked = ' checked="checked"';
                    395:                     }
                    396:                 }
1.362     raeburn   397:                 my $name = 'crsreq_'.$item;
                    398:                 if ($context eq 'requestauthor') {
                    399:                     $name = $item;
                    400:                 }
1.306     raeburn   401:                 $custdisp .= '<tr><td><span class="LC_nobreak"><label>'.
1.362     raeburn   402:                              '<input type="radio" name="'.$name.'" '.
                    403:                              'value="'.$val.'"'.$checked.' />'.
1.306     raeburn   404:                              $reqtitles{$option}.'</label>&nbsp;';
                    405:                 if ($option eq 'autolimit') {
1.362     raeburn   406:                     $custdisp .= '<input type="text" name="'.$name.
                    407:                                  '_limit" size="1" '.
1.314     raeburn   408:                                  'value="'.$currlimit.'" /></span><br />'.
                    409:                                  $reqtitles{'unlimited'};
1.362     raeburn   410:                 } else {
                    411:                     $custdisp .= '</span>';
                    412:                 }
                    413:                 $custdisp .= '</td></tr>';
1.306     raeburn   414:             }
                    415:             $custdisp .= '</table>';
                    416:             $custradio = '</span></td><td>'.&mt('Custom setting').'<br />'.$custdisp;
                    417:         } else {
                    418:             $currdisp = ($curr_access?&mt('Yes'):&mt('No'));
1.362     raeburn   419:             my $name = $context.'_'.$item;
                    420:             if ($context eq 'requestauthor') {
                    421:                 $name = $context;
                    422:             }
1.306     raeburn   423:             $custdisp = '<span class="LC_nobreak"><label>'.
1.362     raeburn   424:                         '<input type="radio" name="'.$name.'"'.
1.361     raeburn   425:                         ' value="1" '.$tool_on.'/>'.&mt('On').'</label>&nbsp;<label>'.
1.362     raeburn   426:                         '<input type="radio" name="'.$name.'" value="0" '.
1.306     raeburn   427:                         $tool_off.'/>'.&mt('Off').'</label></span>';
                    428:             $custradio = ('&nbsp;'x2).'--'.$lt{'cusa'}.':&nbsp;'.$custdisp.
                    429:                           '</span>';
                    430:         }
                    431:         $output .= '  <td'.$colspan.'>'.$custom_access.('&nbsp;'x4).
1.419     raeburn   432:                    $lt{'avai'}.': '.$currdisp.'</td>'."\n".
1.418     raeburn   433:                    &Apache::loncommon::end_data_table_row()."\n";
                    434:         unless (&Apache::lonnet::allowed('udp',$ccdomain)) {
                    435:             $output .=
1.275     raeburn   436:                    &Apache::loncommon::start_data_table_row()."\n".
1.306     raeburn   437:                    '  <td style="vertical-align:top;"><span class="LC_nobreak">'.
                    438:                    $lt{'chse'}.': <label>'.
1.275     raeburn   439:                    '<input type="radio" name="custom'.$item.'" value="0" '.
1.306     raeburn   440:                    $cust_off.'/>'.$lt{'usde'}.'</label>'.('&nbsp;' x3).
                    441:                    '<label><input type="radio" name="custom'.$item.'" value="1" '.
                    442:                    $cust_on.'/>'.$lt{'uscu'}.'</label>'.$custradio.'</td>'.
1.275     raeburn   443:                    &Apache::loncommon::end_data_table_row()."\n";
1.418     raeburn   444:         }
1.275     raeburn   445:     }
                    446:     return $output;
                    447: }
                    448: 
1.300     raeburn   449: sub coursereq_externaluser {
                    450:     my ($ccuname,$ccdomain,$cdom) = @_;
1.306     raeburn   451:     my (@usertools,@options,%validations,%userenv,$output);
1.300     raeburn   452:     my %lt = &Apache::lonlocal::texthash (
                    453:                    'official'   => 'Can request creation of official courses',
                    454:                    'unofficial' => 'Can request creation of unofficial courses',
                    455:                    'community'  => 'Can request creation of communities',
1.384     raeburn   456:                    'textbook'   => 'Can request creation of textbook courses',
1.411     raeburn   457:                    'placement'  => 'Can request creation of placement tests',
1.300     raeburn   458:     );
                    459: 
                    460:     %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    461:                       'reqcrsotherdom.official','reqcrsotherdom.unofficial',
1.411     raeburn   462:                       'reqcrsotherdom.community','reqcrsotherdom.textbook',
                    463:                       'reqcrsotherdom.placement');
                    464:     @usertools = ('official','unofficial','community','textbook','placement');
1.309     raeburn   465:     @options = ('approval','validate','autolimit');
1.306     raeburn   466:     %validations = &Apache::lonnet::auto_courserequest_checks($cdom);
                    467:     my $optregex = join('|',@options);
                    468:     my %reqtitles = &courserequest_titles();
1.300     raeburn   469:     foreach my $item (@usertools) {
1.306     raeburn   470:         my ($curroption,$currlimit,$tooloff);
1.300     raeburn   471:         if ($userenv{'reqcrsotherdom.'.$item} ne '') {
                    472:             my @curr = split(',',$userenv{'reqcrsotherdom.'.$item});
1.314     raeburn   473:             foreach my $req (@curr) {
                    474:                 if ($req =~ /^\Q$cdom\E\:($optregex)=?(\d*)$/) {
                    475:                     $curroption = $1;
                    476:                     $currlimit = $2;
                    477:                     last;
1.306     raeburn   478:                 }
                    479:             }
1.314     raeburn   480:             if (!$curroption) {
                    481:                 $curroption = 'norequest';
                    482:                 $tooloff = ' checked="checked"';
                    483:             }
1.306     raeburn   484:         } else {
                    485:             $curroption = 'norequest';
                    486:             $tooloff = ' checked="checked"';
                    487:         }
                    488:         $output.= &Apache::loncommon::start_data_table_row()."\n".
1.314     raeburn   489:                   '  <td><span class="LC_nobreak">'.$lt{$item}.': </span></td><td>'.
                    490:                   '<table><tr><td valign="top">'."\n".
1.306     raeburn   491:                   '<label><input type="radio" name="reqcrsotherdom_'.$item.
1.314     raeburn   492:                   '" value=""'.$tooloff.' />'.$reqtitles{'norequest'}.
                    493:                   '</label></td>';
1.306     raeburn   494:         foreach my $option (@options) {
                    495:             if ($option eq 'validate') {
                    496:                 my $canvalidate = 0;
                    497:                 if (ref($validations{$item}) eq 'HASH') {
                    498:                     if ($validations{$item}{'_external_'}) {
                    499:                         $canvalidate = 1;
                    500:                     }
                    501:                 }
                    502:                 next if (!$canvalidate);
                    503:             }
                    504:             my $checked = '';
                    505:             if ($option eq $curroption) {
                    506:                 $checked = ' checked="checked"';
                    507:             }
1.314     raeburn   508:             $output .= '<td valign="top"><span class="LC_nobreak"><label>'.
1.306     raeburn   509:                        '<input type="radio" name="reqcrsotherdom_'.$item.
                    510:                        '" value="'.$option.'"'.$checked.' />'.
1.314     raeburn   511:                        $reqtitles{$option}.'</label>';
1.306     raeburn   512:             if ($option eq 'autolimit') {
1.314     raeburn   513:                 $output .= '&nbsp;<input type="text" name="reqcrsotherdom_'.
1.306     raeburn   514:                            $item.'_limit" size="1" '.
1.314     raeburn   515:                            'value="'.$currlimit.'" /></span>'.
                    516:                            '<br />'.$reqtitles{'unlimited'};
                    517:             } else {
                    518:                 $output .= '</span>';
1.300     raeburn   519:             }
1.314     raeburn   520:             $output .= '</td>';
1.300     raeburn   521:         }
1.314     raeburn   522:         $output .= '</td></tr></table></td>'."\n".
1.300     raeburn   523:                    &Apache::loncommon::end_data_table_row()."\n";
                    524:     }
                    525:     return $output;
                    526: }
                    527: 
1.362     raeburn   528: sub domainrole_req {
                    529:     my ($ccuname,$ccdomain) = @_;
                    530:     return '<br /><h3>'.
                    531:            &mt('User Can Request Assignment of Domain Roles?').
                    532:            '</h3>'."\n".
                    533:            &Apache::loncommon::start_data_table().
                    534:            &build_tools_display($ccuname,$ccdomain,
                    535:                                 'requestauthor').
                    536:            &Apache::loncommon::end_data_table();
                    537: }
                    538: 
1.306     raeburn   539: sub courserequest_titles {
                    540:     my %titles = &Apache::lonlocal::texthash (
                    541:                                    official   => 'Official',
                    542:                                    unofficial => 'Unofficial',
                    543:                                    community  => 'Communities',
1.384     raeburn   544:                                    textbook   => 'Textbook',
1.411     raeburn   545:                                    placement  => 'Placement Tests',
1.306     raeburn   546:                                    norequest  => 'Not allowed',
1.309     raeburn   547:                                    approval   => 'Approval by Dom. Coord.',
1.306     raeburn   548:                                    validate   => 'With validation',
                    549:                                    autolimit  => 'Numerical limit',
1.314     raeburn   550:                                    unlimited  => '(blank for unlimited)',
1.306     raeburn   551:                  );
                    552:     return %titles;
                    553: }
                    554: 
                    555: sub courserequest_display {
                    556:     my %titles = &Apache::lonlocal::texthash (
1.309     raeburn   557:                                    approval   => 'Yes, need approval',
1.306     raeburn   558:                                    validate   => 'Yes, with validation',
                    559:                                    norequest  => 'No',
                    560:    );
                    561:    return %titles;
                    562: }
                    563: 
1.362     raeburn   564: sub requestauthor_titles {
                    565:     my %titles = &Apache::lonlocal::texthash (
                    566:                                    norequest  => 'Not allowed',
                    567:                                    approval   => 'Approval by Dom. Coord.',
                    568:                                    automatic  => 'Automatic approval',
                    569:                  );
                    570:     return %titles;
                    571: 
                    572: }
                    573: 
                    574: sub requestauthor_display {
                    575:     my %titles = &Apache::lonlocal::texthash (
                    576:                                    approval   => 'Yes, need approval',
                    577:                                    automatic  => 'Yes, automatic approval',
                    578:                                    norequest  => 'No',
                    579:    );
                    580:    return %titles;
                    581: }
                    582: 
1.383     raeburn   583: sub requestchange_display {
                    584:     my %titles = &Apache::lonlocal::texthash (
                    585:                                    approval   => "availability set to 'on' (approval required)", 
                    586:                                    automatic  => "availability set to 'on' (automatic approval)",
                    587:                                    norequest  => "availability set to 'off'",
                    588:    );
                    589:    return %titles;
                    590: }
                    591: 
1.362     raeburn   592: sub curr_requestauthor {
                    593:     my ($uname,$udom,$isadv,$inststatuses,$domconfig) = @_;
                    594:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
                    595:     if ($uname eq '' || $udom eq '') {
                    596:         $uname = $env{'user.name'};
                    597:         $udom = $env{'user.domain'};
                    598:         $isadv = $env{'user.adv'};
                    599:     }
                    600:     my (%userenv,%settings,$val);
                    601:     my @options = ('automatic','approval');
                    602:     %userenv =
                    603:         &Apache::lonnet::userenvironment($udom,$uname,'requestauthor','inststatus');
                    604:     if ($userenv{'requestauthor'}) {
                    605:         $val = $userenv{'requestauthor'};
                    606:         @{$inststatuses} = ('_custom_');
                    607:     } else {
                    608:         my %alltasks;
                    609:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') {
                    610:             %settings = %{$domconfig->{'requestauthor'}};
                    611:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
                    612:                 $val = $settings{'_LC_adv'};
                    613:                 @{$inststatuses} = ('_LC_adv_');
                    614:             } else {
                    615:                 if ($userenv{'inststatus'} ne '') {
                    616:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
                    617:                 } else {
                    618:                     @{$inststatuses} = ('default');
                    619:                 }
                    620:                 foreach my $status (@{$inststatuses}) {
                    621:                     if (exists($settings{$status})) {
                    622:                         my $value = $settings{$status};
                    623:                         next unless ($value);
                    624:                         unless (exists($alltasks{$value})) {
                    625:                             if (ref($alltasks{$value}) eq 'ARRAY') {
                    626:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
                    627:                                     push(@{$alltasks{$value}},$status);
                    628:                                 }
                    629:                             } else {
                    630:                                 @{$alltasks{$value}} = ($status);
                    631:                             }
                    632:                         }
                    633:                     }
                    634:                 }
                    635:                 foreach my $option (@options) {
                    636:                     if ($alltasks{$option}) {
                    637:                         $val = $option;
                    638:                         last;
                    639:                     }
                    640:                 }
                    641:             }
                    642:         }
                    643:     }
                    644:     return $val;
                    645: }
                    646: 
1.2       www       647: # =================================================================== Phase one
1.1       www       648: 
1.42      matthew   649: sub print_username_entry_form {
1.351     raeburn   650:     my ($r,$context,$response,$srch,$forcenewuser,$crstype,$brcrum) = @_;
1.101     albertel  651:     my $defdom=$env{'request.role.domain'};
1.160     raeburn   652:     my $formtoset = 'crtuser';
                    653:     if (exists($env{'form.startrolename'})) {
                    654:         $formtoset = 'docustom';
                    655:         $env{'form.rolename'} = $env{'form.startrolename'};
1.207     raeburn   656:     } elsif ($env{'form.origform'} eq 'crtusername') {
                    657:         $formtoset =  $env{'form.origform'};
1.160     raeburn   658:     }
                    659: 
                    660:     my ($jsback,$elements) = &crumb_utilities();
                    661: 
                    662:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
1.165     albertel  663:         '<script type="text/javascript">'."\n".
1.301     bisitz    664:         '// <![CDATA['."\n".
                    665:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset})."\n".
                    666:         '// ]]>'."\n".
1.162     raeburn   667:         '</script>'."\n";
1.160     raeburn   668: 
1.324     raeburn   669:     my %existingroles=&Apache::lonuserutils::my_custom_roles($crstype);
                    670:     if (($env{'form.action'} eq 'custom') && (keys(%existingroles) > 0)
                    671:         && (&Apache::lonnet::allowed('mcr','/'))) {
                    672:         $jscript .= &customrole_javascript();
                    673:     }
1.224     raeburn   674:     my $helpitem = 'Course_Change_Privileges';
                    675:     if ($env{'form.action'} eq 'custom') {
                    676:         $helpitem = 'Course_Editing_Custom_Roles';
                    677:     } elsif ($env{'form.action'} eq 'singlestudent') {
                    678:         $helpitem = 'Course_Add_Student';
1.416     raeburn   679:     } elsif ($env{'form.action'} eq 'accesslogs') {
                    680:         $helpitem = 'Domain_User_Access_Logs';
1.224     raeburn   681:     }
1.422     raeburn   682:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$defdom);
1.351     raeburn   683:     if ($env{'form.action'} eq 'custom') {
                    684:         push(@{$brcrum},
                    685:                  {href=>"javascript:backPage(document.crtuser)",       
                    686:                   text=>"Pick custom role",
                    687:                   help => $helpitem,}
                    688:                  );
                    689:     } else {
                    690:         push (@{$brcrum},
                    691:                   {href => "javascript:backPage(document.crtuser)",
                    692:                    text => $breadcrumb_text{'search'},
                    693:                    help => $helpitem,
                    694:                    faq  => 282,
                    695:                    bug  => 'Instructor Interface',}
                    696:                   );
                    697:     }
                    698:     my %loaditems = (
                    699:                 'onload' => "javascript:setFormElements(document.$formtoset)",
                    700:                     );
                    701:     my $args = {bread_crumbs           => $brcrum,
                    702:                 bread_crumbs_component => 'User Management',
                    703:                 add_entries            => \%loaditems,};
                    704:     $r->print(&Apache::loncommon::start_page('User Management',$jscript,$args));
                    705: 
1.71      sakharuk  706:     my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   707:                     'srst' => 'Search for a user and enroll as a student',
1.318     raeburn   708:                     'srme' => 'Search for a user and enroll as a member',
1.229     raeburn   709:                     'srad' => 'Search for a user and modify/add user information or roles',
1.422     raeburn   710:                     'srvu' => 'Search for a user and view user information and roles',
1.416     raeburn   711:                     'srva' => 'Search for a user and view access log information',
1.71      sakharuk  712: 		    'usr'  => "Username",
                    713:                     'dom'  => "Domain",
1.324     raeburn   714:                     'ecrp' => "Define or Edit Custom Role",
                    715:                     'nr'   => "role name",
1.282     schafran  716:                     'cre'  => "Next",
1.71      sakharuk  717: 				       );
1.351     raeburn   718: 
1.214     raeburn   719:     if ($env{'form.action'} eq 'custom') {
1.190     raeburn   720:         if (&Apache::lonnet::allowed('mcr','/')) {
1.324     raeburn   721:             my $newroletext = &mt('Define new custom role:');
                    722:             $r->print('<form action="/adm/createuser" method="post" name="docustom">'.
                    723:                       '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                    724:                       '<input type="hidden" name="phase" value="selected_custom_edit" />'.
                    725:                       '<h3>'.$lt{'ecrp'}.'</h3>'.
                    726:                       &Apache::loncommon::start_data_table().
                    727:                       &Apache::loncommon::start_data_table_row().
                    728:                       '<td>');
                    729:             if (keys(%existingroles) > 0) {
                    730:                 $r->print('<br /><label><input type="radio" name="customroleaction" value="new" checked="checked" onclick="setCustomFields();" /><b>'.$newroletext.'</b></label>');
                    731:             } else {
                    732:                 $r->print('<br /><input type="hidden" name="customroleaction" value="new" /><b>'.$newroletext.'</b>');
                    733:             }
                    734:             $r->print('</td><td align="center">'.$lt{'nr'}.'<br /><input type="text" size="15" name="newrolename" onfocus="setCustomAction('."'new'".');" /></td>'.
                    735:                       &Apache::loncommon::end_data_table_row());
                    736:             if (keys(%existingroles) > 0) {
                    737:                 $r->print(&Apache::loncommon::start_data_table_row().'<td><br />'.
                    738:                           '<label><input type="radio" name="customroleaction" value="edit" onclick="setCustomFields();"/><b>'.
                    739:                           &mt('View/Modify existing role:').'</b></label></td>'.
                    740:                           '<td align="center"><br />'.
                    741:                           '<select name="rolename" onchange="setCustomAction('."'edit'".');">'.
1.326     raeburn   742:                           '<option value="" selected="selected">'.
1.324     raeburn   743:                           &mt('Select'));
                    744:                 foreach my $role (sort(keys(%existingroles))) {
1.326     raeburn   745:                     $r->print('<option value="'.$role.'">'.$role.'</option>');
1.324     raeburn   746:                 }
                    747:                 $r->print('</select>'.
                    748:                           '</td>'.
                    749:                           &Apache::loncommon::end_data_table_row());
                    750:             }
                    751:             $r->print(&Apache::loncommon::end_data_table().'<p>'.
                    752:                       '<input name="customeditor" type="submit" value="'.
                    753:                       $lt{'cre'}.'" /></p>'.
                    754:                       '</form>');
1.190     raeburn   755:         }
1.213     raeburn   756:     } else {
1.229     raeburn   757:         my $actiontext = $lt{'srad'};
1.213     raeburn   758:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   759:             if ($crstype eq 'Community') {
                    760:                 $actiontext = $lt{'srme'};
                    761:             } else {
                    762:                 $actiontext = $lt{'srst'};
                    763:             }
1.416     raeburn   764:         } elsif ($env{'form.action'} eq 'accesslogs') {
1.417     raeburn   765:             $actiontext = $lt{'srva'};
1.422     raeburn   766:         } elsif (($env{'form.action'} eq 'singleuser') &&
                    767:                  ($context eq 'domain') && (!&Apache::lonnet::allowed('mau',$defdom))) {
                    768:             $actiontext = $lt{'srvu'};
1.213     raeburn   769:         }
1.324     raeburn   770:         $r->print("<h3>$actiontext</h3>");
1.213     raeburn   771:         if ($env{'form.origform'} ne 'crtusername') {
1.415     raeburn   772:             if ($response) {
                    773:                $r->print("\n<div>$response</div>".
                    774:                          '<br clear="all" />');
                    775:             }
1.213     raeburn   776:         }
1.416     raeburn   777:         $r->print(&entry_form($defdom,$srch,$forcenewuser,$context,$response,$crstype,1));
1.107     www       778:     }
1.110     albertel  779: }
                    780: 
1.324     raeburn   781: sub customrole_javascript {
                    782:     my $js = <<"END";
                    783: <script type="text/javascript">
                    784: // <![CDATA[
                    785: 
                    786: function setCustomFields() {
                    787:     if (document.docustom.customroleaction.length > 0) {
                    788:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    789:             if (document.docustom.customroleaction[i].checked) {
                    790:                 if (document.docustom.customroleaction[i].value == 'new') {
                    791:                     document.docustom.rolename.selectedIndex = 0;
                    792:                 } else {
                    793:                     document.docustom.newrolename.value = '';
                    794:                 }
                    795:             }
                    796:         }
                    797:     }
                    798:     return;
                    799: }
                    800: 
                    801: function setCustomAction(caller) {
                    802:     if (document.docustom.customroleaction.length > 0) {
                    803:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    804:             if (document.docustom.customroleaction[i].value == caller) {
                    805:                 document.docustom.customroleaction[i].checked = true;
                    806:             }
                    807:         }
                    808:     }
                    809:     setCustomFields();
                    810:     return;
                    811: }
                    812: 
                    813: // ]]>
                    814: </script>
                    815: END
                    816:     return $js;
                    817: }
                    818: 
1.160     raeburn   819: sub entry_form {
1.416     raeburn   820:     my ($dom,$srch,$forcenewuser,$context,$responsemsg,$crstype,$fixeddom) = @_;
1.229     raeburn   821:     my ($usertype,$inexact);
1.214     raeburn   822:     if (ref($srch) eq 'HASH') {
                    823:         if (($srch->{'srchin'} eq 'dom') &&
                    824:             ($srch->{'srchby'} eq 'uname') &&
                    825:             ($srch->{'srchtype'} eq 'exact') &&
                    826:             ($srch->{'srchdomain'} ne '') &&
                    827:             ($srch->{'srchterm'} ne '')) {
1.353     raeburn   828:             my (%curr_rules,%got_rules);
1.214     raeburn   829:             my ($rules,$ruleorder) =
                    830:                 &Apache::lonnet::inst_userrules($srch->{'srchdomain'},'username');
1.353     raeburn   831:             $usertype = &Apache::lonuserutils::check_usertype($srch->{'srchdomain'},$srch->{'srchterm'},$rules,\%curr_rules,\%got_rules);
1.229     raeburn   832:         } else {
                    833:             $inexact = 1;
1.214     raeburn   834:         }
1.207     raeburn   835:     }
1.214     raeburn   836:     my $cancreate =
                    837:         &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
1.412     raeburn   838:     my ($userpicker,$cansearch) = 
1.179     raeburn   839:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
1.416     raeburn   840:                                        'document.crtuser',$cancreate,$usertype,$context,$fixeddom);
1.160     raeburn   841:     my $srchbutton = &mt('Search');
1.229     raeburn   842:     if ($env{'form.action'} eq 'singlestudent') {
                    843:         $srchbutton = &mt('Search and Enroll');
1.416     raeburn   844:     } elsif ($env{'form.action'} eq 'accesslogs') {
                    845:         $srchbutton = &mt('Search');
1.229     raeburn   846:     } elsif ($cancreate && $responsemsg ne '' && $inexact) {
                    847:         $srchbutton = &mt('Search or Add New User');
                    848:     }
1.412     raeburn   849:     my $output;
                    850:     if ($cansearch) {
                    851:         $output = <<"ENDBLOCK";
1.160     raeburn   852: <form action="/adm/createuser" method="post" name="crtuser">
1.190     raeburn   853: <input type="hidden" name="action" value="$env{'form.action'}" />
1.160     raeburn   854: <input type="hidden" name="phase" value="get_user_info" />
                    855: $userpicker
1.179     raeburn   856: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
1.160     raeburn   857: </form>
1.207     raeburn   858: ENDBLOCK
1.412     raeburn   859:     } else {
                    860:         $output = '<p>'.$userpicker.'</p>';
                    861:     }
1.422     raeburn   862:     if (($env{'form.phase'} eq '') && ($env{'form.action'} ne 'accesslogs') &&
                    863:         (!(($env{'form.action'} eq 'singleuser') && ($context eq 'domain') && 
                    864:         (!&Apache::lonnet::allowed('mau',$env{'request.role.domain'}))))) {
1.207     raeburn   865:         my $defdom=$env{'request.role.domain'};
                    866:         my $domform = &Apache::loncommon::select_dom_form($defdom,'srchdomain');
                    867:         my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   868:                   'enro' => 'Enroll one student',
1.318     raeburn   869:                   'enrm' => 'Enroll one member',
1.229     raeburn   870:                   'admo' => 'Add/modify a single user',
                    871:                   'crea' => 'create new user if required',
                    872:                   'uskn' => "username is known",
1.207     raeburn   873:                   'crnu' => 'Create a new user',
                    874:                   'usr'  => 'Username',
                    875:                   'dom'  => 'in domain',
1.229     raeburn   876:                   'enrl' => 'Enroll',
                    877:                   'cram'  => 'Create/Modify user',
1.207     raeburn   878:         );
1.229     raeburn   879:         my $sellink=&Apache::loncommon::selectstudent_link('crtusername','srchterm','srchdomain');
                    880:         my ($title,$buttontext,$showresponse);
1.318     raeburn   881:         if ($env{'form.action'} eq 'singlestudent') {
                    882:             if ($crstype eq 'Community') {
                    883:                 $title = $lt{'enrm'};
                    884:             } else {
                    885:                 $title = $lt{'enro'};
                    886:             }
1.229     raeburn   887:             $buttontext = $lt{'enrl'};
                    888:         } else {
                    889:             $title = $lt{'admo'};
                    890:             $buttontext = $lt{'cram'};
                    891:         }
                    892:         if ($cancreate) {
                    893:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'crea'}.')</span>';
                    894:         } else {
                    895:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'uskn'}.')</span>';
                    896:         }
                    897:         if ($env{'form.origform'} eq 'crtusername') {
                    898:             $showresponse = $responsemsg;
                    899:         }
1.207     raeburn   900:         $output .= <<"ENDDOCUMENT";
1.229     raeburn   901: <br />
1.207     raeburn   902: <form action="/adm/createuser" method="post" name="crtusername">
                    903: <input type="hidden" name="action" value="$env{'form.action'}" />
                    904: <input type="hidden" name="phase" value="createnewuser" />
                    905: <input type="hidden" name="srchtype" value="exact" />
1.233     raeburn   906: <input type="hidden" name="srchby" value="uname" />
1.207     raeburn   907: <input type="hidden" name="srchin" value="dom" />
                    908: <input type="hidden" name="forcenewuser" value="1" />
                    909: <input type="hidden" name="origform" value="crtusername" />
1.229     raeburn   910: <h3>$title</h3>
                    911: $showresponse
1.207     raeburn   912: <table>
                    913:  <tr>
                    914:   <td>$lt{'usr'}:</td>
                    915:   <td><input type="text" size="15" name="srchterm" /></td>
                    916:   <td>&nbsp;$lt{'dom'}:</td><td>$domform</td>
1.229     raeburn   917:   <td>&nbsp;$sellink&nbsp;</td>
                    918:   <td>&nbsp;<input name="userrole" type="submit" value="$buttontext" /></td>
1.207     raeburn   919:  </tr>
                    920: </table>
                    921: </form>
1.160     raeburn   922: ENDDOCUMENT
1.207     raeburn   923:     }
1.160     raeburn   924:     return $output;
                    925: }
1.110     albertel  926: 
                    927: sub user_modification_js {
1.113     raeburn   928:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
                    929:     
1.110     albertel  930:     return <<END;
                    931: <script type="text/javascript" language="Javascript">
1.301     bisitz    932: // <![CDATA[
1.314     raeburn   933: 
1.110     albertel  934:     $pjump_def
                    935:     $dc_setcourse_code
                    936: 
                    937:     function dateset() {
                    938:         eval("document.cu."+document.cu.pres_marker.value+
                    939:             ".value=document.cu.pres_value.value");
1.359     www       940:         modalWindow.close();
1.110     albertel  941:     }
                    942: 
1.113     raeburn   943:     $nondc_setsection_code
1.301     bisitz    944: // ]]>
1.110     albertel  945: </script>
                    946: END
1.2       www       947: }
                    948: 
                    949: # =================================================================== Phase two
1.160     raeburn   950: sub print_user_selection_page {
1.351     raeburn   951:     my ($r,$response,$srch,$srch_results,$srcharray,$context,$opener_elements,$crstype,$brcrum) = @_;
1.160     raeburn   952:     my @fields = ('username','domain','lastname','firstname','permanentemail');
                    953:     my $sortby = $env{'form.sortby'};
                    954: 
                    955:     if (!grep(/^\Q$sortby\E$/,@fields)) {
                    956:         $sortby = 'lastname';
                    957:     }
                    958: 
                    959:     my ($jsback,$elements) = &crumb_utilities();
                    960: 
                    961:     my $jscript = (<<ENDSCRIPT);
                    962: <script type="text/javascript">
1.301     bisitz    963: // <![CDATA[
1.160     raeburn   964: function pickuser(uname,udom) {
                    965:     document.usersrchform.seluname.value=uname;
                    966:     document.usersrchform.seludom.value=udom;
                    967:     document.usersrchform.phase.value="userpicked";
                    968:     document.usersrchform.submit();
                    969: }
                    970: 
                    971: $jsback
1.301     bisitz    972: // ]]>
1.160     raeburn   973: </script>
                    974: ENDSCRIPT
                    975: 
                    976:     my %lt=&Apache::lonlocal::texthash(
1.179     raeburn   977:                                        'usrch'          => "User Search to add/modify roles",
                    978:                                        'stusrch'        => "User Search to enroll student",
1.318     raeburn   979:                                        'memsrch'        => "User Search to enroll member",
1.416     raeburn   980:                                        'srcva'          => "Search for a user and view access log information",
1.422     raeburn   981:                                        'usrvu'          => "User Search to view user roles",
1.179     raeburn   982:                                        'usel'           => "Select a user to add/modify roles",
1.422     raeburn   983:                                        'suvr'           => "Select a user to view roles",
1.318     raeburn   984:                                        'stusel'         => "Select a user to enroll as a student",
                    985:                                        'memsel'         => "Select a user to enroll as a member",
1.416     raeburn   986:                                        'vacsel'         => "Select a user to view access log",
1.160     raeburn   987:                                        'username'       => "username",
                    988:                                        'domain'         => "domain",
                    989:                                        'lastname'       => "last name",
                    990:                                        'firstname'      => "first name",
                    991:                                        'permanentemail' => "permanent e-mail",
                    992:                                       );
1.302     raeburn   993:     if ($context eq 'requestcrs') {
                    994:         $r->print('<div>');
                    995:     } else {
1.422     raeburn   996:         my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$srch->{'srchdomain'});
1.351     raeburn   997:         my $helpitem;
                    998:         if ($env{'form.action'} eq 'singleuser') {
                    999:             $helpitem = 'Course_Change_Privileges';
                   1000:         } elsif ($env{'form.action'} eq 'singlestudent') {
                   1001:             $helpitem = 'Course_Add_Student';
                   1002:         }
                   1003:         push (@{$brcrum},
                   1004:                   {href => "javascript:backPage(document.usersrchform,'','')",
                   1005:                    text => $breadcrumb_text{'search'},
                   1006:                    faq  => 282,
                   1007:                    bug  => 'Instructor Interface',},
                   1008:                   {href => "javascript:backPage(document.usersrchform,'get_user_info','select')",
                   1009:                    text => $breadcrumb_text{'userpicked'},
                   1010:                    faq  => 282,
                   1011:                    bug  => 'Instructor Interface',
                   1012:                    help => $helpitem}
                   1013:                   );
                   1014:         $r->print(&Apache::loncommon::start_page('User Management',$jscript,{bread_crumbs => $brcrum}));
1.302     raeburn  1015:         if ($env{'form.action'} eq 'singleuser') {
1.422     raeburn  1016:             my $readonly;
                   1017:             if (($context eq 'domain') && (!&Apache::lonnet::allowed('mau',$srch->{'srchdomain'}))) {
                   1018:                 $readonly = 1;
                   1019:                 $r->print("<b>$lt{'usrvu'}</b><br />");
                   1020:             } else {
                   1021:                 $r->print("<b>$lt{'usrch'}</b><br />");
                   1022:             }
1.318     raeburn  1023:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
1.422     raeburn  1024:             if ($readonly) {
                   1025:                 $r->print('<h3>'.$lt{'suvr'}.'</h3>');
                   1026:             } else {
                   1027:                 $r->print('<h3>'.$lt{'usel'}.'</h3>');
                   1028:             }
1.302     raeburn  1029:         } elsif ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1030:             $r->print($jscript."<b>");
                   1031:             if ($crstype eq 'Community') {
                   1032:                 $r->print($lt{'memsrch'});
                   1033:             } else {
                   1034:                 $r->print($lt{'stusrch'});
                   1035:             }
                   1036:             $r->print("</b><br />");
                   1037:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
                   1038:             $r->print('</form><h3>');
                   1039:             if ($crstype eq 'Community') {
                   1040:                 $r->print($lt{'memsel'});
                   1041:             } else {
                   1042:                 $r->print($lt{'stusel'});
                   1043:             }
                   1044:             $r->print('</h3>');
1.416     raeburn  1045:         } elsif ($env{'form.action'} eq 'accesslogs') {
                   1046:             $r->print("<b>$lt{'srcva'}</b><br />");
                   1047:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,'accesslogs',undef,undef,1));
                   1048:             $r->print('<h3>'.$lt{'vacsel'}.'</h3>');
1.302     raeburn  1049:         }
1.179     raeburn  1050:     }
1.380     bisitz   1051:     $r->print('<form name="usersrchform" method="post" action="">'.
1.160     raeburn  1052:               &Apache::loncommon::start_data_table()."\n".
                   1053:               &Apache::loncommon::start_data_table_header_row()."\n".
                   1054:               ' <th> </th>'."\n");
                   1055:     foreach my $field (@fields) {
                   1056:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
                   1057:                   "'".$field."'".';document.usersrchform.submit();">'.
                   1058:                   $lt{$field}.'</a></th>'."\n");
                   1059:     }
                   1060:     $r->print(&Apache::loncommon::end_data_table_header_row());
                   1061: 
                   1062:     my @sorted_users = sort {
1.167     albertel 1063:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
1.160     raeburn  1064:             ||
1.167     albertel 1065:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
1.160     raeburn  1066:             ||
                   1067:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
1.167     albertel 1068: 	    ||
                   1069: 	lc($a) cmp lc($b)
1.160     raeburn  1070:         } (keys(%$srch_results));
                   1071: 
                   1072:     foreach my $user (@sorted_users) {
                   1073:         my ($uname,$udom) = split(/:/,$user);
1.302     raeburn  1074:         my $onclick;
                   1075:         if ($context eq 'requestcrs') {
1.314     raeburn  1076:             $onclick =
1.302     raeburn  1077:                 'onclick="javascript:gochoose('."'$uname','$udom',".
                   1078:                                                "'$srch_results->{$user}->{firstname}',".
                   1079:                                                "'$srch_results->{$user}->{lastname}',".
                   1080:                                                "'$srch_results->{$user}->{permanentemail}'".');"';
                   1081:         } else {
1.314     raeburn  1082:             $onclick =
1.302     raeburn  1083:                 ' onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".');"';
                   1084:         }
1.160     raeburn  1085:         $r->print(&Apache::loncommon::start_data_table_row().
1.302     raeburn  1086:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" '.
                   1087:                   $onclick.' /></td>'.
1.160     raeburn  1088:                   '<td><tt>'.$uname.'</tt></td>'.
                   1089:                   '<td><tt>'.$udom.'</tt></td>');
                   1090:         foreach my $field ('lastname','firstname','permanentemail') {
                   1091:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
                   1092:         }
                   1093:         $r->print(&Apache::loncommon::end_data_table_row());
                   1094:     }
                   1095:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
1.179     raeburn  1096:     if (ref($srcharray) eq 'ARRAY') {
                   1097:         foreach my $item (@{$srcharray}) {
                   1098:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
                   1099:         }
                   1100:     }
1.160     raeburn  1101:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
                   1102:               ' <input type="hidden" name="seluname" value="" />'."\n".
                   1103:               ' <input type="hidden" name="seludom" value="" />'."\n".
1.179     raeburn  1104:               ' <input type="hidden" name="currstate" value="select" />'."\n".
1.190     raeburn  1105:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
1.214     raeburn  1106:               ' <input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
1.302     raeburn  1107:     if ($context eq 'requestcrs') {
                   1108:         $r->print($opener_elements.'</form></div>');
                   1109:     } else {
1.351     raeburn  1110:         $r->print($response.'</form>');
1.302     raeburn  1111:     }
1.160     raeburn  1112: }
                   1113: 
                   1114: sub print_user_query_page {
1.351     raeburn  1115:     my ($r,$caller,$brcrum) = @_;
1.160     raeburn  1116: # FIXME - this is for a network-wide name search (similar to catalog search)
                   1117: # To use frames with similar behavior to catalog/portfolio search.
                   1118: # To be implemented. 
                   1119:     return;
                   1120: }
                   1121: 
1.42      matthew  1122: sub print_user_modification_page {
1.375     raeburn  1123:     my ($r,$ccuname,$ccdomain,$srch,$response,$context,$permission,$crstype,
                   1124:         $brcrum,$showcredits) = @_;
1.185     raeburn  1125:     if (($ccuname eq '') || ($ccdomain eq '')) {
1.215     raeburn  1126:         my $usermsg = &mt('No username and/or domain provided.');
                   1127:         $env{'form.phase'} = '';
1.351     raeburn  1128: 	&print_username_entry_form($r,$context,$usermsg,'','',$crstype,$brcrum);
1.58      www      1129:         return;
                   1130:     }
1.213     raeburn  1131:     my ($form,$formname);
                   1132:     if ($env{'form.action'} eq 'singlestudent') {
                   1133:         $form = 'document.enrollstudent';
                   1134:         $formname = 'enrollstudent';
                   1135:     } else {
                   1136:         $form = 'document.cu';
                   1137:         $formname = 'cu';
                   1138:     }
1.188     raeburn  1139:     my %abv_auth = &auth_abbrev();
1.227     raeburn  1140:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
1.185     raeburn  1141:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
                   1142:     if ($uhome eq 'no_host') {
1.215     raeburn  1143:         my $usertype;
                   1144:         my ($rules,$ruleorder) =
                   1145:             &Apache::lonnet::inst_userrules($ccdomain,'username');
                   1146:             $usertype =
1.353     raeburn  1147:                 &Apache::lonuserutils::check_usertype($ccdomain,$ccuname,$rules,
1.362     raeburn  1148:                                                       \%curr_rules,\%got_rules);
1.215     raeburn  1149:         my $cancreate =
                   1150:             &Apache::lonuserutils::can_create_user($ccdomain,$context,
                   1151:                                                    $usertype);
                   1152:         if (!$cancreate) {
1.292     bisitz   1153:             my $helplink = 'javascript:helpMenu('."'display'".')';
1.215     raeburn  1154:             my %usertypetext = (
                   1155:                 official   => 'institutional',
                   1156:                 unofficial => 'non-institutional',
                   1157:             );
                   1158:             my $response;
                   1159:             if ($env{'form.origform'} eq 'crtusername') {
1.362     raeburn  1160:                 $response = '<span class="LC_warning">'.
                   1161:                             &mt('No match found for the username [_1] in LON-CAPA domain: [_2]',
                   1162:                                 '<b>'.$ccuname.'</b>',$ccdomain).
1.215     raeburn  1163:                             '</span><br />';
                   1164:             }
1.292     bisitz   1165:             $response .= '<p class="LC_warning">'
                   1166:                         .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
1.418     raeburn  1167:                         .' ';
                   1168:             if ($context eq 'domain') {
                   1169:                 $response .= &mt('Please contact a [_1] for assistance.',
                   1170:                                  &Apache::lonnet::plaintext('dc'));
                   1171:             } else {
                   1172:                 $response .= &mt('Please contact the [_1]helpdesk[_2] for assistance.'
                   1173:                                 ,'<a href="'.$helplink.'">','</a>');
                   1174:             }
                   1175:             $response .= '</p><br />';
1.215     raeburn  1176:             $env{'form.phase'} = '';
1.351     raeburn  1177:             &print_username_entry_form($r,$context,$response,undef,undef,$crstype,$brcrum);
1.215     raeburn  1178:             return;
                   1179:         }
1.188     raeburn  1180:         $newuser = 1;
1.193     raeburn  1181:         my $checkhash;
                   1182:         my $checks = { 'username' => 1 };
1.196     raeburn  1183:         $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
1.193     raeburn  1184:         &Apache::loncommon::user_rule_check($checkhash,$checks,
1.196     raeburn  1185:             \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
                   1186:         if (ref($alerts{'username'}) eq 'HASH') {
                   1187:             if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
                   1188:                 my $domdesc =
1.193     raeburn  1189:                     &Apache::lonnet::domain($ccdomain,'description');
1.196     raeburn  1190:                 if ($alerts{'username'}{$ccdomain}{$ccuname}) {
                   1191:                     my $userchkmsg;
                   1192:                     if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
                   1193:                         $userchkmsg = 
                   1194:                             &Apache::loncommon::instrule_disallow_msg('username',
1.193     raeburn  1195:                                                                  $domdesc,1).
                   1196:                         &Apache::loncommon::user_rule_formats($ccdomain,
                   1197:                             $domdesc,$curr_rules{$ccdomain}{'username'},
                   1198:                             'username');
1.196     raeburn  1199:                     }
1.215     raeburn  1200:                     $env{'form.phase'} = '';
1.351     raeburn  1201:                     &print_username_entry_form($r,$context,$userchkmsg,undef,undef,$crstype,$brcrum);
1.196     raeburn  1202:                     return;
1.215     raeburn  1203:                 }
1.193     raeburn  1204:             }
1.185     raeburn  1205:         }
1.187     raeburn  1206:     } else {
1.188     raeburn  1207:         $newuser = 0;
1.185     raeburn  1208:     }
1.160     raeburn  1209:     if ($response) {
1.215     raeburn  1210:         $response = '<br />'.$response;
1.160     raeburn  1211:     }
1.149     raeburn  1212: 
1.52      matthew  1213:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.88      raeburn  1214:     my $dc_setcourse_code = '';
1.119     raeburn  1215:     my $nondc_setsection_code = '';                                        
1.112     albertel 1216:     my %loaditem;
1.114     albertel 1217: 
1.216     raeburn  1218:     my $groupslist = &Apache::lonuserutils::get_groupslist();
1.88      raeburn  1219: 
1.375     raeburn  1220:     my $js = &validation_javascript($context,$ccdomain,$pjump_def,$crstype,
1.216     raeburn  1221:                                $groupslist,$newuser,$formname,\%loaditem);
1.422     raeburn  1222:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$ccdomain);
1.224     raeburn  1223:     my $helpitem = 'Course_Change_Privileges';
                   1224:     if ($env{'form.action'} eq 'singlestudent') {
                   1225:         $helpitem = 'Course_Add_Student';
                   1226:     }
1.351     raeburn  1227:     push (@{$brcrum},
                   1228:         {href => "javascript:backPage($form)",
                   1229:          text => $breadcrumb_text{'search'},
                   1230:          faq  => 282,
                   1231:          bug  => 'Instructor Interface',});
                   1232:     if ($env{'form.phase'} eq 'userpicked') {
                   1233:        push(@{$brcrum},
                   1234:               {href => "javascript:backPage($form,'get_user_info','select')",
                   1235:                text => $breadcrumb_text{'userpicked'},
                   1236:                faq  => 282,
                   1237:                bug  => 'Instructor Interface',});
                   1238:     }
                   1239:     push(@{$brcrum},
                   1240:             {href => "javascript:backPage($form,'$env{'form.phase'}','modify')",
                   1241:              text => $breadcrumb_text{'modify'},
                   1242:              faq  => 282,
                   1243:              bug  => 'Instructor Interface',
                   1244:              help => $helpitem});
                   1245:     my $args = {'add_entries'           => \%loaditem,
                   1246:                 'bread_crumbs'          => $brcrum,
                   1247:                 'bread_crumbs_component' => 'User Management'};
                   1248:     if ($env{'form.popup'}) {
                   1249:         $args->{'no_nav_bar'} = 1;
                   1250:     }
                   1251:     my $start_page =
                   1252:         &Apache::loncommon::start_page('User Management',$js,$args);
1.3       www      1253: 
1.25      matthew  1254:     my $forminfo =<<"ENDFORMINFO";
1.216     raeburn  1255: <form action="/adm/createuser" method="post" name="$formname">
1.190     raeburn  1256: <input type="hidden" name="phase" value="update_user_data" />
1.188     raeburn  1257: <input type="hidden" name="ccuname" value="$ccuname" />
                   1258: <input type="hidden" name="ccdomain" value="$ccdomain" />
1.157     albertel 1259: <input type="hidden" name="pres_value"  value="" />
                   1260: <input type="hidden" name="pres_type"   value="" />
                   1261: <input type="hidden" name="pres_marker" value="" />
1.25      matthew  1262: ENDFORMINFO
1.375     raeburn  1263:     my (%inccourses,$roledom,$defaultcredits);
1.329     raeburn  1264:     if ($context eq 'course') {
                   1265:         $inccourses{$env{'request.course.id'}}=1;
                   1266:         $roledom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.375     raeburn  1267:         if ($showcredits) {
                   1268:             $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1269:         }
1.329     raeburn  1270:     } elsif ($context eq 'author') {
                   1271:         $roledom = $env{'request.role.domain'};
                   1272:     } elsif ($context eq 'domain') {
                   1273:         foreach my $key (keys(%env)) {
                   1274:             $roledom = $env{'request.role.domain'};
                   1275:             if ($key=~/^user\.priv\.cm\.\/($roledom)\/($match_username)/) {
                   1276:                 $inccourses{$1.'_'.$2}=1;
                   1277:             }
                   1278:         }
                   1279:     } else {
                   1280:         foreach my $key (keys(%env)) {
                   1281: 	    if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
                   1282: 	        $inccourses{$1.'_'.$2}=1;
                   1283:             }
1.2       www      1284:         }
1.24      matthew  1285:     }
1.389     bisitz   1286:     my $title = '';
1.216     raeburn  1287:     if ($newuser) {
1.427   ! raeburn  1288:         my ($portfolioform,$domroleform);
1.267     raeburn  1289:         if ((&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) ||
                   1290:             (&Apache::lonnet::allowed('mut',$env{'request.role.domain'}))) {
                   1291:             # Current user has quota or user tools modification privileges
1.378     raeburn  1292:             $portfolioform = '<br />'.&user_quotas($ccuname,$ccdomain);
1.134     raeburn  1293:         }
1.383     raeburn  1294:         if ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) &&
                   1295:             ($ccdomain eq $env{'request.role.domain'})) {
1.362     raeburn  1296:             $domroleform = '<br />'.&domainrole_req($ccuname,$ccdomain);
                   1297:         }
1.227     raeburn  1298:         &initialize_authen_forms($ccdomain,$formname);
1.188     raeburn  1299:         my %lt=&Apache::lonlocal::texthash(
                   1300:                 'lg'             => 'Login Data',
1.190     raeburn  1301:                 'hs'             => "Home Server",
1.188     raeburn  1302:         );
1.185     raeburn  1303: 	$r->print(<<ENDTITLE);
1.110     albertel 1304: $start_page
1.160     raeburn  1305: $response
1.25      matthew  1306: $forminfo
1.31      matthew  1307: <script type="text/javascript" language="Javascript">
1.301     bisitz   1308: // <![CDATA[
1.20      harris41 1309: $loginscript
1.301     bisitz   1310: // ]]>
1.31      matthew  1311: </script>
1.20      harris41 1312: <input type='hidden' name='makeuser' value='1' />
1.185     raeburn  1313: ENDTITLE
1.213     raeburn  1314:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1315:             if ($crstype eq 'Community') {
1.389     bisitz   1316:                 $title = &mt('Create New User [_1] in domain [_2] as a member',
                   1317:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1318:             } else {
1.389     bisitz   1319:                 $title = &mt('Create New User [_1] in domain [_2] as a student',
                   1320:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1321:             }
1.389     bisitz   1322:         } else {
                   1323:                 $title = &mt('Create New User [_1] in domain [_2]',
                   1324:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.213     raeburn  1325:         }
1.389     bisitz   1326:         $r->print('<h2>'.$title.'</h2>'."\n");
                   1327:         $r->print('<div class="LC_left_float">');
1.393     raeburn  1328:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1329:                                          $inst_results{$ccuname.':'.$ccdomain}));
                   1330:         # Option to disable student/employee ID conflict checking not offerred for new users.
1.187     raeburn  1331:         my ($home_server_pick,$numlib) = 
                   1332:             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
                   1333:                                                       'default','hide');
                   1334:         if ($numlib > 1) {
                   1335:             $r->print("
1.185     raeburn  1336: <br />
1.187     raeburn  1337: $lt{'hs'}: $home_server_pick
                   1338: <br />");
                   1339:         } else {
                   1340:             $r->print($home_server_pick);
                   1341:         }
1.304     raeburn  1342:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.362     raeburn  1343:             $r->print('<br /><h3>'.
                   1344:                       &mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.304     raeburn  1345:                       &Apache::loncommon::start_data_table().
                   1346:                       &build_tools_display($ccuname,$ccdomain,
                   1347:                                            'requestcourses').
                   1348:                       &Apache::loncommon::end_data_table());
                   1349:         }
1.188     raeburn  1350:         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                   1351:                   $lt{'lg'}.'</h3>');
1.185     raeburn  1352:         my ($fixedauth,$varauth,$authmsg); 
1.193     raeburn  1353:         if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
                   1354:             my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
                   1355:             my ($rules,$ruleorder) = 
                   1356:                 &Apache::lonnet::inst_userrules($ccdomain,'username');
1.185     raeburn  1357:             if (ref($rules) eq 'HASH') {
1.193     raeburn  1358:                 if (ref($rules->{$matchedrule}) eq 'HASH') {
                   1359:                     my $authtype = $rules->{$matchedrule}{'authtype'};
1.185     raeburn  1360:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
1.190     raeburn  1361:                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.275     raeburn  1362:                     } else { 
1.193     raeburn  1363:                         my $authparm = $rules->{$matchedrule}{'authparm'};
1.273     raeburn  1364:                         $authmsg = $rules->{$matchedrule}{'authmsg'};
1.185     raeburn  1365:                         if ($authtype =~ /^krb(4|5)$/) {
                   1366:                             my $ver = $1;
                   1367:                             if ($authparm ne '') {
                   1368:                                 $fixedauth = <<"KERB"; 
                   1369: <input type="hidden" name="login" value="krb" />
                   1370: <input type="hidden" name="krbver" value="$ver" />
                   1371: <input type="hidden" name="krbarg" value="$authparm" />
                   1372: KERB
                   1373:                             }
                   1374:                         } else {
                   1375:                             $fixedauth = 
                   1376: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
1.193     raeburn  1377:                             if ($rules->{$matchedrule}{'authparmfixed'}) {
1.185     raeburn  1378:                                 $fixedauth .=    
                   1379: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
                   1380:                             } else {
1.273     raeburn  1381:                                 if ($authtype eq 'int') {
                   1382:                                     $varauth = '<br />'.
1.301     bisitz   1383: &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  1384:                                 } elsif ($authtype eq 'loc') {
                   1385:                                     $varauth = '<br />'.
                   1386: &mt('[_1] Local Authentication with argument [_2]','','<input type="text" name="'.$authtype.'arg" value="" />')."\n";
                   1387:                                 } else {
                   1388:                                     $varauth =
1.185     raeburn  1389: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
1.273     raeburn  1390:                                 }
1.185     raeburn  1391:                             }
                   1392:                         }
                   1393:                     }
                   1394:                 } else {
1.190     raeburn  1395:                     $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.185     raeburn  1396:                 }
                   1397:             }
                   1398:             if ($authmsg) {
                   1399:                 $r->print(<<ENDAUTH);
                   1400: $fixedauth
                   1401: $authmsg
                   1402: $varauth
                   1403: ENDAUTH
                   1404:             }
                   1405:         } else {
1.190     raeburn  1406:             $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc)); 
1.187     raeburn  1407:         }
1.427   ! raeburn  1408:         $r->print($portfolioform.$domroleform);
1.215     raeburn  1409:         if ($env{'form.action'} eq 'singlestudent') {
                   1410:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1411:                                             $permission,$crstype,$ccuname,
                   1412:                                             $ccdomain,$showcredits));
1.215     raeburn  1413:         }
                   1414:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.216     raeburn  1415:     } else { # user already exists
1.389     bisitz   1416: 	$r->print($start_page.$forminfo);
1.213     raeburn  1417:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1418:             if ($crstype eq 'Community') {
1.389     bisitz   1419:                 $title = &mt('Enroll one member: [_1] in domain [_2]',
                   1420:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1421:             } else {
1.389     bisitz   1422:                 $title = &mt('Enroll one student: [_1] in domain [_2]',
                   1423:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1424:             }
1.213     raeburn  1425:         } else {
1.418     raeburn  1426:             if ($permission->{'cusr'}) {
                   1427:                 $title = &mt('Modify existing user: [_1] in domain [_2]',
                   1428:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
                   1429:             } else {
                   1430:                 $title = &mt('Existing user: [_1] in domain [_2]',
1.389     bisitz   1431:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
1.418     raeburn  1432:             }
1.213     raeburn  1433:         }
1.389     bisitz   1434:         $r->print('<h2>'.$title.'</h2>'."\n");
                   1435:         $r->print('<div class="LC_left_float">');
1.393     raeburn  1436:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1437:                                          $inst_results{$ccuname.':'.$ccdomain}));
1.418     raeburn  1438:         if ((&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) || 
                   1439:             (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) {
1.362     raeburn  1440:             $r->print('<br /><h3>'.&mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.300     raeburn  1441:                       &Apache::loncommon::start_data_table());
1.314     raeburn  1442:             if ($env{'request.role.domain'} eq $ccdomain) {
1.300     raeburn  1443:                 $r->print(&build_tools_display($ccuname,$ccdomain,'requestcourses'));
                   1444:             } else {
                   1445:                 $r->print(&coursereq_externaluser($ccuname,$ccdomain,
                   1446:                                                   $env{'request.role.domain'}));
                   1447:             }
                   1448:             $r->print(&Apache::loncommon::end_data_table());
1.275     raeburn  1449:         }
1.199     raeburn  1450:         $r->print('</div>');
1.427   ! raeburn  1451:         my @order = ('auth','quota','tools','requestauthor');
1.362     raeburn  1452:         my %user_text;
                   1453:         my ($isadv,$isauthor) = 
1.418     raeburn  1454:             &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
1.362     raeburn  1455:         if ((!$isauthor) && 
1.418     raeburn  1456:             ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) ||
                   1457:              (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) &&
                   1458:             ($env{'request.role.domain'} eq $ccdomain)) {
1.362     raeburn  1459:             $user_text{'requestauthor'} = &domainrole_req($ccuname,$ccdomain);
                   1460:         }
                   1461:         $user_text{'auth'} =  &user_authentication($ccuname,$ccdomain,$formname);
1.267     raeburn  1462:         if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
1.418     raeburn  1463:             (&Apache::lonnet::allowed('mut',$ccdomain)) ||
                   1464:             (&Apache::lonnet::allowed('udp',$ccdomain))) {
1.188     raeburn  1465:             # Current user has quota modification privileges
1.378     raeburn  1466:             $user_text{'quota'} = &user_quotas($ccuname,$ccdomain);
1.267     raeburn  1467:         }
                   1468:         if (!&Apache::lonnet::allowed('mpq',$ccdomain)) {
                   1469:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
                   1470:                 my %lt=&Apache::lonlocal::texthash(
1.385     bisitz   1471:                     'dska'  => "Disk quotas for user's portfolio and Authoring Space",
                   1472:                     'youd'  => "You do not have privileges to modify the portfolio and/or Authoring Space quotas for this user.",
1.267     raeburn  1473:                     'ichr'  => "If a change is required, contact a domain coordinator for the domain",
                   1474:                 );
1.362     raeburn  1475:                 $user_text{'quota'} = <<ENDNOPORTPRIV;
1.188     raeburn  1476: <h3>$lt{'dska'}</h3>
                   1477: $lt{'youd'} $lt{'ichr'}: $ccdomain
                   1478: ENDNOPORTPRIV
1.267     raeburn  1479:             }
                   1480:         }
                   1481:         if (!&Apache::lonnet::allowed('mut',$ccdomain)) {
                   1482:             if (&Apache::lonnet::allowed('mut',$env{'request.role.domain'})) {
                   1483:                 my %lt=&Apache::lonlocal::texthash(
                   1484:                     'utav'  => "User Tools Availability",
1.361     raeburn  1485:                     'yodo'  => "You do not have privileges to modify Portfolio, Blog, WebDAV, or Personal Information Page settings for this user.",
1.267     raeburn  1486:                     'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1487:                 );
1.362     raeburn  1488:                 $user_text{'tools'} = <<ENDNOTOOLSPRIV;
1.267     raeburn  1489: <h3>$lt{'utav'}</h3>
                   1490: $lt{'yodo'} $lt{'ifch'}: $ccdomain
                   1491: ENDNOTOOLSPRIV
                   1492:             }
1.188     raeburn  1493:         }
1.362     raeburn  1494:         my $gotdiv = 0; 
                   1495:         foreach my $item (@order) {
                   1496:             if ($user_text{$item} ne '') {
                   1497:                 unless ($gotdiv) {
                   1498:                     $r->print('<div class="LC_left_float">');
                   1499:                     $gotdiv = 1;
                   1500:                 }
                   1501:                 $r->print('<br />'.$user_text{$item});
                   1502:             }
                   1503:         }
                   1504:         if ($env{'form.action'} eq 'singlestudent') {
                   1505:             unless ($gotdiv) {
                   1506:                 $r->print('<div class="LC_left_float">');
1.213     raeburn  1507:             }
1.375     raeburn  1508:             my $credits;
                   1509:             if ($showcredits) {
                   1510:                 $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1511:                 if ($credits eq '') {
                   1512:                     $credits = $defaultcredits;
                   1513:                 }
                   1514:             }
1.374     raeburn  1515:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1516:                                             $permission,$crstype,$ccuname,
                   1517:                                             $ccdomain,$showcredits));
1.374     raeburn  1518:         }
1.362     raeburn  1519:         if ($gotdiv) {
                   1520:             $r->print('</div><div class="LC_clear_float_footer"></div>');
1.188     raeburn  1521:         }
1.418     raeburn  1522:         my $statuses;
                   1523:         if (($context eq 'domain') && (&Apache::lonnet::allowed('udp',$ccdomain)) &&
                   1524:             (!&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1525:             $statuses = ['active'];
                   1526:         } elsif (($context eq 'course') && ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
                   1527:                  ($env{'request.course.sec'} &&
                   1528:                   &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'})))) {
                   1529:             $statuses = ['active'];   
                   1530:         }
1.217     raeburn  1531:         if ($env{'form.action'} ne 'singlestudent') {
1.329     raeburn  1532:             &display_existing_roles($r,$ccuname,$ccdomain,\%inccourses,$context,
1.418     raeburn  1533:                                     $roledom,$crstype,$showcredits,$statuses);
1.217     raeburn  1534:         }
1.25      matthew  1535:     } ## End of new user/old user logic
1.218     raeburn  1536:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1537:         my $btntxt;
                   1538:         if ($crstype eq 'Community') {
                   1539:             $btntxt = &mt('Enroll Member');
                   1540:         } else {
                   1541:             $btntxt = &mt('Enroll Student');
                   1542:         }
                   1543:         $r->print('<br /><input type="button" value="'.$btntxt.'" onclick="setSections(this.form)" />'."\n");
1.418     raeburn  1544:     } elsif ($permission->{'cusr'}) {
1.393     raeburn  1545:         $r->print('<div class="LC_left_float">'.
                   1546:                   '<fieldset><legend>'.&mt('Add Roles').'</legend>');
1.218     raeburn  1547:         my $addrolesdisplay = 0;
                   1548:         if ($context eq 'domain' || $context eq 'author') {
                   1549:             $addrolesdisplay = &new_coauthor_roles($r,$ccuname,$ccdomain);
                   1550:         }
                   1551:         if ($context eq 'domain') {
1.357     raeburn  1552:             my $add_domainroles = &new_domain_roles($r,$ccdomain);
1.218     raeburn  1553:             if (!$addrolesdisplay) {
                   1554:                 $addrolesdisplay = $add_domainroles;
1.2       www      1555:             }
1.375     raeburn  1556:             $r->print(&course_level_dc($env{'request.role.domain'},$showcredits));
1.393     raeburn  1557:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1558:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setCourse()" />'."\n");
1.218     raeburn  1559:         } elsif ($context eq 'author') {
                   1560:             if ($addrolesdisplay) {
1.393     raeburn  1561:                 $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1562:                           '<br /><input type="button" value="'.&mt('Save').'"');
1.218     raeburn  1563:                 if ($newuser) {
1.301     bisitz   1564:                     $r->print(' onclick="auth_check()" \>'."\n");
1.218     raeburn  1565:                 } else {
1.301     bisitz   1566:                     $r->print('onclick="this.form.submit()" \>'."\n");
1.218     raeburn  1567:                 }
1.188     raeburn  1568:             } else {
1.393     raeburn  1569:                 $r->print('</fieldset></div>'.
                   1570:                           '<div class="LC_clear_float_footer"></div>'.
                   1571:                           '<br /><a href="javascript:backPage(document.cu)">'.
1.218     raeburn  1572:                           &mt('Back to previous page').'</a>');
1.188     raeburn  1573:             }
                   1574:         } else {
1.375     raeburn  1575:             $r->print(&course_level_table(\%inccourses,$showcredits,$defaultcredits));
1.393     raeburn  1576:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1577:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setSections(this.form)" />'."\n");
1.188     raeburn  1578:         }
1.88      raeburn  1579:     }
1.188     raeburn  1580:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
1.179     raeburn  1581:     $r->print('<input type="hidden" name="currstate" value="" />');
1.393     raeburn  1582:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" /></form><br /><br />');
1.218     raeburn  1583:     return;
1.2       www      1584: }
1.1       www      1585: 
1.213     raeburn  1586: sub singleuser_breadcrumb {
1.422     raeburn  1587:     my ($crstype,$context,$domain) = @_;
1.213     raeburn  1588:     my %breadcrumb_text;
                   1589:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1590:         if ($crstype eq 'Community') {
                   1591:             $breadcrumb_text{'search'} = 'Enroll a member';
                   1592:         } else {
                   1593:             $breadcrumb_text{'search'} = 'Enroll a student';
                   1594:         }
1.422     raeburn  1595:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1596:         $breadcrumb_text{'modify'} = 'Set section/dates';
1.416     raeburn  1597:     } elsif ($env{'form.action'} eq 'accesslogs') {
                   1598:         $breadcrumb_text{'search'} = 'View access logs for a user';
1.422     raeburn  1599:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1600:         $breadcrumb_text{'activity'} = 'Activity';
                   1601:     } elsif (($env{'form.action'} eq 'singleuser') && ($context eq 'domain') &&
                   1602:              (!&Apache::lonnet::allowed('mau',$domain))) {
                   1603:         $breadcrumb_text{'search'} = "View user's roles";
                   1604:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1605:         $breadcrumb_text{'modify'} = 'User roles';
1.213     raeburn  1606:     } else {
1.229     raeburn  1607:         $breadcrumb_text{'search'} = 'Create/modify a user';
1.422     raeburn  1608:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1609:         $breadcrumb_text{'modify'} = 'Set user role';
1.213     raeburn  1610:     }
                   1611:     return %breadcrumb_text;
                   1612: }
                   1613: 
                   1614: sub date_sections_select {
1.375     raeburn  1615:     my ($context,$newuser,$formname,$permission,$crstype,$ccuname,$ccdomain,
                   1616:         $showcredits) = @_;
                   1617:     my $credits;
                   1618:     if ($showcredits) {
                   1619:         my $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1620:         $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1621:         if ($credits eq '') {
                   1622:             $credits = $defaultcredits;
                   1623:         }
                   1624:     }
1.213     raeburn  1625:     my $cid = $env{'request.course.id'};
                   1626:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity($cid);
                   1627:     my $date_table = '<h3>'.&mt('Starting and Ending Dates').'</h3>'."\n".
                   1628:         &Apache::lonuserutils::date_setting_table(undef,undef,$context,
                   1629:                                                   undef,$formname,$permission);
                   1630:     my $rowtitle = 'Section';
1.375     raeburn  1631:     my $secbox = '<h3>'.&mt('Section and Credits').'</h3>'."\n".
1.213     raeburn  1632:         &Apache::lonuserutils::section_picker($cdom,$cnum,'st',$rowtitle,
1.375     raeburn  1633:                                               $permission,$context,'',$crstype,
                   1634:                                               $showcredits,$credits);
1.213     raeburn  1635:     my $output = $date_table.$secbox;
                   1636:     return $output;
                   1637: }
                   1638: 
1.216     raeburn  1639: sub validation_javascript {
1.375     raeburn  1640:     my ($context,$ccdomain,$pjump_def,$crstype,$groupslist,$newuser,$formname,
1.216     raeburn  1641:         $loaditem) = @_;
                   1642:     my $dc_setcourse_code = '';
                   1643:     my $nondc_setsection_code = '';
                   1644:     if ($context eq 'domain') {
                   1645:         my $dcdom = $env{'request.role.domain'};
                   1646:         $loaditem->{'onload'} = "document.cu.coursedesc.value='';";
1.227     raeburn  1647:         $dc_setcourse_code = 
                   1648:             &Apache::lonuserutils::dc_setcourse_js('cu','singleuser',$context);
1.216     raeburn  1649:     } else {
1.227     raeburn  1650:         my $checkauth; 
                   1651:         if (($newuser) || (&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1652:             $checkauth = 1;
                   1653:         }
                   1654:         if ($context eq 'course') {
                   1655:             $nondc_setsection_code =
                   1656:                 &Apache::lonuserutils::setsections_javascript($formname,$groupslist,
1.375     raeburn  1657:                                                               undef,$checkauth,
                   1658:                                                               $crstype);
1.227     raeburn  1659:         }
                   1660:         if ($checkauth) {
                   1661:             $nondc_setsection_code .= 
                   1662:                 &Apache::lonuserutils::verify_authen($formname,$context);
                   1663:         }
1.216     raeburn  1664:     }
                   1665:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                   1666:                                    $nondc_setsection_code,$groupslist);
                   1667:     my ($jsback,$elements) = &crumb_utilities();
                   1668:     $js .= "\n".
1.301     bisitz   1669:            '<script type="text/javascript">'."\n".
                   1670:            '// <![CDATA['."\n".
                   1671:            $jsback."\n".
                   1672:            '// ]]>'."\n".
                   1673:            '</script>'."\n";
1.216     raeburn  1674:     return $js;
                   1675: }
                   1676: 
1.217     raeburn  1677: sub display_existing_roles {
1.375     raeburn  1678:     my ($r,$ccuname,$ccdomain,$inccourses,$context,$roledom,$crstype,
1.418     raeburn  1679:         $showcredits,$statuses) = @_;
1.329     raeburn  1680:     my $now=time;
1.418     raeburn  1681:     my $showall = 1;
                   1682:     my ($showexpired,$showactive);
                   1683:     if ((ref($statuses) eq 'ARRAY') && (@{$statuses} > 0)) {
                   1684:         $showall = 0;
                   1685:         if (grep(/^expired$/,@{$statuses})) {
                   1686:             $showexpired = 1;
                   1687:         }
                   1688:         if (grep(/^active$/,@{$statuses})) {
                   1689:             $showactive = 1;
                   1690:         }
                   1691:         if ($showexpired && $showactive) {
                   1692:             $showall = 1;
                   1693:         }
                   1694:     }
1.329     raeburn  1695:     my %lt=&Apache::lonlocal::texthash(
1.217     raeburn  1696:                     'rer'  => "Existing Roles",
                   1697:                     'rev'  => "Revoke",
                   1698:                     'del'  => "Delete",
                   1699:                     'ren'  => "Re-Enable",
                   1700:                     'rol'  => "Role",
                   1701:                     'ext'  => "Extent",
1.375     raeburn  1702:                     'crd'  => "Credits",
1.217     raeburn  1703:                     'sta'  => "Start",
                   1704:                     'end'  => "End",
                   1705:                                        );
1.329     raeburn  1706:     my (%rolesdump,%roletext,%sortrole,%roleclass,%rolepriv);
                   1707:     if ($context eq 'course' || $context eq 'author') {
                   1708:         my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
                   1709:         my %roleshash = 
                   1710:             &Apache::lonnet::get_my_roles($ccuname,$ccdomain,'userroles',
                   1711:                               ['active','previous','future'],\@roles,$roledom,1);
                   1712:         foreach my $key (keys(%roleshash)) {
                   1713:             my ($start,$end) = split(':',$roleshash{$key});
                   1714:             next if ($start eq '-1' || $end eq '-1');
                   1715:             my ($rnum,$rdom,$role,$sec) = split(':',$key);
                   1716:             if ($context eq 'course') {
                   1717:                 next unless (($rnum eq $env{'course.'.$env{'request.course.id'}.'.num'})
                   1718:                              && ($rdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}));
                   1719:             } elsif ($context eq 'author') {
                   1720:                 next unless (($rnum eq $env{'user.name'}) && ($rdom eq $env{'request.role.domain'}));
                   1721:             }
                   1722:             my ($newkey,$newvalue,$newrole);
                   1723:             $newkey = '/'.$rdom.'/'.$rnum;
                   1724:             if ($sec ne '') {
                   1725:                 $newkey .= '/'.$sec;
                   1726:             }
                   1727:             $newvalue = $role;
                   1728:             if ($role =~ /^cr/) {
                   1729:                 $newrole = 'cr';
                   1730:             } else {
                   1731:                 $newrole = $role;
                   1732:             }
                   1733:             $newkey .= '_'.$newrole;
                   1734:             if ($start ne '' && $end ne '') {
                   1735:                 $newvalue .= '_'.$end.'_'.$start;
1.335     raeburn  1736:             } elsif ($end ne '') {
                   1737:                 $newvalue .= '_'.$end;
1.329     raeburn  1738:             }
                   1739:             $rolesdump{$newkey} = $newvalue;
                   1740:         }
                   1741:     } else {
1.360     raeburn  1742:         %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
1.329     raeburn  1743:     }
                   1744:     # Build up table of user roles to allow revocation and re-enabling of roles.
                   1745:     my ($tmp) = keys(%rolesdump);
                   1746:     return if ($tmp =~ /^(con_lost|error)/i);
                   1747:     foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
                   1748:                                 my $b1=join('_',(split('_',$b))[1,0]);
                   1749:                                 return $a1 cmp $b1;
                   1750:                             } keys(%rolesdump)) {
                   1751:         next if ($area =~ /^rolesdef/);
                   1752:         my $envkey=$area;
                   1753:         my $role = $rolesdump{$area};
                   1754:         my $thisrole=$area;
                   1755:         $area =~ s/\_\w\w$//;
                   1756:         my ($role_code,$role_end_time,$role_start_time) =
                   1757:             split(/_/,$role);
1.418     raeburn  1758:         my $active=1;
                   1759:         $active=0 if (($role_end_time) && ($now>$role_end_time));
                   1760:         if ($active) {
                   1761:             next unless($showall || $showactive);
                   1762:         } else {
                   1763:             next unless($showall || $showexpired); 
                   1764:         }
1.217     raeburn  1765: # Is this a custom role? Get role owner and title.
1.329     raeburn  1766:         my ($croleudom,$croleuname,$croletitle)=
                   1767:             ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
                   1768:         my $allowed=0;
                   1769:         my $delallowed=0;
                   1770:         my $sortkey=$role_code;
                   1771:         my $class='Unknown';
1.375     raeburn  1772:         my $credits='';
1.418     raeburn  1773:         my $csec;
1.421     raeburn  1774:         if ($area =~ m{^/($match_domain)/($match_courseid)}) {
1.329     raeburn  1775:             $class='Course';
                   1776:             my ($coursedom,$coursedir) = ($1,$2);
                   1777:             my $cid = $1.'_'.$2;
                   1778:             # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
1.421     raeburn  1779:             next if ($envkey =~ m{^/$match_domain/$match_courseid/[A-Za-z0-9]+_gr$});
1.329     raeburn  1780:             my %coursedata=
                   1781:                 &Apache::lonnet::coursedescription($cid);
                   1782:             if ($coursedir =~ /^$match_community$/) {
                   1783:                 $class='Community';
                   1784:             }
                   1785:             $sortkey.="\0$coursedom";
                   1786:             my $carea;
                   1787:             if (defined($coursedata{'description'})) {
                   1788:                 $carea=$coursedata{'description'}.
                   1789:                     '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
                   1790:     &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$coursedir,$coursedom);
                   1791:                 $sortkey.="\0".$coursedata{'description'};
                   1792:             } else {
                   1793:                 if ($class eq 'Community') {
                   1794:                     $carea=&mt('Unavailable community').': '.$area;
                   1795:                     $sortkey.="\0".&mt('Unavailable community').': '.$area;
1.217     raeburn  1796:                 } else {
                   1797:                     $carea=&mt('Unavailable course').': '.$area;
                   1798:                     $sortkey.="\0".&mt('Unavailable course').': '.$area;
                   1799:                 }
1.329     raeburn  1800:             }
                   1801:             $sortkey.="\0$coursedir";
                   1802:             $inccourses->{$cid}=1;
1.375     raeburn  1803:             if (($showcredits) && ($class eq 'Course') && ($role_code eq 'st')) {
                   1804:                 my $defaultcredits = $coursedata{'internal.defaultcredits'};
                   1805:                 $credits =
                   1806:                     &get_user_credits($ccuname,$ccdomain,$defaultcredits,
                   1807:                                       $coursedom,$coursedir);
                   1808:                 if ($credits eq '') {
                   1809:                     $credits = $defaultcredits;
                   1810:                 }
                   1811:             }
1.329     raeburn  1812:             if ((&Apache::lonnet::allowed('c'.$role_code,$coursedom.'/'.$coursedir)) ||
                   1813:                 (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1814:                 $allowed=1;
                   1815:             }
                   1816:             unless ($allowed) {
1.365     raeburn  1817:                 my $isowner = &Apache::lonuserutils::is_courseowner($cid,$coursedata{'internal.courseowner'});
1.329     raeburn  1818:                 if ($isowner) {
                   1819:                     if (($role_code eq 'co') && ($class eq 'Community')) {
                   1820:                         $allowed = 1;
                   1821:                     } elsif (($role_code eq 'cc') && ($class eq 'Course')) {
                   1822:                         $allowed = 1;
                   1823:                     }
1.217     raeburn  1824:                 }
1.329     raeburn  1825:             } 
                   1826:             if ((&Apache::lonnet::allowed('dro',$coursedom)) ||
                   1827:                 (&Apache::lonnet::allowed('dro',$ccdomain))) {
                   1828:                 $delallowed=1;
                   1829:             }
1.217     raeburn  1830: # - custom role. Needs more info, too
1.329     raeburn  1831:             if ($croletitle) {
                   1832:                 if (&Apache::lonnet::allowed('ccr',$coursedom.'/'.$coursedir)) {
                   1833:                     $allowed=1;
                   1834:                     $thisrole.='.'.$role_code;
1.217     raeburn  1835:                 }
1.329     raeburn  1836:             }
1.418     raeburn  1837:             if ($area=~m{^/($match_domain/$match_courseid/(\w+))}) {
                   1838:                 $csec = $2;
                   1839:                 $carea.='<br />'.&mt('Section: [_1]',$csec);
                   1840:                 $sortkey.="\0$csec";
1.329     raeburn  1841:                 if (!$allowed) {
1.418     raeburn  1842:                     if ($env{'request.course.sec'} eq $csec) {
                   1843:                         if (&Apache::lonnet::allowed('c'.$role_code,$1)) {
1.329     raeburn  1844:                             $allowed = 1;
1.217     raeburn  1845:                         }
                   1846:                     }
                   1847:                 }
1.329     raeburn  1848:             }
                   1849:             $area=$carea;
                   1850:         } else {
                   1851:             $sortkey.="\0".$area;
                   1852:             # Determine if current user is able to revoke privileges
                   1853:             if ($area=~m{^/($match_domain)/}) {
                   1854:                 if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
                   1855:                    (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1856:                    $allowed=1;
1.217     raeburn  1857:                 }
1.329     raeburn  1858:                 if (((&Apache::lonnet::allowed('dro',$1))  ||
                   1859:                     (&Apache::lonnet::allowed('dro',$ccdomain))) &&
                   1860:                     ($role_code ne 'dc')) {
                   1861:                     $delallowed=1;
1.217     raeburn  1862:                 }
1.329     raeburn  1863:             } else {
                   1864:                 if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
1.217     raeburn  1865:                     $allowed=1;
                   1866:                 }
                   1867:             }
1.363     raeburn  1868:             if ($role_code eq 'ca' || $role_code eq 'au' || $role_code eq 'aa') {
1.377     raeburn  1869:                 $class='Authoring Space';
1.329     raeburn  1870:             } elsif ($role_code eq 'su') {
                   1871:                 $class='System';
1.217     raeburn  1872:             } else {
1.329     raeburn  1873:                 $class='Domain';
1.217     raeburn  1874:             }
1.329     raeburn  1875:         }
                   1876:         if (($role_code eq 'ca') || ($role_code eq 'aa')) {
                   1877:             $area=~m{/($match_domain)/($match_username)};
                   1878:             if (&Apache::lonuserutils::authorpriv($2,$1)) {
                   1879:                 $allowed=1;
1.217     raeburn  1880:             } else {
1.329     raeburn  1881:                 $allowed=0;
1.217     raeburn  1882:             }
1.329     raeburn  1883:         }
                   1884:         my $row = '';
1.418     raeburn  1885:         if ($showall) {
                   1886:             $row.= '<td>';
                   1887:             if (($active) && ($allowed)) {
                   1888:                 $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
                   1889:             } else {
                   1890:                 if ($active) {
                   1891:                     $row.='&nbsp;';
                   1892:                 } else {
                   1893:                     $row.=&mt('expired or revoked');
                   1894:                 }
                   1895:             }
                   1896:             $row.='</td><td>';
                   1897:             if ($allowed && !$active) {
                   1898:                 $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
                   1899:             } else {
                   1900:                 $row.='&nbsp;';
                   1901:             }
                   1902:             $row.='</td><td>';
                   1903:             if ($delallowed) {
                   1904:                 $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
1.217     raeburn  1905:             } else {
1.418     raeburn  1906:                 $row.='&nbsp;';
1.217     raeburn  1907:             }
1.418     raeburn  1908:             $row.= '</td>'; 
1.329     raeburn  1909:         }
                   1910:         my $plaintext='';
                   1911:         if (!$croletitle) {
1.375     raeburn  1912:             $plaintext=&Apache::lonnet::plaintext($role_code,$class);
                   1913:             if (($showcredits) && ($credits ne '')) {
                   1914:                 $plaintext .= '<br/ ><span class="LC_nobreak">'.
                   1915:                               '<span class="LC_fontsize_small">'.
                   1916:                               &mt('Credits: [_1]',$credits).
                   1917:                               '</span></span>';
                   1918:             }
1.329     raeburn  1919:         } else {
                   1920:             $plaintext=
1.395     bisitz   1921:                 &mt('Custom role [_1][_2]defined by [_3]',
1.346     bisitz   1922:                         '"'.$croletitle.'"',
                   1923:                         '<br />',
                   1924:                         $croleuname.':'.$croleudom);
1.329     raeburn  1925:         }
1.418     raeburn  1926:         $row.= '<td>'.$plaintext.'</td>'.
                   1927:                '<td>'.$area.'</td>'.
                   1928:                '<td>'.($role_start_time?&Apache::lonlocal::locallocaltime($role_start_time)
                   1929:                                             : '&nbsp;' ).'</td>'.
                   1930:                '<td>'.($role_end_time  ?&Apache::lonlocal::locallocaltime($role_end_time)
                   1931:                                             : '&nbsp;' ).'</td>';
1.329     raeburn  1932:         $sortrole{$sortkey}=$envkey;
                   1933:         $roletext{$envkey}=$row;
                   1934:         $roleclass{$envkey}=$class;
1.418     raeburn  1935:         if ($allowed) {
                   1936:             $rolepriv{$envkey}='edit';
                   1937:         } else {
                   1938:             if ($context eq 'domain') {
1.420     raeburn  1939:                 if ((&Apache::lonnet::allowed('vur',$ccdomain)) &&
1.421     raeburn  1940:                     ($envkey=~m{^/$ccdomain/})) {
1.418     raeburn  1941:                     $rolepriv{$envkey}='view';
                   1942:                 }
                   1943:             } elsif ($context eq 'course') {
                   1944:                 if ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
                   1945:                     ($env{'request.course.sec'} && ($env{'request.course.sec'} eq $csec) &&
                   1946:                      &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'}))) {
                   1947:                     $rolepriv{$envkey}='view';
                   1948:                 }
                   1949:             }
                   1950:         }
1.329     raeburn  1951:     } # end of foreach        (table building loop)
                   1952: 
                   1953:     my $rolesdisplay = 0;
                   1954:     my %output = ();
1.377     raeburn  1955:     foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  1956:         $output{$type} = '';
                   1957:         foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                   1958:             if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) {
                   1959:                  $output{$type}.=
                   1960:                       &Apache::loncommon::start_data_table_row().
                   1961:                       $roletext{$sortrole{$which}}.
                   1962:                       &Apache::loncommon::end_data_table_row();
1.217     raeburn  1963:             }
1.329     raeburn  1964:         }
                   1965:         unless($output{$type} eq '') {
                   1966:             $output{$type} = '<tr class="LC_info_row">'.
                   1967:                       "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
                   1968:                       $output{$type};
                   1969:             $rolesdisplay = 1;
                   1970:         }
                   1971:     }
                   1972:     if ($rolesdisplay == 1) {
                   1973:         my $contextrole='';
                   1974:         if ($env{'request.course.id'}) {
                   1975:             if (&Apache::loncommon::course_type() eq 'Community') {
                   1976:                 $contextrole = &mt('Existing Roles in this Community');
1.290     bisitz   1977:             } else {
1.329     raeburn  1978:                 $contextrole = &mt('Existing Roles in this Course');
1.290     bisitz   1979:             }
1.329     raeburn  1980:         } elsif ($env{'request.role'} =~ /^au\./) {
1.377     raeburn  1981:             $contextrole = &mt('Existing Co-Author Roles in your Authoring Space');
1.329     raeburn  1982:         } else {
1.418     raeburn  1983:             if ($showall) {
                   1984:                 $contextrole = &mt('Existing Roles in this Domain');
                   1985:             } elsif ($showactive) {
                   1986:                 $contextrole = &mt('Unexpired Roles in this Domain');
                   1987:             } elsif ($showexpired) {
                   1988:                 $contextrole = &mt('Expired or Revoked Roles in this Domain');
                   1989:             }
1.329     raeburn  1990:         }
1.393     raeburn  1991:         $r->print('<div class="LC_left_float">'.
1.375     raeburn  1992: '<fieldset><legend>'.$contextrole.'</legend>'.
1.217     raeburn  1993: &Apache::loncommon::start_data_table("LC_createuser").
1.418     raeburn  1994: &Apache::loncommon::start_data_table_header_row());
                   1995:         if ($showall) {
                   1996:             $r->print(
1.419     raeburn  1997: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}.'</th>'
1.418     raeburn  1998:             );
                   1999:         } elsif ($showexpired) {
                   2000:             $r->print('<th>'.$lt{'rev'}.'</th>');
                   2001:         }
                   2002:         $r->print(
1.419     raeburn  2003: '<th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.'</th>'.
                   2004: '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
1.217     raeburn  2005: &Apache::loncommon::end_data_table_header_row());
1.377     raeburn  2006:         foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  2007:             if ($output{$type}) {
                   2008:                 $r->print($output{$type}."\n");
1.217     raeburn  2009:             }
                   2010:         }
1.375     raeburn  2011:         $r->print(&Apache::loncommon::end_data_table().
                   2012:                   '</fieldset></div>');
1.329     raeburn  2013:     }
1.217     raeburn  2014:     return;
                   2015: }
                   2016: 
1.218     raeburn  2017: sub new_coauthor_roles {
                   2018:     my ($r,$ccuname,$ccdomain) = @_;
                   2019:     my $addrolesdisplay = 0;
                   2020:     #
                   2021:     # Co-Author
                   2022:     #
                   2023:     if (&Apache::lonuserutils::authorpriv($env{'user.name'},
                   2024:                                           $env{'request.role.domain'}) &&
                   2025:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
                   2026:         # No sense in assigning co-author role to yourself
                   2027:         $addrolesdisplay = 1;
                   2028:         my $cuname=$env{'user.name'};
                   2029:         my $cudom=$env{'request.role.domain'};
                   2030:         my %lt=&Apache::lonlocal::texthash(
1.377     raeburn  2031:                     'cs'   => "Authoring Space",
1.218     raeburn  2032:                     'act'  => "Activate",
                   2033:                     'rol'  => "Role",
                   2034:                     'ext'  => "Extent",
                   2035:                     'sta'  => "Start",
                   2036:                     'end'  => "End",
                   2037:                     'cau'  => "Co-Author",
                   2038:                     'caa'  => "Assistant Co-Author",
                   2039:                     'ssd'  => "Set Start Date",
                   2040:                     'sed'  => "Set End Date"
                   2041:                                        );
                   2042:         $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n".
                   2043:                   &Apache::loncommon::start_data_table()."\n".
                   2044:                   &Apache::loncommon::start_data_table_header_row()."\n".
                   2045:                   '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
                   2046:                   '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
                   2047:                   '<th>'.$lt{'end'}.'</th>'."\n".
                   2048:                   &Apache::loncommon::end_data_table_header_row()."\n".
                   2049:                   &Apache::loncommon::start_data_table_row().'
                   2050:            <td>
1.291     bisitz   2051:             <input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_ca" />
1.218     raeburn  2052:            </td>
                   2053:            <td>'.$lt{'cau'}.'</td>
                   2054:            <td>'.$cudom.'_'.$cuname.'</td>
                   2055:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
                   2056:              <a href=
                   2057: "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>
                   2058: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
                   2059: <a href=
                   2060: "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".
                   2061:               &Apache::loncommon::end_data_table_row()."\n".
                   2062:               &Apache::loncommon::start_data_table_row()."\n".
1.291     bisitz   2063: '<td><input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
1.218     raeburn  2064: <td>'.$lt{'caa'}.'</td>
                   2065: <td>'.$cudom.'_'.$cuname.'</td>
                   2066: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
                   2067: <a href=
                   2068: "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>
                   2069: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
                   2070: <a href=
                   2071: "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".
                   2072:              &Apache::loncommon::end_data_table_row()."\n".
                   2073:              &Apache::loncommon::end_data_table());
                   2074:     } elsif ($env{'request.role'} =~ /^au\./) {
                   2075:         if (!(&Apache::lonuserutils::authorpriv($env{'user.name'},
                   2076:                                                 $env{'request.role.domain'}))) {
                   2077:             $r->print('<span class="LC_error">'.
                   2078:                       &mt('You do not have privileges to assign co-author roles.').
                   2079:                       '</span>');
                   2080:         } elsif (($env{'user.name'} eq $ccuname) &&
                   2081:              ($env{'user.domain'} eq $ccdomain)) {
1.377     raeburn  2082:             $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  2083:         }
                   2084:     }
                   2085:     return $addrolesdisplay;;
                   2086: }
                   2087: 
                   2088: sub new_domain_roles {
1.357     raeburn  2089:     my ($r,$ccdomain) = @_;
1.218     raeburn  2090:     my $addrolesdisplay = 0;
                   2091:     #
                   2092:     # Domain level
                   2093:     #
                   2094:     my $num_domain_level = 0;
                   2095:     my $domaintext =
                   2096:     '<h4>'.&mt('Domain Level').'</h4>'.
                   2097:     &Apache::loncommon::start_data_table().
                   2098:     &Apache::loncommon::start_data_table_header_row().
                   2099:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
                   2100:     &mt('Extent').'</th>'.
                   2101:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
                   2102:     &Apache::loncommon::end_data_table_header_row();
1.312     raeburn  2103:     my @allroles = &Apache::lonuserutils::roles_by_context('domain');
1.218     raeburn  2104:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
1.312     raeburn  2105:         foreach my $role (@allroles) {
                   2106:             next if ($role eq 'ad');
1.357     raeburn  2107:             next if (($role eq 'au') && ($ccdomain ne $thisdomain));
1.218     raeburn  2108:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
                   2109:                my $plrole=&Apache::lonnet::plaintext($role);
                   2110:                my %lt=&Apache::lonlocal::texthash(
                   2111:                     'ssd'  => "Set Start Date",
                   2112:                     'sed'  => "Set End Date"
                   2113:                                        );
                   2114:                $num_domain_level ++;
                   2115:                $domaintext .=
                   2116: &Apache::loncommon::start_data_table_row().
1.291     bisitz   2117: '<td><input type="checkbox" name="act_'.$thisdomain.'_'.$role.'" /></td>
1.218     raeburn  2118: <td>'.$plrole.'</td>
                   2119: <td>'.$thisdomain.'</td>
                   2120: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
                   2121: <a href=
                   2122: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
                   2123: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
                   2124: <a href=
                   2125: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
                   2126: &Apache::loncommon::end_data_table_row();
                   2127:             }
                   2128:         }
                   2129:     }
                   2130:     $domaintext.= &Apache::loncommon::end_data_table();
                   2131:     if ($num_domain_level > 0) {
                   2132:         $r->print($domaintext);
                   2133:         $addrolesdisplay = 1;
                   2134:     }
                   2135:     return $addrolesdisplay;
                   2136: }
                   2137: 
1.188     raeburn  2138: sub user_authentication {
1.227     raeburn  2139:     my ($ccuname,$ccdomain,$formname) = @_;
1.188     raeburn  2140:     my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
1.227     raeburn  2141:     my $outcome;
1.418     raeburn  2142:     my %lt=&Apache::lonlocal::texthash(
                   2143:                    'err'   => "ERROR",
                   2144:                    'uuas'  => "This user has an unrecognized authentication scheme",
                   2145:                    'adcs'  => "Please alert a domain coordinator of this situation",
                   2146:                    'sldb'  => "Please specify login data below",
                   2147:                    'ld'    => "Login Data"
                   2148:     );
1.188     raeburn  2149:     # Check for a bad authentication type
                   2150:     if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth):/) {
                   2151:         # bad authentication scheme
                   2152:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
1.227     raeburn  2153:             &initialize_authen_forms($ccdomain,$formname);
                   2154: 
1.190     raeburn  2155:             my $choices = &Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc);
1.188     raeburn  2156:             $outcome = <<ENDBADAUTH;
                   2157: <script type="text/javascript" language="Javascript">
1.301     bisitz   2158: // <![CDATA[
1.188     raeburn  2159: $loginscript
1.301     bisitz   2160: // ]]>
1.188     raeburn  2161: </script>
                   2162: <span class="LC_error">$lt{'err'}:
                   2163: $lt{'uuas'} ($currentauth). $lt{'sldb'}.</span>
                   2164: <h3>$lt{'ld'}</h3>
                   2165: $choices
                   2166: ENDBADAUTH
                   2167:         } else {
                   2168:             # This user is not allowed to modify the user's
                   2169:             # authentication scheme, so just notify them of the problem
                   2170:             $outcome = <<ENDBADAUTH;
                   2171: <span class="LC_error"> $lt{'err'}: 
                   2172: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
                   2173: </span>
                   2174: ENDBADAUTH
                   2175:         }
                   2176:     } else { # Authentication type is valid
1.418     raeburn  2177:         
1.227     raeburn  2178:         &initialize_authen_forms($ccdomain,$formname,$currentauth,'modifyuser');
1.205     raeburn  2179:         my ($authformcurrent,$can_modify,@authform_others) =
1.188     raeburn  2180:             &modify_login_block($ccdomain,$currentauth);
                   2181:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2182:             # Current user has login modification privileges
                   2183:             $outcome =
                   2184:                        '<script type="text/javascript" language="Javascript">'."\n".
1.301     bisitz   2185:                        '// <![CDATA['."\n".
1.188     raeburn  2186:                        $loginscript."\n".
1.301     bisitz   2187:                        '// ]]>'."\n".
1.188     raeburn  2188:                        '</script>'."\n".
                   2189:                        '<h3>'.$lt{'ld'}.'</h3>'.
                   2190:                        &Apache::loncommon::start_data_table().
1.205     raeburn  2191:                        &Apache::loncommon::start_data_table_row().
1.188     raeburn  2192:                        '<td>'.$authformnop;
1.418     raeburn  2193:             if (($can_modify) && (&Apache::lonnet::allowed('mau',$ccdomain))) {
1.188     raeburn  2194:                 $outcome .= '</td>'."\n".
                   2195:                             &Apache::loncommon::end_data_table_row().
                   2196:                             &Apache::loncommon::start_data_table_row().
                   2197:                             '<td>'.$authformcurrent.'</td>'.
                   2198:                             &Apache::loncommon::end_data_table_row()."\n";
                   2199:             } else {
1.200     raeburn  2200:                 $outcome .= '&nbsp;('.$authformcurrent.')</td>'.
                   2201:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  2202:             }
1.418     raeburn  2203:             if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2204:                 foreach my $item (@authform_others) { 
                   2205:                     $outcome .= &Apache::loncommon::start_data_table_row().
                   2206:                                 '<td>'.$item.'</td>'.
                   2207:                                 &Apache::loncommon::end_data_table_row()."\n";
                   2208:                 }
1.188     raeburn  2209:             }
1.205     raeburn  2210:             $outcome .= &Apache::loncommon::end_data_table();
1.188     raeburn  2211:         } else {
1.418     raeburn  2212:             if (&Apache::lonnet::allowed('udp',$ccdomain)) {
                   2213:                 # Current user has rights to view domain preferences for user's domain
                   2214:                 my $result;
                   2215:                 if ($currentauth =~ /^krb(4|5):([^:]*)$/) {
                   2216:                     my ($krbver,$krbrealm) = ($1,$2);
                   2217:                     if ($krbrealm eq '') {
                   2218:                         $result = &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
                   2219:                     } else {
                   2220:                         $result = &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
1.426     raeburn  2221:                                       $krbrealm,$krbver);
1.418     raeburn  2222:                     }
                   2223:                 } elsif ($currentauth =~ /^internal:/) {
                   2224:                     $result = &mt('Currently internally authenticated.');
                   2225:                 } elsif ($currentauth =~ /^localauth:/) {
                   2226:                     $result = &mt('Currently using local (institutional) authentication.');
                   2227:                 } elsif ($currentauth =~ /^unix:/) {
                   2228:                     $result = &mt('Currently Filesystem Authenticated.');
                   2229:                 }
                   2230:                 $outcome = '<h3>'.$lt{'ld'}.'</h3>'.
                   2231:                            &Apache::loncommon::start_data_table().
                   2232:                            &Apache::loncommon::start_data_table_row().
                   2233:                            '<td>'.$result.'</td>'.
                   2234:                            &Apache::loncommon::end_data_table_row()."\n".
                   2235:                            &Apache::loncommon::end_data_table();
                   2236:             } elsif (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
1.188     raeburn  2237:                 my %lt=&Apache::lonlocal::texthash(
                   2238:                            'ccld'  => "Change Current Login Data",
                   2239:                            'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
                   2240:                            'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   2241:                 );
                   2242:                 $outcome .= <<ENDNOPRIV;
                   2243: <h3>$lt{'ccld'}</h3>
                   2244: $lt{'yodo'} $lt{'ifch'}: $ccdomain
1.235     raeburn  2245: <input type="hidden" name="login" value="nochange" />
1.188     raeburn  2246: ENDNOPRIV
                   2247:             }
                   2248:         }
                   2249:     }  ## End of "check for bad authentication type" logic
                   2250:     return $outcome;
                   2251: }
                   2252: 
1.187     raeburn  2253: sub modify_login_block {
                   2254:     my ($dom,$currentauth) = @_;
                   2255:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2256:     my ($authnum,%can_assign) =
                   2257:         &Apache::loncommon::get_assignable_auth($dom);
1.205     raeburn  2258:     my ($authformcurrent,@authform_others,$show_override_msg);
1.187     raeburn  2259:     if ($currentauth=~/^krb(4|5):/) {
                   2260:         $authformcurrent=$authformkrb;
                   2261:         if ($can_assign{'int'}) {
1.205     raeburn  2262:             push(@authform_others,$authformint);
1.187     raeburn  2263:         }
                   2264:         if ($can_assign{'loc'}) {
1.205     raeburn  2265:             push(@authform_others,$authformloc);
1.187     raeburn  2266:         }
                   2267:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   2268:             $show_override_msg = 1;
                   2269:         }
                   2270:     } elsif ($currentauth=~/^internal:/) {
                   2271:         $authformcurrent=$authformint;
                   2272:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2273:             push(@authform_others,$authformkrb);
1.187     raeburn  2274:         }
                   2275:         if ($can_assign{'loc'}) {
1.205     raeburn  2276:             push(@authform_others,$authformloc);
1.187     raeburn  2277:         }
                   2278:         if ($can_assign{'int'}) {
                   2279:             $show_override_msg = 1;
                   2280:         }
                   2281:     } elsif ($currentauth=~/^unix:/) {
                   2282:         $authformcurrent=$authformfsys;
                   2283:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2284:             push(@authform_others,$authformkrb);
1.187     raeburn  2285:         }
                   2286:         if ($can_assign{'int'}) {
1.205     raeburn  2287:             push(@authform_others,$authformint);
1.187     raeburn  2288:         }
                   2289:         if ($can_assign{'loc'}) {
1.205     raeburn  2290:             push(@authform_others,$authformloc);
1.187     raeburn  2291:         }
                   2292:         if ($can_assign{'fsys'}) {
                   2293:             $show_override_msg = 1;
                   2294:         }
                   2295:     } elsif ($currentauth=~/^localauth:/) {
                   2296:         $authformcurrent=$authformloc;
                   2297:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2298:             push(@authform_others,$authformkrb);
1.187     raeburn  2299:         }
                   2300:         if ($can_assign{'int'}) {
1.205     raeburn  2301:             push(@authform_others,$authformint);
1.187     raeburn  2302:         }
                   2303:         if ($can_assign{'loc'}) {
                   2304:             $show_override_msg = 1;
                   2305:         }
                   2306:     }
                   2307:     if ($show_override_msg) {
1.205     raeburn  2308:         $authformcurrent = '<table><tr><td colspan="3">'.$authformcurrent.
                   2309:                            '</td></tr>'."\n".
                   2310:                            '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
                   2311:                            '<td><b>'.&mt('Currently in use').'</b></td>'.
                   2312:                            '<td align="right"><span class="LC_cusr_emph">'.
1.187     raeburn  2313:                             &mt('will override current values').
1.205     raeburn  2314:                             '</span></td></tr></table>';
1.187     raeburn  2315:     }
1.205     raeburn  2316:     return ($authformcurrent,$show_override_msg,@authform_others); 
1.187     raeburn  2317: }
                   2318: 
1.188     raeburn  2319: sub personal_data_display {
1.391     raeburn  2320:     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$rolesarray,
1.410     raeburn  2321:         $now,$captchaform,$emailusername,$usertype) = @_;
1.388     bisitz   2322:     my ($output,%userenv,%canmodify,%canmodify_status);
1.219     raeburn  2323:     my @userinfo = ('firstname','middlename','lastname','generation',
                   2324:                     'permanentemail','id');
1.252     raeburn  2325:     my $rowcount = 0;
                   2326:     my $editable = 0;
1.391     raeburn  2327:     my %textboxsize = (
                   2328:                        firstname      => '15',
                   2329:                        middlename     => '15',
                   2330:                        lastname       => '15',
                   2331:                        generation     => '5',
                   2332:                        permanentemail => '25',
                   2333:                        id             => '15',
                   2334:                       );
                   2335: 
                   2336:     my %lt=&Apache::lonlocal::texthash(
                   2337:                 'pd'             => "Personal Data",
                   2338:                 'firstname'      => "First Name",
                   2339:                 'middlename'     => "Middle Name",
                   2340:                 'lastname'       => "Last Name",
                   2341:                 'generation'     => "Generation",
                   2342:                 'permanentemail' => "Permanent e-mail address",
                   2343:                 'id'             => "Student/Employee ID",
                   2344:                 'lg'             => "Login Data",
                   2345:                 'inststatus'     => "Affiliation",
                   2346:                 'email'          => 'E-mail address',
                   2347:                 'valid'          => 'Validation',
                   2348:     );
                   2349: 
                   2350:     %canmodify_status =
1.286     raeburn  2351:         &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
                   2352:                                                    ['inststatus'],$rolesarray);
1.253     raeburn  2353:     if (!$newuser) {
1.188     raeburn  2354:         # Get the users information
                   2355:         %userenv = &Apache::lonnet::get('environment',
                   2356:                    ['firstname','middlename','lastname','generation',
1.286     raeburn  2357:                     'permanentemail','id','inststatus'],$ccdomain,$ccuname);
1.219     raeburn  2358:         %canmodify =
                   2359:             &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
1.252     raeburn  2360:                                                        \@userinfo,$rolesarray);
1.257     raeburn  2361:     } elsif ($context eq 'selfcreate') {
1.391     raeburn  2362:         if ($newuser eq 'email') {
1.396     raeburn  2363:             if (ref($emailusername) eq 'HASH') {
                   2364:                 if (ref($emailusername->{$usertype}) eq 'HASH') {
                   2365:                     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
                   2366:                     @userinfo = ();          
                   2367:                     if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
                   2368:                         foreach my $field (@{$infofields}) { 
                   2369:                             if ($emailusername->{$usertype}->{$field}) {
                   2370:                                 push(@userinfo,$field);
                   2371:                                 $canmodify{$field} = 1;
                   2372:                                 unless ($textboxsize{$field}) {
                   2373:                                     $textboxsize{$field} = 25;
                   2374:                                 }
                   2375:                                 unless ($lt{$field}) {
                   2376:                                     $lt{$field} = $infotitles->{$field};
                   2377:                                 }
                   2378:                                 if ($emailusername->{$usertype}->{$field} eq 'required') {
                   2379:                                     $lt{$field} .= '<b>*</b>';
                   2380:                                 }
1.391     raeburn  2381:                             }
                   2382:                         }
                   2383:                     }
                   2384:                 }
                   2385:             }
                   2386:         } else {
                   2387:             %canmodify = &selfcreate_canmodify($context,$ccdomain,\@userinfo,
                   2388:                                                $inst_results,$rolesarray);
                   2389:         }
1.188     raeburn  2390:     }
1.391     raeburn  2391: 
1.188     raeburn  2392:     my $genhelp=&Apache::loncommon::help_open_topic('Generation');
                   2393:     $output = '<h3>'.$lt{'pd'}.'</h3>'.
                   2394:               &Apache::lonhtmlcommon::start_pick_box();
1.391     raeburn  2395:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
1.396     raeburn  2396:         $output .= &Apache::lonhtmlcommon::row_title($lt{'email'}.'<b>*</b>',undef,
1.391     raeburn  2397:                                                      'LC_oddrow_value')."\n".
1.394     raeburn  2398:                    '<input type="text" name="uname" size="25" value="" autocomplete="off" />';
1.391     raeburn  2399:         $rowcount ++;
                   2400:         $output .= &Apache::lonhtmlcommon::row_closure(1);
1.408     raeburn  2401:         my $upassone = '<input type="password" name="upass'.$now.'" size="20" autocomplete="off" />';
                   2402:         my $upasstwo = '<input type="password" name="upasscheck'.$now.'" size="20" autocomplete="off" />';
1.396     raeburn  2403:         $output .= &Apache::lonhtmlcommon::row_title(&mt('Password').'<b>*</b>',
1.391     raeburn  2404:                                                     'LC_pick_box_title',
                   2405:                                                     'LC_oddrow_value')."\n".
                   2406:                    $upassone."\n".
                   2407:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
1.396     raeburn  2408:                    &Apache::lonhtmlcommon::row_title(&mt('Confirm password').'<b>*</b>',
1.391     raeburn  2409:                                                      'LC_pick_box_title',
                   2410:                                                      'LC_oddrow_value')."\n".
                   2411:                    $upasstwo.
                   2412:                    &Apache::lonhtmlcommon::row_closure()."\n";
                   2413:     }
1.188     raeburn  2414:     foreach my $item (@userinfo) {
                   2415:         my $rowtitle = $lt{$item};
1.252     raeburn  2416:         my $hiderow = 0;
1.188     raeburn  2417:         if ($item eq 'generation') {
                   2418:             $rowtitle = $genhelp.$rowtitle;
                   2419:         }
1.252     raeburn  2420:         my $row = &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
1.188     raeburn  2421:         if ($newuser) {
1.210     raeburn  2422:             if (ref($inst_results) eq 'HASH') {
                   2423:                 if ($inst_results->{$item} ne '') {
1.252     raeburn  2424:                     $row .= '<input type="hidden" name="c'.$item.'" value="'.$inst_results->{$item}.'" />'.$inst_results->{$item};
1.210     raeburn  2425:                 } else {
1.252     raeburn  2426:                     if ($context eq 'selfcreate') {
1.391     raeburn  2427:                         if ($canmodify{$item}) {
1.394     raeburn  2428:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.252     raeburn  2429:                             $editable ++;
                   2430:                         } else {
                   2431:                             $hiderow = 1;
                   2432:                         }
1.253     raeburn  2433:                     } else {
                   2434:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2435:                     }
1.210     raeburn  2436:                 }
1.188     raeburn  2437:             } else {
1.252     raeburn  2438:                 if ($context eq 'selfcreate') {
1.401     raeburn  2439:                     if ($canmodify{$item}) {
                   2440:                         if ($newuser eq 'email') {
                   2441:                             $row .= '<input type="text" name="'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.287     raeburn  2442:                         } else {
1.401     raeburn  2443:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.287     raeburn  2444:                         }
1.401     raeburn  2445:                         $editable ++;
                   2446:                     } else {
                   2447:                         $hiderow = 1;
1.252     raeburn  2448:                     }
1.253     raeburn  2449:                 } else {
                   2450:                     $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2451:                 }
1.188     raeburn  2452:             }
                   2453:         } else {
1.219     raeburn  2454:             if ($canmodify{$item}) {
1.252     raeburn  2455:                 $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
1.393     raeburn  2456:                 if (($item eq 'id') && (!$newuser)) {
                   2457:                     $row .= '<br />'.&Apache::lonuserutils::forceid_change($context);
                   2458:                 }
1.188     raeburn  2459:             } else {
1.252     raeburn  2460:                 $row .= $userenv{$item};
1.188     raeburn  2461:             }
                   2462:         }
1.252     raeburn  2463:         $row .= &Apache::lonhtmlcommon::row_closure(1);
                   2464:         if (!$hiderow) {
                   2465:             $output .= $row;
                   2466:             $rowcount ++;
                   2467:         }
1.188     raeburn  2468:     }
1.286     raeburn  2469:     if (($canmodify_status{'inststatus'}) || ($context ne 'selfcreate')) {
                   2470:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($ccdomain);
                   2471:         if (ref($types) eq 'ARRAY') {
                   2472:             if (@{$types} > 0) {
                   2473:                 my ($hiderow,$shown);
                   2474:                 if ($canmodify_status{'inststatus'}) {
                   2475:                     $shown = &pick_inst_statuses($userenv{'inststatus'},$usertypes,$types);
                   2476:                 } else {
                   2477:                     if ($userenv{'inststatus'} eq '') {
                   2478:                         $hiderow = 1;
1.334     raeburn  2479:                     } else {
                   2480:                         my @showitems;
                   2481:                         foreach my $item ( map { &unescape($_); } split(':',$userenv{'inststatus'})) {
                   2482:                             if (exists($usertypes->{$item})) {
                   2483:                                 push(@showitems,$usertypes->{$item});
                   2484:                             } else {
                   2485:                                 push(@showitems,$item);
                   2486:                             }
                   2487:                         }
                   2488:                         if (@showitems) {
                   2489:                             $shown = join(', ',@showitems);
                   2490:                         } else {
                   2491:                             $hiderow = 1;
                   2492:                         }
1.286     raeburn  2493:                     }
                   2494:                 }
                   2495:                 if (!$hiderow) {
1.389     bisitz   2496:                     my $row = &Apache::lonhtmlcommon::row_title(&mt('Affiliations'),undef,'LC_oddrow_value')."\n".
1.286     raeburn  2497:                               $shown.&Apache::lonhtmlcommon::row_closure(1); 
                   2498:                     if ($context eq 'selfcreate') {
                   2499:                         $rowcount ++;
                   2500:                     }
                   2501:                     $output .= $row;
                   2502:                 }
                   2503:             }
                   2504:         }
                   2505:     }
1.391     raeburn  2506:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
                   2507:         if ($captchaform) {
1.410     raeburn  2508:             $output .= &Apache::lonhtmlcommon::row_title($lt{'valid'}.'*',
                   2509:                                                          'LC_pick_box_title')."\n".
                   2510:                        $captchaform."\n".'<br /><br />'.
1.391     raeburn  2511:                        &Apache::lonhtmlcommon::row_closure(1); 
                   2512:             $rowcount ++;
                   2513:         }
                   2514:         my $submit_text = &mt('Create account');
                   2515:         $output .= &Apache::lonhtmlcommon::row_title()."\n".
                   2516:                    '<br /><input type="submit" name="createaccount" value="'.
                   2517:                    $submit_text.'" />'.
1.396     raeburn  2518:                    '<input type="hidden" name="type" value="'.$usertype.'" />'.
1.391     raeburn  2519:                    &Apache::lonhtmlcommon::row_closure(1);
                   2520:     }
1.188     raeburn  2521:     $output .= &Apache::lonhtmlcommon::end_pick_box();
1.206     raeburn  2522:     if (wantarray) {
1.252     raeburn  2523:         if ($context eq 'selfcreate') {
                   2524:             return($output,$rowcount,$editable);
                   2525:         } else {
1.388     bisitz   2526:             return $output;
1.252     raeburn  2527:         }
1.206     raeburn  2528:     } else {
                   2529:         return $output;
                   2530:     }
1.188     raeburn  2531: }
                   2532: 
1.286     raeburn  2533: sub pick_inst_statuses {
                   2534:     my ($curr,$usertypes,$types) = @_;
                   2535:     my ($output,$rem,@currtypes);
                   2536:     if ($curr ne '') {
                   2537:         @currtypes = map { &unescape($_); } split(/:/,$curr);
                   2538:     }
                   2539:     my $numinrow = 2;
                   2540:     if (ref($types) eq 'ARRAY') {
                   2541:         $output = '<table>';
                   2542:         my $lastcolspan; 
                   2543:         for (my $i=0; $i<@{$types}; $i++) {
                   2544:             if (defined($usertypes->{$types->[$i]})) {
                   2545:                 my $rem = $i%($numinrow);
                   2546:                 if ($rem == 0) {
                   2547:                     if ($i<@{$types}-1) {
                   2548:                         if ($i > 0) { 
                   2549:                             $output .= '</tr>';
                   2550:                         }
                   2551:                         $output .= '<tr>';
                   2552:                     }
                   2553:                 } elsif ($i==@{$types}-1) {
                   2554:                     my $colsleft = $numinrow - $rem;
                   2555:                     if ($colsleft > 1) {
                   2556:                         $lastcolspan = ' colspan="'.$colsleft.'"';
                   2557:                     }
                   2558:                 }
                   2559:                 my $check = ' ';
                   2560:                 if (grep(/^\Q$types->[$i]\E$/,@currtypes)) {
                   2561:                     $check = ' checked="checked" ';
                   2562:                 }
                   2563:                 $output .= '<td class="LC_left_item"'.$lastcolspan.'>'.
                   2564:                            '<span class="LC_nobreak"><label>'.
                   2565:                            '<input type="checkbox" name="inststatus" '.
                   2566:                            'value="'.$types->[$i].'"'.$check.'/>'.
                   2567:                            $usertypes->{$types->[$i]}.'</label></span></td>';
                   2568:             }
                   2569:         }
                   2570:         $output .= '</tr></table>';
                   2571:     }
                   2572:     return $output;
                   2573: }
                   2574: 
1.257     raeburn  2575: sub selfcreate_canmodify {
                   2576:     my ($context,$dom,$userinfo,$inst_results,$rolesarray) = @_;
                   2577:     if (ref($inst_results) eq 'HASH') {
                   2578:         my @inststatuses = &get_inststatuses($inst_results);
                   2579:         if (@inststatuses == 0) {
                   2580:             @inststatuses = ('default');
                   2581:         }
                   2582:         $rolesarray = \@inststatuses;
                   2583:     }
                   2584:     my %canmodify =
                   2585:         &Apache::lonuserutils::can_modify_userinfo($context,$dom,$userinfo,
                   2586:                                                    $rolesarray);
                   2587:     return %canmodify;
                   2588: }
                   2589: 
1.252     raeburn  2590: sub get_inststatuses {
                   2591:     my ($insthashref) = @_;
                   2592:     my @inststatuses = ();
                   2593:     if (ref($insthashref) eq 'HASH') {
                   2594:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
                   2595:             @inststatuses = @{$insthashref->{'inststatus'}};
                   2596:         }
                   2597:     }
                   2598:     return @inststatuses;
                   2599: }
                   2600: 
1.4       www      2601: # ================================================================= Phase Three
1.42      matthew  2602: sub update_user_data {
1.375     raeburn  2603:     my ($r,$context,$crstype,$brcrum,$showcredits) = @_; 
1.101     albertel 2604:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                   2605:                                           $env{'form.ccdomain'});
1.27      matthew  2606:     # Error messages
1.188     raeburn  2607:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
1.193     raeburn  2608:     my $end       = '</span><br /><br />';
                   2609:     my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
1.188     raeburn  2610:                     "'$env{'form.prevphase'}','modify')".'" />'.
1.219     raeburn  2611:                     &mt('Return to previous page').'</a>'.
                   2612:                     &Apache::loncommon::end_page();
                   2613:     my $now = time;
1.40      www      2614:     my $title;
1.101     albertel 2615:     if (exists($env{'form.makeuser'})) {
1.40      www      2616: 	$title='Set Privileges for New User';
                   2617:     } else {
                   2618:         $title='Modify User Privileges';
                   2619:     }
1.213     raeburn  2620:     my $newuser = 0;
1.160     raeburn  2621:     my ($jsback,$elements) = &crumb_utilities();
                   2622:     my $jscript = '<script type="text/javascript">'."\n".
1.301     bisitz   2623:                   '// <![CDATA['."\n".
                   2624:                   $jsback."\n".
                   2625:                   '// ]]>'."\n".
                   2626:                   '</script>'."\n";
1.422     raeburn  2627:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$env{'form.ccdomain'});
1.351     raeburn  2628:     push (@{$brcrum},
                   2629:              {href => "javascript:backPage(document.userupdate)",
                   2630:               text => $breadcrumb_text{'search'},
                   2631:               faq  => 282,
                   2632:               bug  => 'Instructor Interface',}
                   2633:              );
                   2634:     if ($env{'form.prevphase'} eq 'userpicked') {
                   2635:         push(@{$brcrum},
                   2636:                {href => "javascript:backPage(document.userupdate,'get_user_info','select')",
                   2637:                 text => $breadcrumb_text{'userpicked'},
                   2638:                 faq  => 282,
                   2639:                 bug  => 'Instructor Interface',});
1.233     raeburn  2640:     }
1.224     raeburn  2641:     my $helpitem = 'Course_Change_Privileges';
                   2642:     if ($env{'form.action'} eq 'singlestudent') {
                   2643:         $helpitem = 'Course_Add_Student';
                   2644:     }
1.351     raeburn  2645:     push(@{$brcrum}, 
                   2646:             {href => "javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
                   2647:              text => $breadcrumb_text{'modify'},
                   2648:              faq  => 282,
                   2649:              bug  => 'Instructor Interface',},
                   2650:             {href => "/adm/createuser",
                   2651:              text => "Result",
                   2652:              faq  => 282,
                   2653:              bug  => 'Instructor Interface',
                   2654:              help => $helpitem});
                   2655:     my $args = {bread_crumbs          => $brcrum,
                   2656:                 bread_crumbs_component => 'User Management'};
                   2657:     if ($env{'form.popup'}) {
                   2658:         $args->{'no_nav_bar'} = 1;
                   2659:     }
                   2660:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
1.188     raeburn  2661:     $r->print(&update_result_form($uhome));
1.27      matthew  2662:     # Check Inputs
1.101     albertel 2663:     if (! $env{'form.ccuname'} ) {
1.193     raeburn  2664: 	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
1.27      matthew  2665: 	return;
                   2666:     }
1.138     albertel 2667:     if (  $env{'form.ccuname'} ne 
                   2668: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
1.281     bisitz   2669: 	$r->print($error.&mt('Invalid login name.').'  '.
                   2670: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid.').
1.193     raeburn  2671: 		  $end.$rtnlink);
1.27      matthew  2672: 	return;
                   2673:     }
1.101     albertel 2674:     if (! $env{'form.ccdomain'}       ) {
1.193     raeburn  2675: 	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
1.27      matthew  2676: 	return;
                   2677:     }
1.138     albertel 2678:     if (  $env{'form.ccdomain'} ne
                   2679: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
1.281     bisitz   2680: 	$r->print($error.&mt('Invalid domain name.').'  '.
                   2681: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid.').
1.193     raeburn  2682: 		  $end.$rtnlink);
1.27      matthew  2683: 	return;
                   2684:     }
1.219     raeburn  2685:     if ($uhome eq 'no_host') {
                   2686:         $newuser = 1;
                   2687:     }
1.101     albertel 2688:     if (! exists($env{'form.makeuser'})) {
1.29      matthew  2689:         # Modifying an existing user, so check the validity of the name
                   2690:         if ($uhome eq 'no_host') {
1.389     bisitz   2691:             $r->print(
                   2692:                 $error
                   2693:                .'<p class="LC_error">'
                   2694:                .&mt('Unable to determine home server for [_1] in domain [_2].',
                   2695:                         '"'.$env{'form.ccuname'}.'"','"'.$env{'form.ccdomain'}.'"')
                   2696:                .'</p>');
1.29      matthew  2697:             return;
                   2698:         }
                   2699:     }
1.27      matthew  2700:     # Determine authentication method and password for the user being modified
                   2701:     my $amode='';
                   2702:     my $genpwd='';
1.101     albertel 2703:     if ($env{'form.login'} eq 'krb') {
1.41      albertel 2704: 	$amode='krb';
1.101     albertel 2705: 	$amode.=$env{'form.krbver'};
                   2706: 	$genpwd=$env{'form.krbarg'};
                   2707:     } elsif ($env{'form.login'} eq 'int') {
1.27      matthew  2708: 	$amode='internal';
1.101     albertel 2709: 	$genpwd=$env{'form.intarg'};
                   2710:     } elsif ($env{'form.login'} eq 'fsys') {
1.27      matthew  2711: 	$amode='unix';
1.101     albertel 2712: 	$genpwd=$env{'form.fsysarg'};
                   2713:     } elsif ($env{'form.login'} eq 'loc') {
1.27      matthew  2714: 	$amode='localauth';
1.101     albertel 2715: 	$genpwd=$env{'form.locarg'};
1.27      matthew  2716: 	$genpwd=" " if (!$genpwd);
1.101     albertel 2717:     } elsif (($env{'form.login'} eq 'nochange') ||
                   2718:              ($env{'form.login'} eq ''        )) { 
1.34      matthew  2719:         # There is no need to tell the user we did not change what they
                   2720:         # did not ask us to change.
1.35      matthew  2721:         # If they are creating a new user but have not specified login
                   2722:         # information this will be caught below.
1.30      matthew  2723:     } else {
1.367     golterma 2724:             $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);
                   2725:             return;
1.27      matthew  2726:     }
1.164     albertel 2727: 
1.188     raeburn  2728:     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
1.367     golterma 2729:                         $env{'form.ccuname'}.' ('.&Apache::loncommon::plainname($env{'form.ccuname'},
                   2730:                         $env{'form.ccdomain'}).')', $env{'form.ccdomain'}).'</h3>');
                   2731:     my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,2);
1.344     bisitz   2732: 
1.193     raeburn  2733:     my (%alerts,%rulematch,%inst_results,%curr_rules);
1.334     raeburn  2734:     my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
1.361     raeburn  2735:     my @usertools = ('aboutme','blog','webdav','portfolio');
1.411     raeburn  2736:     my @requestcourses = ('official','unofficial','community','textbook','placement');
1.362     raeburn  2737:     my @requestauthor = ('requestauthor');
1.286     raeburn  2738:     my ($othertitle,$usertypes,$types) = 
                   2739:         &Apache::loncommon::sorted_inst_types($env{'form.ccdomain'});
1.334     raeburn  2740:     my %canmodify_status =
                   2741:         &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},
                   2742:                                                    ['inststatus']);
1.101     albertel 2743:     if ($env{'form.makeuser'}) {
1.164     albertel 2744: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
1.27      matthew  2745:         # Check for the authentication mode and password
                   2746:         if (! $amode || ! $genpwd) {
1.193     raeburn  2747: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.27      matthew  2748: 	    return;
1.18      albertel 2749: 	}
1.29      matthew  2750:         # Determine desired host
1.101     albertel 2751:         my $desiredhost = $env{'form.hserver'};
1.29      matthew  2752:         if (lc($desiredhost) eq 'default') {
                   2753:             $desiredhost = undef;
                   2754:         } else {
1.147     albertel 2755:             my %home_servers = 
                   2756: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
1.29      matthew  2757:             if (! exists($home_servers{$desiredhost})) {
1.193     raeburn  2758:                 $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
                   2759:                 return;
                   2760:             }
                   2761:         }
                   2762:         # Check ID format
                   2763:         my %checkhash;
                   2764:         my %checks = ('id' => 1);
                   2765:         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
1.219     raeburn  2766:             'newuser' => $newuser, 
1.196     raeburn  2767:             'id' => $env{'form.cid'},
1.193     raeburn  2768:         );
1.196     raeburn  2769:         if ($env{'form.cid'} ne '') {
                   2770:             &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   2771:                                           \%rulematch,\%inst_results,\%curr_rules);
                   2772:             if (ref($alerts{'id'}) eq 'HASH') {
                   2773:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
                   2774:                     my $domdesc =
                   2775:                         &Apache::lonnet::domain($env{'form.ccdomain'},'description');
                   2776:                     if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
                   2777:                         my $userchkmsg;
                   2778:                         if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
                   2779:                             $userchkmsg  = 
                   2780:                                 &Apache::loncommon::instrule_disallow_msg('id',
                   2781:                                                                     $domdesc,1).
                   2782:                                 &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
                   2783:                                     $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
                   2784:                         }
                   2785:                         $r->print($error.&mt('Invalid ID format').$end.
                   2786:                                   $userchkmsg.$rtnlink);
                   2787:                         return;
                   2788:                     }
                   2789:                 }
1.29      matthew  2790:             }
                   2791:         }
1.367     golterma 2792:         &Apache::lonhtmlcommon::Increment_PrgWin($r, \%prog_state);
1.27      matthew  2793: 	# Call modifyuser
                   2794: 	my $result = &Apache::lonnet::modifyuser
1.193     raeburn  2795: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
1.188     raeburn  2796:              $amode,$genpwd,$env{'form.cfirstname'},
                   2797:              $env{'form.cmiddlename'},$env{'form.clastname'},
                   2798:              $env{'form.cgeneration'},undef,$desiredhost,
                   2799:              $env{'form.cpermanentemail'});
1.77      www      2800: 	$r->print(&mt('Generating user').': '.$result);
1.219     raeburn  2801:         $uhome = &Apache::lonnet::homeserver($env{'form.ccuname'},
1.101     albertel 2802:                                                $env{'form.ccdomain'});
1.334     raeburn  2803:         my (%changeHash,%newcustom,%changed,%changedinfo);
1.267     raeburn  2804:         if ($uhome ne 'no_host') {
1.334     raeburn  2805:             if ($context eq 'domain') {
1.378     raeburn  2806:                 foreach my $name ('portfolio','author') {
                   2807:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   2808:                         if ($env{'form.'.$name.'quota'} eq '') {
                   2809:                             $newcustom{$name.'quota'} = 0;
                   2810:                         } else {
                   2811:                             $newcustom{$name.'quota'} = $env{'form.'.$name.'quota'};
                   2812:                             $newcustom{$name.'quota'} =~ s/[^\d\.]//g;
                   2813:                         }
                   2814:                         if (&quota_admin($newcustom{$name.'quota'},\%changeHash,$name)) {
                   2815:                             $changed{$name.'quota'} = 1;
                   2816:                         }
1.334     raeburn  2817:                     }
                   2818:                 }
                   2819:                 foreach my $item (@usertools) {
                   2820:                     if ($env{'form.custom'.$item} == 1) {
                   2821:                         $newcustom{$item} = $env{'form.tools_'.$item};
                   2822:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2823:                                                      \%changeHash,'tools');
                   2824:                     }
1.267     raeburn  2825:                 }
1.334     raeburn  2826:                 foreach my $item (@requestcourses) {
1.341     raeburn  2827:                     if ($env{'form.custom'.$item} == 1) {
                   2828:                         $newcustom{$item} = $env{'form.crsreq_'.$item};
                   2829:                         if ($env{'form.crsreq_'.$item} eq 'autolimit') {
                   2830:                             $newcustom{$item} .= '=';
1.383     raeburn  2831:                             $env{'form.crsreq_'.$item.'_limit'} =~ s/\D+//g;
                   2832:                             if ($env{'form.crsreq_'.$item.'_limit'}) {
1.341     raeburn  2833:                                 $newcustom{$item} .= $env{'form.crsreq_'.$item.'_limit'};
                   2834:                             }
1.334     raeburn  2835:                         }
1.341     raeburn  2836:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2837:                                                       \%changeHash,'requestcourses');
1.334     raeburn  2838:                     }
1.275     raeburn  2839:                 }
1.362     raeburn  2840:                 if ($env{'form.customrequestauthor'} == 1) {
                   2841:                     $newcustom{'requestauthor'} = $env{'form.requestauthor'};
                   2842:                     $changed{'requestauthor'} = &tool_admin('requestauthor',
                   2843:                                                     $newcustom{'requestauthor'},
                   2844:                                                     \%changeHash,'requestauthor');
                   2845:                 }
1.275     raeburn  2846:             }
1.334     raeburn  2847:             if ($canmodify_status{'inststatus'}) {
                   2848:                 if (exists($env{'form.inststatus'})) {
                   2849:                     my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2850:                     if (@inststatuses > 0) {
                   2851:                         $changeHash{'inststatus'} = join(',',@inststatuses);
                   2852:                         $changed{'inststatus'} = $changeHash{'inststatus'};
1.306     raeburn  2853:                     }
                   2854:                 }
1.232     raeburn  2855:             }
1.334     raeburn  2856:             if (keys(%changed)) {
                   2857:                 foreach my $item (@userinfo) {
                   2858:                     $changeHash{$item}  = $env{'form.c'.$item};
1.286     raeburn  2859:                 }
1.267     raeburn  2860:                 my $chgresult =
                   2861:                      &Apache::lonnet::put('environment',\%changeHash,
                   2862:                                           $env{'form.ccdomain'},$env{'form.ccuname'});
                   2863:             } 
1.232     raeburn  2864:         }
1.219     raeburn  2865:         $r->print('<br />'.&mt('Home server').': '.$uhome.' '.
                   2866:                   &Apache::lonnet::hostname($uhome));
1.101     albertel 2867:     } elsif (($env{'form.login'} ne 'nochange') &&
                   2868:              ($env{'form.login'} ne ''        )) {
1.27      matthew  2869: 	# Modify user privileges
                   2870:         if (! $amode || ! $genpwd) {
1.193     raeburn  2871: 	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
1.27      matthew  2872: 	    return;
1.20      harris41 2873: 	}
1.395     bisitz   2874: 	# Only allow authentication modification if the person has authority
1.101     albertel 2875: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.20      harris41 2876: 	    $r->print('Modifying authentication: '.
1.31      matthew  2877:                       &Apache::lonnet::modifyuserauth(
1.101     albertel 2878: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
1.21      harris41 2879:                        $amode,$genpwd));
1.102     albertel 2880:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
1.101     albertel 2881: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
1.4       www      2882: 	} else {
1.27      matthew  2883: 	    # Okay, this is a non-fatal error.
1.395     bisitz   2884: 	    $r->print($error.&mt('You do not have the authority to modify this users authentication information.').$end);    
1.27      matthew  2885: 	}
1.28      matthew  2886:     }
1.344     bisitz   2887:     $r->rflush(); # Finish display of header before time consuming actions start
1.367     golterma 2888:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state);
1.28      matthew  2889:     ##
1.375     raeburn  2890:     my (@userroles,%userupdate,$cnum,$cdom,$defaultcredits,%namechanged);
1.213     raeburn  2891:     if ($context eq 'course') {
1.375     raeburn  2892:         ($cnum,$cdom) =
                   2893:             &Apache::lonuserutils::get_course_identity();
1.318     raeburn  2894:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
1.375     raeburn  2895:         if ($showcredits) {
                   2896:            $defaultcredits = &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   2897:         }
1.213     raeburn  2898:     }
1.101     albertel 2899:     if (! $env{'form.makeuser'} ) {
1.28      matthew  2900:         # Check for need to change
                   2901:         my %userenv = &Apache::lonnet::get
1.134     raeburn  2902:             ('environment',['firstname','middlename','lastname','generation',
1.378     raeburn  2903:              'id','permanentemail','portfolioquota','authorquota','inststatus',
                   2904:              'tools.aboutme','tools.blog','tools.webdav','tools.portfolio',
1.361     raeburn  2905:              'requestcourses.official','requestcourses.unofficial',
1.384     raeburn  2906:              'requestcourses.community','requestcourses.textbook',
                   2907:              'reqcrsotherdom.official','reqcrsotherdom.unofficial',
                   2908:              'reqcrsotherdom.community','reqcrsotherdom.textbook',
1.427   ! raeburn  2909:              'reqcrsotherdom.placement','requestauthor'],
1.160     raeburn  2910:               $env{'form.ccdomain'},$env{'form.ccuname'});
1.28      matthew  2911:         my ($tmp) = keys(%userenv);
                   2912:         if ($tmp =~ /^(con_lost|error)/i) { 
                   2913:             %userenv = ();
                   2914:         }
1.206     raeburn  2915:         my $no_forceid_alert;
                   2916:         # Check to see if user information can be changed
                   2917:         my %domconfig =
                   2918:             &Apache::lonnet::get_dom('configuration',['usermodification'],
                   2919:                                      $env{'form.ccdomain'});
1.213     raeburn  2920:         my @statuses = ('active','future');
                   2921:         my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@statuses,undef,$env{'request.role.domain'});
                   2922:         my ($auname,$audom);
1.220     raeburn  2923:         if ($context eq 'author') {
1.206     raeburn  2924:             $auname = $env{'user.name'};
                   2925:             $audom = $env{'user.domain'};     
                   2926:         }
                   2927:         foreach my $item (keys(%roles)) {
1.220     raeburn  2928:             my ($rolenum,$roledom,$role) = split(/:/,$item,-1);
1.206     raeburn  2929:             if ($context eq 'course') {
                   2930:                 if ($cnum ne '' && $cdom ne '') {
                   2931:                     if ($rolenum eq $cnum && $roledom eq $cdom) {
                   2932:                         if (!grep(/^\Q$role\E$/,@userroles)) {
                   2933:                             push(@userroles,$role);
                   2934:                         }
                   2935:                     }
                   2936:                 }
                   2937:             } elsif ($context eq 'author') {
                   2938:                 if ($rolenum eq $auname && $roledom eq $audom) {
                   2939:                     if (!grep(/^\Q$role\E$/,@userroles)) { 
                   2940:                         push(@userroles,$role);
                   2941:                     }
                   2942:                 }
                   2943:             }
                   2944:         }
1.220     raeburn  2945:         if ($env{'form.action'} eq 'singlestudent') {
                   2946:             if (!grep(/^st$/,@userroles)) {
                   2947:                 push(@userroles,'st');
                   2948:             }
                   2949:         } else {
                   2950:             # Check for course or co-author roles being activated or re-enabled
                   2951:             if ($context eq 'author' || $context eq 'course') {
                   2952:                 foreach my $key (keys(%env)) {
                   2953:                     if ($context eq 'author') {
                   2954:                         if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
                   2955:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2956:                                 push(@userroles,$1);
                   2957:                             }
                   2958:                         } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
                   2959:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2960:                                 push(@userroles,$1);
                   2961:                             }
1.206     raeburn  2962:                         }
1.220     raeburn  2963:                     } elsif ($context eq 'course') {
                   2964:                         if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
                   2965:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2966:                                 push(@userroles,$1);
                   2967:                             }
                   2968:                         } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
                   2969:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2970:                                 push(@userroles,$1);
                   2971:                             }
1.206     raeburn  2972:                         }
                   2973:                     }
                   2974:                 }
                   2975:             }
                   2976:         }
                   2977:         #Check to see if we can change personal data for the user 
                   2978:         my (@mod_disallowed,@longroles);
                   2979:         foreach my $role (@userroles) {
                   2980:             if ($role eq 'cr') {
                   2981:                 push(@longroles,'Custom');
                   2982:             } else {
1.318     raeburn  2983:                 push(@longroles,&Apache::lonnet::plaintext($role,$crstype)); 
1.206     raeburn  2984:             }
                   2985:         }
1.219     raeburn  2986:         my %canmodify = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},\@userinfo,\@userroles);
                   2987:         foreach my $item (@userinfo) {
1.28      matthew  2988:             # Strip leading and trailing whitespace
1.203     raeburn  2989:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
1.219     raeburn  2990:             if (!$canmodify{$item}) {
1.207     raeburn  2991:                 if (defined($env{'form.c'.$item})) {
                   2992:                     if ($env{'form.c'.$item} ne $userenv{$item}) {
                   2993:                         push(@mod_disallowed,$item);
                   2994:                     }
1.206     raeburn  2995:                 }
                   2996:                 $env{'form.c'.$item} = $userenv{$item};
                   2997:             }
1.28      matthew  2998:         }
1.259     bisitz   2999:         # Check to see if we can change the Student/Employee ID
1.196     raeburn  3000:         my $forceid = $env{'form.forceid'};
                   3001:         my $recurseid = $env{'form.recurseid'};
                   3002:         my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
1.203     raeburn  3003:         my %uidhash = &Apache::lonnet::idrget($env{'form.ccdomain'},
                   3004:                                             $env{'form.ccuname'});
                   3005:         if (($uidhash{$env{'form.ccuname'}}) && 
                   3006:             ($uidhash{$env{'form.ccuname'}}!~/error\:/) && 
                   3007:             (!$forceid)) {
                   3008:             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
                   3009:                 $env{'form.cid'} = $userenv{'id'};
1.293     bisitz   3010:                 $no_forceid_alert = &mt('New student/employee ID does not match existing ID for this user.')
1.259     bisitz   3011:                                    .'<br />'
                   3012:                                    .&mt("Change is not permitted without checking the 'Force ID change' checkbox on the previous page.")
                   3013:                                    .'<br />'."\n";
1.203     raeburn  3014:             }
                   3015:         }
                   3016:         if ($env{'form.cid'} ne $userenv{'id'}) {
1.196     raeburn  3017:             my $checkhash;
                   3018:             my $checks = { 'id' => 1 };
                   3019:             $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
                   3020:                    { 'newuser' => $newuser,
                   3021:                      'id'  => $env{'form.cid'}, 
                   3022:                    };
                   3023:             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   3024:                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
                   3025:             if (ref($alerts{'id'}) eq 'HASH') {
                   3026:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
1.203     raeburn  3027:                    $env{'form.cid'} = $userenv{'id'};
1.196     raeburn  3028:                 }
                   3029:             }
                   3030:         }
1.378     raeburn  3031:         my (%quotachanged,%oldquota,%newquota,%olddefquota,%newdefquota, 
                   3032:             $oldinststatus,$newinststatus,%oldisdefault,%newisdefault,%oldsettings,
1.339     raeburn  3033:             %oldsettingstext,%newsettings,%newsettingstext,@disporder,
1.378     raeburn  3034:             %oldsettingstatus,%newsettingstatus);
1.334     raeburn  3035:         @disporder = ('inststatus');
                   3036:         if ($env{'request.role.domain'} eq $env{'form.ccdomain'}) {
1.362     raeburn  3037:             push(@disporder,'requestcourses','requestauthor');
1.334     raeburn  3038:         } else {
                   3039:             push(@disporder,'reqcrsotherdom');
                   3040:         }
                   3041:         push(@disporder,('quota','tools'));
1.338     raeburn  3042:         $oldinststatus = $userenv{'inststatus'};
1.378     raeburn  3043:         foreach my $name ('portfolio','author') {
                   3044:             ($olddefquota{$name},$oldsettingstatus{$name}) = 
                   3045:                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$oldinststatus,$name);
                   3046:             ($newdefquota{$name},$newsettingstatus{$name}) = ($olddefquota{$name},$oldsettingstatus{$name});
                   3047:         }
1.334     raeburn  3048:         my %canshow;
1.220     raeburn  3049:         if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
1.334     raeburn  3050:             $canshow{'quota'} = 1;
1.220     raeburn  3051:         }
1.267     raeburn  3052:         if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
1.334     raeburn  3053:             $canshow{'tools'} = 1;
1.267     raeburn  3054:         }
1.275     raeburn  3055:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
1.334     raeburn  3056:             $canshow{'requestcourses'} = 1;
1.300     raeburn  3057:         } elsif (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.334     raeburn  3058:             $canshow{'reqcrsotherdom'} = 1;
1.275     raeburn  3059:         }
1.286     raeburn  3060:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.334     raeburn  3061:             $canshow{'inststatus'} = 1;
1.286     raeburn  3062:         }
1.362     raeburn  3063:         if (&Apache::lonnet::allowed('cau',$env{'form.ccdomain'})) {
                   3064:             $canshow{'requestauthor'} = 1;
                   3065:         }
1.267     raeburn  3066:         my (%changeHash,%changed);
1.286     raeburn  3067:         if ($oldinststatus eq '') {
1.334     raeburn  3068:             $oldsettings{'inststatus'} = $othertitle; 
1.286     raeburn  3069:         } else {
                   3070:             if (ref($usertypes) eq 'HASH') {
1.334     raeburn  3071:                 $oldsettings{'inststatus'} = join(', ',map{ $usertypes->{ &unescape($_) }; } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  3072:             } else {
1.334     raeburn  3073:                 $oldsettings{'inststatus'} = join(', ',map{ &unescape($_); } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  3074:             }
                   3075:         }
                   3076:         $changeHash{'inststatus'} = $userenv{'inststatus'};
1.334     raeburn  3077:         if ($canmodify_status{'inststatus'}) {
                   3078:             $canshow{'inststatus'} = 1;
1.286     raeburn  3079:             if (exists($env{'form.inststatus'})) {
                   3080:                 my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   3081:                 if (@inststatuses > 0) {
                   3082:                     $newinststatus = join(':',map { &escape($_); } @inststatuses);
                   3083:                     $changeHash{'inststatus'} = $newinststatus;
                   3084:                     if ($newinststatus ne $oldinststatus) {
                   3085:                         $changed{'inststatus'} = $newinststatus;
1.378     raeburn  3086:                         foreach my $name ('portfolio','author') {
                   3087:                             ($newdefquota{$name},$newsettingstatus{$name}) =
                   3088:                                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   3089:                         }
1.286     raeburn  3090:                     }
                   3091:                     if (ref($usertypes) eq 'HASH') {
1.334     raeburn  3092:                         $newsettings{'inststatus'} = join(', ',map{ $usertypes->{$_}; } (@inststatuses)); 
1.286     raeburn  3093:                     } else {
1.337     raeburn  3094:                         $newsettings{'inststatus'} = join(', ',@inststatuses);
1.286     raeburn  3095:                     }
1.334     raeburn  3096:                 }
                   3097:             } else {
                   3098:                 $newinststatus = '';
                   3099:                 $changeHash{'inststatus'} = $newinststatus;
                   3100:                 $newsettings{'inststatus'} = $othertitle;
                   3101:                 if ($newinststatus ne $oldinststatus) {
                   3102:                     $changed{'inststatus'} = $changeHash{'inststatus'};
1.378     raeburn  3103:                     foreach my $name ('portfolio','author') {
                   3104:                         ($newdefquota{$name},$newsettingstatus{$name}) =
                   3105:                             &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   3106:                     }
1.286     raeburn  3107:                 }
                   3108:             }
1.334     raeburn  3109:         } elsif ($context ne 'selfcreate') {
                   3110:             $canshow{'inststatus'} = 1;
1.337     raeburn  3111:             $newsettings{'inststatus'} = $oldsettings{'inststatus'};
1.286     raeburn  3112:         }
1.378     raeburn  3113:         foreach my $name ('portfolio','author') {
                   3114:             $changeHash{$name.'quota'} = $userenv{$name.'quota'};
                   3115:         }
1.334     raeburn  3116:         if ($context eq 'domain') {
1.378     raeburn  3117:             foreach my $name ('portfolio','author') {
                   3118:                 if ($userenv{$name.'quota'} ne '') {
                   3119:                     $oldquota{$name} = $userenv{$name.'quota'};
                   3120:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   3121:                         if ($env{'form.'.$name.'quota'} eq '') {
                   3122:                             $newquota{$name} = 0;
                   3123:                         } else {
                   3124:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   3125:                             $newquota{$name} =~ s/[^\d\.]//g;
                   3126:                         }
                   3127:                         if ($newquota{$name} != $oldquota{$name}) {
                   3128:                             if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   3129:                                 $changed{$name.'quota'} = 1;
                   3130:                             }
                   3131:                         }
1.334     raeburn  3132:                     } else {
1.378     raeburn  3133:                         if (&quota_admin('',\%changeHash,$name)) {
                   3134:                             $changed{$name.'quota'} = 1;
                   3135:                             $newquota{$name} = $newdefquota{$name};
                   3136:                             $newisdefault{$name} = 1;
                   3137:                         }
1.334     raeburn  3138:                     }
1.149     raeburn  3139:                 } else {
1.378     raeburn  3140:                     $oldisdefault{$name} = 1;
                   3141:                     $oldquota{$name} = $olddefquota{$name};
                   3142:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   3143:                         if ($env{'form.'.$name.'quota'} eq '') {
                   3144:                             $newquota{$name} = 0;
                   3145:                         } else {
                   3146:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   3147:                             $newquota{$name} =~ s/[^\d\.]//g;
                   3148:                         }
                   3149:                         if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   3150:                             $changed{$name.'quota'} = 1;
                   3151:                         }
1.334     raeburn  3152:                     } else {
1.378     raeburn  3153:                         $newquota{$name} = $newdefquota{$name};
                   3154:                         $newisdefault{$name} = 1;
1.334     raeburn  3155:                     }
1.378     raeburn  3156:                 }
                   3157:                 if ($oldisdefault{$name}) {
                   3158:                     $oldsettingstext{'quota'}{$name} = &get_defaultquota_text($oldsettingstatus{$name});
1.383     raeburn  3159:                 }  else {
                   3160:                     $oldsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$oldquota{$name});
1.378     raeburn  3161:                 }
                   3162:                 if ($newisdefault{$name}) {
                   3163:                     $newsettingstext{'quota'}{$name} = &get_defaultquota_text($newsettingstatus{$name});
1.383     raeburn  3164:                 } else {
                   3165:                     $newsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$newquota{$name});
1.134     raeburn  3166:                 }
                   3167:             }
1.334     raeburn  3168:             &tool_changes('tools',\@usertools,\%oldsettings,\%oldsettingstext,\%userenv,
                   3169:                           \%changeHash,\%changed,\%newsettings,\%newsettingstext);
                   3170:             if ($env{'form.ccdomain'} eq $env{'request.role.domain'}) {
                   3171:                 &tool_changes('requestcourses',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   3172:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.384     raeburn  3173:                 &tool_changes('requestauthor',\@requestauthor,\%oldsettings,\%oldsettingstext,
                   3174:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  3175:             } else {
1.334     raeburn  3176:                 &tool_changes('reqcrsotherdom',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   3177:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  3178:             }
                   3179:         }
1.334     raeburn  3180:         foreach my $item (@userinfo) {
                   3181:             if ($env{'form.c'.$item} ne $userenv{$item}) {
                   3182:                 $namechanged{$item} = 1;
                   3183:             }
1.204     raeburn  3184:         }
1.378     raeburn  3185:         foreach my $name ('portfolio','author') {
1.390     bisitz   3186:             $oldsettings{'quota'}{$name} = &mt('[_1] MB',$oldquota{$name});
                   3187:             $newsettings{'quota'}{$name} = &mt('[_1] MB',$newquota{$name});
1.378     raeburn  3188:         }
1.334     raeburn  3189:         if ((keys(%namechanged) > 0) || (keys(%changed) > 0)) {
1.267     raeburn  3190:             my ($chgresult,$namechgresult);
                   3191:             if (keys(%changed) > 0) {
                   3192:                 $chgresult = 
1.204     raeburn  3193:                     &Apache::lonnet::put('environment',\%changeHash,
                   3194:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
1.267     raeburn  3195:                 if ($chgresult eq 'ok') {
                   3196:                     if (($env{'user.name'} eq $env{'form.ccuname'}) &&
                   3197:                         ($env{'user.domain'} eq $env{'form.ccdomain'})) {
1.270     raeburn  3198:                         my %newenvhash;
                   3199:                         foreach my $key (keys(%changed)) {
1.411     raeburn  3200:                             if (($key eq 'official') || ($key eq 'unofficial') ||
                   3201:                                 ($key eq 'community') || ($key eq 'textbook') ||
                   3202:                                 ($key eq 'placement')) {
1.279     raeburn  3203:                                 $newenvhash{'environment.requestcourses.'.$key} =
                   3204:                                     $changeHash{'requestcourses.'.$key};
1.362     raeburn  3205:                                 if ($changeHash{'requestcourses.'.$key}) {
1.332     raeburn  3206:                                     $newenvhash{'environment.canrequest.'.$key} = 1;
1.279     raeburn  3207:                                 } else {
                   3208:                                     $newenvhash{'environment.canrequest.'.$key} =
                   3209:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3210:                                             $key,'reload','requestcourses');
                   3211:                                 }
1.362     raeburn  3212:                             } elsif ($key eq 'requestauthor') {
                   3213:                                 $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3214:                                 if ($changeHash{$key}) {
                   3215:                                     $newenvhash{'environment.canrequest.author'} = 1;
                   3216:                                 } else {
                   3217:                                     $newenvhash{'environment.canrequest.author'} =
                   3218:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3219:                                             $key,'reload','requestauthor');
                   3220:                                 }
1.275     raeburn  3221:                             } elsif ($key ne 'quota') {
1.270     raeburn  3222:                                 $newenvhash{'environment.tools.'.$key} = 
                   3223:                                     $changeHash{'tools.'.$key};
1.279     raeburn  3224:                                 if ($changeHash{'tools.'.$key} ne '') {
                   3225:                                     $newenvhash{'environment.availabletools.'.$key} =
                   3226:                                         $changeHash{'tools.'.$key};
                   3227:                                 } else {
                   3228:                                     $newenvhash{'environment.availabletools.'.$key} =
1.367     golterma 3229:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3230:           $key,'reload','tools');
1.279     raeburn  3231:                                 }
1.270     raeburn  3232:                             }
                   3233:                         }
1.271     raeburn  3234:                         if (keys(%newenvhash)) {
                   3235:                             &Apache::lonnet::appenv(\%newenvhash);
                   3236:                         }
1.267     raeburn  3237:                     }
                   3238:                 }
1.204     raeburn  3239:             }
1.334     raeburn  3240:             if (keys(%namechanged) > 0) {
1.337     raeburn  3241:                 foreach my $field (@userinfo) {
                   3242:                     $changeHash{$field}  = $env{'form.c'.$field};
                   3243:                 }
                   3244: # Make the change
1.204     raeburn  3245:                 $namechgresult =
                   3246:                     &Apache::lonnet::modifyuser($env{'form.ccdomain'},
                   3247:                         $env{'form.ccuname'},$changeHash{'id'},undef,undef,
                   3248:                         $changeHash{'firstname'},$changeHash{'middlename'},
                   3249:                         $changeHash{'lastname'},$changeHash{'generation'},
1.337     raeburn  3250:                         $changeHash{'id'},undef,$changeHash{'permanentemail'},undef,\@userinfo);
1.220     raeburn  3251:                 %userupdate = (
                   3252:                                lastname   => $env{'form.clastname'},
                   3253:                                middlename => $env{'form.cmiddlename'},
                   3254:                                firstname  => $env{'form.cfirstname'},
                   3255:                                generation => $env{'form.cgeneration'},
                   3256:                                id         => $env{'form.cid'},
                   3257:                              );
1.204     raeburn  3258:             }
1.334     raeburn  3259:             if (((keys(%namechanged) > 0) && $namechgresult eq 'ok') || 
1.267     raeburn  3260:                 ((keys(%changed) > 0) && $chgresult eq 'ok')) {
1.28      matthew  3261:             # Tell the user we changed the name
1.334     raeburn  3262:                 &display_userinfo($r,1,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3263:                                   \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,
1.334     raeburn  3264:                                   \%oldsettings, \%oldsettingstext,\%newsettings,
                   3265:                                   \%newsettingstext);
1.203     raeburn  3266:                 if ($env{'form.cid'} ne $userenv{'id'}) {
                   3267:                     &Apache::lonnet::idput($env{'form.ccdomain'},
1.407     raeburn  3268:                          {$env{'form.ccuname'} => $env{'form.cid'}},$uhome,'ids');
1.203     raeburn  3269:                     if (($recurseid) &&
                   3270:                         (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
                   3271:                         my $idresult = 
                   3272:                             &Apache::lonuserutils::propagate_id_change(
                   3273:                                 $env{'form.ccuname'},$env{'form.ccdomain'},
                   3274:                                 \%userupdate);
                   3275:                         $r->print('<br />'.$idresult.'<br />');
                   3276:                     }
1.196     raeburn  3277:                 }
1.149     raeburn  3278:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
                   3279:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
                   3280:                     my %newenvhash;
                   3281:                     foreach my $key (keys(%changeHash)) {
                   3282:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3283:                     }
1.238     raeburn  3284:                     &Apache::lonnet::appenv(\%newenvhash);
1.149     raeburn  3285:                 }
1.28      matthew  3286:             } else { # error occurred
1.389     bisitz   3287:                 $r->print(
                   3288:                     '<p class="LC_error">'
                   3289:                    .&mt('Unable to successfully change environment for [_1] in domain [_2].',
                   3290:                             '"'.$env{'form.ccuname'}.'"',
                   3291:                             '"'.$env{'form.ccdomain'}.'"')
                   3292:                    .'</p>');
1.28      matthew  3293:             }
1.334     raeburn  3294:         } else { # End of if ($env ... ) logic
1.275     raeburn  3295:             # They did not want to change the users name, quota, tool availability,
                   3296:             # or ability to request creation of courses, 
1.267     raeburn  3297:             # but we can still tell them what the name and quota and availabilities are  
1.334     raeburn  3298:             &display_userinfo($r,undef,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3299:                               \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,\%oldsettings,
1.334     raeburn  3300:                               \%oldsettingstext,\%newsettings,\%newsettingstext);
1.28      matthew  3301:         }
1.206     raeburn  3302:         if (@mod_disallowed) {
                   3303:             my ($rolestr,$contextname);
                   3304:             if (@longroles > 0) {
                   3305:                 $rolestr = join(', ',@longroles);
                   3306:             } else {
                   3307:                 $rolestr = &mt('No roles');
                   3308:             }
                   3309:             if ($context eq 'course') {
1.399     bisitz   3310:                 $contextname = 'course';
1.206     raeburn  3311:             } elsif ($context eq 'author') {
1.399     bisitz   3312:                 $contextname = 'co-author';
1.206     raeburn  3313:             }
                   3314:             $r->print(&mt('The following fields were not updated: ').'<ul>');
                   3315:             my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   3316:             foreach my $field (@mod_disallowed) {
                   3317:                 $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
                   3318:             }
1.207     raeburn  3319:             $r->print('</ul>');
                   3320:             if (@mod_disallowed == 1) {
1.399     bisitz   3321:                 $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  3322:             } else {
1.399     bisitz   3323:                 $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  3324:             }
1.292     bisitz   3325:             my $helplink = 'javascript:helpMenu('."'display'".')';
                   3326:             $r->print('<span class="LC_cusr_emph">'.$rolestr.'</span><br />'
                   3327:                      .&mt('Please contact your [_1]helpdesk[_2] for more information.'
                   3328:                          ,'<a href="'.$helplink.'">','</a>')
                   3329:                       .'<br />');
1.206     raeburn  3330:         }
1.259     bisitz   3331:         $r->print('<span class="LC_warning">'
                   3332:                   .$no_forceid_alert
                   3333:                   .&Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules)
                   3334:                   .'</span>');
1.4       www      3335:     }
1.367     golterma 3336:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.220     raeburn  3337:     if ($env{'form.action'} eq 'singlestudent') {
1.375     raeburn  3338:         &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context,
                   3339:                                $crstype,$showcredits,$defaultcredits);
1.386     bisitz   3340:         my $linktext = ($crstype eq 'Community' ?
                   3341:             &mt('Enroll Another Member') : &mt('Enroll Another Student'));
                   3342:         $r->print(
                   3343:             &Apache::lonhtmlcommon::actionbox([
                   3344:                 '<a href="javascript:backPage(document.userupdate)">'
                   3345:                .($crstype eq 'Community' ? 
                   3346:                     &mt('Enroll Another Member') : &mt('Enroll Another Student'))
                   3347:                .'</a>']));
1.220     raeburn  3348:     } else {
1.375     raeburn  3349:         my @rolechanges = &update_roles($r,$context,$showcredits);
1.334     raeburn  3350:         if (keys(%namechanged) > 0) {
1.220     raeburn  3351:             if ($context eq 'course') {
                   3352:                 if (@userroles > 0) {
1.225     raeburn  3353:                     if ((@rolechanges == 0) || 
                   3354:                         (!(grep(/^st$/,@rolechanges)))) {
                   3355:                         if (grep(/^st$/,@userroles)) {
                   3356:                             my $classlistupdated =
                   3357:                                 &Apache::lonuserutils::update_classlist($cdom,
1.220     raeburn  3358:                                               $cnum,$env{'form.ccdomain'},
                   3359:                                        $env{'form.ccuname'},\%userupdate);
1.225     raeburn  3360:                         }
1.220     raeburn  3361:                     }
                   3362:                 }
                   3363:             }
                   3364:         }
1.226     raeburn  3365:         my $userinfo = &Apache::loncommon::plainname($env{'form.ccuname'},
1.233     raeburn  3366:                                                      $env{'form.ccdomain'});
                   3367:         if ($env{'form.popup'}) {
                   3368:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   3369:         } else {
1.367     golterma 3370:             $r->print('<br />'.&Apache::lonhtmlcommon::actionbox(['<a href="javascript:backPage(document.userupdate,'."'$env{'form.prevphase'}','modify'".')">'
                   3371:                      .&mt('Modify this user: [_1]','<span class="LC_cusr_emph">'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.' ('.$userinfo.')</span>').'</a>',
                   3372:                      '<a href="javascript:backPage(document.userupdate)">'.&mt('Create/Modify Another User').'</a>']));
1.233     raeburn  3373:         }
1.220     raeburn  3374:     }
                   3375: }
                   3376: 
1.334     raeburn  3377: sub display_userinfo {
1.362     raeburn  3378:     my ($r,$changed,$order,$canshow,$requestcourses,$usertools,$requestauthor,
                   3379:         $userenv,$changedhash,$namechangedhash,$oldsetting,$oldsettingtext,
1.334     raeburn  3380:         $newsetting,$newsettingtext) = @_;
                   3381:     return unless (ref($order) eq 'ARRAY' &&
                   3382:                    ref($canshow) eq 'HASH' && 
                   3383:                    ref($requestcourses) eq 'ARRAY' && 
1.362     raeburn  3384:                    ref($requestauthor) eq 'ARRAY' &&
1.334     raeburn  3385:                    ref($usertools) eq 'ARRAY' && 
                   3386:                    ref($userenv) eq 'HASH' &&
                   3387:                    ref($changedhash) eq 'HASH' &&
                   3388:                    ref($oldsetting) eq 'HASH' &&
                   3389:                    ref($oldsettingtext) eq 'HASH' &&
                   3390:                    ref($newsetting) eq 'HASH' &&
                   3391:                    ref($newsettingtext) eq 'HASH');
                   3392:     my %lt=&Apache::lonlocal::texthash(
1.372     raeburn  3393:          'ui'             => 'User Information',
1.334     raeburn  3394:          'uic'            => 'User Information Changed',
                   3395:          'firstname'      => 'First Name',
                   3396:          'middlename'     => 'Middle Name',
                   3397:          'lastname'       => 'Last Name',
                   3398:          'generation'     => 'Generation',
                   3399:          'id'             => 'Student/Employee ID',
                   3400:          'permanentemail' => 'Permanent e-mail address',
1.378     raeburn  3401:          'portfolioquota' => 'Disk space allocated to portfolio files',
1.385     bisitz   3402:          'authorquota'    => 'Disk space allocated to Authoring Space',
1.334     raeburn  3403:          'blog'           => 'Blog Availability',
1.361     raeburn  3404:          'webdav'         => 'WebDAV Availability',
1.334     raeburn  3405:          'aboutme'        => 'Personal Information Page Availability',
                   3406:          'portfolio'      => 'Portfolio Availability',
                   3407:          'official'       => 'Can Request Official Courses',
                   3408:          'unofficial'     => 'Can Request Unofficial Courses',
                   3409:          'community'      => 'Can Request Communities',
1.384     raeburn  3410:          'textbook'       => 'Can Request Textbook Courses',
1.411     raeburn  3411:          'placement'      => 'Can Request Placement Tests',
1.362     raeburn  3412:          'requestauthor'  => 'Can Request Author Role',
1.334     raeburn  3413:          'inststatus'     => "Affiliation",
                   3414:          'prvs'           => 'Previous Value:',
                   3415:          'chto'           => 'Changed To:'
                   3416:     );
                   3417:     if ($changed) {
1.372     raeburn  3418:         $r->print('<h3>'.$lt{'uic'}.'</h3>'.
1.367     golterma 3419:                 &Apache::loncommon::start_data_table().
                   3420:                 &Apache::loncommon::start_data_table_header_row());
1.334     raeburn  3421:         $r->print("<th>&nbsp;</th>\n");
1.367     golterma 3422:         $r->print('<th><b>'.$lt{'prvs'}.'</b></th>');
                   3423:         $r->print('<th><span class="LC_nobreak"><b>'.$lt{'chto'}.'</b></span></th>');
                   3424:         $r->print(&Apache::loncommon::end_data_table_header_row());
                   3425:         my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
                   3426: 
1.334     raeburn  3427:         foreach my $item (@userinfo) {
                   3428:             my $value = $env{'form.c'.$item};
1.367     golterma 3429:             #show changes only:
1.383     raeburn  3430:             unless ($value eq $userenv->{$item}){
1.367     golterma 3431:                 $r->print(&Apache::loncommon::start_data_table_row());
                   3432:                 $r->print("<td>$lt{$item}</td>\n");
1.383     raeburn  3433:                 $r->print("<td>".$userenv->{$item}."</td>\n");
1.367     golterma 3434:                 $r->print("<td>$value </td>\n");
                   3435:                 $r->print(&Apache::loncommon::end_data_table_row());
1.334     raeburn  3436:             }
                   3437:         }
                   3438:         foreach my $entry (@{$order}) {
1.383     raeburn  3439:             if ($canshow->{$entry}) {
                   3440:                 if (($entry eq 'requestcourses') || ($entry eq 'reqcrsotherdom') || ($entry eq 'requestauthor')) {
                   3441:                     my @items;
                   3442:                     if ($entry eq 'requestauthor') {
                   3443:                         @items = ($entry);
                   3444:                     } else {
                   3445:                         @items = @{$requestcourses};
1.384     raeburn  3446:                     }
1.383     raeburn  3447:                     foreach my $item (@items) {
                   3448:                         if (($newsetting->{$item} ne $oldsetting->{$item}) || 
                   3449:                             ($newsettingtext->{$item} ne $oldsettingtext->{$item})) {
                   3450:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");  
                   3451:                             $r->print("<td>$lt{$item}</td>\n");
                   3452:                             $r->print("<td>".$oldsetting->{$item});
                   3453:                             if ($oldsettingtext->{$item}) {
                   3454:                                 if ($oldsetting->{$item}) {
                   3455:                                     $r->print(' -- ');
                   3456:                                 }
                   3457:                                 $r->print($oldsettingtext->{$item});
                   3458:                             }
                   3459:                             $r->print("</td>\n");
                   3460:                             $r->print("<td>".$newsetting->{$item});
                   3461:                             if ($newsettingtext->{$item}) {
                   3462:                                 if ($newsetting->{$item}) {
                   3463:                                     $r->print(' -- ');
                   3464:                                 }
                   3465:                                 $r->print($newsettingtext->{$item});
                   3466:                             }
                   3467:                             $r->print("</td>\n");
                   3468:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3469:                         }
                   3470:                     }
                   3471:                 } elsif ($entry eq 'tools') {
                   3472:                     foreach my $item (@{$usertools}) {
1.383     raeburn  3473:                         if ($newsetting->{$item} ne $oldsetting->{$item}) {
                   3474:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3475:                             $r->print("<td>$lt{$item}</td>\n");
                   3476:                             $r->print("<td>".$oldsetting->{$item}.' '.$oldsettingtext->{$item}."</td>\n");
                   3477:                             $r->print("<td>".$newsetting->{$item}.' '.$newsettingtext->{$item}."</td>\n");
                   3478:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3479:                         }
                   3480:                     }
1.378     raeburn  3481:                 } elsif ($entry eq 'quota') {
                   3482:                     if ((ref($oldsetting->{$entry}) eq 'HASH') && (ref($oldsettingtext->{$entry}) eq 'HASH') &&
                   3483:                         (ref($newsetting->{$entry}) eq 'HASH') && (ref($newsettingtext->{$entry}) eq 'HASH')) {
                   3484:                         foreach my $name ('portfolio','author') {
1.383     raeburn  3485:                             if ($newsetting->{$entry}->{$name} ne $oldsetting->{$entry}->{$name}) {
                   3486:                                 $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3487:                                 $r->print("<td>$lt{$name.$entry}</td>\n");
                   3488:                                 $r->print("<td>".$oldsettingtext->{$entry}->{$name}."</td>\n");
                   3489:                                 $r->print("<td>".$newsettingtext->{$entry}->{$name}."</td>\n");
                   3490:                                 $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.378     raeburn  3491:                             }
                   3492:                         }
                   3493:                     }
1.334     raeburn  3494:                 } else {
1.383     raeburn  3495:                     if ($newsetting->{$entry} ne $oldsetting->{$entry}) {
                   3496:                         $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3497:                         $r->print("<td>$lt{$entry}</td>\n");
                   3498:                         $r->print("<td>".$oldsetting->{$entry}.' '.$oldsettingtext->{$entry}."</td>\n");
                   3499:                         $r->print("<td>".$newsetting->{$entry}.' '.$newsettingtext->{$entry}."</td>\n");
                   3500:                         $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3501:                     }
                   3502:                 }
                   3503:             }
                   3504:         }
1.367     golterma 3505:         $r->print(&Apache::loncommon::end_data_table().'<br />');
1.372     raeburn  3506:     } else {
                   3507:         $r->print('<h3>'.$lt{'ui'}.'</h3>'.
                   3508:                   '<p>'.&mt('No changes made to user information').'</p>');
1.334     raeburn  3509:     }
                   3510:     return;
                   3511: }
                   3512: 
1.275     raeburn  3513: sub tool_changes {
                   3514:     my ($context,$usertools,$oldaccess,$oldaccesstext,$userenv,$changeHash,
                   3515:         $changed,$newaccess,$newaccesstext) = @_;
                   3516:     if (!((ref($usertools) eq 'ARRAY') && (ref($oldaccess) eq 'HASH') &&
                   3517:           (ref($oldaccesstext) eq 'HASH') && (ref($userenv) eq 'HASH') &&
                   3518:           (ref($changeHash) eq 'HASH') && (ref($changed) eq 'HASH') &&
                   3519:           (ref($newaccess) eq 'HASH') && (ref($newaccesstext) eq 'HASH'))) {
                   3520:         return;
                   3521:     }
1.383     raeburn  3522:     my %reqdisplay = &requestchange_display();
1.300     raeburn  3523:     if ($context eq 'reqcrsotherdom') {
1.309     raeburn  3524:         my @options = ('approval','validate','autolimit');
1.306     raeburn  3525:         my $optregex = join('|',@options);
1.300     raeburn  3526:         my $cdom = $env{'request.role.domain'};
                   3527:         foreach my $tool (@{$usertools}) {
1.383     raeburn  3528:             $oldaccesstext->{$tool} = &mt("availability set to 'off'");
1.314     raeburn  3529:             $newaccesstext->{$tool} = $oldaccesstext->{$tool};
1.300     raeburn  3530:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
1.383     raeburn  3531:             my ($newop,$limit);
1.314     raeburn  3532:             if ($env{'form.'.$context.'_'.$tool}) {
                   3533:                 $newop = $env{'form.'.$context.'_'.$tool};
                   3534:                 if ($newop eq 'autolimit') {
1.383     raeburn  3535:                     $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
1.314     raeburn  3536:                     $limit =~ s/\D+//g;
                   3537:                     $newop .= '='.$limit;
                   3538:                 }
                   3539:             }
1.300     raeburn  3540:             if ($userenv->{$context.'.'.$tool} eq '') {
1.314     raeburn  3541:                 if ($newop) {
                   3542:                     $changed->{$tool}=&tool_admin($tool,$cdom.':'.$newop,
1.300     raeburn  3543:                                                   $changeHash,$context);
                   3544:                     if ($changed->{$tool}) {
1.383     raeburn  3545:                         if ($newop =~ /^autolimit/) {
                   3546:                             if ($limit) {
                   3547:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3548:                             } else {
                   3549:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3550:                             }
                   3551:                         } else {
                   3552:                             $newaccesstext->{$tool} = $reqdisplay{$newop};
                   3553:                         }
1.300     raeburn  3554:                     } else {
                   3555:                         $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3556:                     }
                   3557:                 }
                   3558:             } else {
                   3559:                 my @curr = split(',',$userenv->{$context.'.'.$tool});
                   3560:                 my @new;
                   3561:                 my $changedoms;
1.314     raeburn  3562:                 foreach my $req (@curr) {
                   3563:                     if ($req =~ /^\Q$cdom\E\:($optregex\=?\d*)$/) {
                   3564:                         my $oldop = $1;
1.383     raeburn  3565:                         if ($oldop =~ /^autolimit=(\d*)/) {
                   3566:                             my $limit = $1;
                   3567:                             if ($limit) {
                   3568:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3569:                             } else {
                   3570:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3571:                             }
                   3572:                         } else {
                   3573:                             $oldaccesstext->{$tool} = $reqdisplay{$oldop};
                   3574:                         }
1.314     raeburn  3575:                         if ($oldop ne $newop) {
                   3576:                             $changedoms = 1;
                   3577:                             foreach my $item (@curr) {
                   3578:                                 my ($reqdom,$option) = split(':',$item);
                   3579:                                 unless ($reqdom eq $cdom) {
                   3580:                                     push(@new,$item);
                   3581:                                 }
                   3582:                             }
                   3583:                             if ($newop) {
                   3584:                                 push(@new,$cdom.':'.$newop);
1.300     raeburn  3585:                             }
1.314     raeburn  3586:                             @new = sort(@new);
1.300     raeburn  3587:                         }
1.314     raeburn  3588:                         last;
1.300     raeburn  3589:                     }
1.314     raeburn  3590:                 }
                   3591:                 if ((!$changedoms) && ($newop)) {
1.300     raeburn  3592:                     $changedoms = 1;
1.306     raeburn  3593:                     @new = sort(@curr,$cdom.':'.$newop);
1.300     raeburn  3594:                 }
                   3595:                 if ($changedoms) {
1.314     raeburn  3596:                     my $newdomstr;
1.300     raeburn  3597:                     if (@new) {
                   3598:                         $newdomstr = join(',',@new);
                   3599:                     }
                   3600:                     $changed->{$tool}=&tool_admin($tool,$newdomstr,$changeHash,
                   3601:                                                   $context);
                   3602:                     if ($changed->{$tool}) {
                   3603:                         if ($env{'form.'.$context.'_'.$tool}) {
1.306     raeburn  3604:                             if ($env{'form.'.$context.'_'.$tool} eq 'autolimit') {
1.314     raeburn  3605:                                 my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3606:                                 $limit =~ s/\D+//g;
                   3607:                                 if ($limit) {
1.383     raeburn  3608:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
1.314     raeburn  3609:                                 } else {
1.383     raeburn  3610:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
1.306     raeburn  3611:                                 }
1.314     raeburn  3612:                             } else {
1.306     raeburn  3613:                                 $newaccesstext->{$tool} = $reqdisplay{$env{'form.'.$context.'_'.$tool}};
                   3614:                             }
1.300     raeburn  3615:                         } else {
1.383     raeburn  3616:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
1.300     raeburn  3617:                         }
                   3618:                     }
                   3619:                 }
                   3620:             }
                   3621:         }
                   3622:         return;
                   3623:     }
1.275     raeburn  3624:     foreach my $tool (@{$usertools}) {
1.383     raeburn  3625:         my ($newval,$limit,$envkey);
1.362     raeburn  3626:         $envkey = $context.'.'.$tool;
1.306     raeburn  3627:         if ($context eq 'requestcourses') {
                   3628:             $newval = $env{'form.crsreq_'.$tool};
                   3629:             if ($newval eq 'autolimit') {
1.383     raeburn  3630:                 $limit = $env{'form.crsreq_'.$tool.'_limit'};
                   3631:                 $limit =~ s/\D+//g;
                   3632:                 $newval .= '='.$limit;
1.306     raeburn  3633:             }
1.362     raeburn  3634:         } elsif ($context eq 'requestauthor') {
                   3635:             $newval = $env{'form.'.$context};
                   3636:             $envkey = $context;
1.314     raeburn  3637:         } else {
1.306     raeburn  3638:             $newval = $env{'form.'.$context.'_'.$tool};
                   3639:         }
1.362     raeburn  3640:         if ($userenv->{$envkey} ne '') {
1.275     raeburn  3641:             $oldaccess->{$tool} = &mt('custom');
1.383     raeburn  3642:             if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3643:                 if ($userenv->{$envkey} =~ /^autolimit=(\d*)$/) {
                   3644:                     my $currlimit = $1;
                   3645:                     if ($currlimit eq '') {
                   3646:                         $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3647:                     } else {
                   3648:                         $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$currlimit);
                   3649:                     }
                   3650:                 } elsif ($userenv->{$envkey}) {
                   3651:                     $oldaccesstext->{$tool} = $reqdisplay{$userenv->{$envkey}};
                   3652:                 } else {
                   3653:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3654:                 }
1.275     raeburn  3655:             } else {
1.383     raeburn  3656:                 if ($userenv->{$envkey}) {
                   3657:                     $oldaccesstext->{$tool} = &mt("availability set to 'on'");
                   3658:                 } else {
                   3659:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3660:                 }
1.275     raeburn  3661:             }
1.362     raeburn  3662:             $changeHash->{$envkey} = $userenv->{$envkey};
1.275     raeburn  3663:             if ($env{'form.custom'.$tool} == 1) {
1.362     raeburn  3664:                 if ($newval ne $userenv->{$envkey}) {
1.306     raeburn  3665:                     $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3666:                                                     $context);
1.275     raeburn  3667:                     if ($changed->{$tool}) {
                   3668:                         $newaccess->{$tool} = &mt('custom');
1.383     raeburn  3669:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3670:                             if ($newval =~ /^autolimit/) {
                   3671:                                 if ($limit) {
                   3672:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3673:                                 } else {
                   3674:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3675:                                 }
                   3676:                             } elsif ($newval) {
                   3677:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3678:                             } else {
                   3679:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3680:                             }
1.275     raeburn  3681:                         } else {
1.383     raeburn  3682:                             if ($newval) {
                   3683:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3684:                             } else {
                   3685:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3686:                             }
1.275     raeburn  3687:                         }
                   3688:                     } else {
                   3689:                         $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  3690:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3691:                             if ($newval =~ /^autolimit/) {
                   3692:                                 if ($limit) {
                   3693:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3694:                                 } else {
                   3695:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3696:                                 }
                   3697:                             } elsif ($newval) {
                   3698:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3699:                             } else {
                   3700:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3701:                             }
1.275     raeburn  3702:                         } else {
1.383     raeburn  3703:                             if ($userenv->{$context.'.'.$tool}) {
                   3704:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3705:                             } else {
                   3706:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3707:                             }
1.275     raeburn  3708:                         }
                   3709:                     }
                   3710:                 } else {
                   3711:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3712:                     $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3713:                 }
                   3714:             } else {
                   3715:                 $changed->{$tool} = &tool_admin($tool,'',$changeHash,$context);
                   3716:                 if ($changed->{$tool}) {
                   3717:                     $newaccess->{$tool} = &mt('default');
                   3718:                 } else {
                   3719:                     $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  3720:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3721:                         if ($newval =~ /^autolimit/) {
                   3722:                             if ($limit) {
                   3723:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3724:                             } else {
                   3725:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3726:                             }
                   3727:                         } elsif ($newval) {
                   3728:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3729:                         } else {
                   3730:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3731:                         }
1.275     raeburn  3732:                     } else {
1.383     raeburn  3733:                         if ($userenv->{$context.'.'.$tool}) {
                   3734:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3735:                         } else {
                   3736:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3737:                         }
1.275     raeburn  3738:                     }
                   3739:                 }
                   3740:             }
                   3741:         } else {
                   3742:             $oldaccess->{$tool} = &mt('default');
                   3743:             if ($env{'form.custom'.$tool} == 1) {
1.306     raeburn  3744:                 $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3745:                                                 $context);
1.275     raeburn  3746:                 if ($changed->{$tool}) {
                   3747:                     $newaccess->{$tool} = &mt('custom');
1.383     raeburn  3748:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3749:                         if ($newval =~ /^autolimit/) {
                   3750:                             if ($limit) {
                   3751:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3752:                             } else {
                   3753:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3754:                             }
                   3755:                         } elsif ($newval) {
                   3756:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3757:                         } else {
                   3758:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3759:                         }
1.275     raeburn  3760:                     } else {
1.383     raeburn  3761:                         if ($newval) {
                   3762:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3763:                         } else {
                   3764:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3765:                         }
1.275     raeburn  3766:                     }
                   3767:                 } else {
                   3768:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3769:                 }
                   3770:             } else {
                   3771:                 $newaccess->{$tool} = $oldaccess->{$tool};
                   3772:             }
                   3773:         }
                   3774:     }
                   3775:     return;
                   3776: }
                   3777: 
1.220     raeburn  3778: sub update_roles {
1.375     raeburn  3779:     my ($r,$context,$showcredits) = @_;
1.4       www      3780:     my $now=time;
1.225     raeburn  3781:     my @rolechanges;
1.220     raeburn  3782:     my %disallowed;
1.73      sakharuk 3783:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
1.404     raeburn  3784:     foreach my $key (keys(%env)) {
1.135     raeburn  3785: 	next if (! $env{$key});
1.190     raeburn  3786:         next if ($key eq 'form.action');
1.27      matthew  3787: 	# Revoke roles
1.135     raeburn  3788: 	if ($key=~/^form\.rev/) {
                   3789: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
1.64      www      3790: # Revoke standard role
1.170     albertel 3791: 		my ($scope,$role) = ($1,$2);
                   3792: 		my $result =
                   3793: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
                   3794: 						$env{'form.ccuname'},
1.239     raeburn  3795: 						$scope,$role,'','',$context);
1.367     golterma 3796:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3797:                             &mt('Revoking [_1] in [_2]',
                   3798:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  3799:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   3800:                                 $result ne "ok").'<br />');
                   3801:                 if ($result ne "ok") {
                   3802:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3803:                 }
1.170     albertel 3804: 		if ($role eq 'st') {
1.202     raeburn  3805: 		    my $result = 
1.198     raeburn  3806:                         &Apache::lonuserutils::classlist_drop($scope,
                   3807:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3808: 			    $now);
1.367     golterma 3809:                     $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.53      www      3810: 		}
1.225     raeburn  3811:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3812:                     push(@rolechanges,$role);
                   3813:                 }
1.196     raeburn  3814: 	    }
1.195     raeburn  3815: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}s) {
1.64      www      3816: # Revoke custom role
1.369     bisitz   3817:                 my $result = &Apache::lonnet::revokecustomrole(
                   3818:                     $env{'form.ccdomain'},$env{'form.ccuname'},$1,$2,$3,$4,'','',$context);
1.367     golterma 3819:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3820:                             &mt('Revoking custom role [_1] by [_2] in [_3]',
1.372     raeburn  3821:                                 $4,$3.':'.$2,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3822:                             $result ne 'ok').'<br />');
                   3823:                 if ($result ne "ok") {
                   3824:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3825:                 }
1.225     raeburn  3826:                 if (!grep(/^cr$/,@rolechanges)) {
                   3827:                     push(@rolechanges,'cr');
                   3828:                 }
1.64      www      3829: 	    }
1.135     raeburn  3830: 	} elsif ($key=~/^form\.del/) {
                   3831: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
1.116     raeburn  3832: # Delete standard role
1.170     albertel 3833: 		my ($scope,$role) = ($1,$2);
                   3834: 		my $result =
                   3835: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
                   3836: 						$env{'form.ccuname'},
1.239     raeburn  3837: 						$scope,$role,$now,0,1,'',
                   3838:                                                 $context);
1.367     golterma 3839:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   3840:                             &mt('Deleting [_1] in [_2]',
1.369     bisitz   3841:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  3842:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   3843:                             $result ne 'ok').'<br />');
                   3844:                 if ($result ne "ok") {
                   3845:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3846:                 }
1.367     golterma 3847: 
1.170     albertel 3848: 		if ($role eq 'st') {
1.202     raeburn  3849: 		    my $result = 
1.198     raeburn  3850:                         &Apache::lonuserutils::classlist_drop($scope,
                   3851:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3852: 			    $now);
1.369     bisitz   3853: 		    $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.81      albertel 3854: 		}
1.225     raeburn  3855:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3856:                     push(@rolechanges,$role);
                   3857:                 }
1.116     raeburn  3858:             }
1.139     albertel 3859: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3860:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3861: # Delete custom role
1.369     bisitz   3862:                 my $result =
                   3863:                     &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
                   3864:                         $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
                   3865:                         0,1,$context);
                   3866:                 $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Deleting custom role [_1] by [_2] in [_3]',
1.372     raeburn  3867:                       $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3868:                       $result ne "ok").'<br />');
                   3869:                 if ($result ne "ok") {
                   3870:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3871:                 }
1.367     golterma 3872: 
1.225     raeburn  3873:                 if (!grep(/^cr$/,@rolechanges)) {
                   3874:                     push(@rolechanges,'cr');
                   3875:                 }
1.116     raeburn  3876:             }
1.135     raeburn  3877: 	} elsif ($key=~/^form\.ren/) {
1.101     albertel 3878:             my $udom = $env{'form.ccdomain'};
                   3879:             my $uname = $env{'form.ccuname'};
1.116     raeburn  3880: # Re-enable standard role
1.135     raeburn  3881: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
1.89      raeburn  3882:                 my $url = $1;
                   3883:                 my $role = $2;
                   3884:                 my $logmsg;
                   3885:                 my $output;
                   3886:                 if ($role eq 'st') {
1.141     albertel 3887:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
1.374     raeburn  3888:                         my ($cdom,$cnum,$csec) = ($1,$2,$3);
1.375     raeburn  3889:                         my $credits;
                   3890:                         if ($showcredits) {
                   3891:                             my $defaultcredits = 
                   3892:                                 &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   3893:                             $credits = &get_user_credits($defaultcredits,$cdom,$cnum);
                   3894:                         }
                   3895:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$cdom,$cnum,$csec,$context,$credits);
1.220     raeburn  3896:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course') || ($result eq 'refused')) {
1.223     raeburn  3897:                             if ($result eq 'refused' && $logmsg) {
                   3898:                                 $output = $logmsg;
                   3899:                             } else { 
1.369     bisitz   3900:                                 $output = &mt('Error: [_1]',$result)."\n";
1.223     raeburn  3901:                             }
1.89      raeburn  3902:                         } else {
1.372     raeburn  3903:                             $output = &Apache::lonhtmlcommon::confirm_success(&mt('Assigning [_1] in [_2] starting [_3]',
                   3904:                                         &Apache::lonnet::plaintext($role),
                   3905:                                         &Apache::loncommon::show_role_extent($url,$context,'st'),
                   3906:                                         &Apache::lonlocal::locallocaltime($now))).'<br />'.$logmsg.'<br />';
1.89      raeburn  3907:                         }
                   3908:                     }
                   3909:                 } else {
1.101     albertel 3910: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
1.239     raeburn  3911:                                $env{'form.ccuname'},$url,$role,0,$now,'','',
                   3912:                                $context);
1.367     golterma 3913:                         $output = &Apache::lonhtmlcommon::confirm_success(&mt('Re-enabling [_1] in [_2]',
1.372     raeburn  3914:                                         &Apache::lonnet::plaintext($role),
                   3915:                                         &Apache::loncommon::show_role_extent($url,$context,$role)),$result ne "ok").'<br />';
1.369     bisitz   3916:                     if ($result ne "ok") {
                   3917:                         $output .= &mt('Error: [_1]',$result).'<br />';
                   3918:                     }
                   3919:                 }
1.89      raeburn  3920:                 $r->print($output);
1.225     raeburn  3921:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3922:                     push(@rolechanges,$role);
                   3923:                 }
1.113     raeburn  3924: 	    }
1.116     raeburn  3925: # Re-enable custom role
1.139     albertel 3926: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3927:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3928:                 my $result = &Apache::lonnet::assigncustomrole(
                   3929:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
1.240     raeburn  3930:                                $url,$rdom,$rnam,$rolename,0,$now,undef,$context);
1.369     bisitz   3931:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   3932:                     &mt('Re-enabling custom role [_1] by [_2] in [_3]',
1.372     raeburn  3933:                         $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3934:                     $result ne "ok").'<br />');
                   3935:                 if ($result ne "ok") {
                   3936:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3937:                 }
1.225     raeburn  3938:                 if (!grep(/^cr$/,@rolechanges)) {
                   3939:                     push(@rolechanges,'cr');
                   3940:                 }
1.116     raeburn  3941:             }
1.135     raeburn  3942: 	} elsif ($key=~/^form\.act/) {
1.101     albertel 3943:             my $udom = $env{'form.ccdomain'};
                   3944:             my $uname = $env{'form.ccuname'};
1.141     albertel 3945: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
1.65      www      3946:                 # Activate a custom role
1.83      albertel 3947: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
                   3948: 		my $url='/'.$one.'/'.$two;
                   3949: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
1.65      www      3950: 
1.101     albertel 3951:                 my $start = ( $env{'form.start_'.$full} ?
                   3952:                               $env{'form.start_'.$full} :
1.88      raeburn  3953:                               $now );
1.101     albertel 3954:                 my $end   = ( $env{'form.end_'.$full} ?
                   3955:                               $env{'form.end_'.$full} :
1.88      raeburn  3956:                               0 );
                   3957:                                                                                      
                   3958:                 # split multiple sections
                   3959:                 my %sections = ();
1.101     albertel 3960:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
1.88      raeburn  3961:                 if ($num_sections == 0) {
1.240     raeburn  3962:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3963:                 } else {
1.114     albertel 3964: 		    my %curr_groups =
1.117     raeburn  3965: 			&Apache::longroup::coursegroups($one,$two);
1.404     raeburn  3966:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.113     raeburn  3967:                         if (($sec eq 'none') || ($sec eq 'all') || 
                   3968:                             exists($curr_groups{$sec})) {
                   3969:                             $disallowed{$sec} = $url;
                   3970:                             next;
                   3971:                         }
                   3972:                         my $securl = $url.'/'.$sec;
1.240     raeburn  3973: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3974:                     }
                   3975:                 }
1.225     raeburn  3976:                 if (!grep(/^cr$/,@rolechanges)) {
                   3977:                     push(@rolechanges,'cr');
                   3978:                 }
1.142     raeburn  3979: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
1.27      matthew  3980: 		# Activate roles for sections with 3 id numbers
                   3981: 		# set start, end times, and the url for the class
1.83      albertel 3982: 		my ($one,$two,$three)=($1,$2,$3);
1.101     albertel 3983: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
                   3984: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
1.27      matthew  3985: 			      $now );
1.101     albertel 3986: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
                   3987: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  3988: 			      0 );
1.83      albertel 3989: 		my $url='/'.$one.'/'.$two;
1.88      raeburn  3990:                 my $type = 'three';
                   3991:                 # split multiple sections
                   3992:                 my %sections = ();
1.101     albertel 3993:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
1.375     raeburn  3994:                 my $credits;
                   3995:                 if ($three eq 'st') {
                   3996:                     if ($showcredits) { 
                   3997:                         my $defaultcredits = 
                   3998:                             &Apache::lonuserutils::get_defaultcredits($one,$two);
                   3999:                         $credits = $env{'form.credits_'.$one.'_'.$two.'_'.$three};
                   4000:                         $credits =~ s/[^\d\.]//g;
                   4001:                         if ($credits eq $defaultcredits) {
                   4002:                             undef($credits);
                   4003:                         }
                   4004:                     }
                   4005:                 }
1.88      raeburn  4006:                 if ($num_sections == 0) {
1.375     raeburn  4007:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  4008:                 } else {
1.114     albertel 4009:                     my %curr_groups = 
1.117     raeburn  4010: 			&Apache::longroup::coursegroups($one,$two);
1.88      raeburn  4011:                     my $emptysec = 0;
1.404     raeburn  4012:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.88      raeburn  4013:                         $sec =~ s/\W//g;
1.113     raeburn  4014:                         if ($sec ne '') {
                   4015:                             if (($sec eq 'none') || ($sec eq 'all') || 
                   4016:                                 exists($curr_groups{$sec})) {
                   4017:                                 $disallowed{$sec} = $url;
                   4018:                                 next;
                   4019:                             }
1.88      raeburn  4020:                             my $securl = $url.'/'.$sec;
1.375     raeburn  4021:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec,$context,$credits));
1.88      raeburn  4022:                         } else {
                   4023:                             $emptysec = 1;
                   4024:                         }
                   4025:                     }
                   4026:                     if ($emptysec) {
1.375     raeburn  4027:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  4028:                     }
1.225     raeburn  4029:                 }
                   4030:                 if (!grep(/^\Q$three\E$/,@rolechanges)) {
                   4031:                     push(@rolechanges,$three);
                   4032:                 }
1.135     raeburn  4033: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
1.27      matthew  4034: 		# Activate roles for sections with two id numbers
                   4035: 		# set start, end times, and the url for the class
1.101     albertel 4036: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
                   4037: 			      $env{'form.start_'.$1.'_'.$2} : 
1.27      matthew  4038: 			      $now );
1.101     albertel 4039: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
                   4040: 			      $env{'form.end_'.$1.'_'.$2} :
1.27      matthew  4041: 			      0 );
1.225     raeburn  4042:                 my $one = $1;
                   4043:                 my $two = $2;
                   4044: 		my $url='/'.$one.'/';
1.88      raeburn  4045:                 # split multiple sections
                   4046:                 my %sections = ();
1.225     raeburn  4047:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two},\%sections,$two);
1.88      raeburn  4048:                 if ($num_sections == 0) {
1.240     raeburn  4049:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  4050:                 } else {
                   4051:                     my $emptysec = 0;
1.404     raeburn  4052:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.88      raeburn  4053:                         if ($sec ne '') {
                   4054:                             my $securl = $url.'/'.$sec;
1.240     raeburn  4055:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$two,$start,$end,$one,undef,$sec,$context));
1.88      raeburn  4056:                         } else {
                   4057:                             $emptysec = 1;
                   4058:                         }
                   4059:                     }
                   4060:                     if ($emptysec) {
1.240     raeburn  4061:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  4062:                     }
                   4063:                 }
1.225     raeburn  4064:                 if (!grep(/^\Q$two\E$/,@rolechanges)) {
                   4065:                     push(@rolechanges,$two);
                   4066:                 }
1.64      www      4067: 	    } else {
1.190     raeburn  4068: 		$r->print('<p><span class="LC_error">'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></span></p><br />');
1.64      www      4069:             }
1.113     raeburn  4070:             foreach my $key (sort(keys(%disallowed))) {
1.274     bisitz   4071:                 $r->print('<p class="LC_warning">');
1.113     raeburn  4072:                 if (($key eq 'none') || ($key eq 'all')) {  
1.274     bisitz   4073:                     $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  4074:                 } else {
1.274     bisitz   4075:                     $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  4076:                 }
1.274     bisitz   4077:                 $r->print('</p><p>'
                   4078:                          .&mt('Please [_1]go back[_2] and choose a different section name.'
                   4079:                              ,'<a href="javascript:history.go(-1)'
                   4080:                              ,'</a>')
                   4081:                          .'</p><br />'
                   4082:                 );
1.113     raeburn  4083:             }
                   4084: 	}
1.101     albertel 4085:     } # End of foreach (keys(%env))
1.75      www      4086: # Flush the course logs so reverse user roles immediately updated
1.349     raeburn  4087:     $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.225     raeburn  4088:     if (@rolechanges == 0) {
1.372     raeburn  4089:         $r->print('<p>'.&mt('No roles to modify').'</p>');
1.193     raeburn  4090:     }
1.225     raeburn  4091:     return @rolechanges;
1.220     raeburn  4092: }
                   4093: 
1.375     raeburn  4094: sub get_user_credits {
                   4095:     my ($uname,$udom,$defaultcredits,$cdom,$cnum) = @_;
                   4096:     if ($cdom eq '' || $cnum eq '') {
                   4097:         return unless ($env{'request.course.id'});
                   4098:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4099:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4100:     }
                   4101:     my $credits;
                   4102:     my %currhash =
                   4103:         &Apache::lonnet::get('classlist',[$uname.':'.$udom],$cdom,$cnum);
                   4104:     if (keys(%currhash) > 0) {
                   4105:         my @items = split(/:/,$currhash{$uname.':'.$udom});
                   4106:         my $crdidx = &Apache::loncoursedata::CL_CREDITS() - 3;
                   4107:         $credits = $items[$crdidx];
                   4108:         $credits =~ s/[^\d\.]//g;
                   4109:     }
                   4110:     if ($credits eq $defaultcredits) {
                   4111:         undef($credits);
                   4112:     }
                   4113:     return $credits;
                   4114: }
                   4115: 
1.220     raeburn  4116: sub enroll_single_student {
1.375     raeburn  4117:     my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype,
                   4118:         $showcredits,$defaultcredits) = @_;
1.318     raeburn  4119:     $r->print('<h3>');
                   4120:     if ($crstype eq 'Community') {
                   4121:         $r->print(&mt('Enrolling Member'));
                   4122:     } else {
                   4123:         $r->print(&mt('Enrolling Student'));
                   4124:     }
                   4125:     $r->print('</h3>');
1.220     raeburn  4126: 
                   4127:     # Remove non alphanumeric values from section
                   4128:     $env{'form.sections'}=~s/\W//g;
                   4129: 
1.375     raeburn  4130:     my $credits;
                   4131:     if (($showcredits) && ($env{'form.credits'} ne '')) {
                   4132:         $credits = $env{'form.credits'};
                   4133:         $credits =~ s/[^\d\.]//g;
                   4134:         if ($credits ne '') {
                   4135:             if ($credits eq $defaultcredits) {
                   4136:                 undef($credits);
                   4137:             }
                   4138:         }
                   4139:     }
                   4140: 
1.220     raeburn  4141:     # Clean out any old student roles the user has in this class.
                   4142:     &Apache::lonuserutils::modifystudent($env{'form.ccdomain'},
                   4143:          $env{'form.ccuname'},$env{'request.course.id'},undef,$uhome);
                   4144:     my ($startdate,$enddate) = &Apache::lonuserutils::get_dates_from_form();
                   4145:     my $enroll_result =
                   4146:         &Apache::lonnet::modify_student_enrollment($env{'form.ccdomain'},
                   4147:             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
                   4148:             $env{'form.cmiddlename'},$env{'form.clastname'},
                   4149:             $env{'form.generation'},$env{'form.sections'},$enddate,
1.375     raeburn  4150:             $startdate,'manual',undef,$env{'request.course.id'},'',$context,
                   4151:             $credits);
1.220     raeburn  4152:     if ($enroll_result =~ /^ok/) {
1.381     bisitz   4153:         $r->print(&mt('[_1] enrolled','<b>'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.'</b>'));
1.220     raeburn  4154:         if ($env{'form.sections'} ne '') {
                   4155:             $r->print(' '.&mt('in section [_1]',$env{'form.sections'}));
                   4156:         }
                   4157:         my ($showstart,$showend);
                   4158:         if ($startdate <= $now) {
                   4159:             $showstart = &mt('Access starts immediately');
                   4160:         } else {
                   4161:             $showstart = &mt('Access starts: ').&Apache::lonlocal::locallocaltime($startdate);
                   4162:         }
                   4163:         if ($enddate == 0) {
                   4164:             $showend = &mt('ends: no ending date');
                   4165:         } else {
                   4166:             $showend = &mt('ends: ').&Apache::lonlocal::locallocaltime($enddate);
                   4167:         }
                   4168:         $r->print('.<br />'.$showstart.'; '.$showend);
                   4169:         if ($startdate <= $now && !$newuser) {
1.386     bisitz   4170:             $r->print('<p class="LC_info">');
1.318     raeburn  4171:             if ($crstype eq 'Community') {
1.392     raeburn  4172:                 $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  4173:             } else {
1.392     raeburn  4174:                 $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  4175:            }
                   4176:            $r->print('</p>');
1.220     raeburn  4177:         }
                   4178:     } else {
                   4179:         $r->print(&mt('unable to enroll').": ".$enroll_result);
                   4180:     }
                   4181:     return;
1.188     raeburn  4182: }
                   4183: 
1.204     raeburn  4184: sub get_defaultquota_text {
                   4185:     my ($settingstatus) = @_;
                   4186:     my $defquotatext; 
                   4187:     if ($settingstatus eq '') {
1.383     raeburn  4188:         $defquotatext = &mt('default');
1.204     raeburn  4189:     } else {
                   4190:         my ($usertypes,$order) =
                   4191:             &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
                   4192:         if ($usertypes->{$settingstatus} eq '') {
1.383     raeburn  4193:             $defquotatext = &mt('default');
1.204     raeburn  4194:         } else {
1.383     raeburn  4195:             $defquotatext = &mt('default for [_1]',$usertypes->{$settingstatus});
1.204     raeburn  4196:         }
                   4197:     }
                   4198:     return $defquotatext;
                   4199: }
                   4200: 
1.188     raeburn  4201: sub update_result_form {
                   4202:     my ($uhome) = @_;
                   4203:     my $outcome = 
1.367     golterma 4204:     '<form name="userupdate" method="post" action="">'."\n";
1.160     raeburn  4205:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
1.188     raeburn  4206:         $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  4207:     }
1.207     raeburn  4208:     if ($env{'form.origname'} ne '') {
                   4209:         $outcome .= '<input type="hidden" name="origname" value="'.$env{'form.origname'}.'" />'."\n";
                   4210:     }
1.160     raeburn  4211:     foreach my $item ('sortby','seluname','seludom') {
                   4212:         if (exists($env{'form.'.$item})) {
1.188     raeburn  4213:             $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  4214:         }
                   4215:     }
1.188     raeburn  4216:     if ($uhome eq 'no_host') {
                   4217:         $outcome .= '<input type="hidden" name="forcenewuser" value="1" />'."\n";
                   4218:     }
                   4219:     $outcome .= '<input type="hidden" name="phase" value="" />'."\n".
1.383     raeburn  4220:                 '<input type="hidden" name="currstate" value="" />'."\n".
                   4221:                 '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1.188     raeburn  4222:                 '</form>';
                   4223:     return $outcome;
1.4       www      4224: }
                   4225: 
1.149     raeburn  4226: sub quota_admin {
1.378     raeburn  4227:     my ($setquota,$changeHash,$name) = @_;
1.149     raeburn  4228:     my $quotachanged;
                   4229:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   4230:         # Current user has quota modification privileges
1.267     raeburn  4231:         if (ref($changeHash) eq 'HASH') {
                   4232:             $quotachanged = 1;
1.378     raeburn  4233:             $changeHash->{$name.'quota'} = $setquota;
1.267     raeburn  4234:         }
1.149     raeburn  4235:     }
                   4236:     return $quotachanged;
                   4237: }
                   4238: 
1.267     raeburn  4239: sub tool_admin {
1.275     raeburn  4240:     my ($tool,$settool,$changeHash,$context) = @_;
                   4241:     my $canchange = 0; 
1.279     raeburn  4242:     if ($context eq 'requestcourses') {
1.275     raeburn  4243:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
                   4244:             $canchange = 1;
                   4245:         }
1.300     raeburn  4246:     } elsif ($context eq 'reqcrsotherdom') {
                   4247:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   4248:             $canchange = 1;
                   4249:         }
1.362     raeburn  4250:     } elsif ($context eq 'requestauthor') {
                   4251:         if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
                   4252:             $canchange = 1;
                   4253:         }
1.275     raeburn  4254:     } elsif (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   4255:         # Current user has quota modification privileges
                   4256:         $canchange = 1;
                   4257:     }
1.267     raeburn  4258:     my $toolchanged;
1.275     raeburn  4259:     if ($canchange) {
1.267     raeburn  4260:         if (ref($changeHash) eq 'HASH') {
                   4261:             $toolchanged = 1;
1.362     raeburn  4262:             if ($tool eq 'requestauthor') {
                   4263:                 $changeHash->{$context} = $settool;
                   4264:             } else {
                   4265:                 $changeHash->{$context.'.'.$tool} = $settool;
                   4266:             }
1.267     raeburn  4267:         }
                   4268:     }
                   4269:     return $toolchanged;
                   4270: }
                   4271: 
1.88      raeburn  4272: sub build_roles {
1.89      raeburn  4273:     my ($sectionstr,$sections,$role) = @_;
1.88      raeburn  4274:     my $num_sections = 0;
                   4275:     if ($sectionstr=~ /,/) {
                   4276:         my @secnums = split/,/,$sectionstr;
1.89      raeburn  4277:         if ($role eq 'st') {
                   4278:             $secnums[0] =~ s/\W//g;
                   4279:             $$sections{$secnums[0]} = 1;
                   4280:             $num_sections = 1;
                   4281:         } else {
                   4282:             foreach my $sec (@secnums) {
                   4283:                 $sec =~ ~s/\W//g;
1.150     banghart 4284:                 if (!($sec eq "")) {
1.89      raeburn  4285:                     if (exists($$sections{$sec})) {
                   4286:                         $$sections{$sec} ++;
                   4287:                     } else {
                   4288:                         $$sections{$sec} = 1;
                   4289:                         $num_sections ++;
                   4290:                     }
1.88      raeburn  4291:                 }
                   4292:             }
                   4293:         }
                   4294:     } else {
                   4295:         $sectionstr=~s/\W//g;
                   4296:         unless ($sectionstr eq '') {
                   4297:             $$sections{$sectionstr} = 1;
                   4298:             $num_sections ++;
                   4299:         }
                   4300:     }
1.129     albertel 4301: 
1.88      raeburn  4302:     return $num_sections;
                   4303: }
                   4304: 
1.58      www      4305: # ========================================================== Custom Role Editor
                   4306: 
                   4307: sub custom_role_editor {
1.414     raeburn  4308:     my ($r,$brcrum,$prefix) = @_;
1.324     raeburn  4309:     my $action = $env{'form.customroleaction'};
                   4310:     my $rolename; 
                   4311:     if ($action eq 'new') {
                   4312:         $rolename=$env{'form.newrolename'};
                   4313:     } else {
                   4314:         $rolename=$env{'form.rolename'};
1.59      www      4315:     }
                   4316: 
1.324     raeburn  4317:     my ($crstype,$context);
                   4318:     if ($env{'request.course.id'}) {
                   4319:         $crstype = &Apache::loncommon::course_type();
                   4320:         $context = 'course';
                   4321:     } else {
                   4322:         $context = 'domain';
1.414     raeburn  4323:         $crstype = 'course';
1.324     raeburn  4324:     }
1.351     raeburn  4325: 
                   4326:     $rolename=~s/[^A-Za-z0-9]//gs;
                   4327:     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
                   4328: 	&print_username_entry_form($r,undef,undef,undef,undef,$crstype,$brcrum);
                   4329:         return;
                   4330:     }
                   4331: 
1.414     raeburn  4332:     my $formname = 'form1';
                   4333:     my %privs=();
                   4334:     my $body_top = '<h2>';
                   4335: # ------------------------------------------------------- Does this role exist?
1.59      www      4336:     my ($rdummy,$roledef)=
                   4337: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   4338:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.414     raeburn  4339:         $body_top .= &mt('Existing Role').' "';
1.61      www      4340: # ------------------------------------------------- Get current role privileges
1.414     raeburn  4341:         ($privs{'system'},$privs{'domain'},$privs{'course'})=split(/\_/,$roledef);
                   4342:         if ($privs{'system'} =~ /bre\&S/) {
                   4343:             if ($context eq 'domain') {
1.417     raeburn  4344:                 $crstype = 'Course';
1.414     raeburn  4345:             } elsif ($crstype eq 'Community') {
                   4346:                 $privs{'system'} =~ s/bre\&S//;
                   4347:             }
                   4348:         } elsif ($context eq 'domain') {
                   4349:             $crstype = 'Course';
1.324     raeburn  4350:         }
1.59      www      4351:     } else {
1.414     raeburn  4352:         $body_top .= &mt('New Role').' "';
                   4353:         $roledef='';
1.59      www      4354:     }
1.153     banghart 4355:     $body_top .= $rolename.'"</h2>';
1.414     raeburn  4356: 
                   4357: # ------------------------------------------------------- What can be assigned?
                   4358:     my %full=();
1.417     raeburn  4359:     my %levels=(
1.414     raeburn  4360:                  course => {},
                   4361:                  domain => {},
                   4362:                  system => {},
                   4363:                );
                   4364:     my %levelscurrent=(
                   4365:                         course => {},
                   4366:                         domain => {},
                   4367:                         system => {},
                   4368:                       );
                   4369:     &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
1.160     raeburn  4370:     my ($jsback,$elements) = &crumb_utilities();
1.414     raeburn  4371:     my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
1.417     raeburn  4372:     my $head_script =
1.414     raeburn  4373:         &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,
                   4374:                                                   \%full,\@templateroles,$jsback);
1.351     raeburn  4375:     push (@{$brcrum},
1.414     raeburn  4376:               {href => "javascript:backPage(document.$formname,'pickrole','')",
1.351     raeburn  4377:                text => "Pick custom role",
                   4378:                faq  => 282,bug=>'Instructor Interface',},
1.414     raeburn  4379:               {href => "javascript:backPage(document.$formname,'','')",
1.351     raeburn  4380:                text => "Edit custom role",
                   4381:                faq  => 282,
                   4382:                bug  => 'Instructor Interface',
                   4383:                help => 'Course_Editing_Custom_Roles'}
                   4384:               );
                   4385:     my $args = { bread_crumbs          => $brcrum,
                   4386:                  bread_crumbs_component => 'User Management'};
                   4387:  
                   4388:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',
                   4389:                                              $head_script,$args).
                   4390:               $body_top);
1.414     raeburn  4391:     $r->print('<form name="'.$formname.'" method="post" action="">'."\n".
                   4392:               &Apache::lonuserutils::custom_role_header($context,$crstype,
                   4393:                                                         \@templateroles,$prefix));
1.264     bisitz   4394: 
1.61      www      4395:     $r->print(<<ENDCCF);
                   4396: <input type="hidden" name="phase" value="set_custom_roles" />
                   4397: <input type="hidden" name="rolename" value="$rolename" />
                   4398: ENDCCF
1.414     raeburn  4399:     $r->print(&Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
                   4400:                                                        \%levelscurrent,$prefix));
1.135     raeburn  4401:     $r->print(&Apache::loncommon::end_data_table().
1.190     raeburn  4402:    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
1.160     raeburn  4403:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
1.417     raeburn  4404:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".
1.160     raeburn  4405:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
1.351     raeburn  4406:    '<input type="submit" value="'.&mt('Save').'" /></form>');
1.61      www      4407: }
1.414     raeburn  4408: 
1.61      www      4409: # ---------------------------------------------------------- Call to definerole
                   4410: sub set_custom_role {
1.414     raeburn  4411:     my ($r,$context,$brcrum,$prefix) = @_;
1.101     albertel 4412:     my $rolename=$env{'form.rolename'};
1.63      www      4413:     $rolename=~s/[^A-Za-z0-9]//gs;
1.150     banghart 4414:     if (!$rolename) {
1.414     raeburn  4415: 	&custom_role_editor($r,$brcrum,$prefix);
1.61      www      4416:         return;
                   4417:     }
1.160     raeburn  4418:     my ($jsback,$elements) = &crumb_utilities();
1.301     bisitz   4419:     my $jscript = '<script type="text/javascript">'
                   4420:                  .'// <![CDATA['."\n"
                   4421:                  .$jsback."\n"
                   4422:                  .'// ]]>'."\n"
                   4423:                  .'</script>'."\n";
1.352     raeburn  4424:     push(@{$brcrum},
                   4425:         {href => "javascript:backPage(document.customresult,'pickrole','')",
                   4426:          text => "Pick custom role",
                   4427:          faq  => 282,
                   4428:          bug  => 'Instructor Interface',},
                   4429:         {href => "javascript:backPage(document.customresult,'selected_custom_edit','')",
                   4430:          text => "Edit custom role",
                   4431:          faq  => 282,
                   4432:          bug  => 'Instructor Interface',},
                   4433:         {href => "javascript:backPage(document.customresult,'set_custom_roles','')",
                   4434:          text => "Result",
                   4435:          faq  => 282,
                   4436:          bug  => 'Instructor Interface',
                   4437:          help => 'Course_Editing_Custom_Roles'},
                   4438:         );
                   4439:     my $args = { bread_crumbs           => $brcrum,
1.414     raeburn  4440:                  bread_crumbs_component => 'User Management'};
1.351     raeburn  4441:     $r->print(&Apache::loncommon::start_page('Save Custom Role',$jscript,$args));
1.160     raeburn  4442: 
1.393     raeburn  4443:     my $newrole;
1.61      www      4444:     my ($rdummy,$roledef)=
1.110     albertel 4445: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   4446: 
1.61      www      4447: # ------------------------------------------------------- Does this role exist?
1.188     raeburn  4448:     $r->print('<h3>');
1.61      www      4449:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 4450: 	$r->print(&mt('Existing Role').' "');
1.61      www      4451:     } else {
1.73      sakharuk 4452: 	$r->print(&mt('New Role').' "');
1.61      www      4453: 	$roledef='';
1.393     raeburn  4454:         $newrole = 1;
1.61      www      4455:     }
1.188     raeburn  4456:     $r->print($rolename.'"</h3>');
1.414     raeburn  4457: # ------------------------------------------------- Assign role and show result
1.61      www      4458: 
1.387     bisitz   4459:     my $errmsg;
1.414     raeburn  4460:     my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$prefix);
                   4461:     # Assign role and return result
                   4462:     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
                   4463:                                              $newprivs{'c'});
1.387     bisitz   4464:     if ($result ne 'ok') {
                   4465:         $errmsg = ': '.$result;
                   4466:     }
                   4467:     my $message =
                   4468:         &Apache::lonhtmlcommon::confirm_success(
                   4469:             &mt('Defining Role').$errmsg, ($result eq 'ok' ? 0 : 1));
1.101     albertel 4470:     if ($env{'request.course.id'}) {
                   4471:         my $url='/'.$env{'request.course.id'};
1.63      www      4472:         $url=~s/\_/\//g;
1.387     bisitz   4473:         $result =
                   4474:             &Apache::lonnet::assigncustomrole(
                   4475:                 $env{'user.domain'},$env{'user.name'},
                   4476:                 $url,
                   4477:                 $env{'user.domain'},$env{'user.name'},
                   4478:                 $rolename,undef,undef,undef,$context);
                   4479:         if ($result ne 'ok') {
                   4480:             $errmsg = ': '.$result;
                   4481:         }
                   4482:         $message .=
                   4483:             '<br />'
                   4484:            .&Apache::lonhtmlcommon::confirm_success(
                   4485:                 &mt('Assigning Role to Self').$errmsg, ($result eq 'ok' ? 0 : 1));
1.63      www      4486:     }
1.380     bisitz   4487:     $r->print(
1.387     bisitz   4488:         &Apache::loncommon::confirmwrapper($message)
                   4489:        .'<br />'
                   4490:        .&Apache::lonhtmlcommon::actionbox([
                   4491:             '<a href="javascript:backPage(document.customresult,'."'pickrole'".')">'
                   4492:            .&mt('Create or edit another custom role')
                   4493:            .'</a>'])
1.380     bisitz   4494:        .'<form name="customresult" method="post" action="">'
1.387     bisitz   4495:        .&Apache::lonhtmlcommon::echo_form_input([])
                   4496:        .'</form>'
1.380     bisitz   4497:     );
1.58      www      4498: }
                   4499: 
1.2       www      4500: # ================================================================ Main Handler
                   4501: sub handler {
                   4502:     my $r = shift;
                   4503:     if ($r->header_only) {
1.68      www      4504:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      4505:        $r->send_http_header;
                   4506:        return OK;
                   4507:     }
1.318     raeburn  4508:     my ($context,$crstype);
1.190     raeburn  4509:     if ($env{'request.course.id'}) {
                   4510:         $context = 'course';
1.318     raeburn  4511:         $crstype = &Apache::loncommon::course_type();
1.190     raeburn  4512:     } elsif ($env{'request.role'} =~ /^au\./) {
1.206     raeburn  4513:         $context = 'author';
1.190     raeburn  4514:     } else {
                   4515:         $context = 'domain';
                   4516:     }
1.375     raeburn  4517: 
1.190     raeburn  4518:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.233     raeburn  4519:         ['action','state','callingform','roletype','showrole','bulkaction','popup','phase',
1.391     raeburn  4520:          'username','domain','srchterm','srchdomain','srchin','srchby','srchtype','queue']);
1.190     raeburn  4521:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.351     raeburn  4522:     my $args;
                   4523:     my $brcrum = [];
                   4524:     my $bread_crumbs_component = 'User Management';
1.391     raeburn  4525:     if (($env{'form.action'} ne 'dateselect') && ($env{'form.action'} ne 'displayuserreq')) {
1.351     raeburn  4526:         $brcrum = [{href=>"/adm/createuser",
                   4527:                     text=>"User Management",
                   4528:                     help=>'Course_Create_Class_List,Course_Change_Privileges,Course_View_Class_List,Course_Editing_Custom_Roles,Course_Add_Student,Course_Drop_Student,Course_Automated_Enrollment,Course_Self_Enrollment,Course_Manage_Group'}
                   4529:                   ];
1.202     raeburn  4530:     }
1.289     droeschl 4531:     #SD Following files not added to help, because the corresponding .tex-files seem to
                   4532:     #be missing: Course_Approve_Selfenroll,Course_User_Logs,
1.209     raeburn  4533:     my ($permission,$allowed) = 
1.318     raeburn  4534:         &Apache::lonuserutils::get_permission($context,$crstype);
1.190     raeburn  4535:     if (!$allowed) {
1.358     raeburn  4536:         if ($context eq 'course') {
                   4537:             $r->internal_redirect('/adm/viewclasslist');
                   4538:             return OK;
                   4539:         }
1.190     raeburn  4540:         $env{'user.error.msg'}=
                   4541:             "/adm/createuser:cst:0:0:Cannot create/modify user data ".
                   4542:                                  "or view user status.";
                   4543:         return HTTP_NOT_ACCEPTABLE;
                   4544:     }
                   4545: 
                   4546:     &Apache::loncommon::content_type($r,'text/html');
                   4547:     $r->send_http_header;
                   4548: 
1.375     raeburn  4549:     my $showcredits;
                   4550:     if ((($context eq 'course') && ($crstype eq 'Course')) || 
                   4551:          ($context eq 'domain')) {
                   4552:         my %domdefaults = 
                   4553:             &Apache::lonnet::get_domain_defaults($env{'request.role.domain'});
                   4554:         if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'}) {
                   4555:             $showcredits = 1;
                   4556:         }
                   4557:     }
                   4558: 
1.190     raeburn  4559:     # Main switch on form.action and form.state, as appropriate
                   4560:     if (! exists($env{'form.action'})) {
1.351     raeburn  4561:         $args = {bread_crumbs => $brcrum,
                   4562:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4563:         $r->print(&header(undef,$args));
1.318     raeburn  4564:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4565:     } elsif ($env{'form.action'} eq 'upload' && $permission->{'cusr'}) {
1.351     raeburn  4566:         push(@{$brcrum},
                   4567:               { href => '/adm/createuser?action=upload&state=',
                   4568:                 text => 'Upload Users List',
                   4569:                 help => 'Course_Create_Class_List',
                   4570:               });
                   4571:         $bread_crumbs_component = 'Upload Users List';
                   4572:         $args = {bread_crumbs           => $brcrum,
                   4573:                  bread_crumbs_component => $bread_crumbs_component};
                   4574:         $r->print(&header(undef,$args));
1.190     raeburn  4575:         $r->print('<form name="studentform" method="post" '.
                   4576:                   'enctype="multipart/form-data" '.
                   4577:                   ' action="/adm/createuser">'."\n");
                   4578:         if (! exists($env{'form.state'})) {
                   4579:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4580:         } elsif ($env{'form.state'} eq 'got_file') {
1.375     raeburn  4581:             &Apache::lonuserutils::print_upload_manager_form($r,$context,$permission,
                   4582:                                                              $crstype,$showcredits);
1.190     raeburn  4583:         } elsif ($env{'form.state'} eq 'enrolling') {
                   4584:             if ($env{'form.datatoken'}) {
1.375     raeburn  4585:                 &Apache::lonuserutils::upfile_drop_add($r,$context,$permission,
                   4586:                                                        $showcredits);
1.190     raeburn  4587:             }
                   4588:         } else {
                   4589:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4590:         }
1.416     raeburn  4591:     } elsif (((($env{'form.action'} eq 'singleuser') || ($env{'form.action'}
                   4592:               eq 'singlestudent')) && ($permission->{'cusr'})) ||
1.418     raeburn  4593:              (($env{'form.action'} eq 'singleuser') && ($permission->{'view'})) ||
1.416     raeburn  4594:              (($env{'form.action'} eq 'accesslogs') && ($permission->{'activity'}))) {
1.190     raeburn  4595:         my $phase = $env{'form.phase'};
                   4596:         my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
1.192     albertel 4597: 	&Apache::loncreateuser::restore_prev_selections();
                   4598: 	my $srch;
                   4599: 	foreach my $item (@search) {
                   4600: 	    $srch->{$item} = $env{'form.'.$item};
                   4601: 	}
1.207     raeburn  4602:         if (($phase eq 'get_user_info') || ($phase eq 'userpicked') ||
1.416     raeburn  4603:             ($phase eq 'createnewuser') || ($phase eq 'activity')) {
1.207     raeburn  4604:             if ($env{'form.phase'} eq 'createnewuser') {
                   4605:                 my $response;
                   4606:                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
1.366     bisitz   4607:                     my $response =
                   4608:                         '<span class="LC_warning">'
                   4609:                        .&mt('You must specify a valid username. Only the following are allowed:'
                   4610:                            .' letters numbers - . @')
                   4611:                        .'</span>';
1.221     raeburn  4612:                     $env{'form.phase'} = '';
1.375     raeburn  4613:                     &print_username_entry_form($r,$context,$response,$srch,undef,
                   4614:                                                $crstype,$brcrum,$showcredits);
1.207     raeburn  4615:                 } else {
                   4616:                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
                   4617:                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
                   4618:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4619:                                                   $srch,$response,$context,
1.375     raeburn  4620:                                                   $permission,$crstype,$brcrum,
                   4621:                                                   $showcredits);
1.207     raeburn  4622:                 }
                   4623:             } elsif ($env{'form.phase'} eq 'get_user_info') {
1.190     raeburn  4624:                 my ($currstate,$response,$forcenewuser,$results) = 
1.221     raeburn  4625:                     &user_search_result($context,$srch);
1.190     raeburn  4626:                 if ($env{'form.currstate'} eq 'modify') {
                   4627:                     $currstate = $env{'form.currstate'};
                   4628:                 }
                   4629:                 if ($currstate eq 'select') {
                   4630:                     &print_user_selection_page($r,$response,$srch,$results,
1.351     raeburn  4631:                                                \@search,$context,undef,$crstype,
                   4632:                                                $brcrum);
1.416     raeburn  4633:                 } elsif (($currstate eq 'modify') || ($env{'form.action'} eq 'accesslogs')) {
                   4634:                     my ($ccuname,$ccdomain,$uhome);
1.190     raeburn  4635:                     if (($srch->{'srchby'} eq 'uname') && 
                   4636:                         ($srch->{'srchtype'} eq 'exact')) {
                   4637:                         $ccuname = $srch->{'srchterm'};
                   4638:                         $ccdomain= $srch->{'srchdomain'};
                   4639:                     } else {
                   4640:                         my @matchedunames = keys(%{$results});
                   4641:                         ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
                   4642:                     }
                   4643:                     $ccuname =&LONCAPA::clean_username($ccuname);
                   4644:                     $ccdomain=&LONCAPA::clean_domain($ccdomain);
1.416     raeburn  4645:                     if ($env{'form.action'} eq 'accesslogs') {
                   4646:                         my $uhome;
                   4647:                         if (($ccuname ne '') && ($ccdomain ne '')) {
                   4648:                            $uhome = &Apache::lonnet::homeserver($ccuname,$ccdomain);
                   4649:                         }
                   4650:                         if (($uhome eq '') || ($uhome eq 'no_host')) {
                   4651:                             $env{'form.phase'} = '';
                   4652:                             undef($forcenewuser);
                   4653:                             #if ($response) {
                   4654:                             #    unless ($response =~ m{\Q<br /><br />\E$}) {
                   4655:                             #        $response .= '<br /><br />';
                   4656:                             #    }
                   4657:                             #}
                   4658:                             &print_username_entry_form($r,$context,$response,$srch,
                   4659:                                                        $forcenewuser,$crstype,$brcrum);
                   4660:                         } else {
                   4661:                             &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
                   4662:                         }
                   4663:                     } else {
                   4664:                         if ($env{'form.forcenewuser'}) {
                   4665:                             $response = '';
                   4666:                         }
                   4667:                         &print_user_modification_page($r,$ccuname,$ccdomain,
                   4668:                                                       $srch,$response,$context,
                   4669:                                                       $permission,$crstype,$brcrum);
1.190     raeburn  4670:                     }
                   4671:                 } elsif ($currstate eq 'query') {
1.351     raeburn  4672:                     &print_user_query_page($r,'createuser',$brcrum);
1.190     raeburn  4673:                 } else {
1.229     raeburn  4674:                     $env{'form.phase'} = '';
1.207     raeburn  4675:                     &print_username_entry_form($r,$context,$response,$srch,
1.351     raeburn  4676:                                                $forcenewuser,$crstype,$brcrum);
1.190     raeburn  4677:                 }
                   4678:             } elsif ($env{'form.phase'} eq 'userpicked') {
                   4679:                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
                   4680:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
1.416     raeburn  4681:                 if ($env{'form.action'} eq 'accesslogs') {
                   4682:                     &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
                   4683:                 } else {
                   4684:                     &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
                   4685:                                                   $context,$permission,$crstype,
                   4686:                                                   $brcrum);
                   4687:                 }
                   4688:             } elsif ($env{'form.action'} eq 'accesslogs') {
                   4689:                 my $ccuname = &LONCAPA::clean_username($env{'form.accessuname'});
                   4690:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.accessudom'});
                   4691:                 &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
1.190     raeburn  4692:             }
                   4693:         } elsif ($env{'form.phase'} eq 'update_user_data') {
1.375     raeburn  4694:             &update_user_data($r,$context,$crstype,$brcrum,$showcredits);
1.190     raeburn  4695:         } else {
1.351     raeburn  4696:             &print_username_entry_form($r,$context,undef,$srch,undef,$crstype,
                   4697:                                        $brcrum);
1.190     raeburn  4698:         }
                   4699:     } elsif ($env{'form.action'} eq 'custom' && $permission->{'custom'}) {
1.414     raeburn  4700:         my $prefix;
1.190     raeburn  4701:         if ($env{'form.phase'} eq 'set_custom_roles') {
1.414     raeburn  4702:             &set_custom_role($r,$context,$brcrum,$prefix);
1.190     raeburn  4703:         } else {
1.414     raeburn  4704:             &custom_role_editor($r,$brcrum,$prefix);
1.190     raeburn  4705:         }
1.362     raeburn  4706:     } elsif (($env{'form.action'} eq 'processauthorreq') &&
                   4707:              ($permission->{'cusr'}) && 
                   4708:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   4709:         push(@{$brcrum},
                   4710:                  {href => '/adm/createuser?action=processauthorreq',
1.385     bisitz   4711:                   text => 'Authoring Space requests',
1.362     raeburn  4712:                   help => 'Domain_Role_Approvals'});
                   4713:         $bread_crumbs_component = 'Authoring requests';
                   4714:         if ($env{'form.state'} eq 'done') {
                   4715:             push(@{$brcrum},
                   4716:                      {href => '/adm/createuser?action=authorreqqueue',
                   4717:                       text => 'Result',
                   4718:                       help => 'Domain_Role_Approvals'});
                   4719:             $bread_crumbs_component = 'Authoring request result';
                   4720:         }
                   4721:         $args = { bread_crumbs           => $brcrum,
                   4722:                   bread_crumbs_component => $bread_crumbs_component};
1.391     raeburn  4723:         my $js = &usernamerequest_javascript();
                   4724:         $r->print(&header(&add_script($js),$args));
1.362     raeburn  4725:         if (!exists($env{'form.state'})) {
                   4726:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestauthor',
                   4727:                                                                             $env{'request.role.domain'}));
                   4728:         } elsif ($env{'form.state'} eq 'done') {
                   4729:             $r->print('<h3>'.&mt('Authoring request processing').'</h3>'."\n");
                   4730:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestauthor',
                   4731:                                                                          $env{'request.role.domain'}));
                   4732:         }
1.391     raeburn  4733:     } elsif (($env{'form.action'} eq 'processusernamereq') &&
                   4734:              ($permission->{'cusr'}) &&
                   4735:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   4736:         push(@{$brcrum},
                   4737:                  {href => '/adm/createuser?action=processusernamereq',
                   4738:                   text => 'LON-CAPA account requests',
                   4739:                   help => 'Domain_Username_Approvals'});
                   4740:         $bread_crumbs_component = 'Account requests';
                   4741:         if ($env{'form.state'} eq 'done') {
                   4742:             push(@{$brcrum},
                   4743:                      {href => '/adm/createuser?action=usernamereqqueue',
                   4744:                       text => 'Result',
                   4745:                       help => 'Domain_Username_Approvals'});
                   4746:             $bread_crumbs_component = 'LON-CAPA account request result';
                   4747:         }
                   4748:         $args = { bread_crumbs           => $brcrum,
                   4749:                   bread_crumbs_component => $bread_crumbs_component};
                   4750:         my $js = &usernamerequest_javascript();
                   4751:         $r->print(&header(&add_script($js),$args));
                   4752:         if (!exists($env{'form.state'})) {
                   4753:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestusername',
                   4754:                                                                             $env{'request.role.domain'}));
                   4755:         } elsif ($env{'form.state'} eq 'done') {
                   4756:             $r->print('<h3>'.&mt('LON-CAPA account request processing').'</h3>'."\n");
                   4757:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestusername',
                   4758:                                                                          $env{'request.role.domain'}));
                   4759:         }
                   4760:     } elsif (($env{'form.action'} eq 'displayuserreq') &&
                   4761:              ($permission->{'cusr'})) {
                   4762:         my $dom = $env{'form.domain'};
                   4763:         my $uname = $env{'form.username'};
                   4764:         my $warning;
                   4765:         if (($dom =~ /^$match_domain$/) && (&Apache::lonnet::domain($dom) ne '')) {
                   4766:             if (($dom eq $env{'request.role.domain'}) && (&Apache::lonnet::allowed('ccc',$dom))) {
                   4767:                 if (($uname =~ /^$match_username$/) && ($env{'form.queue'} eq 'approval')) {
                   4768:                     my $uhome = &Apache::lonnet::homeserver($uname,$dom);
                   4769:                     if ($uhome eq 'no_host') {
                   4770:                         my $queue = $env{'form.queue'};
                   4771:                         my $reqkey = &escape($uname).'_'.$queue; 
                   4772:                         my $namespace = 'usernamequeue';
                   4773:                         my $domconfig = &Apache::lonnet::get_domainconfiguser($dom);
                   4774:                         my %queued =
                   4775:                             &Apache::lonnet::get($namespace,[$reqkey],$dom,$domconfig);
                   4776:                         unless ($queued{$reqkey}) {
                   4777:                             $warning = &mt('No information was found for this LON-CAPA account request.');
                   4778:                         }
                   4779:                     } else {
                   4780:                         $warning = &mt('A LON-CAPA account already exists for the requested username and domain.');
                   4781:                     }
                   4782:                 } else {
                   4783:                     $warning = &mt('LON-CAPA account request status check is for an invalid username.');
                   4784:                 }
                   4785:             } else {
                   4786:                 $warning = &mt('You do not have rights to view LON-CAPA account requests in the domain specified.');
                   4787:             }
                   4788:         } else {
                   4789:             $warning = &mt('LON-CAPA account request status check is for an invalid domain.');
                   4790:         }
                   4791:         my $args = { only_body => 1 };
                   4792:         $r->print(&header(undef,$args).
                   4793:                   '<h3>'.&mt('LON-CAPA Account Request Details').'</h3>');
                   4794:         if ($warning ne '') {
                   4795:             $r->print('<div class="LC_warning">'.$warning.'</div>');
                   4796:         } else {
                   4797:             my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
                   4798:             my $domconfiguser = &Apache::lonnet::get_domainconfiguser($dom);
                   4799:             my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   4800:             if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   4801:                 if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
                   4802:                     if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}) eq 'HASH') {
                   4803:                         my %info =
                   4804:                             &Apache::lonnet::get('nohist_requestedusernames',[$uname],$dom,$domconfiguser);
                   4805:                         if (ref($info{$uname}) eq 'HASH') {
1.396     raeburn  4806:                             my $usertype = $info{$uname}{'inststatus'};
                   4807:                             unless ($usertype) {
                   4808:                                 $usertype = 'default';
                   4809:                             }
                   4810:                             if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}) eq 'HASH') {
                   4811:                                 if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
                   4812:                                     $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box());
                   4813:                                     my ($num,$count,$showstatus);
                   4814:                                     $count = scalar(keys(%{$domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}}));
                   4815:                                     unless ($usertype eq 'default') {
                   4816:                                         my ($othertitle,$usertypes,$types) = 
                   4817:                                             &Apache::loncommon::sorted_inst_types($dom);
                   4818:                                         if (ref($usertypes) eq 'HASH') {
                   4819:                                             if ($usertypes->{$usertype}) {
                   4820:                                                 $showstatus = $usertypes->{$usertype};
                   4821:                                                 $count ++;
                   4822:                                             }
                   4823:                                         }
                   4824:                                     }
                   4825:                                     foreach my $field (@{$infofields}) {
                   4826:                                         next unless ($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}{$field});
                   4827:                                         next unless ($infotitles->{$field});
                   4828:                                         $r->print(&Apache::lonhtmlcommon::row_title($infotitles->{$field}).
                   4829:                                                   $info{$uname}{$field});
                   4830:                                         $num ++;
                   4831:                                         if ($count == $num) {
                   4832:                                             $r->print(&Apache::lonhtmlcommon::row_closure(1));
                   4833:                                         } else {
                   4834:                                             $r->print(&Apache::lonhtmlcommon::row_closure());
                   4835:                                         }
                   4836:                                     }
                   4837:                                     if ($showstatus) {
                   4838:                                         $r->print(&Apache::lonhtmlcommon::row_title(&mt('Status type (self-reported)')).
                   4839:                                                   $showstatus.
                   4840:                                                   &Apache::lonhtmlcommon::row_closure(1));
1.391     raeburn  4841:                                     }
1.396     raeburn  4842:                                     $r->print(&Apache::lonhtmlcommon::end_pick_box().'</div>');
1.391     raeburn  4843:                                 }
                   4844:                             }
                   4845:                         }
                   4846:                     }
                   4847:                 }
                   4848:             }
                   4849:             $r->print(&close_popup_form());
                   4850:         }
1.207     raeburn  4851:     } elsif (($env{'form.action'} eq 'listusers') && 
                   4852:              ($permission->{'view'} || $permission->{'cusr'})) {
1.202     raeburn  4853:         if ($env{'form.phase'} eq 'bulkchange') {
1.351     raeburn  4854:             push(@{$brcrum},
                   4855:                     {href => '/adm/createuser?action=listusers',
                   4856:                      text => "List Users"},
                   4857:                     {href => "/adm/createuser",
                   4858:                      text => "Result",
                   4859:                      help => 'Course_View_Class_List'});
                   4860:             $bread_crumbs_component = 'Update Users';
                   4861:             $args = {bread_crumbs           => $brcrum,
                   4862:                      bread_crumbs_component => $bread_crumbs_component};
                   4863:             $r->print(&header(undef,$args));
1.202     raeburn  4864:             my $setting = $env{'form.roletype'};
                   4865:             my $choice = $env{'form.bulkaction'};
                   4866:             if ($permission->{'cusr'}) {
1.336     raeburn  4867:                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice,$crstype);
1.221     raeburn  4868:             } else {
                   4869:                 $r->print(&mt('You are not authorized to make bulk changes to user roles'));
1.223     raeburn  4870:                 $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
1.202     raeburn  4871:             }
                   4872:         } else {
1.351     raeburn  4873:             push(@{$brcrum},
                   4874:                     {href => '/adm/createuser?action=listusers',
                   4875:                      text => "List Users",
                   4876:                      help => 'Course_View_Class_List'});
                   4877:             $bread_crumbs_component = 'List Users';
                   4878:             $args = {bread_crumbs           => $brcrum,
                   4879:                      bread_crumbs_component => $bread_crumbs_component};
1.202     raeburn  4880:             my ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles);
                   4881:             my $formname = 'studentform';
1.364     raeburn  4882:             my $hidecall = "hide_searching();";
1.321     raeburn  4883:             if (($context eq 'domain') && (($env{'form.roletype'} eq 'course') ||
                   4884:                 ($env{'form.roletype'} eq 'community'))) {
                   4885:                 if ($env{'form.roletype'} eq 'course') {
                   4886:                     ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles) = 
                   4887:                         &Apache::lonuserutils::courses_selector($env{'request.role.domain'},
                   4888:                                                                 $formname);
                   4889:                 } elsif ($env{'form.roletype'} eq 'community') {
                   4890:                     $cb_jscript = 
                   4891:                         &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
                   4892:                     my %elements = (
                   4893:                                       coursepick => 'radio',
                   4894:                                       coursetotal => 'text',
                   4895:                                       courselist => 'text',
                   4896:                                    );
                   4897:                     $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   4898:                 }
1.364     raeburn  4899:                 $jscript .= &verify_user_display($context)."\n".
                   4900:                             &Apache::loncommon::check_uncheck_jscript();
1.202     raeburn  4901:                 my $js = &add_script($jscript).$cb_jscript;
                   4902:                 my $loadcode = 
                   4903:                     &Apache::lonuserutils::course_selector_loadcode($formname);
                   4904:                 if ($loadcode ne '') {
1.364     raeburn  4905:                     $args->{add_entries} = {onload => "$loadcode;$hidecall"};
                   4906:                 } else {
                   4907:                     $args->{add_entries} = {onload => $hidecall};
1.202     raeburn  4908:                 }
1.351     raeburn  4909:                 $r->print(&header($js,$args));
1.191     raeburn  4910:             } else {
1.364     raeburn  4911:                 $args->{add_entries} = {onload => $hidecall};
                   4912:                 $jscript = &verify_user_display($context).
                   4913:                            &Apache::loncommon::check_uncheck_jscript(); 
                   4914:                 $r->print(&header(&add_script($jscript),$args));
1.191     raeburn  4915:             }
1.202     raeburn  4916:             &Apache::lonuserutils::print_userlist($r,undef,$permission,$context,
1.375     raeburn  4917:                          $formname,$totcodes,$codetitles,$idlist,$idlist_titles,
                   4918:                          $showcredits);
1.191     raeburn  4919:         }
1.213     raeburn  4920:     } elsif ($env{'form.action'} eq 'drop' && $permission->{'cusr'}) {
1.318     raeburn  4921:         my $brtext;
                   4922:         if ($crstype eq 'Community') {
                   4923:             $brtext = 'Drop Members';
                   4924:         } else {
                   4925:             $brtext = 'Drop Students';
                   4926:         }
1.351     raeburn  4927:         push(@{$brcrum},
                   4928:                 {href => '/adm/createuser?action=drop',
                   4929:                  text => $brtext,
                   4930:                  help => 'Course_Drop_Student'});
                   4931:         if ($env{'form.state'} eq 'done') {
                   4932:             push(@{$brcrum},
                   4933:                      {href=>'/adm/createuser?action=drop',
                   4934:                       text=>"Result"});
                   4935:         }
                   4936:         $bread_crumbs_component = $brtext;
                   4937:         $args = {bread_crumbs           => $brcrum,
                   4938:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4939:         $r->print(&header(undef,$args));
1.213     raeburn  4940:         if (!exists($env{'form.state'})) {
1.318     raeburn  4941:             &Apache::lonuserutils::print_drop_menu($r,$context,$permission,$crstype);
1.213     raeburn  4942:         } elsif ($env{'form.state'} eq 'done') {
                   4943:             &Apache::lonuserutils::update_user_list($r,$context,undef,
                   4944:                                                     $env{'form.action'});
                   4945:         }
1.202     raeburn  4946:     } elsif ($env{'form.action'} eq 'dateselect') {
                   4947:         if ($permission->{'cusr'}) {
1.351     raeburn  4948:             $r->print(&header(undef,{'no_nav_bar' => 1}).
1.375     raeburn  4949:                       &Apache::lonuserutils::date_section_selector($context,$permission,
                   4950:                                                                    $crstype,$showcredits));
1.202     raeburn  4951:         } else {
1.351     raeburn  4952:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   4953:                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>'); 
1.202     raeburn  4954:         }
1.237     raeburn  4955:     } elsif ($env{'form.action'} eq 'selfenroll') {
1.398     raeburn  4956:         if ($permission->{selfenrolladmin}) {
                   4957:             my $cid = $env{'request.course.id'};
                   4958:             my $cdom = $env{'course.'.$cid.'.domain'};
                   4959:             my $cnum = $env{'course.'.$cid.'.num'};
                   4960:             my %currsettings = (
                   4961:                 selfenroll_types              => $env{'course.'.$cid.'.internal.selfenroll_types'},
                   4962:                 selfenroll_registered         => $env{'course.'.$cid.'.internal.selfenroll_registered'},
                   4963:                 selfenroll_section            => $env{'course.'.$cid.'.internal.selfenroll_section'},
                   4964:                 selfenroll_notifylist         => $env{'course.'.$cid.'.internal.selfenroll_notifylist'},
                   4965:                 selfenroll_approval           => $env{'course.'.$cid.'.internal.selfenroll_approval'},
                   4966:                 selfenroll_limit              => $env{'course.'.$cid.'.internal.selfenroll_limit'},
                   4967:                 selfenroll_cap                => $env{'course.'.$cid.'.internal.selfenroll_cap'},
                   4968:                 selfenroll_start_date         => $env{'course.'.$cid.'.internal.selfenroll_start_date'},
                   4969:                 selfenroll_end_date           => $env{'course.'.$cid.'.internal.selfenroll_end_date'},
                   4970:                 selfenroll_start_access       => $env{'course.'.$cid.'.internal.selfenroll_start_access'},
                   4971:                 selfenroll_end_access         => $env{'course.'.$cid.'.internal.selfenroll_end_access'},
                   4972:                 default_enrollment_start_date => $env{'course.'.$cid.'.default_enrollment_start_date'},
                   4973:                 default_enrollment_end_date   => $env{'course.'.$cid.'.default_enrollment_end_date'},
1.400     raeburn  4974:                 uniquecode                    => $env{'course.'.$cid.'.internal.uniquecode'},
1.398     raeburn  4975:             );
                   4976:             push(@{$brcrum},
                   4977:                     {href => '/adm/createuser?action=selfenroll',
                   4978:                      text => "Configure Self-enrollment",
                   4979:                      help => 'Course_Self_Enrollment'});
                   4980:             if (!exists($env{'form.state'})) {
                   4981:                 $args = { bread_crumbs           => $brcrum,
                   4982:                           bread_crumbs_component => 'Configure Self-enrollment'};
                   4983:                 $r->print(&header(undef,$args));
                   4984:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
                   4985:                 &print_selfenroll_menu($r,'course',$cid,$cdom,$cnum,\%currsettings);
                   4986:             } elsif ($env{'form.state'} eq 'done') {
                   4987:                 push (@{$brcrum},
                   4988:                           {href=>'/adm/createuser?action=selfenroll',
                   4989:                            text=>"Result"});
                   4990:                 $args = { bread_crumbs           => $brcrum,
                   4991:                           bread_crumbs_component => 'Self-enrollment result'};
                   4992:                 $r->print(&header(undef,$args));
                   4993:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
1.400     raeburn  4994:                 &update_selfenroll_config($r,$cid,$cdom,$cnum,$context,$crstype,\%currsettings);
1.398     raeburn  4995:             }
                   4996:         } else {
                   4997:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   4998:                      '<span class="LC_error">'.&mt('You do not have permission to configure self-enrollment').'</span>');
1.237     raeburn  4999:         }
1.277     raeburn  5000:     } elsif ($env{'form.action'} eq 'selfenrollqueue') {
1.418     raeburn  5001:         if ($permission->{selfenrolladmin}) {
1.351     raeburn  5002:             push(@{$brcrum},
                   5003:                      {href => '/adm/createuser?action=selfenrollqueue',
1.418     raeburn  5004:                       text => 'Enrollment requests',
1.351     raeburn  5005:                       help => 'Course_Self_Enrollment'});
1.418     raeburn  5006:             $bread_crumbs_component = 'Enrollment requests';
                   5007:             if ($env{'form.state'} eq 'done') {
                   5008:                 push(@{$brcrum},
                   5009:                          {href => '/adm/createuser?action=selfenrollqueue',
                   5010:                           text => 'Result',
                   5011:                           help => 'Course_Self_Enrollment'});
                   5012:                 $bread_crumbs_component = 'Enrollment result';
                   5013:             }
                   5014:             $args = { bread_crumbs           => $brcrum,
                   5015:                       bread_crumbs_component => $bread_crumbs_component};
                   5016:             $r->print(&header(undef,$args));
                   5017:             my $cid = $env{'request.course.id'};
                   5018:             my $cdom = $env{'course.'.$cid.'.domain'};
                   5019:             my $cnum = $env{'course.'.$cid.'.num'};
                   5020:             my $coursedesc = $env{'course.'.$cid.'.description'};
                   5021:             if (!exists($env{'form.state'})) {
                   5022:                 $r->print('<h3>'.&mt('Pending enrollment requests').'</h3>'."\n");
                   5023:                 $r->print(&Apache::loncoursequeueadmin::display_queued_requests($context,
                   5024:                                                                                 $cdom,$cnum));
                   5025:             } elsif ($env{'form.state'} eq 'done') {
                   5026:                 $r->print('<h3>'.&mt('Enrollment request processing').'</h3>'."\n");
                   5027:                 $r->print(&Apache::loncoursequeueadmin::update_request_queue($context,
                   5028:                                $cdom,$cnum,$coursedesc));
                   5029:             }
                   5030:         } else {
                   5031:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5032:                      '<span class="LC_error">'.&mt('You do not have permission to manage self-enrollment').'</span>');
1.351     raeburn  5033:         }
1.418     raeburn  5034:     } elsif ($env{'form.action'} eq 'changelogs') {
                   5035:         if ($permission->{cusr} || $permission->{view}) {
                   5036:             &print_userchangelogs_display($r,$context,$permission,$brcrum);
                   5037:         } else {
                   5038:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5039:                      '<span class="LC_error">'.&mt('You do not have permission to view change logs').'</span>');
1.277     raeburn  5040:         }
1.190     raeburn  5041:     } else {
1.351     raeburn  5042:         $bread_crumbs_component = 'User Management';
                   5043:         $args = { bread_crumbs           => $brcrum,
                   5044:                   bread_crumbs_component => $bread_crumbs_component};
                   5045:         $r->print(&header(undef,$args));
1.318     raeburn  5046:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  5047:     }
1.351     raeburn  5048:     $r->print(&Apache::loncommon::end_page());
1.190     raeburn  5049:     return OK;
                   5050: }
                   5051: 
                   5052: sub header {
1.351     raeburn  5053:     my ($jscript,$args) = @_;
1.190     raeburn  5054:     my $start_page;
1.351     raeburn  5055:     if (ref($args) eq 'HASH') {
                   5056:         $start_page=&Apache::loncommon::start_page('User Management',$jscript,$args);
1.190     raeburn  5057:     } else {
1.351     raeburn  5058:         $start_page=&Apache::loncommon::start_page('User Management',$jscript);
1.190     raeburn  5059:     }
                   5060:     return $start_page;
                   5061: }
1.2       www      5062: 
1.191     raeburn  5063: sub add_script {
                   5064:     my ($js) = @_;
1.301     bisitz   5065:     return '<script type="text/javascript">'."\n"
                   5066:           .'// <![CDATA['."\n"
                   5067:           .$js."\n"
                   5068:           .'// ]]>'."\n"
                   5069:           .'</script>'."\n";
1.191     raeburn  5070: }
                   5071: 
1.391     raeburn  5072: sub usernamerequest_javascript {
                   5073:     my $js = <<ENDJS;
                   5074: 
                   5075: function openusernamereqdisplay(dom,uname,queue) {
                   5076:     var url = '/adm/createuser?action=displayuserreq';
                   5077:     url += '&domain='+dom+'&username='+uname+'&queue='+queue;
                   5078:     var title = 'Account_Request_Browser';
                   5079:     var options = 'scrollbars=1,resizable=1,menubar=0';
                   5080:     options += ',width=700,height=600';
                   5081:     var stdeditbrowser = open(url,title,options,'1');
                   5082:     stdeditbrowser.focus();
                   5083:     return;
                   5084: }
                   5085:  
                   5086: ENDJS
                   5087: }
                   5088: 
                   5089: sub close_popup_form {
                   5090:     my $close= &mt('Close Window');
                   5091:     return << "END";
                   5092: <p><form name="displayreq" action="" method="post">
                   5093: <input type="button" name="closeme" value="$close" onclick="javascript:self.close();" />
                   5094: </form></p>
                   5095: END
                   5096: }
                   5097: 
1.202     raeburn  5098: sub verify_user_display {
1.364     raeburn  5099:     my ($context) = @_;
1.374     raeburn  5100:     my %lt = &Apache::lonlocal::texthash (
                   5101:         course    => 'course(s): description, section(s), status',
                   5102:         community => 'community(s): description, section(s), status',
                   5103:         author    => 'author',
                   5104:     );
1.364     raeburn  5105:     my $photos;
                   5106:     if (($context eq 'course') && $env{'request.course.id'}) {
                   5107:         $photos = $env{'course.'.$env{'request.course.id'}.'.internal.showphoto'};
                   5108:     }
1.202     raeburn  5109:     my $output = <<"END";
                   5110: 
1.364     raeburn  5111: function hide_searching() {
                   5112:     if (document.getElementById('searching')) {
                   5113:         document.getElementById('searching').style.display = 'none';
                   5114:     }
                   5115:     return;
                   5116: }
                   5117: 
1.202     raeburn  5118: function display_update() {
                   5119:     document.studentform.action.value = 'listusers';
                   5120:     document.studentform.phase.value = 'display';
                   5121:     document.studentform.submit();
                   5122: }
                   5123: 
1.364     raeburn  5124: function updateCols(caller) {
                   5125:     var context = '$context';
                   5126:     var photos = '$photos';
                   5127:     if (caller == 'Status') {
1.374     raeburn  5128:         if ((context == 'domain') && 
                   5129:             ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   5130:              (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community'))) {
1.364     raeburn  5131:             document.getElementById('showcolstatus').checked = false;
                   5132:             document.getElementById('showcolstatus').disabled = 'disabled';
                   5133:             document.getElementById('showcolstart').checked = false;
                   5134:             document.getElementById('showcolend').checked = false;
1.374     raeburn  5135:         } else {
                   5136:             if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   5137:                 document.getElementById('showcolstatus').checked = true;
                   5138:                 document.getElementById('showcolstatus').disabled = '';
                   5139:                 document.getElementById('showcolstart').checked = true;
                   5140:                 document.getElementById('showcolend').checked = true;
                   5141:             } else {
                   5142:                 document.getElementById('showcolstatus').checked = false;
                   5143:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   5144:                 document.getElementById('showcolstart').checked = false;
                   5145:                 document.getElementById('showcolend').checked = false;
                   5146:             }
1.364     raeburn  5147:         }
                   5148:     }
                   5149:     if (caller == 'output') {
                   5150:         if (photos == 1) {
                   5151:             if (document.getElementById('showcolphoto')) {
                   5152:                 var photoitem = document.getElementById('showcolphoto');
                   5153:                 if (document.studentform.output.options[document.studentform.output.selectedIndex].value == 'html') {
                   5154:                     photoitem.checked = true;
                   5155:                     photoitem.disabled = '';
                   5156:                 } else {
                   5157:                     photoitem.checked = false;
                   5158:                     photoitem.disabled = 'disabled';
                   5159:                 }
                   5160:             }
                   5161:         }
                   5162:     }
                   5163:     if (caller == 'showrole') {
1.371     raeburn  5164:         if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any') ||
                   5165:             (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'cr')) {
1.364     raeburn  5166:             document.getElementById('showcolrole').checked = true;
                   5167:             document.getElementById('showcolrole').disabled = '';
                   5168:         } else {
                   5169:             document.getElementById('showcolrole').checked = false;
                   5170:             document.getElementById('showcolrole').disabled = 'disabled';
                   5171:         }
1.374     raeburn  5172:         if (context == 'domain') {
1.382     raeburn  5173:             var quotausageshow = 0;
1.374     raeburn  5174:             if ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   5175:                 (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community')) {
                   5176:                 document.getElementById('showcolstatus').checked = false;
                   5177:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   5178:                 document.getElementById('showcolstart').checked = false;
                   5179:                 document.getElementById('showcolend').checked = false;
                   5180:             } else {
                   5181:                 if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   5182:                     document.getElementById('showcolstatus').checked = true;
                   5183:                     document.getElementById('showcolstatus').disabled = '';
                   5184:                     document.getElementById('showcolstart').checked = true;
                   5185:                     document.getElementById('showcolend').checked = true;
                   5186:                 }
                   5187:             }
                   5188:             if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'domain') {
                   5189:                 document.getElementById('showcolextent').disabled = 'disabled';
                   5190:                 document.getElementById('showcolextent').checked = 'false';
                   5191:                 document.getElementById('showextent').style.display='none';
                   5192:                 document.getElementById('showcoltextextent').innerHTML = '';
1.382     raeburn  5193:                 if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'au') ||
                   5194:                     (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any')) {
                   5195:                     if (document.getElementById('showcolauthorusage')) {
                   5196:                         document.getElementById('showcolauthorusage').disabled = '';
                   5197:                     }
                   5198:                     if (document.getElementById('showcolauthorquota')) {
                   5199:                         document.getElementById('showcolauthorquota').disabled = '';
                   5200:                     }
                   5201:                     quotausageshow = 1;
                   5202:                 }
1.374     raeburn  5203:             } else {
                   5204:                 document.getElementById('showextent').style.display='block';
                   5205:                 document.getElementById('showextent').style.textAlign='left';
                   5206:                 document.getElementById('showextent').style.textFace='normal';
                   5207:                 if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'author') {
                   5208:                     document.getElementById('showcolextent').disabled = '';
                   5209:                     document.getElementById('showcolextent').checked = 'true';
                   5210:                     document.getElementById('showcoltextextent').innerHTML="$lt{'author'}";
                   5211:                 } else {
                   5212:                     document.getElementById('showcolextent').disabled = '';
                   5213:                     document.getElementById('showcolextent').checked = 'true';
                   5214:                     if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community') {
                   5215:                         document.getElementById('showcoltextextent').innerHTML="$lt{'community'}";
                   5216:                     } else {
                   5217:                         document.getElementById('showcoltextextent').innerHTML="$lt{'course'}";
                   5218:                     }
                   5219:                 }
                   5220:             }
1.382     raeburn  5221:             if (quotausageshow == 0)  {
                   5222:                 if (document.getElementById('showcolauthorusage')) {
                   5223:                     document.getElementById('showcolauthorusage').checked = false;
                   5224:                     document.getElementById('showcolauthorusage').disabled = 'disabled';
                   5225:                 }
                   5226:                 if (document.getElementById('showcolauthorquota')) {
                   5227:                     document.getElementById('showcolauthorquota').checked = false;
                   5228:                     document.getElementById('showcolauthorquota').disabled = 'disabled';
                   5229:                 }
                   5230:             }
1.374     raeburn  5231:         }
1.364     raeburn  5232:     }
                   5233:     return;
                   5234: }
                   5235: 
1.202     raeburn  5236: END
                   5237:     return $output;
                   5238: 
                   5239: }
                   5240: 
1.190     raeburn  5241: ###############################################################
                   5242: ###############################################################
                   5243: #  Menu Phase One
                   5244: sub print_main_menu {
1.318     raeburn  5245:     my ($permission,$context,$crstype) = @_;
                   5246:     my $linkcontext = $context;
                   5247:     my $stuterm = lc(&Apache::lonnet::plaintext('st',$crstype));
                   5248:     if (($context eq 'course') && ($crstype eq 'Community')) {
                   5249:         $linkcontext = lc($crstype);
                   5250:         $stuterm = 'Members';
                   5251:     }
1.208     raeburn  5252:     my %links = (
1.298     droeschl 5253:                 domain => {
                   5254:                             upload     => 'Upload a File of Users',
                   5255:                             singleuser => 'Add/Modify a User',
                   5256:                             listusers  => 'Manage Users',
                   5257:                             },
                   5258:                 author => {
                   5259:                             upload     => 'Upload a File of Co-authors',
                   5260:                             singleuser => 'Add/Modify a Co-author',
                   5261:                             listusers  => 'Manage Co-authors',
                   5262:                             },
                   5263:                 course => {
                   5264:                             upload     => 'Upload a File of Course Users',
                   5265:                             singleuser => 'Add/Modify a Course User',
1.354     www      5266:                             listusers  => 'List and Modify Multiple Course Users',
1.298     droeschl 5267:                             },
1.318     raeburn  5268:                 community => {
                   5269:                             upload     => 'Upload a File of Community Users',
                   5270:                             singleuser => 'Add/Modify a Community User',
1.354     www      5271:                             listusers  => 'List and Modify Multiple Community Users',
1.318     raeburn  5272:                            },
                   5273:                 );
                   5274:      my %linktitles = (
                   5275:                 domain => {
                   5276:                             singleuser => 'Add a user to the domain, and/or a course or community in the domain.',
                   5277:                             listusers  => 'Show and manage users in this domain.',
                   5278:                             },
                   5279:                 author => {
                   5280:                             singleuser => 'Add a user with a co- or assistant author role.',
                   5281:                             listusers  => 'Show and manage co- or assistant authors.',
                   5282:                             },
                   5283:                 course => {
                   5284:                             singleuser => 'Add a user with a certain role to this course.',
                   5285:                             listusers  => 'Show and manage users in this course.',
                   5286:                             },
                   5287:                 community => {
                   5288:                             singleuser => 'Add a user with a certain role to this community.',
                   5289:                             listusers  => 'Show and manage users in this community.',
                   5290:                            },
1.298     droeschl 5291:                 );
1.418     raeburn  5292:   if ($linkcontext eq 'domain') {
                   5293:       unless ($permission->{'cusr'}) {
                   5294:           $links{'domain'}{'singleuser'} = 'View a User'; 
                   5295:           $linktitles{'domain'}{'singleuser'} = 'View information about a user in the domain';
                   5296:           
                   5297:       }
                   5298:   } elsif ($linkcontext eq 'course') {
                   5299:       unless ($permission->{'cusr'}) {
                   5300:           $links{'course'}{'singleuser'} = 'View a Course User';
                   5301:           $linktitles{'course'}{'singleuser'} = 'View information about a user in this course';
                   5302:           $links{'course'}{'listusers'} = 'List Course Users';
                   5303:           $linktitles{'course'}{'listusers'} = 'Show information about users in this course';
                   5304:       }
                   5305:   } elsif ($linkcontext eq 'community') {
                   5306:       unless ($permission->{'cusr'}) {
                   5307:           $links{'community'}{'singleuser'} = 'View a Community User';
                   5308:           $linktitles{'community'}{'singleuser'} = 'View information about a user in this community';
                   5309:           $links{'community'}{'listusers'} = 'List Community Users';
                   5310:           $linktitles{'community'}{'listusers'} = 'Show information about users in this community';
                   5311:       }
                   5312:   }
1.298     droeschl 5313:   my @menu = ( {categorytitle => 'Single Users', 
                   5314:          items =>
                   5315:          [
                   5316:             {
1.318     raeburn  5317:              linktext => $links{$linkcontext}{'singleuser'},
1.298     droeschl 5318:              icon => 'edit-redo.png',
                   5319:              #help => 'Course_Change_Privileges',
                   5320:              url => '/adm/createuser?action=singleuser',
1.418     raeburn  5321:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  5322:              linktitle => $linktitles{$linkcontext}{'singleuser'},
1.298     droeschl 5323:             },
                   5324:          ]},
                   5325: 
                   5326:          {categorytitle => 'Multiple Users',
                   5327:          items => 
                   5328:          [
                   5329:             {
1.318     raeburn  5330:              linktext => $links{$linkcontext}{'upload'},
1.340     wenzelju 5331:              icon => 'uplusr.png',
1.298     droeschl 5332:              #help => 'Course_Create_Class_List',
                   5333:              url => '/adm/createuser?action=upload',
                   5334:              permission => $permission->{'cusr'},
                   5335:              linktitle => 'Upload a CSV or a text file containing users.',
                   5336:             },
                   5337:             {
1.318     raeburn  5338:              linktext => $links{$linkcontext}{'listusers'},
1.340     wenzelju 5339:              icon => 'mngcu.png',
1.298     droeschl 5340:              #help => 'Course_View_Class_List',
                   5341:              url => '/adm/createuser?action=listusers',
                   5342:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  5343:              linktitle => $linktitles{$linkcontext}{'listusers'}, 
1.298     droeschl 5344:             },
                   5345: 
                   5346:          ]},
                   5347: 
                   5348:          {categorytitle => 'Administration',
                   5349:          items => [ ]},
                   5350:        );
1.415     raeburn  5351: 
1.265     mielkec  5352:     if ($context eq 'domain'){
1.416     raeburn  5353:         push(@{  $menu[0]->{items} }, # Single Users
                   5354:             {
                   5355:              linktext => 'User Access Log',
1.417     raeburn  5356:              icon => 'document-properties.png',
1.425     raeburn  5357:              #help => 'Domain_User_Access_Logs',
1.416     raeburn  5358:              url => '/adm/createuser?action=accesslogs',
                   5359:              permission => $permission->{'activity'},
                   5360:              linktitle => 'View user access log.',
                   5361:             }
                   5362:         );
1.298     droeschl 5363:         
                   5364:         push(@{ $menu[2]->{items} }, #Category: Administration
                   5365:             {
                   5366:              linktext => 'Custom Roles',
                   5367:              icon => 'emblem-photos.png',
                   5368:              #help => 'Course_Editing_Custom_Roles',
                   5369:              url => '/adm/createuser?action=custom',
                   5370:              permission => $permission->{'custom'},
                   5371:              linktitle => 'Configure a custom role.',
                   5372:             },
1.362     raeburn  5373:             {
                   5374:              linktext => 'Authoring Space Requests',
                   5375:              icon => 'selfenrl-queue.png',
                   5376:              #help => 'Domain_Role_Approvals',
                   5377:              url => '/adm/createuser?action=processauthorreq',
                   5378:              permission => $permission->{'cusr'},
                   5379:              linktitle => 'Approve or reject author role requests',
                   5380:             },
1.363     raeburn  5381:             {
1.391     raeburn  5382:              linktext => 'LON-CAPA Account Requests',
                   5383:              icon => 'list-add.png',
                   5384:              #help => 'Domain_Username_Approvals',
                   5385:              url => '/adm/createuser?action=processusernamereq',
                   5386:              permission => $permission->{'cusr'},
                   5387:              linktitle => 'Approve or reject LON-CAPA account requests',
                   5388:             },
                   5389:             {
1.363     raeburn  5390:              linktext => 'Change Log',
                   5391:              icon => 'document-properties.png',
                   5392:              #help => 'Course_User_Logs',
                   5393:              url => '/adm/createuser?action=changelogs',
1.418     raeburn  5394:              permission => ($permission->{'cusr'} || $permission->{'view'}),
1.363     raeburn  5395:              linktitle => 'View change log.',
                   5396:             },
1.298     droeschl 5397:         );
                   5398:         
1.265     mielkec  5399:     }elsif ($context eq 'course'){
1.298     droeschl 5400:         my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
1.318     raeburn  5401: 
                   5402:         my %linktext = (
                   5403:                          'Course'    => {
                   5404:                                           single => 'Add/Modify a Student', 
                   5405:                                           drop   => 'Drop Students',
                   5406:                                           groups => 'Course Groups',
                   5407:                                         },
                   5408:                          'Community' => {
                   5409:                                           single => 'Add/Modify a Member', 
                   5410:                                           drop   => 'Drop Members',
                   5411:                                           groups => 'Community Groups',
                   5412:                                         },
                   5413:                        );
1.411     raeburn  5414:         $linktext{'Placement'} = $linktext{'Course'};
1.318     raeburn  5415: 
                   5416:         my %linktitle = (
                   5417:             'Course' => {
                   5418:                   single => 'Add a user with the role of student to this course',
                   5419:                   drop   => 'Remove a student from this course.',
                   5420:                   groups => 'Manage course groups',
                   5421:                         },
                   5422:             'Community' => {
                   5423:                   single => 'Add a user with the role of member to this community',
                   5424:                   drop   => 'Remove a member from this community.',
                   5425:                   groups => 'Manage community groups',
                   5426:                            },
                   5427:         );
                   5428: 
1.411     raeburn  5429:         $linktitle{'Placement'} = $linktitle{'Course'};
                   5430: 
1.298     droeschl 5431:         push(@{ $menu[0]->{items} }, #Category: Single Users
                   5432:             {   
1.318     raeburn  5433:              linktext => $linktext{$crstype}{'single'},
1.298     droeschl 5434:              #help => 'Course_Add_Student',
                   5435:              icon => 'list-add.png',
                   5436:              url => '/adm/createuser?action=singlestudent',
                   5437:              permission => $permission->{'cusr'},
1.318     raeburn  5438:              linktitle => $linktitle{$crstype}{'single'},
1.298     droeschl 5439:             },
                   5440:         );
                   5441:         
                   5442:         push(@{ $menu[1]->{items} }, #Category: Multiple Users 
                   5443:             {
1.318     raeburn  5444:              linktext => $linktext{$crstype}{'drop'},
1.298     droeschl 5445:              icon => 'edit-undo.png',
                   5446:              #help => 'Course_Drop_Student',
                   5447:              url => '/adm/createuser?action=drop',
                   5448:              permission => $permission->{'cusr'},
1.318     raeburn  5449:              linktitle => $linktitle{$crstype}{'drop'},
1.298     droeschl 5450:             },
                   5451:         );
                   5452:         push(@{ $menu[2]->{items} }, #Category: Administration
                   5453:             {    
                   5454:              linktext => 'Custom Roles',
                   5455:              icon => 'emblem-photos.png',
                   5456:              #help => 'Course_Editing_Custom_Roles',
                   5457:              url => '/adm/createuser?action=custom',
                   5458:              permission => $permission->{'custom'},
                   5459:              linktitle => 'Configure a custom role.',
                   5460:             },
                   5461:             {
1.318     raeburn  5462:              linktext => $linktext{$crstype}{'groups'},
1.333     wenzelju 5463:              icon => 'grps.png',
1.298     droeschl 5464:              #help => 'Course_Manage_Group',
                   5465:              url => '/adm/coursegroups?refpage=cusr',
                   5466:              permission => $permission->{'grp_manage'},
1.318     raeburn  5467:              linktitle => $linktitle{$crstype}{'groups'},
1.298     droeschl 5468:             },
                   5469:             {
1.328     wenzelju 5470:              linktext => 'Change Log',
1.298     droeschl 5471:              icon => 'document-properties.png',
                   5472:              #help => 'Course_User_Logs',
                   5473:              url => '/adm/createuser?action=changelogs',
1.418     raeburn  5474:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.298     droeschl 5475:              linktitle => 'View change log.',
                   5476:             },
                   5477:         );
1.277     raeburn  5478:         if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'}) {
1.298     droeschl 5479:             push(@{ $menu[2]->{items} },
1.398     raeburn  5480:                     {
1.298     droeschl 5481:                      linktext => 'Enrollment Requests',
                   5482:                      icon => 'selfenrl-queue.png',
                   5483:                      #help => 'Course_Approve_Selfenroll',
                   5484:                      url => '/adm/createuser?action=selfenrollqueue',
1.398     raeburn  5485:                      permission => $permission->{'selfenrolladmin'},
1.298     droeschl 5486:                      linktitle =>'Approve or reject enrollment requests.',
                   5487:                     },
                   5488:             );
1.277     raeburn  5489:         }
1.298     droeschl 5490:         
1.265     mielkec  5491:         if (!exists($permission->{'cusr_section'})){
1.320     raeburn  5492:             if ($crstype ne 'Community') {
                   5493:                 push(@{ $menu[2]->{items} },
                   5494:                     {
                   5495:                      linktext => 'Automated Enrollment',
                   5496:                      icon => 'roles.png',
                   5497:                      #help => 'Course_Automated_Enrollment',
                   5498:                      permission => (&Apache::lonnet::auto_run($cnum,$cdom)
1.418     raeburn  5499:                                          && (($permission->{'cusr'}) ||
                   5500:                                              ($permission->{'view'}))),
1.320     raeburn  5501:                      url  => '/adm/populate',
                   5502:                      linktitle => 'Automated enrollment manager.',
                   5503:                     }
                   5504:                 );
                   5505:             }
                   5506:             push(@{ $menu[2]->{items} }, 
1.298     droeschl 5507:                 {
                   5508:                  linktext => 'User Self-Enrollment',
1.342     wenzelju 5509:                  icon => 'self_enroll.png',
1.298     droeschl 5510:                  #help => 'Course_Self_Enrollment',
                   5511:                  url => '/adm/createuser?action=selfenroll',
1.398     raeburn  5512:                  permission => $permission->{'selfenrolladmin'},
1.317     bisitz   5513:                  linktitle => 'Configure user self-enrollment.',
1.298     droeschl 5514:                 },
                   5515:             );
                   5516:         }
1.363     raeburn  5517:     } elsif ($context eq 'author') {
1.370     raeburn  5518:         push(@{ $menu[2]->{items} }, #Category: Administration
1.363     raeburn  5519:             {
                   5520:              linktext => 'Change Log',
                   5521:              icon => 'document-properties.png',
                   5522:              #help => 'Course_User_Logs',
                   5523:              url => '/adm/createuser?action=changelogs',
                   5524:              permission => $permission->{'cusr'},
                   5525:              linktitle => 'View change log.',
                   5526:             },
1.370     raeburn  5527:         );
1.363     raeburn  5528:     }
                   5529:     return Apache::lonhtmlcommon::generate_menu(@menu);
1.250     raeburn  5530: #               { text => 'View Log-in History',
                   5531: #                 help => 'Course_User_Logins',
                   5532: #                 action => 'logins',
                   5533: #                 permission => $permission->{'cusr'},
                   5534: #               });
1.190     raeburn  5535: }
                   5536: 
1.189     albertel 5537: sub restore_prev_selections {
                   5538:     my %saveable_parameters = ('srchby'   => 'scalar',
                   5539: 			       'srchin'   => 'scalar',
                   5540: 			       'srchtype' => 'scalar',
                   5541: 			       );
                   5542:     &Apache::loncommon::store_settings('user','user_picker',
                   5543: 				       \%saveable_parameters);
                   5544:     &Apache::loncommon::restore_settings('user','user_picker',
                   5545: 					 \%saveable_parameters);
                   5546: }
                   5547: 
1.237     raeburn  5548: sub print_selfenroll_menu {
1.418     raeburn  5549:     my ($r,$context,$cid,$cdom,$cnum,$currsettings,$additional,$readonly) = @_;
1.322     raeburn  5550:     my $crstype = &Apache::loncommon::course_type();
1.398     raeburn  5551:     my $formname = 'selfenroll';
1.237     raeburn  5552:     my $nolink = 1;
1.398     raeburn  5553:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
1.237     raeburn  5554:     my $groupslist = &Apache::lonuserutils::get_groupslist();
                   5555:     my $setsec_js = 
                   5556:         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
1.249     raeburn  5557:     my %alerts = &Apache::lonlocal::texthash(
                   5558:         acto => 'Activation of self-enrollment was selected for the following domain(s)',
                   5559:         butn => 'but no user types have been checked.',
                   5560:         wilf => "Please uncheck 'activate' or check at least one type.",
                   5561:     );
1.418     raeburn  5562:     my $disabled;
                   5563:     if ($readonly) {
                   5564:        $disabled = ' disabled="disabled"';
                   5565:     }
1.405     damieng  5566:     &js_escape(\%alerts);
1.249     raeburn  5567:     my $selfenroll_js = <<"ENDSCRIPT";
                   5568: function update_types(caller,num) {
                   5569:     var delidx = getIndexByName('selfenroll_delete');
                   5570:     var actidx = getIndexByName('selfenroll_activate');
                   5571:     if (caller == 'selfenroll_all') {
                   5572:         var selall;
                   5573:         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5574:             if (document.$formname.selfenroll_all[i].checked) {
                   5575:                 selall = document.$formname.selfenroll_all[i].value;
                   5576:             }
                   5577:         }
                   5578:         if (selall == 1) {
                   5579:             if (delidx != -1) {
                   5580:                 if (document.$formname.selfenroll_delete.length) {
                   5581:                     for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5582:                         document.$formname.selfenroll_delete[j].checked = true;
                   5583:                     }
                   5584:                 } else {
                   5585:                     document.$formname.elements[delidx].checked = true;
                   5586:                 }
                   5587:             }
                   5588:             if (actidx != -1) {
                   5589:                 if (document.$formname.selfenroll_activate.length) {
                   5590:                     for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5591:                         document.$formname.selfenroll_activate[j].checked = false;
                   5592:                     }
                   5593:                 } else {
                   5594:                     document.$formname.elements[actidx].checked = false;
                   5595:                 }
                   5596:             }
                   5597:             document.$formname.selfenroll_newdom.selectedIndex = 0; 
                   5598:         }
                   5599:     }
                   5600:     if (caller == 'selfenroll_activate') {
                   5601:         if (document.$formname.selfenroll_activate.length) {
                   5602:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5603:                 if (document.$formname.selfenroll_activate[j].value == num) {
                   5604:                     if (document.$formname.selfenroll_activate[j].checked) {
                   5605:                         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5606:                             if (document.$formname.selfenroll_all[i].value == '1') {
                   5607:                                 document.$formname.selfenroll_all[i].checked = false;
                   5608:                             }
                   5609:                             if (document.$formname.selfenroll_all[i].value == '0') {
                   5610:                                 document.$formname.selfenroll_all[i].checked = true;
                   5611:                             }
                   5612:                         }
                   5613:                     }
                   5614:                 }
                   5615:             }
                   5616:         } else {
                   5617:             for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5618:                 if (document.$formname.selfenroll_all[i].value == '1') {
                   5619:                     document.$formname.selfenroll_all[i].checked = false;
                   5620:                 }
                   5621:                 if (document.$formname.selfenroll_all[i].value == '0') {
                   5622:                     document.$formname.selfenroll_all[i].checked = true;
                   5623:                 }
                   5624:             }
                   5625:         }
                   5626:     }
                   5627:     if (caller == 'selfenroll_delete') {
                   5628:         if (document.$formname.selfenroll_delete.length) {
                   5629:             for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5630:                 if (document.$formname.selfenroll_delete[j].value == num) {
                   5631:                     if (document.$formname.selfenroll_delete[j].checked) {
                   5632:                         var delindex = getIndexByName('selfenroll_types_'+num);
                   5633:                         if (delindex != -1) { 
                   5634:                             if (document.$formname.elements[delindex].length) {
                   5635:                                 for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5636:                                     document.$formname.elements[delindex][k].checked = false;
                   5637:                                 }
                   5638:                             } else {
                   5639:                                 document.$formname.elements[delindex].checked = false;
                   5640:                             }
                   5641:                         }
                   5642:                     }
                   5643:                 }
                   5644:             }
                   5645:         } else {
                   5646:             if (document.$formname.selfenroll_delete.checked) {
                   5647:                 var delindex = getIndexByName('selfenroll_types_'+num);
                   5648:                 if (delindex != -1) {
                   5649:                     if (document.$formname.elements[delindex].length) {
                   5650:                         for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5651:                             document.$formname.elements[delindex][k].checked = false;
                   5652:                         }
                   5653:                     } else {
                   5654:                         document.$formname.elements[delindex].checked = false;
                   5655:                     }
                   5656:                 }
                   5657:             }
                   5658:         }
                   5659:     }
                   5660:     return;
                   5661: }
                   5662: 
                   5663: function validate_types(form) {
                   5664:     var needaction = new Array();
                   5665:     var countfail = 0;
                   5666:     var actidx = getIndexByName('selfenroll_activate');
                   5667:     if (actidx != -1) {
                   5668:         if (document.$formname.selfenroll_activate.length) {
                   5669:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5670:                 var num = document.$formname.selfenroll_activate[j].value;
                   5671:                 if (document.$formname.selfenroll_activate[j].checked) {
                   5672:                     countfail = check_types(num,countfail,needaction)
                   5673:                 }
                   5674:             }
                   5675:         } else {
                   5676:             if (document.$formname.selfenroll_activate.checked) {
1.398     raeburn  5677:                 var num = document.$formname.selfenroll_activate.value;
1.249     raeburn  5678:                 countfail = check_types(num,countfail,needaction)
                   5679:             }
                   5680:         }
                   5681:     }
                   5682:     if (countfail > 0) {
                   5683:         var msg = "$alerts{'acto'}\\n";
                   5684:         var loopend = needaction.length -1;
                   5685:         if (loopend > 0) {
                   5686:             for (var m=0; m<loopend; m++) {
                   5687:                 msg += needaction[m]+", ";
                   5688:             }
                   5689:         }
                   5690:         msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
                   5691:         alert(msg);
                   5692:         return; 
                   5693:     }
                   5694:     setSections(form);
                   5695: }
                   5696: 
                   5697: function check_types(num,countfail,needaction) {
                   5698:     var typeidx = getIndexByName('selfenroll_types_'+num);
                   5699:     var count = 0;
                   5700:     if (typeidx != -1) {
                   5701:         if (document.$formname.elements[typeidx].length) {
                   5702:             for (var k=0; k<document.$formname.elements[typeidx].length; k++) {
                   5703:                 if (document.$formname.elements[typeidx][k].checked) {
                   5704:                     count ++;
                   5705:                 }
                   5706:             }
                   5707:         } else {
                   5708:             if (document.$formname.elements[typeidx].checked) {
                   5709:                 count ++;
                   5710:             }
                   5711:         }
                   5712:         if (count == 0) {
                   5713:             var domidx = getIndexByName('selfenroll_dom_'+num);
                   5714:             if (domidx != -1) {
                   5715:                 var domname = document.$formname.elements[domidx].value;
                   5716:                 needaction[countfail] = domname;
                   5717:                 countfail ++;
                   5718:             }
                   5719:         }
                   5720:     }
                   5721:     return countfail;
                   5722: }
                   5723: 
1.398     raeburn  5724: function toggleNotify() {
                   5725:     var selfenrollApproval = 0;
                   5726:     if (document.$formname.selfenroll_approval.length) {
                   5727:         for (var i=0; i<document.$formname.selfenroll_approval.length; i++) {
                   5728:             if (document.$formname.selfenroll_approval[i].checked) {
                   5729:                 selfenrollApproval = document.$formname.selfenroll_approval[i].value;
                   5730:                 break;        
                   5731:             }
                   5732:         }
                   5733:     }
                   5734:     if (document.getElementById('notified')) {
                   5735:         if (selfenrollApproval == 0) {
                   5736:             document.getElementById('notified').style.display='none';
                   5737:         } else {
                   5738:             document.getElementById('notified').style.display='block';
                   5739:         }
                   5740:     }
                   5741:     return;
                   5742: }
                   5743: 
1.249     raeburn  5744: function getIndexByName(item) {
                   5745:     for (var i=0;i<document.$formname.elements.length;i++) {
                   5746:         if (document.$formname.elements[i].name == item) {
                   5747:             return i;
                   5748:         }
                   5749:     }
                   5750:     return -1;
                   5751: }
                   5752: ENDSCRIPT
1.256     raeburn  5753: 
1.237     raeburn  5754:     my $output = '<script type="text/javascript">'."\n".
1.301     bisitz   5755:                  '// <![CDATA['."\n".
1.249     raeburn  5756:                  $setsec_js."\n".$selfenroll_js."\n".
1.301     bisitz   5757:                  '// ]]>'."\n".
1.237     raeburn  5758:                  '</script>'."\n".
1.256     raeburn  5759:                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n";
1.400     raeburn  5760:  
                   5761:     my $visactions = &cat_visibility();
                   5762:     my ($cathash,%cattype);
                   5763:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   5764:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   5765:         $cathash = $domconfig{'coursecategories'}{'cats'};
                   5766:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
                   5767:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
1.406     raeburn  5768:         if ($cattype{'auth'} eq '') {
                   5769:             $cattype{'auth'} = 'std';
                   5770:         }
                   5771:         if ($cattype{'unauth'} eq '') {
                   5772:             $cattype{'unauth'} = 'std';
                   5773:         }
1.400     raeburn  5774:     } else {
                   5775:         $cathash = {};
                   5776:         $cattype{'auth'} = 'std';
                   5777:         $cattype{'unauth'} = 'std';
                   5778:     }
                   5779:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
                   5780:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   5781:                   '<br />'.
                   5782:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   5783:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
                   5784:                   '</ul>');
                   5785:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
                   5786:         if ($currsettings->{'uniquecode'}) {
                   5787:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
                   5788:         } else {
                   5789:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   5790:                   '<br />'.
                   5791:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   5792:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
                   5793:                   '</ul><br />');
                   5794:         }
                   5795:     } else {
                   5796:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
                   5797:         if (ref($visactions) eq 'HASH') {
                   5798:             if ($visible) {
                   5799:                 $output .= '<p class="LC_info">'.$visactions->{'vis'}.'</p>';
                   5800:            } else {
                   5801:                 $output .= '<p class="LC_warning">'.$visactions->{'miss'}.'</p>'
                   5802:                           .$visactions->{'yous'}.
                   5803:                            '<p>'.$visactions->{'gen'}.'<br />'.$visactions->{'coca'};
                   5804:                 if (ref($vismsgs) eq 'ARRAY') {
                   5805:                     $output .= '<br />'.$visactions->{'make'}.'<ul>';
                   5806:                     foreach my $item (@{$vismsgs}) {
                   5807:                         $output .= '<li>'.$visactions->{$item}.'</li>';
                   5808:                     }
                   5809:                     $output .= '</ul>';
1.256     raeburn  5810:                 }
1.400     raeburn  5811:                 $output .= '</p>';
1.256     raeburn  5812:             }
                   5813:         }
                   5814:     }
1.398     raeburn  5815:     my $actionhref = '/adm/createuser';
                   5816:     if ($context eq 'domain') {
                   5817:         $actionhref = '/adm/modifycourse';
                   5818:     }
1.400     raeburn  5819: 
                   5820:     my %noedit;
                   5821:     unless ($context eq 'domain') {
                   5822:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
                   5823:     }
1.398     raeburn  5824:     $output .= '<form name="'.$formname.'" method="post" action="'.$actionhref.'">'."\n".
1.256     raeburn  5825:                &Apache::lonhtmlcommon::start_pick_box();
1.237     raeburn  5826:     if (ref($row) eq 'ARRAY') {
                   5827:         foreach my $item (@{$row}) {
                   5828:             my $title = $item; 
                   5829:             if (ref($lt) eq 'HASH') {
                   5830:                 $title = $lt->{$item};
                   5831:             }
1.297     bisitz   5832:             $output .= &Apache::lonhtmlcommon::row_title($title);
1.237     raeburn  5833:             if ($item eq 'types') {
1.398     raeburn  5834:                 my $curr_types;
                   5835:                 if (ref($currsettings) eq 'HASH') {
                   5836:                     $curr_types = $currsettings->{'selfenroll_types'};
                   5837:                 }
1.400     raeburn  5838:                 if ($noedit{$item}) {
                   5839:                     if ($curr_types eq '*') {
                   5840:                         $output .= &mt('Any user in any domain');   
                   5841:                     } else {
                   5842:                         my @entries = split(/;/,$curr_types);
                   5843:                         if (@entries > 0) {
                   5844:                             $output .= '<ul>'; 
                   5845:                             foreach my $entry (@entries) {
                   5846:                                 my ($currdom,$typestr) = split(/:/,$entry);
                   5847:                                 next if ($typestr eq '');
                   5848:                                 my $domdesc = &Apache::lonnet::domain($currdom);
                   5849:                                 my @currinsttypes = split(',',$typestr);
                   5850:                                 my ($othertitle,$usertypes,$types) = 
                   5851:                                     &Apache::loncommon::sorted_inst_types($currdom);
                   5852:                                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   5853:                                     $usertypes->{'any'} = &mt('any user'); 
                   5854:                                     if (keys(%{$usertypes}) > 0) {
                   5855:                                         $usertypes->{'other'} = &mt('other users');
                   5856:                                     }
                   5857:                                     my @longinsttypes = map { $usertypes->{$_}; } @currinsttypes;
                   5858:                                     $output .= '<li>'.$domdesc.':'.join(', ',@longinsttypes).'</li>';
                   5859:                                  }
                   5860:                             }
                   5861:                             $output .= '</ul>';
                   5862:                         } else {
                   5863:                             $output .= &mt('None');
                   5864:                         }
                   5865:                     }
                   5866:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   5867:                     next;
                   5868:                 }
1.241     raeburn  5869:                 my $showdomdesc = 1;
                   5870:                 my $includeempty = 1;
                   5871:                 my $num = 0;
                   5872:                 $output .= &Apache::loncommon::start_data_table().
                   5873:                            &Apache::loncommon::start_data_table_row()
                   5874:                            .'<td colspan="2"><span class="LC_nobreak"><label>'
                   5875:                            .&mt('Any user in any domain:')
                   5876:                            .'&nbsp;<input type="radio" name="selfenroll_all" value="1" ';
                   5877:                 if ($curr_types eq '*') {
                   5878:                     $output .= ' checked="checked" '; 
                   5879:                 }
1.249     raeburn  5880:                 $output .= 'onchange="javascript:update_types('.
1.418     raeburn  5881:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('Yes').'</label>'.
1.249     raeburn  5882:                            '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
1.241     raeburn  5883:                 if ($curr_types ne '*') {
                   5884:                     $output .= ' checked="checked" ';
                   5885:                 }
1.249     raeburn  5886:                 $output .= ' onchange="javascript:update_types('.
1.418     raeburn  5887:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('No').'</label></td>'.
1.249     raeburn  5888:                            &Apache::loncommon::end_data_table_row().
                   5889:                            &Apache::loncommon::end_data_table().
                   5890:                            &mt('Or').'<br />'.
                   5891:                            &Apache::loncommon::start_data_table();
1.241     raeburn  5892:                 my %currdoms;
1.249     raeburn  5893:                 if ($curr_types eq '') {
1.241     raeburn  5894:                     $output .= &new_selfenroll_dom_row($cdom,'0');
                   5895:                 } elsif ($curr_types ne '*') {
                   5896:                     my @entries = split(/;/,$curr_types);
                   5897:                     if (@entries > 0) {
                   5898:                         foreach my $entry (@entries) {
                   5899:                             my ($currdom,$typestr) = split(/:/,$entry);
                   5900:                             $currdoms{$currdom} = 1;
                   5901:                             my $domdesc = &Apache::lonnet::domain($currdom);
1.249     raeburn  5902:                             my @currinsttypes = split(',',$typestr);
1.241     raeburn  5903:                             $output .= &Apache::loncommon::start_data_table_row()
                   5904:                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
                   5905:                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
                   5906:                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
                   5907:                                        .'" value="'.$currdom.'" /></span><br />'
                   5908:                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
1.418     raeburn  5909:                                        .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');"'.$disabled.' />'
1.241     raeburn  5910:                                        .&mt('Delete').'</label></span></td>';
1.249     raeburn  5911:                             $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
1.418     raeburn  5912:                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes,$readonly).'</td>'
1.241     raeburn  5913:                                        .&Apache::loncommon::end_data_table_row();
                   5914:                             $num ++;
                   5915:                         }
                   5916:                     }
                   5917:                 }
1.249     raeburn  5918:                 my $add_domtitle = &mt('Users in additional domain:');
1.241     raeburn  5919:                 if ($curr_types eq '*') { 
1.249     raeburn  5920:                     $add_domtitle = &mt('Users in specific domain:');
1.241     raeburn  5921:                 } elsif ($curr_types eq '') {
1.249     raeburn  5922:                     $add_domtitle = &mt('Users in other domain:');
1.241     raeburn  5923:                 }
                   5924:                 $output .= &Apache::loncommon::start_data_table_row()
                   5925:                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
                   5926:                            .&Apache::loncommon::select_dom_form('','selfenroll_newdom',
1.418     raeburn  5927:                                                                 $includeempty,$showdomdesc,'','','',$readonly)
1.241     raeburn  5928:                            .'<input type="hidden" name="selfenroll_types_total" value="'.$num.'" />'
                   5929:                            .'</td>'.&Apache::loncommon::end_data_table_row()
                   5930:                            .&Apache::loncommon::end_data_table();
1.237     raeburn  5931:             } elsif ($item eq 'registered') {
                   5932:                 my ($regon,$regoff);
1.398     raeburn  5933:                 my $registered;
                   5934:                 if (ref($currsettings) eq 'HASH') {
                   5935:                     $registered = $currsettings->{'selfenroll_registered'};
                   5936:                 }
1.400     raeburn  5937:                 if ($noedit{$item}) {
                   5938:                     if ($registered) {
                   5939:                         $output .= &mt('Must be registered in course');
                   5940:                     } else {
                   5941:                         $output .= &mt('No requirement');
                   5942:                     }
                   5943:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   5944:                     next;
                   5945:                 }
1.398     raeburn  5946:                 if ($registered) {
1.237     raeburn  5947:                     $regon = ' checked="checked" ';
1.419     raeburn  5948:                     $regoff = '';
1.237     raeburn  5949:                 } else {
1.419     raeburn  5950:                     $regon = '';
1.237     raeburn  5951:                     $regoff = ' checked="checked" ';
                   5952:                 }
                   5953:                 $output .= '<label>'.
1.419     raeburn  5954:                            '<input type="radio" name="selfenroll_registered" value="1"'.$regon.$disabled.' />'.
1.244     bisitz   5955:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
1.419     raeburn  5956:                            '<input type="radio" name="selfenroll_registered" value="0"'.$regoff.$disabled.' />'.
1.244     bisitz   5957:                            &mt('No').'</label>';
1.237     raeburn  5958:             } elsif ($item eq 'enroll_dates') {
1.398     raeburn  5959:                 my ($starttime,$endtime);
                   5960:                 if (ref($currsettings) eq 'HASH') {
                   5961:                     $starttime = $currsettings->{'selfenroll_start_date'};
                   5962:                     $endtime = $currsettings->{'selfenroll_end_date'};
                   5963:                     if ($starttime eq '') {
                   5964:                         $starttime = $currsettings->{'default_enrollment_start_date'};
                   5965:                     }
                   5966:                     if ($endtime eq '') {
                   5967:                         $endtime = $currsettings->{'default_enrollment_end_date'};
                   5968:                     }
1.237     raeburn  5969:                 }
1.400     raeburn  5970:                 if ($noedit{$item}) {
                   5971:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
                   5972:                                                           &Apache::lonlocal::locallocaltime($endtime));
                   5973:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   5974:                     next;
                   5975:                 }
1.237     raeburn  5976:                 my $startform =
                   5977:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
1.418     raeburn  5978:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  5979:                 my $endform =
                   5980:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
1.418     raeburn  5981:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  5982:                 $output .= &selfenroll_date_forms($startform,$endform);
                   5983:             } elsif ($item eq 'access_dates') {
1.398     raeburn  5984:                 my ($starttime,$endtime);
                   5985:                 if (ref($currsettings) eq 'HASH') {
                   5986:                     $starttime = $currsettings->{'selfenroll_start_access'};
                   5987:                     $endtime = $currsettings->{'selfenroll_end_access'};
                   5988:                     if ($starttime eq '') {
                   5989:                         $starttime = $currsettings->{'default_enrollment_start_date'};
                   5990:                     }
                   5991:                     if ($endtime eq '') {
                   5992:                         $endtime = $currsettings->{'default_enrollment_end_date'};
                   5993:                     }
1.237     raeburn  5994:                 }
1.400     raeburn  5995:                 if ($noedit{$item}) {
                   5996:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
                   5997:                                                           &Apache::lonlocal::locallocaltime($endtime));
                   5998:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   5999:                     next;
                   6000:                 }
1.237     raeburn  6001:                 my $startform =
                   6002:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
1.418     raeburn  6003:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6004:                 my $endform =
                   6005:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
1.418     raeburn  6006:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6007:                 $output .= &selfenroll_date_forms($startform,$endform);
                   6008:             } elsif ($item eq 'section') {
1.398     raeburn  6009:                 my $currsec;
                   6010:                 if (ref($currsettings) eq 'HASH') {
                   6011:                     $currsec = $currsettings->{'selfenroll_section'};
                   6012:                 }
1.237     raeburn  6013:                 my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   6014:                 my $newsecval;
                   6015:                 if ($currsec ne 'none' && $currsec ne '') {
                   6016:                     if (!defined($sections_count{$currsec})) {
                   6017:                         $newsecval = $currsec;
                   6018:                     }
                   6019:                 }
1.400     raeburn  6020:                 if ($noedit{$item}) {
                   6021:                     if ($currsec ne '') {
                   6022:                         $output .= $currsec;
                   6023:                     } else {
                   6024:                         $output .= &mt('No specific section');
                   6025:                     }
                   6026:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6027:                     next;
                   6028:                 }
1.237     raeburn  6029:                 my $sections_select = 
1.418     raeburn  6030:                     &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec,$disabled);
1.237     raeburn  6031:                 $output .= '<table class="LC_createuser">'."\n".
                   6032:                            '<tr class="LC_section_row">'."\n".
                   6033:                            '<td align="center">'.&mt('Existing sections')."\n".
                   6034:                            '<br />'.$sections_select.'</td><td align="center">'.
                   6035:                            &mt('New section').'<br />'."\n".
1.418     raeburn  6036:                            '<input type="text" name="newsec" size="15" value="'.$newsecval.'"'.$disabled.' />'."\n".
1.237     raeburn  6037:                            '<input type="hidden" name="sections" value="" />'."\n".
                   6038:                            '</td></tr></table>'."\n";
1.276     raeburn  6039:             } elsif ($item eq 'approval') {
1.398     raeburn  6040:                 my ($currnotified,$currapproval,%appchecked);
                   6041:                 my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
                   6042:                 if (ref($currsettings) eq 'HASH') { 
                   6043:                     $currnotified = $currsettings->{'selfenroll_notifylist'};
                   6044:                     $currapproval = $currsettings->{'selfenroll_approval'};
                   6045:                 }
                   6046:                 if ($currapproval !~ /^[012]$/) {
                   6047:                     $currapproval = 0;
                   6048:                 }
1.400     raeburn  6049:                 if ($noedit{$item}) {
                   6050:                     $output .=  $selfdescs{'approval'}{$currapproval}.
                   6051:                                 '<br />'.&mt('(Set by Domain Coordinator)');
                   6052:                     next;
                   6053:                 }
1.398     raeburn  6054:                 $appchecked{$currapproval} = ' checked="checked"';
                   6055:                 for my $i (0..2) {
                   6056:                     $output .= '<label>'.
                   6057:                                '<input type="radio" name="selfenroll_approval" value="'.$i.'"'.
1.418     raeburn  6058:                                $appchecked{$i}.' onclick="toggleNotify();"'.$disabled.' />'.
                   6059:                                $selfdescs{'approval'}{$i}.'</label>'.('&nbsp;'x2);
1.276     raeburn  6060:                 }
                   6061:                 my %advhash = &Apache::lonnet::get_course_adv_roles($cid,1);
                   6062:                 my (@ccs,%notified);
1.322     raeburn  6063:                 my $ccrole = 'cc';
                   6064:                 if ($crstype eq 'Community') {
                   6065:                     $ccrole = 'co';
                   6066:                 }
                   6067:                 if ($advhash{$ccrole}) {
                   6068:                     @ccs = split(/,/,$advhash{$ccrole});
1.276     raeburn  6069:                 }
                   6070:                 if ($currnotified) {
                   6071:                     foreach my $current (split(/,/,$currnotified)) {
                   6072:                         $notified{$current} = 1;
                   6073:                         if (!grep(/^\Q$current\E$/,@ccs)) {
                   6074:                             push(@ccs,$current);
                   6075:                         }
                   6076:                     }
                   6077:                 }
                   6078:                 if (@ccs) {
1.398     raeburn  6079:                     my $style;
                   6080:                     unless ($currapproval) {
                   6081:                         $style = ' style="display: none;"'; 
                   6082:                     }
                   6083:                     $output .= '<br /><div id="notified"'.$style.'>'.
                   6084:                                &mt('Personnel to be notified when an enrollment request needs approval, or has been approved:').'&nbsp;'.
                   6085:                                &Apache::loncommon::start_data_table().
1.276     raeburn  6086:                                &Apache::loncommon::start_data_table_row();
                   6087:                     my $count = 0;
                   6088:                     my $numcols = 4;
                   6089:                     foreach my $cc (sort(@ccs)) {
                   6090:                         my $notifyon;
                   6091:                         my ($ccuname,$ccudom) = split(/:/,$cc);
                   6092:                         if ($notified{$cc}) {
                   6093:                             $notifyon = ' checked="checked" ';
                   6094:                         }
                   6095:                         if ($count && !$count%$numcols) {
                   6096:                             $output .= &Apache::loncommon::end_data_table_row().
                   6097:                                        &Apache::loncommon::start_data_table_row()
                   6098:                         }
                   6099:                         $output .= '<td><span class="LC_nobreak"><label>'.
1.418     raeburn  6100:                                    '<input type="checkbox" name="selfenroll_notify"'.$notifyon.' value="'.$cc.'"'.$disabled.' />'.
1.276     raeburn  6101:                                    &Apache::loncommon::plainname($ccuname,$ccudom).
                   6102:                                    '</label></span></td>';
1.343     raeburn  6103:                         $count ++;
1.276     raeburn  6104:                     }
                   6105:                     my $rem = $count%$numcols;
                   6106:                     if ($rem) {
                   6107:                         my $emptycols = $numcols - $rem;
                   6108:                         for (my $i=0; $i<$emptycols; $i++) { 
                   6109:                             $output .= '<td>&nbsp;</td>';
                   6110:                         }
                   6111:                     }
                   6112:                     $output .= &Apache::loncommon::end_data_table_row().
1.398     raeburn  6113:                                &Apache::loncommon::end_data_table().
                   6114:                                '</div>';
1.276     raeburn  6115:                 }
                   6116:             } elsif ($item eq 'limit') {
1.398     raeburn  6117:                 my ($crslimit,$selflimit,$nolimit,$currlim,$currcap);
                   6118:                 if (ref($currsettings) eq 'HASH') {
                   6119:                     $currlim = $currsettings->{'selfenroll_limit'};
                   6120:                     $currcap = $currsettings->{'selfenroll_cap'};
                   6121:                 }
1.400     raeburn  6122:                 if ($noedit{$item}) {
                   6123:                     if (($currlim eq 'allstudents') || ($currlim eq 'selfenrolled')) {
                   6124:                         if ($currlim eq 'allstudents') {
                   6125:                             $output .= &mt('Limit by total students');
                   6126:                         } elsif ($currlim eq 'selfenrolled') {
                   6127:                             $output .= &mt('Limit by total self-enrolled students');
                   6128:                         }
                   6129:                         $output .= ' '.&mt('Maximum: [_1]',$currcap).
                   6130:                                    '<br />'.&mt('(Set by Domain Coordinator)');
                   6131:                     } else {
                   6132:                         $output .= &mt('No limit').'<br />'.&mt('(Set by Domain Coordinator)');
                   6133:                     }
                   6134:                     next;
                   6135:                 }
1.276     raeburn  6136:                 if ($currlim eq 'allstudents') {
                   6137:                     $crslimit = ' checked="checked" ';
                   6138:                     $selflimit = ' ';
                   6139:                     $nolimit = ' ';
                   6140:                 } elsif ($currlim eq 'selfenrolled') {
                   6141:                     $crslimit = ' ';
                   6142:                     $selflimit = ' checked="checked" ';
                   6143:                     $nolimit = ' '; 
                   6144:                 } else {
                   6145:                     $crslimit = ' ';
                   6146:                     $selflimit = ' ';
1.398     raeburn  6147:                     $nolimit = ' checked="checked" ';
1.276     raeburn  6148:                 }
                   6149:                 $output .= '<table><tr><td><label>'.
1.418     raeburn  6150:                            '<input type="radio" name="selfenroll_limit" value="none"'.$nolimit.$disabled.'/>'.
1.276     raeburn  6151:                            &mt('No limit').'</label></td><td><label>'.
1.418     raeburn  6152:                            '<input type="radio" name="selfenroll_limit" value="allstudents"'.$crslimit.$disabled.'/>'.
1.276     raeburn  6153:                            &mt('Limit by total students').'</label></td><td><label>'.
1.418     raeburn  6154:                            '<input type="radio" name="selfenroll_limit" value="selfenrolled"'.$selflimit.$disabled.'/>'.
1.276     raeburn  6155:                            &mt('Limit by total self-enrolled students').
                   6156:                            '</td></tr><tr>'.
                   6157:                            '<td>&nbsp;</td><td colspan="2"><span class="LC_nobreak">'.
                   6158:                            ('&nbsp;'x3).&mt('Maximum number allowed: ').
1.418     raeburn  6159:                            '<input type="text" name="selfenroll_cap" size = "5" value="'.$currcap.'"'.$disabled.' /></td></tr></table>';
1.237     raeburn  6160:             }
                   6161:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   6162:         }
                   6163:     }
1.418     raeburn  6164:     $output .= &Apache::lonhtmlcommon::end_pick_box().'<br />';
                   6165:     unless ($readonly) {
                   6166:         $output .= '<input type="button" name="selfenrollconf" value="'
                   6167:                    .&mt('Save').'" onclick="validate_types(this.form);" />';
                   6168:     }
                   6169:     $output .= '<input type="hidden" name="action" value="selfenroll" />'
                   6170:               .'<input type="hidden" name="state" value="done" />'."\n"
                   6171:               .$additional.'</form>';
1.237     raeburn  6172:     $r->print($output);
                   6173:     return;
                   6174: }
                   6175: 
1.400     raeburn  6176: sub get_noedit_fields {
                   6177:     my ($cdom,$cnum,$crstype,$row) = @_;
                   6178:     my %noedit;
                   6179:     if (ref($row) eq 'ARRAY') {
                   6180:         my %settings = &Apache::lonnet::get('environment',['internal.coursecode','internal.textbook',
                   6181:                                                            'internal.selfenrollmgrdc',
                   6182:                                                            'internal.selfenrollmgrcc'],$cdom,$cnum);
                   6183:         my $type = &Apache::lonuserutils::get_extended_type($cdom,$cnum,$crstype,\%settings);
                   6184:         my (%specific_managebydc,%specific_managebycc,%default_managebydc);
                   6185:         map { $specific_managebydc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrdc'}));
                   6186:         map { $specific_managebycc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrcc'}));
                   6187:         my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
                   6188:         map { $default_managebydc{$_} = 1; } (split(/,/,$domdefaults{$type.'selfenrolladmdc'}));
                   6189: 
                   6190:         foreach my $item (@{$row}) {
                   6191:             next if ($specific_managebycc{$item});
                   6192:             if (($specific_managebydc{$item}) || ($default_managebydc{$item})) {
                   6193:                 $noedit{$item} = 1;
                   6194:             }
                   6195:         }
                   6196:     }
                   6197:     return %noedit;
                   6198: } 
                   6199: 
                   6200: sub visible_in_stdcat {
                   6201:     my ($cdom,$cnum,$domconf) = @_;
                   6202:     my ($cathash,%settable,@vismsgs,$cansetvis,$visible);
                   6203:     unless (ref($domconf) eq 'HASH') {
                   6204:         return ($visible,$cansetvis,\@vismsgs);
                   6205:     }
                   6206:     if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6207:         if ($domconf->{'coursecategories'}{'togglecats'} eq 'crs') {
1.256     raeburn  6208:             $settable{'togglecats'} = 1;
                   6209:         }
1.400     raeburn  6210:         if ($domconf->{'coursecategories'}{'categorize'} eq 'crs') {
1.256     raeburn  6211:             $settable{'categorize'} = 1;
                   6212:         }
1.400     raeburn  6213:         $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6214:     }
1.260     raeburn  6215:     if ($settable{'togglecats'} && $settable{'categorize'}) {
1.256     raeburn  6216:         $cansetvis = &mt('You are able to both assign a course category and choose to exclude this course from the catalog.');   
                   6217:     } elsif ($settable{'togglecats'}) {
                   6218:         $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  6219:     } elsif ($settable{'categorize'}) {
1.256     raeburn  6220:         $cansetvis = &mt('You may assign a course category, but only a Domain Coordinator may choose to exclude this course from the catalog.');  
                   6221:     } else {
                   6222:         $cansetvis = &mt('Only a Domain Coordinator may assign a course category or choose to exclude this course from the catalog.'); 
                   6223:     }
                   6224:      
                   6225:     my %currsettings =
                   6226:         &Apache::lonnet::get('environment',['hidefromcat','categories','internal.coursecode'],
                   6227:                              $cdom,$cnum);
1.400     raeburn  6228:     $visible = 0;
1.256     raeburn  6229:     if ($currsettings{'internal.coursecode'} ne '') {
1.400     raeburn  6230:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6231:             $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6232:             if (ref($cathash) eq 'HASH') {
                   6233:                 if ($cathash->{'instcode::0'} eq '') {
                   6234:                     push(@vismsgs,'dc_addinst'); 
                   6235:                 } else {
                   6236:                     $visible = 1;
                   6237:                 }
                   6238:             } else {
                   6239:                 $visible = 1;
                   6240:             }
                   6241:         } else {
                   6242:             $visible = 1;
                   6243:         }
                   6244:     } else {
                   6245:         if (ref($cathash) eq 'HASH') {
                   6246:             if ($cathash->{'instcode::0'} ne '') {
                   6247:                 push(@vismsgs,'dc_instcode');
                   6248:             }
                   6249:         } else {
                   6250:             push(@vismsgs,'dc_instcode');
                   6251:         }
                   6252:     }
                   6253:     if ($currsettings{'categories'} ne '') {
                   6254:         my $cathash;
1.400     raeburn  6255:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6256:             $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6257:             if (ref($cathash) eq 'HASH') {
                   6258:                 if (keys(%{$cathash}) == 0) {
                   6259:                     push(@vismsgs,'dc_catalog');
                   6260:                 } elsif ((keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} ne '')) {
                   6261:                     push(@vismsgs,'dc_categories');
                   6262:                 } else {
                   6263:                     my @currcategories = split('&',$currsettings{'categories'});
                   6264:                     my $matched = 0;
                   6265:                     foreach my $cat (@currcategories) {
                   6266:                         if ($cathash->{$cat} ne '') {
                   6267:                             $visible = 1;
                   6268:                             $matched = 1;
                   6269:                             last;
                   6270:                         }
                   6271:                     }
                   6272:                     if (!$matched) {
1.260     raeburn  6273:                         if ($settable{'categorize'}) { 
1.256     raeburn  6274:                             push(@vismsgs,'chgcat');
                   6275:                         } else {
                   6276:                             push(@vismsgs,'dc_chgcat');
                   6277:                         }
                   6278:                     }
                   6279:                 }
                   6280:             }
                   6281:         }
                   6282:     } else {
                   6283:         if (ref($cathash) eq 'HASH') {
                   6284:             if ((keys(%{$cathash}) > 1) || 
                   6285:                 (keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} eq '')) {
1.260     raeburn  6286:                 if ($settable{'categorize'}) {
1.256     raeburn  6287:                     push(@vismsgs,'addcat');
                   6288:                 } else {
                   6289:                     push(@vismsgs,'dc_addcat');
                   6290:                 }
                   6291:             }
                   6292:         }
                   6293:     }
                   6294:     if ($currsettings{'hidefromcat'} eq 'yes') {
                   6295:         $visible = 0;
                   6296:         if ($settable{'togglecats'}) {
                   6297:             unshift(@vismsgs,'unhide');
                   6298:         } else {
                   6299:             unshift(@vismsgs,'dc_unhide')
                   6300:         }
                   6301:     }
1.400     raeburn  6302:     return ($visible,$cansetvis,\@vismsgs);
                   6303: }
                   6304: 
                   6305: sub cat_visibility {
                   6306:     my %visactions = &Apache::lonlocal::texthash(
                   6307:                    vis => 'This course/community currently appears in the Course/Community Catalog for this domain.',
                   6308:                    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.',
                   6309:                    miss => 'This course/community does not currently appear in the Course/Community Catalog for this domain.',
                   6310:                    none => 'Display of a course catalog is disabled for this domain.',
                   6311:                    yous => 'You should remedy this if you plan to allow self-enrollment, otherwise students will have difficulty finding this course.',
                   6312:                    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.',
                   6313:                    make => 'Make any changes to self-enrollment settings below, click "Save", then take action to include the course in the Catalog:',
                   6314:                    take => 'Take the following action to ensure the course appears in the Catalog:',
                   6315:                    dc_chgconf => 'Ask a domain coordinator to change the Catalog type for this domain.',
                   6316:                    dc_setcode => 'Ask a domain coordinator to assign a six character code to the course',
                   6317:                    dc_unhide  => 'Ask a domain coordinator to change the "Exclude from course catalog" setting.',
                   6318:                    dc_addinst => 'Ask a domain coordinator to enable display the catalog of "Official courses (with institutional codes)".',
                   6319:                    dc_instcode => 'Ask a domain coordinator to assign an institutional code (if this is an official course).',
                   6320:                    dc_catalog  => 'Ask a domain coordinator to enable or create at least one course category in the domain.',
                   6321:                    dc_categories => 'Ask a domain coordinator to create a hierarchy of categories and sub categories for courses in the domain.',
                   6322:                    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',
                   6323:                    dc_addcat => 'Ask a domain coordinator to assign a category to the course.',
                   6324:     );
                   6325:     $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>"');
                   6326:     $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>"');
                   6327:     $visactions{'addcat'} = &mt('Use [_1]Categorize course[_2] to assign a category to the course.','"<a href="/adm/courseprefs?phase=display&actions=courseinfo">','</a>"');
                   6328:     return \%visactions;
1.256     raeburn  6329: }
                   6330: 
1.241     raeburn  6331: sub new_selfenroll_dom_row {
                   6332:     my ($newdom,$num) = @_;
                   6333:     my $domdesc = &Apache::lonnet::domain($newdom);
                   6334:     my $output;
                   6335:     if ($domdesc ne '') {
                   6336:         $output .= &Apache::loncommon::start_data_table_row()
                   6337:                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
                   6338:                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
1.249     raeburn  6339:                    .'" value="'.$newdom.'" /></span><br />'
                   6340:                    .'<span class="LC_nobreak"><label><input type="checkbox" '
                   6341:                    .'name="selfenroll_activate" value="'.$num.'" '
                   6342:                    .'onchange="javascript:update_types('
                   6343:                    ."'selfenroll_activate','$num'".');" />'
                   6344:                    .&mt('Activate').'</label></span></td>';
1.241     raeburn  6345:         my @currinsttypes;
                   6346:         $output .= '<td>'.&mt('User types:').'<br />'
                   6347:                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
                   6348:                    .&Apache::loncommon::end_data_table_row();
                   6349:     }
                   6350:     return $output;
                   6351: }
                   6352: 
                   6353: sub selfenroll_inst_types {
1.418     raeburn  6354:     my ($num,$currdom,$currinsttypes,$readonly) = @_;
1.241     raeburn  6355:     my $output;
                   6356:     my $numinrow = 4;
                   6357:     my $count = 0;
                   6358:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($currdom);
1.247     raeburn  6359:     my $othervalue = 'any';
1.418     raeburn  6360:     my $disabled;
                   6361:     if ($readonly) {
                   6362:         $disabled = ' disabled="disabled"';
                   6363:     }
1.241     raeburn  6364:     if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
1.251     raeburn  6365:         if (keys(%{$usertypes}) > 0) {
1.247     raeburn  6366:             $othervalue = 'other';
                   6367:         }
1.241     raeburn  6368:         $output .= '<table><tr>';
                   6369:         foreach my $type (@{$types}) {
                   6370:             if (($count > 0) && ($count%$numinrow == 0)) {
                   6371:                 $output .= '</tr><tr>';
                   6372:             }
                   6373:             if (defined($usertypes->{$type})) {
1.257     raeburn  6374:                 my $esc_type = &escape($type);
1.241     raeburn  6375:                 $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.
1.257     raeburn  6376:                            $esc_type.'" ';
1.241     raeburn  6377:                 if (ref($currinsttypes) eq 'ARRAY') {
                   6378:                     if (@{$currinsttypes} > 0) {
1.249     raeburn  6379:                         if (grep(/^any$/,@{$currinsttypes})) {
                   6380:                             $output .= 'checked="checked"';
1.257     raeburn  6381:                         } elsif (grep(/^\Q$esc_type\E$/,@{$currinsttypes})) {
1.241     raeburn  6382:                             $output .= 'checked="checked"';
                   6383:                         }
1.249     raeburn  6384:                     } else {
                   6385:                         $output .= 'checked="checked"';
1.241     raeburn  6386:                     }
                   6387:                 }
1.418     raeburn  6388:                 $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$usertypes->{$type}.'</label></span></td>';
1.241     raeburn  6389:             }
                   6390:             $count ++;
                   6391:         }
                   6392:         if (($count > 0) && ($count%$numinrow == 0)) {
                   6393:             $output .= '</tr><tr>';
                   6394:         }
1.249     raeburn  6395:         $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
1.241     raeburn  6396:         if (ref($currinsttypes) eq 'ARRAY') {
                   6397:             if (@{$currinsttypes} > 0) {
1.249     raeburn  6398:                 if (grep(/^any$/,@{$currinsttypes})) { 
                   6399:                     $output .= ' checked="checked"';
                   6400:                 } elsif ($othervalue eq 'other') {
                   6401:                     if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
                   6402:                         $output .= ' checked="checked"';
                   6403:                     }
1.241     raeburn  6404:                 }
1.249     raeburn  6405:             } else {
                   6406:                 $output .= ' checked="checked"';
1.241     raeburn  6407:             }
1.249     raeburn  6408:         } else {
                   6409:             $output .= ' checked="checked"';
1.241     raeburn  6410:         }
1.418     raeburn  6411:         $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$othertitle.'</label></span></td></tr></table>';
1.241     raeburn  6412:     }
                   6413:     return $output;
                   6414: }
                   6415: 
1.237     raeburn  6416: sub selfenroll_date_forms {
                   6417:     my ($startform,$endform) = @_;
                   6418:     my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
1.244     bisitz   6419:                   &Apache::lonhtmlcommon::row_title(&mt('Start date'),
1.237     raeburn  6420:                                                     'LC_oddrow_value')."\n".
                   6421:                   $startform."\n".
                   6422:                   &Apache::lonhtmlcommon::row_closure(1).
1.244     bisitz   6423:                   &Apache::lonhtmlcommon::row_title(&mt('End date'),
1.237     raeburn  6424:                                                    'LC_oddrow_value')."\n".
                   6425:                   $endform."\n".
                   6426:                   &Apache::lonhtmlcommon::row_closure(1).
                   6427:                   &Apache::lonhtmlcommon::end_pick_box();
                   6428:     return $output;
                   6429: }
                   6430: 
1.239     raeburn  6431: sub print_userchangelogs_display {
1.415     raeburn  6432:     my ($r,$context,$permission,$brcrum) = @_;
1.363     raeburn  6433:     my $formname = 'rolelog';
1.418     raeburn  6434:     my ($username,$domain,$crstype,$viewablesec,%roleslog);
1.363     raeburn  6435:     if ($context eq 'domain') {
                   6436:         $domain = $env{'request.role.domain'};
                   6437:         %roleslog=&Apache::lonnet::dump_dom('nohist_rolelog',$domain);
                   6438:     } else {
                   6439:         if ($context eq 'course') { 
                   6440:             $domain = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   6441:             $username = $env{'course.'.$env{'request.course.id'}.'.num'};
                   6442:             $crstype = &Apache::loncommon::course_type();
1.418     raeburn  6443:             $viewablesec = &Apache::lonuserutils::viewable_section($permission);
1.363     raeburn  6444:             my %saveable_parameters = ('show' => 'scalar',);
                   6445:             &Apache::loncommon::store_course_settings('roles_log',
                   6446:                                                       \%saveable_parameters);
                   6447:             &Apache::loncommon::restore_course_settings('roles_log',
                   6448:                                                         \%saveable_parameters);
                   6449:         } elsif ($context eq 'author') {
                   6450:             $domain = $env{'user.domain'}; 
                   6451:             if ($env{'request.role'} =~ m{^au\./\Q$domain\E/$}) {
                   6452:                 $username = $env{'user.name'};
                   6453:             } else {
                   6454:                 undef($domain);
                   6455:             }
                   6456:         }
                   6457:         if ($domain ne '' && $username ne '') { 
                   6458:             %roleslog=&Apache::lonnet::dump('nohist_rolelog',$domain,$username);
                   6459:         }
                   6460:     }
1.239     raeburn  6461:     if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
                   6462: 
1.415     raeburn  6463:     my $helpitem;
                   6464:     if ($context eq 'course') {
                   6465:         $helpitem = 'Course_User_Logs';
                   6466:     }
                   6467:     push (@{$brcrum},
                   6468:              {href => '/adm/createuser?action=changelogs',
                   6469:               text => 'User Management Logs',
                   6470:               help => $helpitem});
                   6471:     my $bread_crumbs_component = 'User Changes';
                   6472:     my $args = { bread_crumbs           => $brcrum,
                   6473:                  bread_crumbs_component => $bread_crumbs_component};
                   6474: 
                   6475:     # Create navigation javascript
                   6476:     my $jsnav = &userlogdisplay_js($formname);
                   6477: 
                   6478:     my $jscript = (<<ENDSCRIPT);
                   6479: <script type="text/javascript">
                   6480: // <![CDATA[
                   6481: $jsnav
                   6482: // ]]>
                   6483: </script>
                   6484: ENDSCRIPT
                   6485: 
                   6486:     # print page header
                   6487:     $r->print(&header($jscript,$args));
                   6488: 
1.239     raeburn  6489:     # set defaults
                   6490:     my $now = time();
                   6491:     my $defstart = $now - (7*24*3600); #7 days ago 
                   6492:     my %defaults = (
                   6493:                      page               => '1',
                   6494:                      show               => '10',
                   6495:                      role               => 'any',
                   6496:                      chgcontext         => 'any',
                   6497:                      rolelog_start_date => $defstart,
                   6498:                      rolelog_end_date   => $now,
                   6499:                    );
                   6500:     my $more_records = 0;
                   6501: 
                   6502:     # set current
                   6503:     my %curr;
                   6504:     foreach my $item ('show','page','role','chgcontext') {
                   6505:         $curr{$item} = $env{'form.'.$item};
                   6506:     }
                   6507:     my ($startdate,$enddate) = 
                   6508:         &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
                   6509:     $curr{'rolelog_start_date'} = $startdate;
                   6510:     $curr{'rolelog_end_date'} = $enddate;
                   6511:     foreach my $key (keys(%defaults)) {
                   6512:         if ($curr{$key} eq '') {
                   6513:             $curr{$key} = $defaults{$key};
                   6514:         }
                   6515:     }
1.248     raeburn  6516:     my (%whodunit,%changed,$version);
                   6517:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
1.239     raeburn  6518:     my ($minshown,$maxshown);
1.255     raeburn  6519:     $minshown = 1;
1.239     raeburn  6520:     my $count = 0;
1.415     raeburn  6521:     if ($curr{'show'} =~ /\D/) {
                   6522:         $curr{'page'} = 1;
                   6523:     } else {
1.239     raeburn  6524:         $maxshown = $curr{'page'} * $curr{'show'};
                   6525:         if ($curr{'page'} > 1) {
                   6526:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   6527:         }
                   6528:     }
1.301     bisitz   6529: 
1.327     raeburn  6530:     # Form Header
                   6531:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
1.363     raeburn  6532:               &role_display_filter($context,$formname,$domain,$username,\%curr,
                   6533:                                    $version,$crstype));
1.327     raeburn  6534: 
                   6535:     my $showntableheader = 0;
                   6536: 
                   6537:     # Table Header
                   6538:     my $tableheader = 
                   6539:         &Apache::loncommon::start_data_table_header_row()
                   6540:        .'<th>&nbsp;</th>'
                   6541:        .'<th>'.&mt('When').'</th>'
                   6542:        .'<th>'.&mt('Who made the change').'</th>'
                   6543:        .'<th>'.&mt('Changed User').'</th>'
1.363     raeburn  6544:        .'<th>'.&mt('Role').'</th>';
                   6545: 
                   6546:     if ($context eq 'course') {
                   6547:         $tableheader .= '<th>'.&mt('Section').'</th>';
                   6548:     }
                   6549:     $tableheader .=
                   6550:         '<th>'.&mt('Context').'</th>'
1.327     raeburn  6551:        .'<th>'.&mt('Start').'</th>'
                   6552:        .'<th>'.&mt('End').'</th>'
                   6553:        .&Apache::loncommon::end_data_table_header_row();
                   6554: 
                   6555:     # Display user change log data
1.239     raeburn  6556:     foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
                   6557:         next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
                   6558:                  ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
1.415     raeburn  6559:         if ($curr{'show'} !~ /\D/) {
1.239     raeburn  6560:             if ($count >= $curr{'page'} * $curr{'show'}) {
                   6561:                 $more_records = 1;
                   6562:                 last;
                   6563:             }
                   6564:         }
                   6565:         if ($curr{'role'} ne 'any') {
                   6566:             next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
                   6567:         }
                   6568:         if ($curr{'chgcontext'} ne 'any') {
                   6569:             if ($curr{'chgcontext'} eq 'selfenroll') {
                   6570:                 next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
                   6571:             } else {
                   6572:                 next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
                   6573:             }
                   6574:         }
1.418     raeburn  6575:         if (($context eq 'course') && ($viewablesec ne '')) {
                   6576:             next if ($roleslog{$id}{'logentry'}{'section'} ne $viewablesec); 
                   6577:         }
1.239     raeburn  6578:         $count ++;
                   6579:         next if ($count < $minshown);
1.327     raeburn  6580:         unless ($showntableheader) {
1.415     raeburn  6581:             $r->print(&Apache::loncommon::start_data_table()
1.327     raeburn  6582:                      .$tableheader);
                   6583:             $r->rflush();
                   6584:             $showntableheader = 1;
                   6585:         }
1.239     raeburn  6586:         if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
                   6587:             $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
                   6588:                 &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
                   6589:         }
                   6590:         if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
                   6591:             $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
                   6592:                 &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
                   6593:         }
                   6594:         my $sec = $roleslog{$id}{'logentry'}{'section'};
                   6595:         if ($sec eq '') {
                   6596:             $sec = &mt('None');
                   6597:         }
                   6598:         my ($rolestart,$roleend);
                   6599:         if ($roleslog{$id}{'delflag'}) {
                   6600:             $rolestart = &mt('deleted');
                   6601:             $roleend = &mt('deleted');
                   6602:         } else {
                   6603:             $rolestart = $roleslog{$id}{'logentry'}{'start'};
                   6604:             $roleend = $roleslog{$id}{'logentry'}{'end'};
                   6605:             if ($rolestart eq '' || $rolestart == 0) {
                   6606:                 $rolestart = &mt('No start date'); 
                   6607:             } else {
                   6608:                 $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
                   6609:             }
                   6610:             if ($roleend eq '' || $roleend == 0) { 
                   6611:                 $roleend = &mt('No end date');
                   6612:             } else {
                   6613:                 $roleend = &Apache::lonlocal::locallocaltime($roleend);
                   6614:             }
                   6615:         }
                   6616:         my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
                   6617:         if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
                   6618:             $chgcontext = 'selfenroll';
                   6619:         }
1.363     raeburn  6620:         my %lt = &rolechg_contexts($context,$crstype);
1.239     raeburn  6621:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
                   6622:             $chgcontext = $lt{$chgcontext};
                   6623:         }
1.327     raeburn  6624:         $r->print(
1.301     bisitz   6625:             &Apache::loncommon::start_data_table_row()
                   6626:            .'<td>'.$count.'</td>'
                   6627:            .'<td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td>'
                   6628:            .'<td>'.$whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.'</td>'
                   6629:            .'<td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td>'
1.363     raeburn  6630:            .'<td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'},$crstype).'</td>');
                   6631:         if ($context eq 'course') { 
                   6632:             $r->print('<td>'.$sec.'</td>');
                   6633:         }
                   6634:         $r->print(
                   6635:             '<td>'.$chgcontext.'</td>'
1.301     bisitz   6636:            .'<td>'.$rolestart.'</td>'
                   6637:            .'<td>'.$roleend.'</td>'
1.327     raeburn  6638:            .&Apache::loncommon::end_data_table_row()."\n");
1.301     bisitz   6639:     }
                   6640: 
1.327     raeburn  6641:     if ($showntableheader) { # Table footer, if content displayed above
1.415     raeburn  6642:         $r->print(&Apache::loncommon::end_data_table().
                   6643:                   &userlogdisplay_navlinks(\%curr,$more_records));
1.327     raeburn  6644:     } else { # No content displayed above
1.301     bisitz   6645:         $r->print('<p class="LC_info">'
                   6646:                  .&mt('There are no records to display.')
                   6647:                  .'</p>'
                   6648:         );
1.239     raeburn  6649:     }
1.301     bisitz   6650: 
1.327     raeburn  6651:     # Form Footer
                   6652:     $r->print( 
                   6653:         '<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   6654:        .'<input type="hidden" name="action" value="changelogs" />'
                   6655:        .'</form>');
                   6656:     return;
                   6657: }
1.301     bisitz   6658: 
1.416     raeburn  6659: sub print_useraccesslogs_display {
                   6660:     my ($r,$uname,$udom,$permission,$brcrum) = @_;
                   6661:     my $formname = 'accesslog';
                   6662:     my $form = 'document.accesslog';
                   6663: 
                   6664: # set breadcrumbs
1.422     raeburn  6665:     my %breadcrumb_text = &singleuser_breadcrumb('','domain',$udom);
1.416     raeburn  6666:     push (@{$brcrum},
                   6667:         {href => "javascript:backPage($form)",
                   6668:          text => $breadcrumb_text{'search'}});
                   6669:     my (@prevphases,$prevphasestr);
                   6670:     if ($env{'form.prevphases'}) {
                   6671:         @prevphases = split(/,/,$env{'form.prevphases'});
                   6672:         $prevphasestr = $env{'form.prevphases'};
                   6673:     }
                   6674:     if (($env{'form.phase'} eq 'userpicked') || (grep(/^userpicked$/,@prevphases))) {
                   6675:         push(@{$brcrum},
                   6676:               {href => "javascript:backPage($form,'get_user_info','select')",
                   6677:                text => $breadcrumb_text{'userpicked'}});
                   6678:         if ($env{'form.phase'} eq 'userpicked') {
                   6679:             $prevphasestr = 'userpicked';
                   6680:         }
                   6681:     }
                   6682:     push(@{$brcrum},
                   6683:              {href => '/adm/createuser?action=accesslogs',
                   6684:               text => 'User access logs',
1.424     raeburn  6685:               help => 'Domain_User_Access_Logs'});
1.416     raeburn  6686:     my $bread_crumbs_component = 'User Access Logs';
                   6687:     my $args = { bread_crumbs           => $brcrum,
                   6688:                  bread_crumbs_component => 'User Management'};
1.423     raeburn  6689:     if ($env{'form.popup'}) {
                   6690:         $args->{'no_nav_bar'} = 1;
                   6691:     }
1.416     raeburn  6692: 
1.417     raeburn  6693: # set javascript
1.416     raeburn  6694:     my ($jsback,$elements) = &crumb_utilities();
                   6695:     my $jsnav = &userlogdisplay_js($formname);
                   6696: 
                   6697:     my $jscript = (<<ENDSCRIPT);
                   6698: <script type="text/javascript">
                   6699: // <![CDATA[
                   6700: 
                   6701: $jsback
                   6702: $jsnav
                   6703: 
                   6704: // ]]>
                   6705: </script>
                   6706: 
                   6707: ENDSCRIPT
                   6708: 
1.417     raeburn  6709: # print page header
1.416     raeburn  6710:     $r->print(&header($jscript,$args));
                   6711: 
                   6712: # early out unless log data can be displayed.
                   6713:     unless ($permission->{'activity'}) {
                   6714:         $r->print('<p class="LC_warning">'
                   6715:                  .&mt('You do not have rights to display user access logs.')
                   6716:                  .'</p>'
                   6717:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   6718:         return;
                   6719:     }
                   6720: 
                   6721:     unless ($udom eq $env{'request.role.domain'}) {
                   6722:         $r->print('<p class="LC_warning">'
                   6723:                  .&mt("User's domain must match role's domain")
                   6724:                  .'</p>'
                   6725:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
1.417     raeburn  6726:         return;
1.416     raeburn  6727:     }
                   6728: 
                   6729:     if (($uname eq '') || ($udom eq '')) {
                   6730:         $r->print('<p class="LC_warning">'
                   6731:                  .&mt('Invalid username or domain')
                   6732:                  .'</p>'
                   6733:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   6734:         return;
                   6735:     }
                   6736: 
                   6737: # set defaults
                   6738:     my $now = time();
                   6739:     my $defstart = $now - (7*24*3600);
                   6740:     my %defaults = (
                   6741:                      page                 => '1',
                   6742:                      show                 => '10',
                   6743:                      activity             => 'any',
                   6744:                      accesslog_start_date => $defstart,
                   6745:                      accesslog_end_date   => $now,
                   6746:                    );
                   6747:     my $more_records = 0;
                   6748: 
                   6749: # set current
                   6750:     my %curr;
                   6751:     foreach my $item ('show','page','activity') {
                   6752:         $curr{$item} = $env{'form.'.$item};
                   6753:     }
                   6754:     my ($startdate,$enddate) =
                   6755:         &Apache::lonuserutils::get_dates_from_form('accesslog_start_date','accesslog_end_date');
                   6756:     $curr{'accesslog_start_date'} = $startdate;
                   6757:     $curr{'accesslog_end_date'} = $enddate;
                   6758:     foreach my $key (keys(%defaults)) {
                   6759:         if ($curr{$key} eq '') {
                   6760:             $curr{$key} = $defaults{$key};
                   6761:         }
                   6762:     }
                   6763:     my ($minshown,$maxshown);
                   6764:     $minshown = 1;
                   6765:     my $count = 0;
                   6766:     if ($curr{'show'} =~ /\D/) {
                   6767:         $curr{'page'} = 1;
                   6768:     } else {
                   6769:         $maxshown = $curr{'page'} * $curr{'show'};
                   6770:         if ($curr{'page'} > 1) {
                   6771:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   6772:         }
                   6773:     }
                   6774: 
                   6775: # form header
                   6776:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
                   6777:               &activity_display_filter($formname,\%curr));
                   6778: 
                   6779:     my $showntableheader = 0;
                   6780:     my ($nav_script,$nav_links);
                   6781: 
                   6782: # table header
                   6783:     my $tableheader =
                   6784:         &Apache::loncommon::start_data_table_header_row()
                   6785:        .'<th>&nbsp;</th>'
                   6786:        .'<th>'.&mt('When').'</th>'
                   6787:        .'<th>'.&mt('HostID').'</th>'
                   6788:        .'<th>'.&mt('Event').'</th>'
                   6789:        .'<th>'.&mt('Other data').'</th>'
                   6790:        .&Apache::loncommon::end_data_table_header_row();
                   6791: 
                   6792:     my %filters=(
                   6793:         start  => $curr{'accesslog_start_date'},
                   6794:         end    => $curr{'accesslog_end_date'},
                   6795:         action => $curr{'activity'},
                   6796:     );
                   6797: 
                   6798:     my $reply = &Apache::lonnet::userlog_query($uname,$udom,%filters);
                   6799:     unless ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   6800:         my (%courses,%missing);
                   6801:         my @results = split(/\&/,$reply);
                   6802:         foreach my $item (reverse(@results)) {
                   6803:             my ($timestamp,$host,$event) = split(/:/,$item);
                   6804:             next unless ($event =~ /^(Log|Role)/);
                   6805:             if ($curr{'show'} !~ /\D/) {
                   6806:                 if ($count >= $curr{'page'} * $curr{'show'}) {
                   6807:                     $more_records = 1;
                   6808:                     last;
                   6809:                 }
                   6810:             }
                   6811:             $count ++;
                   6812:             next if ($count < $minshown);
                   6813:             unless ($showntableheader) {
                   6814:                 $r->print($nav_script
                   6815:                          .&Apache::loncommon::start_data_table()
                   6816:                          .$tableheader);
                   6817:                 $r->rflush();
                   6818:                 $showntableheader = 1;
                   6819:             }
1.418     raeburn  6820:             my ($shown,$extra);
1.416     raeburn  6821:             my ($event,$data) = split(/\s+/,&unescape($event));
                   6822:             if ($event eq 'Role') {
                   6823:                 my ($rolecode,$extent) = split(/\./,$data,2);
                   6824:                 next if ($extent eq '');
                   6825:                 my ($crstype,$desc,$info);
1.418     raeburn  6826:                 if ($extent =~ m{^/($match_domain)/($match_courseid)(?:/(\w+)|)$}) {
                   6827:                     my ($cdom,$cnum,$sec) = ($1,$2,$3);
1.416     raeburn  6828:                     my $cid = $cdom.'_'.$cnum;
                   6829:                     if (exists($courses{$cid})) {
                   6830:                         $crstype = $courses{$cid}{'type'};
                   6831:                         $desc = $courses{$cid}{'description'};
                   6832:                     } elsif ($missing{$cid}) {
                   6833:                         $crstype = 'Course';
                   6834:                         $desc = 'Course/Community';
                   6835:                     } else {
                   6836:                         my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   6837:                         if (ref($crsinfo{$cdom.'_'.$cnum}) eq 'HASH') {
                   6838:                             $courses{$cid} = $crsinfo{$cid};
                   6839:                             $crstype = $crsinfo{$cid}{'type'};
                   6840:                             $desc = $crsinfo{$cid}{'description'};
                   6841:                         } else {
                   6842:                             $missing{$cid} = 1;
                   6843:                         }
                   6844:                     }
                   6845:                     $extra = &mt($crstype).': <a href="/public/'.$cdom.'/'.$cnum.'/syllabus">'.$desc.'</a>';
1.418     raeburn  6846:                     if ($sec ne '') {
                   6847:                        $extra .= ' ('.&mt('Section: [_1]',$sec).')';
                   6848:                     }
1.416     raeburn  6849:                 } elsif ($extent =~ m{^/($match_domain)/($match_username|$)}) {
                   6850:                     my ($dom,$name) = ($1,$2);
                   6851:                     if ($rolecode eq 'au') {
                   6852:                         $extra = '';
                   6853:                     } elsif ($rolecode =~ /^(ca|aa)$/) {
1.417     raeburn  6854:                         $extra = &mt('Authoring Space: [_1]',$name.':'.$dom);
1.416     raeburn  6855:                     } elsif ($rolecode =~ /^(li|dg|dh|dc|sc)$/) {
                   6856:                         $extra = &mt('Domain: [_1]',$dom);
                   6857:                     }
                   6858:                 }
                   6859:                 my $rolename;
                   6860:                 if ($rolecode =~ m{^cr/($match_domain)/($match_username)/(\w+)}) {
                   6861:                     my $role = $3;
1.417     raeburn  6862:                     my $owner = "($2:$1)";
1.416     raeburn  6863:                     if ($2 eq $1.'-domainconfig') {
                   6864:                         $owner = '(ad hoc)';
1.417     raeburn  6865:                     }
1.416     raeburn  6866:                     $rolename = &mt('Custom role: [_1]',$role.' '.$owner);
                   6867:                 } else {
                   6868:                     $rolename = &Apache::lonnet::plaintext($rolecode,$crstype);
                   6869:                 }
                   6870:                 $shown = &mt('Role selection: [_1]',$rolename);
                   6871:             } else {
                   6872:                 $shown = &mt($event);
                   6873:                 if ($data ne '') {
                   6874:                    $extra = &mt('Client IP address: [_1]',$data);
                   6875:                 }
                   6876:             }
                   6877:             $r->print(
                   6878:             &Apache::loncommon::start_data_table_row()
                   6879:            .'<td>'.$count.'</td>'
                   6880:            .'<td>'.&Apache::lonlocal::locallocaltime($timestamp).'</td>'
                   6881:            .'<td>'.$host.'</td>'
                   6882:            .'<td>'.$shown.'</td>'
                   6883:            .'<td>'.$extra.'</td>'
                   6884:            .&Apache::loncommon::end_data_table_row()."\n");
                   6885:         }
                   6886:     }
                   6887: 
                   6888:     if ($showntableheader) { # Table footer, if content displayed above
                   6889:         $r->print(&Apache::loncommon::end_data_table().
                   6890:                   &userlogdisplay_navlinks(\%curr,$more_records));
                   6891:     } else { # No content displayed above
                   6892:         $r->print('<p class="LC_info">'
                   6893:                  .&mt('There are no records to display.')
                   6894:                  .'</p>');
                   6895:     }
                   6896: 
1.423     raeburn  6897:     if ($env{'form.popup'} == 1) {
                   6898:         $r->print('<input type="hidden" name="popup" value="1" />'."\n");
                   6899:     }
                   6900: 
1.416     raeburn  6901:     # Form Footer
                   6902:     $r->print(
                   6903:         '<input type="hidden" name="currstate" value="" />'
                   6904:        .'<input type="hidden" name="accessuname" value="'.$uname.'" />'
                   6905:        .'<input type="hidden" name="accessudom" value="'.$udom.'" />'
                   6906:        .'<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   6907:        .'<input type="hidden" name="prevphases" value="'.$prevphasestr.'" />'
                   6908:        .'<input type="hidden" name="phase" value="activity" />'
                   6909:        .'<input type="hidden" name="action" value="accesslogs" />'
                   6910:        .'<input type="hidden" name="srchdomain" value="'.$udom.'" />'
                   6911:        .'<input type="hidden" name="srchby" value="'.$env{'form.srchby'}.'" />'
                   6912:        .'<input type="hidden" name="srchtype" value="'.$env{'form.srchtype'}.'" />'
                   6913:        .'<input type="hidden" name="srchterm" value="'.&HTML::Entities::encode($env{'form.srchterm'},'<>"&').'" />'
                   6914:        .'<input type="hidden" name="srchin" value="'.$env{'form.srchin'}.'" />'
                   6915:        .'</form>');
                   6916:     return;
                   6917: }
                   6918: 
                   6919: sub earlyout_accesslog_form {
                   6920:     my ($formname,$prevphasestr,$udom) = @_;
                   6921:     my $srchterm = &HTML::Entities::encode($env{'form.srchterm'},'<>"&');
                   6922:    return <<"END";
                   6923: <form action="/adm/createuser" method="post" name="$formname">
                   6924: <input type="hidden" name="currstate" value="" />
                   6925: <input type="hidden" name="prevphases" value="$prevphasestr" />
                   6926: <input type="hidden" name="phase" value="activity" />
                   6927: <input type="hidden" name="action" value="accesslogs" />
                   6928: <input type="hidden" name="srchdomain" value="$udom" />
                   6929: <input type="hidden" name="srchby" value="$env{'form.srchby'}" />
                   6930: <input type="hidden" name="srchtype" value="$env{'form.srchtype'}" />
                   6931: <input type="hidden" name="srchterm" value="$srchterm" />
                   6932: <input type="hidden" name="srchin" value="$env{'form.srchin'}" />
                   6933: </form>
                   6934: END
                   6935: }
                   6936: 
                   6937: sub activity_display_filter {
                   6938:     my ($formname,$curr) = @_;
                   6939:     my $nolink = 1;
                   6940:     my $output = '<table><tr><td valign="top">'.
                   6941:                  '<span class="LC_nobreak"><b>'.&mt('Actions/page:').'</b></span><br />'.
                   6942:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   6943:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   6944:                  '</td><td>&nbsp;&nbsp;</td>';
                   6945:     my $startform =
                   6946:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_start_date',
                   6947:                                             $curr->{'accesslog_start_date'},undef,
                   6948:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   6949:     my $endform =
                   6950:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_end_date',
                   6951:                                             $curr->{'accesslog_end_date'},undef,
                   6952:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   6953:     my %lt = &Apache::lonlocal::texthash (
                   6954:                                           activity => 'Activity',
                   6955:                                           Role     => 'Role selection',
                   6956:                                           log      => 'Log-in or Logout',
                   6957:     );
                   6958:     $output .= '<td valign="top"><b>'.&mt('Window during which actions occurred:').'</b><br />'.
                   6959:                '<table><tr><td>'.&mt('After:').
                   6960:                '</td><td>'.$startform.'</td></tr>'.
                   6961:                '<tr><td>'.&mt('Before:').'</td>'.
                   6962:                '<td>'.$endform.'</td></tr></table>'.
                   6963:                '</td>'.
                   6964:                '<td>&nbsp;&nbsp;</td>'.
                   6965:                '<td valign="top"><b>'.&mt('Activities').'</b><br />'.
                   6966:                '<select name="activity"><option value="any"';
                   6967:     if ($curr->{'activity'} eq 'any') {
                   6968:         $output .= ' selected="selected"';
                   6969:     }
                   6970:     $output .= '>'.&mt('Any').'</option>'."\n";
                   6971:     foreach my $activity ('Role','log') {
                   6972:         my $selstr = '';
                   6973:         if ($activity eq $curr->{'activity'}) {
                   6974:             $selstr = ' selected="selected"';
                   6975:         }
                   6976:         $output .= '<option value="'.$activity.'"'.$selstr.'>'.$lt{$activity}.'</option>';
                   6977:     }
                   6978:     $output .= '</select></td>'.
                   6979:                '</tr></table>';
                   6980:     # Update Display button
                   6981:     $output .= '<p>'
                   6982:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   6983:               .'</p>';
                   6984:     return $output;
                   6985: }
                   6986: 
1.415     raeburn  6987: sub userlogdisplay_js {
                   6988:     my ($formname) = @_;
                   6989:     return <<"ENDSCRIPT";
                   6990: 
1.239     raeburn  6991: function chgPage(caller) {
                   6992:     if (caller == 'previous') {
                   6993:         document.$formname.page.value --;
                   6994:     }
                   6995:     if (caller == 'next') {
                   6996:         document.$formname.page.value ++;
                   6997:     }
1.327     raeburn  6998:     document.$formname.submit();
1.239     raeburn  6999:     return;
                   7000: }
                   7001: ENDSCRIPT
1.415     raeburn  7002: }
                   7003: 
                   7004: sub userlogdisplay_navlinks {
                   7005:     my ($curr,$more_records) = @_;
                   7006:     return unless(ref($curr) eq 'HASH');
                   7007:     # Navigation Buttons
                   7008:     my $nav_links = '<p>';
                   7009:     if (($curr->{'page'} > 1) || ($more_records)) {
                   7010:         if (($curr->{'page'} > 1) && ($curr->{'show'} !~ /\D/)) {
                   7011:             $nav_links .= '<input type="button"'
                   7012:                          .' onclick="javascript:chgPage('."'previous'".');"'
                   7013:                          .' value="'.&mt('Previous [_1] changes',$curr->{'show'})
                   7014:                          .'" /> ';
                   7015:         }
                   7016:         if ($more_records) {
                   7017:             $nav_links .= '<input type="button"'
                   7018:                          .' onclick="javascript:chgPage('."'next'".');"'
                   7019:                          .' value="'.&mt('Next [_1] changes',$curr->{'show'})
                   7020:                          .'" />';
1.301     bisitz   7021:         }
                   7022:     }
1.415     raeburn  7023:     $nav_links .= '</p>';
                   7024:     return $nav_links;
1.239     raeburn  7025: }
                   7026: 
                   7027: sub role_display_filter {
1.363     raeburn  7028:     my ($context,$formname,$cdom,$cnum,$curr,$version,$crstype) = @_;
                   7029:     my $lctype;
                   7030:     if ($context eq 'course') {
                   7031:         $lctype = lc($crstype);
                   7032:     }
1.239     raeburn  7033:     my $nolink = 1;
                   7034:     my $output = '<table><tr><td valign="top">'.
1.301     bisitz   7035:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b></span><br />'.
1.239     raeburn  7036:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   7037:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   7038:                  '</td><td>&nbsp;&nbsp;</td>';
                   7039:     my $startform =
                   7040:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
                   7041:                                             $curr->{'rolelog_start_date'},undef,
                   7042:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   7043:     my $endform =
                   7044:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
                   7045:                                             $curr->{'rolelog_end_date'},undef,
                   7046:                                             undef,undef,undef,undef,undef,undef,$nolink);
1.363     raeburn  7047:     my %lt = &rolechg_contexts($context,$crstype);
1.301     bisitz   7048:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br />'.
                   7049:                '<table><tr><td>'.&mt('After:').
                   7050:                '</td><td>'.$startform.'</td></tr>'.
                   7051:                '<tr><td>'.&mt('Before:').'</td>'.
                   7052:                '<td>'.$endform.'</td></tr></table>'.
                   7053:                '</td>'.
                   7054:                '<td>&nbsp;&nbsp;</td>'.
1.239     raeburn  7055:                '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
                   7056:                '<select name="role"><option value="any"';
                   7057:     if ($curr->{'role'} eq 'any') {
                   7058:         $output .= ' selected="selected"';
                   7059:     }
                   7060:     $output .=  '>'.&mt('Any').'</option>'."\n";
1.363     raeburn  7061:     my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
1.239     raeburn  7062:     foreach my $role (@roles) {
                   7063:         my $plrole;
                   7064:         if ($role eq 'cr') {
                   7065:             $plrole = &mt('Custom Role');
                   7066:         } else {
1.318     raeburn  7067:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.239     raeburn  7068:         }
                   7069:         my $selstr = '';
                   7070:         if ($role eq $curr->{'role'}) {
                   7071:             $selstr = ' selected="selected"';
                   7072:         }
                   7073:         $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
                   7074:     }
1.301     bisitz   7075:     $output .= '</select></td>'.
                   7076:                '<td>&nbsp;&nbsp;</td>'.
                   7077:                '<td valign="top"><b>'.
1.239     raeburn  7078:                &mt('Context:').'</b><br /><select name="chgcontext">';
1.363     raeburn  7079:     my @posscontexts;
                   7080:     if ($context eq 'course') {
1.376     raeburn  7081:         @posscontexts = ('any','automated','updatenow','createcourse','course','domain','selfenroll','requestcourses');
1.363     raeburn  7082:     } elsif ($context eq 'domain') {
                   7083:         @posscontexts = ('any','domain','requestauthor','domconfig','server');
                   7084:     } else {
                   7085:         @posscontexts = ('any','author','domain');
                   7086:     } 
                   7087:     foreach my $chgtype (@posscontexts) {
1.239     raeburn  7088:         my $selstr = '';
                   7089:         if ($curr->{'chgcontext'} eq $chgtype) {
1.301     bisitz   7090:             $selstr = ' selected="selected"';
1.239     raeburn  7091:         }
1.363     raeburn  7092:         if ($context eq 'course') {
1.376     raeburn  7093:             if (($chgtype eq 'automated') || ($chgtype eq 'updatenow')) {
1.363     raeburn  7094:                 next if (!&Apache::lonnet::auto_run($cnum,$cdom));
                   7095:             }
1.239     raeburn  7096:         }
                   7097:         $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
1.248     raeburn  7098:     }
1.303     bisitz   7099:     $output .= '</select></td>'
                   7100:               .'</tr></table>';
                   7101: 
                   7102:     # Update Display button
                   7103:     $output .= '<p>'
                   7104:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   7105:               .'</p>';
                   7106: 
                   7107:     # Server version info
1.363     raeburn  7108:     my $needsrev = '2.11.0';
                   7109:     if ($context eq 'course') {
                   7110:         $needsrev = '2.7.0';
                   7111:     }
                   7112:     
1.303     bisitz   7113:     $output .= '<p class="LC_info">'
                   7114:               .&mt('Only changes made from servers running LON-CAPA [_1] or later are displayed.'
1.363     raeburn  7115:                   ,$needsrev);
1.248     raeburn  7116:     if ($version) {
1.303     bisitz   7117:         $output .= ' '.&mt('This LON-CAPA server is version [_1]',$version);
                   7118:     }
                   7119:     $output .= '</p><hr />';
1.239     raeburn  7120:     return $output;
                   7121: }
                   7122: 
                   7123: sub rolechg_contexts {
1.363     raeburn  7124:     my ($context,$crstype) = @_;
                   7125:     my %lt;
                   7126:     if ($context eq 'course') {
                   7127:         %lt = &Apache::lonlocal::texthash (
1.239     raeburn  7128:                                              any          => 'Any',
1.376     raeburn  7129:                                              automated    => 'Automated Enrollment',
1.239     raeburn  7130:                                              updatenow    => 'Roster Update',
                   7131:                                              createcourse => 'Course Creation',
                   7132:                                              course       => 'User Management in course',
                   7133:                                              domain       => 'User Management in domain',
1.313     raeburn  7134:                                              selfenroll   => 'Self-enrolled',
1.318     raeburn  7135:                                              requestcourses => 'Course Request',
1.239     raeburn  7136:                                          );
1.363     raeburn  7137:         if ($crstype eq 'Community') {
                   7138:             $lt{'createcourse'} = &mt('Community Creation');
                   7139:             $lt{'course'} = &mt('User Management in community');
                   7140:             $lt{'requestcourses'} = &mt('Community Request');
                   7141:         }
                   7142:     } elsif ($context eq 'domain') {
                   7143:         %lt = &Apache::lonlocal::texthash (
                   7144:                                              any           => 'Any',
                   7145:                                              domain        => 'User Management in domain',
                   7146:                                              requestauthor => 'Authoring Request',
                   7147:                                              server        => 'Command line script (DC role)',
                   7148:                                              domconfig     => 'Self-enrolled',
                   7149:                                          );
                   7150:     } else {
                   7151:         %lt = &Apache::lonlocal::texthash (
                   7152:                                              any    => 'Any',
                   7153:                                              domain => 'User Management in domain',
                   7154:                                              author => 'User Management by author',
                   7155:                                          );
                   7156:     } 
1.239     raeburn  7157:     return %lt;
                   7158: }
                   7159: 
1.27      matthew  7160: #-------------------------------------------------- functions for &phase_two
1.160     raeburn  7161: sub user_search_result {
1.221     raeburn  7162:     my ($context,$srch) = @_;
1.160     raeburn  7163:     my %allhomes;
                   7164:     my %inst_matches;
                   7165:     my %srch_results;
1.181     raeburn  7166:     my ($response,$currstate,$forcenewuser,$dirsrchres);
1.183     raeburn  7167:     $srch->{'srchterm'} =~ s/\s+/ /g;
1.176     raeburn  7168:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
1.160     raeburn  7169:         $response = &mt('Invalid search.');
                   7170:     }
                   7171:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
                   7172:         $response = &mt('Invalid search.');
                   7173:     }
1.177     raeburn  7174:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
1.160     raeburn  7175:         $response = &mt('Invalid search.');
                   7176:     }
                   7177:     if ($srch->{'srchterm'} eq '') {
                   7178:         $response = &mt('You must enter a search term.');
                   7179:     }
1.183     raeburn  7180:     if ($srch->{'srchterm'} =~ /^\s+$/) {
                   7181:         $response = &mt('Your search term must contain more than just spaces.');
                   7182:     }
1.160     raeburn  7183:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
                   7184:         if (($srch->{'srchdomain'} eq '') || 
1.163     albertel 7185: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
1.160     raeburn  7186:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
                   7187:         }
                   7188:     }
                   7189:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
                   7190:         ($srch->{'srchin'} eq 'alc')) {
1.176     raeburn  7191:         if ($srch->{'srchby'} eq 'uname') {
1.243     raeburn  7192:             my $unamecheck = $srch->{'srchterm'};
                   7193:             if ($srch->{'srchtype'} eq 'contains') {
                   7194:                 if ($unamecheck !~ /^\w/) {
                   7195:                     $unamecheck = 'a'.$unamecheck; 
                   7196:                 }
                   7197:             }
                   7198:             if ($unamecheck !~ /^$match_username$/) {
1.176     raeburn  7199:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
                   7200:             }
1.160     raeburn  7201:         }
                   7202:     }
1.180     raeburn  7203:     if ($response ne '') {
1.413     raeburn  7204:         $response = '<span class="LC_warning">'.$response.'</span><br />';
1.180     raeburn  7205:     }
1.160     raeburn  7206:     if ($srch->{'srchin'} eq 'instd') {
1.412     raeburn  7207:         my $instd_chk = &instdirectorysrch_check($srch);
1.160     raeburn  7208:         if ($instd_chk ne 'ok') {
1.412     raeburn  7209:             my $domd_chk = &domdirectorysrch_check($srch);
1.413     raeburn  7210:             $response .= '<span class="LC_warning">'.$instd_chk.'</span><br />';
1.412     raeburn  7211:             if ($domd_chk eq 'ok') {
1.413     raeburn  7212:                 $response .= &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.');
1.412     raeburn  7213:             }
1.415     raeburn  7214:             $response .= '<br />';
1.412     raeburn  7215:         }
                   7216:     } else {
1.417     raeburn  7217:         unless (($context eq 'requestcrs') && ($srch->{'srchtype'} eq 'exact')) {
1.412     raeburn  7218:             my $domd_chk = &domdirectorysrch_check($srch);
                   7219:             if ($domd_chk ne 'ok') {
                   7220:                 my $instd_chk = &instdirectorysrch_check($srch);
1.413     raeburn  7221:                 $response .= '<span class="LC_warning">'.$domd_chk.'</span><br />';
1.412     raeburn  7222:                 if ($instd_chk eq 'ok') {
1.413     raeburn  7223:                     $response .= &mt('You may want to search in the institutional directory instead of the LON-CAPA domain.');
1.412     raeburn  7224:                 }
1.415     raeburn  7225:                 $response .= '<br />';
1.412     raeburn  7226:             }
1.160     raeburn  7227:         }
                   7228:     }
                   7229:     if ($response ne '') {
1.180     raeburn  7230:         return ($currstate,$response);
1.160     raeburn  7231:     }
                   7232:     if ($srch->{'srchby'} eq 'uname') {
                   7233:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
                   7234:             if ($env{'form.forcenew'}) {
                   7235:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
                   7236:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   7237:                     if ($uhome eq 'no_host') {
                   7238:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.180     raeburn  7239:                         my $showdom = &display_domain_info($env{'request.role.domain'});
                   7240:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
1.160     raeburn  7241:                     } else {
1.179     raeburn  7242:                         $currstate = 'modify';
1.160     raeburn  7243:                     }
                   7244:                 } else {
1.179     raeburn  7245:                     $currstate = 'modify';
1.160     raeburn  7246:                 }
                   7247:             } else {
                   7248:                 if ($srch->{'srchin'} eq 'dom') {
1.162     raeburn  7249:                     if ($srch->{'srchtype'} eq 'exact') {
                   7250:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   7251:                         if ($uhome eq 'no_host') {
1.179     raeburn  7252:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  7253:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  7254:                         } else {
1.179     raeburn  7255:                             $currstate = 'modify';
1.416     raeburn  7256:                             if ($env{'form.action'} eq 'accesslogs') {
                   7257:                                 $currstate = 'activity';
                   7258:                             }
1.310     raeburn  7259:                             my $uname = $srch->{'srchterm'};
                   7260:                             my $udom = $srch->{'srchdomain'};
                   7261:                             $srch_results{$uname.':'.$udom} =
                   7262:                                 { &Apache::lonnet::get('environment',
                   7263:                                                        ['firstname',
                   7264:                                                         'lastname',
                   7265:                                                         'permanentemail'],
                   7266:                                                          $udom,$uname)
                   7267:                                 };
1.162     raeburn  7268:                         }
                   7269:                     } else {
                   7270:                         %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  7271:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  7272:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  7273:                     }
                   7274:                 } else {
1.167     albertel 7275:                     my $courseusers = &get_courseusers();
1.162     raeburn  7276:                     if ($srch->{'srchtype'} eq 'exact') {
1.167     albertel 7277:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
1.179     raeburn  7278:                             $currstate = 'modify';
1.162     raeburn  7279:                         } else {
1.179     raeburn  7280:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  7281:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  7282:                         }
1.160     raeburn  7283:                     } else {
1.167     albertel 7284:                         foreach my $user (keys(%$courseusers)) {
1.162     raeburn  7285:                             my ($cuname,$cudomain) = split(/:/,$user);
                   7286:                             if ($cudomain eq $srch->{'srchdomain'}) {
1.177     raeburn  7287:                                 my $matched = 0;
                   7288:                                 if ($srch->{'srchtype'} eq 'begins') {
                   7289:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
                   7290:                                         $matched = 1;
                   7291:                                     }
                   7292:                                 } else {
                   7293:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
                   7294:                                         $matched = 1;
                   7295:                                     }
                   7296:                                 }
                   7297:                                 if ($matched) {
1.167     albertel 7298:                                     $srch_results{$user} = 
                   7299: 					{&Apache::lonnet::get('environment',
                   7300: 							     ['firstname',
                   7301: 							      'lastname',
1.194     albertel 7302: 							      'permanentemail'],
                   7303: 							      $cudomain,$cuname)};
1.162     raeburn  7304:                                 }
                   7305:                             }
                   7306:                         }
1.179     raeburn  7307:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  7308:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  7309:                     }
                   7310:                 }
                   7311:             }
                   7312:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  7313:             $currstate = 'query';
1.160     raeburn  7314:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  7315:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
                   7316:             if ($dirsrchres eq 'ok') {
                   7317:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7318:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  7319:             } else {
                   7320:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   7321:                 $response = '<span class="LC_warning">'.
                   7322:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   7323:                     '</span><br />'.
                   7324:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
1.415     raeburn  7325:                     '<br />'; 
1.181     raeburn  7326:             }
1.160     raeburn  7327:         }
                   7328:     } else {
                   7329:         if ($srch->{'srchin'} eq 'dom') {
                   7330:             %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  7331:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7332:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  7333:         } elsif ($srch->{'srchin'} eq 'crs') {
1.167     albertel 7334:             my $courseusers = &get_courseusers(); 
                   7335:             foreach my $user (keys(%$courseusers)) {
1.160     raeburn  7336:                 my ($uname,$udom) = split(/:/,$user);
                   7337:                 my %names = &Apache::loncommon::getnames($uname,$udom);
                   7338:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
                   7339:                 if ($srch->{'srchby'} eq 'lastname') {
                   7340:                     if ((($srch->{'srchtype'} eq 'exact') && 
                   7341:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
1.177     raeburn  7342:                         (($srch->{'srchtype'} eq 'begins') &&
                   7343:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
1.160     raeburn  7344:                         (($srch->{'srchtype'} eq 'contains') &&
                   7345:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
                   7346:                         $srch_results{$user} = {firstname => $names{'firstname'},
                   7347:                                             lastname => $names{'lastname'},
                   7348:                                             permanentemail => $emails{'permanentemail'},
                   7349:                                            };
                   7350:                     }
                   7351:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
                   7352:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
1.177     raeburn  7353:                     $srchlast =~ s/\s+$//;
                   7354:                     $srchfirst =~ s/^\s+//;
1.160     raeburn  7355:                     if ($srch->{'srchtype'} eq 'exact') {
                   7356:                         if (($names{'lastname'} eq $srchlast) &&
                   7357:                             ($names{'firstname'} eq $srchfirst)) {
                   7358:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   7359:                                                 lastname => $names{'lastname'},
                   7360:                                                 permanentemail => $emails{'permanentemail'},
                   7361: 
                   7362:                                            };
                   7363:                         }
1.177     raeburn  7364:                     } elsif ($srch->{'srchtype'} eq 'begins') {
                   7365:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
                   7366:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
                   7367:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   7368:                                                 lastname => $names{'lastname'},
                   7369:                                                 permanentemail => $emails{'permanentemail'},
                   7370:                                                };
                   7371:                         }
                   7372:                     } else {
1.160     raeburn  7373:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
                   7374:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
                   7375:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   7376:                                                 lastname => $names{'lastname'},
                   7377:                                                 permanentemail => $emails{'permanentemail'},
                   7378:                                                };
                   7379:                         }
                   7380:                     }
                   7381:                 }
                   7382:             }
1.179     raeburn  7383:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7384:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  7385:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  7386:             $currstate = 'query';
1.160     raeburn  7387:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  7388:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
                   7389:             if ($dirsrchres eq 'ok') {
                   7390:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7391:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  7392:             } else {
1.412     raeburn  7393:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   7394:                 $response = '<span class="LC_warning">'.
1.181     raeburn  7395:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   7396:                     '</span><br />'.
                   7397:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
1.415     raeburn  7398:                     '<br />';
1.181     raeburn  7399:             }
1.160     raeburn  7400:         }
                   7401:     }
1.179     raeburn  7402:     return ($currstate,$response,$forcenewuser,\%srch_results);
1.160     raeburn  7403: }
                   7404: 
1.412     raeburn  7405: sub domdirectorysrch_check {
                   7406:     my ($srch) = @_;
                   7407:     my $response;
                   7408:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   7409:                                              ['directorysrch'],$srch->{'srchdomain'});
                   7410:     my $showdom = &display_domain_info($srch->{'srchdomain'});
                   7411:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   7412:         if ($dom_inst_srch{'directorysrch'}{'lcavailable'} eq '0') {
                   7413:             return &mt('LON-CAPA directory search is not available in domain: [_1]',$showdom);
                   7414:         }
                   7415:         if ($dom_inst_srch{'directorysrch'}{'lclocalonly'}) {
                   7416:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
                   7417:                 return &mt('LON-CAPA directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom);
                   7418:             }
                   7419:         }
                   7420:     }
                   7421:     return 'ok';
                   7422: }
                   7423: 
                   7424: sub instdirectorysrch_check {
1.160     raeburn  7425:     my ($srch) = @_;
                   7426:     my $can_search = 0;
                   7427:     my $response;
                   7428:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   7429:                                              ['directorysrch'],$srch->{'srchdomain'});
1.180     raeburn  7430:     my $showdom = &display_domain_info($srch->{'srchdomain'});
1.160     raeburn  7431:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   7432:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
1.180     raeburn  7433:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
1.160     raeburn  7434:         }
                   7435:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
                   7436:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
1.180     raeburn  7437:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
1.160     raeburn  7438:             }
                   7439:             my @usertypes = split(/:/,$env{'environment.inststatus'});
                   7440:             if (!@usertypes) {
                   7441:                 push(@usertypes,'default');
                   7442:             }
                   7443:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
                   7444:                 foreach my $type (@usertypes) {
                   7445:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
                   7446:                         $can_search = 1;
                   7447:                         last;
                   7448:                     }
                   7449:                 }
                   7450:             }
                   7451:             if (!$can_search) {
                   7452:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
                   7453:                 my @longtypes; 
                   7454:                 foreach my $item (@usertypes) {
1.229     raeburn  7455:                     if (defined($insttypes->{$item})) { 
                   7456:                         push (@longtypes,$insttypes->{$item});
                   7457:                     } elsif ($item eq 'default') {
                   7458:                         push (@longtypes,&mt('other')); 
                   7459:                     }
1.160     raeburn  7460:                 }
                   7461:                 my $insttype_str = join(', ',@longtypes); 
1.180     raeburn  7462:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
1.229     raeburn  7463:             }
1.160     raeburn  7464:         } else {
                   7465:             $can_search = 1;
                   7466:         }
                   7467:     } else {
1.180     raeburn  7468:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
1.160     raeburn  7469:     }
                   7470:     my %longtext = &Apache::lonlocal::texthash (
1.167     albertel 7471:                        uname     => 'username',
1.160     raeburn  7472:                        lastfirst => 'last name, first name',
1.167     albertel 7473:                        lastname  => 'last name',
1.172     raeburn  7474:                        contains  => 'contains',
1.178     raeburn  7475:                        exact     => 'as exact match to',
                   7476:                        begins    => 'begins with',
1.160     raeburn  7477:                    );
                   7478:     if ($can_search) {
                   7479:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
                   7480:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
1.180     raeburn  7481:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
1.160     raeburn  7482:             }
                   7483:         } else {
1.180     raeburn  7484:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
1.160     raeburn  7485:         }
                   7486:     }
                   7487:     if ($can_search) {
1.178     raeburn  7488:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
                   7489:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
                   7490:                 return 'ok';
                   7491:             } else {
1.180     raeburn  7492:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  7493:             }
                   7494:         } else {
                   7495:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
                   7496:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
                   7497:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
                   7498:                 return 'ok';
                   7499:             } else {
1.180     raeburn  7500:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  7501:             }
1.160     raeburn  7502:         }
                   7503:     }
                   7504: }
                   7505: 
                   7506: sub get_courseusers {
                   7507:     my %advhash;
1.167     albertel 7508:     my $classlist = &Apache::loncoursedata::get_classlist();
1.160     raeburn  7509:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                   7510:     foreach my $role (sort(keys(%coursepersonnel))) {
                   7511:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
1.167     albertel 7512: 	    if (!exists($classlist->{$user})) {
                   7513: 		$classlist->{$user} = [];
                   7514: 	    }
1.160     raeburn  7515:         }
                   7516:     }
1.167     albertel 7517:     return $classlist;
1.160     raeburn  7518: }
                   7519: 
                   7520: sub build_search_response {
1.221     raeburn  7521:     my ($context,$srch,%srch_results) = @_;
1.179     raeburn  7522:     my ($currstate,$response,$forcenewuser);
1.160     raeburn  7523:     my %names = (
1.330     bisitz   7524:           'uname'     => 'username',
                   7525:           'lastname'  => 'last name',
1.160     raeburn  7526:           'lastfirst' => 'last name, first name',
1.330     bisitz   7527:           'crs'       => 'this course',
                   7528:           'dom'       => 'LON-CAPA domain',
                   7529:           'instd'     => 'the institutional directory for domain',
1.160     raeburn  7530:     );
                   7531: 
                   7532:     my %single = (
1.180     raeburn  7533:                    begins   => 'A match',
1.160     raeburn  7534:                    contains => 'A match',
1.180     raeburn  7535:                    exact    => 'An exact match',
1.160     raeburn  7536:                  );
                   7537:     my %nomatch = (
1.180     raeburn  7538:                    begins   => 'No match',
1.160     raeburn  7539:                    contains => 'No match',
1.180     raeburn  7540:                    exact    => 'No exact match',
1.160     raeburn  7541:                   );
                   7542:     if (keys(%srch_results) > 1) {
1.179     raeburn  7543:         $currstate = 'select';
1.160     raeburn  7544:     } else {
                   7545:         if (keys(%srch_results) == 1) {
1.416     raeburn  7546:             if ($env{'form.action'} eq 'accesslogs') {
                   7547:                 $currstate = 'activity';
                   7548:             } else {
                   7549:                 $currstate = 'modify';
                   7550:             }
1.180     raeburn  7551:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
                   7552:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   7553:                 $response .= ': '.&display_domain_info($srch->{'srchdomain'});
1.180     raeburn  7554:             }
1.330     bisitz   7555:         } else { # Search has nothing found. Prepare message to user.
                   7556:             $response = '<span class="LC_warning">';
1.180     raeburn  7557:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   7558:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}: [_2]",
                   7559:                                  '<b>'.$srch->{'srchterm'}.'</b>',
                   7560:                                  &display_domain_info($srch->{'srchdomain'}));
                   7561:             } else {
                   7562:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}.",
                   7563:                                  '<b>'.$srch->{'srchterm'}.'</b>');
1.180     raeburn  7564:             }
                   7565:             $response .= '</span>';
1.330     bisitz   7566: 
1.160     raeburn  7567:             if ($srch->{'srchin'} ne 'alc') {
                   7568:                 $forcenewuser = 1;
                   7569:                 my $cansrchinst = 0; 
                   7570:                 if ($srch->{'srchdomain'}) {
                   7571:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
                   7572:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
                   7573:                         if ($domconfig{'directorysrch'}{'available'}) {
                   7574:                             $cansrchinst = 1;
                   7575:                         } 
                   7576:                     }
                   7577:                 }
1.180     raeburn  7578:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
                   7579:                      ($srch->{'srchby'} eq 'lastname')) &&
                   7580:                     ($srch->{'srchin'} eq 'dom')) {
                   7581:                     if ($cansrchinst) {
                   7582:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
1.160     raeburn  7583:                     }
                   7584:                 }
1.180     raeburn  7585:                 if ($srch->{'srchin'} eq 'crs') {
                   7586:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
                   7587:                 }
                   7588:             }
1.305     raeburn  7589:             my $createdom = $env{'request.role.domain'};
                   7590:             if ($context eq 'requestcrs') {
                   7591:                 if ($env{'form.coursedom'} ne '') {
                   7592:                     $createdom = $env{'form.coursedom'};
                   7593:                 }
                   7594:             }
1.416     raeburn  7595:             unless (($env{'form.action'} eq 'accesslogs') || (($srch->{'srchby'} eq 'uname') && ($srch->{'srchin'} eq 'dom') &&
                   7596:                     ($srch->{'srchtype'} eq 'exact') && ($srch->{'srchdomain'} eq $createdom))) {
1.221     raeburn  7597:                 my $cancreate =
1.305     raeburn  7598:                     &Apache::lonuserutils::can_create_user($createdom,$context);
                   7599:                 my $targetdom = '<span class="LC_cusr_emph">'.$createdom.'</span>';
1.221     raeburn  7600:                 if ($cancreate) {
1.305     raeburn  7601:                     my $showdom = &display_domain_info($createdom); 
1.266     bisitz   7602:                     $response .= '<br /><br />'
                   7603:                                 .'<b>'.&mt('To add a new user:').'</b>'
1.305     raeburn  7604:                                 .'<br />';
                   7605:                     if ($context eq 'requestcrs') {
                   7606:                         $response .= &mt("(You can only define new users in the new course's domain - [_1])",$targetdom);
                   7607:                     } else {
                   7608:                         $response .= &mt("(You can only create new users in your current role's domain - [_1])",$targetdom);
                   7609:                     }
                   7610:                     $response .='<ul><li>'
1.266     bisitz   7611:                                 .&mt("Set 'Domain/institution to search' to: [_1]",'<span class="LC_cusr_emph">'.$showdom.'</span>')
                   7612:                                 .'</li><li>'
                   7613:                                 .&mt("Set 'Search criteria' to: [_1]username is ..... in selected LON-CAPA domain[_2]",'<span class="LC_cusr_emph">','</span>')
                   7614:                                 .'</li><li>'
                   7615:                                 .&mt('Provide the proposed username')
                   7616:                                 .'</li><li>'
                   7617:                                 .&mt("Click 'Search'")
                   7618:                                 .'</li></ul><br />';
1.221     raeburn  7619:                 } else {
1.422     raeburn  7620:                     unless (($context eq 'domain') && ($env{'form.action'} eq 'singleuser')) {
                   7621:                         my $helplink = ' href="javascript:helpMenu('."'display'".')"';
                   7622:                         $response .= '<br /><br />';
                   7623:                         if ($context eq 'requestcrs') {
                   7624:                             $response .= &mt("You are not authorized to define new users in the new course's domain - [_1].",$targetdom);
                   7625:                         } else {
                   7626:                             $response .= &mt("You are not authorized to create new users in your current role's domain - [_1].",$targetdom);
                   7627:                         }
                   7628:                         $response .= '<br />'
                   7629:                                      .&mt('Please contact the [_1]helpdesk[_2] if you need to create a new user.'
                   7630:                                         ,' <a'.$helplink.'>'
                   7631:                                         ,'</a>')
                   7632:                                      .'<br />';
                   7633:                     
1.305     raeburn  7634:                     }
1.221     raeburn  7635:                 }
1.160     raeburn  7636:             }
                   7637:         }
                   7638:     }
1.179     raeburn  7639:     return ($currstate,$response,$forcenewuser);
1.160     raeburn  7640: }
                   7641: 
1.180     raeburn  7642: sub display_domain_info {
                   7643:     my ($dom) = @_;
                   7644:     my $output = $dom;
                   7645:     if ($dom ne '') { 
                   7646:         my $domdesc = &Apache::lonnet::domain($dom,'description');
                   7647:         if ($domdesc ne '') {
                   7648:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
                   7649:         }
                   7650:     }
                   7651:     return $output;
                   7652: }
                   7653: 
1.160     raeburn  7654: sub crumb_utilities {
                   7655:     my %elements = (
                   7656:        crtuser => {
                   7657:            srchterm => 'text',
1.172     raeburn  7658:            srchin => 'selectbox',
1.160     raeburn  7659:            srchby => 'selectbox',
                   7660:            srchtype => 'selectbox',
                   7661:            srchdomain => 'selectbox',
                   7662:        },
1.207     raeburn  7663:        crtusername => {
                   7664:            srchterm => 'text',
                   7665:            srchdomain => 'selectbox',
                   7666:        },
1.160     raeburn  7667:        docustom => {
                   7668:            rolename => 'selectbox',
                   7669:            newrolename => 'textbox',
                   7670:        },
1.179     raeburn  7671:        studentform => {
                   7672:            srchterm => 'text',
                   7673:            srchin => 'selectbox',
                   7674:            srchby => 'selectbox',
                   7675:            srchtype => 'selectbox',
                   7676:            srchdomain => 'selectbox',
                   7677:        },
1.160     raeburn  7678:     );
                   7679: 
                   7680:     my $jsback .= qq|
                   7681: function backPage(formname,prevphase,prevstate) {
1.211     raeburn  7682:     if (typeof prevphase == 'undefined') {
                   7683:         formname.phase.value = '';
                   7684:     }
                   7685:     else {  
                   7686:         formname.phase.value = prevphase;
                   7687:     }
                   7688:     if (typeof prevstate == 'undefined') {
                   7689:         formname.currstate.value = '';
                   7690:     }
                   7691:     else {
                   7692:         formname.currstate.value = prevstate;
                   7693:     }
1.160     raeburn  7694:     formname.submit();
                   7695: }
                   7696: |;
                   7697:     return ($jsback,\%elements);
                   7698: }
                   7699: 
1.26      matthew  7700: sub course_level_table {
1.375     raeburn  7701:     my ($inccourses,$showcredits,$defaultcredits) = @_;
                   7702:     return unless (ref($inccourses) eq 'HASH');
1.26      matthew  7703:     my $table = '';
1.62      www      7704: # Custom Roles?
                   7705: 
1.190     raeburn  7706:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.89      raeburn  7707:     my %lt=&Apache::lonlocal::texthash(
                   7708:             'exs'  => "Existing sections",
                   7709:             'new'  => "Define new section",
                   7710:             'ssd'  => "Set Start Date",
                   7711:             'sed'  => "Set End Date",
1.131     raeburn  7712:             'crl'  => "Course Level",
1.89      raeburn  7713:             'act'  => "Activate",
                   7714:             'rol'  => "Role",
                   7715:             'ext'  => "Extent",
1.113     raeburn  7716:             'grs'  => "Section",
1.375     raeburn  7717:             'crd'  => "Credits",
1.89      raeburn  7718:             'sta'  => "Start",
                   7719:             'end'  => "End"
                   7720:     );
1.62      www      7721: 
1.375     raeburn  7722:     foreach my $protectedcourse (sort(keys(%{$inccourses}))) {
1.135     raeburn  7723: 	my $thiscourse=$protectedcourse;
1.26      matthew  7724: 	$thiscourse=~s:_:/:g;
                   7725: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
1.365     raeburn  7726:         my $isowner = &Apache::lonuserutils::is_courseowner($protectedcourse,$coursedata{'internal.courseowner'});
1.26      matthew  7727: 	my $area=$coursedata{'description'};
1.321     raeburn  7728:         my $crstype=$coursedata{'type'};
1.135     raeburn  7729: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
1.89      raeburn  7730: 	my ($domain,$cnum)=split(/\//,$thiscourse);
1.115     albertel 7731:         my %sections_count;
1.101     albertel 7732:         if (defined($env{'request.course.id'})) {
                   7733:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
1.115     albertel 7734:                 %sections_count = 
                   7735: 		    &Apache::loncommon::get_sections($domain,$cnum);
1.92      raeburn  7736:             }
                   7737:         }
1.321     raeburn  7738:         my @roles = &Apache::lonuserutils::roles_by_context('course','',$crstype);
1.213     raeburn  7739: 	foreach my $role (@roles) {
1.321     raeburn  7740:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.329     raeburn  7741: 	    if ((&Apache::lonnet::allowed('c'.$role,$thiscourse)) ||
                   7742:                 ((($role eq 'cc') || ($role eq 'co')) && ($isowner))) {
1.221     raeburn  7743:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  7744:                                             $plrole,\%sections_count,\%lt,
1.402     raeburn  7745:                                             $showcredits,$defaultcredits,$crstype);
1.221     raeburn  7746:             } elsif ($env{'request.course.sec'} ne '') {
                   7747:                 if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
                   7748:                                              $env{'request.course.sec'})) {
                   7749:                     $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  7750:                                                 $plrole,\%sections_count,\%lt,
1.402     raeburn  7751:                                                 $showcredits,$defaultcredits,$crstype);
1.26      matthew  7752:                 }
                   7753:             }
                   7754:         }
1.221     raeburn  7755:         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
1.324     raeburn  7756:             foreach my $cust (sort(keys(%customroles))) {
                   7757:                 next if ($crstype eq 'Community' && $customroles{$cust} =~ /bre\&S/);
1.221     raeburn  7758:                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
                   7759:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.402     raeburn  7760:                                             $cust,\%sections_count,\%lt,
                   7761:                                             $showcredits,$defaultcredits,$crstype);
1.221     raeburn  7762:             }
1.62      www      7763: 	}
1.26      matthew  7764:     }
                   7765:     return '' if ($table eq ''); # return nothing if there is nothing 
                   7766:                                  # in the table
1.188     raeburn  7767:     my $result;
                   7768:     if (!$env{'request.course.id'}) {
                   7769:         $result = '<h4>'.$lt{'crl'}.'</h4>'."\n";
                   7770:     }
                   7771:     $result .= 
1.136     raeburn  7772: &Apache::loncommon::start_data_table().
                   7773: &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  7774: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
1.402     raeburn  7775: '<th>'.$lt{'ext'}.'</th><th>'."\n";
                   7776:     if ($showcredits) {
                   7777:         $result .= $lt{'crd'}.'</th>';
                   7778:     }
                   7779:     $result .=
1.375     raeburn  7780: '<th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th>'."\n".
                   7781: '<th>'.$lt{'end'}.'</th>'.
1.136     raeburn  7782: &Apache::loncommon::end_data_table_header_row().
                   7783: $table.
                   7784: &Apache::loncommon::end_data_table();
1.26      matthew  7785:     return $result;
                   7786: }
1.88      raeburn  7787: 
1.221     raeburn  7788: sub course_level_row {
1.375     raeburn  7789:     my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,
1.402     raeburn  7790:         $lt,$showcredits,$defaultcredits,$crstype) = @_;
1.375     raeburn  7791:     my $creditem;
1.222     raeburn  7792:     my $row = &Apache::loncommon::start_data_table_row().
                   7793:               ' <td><input type="checkbox" name="act_'.
                   7794:               $protectedcourse.'_'.$role.'" /></td>'."\n".
                   7795:               ' <td>'.$plrole.'</td>'."\n".
                   7796:               ' <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
1.402     raeburn  7797:     if (($showcredits) && ($role eq 'st') && ($crstype eq 'Course')) {
1.375     raeburn  7798:         $row .= 
                   7799:             '<td><input type="text" name="credits_'.$protectedcourse.'_'.
                   7800:             $role.'" size="3" value="'.$defaultcredits.'" /></td>';
                   7801:     } else {
                   7802:         $row .= '<td>&nbsp;</td>';
                   7803:     }
1.322     raeburn  7804:     if (($role eq 'cc') || ($role eq 'co')) {
1.222     raeburn  7805:         $row .= '<td>&nbsp;</td>';
1.221     raeburn  7806:     } elsif ($env{'request.course.sec'} ne '') {
1.222     raeburn  7807:         $row .= ' <td><input type="hidden" value="'.
                   7808:                 $env{'request.course.sec'}.'" '.
                   7809:                 'name="sec_'.$protectedcourse.'_'.$role.'" />'.
                   7810:                 $env{'request.course.sec'}.'</td>';
1.221     raeburn  7811:     } else {
                   7812:         if (ref($sections_count) eq 'HASH') {
                   7813:             my $currsec = 
                   7814:                 &Apache::lonuserutils::course_sections($sections_count,
                   7815:                                                        $protectedcourse.'_'.$role);
1.222     raeburn  7816:             $row .= '<td><table class="LC_createuser">'."\n".
                   7817:                     '<tr class="LC_section_row">'."\n".
                   7818:                     ' <td valign="top">'.$lt->{'exs'}.'<br />'.
                   7819:                        $currsec.'</td>'."\n".
                   7820:                      ' <td>&nbsp;&nbsp;</td>'."\n".
                   7821:                      ' <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
1.221     raeburn  7822:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
                   7823:                      '" value="" />'.
                   7824:                      '<input type="hidden" '.
                   7825:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
1.222     raeburn  7826:                      '</tr></table></td>'."\n";
1.221     raeburn  7827:         } else {
1.222     raeburn  7828:             $row .= '<td><input type="text" size="10" '.
1.375     raeburn  7829:                     'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n";
1.221     raeburn  7830:         }
                   7831:     }
1.222     raeburn  7832:     $row .= <<ENDTIMEENTRY;
                   7833: <td><input type="hidden" name="start_$protectedcourse\_$role" value="" />
1.221     raeburn  7834: <a href=
                   7835: "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  7836: <td><input type="hidden" name="end_$protectedcourse\_$role" value="" />
1.221     raeburn  7837: <a href=
                   7838: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
                   7839: ENDTIMEENTRY
1.222     raeburn  7840:     $row .= &Apache::loncommon::end_data_table_row();
                   7841:     return $row;
1.221     raeburn  7842: }
                   7843: 
1.88      raeburn  7844: sub course_level_dc {
1.375     raeburn  7845:     my ($dcdom,$showcredits) = @_;
1.190     raeburn  7846:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.213     raeburn  7847:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.88      raeburn  7848:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
                   7849:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
1.133     raeburn  7850:                       '<input type="hidden" name="dccourse" value="" />';
1.355     www      7851:     my $courseform=&Apache::loncommon::selectcourse_link
1.356     raeburn  7852:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Select','crstype');
1.375     raeburn  7853:     my $credit_elem;
                   7854:     if ($showcredits) {
                   7855:         $credit_elem = 'credits';
                   7856:     }
                   7857:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu','role','Course/Community Browser',$credit_elem);
1.88      raeburn  7858:     my %lt=&Apache::lonlocal::texthash(
                   7859:                     'rol'  => "Role",
1.113     raeburn  7860:                     'grs'  => "Section",
1.88      raeburn  7861:                     'exs'  => "Existing sections",
                   7862:                     'new'  => "Define new section", 
                   7863:                     'sta'  => "Start",
                   7864:                     'end'  => "End",
                   7865:                     'ssd'  => "Set Start Date",
1.355     www      7866:                     'sed'  => "Set End Date",
1.375     raeburn  7867:                     'scc'  => "Course/Community",
                   7868:                     'crd'  => "Credits",
1.88      raeburn  7869:                   );
1.323     raeburn  7870:     my $header = '<h4>'.&mt('Course/Community Level').'</h4>'.
1.136     raeburn  7871:                  &Apache::loncommon::start_data_table().
                   7872:                  &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  7873:                  '<th>'.$lt{'scc'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
1.397     bisitz   7874:                  '<th>'.$lt{'grs'}.'</th>'."\n";
                   7875:     $header .=   '<th>'.$lt{'crd'}.'</th>'."\n" if ($showcredits);
                   7876:     $header .=   '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'."\n".
1.136     raeburn  7877:                  &Apache::loncommon::end_data_table_header_row();
1.143     raeburn  7878:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
1.356     raeburn  7879:                      '<td><br /><span class="LC_nobreak"><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc','','','','crstype'".')" />'.
                   7880:                      $courseform.('&nbsp;' x4).'</span></td>'."\n".
1.389     bisitz   7881:                      '<td valign="top"><br /><select name="role">'."\n";
1.213     raeburn  7882:     foreach my $role (@roles) {
1.135     raeburn  7883:         my $plrole=&Apache::lonnet::plaintext($role);
1.389     bisitz   7884:         $otheritems .= '  <option value="'.$role.'">'.$plrole.'</option>';
1.88      raeburn  7885:     }
1.404     raeburn  7886:     if ( keys(%customroles) > 0) {
                   7887:         foreach my $cust (sort(keys(%customroles))) {
1.101     albertel 7888:             my $custrole='cr_cr_'.$env{'user.domain'}.
1.135     raeburn  7889:                     '_'.$env{'user.name'}.'_'.$cust;
1.389     bisitz   7890:             $otheritems .= '  <option value="'.$custrole.'">'.$cust.'</option>';
1.88      raeburn  7891:         }
                   7892:     }
                   7893:     $otheritems .= '</select></td><td>'.
                   7894:                      '<table border="0" cellspacing="0" cellpadding="0">'.
                   7895:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
1.389     bisitz   7896:                      ' <option value="">&lt;--'.&mt('Pick course first').'</option></select></td>'.
1.88      raeburn  7897:                      '<td>&nbsp;&nbsp;</td>'.
                   7898:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
1.113     raeburn  7899:                      '<input type="text" name="newsec" value="" />'.
1.237     raeburn  7900:                      '<input type="hidden" name="section" value="" />'.
1.323     raeburn  7901:                      '<input type="hidden" name="groups" value="" />'.
                   7902:                      '<input type="hidden" name="crstype" value="" /></td>'.
1.375     raeburn  7903:                      '</tr></table></td>'."\n";
                   7904:     if ($showcredits) {
                   7905:         $otheritems .= '<td><br />'."\n".
1.397     bisitz   7906:                        '<input type="text" size="3" name="credits" value="" /></td>'."\n";
1.375     raeburn  7907:     }
1.88      raeburn  7908:     $otheritems .= <<ENDTIMEENTRY;
1.323     raeburn  7909: <td><br /><input type="hidden" name="start" value='' />
1.88      raeburn  7910: <a href=
                   7911: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.323     raeburn  7912: <td><br /><input type="hidden" name="end" value='' />
1.88      raeburn  7913: <a href=
                   7914: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
                   7915: ENDTIMEENTRY
1.136     raeburn  7916:     $otheritems .= &Apache::loncommon::end_data_table_row().
                   7917:                    &Apache::loncommon::end_data_table()."\n";
1.88      raeburn  7918:     return $cb_jscript.$header.$hiddenitems.$otheritems;
                   7919: }
                   7920: 
1.237     raeburn  7921: sub update_selfenroll_config {
1.400     raeburn  7922:     my ($r,$cid,$cdom,$cnum,$context,$crstype,$currsettings) = @_;
1.398     raeburn  7923:     return unless (ref($currsettings) eq 'HASH');
                   7924:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
                   7925:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
1.237     raeburn  7926:     my (%changes,%warning);
1.241     raeburn  7927:     my $curr_types;
1.400     raeburn  7928:     my %noedit;
                   7929:     unless ($context eq 'domain') {
                   7930:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
                   7931:     }
1.237     raeburn  7932:     if (ref($row) eq 'ARRAY') {
                   7933:         foreach my $item (@{$row}) {
1.400     raeburn  7934:             next if ($noedit{$item});
1.237     raeburn  7935:             if ($item eq 'enroll_dates') {
                   7936:                 my (%currenrolldate,%newenrolldate);
                   7937:                 foreach my $type ('start','end') {
1.398     raeburn  7938:                     $currenrolldate{$type} = $currsettings->{'selfenroll_'.$type.'_date'};
1.237     raeburn  7939:                     $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
                   7940:                     if ($newenrolldate{$type} ne $currenrolldate{$type}) {
                   7941:                         $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
                   7942:                     }
                   7943:                 }
                   7944:             } elsif ($item eq 'access_dates') {
                   7945:                 my (%currdate,%newdate);
                   7946:                 foreach my $type ('start','end') {
1.398     raeburn  7947:                     $currdate{$type} = $currsettings->{'selfenroll_'.$type.'_access'};
1.237     raeburn  7948:                     $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
                   7949:                     if ($newdate{$type} ne $currdate{$type}) {
                   7950:                         $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
                   7951:                     }
                   7952:                 }
1.241     raeburn  7953:             } elsif ($item eq 'types') {
1.398     raeburn  7954:                 $curr_types = $currsettings->{'selfenroll_'.$item};
1.241     raeburn  7955:                 if ($env{'form.selfenroll_all'}) {
                   7956:                     if ($curr_types ne '*') {
                   7957:                         $changes{'internal.selfenroll_types'} = '*';
                   7958:                     } else {
                   7959:                         next;
                   7960:                     }
                   7961:                 } else {
1.249     raeburn  7962:                     my %currdoms;
1.241     raeburn  7963:                     my @entries = split(/;/,$curr_types);
                   7964:                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
1.249     raeburn  7965:                     my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
1.241     raeburn  7966:                     my $newnum = 0;
1.249     raeburn  7967:                     my @latesttypes;
                   7968:                     foreach my $num (@activations) {
                   7969:                         my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
                   7970:                         if (@types > 0) {
1.241     raeburn  7971:                             @types = sort(@types);
                   7972:                             my $typestr = join(',',@types);
1.249     raeburn  7973:                             my $typedom = $env{'form.selfenroll_dom_'.$num};
                   7974:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   7975:                             $currdoms{$typedom} = 1;
1.241     raeburn  7976:                             $newnum ++;
                   7977:                         }
                   7978:                     }
1.338     raeburn  7979:                     for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {
                   7980:                         if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
1.249     raeburn  7981:                             my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
                   7982:                             if (@types > 0) {
                   7983:                                 @types = sort(@types);
                   7984:                                 my $typestr = join(',',@types);
                   7985:                                 my $typedom = $env{'form.selfenroll_dom_'.$j};
                   7986:                                 $latesttypes[$newnum] = $typedom.':'.$typestr;
                   7987:                                 $currdoms{$typedom} = 1;
                   7988:                                 $newnum ++;
                   7989:                             }
                   7990:                         }
                   7991:                     }
                   7992:                     if ($env{'form.selfenroll_newdom'} ne '') {
                   7993:                         my $typedom = $env{'form.selfenroll_newdom'};
                   7994:                         if ((!defined($currdoms{$typedom})) && 
                   7995:                             (&Apache::lonnet::domain($typedom) ne '')) {
                   7996:                             my $typestr;
                   7997:                             my ($othertitle,$usertypes,$types) = 
                   7998:                                 &Apache::loncommon::sorted_inst_types($typedom);
                   7999:                             my $othervalue = 'any';
                   8000:                             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   8001:                                 if (@{$types} > 0) {
1.257     raeburn  8002:                                     my @esc_types = map { &escape($_); } @{$types};
1.249     raeburn  8003:                                     $othervalue = 'other';
1.258     raeburn  8004:                                     $typestr = join(',',(@esc_types,$othervalue));
1.249     raeburn  8005:                                 }
                   8006:                                 $typestr = $othervalue;
                   8007:                             } else {
                   8008:                                 $typestr = $othervalue;
                   8009:                             } 
                   8010:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   8011:                             $newnum ++ ;
                   8012:                         }
                   8013:                     }
1.241     raeburn  8014:                     my $selfenroll_types = join(';',@latesttypes);
                   8015:                     if ($selfenroll_types ne $curr_types) {
                   8016:                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
                   8017:                     }
                   8018:                 }
1.276     raeburn  8019:             } elsif ($item eq 'limit') {
                   8020:                 my $newlimit = $env{'form.selfenroll_limit'};
                   8021:                 my $newcap = $env{'form.selfenroll_cap'};
                   8022:                 $newcap =~s/\s+//g;
1.398     raeburn  8023:                 my $currlimit =  $currsettings->{'selfenroll_limit'};
1.276     raeburn  8024:                 $currlimit = 'none' if ($currlimit eq '');
1.398     raeburn  8025:                 my $currcap = $currsettings->{'selfenroll_cap'};
1.276     raeburn  8026:                 if ($newlimit ne $currlimit) {
                   8027:                     if ($newlimit ne 'none') {
                   8028:                         if ($newcap =~ /^\d+$/) {
                   8029:                             if ($newcap ne $currcap) {
                   8030:                                 $changes{'internal.selfenroll_cap'} = $newcap;
                   8031:                             }
                   8032:                             $changes{'internal.selfenroll_limit'} = $newlimit;
                   8033:                         } else {
1.398     raeburn  8034:                             $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
                   8035:                                 &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.'); 
1.276     raeburn  8036:                         }
                   8037:                     } elsif ($currcap ne '') {
                   8038:                         $changes{'internal.selfenroll_cap'} = '';
                   8039:                         $changes{'internal.selfenroll_limit'} = $newlimit; 
                   8040:                     }
                   8041:                 } elsif ($currlimit ne 'none') {
                   8042:                     if ($newcap =~ /^\d+$/) {
                   8043:                         if ($newcap ne $currcap) {
                   8044:                             $changes{'internal.selfenroll_cap'} = $newcap;
                   8045:                         }
                   8046:                     } else {
1.398     raeburn  8047:                         $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
                   8048:                             &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.');
1.276     raeburn  8049:                     }
                   8050:                 }
                   8051:             } elsif ($item eq 'approval') {
                   8052:                 my (@currnotified,@newnotified);
1.398     raeburn  8053:                 my $currapproval = $currsettings->{'selfenroll_approval'};
                   8054:                 my $currnotifylist = $currsettings->{'selfenroll_notifylist'};
1.276     raeburn  8055:                 if ($currnotifylist ne '') {
                   8056:                     @currnotified = split(/,/,$currnotifylist);
                   8057:                     @currnotified = sort(@currnotified);
                   8058:                 }
                   8059:                 my $newapproval = $env{'form.selfenroll_approval'};
                   8060:                 @newnotified = &Apache::loncommon::get_env_multiple('form.selfenroll_notify');
                   8061:                 @newnotified = sort(@newnotified);
                   8062:                 if ($newapproval ne $currapproval) {
                   8063:                     $changes{'internal.selfenroll_approval'} = $newapproval;
                   8064:                     if (!$newapproval) {
                   8065:                         if ($currnotifylist ne '') {
                   8066:                             $changes{'internal.selfenroll_notifylist'} = '';
                   8067:                         }
                   8068:                     } else {
                   8069:                         my @differences =  
1.295     raeburn  8070:                             &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  8071:                         if (@differences > 0) {
                   8072:                             if (@newnotified > 0) {
                   8073:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   8074:                             } else {
                   8075:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   8076:                             }
                   8077:                         }
                   8078:                     }
                   8079:                 } else {
1.295     raeburn  8080:                     my @differences = &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  8081:                     if (@differences > 0) {
                   8082:                         if (@newnotified > 0) {
                   8083:                             $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   8084:                         } else {
                   8085:                             $changes{'internal.selfenroll_notifylist'} = '';
                   8086:                         }
                   8087:                     }
                   8088:                 }
1.237     raeburn  8089:             } else {
1.398     raeburn  8090:                 my $curr_val = $currsettings->{'selfenroll_'.$item};
1.237     raeburn  8091:                 my $newval = $env{'form.selfenroll_'.$item};
                   8092:                 if ($item eq 'section') {
                   8093:                     $newval = $env{'form.sections'};
1.241     raeburn  8094:                     if (defined($curr_groups{$newval})) {
1.237     raeburn  8095:                         $newval = $curr_val;
1.398     raeburn  8096:                         $warning{$item} = &mt('Section for self-enrolled users unchanged as the proposed section is a group').'<br />'.
                   8097:                                           &mt('Group names and section names must be distinct');
1.237     raeburn  8098:                     } elsif ($newval eq 'all') {
                   8099:                         $newval = $curr_val;
1.274     bisitz   8100:                         $warning{$item} = &mt('Section for self-enrolled users unchanged, as "all" is a reserved section name.');
1.237     raeburn  8101:                     }
                   8102:                     if ($newval eq '') {
                   8103:                         $newval = 'none';
                   8104:                     }
                   8105:                 }
                   8106:                 if ($newval ne $curr_val) {
                   8107:                     $changes{'internal.selfenroll_'.$item} = $newval;
                   8108:                 }
1.241     raeburn  8109:             }
1.237     raeburn  8110:         }
                   8111:         if (keys(%warning) > 0) {
                   8112:             foreach my $item (@{$row}) {
                   8113:                 if (exists($warning{$item})) {
                   8114:                     $r->print($warning{$item}.'<br />');
                   8115:                 }
                   8116:             } 
                   8117:         }
                   8118:         if (keys(%changes) > 0) {
                   8119:             my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
                   8120:             if ($putresult eq 'ok') {
                   8121:                 if ((exists($changes{'internal.selfenroll_types'})) ||
                   8122:                     (exists($changes{'internal.selfenroll_start_date'}))  ||
                   8123:                     (exists($changes{'internal.selfenroll_end_date'}))) {
                   8124:                     my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
                   8125:                                                                 $cnum,undef,undef,'Course');
                   8126:                     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
1.398     raeburn  8127:                     if (ref($crsinfo{$cid}) eq 'HASH') {
1.237     raeburn  8128:                         foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
                   8129:                             if (exists($changes{'internal.'.$item})) {
1.398     raeburn  8130:                                 $crsinfo{$cid}{$item} = $changes{'internal.'.$item};
1.237     raeburn  8131:                             }
                   8132:                         }
                   8133:                         my $crsputresult =
                   8134:                             &Apache::lonnet::courseidput($cdom,\%crsinfo,
                   8135:                                                          $chome,'notime');
                   8136:                     }
                   8137:                 }
                   8138:                 $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
                   8139:                 foreach my $item (@{$row}) {
                   8140:                     my $title = $item;
                   8141:                     if (ref($lt) eq 'HASH') {
                   8142:                         $title = $lt->{$item};
                   8143:                     }
                   8144:                     if ($item eq 'enroll_dates') {
                   8145:                         foreach my $type ('start','end') {
                   8146:                             if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
                   8147:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
1.244     bisitz   8148:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  8149:                                           $title,$type,$newdate).'</li>');
                   8150:                             }
                   8151:                         }
                   8152:                     } elsif ($item eq 'access_dates') {
                   8153:                         foreach my $type ('start','end') {
                   8154:                             if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
                   8155:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
1.244     bisitz   8156:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  8157:                                           $title,$type,$newdate).'</li>');
                   8158:                             }
                   8159:                         }
1.276     raeburn  8160:                     } elsif ($item eq 'limit') {
                   8161:                         if ((exists($changes{'internal.selfenroll_limit'})) ||
                   8162:                             (exists($changes{'internal.selfenroll_cap'}))) {
                   8163:                             my ($newval,$newcap);
                   8164:                             if ($changes{'internal.selfenroll_cap'} ne '') {
                   8165:                                 $newcap = $changes{'internal.selfenroll_cap'}
                   8166:                             } else {
1.398     raeburn  8167:                                 $newcap = $currsettings->{'selfenroll_cap'};
1.276     raeburn  8168:                             }
                   8169:                             if ($changes{'internal.selfenroll_limit'} eq 'none') {
                   8170:                                 $newval = &mt('No limit');
                   8171:                             } elsif ($changes{'internal.selfenroll_limit'} eq 
                   8172:                                      'allstudents') {
                   8173:                                 $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   8174:                             } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
                   8175:                                 $newval = &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
                   8176:                             } else {
1.398     raeburn  8177:                                 my $currlimit =  $currsettings->{'selfenroll_limit'};
1.276     raeburn  8178:                                 if ($currlimit eq 'allstudents') {
                   8179:                                     $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   8180:                                 } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
1.308     raeburn  8181:                                     $newval =  &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
1.276     raeburn  8182:                                 }
                   8183:                             }
                   8184:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
                   8185:                         }
                   8186:                     } elsif ($item eq 'approval') {
                   8187:                         if ((exists($changes{'internal.selfenroll_approval'})) ||
                   8188:                             (exists($changes{'internal.selfenroll_notifylist'}))) {
1.398     raeburn  8189:                             my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
1.276     raeburn  8190:                             my ($newval,$newnotify);
                   8191:                             if (exists($changes{'internal.selfenroll_notifylist'})) {
                   8192:                                 $newnotify = $changes{'internal.selfenroll_notifylist'};
                   8193:                             } else {   
1.398     raeburn  8194:                                 $newnotify = $currsettings->{'selfenroll_notifylist'};
1.276     raeburn  8195:                             }
1.398     raeburn  8196:                             if (exists($changes{'internal.selfenroll_approval'})) {
                   8197:                                 if ($changes{'internal.selfenroll_approval'} !~ /^[012]$/) {
                   8198:                                     $changes{'internal.selfenroll_approval'} = '0';
                   8199:                                 }
                   8200:                                 $newval = $selfdescs{'approval'}{$changes{'internal.selfenroll_approval'}};
1.276     raeburn  8201:                             } else {
1.398     raeburn  8202:                                 my $currapproval = $currsettings->{'selfenroll_approval'}; 
                   8203:                                 if ($currapproval !~ /^[012]$/) {
                   8204:                                     $currapproval = 0;
1.276     raeburn  8205:                                 }
1.398     raeburn  8206:                                 $newval = $selfdescs{'approval'}{$currapproval};
1.276     raeburn  8207:                             }
                   8208:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval));
                   8209:                             if ($newnotify) {
1.277     raeburn  8210:                                 $r->print('<br />'.&mt('The following will be notified when an enrollment request needs approval, or has been approved: [_1].',$newnotify));
1.276     raeburn  8211:                             } else {
1.277     raeburn  8212:                                 $r->print('<br />'.&mt('No notifications sent when an enrollment request needs approval, or has been approved.'));
1.276     raeburn  8213:                             }
                   8214:                             $r->print('</li>'."\n");
                   8215:                         }
1.237     raeburn  8216:                     } else {
                   8217:                         if (exists($changes{'internal.selfenroll_'.$item})) {
1.241     raeburn  8218:                             my $newval = $changes{'internal.selfenroll_'.$item};
                   8219:                             if ($item eq 'types') {
                   8220:                                 if ($newval eq '') {
                   8221:                                     $newval = &mt('None');
                   8222:                                 } elsif ($newval eq '*') {
                   8223:                                     $newval = &mt('Any user in any domain');
                   8224:                                 }
1.245     raeburn  8225:                             } elsif ($item eq 'registered') {
                   8226:                                 if ($newval eq '1') {
                   8227:                                     $newval = &mt('Yes');
                   8228:                                 } elsif ($newval eq '0') {
                   8229:                                     $newval = &mt('No');
                   8230:                                 }
1.241     raeburn  8231:                             }
1.244     bisitz   8232:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
1.237     raeburn  8233:                         }
                   8234:                     }
                   8235:                 }
                   8236:                 $r->print('</ul>');
1.398     raeburn  8237:                 if ($env{'course.'.$cid.'.description'} ne '') {
                   8238:                     my %newenvhash;
                   8239:                     foreach my $key (keys(%changes)) {
                   8240:                         $newenvhash{'course.'.$cid.'.'.$key} = $changes{$key};
                   8241:                     }
                   8242:                     &Apache::lonnet::appenv(\%newenvhash);
1.237     raeburn  8243:                 }
                   8244:             } else {
1.398     raeburn  8245:                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.
                   8246:                           &mt('The error was: [_1].',$putresult));
1.237     raeburn  8247:             }
                   8248:         } else {
1.249     raeburn  8249:             $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.237     raeburn  8250:         }
                   8251:     } else {
1.249     raeburn  8252:         $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.241     raeburn  8253:     }
1.400     raeburn  8254:     my $visactions = &cat_visibility();
                   8255:     my ($cathash,%cattype);
                   8256:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   8257:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   8258:         $cathash = $domconfig{'coursecategories'}{'cats'};
                   8259:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
                   8260:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
                   8261:     } else {
                   8262:         $cathash = {};
                   8263:         $cattype{'auth'} = 'std';
                   8264:         $cattype{'unauth'} = 'std';
                   8265:     }
                   8266:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
                   8267:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   8268:                   '<br />'.
                   8269:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   8270:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
                   8271:                   '</ul>');
                   8272:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
                   8273:         if ($currsettings->{'uniquecode'}) {
                   8274:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
                   8275:         } else {
1.366     bisitz   8276:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
1.400     raeburn  8277:                   '<br />'.
                   8278:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   8279:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
                   8280:                   '</ul><br />');
                   8281:         }
                   8282:     } else {
                   8283:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
                   8284:         if (ref($visactions) eq 'HASH') {
                   8285:             if (!$visible) {
                   8286:                 $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   8287:                           '<br />');
                   8288:                 if (ref($vismsgs) eq 'ARRAY') {
                   8289:                     $r->print('<br />'.$visactions->{'take'}.'<ul>');
                   8290:                     foreach my $item (@{$vismsgs}) {
                   8291:                         $r->print('<li>'.$visactions->{$item}.'</li>');
                   8292:                     }
                   8293:                     $r->print('</ul>');
1.256     raeburn  8294:                 }
1.400     raeburn  8295:                 $r->print($cansetvis);
1.256     raeburn  8296:             }
                   8297:         }
                   8298:     } 
1.237     raeburn  8299:     return;
                   8300: }
                   8301: 
1.27      matthew  8302: #---------------------------------------------- end functions for &phase_two
1.29      matthew  8303: 
                   8304: #--------------------------------- functions for &phase_two and &phase_three
                   8305: 
                   8306: #--------------------------end of functions for &phase_two and &phase_three
1.372     raeburn  8307: 
1.1       www      8308: 1;
                   8309: __END__
1.2       www      8310: 
                   8311: 

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