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

1.20      harris41    1: # The LearningOnline Network with CAPA
1.1       www         2: # Create a user
                      3: #
1.406.2.8! raeburn     4: # $Id: loncreateuser.pm,v 1.406.2.7 2016/11/13 15:47:04 raeburn Exp $
1.22      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.20      harris41   28: ###
                     29: 
1.1       www        30: package Apache::loncreateuser;
1.66      bowersj2   31: 
                     32: =pod
                     33: 
                     34: =head1 NAME
                     35: 
1.263     jms        36: Apache::loncreateuser.pm
1.66      bowersj2   37: 
                     38: =head1 SYNOPSIS
                     39: 
1.263     jms        40:     Handler to create users and custom roles
                     41: 
                     42:     Provides an Apache handler for creating users,
1.66      bowersj2   43:     editing their login parameters, roles, and removing roles, and
                     44:     also creating and assigning custom roles.
                     45: 
                     46: =head1 OVERVIEW
                     47: 
                     48: =head2 Custom Roles
                     49: 
                     50: In LON-CAPA, roles are actually collections of privileges. "Teaching
                     51: Assistant", "Course Coordinator", and other such roles are really just
                     52: collection of privileges that are useful in many circumstances.
                     53: 
1.324     raeburn    54: Custom roles can be defined by a Domain Coordinator, Course Coordinator
                     55: or Community Coordinator via the Manage User functionality.
                     56: The custom role editor screen will show all privileges which can be
                     57: assigned to users. For a complete list of privileges, please see 
                     58: C</home/httpd/lonTabs/rolesplain.tab>.
1.66      bowersj2   59: 
1.324     raeburn    60: Custom role definitions are stored in the C<roles.db> file of the creator
                     61: of the role.
1.66      bowersj2   62: 
                     63: =cut
1.1       www        64: 
                     65: use strict;
                     66: use Apache::Constants qw(:common :http);
                     67: use Apache::lonnet;
1.54      bowersj2   68: use Apache::loncommon;
1.68      www        69: use Apache::lonlocal;
1.117     raeburn    70: use Apache::longroup;
1.190     raeburn    71: use Apache::lonuserutils;
1.307     raeburn    72: use Apache::loncoursequeueadmin;
1.139     albertel   73: use LONCAPA qw(:DEFAULT :match);
1.1       www        74: 
1.20      harris41   75: my $loginscript; # piece of javascript used in two separate instances
                     76: my $authformnop;
                     77: my $authformkrb;
                     78: my $authformint;
                     79: my $authformfsys;
                     80: my $authformloc;
                     81: 
1.94      matthew    82: sub initialize_authen_forms {
1.227     raeburn    83:     my ($dom,$formname,$curr_authtype,$mode) = @_;
                     84:     my ($krbdef,$krbdefdom) = &Apache::loncommon::get_kerberos_defaults($dom);
                     85:     my %param = ( formname => $formname,
1.187     raeburn    86:                   kerb_def_dom => $krbdefdom,
1.227     raeburn    87:                   kerb_def_auth => $krbdef,
1.187     raeburn    88:                   domain => $dom,
                     89:                 );
1.188     raeburn    90:     my %abv_auth = &auth_abbrev();
1.227     raeburn    91:     if ($curr_authtype =~ /^(krb4|krb5|internal|localauth|unix):(.*)$/) {
1.188     raeburn    92:         my $long_auth = $1;
1.227     raeburn    93:         my $curr_autharg = $2;
1.188     raeburn    94:         my %abv_auth = &auth_abbrev();
                     95:         $param{'curr_authtype'} = $abv_auth{$long_auth};
                     96:         if ($long_auth =~ /^krb(4|5)$/) {
                     97:             $param{'curr_kerb_ver'} = $1;
1.227     raeburn    98:             $param{'curr_autharg'} = $curr_autharg;
1.188     raeburn    99:         }
1.205     raeburn   100:         if ($mode eq 'modifyuser') {
                    101:             $param{'mode'} = $mode;
                    102:         }
1.187     raeburn   103:     }
1.227     raeburn   104:     $loginscript  = &Apache::loncommon::authform_header(%param);
                    105:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
1.31      matthew   106:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
                    107:     $authformint  = &Apache::loncommon::authform_internal(%param);
                    108:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
                    109:     $authformloc  = &Apache::loncommon::authform_local(%param);
1.20      harris41  110: }
                    111: 
1.188     raeburn   112: sub auth_abbrev {
                    113:     my %abv_auth = (
1.368     raeburn   114:                      krb5      => 'krb',
                    115:                      krb4      => 'krb',
                    116:                      internal  => 'int',
                    117:                      localauth => 'loc',
                    118:                      unix      => 'fsys',
1.188     raeburn   119:                    );
                    120:     return %abv_auth;
                    121: }
1.43      www       122: 
1.134     raeburn   123: # ====================================================
                    124: 
1.378     raeburn   125: sub user_quotas {
1.134     raeburn   126:     my ($ccuname,$ccdomain) = @_;
                    127:     my %lt = &Apache::lonlocal::texthash(
1.267     raeburn   128:                    'usrt'      => "User Tools",
                    129:                    'cust'      => "Custom quota",
                    130:                    'chqu'      => "Change quota",
1.134     raeburn   131:     );
1.378     raeburn   132:    
1.149     raeburn   133:     my $quota_javascript = <<"END_SCRIPT";
                    134: <script type="text/javascript">
1.301     bisitz    135: // <![CDATA[
1.378     raeburn   136: function quota_changes(caller,context) {
                    137:     var customoff = document.getElementById('custom_'+context+'quota_off');
                    138:     var customon = document.getElementById('custom_'+context+'quota_on');
                    139:     var number = document.getElementById(context+'quota');
1.149     raeburn   140:     if (caller == "custom") {
1.378     raeburn   141:         if (customoff) {
                    142:             if (customoff.checked) {
                    143:                 number.value = "";
                    144:             }
1.149     raeburn   145:         }
                    146:     }
                    147:     if (caller == "quota") {
1.378     raeburn   148:         if (customon) {
                    149:             customon.checked = true;
                    150:         }
1.149     raeburn   151:     }
1.378     raeburn   152:     return;
1.149     raeburn   153: }
1.301     bisitz    154: // ]]>
1.149     raeburn   155: </script>
                    156: END_SCRIPT
1.378     raeburn   157:     my $longinsttype;
                    158:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
1.267     raeburn   159:     my $output = $quota_javascript."\n".
                    160:                  '<h3>'.$lt{'usrt'}.'</h3>'."\n".
                    161:                  &Apache::loncommon::start_data_table();
                    162: 
1.406.2.6  raeburn   163:     if ((&Apache::lonnet::allowed('mut',$ccdomain)) ||
                    164:         (&Apache::lonnet::allowed('udp',$ccdomain))) {
1.275     raeburn   165:         $output .= &build_tools_display($ccuname,$ccdomain,'tools');
1.267     raeburn   166:     }
1.378     raeburn   167: 
                    168:     my %titles = &Apache::lonlocal::texthash (
                    169:                     portfolio => "Disk space allocated to user's portfolio files",
1.385     bisitz    170:                     author    => "Disk space allocated to user's Authoring Space (if role assigned)",
1.378     raeburn   171:                  );
                    172:     foreach my $name ('portfolio','author') {
                    173:         my ($currquota,$quotatype,$inststatus,$defquota) =
                    174:             &Apache::loncommon::get_user_quota($ccuname,$ccdomain,$name);
                    175:         if ($longinsttype eq '') { 
                    176:             if ($inststatus ne '') {
                    177:                 if ($usertypes->{$inststatus} ne '') {
                    178:                     $longinsttype = $usertypes->{$inststatus};
                    179:                 }
                    180:             }
                    181:         }
                    182:         my ($showquota,$custom_on,$custom_off,$defaultinfo);
                    183:         $custom_on = ' ';
                    184:         $custom_off = ' checked="checked" ';
                    185:         if ($quotatype eq 'custom') {
                    186:             $custom_on = $custom_off;
                    187:             $custom_off = ' ';
                    188:             $showquota = $currquota;
                    189:             if ($longinsttype eq '') {
                    190:                 $defaultinfo = &mt('For this user, the default quota would be [_1]'
1.383     raeburn   191:                               .' MB.',$defquota);
1.378     raeburn   192:             } else {
                    193:                 $defaultinfo = &mt("For this user, the default quota would be [_1]".
1.383     raeburn   194:                                    " MB, as determined by the user's institutional".
1.378     raeburn   195:                                    " affiliation ([_2]).",$defquota,$longinsttype);
                    196:             }
                    197:         } else {
                    198:             if ($longinsttype eq '') {
                    199:                 $defaultinfo = &mt('For this user, the default quota is [_1]'
1.383     raeburn   200:                               .' MB.',$defquota);
1.378     raeburn   201:             } else {
                    202:                 $defaultinfo = &mt("For this user, the default quota of [_1]".
1.383     raeburn   203:                                    " MB, is determined by the user's institutional".
1.378     raeburn   204:                                    " affiliation ([_2]).",$defquota,$longinsttype);
                    205:             }
                    206:         }
                    207: 
                    208:         if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
                    209:             $output .= '<tr class="LC_info_row">'."\n".
                    210:                        '    <td>'.$titles{$name}.'</td>'."\n".
                    211:                        '  </tr>'."\n".
                    212:                        &Apache::loncommon::start_data_table_row()."\n".
1.390     bisitz    213:                        '  <td><span class="LC_nobreak">'.
                    214:                        &mt('Current quota: [_1] MB',$currquota).'</span>&nbsp;&nbsp;'.
1.378     raeburn   215:                        $defaultinfo.'</td>'."\n".
                    216:                        &Apache::loncommon::end_data_table_row()."\n".
                    217:                        &Apache::loncommon::start_data_table_row()."\n".
                    218:                        '  <td><span class="LC_nobreak">'.$lt{'chqu'}.
                    219:                        ': <label>'.
                    220:                        '<input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_off" '.
1.379     raeburn   221:                        'value="0" '.$custom_off.' onchange="javascript:quota_changes('."'custom','$name'".');"'.
1.390     bisitz    222:                        ' /><span class="LC_nobreak">'.
                    223:                        &mt('Default ([_1] MB)',$defquota).'</span></label>&nbsp;'.
1.378     raeburn   224:                        '&nbsp;<label><input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_on" '.
1.379     raeburn   225:                        'value="1" '.$custom_on.'  onchange="javascript:quota_changes('."'custom','$name'".');"'.
1.378     raeburn   226:                        ' />'.$lt{'cust'}.':</label>&nbsp;'.
1.379     raeburn   227:                        '<input type="text" name="'.$name.'quota" id="'.$name.'quota" size ="5" '.
                    228:                        'value="'.$showquota.'" onfocus="javascript:quota_changes('."'quota','$name'".');"'.
1.390     bisitz    229:                        ' />&nbsp;'.&mt('MB').'</span></td>'."\n".
1.378     raeburn   230:                        &Apache::loncommon::end_data_table_row()."\n";
                    231:         }
                    232:     }
1.267     raeburn   233:     $output .= &Apache::loncommon::end_data_table();
1.134     raeburn   234:     return $output;
                    235: }
                    236: 
1.275     raeburn   237: sub build_tools_display {
                    238:     my ($ccuname,$ccdomain,$context) = @_;
1.306     raeburn   239:     my (@usertools,%userenv,$output,@options,%validations,%reqtitles,%reqdisplay,
1.332     raeburn   240:         $colspan,$isadv,%domconfig);
1.275     raeburn   241:     my %lt = &Apache::lonlocal::texthash (
                    242:                    'blog'       => "Personal User Blog",
                    243:                    'aboutme'    => "Personal Information Page",
1.385     bisitz    244:                    'webdav'     => "WebDAV access to Authoring Spaces (if SSL and author/co-author)",
1.275     raeburn   245:                    'portfolio'  => "Personal User Portfolio",
                    246:                    'avai'       => "Available",
                    247:                    'cusa'       => "availability",
                    248:                    'chse'       => "Change setting",
                    249:                    'usde'       => "Use default",
                    250:                    'uscu'       => "Use custom",
                    251:                    'official'   => 'Can request creation of official courses',
1.299     raeburn   252:                    'unofficial' => 'Can request creation of unofficial courses',
                    253:                    'community'  => 'Can request creation of communities',
1.384     raeburn   254:                    'textbook'   => 'Can request creation of textbook courses',
1.362     raeburn   255:                    'requestauthor'  => 'Can request author space',
1.275     raeburn   256:     );
1.279     raeburn   257:     if ($context eq 'requestcourses') {
1.275     raeburn   258:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.299     raeburn   259:                       'requestcourses.official','requestcourses.unofficial',
1.384     raeburn   260:                       'requestcourses.community','requestcourses.textbook');
                    261:         @usertools = ('official','unofficial','community','textbook');
1.309     raeburn   262:         @options =('norequest','approval','autolimit','validate');
1.306     raeburn   263:         %validations = &Apache::lonnet::auto_courserequest_checks($ccdomain);
                    264:         %reqtitles = &courserequest_titles();
                    265:         %reqdisplay = &courserequest_display();
                    266:         $colspan = ' colspan="2"';
1.332     raeburn   267:         %domconfig =
                    268:             &Apache::lonnet::get_dom('configuration',['requestcourses'],$ccdomain);
1.406.2.6  raeburn   269:         $isadv = &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
1.362     raeburn   270:     } elsif ($context eq 'requestauthor') {
                    271:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    272:                                                     'requestauthor');
                    273:         @usertools = ('requestauthor');
                    274:         @options =('norequest','approval','automatic');
                    275:         %reqtitles = &requestauthor_titles();
                    276:         %reqdisplay = &requestauthor_display();
                    277:         $colspan = ' colspan="2"';
                    278:         %domconfig =
                    279:             &Apache::lonnet::get_dom('configuration',['requestauthor'],$ccdomain);
1.275     raeburn   280:     } else {
                    281:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.361     raeburn   282:                           'tools.aboutme','tools.portfolio','tools.blog',
                    283:                           'tools.webdav');
                    284:         @usertools = ('aboutme','blog','webdav','portfolio');
1.275     raeburn   285:     }
                    286:     foreach my $item (@usertools) {
1.306     raeburn   287:         my ($custom_access,$curr_access,$cust_on,$cust_off,$tool_on,$tool_off,
                    288:             $currdisp,$custdisp,$custradio);
1.275     raeburn   289:         $cust_off = 'checked="checked" ';
                    290:         $tool_on = 'checked="checked" ';
                    291:         $curr_access =  
                    292:             &Apache::lonnet::usertools_access($ccuname,$ccdomain,$item,undef,
                    293:                                               $context);
1.362     raeburn   294:         if ($context eq 'requestauthor') {
                    295:             if ($userenv{$context} ne '') {
                    296:                 $cust_on = ' checked="checked" ';
                    297:                 $cust_off = '';
                    298:             }  
                    299:         } elsif ($userenv{$context.'.'.$item} ne '') {
1.306     raeburn   300:             $cust_on = ' checked="checked" ';
                    301:             $cust_off = '';
                    302:         }
                    303:         if ($context eq 'requestcourses') {
                    304:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   305:                 $custom_access = &mt('Currently from default setting.');
1.306     raeburn   306:             } else {
                    307:                 $custom_access = &mt('Currently from custom setting.');
1.275     raeburn   308:             }
1.362     raeburn   309:         } elsif ($context eq 'requestauthor') {
                    310:             if ($userenv{$context} eq '') {
                    311:                 $custom_access = &mt('Currently from default setting.');
                    312:             } else {
                    313:                 $custom_access = &mt('Currently from custom setting.');
                    314:             }
1.275     raeburn   315:         } else {
1.306     raeburn   316:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   317:                 $custom_access =
1.306     raeburn   318:                     &mt('Availability determined currently from default setting.');
                    319:                 if (!$curr_access) {
                    320:                     $tool_off = 'checked="checked" ';
                    321:                     $tool_on = '';
                    322:                 }
                    323:             } else {
1.314     raeburn   324:                 $custom_access =
1.306     raeburn   325:                     &mt('Availability determined currently from custom setting.');
                    326:                 if ($userenv{$context.'.'.$item} == 0) {
                    327:                     $tool_off = 'checked="checked" ';
                    328:                     $tool_on = '';
                    329:                 }
1.275     raeburn   330:             }
                    331:         }
                    332:         $output .= '  <tr class="LC_info_row">'."\n".
1.306     raeburn   333:                    '   <td'.$colspan.'>'.$lt{$item}.'</td>'."\n".
1.275     raeburn   334:                    '  </tr>'."\n".
1.306     raeburn   335:                    &Apache::loncommon::start_data_table_row()."\n";
1.362     raeburn   336:         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.306     raeburn   337:             my ($curroption,$currlimit);
1.362     raeburn   338:             my $envkey = $context.'.'.$item;
                    339:             if ($context eq 'requestauthor') {
                    340:                 $envkey = $context;
                    341:             }
                    342:             if ($userenv{$envkey} ne '') {
                    343:                 $curroption = $userenv{$envkey};
1.332     raeburn   344:             } else {
                    345:                 my (@inststatuses);
1.362     raeburn   346:                 if ($context eq 'requestcourses') {
                    347:                     $curroption =
                    348:                         &Apache::loncoursequeueadmin::get_processtype('course',$ccuname,$ccdomain,
                    349:                                                                       $isadv,$ccdomain,$item,
                    350:                                                                       \@inststatuses,\%domconfig);
                    351:                 } else {
                    352:                      $curroption = 
                    353:                          &Apache::loncoursequeueadmin::get_processtype('requestauthor',$ccuname,$ccdomain,
                    354:                                                                        $isadv,$ccdomain,undef,
                    355:                                                                        \@inststatuses,\%domconfig);
                    356:                 }
1.332     raeburn   357:             }
1.306     raeburn   358:             if (!$curroption) {
                    359:                 $curroption = 'norequest';
                    360:             }
                    361:             if ($curroption =~ /^autolimit=(\d*)$/) {
                    362:                 $currlimit = $1;
1.314     raeburn   363:                 if ($currlimit eq '') {
                    364:                     $currdisp = &mt('Yes, automatic creation');
                    365:                 } else {
                    366:                     $currdisp = &mt('Yes, up to [quant,_1,request]/user',$currlimit);
                    367:                 }
1.306     raeburn   368:             } else {
                    369:                 $currdisp = $reqdisplay{$curroption};
                    370:             }
                    371:             $custdisp = '<table>';
                    372:             foreach my $option (@options) {
                    373:                 my $val = $option;
                    374:                 if ($option eq 'norequest') {
                    375:                     $val = 0;
                    376:                 }
                    377:                 if ($option eq 'validate') {
                    378:                     my $canvalidate = 0;
                    379:                     if (ref($validations{$item}) eq 'HASH') {
                    380:                         if ($validations{$item}{'_custom_'}) {
                    381:                             $canvalidate = 1;
                    382:                         }
                    383:                     }
                    384:                     next if (!$canvalidate);
                    385:                 }
                    386:                 my $checked = '';
                    387:                 if ($option eq $curroption) {
                    388:                     $checked = ' checked="checked"';
                    389:                 } elsif ($option eq 'autolimit') {
                    390:                     if ($curroption =~ /^autolimit/) {
                    391:                         $checked = ' checked="checked"';
                    392:                     }
                    393:                 }
1.362     raeburn   394:                 my $name = 'crsreq_'.$item;
                    395:                 if ($context eq 'requestauthor') {
                    396:                     $name = $item;
                    397:                 }
1.306     raeburn   398:                 $custdisp .= '<tr><td><span class="LC_nobreak"><label>'.
1.362     raeburn   399:                              '<input type="radio" name="'.$name.'" '.
                    400:                              'value="'.$val.'"'.$checked.' />'.
1.306     raeburn   401:                              $reqtitles{$option}.'</label>&nbsp;';
                    402:                 if ($option eq 'autolimit') {
1.362     raeburn   403:                     $custdisp .= '<input type="text" name="'.$name.
                    404:                                  '_limit" size="1" '.
1.314     raeburn   405:                                  'value="'.$currlimit.'" /></span><br />'.
                    406:                                  $reqtitles{'unlimited'};
1.362     raeburn   407:                 } else {
                    408:                     $custdisp .= '</span>';
                    409:                 }
                    410:                 $custdisp .= '</td></tr>';
1.306     raeburn   411:             }
                    412:             $custdisp .= '</table>';
                    413:             $custradio = '</span></td><td>'.&mt('Custom setting').'<br />'.$custdisp;
                    414:         } else {
                    415:             $currdisp = ($curr_access?&mt('Yes'):&mt('No'));
1.362     raeburn   416:             my $name = $context.'_'.$item;
                    417:             if ($context eq 'requestauthor') {
                    418:                 $name = $context;
                    419:             }
1.306     raeburn   420:             $custdisp = '<span class="LC_nobreak"><label>'.
1.362     raeburn   421:                         '<input type="radio" name="'.$name.'"'.
1.361     raeburn   422:                         ' value="1" '.$tool_on.'/>'.&mt('On').'</label>&nbsp;<label>'.
1.362     raeburn   423:                         '<input type="radio" name="'.$name.'" value="0" '.
1.306     raeburn   424:                         $tool_off.'/>'.&mt('Off').'</label></span>';
                    425:             $custradio = ('&nbsp;'x2).'--'.$lt{'cusa'}.':&nbsp;'.$custdisp.
                    426:                           '</span>';
                    427:         }
                    428:         $output .= '  <td'.$colspan.'>'.$custom_access.('&nbsp;'x4).
                    429:                    $lt{'avai'}.': '.$currdisp.'</td>'."\n".
1.406.2.6  raeburn   430:                    &Apache::loncommon::end_data_table_row()."\n";
                    431:         unless (&Apache::lonnet::allowed('udp',$ccdomain)) {
                    432:             $output .=
1.275     raeburn   433:                    &Apache::loncommon::start_data_table_row()."\n".
1.306     raeburn   434:                    '  <td style="vertical-align:top;"><span class="LC_nobreak">'.
                    435:                    $lt{'chse'}.': <label>'.
1.275     raeburn   436:                    '<input type="radio" name="custom'.$item.'" value="0" '.
1.306     raeburn   437:                    $cust_off.'/>'.$lt{'usde'}.'</label>'.('&nbsp;' x3).
                    438:                    '<label><input type="radio" name="custom'.$item.'" value="1" '.
                    439:                    $cust_on.'/>'.$lt{'uscu'}.'</label>'.$custradio.'</td>'.
1.275     raeburn   440:                    &Apache::loncommon::end_data_table_row()."\n";
1.406.2.6  raeburn   441:         }
1.275     raeburn   442:     }
                    443:     return $output;
                    444: }
                    445: 
1.300     raeburn   446: sub coursereq_externaluser {
                    447:     my ($ccuname,$ccdomain,$cdom) = @_;
1.306     raeburn   448:     my (@usertools,@options,%validations,%userenv,$output);
1.300     raeburn   449:     my %lt = &Apache::lonlocal::texthash (
                    450:                    'official'   => 'Can request creation of official courses',
                    451:                    'unofficial' => 'Can request creation of unofficial courses',
                    452:                    'community'  => 'Can request creation of communities',
1.384     raeburn   453:                    'textbook'   => 'Can request creation of textbook courses',
1.300     raeburn   454:     );
                    455: 
                    456:     %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    457:                       'reqcrsotherdom.official','reqcrsotherdom.unofficial',
1.384     raeburn   458:                       'reqcrsotherdom.community','reqcrsotherdom.textbook');
                    459:     @usertools = ('official','unofficial','community','textbook');
1.309     raeburn   460:     @options = ('approval','validate','autolimit');
1.306     raeburn   461:     %validations = &Apache::lonnet::auto_courserequest_checks($cdom);
                    462:     my $optregex = join('|',@options);
                    463:     my %reqtitles = &courserequest_titles();
1.300     raeburn   464:     foreach my $item (@usertools) {
1.306     raeburn   465:         my ($curroption,$currlimit,$tooloff);
1.300     raeburn   466:         if ($userenv{'reqcrsotherdom.'.$item} ne '') {
                    467:             my @curr = split(',',$userenv{'reqcrsotherdom.'.$item});
1.314     raeburn   468:             foreach my $req (@curr) {
                    469:                 if ($req =~ /^\Q$cdom\E\:($optregex)=?(\d*)$/) {
                    470:                     $curroption = $1;
                    471:                     $currlimit = $2;
                    472:                     last;
1.306     raeburn   473:                 }
                    474:             }
1.314     raeburn   475:             if (!$curroption) {
                    476:                 $curroption = 'norequest';
                    477:                 $tooloff = ' checked="checked"';
                    478:             }
1.306     raeburn   479:         } else {
                    480:             $curroption = 'norequest';
                    481:             $tooloff = ' checked="checked"';
                    482:         }
                    483:         $output.= &Apache::loncommon::start_data_table_row()."\n".
1.314     raeburn   484:                   '  <td><span class="LC_nobreak">'.$lt{$item}.': </span></td><td>'.
                    485:                   '<table><tr><td valign="top">'."\n".
1.306     raeburn   486:                   '<label><input type="radio" name="reqcrsotherdom_'.$item.
1.314     raeburn   487:                   '" value=""'.$tooloff.' />'.$reqtitles{'norequest'}.
                    488:                   '</label></td>';
1.306     raeburn   489:         foreach my $option (@options) {
                    490:             if ($option eq 'validate') {
                    491:                 my $canvalidate = 0;
                    492:                 if (ref($validations{$item}) eq 'HASH') {
                    493:                     if ($validations{$item}{'_external_'}) {
                    494:                         $canvalidate = 1;
                    495:                     }
                    496:                 }
                    497:                 next if (!$canvalidate);
                    498:             }
                    499:             my $checked = '';
                    500:             if ($option eq $curroption) {
                    501:                 $checked = ' checked="checked"';
                    502:             }
1.314     raeburn   503:             $output .= '<td valign="top"><span class="LC_nobreak"><label>'.
1.306     raeburn   504:                        '<input type="radio" name="reqcrsotherdom_'.$item.
                    505:                        '" value="'.$option.'"'.$checked.' />'.
1.314     raeburn   506:                        $reqtitles{$option}.'</label>';
1.306     raeburn   507:             if ($option eq 'autolimit') {
1.314     raeburn   508:                 $output .= '&nbsp;<input type="text" name="reqcrsotherdom_'.
1.306     raeburn   509:                            $item.'_limit" size="1" '.
1.314     raeburn   510:                            'value="'.$currlimit.'" /></span>'.
                    511:                            '<br />'.$reqtitles{'unlimited'};
                    512:             } else {
                    513:                 $output .= '</span>';
1.300     raeburn   514:             }
1.314     raeburn   515:             $output .= '</td>';
1.300     raeburn   516:         }
1.314     raeburn   517:         $output .= '</td></tr></table></td>'."\n".
1.300     raeburn   518:                    &Apache::loncommon::end_data_table_row()."\n";
                    519:     }
                    520:     return $output;
                    521: }
                    522: 
1.362     raeburn   523: sub domainrole_req {
                    524:     my ($ccuname,$ccdomain) = @_;
                    525:     return '<br /><h3>'.
                    526:            &mt('User Can Request Assignment of Domain Roles?').
                    527:            '</h3>'."\n".
                    528:            &Apache::loncommon::start_data_table().
                    529:            &build_tools_display($ccuname,$ccdomain,
                    530:                                 'requestauthor').
                    531:            &Apache::loncommon::end_data_table();
                    532: }
                    533: 
1.406.2.5  raeburn   534: sub domadhocroles {
                    535:     my ($ccuname,$ccdomain) = @_;
                    536:     my $confname = &Apache::lonnet::get_domainconfiguser($env{'request.role.domain'});
                    537:     my %existing=&Apache::lonnet::dump('roles',$env{'request.role.domain'},
                    538:                                        $confname,'rolesdef_');
1.406.2.6  raeburn   539:     my ($output,$canmodify);
                    540:     if (&Apache::lonnet::allowed('cdh',$env{'request.role.domain'})) {
                    541:         $canmodify = 1;
                    542:     }
1.406.2.5  raeburn   543:     if (keys(%existing) > 0) {
                    544:         my @current;
                    545:         my $curradhoc = 'adhocroles.'.$env{'request.role.domain'};
                    546:         my %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,$curradhoc);
                    547:         if ($userenv{$curradhoc}) {
                    548:             @current = split(/,/,$userenv{$curradhoc});
                    549:         }
1.406.2.6  raeburn   550:         if (!$canmodify && !@current) {
                    551:             return;
                    552:         }
1.406.2.5  raeburn   553:         my %customroles;
                    554:         foreach my $key (keys(%existing)) {
                    555:             if ($key=~/^rolesdef\_(\w+)$/) {
                    556:                 my $rolename = $1;
                    557:                 my %privs;
                    558:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
                    559:                 $customroles{$rolename} = \%privs;
                    560:             }
                    561:         }
                    562:         $output = '<br /><h3>'.
                    563:                   &mt('Ad Hoc Course Roles Selectable via Helpdesk Role').
                    564:                   '</h3>'."\n".
                    565:                   &Apache::loncommon::start_data_table().
1.406.2.6  raeburn   566:                   &Apache::loncommon::start_data_table_header_row();
                    567:         if ($canmodify) {
                    568:             $output .= '<th>'.&mt('Action').'</th>';
                    569:         }
                    570:         $output .= '<th>'.&mt('Role').'</th>'.
                    571:                    '<th>'.&mt('Privileges in Course').'<th>'.
                    572:                    &Apache::loncommon::end_data_table_header_row();
1.406.2.5  raeburn   573:         foreach my $key (sort(keys(%customroles))) {
1.406.2.6  raeburn   574:             next if ((!$canmodify) && (!grep(/^\Q$key\E$/,@current)));
1.406.2.5  raeburn   575:             $output .= &Apache::loncommon::start_data_table_row();
1.406.2.6  raeburn   576:             if ($canmodify) {
                    577:                 if (grep(/^\Q$key\E$/,@current)) {
                    578:                     $output .= '<td><label>'.
                    579:                                '<input type="checkbox" name="adhocroledel" value="'.$key.'" />'.
                    580:                                &mt('Delete').'</label>'.
                    581:                                '</td>';
                    582:                 } else {
                    583:                     $output .= '<td><label>'.
                    584:                                '<input type="checkbox" name="adhocroleadd" value="'.$key.'" />'.
                    585:                                &mt('Add').'</label>'.
                    586:                                '</td>';
                    587:                 }
1.406.2.5  raeburn   588:             }
                    589:             $output .= '<td>'.$key.'</td><td>';
                    590:             foreach my $level ('course','domain','system') {
                    591:                 if ($customroles{$key}{$level}) {
                    592:                     my $suffix;
                    593:                     if (($level eq 'domain') || ($level eq 'system')) {
                    594:                         $suffix = '&nbsp;('.&mt($level).')';
                    595:                     }
                    596:                     my @privs = split(/:/,$customroles{$key}{$level});
                    597:                     foreach my $item (@privs) {
                    598:                         next if ($item eq '');
                    599:                         my ($priv,$cond) = split(/\&/,$item);
                    600:                         $output .= &Apache::lonnet::plaintext($priv,'Course').$suffix.'<br />';
                    601:                     }
                    602:                 }
                    603:             }
                    604:             $output .= '</td>'.
                    605:                        &Apache::loncommon::end_data_table_row();
                    606:         }
                    607:         $output .= &Apache::loncommon::end_data_table();
                    608:     }
                    609:     return $output;
                    610: }
                    611: 
1.306     raeburn   612: sub courserequest_titles {
                    613:     my %titles = &Apache::lonlocal::texthash (
                    614:                                    official   => 'Official',
                    615:                                    unofficial => 'Unofficial',
                    616:                                    community  => 'Communities',
1.384     raeburn   617:                                    textbook   => 'Textbook',
1.306     raeburn   618:                                    norequest  => 'Not allowed',
1.309     raeburn   619:                                    approval   => 'Approval by Dom. Coord.',
1.306     raeburn   620:                                    validate   => 'With validation',
                    621:                                    autolimit  => 'Numerical limit',
1.314     raeburn   622:                                    unlimited  => '(blank for unlimited)',
1.306     raeburn   623:                  );
                    624:     return %titles;
                    625: }
                    626: 
                    627: sub courserequest_display {
                    628:     my %titles = &Apache::lonlocal::texthash (
1.309     raeburn   629:                                    approval   => 'Yes, need approval',
1.306     raeburn   630:                                    validate   => 'Yes, with validation',
                    631:                                    norequest  => 'No',
                    632:    );
                    633:    return %titles;
                    634: }
                    635: 
1.362     raeburn   636: sub requestauthor_titles {
                    637:     my %titles = &Apache::lonlocal::texthash (
                    638:                                    norequest  => 'Not allowed',
                    639:                                    approval   => 'Approval by Dom. Coord.',
                    640:                                    automatic  => 'Automatic approval',
                    641:                  );
                    642:     return %titles;
                    643: 
                    644: }
                    645: 
                    646: sub requestauthor_display {
                    647:     my %titles = &Apache::lonlocal::texthash (
                    648:                                    approval   => 'Yes, need approval',
                    649:                                    automatic  => 'Yes, automatic approval',
                    650:                                    norequest  => 'No',
                    651:    );
                    652:    return %titles;
                    653: }
                    654: 
1.383     raeburn   655: sub requestchange_display {
                    656:     my %titles = &Apache::lonlocal::texthash (
                    657:                                    approval   => "availability set to 'on' (approval required)", 
                    658:                                    automatic  => "availability set to 'on' (automatic approval)",
                    659:                                    norequest  => "availability set to 'off'",
                    660:    );
                    661:    return %titles;
                    662: }
                    663: 
1.362     raeburn   664: sub curr_requestauthor {
                    665:     my ($uname,$udom,$isadv,$inststatuses,$domconfig) = @_;
                    666:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
                    667:     if ($uname eq '' || $udom eq '') {
                    668:         $uname = $env{'user.name'};
                    669:         $udom = $env{'user.domain'};
                    670:         $isadv = $env{'user.adv'};
                    671:     }
                    672:     my (%userenv,%settings,$val);
                    673:     my @options = ('automatic','approval');
                    674:     %userenv =
                    675:         &Apache::lonnet::userenvironment($udom,$uname,'requestauthor','inststatus');
                    676:     if ($userenv{'requestauthor'}) {
                    677:         $val = $userenv{'requestauthor'};
                    678:         @{$inststatuses} = ('_custom_');
                    679:     } else {
                    680:         my %alltasks;
                    681:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') {
                    682:             %settings = %{$domconfig->{'requestauthor'}};
                    683:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
                    684:                 $val = $settings{'_LC_adv'};
                    685:                 @{$inststatuses} = ('_LC_adv_');
                    686:             } else {
                    687:                 if ($userenv{'inststatus'} ne '') {
                    688:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
                    689:                 } else {
                    690:                     @{$inststatuses} = ('default');
                    691:                 }
                    692:                 foreach my $status (@{$inststatuses}) {
                    693:                     if (exists($settings{$status})) {
                    694:                         my $value = $settings{$status};
                    695:                         next unless ($value);
                    696:                         unless (exists($alltasks{$value})) {
                    697:                             if (ref($alltasks{$value}) eq 'ARRAY') {
                    698:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
                    699:                                     push(@{$alltasks{$value}},$status);
                    700:                                 }
                    701:                             } else {
                    702:                                 @{$alltasks{$value}} = ($status);
                    703:                             }
                    704:                         }
                    705:                     }
                    706:                 }
                    707:                 foreach my $option (@options) {
                    708:                     if ($alltasks{$option}) {
                    709:                         $val = $option;
                    710:                         last;
                    711:                     }
                    712:                 }
                    713:             }
                    714:         }
                    715:     }
                    716:     return $val;
                    717: }
                    718: 
1.2       www       719: # =================================================================== Phase one
1.1       www       720: 
1.42      matthew   721: sub print_username_entry_form {
1.351     raeburn   722:     my ($r,$context,$response,$srch,$forcenewuser,$crstype,$brcrum) = @_;
1.101     albertel  723:     my $defdom=$env{'request.role.domain'};
1.160     raeburn   724:     my $formtoset = 'crtuser';
                    725:     if (exists($env{'form.startrolename'})) {
                    726:         $formtoset = 'docustom';
                    727:         $env{'form.rolename'} = $env{'form.startrolename'};
1.207     raeburn   728:     } elsif ($env{'form.origform'} eq 'crtusername') {
                    729:         $formtoset =  $env{'form.origform'};
1.160     raeburn   730:     }
                    731: 
                    732:     my ($jsback,$elements) = &crumb_utilities();
                    733: 
                    734:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
1.165     albertel  735:         '<script type="text/javascript">'."\n".
1.301     bisitz    736:         '// <![CDATA['."\n".
                    737:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset})."\n".
                    738:         '// ]]>'."\n".
1.162     raeburn   739:         '</script>'."\n";
1.160     raeburn   740: 
1.324     raeburn   741:     my %existingroles=&Apache::lonuserutils::my_custom_roles($crstype);
                    742:     if (($env{'form.action'} eq 'custom') && (keys(%existingroles) > 0)
                    743:         && (&Apache::lonnet::allowed('mcr','/'))) {
                    744:         $jscript .= &customrole_javascript();
                    745:     }
1.224     raeburn   746:     my $helpitem = 'Course_Change_Privileges';
                    747:     if ($env{'form.action'} eq 'custom') {
                    748:         $helpitem = 'Course_Editing_Custom_Roles';
                    749:     } elsif ($env{'form.action'} eq 'singlestudent') {
                    750:         $helpitem = 'Course_Add_Student';
1.406.2.5  raeburn   751:     } elsif ($env{'form.action'} eq 'accesslogs') {
                    752:         $helpitem = 'Domain_User_Access_Logs';
1.224     raeburn   753:     }
1.406.2.7  raeburn   754:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$defdom);
1.351     raeburn   755:     if ($env{'form.action'} eq 'custom') {
                    756:         push(@{$brcrum},
                    757:                  {href=>"javascript:backPage(document.crtuser)",       
                    758:                   text=>"Pick custom role",
                    759:                   help => $helpitem,}
                    760:                  );
                    761:     } else {
                    762:         push (@{$brcrum},
                    763:                   {href => "javascript:backPage(document.crtuser)",
                    764:                    text => $breadcrumb_text{'search'},
                    765:                    help => $helpitem,
                    766:                    faq  => 282,
                    767:                    bug  => 'Instructor Interface',}
                    768:                   );
                    769:     }
                    770:     my %loaditems = (
                    771:                 'onload' => "javascript:setFormElements(document.$formtoset)",
                    772:                     );
                    773:     my $args = {bread_crumbs           => $brcrum,
                    774:                 bread_crumbs_component => 'User Management',
                    775:                 add_entries            => \%loaditems,};
                    776:     $r->print(&Apache::loncommon::start_page('User Management',$jscript,$args));
                    777: 
1.71      sakharuk  778:     my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   779:                     'srst' => 'Search for a user and enroll as a student',
1.318     raeburn   780:                     'srme' => 'Search for a user and enroll as a member',
1.229     raeburn   781:                     'srad' => 'Search for a user and modify/add user information or roles',
1.406.2.7  raeburn   782:                     'srvu' => 'Search for a user and view user information and roles',
1.406.2.5  raeburn   783:                     'srva' => 'Search for a user and view access log information',
1.71      sakharuk  784: 		    'usr'  => "Username",
                    785:                     'dom'  => "Domain",
1.324     raeburn   786:                     'ecrp' => "Define or Edit Custom Role",
                    787:                     'nr'   => "role name",
1.282     schafran  788:                     'cre'  => "Next",
1.71      sakharuk  789: 				       );
1.351     raeburn   790: 
1.214     raeburn   791:     if ($env{'form.action'} eq 'custom') {
1.190     raeburn   792:         if (&Apache::lonnet::allowed('mcr','/')) {
1.324     raeburn   793:             my $newroletext = &mt('Define new custom role:');
                    794:             $r->print('<form action="/adm/createuser" method="post" name="docustom">'.
                    795:                       '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                    796:                       '<input type="hidden" name="phase" value="selected_custom_edit" />'.
                    797:                       '<h3>'.$lt{'ecrp'}.'</h3>'.
                    798:                       &Apache::loncommon::start_data_table().
                    799:                       &Apache::loncommon::start_data_table_row().
                    800:                       '<td>');
                    801:             if (keys(%existingroles) > 0) {
                    802:                 $r->print('<br /><label><input type="radio" name="customroleaction" value="new" checked="checked" onclick="setCustomFields();" /><b>'.$newroletext.'</b></label>');
                    803:             } else {
                    804:                 $r->print('<br /><input type="hidden" name="customroleaction" value="new" /><b>'.$newroletext.'</b>');
                    805:             }
                    806:             $r->print('</td><td align="center">'.$lt{'nr'}.'<br /><input type="text" size="15" name="newrolename" onfocus="setCustomAction('."'new'".');" /></td>'.
                    807:                       &Apache::loncommon::end_data_table_row());
                    808:             if (keys(%existingroles) > 0) {
                    809:                 $r->print(&Apache::loncommon::start_data_table_row().'<td><br />'.
                    810:                           '<label><input type="radio" name="customroleaction" value="edit" onclick="setCustomFields();"/><b>'.
                    811:                           &mt('View/Modify existing role:').'</b></label></td>'.
                    812:                           '<td align="center"><br />'.
                    813:                           '<select name="rolename" onchange="setCustomAction('."'edit'".');">'.
1.326     raeburn   814:                           '<option value="" selected="selected">'.
1.324     raeburn   815:                           &mt('Select'));
                    816:                 foreach my $role (sort(keys(%existingroles))) {
1.326     raeburn   817:                     $r->print('<option value="'.$role.'">'.$role.'</option>');
1.324     raeburn   818:                 }
                    819:                 $r->print('</select>'.
                    820:                           '</td>'.
                    821:                           &Apache::loncommon::end_data_table_row());
                    822:             }
                    823:             $r->print(&Apache::loncommon::end_data_table().'<p>'.
                    824:                       '<input name="customeditor" type="submit" value="'.
                    825:                       $lt{'cre'}.'" /></p>'.
                    826:                       '</form>');
1.190     raeburn   827:         }
1.213     raeburn   828:     } else {
1.229     raeburn   829:         my $actiontext = $lt{'srad'};
1.213     raeburn   830:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   831:             if ($crstype eq 'Community') {
                    832:                 $actiontext = $lt{'srme'};
                    833:             } else {
                    834:                 $actiontext = $lt{'srst'};
                    835:             }
1.406.2.5  raeburn   836:         } elsif ($env{'form.action'} eq 'accesslogs') {
                    837:             $actiontext = $lt{'srva'};
1.406.2.7  raeburn   838:         } elsif (($env{'form.action'} eq 'singleuser') &&
                    839:                  ($context eq 'domain') && (!&Apache::lonnet::allowed('mau',$defdom))) {
                    840:             $actiontext = $lt{'srvu'};
1.213     raeburn   841:         }
1.324     raeburn   842:         $r->print("<h3>$actiontext</h3>");
1.213     raeburn   843:         if ($env{'form.origform'} ne 'crtusername') {
1.406.2.5  raeburn   844:             if ($response) {
                    845:                $r->print("\n<div>$response</div>".
                    846:                          '<br clear="all" />');
                    847:             }
1.213     raeburn   848:         }
1.406.2.5  raeburn   849:         $r->print(&entry_form($defdom,$srch,$forcenewuser,$context,$response,$crstype,1));
1.107     www       850:     }
1.110     albertel  851: }
                    852: 
1.324     raeburn   853: sub customrole_javascript {
                    854:     my $js = <<"END";
                    855: <script type="text/javascript">
                    856: // <![CDATA[
                    857: 
                    858: function setCustomFields() {
                    859:     if (document.docustom.customroleaction.length > 0) {
                    860:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    861:             if (document.docustom.customroleaction[i].checked) {
                    862:                 if (document.docustom.customroleaction[i].value == 'new') {
                    863:                     document.docustom.rolename.selectedIndex = 0;
                    864:                 } else {
                    865:                     document.docustom.newrolename.value = '';
                    866:                 }
                    867:             }
                    868:         }
                    869:     }
                    870:     return;
                    871: }
                    872: 
                    873: function setCustomAction(caller) {
                    874:     if (document.docustom.customroleaction.length > 0) {
                    875:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    876:             if (document.docustom.customroleaction[i].value == caller) {
                    877:                 document.docustom.customroleaction[i].checked = true;
                    878:             }
                    879:         }
                    880:     }
                    881:     setCustomFields();
                    882:     return;
                    883: }
                    884: 
                    885: // ]]>
                    886: </script>
                    887: END
                    888:     return $js;
                    889: }
                    890: 
1.160     raeburn   891: sub entry_form {
1.406.2.5  raeburn   892:     my ($dom,$srch,$forcenewuser,$context,$responsemsg,$crstype,$fixeddom) = @_;
1.229     raeburn   893:     my ($usertype,$inexact);
1.214     raeburn   894:     if (ref($srch) eq 'HASH') {
                    895:         if (($srch->{'srchin'} eq 'dom') &&
                    896:             ($srch->{'srchby'} eq 'uname') &&
                    897:             ($srch->{'srchtype'} eq 'exact') &&
                    898:             ($srch->{'srchdomain'} ne '') &&
                    899:             ($srch->{'srchterm'} ne '')) {
1.353     raeburn   900:             my (%curr_rules,%got_rules);
1.214     raeburn   901:             my ($rules,$ruleorder) =
                    902:                 &Apache::lonnet::inst_userrules($srch->{'srchdomain'},'username');
1.353     raeburn   903:             $usertype = &Apache::lonuserutils::check_usertype($srch->{'srchdomain'},$srch->{'srchterm'},$rules,\%curr_rules,\%got_rules);
1.229     raeburn   904:         } else {
                    905:             $inexact = 1;
1.214     raeburn   906:         }
1.207     raeburn   907:     }
1.214     raeburn   908:     my $cancreate =
                    909:         &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
1.406.2.3  raeburn   910:     my ($userpicker,$cansearch) = 
1.179     raeburn   911:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
1.406.2.5  raeburn   912:                                        'document.crtuser',$cancreate,$usertype,$context,$fixeddom);
1.160     raeburn   913:     my $srchbutton = &mt('Search');
1.229     raeburn   914:     if ($env{'form.action'} eq 'singlestudent') {
                    915:         $srchbutton = &mt('Search and Enroll');
1.406.2.5  raeburn   916:     } elsif ($env{'form.action'} eq 'accesslogs') {
                    917:         $srchbutton = &mt('Search');
1.229     raeburn   918:     } elsif ($cancreate && $responsemsg ne '' && $inexact) {
                    919:         $srchbutton = &mt('Search or Add New User');
                    920:     }
1.406.2.3  raeburn   921:     my $output;
                    922:     if ($cansearch) {
                    923:         $output = <<"ENDBLOCK";
1.160     raeburn   924: <form action="/adm/createuser" method="post" name="crtuser">
1.190     raeburn   925: <input type="hidden" name="action" value="$env{'form.action'}" />
1.160     raeburn   926: <input type="hidden" name="phase" value="get_user_info" />
                    927: $userpicker
1.179     raeburn   928: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
1.160     raeburn   929: </form>
1.207     raeburn   930: ENDBLOCK
1.406.2.3  raeburn   931:     } else {
                    932:         $output = '<p>'.$userpicker.'</p>';
                    933:     }
1.406.2.7  raeburn   934:     if (($env{'form.phase'} eq '') && ($env{'form.action'} ne 'accesslogs') &&
                    935:         (!(($env{'form.action'} eq 'singleuser') && ($context eq 'domain') &&
                    936:         (!&Apache::lonnet::allowed('mau',$env{'request.role.domain'}))))) {
1.207     raeburn   937:         my $defdom=$env{'request.role.domain'};
                    938:         my $domform = &Apache::loncommon::select_dom_form($defdom,'srchdomain');
                    939:         my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   940:                   'enro' => 'Enroll one student',
1.318     raeburn   941:                   'enrm' => 'Enroll one member',
1.229     raeburn   942:                   'admo' => 'Add/modify a single user',
                    943:                   'crea' => 'create new user if required',
                    944:                   'uskn' => "username is known",
1.207     raeburn   945:                   'crnu' => 'Create a new user',
                    946:                   'usr'  => 'Username',
                    947:                   'dom'  => 'in domain',
1.229     raeburn   948:                   'enrl' => 'Enroll',
                    949:                   'cram'  => 'Create/Modify user',
1.207     raeburn   950:         );
1.229     raeburn   951:         my $sellink=&Apache::loncommon::selectstudent_link('crtusername','srchterm','srchdomain');
                    952:         my ($title,$buttontext,$showresponse);
1.318     raeburn   953:         if ($env{'form.action'} eq 'singlestudent') {
                    954:             if ($crstype eq 'Community') {
                    955:                 $title = $lt{'enrm'};
                    956:             } else {
                    957:                 $title = $lt{'enro'};
                    958:             }
1.229     raeburn   959:             $buttontext = $lt{'enrl'};
                    960:         } else {
                    961:             $title = $lt{'admo'};
                    962:             $buttontext = $lt{'cram'};
                    963:         }
                    964:         if ($cancreate) {
                    965:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'crea'}.')</span>';
                    966:         } else {
                    967:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'uskn'}.')</span>';
                    968:         }
                    969:         if ($env{'form.origform'} eq 'crtusername') {
                    970:             $showresponse = $responsemsg;
                    971:         }
1.207     raeburn   972:         $output .= <<"ENDDOCUMENT";
1.229     raeburn   973: <br />
1.207     raeburn   974: <form action="/adm/createuser" method="post" name="crtusername">
                    975: <input type="hidden" name="action" value="$env{'form.action'}" />
                    976: <input type="hidden" name="phase" value="createnewuser" />
                    977: <input type="hidden" name="srchtype" value="exact" />
1.233     raeburn   978: <input type="hidden" name="srchby" value="uname" />
1.207     raeburn   979: <input type="hidden" name="srchin" value="dom" />
                    980: <input type="hidden" name="forcenewuser" value="1" />
                    981: <input type="hidden" name="origform" value="crtusername" />
1.229     raeburn   982: <h3>$title</h3>
                    983: $showresponse
1.207     raeburn   984: <table>
                    985:  <tr>
                    986:   <td>$lt{'usr'}:</td>
                    987:   <td><input type="text" size="15" name="srchterm" /></td>
                    988:   <td>&nbsp;$lt{'dom'}:</td><td>$domform</td>
1.229     raeburn   989:   <td>&nbsp;$sellink&nbsp;</td>
                    990:   <td>&nbsp;<input name="userrole" type="submit" value="$buttontext" /></td>
1.207     raeburn   991:  </tr>
                    992: </table>
                    993: </form>
1.160     raeburn   994: ENDDOCUMENT
1.207     raeburn   995:     }
1.160     raeburn   996:     return $output;
                    997: }
1.110     albertel  998: 
                    999: sub user_modification_js {
1.113     raeburn  1000:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
                   1001:     
1.110     albertel 1002:     return <<END;
                   1003: <script type="text/javascript" language="Javascript">
1.301     bisitz   1004: // <![CDATA[
1.314     raeburn  1005: 
1.110     albertel 1006:     $pjump_def
                   1007:     $dc_setcourse_code
                   1008: 
                   1009:     function dateset() {
                   1010:         eval("document.cu."+document.cu.pres_marker.value+
                   1011:             ".value=document.cu.pres_value.value");
1.359     www      1012:         modalWindow.close();
1.110     albertel 1013:     }
                   1014: 
1.113     raeburn  1015:     $nondc_setsection_code
1.301     bisitz   1016: // ]]>
1.110     albertel 1017: </script>
                   1018: END
1.2       www      1019: }
                   1020: 
                   1021: # =================================================================== Phase two
1.160     raeburn  1022: sub print_user_selection_page {
1.351     raeburn  1023:     my ($r,$response,$srch,$srch_results,$srcharray,$context,$opener_elements,$crstype,$brcrum) = @_;
1.160     raeburn  1024:     my @fields = ('username','domain','lastname','firstname','permanentemail');
                   1025:     my $sortby = $env{'form.sortby'};
                   1026: 
                   1027:     if (!grep(/^\Q$sortby\E$/,@fields)) {
                   1028:         $sortby = 'lastname';
                   1029:     }
                   1030: 
                   1031:     my ($jsback,$elements) = &crumb_utilities();
                   1032: 
                   1033:     my $jscript = (<<ENDSCRIPT);
                   1034: <script type="text/javascript">
1.301     bisitz   1035: // <![CDATA[
1.160     raeburn  1036: function pickuser(uname,udom) {
                   1037:     document.usersrchform.seluname.value=uname;
                   1038:     document.usersrchform.seludom.value=udom;
                   1039:     document.usersrchform.phase.value="userpicked";
                   1040:     document.usersrchform.submit();
                   1041: }
                   1042: 
                   1043: $jsback
1.301     bisitz   1044: // ]]>
1.160     raeburn  1045: </script>
                   1046: ENDSCRIPT
                   1047: 
                   1048:     my %lt=&Apache::lonlocal::texthash(
1.179     raeburn  1049:                                        'usrch'          => "User Search to add/modify roles",
                   1050:                                        'stusrch'        => "User Search to enroll student",
1.318     raeburn  1051:                                        'memsrch'        => "User Search to enroll member",
1.406.2.5  raeburn  1052:                                        'srcva'          => "Search for a user and view access log information",
1.406.2.7  raeburn  1053:                                        'usrvu'          => "User Search to view user roles",
1.179     raeburn  1054:                                        'usel'           => "Select a user to add/modify roles",
1.406.2.7  raeburn  1055:                                        'suvr'           => "Select a user to view roles",
1.318     raeburn  1056:                                        'stusel'         => "Select a user to enroll as a student",
                   1057:                                        'memsel'         => "Select a user to enroll as a member",
1.406.2.5  raeburn  1058:                                        'vacsel'         => "Select a user to view access log",
1.160     raeburn  1059:                                        'username'       => "username",
                   1060:                                        'domain'         => "domain",
                   1061:                                        'lastname'       => "last name",
                   1062:                                        'firstname'      => "first name",
                   1063:                                        'permanentemail' => "permanent e-mail",
                   1064:                                       );
1.302     raeburn  1065:     if ($context eq 'requestcrs') {
                   1066:         $r->print('<div>');
                   1067:     } else {
1.406.2.7  raeburn  1068:         my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$srch->{'srchdomain'});
1.351     raeburn  1069:         my $helpitem;
                   1070:         if ($env{'form.action'} eq 'singleuser') {
                   1071:             $helpitem = 'Course_Change_Privileges';
                   1072:         } elsif ($env{'form.action'} eq 'singlestudent') {
                   1073:             $helpitem = 'Course_Add_Student';
                   1074:         }
                   1075:         push (@{$brcrum},
                   1076:                   {href => "javascript:backPage(document.usersrchform,'','')",
                   1077:                    text => $breadcrumb_text{'search'},
                   1078:                    faq  => 282,
                   1079:                    bug  => 'Instructor Interface',},
                   1080:                   {href => "javascript:backPage(document.usersrchform,'get_user_info','select')",
                   1081:                    text => $breadcrumb_text{'userpicked'},
                   1082:                    faq  => 282,
                   1083:                    bug  => 'Instructor Interface',
                   1084:                    help => $helpitem}
                   1085:                   );
                   1086:         $r->print(&Apache::loncommon::start_page('User Management',$jscript,{bread_crumbs => $brcrum}));
1.302     raeburn  1087:         if ($env{'form.action'} eq 'singleuser') {
1.406.2.7  raeburn  1088:             my $readonly;
                   1089:             if (($context eq 'domain') && (!&Apache::lonnet::allowed('mau',$srch->{'srchdomain'}))) {
                   1090:                 $readonly = 1;
                   1091:                 $r->print("<b>$lt{'usrvu'}</b><br />");
                   1092:             } else {
                   1093:                 $r->print("<b>$lt{'usrch'}</b><br />");
                   1094:             }
1.318     raeburn  1095:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
1.406.2.7  raeburn  1096:             if ($readonly) {
                   1097:                 $r->print('<h3>'.$lt{'suvr'}.'</h3>');
                   1098:             } else {
                   1099:                 $r->print('<h3>'.$lt{'usel'}.'</h3>');
                   1100:             }
1.302     raeburn  1101:         } elsif ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1102:             $r->print($jscript."<b>");
                   1103:             if ($crstype eq 'Community') {
                   1104:                 $r->print($lt{'memsrch'});
                   1105:             } else {
                   1106:                 $r->print($lt{'stusrch'});
                   1107:             }
                   1108:             $r->print("</b><br />");
                   1109:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
                   1110:             $r->print('</form><h3>');
                   1111:             if ($crstype eq 'Community') {
                   1112:                 $r->print($lt{'memsel'});
                   1113:             } else {
                   1114:                 $r->print($lt{'stusel'});
                   1115:             }
                   1116:             $r->print('</h3>');
1.406.2.5  raeburn  1117:         } elsif ($env{'form.action'} eq 'accesslogs') {
                   1118:             $r->print("<b>$lt{'srcva'}</b><br />");
                   1119:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,'accesslogs',undef,undef,1));
                   1120:             $r->print('<h3>'.$lt{'vacsel'}.'</h3>');
1.302     raeburn  1121:         }
1.179     raeburn  1122:     }
1.380     bisitz   1123:     $r->print('<form name="usersrchform" method="post" action="">'.
1.160     raeburn  1124:               &Apache::loncommon::start_data_table()."\n".
                   1125:               &Apache::loncommon::start_data_table_header_row()."\n".
                   1126:               ' <th> </th>'."\n");
                   1127:     foreach my $field (@fields) {
                   1128:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
                   1129:                   "'".$field."'".';document.usersrchform.submit();">'.
                   1130:                   $lt{$field}.'</a></th>'."\n");
                   1131:     }
                   1132:     $r->print(&Apache::loncommon::end_data_table_header_row());
                   1133: 
                   1134:     my @sorted_users = sort {
1.167     albertel 1135:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
1.160     raeburn  1136:             ||
1.167     albertel 1137:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
1.160     raeburn  1138:             ||
                   1139:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
1.167     albertel 1140: 	    ||
                   1141: 	lc($a) cmp lc($b)
1.160     raeburn  1142:         } (keys(%$srch_results));
                   1143: 
                   1144:     foreach my $user (@sorted_users) {
                   1145:         my ($uname,$udom) = split(/:/,$user);
1.302     raeburn  1146:         my $onclick;
                   1147:         if ($context eq 'requestcrs') {
1.314     raeburn  1148:             $onclick =
1.302     raeburn  1149:                 'onclick="javascript:gochoose('."'$uname','$udom',".
                   1150:                                                "'$srch_results->{$user}->{firstname}',".
                   1151:                                                "'$srch_results->{$user}->{lastname}',".
                   1152:                                                "'$srch_results->{$user}->{permanentemail}'".');"';
                   1153:         } else {
1.314     raeburn  1154:             $onclick =
1.302     raeburn  1155:                 ' onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".');"';
                   1156:         }
1.160     raeburn  1157:         $r->print(&Apache::loncommon::start_data_table_row().
1.302     raeburn  1158:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" '.
                   1159:                   $onclick.' /></td>'.
1.160     raeburn  1160:                   '<td><tt>'.$uname.'</tt></td>'.
                   1161:                   '<td><tt>'.$udom.'</tt></td>');
                   1162:         foreach my $field ('lastname','firstname','permanentemail') {
                   1163:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
                   1164:         }
                   1165:         $r->print(&Apache::loncommon::end_data_table_row());
                   1166:     }
                   1167:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
1.179     raeburn  1168:     if (ref($srcharray) eq 'ARRAY') {
                   1169:         foreach my $item (@{$srcharray}) {
                   1170:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
                   1171:         }
                   1172:     }
1.160     raeburn  1173:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
                   1174:               ' <input type="hidden" name="seluname" value="" />'."\n".
                   1175:               ' <input type="hidden" name="seludom" value="" />'."\n".
1.179     raeburn  1176:               ' <input type="hidden" name="currstate" value="select" />'."\n".
1.190     raeburn  1177:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
1.214     raeburn  1178:               ' <input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
1.302     raeburn  1179:     if ($context eq 'requestcrs') {
                   1180:         $r->print($opener_elements.'</form></div>');
                   1181:     } else {
1.351     raeburn  1182:         $r->print($response.'</form>');
1.302     raeburn  1183:     }
1.160     raeburn  1184: }
                   1185: 
                   1186: sub print_user_query_page {
1.351     raeburn  1187:     my ($r,$caller,$brcrum) = @_;
1.160     raeburn  1188: # FIXME - this is for a network-wide name search (similar to catalog search)
                   1189: # To use frames with similar behavior to catalog/portfolio search.
                   1190: # To be implemented. 
                   1191:     return;
                   1192: }
                   1193: 
1.42      matthew  1194: sub print_user_modification_page {
1.375     raeburn  1195:     my ($r,$ccuname,$ccdomain,$srch,$response,$context,$permission,$crstype,
                   1196:         $brcrum,$showcredits) = @_;
1.185     raeburn  1197:     if (($ccuname eq '') || ($ccdomain eq '')) {
1.215     raeburn  1198:         my $usermsg = &mt('No username and/or domain provided.');
                   1199:         $env{'form.phase'} = '';
1.351     raeburn  1200: 	&print_username_entry_form($r,$context,$usermsg,'','',$crstype,$brcrum);
1.58      www      1201:         return;
                   1202:     }
1.213     raeburn  1203:     my ($form,$formname);
                   1204:     if ($env{'form.action'} eq 'singlestudent') {
                   1205:         $form = 'document.enrollstudent';
                   1206:         $formname = 'enrollstudent';
                   1207:     } else {
                   1208:         $form = 'document.cu';
                   1209:         $formname = 'cu';
                   1210:     }
1.188     raeburn  1211:     my %abv_auth = &auth_abbrev();
1.227     raeburn  1212:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
1.185     raeburn  1213:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
                   1214:     if ($uhome eq 'no_host') {
1.215     raeburn  1215:         my $usertype;
                   1216:         my ($rules,$ruleorder) =
                   1217:             &Apache::lonnet::inst_userrules($ccdomain,'username');
                   1218:             $usertype =
1.353     raeburn  1219:                 &Apache::lonuserutils::check_usertype($ccdomain,$ccuname,$rules,
1.362     raeburn  1220:                                                       \%curr_rules,\%got_rules);
1.215     raeburn  1221:         my $cancreate =
                   1222:             &Apache::lonuserutils::can_create_user($ccdomain,$context,
                   1223:                                                    $usertype);
                   1224:         if (!$cancreate) {
1.292     bisitz   1225:             my $helplink = 'javascript:helpMenu('."'display'".')';
1.215     raeburn  1226:             my %usertypetext = (
                   1227:                 official   => 'institutional',
                   1228:                 unofficial => 'non-institutional',
                   1229:             );
                   1230:             my $response;
                   1231:             if ($env{'form.origform'} eq 'crtusername') {
1.362     raeburn  1232:                 $response = '<span class="LC_warning">'.
                   1233:                             &mt('No match found for the username [_1] in LON-CAPA domain: [_2]',
                   1234:                                 '<b>'.$ccuname.'</b>',$ccdomain).
1.215     raeburn  1235:                             '</span><br />';
                   1236:             }
1.292     bisitz   1237:             $response .= '<p class="LC_warning">'
                   1238:                         .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
1.406.2.6  raeburn  1239:                         .' ';
                   1240:             if ($context eq 'domain') {
                   1241:                 $response .= &mt('Please contact a [_1] for assistance.',
                   1242:                                  &Apache::lonnet::plaintext('dc'));
                   1243:             } else {
                   1244:                 $response .= &mt('Please contact the [_1]helpdesk[_2] for assistance.'
                   1245:                                 ,'<a href="'.$helplink.'">','</a>');
                   1246:             }
                   1247:             $response .= '</p><br />';
1.215     raeburn  1248:             $env{'form.phase'} = '';
1.351     raeburn  1249:             &print_username_entry_form($r,$context,$response,undef,undef,$crstype,$brcrum);
1.215     raeburn  1250:             return;
                   1251:         }
1.188     raeburn  1252:         $newuser = 1;
1.193     raeburn  1253:         my $checkhash;
                   1254:         my $checks = { 'username' => 1 };
1.196     raeburn  1255:         $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
1.193     raeburn  1256:         &Apache::loncommon::user_rule_check($checkhash,$checks,
1.196     raeburn  1257:             \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
                   1258:         if (ref($alerts{'username'}) eq 'HASH') {
                   1259:             if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
                   1260:                 my $domdesc =
1.193     raeburn  1261:                     &Apache::lonnet::domain($ccdomain,'description');
1.196     raeburn  1262:                 if ($alerts{'username'}{$ccdomain}{$ccuname}) {
                   1263:                     my $userchkmsg;
                   1264:                     if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
                   1265:                         $userchkmsg = 
                   1266:                             &Apache::loncommon::instrule_disallow_msg('username',
1.193     raeburn  1267:                                                                  $domdesc,1).
                   1268:                         &Apache::loncommon::user_rule_formats($ccdomain,
                   1269:                             $domdesc,$curr_rules{$ccdomain}{'username'},
                   1270:                             'username');
1.196     raeburn  1271:                     }
1.215     raeburn  1272:                     $env{'form.phase'} = '';
1.351     raeburn  1273:                     &print_username_entry_form($r,$context,$userchkmsg,undef,undef,$crstype,$brcrum);
1.196     raeburn  1274:                     return;
1.215     raeburn  1275:                 }
1.193     raeburn  1276:             }
1.185     raeburn  1277:         }
1.187     raeburn  1278:     } else {
1.188     raeburn  1279:         $newuser = 0;
1.185     raeburn  1280:     }
1.160     raeburn  1281:     if ($response) {
1.215     raeburn  1282:         $response = '<br />'.$response;
1.160     raeburn  1283:     }
1.149     raeburn  1284: 
1.52      matthew  1285:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.88      raeburn  1286:     my $dc_setcourse_code = '';
1.119     raeburn  1287:     my $nondc_setsection_code = '';                                        
1.112     albertel 1288:     my %loaditem;
1.114     albertel 1289: 
1.216     raeburn  1290:     my $groupslist = &Apache::lonuserutils::get_groupslist();
1.88      raeburn  1291: 
1.375     raeburn  1292:     my $js = &validation_javascript($context,$ccdomain,$pjump_def,$crstype,
1.216     raeburn  1293:                                $groupslist,$newuser,$formname,\%loaditem);
1.406.2.7  raeburn  1294:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$ccdomain);
1.224     raeburn  1295:     my $helpitem = 'Course_Change_Privileges';
                   1296:     if ($env{'form.action'} eq 'singlestudent') {
                   1297:         $helpitem = 'Course_Add_Student';
                   1298:     }
1.351     raeburn  1299:     push (@{$brcrum},
                   1300:         {href => "javascript:backPage($form)",
                   1301:          text => $breadcrumb_text{'search'},
                   1302:          faq  => 282,
                   1303:          bug  => 'Instructor Interface',});
                   1304:     if ($env{'form.phase'} eq 'userpicked') {
                   1305:        push(@{$brcrum},
                   1306:               {href => "javascript:backPage($form,'get_user_info','select')",
                   1307:                text => $breadcrumb_text{'userpicked'},
                   1308:                faq  => 282,
                   1309:                bug  => 'Instructor Interface',});
                   1310:     }
                   1311:     push(@{$brcrum},
                   1312:             {href => "javascript:backPage($form,'$env{'form.phase'}','modify')",
                   1313:              text => $breadcrumb_text{'modify'},
                   1314:              faq  => 282,
                   1315:              bug  => 'Instructor Interface',
                   1316:              help => $helpitem});
                   1317:     my $args = {'add_entries'           => \%loaditem,
                   1318:                 'bread_crumbs'          => $brcrum,
                   1319:                 'bread_crumbs_component' => 'User Management'};
                   1320:     if ($env{'form.popup'}) {
                   1321:         $args->{'no_nav_bar'} = 1;
                   1322:     }
                   1323:     my $start_page =
                   1324:         &Apache::loncommon::start_page('User Management',$js,$args);
1.3       www      1325: 
1.25      matthew  1326:     my $forminfo =<<"ENDFORMINFO";
1.216     raeburn  1327: <form action="/adm/createuser" method="post" name="$formname">
1.190     raeburn  1328: <input type="hidden" name="phase" value="update_user_data" />
1.188     raeburn  1329: <input type="hidden" name="ccuname" value="$ccuname" />
                   1330: <input type="hidden" name="ccdomain" value="$ccdomain" />
1.157     albertel 1331: <input type="hidden" name="pres_value"  value="" />
                   1332: <input type="hidden" name="pres_type"   value="" />
                   1333: <input type="hidden" name="pres_marker" value="" />
1.25      matthew  1334: ENDFORMINFO
1.375     raeburn  1335:     my (%inccourses,$roledom,$defaultcredits);
1.329     raeburn  1336:     if ($context eq 'course') {
                   1337:         $inccourses{$env{'request.course.id'}}=1;
                   1338:         $roledom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.375     raeburn  1339:         if ($showcredits) {
                   1340:             $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1341:         }
1.329     raeburn  1342:     } elsif ($context eq 'author') {
                   1343:         $roledom = $env{'request.role.domain'};
                   1344:     } elsif ($context eq 'domain') {
                   1345:         foreach my $key (keys(%env)) {
                   1346:             $roledom = $env{'request.role.domain'};
                   1347:             if ($key=~/^user\.priv\.cm\.\/($roledom)\/($match_username)/) {
                   1348:                 $inccourses{$1.'_'.$2}=1;
                   1349:             }
                   1350:         }
                   1351:     } else {
                   1352:         foreach my $key (keys(%env)) {
                   1353: 	    if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
                   1354: 	        $inccourses{$1.'_'.$2}=1;
                   1355:             }
1.2       www      1356:         }
1.24      matthew  1357:     }
1.389     bisitz   1358:     my $title = '';
1.216     raeburn  1359:     if ($newuser) {
1.406.2.5  raeburn  1360:         my ($portfolioform,$domroleform,$adhocroleform);
1.267     raeburn  1361:         if ((&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) ||
                   1362:             (&Apache::lonnet::allowed('mut',$env{'request.role.domain'}))) {
                   1363:             # Current user has quota or user tools modification privileges
1.378     raeburn  1364:             $portfolioform = '<br />'.&user_quotas($ccuname,$ccdomain);
1.134     raeburn  1365:         }
1.383     raeburn  1366:         if ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) &&
                   1367:             ($ccdomain eq $env{'request.role.domain'})) {
1.362     raeburn  1368:             $domroleform = '<br />'.&domainrole_req($ccuname,$ccdomain);
                   1369:         }
1.406.2.5  raeburn  1370:         if (&Apache::lonnet::allowed('cdh',$env{'request.role.domain'})) {
                   1371:             $adhocroleform = &domadhocroles($ccuname,$ccdomain);
                   1372:             if ($adhocroleform) {
                   1373:                 $adhocroleform = '<br />'.$adhocroleform;
                   1374:             }
                   1375:         }
1.227     raeburn  1376:         &initialize_authen_forms($ccdomain,$formname);
1.188     raeburn  1377:         my %lt=&Apache::lonlocal::texthash(
                   1378:                 'lg'             => 'Login Data',
1.190     raeburn  1379:                 'hs'             => "Home Server",
1.188     raeburn  1380:         );
1.185     raeburn  1381: 	$r->print(<<ENDTITLE);
1.110     albertel 1382: $start_page
1.160     raeburn  1383: $response
1.25      matthew  1384: $forminfo
1.31      matthew  1385: <script type="text/javascript" language="Javascript">
1.301     bisitz   1386: // <![CDATA[
1.20      harris41 1387: $loginscript
1.301     bisitz   1388: // ]]>
1.31      matthew  1389: </script>
1.20      harris41 1390: <input type='hidden' name='makeuser' value='1' />
1.185     raeburn  1391: ENDTITLE
1.213     raeburn  1392:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1393:             if ($crstype eq 'Community') {
1.389     bisitz   1394:                 $title = &mt('Create New User [_1] in domain [_2] as a member',
                   1395:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1396:             } else {
1.389     bisitz   1397:                 $title = &mt('Create New User [_1] in domain [_2] as a student',
                   1398:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1399:             }
1.389     bisitz   1400:         } else {
                   1401:                 $title = &mt('Create New User [_1] in domain [_2]',
                   1402:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.213     raeburn  1403:         }
1.389     bisitz   1404:         $r->print('<h2>'.$title.'</h2>'."\n");
                   1405:         $r->print('<div class="LC_left_float">');
1.393     raeburn  1406:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1407:                                          $inst_results{$ccuname.':'.$ccdomain}));
                   1408:         # Option to disable student/employee ID conflict checking not offerred for new users.
1.187     raeburn  1409:         my ($home_server_pick,$numlib) = 
                   1410:             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
                   1411:                                                       'default','hide');
                   1412:         if ($numlib > 1) {
                   1413:             $r->print("
1.185     raeburn  1414: <br />
1.187     raeburn  1415: $lt{'hs'}: $home_server_pick
                   1416: <br />");
                   1417:         } else {
                   1418:             $r->print($home_server_pick);
                   1419:         }
1.304     raeburn  1420:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.362     raeburn  1421:             $r->print('<br /><h3>'.
                   1422:                       &mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.304     raeburn  1423:                       &Apache::loncommon::start_data_table().
                   1424:                       &build_tools_display($ccuname,$ccdomain,
                   1425:                                            'requestcourses').
                   1426:                       &Apache::loncommon::end_data_table());
                   1427:         }
1.188     raeburn  1428:         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                   1429:                   $lt{'lg'}.'</h3>');
1.185     raeburn  1430:         my ($fixedauth,$varauth,$authmsg); 
1.193     raeburn  1431:         if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
                   1432:             my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
                   1433:             my ($rules,$ruleorder) = 
                   1434:                 &Apache::lonnet::inst_userrules($ccdomain,'username');
1.185     raeburn  1435:             if (ref($rules) eq 'HASH') {
1.193     raeburn  1436:                 if (ref($rules->{$matchedrule}) eq 'HASH') {
                   1437:                     my $authtype = $rules->{$matchedrule}{'authtype'};
1.185     raeburn  1438:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
1.190     raeburn  1439:                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.275     raeburn  1440:                     } else { 
1.193     raeburn  1441:                         my $authparm = $rules->{$matchedrule}{'authparm'};
1.273     raeburn  1442:                         $authmsg = $rules->{$matchedrule}{'authmsg'};
1.185     raeburn  1443:                         if ($authtype =~ /^krb(4|5)$/) {
                   1444:                             my $ver = $1;
                   1445:                             if ($authparm ne '') {
                   1446:                                 $fixedauth = <<"KERB"; 
                   1447: <input type="hidden" name="login" value="krb" />
                   1448: <input type="hidden" name="krbver" value="$ver" />
                   1449: <input type="hidden" name="krbarg" value="$authparm" />
                   1450: KERB
                   1451:                             }
                   1452:                         } else {
                   1453:                             $fixedauth = 
                   1454: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
1.193     raeburn  1455:                             if ($rules->{$matchedrule}{'authparmfixed'}) {
1.185     raeburn  1456:                                 $fixedauth .=    
                   1457: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
                   1458:                             } else {
1.273     raeburn  1459:                                 if ($authtype eq 'int') {
                   1460:                                     $varauth = '<br />'.
1.301     bisitz   1461: &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  1462:                                 } elsif ($authtype eq 'loc') {
                   1463:                                     $varauth = '<br />'.
                   1464: &mt('[_1] Local Authentication with argument [_2]','','<input type="text" name="'.$authtype.'arg" value="" />')."\n";
                   1465:                                 } else {
                   1466:                                     $varauth =
1.185     raeburn  1467: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
1.273     raeburn  1468:                                 }
1.185     raeburn  1469:                             }
                   1470:                         }
                   1471:                     }
                   1472:                 } else {
1.190     raeburn  1473:                     $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.185     raeburn  1474:                 }
                   1475:             }
                   1476:             if ($authmsg) {
                   1477:                 $r->print(<<ENDAUTH);
                   1478: $fixedauth
                   1479: $authmsg
                   1480: $varauth
                   1481: ENDAUTH
                   1482:             }
                   1483:         } else {
1.190     raeburn  1484:             $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc)); 
1.187     raeburn  1485:         }
1.406.2.5  raeburn  1486:         $r->print($portfolioform.$domroleform.$adhocroleform);
1.215     raeburn  1487:         if ($env{'form.action'} eq 'singlestudent') {
                   1488:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1489:                                             $permission,$crstype,$ccuname,
                   1490:                                             $ccdomain,$showcredits));
1.215     raeburn  1491:         }
                   1492:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.216     raeburn  1493:     } else { # user already exists
1.389     bisitz   1494: 	$r->print($start_page.$forminfo);
1.213     raeburn  1495:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1496:             if ($crstype eq 'Community') {
1.389     bisitz   1497:                 $title = &mt('Enroll one member: [_1] in domain [_2]',
                   1498:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1499:             } else {
1.389     bisitz   1500:                 $title = &mt('Enroll one student: [_1] in domain [_2]',
                   1501:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1502:             }
1.213     raeburn  1503:         } else {
1.406.2.6  raeburn  1504:             if ($permission->{'cusr'}) {
                   1505:                 $title = &mt('Modify existing user: [_1] in domain [_2]',
                   1506:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
                   1507:             } else {
                   1508:                 $title = &mt('Existing user: [_1] in domain [_2]',
1.389     bisitz   1509:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
1.406.2.6  raeburn  1510:             }
1.213     raeburn  1511:         }
1.389     bisitz   1512:         $r->print('<h2>'.$title.'</h2>'."\n");
                   1513:         $r->print('<div class="LC_left_float">');
1.393     raeburn  1514:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1515:                                          $inst_results{$ccuname.':'.$ccdomain}));
1.406.2.6  raeburn  1516:         if ((&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) ||
                   1517:             (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) {
1.362     raeburn  1518:             $r->print('<br /><h3>'.&mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.300     raeburn  1519:                       &Apache::loncommon::start_data_table());
1.314     raeburn  1520:             if ($env{'request.role.domain'} eq $ccdomain) {
1.300     raeburn  1521:                 $r->print(&build_tools_display($ccuname,$ccdomain,'requestcourses'));
                   1522:             } else {
                   1523:                 $r->print(&coursereq_externaluser($ccuname,$ccdomain,
                   1524:                                                   $env{'request.role.domain'}));
                   1525:             }
                   1526:             $r->print(&Apache::loncommon::end_data_table());
1.275     raeburn  1527:         }
1.199     raeburn  1528:         $r->print('</div>');
1.406.2.5  raeburn  1529:         my @order = ('auth','quota','tools','requestauthor','adhocroles');
1.362     raeburn  1530:         my %user_text;
                   1531:         my ($isadv,$isauthor) = 
1.406.2.6  raeburn  1532:             &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
1.362     raeburn  1533:         if ((!$isauthor) && 
1.406.2.6  raeburn  1534:             ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) ||
                   1535:              (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) &&
                   1536:              ($env{'request.role.domain'} eq $ccdomain)) {
1.362     raeburn  1537:             $user_text{'requestauthor'} = &domainrole_req($ccuname,$ccdomain);
                   1538:         }
1.406.2.6  raeburn  1539:         if ((&Apache::lonnet::allowed('cdh',$env{'request.role.domain'})) ||
                   1540:             (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) {
1.406.2.5  raeburn  1541:             $user_text{'adhocroles'} = &domadhocroles($ccuname,$ccdomain);
                   1542:         }
1.362     raeburn  1543:         $user_text{'auth'} =  &user_authentication($ccuname,$ccdomain,$formname);
1.267     raeburn  1544:         if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
1.406.2.6  raeburn  1545:             (&Apache::lonnet::allowed('mut',$ccdomain)) ||
                   1546:             (&Apache::lonnet::allowed('udp',$ccdomain))) {
1.188     raeburn  1547:             # Current user has quota modification privileges
1.378     raeburn  1548:             $user_text{'quota'} = &user_quotas($ccuname,$ccdomain);
1.267     raeburn  1549:         }
                   1550:         if (!&Apache::lonnet::allowed('mpq',$ccdomain)) {
                   1551:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
                   1552:                 my %lt=&Apache::lonlocal::texthash(
1.385     bisitz   1553:                     'dska'  => "Disk quotas for user's portfolio and Authoring Space",
                   1554:                     'youd'  => "You do not have privileges to modify the portfolio and/or Authoring Space quotas for this user.",
1.267     raeburn  1555:                     'ichr'  => "If a change is required, contact a domain coordinator for the domain",
                   1556:                 );
1.362     raeburn  1557:                 $user_text{'quota'} = <<ENDNOPORTPRIV;
1.188     raeburn  1558: <h3>$lt{'dska'}</h3>
                   1559: $lt{'youd'} $lt{'ichr'}: $ccdomain
                   1560: ENDNOPORTPRIV
1.267     raeburn  1561:             }
                   1562:         }
                   1563:         if (!&Apache::lonnet::allowed('mut',$ccdomain)) {
                   1564:             if (&Apache::lonnet::allowed('mut',$env{'request.role.domain'})) {
                   1565:                 my %lt=&Apache::lonlocal::texthash(
                   1566:                     'utav'  => "User Tools Availability",
1.361     raeburn  1567:                     'yodo'  => "You do not have privileges to modify Portfolio, Blog, WebDAV, or Personal Information Page settings for this user.",
1.267     raeburn  1568:                     'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1569:                 );
1.362     raeburn  1570:                 $user_text{'tools'} = <<ENDNOTOOLSPRIV;
1.267     raeburn  1571: <h3>$lt{'utav'}</h3>
                   1572: $lt{'yodo'} $lt{'ifch'}: $ccdomain
                   1573: ENDNOTOOLSPRIV
                   1574:             }
1.188     raeburn  1575:         }
1.362     raeburn  1576:         my $gotdiv = 0; 
                   1577:         foreach my $item (@order) {
                   1578:             if ($user_text{$item} ne '') {
                   1579:                 unless ($gotdiv) {
                   1580:                     $r->print('<div class="LC_left_float">');
                   1581:                     $gotdiv = 1;
                   1582:                 }
                   1583:                 $r->print('<br />'.$user_text{$item});
                   1584:             }
                   1585:         }
                   1586:         if ($env{'form.action'} eq 'singlestudent') {
                   1587:             unless ($gotdiv) {
                   1588:                 $r->print('<div class="LC_left_float">');
1.213     raeburn  1589:             }
1.375     raeburn  1590:             my $credits;
                   1591:             if ($showcredits) {
                   1592:                 $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1593:                 if ($credits eq '') {
                   1594:                     $credits = $defaultcredits;
                   1595:                 }
                   1596:             }
1.374     raeburn  1597:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1598:                                             $permission,$crstype,$ccuname,
                   1599:                                             $ccdomain,$showcredits));
1.374     raeburn  1600:         }
1.362     raeburn  1601:         if ($gotdiv) {
                   1602:             $r->print('</div><div class="LC_clear_float_footer"></div>');
1.188     raeburn  1603:         }
1.406.2.6  raeburn  1604:         my $statuses;
                   1605:         if (($context eq 'domain') && (&Apache::lonnet::allowed('udp',$ccdomain)) &&
                   1606:             (!&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1607:             $statuses = ['active'];
                   1608:         } elsif (($context eq 'course') && ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
                   1609:                  ($env{'request.course.sec'} &&
                   1610:                   &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'})))) {
                   1611:             $statuses = ['active'];
                   1612:         }
1.217     raeburn  1613:         if ($env{'form.action'} ne 'singlestudent') {
1.329     raeburn  1614:             &display_existing_roles($r,$ccuname,$ccdomain,\%inccourses,$context,
1.406.2.6  raeburn  1615:                                     $roledom,$crstype,$showcredits,$statuses);
1.217     raeburn  1616:         }
1.25      matthew  1617:     } ## End of new user/old user logic
1.218     raeburn  1618:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1619:         my $btntxt;
                   1620:         if ($crstype eq 'Community') {
                   1621:             $btntxt = &mt('Enroll Member');
                   1622:         } else {
                   1623:             $btntxt = &mt('Enroll Student');
                   1624:         }
                   1625:         $r->print('<br /><input type="button" value="'.$btntxt.'" onclick="setSections(this.form)" />'."\n");
1.406.2.6  raeburn  1626:     } elsif ($permission->{'cusr'}) {
1.393     raeburn  1627:         $r->print('<div class="LC_left_float">'.
                   1628:                   '<fieldset><legend>'.&mt('Add Roles').'</legend>');
1.218     raeburn  1629:         my $addrolesdisplay = 0;
                   1630:         if ($context eq 'domain' || $context eq 'author') {
                   1631:             $addrolesdisplay = &new_coauthor_roles($r,$ccuname,$ccdomain);
                   1632:         }
                   1633:         if ($context eq 'domain') {
1.357     raeburn  1634:             my $add_domainroles = &new_domain_roles($r,$ccdomain);
1.218     raeburn  1635:             if (!$addrolesdisplay) {
                   1636:                 $addrolesdisplay = $add_domainroles;
1.2       www      1637:             }
1.375     raeburn  1638:             $r->print(&course_level_dc($env{'request.role.domain'},$showcredits));
1.393     raeburn  1639:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1640:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setCourse()" />'."\n");
1.218     raeburn  1641:         } elsif ($context eq 'author') {
                   1642:             if ($addrolesdisplay) {
1.393     raeburn  1643:                 $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1644:                           '<br /><input type="button" value="'.&mt('Save').'"');
1.218     raeburn  1645:                 if ($newuser) {
1.301     bisitz   1646:                     $r->print(' onclick="auth_check()" \>'."\n");
1.218     raeburn  1647:                 } else {
1.301     bisitz   1648:                     $r->print('onclick="this.form.submit()" \>'."\n");
1.218     raeburn  1649:                 }
1.188     raeburn  1650:             } else {
1.393     raeburn  1651:                 $r->print('</fieldset></div>'.
                   1652:                           '<div class="LC_clear_float_footer"></div>'.
                   1653:                           '<br /><a href="javascript:backPage(document.cu)">'.
1.218     raeburn  1654:                           &mt('Back to previous page').'</a>');
1.188     raeburn  1655:             }
                   1656:         } else {
1.375     raeburn  1657:             $r->print(&course_level_table(\%inccourses,$showcredits,$defaultcredits));
1.393     raeburn  1658:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1659:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setSections(this.form)" />'."\n");
1.188     raeburn  1660:         }
1.88      raeburn  1661:     }
1.188     raeburn  1662:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
1.179     raeburn  1663:     $r->print('<input type="hidden" name="currstate" value="" />');
1.393     raeburn  1664:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" /></form><br /><br />');
1.218     raeburn  1665:     return;
1.2       www      1666: }
1.1       www      1667: 
1.213     raeburn  1668: sub singleuser_breadcrumb {
1.406.2.7  raeburn  1669:     my ($crstype,$context,$domain) = @_;
1.213     raeburn  1670:     my %breadcrumb_text;
                   1671:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1672:         if ($crstype eq 'Community') {
                   1673:             $breadcrumb_text{'search'} = 'Enroll a member';
                   1674:         } else {
                   1675:             $breadcrumb_text{'search'} = 'Enroll a student';
                   1676:         }
1.406.2.7  raeburn  1677:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1678:         $breadcrumb_text{'modify'} = 'Set section/dates';
1.406.2.5  raeburn  1679:     } elsif ($env{'form.action'} eq 'accesslogs') {
                   1680:         $breadcrumb_text{'search'} = 'View access logs for a user';
1.406.2.7  raeburn  1681:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1682:         $breadcrumb_text{'activity'} = 'Activity';
                   1683:     } elsif (($env{'form.action'} eq 'singleuser') && ($context eq 'domain') &&
                   1684:              (!&Apache::lonnet::allowed('mau',$domain))) {
                   1685:         $breadcrumb_text{'search'} = "View user's roles";
                   1686:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1687:         $breadcrumb_text{'modify'} = 'User roles';
1.213     raeburn  1688:     } else {
1.229     raeburn  1689:         $breadcrumb_text{'search'} = 'Create/modify a user';
1.406.2.7  raeburn  1690:         $breadcrumb_text{'userpicked'} = 'Select a user';
                   1691:         $breadcrumb_text{'modify'} = 'Set user role';
1.213     raeburn  1692:     }
                   1693:     return %breadcrumb_text;
                   1694: }
                   1695: 
                   1696: sub date_sections_select {
1.375     raeburn  1697:     my ($context,$newuser,$formname,$permission,$crstype,$ccuname,$ccdomain,
                   1698:         $showcredits) = @_;
                   1699:     my $credits;
                   1700:     if ($showcredits) {
                   1701:         my $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1702:         $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1703:         if ($credits eq '') {
                   1704:             $credits = $defaultcredits;
                   1705:         }
                   1706:     }
1.213     raeburn  1707:     my $cid = $env{'request.course.id'};
                   1708:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity($cid);
                   1709:     my $date_table = '<h3>'.&mt('Starting and Ending Dates').'</h3>'."\n".
                   1710:         &Apache::lonuserutils::date_setting_table(undef,undef,$context,
                   1711:                                                   undef,$formname,$permission);
                   1712:     my $rowtitle = 'Section';
1.375     raeburn  1713:     my $secbox = '<h3>'.&mt('Section and Credits').'</h3>'."\n".
1.213     raeburn  1714:         &Apache::lonuserutils::section_picker($cdom,$cnum,'st',$rowtitle,
1.375     raeburn  1715:                                               $permission,$context,'',$crstype,
                   1716:                                               $showcredits,$credits);
1.213     raeburn  1717:     my $output = $date_table.$secbox;
                   1718:     return $output;
                   1719: }
                   1720: 
1.216     raeburn  1721: sub validation_javascript {
1.375     raeburn  1722:     my ($context,$ccdomain,$pjump_def,$crstype,$groupslist,$newuser,$formname,
1.216     raeburn  1723:         $loaditem) = @_;
                   1724:     my $dc_setcourse_code = '';
                   1725:     my $nondc_setsection_code = '';
                   1726:     if ($context eq 'domain') {
                   1727:         my $dcdom = $env{'request.role.domain'};
                   1728:         $loaditem->{'onload'} = "document.cu.coursedesc.value='';";
1.227     raeburn  1729:         $dc_setcourse_code = 
                   1730:             &Apache::lonuserutils::dc_setcourse_js('cu','singleuser',$context);
1.216     raeburn  1731:     } else {
1.227     raeburn  1732:         my $checkauth; 
                   1733:         if (($newuser) || (&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1734:             $checkauth = 1;
                   1735:         }
                   1736:         if ($context eq 'course') {
                   1737:             $nondc_setsection_code =
                   1738:                 &Apache::lonuserutils::setsections_javascript($formname,$groupslist,
1.375     raeburn  1739:                                                               undef,$checkauth,
                   1740:                                                               $crstype);
1.227     raeburn  1741:         }
                   1742:         if ($checkauth) {
                   1743:             $nondc_setsection_code .= 
                   1744:                 &Apache::lonuserutils::verify_authen($formname,$context);
                   1745:         }
1.216     raeburn  1746:     }
                   1747:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                   1748:                                    $nondc_setsection_code,$groupslist);
                   1749:     my ($jsback,$elements) = &crumb_utilities();
                   1750:     $js .= "\n".
1.301     bisitz   1751:            '<script type="text/javascript">'."\n".
                   1752:            '// <![CDATA['."\n".
                   1753:            $jsback."\n".
                   1754:            '// ]]>'."\n".
                   1755:            '</script>'."\n";
1.216     raeburn  1756:     return $js;
                   1757: }
                   1758: 
1.217     raeburn  1759: sub display_existing_roles {
1.375     raeburn  1760:     my ($r,$ccuname,$ccdomain,$inccourses,$context,$roledom,$crstype,
1.406.2.6  raeburn  1761:         $showcredits,$statuses) = @_;
1.329     raeburn  1762:     my $now=time;
1.406.2.6  raeburn  1763:     my $showall = 1;
                   1764:     my ($showexpired,$showactive);
                   1765:     if ((ref($statuses) eq 'ARRAY') && (@{$statuses} > 0)) {
                   1766:         $showall = 0;
                   1767:         if (grep(/^expired$/,@{$statuses})) {
                   1768:             $showexpired = 1;
                   1769:         }
                   1770:         if (grep(/^active$/,@{$statuses})) {
                   1771:             $showactive = 1;
                   1772:         }
                   1773:         if ($showexpired && $showactive) {
                   1774:             $showall = 1;
                   1775:         }
                   1776:     }
1.329     raeburn  1777:     my %lt=&Apache::lonlocal::texthash(
1.217     raeburn  1778:                     'rer'  => "Existing Roles",
                   1779:                     'rev'  => "Revoke",
                   1780:                     'del'  => "Delete",
                   1781:                     'ren'  => "Re-Enable",
                   1782:                     'rol'  => "Role",
                   1783:                     'ext'  => "Extent",
1.375     raeburn  1784:                     'crd'  => "Credits",
1.217     raeburn  1785:                     'sta'  => "Start",
                   1786:                     'end'  => "End",
                   1787:                                        );
1.329     raeburn  1788:     my (%rolesdump,%roletext,%sortrole,%roleclass,%rolepriv);
                   1789:     if ($context eq 'course' || $context eq 'author') {
                   1790:         my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
                   1791:         my %roleshash = 
                   1792:             &Apache::lonnet::get_my_roles($ccuname,$ccdomain,'userroles',
                   1793:                               ['active','previous','future'],\@roles,$roledom,1);
                   1794:         foreach my $key (keys(%roleshash)) {
                   1795:             my ($start,$end) = split(':',$roleshash{$key});
                   1796:             next if ($start eq '-1' || $end eq '-1');
                   1797:             my ($rnum,$rdom,$role,$sec) = split(':',$key);
                   1798:             if ($context eq 'course') {
                   1799:                 next unless (($rnum eq $env{'course.'.$env{'request.course.id'}.'.num'})
                   1800:                              && ($rdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}));
                   1801:             } elsif ($context eq 'author') {
                   1802:                 next unless (($rnum eq $env{'user.name'}) && ($rdom eq $env{'request.role.domain'}));
                   1803:             }
                   1804:             my ($newkey,$newvalue,$newrole);
                   1805:             $newkey = '/'.$rdom.'/'.$rnum;
                   1806:             if ($sec ne '') {
                   1807:                 $newkey .= '/'.$sec;
                   1808:             }
                   1809:             $newvalue = $role;
                   1810:             if ($role =~ /^cr/) {
                   1811:                 $newrole = 'cr';
                   1812:             } else {
                   1813:                 $newrole = $role;
                   1814:             }
                   1815:             $newkey .= '_'.$newrole;
                   1816:             if ($start ne '' && $end ne '') {
                   1817:                 $newvalue .= '_'.$end.'_'.$start;
1.335     raeburn  1818:             } elsif ($end ne '') {
                   1819:                 $newvalue .= '_'.$end;
1.329     raeburn  1820:             }
                   1821:             $rolesdump{$newkey} = $newvalue;
                   1822:         }
                   1823:     } else {
1.360     raeburn  1824:         %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
1.329     raeburn  1825:     }
                   1826:     # Build up table of user roles to allow revocation and re-enabling of roles.
                   1827:     my ($tmp) = keys(%rolesdump);
                   1828:     return if ($tmp =~ /^(con_lost|error)/i);
                   1829:     foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
                   1830:                                 my $b1=join('_',(split('_',$b))[1,0]);
                   1831:                                 return $a1 cmp $b1;
                   1832:                             } keys(%rolesdump)) {
                   1833:         next if ($area =~ /^rolesdef/);
                   1834:         my $envkey=$area;
                   1835:         my $role = $rolesdump{$area};
                   1836:         my $thisrole=$area;
                   1837:         $area =~ s/\_\w\w$//;
                   1838:         my ($role_code,$role_end_time,$role_start_time) =
                   1839:             split(/_/,$role);
1.406.2.6  raeburn  1840:         my $active=1;
                   1841:         $active=0 if (($role_end_time) && ($now>$role_end_time));
                   1842:         if ($active) {
                   1843:             next unless($showall || $showactive);
                   1844:         } else {
                   1845:             next unless($showall || $showexpired);
                   1846:         }
1.217     raeburn  1847: # Is this a custom role? Get role owner and title.
1.329     raeburn  1848:         my ($croleudom,$croleuname,$croletitle)=
                   1849:             ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
                   1850:         my $allowed=0;
                   1851:         my $delallowed=0;
                   1852:         my $sortkey=$role_code;
                   1853:         my $class='Unknown';
1.375     raeburn  1854:         my $credits='';
1.406.2.6  raeburn  1855:         my $csec;
1.406.2.7  raeburn  1856:         if ($area =~ m{^/($match_domain)/($match_courseid)}) {
1.329     raeburn  1857:             $class='Course';
                   1858:             my ($coursedom,$coursedir) = ($1,$2);
                   1859:             my $cid = $1.'_'.$2;
                   1860:             # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
1.406.2.7  raeburn  1861:             next if ($envkey =~ m{^/$match_domain/$match_courseid/[A-Za-z0-9]+_gr$});
1.329     raeburn  1862:             my %coursedata=
                   1863:                 &Apache::lonnet::coursedescription($cid);
                   1864:             if ($coursedir =~ /^$match_community$/) {
                   1865:                 $class='Community';
                   1866:             }
                   1867:             $sortkey.="\0$coursedom";
                   1868:             my $carea;
                   1869:             if (defined($coursedata{'description'})) {
                   1870:                 $carea=$coursedata{'description'}.
                   1871:                     '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
                   1872:     &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$coursedir,$coursedom);
                   1873:                 $sortkey.="\0".$coursedata{'description'};
                   1874:             } else {
                   1875:                 if ($class eq 'Community') {
                   1876:                     $carea=&mt('Unavailable community').': '.$area;
                   1877:                     $sortkey.="\0".&mt('Unavailable community').': '.$area;
1.217     raeburn  1878:                 } else {
                   1879:                     $carea=&mt('Unavailable course').': '.$area;
                   1880:                     $sortkey.="\0".&mt('Unavailable course').': '.$area;
                   1881:                 }
1.329     raeburn  1882:             }
                   1883:             $sortkey.="\0$coursedir";
                   1884:             $inccourses->{$cid}=1;
1.375     raeburn  1885:             if (($showcredits) && ($class eq 'Course') && ($role_code eq 'st')) {
                   1886:                 my $defaultcredits = $coursedata{'internal.defaultcredits'};
                   1887:                 $credits =
                   1888:                     &get_user_credits($ccuname,$ccdomain,$defaultcredits,
                   1889:                                       $coursedom,$coursedir);
                   1890:                 if ($credits eq '') {
                   1891:                     $credits = $defaultcredits;
                   1892:                 }
                   1893:             }
1.329     raeburn  1894:             if ((&Apache::lonnet::allowed('c'.$role_code,$coursedom.'/'.$coursedir)) ||
                   1895:                 (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1896:                 $allowed=1;
                   1897:             }
                   1898:             unless ($allowed) {
1.365     raeburn  1899:                 my $isowner = &Apache::lonuserutils::is_courseowner($cid,$coursedata{'internal.courseowner'});
1.329     raeburn  1900:                 if ($isowner) {
                   1901:                     if (($role_code eq 'co') && ($class eq 'Community')) {
                   1902:                         $allowed = 1;
                   1903:                     } elsif (($role_code eq 'cc') && ($class eq 'Course')) {
                   1904:                         $allowed = 1;
                   1905:                     }
1.217     raeburn  1906:                 }
1.329     raeburn  1907:             } 
                   1908:             if ((&Apache::lonnet::allowed('dro',$coursedom)) ||
                   1909:                 (&Apache::lonnet::allowed('dro',$ccdomain))) {
                   1910:                 $delallowed=1;
                   1911:             }
1.217     raeburn  1912: # - custom role. Needs more info, too
1.329     raeburn  1913:             if ($croletitle) {
                   1914:                 if (&Apache::lonnet::allowed('ccr',$coursedom.'/'.$coursedir)) {
                   1915:                     $allowed=1;
                   1916:                     $thisrole.='.'.$role_code;
1.217     raeburn  1917:                 }
1.329     raeburn  1918:             }
1.406.2.6  raeburn  1919:             if ($area=~m{^/($match_domain/$match_courseid/(\w+))}) {
                   1920:                 $csec = $2;
                   1921:                 $carea.='<br />'.&mt('Section: [_1]',$csec);
                   1922:                 $sortkey.="\0$csec";
1.329     raeburn  1923:                 if (!$allowed) {
1.406.2.6  raeburn  1924:                     if ($env{'request.course.sec'} eq $csec) {
                   1925:                         if (&Apache::lonnet::allowed('c'.$role_code,$1)) {
1.329     raeburn  1926:                             $allowed = 1;
1.217     raeburn  1927:                         }
                   1928:                     }
                   1929:                 }
1.329     raeburn  1930:             }
                   1931:             $area=$carea;
                   1932:         } else {
                   1933:             $sortkey.="\0".$area;
                   1934:             # Determine if current user is able to revoke privileges
                   1935:             if ($area=~m{^/($match_domain)/}) {
                   1936:                 if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
                   1937:                    (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1938:                    $allowed=1;
1.217     raeburn  1939:                 }
1.329     raeburn  1940:                 if (((&Apache::lonnet::allowed('dro',$1))  ||
                   1941:                     (&Apache::lonnet::allowed('dro',$ccdomain))) &&
                   1942:                     ($role_code ne 'dc')) {
                   1943:                     $delallowed=1;
1.217     raeburn  1944:                 }
1.329     raeburn  1945:             } else {
                   1946:                 if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
1.217     raeburn  1947:                     $allowed=1;
                   1948:                 }
                   1949:             }
1.363     raeburn  1950:             if ($role_code eq 'ca' || $role_code eq 'au' || $role_code eq 'aa') {
1.377     raeburn  1951:                 $class='Authoring Space';
1.329     raeburn  1952:             } elsif ($role_code eq 'su') {
                   1953:                 $class='System';
1.217     raeburn  1954:             } else {
1.329     raeburn  1955:                 $class='Domain';
1.217     raeburn  1956:             }
1.329     raeburn  1957:         }
                   1958:         if (($role_code eq 'ca') || ($role_code eq 'aa')) {
                   1959:             $area=~m{/($match_domain)/($match_username)};
                   1960:             if (&Apache::lonuserutils::authorpriv($2,$1)) {
                   1961:                 $allowed=1;
1.217     raeburn  1962:             } else {
1.329     raeburn  1963:                 $allowed=0;
1.217     raeburn  1964:             }
1.329     raeburn  1965:         }
                   1966:         my $row = '';
1.406.2.6  raeburn  1967:         if ($showall) {
                   1968:             $row.= '<td>';
                   1969:             if (($active) && ($allowed)) {
                   1970:                 $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
1.217     raeburn  1971:             } else {
1.406.2.6  raeburn  1972:                 if ($active) {
                   1973:                     $row.='&nbsp;';
                   1974:                 } else {
                   1975:                     $row.=&mt('expired or revoked');
                   1976:                 }
1.217     raeburn  1977:             }
1.406.2.6  raeburn  1978:             $row.='</td><td>';
                   1979:             if ($allowed && !$active) {
                   1980:                 $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
                   1981:             } else {
                   1982:                 $row.='&nbsp;';
                   1983:             }
                   1984:             $row.='</td><td>';
                   1985:             if ($delallowed) {
                   1986:                 $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
                   1987:             } else {
                   1988:                 $row.='&nbsp;';
                   1989:             }
                   1990:             $row.= '</td>';
1.329     raeburn  1991:         }
                   1992:         my $plaintext='';
                   1993:         if (!$croletitle) {
1.375     raeburn  1994:             $plaintext=&Apache::lonnet::plaintext($role_code,$class);
                   1995:             if (($showcredits) && ($credits ne '')) {
                   1996:                 $plaintext .= '<br/ ><span class="LC_nobreak">'.
                   1997:                               '<span class="LC_fontsize_small">'.
                   1998:                               &mt('Credits: [_1]',$credits).
                   1999:                               '</span></span>';
                   2000:             }
1.329     raeburn  2001:         } else {
                   2002:             $plaintext=
1.395     bisitz   2003:                 &mt('Custom role [_1][_2]defined by [_3]',
1.346     bisitz   2004:                         '"'.$croletitle.'"',
                   2005:                         '<br />',
                   2006:                         $croleuname.':'.$croleudom);
1.329     raeburn  2007:         }
1.406.2.6  raeburn  2008:         $row.= '<td>'.$plaintext.'</td>'.
                   2009:                '<td>'.$area.'</td>'.
                   2010:                '<td>'.($role_start_time?&Apache::lonlocal::locallocaltime($role_start_time)
                   2011:                                             : '&nbsp;' ).'</td>'.
                   2012:                '<td>'.($role_end_time  ?&Apache::lonlocal::locallocaltime($role_end_time)
                   2013:                                             : '&nbsp;' ).'</td>';
1.329     raeburn  2014:         $sortrole{$sortkey}=$envkey;
                   2015:         $roletext{$envkey}=$row;
                   2016:         $roleclass{$envkey}=$class;
1.406.2.6  raeburn  2017:         if ($allowed) {
                   2018:             $rolepriv{$envkey}='edit';
                   2019:         } else {
                   2020:             if ($context eq 'domain') {
1.406.2.7  raeburn  2021:                 if ((&Apache::lonnet::allowed('vur',$ccdomain)) &&
                   2022:                     ($envkey=~m{^/$ccdomain/})) {
1.406.2.6  raeburn  2023:                     $rolepriv{$envkey}='view';
                   2024:                 }
                   2025:             } elsif ($context eq 'course') {
                   2026:                 if ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
                   2027:                     ($env{'request.course.sec'} && ($env{'request.course.sec'} eq $csec) &&
                   2028:                      &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'}))) {
                   2029:                     $rolepriv{$envkey}='view';
                   2030:                 }
                   2031:             }
                   2032:         }
1.329     raeburn  2033:     } # end of foreach        (table building loop)
                   2034: 
                   2035:     my $rolesdisplay = 0;
                   2036:     my %output = ();
1.377     raeburn  2037:     foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  2038:         $output{$type} = '';
                   2039:         foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                   2040:             if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) {
                   2041:                  $output{$type}.=
                   2042:                       &Apache::loncommon::start_data_table_row().
                   2043:                       $roletext{$sortrole{$which}}.
                   2044:                       &Apache::loncommon::end_data_table_row();
1.217     raeburn  2045:             }
1.329     raeburn  2046:         }
                   2047:         unless($output{$type} eq '') {
                   2048:             $output{$type} = '<tr class="LC_info_row">'.
                   2049:                       "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
                   2050:                       $output{$type};
                   2051:             $rolesdisplay = 1;
                   2052:         }
                   2053:     }
                   2054:     if ($rolesdisplay == 1) {
                   2055:         my $contextrole='';
                   2056:         if ($env{'request.course.id'}) {
                   2057:             if (&Apache::loncommon::course_type() eq 'Community') {
                   2058:                 $contextrole = &mt('Existing Roles in this Community');
1.290     bisitz   2059:             } else {
1.329     raeburn  2060:                 $contextrole = &mt('Existing Roles in this Course');
1.290     bisitz   2061:             }
1.329     raeburn  2062:         } elsif ($env{'request.role'} =~ /^au\./) {
1.377     raeburn  2063:             $contextrole = &mt('Existing Co-Author Roles in your Authoring Space');
1.329     raeburn  2064:         } else {
1.406.2.6  raeburn  2065:             if ($showall) {
                   2066:                 $contextrole = &mt('Existing Roles in this Domain');
                   2067:             } elsif ($showactive) {
                   2068:                 $contextrole = &mt('Unexpired Roles in this Domain');
                   2069:             } elsif ($showexpired) {
                   2070:                 $contextrole = &mt('Expired or Revoked Roles in this Domain');
                   2071:             }
1.329     raeburn  2072:         }
1.393     raeburn  2073:         $r->print('<div class="LC_left_float">'.
1.375     raeburn  2074: '<fieldset><legend>'.$contextrole.'</legend>'.
1.217     raeburn  2075: &Apache::loncommon::start_data_table("LC_createuser").
1.406.2.6  raeburn  2076: &Apache::loncommon::start_data_table_header_row());
                   2077:         if ($showall) {
                   2078:             $r->print(
                   2079: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}.'</th>'
                   2080:             );
                   2081:         } elsif ($showexpired) {
                   2082:             $r->print('<th>'.$lt{'rev'}.'</th>');
                   2083:         }
                   2084:         $r->print(
                   2085: '<th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.'</th>'.
                   2086: '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
1.217     raeburn  2087: &Apache::loncommon::end_data_table_header_row());
1.377     raeburn  2088:         foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  2089:             if ($output{$type}) {
                   2090:                 $r->print($output{$type}."\n");
1.217     raeburn  2091:             }
                   2092:         }
1.375     raeburn  2093:         $r->print(&Apache::loncommon::end_data_table().
                   2094:                   '</fieldset></div>');
1.329     raeburn  2095:     }
1.217     raeburn  2096:     return;
                   2097: }
                   2098: 
1.218     raeburn  2099: sub new_coauthor_roles {
                   2100:     my ($r,$ccuname,$ccdomain) = @_;
                   2101:     my $addrolesdisplay = 0;
                   2102:     #
                   2103:     # Co-Author
                   2104:     #
                   2105:     if (&Apache::lonuserutils::authorpriv($env{'user.name'},
                   2106:                                           $env{'request.role.domain'}) &&
                   2107:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
                   2108:         # No sense in assigning co-author role to yourself
                   2109:         $addrolesdisplay = 1;
                   2110:         my $cuname=$env{'user.name'};
                   2111:         my $cudom=$env{'request.role.domain'};
                   2112:         my %lt=&Apache::lonlocal::texthash(
1.377     raeburn  2113:                     'cs'   => "Authoring Space",
1.218     raeburn  2114:                     'act'  => "Activate",
                   2115:                     'rol'  => "Role",
                   2116:                     'ext'  => "Extent",
                   2117:                     'sta'  => "Start",
                   2118:                     'end'  => "End",
                   2119:                     'cau'  => "Co-Author",
                   2120:                     'caa'  => "Assistant Co-Author",
                   2121:                     'ssd'  => "Set Start Date",
                   2122:                     'sed'  => "Set End Date"
                   2123:                                        );
                   2124:         $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n".
                   2125:                   &Apache::loncommon::start_data_table()."\n".
                   2126:                   &Apache::loncommon::start_data_table_header_row()."\n".
                   2127:                   '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
                   2128:                   '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
                   2129:                   '<th>'.$lt{'end'}.'</th>'."\n".
                   2130:                   &Apache::loncommon::end_data_table_header_row()."\n".
                   2131:                   &Apache::loncommon::start_data_table_row().'
                   2132:            <td>
1.291     bisitz   2133:             <input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_ca" />
1.218     raeburn  2134:            </td>
                   2135:            <td>'.$lt{'cau'}.'</td>
                   2136:            <td>'.$cudom.'_'.$cuname.'</td>
                   2137:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
                   2138:              <a href=
                   2139: "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>
                   2140: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
                   2141: <a href=
                   2142: "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".
                   2143:               &Apache::loncommon::end_data_table_row()."\n".
                   2144:               &Apache::loncommon::start_data_table_row()."\n".
1.291     bisitz   2145: '<td><input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
1.218     raeburn  2146: <td>'.$lt{'caa'}.'</td>
                   2147: <td>'.$cudom.'_'.$cuname.'</td>
                   2148: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
                   2149: <a href=
                   2150: "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>
                   2151: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
                   2152: <a href=
                   2153: "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".
                   2154:              &Apache::loncommon::end_data_table_row()."\n".
                   2155:              &Apache::loncommon::end_data_table());
                   2156:     } elsif ($env{'request.role'} =~ /^au\./) {
                   2157:         if (!(&Apache::lonuserutils::authorpriv($env{'user.name'},
                   2158:                                                 $env{'request.role.domain'}))) {
                   2159:             $r->print('<span class="LC_error">'.
                   2160:                       &mt('You do not have privileges to assign co-author roles.').
                   2161:                       '</span>');
                   2162:         } elsif (($env{'user.name'} eq $ccuname) &&
                   2163:              ($env{'user.domain'} eq $ccdomain)) {
1.377     raeburn  2164:             $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  2165:         }
                   2166:     }
                   2167:     return $addrolesdisplay;;
                   2168: }
                   2169: 
                   2170: sub new_domain_roles {
1.357     raeburn  2171:     my ($r,$ccdomain) = @_;
1.218     raeburn  2172:     my $addrolesdisplay = 0;
                   2173:     #
                   2174:     # Domain level
                   2175:     #
                   2176:     my $num_domain_level = 0;
                   2177:     my $domaintext =
                   2178:     '<h4>'.&mt('Domain Level').'</h4>'.
                   2179:     &Apache::loncommon::start_data_table().
                   2180:     &Apache::loncommon::start_data_table_header_row().
                   2181:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
                   2182:     &mt('Extent').'</th>'.
                   2183:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
                   2184:     &Apache::loncommon::end_data_table_header_row();
1.312     raeburn  2185:     my @allroles = &Apache::lonuserutils::roles_by_context('domain');
1.218     raeburn  2186:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
1.312     raeburn  2187:         foreach my $role (@allroles) {
                   2188:             next if ($role eq 'ad');
1.357     raeburn  2189:             next if (($role eq 'au') && ($ccdomain ne $thisdomain));
1.218     raeburn  2190:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
                   2191:                my $plrole=&Apache::lonnet::plaintext($role);
                   2192:                my %lt=&Apache::lonlocal::texthash(
                   2193:                     'ssd'  => "Set Start Date",
                   2194:                     'sed'  => "Set End Date"
                   2195:                                        );
                   2196:                $num_domain_level ++;
                   2197:                $domaintext .=
                   2198: &Apache::loncommon::start_data_table_row().
1.291     bisitz   2199: '<td><input type="checkbox" name="act_'.$thisdomain.'_'.$role.'" /></td>
1.218     raeburn  2200: <td>'.$plrole.'</td>
                   2201: <td>'.$thisdomain.'</td>
                   2202: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
                   2203: <a href=
                   2204: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
                   2205: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
                   2206: <a href=
                   2207: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
                   2208: &Apache::loncommon::end_data_table_row();
                   2209:             }
                   2210:         }
                   2211:     }
                   2212:     $domaintext.= &Apache::loncommon::end_data_table();
                   2213:     if ($num_domain_level > 0) {
                   2214:         $r->print($domaintext);
                   2215:         $addrolesdisplay = 1;
                   2216:     }
                   2217:     return $addrolesdisplay;
                   2218: }
                   2219: 
1.188     raeburn  2220: sub user_authentication {
1.227     raeburn  2221:     my ($ccuname,$ccdomain,$formname) = @_;
1.188     raeburn  2222:     my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
1.227     raeburn  2223:     my $outcome;
1.406.2.6  raeburn  2224:     my %lt=&Apache::lonlocal::texthash(
                   2225:                    'err'   => "ERROR",
                   2226:                    'uuas'  => "This user has an unrecognized authentication scheme",
                   2227:                    'adcs'  => "Please alert a domain coordinator of this situation",
                   2228:                    'sldb'  => "Please specify login data below",
                   2229:                    'ld'    => "Login Data"
                   2230:     );
1.188     raeburn  2231:     # Check for a bad authentication type
                   2232:     if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth):/) {
                   2233:         # bad authentication scheme
                   2234:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
1.227     raeburn  2235:             &initialize_authen_forms($ccdomain,$formname);
                   2236: 
1.190     raeburn  2237:             my $choices = &Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc);
1.188     raeburn  2238:             $outcome = <<ENDBADAUTH;
                   2239: <script type="text/javascript" language="Javascript">
1.301     bisitz   2240: // <![CDATA[
1.188     raeburn  2241: $loginscript
1.301     bisitz   2242: // ]]>
1.188     raeburn  2243: </script>
                   2244: <span class="LC_error">$lt{'err'}:
                   2245: $lt{'uuas'} ($currentauth). $lt{'sldb'}.</span>
                   2246: <h3>$lt{'ld'}</h3>
                   2247: $choices
                   2248: ENDBADAUTH
                   2249:         } else {
                   2250:             # This user is not allowed to modify the user's
                   2251:             # authentication scheme, so just notify them of the problem
                   2252:             $outcome = <<ENDBADAUTH;
                   2253: <span class="LC_error"> $lt{'err'}: 
                   2254: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
                   2255: </span>
                   2256: ENDBADAUTH
                   2257:         }
                   2258:     } else { # Authentication type is valid
1.227     raeburn  2259:         &initialize_authen_forms($ccdomain,$formname,$currentauth,'modifyuser');
1.205     raeburn  2260:         my ($authformcurrent,$can_modify,@authform_others) =
1.188     raeburn  2261:             &modify_login_block($ccdomain,$currentauth);
                   2262:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2263:             # Current user has login modification privileges
                   2264:             $outcome =
                   2265:                        '<script type="text/javascript" language="Javascript">'."\n".
1.301     bisitz   2266:                        '// <![CDATA['."\n".
1.188     raeburn  2267:                        $loginscript."\n".
1.301     bisitz   2268:                        '// ]]>'."\n".
1.188     raeburn  2269:                        '</script>'."\n".
                   2270:                        '<h3>'.$lt{'ld'}.'</h3>'.
                   2271:                        &Apache::loncommon::start_data_table().
1.205     raeburn  2272:                        &Apache::loncommon::start_data_table_row().
1.188     raeburn  2273:                        '<td>'.$authformnop;
1.406.2.6  raeburn  2274:             if (($can_modify) && (&Apache::lonnet::allowed('mau',$ccdomain))) {
1.188     raeburn  2275:                 $outcome .= '</td>'."\n".
                   2276:                             &Apache::loncommon::end_data_table_row().
                   2277:                             &Apache::loncommon::start_data_table_row().
                   2278:                             '<td>'.$authformcurrent.'</td>'.
                   2279:                             &Apache::loncommon::end_data_table_row()."\n";
                   2280:             } else {
1.200     raeburn  2281:                 $outcome .= '&nbsp;('.$authformcurrent.')</td>'.
                   2282:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  2283:             }
1.406.2.6  raeburn  2284:             if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2285:                 foreach my $item (@authform_others) { 
                   2286:                     $outcome .= &Apache::loncommon::start_data_table_row().
                   2287:                                 '<td>'.$item.'</td>'.
                   2288:                                 &Apache::loncommon::end_data_table_row()."\n";
                   2289:                 }
1.188     raeburn  2290:             }
1.205     raeburn  2291:             $outcome .= &Apache::loncommon::end_data_table();
1.188     raeburn  2292:         } else {
1.406.2.6  raeburn  2293:             if (&Apache::lonnet::allowed('udp',$ccdomain)) {
                   2294:                 # Current user has rights to view domain preferences for user's domain
                   2295:                 my $result;
                   2296:                 if ($currentauth =~ /^krb(4|5):([^:]*)$/) {
                   2297:                     my ($krbver,$krbrealm) = ($1,$2);
                   2298:                     if ($krbrealm eq '') {
                   2299:                         $result = &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
                   2300:                     } else {
                   2301:                         $result = &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
                   2302:                                       $krbver,$krbrealm);
                   2303:                     }
                   2304:                 } elsif ($currentauth =~ /^internal:/) {
                   2305:                     $result = &mt('Currently internally authenticated.');
                   2306:                 } elsif ($currentauth =~ /^localauth:/) {
                   2307:                     $result = &mt('Currently using local (institutional) authentication.');
                   2308:                 } elsif ($currentauth =~ /^unix:/) {
                   2309:                     $result = &mt('Currently Filesystem Authenticated.');
                   2310:                 }
                   2311:                 $outcome = '<h3>'.$lt{'ld'}.'</h3>'.
                   2312:                            &Apache::loncommon::start_data_table().
                   2313:                            &Apache::loncommon::start_data_table_row().
                   2314:                            '<td>'.$result.'</td>'.
                   2315:                            &Apache::loncommon::end_data_table_row()."\n".
                   2316:                            &Apache::loncommon::end_data_table();
                   2317:             } elsif (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
1.188     raeburn  2318:                 my %lt=&Apache::lonlocal::texthash(
                   2319:                            'ccld'  => "Change Current Login Data",
                   2320:                            'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
                   2321:                            'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   2322:                 );
                   2323:                 $outcome .= <<ENDNOPRIV;
                   2324: <h3>$lt{'ccld'}</h3>
                   2325: $lt{'yodo'} $lt{'ifch'}: $ccdomain
1.235     raeburn  2326: <input type="hidden" name="login" value="nochange" />
1.188     raeburn  2327: ENDNOPRIV
                   2328:             }
                   2329:         }
                   2330:     }  ## End of "check for bad authentication type" logic
                   2331:     return $outcome;
                   2332: }
                   2333: 
1.187     raeburn  2334: sub modify_login_block {
                   2335:     my ($dom,$currentauth) = @_;
                   2336:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2337:     my ($authnum,%can_assign) =
                   2338:         &Apache::loncommon::get_assignable_auth($dom);
1.205     raeburn  2339:     my ($authformcurrent,@authform_others,$show_override_msg);
1.187     raeburn  2340:     if ($currentauth=~/^krb(4|5):/) {
                   2341:         $authformcurrent=$authformkrb;
                   2342:         if ($can_assign{'int'}) {
1.205     raeburn  2343:             push(@authform_others,$authformint);
1.187     raeburn  2344:         }
                   2345:         if ($can_assign{'loc'}) {
1.205     raeburn  2346:             push(@authform_others,$authformloc);
1.187     raeburn  2347:         }
                   2348:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   2349:             $show_override_msg = 1;
                   2350:         }
                   2351:     } elsif ($currentauth=~/^internal:/) {
                   2352:         $authformcurrent=$authformint;
                   2353:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2354:             push(@authform_others,$authformkrb);
1.187     raeburn  2355:         }
                   2356:         if ($can_assign{'loc'}) {
1.205     raeburn  2357:             push(@authform_others,$authformloc);
1.187     raeburn  2358:         }
                   2359:         if ($can_assign{'int'}) {
                   2360:             $show_override_msg = 1;
                   2361:         }
                   2362:     } elsif ($currentauth=~/^unix:/) {
                   2363:         $authformcurrent=$authformfsys;
                   2364:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2365:             push(@authform_others,$authformkrb);
1.187     raeburn  2366:         }
                   2367:         if ($can_assign{'int'}) {
1.205     raeburn  2368:             push(@authform_others,$authformint);
1.187     raeburn  2369:         }
                   2370:         if ($can_assign{'loc'}) {
1.205     raeburn  2371:             push(@authform_others,$authformloc);
1.187     raeburn  2372:         }
                   2373:         if ($can_assign{'fsys'}) {
                   2374:             $show_override_msg = 1;
                   2375:         }
                   2376:     } elsif ($currentauth=~/^localauth:/) {
                   2377:         $authformcurrent=$authformloc;
                   2378:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2379:             push(@authform_others,$authformkrb);
1.187     raeburn  2380:         }
                   2381:         if ($can_assign{'int'}) {
1.205     raeburn  2382:             push(@authform_others,$authformint);
1.187     raeburn  2383:         }
                   2384:         if ($can_assign{'loc'}) {
                   2385:             $show_override_msg = 1;
                   2386:         }
                   2387:     }
                   2388:     if ($show_override_msg) {
1.205     raeburn  2389:         $authformcurrent = '<table><tr><td colspan="3">'.$authformcurrent.
                   2390:                            '</td></tr>'."\n".
                   2391:                            '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
                   2392:                            '<td><b>'.&mt('Currently in use').'</b></td>'.
                   2393:                            '<td align="right"><span class="LC_cusr_emph">'.
1.187     raeburn  2394:                             &mt('will override current values').
1.205     raeburn  2395:                             '</span></td></tr></table>';
1.187     raeburn  2396:     }
1.205     raeburn  2397:     return ($authformcurrent,$show_override_msg,@authform_others); 
1.187     raeburn  2398: }
                   2399: 
1.188     raeburn  2400: sub personal_data_display {
1.391     raeburn  2401:     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$rolesarray,
1.396     raeburn  2402:         $now,$captchaform,$emailusername,$usertype) = @_;
1.388     bisitz   2403:     my ($output,%userenv,%canmodify,%canmodify_status);
1.219     raeburn  2404:     my @userinfo = ('firstname','middlename','lastname','generation',
                   2405:                     'permanentemail','id');
1.252     raeburn  2406:     my $rowcount = 0;
                   2407:     my $editable = 0;
1.391     raeburn  2408:     my %textboxsize = (
                   2409:                        firstname      => '15',
                   2410:                        middlename     => '15',
                   2411:                        lastname       => '15',
                   2412:                        generation     => '5',
                   2413:                        permanentemail => '25',
                   2414:                        id             => '15',
                   2415:                       );
                   2416: 
                   2417:     my %lt=&Apache::lonlocal::texthash(
                   2418:                 'pd'             => "Personal Data",
                   2419:                 'firstname'      => "First Name",
                   2420:                 'middlename'     => "Middle Name",
                   2421:                 'lastname'       => "Last Name",
                   2422:                 'generation'     => "Generation",
                   2423:                 'permanentemail' => "Permanent e-mail address",
                   2424:                 'id'             => "Student/Employee ID",
                   2425:                 'lg'             => "Login Data",
                   2426:                 'inststatus'     => "Affiliation",
                   2427:                 'email'          => 'E-mail address',
                   2428:                 'valid'          => 'Validation',
                   2429:     );
                   2430: 
                   2431:     %canmodify_status =
1.286     raeburn  2432:         &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
                   2433:                                                    ['inststatus'],$rolesarray);
1.253     raeburn  2434:     if (!$newuser) {
1.188     raeburn  2435:         # Get the users information
                   2436:         %userenv = &Apache::lonnet::get('environment',
                   2437:                    ['firstname','middlename','lastname','generation',
1.286     raeburn  2438:                     'permanentemail','id','inststatus'],$ccdomain,$ccuname);
1.219     raeburn  2439:         %canmodify =
                   2440:             &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
1.252     raeburn  2441:                                                        \@userinfo,$rolesarray);
1.257     raeburn  2442:     } elsif ($context eq 'selfcreate') {
1.391     raeburn  2443:         if ($newuser eq 'email') {
1.396     raeburn  2444:             if (ref($emailusername) eq 'HASH') {
                   2445:                 if (ref($emailusername->{$usertype}) eq 'HASH') {
                   2446:                     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
                   2447:                     @userinfo = ();          
                   2448:                     if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
                   2449:                         foreach my $field (@{$infofields}) { 
                   2450:                             if ($emailusername->{$usertype}->{$field}) {
                   2451:                                 push(@userinfo,$field);
                   2452:                                 $canmodify{$field} = 1;
                   2453:                                 unless ($textboxsize{$field}) {
                   2454:                                     $textboxsize{$field} = 25;
                   2455:                                 }
                   2456:                                 unless ($lt{$field}) {
                   2457:                                     $lt{$field} = $infotitles->{$field};
                   2458:                                 }
                   2459:                                 if ($emailusername->{$usertype}->{$field} eq 'required') {
                   2460:                                     $lt{$field} .= '<b>*</b>';
                   2461:                                 }
1.391     raeburn  2462:                             }
                   2463:                         }
                   2464:                     }
                   2465:                 }
                   2466:             }
                   2467:         } else {
                   2468:             %canmodify = &selfcreate_canmodify($context,$ccdomain,\@userinfo,
                   2469:                                                $inst_results,$rolesarray);
                   2470:         }
1.188     raeburn  2471:     }
1.391     raeburn  2472: 
1.188     raeburn  2473:     my $genhelp=&Apache::loncommon::help_open_topic('Generation');
                   2474:     $output = '<h3>'.$lt{'pd'}.'</h3>'.
                   2475:               &Apache::lonhtmlcommon::start_pick_box();
1.391     raeburn  2476:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
1.396     raeburn  2477:         $output .= &Apache::lonhtmlcommon::row_title($lt{'email'}.'<b>*</b>',undef,
1.391     raeburn  2478:                                                      'LC_oddrow_value')."\n".
1.394     raeburn  2479:                    '<input type="text" name="uname" size="25" value="" autocomplete="off" />';
1.391     raeburn  2480:         $rowcount ++;
                   2481:         $output .= &Apache::lonhtmlcommon::row_closure(1);
1.406.2.1  raeburn  2482:         my $upassone = '<input type="password" name="upass'.$now.'" size="20" autocomplete="off" />';
                   2483:         my $upasstwo = '<input type="password" name="upasscheck'.$now.'" size="20" autocomplete="off" />';
1.396     raeburn  2484:         $output .= &Apache::lonhtmlcommon::row_title(&mt('Password').'<b>*</b>',
1.391     raeburn  2485:                                                     'LC_pick_box_title',
                   2486:                                                     'LC_oddrow_value')."\n".
                   2487:                    $upassone."\n".
                   2488:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
1.396     raeburn  2489:                    &Apache::lonhtmlcommon::row_title(&mt('Confirm password').'<b>*</b>',
1.391     raeburn  2490:                                                      'LC_pick_box_title',
                   2491:                                                      'LC_oddrow_value')."\n".
                   2492:                    $upasstwo.
                   2493:                    &Apache::lonhtmlcommon::row_closure()."\n";
                   2494:     }
1.188     raeburn  2495:     foreach my $item (@userinfo) {
                   2496:         my $rowtitle = $lt{$item};
1.252     raeburn  2497:         my $hiderow = 0;
1.188     raeburn  2498:         if ($item eq 'generation') {
                   2499:             $rowtitle = $genhelp.$rowtitle;
                   2500:         }
1.252     raeburn  2501:         my $row = &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
1.188     raeburn  2502:         if ($newuser) {
1.210     raeburn  2503:             if (ref($inst_results) eq 'HASH') {
                   2504:                 if ($inst_results->{$item} ne '') {
1.252     raeburn  2505:                     $row .= '<input type="hidden" name="c'.$item.'" value="'.$inst_results->{$item}.'" />'.$inst_results->{$item};
1.210     raeburn  2506:                 } else {
1.252     raeburn  2507:                     if ($context eq 'selfcreate') {
1.391     raeburn  2508:                         if ($canmodify{$item}) {
1.394     raeburn  2509:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.252     raeburn  2510:                             $editable ++;
                   2511:                         } else {
                   2512:                             $hiderow = 1;
                   2513:                         }
1.253     raeburn  2514:                     } else {
                   2515:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2516:                     }
1.210     raeburn  2517:                 }
1.188     raeburn  2518:             } else {
1.252     raeburn  2519:                 if ($context eq 'selfcreate') {
1.401     raeburn  2520:                     if ($canmodify{$item}) {
                   2521:                         if ($newuser eq 'email') {
                   2522:                             $row .= '<input type="text" name="'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.287     raeburn  2523:                         } else {
1.401     raeburn  2524:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.287     raeburn  2525:                         }
1.401     raeburn  2526:                         $editable ++;
                   2527:                     } else {
                   2528:                         $hiderow = 1;
1.252     raeburn  2529:                     }
1.253     raeburn  2530:                 } else {
                   2531:                     $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2532:                 }
1.188     raeburn  2533:             }
                   2534:         } else {
1.219     raeburn  2535:             if ($canmodify{$item}) {
1.252     raeburn  2536:                 $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
1.393     raeburn  2537:                 if (($item eq 'id') && (!$newuser)) {
                   2538:                     $row .= '<br />'.&Apache::lonuserutils::forceid_change($context);
                   2539:                 }
1.188     raeburn  2540:             } else {
1.252     raeburn  2541:                 $row .= $userenv{$item};
1.188     raeburn  2542:             }
                   2543:         }
1.252     raeburn  2544:         $row .= &Apache::lonhtmlcommon::row_closure(1);
                   2545:         if (!$hiderow) {
                   2546:             $output .= $row;
                   2547:             $rowcount ++;
                   2548:         }
1.188     raeburn  2549:     }
1.286     raeburn  2550:     if (($canmodify_status{'inststatus'}) || ($context ne 'selfcreate')) {
                   2551:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($ccdomain);
                   2552:         if (ref($types) eq 'ARRAY') {
                   2553:             if (@{$types} > 0) {
                   2554:                 my ($hiderow,$shown);
                   2555:                 if ($canmodify_status{'inststatus'}) {
                   2556:                     $shown = &pick_inst_statuses($userenv{'inststatus'},$usertypes,$types);
                   2557:                 } else {
                   2558:                     if ($userenv{'inststatus'} eq '') {
                   2559:                         $hiderow = 1;
1.334     raeburn  2560:                     } else {
                   2561:                         my @showitems;
                   2562:                         foreach my $item ( map { &unescape($_); } split(':',$userenv{'inststatus'})) {
                   2563:                             if (exists($usertypes->{$item})) {
                   2564:                                 push(@showitems,$usertypes->{$item});
                   2565:                             } else {
                   2566:                                 push(@showitems,$item);
                   2567:                             }
                   2568:                         }
                   2569:                         if (@showitems) {
                   2570:                             $shown = join(', ',@showitems);
                   2571:                         } else {
                   2572:                             $hiderow = 1;
                   2573:                         }
1.286     raeburn  2574:                     }
                   2575:                 }
                   2576:                 if (!$hiderow) {
1.389     bisitz   2577:                     my $row = &Apache::lonhtmlcommon::row_title(&mt('Affiliations'),undef,'LC_oddrow_value')."\n".
1.286     raeburn  2578:                               $shown.&Apache::lonhtmlcommon::row_closure(1); 
                   2579:                     if ($context eq 'selfcreate') {
                   2580:                         $rowcount ++;
                   2581:                     }
                   2582:                     $output .= $row;
                   2583:                 }
                   2584:             }
                   2585:         }
                   2586:     }
1.391     raeburn  2587:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
                   2588:         if ($captchaform) {
1.406.2.2  raeburn  2589:             $output .= &Apache::lonhtmlcommon::row_title($lt{'valid'}.'*',
1.391     raeburn  2590:                                                          'LC_pick_box_title')."\n".
                   2591:                        $captchaform."\n".'<br /><br />'.
                   2592:                        &Apache::lonhtmlcommon::row_closure(1); 
                   2593:             $rowcount ++;
                   2594:         }
                   2595:         my $submit_text = &mt('Create account');
                   2596:         $output .= &Apache::lonhtmlcommon::row_title()."\n".
                   2597:                    '<br /><input type="submit" name="createaccount" value="'.
                   2598:                    $submit_text.'" />'.
1.396     raeburn  2599:                    '<input type="hidden" name="type" value="'.$usertype.'" />'.
1.391     raeburn  2600:                    &Apache::lonhtmlcommon::row_closure(1);
                   2601:     }
1.188     raeburn  2602:     $output .= &Apache::lonhtmlcommon::end_pick_box();
1.206     raeburn  2603:     if (wantarray) {
1.252     raeburn  2604:         if ($context eq 'selfcreate') {
                   2605:             return($output,$rowcount,$editable);
                   2606:         } else {
1.388     bisitz   2607:             return $output;
1.252     raeburn  2608:         }
1.206     raeburn  2609:     } else {
                   2610:         return $output;
                   2611:     }
1.188     raeburn  2612: }
                   2613: 
1.286     raeburn  2614: sub pick_inst_statuses {
                   2615:     my ($curr,$usertypes,$types) = @_;
                   2616:     my ($output,$rem,@currtypes);
                   2617:     if ($curr ne '') {
                   2618:         @currtypes = map { &unescape($_); } split(/:/,$curr);
                   2619:     }
                   2620:     my $numinrow = 2;
                   2621:     if (ref($types) eq 'ARRAY') {
                   2622:         $output = '<table>';
                   2623:         my $lastcolspan; 
                   2624:         for (my $i=0; $i<@{$types}; $i++) {
                   2625:             if (defined($usertypes->{$types->[$i]})) {
                   2626:                 my $rem = $i%($numinrow);
                   2627:                 if ($rem == 0) {
                   2628:                     if ($i<@{$types}-1) {
                   2629:                         if ($i > 0) { 
                   2630:                             $output .= '</tr>';
                   2631:                         }
                   2632:                         $output .= '<tr>';
                   2633:                     }
                   2634:                 } elsif ($i==@{$types}-1) {
                   2635:                     my $colsleft = $numinrow - $rem;
                   2636:                     if ($colsleft > 1) {
                   2637:                         $lastcolspan = ' colspan="'.$colsleft.'"';
                   2638:                     }
                   2639:                 }
                   2640:                 my $check = ' ';
                   2641:                 if (grep(/^\Q$types->[$i]\E$/,@currtypes)) {
                   2642:                     $check = ' checked="checked" ';
                   2643:                 }
                   2644:                 $output .= '<td class="LC_left_item"'.$lastcolspan.'>'.
                   2645:                            '<span class="LC_nobreak"><label>'.
                   2646:                            '<input type="checkbox" name="inststatus" '.
                   2647:                            'value="'.$types->[$i].'"'.$check.'/>'.
                   2648:                            $usertypes->{$types->[$i]}.'</label></span></td>';
                   2649:             }
                   2650:         }
                   2651:         $output .= '</tr></table>';
                   2652:     }
                   2653:     return $output;
                   2654: }
                   2655: 
1.257     raeburn  2656: sub selfcreate_canmodify {
                   2657:     my ($context,$dom,$userinfo,$inst_results,$rolesarray) = @_;
                   2658:     if (ref($inst_results) eq 'HASH') {
                   2659:         my @inststatuses = &get_inststatuses($inst_results);
                   2660:         if (@inststatuses == 0) {
                   2661:             @inststatuses = ('default');
                   2662:         }
                   2663:         $rolesarray = \@inststatuses;
                   2664:     }
                   2665:     my %canmodify =
                   2666:         &Apache::lonuserutils::can_modify_userinfo($context,$dom,$userinfo,
                   2667:                                                    $rolesarray);
                   2668:     return %canmodify;
                   2669: }
                   2670: 
1.252     raeburn  2671: sub get_inststatuses {
                   2672:     my ($insthashref) = @_;
                   2673:     my @inststatuses = ();
                   2674:     if (ref($insthashref) eq 'HASH') {
                   2675:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
                   2676:             @inststatuses = @{$insthashref->{'inststatus'}};
                   2677:         }
                   2678:     }
                   2679:     return @inststatuses;
                   2680: }
                   2681: 
1.4       www      2682: # ================================================================= Phase Three
1.42      matthew  2683: sub update_user_data {
1.375     raeburn  2684:     my ($r,$context,$crstype,$brcrum,$showcredits) = @_; 
1.101     albertel 2685:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                   2686:                                           $env{'form.ccdomain'});
1.27      matthew  2687:     # Error messages
1.188     raeburn  2688:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
1.193     raeburn  2689:     my $end       = '</span><br /><br />';
                   2690:     my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
1.188     raeburn  2691:                     "'$env{'form.prevphase'}','modify')".'" />'.
1.219     raeburn  2692:                     &mt('Return to previous page').'</a>'.
                   2693:                     &Apache::loncommon::end_page();
                   2694:     my $now = time;
1.40      www      2695:     my $title;
1.101     albertel 2696:     if (exists($env{'form.makeuser'})) {
1.40      www      2697: 	$title='Set Privileges for New User';
                   2698:     } else {
                   2699:         $title='Modify User Privileges';
                   2700:     }
1.213     raeburn  2701:     my $newuser = 0;
1.160     raeburn  2702:     my ($jsback,$elements) = &crumb_utilities();
                   2703:     my $jscript = '<script type="text/javascript">'."\n".
1.301     bisitz   2704:                   '// <![CDATA['."\n".
                   2705:                   $jsback."\n".
                   2706:                   '// ]]>'."\n".
                   2707:                   '</script>'."\n";
1.406.2.7  raeburn  2708:     my %breadcrumb_text = &singleuser_breadcrumb($crstype,$context,$env{'form.ccdomain'});
1.351     raeburn  2709:     push (@{$brcrum},
                   2710:              {href => "javascript:backPage(document.userupdate)",
                   2711:               text => $breadcrumb_text{'search'},
                   2712:               faq  => 282,
                   2713:               bug  => 'Instructor Interface',}
                   2714:              );
                   2715:     if ($env{'form.prevphase'} eq 'userpicked') {
                   2716:         push(@{$brcrum},
                   2717:                {href => "javascript:backPage(document.userupdate,'get_user_info','select')",
                   2718:                 text => $breadcrumb_text{'userpicked'},
                   2719:                 faq  => 282,
                   2720:                 bug  => 'Instructor Interface',});
1.233     raeburn  2721:     }
1.224     raeburn  2722:     my $helpitem = 'Course_Change_Privileges';
                   2723:     if ($env{'form.action'} eq 'singlestudent') {
                   2724:         $helpitem = 'Course_Add_Student';
                   2725:     }
1.351     raeburn  2726:     push(@{$brcrum}, 
                   2727:             {href => "javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
                   2728:              text => $breadcrumb_text{'modify'},
                   2729:              faq  => 282,
                   2730:              bug  => 'Instructor Interface',},
                   2731:             {href => "/adm/createuser",
                   2732:              text => "Result",
                   2733:              faq  => 282,
                   2734:              bug  => 'Instructor Interface',
                   2735:              help => $helpitem});
                   2736:     my $args = {bread_crumbs          => $brcrum,
                   2737:                 bread_crumbs_component => 'User Management'};
                   2738:     if ($env{'form.popup'}) {
                   2739:         $args->{'no_nav_bar'} = 1;
                   2740:     }
                   2741:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
1.188     raeburn  2742:     $r->print(&update_result_form($uhome));
1.27      matthew  2743:     # Check Inputs
1.101     albertel 2744:     if (! $env{'form.ccuname'} ) {
1.193     raeburn  2745: 	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
1.27      matthew  2746: 	return;
                   2747:     }
1.138     albertel 2748:     if (  $env{'form.ccuname'} ne 
                   2749: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
1.281     bisitz   2750: 	$r->print($error.&mt('Invalid login name.').'  '.
                   2751: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid.').
1.193     raeburn  2752: 		  $end.$rtnlink);
1.27      matthew  2753: 	return;
                   2754:     }
1.101     albertel 2755:     if (! $env{'form.ccdomain'}       ) {
1.193     raeburn  2756: 	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
1.27      matthew  2757: 	return;
                   2758:     }
1.138     albertel 2759:     if (  $env{'form.ccdomain'} ne
                   2760: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
1.281     bisitz   2761: 	$r->print($error.&mt('Invalid domain name.').'  '.
                   2762: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid.').
1.193     raeburn  2763: 		  $end.$rtnlink);
1.27      matthew  2764: 	return;
                   2765:     }
1.219     raeburn  2766:     if ($uhome eq 'no_host') {
                   2767:         $newuser = 1;
                   2768:     }
1.101     albertel 2769:     if (! exists($env{'form.makeuser'})) {
1.29      matthew  2770:         # Modifying an existing user, so check the validity of the name
                   2771:         if ($uhome eq 'no_host') {
1.389     bisitz   2772:             $r->print(
                   2773:                 $error
                   2774:                .'<p class="LC_error">'
                   2775:                .&mt('Unable to determine home server for [_1] in domain [_2].',
                   2776:                         '"'.$env{'form.ccuname'}.'"','"'.$env{'form.ccdomain'}.'"')
                   2777:                .'</p>');
1.29      matthew  2778:             return;
                   2779:         }
                   2780:     }
1.27      matthew  2781:     # Determine authentication method and password for the user being modified
                   2782:     my $amode='';
                   2783:     my $genpwd='';
1.101     albertel 2784:     if ($env{'form.login'} eq 'krb') {
1.41      albertel 2785: 	$amode='krb';
1.101     albertel 2786: 	$amode.=$env{'form.krbver'};
                   2787: 	$genpwd=$env{'form.krbarg'};
                   2788:     } elsif ($env{'form.login'} eq 'int') {
1.27      matthew  2789: 	$amode='internal';
1.101     albertel 2790: 	$genpwd=$env{'form.intarg'};
                   2791:     } elsif ($env{'form.login'} eq 'fsys') {
1.27      matthew  2792: 	$amode='unix';
1.101     albertel 2793: 	$genpwd=$env{'form.fsysarg'};
                   2794:     } elsif ($env{'form.login'} eq 'loc') {
1.27      matthew  2795: 	$amode='localauth';
1.101     albertel 2796: 	$genpwd=$env{'form.locarg'};
1.27      matthew  2797: 	$genpwd=" " if (!$genpwd);
1.101     albertel 2798:     } elsif (($env{'form.login'} eq 'nochange') ||
                   2799:              ($env{'form.login'} eq ''        )) { 
1.34      matthew  2800:         # There is no need to tell the user we did not change what they
                   2801:         # did not ask us to change.
1.35      matthew  2802:         # If they are creating a new user but have not specified login
                   2803:         # information this will be caught below.
1.30      matthew  2804:     } else {
1.367     golterma 2805:             $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);
                   2806:             return;
1.27      matthew  2807:     }
1.164     albertel 2808: 
1.188     raeburn  2809:     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
1.367     golterma 2810:                         $env{'form.ccuname'}.' ('.&Apache::loncommon::plainname($env{'form.ccuname'},
                   2811:                         $env{'form.ccdomain'}).')', $env{'form.ccdomain'}).'</h3>');
                   2812:     my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,2);
1.344     bisitz   2813: 
1.193     raeburn  2814:     my (%alerts,%rulematch,%inst_results,%curr_rules);
1.334     raeburn  2815:     my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
1.361     raeburn  2816:     my @usertools = ('aboutme','blog','webdav','portfolio');
1.384     raeburn  2817:     my @requestcourses = ('official','unofficial','community','textbook');
1.362     raeburn  2818:     my @requestauthor = ('requestauthor');
1.286     raeburn  2819:     my ($othertitle,$usertypes,$types) = 
                   2820:         &Apache::loncommon::sorted_inst_types($env{'form.ccdomain'});
1.334     raeburn  2821:     my %canmodify_status =
                   2822:         &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},
                   2823:                                                    ['inststatus']);
1.101     albertel 2824:     if ($env{'form.makeuser'}) {
1.164     albertel 2825: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
1.27      matthew  2826:         # Check for the authentication mode and password
                   2827:         if (! $amode || ! $genpwd) {
1.193     raeburn  2828: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.27      matthew  2829: 	    return;
1.18      albertel 2830: 	}
1.29      matthew  2831:         # Determine desired host
1.101     albertel 2832:         my $desiredhost = $env{'form.hserver'};
1.29      matthew  2833:         if (lc($desiredhost) eq 'default') {
                   2834:             $desiredhost = undef;
                   2835:         } else {
1.147     albertel 2836:             my %home_servers = 
                   2837: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
1.29      matthew  2838:             if (! exists($home_servers{$desiredhost})) {
1.193     raeburn  2839:                 $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
                   2840:                 return;
                   2841:             }
                   2842:         }
                   2843:         # Check ID format
                   2844:         my %checkhash;
                   2845:         my %checks = ('id' => 1);
                   2846:         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
1.219     raeburn  2847:             'newuser' => $newuser, 
1.196     raeburn  2848:             'id' => $env{'form.cid'},
1.193     raeburn  2849:         );
1.196     raeburn  2850:         if ($env{'form.cid'} ne '') {
                   2851:             &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   2852:                                           \%rulematch,\%inst_results,\%curr_rules);
                   2853:             if (ref($alerts{'id'}) eq 'HASH') {
                   2854:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
                   2855:                     my $domdesc =
                   2856:                         &Apache::lonnet::domain($env{'form.ccdomain'},'description');
                   2857:                     if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
                   2858:                         my $userchkmsg;
                   2859:                         if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
                   2860:                             $userchkmsg  = 
                   2861:                                 &Apache::loncommon::instrule_disallow_msg('id',
                   2862:                                                                     $domdesc,1).
                   2863:                                 &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
                   2864:                                     $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
                   2865:                         }
                   2866:                         $r->print($error.&mt('Invalid ID format').$end.
                   2867:                                   $userchkmsg.$rtnlink);
                   2868:                         return;
                   2869:                     }
                   2870:                 }
1.29      matthew  2871:             }
                   2872:         }
1.367     golterma 2873:         &Apache::lonhtmlcommon::Increment_PrgWin($r, \%prog_state);
1.27      matthew  2874: 	# Call modifyuser
                   2875: 	my $result = &Apache::lonnet::modifyuser
1.193     raeburn  2876: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
1.188     raeburn  2877:              $amode,$genpwd,$env{'form.cfirstname'},
                   2878:              $env{'form.cmiddlename'},$env{'form.clastname'},
                   2879:              $env{'form.cgeneration'},undef,$desiredhost,
                   2880:              $env{'form.cpermanentemail'});
1.77      www      2881: 	$r->print(&mt('Generating user').': '.$result);
1.219     raeburn  2882:         $uhome = &Apache::lonnet::homeserver($env{'form.ccuname'},
1.101     albertel 2883:                                                $env{'form.ccdomain'});
1.334     raeburn  2884:         my (%changeHash,%newcustom,%changed,%changedinfo);
1.267     raeburn  2885:         if ($uhome ne 'no_host') {
1.334     raeburn  2886:             if ($context eq 'domain') {
1.378     raeburn  2887:                 foreach my $name ('portfolio','author') {
                   2888:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   2889:                         if ($env{'form.'.$name.'quota'} eq '') {
                   2890:                             $newcustom{$name.'quota'} = 0;
                   2891:                         } else {
                   2892:                             $newcustom{$name.'quota'} = $env{'form.'.$name.'quota'};
                   2893:                             $newcustom{$name.'quota'} =~ s/[^\d\.]//g;
                   2894:                         }
                   2895:                         if (&quota_admin($newcustom{$name.'quota'},\%changeHash,$name)) {
                   2896:                             $changed{$name.'quota'} = 1;
                   2897:                         }
1.334     raeburn  2898:                     }
                   2899:                 }
                   2900:                 foreach my $item (@usertools) {
                   2901:                     if ($env{'form.custom'.$item} == 1) {
                   2902:                         $newcustom{$item} = $env{'form.tools_'.$item};
                   2903:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2904:                                                      \%changeHash,'tools');
                   2905:                     }
1.267     raeburn  2906:                 }
1.334     raeburn  2907:                 foreach my $item (@requestcourses) {
1.341     raeburn  2908:                     if ($env{'form.custom'.$item} == 1) {
                   2909:                         $newcustom{$item} = $env{'form.crsreq_'.$item};
                   2910:                         if ($env{'form.crsreq_'.$item} eq 'autolimit') {
                   2911:                             $newcustom{$item} .= '=';
1.383     raeburn  2912:                             $env{'form.crsreq_'.$item.'_limit'} =~ s/\D+//g;
                   2913:                             if ($env{'form.crsreq_'.$item.'_limit'}) {
1.341     raeburn  2914:                                 $newcustom{$item} .= $env{'form.crsreq_'.$item.'_limit'};
                   2915:                             }
1.334     raeburn  2916:                         }
1.341     raeburn  2917:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2918:                                                       \%changeHash,'requestcourses');
1.334     raeburn  2919:                     }
1.275     raeburn  2920:                 }
1.362     raeburn  2921:                 if ($env{'form.customrequestauthor'} == 1) {
                   2922:                     $newcustom{'requestauthor'} = $env{'form.requestauthor'};
                   2923:                     $changed{'requestauthor'} = &tool_admin('requestauthor',
                   2924:                                                     $newcustom{'requestauthor'},
                   2925:                                                     \%changeHash,'requestauthor');
                   2926:                 }
1.406.2.5  raeburn  2927:                 if (&Apache::lonnet::allowed('cdh',$env{'request.role.domain'})) {
                   2928:                     my @adds = &Apache::loncommon::get_env_multiple('form.adhocroleadd');
                   2929:                     if (&adhocrole_changes(\%changeHash)) {
                   2930:                         $changed{'adhocroles.'.$env{'request.role.domain'}} = $changeHash{'adhocroles.'.$env{'request.role.domain'}};
                   2931:                     }
                   2932:                 }
1.275     raeburn  2933:             }
1.334     raeburn  2934:             if ($canmodify_status{'inststatus'}) {
                   2935:                 if (exists($env{'form.inststatus'})) {
                   2936:                     my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2937:                     if (@inststatuses > 0) {
                   2938:                         $changeHash{'inststatus'} = join(',',@inststatuses);
                   2939:                         $changed{'inststatus'} = $changeHash{'inststatus'};
1.306     raeburn  2940:                     }
                   2941:                 }
1.232     raeburn  2942:             }
1.334     raeburn  2943:             if (keys(%changed)) {
                   2944:                 foreach my $item (@userinfo) {
                   2945:                     $changeHash{$item}  = $env{'form.c'.$item};
1.286     raeburn  2946:                 }
1.267     raeburn  2947:                 my $chgresult =
                   2948:                      &Apache::lonnet::put('environment',\%changeHash,
                   2949:                                           $env{'form.ccdomain'},$env{'form.ccuname'});
                   2950:             } 
1.232     raeburn  2951:         }
1.219     raeburn  2952:         $r->print('<br />'.&mt('Home server').': '.$uhome.' '.
                   2953:                   &Apache::lonnet::hostname($uhome));
1.101     albertel 2954:     } elsif (($env{'form.login'} ne 'nochange') &&
                   2955:              ($env{'form.login'} ne ''        )) {
1.27      matthew  2956: 	# Modify user privileges
                   2957:         if (! $amode || ! $genpwd) {
1.193     raeburn  2958: 	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
1.27      matthew  2959: 	    return;
1.20      harris41 2960: 	}
1.395     bisitz   2961: 	# Only allow authentication modification if the person has authority
1.101     albertel 2962: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.20      harris41 2963: 	    $r->print('Modifying authentication: '.
1.31      matthew  2964:                       &Apache::lonnet::modifyuserauth(
1.101     albertel 2965: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
1.21      harris41 2966:                        $amode,$genpwd));
1.102     albertel 2967:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
1.101     albertel 2968: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
1.4       www      2969: 	} else {
1.27      matthew  2970: 	    # Okay, this is a non-fatal error.
1.395     bisitz   2971: 	    $r->print($error.&mt('You do not have the authority to modify this users authentication information.').$end);    
1.27      matthew  2972: 	}
1.28      matthew  2973:     }
1.344     bisitz   2974:     $r->rflush(); # Finish display of header before time consuming actions start
1.367     golterma 2975:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state);
1.28      matthew  2976:     ##
1.375     raeburn  2977:     my (@userroles,%userupdate,$cnum,$cdom,$defaultcredits,%namechanged);
1.213     raeburn  2978:     if ($context eq 'course') {
1.375     raeburn  2979:         ($cnum,$cdom) =
                   2980:             &Apache::lonuserutils::get_course_identity();
1.318     raeburn  2981:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
1.375     raeburn  2982:         if ($showcredits) {
                   2983:            $defaultcredits = &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   2984:         }
1.213     raeburn  2985:     }
1.101     albertel 2986:     if (! $env{'form.makeuser'} ) {
1.28      matthew  2987:         # Check for need to change
                   2988:         my %userenv = &Apache::lonnet::get
1.134     raeburn  2989:             ('environment',['firstname','middlename','lastname','generation',
1.378     raeburn  2990:              'id','permanentemail','portfolioquota','authorquota','inststatus',
                   2991:              'tools.aboutme','tools.blog','tools.webdav','tools.portfolio',
1.361     raeburn  2992:              'requestcourses.official','requestcourses.unofficial',
1.384     raeburn  2993:              'requestcourses.community','requestcourses.textbook',
                   2994:              'reqcrsotherdom.official','reqcrsotherdom.unofficial',
                   2995:              'reqcrsotherdom.community','reqcrsotherdom.textbook',
1.406.2.5  raeburn  2996:              'requestauthor','adhocroles.'.$env{'request.role.domain'}],
1.160     raeburn  2997:               $env{'form.ccdomain'},$env{'form.ccuname'});
1.28      matthew  2998:         my ($tmp) = keys(%userenv);
                   2999:         if ($tmp =~ /^(con_lost|error)/i) { 
                   3000:             %userenv = ();
                   3001:         }
1.206     raeburn  3002:         my $no_forceid_alert;
                   3003:         # Check to see if user information can be changed
                   3004:         my %domconfig =
                   3005:             &Apache::lonnet::get_dom('configuration',['usermodification'],
                   3006:                                      $env{'form.ccdomain'});
1.213     raeburn  3007:         my @statuses = ('active','future');
                   3008:         my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@statuses,undef,$env{'request.role.domain'});
                   3009:         my ($auname,$audom);
1.220     raeburn  3010:         if ($context eq 'author') {
1.206     raeburn  3011:             $auname = $env{'user.name'};
                   3012:             $audom = $env{'user.domain'};     
                   3013:         }
                   3014:         foreach my $item (keys(%roles)) {
1.220     raeburn  3015:             my ($rolenum,$roledom,$role) = split(/:/,$item,-1);
1.206     raeburn  3016:             if ($context eq 'course') {
                   3017:                 if ($cnum ne '' && $cdom ne '') {
                   3018:                     if ($rolenum eq $cnum && $roledom eq $cdom) {
                   3019:                         if (!grep(/^\Q$role\E$/,@userroles)) {
                   3020:                             push(@userroles,$role);
                   3021:                         }
                   3022:                     }
                   3023:                 }
                   3024:             } elsif ($context eq 'author') {
                   3025:                 if ($rolenum eq $auname && $roledom eq $audom) {
                   3026:                     if (!grep(/^\Q$role\E$/,@userroles)) { 
                   3027:                         push(@userroles,$role);
                   3028:                     }
                   3029:                 }
                   3030:             }
                   3031:         }
1.220     raeburn  3032:         if ($env{'form.action'} eq 'singlestudent') {
                   3033:             if (!grep(/^st$/,@userroles)) {
                   3034:                 push(@userroles,'st');
                   3035:             }
                   3036:         } else {
                   3037:             # Check for course or co-author roles being activated or re-enabled
                   3038:             if ($context eq 'author' || $context eq 'course') {
                   3039:                 foreach my $key (keys(%env)) {
                   3040:                     if ($context eq 'author') {
                   3041:                         if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
                   3042:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3043:                                 push(@userroles,$1);
                   3044:                             }
                   3045:                         } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
                   3046:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3047:                                 push(@userroles,$1);
                   3048:                             }
1.206     raeburn  3049:                         }
1.220     raeburn  3050:                     } elsif ($context eq 'course') {
                   3051:                         if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
                   3052:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3053:                                 push(@userroles,$1);
                   3054:                             }
                   3055:                         } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
                   3056:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3057:                                 push(@userroles,$1);
                   3058:                             }
1.206     raeburn  3059:                         }
                   3060:                     }
                   3061:                 }
                   3062:             }
                   3063:         }
                   3064:         #Check to see if we can change personal data for the user 
                   3065:         my (@mod_disallowed,@longroles);
                   3066:         foreach my $role (@userroles) {
                   3067:             if ($role eq 'cr') {
                   3068:                 push(@longroles,'Custom');
                   3069:             } else {
1.318     raeburn  3070:                 push(@longroles,&Apache::lonnet::plaintext($role,$crstype)); 
1.206     raeburn  3071:             }
                   3072:         }
1.219     raeburn  3073:         my %canmodify = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},\@userinfo,\@userroles);
                   3074:         foreach my $item (@userinfo) {
1.28      matthew  3075:             # Strip leading and trailing whitespace
1.203     raeburn  3076:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
1.219     raeburn  3077:             if (!$canmodify{$item}) {
1.207     raeburn  3078:                 if (defined($env{'form.c'.$item})) {
                   3079:                     if ($env{'form.c'.$item} ne $userenv{$item}) {
                   3080:                         push(@mod_disallowed,$item);
                   3081:                     }
1.206     raeburn  3082:                 }
                   3083:                 $env{'form.c'.$item} = $userenv{$item};
                   3084:             }
1.28      matthew  3085:         }
1.259     bisitz   3086:         # Check to see if we can change the Student/Employee ID
1.196     raeburn  3087:         my $forceid = $env{'form.forceid'};
                   3088:         my $recurseid = $env{'form.recurseid'};
                   3089:         my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
1.203     raeburn  3090:         my %uidhash = &Apache::lonnet::idrget($env{'form.ccdomain'},
                   3091:                                             $env{'form.ccuname'});
                   3092:         if (($uidhash{$env{'form.ccuname'}}) && 
                   3093:             ($uidhash{$env{'form.ccuname'}}!~/error\:/) && 
                   3094:             (!$forceid)) {
                   3095:             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
                   3096:                 $env{'form.cid'} = $userenv{'id'};
1.293     bisitz   3097:                 $no_forceid_alert = &mt('New student/employee ID does not match existing ID for this user.')
1.259     bisitz   3098:                                    .'<br />'
                   3099:                                    .&mt("Change is not permitted without checking the 'Force ID change' checkbox on the previous page.")
                   3100:                                    .'<br />'."\n";
1.203     raeburn  3101:             }
                   3102:         }
                   3103:         if ($env{'form.cid'} ne $userenv{'id'}) {
1.196     raeburn  3104:             my $checkhash;
                   3105:             my $checks = { 'id' => 1 };
                   3106:             $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
                   3107:                    { 'newuser' => $newuser,
                   3108:                      'id'  => $env{'form.cid'}, 
                   3109:                    };
                   3110:             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   3111:                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
                   3112:             if (ref($alerts{'id'}) eq 'HASH') {
                   3113:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
1.203     raeburn  3114:                    $env{'form.cid'} = $userenv{'id'};
1.196     raeburn  3115:                 }
                   3116:             }
                   3117:         }
1.378     raeburn  3118:         my (%quotachanged,%oldquota,%newquota,%olddefquota,%newdefquota, 
                   3119:             $oldinststatus,$newinststatus,%oldisdefault,%newisdefault,%oldsettings,
1.339     raeburn  3120:             %oldsettingstext,%newsettings,%newsettingstext,@disporder,
1.378     raeburn  3121:             %oldsettingstatus,%newsettingstatus);
1.334     raeburn  3122:         @disporder = ('inststatus');
                   3123:         if ($env{'request.role.domain'} eq $env{'form.ccdomain'}) {
1.362     raeburn  3124:             push(@disporder,'requestcourses','requestauthor');
1.334     raeburn  3125:         } else {
                   3126:             push(@disporder,'reqcrsotherdom');
                   3127:         }
                   3128:         push(@disporder,('quota','tools'));
1.338     raeburn  3129:         $oldinststatus = $userenv{'inststatus'};
1.378     raeburn  3130:         foreach my $name ('portfolio','author') {
                   3131:             ($olddefquota{$name},$oldsettingstatus{$name}) = 
                   3132:                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$oldinststatus,$name);
                   3133:             ($newdefquota{$name},$newsettingstatus{$name}) = ($olddefquota{$name},$oldsettingstatus{$name});
                   3134:         }
1.406.2.5  raeburn  3135:         push(@disporder,'adhocroles');
1.334     raeburn  3136:         my %canshow;
1.220     raeburn  3137:         if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
1.334     raeburn  3138:             $canshow{'quota'} = 1;
1.220     raeburn  3139:         }
1.267     raeburn  3140:         if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
1.334     raeburn  3141:             $canshow{'tools'} = 1;
1.267     raeburn  3142:         }
1.275     raeburn  3143:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
1.334     raeburn  3144:             $canshow{'requestcourses'} = 1;
1.300     raeburn  3145:         } elsif (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.334     raeburn  3146:             $canshow{'reqcrsotherdom'} = 1;
1.275     raeburn  3147:         }
1.286     raeburn  3148:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.334     raeburn  3149:             $canshow{'inststatus'} = 1;
1.286     raeburn  3150:         }
1.362     raeburn  3151:         if (&Apache::lonnet::allowed('cau',$env{'form.ccdomain'})) {
                   3152:             $canshow{'requestauthor'} = 1;
                   3153:         }
1.406.2.5  raeburn  3154:         if (&Apache::lonnet::allowed('cdh',$env{'request.role.domain'})) {
                   3155:             $canshow{'adhocroles'} = 1;
                   3156:         }
1.267     raeburn  3157:         my (%changeHash,%changed);
1.286     raeburn  3158:         if ($oldinststatus eq '') {
1.334     raeburn  3159:             $oldsettings{'inststatus'} = $othertitle; 
1.286     raeburn  3160:         } else {
                   3161:             if (ref($usertypes) eq 'HASH') {
1.334     raeburn  3162:                 $oldsettings{'inststatus'} = join(', ',map{ $usertypes->{ &unescape($_) }; } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  3163:             } else {
1.334     raeburn  3164:                 $oldsettings{'inststatus'} = join(', ',map{ &unescape($_); } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  3165:             }
                   3166:         }
                   3167:         $changeHash{'inststatus'} = $userenv{'inststatus'};
1.334     raeburn  3168:         if ($canmodify_status{'inststatus'}) {
                   3169:             $canshow{'inststatus'} = 1;
1.286     raeburn  3170:             if (exists($env{'form.inststatus'})) {
                   3171:                 my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   3172:                 if (@inststatuses > 0) {
                   3173:                     $newinststatus = join(':',map { &escape($_); } @inststatuses);
                   3174:                     $changeHash{'inststatus'} = $newinststatus;
                   3175:                     if ($newinststatus ne $oldinststatus) {
                   3176:                         $changed{'inststatus'} = $newinststatus;
1.378     raeburn  3177:                         foreach my $name ('portfolio','author') {
                   3178:                             ($newdefquota{$name},$newsettingstatus{$name}) =
                   3179:                                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   3180:                         }
1.286     raeburn  3181:                     }
                   3182:                     if (ref($usertypes) eq 'HASH') {
1.334     raeburn  3183:                         $newsettings{'inststatus'} = join(', ',map{ $usertypes->{$_}; } (@inststatuses)); 
1.286     raeburn  3184:                     } else {
1.337     raeburn  3185:                         $newsettings{'inststatus'} = join(', ',@inststatuses);
1.286     raeburn  3186:                     }
1.334     raeburn  3187:                 }
                   3188:             } else {
                   3189:                 $newinststatus = '';
                   3190:                 $changeHash{'inststatus'} = $newinststatus;
                   3191:                 $newsettings{'inststatus'} = $othertitle;
                   3192:                 if ($newinststatus ne $oldinststatus) {
                   3193:                     $changed{'inststatus'} = $changeHash{'inststatus'};
1.378     raeburn  3194:                     foreach my $name ('portfolio','author') {
                   3195:                         ($newdefquota{$name},$newsettingstatus{$name}) =
                   3196:                             &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   3197:                     }
1.286     raeburn  3198:                 }
                   3199:             }
1.334     raeburn  3200:         } elsif ($context ne 'selfcreate') {
                   3201:             $canshow{'inststatus'} = 1;
1.337     raeburn  3202:             $newsettings{'inststatus'} = $oldsettings{'inststatus'};
1.286     raeburn  3203:         }
1.378     raeburn  3204:         foreach my $name ('portfolio','author') {
                   3205:             $changeHash{$name.'quota'} = $userenv{$name.'quota'};
                   3206:         }
1.334     raeburn  3207:         if ($context eq 'domain') {
1.378     raeburn  3208:             foreach my $name ('portfolio','author') {
                   3209:                 if ($userenv{$name.'quota'} ne '') {
                   3210:                     $oldquota{$name} = $userenv{$name.'quota'};
                   3211:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   3212:                         if ($env{'form.'.$name.'quota'} eq '') {
                   3213:                             $newquota{$name} = 0;
                   3214:                         } else {
                   3215:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   3216:                             $newquota{$name} =~ s/[^\d\.]//g;
                   3217:                         }
                   3218:                         if ($newquota{$name} != $oldquota{$name}) {
                   3219:                             if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   3220:                                 $changed{$name.'quota'} = 1;
                   3221:                             }
                   3222:                         }
1.334     raeburn  3223:                     } else {
1.378     raeburn  3224:                         if (&quota_admin('',\%changeHash,$name)) {
                   3225:                             $changed{$name.'quota'} = 1;
                   3226:                             $newquota{$name} = $newdefquota{$name};
                   3227:                             $newisdefault{$name} = 1;
                   3228:                         }
1.334     raeburn  3229:                     }
1.149     raeburn  3230:                 } else {
1.378     raeburn  3231:                     $oldisdefault{$name} = 1;
                   3232:                     $oldquota{$name} = $olddefquota{$name};
                   3233:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   3234:                         if ($env{'form.'.$name.'quota'} eq '') {
                   3235:                             $newquota{$name} = 0;
                   3236:                         } else {
                   3237:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   3238:                             $newquota{$name} =~ s/[^\d\.]//g;
                   3239:                         }
                   3240:                         if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   3241:                             $changed{$name.'quota'} = 1;
                   3242:                         }
1.334     raeburn  3243:                     } else {
1.378     raeburn  3244:                         $newquota{$name} = $newdefquota{$name};
                   3245:                         $newisdefault{$name} = 1;
1.334     raeburn  3246:                     }
1.378     raeburn  3247:                 }
                   3248:                 if ($oldisdefault{$name}) {
                   3249:                     $oldsettingstext{'quota'}{$name} = &get_defaultquota_text($oldsettingstatus{$name});
1.383     raeburn  3250:                 }  else {
                   3251:                     $oldsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$oldquota{$name});
1.378     raeburn  3252:                 }
                   3253:                 if ($newisdefault{$name}) {
                   3254:                     $newsettingstext{'quota'}{$name} = &get_defaultquota_text($newsettingstatus{$name});
1.383     raeburn  3255:                 } else {
                   3256:                     $newsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$newquota{$name});
1.134     raeburn  3257:                 }
                   3258:             }
1.334     raeburn  3259:             &tool_changes('tools',\@usertools,\%oldsettings,\%oldsettingstext,\%userenv,
                   3260:                           \%changeHash,\%changed,\%newsettings,\%newsettingstext);
                   3261:             if ($env{'form.ccdomain'} eq $env{'request.role.domain'}) {
                   3262:                 &tool_changes('requestcourses',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   3263:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.384     raeburn  3264:                 &tool_changes('requestauthor',\@requestauthor,\%oldsettings,\%oldsettingstext,
                   3265:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  3266:             } else {
1.334     raeburn  3267:                 &tool_changes('reqcrsotherdom',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   3268:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  3269:             }
1.406.2.5  raeburn  3270:             if ($userenv{'adhocroles.'.$env{'request.role.domain'}}) {
                   3271:                 $changeHash{'adhocroles.'.$env{'request.role.domain'}} = $userenv{'adhocroles.'.$env{'request.role.domain'}};
                   3272:             }
                   3273:             if (&adhocrole_changes(\%changeHash,\%userenv)) {
                   3274:                 $changed{'adhocroles'} = 1;
                   3275:                 $oldsettings{'adhocroles'} = $userenv{'adhocroles.'.$env{'request.role.domain'}};
                   3276:                 $newsettings{'adhocroles'} = $changeHash{'adhocroles.'.$env{'request.role.domain'}};
                   3277:             }
1.149     raeburn  3278:         }
1.334     raeburn  3279:         foreach my $item (@userinfo) {
                   3280:             if ($env{'form.c'.$item} ne $userenv{$item}) {
                   3281:                 $namechanged{$item} = 1;
                   3282:             }
1.204     raeburn  3283:         }
1.378     raeburn  3284:         foreach my $name ('portfolio','author') {
1.390     bisitz   3285:             $oldsettings{'quota'}{$name} = &mt('[_1] MB',$oldquota{$name});
                   3286:             $newsettings{'quota'}{$name} = &mt('[_1] MB',$newquota{$name});
1.378     raeburn  3287:         }
1.334     raeburn  3288:         if ((keys(%namechanged) > 0) || (keys(%changed) > 0)) {
1.267     raeburn  3289:             my ($chgresult,$namechgresult);
                   3290:             if (keys(%changed) > 0) {
                   3291:                 $chgresult = 
1.204     raeburn  3292:                     &Apache::lonnet::put('environment',\%changeHash,
                   3293:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
1.267     raeburn  3294:                 if ($chgresult eq 'ok') {
                   3295:                     if (($env{'user.name'} eq $env{'form.ccuname'}) &&
                   3296:                         ($env{'user.domain'} eq $env{'form.ccdomain'})) {
1.270     raeburn  3297:                         my %newenvhash;
                   3298:                         foreach my $key (keys(%changed)) {
1.299     raeburn  3299:                             if (($key eq 'official') || ($key eq 'unofficial')
1.403     raeburn  3300:                                 || ($key eq 'community') || ($key eq 'textbook')) {
1.279     raeburn  3301:                                 $newenvhash{'environment.requestcourses.'.$key} =
                   3302:                                     $changeHash{'requestcourses.'.$key};
1.362     raeburn  3303:                                 if ($changeHash{'requestcourses.'.$key}) {
1.332     raeburn  3304:                                     $newenvhash{'environment.canrequest.'.$key} = 1;
1.279     raeburn  3305:                                 } else {
                   3306:                                     $newenvhash{'environment.canrequest.'.$key} =
                   3307:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3308:                                             $key,'reload','requestcourses');
                   3309:                                 }
1.362     raeburn  3310:                             } elsif ($key eq 'requestauthor') {
                   3311:                                 $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3312:                                 if ($changeHash{$key}) {
                   3313:                                     $newenvhash{'environment.canrequest.author'} = 1;
                   3314:                                 } else {
                   3315:                                     $newenvhash{'environment.canrequest.author'} =
                   3316:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3317:                                             $key,'reload','requestauthor');
                   3318:                                 }
1.406.2.5  raeburn  3319:                             } elsif ($key eq 'adhocroles') {
                   3320:                                 $newenvhash{'adhocroles.'.$env{'request.role.domain'}} =
                   3321:                                     $changeHash{'adhocroles.'.$env{'request.role.domain'}};
1.275     raeburn  3322:                             } elsif ($key ne 'quota') {
1.270     raeburn  3323:                                 $newenvhash{'environment.tools.'.$key} = 
                   3324:                                     $changeHash{'tools.'.$key};
1.279     raeburn  3325:                                 if ($changeHash{'tools.'.$key} ne '') {
                   3326:                                     $newenvhash{'environment.availabletools.'.$key} =
                   3327:                                         $changeHash{'tools.'.$key};
                   3328:                                 } else {
                   3329:                                     $newenvhash{'environment.availabletools.'.$key} =
1.367     golterma 3330:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3331:           $key,'reload','tools');
1.279     raeburn  3332:                                 }
1.270     raeburn  3333:                             }
                   3334:                         }
1.271     raeburn  3335:                         if (keys(%newenvhash)) {
                   3336:                             &Apache::lonnet::appenv(\%newenvhash);
                   3337:                         }
1.267     raeburn  3338:                     }
                   3339:                 }
1.204     raeburn  3340:             }
1.334     raeburn  3341:             if (keys(%namechanged) > 0) {
1.337     raeburn  3342:                 foreach my $field (@userinfo) {
                   3343:                     $changeHash{$field}  = $env{'form.c'.$field};
                   3344:                 }
                   3345: # Make the change
1.204     raeburn  3346:                 $namechgresult =
                   3347:                     &Apache::lonnet::modifyuser($env{'form.ccdomain'},
                   3348:                         $env{'form.ccuname'},$changeHash{'id'},undef,undef,
                   3349:                         $changeHash{'firstname'},$changeHash{'middlename'},
                   3350:                         $changeHash{'lastname'},$changeHash{'generation'},
1.337     raeburn  3351:                         $changeHash{'id'},undef,$changeHash{'permanentemail'},undef,\@userinfo);
1.220     raeburn  3352:                 %userupdate = (
                   3353:                                lastname   => $env{'form.clastname'},
                   3354:                                middlename => $env{'form.cmiddlename'},
                   3355:                                firstname  => $env{'form.cfirstname'},
                   3356:                                generation => $env{'form.cgeneration'},
                   3357:                                id         => $env{'form.cid'},
                   3358:                              );
1.204     raeburn  3359:             }
1.334     raeburn  3360:             if (((keys(%namechanged) > 0) && $namechgresult eq 'ok') || 
1.267     raeburn  3361:                 ((keys(%changed) > 0) && $chgresult eq 'ok')) {
1.28      matthew  3362:             # Tell the user we changed the name
1.334     raeburn  3363:                 &display_userinfo($r,1,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3364:                                   \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,
1.334     raeburn  3365:                                   \%oldsettings, \%oldsettingstext,\%newsettings,
                   3366:                                   \%newsettingstext);
1.203     raeburn  3367:                 if ($env{'form.cid'} ne $userenv{'id'}) {
                   3368:                     &Apache::lonnet::idput($env{'form.ccdomain'},
1.406.2.5  raeburn  3369:                          {$env{'form.ccuname'} => $env{'form.cid'}});
1.203     raeburn  3370:                     if (($recurseid) &&
                   3371:                         (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
                   3372:                         my $idresult = 
                   3373:                             &Apache::lonuserutils::propagate_id_change(
                   3374:                                 $env{'form.ccuname'},$env{'form.ccdomain'},
                   3375:                                 \%userupdate);
                   3376:                         $r->print('<br />'.$idresult.'<br />');
                   3377:                     }
1.196     raeburn  3378:                 }
1.149     raeburn  3379:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
                   3380:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
                   3381:                     my %newenvhash;
                   3382:                     foreach my $key (keys(%changeHash)) {
                   3383:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3384:                     }
1.238     raeburn  3385:                     &Apache::lonnet::appenv(\%newenvhash);
1.149     raeburn  3386:                 }
1.28      matthew  3387:             } else { # error occurred
1.389     bisitz   3388:                 $r->print(
                   3389:                     '<p class="LC_error">'
                   3390:                    .&mt('Unable to successfully change environment for [_1] in domain [_2].',
                   3391:                             '"'.$env{'form.ccuname'}.'"',
                   3392:                             '"'.$env{'form.ccdomain'}.'"')
                   3393:                    .'</p>');
1.28      matthew  3394:             }
1.334     raeburn  3395:         } else { # End of if ($env ... ) logic
1.275     raeburn  3396:             # They did not want to change the users name, quota, tool availability,
                   3397:             # or ability to request creation of courses, 
1.267     raeburn  3398:             # but we can still tell them what the name and quota and availabilities are  
1.334     raeburn  3399:             &display_userinfo($r,undef,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3400:                               \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,\%oldsettings,
1.334     raeburn  3401:                               \%oldsettingstext,\%newsettings,\%newsettingstext);
1.28      matthew  3402:         }
1.206     raeburn  3403:         if (@mod_disallowed) {
                   3404:             my ($rolestr,$contextname);
                   3405:             if (@longroles > 0) {
                   3406:                 $rolestr = join(', ',@longroles);
                   3407:             } else {
                   3408:                 $rolestr = &mt('No roles');
                   3409:             }
                   3410:             if ($context eq 'course') {
1.399     bisitz   3411:                 $contextname = 'course';
1.206     raeburn  3412:             } elsif ($context eq 'author') {
1.399     bisitz   3413:                 $contextname = 'co-author';
1.206     raeburn  3414:             }
                   3415:             $r->print(&mt('The following fields were not updated: ').'<ul>');
                   3416:             my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   3417:             foreach my $field (@mod_disallowed) {
                   3418:                 $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
                   3419:             }
1.207     raeburn  3420:             $r->print('</ul>');
                   3421:             if (@mod_disallowed == 1) {
1.399     bisitz   3422:                 $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  3423:             } else {
1.399     bisitz   3424:                 $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  3425:             }
1.292     bisitz   3426:             my $helplink = 'javascript:helpMenu('."'display'".')';
                   3427:             $r->print('<span class="LC_cusr_emph">'.$rolestr.'</span><br />'
                   3428:                      .&mt('Please contact your [_1]helpdesk[_2] for more information.'
                   3429:                          ,'<a href="'.$helplink.'">','</a>')
                   3430:                       .'<br />');
1.206     raeburn  3431:         }
1.259     bisitz   3432:         $r->print('<span class="LC_warning">'
                   3433:                   .$no_forceid_alert
                   3434:                   .&Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules)
                   3435:                   .'</span>');
1.4       www      3436:     }
1.367     golterma 3437:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.220     raeburn  3438:     if ($env{'form.action'} eq 'singlestudent') {
1.375     raeburn  3439:         &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context,
                   3440:                                $crstype,$showcredits,$defaultcredits);
1.386     bisitz   3441:         my $linktext = ($crstype eq 'Community' ?
                   3442:             &mt('Enroll Another Member') : &mt('Enroll Another Student'));
                   3443:         $r->print(
                   3444:             &Apache::lonhtmlcommon::actionbox([
                   3445:                 '<a href="javascript:backPage(document.userupdate)">'
                   3446:                .($crstype eq 'Community' ? 
                   3447:                     &mt('Enroll Another Member') : &mt('Enroll Another Student'))
                   3448:                .'</a>']));
1.220     raeburn  3449:     } else {
1.375     raeburn  3450:         my @rolechanges = &update_roles($r,$context,$showcredits);
1.334     raeburn  3451:         if (keys(%namechanged) > 0) {
1.220     raeburn  3452:             if ($context eq 'course') {
                   3453:                 if (@userroles > 0) {
1.225     raeburn  3454:                     if ((@rolechanges == 0) || 
                   3455:                         (!(grep(/^st$/,@rolechanges)))) {
                   3456:                         if (grep(/^st$/,@userroles)) {
                   3457:                             my $classlistupdated =
                   3458:                                 &Apache::lonuserutils::update_classlist($cdom,
1.220     raeburn  3459:                                               $cnum,$env{'form.ccdomain'},
                   3460:                                        $env{'form.ccuname'},\%userupdate);
1.225     raeburn  3461:                         }
1.220     raeburn  3462:                     }
                   3463:                 }
                   3464:             }
                   3465:         }
1.226     raeburn  3466:         my $userinfo = &Apache::loncommon::plainname($env{'form.ccuname'},
1.233     raeburn  3467:                                                      $env{'form.ccdomain'});
                   3468:         if ($env{'form.popup'}) {
                   3469:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   3470:         } else {
1.367     golterma 3471:             $r->print('<br />'.&Apache::lonhtmlcommon::actionbox(['<a href="javascript:backPage(document.userupdate,'."'$env{'form.prevphase'}','modify'".')">'
                   3472:                      .&mt('Modify this user: [_1]','<span class="LC_cusr_emph">'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.' ('.$userinfo.')</span>').'</a>',
                   3473:                      '<a href="javascript:backPage(document.userupdate)">'.&mt('Create/Modify Another User').'</a>']));
1.233     raeburn  3474:         }
1.220     raeburn  3475:     }
                   3476: }
                   3477: 
1.334     raeburn  3478: sub display_userinfo {
1.362     raeburn  3479:     my ($r,$changed,$order,$canshow,$requestcourses,$usertools,$requestauthor,
                   3480:         $userenv,$changedhash,$namechangedhash,$oldsetting,$oldsettingtext,
1.334     raeburn  3481:         $newsetting,$newsettingtext) = @_;
                   3482:     return unless (ref($order) eq 'ARRAY' &&
                   3483:                    ref($canshow) eq 'HASH' && 
                   3484:                    ref($requestcourses) eq 'ARRAY' && 
1.362     raeburn  3485:                    ref($requestauthor) eq 'ARRAY' &&
1.334     raeburn  3486:                    ref($usertools) eq 'ARRAY' && 
                   3487:                    ref($userenv) eq 'HASH' &&
                   3488:                    ref($changedhash) eq 'HASH' &&
                   3489:                    ref($oldsetting) eq 'HASH' &&
                   3490:                    ref($oldsettingtext) eq 'HASH' &&
                   3491:                    ref($newsetting) eq 'HASH' &&
                   3492:                    ref($newsettingtext) eq 'HASH');
                   3493:     my %lt=&Apache::lonlocal::texthash(
1.372     raeburn  3494:          'ui'             => 'User Information',
1.334     raeburn  3495:          'uic'            => 'User Information Changed',
                   3496:          'firstname'      => 'First Name',
                   3497:          'middlename'     => 'Middle Name',
                   3498:          'lastname'       => 'Last Name',
                   3499:          'generation'     => 'Generation',
                   3500:          'id'             => 'Student/Employee ID',
                   3501:          'permanentemail' => 'Permanent e-mail address',
1.378     raeburn  3502:          'portfolioquota' => 'Disk space allocated to portfolio files',
1.385     bisitz   3503:          'authorquota'    => 'Disk space allocated to Authoring Space',
1.334     raeburn  3504:          'blog'           => 'Blog Availability',
1.361     raeburn  3505:          'webdav'         => 'WebDAV Availability',
1.334     raeburn  3506:          'aboutme'        => 'Personal Information Page Availability',
                   3507:          'portfolio'      => 'Portfolio Availability',
                   3508:          'official'       => 'Can Request Official Courses',
                   3509:          'unofficial'     => 'Can Request Unofficial Courses',
                   3510:          'community'      => 'Can Request Communities',
1.384     raeburn  3511:          'textbook'       => 'Can Request Textbook Courses',
1.362     raeburn  3512:          'requestauthor'  => 'Can Request Author Role',
1.406.2.5  raeburn  3513:          'adhocroles'     => 'Ad Hoc Roles Selectable via Helpdesk Role',
1.334     raeburn  3514:          'inststatus'     => "Affiliation",
                   3515:          'prvs'           => 'Previous Value:',
                   3516:          'chto'           => 'Changed To:'
                   3517:     );
                   3518:     if ($changed) {
1.372     raeburn  3519:         $r->print('<h3>'.$lt{'uic'}.'</h3>'.
1.367     golterma 3520:                 &Apache::loncommon::start_data_table().
                   3521:                 &Apache::loncommon::start_data_table_header_row());
1.334     raeburn  3522:         $r->print("<th>&nbsp;</th>\n");
1.367     golterma 3523:         $r->print('<th><b>'.$lt{'prvs'}.'</b></th>');
                   3524:         $r->print('<th><span class="LC_nobreak"><b>'.$lt{'chto'}.'</b></span></th>');
                   3525:         $r->print(&Apache::loncommon::end_data_table_header_row());
                   3526:         my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
                   3527: 
1.334     raeburn  3528:         foreach my $item (@userinfo) {
                   3529:             my $value = $env{'form.c'.$item};
1.367     golterma 3530:             #show changes only:
1.383     raeburn  3531:             unless ($value eq $userenv->{$item}){
1.367     golterma 3532:                 $r->print(&Apache::loncommon::start_data_table_row());
                   3533:                 $r->print("<td>$lt{$item}</td>\n");
1.383     raeburn  3534:                 $r->print("<td>".$userenv->{$item}."</td>\n");
1.367     golterma 3535:                 $r->print("<td>$value </td>\n");
                   3536:                 $r->print(&Apache::loncommon::end_data_table_row());
1.334     raeburn  3537:             }
                   3538:         }
                   3539:         foreach my $entry (@{$order}) {
1.383     raeburn  3540:             if ($canshow->{$entry}) {
                   3541:                 if (($entry eq 'requestcourses') || ($entry eq 'reqcrsotherdom') || ($entry eq 'requestauthor')) {
                   3542:                     my @items;
                   3543:                     if ($entry eq 'requestauthor') {
                   3544:                         @items = ($entry);
                   3545:                     } else {
                   3546:                         @items = @{$requestcourses};
1.384     raeburn  3547:                     }
1.383     raeburn  3548:                     foreach my $item (@items) {
                   3549:                         if (($newsetting->{$item} ne $oldsetting->{$item}) || 
                   3550:                             ($newsettingtext->{$item} ne $oldsettingtext->{$item})) {
                   3551:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");  
                   3552:                             $r->print("<td>$lt{$item}</td>\n");
                   3553:                             $r->print("<td>".$oldsetting->{$item});
                   3554:                             if ($oldsettingtext->{$item}) {
                   3555:                                 if ($oldsetting->{$item}) {
                   3556:                                     $r->print(' -- ');
                   3557:                                 }
                   3558:                                 $r->print($oldsettingtext->{$item});
                   3559:                             }
                   3560:                             $r->print("</td>\n");
                   3561:                             $r->print("<td>".$newsetting->{$item});
                   3562:                             if ($newsettingtext->{$item}) {
                   3563:                                 if ($newsetting->{$item}) {
                   3564:                                     $r->print(' -- ');
                   3565:                                 }
                   3566:                                 $r->print($newsettingtext->{$item});
                   3567:                             }
                   3568:                             $r->print("</td>\n");
                   3569:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3570:                         }
                   3571:                     }
                   3572:                 } elsif ($entry eq 'tools') {
                   3573:                     foreach my $item (@{$usertools}) {
1.383     raeburn  3574:                         if ($newsetting->{$item} ne $oldsetting->{$item}) {
                   3575:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3576:                             $r->print("<td>$lt{$item}</td>\n");
                   3577:                             $r->print("<td>".$oldsetting->{$item}.' '.$oldsettingtext->{$item}."</td>\n");
                   3578:                             $r->print("<td>".$newsetting->{$item}.' '.$newsettingtext->{$item}."</td>\n");
                   3579:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3580:                         }
                   3581:                     }
1.378     raeburn  3582:                 } elsif ($entry eq 'quota') {
                   3583:                     if ((ref($oldsetting->{$entry}) eq 'HASH') && (ref($oldsettingtext->{$entry}) eq 'HASH') &&
                   3584:                         (ref($newsetting->{$entry}) eq 'HASH') && (ref($newsettingtext->{$entry}) eq 'HASH')) {
                   3585:                         foreach my $name ('portfolio','author') {
1.383     raeburn  3586:                             if ($newsetting->{$entry}->{$name} ne $oldsetting->{$entry}->{$name}) {
                   3587:                                 $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3588:                                 $r->print("<td>$lt{$name.$entry}</td>\n");
                   3589:                                 $r->print("<td>".$oldsettingtext->{$entry}->{$name}."</td>\n");
                   3590:                                 $r->print("<td>".$newsettingtext->{$entry}->{$name}."</td>\n");
                   3591:                                 $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.378     raeburn  3592:                             }
                   3593:                         }
                   3594:                     }
1.334     raeburn  3595:                 } else {
1.383     raeburn  3596:                     if ($newsetting->{$entry} ne $oldsetting->{$entry}) {
                   3597:                         $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3598:                         $r->print("<td>$lt{$entry}</td>\n");
                   3599:                         $r->print("<td>".$oldsetting->{$entry}.' '.$oldsettingtext->{$entry}."</td>\n");
                   3600:                         $r->print("<td>".$newsetting->{$entry}.' '.$newsettingtext->{$entry}."</td>\n");
                   3601:                         $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3602:                     }
                   3603:                 }
                   3604:             }
                   3605:         }
1.367     golterma 3606:         $r->print(&Apache::loncommon::end_data_table().'<br />');
1.372     raeburn  3607:     } else {
                   3608:         $r->print('<h3>'.$lt{'ui'}.'</h3>'.
                   3609:                   '<p>'.&mt('No changes made to user information').'</p>');
1.334     raeburn  3610:     }
                   3611:     return;
                   3612: }
                   3613: 
1.275     raeburn  3614: sub tool_changes {
                   3615:     my ($context,$usertools,$oldaccess,$oldaccesstext,$userenv,$changeHash,
                   3616:         $changed,$newaccess,$newaccesstext) = @_;
                   3617:     if (!((ref($usertools) eq 'ARRAY') && (ref($oldaccess) eq 'HASH') &&
                   3618:           (ref($oldaccesstext) eq 'HASH') && (ref($userenv) eq 'HASH') &&
                   3619:           (ref($changeHash) eq 'HASH') && (ref($changed) eq 'HASH') &&
                   3620:           (ref($newaccess) eq 'HASH') && (ref($newaccesstext) eq 'HASH'))) {
                   3621:         return;
                   3622:     }
1.383     raeburn  3623:     my %reqdisplay = &requestchange_display();
1.300     raeburn  3624:     if ($context eq 'reqcrsotherdom') {
1.309     raeburn  3625:         my @options = ('approval','validate','autolimit');
1.306     raeburn  3626:         my $optregex = join('|',@options);
1.300     raeburn  3627:         my $cdom = $env{'request.role.domain'};
                   3628:         foreach my $tool (@{$usertools}) {
1.383     raeburn  3629:             $oldaccesstext->{$tool} = &mt("availability set to 'off'");
1.314     raeburn  3630:             $newaccesstext->{$tool} = $oldaccesstext->{$tool};
1.300     raeburn  3631:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
1.383     raeburn  3632:             my ($newop,$limit);
1.314     raeburn  3633:             if ($env{'form.'.$context.'_'.$tool}) {
                   3634:                 $newop = $env{'form.'.$context.'_'.$tool};
                   3635:                 if ($newop eq 'autolimit') {
1.383     raeburn  3636:                     $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
1.314     raeburn  3637:                     $limit =~ s/\D+//g;
                   3638:                     $newop .= '='.$limit;
                   3639:                 }
                   3640:             }
1.300     raeburn  3641:             if ($userenv->{$context.'.'.$tool} eq '') {
1.314     raeburn  3642:                 if ($newop) {
                   3643:                     $changed->{$tool}=&tool_admin($tool,$cdom.':'.$newop,
1.300     raeburn  3644:                                                   $changeHash,$context);
                   3645:                     if ($changed->{$tool}) {
1.383     raeburn  3646:                         if ($newop =~ /^autolimit/) {
                   3647:                             if ($limit) {
                   3648:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3649:                             } else {
                   3650:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3651:                             }
                   3652:                         } else {
                   3653:                             $newaccesstext->{$tool} = $reqdisplay{$newop};
                   3654:                         }
1.300     raeburn  3655:                     } else {
                   3656:                         $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3657:                     }
                   3658:                 }
                   3659:             } else {
                   3660:                 my @curr = split(',',$userenv->{$context.'.'.$tool});
                   3661:                 my @new;
                   3662:                 my $changedoms;
1.314     raeburn  3663:                 foreach my $req (@curr) {
                   3664:                     if ($req =~ /^\Q$cdom\E\:($optregex\=?\d*)$/) {
                   3665:                         my $oldop = $1;
1.383     raeburn  3666:                         if ($oldop =~ /^autolimit=(\d*)/) {
                   3667:                             my $limit = $1;
                   3668:                             if ($limit) {
                   3669:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3670:                             } else {
                   3671:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3672:                             }
                   3673:                         } else {
                   3674:                             $oldaccesstext->{$tool} = $reqdisplay{$oldop};
                   3675:                         }
1.314     raeburn  3676:                         if ($oldop ne $newop) {
                   3677:                             $changedoms = 1;
                   3678:                             foreach my $item (@curr) {
                   3679:                                 my ($reqdom,$option) = split(':',$item);
                   3680:                                 unless ($reqdom eq $cdom) {
                   3681:                                     push(@new,$item);
                   3682:                                 }
                   3683:                             }
                   3684:                             if ($newop) {
                   3685:                                 push(@new,$cdom.':'.$newop);
1.300     raeburn  3686:                             }
1.314     raeburn  3687:                             @new = sort(@new);
1.300     raeburn  3688:                         }
1.314     raeburn  3689:                         last;
1.300     raeburn  3690:                     }
1.314     raeburn  3691:                 }
                   3692:                 if ((!$changedoms) && ($newop)) {
1.300     raeburn  3693:                     $changedoms = 1;
1.306     raeburn  3694:                     @new = sort(@curr,$cdom.':'.$newop);
1.300     raeburn  3695:                 }
                   3696:                 if ($changedoms) {
1.314     raeburn  3697:                     my $newdomstr;
1.300     raeburn  3698:                     if (@new) {
                   3699:                         $newdomstr = join(',',@new);
                   3700:                     }
                   3701:                     $changed->{$tool}=&tool_admin($tool,$newdomstr,$changeHash,
                   3702:                                                   $context);
                   3703:                     if ($changed->{$tool}) {
                   3704:                         if ($env{'form.'.$context.'_'.$tool}) {
1.306     raeburn  3705:                             if ($env{'form.'.$context.'_'.$tool} eq 'autolimit') {
1.314     raeburn  3706:                                 my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3707:                                 $limit =~ s/\D+//g;
                   3708:                                 if ($limit) {
1.383     raeburn  3709:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
1.314     raeburn  3710:                                 } else {
1.383     raeburn  3711:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
1.306     raeburn  3712:                                 }
1.314     raeburn  3713:                             } else {
1.306     raeburn  3714:                                 $newaccesstext->{$tool} = $reqdisplay{$env{'form.'.$context.'_'.$tool}};
                   3715:                             }
1.300     raeburn  3716:                         } else {
1.383     raeburn  3717:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
1.300     raeburn  3718:                         }
                   3719:                     }
                   3720:                 }
                   3721:             }
                   3722:         }
                   3723:         return;
                   3724:     }
1.275     raeburn  3725:     foreach my $tool (@{$usertools}) {
1.383     raeburn  3726:         my ($newval,$limit,$envkey);
1.362     raeburn  3727:         $envkey = $context.'.'.$tool;
1.306     raeburn  3728:         if ($context eq 'requestcourses') {
                   3729:             $newval = $env{'form.crsreq_'.$tool};
                   3730:             if ($newval eq 'autolimit') {
1.383     raeburn  3731:                 $limit = $env{'form.crsreq_'.$tool.'_limit'};
                   3732:                 $limit =~ s/\D+//g;
                   3733:                 $newval .= '='.$limit;
1.306     raeburn  3734:             }
1.362     raeburn  3735:         } elsif ($context eq 'requestauthor') {
                   3736:             $newval = $env{'form.'.$context};
                   3737:             $envkey = $context;
1.314     raeburn  3738:         } else {
1.306     raeburn  3739:             $newval = $env{'form.'.$context.'_'.$tool};
                   3740:         }
1.362     raeburn  3741:         if ($userenv->{$envkey} ne '') {
1.275     raeburn  3742:             $oldaccess->{$tool} = &mt('custom');
1.383     raeburn  3743:             if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3744:                 if ($userenv->{$envkey} =~ /^autolimit=(\d*)$/) {
                   3745:                     my $currlimit = $1;
                   3746:                     if ($currlimit eq '') {
                   3747:                         $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3748:                     } else {
                   3749:                         $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$currlimit);
                   3750:                     }
                   3751:                 } elsif ($userenv->{$envkey}) {
                   3752:                     $oldaccesstext->{$tool} = $reqdisplay{$userenv->{$envkey}};
                   3753:                 } else {
                   3754:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3755:                 }
1.275     raeburn  3756:             } else {
1.383     raeburn  3757:                 if ($userenv->{$envkey}) {
                   3758:                     $oldaccesstext->{$tool} = &mt("availability set to 'on'");
                   3759:                 } else {
                   3760:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3761:                 }
1.275     raeburn  3762:             }
1.362     raeburn  3763:             $changeHash->{$envkey} = $userenv->{$envkey};
1.275     raeburn  3764:             if ($env{'form.custom'.$tool} == 1) {
1.362     raeburn  3765:                 if ($newval ne $userenv->{$envkey}) {
1.306     raeburn  3766:                     $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3767:                                                     $context);
1.275     raeburn  3768:                     if ($changed->{$tool}) {
                   3769:                         $newaccess->{$tool} = &mt('custom');
1.383     raeburn  3770:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3771:                             if ($newval =~ /^autolimit/) {
                   3772:                                 if ($limit) {
                   3773:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3774:                                 } else {
                   3775:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3776:                                 }
                   3777:                             } elsif ($newval) {
                   3778:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3779:                             } else {
                   3780:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3781:                             }
1.275     raeburn  3782:                         } else {
1.383     raeburn  3783:                             if ($newval) {
                   3784:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3785:                             } else {
                   3786:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3787:                             }
1.275     raeburn  3788:                         }
                   3789:                     } else {
                   3790:                         $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  3791:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3792:                             if ($newval =~ /^autolimit/) {
                   3793:                                 if ($limit) {
                   3794:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3795:                                 } else {
                   3796:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3797:                                 }
                   3798:                             } elsif ($newval) {
                   3799:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3800:                             } else {
                   3801:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3802:                             }
1.275     raeburn  3803:                         } else {
1.383     raeburn  3804:                             if ($userenv->{$context.'.'.$tool}) {
                   3805:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3806:                             } else {
                   3807:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3808:                             }
1.275     raeburn  3809:                         }
                   3810:                     }
                   3811:                 } else {
                   3812:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3813:                     $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3814:                 }
                   3815:             } else {
                   3816:                 $changed->{$tool} = &tool_admin($tool,'',$changeHash,$context);
                   3817:                 if ($changed->{$tool}) {
                   3818:                     $newaccess->{$tool} = &mt('default');
                   3819:                 } else {
                   3820:                     $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  3821:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3822:                         if ($newval =~ /^autolimit/) {
                   3823:                             if ($limit) {
                   3824:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3825:                             } else {
                   3826:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3827:                             }
                   3828:                         } elsif ($newval) {
                   3829:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3830:                         } else {
                   3831:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3832:                         }
1.275     raeburn  3833:                     } else {
1.383     raeburn  3834:                         if ($userenv->{$context.'.'.$tool}) {
                   3835:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3836:                         } else {
                   3837:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3838:                         }
1.275     raeburn  3839:                     }
                   3840:                 }
                   3841:             }
                   3842:         } else {
                   3843:             $oldaccess->{$tool} = &mt('default');
                   3844:             if ($env{'form.custom'.$tool} == 1) {
1.306     raeburn  3845:                 $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3846:                                                 $context);
1.275     raeburn  3847:                 if ($changed->{$tool}) {
                   3848:                     $newaccess->{$tool} = &mt('custom');
1.383     raeburn  3849:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3850:                         if ($newval =~ /^autolimit/) {
                   3851:                             if ($limit) {
                   3852:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3853:                             } else {
                   3854:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3855:                             }
                   3856:                         } elsif ($newval) {
                   3857:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3858:                         } else {
                   3859:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3860:                         }
1.275     raeburn  3861:                     } else {
1.383     raeburn  3862:                         if ($newval) {
                   3863:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3864:                         } else {
                   3865:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3866:                         }
1.275     raeburn  3867:                     }
                   3868:                 } else {
                   3869:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3870:                 }
                   3871:             } else {
                   3872:                 $newaccess->{$tool} = $oldaccess->{$tool};
                   3873:             }
                   3874:         }
                   3875:     }
                   3876:     return;
                   3877: }
                   3878: 
1.406.2.5  raeburn  3879: sub adhocrole_changes {
                   3880:     my ($changehashref,$userenv) = @_;
                   3881:     my @adds = &Apache::loncommon::get_env_multiple('form.adhocroleadd');
                   3882:     my @dels = &Apache::loncommon::get_env_multiple('form.adhocroledel');
                   3883:     my (@saved,@added,@alladhoc,$changed);
                   3884:     my $adhoc_key = 'adhocroles.'.$env{'request.role.domain'};
                   3885:     if (!$env{'form.makeuser'}) {
                   3886:         if (ref($userenv) eq 'HASH') {
                   3887:             my @current;
                   3888:             if ($userenv->{$adhoc_key}) {
                   3889:                 @current = split(/,/,$userenv->{$adhoc_key});
                   3890:                 if (@dels) {
                   3891:                     foreach my $curr (@current) {
                   3892:                         next if ($curr eq '');
                   3893:                         unless (grep(/\Q$curr\E$/,@dels)) {
                   3894:                             push(@saved,$curr);
                   3895:                         }
                   3896:                     }
                   3897:                     $changed = 1;
                   3898:                 } else {
                   3899:                     @saved = @current;
                   3900:                 }
                   3901:             }
                   3902:         }
                   3903:     }
                   3904:     if (@adds) {
                   3905:         my $confname = &Apache::lonnet::get_domainconfiguser($env{'request.role.domain'});
                   3906:         my %existing=&Apache::lonnet::dump('roles',$env{'request.role.domain'},
                   3907:                                            $confname,'rolesdef_');
                   3908:         foreach my $poss (@adds) {
                   3909:             if (exists($existing{'rolesdef_'.$poss})) {
                   3910:                 push(@added,$poss);
                   3911:                 $changed = 1;
                   3912:             }
                   3913:         }
                   3914:     }
                   3915:     if (@added) {
                   3916:         if (@saved) {
                   3917:             foreach my $add (@added) {
                   3918:                 unless (grep(/^\Q$add\E$/,@saved)) {
                   3919:                     push(@alladhoc,$add);
                   3920:                 }
                   3921:             }
                   3922:         } else {
                   3923:             push(@alladhoc,@added);
                   3924:         }
                   3925:     }
                   3926:     if (@saved) {
                   3927:         push(@alladhoc,@saved);
                   3928:     }
                   3929:     if (@alladhoc) {
                   3930:         my $adhocstr = join(',',sort(@alladhoc));
                   3931:         $changehashref->{$adhoc_key} = $adhocstr;
                   3932:     } elsif (@dels) {
                   3933:         &Apache::lonnet::del('environment',[$adhoc_key],$env{'form.ccdomain'},$env{'form.ccuname'});
                   3934:         delete($changehashref->{$adhoc_key});
                   3935:         if (($env{'form.ccdomain'} eq $env{'user.domain'}) &&
                   3936:             ($env{'form.ccuname'} eq $env{'user.name'})) {
                   3937:             &Apache::lonnet::delenv($adhoc_key);
                   3938:         }
                   3939:     }
                   3940:     return $changed;
                   3941: }
                   3942: 
1.220     raeburn  3943: sub update_roles {
1.375     raeburn  3944:     my ($r,$context,$showcredits) = @_;
1.4       www      3945:     my $now=time;
1.225     raeburn  3946:     my @rolechanges;
1.220     raeburn  3947:     my %disallowed;
1.73      sakharuk 3948:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
1.404     raeburn  3949:     foreach my $key (keys(%env)) {
1.135     raeburn  3950: 	next if (! $env{$key});
1.190     raeburn  3951:         next if ($key eq 'form.action');
1.27      matthew  3952: 	# Revoke roles
1.135     raeburn  3953: 	if ($key=~/^form\.rev/) {
                   3954: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
1.64      www      3955: # Revoke standard role
1.170     albertel 3956: 		my ($scope,$role) = ($1,$2);
                   3957: 		my $result =
                   3958: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
                   3959: 						$env{'form.ccuname'},
1.239     raeburn  3960: 						$scope,$role,'','',$context);
1.367     golterma 3961:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3962:                             &mt('Revoking [_1] in [_2]',
                   3963:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  3964:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   3965:                                 $result ne "ok").'<br />');
                   3966:                 if ($result ne "ok") {
                   3967:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3968:                 }
1.170     albertel 3969: 		if ($role eq 'st') {
1.202     raeburn  3970: 		    my $result = 
1.198     raeburn  3971:                         &Apache::lonuserutils::classlist_drop($scope,
                   3972:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3973: 			    $now);
1.367     golterma 3974:                     $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.53      www      3975: 		}
1.225     raeburn  3976:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3977:                     push(@rolechanges,$role);
                   3978:                 }
1.196     raeburn  3979: 	    }
1.195     raeburn  3980: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}s) {
1.64      www      3981: # Revoke custom role
1.369     bisitz   3982:                 my $result = &Apache::lonnet::revokecustomrole(
                   3983:                     $env{'form.ccdomain'},$env{'form.ccuname'},$1,$2,$3,$4,'','',$context);
1.367     golterma 3984:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3985:                             &mt('Revoking custom role [_1] by [_2] in [_3]',
1.372     raeburn  3986:                                 $4,$3.':'.$2,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3987:                             $result ne 'ok').'<br />');
                   3988:                 if ($result ne "ok") {
                   3989:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3990:                 }
1.225     raeburn  3991:                 if (!grep(/^cr$/,@rolechanges)) {
                   3992:                     push(@rolechanges,'cr');
                   3993:                 }
1.64      www      3994: 	    }
1.135     raeburn  3995: 	} elsif ($key=~/^form\.del/) {
                   3996: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
1.116     raeburn  3997: # Delete standard role
1.170     albertel 3998: 		my ($scope,$role) = ($1,$2);
                   3999: 		my $result =
                   4000: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
                   4001: 						$env{'form.ccuname'},
1.239     raeburn  4002: 						$scope,$role,$now,0,1,'',
                   4003:                                                 $context);
1.367     golterma 4004:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   4005:                             &mt('Deleting [_1] in [_2]',
1.369     bisitz   4006:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  4007:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   4008:                             $result ne 'ok').'<br />');
                   4009:                 if ($result ne "ok") {
                   4010:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4011:                 }
1.367     golterma 4012: 
1.170     albertel 4013: 		if ($role eq 'st') {
1.202     raeburn  4014: 		    my $result = 
1.198     raeburn  4015:                         &Apache::lonuserutils::classlist_drop($scope,
                   4016:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  4017: 			    $now);
1.369     bisitz   4018: 		    $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.81      albertel 4019: 		}
1.225     raeburn  4020:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   4021:                     push(@rolechanges,$role);
                   4022:                 }
1.116     raeburn  4023:             }
1.139     albertel 4024: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  4025:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   4026: # Delete custom role
1.369     bisitz   4027:                 my $result =
                   4028:                     &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
                   4029:                         $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
                   4030:                         0,1,$context);
                   4031:                 $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Deleting custom role [_1] by [_2] in [_3]',
1.372     raeburn  4032:                       $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   4033:                       $result ne "ok").'<br />');
                   4034:                 if ($result ne "ok") {
                   4035:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4036:                 }
1.367     golterma 4037: 
1.225     raeburn  4038:                 if (!grep(/^cr$/,@rolechanges)) {
                   4039:                     push(@rolechanges,'cr');
                   4040:                 }
1.116     raeburn  4041:             }
1.135     raeburn  4042: 	} elsif ($key=~/^form\.ren/) {
1.101     albertel 4043:             my $udom = $env{'form.ccdomain'};
                   4044:             my $uname = $env{'form.ccuname'};
1.116     raeburn  4045: # Re-enable standard role
1.135     raeburn  4046: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
1.89      raeburn  4047:                 my $url = $1;
                   4048:                 my $role = $2;
                   4049:                 my $logmsg;
                   4050:                 my $output;
                   4051:                 if ($role eq 'st') {
1.141     albertel 4052:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
1.374     raeburn  4053:                         my ($cdom,$cnum,$csec) = ($1,$2,$3);
1.375     raeburn  4054:                         my $credits;
                   4055:                         if ($showcredits) {
                   4056:                             my $defaultcredits = 
                   4057:                                 &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   4058:                             $credits = &get_user_credits($defaultcredits,$cdom,$cnum);
                   4059:                         }
                   4060:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$cdom,$cnum,$csec,$context,$credits);
1.220     raeburn  4061:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course') || ($result eq 'refused')) {
1.223     raeburn  4062:                             if ($result eq 'refused' && $logmsg) {
                   4063:                                 $output = $logmsg;
                   4064:                             } else { 
1.369     bisitz   4065:                                 $output = &mt('Error: [_1]',$result)."\n";
1.223     raeburn  4066:                             }
1.89      raeburn  4067:                         } else {
1.372     raeburn  4068:                             $output = &Apache::lonhtmlcommon::confirm_success(&mt('Assigning [_1] in [_2] starting [_3]',
                   4069:                                         &Apache::lonnet::plaintext($role),
                   4070:                                         &Apache::loncommon::show_role_extent($url,$context,'st'),
                   4071:                                         &Apache::lonlocal::locallocaltime($now))).'<br />'.$logmsg.'<br />';
1.89      raeburn  4072:                         }
                   4073:                     }
                   4074:                 } else {
1.101     albertel 4075: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
1.239     raeburn  4076:                                $env{'form.ccuname'},$url,$role,0,$now,'','',
                   4077:                                $context);
1.367     golterma 4078:                         $output = &Apache::lonhtmlcommon::confirm_success(&mt('Re-enabling [_1] in [_2]',
1.372     raeburn  4079:                                         &Apache::lonnet::plaintext($role),
                   4080:                                         &Apache::loncommon::show_role_extent($url,$context,$role)),$result ne "ok").'<br />';
1.369     bisitz   4081:                     if ($result ne "ok") {
                   4082:                         $output .= &mt('Error: [_1]',$result).'<br />';
                   4083:                     }
                   4084:                 }
1.89      raeburn  4085:                 $r->print($output);
1.225     raeburn  4086:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   4087:                     push(@rolechanges,$role);
                   4088:                 }
1.113     raeburn  4089: 	    }
1.116     raeburn  4090: # Re-enable custom role
1.139     albertel 4091: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  4092:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   4093:                 my $result = &Apache::lonnet::assigncustomrole(
                   4094:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
1.240     raeburn  4095:                                $url,$rdom,$rnam,$rolename,0,$now,undef,$context);
1.369     bisitz   4096:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   4097:                     &mt('Re-enabling custom role [_1] by [_2] in [_3]',
1.372     raeburn  4098:                         $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   4099:                     $result ne "ok").'<br />');
                   4100:                 if ($result ne "ok") {
                   4101:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4102:                 }
1.225     raeburn  4103:                 if (!grep(/^cr$/,@rolechanges)) {
                   4104:                     push(@rolechanges,'cr');
                   4105:                 }
1.116     raeburn  4106:             }
1.135     raeburn  4107: 	} elsif ($key=~/^form\.act/) {
1.101     albertel 4108:             my $udom = $env{'form.ccdomain'};
                   4109:             my $uname = $env{'form.ccuname'};
1.141     albertel 4110: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
1.65      www      4111:                 # Activate a custom role
1.83      albertel 4112: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
                   4113: 		my $url='/'.$one.'/'.$two;
                   4114: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
1.65      www      4115: 
1.101     albertel 4116:                 my $start = ( $env{'form.start_'.$full} ?
                   4117:                               $env{'form.start_'.$full} :
1.88      raeburn  4118:                               $now );
1.101     albertel 4119:                 my $end   = ( $env{'form.end_'.$full} ?
                   4120:                               $env{'form.end_'.$full} :
1.88      raeburn  4121:                               0 );
                   4122:                                                                                      
                   4123:                 # split multiple sections
                   4124:                 my %sections = ();
1.101     albertel 4125:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
1.88      raeburn  4126:                 if ($num_sections == 0) {
1.240     raeburn  4127:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end,$context));
1.88      raeburn  4128:                 } else {
1.114     albertel 4129: 		    my %curr_groups =
1.117     raeburn  4130: 			&Apache::longroup::coursegroups($one,$two);
1.404     raeburn  4131:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.113     raeburn  4132:                         if (($sec eq 'none') || ($sec eq 'all') || 
                   4133:                             exists($curr_groups{$sec})) {
                   4134:                             $disallowed{$sec} = $url;
                   4135:                             next;
                   4136:                         }
                   4137:                         my $securl = $url.'/'.$sec;
1.240     raeburn  4138: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end,$context));
1.88      raeburn  4139:                     }
                   4140:                 }
1.225     raeburn  4141:                 if (!grep(/^cr$/,@rolechanges)) {
                   4142:                     push(@rolechanges,'cr');
                   4143:                 }
1.142     raeburn  4144: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
1.27      matthew  4145: 		# Activate roles for sections with 3 id numbers
                   4146: 		# set start, end times, and the url for the class
1.83      albertel 4147: 		my ($one,$two,$three)=($1,$2,$3);
1.101     albertel 4148: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
                   4149: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
1.27      matthew  4150: 			      $now );
1.101     albertel 4151: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
                   4152: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  4153: 			      0 );
1.83      albertel 4154: 		my $url='/'.$one.'/'.$two;
1.88      raeburn  4155:                 my $type = 'three';
                   4156:                 # split multiple sections
                   4157:                 my %sections = ();
1.101     albertel 4158:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
1.375     raeburn  4159:                 my $credits;
                   4160:                 if ($three eq 'st') {
                   4161:                     if ($showcredits) { 
                   4162:                         my $defaultcredits = 
                   4163:                             &Apache::lonuserutils::get_defaultcredits($one,$two);
                   4164:                         $credits = $env{'form.credits_'.$one.'_'.$two.'_'.$three};
                   4165:                         $credits =~ s/[^\d\.]//g;
                   4166:                         if ($credits eq $defaultcredits) {
                   4167:                             undef($credits);
                   4168:                         }
                   4169:                     }
                   4170:                 }
1.88      raeburn  4171:                 if ($num_sections == 0) {
1.375     raeburn  4172:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  4173:                 } else {
1.114     albertel 4174:                     my %curr_groups = 
1.117     raeburn  4175: 			&Apache::longroup::coursegroups($one,$two);
1.88      raeburn  4176:                     my $emptysec = 0;
1.404     raeburn  4177:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.88      raeburn  4178:                         $sec =~ s/\W//g;
1.113     raeburn  4179:                         if ($sec ne '') {
                   4180:                             if (($sec eq 'none') || ($sec eq 'all') || 
                   4181:                                 exists($curr_groups{$sec})) {
                   4182:                                 $disallowed{$sec} = $url;
                   4183:                                 next;
                   4184:                             }
1.88      raeburn  4185:                             my $securl = $url.'/'.$sec;
1.375     raeburn  4186:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec,$context,$credits));
1.88      raeburn  4187:                         } else {
                   4188:                             $emptysec = 1;
                   4189:                         }
                   4190:                     }
                   4191:                     if ($emptysec) {
1.375     raeburn  4192:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  4193:                     }
1.225     raeburn  4194:                 }
                   4195:                 if (!grep(/^\Q$three\E$/,@rolechanges)) {
                   4196:                     push(@rolechanges,$three);
                   4197:                 }
1.135     raeburn  4198: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
1.27      matthew  4199: 		# Activate roles for sections with two id numbers
                   4200: 		# set start, end times, and the url for the class
1.101     albertel 4201: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
                   4202: 			      $env{'form.start_'.$1.'_'.$2} : 
1.27      matthew  4203: 			      $now );
1.101     albertel 4204: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
                   4205: 			      $env{'form.end_'.$1.'_'.$2} :
1.27      matthew  4206: 			      0 );
1.225     raeburn  4207:                 my $one = $1;
                   4208:                 my $two = $2;
                   4209: 		my $url='/'.$one.'/';
1.88      raeburn  4210:                 # split multiple sections
                   4211:                 my %sections = ();
1.225     raeburn  4212:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two},\%sections,$two);
1.88      raeburn  4213:                 if ($num_sections == 0) {
1.240     raeburn  4214:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  4215:                 } else {
                   4216:                     my $emptysec = 0;
1.404     raeburn  4217:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.88      raeburn  4218:                         if ($sec ne '') {
                   4219:                             my $securl = $url.'/'.$sec;
1.240     raeburn  4220:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$two,$start,$end,$one,undef,$sec,$context));
1.88      raeburn  4221:                         } else {
                   4222:                             $emptysec = 1;
                   4223:                         }
                   4224:                     }
                   4225:                     if ($emptysec) {
1.240     raeburn  4226:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  4227:                     }
                   4228:                 }
1.225     raeburn  4229:                 if (!grep(/^\Q$two\E$/,@rolechanges)) {
                   4230:                     push(@rolechanges,$two);
                   4231:                 }
1.64      www      4232: 	    } else {
1.190     raeburn  4233: 		$r->print('<p><span class="LC_error">'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></span></p><br />');
1.64      www      4234:             }
1.113     raeburn  4235:             foreach my $key (sort(keys(%disallowed))) {
1.274     bisitz   4236:                 $r->print('<p class="LC_warning">');
1.113     raeburn  4237:                 if (($key eq 'none') || ($key eq 'all')) {  
1.274     bisitz   4238:                     $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  4239:                 } else {
1.274     bisitz   4240:                     $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  4241:                 }
1.274     bisitz   4242:                 $r->print('</p><p>'
                   4243:                          .&mt('Please [_1]go back[_2] and choose a different section name.'
                   4244:                              ,'<a href="javascript:history.go(-1)'
                   4245:                              ,'</a>')
                   4246:                          .'</p><br />'
                   4247:                 );
1.113     raeburn  4248:             }
                   4249: 	}
1.101     albertel 4250:     } # End of foreach (keys(%env))
1.75      www      4251: # Flush the course logs so reverse user roles immediately updated
1.349     raeburn  4252:     $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.225     raeburn  4253:     if (@rolechanges == 0) {
1.372     raeburn  4254:         $r->print('<p>'.&mt('No roles to modify').'</p>');
1.193     raeburn  4255:     }
1.225     raeburn  4256:     return @rolechanges;
1.220     raeburn  4257: }
                   4258: 
1.375     raeburn  4259: sub get_user_credits {
                   4260:     my ($uname,$udom,$defaultcredits,$cdom,$cnum) = @_;
                   4261:     if ($cdom eq '' || $cnum eq '') {
                   4262:         return unless ($env{'request.course.id'});
                   4263:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4264:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4265:     }
                   4266:     my $credits;
                   4267:     my %currhash =
                   4268:         &Apache::lonnet::get('classlist',[$uname.':'.$udom],$cdom,$cnum);
                   4269:     if (keys(%currhash) > 0) {
                   4270:         my @items = split(/:/,$currhash{$uname.':'.$udom});
                   4271:         my $crdidx = &Apache::loncoursedata::CL_CREDITS() - 3;
                   4272:         $credits = $items[$crdidx];
                   4273:         $credits =~ s/[^\d\.]//g;
                   4274:     }
                   4275:     if ($credits eq $defaultcredits) {
                   4276:         undef($credits);
                   4277:     }
                   4278:     return $credits;
                   4279: }
                   4280: 
1.220     raeburn  4281: sub enroll_single_student {
1.375     raeburn  4282:     my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype,
                   4283:         $showcredits,$defaultcredits) = @_;
1.318     raeburn  4284:     $r->print('<h3>');
                   4285:     if ($crstype eq 'Community') {
                   4286:         $r->print(&mt('Enrolling Member'));
                   4287:     } else {
                   4288:         $r->print(&mt('Enrolling Student'));
                   4289:     }
                   4290:     $r->print('</h3>');
1.220     raeburn  4291: 
                   4292:     # Remove non alphanumeric values from section
                   4293:     $env{'form.sections'}=~s/\W//g;
                   4294: 
1.375     raeburn  4295:     my $credits;
                   4296:     if (($showcredits) && ($env{'form.credits'} ne '')) {
                   4297:         $credits = $env{'form.credits'};
                   4298:         $credits =~ s/[^\d\.]//g;
                   4299:         if ($credits ne '') {
                   4300:             if ($credits eq $defaultcredits) {
                   4301:                 undef($credits);
                   4302:             }
                   4303:         }
                   4304:     }
                   4305: 
1.220     raeburn  4306:     # Clean out any old student roles the user has in this class.
                   4307:     &Apache::lonuserutils::modifystudent($env{'form.ccdomain'},
                   4308:          $env{'form.ccuname'},$env{'request.course.id'},undef,$uhome);
                   4309:     my ($startdate,$enddate) = &Apache::lonuserutils::get_dates_from_form();
                   4310:     my $enroll_result =
                   4311:         &Apache::lonnet::modify_student_enrollment($env{'form.ccdomain'},
                   4312:             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
                   4313:             $env{'form.cmiddlename'},$env{'form.clastname'},
                   4314:             $env{'form.generation'},$env{'form.sections'},$enddate,
1.375     raeburn  4315:             $startdate,'manual',undef,$env{'request.course.id'},'',$context,
                   4316:             $credits);
1.220     raeburn  4317:     if ($enroll_result =~ /^ok/) {
1.381     bisitz   4318:         $r->print(&mt('[_1] enrolled','<b>'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.'</b>'));
1.220     raeburn  4319:         if ($env{'form.sections'} ne '') {
                   4320:             $r->print(' '.&mt('in section [_1]',$env{'form.sections'}));
                   4321:         }
                   4322:         my ($showstart,$showend);
                   4323:         if ($startdate <= $now) {
                   4324:             $showstart = &mt('Access starts immediately');
                   4325:         } else {
                   4326:             $showstart = &mt('Access starts: ').&Apache::lonlocal::locallocaltime($startdate);
                   4327:         }
                   4328:         if ($enddate == 0) {
                   4329:             $showend = &mt('ends: no ending date');
                   4330:         } else {
                   4331:             $showend = &mt('ends: ').&Apache::lonlocal::locallocaltime($enddate);
                   4332:         }
                   4333:         $r->print('.<br />'.$showstart.'; '.$showend);
                   4334:         if ($startdate <= $now && !$newuser) {
1.386     bisitz   4335:             $r->print('<p class="LC_info">');
1.318     raeburn  4336:             if ($crstype eq 'Community') {
1.392     raeburn  4337:                 $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  4338:             } else {
1.392     raeburn  4339:                 $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  4340:            }
                   4341:            $r->print('</p>');
1.220     raeburn  4342:         }
                   4343:     } else {
                   4344:         $r->print(&mt('unable to enroll').": ".$enroll_result);
                   4345:     }
                   4346:     return;
1.188     raeburn  4347: }
                   4348: 
1.204     raeburn  4349: sub get_defaultquota_text {
                   4350:     my ($settingstatus) = @_;
                   4351:     my $defquotatext; 
                   4352:     if ($settingstatus eq '') {
1.383     raeburn  4353:         $defquotatext = &mt('default');
1.204     raeburn  4354:     } else {
                   4355:         my ($usertypes,$order) =
                   4356:             &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
                   4357:         if ($usertypes->{$settingstatus} eq '') {
1.383     raeburn  4358:             $defquotatext = &mt('default');
1.204     raeburn  4359:         } else {
1.383     raeburn  4360:             $defquotatext = &mt('default for [_1]',$usertypes->{$settingstatus});
1.204     raeburn  4361:         }
                   4362:     }
                   4363:     return $defquotatext;
                   4364: }
                   4365: 
1.188     raeburn  4366: sub update_result_form {
                   4367:     my ($uhome) = @_;
                   4368:     my $outcome = 
1.367     golterma 4369:     '<form name="userupdate" method="post" action="">'."\n";
1.160     raeburn  4370:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
1.188     raeburn  4371:         $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  4372:     }
1.207     raeburn  4373:     if ($env{'form.origname'} ne '') {
                   4374:         $outcome .= '<input type="hidden" name="origname" value="'.$env{'form.origname'}.'" />'."\n";
                   4375:     }
1.160     raeburn  4376:     foreach my $item ('sortby','seluname','seludom') {
                   4377:         if (exists($env{'form.'.$item})) {
1.188     raeburn  4378:             $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  4379:         }
                   4380:     }
1.188     raeburn  4381:     if ($uhome eq 'no_host') {
                   4382:         $outcome .= '<input type="hidden" name="forcenewuser" value="1" />'."\n";
                   4383:     }
                   4384:     $outcome .= '<input type="hidden" name="phase" value="" />'."\n".
1.383     raeburn  4385:                 '<input type="hidden" name="currstate" value="" />'."\n".
                   4386:                 '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1.188     raeburn  4387:                 '</form>';
                   4388:     return $outcome;
1.4       www      4389: }
                   4390: 
1.149     raeburn  4391: sub quota_admin {
1.378     raeburn  4392:     my ($setquota,$changeHash,$name) = @_;
1.149     raeburn  4393:     my $quotachanged;
                   4394:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   4395:         # Current user has quota modification privileges
1.267     raeburn  4396:         if (ref($changeHash) eq 'HASH') {
                   4397:             $quotachanged = 1;
1.378     raeburn  4398:             $changeHash->{$name.'quota'} = $setquota;
1.267     raeburn  4399:         }
1.149     raeburn  4400:     }
                   4401:     return $quotachanged;
                   4402: }
                   4403: 
1.267     raeburn  4404: sub tool_admin {
1.275     raeburn  4405:     my ($tool,$settool,$changeHash,$context) = @_;
                   4406:     my $canchange = 0; 
1.279     raeburn  4407:     if ($context eq 'requestcourses') {
1.275     raeburn  4408:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
                   4409:             $canchange = 1;
                   4410:         }
1.300     raeburn  4411:     } elsif ($context eq 'reqcrsotherdom') {
                   4412:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   4413:             $canchange = 1;
                   4414:         }
1.362     raeburn  4415:     } elsif ($context eq 'requestauthor') {
                   4416:         if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
                   4417:             $canchange = 1;
                   4418:         }
1.275     raeburn  4419:     } elsif (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   4420:         # Current user has quota modification privileges
                   4421:         $canchange = 1;
                   4422:     }
1.267     raeburn  4423:     my $toolchanged;
1.275     raeburn  4424:     if ($canchange) {
1.267     raeburn  4425:         if (ref($changeHash) eq 'HASH') {
                   4426:             $toolchanged = 1;
1.362     raeburn  4427:             if ($tool eq 'requestauthor') {
                   4428:                 $changeHash->{$context} = $settool;
                   4429:             } else {
                   4430:                 $changeHash->{$context.'.'.$tool} = $settool;
                   4431:             }
1.267     raeburn  4432:         }
                   4433:     }
                   4434:     return $toolchanged;
                   4435: }
                   4436: 
1.88      raeburn  4437: sub build_roles {
1.89      raeburn  4438:     my ($sectionstr,$sections,$role) = @_;
1.88      raeburn  4439:     my $num_sections = 0;
                   4440:     if ($sectionstr=~ /,/) {
                   4441:         my @secnums = split/,/,$sectionstr;
1.89      raeburn  4442:         if ($role eq 'st') {
                   4443:             $secnums[0] =~ s/\W//g;
                   4444:             $$sections{$secnums[0]} = 1;
                   4445:             $num_sections = 1;
                   4446:         } else {
                   4447:             foreach my $sec (@secnums) {
                   4448:                 $sec =~ ~s/\W//g;
1.150     banghart 4449:                 if (!($sec eq "")) {
1.89      raeburn  4450:                     if (exists($$sections{$sec})) {
                   4451:                         $$sections{$sec} ++;
                   4452:                     } else {
                   4453:                         $$sections{$sec} = 1;
                   4454:                         $num_sections ++;
                   4455:                     }
1.88      raeburn  4456:                 }
                   4457:             }
                   4458:         }
                   4459:     } else {
                   4460:         $sectionstr=~s/\W//g;
                   4461:         unless ($sectionstr eq '') {
                   4462:             $$sections{$sectionstr} = 1;
                   4463:             $num_sections ++;
                   4464:         }
                   4465:     }
1.129     albertel 4466: 
1.88      raeburn  4467:     return $num_sections;
                   4468: }
                   4469: 
1.58      www      4470: # ========================================================== Custom Role Editor
                   4471: 
                   4472: sub custom_role_editor {
1.406.2.5  raeburn  4473:     my ($r,$brcrum,$prefix) = @_;
1.324     raeburn  4474:     my $action = $env{'form.customroleaction'};
                   4475:     my $rolename; 
                   4476:     if ($action eq 'new') {
                   4477:         $rolename=$env{'form.newrolename'};
                   4478:     } else {
                   4479:         $rolename=$env{'form.rolename'};
1.59      www      4480:     }
                   4481: 
1.324     raeburn  4482:     my ($crstype,$context);
                   4483:     if ($env{'request.course.id'}) {
                   4484:         $crstype = &Apache::loncommon::course_type();
                   4485:         $context = 'course';
                   4486:     } else {
                   4487:         $context = 'domain';
1.406.2.5  raeburn  4488:         $crstype = 'course';
1.324     raeburn  4489:     }
1.351     raeburn  4490: 
                   4491:     $rolename=~s/[^A-Za-z0-9]//gs;
                   4492:     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
                   4493: 	&print_username_entry_form($r,undef,undef,undef,undef,$crstype,$brcrum);
                   4494:         return;
                   4495:     }
                   4496: 
1.406.2.5  raeburn  4497:     my $formname = 'form1';
                   4498:     my %privs=();
                   4499:     my $body_top = '<h2>';
                   4500: # ------------------------------------------------------- Does this role exist?
1.59      www      4501:     my ($rdummy,$roledef)=
                   4502: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   4503:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.406.2.5  raeburn  4504:         $body_top .= &mt('Existing Role').' "';
1.61      www      4505: # ------------------------------------------------- Get current role privileges
1.406.2.5  raeburn  4506:         ($privs{'system'},$privs{'domain'},$privs{'course'})=split(/\_/,$roledef);
                   4507:         if ($privs{'system'} =~ /bre\&S/) {
                   4508:             if ($context eq 'domain') {
                   4509:                 $crstype = 'Course';
                   4510:             } elsif ($crstype eq 'Community') {
                   4511:                 $privs{'system'} =~ s/bre\&S//;
                   4512:             }
                   4513:         } elsif ($context eq 'domain') {
                   4514:             $crstype = 'Course';
1.324     raeburn  4515:         }
1.59      www      4516:     } else {
1.406.2.5  raeburn  4517:         $body_top .= &mt('New Role').' "';
                   4518:         $roledef='';
1.59      www      4519:     }
1.153     banghart 4520:     $body_top .= $rolename.'"</h2>';
1.406.2.5  raeburn  4521: 
                   4522: # ------------------------------------------------------- What can be assigned?
                   4523:     my %full=();
                   4524:     my %levels=(
                   4525:                  course => {},
                   4526:                  domain => {},
                   4527:                  system => {},
                   4528:                );
                   4529:     my %levelscurrent=(
                   4530:                         course => {},
                   4531:                         domain => {},
                   4532:                         system => {},
                   4533:                       );
                   4534:     &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
1.160     raeburn  4535:     my ($jsback,$elements) = &crumb_utilities();
1.406.2.5  raeburn  4536:     my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
                   4537:     my $head_script =
                   4538:         &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,
                   4539:                                                   \%full,\@templateroles,$jsback);
1.351     raeburn  4540:     push (@{$brcrum},
1.406.2.5  raeburn  4541:               {href => "javascript:backPage(document.$formname,'pickrole','')",
1.351     raeburn  4542:                text => "Pick custom role",
                   4543:                faq  => 282,bug=>'Instructor Interface',},
1.406.2.5  raeburn  4544:               {href => "javascript:backPage(document.$formname,'','')",
1.351     raeburn  4545:                text => "Edit custom role",
                   4546:                faq  => 282,
                   4547:                bug  => 'Instructor Interface',
                   4548:                help => 'Course_Editing_Custom_Roles'}
                   4549:               );
                   4550:     my $args = { bread_crumbs          => $brcrum,
                   4551:                  bread_crumbs_component => 'User Management'};
1.406.2.5  raeburn  4552: 
1.351     raeburn  4553:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',
                   4554:                                              $head_script,$args).
                   4555:               $body_top);
1.406.2.5  raeburn  4556:     $r->print('<form name="'.$formname.'" method="post" action="">'."\n".
                   4557:               &Apache::lonuserutils::custom_role_header($context,$crstype,
                   4558:                                                         \@templateroles,$prefix));
1.264     bisitz   4559: 
1.61      www      4560:     $r->print(<<ENDCCF);
                   4561: <input type="hidden" name="phase" value="set_custom_roles" />
                   4562: <input type="hidden" name="rolename" value="$rolename" />
                   4563: ENDCCF
1.406.2.5  raeburn  4564:     $r->print(&Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
                   4565:                                                        \%levelscurrent,$prefix));
1.135     raeburn  4566:     $r->print(&Apache::loncommon::end_data_table().
1.190     raeburn  4567:    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
1.160     raeburn  4568:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
1.406.2.5  raeburn  4569:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".
1.160     raeburn  4570:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
1.351     raeburn  4571:    '<input type="submit" value="'.&mt('Save').'" /></form>');
1.61      www      4572: }
1.406.2.5  raeburn  4573: 
1.61      www      4574: # ---------------------------------------------------------- Call to definerole
                   4575: sub set_custom_role {
1.406.2.5  raeburn  4576:     my ($r,$context,$brcrum,$prefix) = @_;
1.101     albertel 4577:     my $rolename=$env{'form.rolename'};
1.63      www      4578:     $rolename=~s/[^A-Za-z0-9]//gs;
1.150     banghart 4579:     if (!$rolename) {
1.406.2.5  raeburn  4580: 	&custom_role_editor($r,$brcrum,$prefix);
1.61      www      4581:         return;
                   4582:     }
1.160     raeburn  4583:     my ($jsback,$elements) = &crumb_utilities();
1.301     bisitz   4584:     my $jscript = '<script type="text/javascript">'
                   4585:                  .'// <![CDATA['."\n"
                   4586:                  .$jsback."\n"
                   4587:                  .'// ]]>'."\n"
                   4588:                  .'</script>'."\n";
1.352     raeburn  4589:     push(@{$brcrum},
                   4590:         {href => "javascript:backPage(document.customresult,'pickrole','')",
                   4591:          text => "Pick custom role",
                   4592:          faq  => 282,
                   4593:          bug  => 'Instructor Interface',},
                   4594:         {href => "javascript:backPage(document.customresult,'selected_custom_edit','')",
                   4595:          text => "Edit custom role",
                   4596:          faq  => 282,
                   4597:          bug  => 'Instructor Interface',},
                   4598:         {href => "javascript:backPage(document.customresult,'set_custom_roles','')",
                   4599:          text => "Result",
                   4600:          faq  => 282,
                   4601:          bug  => 'Instructor Interface',
                   4602:          help => 'Course_Editing_Custom_Roles'},
                   4603:         );
                   4604:     my $args = { bread_crumbs           => $brcrum,
1.406.2.5  raeburn  4605:                  bread_crumbs_component => 'User Management'};
1.351     raeburn  4606:     $r->print(&Apache::loncommon::start_page('Save Custom Role',$jscript,$args));
1.160     raeburn  4607: 
1.393     raeburn  4608:     my $newrole;
1.61      www      4609:     my ($rdummy,$roledef)=
1.110     albertel 4610: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   4611: 
1.61      www      4612: # ------------------------------------------------------- Does this role exist?
1.188     raeburn  4613:     $r->print('<h3>');
1.61      www      4614:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 4615: 	$r->print(&mt('Existing Role').' "');
1.61      www      4616:     } else {
1.73      sakharuk 4617: 	$r->print(&mt('New Role').' "');
1.61      www      4618: 	$roledef='';
1.393     raeburn  4619:         $newrole = 1;
1.61      www      4620:     }
1.188     raeburn  4621:     $r->print($rolename.'"</h3>');
1.406.2.5  raeburn  4622: # ------------------------------------------------- Assign role and show result
1.61      www      4623: 
1.387     bisitz   4624:     my $errmsg;
1.406.2.5  raeburn  4625:     my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$prefix);
                   4626:     # Assign role and return result
                   4627:     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
                   4628:                                              $newprivs{'c'});
1.387     bisitz   4629:     if ($result ne 'ok') {
                   4630:         $errmsg = ': '.$result;
                   4631:     }
                   4632:     my $message =
                   4633:         &Apache::lonhtmlcommon::confirm_success(
                   4634:             &mt('Defining Role').$errmsg, ($result eq 'ok' ? 0 : 1));
1.101     albertel 4635:     if ($env{'request.course.id'}) {
                   4636:         my $url='/'.$env{'request.course.id'};
1.63      www      4637:         $url=~s/\_/\//g;
1.387     bisitz   4638:         $result =
                   4639:             &Apache::lonnet::assigncustomrole(
                   4640:                 $env{'user.domain'},$env{'user.name'},
                   4641:                 $url,
                   4642:                 $env{'user.domain'},$env{'user.name'},
                   4643:                 $rolename,undef,undef,undef,$context);
                   4644:         if ($result ne 'ok') {
                   4645:             $errmsg = ': '.$result;
                   4646:         }
                   4647:         $message .=
                   4648:             '<br />'
                   4649:            .&Apache::lonhtmlcommon::confirm_success(
                   4650:                 &mt('Assigning Role to Self').$errmsg, ($result eq 'ok' ? 0 : 1));
1.63      www      4651:     }
1.380     bisitz   4652:     $r->print(
1.387     bisitz   4653:         &Apache::loncommon::confirmwrapper($message)
                   4654:        .'<br />'
                   4655:        .&Apache::lonhtmlcommon::actionbox([
                   4656:             '<a href="javascript:backPage(document.customresult,'."'pickrole'".')">'
                   4657:            .&mt('Create or edit another custom role')
                   4658:            .'</a>'])
1.380     bisitz   4659:        .'<form name="customresult" method="post" action="">'
1.387     bisitz   4660:        .&Apache::lonhtmlcommon::echo_form_input([])
                   4661:        .'</form>'
1.380     bisitz   4662:     );
1.58      www      4663: }
                   4664: 
1.2       www      4665: # ================================================================ Main Handler
                   4666: sub handler {
                   4667:     my $r = shift;
                   4668:     if ($r->header_only) {
1.68      www      4669:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      4670:        $r->send_http_header;
                   4671:        return OK;
                   4672:     }
1.318     raeburn  4673:     my ($context,$crstype);
1.190     raeburn  4674:     if ($env{'request.course.id'}) {
                   4675:         $context = 'course';
1.318     raeburn  4676:         $crstype = &Apache::loncommon::course_type();
1.190     raeburn  4677:     } elsif ($env{'request.role'} =~ /^au\./) {
1.206     raeburn  4678:         $context = 'author';
1.190     raeburn  4679:     } else {
                   4680:         $context = 'domain';
                   4681:     }
1.375     raeburn  4682: 
1.190     raeburn  4683:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.233     raeburn  4684:         ['action','state','callingform','roletype','showrole','bulkaction','popup','phase',
1.391     raeburn  4685:          'username','domain','srchterm','srchdomain','srchin','srchby','srchtype','queue']);
1.190     raeburn  4686:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.351     raeburn  4687:     my $args;
                   4688:     my $brcrum = [];
                   4689:     my $bread_crumbs_component = 'User Management';
1.391     raeburn  4690:     if (($env{'form.action'} ne 'dateselect') && ($env{'form.action'} ne 'displayuserreq')) {
1.351     raeburn  4691:         $brcrum = [{href=>"/adm/createuser",
                   4692:                     text=>"User Management",
                   4693:                     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'}
                   4694:                   ];
1.202     raeburn  4695:     }
1.289     droeschl 4696:     #SD Following files not added to help, because the corresponding .tex-files seem to
                   4697:     #be missing: Course_Approve_Selfenroll,Course_User_Logs,
1.209     raeburn  4698:     my ($permission,$allowed) = 
1.318     raeburn  4699:         &Apache::lonuserutils::get_permission($context,$crstype);
1.190     raeburn  4700:     if (!$allowed) {
1.358     raeburn  4701:         if ($context eq 'course') {
                   4702:             $r->internal_redirect('/adm/viewclasslist');
                   4703:             return OK;
                   4704:         }
1.190     raeburn  4705:         $env{'user.error.msg'}=
                   4706:             "/adm/createuser:cst:0:0:Cannot create/modify user data ".
                   4707:                                  "or view user status.";
                   4708:         return HTTP_NOT_ACCEPTABLE;
                   4709:     }
                   4710: 
                   4711:     &Apache::loncommon::content_type($r,'text/html');
                   4712:     $r->send_http_header;
                   4713: 
1.375     raeburn  4714:     my $showcredits;
                   4715:     if ((($context eq 'course') && ($crstype eq 'Course')) || 
                   4716:          ($context eq 'domain')) {
                   4717:         my %domdefaults = 
                   4718:             &Apache::lonnet::get_domain_defaults($env{'request.role.domain'});
                   4719:         if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'}) {
                   4720:             $showcredits = 1;
                   4721:         }
                   4722:     }
                   4723: 
1.190     raeburn  4724:     # Main switch on form.action and form.state, as appropriate
                   4725:     if (! exists($env{'form.action'})) {
1.351     raeburn  4726:         $args = {bread_crumbs => $brcrum,
                   4727:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4728:         $r->print(&header(undef,$args));
1.318     raeburn  4729:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4730:     } elsif ($env{'form.action'} eq 'upload' && $permission->{'cusr'}) {
1.351     raeburn  4731:         push(@{$brcrum},
                   4732:               { href => '/adm/createuser?action=upload&state=',
                   4733:                 text => 'Upload Users List',
                   4734:                 help => 'Course_Create_Class_List',
                   4735:               });
                   4736:         $bread_crumbs_component = 'Upload Users List';
                   4737:         $args = {bread_crumbs           => $brcrum,
                   4738:                  bread_crumbs_component => $bread_crumbs_component};
                   4739:         $r->print(&header(undef,$args));
1.190     raeburn  4740:         $r->print('<form name="studentform" method="post" '.
                   4741:                   'enctype="multipart/form-data" '.
                   4742:                   ' action="/adm/createuser">'."\n");
                   4743:         if (! exists($env{'form.state'})) {
                   4744:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4745:         } elsif ($env{'form.state'} eq 'got_file') {
1.375     raeburn  4746:             &Apache::lonuserutils::print_upload_manager_form($r,$context,$permission,
                   4747:                                                              $crstype,$showcredits);
1.190     raeburn  4748:         } elsif ($env{'form.state'} eq 'enrolling') {
                   4749:             if ($env{'form.datatoken'}) {
1.375     raeburn  4750:                 &Apache::lonuserutils::upfile_drop_add($r,$context,$permission,
                   4751:                                                        $showcredits);
1.190     raeburn  4752:             }
                   4753:         } else {
                   4754:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4755:         }
1.406.2.5  raeburn  4756:     } elsif (((($env{'form.action'} eq 'singleuser') || ($env{'form.action'}
                   4757:               eq 'singlestudent')) && ($permission->{'cusr'})) ||
1.406.2.6  raeburn  4758:              (($env{'form.action'} eq 'singleuser') && ($permission->{'view'})) ||
1.406.2.5  raeburn  4759:              (($env{'form.action'} eq 'accesslogs') && ($permission->{'activity'}))) {
1.190     raeburn  4760:         my $phase = $env{'form.phase'};
                   4761:         my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
1.192     albertel 4762: 	&Apache::loncreateuser::restore_prev_selections();
                   4763: 	my $srch;
                   4764: 	foreach my $item (@search) {
                   4765: 	    $srch->{$item} = $env{'form.'.$item};
                   4766: 	}
1.207     raeburn  4767:         if (($phase eq 'get_user_info') || ($phase eq 'userpicked') ||
1.406.2.5  raeburn  4768:             ($phase eq 'createnewuser') || ($phase eq 'activity')) {
1.207     raeburn  4769:             if ($env{'form.phase'} eq 'createnewuser') {
                   4770:                 my $response;
                   4771:                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
1.366     bisitz   4772:                     my $response =
                   4773:                         '<span class="LC_warning">'
                   4774:                        .&mt('You must specify a valid username. Only the following are allowed:'
                   4775:                            .' letters numbers - . @')
                   4776:                        .'</span>';
1.221     raeburn  4777:                     $env{'form.phase'} = '';
1.375     raeburn  4778:                     &print_username_entry_form($r,$context,$response,$srch,undef,
                   4779:                                                $crstype,$brcrum,$showcredits);
1.207     raeburn  4780:                 } else {
                   4781:                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
                   4782:                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
                   4783:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4784:                                                   $srch,$response,$context,
1.375     raeburn  4785:                                                   $permission,$crstype,$brcrum,
                   4786:                                                   $showcredits);
1.207     raeburn  4787:                 }
                   4788:             } elsif ($env{'form.phase'} eq 'get_user_info') {
1.190     raeburn  4789:                 my ($currstate,$response,$forcenewuser,$results) = 
1.221     raeburn  4790:                     &user_search_result($context,$srch);
1.190     raeburn  4791:                 if ($env{'form.currstate'} eq 'modify') {
                   4792:                     $currstate = $env{'form.currstate'};
                   4793:                 }
                   4794:                 if ($currstate eq 'select') {
                   4795:                     &print_user_selection_page($r,$response,$srch,$results,
1.351     raeburn  4796:                                                \@search,$context,undef,$crstype,
                   4797:                                                $brcrum);
1.406.2.5  raeburn  4798:                 } elsif (($currstate eq 'modify') || ($env{'form.action'} eq 'accesslogs')) {
                   4799:                     my ($ccuname,$ccdomain,$uhome);
1.190     raeburn  4800:                     if (($srch->{'srchby'} eq 'uname') && 
                   4801:                         ($srch->{'srchtype'} eq 'exact')) {
                   4802:                         $ccuname = $srch->{'srchterm'};
                   4803:                         $ccdomain= $srch->{'srchdomain'};
                   4804:                     } else {
                   4805:                         my @matchedunames = keys(%{$results});
                   4806:                         ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
                   4807:                     }
                   4808:                     $ccuname =&LONCAPA::clean_username($ccuname);
                   4809:                     $ccdomain=&LONCAPA::clean_domain($ccdomain);
1.406.2.5  raeburn  4810:                     if ($env{'form.action'} eq 'accesslogs') {
                   4811:                         my $uhome;
                   4812:                         if (($ccuname ne '') && ($ccdomain ne '')) {
                   4813:                            $uhome = &Apache::lonnet::homeserver($ccuname,$ccdomain);
                   4814:                         }
                   4815:                         if (($uhome eq '') || ($uhome eq 'no_host')) {
                   4816:                             $env{'form.phase'} = '';
                   4817:                             undef($forcenewuser);
                   4818:                             #if ($response) {
                   4819:                             #    unless ($response =~ m{\Q<br /><br />\E$}) {
                   4820:                             #        $response .= '<br /><br />';
                   4821:                             #    }
                   4822:                             #}
                   4823:                             &print_username_entry_form($r,$context,$response,$srch,
                   4824:                                                        $forcenewuser,$crstype,$brcrum);
                   4825:                         } else {
                   4826:                             &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
                   4827:                         }
                   4828:                     } else {
                   4829:                         if ($env{'form.forcenewuser'}) {
                   4830:                             $response = '';
                   4831:                         }
                   4832:                         &print_user_modification_page($r,$ccuname,$ccdomain,
                   4833:                                                       $srch,$response,$context,
                   4834:                                                       $permission,$crstype,$brcrum);
1.190     raeburn  4835:                     }
                   4836:                 } elsif ($currstate eq 'query') {
1.351     raeburn  4837:                     &print_user_query_page($r,'createuser',$brcrum);
1.190     raeburn  4838:                 } else {
1.229     raeburn  4839:                     $env{'form.phase'} = '';
1.207     raeburn  4840:                     &print_username_entry_form($r,$context,$response,$srch,
1.351     raeburn  4841:                                                $forcenewuser,$crstype,$brcrum);
1.190     raeburn  4842:                 }
                   4843:             } elsif ($env{'form.phase'} eq 'userpicked') {
                   4844:                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
                   4845:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
1.406.2.5  raeburn  4846:                 if ($env{'form.action'} eq 'accesslogs') {
                   4847:                     &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
                   4848:                 } else {
                   4849:                     &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
                   4850:                                                   $context,$permission,$crstype,
                   4851:                                                   $brcrum);
                   4852:                 }
                   4853:             } elsif ($env{'form.action'} eq 'accesslogs') {
                   4854:                 my $ccuname = &LONCAPA::clean_username($env{'form.accessuname'});
                   4855:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.accessudom'});
                   4856:                 &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
1.190     raeburn  4857:             }
                   4858:         } elsif ($env{'form.phase'} eq 'update_user_data') {
1.375     raeburn  4859:             &update_user_data($r,$context,$crstype,$brcrum,$showcredits);
1.190     raeburn  4860:         } else {
1.351     raeburn  4861:             &print_username_entry_form($r,$context,undef,$srch,undef,$crstype,
                   4862:                                        $brcrum);
1.190     raeburn  4863:         }
                   4864:     } elsif ($env{'form.action'} eq 'custom' && $permission->{'custom'}) {
1.406.2.5  raeburn  4865:         my $prefix;
1.190     raeburn  4866:         if ($env{'form.phase'} eq 'set_custom_roles') {
1.406.2.5  raeburn  4867:             &set_custom_role($r,$context,$brcrum,$prefix);
1.190     raeburn  4868:         } else {
1.406.2.5  raeburn  4869:             &custom_role_editor($r,$brcrum,$prefix);
1.190     raeburn  4870:         }
1.362     raeburn  4871:     } elsif (($env{'form.action'} eq 'processauthorreq') &&
                   4872:              ($permission->{'cusr'}) && 
                   4873:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   4874:         push(@{$brcrum},
                   4875:                  {href => '/adm/createuser?action=processauthorreq',
1.385     bisitz   4876:                   text => 'Authoring Space requests',
1.362     raeburn  4877:                   help => 'Domain_Role_Approvals'});
                   4878:         $bread_crumbs_component = 'Authoring requests';
                   4879:         if ($env{'form.state'} eq 'done') {
                   4880:             push(@{$brcrum},
                   4881:                      {href => '/adm/createuser?action=authorreqqueue',
                   4882:                       text => 'Result',
                   4883:                       help => 'Domain_Role_Approvals'});
                   4884:             $bread_crumbs_component = 'Authoring request result';
                   4885:         }
                   4886:         $args = { bread_crumbs           => $brcrum,
                   4887:                   bread_crumbs_component => $bread_crumbs_component};
1.391     raeburn  4888:         my $js = &usernamerequest_javascript();
                   4889:         $r->print(&header(&add_script($js),$args));
1.362     raeburn  4890:         if (!exists($env{'form.state'})) {
                   4891:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestauthor',
                   4892:                                                                             $env{'request.role.domain'}));
                   4893:         } elsif ($env{'form.state'} eq 'done') {
                   4894:             $r->print('<h3>'.&mt('Authoring request processing').'</h3>'."\n");
                   4895:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestauthor',
                   4896:                                                                          $env{'request.role.domain'}));
                   4897:         }
1.391     raeburn  4898:     } elsif (($env{'form.action'} eq 'processusernamereq') &&
                   4899:              ($permission->{'cusr'}) &&
                   4900:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   4901:         push(@{$brcrum},
                   4902:                  {href => '/adm/createuser?action=processusernamereq',
                   4903:                   text => 'LON-CAPA account requests',
                   4904:                   help => 'Domain_Username_Approvals'});
                   4905:         $bread_crumbs_component = 'Account requests';
                   4906:         if ($env{'form.state'} eq 'done') {
                   4907:             push(@{$brcrum},
                   4908:                      {href => '/adm/createuser?action=usernamereqqueue',
                   4909:                       text => 'Result',
                   4910:                       help => 'Domain_Username_Approvals'});
                   4911:             $bread_crumbs_component = 'LON-CAPA account request result';
                   4912:         }
                   4913:         $args = { bread_crumbs           => $brcrum,
                   4914:                   bread_crumbs_component => $bread_crumbs_component};
                   4915:         my $js = &usernamerequest_javascript();
                   4916:         $r->print(&header(&add_script($js),$args));
                   4917:         if (!exists($env{'form.state'})) {
                   4918:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestusername',
                   4919:                                                                             $env{'request.role.domain'}));
                   4920:         } elsif ($env{'form.state'} eq 'done') {
                   4921:             $r->print('<h3>'.&mt('LON-CAPA account request processing').'</h3>'."\n");
                   4922:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestusername',
                   4923:                                                                          $env{'request.role.domain'}));
                   4924:         }
                   4925:     } elsif (($env{'form.action'} eq 'displayuserreq') &&
                   4926:              ($permission->{'cusr'})) {
                   4927:         my $dom = $env{'form.domain'};
                   4928:         my $uname = $env{'form.username'};
                   4929:         my $warning;
                   4930:         if (($dom =~ /^$match_domain$/) && (&Apache::lonnet::domain($dom) ne '')) {
                   4931:             if (($dom eq $env{'request.role.domain'}) && (&Apache::lonnet::allowed('ccc',$dom))) {
                   4932:                 if (($uname =~ /^$match_username$/) && ($env{'form.queue'} eq 'approval')) {
                   4933:                     my $uhome = &Apache::lonnet::homeserver($uname,$dom);
                   4934:                     if ($uhome eq 'no_host') {
                   4935:                         my $queue = $env{'form.queue'};
                   4936:                         my $reqkey = &escape($uname).'_'.$queue; 
                   4937:                         my $namespace = 'usernamequeue';
                   4938:                         my $domconfig = &Apache::lonnet::get_domainconfiguser($dom);
                   4939:                         my %queued =
                   4940:                             &Apache::lonnet::get($namespace,[$reqkey],$dom,$domconfig);
                   4941:                         unless ($queued{$reqkey}) {
                   4942:                             $warning = &mt('No information was found for this LON-CAPA account request.');
                   4943:                         }
                   4944:                     } else {
                   4945:                         $warning = &mt('A LON-CAPA account already exists for the requested username and domain.');
                   4946:                     }
                   4947:                 } else {
                   4948:                     $warning = &mt('LON-CAPA account request status check is for an invalid username.');
                   4949:                 }
                   4950:             } else {
                   4951:                 $warning = &mt('You do not have rights to view LON-CAPA account requests in the domain specified.');
                   4952:             }
                   4953:         } else {
                   4954:             $warning = &mt('LON-CAPA account request status check is for an invalid domain.');
                   4955:         }
                   4956:         my $args = { only_body => 1 };
                   4957:         $r->print(&header(undef,$args).
                   4958:                   '<h3>'.&mt('LON-CAPA Account Request Details').'</h3>');
                   4959:         if ($warning ne '') {
                   4960:             $r->print('<div class="LC_warning">'.$warning.'</div>');
                   4961:         } else {
                   4962:             my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
                   4963:             my $domconfiguser = &Apache::lonnet::get_domainconfiguser($dom);
                   4964:             my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   4965:             if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   4966:                 if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
                   4967:                     if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}) eq 'HASH') {
                   4968:                         my %info =
                   4969:                             &Apache::lonnet::get('nohist_requestedusernames',[$uname],$dom,$domconfiguser);
                   4970:                         if (ref($info{$uname}) eq 'HASH') {
1.396     raeburn  4971:                             my $usertype = $info{$uname}{'inststatus'};
                   4972:                             unless ($usertype) {
                   4973:                                 $usertype = 'default';
                   4974:                             }
                   4975:                             if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}) eq 'HASH') {
                   4976:                                 if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
                   4977:                                     $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box());
                   4978:                                     my ($num,$count,$showstatus);
                   4979:                                     $count = scalar(keys(%{$domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}}));
                   4980:                                     unless ($usertype eq 'default') {
                   4981:                                         my ($othertitle,$usertypes,$types) = 
                   4982:                                             &Apache::loncommon::sorted_inst_types($dom);
                   4983:                                         if (ref($usertypes) eq 'HASH') {
                   4984:                                             if ($usertypes->{$usertype}) {
                   4985:                                                 $showstatus = $usertypes->{$usertype};
                   4986:                                                 $count ++;
                   4987:                                             }
                   4988:                                         }
                   4989:                                     }
                   4990:                                     foreach my $field (@{$infofields}) {
                   4991:                                         next unless ($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}{$field});
                   4992:                                         next unless ($infotitles->{$field});
                   4993:                                         $r->print(&Apache::lonhtmlcommon::row_title($infotitles->{$field}).
                   4994:                                                   $info{$uname}{$field});
                   4995:                                         $num ++;
                   4996:                                         if ($count == $num) {
                   4997:                                             $r->print(&Apache::lonhtmlcommon::row_closure(1));
                   4998:                                         } else {
                   4999:                                             $r->print(&Apache::lonhtmlcommon::row_closure());
                   5000:                                         }
                   5001:                                     }
                   5002:                                     if ($showstatus) {
                   5003:                                         $r->print(&Apache::lonhtmlcommon::row_title(&mt('Status type (self-reported)')).
                   5004:                                                   $showstatus.
                   5005:                                                   &Apache::lonhtmlcommon::row_closure(1));
1.391     raeburn  5006:                                     }
1.396     raeburn  5007:                                     $r->print(&Apache::lonhtmlcommon::end_pick_box().'</div>');
1.391     raeburn  5008:                                 }
                   5009:                             }
                   5010:                         }
                   5011:                     }
                   5012:                 }
                   5013:             }
                   5014:             $r->print(&close_popup_form());
                   5015:         }
1.207     raeburn  5016:     } elsif (($env{'form.action'} eq 'listusers') && 
                   5017:              ($permission->{'view'} || $permission->{'cusr'})) {
1.202     raeburn  5018:         if ($env{'form.phase'} eq 'bulkchange') {
1.351     raeburn  5019:             push(@{$brcrum},
                   5020:                     {href => '/adm/createuser?action=listusers',
                   5021:                      text => "List Users"},
                   5022:                     {href => "/adm/createuser",
                   5023:                      text => "Result",
                   5024:                      help => 'Course_View_Class_List'});
                   5025:             $bread_crumbs_component = 'Update Users';
                   5026:             $args = {bread_crumbs           => $brcrum,
                   5027:                      bread_crumbs_component => $bread_crumbs_component};
                   5028:             $r->print(&header(undef,$args));
1.202     raeburn  5029:             my $setting = $env{'form.roletype'};
                   5030:             my $choice = $env{'form.bulkaction'};
                   5031:             if ($permission->{'cusr'}) {
1.336     raeburn  5032:                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice,$crstype);
1.221     raeburn  5033:             } else {
                   5034:                 $r->print(&mt('You are not authorized to make bulk changes to user roles'));
1.223     raeburn  5035:                 $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
1.202     raeburn  5036:             }
                   5037:         } else {
1.351     raeburn  5038:             push(@{$brcrum},
                   5039:                     {href => '/adm/createuser?action=listusers',
                   5040:                      text => "List Users",
                   5041:                      help => 'Course_View_Class_List'});
                   5042:             $bread_crumbs_component = 'List Users';
                   5043:             $args = {bread_crumbs           => $brcrum,
                   5044:                      bread_crumbs_component => $bread_crumbs_component};
1.202     raeburn  5045:             my ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles);
                   5046:             my $formname = 'studentform';
1.364     raeburn  5047:             my $hidecall = "hide_searching();";
1.321     raeburn  5048:             if (($context eq 'domain') && (($env{'form.roletype'} eq 'course') ||
                   5049:                 ($env{'form.roletype'} eq 'community'))) {
                   5050:                 if ($env{'form.roletype'} eq 'course') {
                   5051:                     ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles) = 
                   5052:                         &Apache::lonuserutils::courses_selector($env{'request.role.domain'},
                   5053:                                                                 $formname);
                   5054:                 } elsif ($env{'form.roletype'} eq 'community') {
                   5055:                     $cb_jscript = 
                   5056:                         &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
                   5057:                     my %elements = (
                   5058:                                       coursepick => 'radio',
                   5059:                                       coursetotal => 'text',
                   5060:                                       courselist => 'text',
                   5061:                                    );
                   5062:                     $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   5063:                 }
1.364     raeburn  5064:                 $jscript .= &verify_user_display($context)."\n".
                   5065:                             &Apache::loncommon::check_uncheck_jscript();
1.202     raeburn  5066:                 my $js = &add_script($jscript).$cb_jscript;
                   5067:                 my $loadcode = 
                   5068:                     &Apache::lonuserutils::course_selector_loadcode($formname);
                   5069:                 if ($loadcode ne '') {
1.364     raeburn  5070:                     $args->{add_entries} = {onload => "$loadcode;$hidecall"};
                   5071:                 } else {
                   5072:                     $args->{add_entries} = {onload => $hidecall};
1.202     raeburn  5073:                 }
1.351     raeburn  5074:                 $r->print(&header($js,$args));
1.191     raeburn  5075:             } else {
1.364     raeburn  5076:                 $args->{add_entries} = {onload => $hidecall};
                   5077:                 $jscript = &verify_user_display($context).
                   5078:                            &Apache::loncommon::check_uncheck_jscript(); 
                   5079:                 $r->print(&header(&add_script($jscript),$args));
1.191     raeburn  5080:             }
1.202     raeburn  5081:             &Apache::lonuserutils::print_userlist($r,undef,$permission,$context,
1.375     raeburn  5082:                          $formname,$totcodes,$codetitles,$idlist,$idlist_titles,
                   5083:                          $showcredits);
1.191     raeburn  5084:         }
1.213     raeburn  5085:     } elsif ($env{'form.action'} eq 'drop' && $permission->{'cusr'}) {
1.318     raeburn  5086:         my $brtext;
                   5087:         if ($crstype eq 'Community') {
                   5088:             $brtext = 'Drop Members';
                   5089:         } else {
                   5090:             $brtext = 'Drop Students';
                   5091:         }
1.351     raeburn  5092:         push(@{$brcrum},
                   5093:                 {href => '/adm/createuser?action=drop',
                   5094:                  text => $brtext,
                   5095:                  help => 'Course_Drop_Student'});
                   5096:         if ($env{'form.state'} eq 'done') {
                   5097:             push(@{$brcrum},
                   5098:                      {href=>'/adm/createuser?action=drop',
                   5099:                       text=>"Result"});
                   5100:         }
                   5101:         $bread_crumbs_component = $brtext;
                   5102:         $args = {bread_crumbs           => $brcrum,
                   5103:                  bread_crumbs_component => $bread_crumbs_component}; 
                   5104:         $r->print(&header(undef,$args));
1.213     raeburn  5105:         if (!exists($env{'form.state'})) {
1.318     raeburn  5106:             &Apache::lonuserutils::print_drop_menu($r,$context,$permission,$crstype);
1.213     raeburn  5107:         } elsif ($env{'form.state'} eq 'done') {
                   5108:             &Apache::lonuserutils::update_user_list($r,$context,undef,
                   5109:                                                     $env{'form.action'});
                   5110:         }
1.202     raeburn  5111:     } elsif ($env{'form.action'} eq 'dateselect') {
                   5112:         if ($permission->{'cusr'}) {
1.351     raeburn  5113:             $r->print(&header(undef,{'no_nav_bar' => 1}).
1.375     raeburn  5114:                       &Apache::lonuserutils::date_section_selector($context,$permission,
                   5115:                                                                    $crstype,$showcredits));
1.202     raeburn  5116:         } else {
1.351     raeburn  5117:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5118:                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>'); 
1.202     raeburn  5119:         }
1.237     raeburn  5120:     } elsif ($env{'form.action'} eq 'selfenroll') {
1.398     raeburn  5121:         if ($permission->{selfenrolladmin}) {
                   5122:             my $cid = $env{'request.course.id'};
                   5123:             my $cdom = $env{'course.'.$cid.'.domain'};
                   5124:             my $cnum = $env{'course.'.$cid.'.num'};
                   5125:             my %currsettings = (
                   5126:                 selfenroll_types              => $env{'course.'.$cid.'.internal.selfenroll_types'},
                   5127:                 selfenroll_registered         => $env{'course.'.$cid.'.internal.selfenroll_registered'},
                   5128:                 selfenroll_section            => $env{'course.'.$cid.'.internal.selfenroll_section'},
                   5129:                 selfenroll_notifylist         => $env{'course.'.$cid.'.internal.selfenroll_notifylist'},
                   5130:                 selfenroll_approval           => $env{'course.'.$cid.'.internal.selfenroll_approval'},
                   5131:                 selfenroll_limit              => $env{'course.'.$cid.'.internal.selfenroll_limit'},
                   5132:                 selfenroll_cap                => $env{'course.'.$cid.'.internal.selfenroll_cap'},
                   5133:                 selfenroll_start_date         => $env{'course.'.$cid.'.internal.selfenroll_start_date'},
                   5134:                 selfenroll_end_date           => $env{'course.'.$cid.'.internal.selfenroll_end_date'},
                   5135:                 selfenroll_start_access       => $env{'course.'.$cid.'.internal.selfenroll_start_access'},
                   5136:                 selfenroll_end_access         => $env{'course.'.$cid.'.internal.selfenroll_end_access'},
                   5137:                 default_enrollment_start_date => $env{'course.'.$cid.'.default_enrollment_start_date'},
                   5138:                 default_enrollment_end_date   => $env{'course.'.$cid.'.default_enrollment_end_date'},
1.400     raeburn  5139:                 uniquecode                    => $env{'course.'.$cid.'.internal.uniquecode'},
1.398     raeburn  5140:             );
                   5141:             push(@{$brcrum},
                   5142:                     {href => '/adm/createuser?action=selfenroll',
                   5143:                      text => "Configure Self-enrollment",
                   5144:                      help => 'Course_Self_Enrollment'});
                   5145:             if (!exists($env{'form.state'})) {
                   5146:                 $args = { bread_crumbs           => $brcrum,
                   5147:                           bread_crumbs_component => 'Configure Self-enrollment'};
                   5148:                 $r->print(&header(undef,$args));
                   5149:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
                   5150:                 &print_selfenroll_menu($r,'course',$cid,$cdom,$cnum,\%currsettings);
                   5151:             } elsif ($env{'form.state'} eq 'done') {
                   5152:                 push (@{$brcrum},
                   5153:                           {href=>'/adm/createuser?action=selfenroll',
                   5154:                            text=>"Result"});
                   5155:                 $args = { bread_crumbs           => $brcrum,
                   5156:                           bread_crumbs_component => 'Self-enrollment result'};
                   5157:                 $r->print(&header(undef,$args));
                   5158:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
1.400     raeburn  5159:                 &update_selfenroll_config($r,$cid,$cdom,$cnum,$context,$crstype,\%currsettings);
1.398     raeburn  5160:             }
                   5161:         } else {
                   5162:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5163:                      '<span class="LC_error">'.&mt('You do not have permission to configure self-enrollment').'</span>');
1.237     raeburn  5164:         }
1.277     raeburn  5165:     } elsif ($env{'form.action'} eq 'selfenrollqueue') {
1.406.2.6  raeburn  5166:         if ($permission->{selfenrolladmin}) {
1.351     raeburn  5167:             push(@{$brcrum},
                   5168:                      {href => '/adm/createuser?action=selfenrollqueue',
1.406.2.6  raeburn  5169:                       text => 'Enrollment requests',
1.351     raeburn  5170:                       help => 'Course_Self_Enrollment'});
1.406.2.6  raeburn  5171:             $bread_crumbs_component = 'Enrollment requests';
                   5172:             if ($env{'form.state'} eq 'done') {
                   5173:                 push(@{$brcrum},
                   5174:                          {href => '/adm/createuser?action=selfenrollqueue',
                   5175:                           text => 'Result',
                   5176:                           help => 'Course_Self_Enrollment'});
                   5177:                 $bread_crumbs_component = 'Enrollment result';
                   5178:             }
                   5179:             $args = { bread_crumbs           => $brcrum,
                   5180:                       bread_crumbs_component => $bread_crumbs_component};
                   5181:             $r->print(&header(undef,$args));
                   5182:             my $cid = $env{'request.course.id'};
                   5183:             my $cdom = $env{'course.'.$cid.'.domain'};
                   5184:             my $cnum = $env{'course.'.$cid.'.num'};
                   5185:             my $coursedesc = $env{'course.'.$cid.'.description'};
                   5186:             if (!exists($env{'form.state'})) {
                   5187:                 $r->print('<h3>'.&mt('Pending enrollment requests').'</h3>'."\n");
                   5188:                 $r->print(&Apache::loncoursequeueadmin::display_queued_requests($context,
                   5189:                                                                                 $cdom,$cnum));
                   5190:             } elsif ($env{'form.state'} eq 'done') {
                   5191:                 $r->print('<h3>'.&mt('Enrollment request processing').'</h3>'."\n");
                   5192:                 $r->print(&Apache::loncoursequeueadmin::update_request_queue($context,
                   5193:                               $cdom,$cnum,$coursedesc));
                   5194:             }
                   5195:         } else {
                   5196:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5197:                      '<span class="LC_error">'.&mt('You do not have permission to manage self-enrollment').'</span>');
1.277     raeburn  5198:         }
1.406.2.6  raeburn  5199: 
1.239     raeburn  5200:     } elsif ($env{'form.action'} eq 'changelogs') {
1.406.2.6  raeburn  5201:         if ($permission->{cusr} || $permission->{view}) {
                   5202:             &print_userchangelogs_display($r,$context,$permission,$brcrum);
                   5203:         } else {
                   5204:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5205:                      '<span class="LC_error">'.&mt('You do not have permission to view change logs').'</span>');
                   5206:         }
1.190     raeburn  5207:     } else {
1.351     raeburn  5208:         $bread_crumbs_component = 'User Management';
                   5209:         $args = { bread_crumbs           => $brcrum,
                   5210:                   bread_crumbs_component => $bread_crumbs_component};
                   5211:         $r->print(&header(undef,$args));
1.318     raeburn  5212:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  5213:     }
1.351     raeburn  5214:     $r->print(&Apache::loncommon::end_page());
1.190     raeburn  5215:     return OK;
                   5216: }
                   5217: 
                   5218: sub header {
1.351     raeburn  5219:     my ($jscript,$args) = @_;
1.190     raeburn  5220:     my $start_page;
1.351     raeburn  5221:     if (ref($args) eq 'HASH') {
                   5222:         $start_page=&Apache::loncommon::start_page('User Management',$jscript,$args);
1.190     raeburn  5223:     } else {
1.351     raeburn  5224:         $start_page=&Apache::loncommon::start_page('User Management',$jscript);
1.190     raeburn  5225:     }
                   5226:     return $start_page;
                   5227: }
1.2       www      5228: 
1.191     raeburn  5229: sub add_script {
                   5230:     my ($js) = @_;
1.301     bisitz   5231:     return '<script type="text/javascript">'."\n"
                   5232:           .'// <![CDATA['."\n"
                   5233:           .$js."\n"
                   5234:           .'// ]]>'."\n"
                   5235:           .'</script>'."\n";
1.191     raeburn  5236: }
                   5237: 
1.391     raeburn  5238: sub usernamerequest_javascript {
                   5239:     my $js = <<ENDJS;
                   5240: 
                   5241: function openusernamereqdisplay(dom,uname,queue) {
                   5242:     var url = '/adm/createuser?action=displayuserreq';
                   5243:     url += '&domain='+dom+'&username='+uname+'&queue='+queue;
                   5244:     var title = 'Account_Request_Browser';
                   5245:     var options = 'scrollbars=1,resizable=1,menubar=0';
                   5246:     options += ',width=700,height=600';
                   5247:     var stdeditbrowser = open(url,title,options,'1');
                   5248:     stdeditbrowser.focus();
                   5249:     return;
                   5250: }
                   5251:  
                   5252: ENDJS
                   5253: }
                   5254: 
                   5255: sub close_popup_form {
                   5256:     my $close= &mt('Close Window');
                   5257:     return << "END";
                   5258: <p><form name="displayreq" action="" method="post">
                   5259: <input type="button" name="closeme" value="$close" onclick="javascript:self.close();" />
                   5260: </form></p>
                   5261: END
                   5262: }
                   5263: 
1.202     raeburn  5264: sub verify_user_display {
1.364     raeburn  5265:     my ($context) = @_;
1.374     raeburn  5266:     my %lt = &Apache::lonlocal::texthash (
                   5267:         course    => 'course(s): description, section(s), status',
                   5268:         community => 'community(s): description, section(s), status',
                   5269:         author    => 'author',
                   5270:     );
1.364     raeburn  5271:     my $photos;
                   5272:     if (($context eq 'course') && $env{'request.course.id'}) {
                   5273:         $photos = $env{'course.'.$env{'request.course.id'}.'.internal.showphoto'};
                   5274:     }
1.202     raeburn  5275:     my $output = <<"END";
                   5276: 
1.364     raeburn  5277: function hide_searching() {
                   5278:     if (document.getElementById('searching')) {
                   5279:         document.getElementById('searching').style.display = 'none';
                   5280:     }
                   5281:     return;
                   5282: }
                   5283: 
1.202     raeburn  5284: function display_update() {
                   5285:     document.studentform.action.value = 'listusers';
                   5286:     document.studentform.phase.value = 'display';
                   5287:     document.studentform.submit();
                   5288: }
                   5289: 
1.364     raeburn  5290: function updateCols(caller) {
                   5291:     var context = '$context';
                   5292:     var photos = '$photos';
                   5293:     if (caller == 'Status') {
1.374     raeburn  5294:         if ((context == 'domain') && 
                   5295:             ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   5296:              (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community'))) {
1.364     raeburn  5297:             document.getElementById('showcolstatus').checked = false;
                   5298:             document.getElementById('showcolstatus').disabled = 'disabled';
                   5299:             document.getElementById('showcolstart').checked = false;
                   5300:             document.getElementById('showcolend').checked = false;
1.374     raeburn  5301:         } else {
                   5302:             if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   5303:                 document.getElementById('showcolstatus').checked = true;
                   5304:                 document.getElementById('showcolstatus').disabled = '';
                   5305:                 document.getElementById('showcolstart').checked = true;
                   5306:                 document.getElementById('showcolend').checked = true;
                   5307:             } else {
                   5308:                 document.getElementById('showcolstatus').checked = false;
                   5309:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   5310:                 document.getElementById('showcolstart').checked = false;
                   5311:                 document.getElementById('showcolend').checked = false;
                   5312:             }
1.364     raeburn  5313:         }
                   5314:     }
                   5315:     if (caller == 'output') {
                   5316:         if (photos == 1) {
                   5317:             if (document.getElementById('showcolphoto')) {
                   5318:                 var photoitem = document.getElementById('showcolphoto');
                   5319:                 if (document.studentform.output.options[document.studentform.output.selectedIndex].value == 'html') {
                   5320:                     photoitem.checked = true;
                   5321:                     photoitem.disabled = '';
                   5322:                 } else {
                   5323:                     photoitem.checked = false;
                   5324:                     photoitem.disabled = 'disabled';
                   5325:                 }
                   5326:             }
                   5327:         }
                   5328:     }
                   5329:     if (caller == 'showrole') {
1.371     raeburn  5330:         if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any') ||
                   5331:             (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'cr')) {
1.364     raeburn  5332:             document.getElementById('showcolrole').checked = true;
                   5333:             document.getElementById('showcolrole').disabled = '';
                   5334:         } else {
                   5335:             document.getElementById('showcolrole').checked = false;
                   5336:             document.getElementById('showcolrole').disabled = 'disabled';
                   5337:         }
1.374     raeburn  5338:         if (context == 'domain') {
1.382     raeburn  5339:             var quotausageshow = 0;
1.374     raeburn  5340:             if ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   5341:                 (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community')) {
                   5342:                 document.getElementById('showcolstatus').checked = false;
                   5343:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   5344:                 document.getElementById('showcolstart').checked = false;
                   5345:                 document.getElementById('showcolend').checked = false;
                   5346:             } else {
                   5347:                 if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   5348:                     document.getElementById('showcolstatus').checked = true;
                   5349:                     document.getElementById('showcolstatus').disabled = '';
                   5350:                     document.getElementById('showcolstart').checked = true;
                   5351:                     document.getElementById('showcolend').checked = true;
                   5352:                 }
                   5353:             }
                   5354:             if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'domain') {
                   5355:                 document.getElementById('showcolextent').disabled = 'disabled';
                   5356:                 document.getElementById('showcolextent').checked = 'false';
                   5357:                 document.getElementById('showextent').style.display='none';
                   5358:                 document.getElementById('showcoltextextent').innerHTML = '';
1.382     raeburn  5359:                 if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'au') ||
                   5360:                     (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any')) {
                   5361:                     if (document.getElementById('showcolauthorusage')) {
                   5362:                         document.getElementById('showcolauthorusage').disabled = '';
                   5363:                     }
                   5364:                     if (document.getElementById('showcolauthorquota')) {
                   5365:                         document.getElementById('showcolauthorquota').disabled = '';
                   5366:                     }
                   5367:                     quotausageshow = 1;
                   5368:                 }
1.374     raeburn  5369:             } else {
                   5370:                 document.getElementById('showextent').style.display='block';
                   5371:                 document.getElementById('showextent').style.textAlign='left';
                   5372:                 document.getElementById('showextent').style.textFace='normal';
                   5373:                 if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'author') {
                   5374:                     document.getElementById('showcolextent').disabled = '';
                   5375:                     document.getElementById('showcolextent').checked = 'true';
                   5376:                     document.getElementById('showcoltextextent').innerHTML="$lt{'author'}";
                   5377:                 } else {
                   5378:                     document.getElementById('showcolextent').disabled = '';
                   5379:                     document.getElementById('showcolextent').checked = 'true';
                   5380:                     if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community') {
                   5381:                         document.getElementById('showcoltextextent').innerHTML="$lt{'community'}";
                   5382:                     } else {
                   5383:                         document.getElementById('showcoltextextent').innerHTML="$lt{'course'}";
                   5384:                     }
                   5385:                 }
                   5386:             }
1.382     raeburn  5387:             if (quotausageshow == 0)  {
                   5388:                 if (document.getElementById('showcolauthorusage')) {
                   5389:                     document.getElementById('showcolauthorusage').checked = false;
                   5390:                     document.getElementById('showcolauthorusage').disabled = 'disabled';
                   5391:                 }
                   5392:                 if (document.getElementById('showcolauthorquota')) {
                   5393:                     document.getElementById('showcolauthorquota').checked = false;
                   5394:                     document.getElementById('showcolauthorquota').disabled = 'disabled';
                   5395:                 }
                   5396:             }
1.374     raeburn  5397:         }
1.364     raeburn  5398:     }
                   5399:     return;
                   5400: }
                   5401: 
1.202     raeburn  5402: END
                   5403:     return $output;
                   5404: 
                   5405: }
                   5406: 
1.190     raeburn  5407: ###############################################################
                   5408: ###############################################################
                   5409: #  Menu Phase One
                   5410: sub print_main_menu {
1.318     raeburn  5411:     my ($permission,$context,$crstype) = @_;
                   5412:     my $linkcontext = $context;
                   5413:     my $stuterm = lc(&Apache::lonnet::plaintext('st',$crstype));
                   5414:     if (($context eq 'course') && ($crstype eq 'Community')) {
                   5415:         $linkcontext = lc($crstype);
                   5416:         $stuterm = 'Members';
                   5417:     }
1.208     raeburn  5418:     my %links = (
1.298     droeschl 5419:                 domain => {
                   5420:                             upload     => 'Upload a File of Users',
                   5421:                             singleuser => 'Add/Modify a User',
                   5422:                             listusers  => 'Manage Users',
                   5423:                             },
                   5424:                 author => {
                   5425:                             upload     => 'Upload a File of Co-authors',
                   5426:                             singleuser => 'Add/Modify a Co-author',
                   5427:                             listusers  => 'Manage Co-authors',
                   5428:                             },
                   5429:                 course => {
                   5430:                             upload     => 'Upload a File of Course Users',
                   5431:                             singleuser => 'Add/Modify a Course User',
1.354     www      5432:                             listusers  => 'List and Modify Multiple Course Users',
1.298     droeschl 5433:                             },
1.318     raeburn  5434:                 community => {
                   5435:                             upload     => 'Upload a File of Community Users',
                   5436:                             singleuser => 'Add/Modify a Community User',
1.354     www      5437:                             listusers  => 'List and Modify Multiple Community Users',
1.318     raeburn  5438:                            },
                   5439:                 );
                   5440:      my %linktitles = (
                   5441:                 domain => {
                   5442:                             singleuser => 'Add a user to the domain, and/or a course or community in the domain.',
                   5443:                             listusers  => 'Show and manage users in this domain.',
                   5444:                             },
                   5445:                 author => {
                   5446:                             singleuser => 'Add a user with a co- or assistant author role.',
                   5447:                             listusers  => 'Show and manage co- or assistant authors.',
                   5448:                             },
                   5449:                 course => {
                   5450:                             singleuser => 'Add a user with a certain role to this course.',
                   5451:                             listusers  => 'Show and manage users in this course.',
                   5452:                             },
                   5453:                 community => {
                   5454:                             singleuser => 'Add a user with a certain role to this community.',
                   5455:                             listusers  => 'Show and manage users in this community.',
                   5456:                            },
1.298     droeschl 5457:                 );
1.406.2.6  raeburn  5458:   if ($linkcontext eq 'domain') {
                   5459:       unless ($permission->{'cusr'}) {
                   5460:           $links{'domain'}{'singleuser'} = 'View a User';
                   5461:           $linktitles{'domain'}{'singleuser'} = 'View information about a user in the domain';
                   5462: 
                   5463:       }
                   5464:   } elsif ($linkcontext eq 'course') {
                   5465:       unless ($permission->{'cusr'}) {
                   5466:           $links{'course'}{'singleuser'} = 'View a Course User';
                   5467:           $linktitles{'course'}{'singleuser'} = 'View information about a user in this course';
                   5468:           $links{'course'}{'listusers'} = 'List Course Users';
                   5469:           $linktitles{'course'}{'listusers'} = 'Show information about users in this course';
                   5470:       }
                   5471:   } elsif ($linkcontext eq 'community') {
                   5472:       unless ($permission->{'cusr'}) {
                   5473:           $links{'community'}{'singleuser'} = 'View a Community User';
                   5474:           $linktitles{'community'}{'singleuser'} = 'View information about a user in this community';
                   5475:           $links{'community'}{'listusers'} = 'List Community Users';
                   5476:           $linktitles{'community'}{'listusers'} = 'Show information about users in this community';
                   5477:       }
                   5478:   }
1.298     droeschl 5479:   my @menu = ( {categorytitle => 'Single Users', 
                   5480:          items =>
                   5481:          [
                   5482:             {
1.318     raeburn  5483:              linktext => $links{$linkcontext}{'singleuser'},
1.298     droeschl 5484:              icon => 'edit-redo.png',
                   5485:              #help => 'Course_Change_Privileges',
                   5486:              url => '/adm/createuser?action=singleuser',
1.406.2.6  raeburn  5487:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  5488:              linktitle => $linktitles{$linkcontext}{'singleuser'},
1.298     droeschl 5489:             },
                   5490:          ]},
                   5491: 
                   5492:          {categorytitle => 'Multiple Users',
                   5493:          items => 
                   5494:          [
                   5495:             {
1.318     raeburn  5496:              linktext => $links{$linkcontext}{'upload'},
1.340     wenzelju 5497:              icon => 'uplusr.png',
1.298     droeschl 5498:              #help => 'Course_Create_Class_List',
                   5499:              url => '/adm/createuser?action=upload',
                   5500:              permission => $permission->{'cusr'},
                   5501:              linktitle => 'Upload a CSV or a text file containing users.',
                   5502:             },
                   5503:             {
1.318     raeburn  5504:              linktext => $links{$linkcontext}{'listusers'},
1.340     wenzelju 5505:              icon => 'mngcu.png',
1.298     droeschl 5506:              #help => 'Course_View_Class_List',
                   5507:              url => '/adm/createuser?action=listusers',
                   5508:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  5509:              linktitle => $linktitles{$linkcontext}{'listusers'}, 
1.298     droeschl 5510:             },
                   5511: 
                   5512:          ]},
                   5513: 
                   5514:          {categorytitle => 'Administration',
                   5515:          items => [ ]},
                   5516:        );
1.406.2.5  raeburn  5517: 
1.265     mielkec  5518:     if ($context eq 'domain'){
1.406.2.5  raeburn  5519:         push(@{  $menu[0]->{items} }, # Single Users
                   5520:             {
                   5521:              linktext => 'User Access Log',
                   5522:              icon => 'document-properties.png',
1.406.2.8! raeburn  5523:              #help => 'Domain_User_Access_Logs',
1.406.2.5  raeburn  5524:              url => '/adm/createuser?action=accesslogs',
                   5525:              permission => $permission->{'activity'},
                   5526:              linktitle => 'View user access log.',
                   5527:             }
                   5528:         );
1.298     droeschl 5529:         
                   5530:         push(@{ $menu[2]->{items} }, #Category: Administration
                   5531:             {
                   5532:              linktext => 'Custom Roles',
                   5533:              icon => 'emblem-photos.png',
                   5534:              #help => 'Course_Editing_Custom_Roles',
                   5535:              url => '/adm/createuser?action=custom',
                   5536:              permission => $permission->{'custom'},
                   5537:              linktitle => 'Configure a custom role.',
                   5538:             },
1.362     raeburn  5539:             {
                   5540:              linktext => 'Authoring Space Requests',
                   5541:              icon => 'selfenrl-queue.png',
                   5542:              #help => 'Domain_Role_Approvals',
                   5543:              url => '/adm/createuser?action=processauthorreq',
                   5544:              permission => $permission->{'cusr'},
                   5545:              linktitle => 'Approve or reject author role requests',
                   5546:             },
1.363     raeburn  5547:             {
1.391     raeburn  5548:              linktext => 'LON-CAPA Account Requests',
                   5549:              icon => 'list-add.png',
                   5550:              #help => 'Domain_Username_Approvals',
                   5551:              url => '/adm/createuser?action=processusernamereq',
                   5552:              permission => $permission->{'cusr'},
                   5553:              linktitle => 'Approve or reject LON-CAPA account requests',
                   5554:             },
                   5555:             {
1.363     raeburn  5556:              linktext => 'Change Log',
                   5557:              icon => 'document-properties.png',
                   5558:              #help => 'Course_User_Logs',
                   5559:              url => '/adm/createuser?action=changelogs',
1.406.2.6  raeburn  5560:              permission => ($permission->{'cusr'} || $permission->{'view'}),
1.363     raeburn  5561:              linktitle => 'View change log.',
                   5562:             },
1.298     droeschl 5563:         );
                   5564:         
1.265     mielkec  5565:     }elsif ($context eq 'course'){
1.298     droeschl 5566:         my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
1.318     raeburn  5567: 
                   5568:         my %linktext = (
                   5569:                          'Course'    => {
                   5570:                                           single => 'Add/Modify a Student', 
                   5571:                                           drop   => 'Drop Students',
                   5572:                                           groups => 'Course Groups',
                   5573:                                         },
                   5574:                          'Community' => {
                   5575:                                           single => 'Add/Modify a Member', 
                   5576:                                           drop   => 'Drop Members',
                   5577:                                           groups => 'Community Groups',
                   5578:                                         },
                   5579:                        );
                   5580: 
                   5581:         my %linktitle = (
                   5582:             'Course' => {
                   5583:                   single => 'Add a user with the role of student to this course',
                   5584:                   drop   => 'Remove a student from this course.',
                   5585:                   groups => 'Manage course groups',
                   5586:                         },
                   5587:             'Community' => {
                   5588:                   single => 'Add a user with the role of member to this community',
                   5589:                   drop   => 'Remove a member from this community.',
                   5590:                   groups => 'Manage community groups',
                   5591:                            },
                   5592:         );
                   5593: 
1.298     droeschl 5594:         push(@{ $menu[0]->{items} }, #Category: Single Users
                   5595:             {   
1.318     raeburn  5596:              linktext => $linktext{$crstype}{'single'},
1.298     droeschl 5597:              #help => 'Course_Add_Student',
                   5598:              icon => 'list-add.png',
                   5599:              url => '/adm/createuser?action=singlestudent',
                   5600:              permission => $permission->{'cusr'},
1.318     raeburn  5601:              linktitle => $linktitle{$crstype}{'single'},
1.298     droeschl 5602:             },
                   5603:         );
                   5604:         
                   5605:         push(@{ $menu[1]->{items} }, #Category: Multiple Users 
                   5606:             {
1.318     raeburn  5607:              linktext => $linktext{$crstype}{'drop'},
1.298     droeschl 5608:              icon => 'edit-undo.png',
                   5609:              #help => 'Course_Drop_Student',
                   5610:              url => '/adm/createuser?action=drop',
                   5611:              permission => $permission->{'cusr'},
1.318     raeburn  5612:              linktitle => $linktitle{$crstype}{'drop'},
1.298     droeschl 5613:             },
                   5614:         );
                   5615:         push(@{ $menu[2]->{items} }, #Category: Administration
                   5616:             {    
                   5617:              linktext => 'Custom Roles',
                   5618:              icon => 'emblem-photos.png',
                   5619:              #help => 'Course_Editing_Custom_Roles',
                   5620:              url => '/adm/createuser?action=custom',
                   5621:              permission => $permission->{'custom'},
                   5622:              linktitle => 'Configure a custom role.',
                   5623:             },
                   5624:             {
1.318     raeburn  5625:              linktext => $linktext{$crstype}{'groups'},
1.333     wenzelju 5626:              icon => 'grps.png',
1.298     droeschl 5627:              #help => 'Course_Manage_Group',
                   5628:              url => '/adm/coursegroups?refpage=cusr',
                   5629:              permission => $permission->{'grp_manage'},
1.318     raeburn  5630:              linktitle => $linktitle{$crstype}{'groups'},
1.298     droeschl 5631:             },
                   5632:             {
1.328     wenzelju 5633:              linktext => 'Change Log',
1.298     droeschl 5634:              icon => 'document-properties.png',
                   5635:              #help => 'Course_User_Logs',
                   5636:              url => '/adm/createuser?action=changelogs',
1.406.2.6  raeburn  5637:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.298     droeschl 5638:              linktitle => 'View change log.',
                   5639:             },
                   5640:         );
1.277     raeburn  5641:         if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'}) {
1.298     droeschl 5642:             push(@{ $menu[2]->{items} },
1.398     raeburn  5643:                     {
1.298     droeschl 5644:                      linktext => 'Enrollment Requests',
                   5645:                      icon => 'selfenrl-queue.png',
                   5646:                      #help => 'Course_Approve_Selfenroll',
                   5647:                      url => '/adm/createuser?action=selfenrollqueue',
1.398     raeburn  5648:                      permission => $permission->{'selfenrolladmin'},
1.298     droeschl 5649:                      linktitle =>'Approve or reject enrollment requests.',
                   5650:                     },
                   5651:             );
1.277     raeburn  5652:         }
1.298     droeschl 5653:         
1.265     mielkec  5654:         if (!exists($permission->{'cusr_section'})){
1.320     raeburn  5655:             if ($crstype ne 'Community') {
                   5656:                 push(@{ $menu[2]->{items} },
                   5657:                     {
                   5658:                      linktext => 'Automated Enrollment',
                   5659:                      icon => 'roles.png',
                   5660:                      #help => 'Course_Automated_Enrollment',
                   5661:                      permission => (&Apache::lonnet::auto_run($cnum,$cdom)
1.406.2.6  raeburn  5662:                                          && (($permission->{'cusr'}) ||
                   5663:                                              ($permission->{'view'}))),
1.320     raeburn  5664:                      url  => '/adm/populate',
                   5665:                      linktitle => 'Automated enrollment manager.',
                   5666:                     }
                   5667:                 );
                   5668:             }
                   5669:             push(@{ $menu[2]->{items} }, 
1.298     droeschl 5670:                 {
                   5671:                  linktext => 'User Self-Enrollment',
1.342     wenzelju 5672:                  icon => 'self_enroll.png',
1.298     droeschl 5673:                  #help => 'Course_Self_Enrollment',
                   5674:                  url => '/adm/createuser?action=selfenroll',
1.398     raeburn  5675:                  permission => $permission->{'selfenrolladmin'},
1.317     bisitz   5676:                  linktitle => 'Configure user self-enrollment.',
1.298     droeschl 5677:                 },
                   5678:             );
                   5679:         }
1.363     raeburn  5680:     } elsif ($context eq 'author') {
1.370     raeburn  5681:         push(@{ $menu[2]->{items} }, #Category: Administration
1.363     raeburn  5682:             {
                   5683:              linktext => 'Change Log',
                   5684:              icon => 'document-properties.png',
                   5685:              #help => 'Course_User_Logs',
                   5686:              url => '/adm/createuser?action=changelogs',
                   5687:              permission => $permission->{'cusr'},
                   5688:              linktitle => 'View change log.',
                   5689:             },
1.370     raeburn  5690:         );
1.363     raeburn  5691:     }
                   5692:     return Apache::lonhtmlcommon::generate_menu(@menu);
1.250     raeburn  5693: #               { text => 'View Log-in History',
                   5694: #                 help => 'Course_User_Logins',
                   5695: #                 action => 'logins',
                   5696: #                 permission => $permission->{'cusr'},
                   5697: #               });
1.190     raeburn  5698: }
                   5699: 
1.189     albertel 5700: sub restore_prev_selections {
                   5701:     my %saveable_parameters = ('srchby'   => 'scalar',
                   5702: 			       'srchin'   => 'scalar',
                   5703: 			       'srchtype' => 'scalar',
                   5704: 			       );
                   5705:     &Apache::loncommon::store_settings('user','user_picker',
                   5706: 				       \%saveable_parameters);
                   5707:     &Apache::loncommon::restore_settings('user','user_picker',
                   5708: 					 \%saveable_parameters);
                   5709: }
                   5710: 
1.237     raeburn  5711: sub print_selfenroll_menu {
1.406.2.6  raeburn  5712:     my ($r,$context,$cid,$cdom,$cnum,$currsettings,$additional,$readonly) = @_;
1.322     raeburn  5713:     my $crstype = &Apache::loncommon::course_type();
1.398     raeburn  5714:     my $formname = 'selfenroll';
1.237     raeburn  5715:     my $nolink = 1;
1.398     raeburn  5716:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
1.237     raeburn  5717:     my $groupslist = &Apache::lonuserutils::get_groupslist();
                   5718:     my $setsec_js = 
                   5719:         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
1.249     raeburn  5720:     my %alerts = &Apache::lonlocal::texthash(
                   5721:         acto => 'Activation of self-enrollment was selected for the following domain(s)',
                   5722:         butn => 'but no user types have been checked.',
                   5723:         wilf => "Please uncheck 'activate' or check at least one type.",
                   5724:     );
1.406.2.6  raeburn  5725:     my $disabled;
                   5726:     if ($readonly) {
                   5727:        $disabled = ' disabled="disabled"';
                   5728:     }
1.405     damieng  5729:     &js_escape(\%alerts);
1.249     raeburn  5730:     my $selfenroll_js = <<"ENDSCRIPT";
                   5731: function update_types(caller,num) {
                   5732:     var delidx = getIndexByName('selfenroll_delete');
                   5733:     var actidx = getIndexByName('selfenroll_activate');
                   5734:     if (caller == 'selfenroll_all') {
                   5735:         var selall;
                   5736:         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5737:             if (document.$formname.selfenroll_all[i].checked) {
                   5738:                 selall = document.$formname.selfenroll_all[i].value;
                   5739:             }
                   5740:         }
                   5741:         if (selall == 1) {
                   5742:             if (delidx != -1) {
                   5743:                 if (document.$formname.selfenroll_delete.length) {
                   5744:                     for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5745:                         document.$formname.selfenroll_delete[j].checked = true;
                   5746:                     }
                   5747:                 } else {
                   5748:                     document.$formname.elements[delidx].checked = true;
                   5749:                 }
                   5750:             }
                   5751:             if (actidx != -1) {
                   5752:                 if (document.$formname.selfenroll_activate.length) {
                   5753:                     for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5754:                         document.$formname.selfenroll_activate[j].checked = false;
                   5755:                     }
                   5756:                 } else {
                   5757:                     document.$formname.elements[actidx].checked = false;
                   5758:                 }
                   5759:             }
                   5760:             document.$formname.selfenroll_newdom.selectedIndex = 0; 
                   5761:         }
                   5762:     }
                   5763:     if (caller == 'selfenroll_activate') {
                   5764:         if (document.$formname.selfenroll_activate.length) {
                   5765:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5766:                 if (document.$formname.selfenroll_activate[j].value == num) {
                   5767:                     if (document.$formname.selfenroll_activate[j].checked) {
                   5768:                         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5769:                             if (document.$formname.selfenroll_all[i].value == '1') {
                   5770:                                 document.$formname.selfenroll_all[i].checked = false;
                   5771:                             }
                   5772:                             if (document.$formname.selfenroll_all[i].value == '0') {
                   5773:                                 document.$formname.selfenroll_all[i].checked = true;
                   5774:                             }
                   5775:                         }
                   5776:                     }
                   5777:                 }
                   5778:             }
                   5779:         } else {
                   5780:             for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5781:                 if (document.$formname.selfenroll_all[i].value == '1') {
                   5782:                     document.$formname.selfenroll_all[i].checked = false;
                   5783:                 }
                   5784:                 if (document.$formname.selfenroll_all[i].value == '0') {
                   5785:                     document.$formname.selfenroll_all[i].checked = true;
                   5786:                 }
                   5787:             }
                   5788:         }
                   5789:     }
                   5790:     if (caller == 'selfenroll_delete') {
                   5791:         if (document.$formname.selfenroll_delete.length) {
                   5792:             for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5793:                 if (document.$formname.selfenroll_delete[j].value == num) {
                   5794:                     if (document.$formname.selfenroll_delete[j].checked) {
                   5795:                         var delindex = getIndexByName('selfenroll_types_'+num);
                   5796:                         if (delindex != -1) { 
                   5797:                             if (document.$formname.elements[delindex].length) {
                   5798:                                 for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5799:                                     document.$formname.elements[delindex][k].checked = false;
                   5800:                                 }
                   5801:                             } else {
                   5802:                                 document.$formname.elements[delindex].checked = false;
                   5803:                             }
                   5804:                         }
                   5805:                     }
                   5806:                 }
                   5807:             }
                   5808:         } else {
                   5809:             if (document.$formname.selfenroll_delete.checked) {
                   5810:                 var delindex = getIndexByName('selfenroll_types_'+num);
                   5811:                 if (delindex != -1) {
                   5812:                     if (document.$formname.elements[delindex].length) {
                   5813:                         for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5814:                             document.$formname.elements[delindex][k].checked = false;
                   5815:                         }
                   5816:                     } else {
                   5817:                         document.$formname.elements[delindex].checked = false;
                   5818:                     }
                   5819:                 }
                   5820:             }
                   5821:         }
                   5822:     }
                   5823:     return;
                   5824: }
                   5825: 
                   5826: function validate_types(form) {
                   5827:     var needaction = new Array();
                   5828:     var countfail = 0;
                   5829:     var actidx = getIndexByName('selfenroll_activate');
                   5830:     if (actidx != -1) {
                   5831:         if (document.$formname.selfenroll_activate.length) {
                   5832:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5833:                 var num = document.$formname.selfenroll_activate[j].value;
                   5834:                 if (document.$formname.selfenroll_activate[j].checked) {
                   5835:                     countfail = check_types(num,countfail,needaction)
                   5836:                 }
                   5837:             }
                   5838:         } else {
                   5839:             if (document.$formname.selfenroll_activate.checked) {
1.398     raeburn  5840:                 var num = document.$formname.selfenroll_activate.value;
1.249     raeburn  5841:                 countfail = check_types(num,countfail,needaction)
                   5842:             }
                   5843:         }
                   5844:     }
                   5845:     if (countfail > 0) {
                   5846:         var msg = "$alerts{'acto'}\\n";
                   5847:         var loopend = needaction.length -1;
                   5848:         if (loopend > 0) {
                   5849:             for (var m=0; m<loopend; m++) {
                   5850:                 msg += needaction[m]+", ";
                   5851:             }
                   5852:         }
                   5853:         msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
                   5854:         alert(msg);
                   5855:         return; 
                   5856:     }
                   5857:     setSections(form);
                   5858: }
                   5859: 
                   5860: function check_types(num,countfail,needaction) {
                   5861:     var typeidx = getIndexByName('selfenroll_types_'+num);
                   5862:     var count = 0;
                   5863:     if (typeidx != -1) {
                   5864:         if (document.$formname.elements[typeidx].length) {
                   5865:             for (var k=0; k<document.$formname.elements[typeidx].length; k++) {
                   5866:                 if (document.$formname.elements[typeidx][k].checked) {
                   5867:                     count ++;
                   5868:                 }
                   5869:             }
                   5870:         } else {
                   5871:             if (document.$formname.elements[typeidx].checked) {
                   5872:                 count ++;
                   5873:             }
                   5874:         }
                   5875:         if (count == 0) {
                   5876:             var domidx = getIndexByName('selfenroll_dom_'+num);
                   5877:             if (domidx != -1) {
                   5878:                 var domname = document.$formname.elements[domidx].value;
                   5879:                 needaction[countfail] = domname;
                   5880:                 countfail ++;
                   5881:             }
                   5882:         }
                   5883:     }
                   5884:     return countfail;
                   5885: }
                   5886: 
1.398     raeburn  5887: function toggleNotify() {
                   5888:     var selfenrollApproval = 0;
                   5889:     if (document.$formname.selfenroll_approval.length) {
                   5890:         for (var i=0; i<document.$formname.selfenroll_approval.length; i++) {
                   5891:             if (document.$formname.selfenroll_approval[i].checked) {
                   5892:                 selfenrollApproval = document.$formname.selfenroll_approval[i].value;
                   5893:                 break;        
                   5894:             }
                   5895:         }
                   5896:     }
                   5897:     if (document.getElementById('notified')) {
                   5898:         if (selfenrollApproval == 0) {
                   5899:             document.getElementById('notified').style.display='none';
                   5900:         } else {
                   5901:             document.getElementById('notified').style.display='block';
                   5902:         }
                   5903:     }
                   5904:     return;
                   5905: }
                   5906: 
1.249     raeburn  5907: function getIndexByName(item) {
                   5908:     for (var i=0;i<document.$formname.elements.length;i++) {
                   5909:         if (document.$formname.elements[i].name == item) {
                   5910:             return i;
                   5911:         }
                   5912:     }
                   5913:     return -1;
                   5914: }
                   5915: ENDSCRIPT
1.256     raeburn  5916: 
1.237     raeburn  5917:     my $output = '<script type="text/javascript">'."\n".
1.301     bisitz   5918:                  '// <![CDATA['."\n".
1.249     raeburn  5919:                  $setsec_js."\n".$selfenroll_js."\n".
1.301     bisitz   5920:                  '// ]]>'."\n".
1.237     raeburn  5921:                  '</script>'."\n".
1.256     raeburn  5922:                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n";
1.400     raeburn  5923:  
                   5924:     my $visactions = &cat_visibility();
                   5925:     my ($cathash,%cattype);
                   5926:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   5927:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   5928:         $cathash = $domconfig{'coursecategories'}{'cats'};
                   5929:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
                   5930:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
1.406     raeburn  5931:         if ($cattype{'auth'} eq '') {
                   5932:             $cattype{'auth'} = 'std';
                   5933:         }
                   5934:         if ($cattype{'unauth'} eq '') {
                   5935:             $cattype{'unauth'} = 'std';
                   5936:         }
1.400     raeburn  5937:     } else {
                   5938:         $cathash = {};
                   5939:         $cattype{'auth'} = 'std';
                   5940:         $cattype{'unauth'} = 'std';
                   5941:     }
                   5942:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
                   5943:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   5944:                   '<br />'.
                   5945:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   5946:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
                   5947:                   '</ul>');
                   5948:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
                   5949:         if ($currsettings->{'uniquecode'}) {
                   5950:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
                   5951:         } else {
                   5952:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   5953:                   '<br />'.
                   5954:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   5955:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
                   5956:                   '</ul><br />');
                   5957:         }
                   5958:     } else {
                   5959:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
                   5960:         if (ref($visactions) eq 'HASH') {
                   5961:             if ($visible) {
                   5962:                 $output .= '<p class="LC_info">'.$visactions->{'vis'}.'</p>';
                   5963:            } else {
                   5964:                 $output .= '<p class="LC_warning">'.$visactions->{'miss'}.'</p>'
                   5965:                           .$visactions->{'yous'}.
                   5966:                            '<p>'.$visactions->{'gen'}.'<br />'.$visactions->{'coca'};
                   5967:                 if (ref($vismsgs) eq 'ARRAY') {
                   5968:                     $output .= '<br />'.$visactions->{'make'}.'<ul>';
                   5969:                     foreach my $item (@{$vismsgs}) {
                   5970:                         $output .= '<li>'.$visactions->{$item}.'</li>';
                   5971:                     }
                   5972:                     $output .= '</ul>';
1.256     raeburn  5973:                 }
1.400     raeburn  5974:                 $output .= '</p>';
1.256     raeburn  5975:             }
                   5976:         }
                   5977:     }
1.398     raeburn  5978:     my $actionhref = '/adm/createuser';
                   5979:     if ($context eq 'domain') {
                   5980:         $actionhref = '/adm/modifycourse';
                   5981:     }
1.400     raeburn  5982: 
                   5983:     my %noedit;
                   5984:     unless ($context eq 'domain') {
                   5985:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
                   5986:     }
1.398     raeburn  5987:     $output .= '<form name="'.$formname.'" method="post" action="'.$actionhref.'">'."\n".
1.256     raeburn  5988:                &Apache::lonhtmlcommon::start_pick_box();
1.237     raeburn  5989:     if (ref($row) eq 'ARRAY') {
                   5990:         foreach my $item (@{$row}) {
                   5991:             my $title = $item; 
                   5992:             if (ref($lt) eq 'HASH') {
                   5993:                 $title = $lt->{$item};
                   5994:             }
1.297     bisitz   5995:             $output .= &Apache::lonhtmlcommon::row_title($title);
1.237     raeburn  5996:             if ($item eq 'types') {
1.398     raeburn  5997:                 my $curr_types;
                   5998:                 if (ref($currsettings) eq 'HASH') {
                   5999:                     $curr_types = $currsettings->{'selfenroll_types'};
                   6000:                 }
1.400     raeburn  6001:                 if ($noedit{$item}) {
                   6002:                     if ($curr_types eq '*') {
                   6003:                         $output .= &mt('Any user in any domain');   
                   6004:                     } else {
                   6005:                         my @entries = split(/;/,$curr_types);
                   6006:                         if (@entries > 0) {
                   6007:                             $output .= '<ul>'; 
                   6008:                             foreach my $entry (@entries) {
                   6009:                                 my ($currdom,$typestr) = split(/:/,$entry);
                   6010:                                 next if ($typestr eq '');
                   6011:                                 my $domdesc = &Apache::lonnet::domain($currdom);
                   6012:                                 my @currinsttypes = split(',',$typestr);
                   6013:                                 my ($othertitle,$usertypes,$types) = 
                   6014:                                     &Apache::loncommon::sorted_inst_types($currdom);
                   6015:                                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   6016:                                     $usertypes->{'any'} = &mt('any user'); 
                   6017:                                     if (keys(%{$usertypes}) > 0) {
                   6018:                                         $usertypes->{'other'} = &mt('other users');
                   6019:                                     }
                   6020:                                     my @longinsttypes = map { $usertypes->{$_}; } @currinsttypes;
                   6021:                                     $output .= '<li>'.$domdesc.':'.join(', ',@longinsttypes).'</li>';
                   6022:                                  }
                   6023:                             }
                   6024:                             $output .= '</ul>';
                   6025:                         } else {
                   6026:                             $output .= &mt('None');
                   6027:                         }
                   6028:                     }
                   6029:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6030:                     next;
                   6031:                 }
1.241     raeburn  6032:                 my $showdomdesc = 1;
                   6033:                 my $includeempty = 1;
                   6034:                 my $num = 0;
                   6035:                 $output .= &Apache::loncommon::start_data_table().
                   6036:                            &Apache::loncommon::start_data_table_row()
                   6037:                            .'<td colspan="2"><span class="LC_nobreak"><label>'
                   6038:                            .&mt('Any user in any domain:')
                   6039:                            .'&nbsp;<input type="radio" name="selfenroll_all" value="1" ';
                   6040:                 if ($curr_types eq '*') {
                   6041:                     $output .= ' checked="checked" '; 
                   6042:                 }
1.249     raeburn  6043:                 $output .= 'onchange="javascript:update_types('.
1.406.2.6  raeburn  6044:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('Yes').'</label>'.
1.249     raeburn  6045:                            '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
1.241     raeburn  6046:                 if ($curr_types ne '*') {
                   6047:                     $output .= ' checked="checked" ';
                   6048:                 }
1.249     raeburn  6049:                 $output .= ' onchange="javascript:update_types('.
1.406.2.6  raeburn  6050:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('No').'</label></td>'.
1.249     raeburn  6051:                            &Apache::loncommon::end_data_table_row().
                   6052:                            &Apache::loncommon::end_data_table().
                   6053:                            &mt('Or').'<br />'.
                   6054:                            &Apache::loncommon::start_data_table();
1.241     raeburn  6055:                 my %currdoms;
1.249     raeburn  6056:                 if ($curr_types eq '') {
1.241     raeburn  6057:                     $output .= &new_selfenroll_dom_row($cdom,'0');
                   6058:                 } elsif ($curr_types ne '*') {
                   6059:                     my @entries = split(/;/,$curr_types);
                   6060:                     if (@entries > 0) {
                   6061:                         foreach my $entry (@entries) {
                   6062:                             my ($currdom,$typestr) = split(/:/,$entry);
                   6063:                             $currdoms{$currdom} = 1;
                   6064:                             my $domdesc = &Apache::lonnet::domain($currdom);
1.249     raeburn  6065:                             my @currinsttypes = split(',',$typestr);
1.241     raeburn  6066:                             $output .= &Apache::loncommon::start_data_table_row()
                   6067:                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
                   6068:                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
                   6069:                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
                   6070:                                        .'" value="'.$currdom.'" /></span><br />'
                   6071:                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
1.406.2.6  raeburn  6072:                                        .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');"'.$disabled.' />'
1.241     raeburn  6073:                                        .&mt('Delete').'</label></span></td>';
1.249     raeburn  6074:                             $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
1.406.2.6  raeburn  6075:                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes,$readonly).'</td>'
1.241     raeburn  6076:                                        .&Apache::loncommon::end_data_table_row();
                   6077:                             $num ++;
                   6078:                         }
                   6079:                     }
                   6080:                 }
1.249     raeburn  6081:                 my $add_domtitle = &mt('Users in additional domain:');
1.241     raeburn  6082:                 if ($curr_types eq '*') { 
1.249     raeburn  6083:                     $add_domtitle = &mt('Users in specific domain:');
1.241     raeburn  6084:                 } elsif ($curr_types eq '') {
1.249     raeburn  6085:                     $add_domtitle = &mt('Users in other domain:');
1.241     raeburn  6086:                 }
                   6087:                 $output .= &Apache::loncommon::start_data_table_row()
                   6088:                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
                   6089:                            .&Apache::loncommon::select_dom_form('','selfenroll_newdom',
1.406.2.6  raeburn  6090:                                                                 $includeempty,$showdomdesc,'','','',$readonly)
1.241     raeburn  6091:                            .'<input type="hidden" name="selfenroll_types_total" value="'.$num.'" />'
                   6092:                            .'</td>'.&Apache::loncommon::end_data_table_row()
                   6093:                            .&Apache::loncommon::end_data_table();
1.237     raeburn  6094:             } elsif ($item eq 'registered') {
                   6095:                 my ($regon,$regoff);
1.398     raeburn  6096:                 my $registered;
                   6097:                 if (ref($currsettings) eq 'HASH') {
                   6098:                     $registered = $currsettings->{'selfenroll_registered'};
                   6099:                 }
1.400     raeburn  6100:                 if ($noedit{$item}) {
                   6101:                     if ($registered) {
                   6102:                         $output .= &mt('Must be registered in course');
                   6103:                     } else {
                   6104:                         $output .= &mt('No requirement');
                   6105:                     }
                   6106:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6107:                     next;
                   6108:                 }
1.398     raeburn  6109:                 if ($registered) {
1.237     raeburn  6110:                     $regon = ' checked="checked" ';
1.406.2.6  raeburn  6111:                     $regoff = '';
1.237     raeburn  6112:                 } else {
1.406.2.6  raeburn  6113:                     $regon = '';
1.237     raeburn  6114:                     $regoff = ' checked="checked" ';
                   6115:                 }
                   6116:                 $output .= '<label>'.
1.406.2.6  raeburn  6117:                            '<input type="radio" name="selfenroll_registered" value="1"'.$regon.$disabled.' />'.
1.244     bisitz   6118:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
1.406.2.6  raeburn  6119:                            '<input type="radio" name="selfenroll_registered" value="0"'.$regoff.$disabled.' />'.
1.244     bisitz   6120:                            &mt('No').'</label>';
1.237     raeburn  6121:             } elsif ($item eq 'enroll_dates') {
1.398     raeburn  6122:                 my ($starttime,$endtime);
                   6123:                 if (ref($currsettings) eq 'HASH') {
                   6124:                     $starttime = $currsettings->{'selfenroll_start_date'};
                   6125:                     $endtime = $currsettings->{'selfenroll_end_date'};
                   6126:                     if ($starttime eq '') {
                   6127:                         $starttime = $currsettings->{'default_enrollment_start_date'};
                   6128:                     }
                   6129:                     if ($endtime eq '') {
                   6130:                         $endtime = $currsettings->{'default_enrollment_end_date'};
                   6131:                     }
1.237     raeburn  6132:                 }
1.400     raeburn  6133:                 if ($noedit{$item}) {
                   6134:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
                   6135:                                                           &Apache::lonlocal::locallocaltime($endtime));
                   6136:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6137:                     next;
                   6138:                 }
1.237     raeburn  6139:                 my $startform =
                   6140:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
1.406.2.6  raeburn  6141:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6142:                 my $endform =
                   6143:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
1.406.2.6  raeburn  6144:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6145:                 $output .= &selfenroll_date_forms($startform,$endform);
                   6146:             } elsif ($item eq 'access_dates') {
1.398     raeburn  6147:                 my ($starttime,$endtime);
                   6148:                 if (ref($currsettings) eq 'HASH') {
                   6149:                     $starttime = $currsettings->{'selfenroll_start_access'};
                   6150:                     $endtime = $currsettings->{'selfenroll_end_access'};
                   6151:                     if ($starttime eq '') {
                   6152:                         $starttime = $currsettings->{'default_enrollment_start_date'};
                   6153:                     }
                   6154:                     if ($endtime eq '') {
                   6155:                         $endtime = $currsettings->{'default_enrollment_end_date'};
                   6156:                     }
1.237     raeburn  6157:                 }
1.400     raeburn  6158:                 if ($noedit{$item}) {
                   6159:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
                   6160:                                                           &Apache::lonlocal::locallocaltime($endtime));
                   6161:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6162:                     next;
                   6163:                 }
1.237     raeburn  6164:                 my $startform =
                   6165:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
1.406.2.6  raeburn  6166:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6167:                 my $endform =
                   6168:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
1.406.2.6  raeburn  6169:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6170:                 $output .= &selfenroll_date_forms($startform,$endform);
                   6171:             } elsif ($item eq 'section') {
1.398     raeburn  6172:                 my $currsec;
                   6173:                 if (ref($currsettings) eq 'HASH') {
                   6174:                     $currsec = $currsettings->{'selfenroll_section'};
                   6175:                 }
1.237     raeburn  6176:                 my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   6177:                 my $newsecval;
                   6178:                 if ($currsec ne 'none' && $currsec ne '') {
                   6179:                     if (!defined($sections_count{$currsec})) {
                   6180:                         $newsecval = $currsec;
                   6181:                     }
                   6182:                 }
1.400     raeburn  6183:                 if ($noedit{$item}) {
                   6184:                     if ($currsec ne '') {
                   6185:                         $output .= $currsec;
                   6186:                     } else {
                   6187:                         $output .= &mt('No specific section');
                   6188:                     }
                   6189:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6190:                     next;
                   6191:                 }
1.237     raeburn  6192:                 my $sections_select = 
1.406.2.6  raeburn  6193:                     &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec,$disabled);
1.237     raeburn  6194:                 $output .= '<table class="LC_createuser">'."\n".
                   6195:                            '<tr class="LC_section_row">'."\n".
                   6196:                            '<td align="center">'.&mt('Existing sections')."\n".
                   6197:                            '<br />'.$sections_select.'</td><td align="center">'.
                   6198:                            &mt('New section').'<br />'."\n".
1.406.2.6  raeburn  6199:                            '<input type="text" name="newsec" size="15" value="'.$newsecval.'"'.$disabled.' />'."\n".
1.237     raeburn  6200:                            '<input type="hidden" name="sections" value="" />'."\n".
                   6201:                            '</td></tr></table>'."\n";
1.276     raeburn  6202:             } elsif ($item eq 'approval') {
1.398     raeburn  6203:                 my ($currnotified,$currapproval,%appchecked);
                   6204:                 my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
1.406.2.6  raeburn  6205:                 if (ref($currsettings) eq 'HASH') {
1.398     raeburn  6206:                     $currnotified = $currsettings->{'selfenroll_notifylist'};
                   6207:                     $currapproval = $currsettings->{'selfenroll_approval'};
                   6208:                 }
                   6209:                 if ($currapproval !~ /^[012]$/) {
                   6210:                     $currapproval = 0;
                   6211:                 }
1.400     raeburn  6212:                 if ($noedit{$item}) {
                   6213:                     $output .=  $selfdescs{'approval'}{$currapproval}.
                   6214:                                 '<br />'.&mt('(Set by Domain Coordinator)');
                   6215:                     next;
                   6216:                 }
1.398     raeburn  6217:                 $appchecked{$currapproval} = ' checked="checked"';
                   6218:                 for my $i (0..2) {
                   6219:                     $output .= '<label>'.
                   6220:                                '<input type="radio" name="selfenroll_approval" value="'.$i.'"'.
1.406.2.6  raeburn  6221:                                $appchecked{$i}.' onclick="toggleNotify();"'.$disabled.' />'.
                   6222:                                $selfdescs{'approval'}{$i}.'</label>'.('&nbsp;'x2);
1.276     raeburn  6223:                 }
                   6224:                 my %advhash = &Apache::lonnet::get_course_adv_roles($cid,1);
                   6225:                 my (@ccs,%notified);
1.322     raeburn  6226:                 my $ccrole = 'cc';
                   6227:                 if ($crstype eq 'Community') {
                   6228:                     $ccrole = 'co';
                   6229:                 }
                   6230:                 if ($advhash{$ccrole}) {
                   6231:                     @ccs = split(/,/,$advhash{$ccrole});
1.276     raeburn  6232:                 }
                   6233:                 if ($currnotified) {
                   6234:                     foreach my $current (split(/,/,$currnotified)) {
                   6235:                         $notified{$current} = 1;
                   6236:                         if (!grep(/^\Q$current\E$/,@ccs)) {
                   6237:                             push(@ccs,$current);
                   6238:                         }
                   6239:                     }
                   6240:                 }
                   6241:                 if (@ccs) {
1.398     raeburn  6242:                     my $style;
                   6243:                     unless ($currapproval) {
                   6244:                         $style = ' style="display: none;"'; 
                   6245:                     }
                   6246:                     $output .= '<br /><div id="notified"'.$style.'>'.
                   6247:                                &mt('Personnel to be notified when an enrollment request needs approval, or has been approved:').'&nbsp;'.
                   6248:                                &Apache::loncommon::start_data_table().
1.276     raeburn  6249:                                &Apache::loncommon::start_data_table_row();
                   6250:                     my $count = 0;
                   6251:                     my $numcols = 4;
                   6252:                     foreach my $cc (sort(@ccs)) {
                   6253:                         my $notifyon;
                   6254:                         my ($ccuname,$ccudom) = split(/:/,$cc);
                   6255:                         if ($notified{$cc}) {
                   6256:                             $notifyon = ' checked="checked" ';
                   6257:                         }
                   6258:                         if ($count && !$count%$numcols) {
                   6259:                             $output .= &Apache::loncommon::end_data_table_row().
                   6260:                                        &Apache::loncommon::start_data_table_row()
                   6261:                         }
                   6262:                         $output .= '<td><span class="LC_nobreak"><label>'.
1.406.2.6  raeburn  6263:                                    '<input type="checkbox" name="selfenroll_notify"'.$notifyon.' value="'.$cc.'"'.$disabled.' />'.
1.276     raeburn  6264:                                    &Apache::loncommon::plainname($ccuname,$ccudom).
                   6265:                                    '</label></span></td>';
1.343     raeburn  6266:                         $count ++;
1.276     raeburn  6267:                     }
                   6268:                     my $rem = $count%$numcols;
                   6269:                     if ($rem) {
                   6270:                         my $emptycols = $numcols - $rem;
                   6271:                         for (my $i=0; $i<$emptycols; $i++) { 
                   6272:                             $output .= '<td>&nbsp;</td>';
                   6273:                         }
                   6274:                     }
                   6275:                     $output .= &Apache::loncommon::end_data_table_row().
1.398     raeburn  6276:                                &Apache::loncommon::end_data_table().
                   6277:                                '</div>';
1.276     raeburn  6278:                 }
                   6279:             } elsif ($item eq 'limit') {
1.398     raeburn  6280:                 my ($crslimit,$selflimit,$nolimit,$currlim,$currcap);
                   6281:                 if (ref($currsettings) eq 'HASH') {
                   6282:                     $currlim = $currsettings->{'selfenroll_limit'};
                   6283:                     $currcap = $currsettings->{'selfenroll_cap'};
                   6284:                 }
1.400     raeburn  6285:                 if ($noedit{$item}) {
                   6286:                     if (($currlim eq 'allstudents') || ($currlim eq 'selfenrolled')) {
                   6287:                         if ($currlim eq 'allstudents') {
                   6288:                             $output .= &mt('Limit by total students');
                   6289:                         } elsif ($currlim eq 'selfenrolled') {
                   6290:                             $output .= &mt('Limit by total self-enrolled students');
                   6291:                         }
                   6292:                         $output .= ' '.&mt('Maximum: [_1]',$currcap).
                   6293:                                    '<br />'.&mt('(Set by Domain Coordinator)');
                   6294:                     } else {
                   6295:                         $output .= &mt('No limit').'<br />'.&mt('(Set by Domain Coordinator)');
                   6296:                     }
                   6297:                     next;
                   6298:                 }
1.276     raeburn  6299:                 if ($currlim eq 'allstudents') {
                   6300:                     $crslimit = ' checked="checked" ';
                   6301:                     $selflimit = ' ';
                   6302:                     $nolimit = ' ';
                   6303:                 } elsif ($currlim eq 'selfenrolled') {
                   6304:                     $crslimit = ' ';
                   6305:                     $selflimit = ' checked="checked" ';
                   6306:                     $nolimit = ' '; 
                   6307:                 } else {
                   6308:                     $crslimit = ' ';
                   6309:                     $selflimit = ' ';
1.398     raeburn  6310:                     $nolimit = ' checked="checked" ';
1.276     raeburn  6311:                 }
                   6312:                 $output .= '<table><tr><td><label>'.
1.406.2.6  raeburn  6313:                            '<input type="radio" name="selfenroll_limit" value="none"'.$nolimit.$disabled.'/>'.
1.276     raeburn  6314:                            &mt('No limit').'</label></td><td><label>'.
1.406.2.6  raeburn  6315:                            '<input type="radio" name="selfenroll_limit" value="allstudents"'.$crslimit.$disabled.'/>'.
1.276     raeburn  6316:                            &mt('Limit by total students').'</label></td><td><label>'.
1.406.2.6  raeburn  6317:                            '<input type="radio" name="selfenroll_limit" value="selfenrolled"'.$selflimit.$disabled.'/>'.
1.276     raeburn  6318:                            &mt('Limit by total self-enrolled students').
                   6319:                            '</td></tr><tr>'.
                   6320:                            '<td>&nbsp;</td><td colspan="2"><span class="LC_nobreak">'.
                   6321:                            ('&nbsp;'x3).&mt('Maximum number allowed: ').
1.406.2.6  raeburn  6322:                            '<input type="text" name="selfenroll_cap" size = "5" value="'.$currcap.'"'.$disabled.' /></td></tr></table>';
1.237     raeburn  6323:             }
                   6324:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   6325:         }
                   6326:     }
1.406.2.6  raeburn  6327:     $output .= &Apache::lonhtmlcommon::end_pick_box().'<br />';
                   6328:     unless ($readonly) {
                   6329:         $output .= '<input type="button" name="selfenrollconf" value="'
                   6330:                    .&mt('Save').'" onclick="validate_types(this.form);" />';
                   6331:     }
                   6332:     $output .= '<input type="hidden" name="action" value="selfenroll" />'
                   6333:                .'<input type="hidden" name="state" value="done" />'."\n"
                   6334:                .$additional.'</form>';
1.237     raeburn  6335:     $r->print($output);
                   6336:     return;
                   6337: }
                   6338: 
1.400     raeburn  6339: sub get_noedit_fields {
                   6340:     my ($cdom,$cnum,$crstype,$row) = @_;
                   6341:     my %noedit;
                   6342:     if (ref($row) eq 'ARRAY') {
                   6343:         my %settings = &Apache::lonnet::get('environment',['internal.coursecode','internal.textbook',
                   6344:                                                            'internal.selfenrollmgrdc',
                   6345:                                                            'internal.selfenrollmgrcc'],$cdom,$cnum);
                   6346:         my $type = &Apache::lonuserutils::get_extended_type($cdom,$cnum,$crstype,\%settings);
                   6347:         my (%specific_managebydc,%specific_managebycc,%default_managebydc);
                   6348:         map { $specific_managebydc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrdc'}));
                   6349:         map { $specific_managebycc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrcc'}));
                   6350:         my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
                   6351:         map { $default_managebydc{$_} = 1; } (split(/,/,$domdefaults{$type.'selfenrolladmdc'}));
                   6352: 
                   6353:         foreach my $item (@{$row}) {
                   6354:             next if ($specific_managebycc{$item});
                   6355:             if (($specific_managebydc{$item}) || ($default_managebydc{$item})) {
                   6356:                 $noedit{$item} = 1;
                   6357:             }
                   6358:         }
                   6359:     }
                   6360:     return %noedit;
                   6361: } 
                   6362: 
                   6363: sub visible_in_stdcat {
                   6364:     my ($cdom,$cnum,$domconf) = @_;
                   6365:     my ($cathash,%settable,@vismsgs,$cansetvis,$visible);
                   6366:     unless (ref($domconf) eq 'HASH') {
                   6367:         return ($visible,$cansetvis,\@vismsgs);
                   6368:     }
                   6369:     if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6370:         if ($domconf->{'coursecategories'}{'togglecats'} eq 'crs') {
1.256     raeburn  6371:             $settable{'togglecats'} = 1;
                   6372:         }
1.400     raeburn  6373:         if ($domconf->{'coursecategories'}{'categorize'} eq 'crs') {
1.256     raeburn  6374:             $settable{'categorize'} = 1;
                   6375:         }
1.400     raeburn  6376:         $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6377:     }
1.260     raeburn  6378:     if ($settable{'togglecats'} && $settable{'categorize'}) {
1.256     raeburn  6379:         $cansetvis = &mt('You are able to both assign a course category and choose to exclude this course from the catalog.');   
                   6380:     } elsif ($settable{'togglecats'}) {
                   6381:         $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  6382:     } elsif ($settable{'categorize'}) {
1.256     raeburn  6383:         $cansetvis = &mt('You may assign a course category, but only a Domain Coordinator may choose to exclude this course from the catalog.');  
                   6384:     } else {
                   6385:         $cansetvis = &mt('Only a Domain Coordinator may assign a course category or choose to exclude this course from the catalog.'); 
                   6386:     }
                   6387:      
                   6388:     my %currsettings =
                   6389:         &Apache::lonnet::get('environment',['hidefromcat','categories','internal.coursecode'],
                   6390:                              $cdom,$cnum);
1.400     raeburn  6391:     $visible = 0;
1.256     raeburn  6392:     if ($currsettings{'internal.coursecode'} ne '') {
1.400     raeburn  6393:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6394:             $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6395:             if (ref($cathash) eq 'HASH') {
                   6396:                 if ($cathash->{'instcode::0'} eq '') {
                   6397:                     push(@vismsgs,'dc_addinst'); 
                   6398:                 } else {
                   6399:                     $visible = 1;
                   6400:                 }
                   6401:             } else {
                   6402:                 $visible = 1;
                   6403:             }
                   6404:         } else {
                   6405:             $visible = 1;
                   6406:         }
                   6407:     } else {
                   6408:         if (ref($cathash) eq 'HASH') {
                   6409:             if ($cathash->{'instcode::0'} ne '') {
                   6410:                 push(@vismsgs,'dc_instcode');
                   6411:             }
                   6412:         } else {
                   6413:             push(@vismsgs,'dc_instcode');
                   6414:         }
                   6415:     }
                   6416:     if ($currsettings{'categories'} ne '') {
                   6417:         my $cathash;
1.400     raeburn  6418:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6419:             $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6420:             if (ref($cathash) eq 'HASH') {
                   6421:                 if (keys(%{$cathash}) == 0) {
                   6422:                     push(@vismsgs,'dc_catalog');
                   6423:                 } elsif ((keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} ne '')) {
                   6424:                     push(@vismsgs,'dc_categories');
                   6425:                 } else {
                   6426:                     my @currcategories = split('&',$currsettings{'categories'});
                   6427:                     my $matched = 0;
                   6428:                     foreach my $cat (@currcategories) {
                   6429:                         if ($cathash->{$cat} ne '') {
                   6430:                             $visible = 1;
                   6431:                             $matched = 1;
                   6432:                             last;
                   6433:                         }
                   6434:                     }
                   6435:                     if (!$matched) {
1.260     raeburn  6436:                         if ($settable{'categorize'}) { 
1.256     raeburn  6437:                             push(@vismsgs,'chgcat');
                   6438:                         } else {
                   6439:                             push(@vismsgs,'dc_chgcat');
                   6440:                         }
                   6441:                     }
                   6442:                 }
                   6443:             }
                   6444:         }
                   6445:     } else {
                   6446:         if (ref($cathash) eq 'HASH') {
                   6447:             if ((keys(%{$cathash}) > 1) || 
                   6448:                 (keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} eq '')) {
1.260     raeburn  6449:                 if ($settable{'categorize'}) {
1.256     raeburn  6450:                     push(@vismsgs,'addcat');
                   6451:                 } else {
                   6452:                     push(@vismsgs,'dc_addcat');
                   6453:                 }
                   6454:             }
                   6455:         }
                   6456:     }
                   6457:     if ($currsettings{'hidefromcat'} eq 'yes') {
                   6458:         $visible = 0;
                   6459:         if ($settable{'togglecats'}) {
                   6460:             unshift(@vismsgs,'unhide');
                   6461:         } else {
                   6462:             unshift(@vismsgs,'dc_unhide')
                   6463:         }
                   6464:     }
1.400     raeburn  6465:     return ($visible,$cansetvis,\@vismsgs);
                   6466: }
                   6467: 
                   6468: sub cat_visibility {
                   6469:     my %visactions = &Apache::lonlocal::texthash(
                   6470:                    vis => 'This course/community currently appears in the Course/Community Catalog for this domain.',
                   6471:                    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.',
                   6472:                    miss => 'This course/community does not currently appear in the Course/Community Catalog for this domain.',
                   6473:                    none => 'Display of a course catalog is disabled for this domain.',
                   6474:                    yous => 'You should remedy this if you plan to allow self-enrollment, otherwise students will have difficulty finding this course.',
                   6475:                    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.',
                   6476:                    make => 'Make any changes to self-enrollment settings below, click "Save", then take action to include the course in the Catalog:',
                   6477:                    take => 'Take the following action to ensure the course appears in the Catalog:',
                   6478:                    dc_chgconf => 'Ask a domain coordinator to change the Catalog type for this domain.',
                   6479:                    dc_setcode => 'Ask a domain coordinator to assign a six character code to the course',
                   6480:                    dc_unhide  => 'Ask a domain coordinator to change the "Exclude from course catalog" setting.',
                   6481:                    dc_addinst => 'Ask a domain coordinator to enable display the catalog of "Official courses (with institutional codes)".',
                   6482:                    dc_instcode => 'Ask a domain coordinator to assign an institutional code (if this is an official course).',
                   6483:                    dc_catalog  => 'Ask a domain coordinator to enable or create at least one course category in the domain.',
                   6484:                    dc_categories => 'Ask a domain coordinator to create a hierarchy of categories and sub categories for courses in the domain.',
                   6485:                    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',
                   6486:                    dc_addcat => 'Ask a domain coordinator to assign a category to the course.',
                   6487:     );
                   6488:     $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>"');
                   6489:     $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>"');
                   6490:     $visactions{'addcat'} = &mt('Use [_1]Categorize course[_2] to assign a category to the course.','"<a href="/adm/courseprefs?phase=display&actions=courseinfo">','</a>"');
                   6491:     return \%visactions;
1.256     raeburn  6492: }
                   6493: 
1.241     raeburn  6494: sub new_selfenroll_dom_row {
                   6495:     my ($newdom,$num) = @_;
                   6496:     my $domdesc = &Apache::lonnet::domain($newdom);
                   6497:     my $output;
                   6498:     if ($domdesc ne '') {
                   6499:         $output .= &Apache::loncommon::start_data_table_row()
                   6500:                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
                   6501:                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
1.249     raeburn  6502:                    .'" value="'.$newdom.'" /></span><br />'
                   6503:                    .'<span class="LC_nobreak"><label><input type="checkbox" '
                   6504:                    .'name="selfenroll_activate" value="'.$num.'" '
                   6505:                    .'onchange="javascript:update_types('
                   6506:                    ."'selfenroll_activate','$num'".');" />'
                   6507:                    .&mt('Activate').'</label></span></td>';
1.241     raeburn  6508:         my @currinsttypes;
                   6509:         $output .= '<td>'.&mt('User types:').'<br />'
                   6510:                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
                   6511:                    .&Apache::loncommon::end_data_table_row();
                   6512:     }
                   6513:     return $output;
                   6514: }
                   6515: 
                   6516: sub selfenroll_inst_types {
1.406.2.6  raeburn  6517:     my ($num,$currdom,$currinsttypes,$readonly) = @_;
1.241     raeburn  6518:     my $output;
                   6519:     my $numinrow = 4;
                   6520:     my $count = 0;
                   6521:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($currdom);
1.247     raeburn  6522:     my $othervalue = 'any';
1.406.2.6  raeburn  6523:     my $disabled;
                   6524:     if ($readonly) {
                   6525:         $disabled = ' disabled="disabled"';
                   6526:     }
1.241     raeburn  6527:     if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
1.251     raeburn  6528:         if (keys(%{$usertypes}) > 0) {
1.247     raeburn  6529:             $othervalue = 'other';
                   6530:         }
1.241     raeburn  6531:         $output .= '<table><tr>';
                   6532:         foreach my $type (@{$types}) {
                   6533:             if (($count > 0) && ($count%$numinrow == 0)) {
                   6534:                 $output .= '</tr><tr>';
                   6535:             }
                   6536:             if (defined($usertypes->{$type})) {
1.257     raeburn  6537:                 my $esc_type = &escape($type);
1.241     raeburn  6538:                 $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.
1.257     raeburn  6539:                            $esc_type.'" ';
1.241     raeburn  6540:                 if (ref($currinsttypes) eq 'ARRAY') {
                   6541:                     if (@{$currinsttypes} > 0) {
1.249     raeburn  6542:                         if (grep(/^any$/,@{$currinsttypes})) {
                   6543:                             $output .= 'checked="checked"';
1.257     raeburn  6544:                         } elsif (grep(/^\Q$esc_type\E$/,@{$currinsttypes})) {
1.241     raeburn  6545:                             $output .= 'checked="checked"';
                   6546:                         }
1.249     raeburn  6547:                     } else {
                   6548:                         $output .= 'checked="checked"';
1.241     raeburn  6549:                     }
                   6550:                 }
1.406.2.6  raeburn  6551:                 $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$usertypes->{$type}.'</label></span></td>';
1.241     raeburn  6552:             }
                   6553:             $count ++;
                   6554:         }
                   6555:         if (($count > 0) && ($count%$numinrow == 0)) {
                   6556:             $output .= '</tr><tr>';
                   6557:         }
1.249     raeburn  6558:         $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
1.241     raeburn  6559:         if (ref($currinsttypes) eq 'ARRAY') {
                   6560:             if (@{$currinsttypes} > 0) {
1.249     raeburn  6561:                 if (grep(/^any$/,@{$currinsttypes})) { 
                   6562:                     $output .= ' checked="checked"';
                   6563:                 } elsif ($othervalue eq 'other') {
                   6564:                     if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
                   6565:                         $output .= ' checked="checked"';
                   6566:                     }
1.241     raeburn  6567:                 }
1.249     raeburn  6568:             } else {
                   6569:                 $output .= ' checked="checked"';
1.241     raeburn  6570:             }
1.249     raeburn  6571:         } else {
                   6572:             $output .= ' checked="checked"';
1.241     raeburn  6573:         }
1.406.2.6  raeburn  6574:         $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$othertitle.'</label></span></td></tr></table>';
1.241     raeburn  6575:     }
                   6576:     return $output;
                   6577: }
                   6578: 
1.237     raeburn  6579: sub selfenroll_date_forms {
                   6580:     my ($startform,$endform) = @_;
                   6581:     my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
1.244     bisitz   6582:                   &Apache::lonhtmlcommon::row_title(&mt('Start date'),
1.237     raeburn  6583:                                                     'LC_oddrow_value')."\n".
                   6584:                   $startform."\n".
                   6585:                   &Apache::lonhtmlcommon::row_closure(1).
1.244     bisitz   6586:                   &Apache::lonhtmlcommon::row_title(&mt('End date'),
1.237     raeburn  6587:                                                    'LC_oddrow_value')."\n".
                   6588:                   $endform."\n".
                   6589:                   &Apache::lonhtmlcommon::row_closure(1).
                   6590:                   &Apache::lonhtmlcommon::end_pick_box();
                   6591:     return $output;
                   6592: }
                   6593: 
1.239     raeburn  6594: sub print_userchangelogs_display {
1.406.2.5  raeburn  6595:     my ($r,$context,$permission,$brcrum) = @_;
1.363     raeburn  6596:     my $formname = 'rolelog';
1.406.2.6  raeburn  6597:     my ($username,$domain,$crstype,$viewablesec,%roleslog);
1.363     raeburn  6598:     if ($context eq 'domain') {
                   6599:         $domain = $env{'request.role.domain'};
                   6600:         %roleslog=&Apache::lonnet::dump_dom('nohist_rolelog',$domain);
                   6601:     } else {
                   6602:         if ($context eq 'course') { 
                   6603:             $domain = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   6604:             $username = $env{'course.'.$env{'request.course.id'}.'.num'};
                   6605:             $crstype = &Apache::loncommon::course_type();
1.406.2.6  raeburn  6606:             $viewablesec = &Apache::lonuserutils::viewable_section($permission);
1.363     raeburn  6607:             my %saveable_parameters = ('show' => 'scalar',);
                   6608:             &Apache::loncommon::store_course_settings('roles_log',
                   6609:                                                       \%saveable_parameters);
                   6610:             &Apache::loncommon::restore_course_settings('roles_log',
                   6611:                                                         \%saveable_parameters);
                   6612:         } elsif ($context eq 'author') {
                   6613:             $domain = $env{'user.domain'}; 
                   6614:             if ($env{'request.role'} =~ m{^au\./\Q$domain\E/$}) {
                   6615:                 $username = $env{'user.name'};
                   6616:             } else {
                   6617:                 undef($domain);
                   6618:             }
                   6619:         }
                   6620:         if ($domain ne '' && $username ne '') { 
                   6621:             %roleslog=&Apache::lonnet::dump('nohist_rolelog',$domain,$username);
                   6622:         }
                   6623:     }
1.239     raeburn  6624:     if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
                   6625: 
1.406.2.5  raeburn  6626:     my $helpitem;
                   6627:     if ($context eq 'course') {
                   6628:         $helpitem = 'Course_User_Logs';
                   6629:     }
                   6630:     push (@{$brcrum},
                   6631:              {href => '/adm/createuser?action=changelogs',
                   6632:               text => 'User Management Logs',
                   6633:               help => $helpitem});
                   6634:     my $bread_crumbs_component = 'User Changes';
                   6635:     my $args = { bread_crumbs           => $brcrum,
                   6636:                  bread_crumbs_component => $bread_crumbs_component};
                   6637: 
                   6638:     # Create navigation javascript
                   6639:     my $jsnav = &userlogdisplay_js($formname);
                   6640: 
                   6641:     my $jscript = (<<ENDSCRIPT);
                   6642: <script type="text/javascript">
                   6643: // <![CDATA[
                   6644: $jsnav
                   6645: // ]]>
                   6646: </script>
                   6647: ENDSCRIPT
                   6648: 
                   6649:     # print page header
                   6650:     $r->print(&header($jscript,$args));
                   6651: 
1.239     raeburn  6652:     # set defaults
                   6653:     my $now = time();
                   6654:     my $defstart = $now - (7*24*3600); #7 days ago 
                   6655:     my %defaults = (
                   6656:                      page               => '1',
                   6657:                      show               => '10',
                   6658:                      role               => 'any',
                   6659:                      chgcontext         => 'any',
                   6660:                      rolelog_start_date => $defstart,
                   6661:                      rolelog_end_date   => $now,
                   6662:                    );
                   6663:     my $more_records = 0;
                   6664: 
                   6665:     # set current
                   6666:     my %curr;
                   6667:     foreach my $item ('show','page','role','chgcontext') {
                   6668:         $curr{$item} = $env{'form.'.$item};
                   6669:     }
                   6670:     my ($startdate,$enddate) = 
                   6671:         &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
                   6672:     $curr{'rolelog_start_date'} = $startdate;
                   6673:     $curr{'rolelog_end_date'} = $enddate;
                   6674:     foreach my $key (keys(%defaults)) {
                   6675:         if ($curr{$key} eq '') {
                   6676:             $curr{$key} = $defaults{$key};
                   6677:         }
                   6678:     }
1.248     raeburn  6679:     my (%whodunit,%changed,$version);
                   6680:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
1.239     raeburn  6681:     my ($minshown,$maxshown);
1.255     raeburn  6682:     $minshown = 1;
1.239     raeburn  6683:     my $count = 0;
1.406.2.5  raeburn  6684:     if ($curr{'show'} =~ /\D/) {
                   6685:         $curr{'page'} = 1;
                   6686:     } else {
1.239     raeburn  6687:         $maxshown = $curr{'page'} * $curr{'show'};
                   6688:         if ($curr{'page'} > 1) {
                   6689:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   6690:         }
                   6691:     }
1.301     bisitz   6692: 
1.327     raeburn  6693:     # Form Header
                   6694:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
1.363     raeburn  6695:               &role_display_filter($context,$formname,$domain,$username,\%curr,
                   6696:                                    $version,$crstype));
1.327     raeburn  6697: 
                   6698:     my $showntableheader = 0;
                   6699: 
                   6700:     # Table Header
                   6701:     my $tableheader = 
                   6702:         &Apache::loncommon::start_data_table_header_row()
                   6703:        .'<th>&nbsp;</th>'
                   6704:        .'<th>'.&mt('When').'</th>'
                   6705:        .'<th>'.&mt('Who made the change').'</th>'
                   6706:        .'<th>'.&mt('Changed User').'</th>'
1.363     raeburn  6707:        .'<th>'.&mt('Role').'</th>';
                   6708: 
                   6709:     if ($context eq 'course') {
                   6710:         $tableheader .= '<th>'.&mt('Section').'</th>';
                   6711:     }
                   6712:     $tableheader .=
                   6713:         '<th>'.&mt('Context').'</th>'
1.327     raeburn  6714:        .'<th>'.&mt('Start').'</th>'
                   6715:        .'<th>'.&mt('End').'</th>'
                   6716:        .&Apache::loncommon::end_data_table_header_row();
                   6717: 
                   6718:     # Display user change log data
1.239     raeburn  6719:     foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
                   6720:         next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
                   6721:                  ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
1.406.2.5  raeburn  6722:         if ($curr{'show'} !~ /\D/) {
1.239     raeburn  6723:             if ($count >= $curr{'page'} * $curr{'show'}) {
                   6724:                 $more_records = 1;
                   6725:                 last;
                   6726:             }
                   6727:         }
                   6728:         if ($curr{'role'} ne 'any') {
                   6729:             next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
                   6730:         }
                   6731:         if ($curr{'chgcontext'} ne 'any') {
                   6732:             if ($curr{'chgcontext'} eq 'selfenroll') {
                   6733:                 next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
                   6734:             } else {
                   6735:                 next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
                   6736:             }
                   6737:         }
1.406.2.6  raeburn  6738:         if (($context eq 'course') && ($viewablesec ne '')) {
                   6739:             next if ($roleslog{$id}{'logentry'}{'section'} ne $viewablesec);
                   6740:         }
1.239     raeburn  6741:         $count ++;
                   6742:         next if ($count < $minshown);
1.327     raeburn  6743:         unless ($showntableheader) {
1.406.2.5  raeburn  6744:             $r->print(&Apache::loncommon::start_data_table()
1.327     raeburn  6745:                      .$tableheader);
                   6746:             $r->rflush();
                   6747:             $showntableheader = 1;
                   6748:         }
1.239     raeburn  6749:         if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
                   6750:             $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
                   6751:                 &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
                   6752:         }
                   6753:         if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
                   6754:             $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
                   6755:                 &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
                   6756:         }
                   6757:         my $sec = $roleslog{$id}{'logentry'}{'section'};
                   6758:         if ($sec eq '') {
                   6759:             $sec = &mt('None');
                   6760:         }
                   6761:         my ($rolestart,$roleend);
                   6762:         if ($roleslog{$id}{'delflag'}) {
                   6763:             $rolestart = &mt('deleted');
                   6764:             $roleend = &mt('deleted');
                   6765:         } else {
                   6766:             $rolestart = $roleslog{$id}{'logentry'}{'start'};
                   6767:             $roleend = $roleslog{$id}{'logentry'}{'end'};
                   6768:             if ($rolestart eq '' || $rolestart == 0) {
                   6769:                 $rolestart = &mt('No start date'); 
                   6770:             } else {
                   6771:                 $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
                   6772:             }
                   6773:             if ($roleend eq '' || $roleend == 0) { 
                   6774:                 $roleend = &mt('No end date');
                   6775:             } else {
                   6776:                 $roleend = &Apache::lonlocal::locallocaltime($roleend);
                   6777:             }
                   6778:         }
                   6779:         my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
                   6780:         if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
                   6781:             $chgcontext = 'selfenroll';
                   6782:         }
1.363     raeburn  6783:         my %lt = &rolechg_contexts($context,$crstype);
1.239     raeburn  6784:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
                   6785:             $chgcontext = $lt{$chgcontext};
                   6786:         }
1.327     raeburn  6787:         $r->print(
1.301     bisitz   6788:             &Apache::loncommon::start_data_table_row()
                   6789:            .'<td>'.$count.'</td>'
                   6790:            .'<td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td>'
                   6791:            .'<td>'.$whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.'</td>'
                   6792:            .'<td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td>'
1.363     raeburn  6793:            .'<td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'},$crstype).'</td>');
                   6794:         if ($context eq 'course') { 
                   6795:             $r->print('<td>'.$sec.'</td>');
                   6796:         }
                   6797:         $r->print(
                   6798:             '<td>'.$chgcontext.'</td>'
1.301     bisitz   6799:            .'<td>'.$rolestart.'</td>'
                   6800:            .'<td>'.$roleend.'</td>'
1.327     raeburn  6801:            .&Apache::loncommon::end_data_table_row()."\n");
1.301     bisitz   6802:     }
                   6803: 
1.327     raeburn  6804:     if ($showntableheader) { # Table footer, if content displayed above
1.406.2.5  raeburn  6805:         $r->print(&Apache::loncommon::end_data_table().
                   6806:                   &userlogdisplay_navlinks(\%curr,$more_records));
1.327     raeburn  6807:     } else { # No content displayed above
1.301     bisitz   6808:         $r->print('<p class="LC_info">'
                   6809:                  .&mt('There are no records to display.')
                   6810:                  .'</p>'
                   6811:         );
1.239     raeburn  6812:     }
1.301     bisitz   6813: 
1.327     raeburn  6814:     # Form Footer
                   6815:     $r->print( 
                   6816:         '<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   6817:        .'<input type="hidden" name="action" value="changelogs" />'
                   6818:        .'</form>');
                   6819:     return;
                   6820: }
1.301     bisitz   6821: 
1.406.2.5  raeburn  6822: sub print_useraccesslogs_display {
                   6823:     my ($r,$uname,$udom,$permission,$brcrum) = @_;
                   6824:     my $formname = 'accesslog';
                   6825:     my $form = 'document.accesslog';
                   6826: 
                   6827: # set breadcrumbs
1.406.2.7  raeburn  6828:     my %breadcrumb_text = &singleuser_breadcrumb('','domain',$udom);
1.406.2.5  raeburn  6829:     push (@{$brcrum},
                   6830:         {href => "javascript:backPage($form)",
                   6831:          text => $breadcrumb_text{'search'}});
                   6832:     my (@prevphases,$prevphasestr);
                   6833:     if ($env{'form.prevphases'}) {
                   6834:         @prevphases = split(/,/,$env{'form.prevphases'});
                   6835:         $prevphasestr = $env{'form.prevphases'};
                   6836:     }
                   6837:     if (($env{'form.phase'} eq 'userpicked') || (grep(/^userpicked$/,@prevphases))) {
                   6838:         push(@{$brcrum},
                   6839:               {href => "javascript:backPage($form,'get_user_info','select')",
                   6840:                text => $breadcrumb_text{'userpicked'}});
                   6841:         if ($env{'form.phase'} eq 'userpicked') {
                   6842:             $prevphasestr = 'userpicked';
                   6843:         }
                   6844:     }
                   6845:     push(@{$brcrum},
                   6846:              {href => '/adm/createuser?action=accesslogs',
                   6847:               text => 'User access logs',
1.406.2.8! raeburn  6848:               help => 'Domain_User_Access_Logs'});
1.406.2.5  raeburn  6849:     my $bread_crumbs_component = 'User Access Logs';
                   6850:     my $args = { bread_crumbs           => $brcrum,
                   6851:                  bread_crumbs_component => 'User Management'};
1.406.2.8! raeburn  6852:     if ($env{'form.popup'}) {
        !          6853:         $args->{'no_nav_bar'} = 1;
        !          6854:     }
1.406.2.5  raeburn  6855: 
                   6856: # set javascript
                   6857:     my ($jsback,$elements) = &crumb_utilities();
                   6858:     my $jsnav = &userlogdisplay_js($formname);
                   6859: 
                   6860:     my $jscript = (<<ENDSCRIPT);
1.239     raeburn  6861: <script type="text/javascript">
1.301     bisitz   6862: // <![CDATA[
1.406.2.5  raeburn  6863: 
                   6864: $jsback
                   6865: $jsnav
                   6866: 
                   6867: // ]]>
                   6868: </script>
                   6869: 
                   6870: ENDSCRIPT
                   6871: 
                   6872: # print page header
                   6873:     $r->print(&header($jscript,$args));
                   6874: 
                   6875: # early out unless log data can be displayed.
                   6876:     unless ($permission->{'activity'}) {
                   6877:         $r->print('<p class="LC_warning">'
                   6878:                  .&mt('You do not have rights to display user access logs.')
                   6879:                  .'</p>'
                   6880:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   6881:         return;
                   6882:     }
                   6883: 
                   6884:     unless ($udom eq $env{'request.role.domain'}) {
                   6885:         $r->print('<p class="LC_warning">'
                   6886:                  .&mt("User's domain must match role's domain")
                   6887:                  .'</p>'
                   6888:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   6889:         return;
                   6890:     }
                   6891: 
                   6892:     if (($uname eq '') || ($udom eq '')) {
                   6893:         $r->print('<p class="LC_warning">'
                   6894:                  .&mt('Invalid username or domain')
                   6895:                  .'</p>'
                   6896:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   6897:         return;
                   6898:     }
                   6899: 
                   6900: # set defaults
                   6901:     my $now = time();
                   6902:     my $defstart = $now - (7*24*3600);
                   6903:     my %defaults = (
                   6904:                      page                 => '1',
                   6905:                      show                 => '10',
                   6906:                      activity             => 'any',
                   6907:                      accesslog_start_date => $defstart,
                   6908:                      accesslog_end_date   => $now,
                   6909:                    );
                   6910:     my $more_records = 0;
                   6911: 
                   6912: # set current
                   6913:     my %curr;
                   6914:     foreach my $item ('show','page','activity') {
                   6915:         $curr{$item} = $env{'form.'.$item};
                   6916:     }
                   6917:     my ($startdate,$enddate) =
                   6918:         &Apache::lonuserutils::get_dates_from_form('accesslog_start_date','accesslog_end_date');
                   6919:     $curr{'accesslog_start_date'} = $startdate;
                   6920:     $curr{'accesslog_end_date'} = $enddate;
                   6921:     foreach my $key (keys(%defaults)) {
                   6922:         if ($curr{$key} eq '') {
                   6923:             $curr{$key} = $defaults{$key};
                   6924:         }
                   6925:     }
                   6926:     my ($minshown,$maxshown);
                   6927:     $minshown = 1;
                   6928:     my $count = 0;
                   6929:     if ($curr{'show'} =~ /\D/) {
                   6930:         $curr{'page'} = 1;
                   6931:     } else {
                   6932:         $maxshown = $curr{'page'} * $curr{'show'};
                   6933:         if ($curr{'page'} > 1) {
                   6934:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   6935:         }
                   6936:     }
                   6937: 
                   6938: # form header
                   6939:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
                   6940:               &activity_display_filter($formname,\%curr));
                   6941: 
                   6942:     my $showntableheader = 0;
                   6943:     my ($nav_script,$nav_links);
                   6944: 
                   6945: # table header
                   6946:     my $tableheader =
                   6947:         &Apache::loncommon::start_data_table_header_row()
                   6948:        .'<th>&nbsp;</th>'
                   6949:        .'<th>'.&mt('When').'</th>'
                   6950:        .'<th>'.&mt('HostID').'</th>'
                   6951:        .'<th>'.&mt('Event').'</th>'
                   6952:        .'<th>'.&mt('Other data').'</th>'
                   6953:        .&Apache::loncommon::end_data_table_header_row();
                   6954: 
                   6955:     my %filters=(
                   6956:         start  => $curr{'accesslog_start_date'},
                   6957:         end    => $curr{'accesslog_end_date'},
                   6958:         action => $curr{'activity'},
                   6959:     );
                   6960: 
                   6961:     my $reply = &Apache::lonnet::userlog_query($uname,$udom,%filters);
                   6962:     unless ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   6963:         my (%courses,%missing);
                   6964:         my @results = split(/\&/,$reply);
                   6965:         foreach my $item (reverse(@results)) {
                   6966:             my ($timestamp,$host,$event) = split(/:/,$item);
                   6967:             next unless ($event =~ /^(Log|Role)/);
                   6968:             if ($curr{'show'} !~ /\D/) {
                   6969:                 if ($count >= $curr{'page'} * $curr{'show'}) {
                   6970:                     $more_records = 1;
                   6971:                     last;
                   6972:                 }
                   6973:             }
                   6974:             $count ++;
                   6975:             next if ($count < $minshown);
                   6976:             unless ($showntableheader) {
                   6977:                 $r->print($nav_script
                   6978:                          .&Apache::loncommon::start_data_table()
                   6979:                          .$tableheader);
                   6980:                 $r->rflush();
                   6981:                 $showntableheader = 1;
                   6982:             }
1.406.2.6  raeburn  6983:             my ($shown,$extra);
1.406.2.5  raeburn  6984:             my ($event,$data) = split(/\s+/,&unescape($event));
                   6985:             if ($event eq 'Role') {
                   6986:                 my ($rolecode,$extent) = split(/\./,$data,2);
                   6987:                 next if ($extent eq '');
                   6988:                 my ($crstype,$desc,$info);
1.406.2.6  raeburn  6989:                 if ($extent =~ m{^/($match_domain)/($match_courseid)(?:/(\w+)|)$}) {
                   6990:                     my ($cdom,$cnum,$sec) = ($1,$2,$3);
1.406.2.5  raeburn  6991:                     my $cid = $cdom.'_'.$cnum;
                   6992:                     if (exists($courses{$cid})) {
                   6993:                         $crstype = $courses{$cid}{'type'};
                   6994:                         $desc = $courses{$cid}{'description'};
                   6995:                     } elsif ($missing{$cid}) {
                   6996:                         $crstype = 'Course';
                   6997:                         $desc = 'Course/Community';
                   6998:                     } else {
                   6999:                         my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   7000:                         if (ref($crsinfo{$cdom.'_'.$cnum}) eq 'HASH') {
                   7001:                             $courses{$cid} = $crsinfo{$cid};
                   7002:                             $crstype = $crsinfo{$cid}{'type'};
                   7003:                             $desc = $crsinfo{$cid}{'description'};
                   7004:                         } else {
                   7005:                             $missing{$cid} = 1;
                   7006:                         }
                   7007:                     }
                   7008:                     $extra = &mt($crstype).': <a href="/public/'.$cdom.'/'.$cnum.'/syllabus">'.$desc.'</a>';
1.406.2.6  raeburn  7009:                     if ($sec ne '') {
                   7010:                        $extra .= ' ('.&mt('Section: [_1]',$sec).')';
                   7011:                     }
1.406.2.5  raeburn  7012:                 } elsif ($extent =~ m{^/($match_domain)/($match_username|$)}) {
                   7013:                     my ($dom,$name) = ($1,$2);
                   7014:                     if ($rolecode eq 'au') {
                   7015:                         $extra = '';
                   7016:                     } elsif ($rolecode =~ /^(ca|aa)$/) {
                   7017:                         $extra = &mt('Authoring Space: [_1]',$name.':'.$dom);
                   7018:                     } elsif ($rolecode =~ /^(li|dg|dh|dc|sc)$/) {
                   7019:                         $extra = &mt('Domain: [_1]',$dom);
                   7020:                     }
                   7021:                 }
                   7022:                 my $rolename;
                   7023:                 if ($rolecode =~ m{^cr/($match_domain)/($match_username)/(\w+)}) {
                   7024:                     my $role = $3;
                   7025:                     my $owner = "($2:$1)";
                   7026:                     if ($2 eq $1.'-domainconfig') {
                   7027:                         $owner = '(ad hoc)';
                   7028:                     }
                   7029:                     $rolename = &mt('Custom role: [_1]',$role.' '.$owner);
                   7030:                 } else {
                   7031:                     $rolename = &Apache::lonnet::plaintext($rolecode,$crstype);
                   7032:                 }
                   7033:                 $shown = &mt('Role selection: [_1]',$rolename);
                   7034:             } else {
                   7035:                 $shown = &mt($event);
                   7036:                 if ($data ne '') {
                   7037:                    $extra = &mt('Client IP address: [_1]',$data);
                   7038:                 }
                   7039:             }
                   7040:             $r->print(
                   7041:             &Apache::loncommon::start_data_table_row()
                   7042:            .'<td>'.$count.'</td>'
                   7043:            .'<td>'.&Apache::lonlocal::locallocaltime($timestamp).'</td>'
                   7044:            .'<td>'.$host.'</td>'
                   7045:            .'<td>'.$shown.'</td>'
                   7046:            .'<td>'.$extra.'</td>'
                   7047:            .&Apache::loncommon::end_data_table_row()."\n");
                   7048:         }
                   7049:     }
                   7050: 
                   7051:     if ($showntableheader) { # Table footer, if content displayed above
                   7052:         $r->print(&Apache::loncommon::end_data_table().
                   7053:                   &userlogdisplay_navlinks(\%curr,$more_records));
                   7054:     } else { # No content displayed above
                   7055:         $r->print('<p class="LC_info">'
                   7056:                  .&mt('There are no records to display.')
                   7057:                  .'</p>');
                   7058:     }
                   7059: 
1.406.2.8! raeburn  7060:     if ($env{'form.popup'} == 1) {
        !          7061:         $r->print('<input type="hidden" name="popup" value="1" />'."\n");
        !          7062:     }
        !          7063: 
1.406.2.5  raeburn  7064:     # Form Footer
                   7065:     $r->print(
                   7066:         '<input type="hidden" name="currstate" value="" />'
                   7067:        .'<input type="hidden" name="accessuname" value="'.$uname.'" />'
                   7068:        .'<input type="hidden" name="accessudom" value="'.$udom.'" />'
                   7069:        .'<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   7070:        .'<input type="hidden" name="prevphases" value="'.$prevphasestr.'" />'
                   7071:        .'<input type="hidden" name="phase" value="activity" />'
                   7072:        .'<input type="hidden" name="action" value="accesslogs" />'
                   7073:        .'<input type="hidden" name="srchdomain" value="'.$udom.'" />'
                   7074:        .'<input type="hidden" name="srchby" value="'.$env{'form.srchby'}.'" />'
                   7075:        .'<input type="hidden" name="srchtype" value="'.$env{'form.srchtype'}.'" />'
                   7076:        .'<input type="hidden" name="srchterm" value="'.&HTML::Entities::encode($env{'form.srchterm'},'<>"&').'" />'
                   7077:        .'<input type="hidden" name="srchin" value="'.$env{'form.srchin'}.'" />'
                   7078:        .'</form>');
                   7079:     return;
                   7080: }
                   7081: 
                   7082: sub earlyout_accesslog_form {
                   7083:     my ($formname,$prevphasestr,$udom) = @_;
                   7084:     my $srchterm = &HTML::Entities::encode($env{'form.srchterm'},'<>"&');
                   7085:    return <<"END";
                   7086: <form action="/adm/createuser" method="post" name="$formname">
                   7087: <input type="hidden" name="currstate" value="" />
                   7088: <input type="hidden" name="prevphases" value="$prevphasestr" />
                   7089: <input type="hidden" name="phase" value="activity" />
                   7090: <input type="hidden" name="action" value="accesslogs" />
                   7091: <input type="hidden" name="srchdomain" value="$udom" />
                   7092: <input type="hidden" name="srchby" value="$env{'form.srchby'}" />
                   7093: <input type="hidden" name="srchtype" value="$env{'form.srchtype'}" />
                   7094: <input type="hidden" name="srchterm" value="$srchterm" />
                   7095: <input type="hidden" name="srchin" value="$env{'form.srchin'}" />
                   7096: </form>
                   7097: END
                   7098: }
                   7099: 
                   7100: sub activity_display_filter {
                   7101:     my ($formname,$curr) = @_;
                   7102:     my $nolink = 1;
                   7103:     my $output = '<table><tr><td valign="top">'.
                   7104:                  '<span class="LC_nobreak"><b>'.&mt('Actions/page:').'</b></span><br />'.
                   7105:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   7106:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   7107:                  '</td><td>&nbsp;&nbsp;</td>';
                   7108:     my $startform =
                   7109:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_start_date',
                   7110:                                             $curr->{'accesslog_start_date'},undef,
                   7111:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   7112:     my $endform =
                   7113:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_end_date',
                   7114:                                             $curr->{'accesslog_end_date'},undef,
                   7115:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   7116:     my %lt = &Apache::lonlocal::texthash (
                   7117:                                           activity => 'Activity',
                   7118:                                           Role     => 'Role selection',
                   7119:                                           log      => 'Log-in or Logout',
                   7120:     );
                   7121:     $output .= '<td valign="top"><b>'.&mt('Window during which actions occurred:').'</b><br />'.
                   7122:                '<table><tr><td>'.&mt('After:').
                   7123:                '</td><td>'.$startform.'</td></tr>'.
                   7124:                '<tr><td>'.&mt('Before:').'</td>'.
                   7125:                '<td>'.$endform.'</td></tr></table>'.
                   7126:                '</td>'.
                   7127:                '<td>&nbsp;&nbsp;</td>'.
                   7128:                '<td valign="top"><b>'.&mt('Activities').'</b><br />'.
                   7129:                '<select name="activity"><option value="any"';
                   7130:     if ($curr->{'activity'} eq 'any') {
                   7131:         $output .= ' selected="selected"';
                   7132:     }
                   7133:     $output .= '>'.&mt('Any').'</option>'."\n";
                   7134:     foreach my $activity ('Role','log') {
                   7135:         my $selstr = '';
                   7136:         if ($activity eq $curr->{'activity'}) {
                   7137:             $selstr = ' selected="selected"';
                   7138:         }
                   7139:         $output .= '<option value="'.$activity.'"'.$selstr.'>'.$lt{$activity}.'</option>';
                   7140:     }
                   7141:     $output .= '</select></td>'.
                   7142:                '</tr></table>';
                   7143:     # Update Display button
                   7144:     $output .= '<p>'
                   7145:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   7146:               .'</p>';
                   7147:     return $output;
                   7148: }
                   7149: 
                   7150: sub userlogdisplay_js {
                   7151:     my ($formname) = @_;
                   7152:     return <<"ENDSCRIPT";
                   7153: 
1.239     raeburn  7154: function chgPage(caller) {
                   7155:     if (caller == 'previous') {
                   7156:         document.$formname.page.value --;
                   7157:     }
                   7158:     if (caller == 'next') {
                   7159:         document.$formname.page.value ++;
                   7160:     }
1.327     raeburn  7161:     document.$formname.submit();
1.239     raeburn  7162:     return;
                   7163: }
                   7164: ENDSCRIPT
1.406.2.5  raeburn  7165: }
                   7166: 
                   7167: sub userlogdisplay_navlinks {
                   7168:     my ($curr,$more_records) = @_;
                   7169:     return unless(ref($curr) eq 'HASH');
                   7170:     # Navigation Buttons
                   7171:     my $nav_links = '<p>';
                   7172:     if (($curr->{'page'} > 1) || ($more_records)) {
                   7173:         if (($curr->{'page'} > 1) && ($curr->{'show'} !~ /\D/)) {
                   7174:             $nav_links .= '<input type="button"'
                   7175:                          .' onclick="javascript:chgPage('."'previous'".');"'
                   7176:                          .' value="'.&mt('Previous [_1] changes',$curr->{'show'})
                   7177:                          .'" /> ';
                   7178:         }
                   7179:         if ($more_records) {
                   7180:             $nav_links .= '<input type="button"'
                   7181:                          .' onclick="javascript:chgPage('."'next'".');"'
                   7182:                          .' value="'.&mt('Next [_1] changes',$curr->{'show'})
                   7183:                          .'" />';
1.301     bisitz   7184:         }
                   7185:     }
1.406.2.5  raeburn  7186:     $nav_links .= '</p>';
                   7187:     return $nav_links;
1.239     raeburn  7188: }
                   7189: 
                   7190: sub role_display_filter {
1.363     raeburn  7191:     my ($context,$formname,$cdom,$cnum,$curr,$version,$crstype) = @_;
                   7192:     my $lctype;
                   7193:     if ($context eq 'course') {
                   7194:         $lctype = lc($crstype);
                   7195:     }
1.239     raeburn  7196:     my $nolink = 1;
                   7197:     my $output = '<table><tr><td valign="top">'.
1.301     bisitz   7198:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b></span><br />'.
1.239     raeburn  7199:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   7200:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   7201:                  '</td><td>&nbsp;&nbsp;</td>';
                   7202:     my $startform =
                   7203:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
                   7204:                                             $curr->{'rolelog_start_date'},undef,
                   7205:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   7206:     my $endform =
                   7207:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
                   7208:                                             $curr->{'rolelog_end_date'},undef,
                   7209:                                             undef,undef,undef,undef,undef,undef,$nolink);
1.363     raeburn  7210:     my %lt = &rolechg_contexts($context,$crstype);
1.301     bisitz   7211:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br />'.
                   7212:                '<table><tr><td>'.&mt('After:').
                   7213:                '</td><td>'.$startform.'</td></tr>'.
                   7214:                '<tr><td>'.&mt('Before:').'</td>'.
                   7215:                '<td>'.$endform.'</td></tr></table>'.
                   7216:                '</td>'.
                   7217:                '<td>&nbsp;&nbsp;</td>'.
1.239     raeburn  7218:                '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
                   7219:                '<select name="role"><option value="any"';
                   7220:     if ($curr->{'role'} eq 'any') {
                   7221:         $output .= ' selected="selected"';
                   7222:     }
                   7223:     $output .=  '>'.&mt('Any').'</option>'."\n";
1.363     raeburn  7224:     my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
1.239     raeburn  7225:     foreach my $role (@roles) {
                   7226:         my $plrole;
                   7227:         if ($role eq 'cr') {
                   7228:             $plrole = &mt('Custom Role');
                   7229:         } else {
1.318     raeburn  7230:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.239     raeburn  7231:         }
                   7232:         my $selstr = '';
                   7233:         if ($role eq $curr->{'role'}) {
                   7234:             $selstr = ' selected="selected"';
                   7235:         }
                   7236:         $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
                   7237:     }
1.301     bisitz   7238:     $output .= '</select></td>'.
                   7239:                '<td>&nbsp;&nbsp;</td>'.
                   7240:                '<td valign="top"><b>'.
1.239     raeburn  7241:                &mt('Context:').'</b><br /><select name="chgcontext">';
1.363     raeburn  7242:     my @posscontexts;
                   7243:     if ($context eq 'course') {
1.376     raeburn  7244:         @posscontexts = ('any','automated','updatenow','createcourse','course','domain','selfenroll','requestcourses');
1.363     raeburn  7245:     } elsif ($context eq 'domain') {
                   7246:         @posscontexts = ('any','domain','requestauthor','domconfig','server');
                   7247:     } else {
                   7248:         @posscontexts = ('any','author','domain');
                   7249:     } 
                   7250:     foreach my $chgtype (@posscontexts) {
1.239     raeburn  7251:         my $selstr = '';
                   7252:         if ($curr->{'chgcontext'} eq $chgtype) {
1.301     bisitz   7253:             $selstr = ' selected="selected"';
1.239     raeburn  7254:         }
1.363     raeburn  7255:         if ($context eq 'course') {
1.376     raeburn  7256:             if (($chgtype eq 'automated') || ($chgtype eq 'updatenow')) {
1.363     raeburn  7257:                 next if (!&Apache::lonnet::auto_run($cnum,$cdom));
                   7258:             }
1.239     raeburn  7259:         }
                   7260:         $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
1.248     raeburn  7261:     }
1.303     bisitz   7262:     $output .= '</select></td>'
                   7263:               .'</tr></table>';
                   7264: 
                   7265:     # Update Display button
                   7266:     $output .= '<p>'
                   7267:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   7268:               .'</p>';
                   7269: 
                   7270:     # Server version info
1.363     raeburn  7271:     my $needsrev = '2.11.0';
                   7272:     if ($context eq 'course') {
                   7273:         $needsrev = '2.7.0';
                   7274:     }
                   7275:     
1.303     bisitz   7276:     $output .= '<p class="LC_info">'
                   7277:               .&mt('Only changes made from servers running LON-CAPA [_1] or later are displayed.'
1.363     raeburn  7278:                   ,$needsrev);
1.248     raeburn  7279:     if ($version) {
1.303     bisitz   7280:         $output .= ' '.&mt('This LON-CAPA server is version [_1]',$version);
                   7281:     }
                   7282:     $output .= '</p><hr />';
1.239     raeburn  7283:     return $output;
                   7284: }
                   7285: 
                   7286: sub rolechg_contexts {
1.363     raeburn  7287:     my ($context,$crstype) = @_;
                   7288:     my %lt;
                   7289:     if ($context eq 'course') {
                   7290:         %lt = &Apache::lonlocal::texthash (
1.239     raeburn  7291:                                              any          => 'Any',
1.376     raeburn  7292:                                              automated    => 'Automated Enrollment',
1.239     raeburn  7293:                                              updatenow    => 'Roster Update',
                   7294:                                              createcourse => 'Course Creation',
                   7295:                                              course       => 'User Management in course',
                   7296:                                              domain       => 'User Management in domain',
1.313     raeburn  7297:                                              selfenroll   => 'Self-enrolled',
1.318     raeburn  7298:                                              requestcourses => 'Course Request',
1.239     raeburn  7299:                                          );
1.363     raeburn  7300:         if ($crstype eq 'Community') {
                   7301:             $lt{'createcourse'} = &mt('Community Creation');
                   7302:             $lt{'course'} = &mt('User Management in community');
                   7303:             $lt{'requestcourses'} = &mt('Community Request');
                   7304:         }
                   7305:     } elsif ($context eq 'domain') {
                   7306:         %lt = &Apache::lonlocal::texthash (
                   7307:                                              any           => 'Any',
                   7308:                                              domain        => 'User Management in domain',
                   7309:                                              requestauthor => 'Authoring Request',
                   7310:                                              server        => 'Command line script (DC role)',
                   7311:                                              domconfig     => 'Self-enrolled',
                   7312:                                          );
                   7313:     } else {
                   7314:         %lt = &Apache::lonlocal::texthash (
                   7315:                                              any    => 'Any',
                   7316:                                              domain => 'User Management in domain',
                   7317:                                              author => 'User Management by author',
                   7318:                                          );
                   7319:     } 
1.239     raeburn  7320:     return %lt;
                   7321: }
                   7322: 
1.27      matthew  7323: #-------------------------------------------------- functions for &phase_two
1.160     raeburn  7324: sub user_search_result {
1.221     raeburn  7325:     my ($context,$srch) = @_;
1.160     raeburn  7326:     my %allhomes;
                   7327:     my %inst_matches;
                   7328:     my %srch_results;
1.181     raeburn  7329:     my ($response,$currstate,$forcenewuser,$dirsrchres);
1.183     raeburn  7330:     $srch->{'srchterm'} =~ s/\s+/ /g;
1.176     raeburn  7331:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
1.160     raeburn  7332:         $response = &mt('Invalid search.');
                   7333:     }
                   7334:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
                   7335:         $response = &mt('Invalid search.');
                   7336:     }
1.177     raeburn  7337:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
1.160     raeburn  7338:         $response = &mt('Invalid search.');
                   7339:     }
                   7340:     if ($srch->{'srchterm'} eq '') {
                   7341:         $response = &mt('You must enter a search term.');
                   7342:     }
1.183     raeburn  7343:     if ($srch->{'srchterm'} =~ /^\s+$/) {
                   7344:         $response = &mt('Your search term must contain more than just spaces.');
                   7345:     }
1.160     raeburn  7346:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
                   7347:         if (($srch->{'srchdomain'} eq '') || 
1.163     albertel 7348: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
1.160     raeburn  7349:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
                   7350:         }
                   7351:     }
                   7352:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
                   7353:         ($srch->{'srchin'} eq 'alc')) {
1.176     raeburn  7354:         if ($srch->{'srchby'} eq 'uname') {
1.243     raeburn  7355:             my $unamecheck = $srch->{'srchterm'};
                   7356:             if ($srch->{'srchtype'} eq 'contains') {
                   7357:                 if ($unamecheck !~ /^\w/) {
                   7358:                     $unamecheck = 'a'.$unamecheck; 
                   7359:                 }
                   7360:             }
                   7361:             if ($unamecheck !~ /^$match_username$/) {
1.176     raeburn  7362:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
                   7363:             }
1.160     raeburn  7364:         }
                   7365:     }
1.180     raeburn  7366:     if ($response ne '') {
1.406.2.4  raeburn  7367:         $response = '<span class="LC_warning">'.$response.'</span><br />';
1.180     raeburn  7368:     }
1.160     raeburn  7369:     if ($srch->{'srchin'} eq 'instd') {
1.406.2.3  raeburn  7370:         my $instd_chk = &instdirectorysrch_check($srch);
1.160     raeburn  7371:         if ($instd_chk ne 'ok') {
1.406.2.3  raeburn  7372:             my $domd_chk = &domdirectorysrch_check($srch);
1.406.2.4  raeburn  7373:             $response .= '<span class="LC_warning">'.$instd_chk.'</span><br />';
1.406.2.3  raeburn  7374:             if ($domd_chk eq 'ok') {
1.406.2.4  raeburn  7375:                 $response .= &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.');
1.406.2.3  raeburn  7376:             }
1.406.2.5  raeburn  7377:             $response .= '<br />';
1.406.2.3  raeburn  7378:         }
                   7379:     } else {
                   7380:         unless (($context eq 'requestcrs') && ($srch->{'srchtype'} eq 'exact')) {
                   7381:             my $domd_chk = &domdirectorysrch_check($srch);
                   7382:             if ($domd_chk ne 'ok') {
                   7383:                 my $instd_chk = &instdirectorysrch_check($srch);
1.406.2.4  raeburn  7384:                 $response .= '<span class="LC_warning">'.$domd_chk.'</span><br />';
1.406.2.3  raeburn  7385:                 if ($instd_chk eq 'ok') {
1.406.2.4  raeburn  7386:                     $response .= &mt('You may want to search in the institutional directory instead of the LON-CAPA domain.');
1.406.2.3  raeburn  7387:                 }
1.406.2.5  raeburn  7388:                 $response .= '<br />';
1.406.2.3  raeburn  7389:             }
1.160     raeburn  7390:         }
                   7391:     }
                   7392:     if ($response ne '') {
1.180     raeburn  7393:         return ($currstate,$response);
1.160     raeburn  7394:     }
                   7395:     if ($srch->{'srchby'} eq 'uname') {
                   7396:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
                   7397:             if ($env{'form.forcenew'}) {
                   7398:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
                   7399:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   7400:                     if ($uhome eq 'no_host') {
                   7401:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.180     raeburn  7402:                         my $showdom = &display_domain_info($env{'request.role.domain'});
                   7403:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
1.160     raeburn  7404:                     } else {
1.179     raeburn  7405:                         $currstate = 'modify';
1.160     raeburn  7406:                     }
                   7407:                 } else {
1.179     raeburn  7408:                     $currstate = 'modify';
1.160     raeburn  7409:                 }
                   7410:             } else {
                   7411:                 if ($srch->{'srchin'} eq 'dom') {
1.162     raeburn  7412:                     if ($srch->{'srchtype'} eq 'exact') {
                   7413:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   7414:                         if ($uhome eq 'no_host') {
1.179     raeburn  7415:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  7416:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  7417:                         } else {
1.179     raeburn  7418:                             $currstate = 'modify';
1.406.2.5  raeburn  7419:                             if ($env{'form.action'} eq 'accesslogs') {
                   7420:                                 $currstate = 'activity';
                   7421:                             }
1.310     raeburn  7422:                             my $uname = $srch->{'srchterm'};
                   7423:                             my $udom = $srch->{'srchdomain'};
                   7424:                             $srch_results{$uname.':'.$udom} =
                   7425:                                 { &Apache::lonnet::get('environment',
                   7426:                                                        ['firstname',
                   7427:                                                         'lastname',
                   7428:                                                         'permanentemail'],
                   7429:                                                          $udom,$uname)
                   7430:                                 };
1.162     raeburn  7431:                         }
                   7432:                     } else {
                   7433:                         %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  7434:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  7435:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  7436:                     }
                   7437:                 } else {
1.167     albertel 7438:                     my $courseusers = &get_courseusers();
1.162     raeburn  7439:                     if ($srch->{'srchtype'} eq 'exact') {
1.167     albertel 7440:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
1.179     raeburn  7441:                             $currstate = 'modify';
1.162     raeburn  7442:                         } else {
1.179     raeburn  7443:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  7444:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  7445:                         }
1.160     raeburn  7446:                     } else {
1.167     albertel 7447:                         foreach my $user (keys(%$courseusers)) {
1.162     raeburn  7448:                             my ($cuname,$cudomain) = split(/:/,$user);
                   7449:                             if ($cudomain eq $srch->{'srchdomain'}) {
1.177     raeburn  7450:                                 my $matched = 0;
                   7451:                                 if ($srch->{'srchtype'} eq 'begins') {
                   7452:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
                   7453:                                         $matched = 1;
                   7454:                                     }
                   7455:                                 } else {
                   7456:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
                   7457:                                         $matched = 1;
                   7458:                                     }
                   7459:                                 }
                   7460:                                 if ($matched) {
1.167     albertel 7461:                                     $srch_results{$user} = 
                   7462: 					{&Apache::lonnet::get('environment',
                   7463: 							     ['firstname',
                   7464: 							      'lastname',
1.194     albertel 7465: 							      'permanentemail'],
                   7466: 							      $cudomain,$cuname)};
1.162     raeburn  7467:                                 }
                   7468:                             }
                   7469:                         }
1.179     raeburn  7470:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  7471:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  7472:                     }
                   7473:                 }
                   7474:             }
                   7475:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  7476:             $currstate = 'query';
1.160     raeburn  7477:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  7478:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
                   7479:             if ($dirsrchres eq 'ok') {
                   7480:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7481:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  7482:             } else {
                   7483:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   7484:                 $response = '<span class="LC_warning">'.
                   7485:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   7486:                     '</span><br />'.
                   7487:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
1.406.2.5  raeburn  7488:                     '<br />'; 
1.181     raeburn  7489:             }
1.160     raeburn  7490:         }
                   7491:     } else {
                   7492:         if ($srch->{'srchin'} eq 'dom') {
                   7493:             %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  7494:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7495:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  7496:         } elsif ($srch->{'srchin'} eq 'crs') {
1.167     albertel 7497:             my $courseusers = &get_courseusers(); 
                   7498:             foreach my $user (keys(%$courseusers)) {
1.160     raeburn  7499:                 my ($uname,$udom) = split(/:/,$user);
                   7500:                 my %names = &Apache::loncommon::getnames($uname,$udom);
                   7501:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
                   7502:                 if ($srch->{'srchby'} eq 'lastname') {
                   7503:                     if ((($srch->{'srchtype'} eq 'exact') && 
                   7504:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
1.177     raeburn  7505:                         (($srch->{'srchtype'} eq 'begins') &&
                   7506:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
1.160     raeburn  7507:                         (($srch->{'srchtype'} eq 'contains') &&
                   7508:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
                   7509:                         $srch_results{$user} = {firstname => $names{'firstname'},
                   7510:                                             lastname => $names{'lastname'},
                   7511:                                             permanentemail => $emails{'permanentemail'},
                   7512:                                            };
                   7513:                     }
                   7514:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
                   7515:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
1.177     raeburn  7516:                     $srchlast =~ s/\s+$//;
                   7517:                     $srchfirst =~ s/^\s+//;
1.160     raeburn  7518:                     if ($srch->{'srchtype'} eq 'exact') {
                   7519:                         if (($names{'lastname'} eq $srchlast) &&
                   7520:                             ($names{'firstname'} eq $srchfirst)) {
                   7521:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   7522:                                                 lastname => $names{'lastname'},
                   7523:                                                 permanentemail => $emails{'permanentemail'},
                   7524: 
                   7525:                                            };
                   7526:                         }
1.177     raeburn  7527:                     } elsif ($srch->{'srchtype'} eq 'begins') {
                   7528:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
                   7529:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
                   7530:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   7531:                                                 lastname => $names{'lastname'},
                   7532:                                                 permanentemail => $emails{'permanentemail'},
                   7533:                                                };
                   7534:                         }
                   7535:                     } else {
1.160     raeburn  7536:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
                   7537:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
                   7538:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   7539:                                                 lastname => $names{'lastname'},
                   7540:                                                 permanentemail => $emails{'permanentemail'},
                   7541:                                                };
                   7542:                         }
                   7543:                     }
                   7544:                 }
                   7545:             }
1.179     raeburn  7546:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7547:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  7548:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  7549:             $currstate = 'query';
1.160     raeburn  7550:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  7551:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
                   7552:             if ($dirsrchres eq 'ok') {
                   7553:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7554:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  7555:             } else {
1.406.2.5  raeburn  7556:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   7557:                 $response = '<span class="LC_warning">'.
1.181     raeburn  7558:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   7559:                     '</span><br />'.
                   7560:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
1.406.2.5  raeburn  7561:                     '<br />';
1.181     raeburn  7562:             }
1.160     raeburn  7563:         }
                   7564:     }
1.179     raeburn  7565:     return ($currstate,$response,$forcenewuser,\%srch_results);
1.160     raeburn  7566: }
                   7567: 
1.406.2.3  raeburn  7568: sub domdirectorysrch_check {
                   7569:     my ($srch) = @_;
                   7570:     my $response;
                   7571:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   7572:                                              ['directorysrch'],$srch->{'srchdomain'});
                   7573:     my $showdom = &display_domain_info($srch->{'srchdomain'});
                   7574:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   7575:         if ($dom_inst_srch{'directorysrch'}{'lcavailable'} eq '0') {
                   7576:             return &mt('LON-CAPA directory search is not available in domain: [_1]',$showdom);
                   7577:         }
                   7578:         if ($dom_inst_srch{'directorysrch'}{'lclocalonly'}) {
                   7579:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
                   7580:                 return &mt('LON-CAPA directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom);
                   7581:             }
                   7582:         }
                   7583:     }
                   7584:     return 'ok';
                   7585: }
                   7586: 
                   7587: sub instdirectorysrch_check {
1.160     raeburn  7588:     my ($srch) = @_;
                   7589:     my $can_search = 0;
                   7590:     my $response;
                   7591:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   7592:                                              ['directorysrch'],$srch->{'srchdomain'});
1.180     raeburn  7593:     my $showdom = &display_domain_info($srch->{'srchdomain'});
1.160     raeburn  7594:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   7595:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
1.180     raeburn  7596:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
1.160     raeburn  7597:         }
                   7598:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
                   7599:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
1.180     raeburn  7600:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
1.160     raeburn  7601:             }
                   7602:             my @usertypes = split(/:/,$env{'environment.inststatus'});
                   7603:             if (!@usertypes) {
                   7604:                 push(@usertypes,'default');
                   7605:             }
                   7606:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
                   7607:                 foreach my $type (@usertypes) {
                   7608:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
                   7609:                         $can_search = 1;
                   7610:                         last;
                   7611:                     }
                   7612:                 }
                   7613:             }
                   7614:             if (!$can_search) {
                   7615:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
                   7616:                 my @longtypes; 
                   7617:                 foreach my $item (@usertypes) {
1.229     raeburn  7618:                     if (defined($insttypes->{$item})) { 
                   7619:                         push (@longtypes,$insttypes->{$item});
                   7620:                     } elsif ($item eq 'default') {
                   7621:                         push (@longtypes,&mt('other')); 
                   7622:                     }
1.160     raeburn  7623:                 }
                   7624:                 my $insttype_str = join(', ',@longtypes); 
1.180     raeburn  7625:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
1.229     raeburn  7626:             }
1.160     raeburn  7627:         } else {
                   7628:             $can_search = 1;
                   7629:         }
                   7630:     } else {
1.180     raeburn  7631:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
1.160     raeburn  7632:     }
                   7633:     my %longtext = &Apache::lonlocal::texthash (
1.167     albertel 7634:                        uname     => 'username',
1.160     raeburn  7635:                        lastfirst => 'last name, first name',
1.167     albertel 7636:                        lastname  => 'last name',
1.172     raeburn  7637:                        contains  => 'contains',
1.178     raeburn  7638:                        exact     => 'as exact match to',
                   7639:                        begins    => 'begins with',
1.160     raeburn  7640:                    );
                   7641:     if ($can_search) {
                   7642:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
                   7643:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
1.180     raeburn  7644:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
1.160     raeburn  7645:             }
                   7646:         } else {
1.180     raeburn  7647:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
1.160     raeburn  7648:         }
                   7649:     }
                   7650:     if ($can_search) {
1.178     raeburn  7651:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
                   7652:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
                   7653:                 return 'ok';
                   7654:             } else {
1.180     raeburn  7655:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  7656:             }
                   7657:         } else {
                   7658:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
                   7659:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
                   7660:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
                   7661:                 return 'ok';
                   7662:             } else {
1.180     raeburn  7663:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  7664:             }
1.160     raeburn  7665:         }
                   7666:     }
                   7667: }
                   7668: 
                   7669: sub get_courseusers {
                   7670:     my %advhash;
1.167     albertel 7671:     my $classlist = &Apache::loncoursedata::get_classlist();
1.160     raeburn  7672:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                   7673:     foreach my $role (sort(keys(%coursepersonnel))) {
                   7674:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
1.167     albertel 7675: 	    if (!exists($classlist->{$user})) {
                   7676: 		$classlist->{$user} = [];
                   7677: 	    }
1.160     raeburn  7678:         }
                   7679:     }
1.167     albertel 7680:     return $classlist;
1.160     raeburn  7681: }
                   7682: 
                   7683: sub build_search_response {
1.221     raeburn  7684:     my ($context,$srch,%srch_results) = @_;
1.179     raeburn  7685:     my ($currstate,$response,$forcenewuser);
1.160     raeburn  7686:     my %names = (
1.330     bisitz   7687:           'uname'     => 'username',
                   7688:           'lastname'  => 'last name',
1.160     raeburn  7689:           'lastfirst' => 'last name, first name',
1.330     bisitz   7690:           'crs'       => 'this course',
                   7691:           'dom'       => 'LON-CAPA domain',
                   7692:           'instd'     => 'the institutional directory for domain',
1.160     raeburn  7693:     );
                   7694: 
                   7695:     my %single = (
1.180     raeburn  7696:                    begins   => 'A match',
1.160     raeburn  7697:                    contains => 'A match',
1.180     raeburn  7698:                    exact    => 'An exact match',
1.160     raeburn  7699:                  );
                   7700:     my %nomatch = (
1.180     raeburn  7701:                    begins   => 'No match',
1.160     raeburn  7702:                    contains => 'No match',
1.180     raeburn  7703:                    exact    => 'No exact match',
1.160     raeburn  7704:                   );
                   7705:     if (keys(%srch_results) > 1) {
1.179     raeburn  7706:         $currstate = 'select';
1.160     raeburn  7707:     } else {
                   7708:         if (keys(%srch_results) == 1) {
1.406.2.5  raeburn  7709:             if ($env{'form.action'} eq 'accesslogs') {
                   7710:                 $currstate = 'activity';
                   7711:             } else {
                   7712:                 $currstate = 'modify';
                   7713:             }
1.180     raeburn  7714:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
                   7715:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   7716:                 $response .= ': '.&display_domain_info($srch->{'srchdomain'});
1.180     raeburn  7717:             }
1.330     bisitz   7718:         } else { # Search has nothing found. Prepare message to user.
                   7719:             $response = '<span class="LC_warning">';
1.180     raeburn  7720:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   7721:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}: [_2]",
                   7722:                                  '<b>'.$srch->{'srchterm'}.'</b>',
                   7723:                                  &display_domain_info($srch->{'srchdomain'}));
                   7724:             } else {
                   7725:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}.",
                   7726:                                  '<b>'.$srch->{'srchterm'}.'</b>');
1.180     raeburn  7727:             }
                   7728:             $response .= '</span>';
1.330     bisitz   7729: 
1.160     raeburn  7730:             if ($srch->{'srchin'} ne 'alc') {
                   7731:                 $forcenewuser = 1;
                   7732:                 my $cansrchinst = 0; 
                   7733:                 if ($srch->{'srchdomain'}) {
                   7734:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
                   7735:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
                   7736:                         if ($domconfig{'directorysrch'}{'available'}) {
                   7737:                             $cansrchinst = 1;
                   7738:                         } 
                   7739:                     }
                   7740:                 }
1.180     raeburn  7741:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
                   7742:                      ($srch->{'srchby'} eq 'lastname')) &&
                   7743:                     ($srch->{'srchin'} eq 'dom')) {
                   7744:                     if ($cansrchinst) {
                   7745:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
1.160     raeburn  7746:                     }
                   7747:                 }
1.180     raeburn  7748:                 if ($srch->{'srchin'} eq 'crs') {
                   7749:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
                   7750:                 }
                   7751:             }
1.305     raeburn  7752:             my $createdom = $env{'request.role.domain'};
                   7753:             if ($context eq 'requestcrs') {
                   7754:                 if ($env{'form.coursedom'} ne '') {
                   7755:                     $createdom = $env{'form.coursedom'};
                   7756:                 }
                   7757:             }
1.406.2.5  raeburn  7758:             unless (($env{'form.action'} eq 'accesslogs') || (($srch->{'srchby'} eq 'uname') && ($srch->{'srchin'} eq 'dom') &&
                   7759:                     ($srch->{'srchtype'} eq 'exact') && ($srch->{'srchdomain'} eq $createdom))) {
1.221     raeburn  7760:                 my $cancreate =
1.305     raeburn  7761:                     &Apache::lonuserutils::can_create_user($createdom,$context);
                   7762:                 my $targetdom = '<span class="LC_cusr_emph">'.$createdom.'</span>';
1.221     raeburn  7763:                 if ($cancreate) {
1.305     raeburn  7764:                     my $showdom = &display_domain_info($createdom); 
1.266     bisitz   7765:                     $response .= '<br /><br />'
                   7766:                                 .'<b>'.&mt('To add a new user:').'</b>'
1.305     raeburn  7767:                                 .'<br />';
                   7768:                     if ($context eq 'requestcrs') {
                   7769:                         $response .= &mt("(You can only define new users in the new course's domain - [_1])",$targetdom);
                   7770:                     } else {
                   7771:                         $response .= &mt("(You can only create new users in your current role's domain - [_1])",$targetdom);
                   7772:                     }
                   7773:                     $response .='<ul><li>'
1.266     bisitz   7774:                                 .&mt("Set 'Domain/institution to search' to: [_1]",'<span class="LC_cusr_emph">'.$showdom.'</span>')
                   7775:                                 .'</li><li>'
                   7776:                                 .&mt("Set 'Search criteria' to: [_1]username is ..... in selected LON-CAPA domain[_2]",'<span class="LC_cusr_emph">','</span>')
                   7777:                                 .'</li><li>'
                   7778:                                 .&mt('Provide the proposed username')
                   7779:                                 .'</li><li>'
                   7780:                                 .&mt("Click 'Search'")
                   7781:                                 .'</li></ul><br />';
1.221     raeburn  7782:                 } else {
1.406.2.7  raeburn  7783:                     unless (($context eq 'domain') && ($env{'form.action'} eq 'singleuser')) {
                   7784:                         my $helplink = ' href="javascript:helpMenu('."'display'".')"';
                   7785:                         $response .= '<br /><br />';
                   7786:                         if ($context eq 'requestcrs') {
                   7787:                             $response .= &mt("You are not authorized to define new users in the new course's domain - [_1].",$targetdom);
                   7788:                         } else {
                   7789:                             $response .= &mt("You are not authorized to create new users in your current role's domain - [_1].",$targetdom);
                   7790:                         }
                   7791:                         $response .= '<br />'
                   7792:                                      .&mt('Please contact the [_1]helpdesk[_2] if you need to create a new user.'
                   7793:                                         ,' <a'.$helplink.'>'
                   7794:                                         ,'</a>')
                   7795:                                      .'<br />';
1.305     raeburn  7796:                     }
1.221     raeburn  7797:                 }
1.160     raeburn  7798:             }
                   7799:         }
                   7800:     }
1.179     raeburn  7801:     return ($currstate,$response,$forcenewuser);
1.160     raeburn  7802: }
                   7803: 
1.180     raeburn  7804: sub display_domain_info {
                   7805:     my ($dom) = @_;
                   7806:     my $output = $dom;
                   7807:     if ($dom ne '') { 
                   7808:         my $domdesc = &Apache::lonnet::domain($dom,'description');
                   7809:         if ($domdesc ne '') {
                   7810:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
                   7811:         }
                   7812:     }
                   7813:     return $output;
                   7814: }
                   7815: 
1.160     raeburn  7816: sub crumb_utilities {
                   7817:     my %elements = (
                   7818:        crtuser => {
                   7819:            srchterm => 'text',
1.172     raeburn  7820:            srchin => 'selectbox',
1.160     raeburn  7821:            srchby => 'selectbox',
                   7822:            srchtype => 'selectbox',
                   7823:            srchdomain => 'selectbox',
                   7824:        },
1.207     raeburn  7825:        crtusername => {
                   7826:            srchterm => 'text',
                   7827:            srchdomain => 'selectbox',
                   7828:        },
1.160     raeburn  7829:        docustom => {
                   7830:            rolename => 'selectbox',
                   7831:            newrolename => 'textbox',
                   7832:        },
1.179     raeburn  7833:        studentform => {
                   7834:            srchterm => 'text',
                   7835:            srchin => 'selectbox',
                   7836:            srchby => 'selectbox',
                   7837:            srchtype => 'selectbox',
                   7838:            srchdomain => 'selectbox',
                   7839:        },
1.160     raeburn  7840:     );
                   7841: 
                   7842:     my $jsback .= qq|
                   7843: function backPage(formname,prevphase,prevstate) {
1.211     raeburn  7844:     if (typeof prevphase == 'undefined') {
                   7845:         formname.phase.value = '';
                   7846:     }
                   7847:     else {  
                   7848:         formname.phase.value = prevphase;
                   7849:     }
                   7850:     if (typeof prevstate == 'undefined') {
                   7851:         formname.currstate.value = '';
                   7852:     }
                   7853:     else {
                   7854:         formname.currstate.value = prevstate;
                   7855:     }
1.160     raeburn  7856:     formname.submit();
                   7857: }
                   7858: |;
                   7859:     return ($jsback,\%elements);
                   7860: }
                   7861: 
1.26      matthew  7862: sub course_level_table {
1.375     raeburn  7863:     my ($inccourses,$showcredits,$defaultcredits) = @_;
                   7864:     return unless (ref($inccourses) eq 'HASH');
1.26      matthew  7865:     my $table = '';
1.62      www      7866: # Custom Roles?
                   7867: 
1.190     raeburn  7868:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.89      raeburn  7869:     my %lt=&Apache::lonlocal::texthash(
                   7870:             'exs'  => "Existing sections",
                   7871:             'new'  => "Define new section",
                   7872:             'ssd'  => "Set Start Date",
                   7873:             'sed'  => "Set End Date",
1.131     raeburn  7874:             'crl'  => "Course Level",
1.89      raeburn  7875:             'act'  => "Activate",
                   7876:             'rol'  => "Role",
                   7877:             'ext'  => "Extent",
1.113     raeburn  7878:             'grs'  => "Section",
1.375     raeburn  7879:             'crd'  => "Credits",
1.89      raeburn  7880:             'sta'  => "Start",
                   7881:             'end'  => "End"
                   7882:     );
1.62      www      7883: 
1.375     raeburn  7884:     foreach my $protectedcourse (sort(keys(%{$inccourses}))) {
1.135     raeburn  7885: 	my $thiscourse=$protectedcourse;
1.26      matthew  7886: 	$thiscourse=~s:_:/:g;
                   7887: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
1.365     raeburn  7888:         my $isowner = &Apache::lonuserutils::is_courseowner($protectedcourse,$coursedata{'internal.courseowner'});
1.26      matthew  7889: 	my $area=$coursedata{'description'};
1.321     raeburn  7890:         my $crstype=$coursedata{'type'};
1.135     raeburn  7891: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
1.89      raeburn  7892: 	my ($domain,$cnum)=split(/\//,$thiscourse);
1.115     albertel 7893:         my %sections_count;
1.101     albertel 7894:         if (defined($env{'request.course.id'})) {
                   7895:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
1.115     albertel 7896:                 %sections_count = 
                   7897: 		    &Apache::loncommon::get_sections($domain,$cnum);
1.92      raeburn  7898:             }
                   7899:         }
1.321     raeburn  7900:         my @roles = &Apache::lonuserutils::roles_by_context('course','',$crstype);
1.213     raeburn  7901: 	foreach my $role (@roles) {
1.321     raeburn  7902:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.329     raeburn  7903: 	    if ((&Apache::lonnet::allowed('c'.$role,$thiscourse)) ||
                   7904:                 ((($role eq 'cc') || ($role eq 'co')) && ($isowner))) {
1.221     raeburn  7905:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  7906:                                             $plrole,\%sections_count,\%lt,
1.402     raeburn  7907:                                             $showcredits,$defaultcredits,$crstype);
1.221     raeburn  7908:             } elsif ($env{'request.course.sec'} ne '') {
                   7909:                 if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
                   7910:                                              $env{'request.course.sec'})) {
                   7911:                     $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  7912:                                                 $plrole,\%sections_count,\%lt,
1.402     raeburn  7913:                                                 $showcredits,$defaultcredits,$crstype);
1.26      matthew  7914:                 }
                   7915:             }
                   7916:         }
1.221     raeburn  7917:         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
1.324     raeburn  7918:             foreach my $cust (sort(keys(%customroles))) {
                   7919:                 next if ($crstype eq 'Community' && $customroles{$cust} =~ /bre\&S/);
1.221     raeburn  7920:                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
                   7921:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.402     raeburn  7922:                                             $cust,\%sections_count,\%lt,
                   7923:                                             $showcredits,$defaultcredits,$crstype);
1.221     raeburn  7924:             }
1.62      www      7925: 	}
1.26      matthew  7926:     }
                   7927:     return '' if ($table eq ''); # return nothing if there is nothing 
                   7928:                                  # in the table
1.188     raeburn  7929:     my $result;
                   7930:     if (!$env{'request.course.id'}) {
                   7931:         $result = '<h4>'.$lt{'crl'}.'</h4>'."\n";
                   7932:     }
                   7933:     $result .= 
1.136     raeburn  7934: &Apache::loncommon::start_data_table().
                   7935: &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  7936: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
1.402     raeburn  7937: '<th>'.$lt{'ext'}.'</th><th>'."\n";
                   7938:     if ($showcredits) {
                   7939:         $result .= $lt{'crd'}.'</th>';
                   7940:     }
                   7941:     $result .=
1.375     raeburn  7942: '<th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th>'."\n".
                   7943: '<th>'.$lt{'end'}.'</th>'.
1.136     raeburn  7944: &Apache::loncommon::end_data_table_header_row().
                   7945: $table.
                   7946: &Apache::loncommon::end_data_table();
1.26      matthew  7947:     return $result;
                   7948: }
1.88      raeburn  7949: 
1.221     raeburn  7950: sub course_level_row {
1.375     raeburn  7951:     my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,
1.402     raeburn  7952:         $lt,$showcredits,$defaultcredits,$crstype) = @_;
1.375     raeburn  7953:     my $creditem;
1.222     raeburn  7954:     my $row = &Apache::loncommon::start_data_table_row().
                   7955:               ' <td><input type="checkbox" name="act_'.
                   7956:               $protectedcourse.'_'.$role.'" /></td>'."\n".
                   7957:               ' <td>'.$plrole.'</td>'."\n".
                   7958:               ' <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
1.402     raeburn  7959:     if (($showcredits) && ($role eq 'st') && ($crstype eq 'Course')) {
1.375     raeburn  7960:         $row .= 
                   7961:             '<td><input type="text" name="credits_'.$protectedcourse.'_'.
                   7962:             $role.'" size="3" value="'.$defaultcredits.'" /></td>';
                   7963:     } else {
                   7964:         $row .= '<td>&nbsp;</td>';
                   7965:     }
1.322     raeburn  7966:     if (($role eq 'cc') || ($role eq 'co')) {
1.222     raeburn  7967:         $row .= '<td>&nbsp;</td>';
1.221     raeburn  7968:     } elsif ($env{'request.course.sec'} ne '') {
1.222     raeburn  7969:         $row .= ' <td><input type="hidden" value="'.
                   7970:                 $env{'request.course.sec'}.'" '.
                   7971:                 'name="sec_'.$protectedcourse.'_'.$role.'" />'.
                   7972:                 $env{'request.course.sec'}.'</td>';
1.221     raeburn  7973:     } else {
                   7974:         if (ref($sections_count) eq 'HASH') {
                   7975:             my $currsec = 
                   7976:                 &Apache::lonuserutils::course_sections($sections_count,
                   7977:                                                        $protectedcourse.'_'.$role);
1.222     raeburn  7978:             $row .= '<td><table class="LC_createuser">'."\n".
                   7979:                     '<tr class="LC_section_row">'."\n".
                   7980:                     ' <td valign="top">'.$lt->{'exs'}.'<br />'.
                   7981:                        $currsec.'</td>'."\n".
                   7982:                      ' <td>&nbsp;&nbsp;</td>'."\n".
                   7983:                      ' <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
1.221     raeburn  7984:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
                   7985:                      '" value="" />'.
                   7986:                      '<input type="hidden" '.
                   7987:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
1.222     raeburn  7988:                      '</tr></table></td>'."\n";
1.221     raeburn  7989:         } else {
1.222     raeburn  7990:             $row .= '<td><input type="text" size="10" '.
1.375     raeburn  7991:                     'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n";
1.221     raeburn  7992:         }
                   7993:     }
1.222     raeburn  7994:     $row .= <<ENDTIMEENTRY;
                   7995: <td><input type="hidden" name="start_$protectedcourse\_$role" value="" />
1.221     raeburn  7996: <a href=
                   7997: "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  7998: <td><input type="hidden" name="end_$protectedcourse\_$role" value="" />
1.221     raeburn  7999: <a href=
                   8000: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
                   8001: ENDTIMEENTRY
1.222     raeburn  8002:     $row .= &Apache::loncommon::end_data_table_row();
                   8003:     return $row;
1.221     raeburn  8004: }
                   8005: 
1.88      raeburn  8006: sub course_level_dc {
1.375     raeburn  8007:     my ($dcdom,$showcredits) = @_;
1.190     raeburn  8008:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.213     raeburn  8009:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.88      raeburn  8010:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
                   8011:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
1.133     raeburn  8012:                       '<input type="hidden" name="dccourse" value="" />';
1.355     www      8013:     my $courseform=&Apache::loncommon::selectcourse_link
1.356     raeburn  8014:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Select','crstype');
1.375     raeburn  8015:     my $credit_elem;
                   8016:     if ($showcredits) {
                   8017:         $credit_elem = 'credits';
                   8018:     }
                   8019:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu','role','Course/Community Browser',$credit_elem);
1.88      raeburn  8020:     my %lt=&Apache::lonlocal::texthash(
                   8021:                     'rol'  => "Role",
1.113     raeburn  8022:                     'grs'  => "Section",
1.88      raeburn  8023:                     'exs'  => "Existing sections",
                   8024:                     'new'  => "Define new section", 
                   8025:                     'sta'  => "Start",
                   8026:                     'end'  => "End",
                   8027:                     'ssd'  => "Set Start Date",
1.355     www      8028:                     'sed'  => "Set End Date",
1.375     raeburn  8029:                     'scc'  => "Course/Community",
                   8030:                     'crd'  => "Credits",
1.88      raeburn  8031:                   );
1.323     raeburn  8032:     my $header = '<h4>'.&mt('Course/Community Level').'</h4>'.
1.136     raeburn  8033:                  &Apache::loncommon::start_data_table().
                   8034:                  &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  8035:                  '<th>'.$lt{'scc'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
1.397     bisitz   8036:                  '<th>'.$lt{'grs'}.'</th>'."\n";
                   8037:     $header .=   '<th>'.$lt{'crd'}.'</th>'."\n" if ($showcredits);
                   8038:     $header .=   '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'."\n".
1.136     raeburn  8039:                  &Apache::loncommon::end_data_table_header_row();
1.143     raeburn  8040:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
1.356     raeburn  8041:                      '<td><br /><span class="LC_nobreak"><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc','','','','crstype'".')" />'.
                   8042:                      $courseform.('&nbsp;' x4).'</span></td>'."\n".
1.389     bisitz   8043:                      '<td valign="top"><br /><select name="role">'."\n";
1.213     raeburn  8044:     foreach my $role (@roles) {
1.135     raeburn  8045:         my $plrole=&Apache::lonnet::plaintext($role);
1.389     bisitz   8046:         $otheritems .= '  <option value="'.$role.'">'.$plrole.'</option>';
1.88      raeburn  8047:     }
1.404     raeburn  8048:     if ( keys(%customroles) > 0) {
                   8049:         foreach my $cust (sort(keys(%customroles))) {
1.101     albertel 8050:             my $custrole='cr_cr_'.$env{'user.domain'}.
1.135     raeburn  8051:                     '_'.$env{'user.name'}.'_'.$cust;
1.389     bisitz   8052:             $otheritems .= '  <option value="'.$custrole.'">'.$cust.'</option>';
1.88      raeburn  8053:         }
                   8054:     }
                   8055:     $otheritems .= '</select></td><td>'.
                   8056:                      '<table border="0" cellspacing="0" cellpadding="0">'.
                   8057:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
1.389     bisitz   8058:                      ' <option value="">&lt;--'.&mt('Pick course first').'</option></select></td>'.
1.88      raeburn  8059:                      '<td>&nbsp;&nbsp;</td>'.
                   8060:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
1.113     raeburn  8061:                      '<input type="text" name="newsec" value="" />'.
1.237     raeburn  8062:                      '<input type="hidden" name="section" value="" />'.
1.323     raeburn  8063:                      '<input type="hidden" name="groups" value="" />'.
                   8064:                      '<input type="hidden" name="crstype" value="" /></td>'.
1.375     raeburn  8065:                      '</tr></table></td>'."\n";
                   8066:     if ($showcredits) {
                   8067:         $otheritems .= '<td><br />'."\n".
1.397     bisitz   8068:                        '<input type="text" size="3" name="credits" value="" /></td>'."\n";
1.375     raeburn  8069:     }
1.88      raeburn  8070:     $otheritems .= <<ENDTIMEENTRY;
1.323     raeburn  8071: <td><br /><input type="hidden" name="start" value='' />
1.88      raeburn  8072: <a href=
                   8073: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.323     raeburn  8074: <td><br /><input type="hidden" name="end" value='' />
1.88      raeburn  8075: <a href=
                   8076: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
                   8077: ENDTIMEENTRY
1.136     raeburn  8078:     $otheritems .= &Apache::loncommon::end_data_table_row().
                   8079:                    &Apache::loncommon::end_data_table()."\n";
1.88      raeburn  8080:     return $cb_jscript.$header.$hiddenitems.$otheritems;
                   8081: }
                   8082: 
1.237     raeburn  8083: sub update_selfenroll_config {
1.400     raeburn  8084:     my ($r,$cid,$cdom,$cnum,$context,$crstype,$currsettings) = @_;
1.398     raeburn  8085:     return unless (ref($currsettings) eq 'HASH');
                   8086:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
                   8087:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
1.237     raeburn  8088:     my (%changes,%warning);
1.241     raeburn  8089:     my $curr_types;
1.400     raeburn  8090:     my %noedit;
                   8091:     unless ($context eq 'domain') {
                   8092:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
                   8093:     }
1.237     raeburn  8094:     if (ref($row) eq 'ARRAY') {
                   8095:         foreach my $item (@{$row}) {
1.400     raeburn  8096:             next if ($noedit{$item});
1.237     raeburn  8097:             if ($item eq 'enroll_dates') {
                   8098:                 my (%currenrolldate,%newenrolldate);
                   8099:                 foreach my $type ('start','end') {
1.398     raeburn  8100:                     $currenrolldate{$type} = $currsettings->{'selfenroll_'.$type.'_date'};
1.237     raeburn  8101:                     $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
                   8102:                     if ($newenrolldate{$type} ne $currenrolldate{$type}) {
                   8103:                         $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
                   8104:                     }
                   8105:                 }
                   8106:             } elsif ($item eq 'access_dates') {
                   8107:                 my (%currdate,%newdate);
                   8108:                 foreach my $type ('start','end') {
1.398     raeburn  8109:                     $currdate{$type} = $currsettings->{'selfenroll_'.$type.'_access'};
1.237     raeburn  8110:                     $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
                   8111:                     if ($newdate{$type} ne $currdate{$type}) {
                   8112:                         $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
                   8113:                     }
                   8114:                 }
1.241     raeburn  8115:             } elsif ($item eq 'types') {
1.398     raeburn  8116:                 $curr_types = $currsettings->{'selfenroll_'.$item};
1.241     raeburn  8117:                 if ($env{'form.selfenroll_all'}) {
                   8118:                     if ($curr_types ne '*') {
                   8119:                         $changes{'internal.selfenroll_types'} = '*';
                   8120:                     } else {
                   8121:                         next;
                   8122:                     }
                   8123:                 } else {
1.249     raeburn  8124:                     my %currdoms;
1.241     raeburn  8125:                     my @entries = split(/;/,$curr_types);
                   8126:                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
1.249     raeburn  8127:                     my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
1.241     raeburn  8128:                     my $newnum = 0;
1.249     raeburn  8129:                     my @latesttypes;
                   8130:                     foreach my $num (@activations) {
                   8131:                         my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
                   8132:                         if (@types > 0) {
1.241     raeburn  8133:                             @types = sort(@types);
                   8134:                             my $typestr = join(',',@types);
1.249     raeburn  8135:                             my $typedom = $env{'form.selfenroll_dom_'.$num};
                   8136:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   8137:                             $currdoms{$typedom} = 1;
1.241     raeburn  8138:                             $newnum ++;
                   8139:                         }
                   8140:                     }
1.338     raeburn  8141:                     for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {
                   8142:                         if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
1.249     raeburn  8143:                             my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
                   8144:                             if (@types > 0) {
                   8145:                                 @types = sort(@types);
                   8146:                                 my $typestr = join(',',@types);
                   8147:                                 my $typedom = $env{'form.selfenroll_dom_'.$j};
                   8148:                                 $latesttypes[$newnum] = $typedom.':'.$typestr;
                   8149:                                 $currdoms{$typedom} = 1;
                   8150:                                 $newnum ++;
                   8151:                             }
                   8152:                         }
                   8153:                     }
                   8154:                     if ($env{'form.selfenroll_newdom'} ne '') {
                   8155:                         my $typedom = $env{'form.selfenroll_newdom'};
                   8156:                         if ((!defined($currdoms{$typedom})) && 
                   8157:                             (&Apache::lonnet::domain($typedom) ne '')) {
                   8158:                             my $typestr;
                   8159:                             my ($othertitle,$usertypes,$types) = 
                   8160:                                 &Apache::loncommon::sorted_inst_types($typedom);
                   8161:                             my $othervalue = 'any';
                   8162:                             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   8163:                                 if (@{$types} > 0) {
1.257     raeburn  8164:                                     my @esc_types = map { &escape($_); } @{$types};
1.249     raeburn  8165:                                     $othervalue = 'other';
1.258     raeburn  8166:                                     $typestr = join(',',(@esc_types,$othervalue));
1.249     raeburn  8167:                                 }
                   8168:                                 $typestr = $othervalue;
                   8169:                             } else {
                   8170:                                 $typestr = $othervalue;
                   8171:                             } 
                   8172:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   8173:                             $newnum ++ ;
                   8174:                         }
                   8175:                     }
1.241     raeburn  8176:                     my $selfenroll_types = join(';',@latesttypes);
                   8177:                     if ($selfenroll_types ne $curr_types) {
                   8178:                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
                   8179:                     }
                   8180:                 }
1.276     raeburn  8181:             } elsif ($item eq 'limit') {
                   8182:                 my $newlimit = $env{'form.selfenroll_limit'};
                   8183:                 my $newcap = $env{'form.selfenroll_cap'};
                   8184:                 $newcap =~s/\s+//g;
1.398     raeburn  8185:                 my $currlimit =  $currsettings->{'selfenroll_limit'};
1.276     raeburn  8186:                 $currlimit = 'none' if ($currlimit eq '');
1.398     raeburn  8187:                 my $currcap = $currsettings->{'selfenroll_cap'};
1.276     raeburn  8188:                 if ($newlimit ne $currlimit) {
                   8189:                     if ($newlimit ne 'none') {
                   8190:                         if ($newcap =~ /^\d+$/) {
                   8191:                             if ($newcap ne $currcap) {
                   8192:                                 $changes{'internal.selfenroll_cap'} = $newcap;
                   8193:                             }
                   8194:                             $changes{'internal.selfenroll_limit'} = $newlimit;
                   8195:                         } else {
1.398     raeburn  8196:                             $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
                   8197:                                 &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.'); 
1.276     raeburn  8198:                         }
                   8199:                     } elsif ($currcap ne '') {
                   8200:                         $changes{'internal.selfenroll_cap'} = '';
                   8201:                         $changes{'internal.selfenroll_limit'} = $newlimit; 
                   8202:                     }
                   8203:                 } elsif ($currlimit ne 'none') {
                   8204:                     if ($newcap =~ /^\d+$/) {
                   8205:                         if ($newcap ne $currcap) {
                   8206:                             $changes{'internal.selfenroll_cap'} = $newcap;
                   8207:                         }
                   8208:                     } else {
1.398     raeburn  8209:                         $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
                   8210:                             &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.');
1.276     raeburn  8211:                     }
                   8212:                 }
                   8213:             } elsif ($item eq 'approval') {
                   8214:                 my (@currnotified,@newnotified);
1.398     raeburn  8215:                 my $currapproval = $currsettings->{'selfenroll_approval'};
                   8216:                 my $currnotifylist = $currsettings->{'selfenroll_notifylist'};
1.276     raeburn  8217:                 if ($currnotifylist ne '') {
                   8218:                     @currnotified = split(/,/,$currnotifylist);
                   8219:                     @currnotified = sort(@currnotified);
                   8220:                 }
                   8221:                 my $newapproval = $env{'form.selfenroll_approval'};
                   8222:                 @newnotified = &Apache::loncommon::get_env_multiple('form.selfenroll_notify');
                   8223:                 @newnotified = sort(@newnotified);
                   8224:                 if ($newapproval ne $currapproval) {
                   8225:                     $changes{'internal.selfenroll_approval'} = $newapproval;
                   8226:                     if (!$newapproval) {
                   8227:                         if ($currnotifylist ne '') {
                   8228:                             $changes{'internal.selfenroll_notifylist'} = '';
                   8229:                         }
                   8230:                     } else {
                   8231:                         my @differences =  
1.295     raeburn  8232:                             &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  8233:                         if (@differences > 0) {
                   8234:                             if (@newnotified > 0) {
                   8235:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   8236:                             } else {
                   8237:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   8238:                             }
                   8239:                         }
                   8240:                     }
                   8241:                 } else {
1.295     raeburn  8242:                     my @differences = &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  8243:                     if (@differences > 0) {
                   8244:                         if (@newnotified > 0) {
                   8245:                             $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   8246:                         } else {
                   8247:                             $changes{'internal.selfenroll_notifylist'} = '';
                   8248:                         }
                   8249:                     }
                   8250:                 }
1.237     raeburn  8251:             } else {
1.398     raeburn  8252:                 my $curr_val = $currsettings->{'selfenroll_'.$item};
1.237     raeburn  8253:                 my $newval = $env{'form.selfenroll_'.$item};
                   8254:                 if ($item eq 'section') {
                   8255:                     $newval = $env{'form.sections'};
1.241     raeburn  8256:                     if (defined($curr_groups{$newval})) {
1.237     raeburn  8257:                         $newval = $curr_val;
1.398     raeburn  8258:                         $warning{$item} = &mt('Section for self-enrolled users unchanged as the proposed section is a group').'<br />'.
                   8259:                                           &mt('Group names and section names must be distinct');
1.237     raeburn  8260:                     } elsif ($newval eq 'all') {
                   8261:                         $newval = $curr_val;
1.274     bisitz   8262:                         $warning{$item} = &mt('Section for self-enrolled users unchanged, as "all" is a reserved section name.');
1.237     raeburn  8263:                     }
                   8264:                     if ($newval eq '') {
                   8265:                         $newval = 'none';
                   8266:                     }
                   8267:                 }
                   8268:                 if ($newval ne $curr_val) {
                   8269:                     $changes{'internal.selfenroll_'.$item} = $newval;
                   8270:                 }
1.241     raeburn  8271:             }
1.237     raeburn  8272:         }
                   8273:         if (keys(%warning) > 0) {
                   8274:             foreach my $item (@{$row}) {
                   8275:                 if (exists($warning{$item})) {
                   8276:                     $r->print($warning{$item}.'<br />');
                   8277:                 }
                   8278:             } 
                   8279:         }
                   8280:         if (keys(%changes) > 0) {
                   8281:             my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
                   8282:             if ($putresult eq 'ok') {
                   8283:                 if ((exists($changes{'internal.selfenroll_types'})) ||
                   8284:                     (exists($changes{'internal.selfenroll_start_date'}))  ||
                   8285:                     (exists($changes{'internal.selfenroll_end_date'}))) {
                   8286:                     my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
                   8287:                                                                 $cnum,undef,undef,'Course');
                   8288:                     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
1.398     raeburn  8289:                     if (ref($crsinfo{$cid}) eq 'HASH') {
1.237     raeburn  8290:                         foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
                   8291:                             if (exists($changes{'internal.'.$item})) {
1.398     raeburn  8292:                                 $crsinfo{$cid}{$item} = $changes{'internal.'.$item};
1.237     raeburn  8293:                             }
                   8294:                         }
                   8295:                         my $crsputresult =
                   8296:                             &Apache::lonnet::courseidput($cdom,\%crsinfo,
                   8297:                                                          $chome,'notime');
                   8298:                     }
                   8299:                 }
                   8300:                 $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
                   8301:                 foreach my $item (@{$row}) {
                   8302:                     my $title = $item;
                   8303:                     if (ref($lt) eq 'HASH') {
                   8304:                         $title = $lt->{$item};
                   8305:                     }
                   8306:                     if ($item eq 'enroll_dates') {
                   8307:                         foreach my $type ('start','end') {
                   8308:                             if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
                   8309:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
1.244     bisitz   8310:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  8311:                                           $title,$type,$newdate).'</li>');
                   8312:                             }
                   8313:                         }
                   8314:                     } elsif ($item eq 'access_dates') {
                   8315:                         foreach my $type ('start','end') {
                   8316:                             if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
                   8317:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
1.244     bisitz   8318:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  8319:                                           $title,$type,$newdate).'</li>');
                   8320:                             }
                   8321:                         }
1.276     raeburn  8322:                     } elsif ($item eq 'limit') {
                   8323:                         if ((exists($changes{'internal.selfenroll_limit'})) ||
                   8324:                             (exists($changes{'internal.selfenroll_cap'}))) {
                   8325:                             my ($newval,$newcap);
                   8326:                             if ($changes{'internal.selfenroll_cap'} ne '') {
                   8327:                                 $newcap = $changes{'internal.selfenroll_cap'}
                   8328:                             } else {
1.398     raeburn  8329:                                 $newcap = $currsettings->{'selfenroll_cap'};
1.276     raeburn  8330:                             }
                   8331:                             if ($changes{'internal.selfenroll_limit'} eq 'none') {
                   8332:                                 $newval = &mt('No limit');
                   8333:                             } elsif ($changes{'internal.selfenroll_limit'} eq 
                   8334:                                      'allstudents') {
                   8335:                                 $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   8336:                             } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
                   8337:                                 $newval = &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
                   8338:                             } else {
1.398     raeburn  8339:                                 my $currlimit =  $currsettings->{'selfenroll_limit'};
1.276     raeburn  8340:                                 if ($currlimit eq 'allstudents') {
                   8341:                                     $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   8342:                                 } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
1.308     raeburn  8343:                                     $newval =  &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
1.276     raeburn  8344:                                 }
                   8345:                             }
                   8346:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
                   8347:                         }
                   8348:                     } elsif ($item eq 'approval') {
                   8349:                         if ((exists($changes{'internal.selfenroll_approval'})) ||
                   8350:                             (exists($changes{'internal.selfenroll_notifylist'}))) {
1.398     raeburn  8351:                             my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
1.276     raeburn  8352:                             my ($newval,$newnotify);
                   8353:                             if (exists($changes{'internal.selfenroll_notifylist'})) {
                   8354:                                 $newnotify = $changes{'internal.selfenroll_notifylist'};
                   8355:                             } else {   
1.398     raeburn  8356:                                 $newnotify = $currsettings->{'selfenroll_notifylist'};
1.276     raeburn  8357:                             }
1.398     raeburn  8358:                             if (exists($changes{'internal.selfenroll_approval'})) {
                   8359:                                 if ($changes{'internal.selfenroll_approval'} !~ /^[012]$/) {
                   8360:                                     $changes{'internal.selfenroll_approval'} = '0';
                   8361:                                 }
                   8362:                                 $newval = $selfdescs{'approval'}{$changes{'internal.selfenroll_approval'}};
1.276     raeburn  8363:                             } else {
1.398     raeburn  8364:                                 my $currapproval = $currsettings->{'selfenroll_approval'}; 
                   8365:                                 if ($currapproval !~ /^[012]$/) {
                   8366:                                     $currapproval = 0;
1.276     raeburn  8367:                                 }
1.398     raeburn  8368:                                 $newval = $selfdescs{'approval'}{$currapproval};
1.276     raeburn  8369:                             }
                   8370:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval));
                   8371:                             if ($newnotify) {
1.277     raeburn  8372:                                 $r->print('<br />'.&mt('The following will be notified when an enrollment request needs approval, or has been approved: [_1].',$newnotify));
1.276     raeburn  8373:                             } else {
1.277     raeburn  8374:                                 $r->print('<br />'.&mt('No notifications sent when an enrollment request needs approval, or has been approved.'));
1.276     raeburn  8375:                             }
                   8376:                             $r->print('</li>'."\n");
                   8377:                         }
1.237     raeburn  8378:                     } else {
                   8379:                         if (exists($changes{'internal.selfenroll_'.$item})) {
1.241     raeburn  8380:                             my $newval = $changes{'internal.selfenroll_'.$item};
                   8381:                             if ($item eq 'types') {
                   8382:                                 if ($newval eq '') {
                   8383:                                     $newval = &mt('None');
                   8384:                                 } elsif ($newval eq '*') {
                   8385:                                     $newval = &mt('Any user in any domain');
                   8386:                                 }
1.245     raeburn  8387:                             } elsif ($item eq 'registered') {
                   8388:                                 if ($newval eq '1') {
                   8389:                                     $newval = &mt('Yes');
                   8390:                                 } elsif ($newval eq '0') {
                   8391:                                     $newval = &mt('No');
                   8392:                                 }
1.241     raeburn  8393:                             }
1.244     bisitz   8394:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
1.237     raeburn  8395:                         }
                   8396:                     }
                   8397:                 }
                   8398:                 $r->print('</ul>');
1.398     raeburn  8399:                 if ($env{'course.'.$cid.'.description'} ne '') {
                   8400:                     my %newenvhash;
                   8401:                     foreach my $key (keys(%changes)) {
                   8402:                         $newenvhash{'course.'.$cid.'.'.$key} = $changes{$key};
                   8403:                     }
                   8404:                     &Apache::lonnet::appenv(\%newenvhash);
1.237     raeburn  8405:                 }
                   8406:             } else {
1.398     raeburn  8407:                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.
                   8408:                           &mt('The error was: [_1].',$putresult));
1.237     raeburn  8409:             }
                   8410:         } else {
1.249     raeburn  8411:             $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.237     raeburn  8412:         }
                   8413:     } else {
1.249     raeburn  8414:         $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.241     raeburn  8415:     }
1.400     raeburn  8416:     my $visactions = &cat_visibility();
                   8417:     my ($cathash,%cattype);
                   8418:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   8419:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   8420:         $cathash = $domconfig{'coursecategories'}{'cats'};
                   8421:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
                   8422:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
                   8423:     } else {
                   8424:         $cathash = {};
                   8425:         $cattype{'auth'} = 'std';
                   8426:         $cattype{'unauth'} = 'std';
                   8427:     }
                   8428:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
                   8429:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   8430:                   '<br />'.
                   8431:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   8432:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
                   8433:                   '</ul>');
                   8434:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
                   8435:         if ($currsettings->{'uniquecode'}) {
                   8436:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
                   8437:         } else {
1.366     bisitz   8438:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
1.400     raeburn  8439:                   '<br />'.
                   8440:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   8441:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
                   8442:                   '</ul><br />');
                   8443:         }
                   8444:     } else {
                   8445:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
                   8446:         if (ref($visactions) eq 'HASH') {
                   8447:             if (!$visible) {
                   8448:                 $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   8449:                           '<br />');
                   8450:                 if (ref($vismsgs) eq 'ARRAY') {
                   8451:                     $r->print('<br />'.$visactions->{'take'}.'<ul>');
                   8452:                     foreach my $item (@{$vismsgs}) {
                   8453:                         $r->print('<li>'.$visactions->{$item}.'</li>');
                   8454:                     }
                   8455:                     $r->print('</ul>');
1.256     raeburn  8456:                 }
1.400     raeburn  8457:                 $r->print($cansetvis);
1.256     raeburn  8458:             }
                   8459:         }
                   8460:     } 
1.237     raeburn  8461:     return;
                   8462: }
                   8463: 
1.27      matthew  8464: #---------------------------------------------- end functions for &phase_two
1.29      matthew  8465: 
                   8466: #--------------------------------- functions for &phase_two and &phase_three
                   8467: 
                   8468: #--------------------------end of functions for &phase_two and &phase_three
1.372     raeburn  8469: 
1.1       www      8470: 1;
                   8471: __END__
1.2       www      8472: 
                   8473: 

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