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

1.20      harris41    1: # The LearningOnline Network with CAPA
1.1       www         2: # Create a user
                      3: #
1.418   ! raeburn     4: # $Id: loncreateuser.pm,v 1.417 2016/10/22 17:56:12 raeburn Exp $
1.22      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.20      harris41   28: ###
                     29: 
1.1       www        30: package Apache::loncreateuser;
1.66      bowersj2   31: 
                     32: =pod
                     33: 
                     34: =head1 NAME
                     35: 
1.263     jms        36: Apache::loncreateuser.pm
1.66      bowersj2   37: 
                     38: =head1 SYNOPSIS
                     39: 
1.263     jms        40:     Handler to create users and custom roles
                     41: 
                     42:     Provides an Apache handler for creating users,
1.66      bowersj2   43:     editing their login parameters, roles, and removing roles, and
                     44:     also creating and assigning custom roles.
                     45: 
                     46: =head1 OVERVIEW
                     47: 
                     48: =head2 Custom Roles
                     49: 
                     50: In LON-CAPA, roles are actually collections of privileges. "Teaching
                     51: Assistant", "Course Coordinator", and other such roles are really just
                     52: collection of privileges that are useful in many circumstances.
                     53: 
1.324     raeburn    54: Custom roles can be defined by a Domain Coordinator, Course Coordinator
                     55: or Community Coordinator via the Manage User functionality.
                     56: The custom role editor screen will show all privileges which can be
                     57: assigned to users. For a complete list of privileges, please see 
                     58: C</home/httpd/lonTabs/rolesplain.tab>.
1.66      bowersj2   59: 
1.324     raeburn    60: Custom role definitions are stored in the C<roles.db> file of the creator
                     61: of the role.
1.66      bowersj2   62: 
                     63: =cut
1.1       www        64: 
                     65: use strict;
                     66: use Apache::Constants qw(:common :http);
                     67: use Apache::lonnet;
1.54      bowersj2   68: use Apache::loncommon;
1.68      www        69: use Apache::lonlocal;
1.117     raeburn    70: use Apache::longroup;
1.190     raeburn    71: use Apache::lonuserutils;
1.307     raeburn    72: use Apache::loncoursequeueadmin;
1.139     albertel   73: use LONCAPA qw(:DEFAULT :match);
1.1       www        74: 
1.20      harris41   75: my $loginscript; # piece of javascript used in two separate instances
                     76: my $authformnop;
                     77: my $authformkrb;
                     78: my $authformint;
                     79: my $authformfsys;
                     80: my $authformloc;
                     81: 
1.94      matthew    82: sub initialize_authen_forms {
1.227     raeburn    83:     my ($dom,$formname,$curr_authtype,$mode) = @_;
                     84:     my ($krbdef,$krbdefdom) = &Apache::loncommon::get_kerberos_defaults($dom);
                     85:     my %param = ( formname => $formname,
1.187     raeburn    86:                   kerb_def_dom => $krbdefdom,
1.227     raeburn    87:                   kerb_def_auth => $krbdef,
1.187     raeburn    88:                   domain => $dom,
                     89:                 );
1.188     raeburn    90:     my %abv_auth = &auth_abbrev();
1.227     raeburn    91:     if ($curr_authtype =~ /^(krb4|krb5|internal|localauth|unix):(.*)$/) {
1.188     raeburn    92:         my $long_auth = $1;
1.227     raeburn    93:         my $curr_autharg = $2;
1.188     raeburn    94:         my %abv_auth = &auth_abbrev();
                     95:         $param{'curr_authtype'} = $abv_auth{$long_auth};
                     96:         if ($long_auth =~ /^krb(4|5)$/) {
                     97:             $param{'curr_kerb_ver'} = $1;
1.227     raeburn    98:             $param{'curr_autharg'} = $curr_autharg;
1.188     raeburn    99:         }
1.205     raeburn   100:         if ($mode eq 'modifyuser') {
                    101:             $param{'mode'} = $mode;
                    102:         }
1.187     raeburn   103:     }
1.227     raeburn   104:     $loginscript  = &Apache::loncommon::authform_header(%param);
                    105:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
1.31      matthew   106:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
                    107:     $authformint  = &Apache::loncommon::authform_internal(%param);
                    108:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
                    109:     $authformloc  = &Apache::loncommon::authform_local(%param);
1.20      harris41  110: }
                    111: 
1.188     raeburn   112: sub auth_abbrev {
                    113:     my %abv_auth = (
1.368     raeburn   114:                      krb5      => 'krb',
                    115:                      krb4      => 'krb',
                    116:                      internal  => 'int',
                    117:                      localauth => 'loc',
                    118:                      unix      => 'fsys',
1.188     raeburn   119:                    );
                    120:     return %abv_auth;
                    121: }
1.43      www       122: 
1.134     raeburn   123: # ====================================================
                    124: 
1.378     raeburn   125: sub user_quotas {
1.134     raeburn   126:     my ($ccuname,$ccdomain) = @_;
                    127:     my %lt = &Apache::lonlocal::texthash(
1.267     raeburn   128:                    'usrt'      => "User Tools",
                    129:                    'cust'      => "Custom quota",
                    130:                    'chqu'      => "Change quota",
1.134     raeburn   131:     );
1.378     raeburn   132:    
1.149     raeburn   133:     my $quota_javascript = <<"END_SCRIPT";
                    134: <script type="text/javascript">
1.301     bisitz    135: // <![CDATA[
1.378     raeburn   136: function quota_changes(caller,context) {
                    137:     var customoff = document.getElementById('custom_'+context+'quota_off');
                    138:     var customon = document.getElementById('custom_'+context+'quota_on');
                    139:     var number = document.getElementById(context+'quota');
1.149     raeburn   140:     if (caller == "custom") {
1.378     raeburn   141:         if (customoff) {
                    142:             if (customoff.checked) {
                    143:                 number.value = "";
                    144:             }
1.149     raeburn   145:         }
                    146:     }
                    147:     if (caller == "quota") {
1.378     raeburn   148:         if (customon) {
                    149:             customon.checked = true;
                    150:         }
1.149     raeburn   151:     }
1.378     raeburn   152:     return;
1.149     raeburn   153: }
1.301     bisitz    154: // ]]>
1.149     raeburn   155: </script>
                    156: END_SCRIPT
1.378     raeburn   157:     my $longinsttype;
                    158:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
1.267     raeburn   159:     my $output = $quota_javascript."\n".
                    160:                  '<h3>'.$lt{'usrt'}.'</h3>'."\n".
                    161:                  &Apache::loncommon::start_data_table();
                    162: 
1.418   ! raeburn   163:     if ((&Apache::lonnet::allowed('mut',$ccdomain)) ||
        !           164:         (&Apache::lonnet::allowed('udp',$ccdomain))) {
1.275     raeburn   165:         $output .= &build_tools_display($ccuname,$ccdomain,'tools');
1.267     raeburn   166:     }
1.378     raeburn   167: 
                    168:     my %titles = &Apache::lonlocal::texthash (
                    169:                     portfolio => "Disk space allocated to user's portfolio files",
1.385     bisitz    170:                     author    => "Disk space allocated to user's Authoring Space (if role assigned)",
1.378     raeburn   171:                  );
                    172:     foreach my $name ('portfolio','author') {
                    173:         my ($currquota,$quotatype,$inststatus,$defquota) =
                    174:             &Apache::loncommon::get_user_quota($ccuname,$ccdomain,$name);
                    175:         if ($longinsttype eq '') { 
                    176:             if ($inststatus ne '') {
                    177:                 if ($usertypes->{$inststatus} ne '') {
                    178:                     $longinsttype = $usertypes->{$inststatus};
                    179:                 }
                    180:             }
                    181:         }
                    182:         my ($showquota,$custom_on,$custom_off,$defaultinfo);
                    183:         $custom_on = ' ';
                    184:         $custom_off = ' checked="checked" ';
                    185:         if ($quotatype eq 'custom') {
                    186:             $custom_on = $custom_off;
                    187:             $custom_off = ' ';
                    188:             $showquota = $currquota;
                    189:             if ($longinsttype eq '') {
                    190:                 $defaultinfo = &mt('For this user, the default quota would be [_1]'
1.383     raeburn   191:                               .' MB.',$defquota);
1.378     raeburn   192:             } else {
                    193:                 $defaultinfo = &mt("For this user, the default quota would be [_1]".
1.383     raeburn   194:                                    " MB, as determined by the user's institutional".
1.378     raeburn   195:                                    " affiliation ([_2]).",$defquota,$longinsttype);
                    196:             }
                    197:         } else {
                    198:             if ($longinsttype eq '') {
                    199:                 $defaultinfo = &mt('For this user, the default quota is [_1]'
1.383     raeburn   200:                               .' MB.',$defquota);
1.378     raeburn   201:             } else {
                    202:                 $defaultinfo = &mt("For this user, the default quota of [_1]".
1.383     raeburn   203:                                    " MB, is determined by the user's institutional".
1.378     raeburn   204:                                    " affiliation ([_2]).",$defquota,$longinsttype);
                    205:             }
                    206:         }
                    207: 
                    208:         if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
                    209:             $output .= '<tr class="LC_info_row">'."\n".
                    210:                        '    <td>'.$titles{$name}.'</td>'."\n".
                    211:                        '  </tr>'."\n".
                    212:                        &Apache::loncommon::start_data_table_row()."\n".
1.390     bisitz    213:                        '  <td><span class="LC_nobreak">'.
                    214:                        &mt('Current quota: [_1] MB',$currquota).'</span>&nbsp;&nbsp;'.
1.378     raeburn   215:                        $defaultinfo.'</td>'."\n".
                    216:                        &Apache::loncommon::end_data_table_row()."\n".
                    217:                        &Apache::loncommon::start_data_table_row()."\n".
                    218:                        '  <td><span class="LC_nobreak">'.$lt{'chqu'}.
                    219:                        ': <label>'.
                    220:                        '<input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_off" '.
1.379     raeburn   221:                        'value="0" '.$custom_off.' onchange="javascript:quota_changes('."'custom','$name'".');"'.
1.390     bisitz    222:                        ' /><span class="LC_nobreak">'.
                    223:                        &mt('Default ([_1] MB)',$defquota).'</span></label>&nbsp;'.
1.378     raeburn   224:                        '&nbsp;<label><input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_on" '.
1.379     raeburn   225:                        'value="1" '.$custom_on.'  onchange="javascript:quota_changes('."'custom','$name'".');"'.
1.378     raeburn   226:                        ' />'.$lt{'cust'}.':</label>&nbsp;'.
1.379     raeburn   227:                        '<input type="text" name="'.$name.'quota" id="'.$name.'quota" size ="5" '.
                    228:                        'value="'.$showquota.'" onfocus="javascript:quota_changes('."'quota','$name'".');"'.
1.390     bisitz    229:                        ' />&nbsp;'.&mt('MB').'</span></td>'."\n".
1.378     raeburn   230:                        &Apache::loncommon::end_data_table_row()."\n";
                    231:         }
                    232:     }
1.267     raeburn   233:     $output .= &Apache::loncommon::end_data_table();
1.134     raeburn   234:     return $output;
                    235: }
                    236: 
1.275     raeburn   237: sub build_tools_display {
                    238:     my ($ccuname,$ccdomain,$context) = @_;
1.306     raeburn   239:     my (@usertools,%userenv,$output,@options,%validations,%reqtitles,%reqdisplay,
1.332     raeburn   240:         $colspan,$isadv,%domconfig);
1.275     raeburn   241:     my %lt = &Apache::lonlocal::texthash (
                    242:                    'blog'       => "Personal User Blog",
                    243:                    'aboutme'    => "Personal Information Page",
1.385     bisitz    244:                    'webdav'     => "WebDAV access to Authoring Spaces (if SSL and author/co-author)",
1.275     raeburn   245:                    'portfolio'  => "Personal User Portfolio",
                    246:                    'avai'       => "Available",
                    247:                    'cusa'       => "availability",
                    248:                    'chse'       => "Change setting",
                    249:                    'usde'       => "Use default",
                    250:                    'uscu'       => "Use custom",
                    251:                    'official'   => 'Can request creation of official courses',
1.299     raeburn   252:                    'unofficial' => 'Can request creation of unofficial courses',
                    253:                    'community'  => 'Can request creation of communities',
1.384     raeburn   254:                    'textbook'   => 'Can request creation of textbook courses',
1.411     raeburn   255:                    'placement'  => 'Can request creation of placement tests',
1.362     raeburn   256:                    'requestauthor'  => 'Can request author space',
1.275     raeburn   257:     );
1.279     raeburn   258:     if ($context eq 'requestcourses') {
1.275     raeburn   259:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.299     raeburn   260:                       'requestcourses.official','requestcourses.unofficial',
1.411     raeburn   261:                       'requestcourses.community','requestcourses.textbook',
                    262:                       'requestcourses.placement');
                    263:         @usertools = ('official','unofficial','community','textbook','placement');
1.309     raeburn   264:         @options =('norequest','approval','autolimit','validate');
1.306     raeburn   265:         %validations = &Apache::lonnet::auto_courserequest_checks($ccdomain);
                    266:         %reqtitles = &courserequest_titles();
                    267:         %reqdisplay = &courserequest_display();
                    268:         $colspan = ' colspan="2"';
1.332     raeburn   269:         %domconfig =
                    270:             &Apache::lonnet::get_dom('configuration',['requestcourses'],$ccdomain);
1.418   ! raeburn   271:         $isadv = &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
1.362     raeburn   272:     } elsif ($context eq 'requestauthor') {
                    273:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    274:                                                     'requestauthor');
                    275:         @usertools = ('requestauthor');
                    276:         @options =('norequest','approval','automatic');
                    277:         %reqtitles = &requestauthor_titles();
                    278:         %reqdisplay = &requestauthor_display();
                    279:         $colspan = ' colspan="2"';
                    280:         %domconfig =
                    281:             &Apache::lonnet::get_dom('configuration',['requestauthor'],$ccdomain);
1.275     raeburn   282:     } else {
                    283:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.361     raeburn   284:                           'tools.aboutme','tools.portfolio','tools.blog',
                    285:                           'tools.webdav');
                    286:         @usertools = ('aboutme','blog','webdav','portfolio');
1.275     raeburn   287:     }
                    288:     foreach my $item (@usertools) {
1.306     raeburn   289:         my ($custom_access,$curr_access,$cust_on,$cust_off,$tool_on,$tool_off,
                    290:             $currdisp,$custdisp,$custradio);
1.275     raeburn   291:         $cust_off = 'checked="checked" ';
                    292:         $tool_on = 'checked="checked" ';
                    293:         $curr_access =  
                    294:             &Apache::lonnet::usertools_access($ccuname,$ccdomain,$item,undef,
                    295:                                               $context);
1.362     raeburn   296:         if ($context eq 'requestauthor') {
                    297:             if ($userenv{$context} ne '') {
                    298:                 $cust_on = ' checked="checked" ';
                    299:                 $cust_off = '';
                    300:             }  
                    301:         } elsif ($userenv{$context.'.'.$item} ne '') {
1.306     raeburn   302:             $cust_on = ' checked="checked" ';
                    303:             $cust_off = '';
                    304:         }
                    305:         if ($context eq 'requestcourses') {
                    306:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   307:                 $custom_access = &mt('Currently from default setting.');
1.306     raeburn   308:             } else {
                    309:                 $custom_access = &mt('Currently from custom setting.');
1.275     raeburn   310:             }
1.362     raeburn   311:         } elsif ($context eq 'requestauthor') {
                    312:             if ($userenv{$context} eq '') {
                    313:                 $custom_access = &mt('Currently from default setting.');
                    314:             } else {
                    315:                 $custom_access = &mt('Currently from custom setting.');
                    316:             }
1.275     raeburn   317:         } else {
1.306     raeburn   318:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   319:                 $custom_access =
1.306     raeburn   320:                     &mt('Availability determined currently from default setting.');
                    321:                 if (!$curr_access) {
                    322:                     $tool_off = 'checked="checked" ';
                    323:                     $tool_on = '';
                    324:                 }
                    325:             } else {
1.314     raeburn   326:                 $custom_access =
1.306     raeburn   327:                     &mt('Availability determined currently from custom setting.');
                    328:                 if ($userenv{$context.'.'.$item} == 0) {
                    329:                     $tool_off = 'checked="checked" ';
                    330:                     $tool_on = '';
                    331:                 }
1.275     raeburn   332:             }
                    333:         }
                    334:         $output .= '  <tr class="LC_info_row">'."\n".
1.306     raeburn   335:                    '   <td'.$colspan.'>'.$lt{$item}.'</td>'."\n".
1.275     raeburn   336:                    '  </tr>'."\n".
1.306     raeburn   337:                    &Apache::loncommon::start_data_table_row()."\n";
1.418   ! raeburn   338:   
1.362     raeburn   339:         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.306     raeburn   340:             my ($curroption,$currlimit);
1.362     raeburn   341:             my $envkey = $context.'.'.$item;
                    342:             if ($context eq 'requestauthor') {
                    343:                 $envkey = $context;
                    344:             }
                    345:             if ($userenv{$envkey} ne '') {
                    346:                 $curroption = $userenv{$envkey};
1.332     raeburn   347:             } else {
                    348:                 my (@inststatuses);
1.362     raeburn   349:                 if ($context eq 'requestcourses') {
                    350:                     $curroption =
                    351:                         &Apache::loncoursequeueadmin::get_processtype('course',$ccuname,$ccdomain,
                    352:                                                                       $isadv,$ccdomain,$item,
                    353:                                                                       \@inststatuses,\%domconfig);
                    354:                 } else {
                    355:                      $curroption = 
                    356:                          &Apache::loncoursequeueadmin::get_processtype('requestauthor',$ccuname,$ccdomain,
                    357:                                                                        $isadv,$ccdomain,undef,
                    358:                                                                        \@inststatuses,\%domconfig);
                    359:                 }
1.332     raeburn   360:             }
1.306     raeburn   361:             if (!$curroption) {
                    362:                 $curroption = 'norequest';
                    363:             }
                    364:             if ($curroption =~ /^autolimit=(\d*)$/) {
                    365:                 $currlimit = $1;
1.314     raeburn   366:                 if ($currlimit eq '') {
                    367:                     $currdisp = &mt('Yes, automatic creation');
                    368:                 } else {
                    369:                     $currdisp = &mt('Yes, up to [quant,_1,request]/user',$currlimit);
                    370:                 }
1.306     raeburn   371:             } else {
                    372:                 $currdisp = $reqdisplay{$curroption};
                    373:             }
                    374:             $custdisp = '<table>';
                    375:             foreach my $option (@options) {
                    376:                 my $val = $option;
                    377:                 if ($option eq 'norequest') {
                    378:                     $val = 0;
                    379:                 }
                    380:                 if ($option eq 'validate') {
                    381:                     my $canvalidate = 0;
                    382:                     if (ref($validations{$item}) eq 'HASH') {
                    383:                         if ($validations{$item}{'_custom_'}) {
                    384:                             $canvalidate = 1;
                    385:                         }
                    386:                     }
                    387:                     next if (!$canvalidate);
                    388:                 }
                    389:                 my $checked = '';
                    390:                 if ($option eq $curroption) {
                    391:                     $checked = ' checked="checked"';
                    392:                 } elsif ($option eq 'autolimit') {
                    393:                     if ($curroption =~ /^autolimit/) {
                    394:                         $checked = ' checked="checked"';
                    395:                     }
                    396:                 }
1.362     raeburn   397:                 my $name = 'crsreq_'.$item;
                    398:                 if ($context eq 'requestauthor') {
                    399:                     $name = $item;
                    400:                 }
1.306     raeburn   401:                 $custdisp .= '<tr><td><span class="LC_nobreak"><label>'.
1.362     raeburn   402:                              '<input type="radio" name="'.$name.'" '.
                    403:                              'value="'.$val.'"'.$checked.' />'.
1.306     raeburn   404:                              $reqtitles{$option}.'</label>&nbsp;';
                    405:                 if ($option eq 'autolimit') {
1.362     raeburn   406:                     $custdisp .= '<input type="text" name="'.$name.
                    407:                                  '_limit" size="1" '.
1.314     raeburn   408:                                  'value="'.$currlimit.'" /></span><br />'.
                    409:                                  $reqtitles{'unlimited'};
1.362     raeburn   410:                 } else {
                    411:                     $custdisp .= '</span>';
                    412:                 }
                    413:                 $custdisp .= '</td></tr>';
1.306     raeburn   414:             }
                    415:             $custdisp .= '</table>';
                    416:             $custradio = '</span></td><td>'.&mt('Custom setting').'<br />'.$custdisp;
                    417:         } else {
                    418:             $currdisp = ($curr_access?&mt('Yes'):&mt('No'));
1.362     raeburn   419:             my $name = $context.'_'.$item;
                    420:             if ($context eq 'requestauthor') {
                    421:                 $name = $context;
                    422:             }
1.306     raeburn   423:             $custdisp = '<span class="LC_nobreak"><label>'.
1.362     raeburn   424:                         '<input type="radio" name="'.$name.'"'.
1.361     raeburn   425:                         ' value="1" '.$tool_on.'/>'.&mt('On').'</label>&nbsp;<label>'.
1.362     raeburn   426:                         '<input type="radio" name="'.$name.'" value="0" '.
1.306     raeburn   427:                         $tool_off.'/>'.&mt('Off').'</label></span>';
                    428:             $custradio = ('&nbsp;'x2).'--'.$lt{'cusa'}.':&nbsp;'.$custdisp.
                    429:                           '</span>';
                    430:         }
                    431:         $output .= '  <td'.$colspan.'>'.$custom_access.('&nbsp;'x4).
1.418   ! raeburn   432:                    $lt{'avai'}.': '.$currdisp.'</td>'."\n";
        !           433:                    &Apache::loncommon::end_data_table_row()."\n";
        !           434:         unless (&Apache::lonnet::allowed('udp',$ccdomain)) {
        !           435:             $output .=
1.275     raeburn   436:                    &Apache::loncommon::start_data_table_row()."\n".
1.306     raeburn   437:                    '  <td style="vertical-align:top;"><span class="LC_nobreak">'.
                    438:                    $lt{'chse'}.': <label>'.
1.275     raeburn   439:                    '<input type="radio" name="custom'.$item.'" value="0" '.
1.306     raeburn   440:                    $cust_off.'/>'.$lt{'usde'}.'</label>'.('&nbsp;' x3).
                    441:                    '<label><input type="radio" name="custom'.$item.'" value="1" '.
                    442:                    $cust_on.'/>'.$lt{'uscu'}.'</label>'.$custradio.'</td>'.
1.275     raeburn   443:                    &Apache::loncommon::end_data_table_row()."\n";
1.418   ! raeburn   444:         }
1.275     raeburn   445:     }
                    446:     return $output;
                    447: }
                    448: 
1.300     raeburn   449: sub coursereq_externaluser {
                    450:     my ($ccuname,$ccdomain,$cdom) = @_;
1.306     raeburn   451:     my (@usertools,@options,%validations,%userenv,$output);
1.300     raeburn   452:     my %lt = &Apache::lonlocal::texthash (
                    453:                    'official'   => 'Can request creation of official courses',
                    454:                    'unofficial' => 'Can request creation of unofficial courses',
                    455:                    'community'  => 'Can request creation of communities',
1.384     raeburn   456:                    'textbook'   => 'Can request creation of textbook courses',
1.411     raeburn   457:                    'placement'  => 'Can request creation of placement tests',
1.300     raeburn   458:     );
                    459: 
                    460:     %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    461:                       'reqcrsotherdom.official','reqcrsotherdom.unofficial',
1.411     raeburn   462:                       'reqcrsotherdom.community','reqcrsotherdom.textbook',
                    463:                       'reqcrsotherdom.placement');
                    464:     @usertools = ('official','unofficial','community','textbook','placement');
1.309     raeburn   465:     @options = ('approval','validate','autolimit');
1.306     raeburn   466:     %validations = &Apache::lonnet::auto_courserequest_checks($cdom);
                    467:     my $optregex = join('|',@options);
                    468:     my %reqtitles = &courserequest_titles();
1.300     raeburn   469:     foreach my $item (@usertools) {
1.306     raeburn   470:         my ($curroption,$currlimit,$tooloff);
1.300     raeburn   471:         if ($userenv{'reqcrsotherdom.'.$item} ne '') {
                    472:             my @curr = split(',',$userenv{'reqcrsotherdom.'.$item});
1.314     raeburn   473:             foreach my $req (@curr) {
                    474:                 if ($req =~ /^\Q$cdom\E\:($optregex)=?(\d*)$/) {
                    475:                     $curroption = $1;
                    476:                     $currlimit = $2;
                    477:                     last;
1.306     raeburn   478:                 }
                    479:             }
1.314     raeburn   480:             if (!$curroption) {
                    481:                 $curroption = 'norequest';
                    482:                 $tooloff = ' checked="checked"';
                    483:             }
1.306     raeburn   484:         } else {
                    485:             $curroption = 'norequest';
                    486:             $tooloff = ' checked="checked"';
                    487:         }
                    488:         $output.= &Apache::loncommon::start_data_table_row()."\n".
1.314     raeburn   489:                   '  <td><span class="LC_nobreak">'.$lt{$item}.': </span></td><td>'.
                    490:                   '<table><tr><td valign="top">'."\n".
1.306     raeburn   491:                   '<label><input type="radio" name="reqcrsotherdom_'.$item.
1.314     raeburn   492:                   '" value=""'.$tooloff.' />'.$reqtitles{'norequest'}.
                    493:                   '</label></td>';
1.306     raeburn   494:         foreach my $option (@options) {
                    495:             if ($option eq 'validate') {
                    496:                 my $canvalidate = 0;
                    497:                 if (ref($validations{$item}) eq 'HASH') {
                    498:                     if ($validations{$item}{'_external_'}) {
                    499:                         $canvalidate = 1;
                    500:                     }
                    501:                 }
                    502:                 next if (!$canvalidate);
                    503:             }
                    504:             my $checked = '';
                    505:             if ($option eq $curroption) {
                    506:                 $checked = ' checked="checked"';
                    507:             }
1.314     raeburn   508:             $output .= '<td valign="top"><span class="LC_nobreak"><label>'.
1.306     raeburn   509:                        '<input type="radio" name="reqcrsotherdom_'.$item.
                    510:                        '" value="'.$option.'"'.$checked.' />'.
1.314     raeburn   511:                        $reqtitles{$option}.'</label>';
1.306     raeburn   512:             if ($option eq 'autolimit') {
1.314     raeburn   513:                 $output .= '&nbsp;<input type="text" name="reqcrsotherdom_'.
1.306     raeburn   514:                            $item.'_limit" size="1" '.
1.314     raeburn   515:                            'value="'.$currlimit.'" /></span>'.
                    516:                            '<br />'.$reqtitles{'unlimited'};
                    517:             } else {
                    518:                 $output .= '</span>';
1.300     raeburn   519:             }
1.314     raeburn   520:             $output .= '</td>';
1.300     raeburn   521:         }
1.314     raeburn   522:         $output .= '</td></tr></table></td>'."\n".
1.300     raeburn   523:                    &Apache::loncommon::end_data_table_row()."\n";
                    524:     }
                    525:     return $output;
                    526: }
                    527: 
1.362     raeburn   528: sub domainrole_req {
                    529:     my ($ccuname,$ccdomain) = @_;
                    530:     return '<br /><h3>'.
                    531:            &mt('User Can Request Assignment of Domain Roles?').
                    532:            '</h3>'."\n".
                    533:            &Apache::loncommon::start_data_table().
                    534:            &build_tools_display($ccuname,$ccdomain,
                    535:                                 'requestauthor').
                    536:            &Apache::loncommon::end_data_table();
                    537: }
                    538: 
1.414     raeburn   539: sub domadhocroles {
                    540:     my ($ccuname,$ccdomain) = @_;
1.417     raeburn   541:     my $confname = &Apache::lonnet::get_domainconfiguser($env{'request.role.domain'});
1.414     raeburn   542:     my %existing=&Apache::lonnet::dump('roles',$env{'request.role.domain'},
                    543:                                        $confname,'rolesdef_');
1.418   ! raeburn   544:     my ($output,$canmodify);
        !           545:     if (&Apache::lonnet::allowed('cdh',$env{'request.role.domain'})) {
        !           546:         $canmodify = 1;
        !           547:     }
1.414     raeburn   548:     if (keys(%existing) > 0) {
                    549:         my @current;
1.417     raeburn   550:         my $curradhoc = 'adhocroles.'.$env{'request.role.domain'};
1.414     raeburn   551:         my %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,$curradhoc);
                    552:         if ($userenv{$curradhoc}) {
                    553:             @current = split(/,/,$userenv{$curradhoc});
                    554:         }
1.418   ! raeburn   555:         if (!$canmodify && !@current) {
        !           556:             return;
        !           557:         }
1.414     raeburn   558:         my %customroles;
                    559:         foreach my $key (keys(%existing)) {
                    560:             if ($key=~/^rolesdef\_(\w+)$/) {
                    561:                 my $rolename = $1;
                    562:                 my %privs;
                    563:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
                    564:                 $customroles{$rolename} = \%privs;
                    565:             }
                    566:         }
                    567:         $output = '<br /><h3>'.
                    568:                   &mt('Ad Hoc Course Roles Selectable via Helpdesk Role').
                    569:                   '</h3>'."\n".
                    570:                   &Apache::loncommon::start_data_table().
1.418   ! raeburn   571:                   &Apache::loncommon::start_data_table_header_row();
        !           572:         if ($canmodify) {
        !           573:             $output .= '<th>'.&mt('Action').'</th>';
        !           574:         }
        !           575:         $output .= '<th>'.&mt('Role').'</th>'.
        !           576:                    '<th>'.&mt('Privileges in Course').'<th>'.
        !           577:                    &Apache::loncommon::end_data_table_header_row();
1.414     raeburn   578:         foreach my $key (sort(keys(%customroles))) {
1.418   ! raeburn   579:             next if ((!$canmodify) && (!grep(/^\Q$key\E$/,@current)));
1.414     raeburn   580:             $output .= &Apache::loncommon::start_data_table_row();
1.418   ! raeburn   581:             if ($canmodify) {
        !           582:                 if (grep(/^\Q$key\E$/,@current)) {
        !           583:                     $output .= '<td><label>'.
        !           584:                                '<input type="checkbox" name="adhocroledel" value="'.$key.'" />'.
        !           585:                                &mt('Delete').'</label>'.
        !           586:                                '</td>';
        !           587:                 } else {
        !           588:                     $output .= '<td><label>'.
        !           589:                                '<input type="checkbox" name="adhocroleadd" value="'.$key.'" />'.
        !           590:                                &mt('Add').'</label>'.
        !           591:                                '</td>';
        !           592:                 }
1.414     raeburn   593:             }
                    594:             $output .= '<td>'.$key.'</td><td>';
                    595:             foreach my $level ('course','domain','system') {
                    596:                 if ($customroles{$key}{$level}) {
                    597:                     my $suffix;
                    598:                     if (($level eq 'domain') || ($level eq 'system')) {
                    599:                         $suffix = '&nbsp;('.&mt($level).')';
                    600:                     }
                    601:                     my @privs = split(/:/,$customroles{$key}{$level});
                    602:                     foreach my $item (@privs) {
1.417     raeburn   603:                         next if ($item eq '');
1.414     raeburn   604:                         my ($priv,$cond) = split(/\&/,$item);
                    605:                         $output .= &Apache::lonnet::plaintext($priv,'Course').$suffix.'<br />';
                    606:                     }
                    607:                 }
                    608:             }
                    609:             $output .= '</td>'.
                    610:                        &Apache::loncommon::end_data_table_row();
                    611:         }
                    612:         $output .= &Apache::loncommon::end_data_table();
                    613:     }
                    614:     return $output;
                    615: }
                    616: 
1.306     raeburn   617: sub courserequest_titles {
                    618:     my %titles = &Apache::lonlocal::texthash (
                    619:                                    official   => 'Official',
                    620:                                    unofficial => 'Unofficial',
                    621:                                    community  => 'Communities',
1.384     raeburn   622:                                    textbook   => 'Textbook',
1.411     raeburn   623:                                    placement  => 'Placement Tests',
1.306     raeburn   624:                                    norequest  => 'Not allowed',
1.309     raeburn   625:                                    approval   => 'Approval by Dom. Coord.',
1.306     raeburn   626:                                    validate   => 'With validation',
                    627:                                    autolimit  => 'Numerical limit',
1.314     raeburn   628:                                    unlimited  => '(blank for unlimited)',
1.306     raeburn   629:                  );
                    630:     return %titles;
                    631: }
                    632: 
                    633: sub courserequest_display {
                    634:     my %titles = &Apache::lonlocal::texthash (
1.309     raeburn   635:                                    approval   => 'Yes, need approval',
1.306     raeburn   636:                                    validate   => 'Yes, with validation',
                    637:                                    norequest  => 'No',
                    638:    );
                    639:    return %titles;
                    640: }
                    641: 
1.362     raeburn   642: sub requestauthor_titles {
                    643:     my %titles = &Apache::lonlocal::texthash (
                    644:                                    norequest  => 'Not allowed',
                    645:                                    approval   => 'Approval by Dom. Coord.',
                    646:                                    automatic  => 'Automatic approval',
                    647:                  );
                    648:     return %titles;
                    649: 
                    650: }
                    651: 
                    652: sub requestauthor_display {
                    653:     my %titles = &Apache::lonlocal::texthash (
                    654:                                    approval   => 'Yes, need approval',
                    655:                                    automatic  => 'Yes, automatic approval',
                    656:                                    norequest  => 'No',
                    657:    );
                    658:    return %titles;
                    659: }
                    660: 
1.383     raeburn   661: sub requestchange_display {
                    662:     my %titles = &Apache::lonlocal::texthash (
                    663:                                    approval   => "availability set to 'on' (approval required)", 
                    664:                                    automatic  => "availability set to 'on' (automatic approval)",
                    665:                                    norequest  => "availability set to 'off'",
                    666:    );
                    667:    return %titles;
                    668: }
                    669: 
1.362     raeburn   670: sub curr_requestauthor {
                    671:     my ($uname,$udom,$isadv,$inststatuses,$domconfig) = @_;
                    672:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
                    673:     if ($uname eq '' || $udom eq '') {
                    674:         $uname = $env{'user.name'};
                    675:         $udom = $env{'user.domain'};
                    676:         $isadv = $env{'user.adv'};
                    677:     }
                    678:     my (%userenv,%settings,$val);
                    679:     my @options = ('automatic','approval');
                    680:     %userenv =
                    681:         &Apache::lonnet::userenvironment($udom,$uname,'requestauthor','inststatus');
                    682:     if ($userenv{'requestauthor'}) {
                    683:         $val = $userenv{'requestauthor'};
                    684:         @{$inststatuses} = ('_custom_');
                    685:     } else {
                    686:         my %alltasks;
                    687:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') {
                    688:             %settings = %{$domconfig->{'requestauthor'}};
                    689:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
                    690:                 $val = $settings{'_LC_adv'};
                    691:                 @{$inststatuses} = ('_LC_adv_');
                    692:             } else {
                    693:                 if ($userenv{'inststatus'} ne '') {
                    694:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
                    695:                 } else {
                    696:                     @{$inststatuses} = ('default');
                    697:                 }
                    698:                 foreach my $status (@{$inststatuses}) {
                    699:                     if (exists($settings{$status})) {
                    700:                         my $value = $settings{$status};
                    701:                         next unless ($value);
                    702:                         unless (exists($alltasks{$value})) {
                    703:                             if (ref($alltasks{$value}) eq 'ARRAY') {
                    704:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
                    705:                                     push(@{$alltasks{$value}},$status);
                    706:                                 }
                    707:                             } else {
                    708:                                 @{$alltasks{$value}} = ($status);
                    709:                             }
                    710:                         }
                    711:                     }
                    712:                 }
                    713:                 foreach my $option (@options) {
                    714:                     if ($alltasks{$option}) {
                    715:                         $val = $option;
                    716:                         last;
                    717:                     }
                    718:                 }
                    719:             }
                    720:         }
                    721:     }
                    722:     return $val;
                    723: }
                    724: 
1.2       www       725: # =================================================================== Phase one
1.1       www       726: 
1.42      matthew   727: sub print_username_entry_form {
1.351     raeburn   728:     my ($r,$context,$response,$srch,$forcenewuser,$crstype,$brcrum) = @_;
1.101     albertel  729:     my $defdom=$env{'request.role.domain'};
1.160     raeburn   730:     my $formtoset = 'crtuser';
                    731:     if (exists($env{'form.startrolename'})) {
                    732:         $formtoset = 'docustom';
                    733:         $env{'form.rolename'} = $env{'form.startrolename'};
1.207     raeburn   734:     } elsif ($env{'form.origform'} eq 'crtusername') {
                    735:         $formtoset =  $env{'form.origform'};
1.160     raeburn   736:     }
                    737: 
                    738:     my ($jsback,$elements) = &crumb_utilities();
                    739: 
                    740:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
1.165     albertel  741:         '<script type="text/javascript">'."\n".
1.301     bisitz    742:         '// <![CDATA['."\n".
                    743:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset})."\n".
                    744:         '// ]]>'."\n".
1.162     raeburn   745:         '</script>'."\n";
1.160     raeburn   746: 
1.324     raeburn   747:     my %existingroles=&Apache::lonuserutils::my_custom_roles($crstype);
                    748:     if (($env{'form.action'} eq 'custom') && (keys(%existingroles) > 0)
                    749:         && (&Apache::lonnet::allowed('mcr','/'))) {
                    750:         $jscript .= &customrole_javascript();
                    751:     }
1.224     raeburn   752:     my $helpitem = 'Course_Change_Privileges';
                    753:     if ($env{'form.action'} eq 'custom') {
                    754:         $helpitem = 'Course_Editing_Custom_Roles';
                    755:     } elsif ($env{'form.action'} eq 'singlestudent') {
                    756:         $helpitem = 'Course_Add_Student';
1.416     raeburn   757:     } elsif ($env{'form.action'} eq 'accesslogs') {
                    758:         $helpitem = 'Domain_User_Access_Logs';
1.224     raeburn   759:     }
1.351     raeburn   760:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
                    761:     if ($env{'form.action'} eq 'custom') {
                    762:         push(@{$brcrum},
                    763:                  {href=>"javascript:backPage(document.crtuser)",       
                    764:                   text=>"Pick custom role",
                    765:                   help => $helpitem,}
                    766:                  );
                    767:     } else {
                    768:         push (@{$brcrum},
                    769:                   {href => "javascript:backPage(document.crtuser)",
                    770:                    text => $breadcrumb_text{'search'},
                    771:                    help => $helpitem,
                    772:                    faq  => 282,
                    773:                    bug  => 'Instructor Interface',}
                    774:                   );
                    775:     }
                    776:     my %loaditems = (
                    777:                 'onload' => "javascript:setFormElements(document.$formtoset)",
                    778:                     );
                    779:     my $args = {bread_crumbs           => $brcrum,
                    780:                 bread_crumbs_component => 'User Management',
                    781:                 add_entries            => \%loaditems,};
                    782:     $r->print(&Apache::loncommon::start_page('User Management',$jscript,$args));
                    783: 
1.71      sakharuk  784:     my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   785:                     'srst' => 'Search for a user and enroll as a student',
1.318     raeburn   786:                     'srme' => 'Search for a user and enroll as a member',
1.229     raeburn   787:                     'srad' => 'Search for a user and modify/add user information or roles',
1.416     raeburn   788:                     'srva' => 'Search for a user and view access log information',
1.71      sakharuk  789: 		    'usr'  => "Username",
                    790:                     'dom'  => "Domain",
1.324     raeburn   791:                     'ecrp' => "Define or Edit Custom Role",
                    792:                     'nr'   => "role name",
1.282     schafran  793:                     'cre'  => "Next",
1.71      sakharuk  794: 				       );
1.351     raeburn   795: 
1.214     raeburn   796:     if ($env{'form.action'} eq 'custom') {
1.190     raeburn   797:         if (&Apache::lonnet::allowed('mcr','/')) {
1.324     raeburn   798:             my $newroletext = &mt('Define new custom role:');
                    799:             $r->print('<form action="/adm/createuser" method="post" name="docustom">'.
                    800:                       '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                    801:                       '<input type="hidden" name="phase" value="selected_custom_edit" />'.
                    802:                       '<h3>'.$lt{'ecrp'}.'</h3>'.
                    803:                       &Apache::loncommon::start_data_table().
                    804:                       &Apache::loncommon::start_data_table_row().
                    805:                       '<td>');
                    806:             if (keys(%existingroles) > 0) {
                    807:                 $r->print('<br /><label><input type="radio" name="customroleaction" value="new" checked="checked" onclick="setCustomFields();" /><b>'.$newroletext.'</b></label>');
                    808:             } else {
                    809:                 $r->print('<br /><input type="hidden" name="customroleaction" value="new" /><b>'.$newroletext.'</b>');
                    810:             }
                    811:             $r->print('</td><td align="center">'.$lt{'nr'}.'<br /><input type="text" size="15" name="newrolename" onfocus="setCustomAction('."'new'".');" /></td>'.
                    812:                       &Apache::loncommon::end_data_table_row());
                    813:             if (keys(%existingroles) > 0) {
                    814:                 $r->print(&Apache::loncommon::start_data_table_row().'<td><br />'.
                    815:                           '<label><input type="radio" name="customroleaction" value="edit" onclick="setCustomFields();"/><b>'.
                    816:                           &mt('View/Modify existing role:').'</b></label></td>'.
                    817:                           '<td align="center"><br />'.
                    818:                           '<select name="rolename" onchange="setCustomAction('."'edit'".');">'.
1.326     raeburn   819:                           '<option value="" selected="selected">'.
1.324     raeburn   820:                           &mt('Select'));
                    821:                 foreach my $role (sort(keys(%existingroles))) {
1.326     raeburn   822:                     $r->print('<option value="'.$role.'">'.$role.'</option>');
1.324     raeburn   823:                 }
                    824:                 $r->print('</select>'.
                    825:                           '</td>'.
                    826:                           &Apache::loncommon::end_data_table_row());
                    827:             }
                    828:             $r->print(&Apache::loncommon::end_data_table().'<p>'.
                    829:                       '<input name="customeditor" type="submit" value="'.
                    830:                       $lt{'cre'}.'" /></p>'.
                    831:                       '</form>');
1.190     raeburn   832:         }
1.213     raeburn   833:     } else {
1.229     raeburn   834:         my $actiontext = $lt{'srad'};
1.213     raeburn   835:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   836:             if ($crstype eq 'Community') {
                    837:                 $actiontext = $lt{'srme'};
                    838:             } else {
                    839:                 $actiontext = $lt{'srst'};
                    840:             }
1.416     raeburn   841:         } elsif ($env{'form.action'} eq 'accesslogs') {
1.417     raeburn   842:             $actiontext = $lt{'srva'};
1.213     raeburn   843:         }
1.324     raeburn   844:         $r->print("<h3>$actiontext</h3>");
1.213     raeburn   845:         if ($env{'form.origform'} ne 'crtusername') {
1.415     raeburn   846:             if ($response) {
                    847:                $r->print("\n<div>$response</div>".
                    848:                          '<br clear="all" />');
                    849:             }
1.213     raeburn   850:         }
1.416     raeburn   851:         $r->print(&entry_form($defdom,$srch,$forcenewuser,$context,$response,$crstype,1));
1.107     www       852:     }
1.110     albertel  853: }
                    854: 
1.324     raeburn   855: sub customrole_javascript {
                    856:     my $js = <<"END";
                    857: <script type="text/javascript">
                    858: // <![CDATA[
                    859: 
                    860: function setCustomFields() {
                    861:     if (document.docustom.customroleaction.length > 0) {
                    862:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    863:             if (document.docustom.customroleaction[i].checked) {
                    864:                 if (document.docustom.customroleaction[i].value == 'new') {
                    865:                     document.docustom.rolename.selectedIndex = 0;
                    866:                 } else {
                    867:                     document.docustom.newrolename.value = '';
                    868:                 }
                    869:             }
                    870:         }
                    871:     }
                    872:     return;
                    873: }
                    874: 
                    875: function setCustomAction(caller) {
                    876:     if (document.docustom.customroleaction.length > 0) {
                    877:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    878:             if (document.docustom.customroleaction[i].value == caller) {
                    879:                 document.docustom.customroleaction[i].checked = true;
                    880:             }
                    881:         }
                    882:     }
                    883:     setCustomFields();
                    884:     return;
                    885: }
                    886: 
                    887: // ]]>
                    888: </script>
                    889: END
                    890:     return $js;
                    891: }
                    892: 
1.160     raeburn   893: sub entry_form {
1.416     raeburn   894:     my ($dom,$srch,$forcenewuser,$context,$responsemsg,$crstype,$fixeddom) = @_;
1.229     raeburn   895:     my ($usertype,$inexact);
1.214     raeburn   896:     if (ref($srch) eq 'HASH') {
                    897:         if (($srch->{'srchin'} eq 'dom') &&
                    898:             ($srch->{'srchby'} eq 'uname') &&
                    899:             ($srch->{'srchtype'} eq 'exact') &&
                    900:             ($srch->{'srchdomain'} ne '') &&
                    901:             ($srch->{'srchterm'} ne '')) {
1.353     raeburn   902:             my (%curr_rules,%got_rules);
1.214     raeburn   903:             my ($rules,$ruleorder) =
                    904:                 &Apache::lonnet::inst_userrules($srch->{'srchdomain'},'username');
1.353     raeburn   905:             $usertype = &Apache::lonuserutils::check_usertype($srch->{'srchdomain'},$srch->{'srchterm'},$rules,\%curr_rules,\%got_rules);
1.229     raeburn   906:         } else {
                    907:             $inexact = 1;
1.214     raeburn   908:         }
1.207     raeburn   909:     }
1.214     raeburn   910:     my $cancreate =
                    911:         &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
1.412     raeburn   912:     my ($userpicker,$cansearch) = 
1.179     raeburn   913:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
1.416     raeburn   914:                                        'document.crtuser',$cancreate,$usertype,$context,$fixeddom);
1.160     raeburn   915:     my $srchbutton = &mt('Search');
1.229     raeburn   916:     if ($env{'form.action'} eq 'singlestudent') {
                    917:         $srchbutton = &mt('Search and Enroll');
1.416     raeburn   918:     } elsif ($env{'form.action'} eq 'accesslogs') {
                    919:         $srchbutton = &mt('Search');
1.229     raeburn   920:     } elsif ($cancreate && $responsemsg ne '' && $inexact) {
                    921:         $srchbutton = &mt('Search or Add New User');
                    922:     }
1.412     raeburn   923:     my $output;
                    924:     if ($cansearch) {
                    925:         $output = <<"ENDBLOCK";
1.160     raeburn   926: <form action="/adm/createuser" method="post" name="crtuser">
1.190     raeburn   927: <input type="hidden" name="action" value="$env{'form.action'}" />
1.160     raeburn   928: <input type="hidden" name="phase" value="get_user_info" />
                    929: $userpicker
1.179     raeburn   930: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
1.160     raeburn   931: </form>
1.207     raeburn   932: ENDBLOCK
1.412     raeburn   933:     } else {
                    934:         $output = '<p>'.$userpicker.'</p>';
                    935:     }
1.416     raeburn   936:     if (($env{'form.phase'} eq '') && ($env{'form.action'} ne 'accesslogs')) {
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.416     raeburn  1052:                                        'srcva'          => "Search for a user and view access log information",
1.179     raeburn  1053:                                        'usel'           => "Select a user to add/modify roles",
1.318     raeburn  1054:                                        'stusel'         => "Select a user to enroll as a student",
                   1055:                                        'memsel'         => "Select a user to enroll as a member",
1.416     raeburn  1056:                                        'vacsel'         => "Select a user to view access log",
1.160     raeburn  1057:                                        'username'       => "username",
                   1058:                                        'domain'         => "domain",
                   1059:                                        'lastname'       => "last name",
                   1060:                                        'firstname'      => "first name",
                   1061:                                        'permanentemail' => "permanent e-mail",
                   1062:                                       );
1.302     raeburn  1063:     if ($context eq 'requestcrs') {
                   1064:         $r->print('<div>');
                   1065:     } else {
1.318     raeburn  1066:         my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.351     raeburn  1067:         my $helpitem;
                   1068:         if ($env{'form.action'} eq 'singleuser') {
                   1069:             $helpitem = 'Course_Change_Privileges';
                   1070:         } elsif ($env{'form.action'} eq 'singlestudent') {
                   1071:             $helpitem = 'Course_Add_Student';
                   1072:         }
                   1073:         push (@{$brcrum},
                   1074:                   {href => "javascript:backPage(document.usersrchform,'','')",
                   1075:                    text => $breadcrumb_text{'search'},
                   1076:                    faq  => 282,
                   1077:                    bug  => 'Instructor Interface',},
                   1078:                   {href => "javascript:backPage(document.usersrchform,'get_user_info','select')",
                   1079:                    text => $breadcrumb_text{'userpicked'},
                   1080:                    faq  => 282,
                   1081:                    bug  => 'Instructor Interface',
                   1082:                    help => $helpitem}
                   1083:                   );
                   1084:         $r->print(&Apache::loncommon::start_page('User Management',$jscript,{bread_crumbs => $brcrum}));
1.302     raeburn  1085:         if ($env{'form.action'} eq 'singleuser') {
                   1086:             $r->print("<b>$lt{'usrch'}</b><br />");
1.318     raeburn  1087:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
1.302     raeburn  1088:             $r->print('<h3>'.$lt{'usel'}.'</h3>');
                   1089:         } elsif ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1090:             $r->print($jscript."<b>");
                   1091:             if ($crstype eq 'Community') {
                   1092:                 $r->print($lt{'memsrch'});
                   1093:             } else {
                   1094:                 $r->print($lt{'stusrch'});
                   1095:             }
                   1096:             $r->print("</b><br />");
                   1097:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
                   1098:             $r->print('</form><h3>');
                   1099:             if ($crstype eq 'Community') {
                   1100:                 $r->print($lt{'memsel'});
                   1101:             } else {
                   1102:                 $r->print($lt{'stusel'});
                   1103:             }
                   1104:             $r->print('</h3>');
1.416     raeburn  1105:         } elsif ($env{'form.action'} eq 'accesslogs') {
                   1106:             $r->print("<b>$lt{'srcva'}</b><br />");
                   1107:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,'accesslogs',undef,undef,1));
                   1108:             $r->print('<h3>'.$lt{'vacsel'}.'</h3>');
1.302     raeburn  1109:         }
1.179     raeburn  1110:     }
1.380     bisitz   1111:     $r->print('<form name="usersrchform" method="post" action="">'.
1.160     raeburn  1112:               &Apache::loncommon::start_data_table()."\n".
                   1113:               &Apache::loncommon::start_data_table_header_row()."\n".
                   1114:               ' <th> </th>'."\n");
                   1115:     foreach my $field (@fields) {
                   1116:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
                   1117:                   "'".$field."'".';document.usersrchform.submit();">'.
                   1118:                   $lt{$field}.'</a></th>'."\n");
                   1119:     }
                   1120:     $r->print(&Apache::loncommon::end_data_table_header_row());
                   1121: 
                   1122:     my @sorted_users = sort {
1.167     albertel 1123:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
1.160     raeburn  1124:             ||
1.167     albertel 1125:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
1.160     raeburn  1126:             ||
                   1127:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
1.167     albertel 1128: 	    ||
                   1129: 	lc($a) cmp lc($b)
1.160     raeburn  1130:         } (keys(%$srch_results));
                   1131: 
                   1132:     foreach my $user (@sorted_users) {
                   1133:         my ($uname,$udom) = split(/:/,$user);
1.302     raeburn  1134:         my $onclick;
                   1135:         if ($context eq 'requestcrs') {
1.314     raeburn  1136:             $onclick =
1.302     raeburn  1137:                 'onclick="javascript:gochoose('."'$uname','$udom',".
                   1138:                                                "'$srch_results->{$user}->{firstname}',".
                   1139:                                                "'$srch_results->{$user}->{lastname}',".
                   1140:                                                "'$srch_results->{$user}->{permanentemail}'".');"';
                   1141:         } else {
1.314     raeburn  1142:             $onclick =
1.302     raeburn  1143:                 ' onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".');"';
                   1144:         }
1.160     raeburn  1145:         $r->print(&Apache::loncommon::start_data_table_row().
1.302     raeburn  1146:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" '.
                   1147:                   $onclick.' /></td>'.
1.160     raeburn  1148:                   '<td><tt>'.$uname.'</tt></td>'.
                   1149:                   '<td><tt>'.$udom.'</tt></td>');
                   1150:         foreach my $field ('lastname','firstname','permanentemail') {
                   1151:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
                   1152:         }
                   1153:         $r->print(&Apache::loncommon::end_data_table_row());
                   1154:     }
                   1155:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
1.179     raeburn  1156:     if (ref($srcharray) eq 'ARRAY') {
                   1157:         foreach my $item (@{$srcharray}) {
                   1158:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
                   1159:         }
                   1160:     }
1.160     raeburn  1161:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
                   1162:               ' <input type="hidden" name="seluname" value="" />'."\n".
                   1163:               ' <input type="hidden" name="seludom" value="" />'."\n".
1.179     raeburn  1164:               ' <input type="hidden" name="currstate" value="select" />'."\n".
1.190     raeburn  1165:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
1.214     raeburn  1166:               ' <input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
1.302     raeburn  1167:     if ($context eq 'requestcrs') {
                   1168:         $r->print($opener_elements.'</form></div>');
                   1169:     } else {
1.351     raeburn  1170:         $r->print($response.'</form>');
1.302     raeburn  1171:     }
1.160     raeburn  1172: }
                   1173: 
                   1174: sub print_user_query_page {
1.351     raeburn  1175:     my ($r,$caller,$brcrum) = @_;
1.160     raeburn  1176: # FIXME - this is for a network-wide name search (similar to catalog search)
                   1177: # To use frames with similar behavior to catalog/portfolio search.
                   1178: # To be implemented. 
                   1179:     return;
                   1180: }
                   1181: 
1.42      matthew  1182: sub print_user_modification_page {
1.375     raeburn  1183:     my ($r,$ccuname,$ccdomain,$srch,$response,$context,$permission,$crstype,
                   1184:         $brcrum,$showcredits) = @_;
1.185     raeburn  1185:     if (($ccuname eq '') || ($ccdomain eq '')) {
1.215     raeburn  1186:         my $usermsg = &mt('No username and/or domain provided.');
                   1187:         $env{'form.phase'} = '';
1.351     raeburn  1188: 	&print_username_entry_form($r,$context,$usermsg,'','',$crstype,$brcrum);
1.58      www      1189:         return;
                   1190:     }
1.213     raeburn  1191:     my ($form,$formname);
                   1192:     if ($env{'form.action'} eq 'singlestudent') {
                   1193:         $form = 'document.enrollstudent';
                   1194:         $formname = 'enrollstudent';
                   1195:     } else {
                   1196:         $form = 'document.cu';
                   1197:         $formname = 'cu';
                   1198:     }
1.188     raeburn  1199:     my %abv_auth = &auth_abbrev();
1.227     raeburn  1200:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
1.185     raeburn  1201:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
                   1202:     if ($uhome eq 'no_host') {
1.215     raeburn  1203:         my $usertype;
                   1204:         my ($rules,$ruleorder) =
                   1205:             &Apache::lonnet::inst_userrules($ccdomain,'username');
                   1206:             $usertype =
1.353     raeburn  1207:                 &Apache::lonuserutils::check_usertype($ccdomain,$ccuname,$rules,
1.362     raeburn  1208:                                                       \%curr_rules,\%got_rules);
1.215     raeburn  1209:         my $cancreate =
                   1210:             &Apache::lonuserutils::can_create_user($ccdomain,$context,
                   1211:                                                    $usertype);
                   1212:         if (!$cancreate) {
1.292     bisitz   1213:             my $helplink = 'javascript:helpMenu('."'display'".')';
1.215     raeburn  1214:             my %usertypetext = (
                   1215:                 official   => 'institutional',
                   1216:                 unofficial => 'non-institutional',
                   1217:             );
                   1218:             my $response;
                   1219:             if ($env{'form.origform'} eq 'crtusername') {
1.362     raeburn  1220:                 $response = '<span class="LC_warning">'.
                   1221:                             &mt('No match found for the username [_1] in LON-CAPA domain: [_2]',
                   1222:                                 '<b>'.$ccuname.'</b>',$ccdomain).
1.215     raeburn  1223:                             '</span><br />';
                   1224:             }
1.292     bisitz   1225:             $response .= '<p class="LC_warning">'
                   1226:                         .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
1.418   ! raeburn  1227:                         .' ';
        !          1228:             if ($context eq 'domain') {
        !          1229:                 $response .= &mt('Please contact a [_1] for assistance.',
        !          1230:                                  &Apache::lonnet::plaintext('dc'));
        !          1231:             } else {
        !          1232:                 $response .= &mt('Please contact the [_1]helpdesk[_2] for assistance.'
        !          1233:                                 ,'<a href="'.$helplink.'">','</a>');
        !          1234:             }
        !          1235:             $response .= '</p><br />';
1.215     raeburn  1236:             $env{'form.phase'} = '';
1.351     raeburn  1237:             &print_username_entry_form($r,$context,$response,undef,undef,$crstype,$brcrum);
1.215     raeburn  1238:             return;
                   1239:         }
1.188     raeburn  1240:         $newuser = 1;
1.193     raeburn  1241:         my $checkhash;
                   1242:         my $checks = { 'username' => 1 };
1.196     raeburn  1243:         $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
1.193     raeburn  1244:         &Apache::loncommon::user_rule_check($checkhash,$checks,
1.196     raeburn  1245:             \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
                   1246:         if (ref($alerts{'username'}) eq 'HASH') {
                   1247:             if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
                   1248:                 my $domdesc =
1.193     raeburn  1249:                     &Apache::lonnet::domain($ccdomain,'description');
1.196     raeburn  1250:                 if ($alerts{'username'}{$ccdomain}{$ccuname}) {
                   1251:                     my $userchkmsg;
                   1252:                     if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
                   1253:                         $userchkmsg = 
                   1254:                             &Apache::loncommon::instrule_disallow_msg('username',
1.193     raeburn  1255:                                                                  $domdesc,1).
                   1256:                         &Apache::loncommon::user_rule_formats($ccdomain,
                   1257:                             $domdesc,$curr_rules{$ccdomain}{'username'},
                   1258:                             'username');
1.196     raeburn  1259:                     }
1.215     raeburn  1260:                     $env{'form.phase'} = '';
1.351     raeburn  1261:                     &print_username_entry_form($r,$context,$userchkmsg,undef,undef,$crstype,$brcrum);
1.196     raeburn  1262:                     return;
1.215     raeburn  1263:                 }
1.193     raeburn  1264:             }
1.185     raeburn  1265:         }
1.187     raeburn  1266:     } else {
1.188     raeburn  1267:         $newuser = 0;
1.185     raeburn  1268:     }
1.160     raeburn  1269:     if ($response) {
1.215     raeburn  1270:         $response = '<br />'.$response;
1.160     raeburn  1271:     }
1.149     raeburn  1272: 
1.52      matthew  1273:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.88      raeburn  1274:     my $dc_setcourse_code = '';
1.119     raeburn  1275:     my $nondc_setsection_code = '';                                        
1.112     albertel 1276:     my %loaditem;
1.114     albertel 1277: 
1.216     raeburn  1278:     my $groupslist = &Apache::lonuserutils::get_groupslist();
1.88      raeburn  1279: 
1.375     raeburn  1280:     my $js = &validation_javascript($context,$ccdomain,$pjump_def,$crstype,
1.216     raeburn  1281:                                $groupslist,$newuser,$formname,\%loaditem);
1.318     raeburn  1282:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.224     raeburn  1283:     my $helpitem = 'Course_Change_Privileges';
                   1284:     if ($env{'form.action'} eq 'singlestudent') {
                   1285:         $helpitem = 'Course_Add_Student';
                   1286:     }
1.351     raeburn  1287:     push (@{$brcrum},
                   1288:         {href => "javascript:backPage($form)",
                   1289:          text => $breadcrumb_text{'search'},
                   1290:          faq  => 282,
                   1291:          bug  => 'Instructor Interface',});
                   1292:     if ($env{'form.phase'} eq 'userpicked') {
                   1293:        push(@{$brcrum},
                   1294:               {href => "javascript:backPage($form,'get_user_info','select')",
                   1295:                text => $breadcrumb_text{'userpicked'},
                   1296:                faq  => 282,
                   1297:                bug  => 'Instructor Interface',});
                   1298:     }
                   1299:     push(@{$brcrum},
                   1300:             {href => "javascript:backPage($form,'$env{'form.phase'}','modify')",
                   1301:              text => $breadcrumb_text{'modify'},
                   1302:              faq  => 282,
                   1303:              bug  => 'Instructor Interface',
                   1304:              help => $helpitem});
                   1305:     my $args = {'add_entries'           => \%loaditem,
                   1306:                 'bread_crumbs'          => $brcrum,
                   1307:                 'bread_crumbs_component' => 'User Management'};
                   1308:     if ($env{'form.popup'}) {
                   1309:         $args->{'no_nav_bar'} = 1;
                   1310:     }
                   1311:     my $start_page =
                   1312:         &Apache::loncommon::start_page('User Management',$js,$args);
1.3       www      1313: 
1.25      matthew  1314:     my $forminfo =<<"ENDFORMINFO";
1.216     raeburn  1315: <form action="/adm/createuser" method="post" name="$formname">
1.190     raeburn  1316: <input type="hidden" name="phase" value="update_user_data" />
1.188     raeburn  1317: <input type="hidden" name="ccuname" value="$ccuname" />
                   1318: <input type="hidden" name="ccdomain" value="$ccdomain" />
1.157     albertel 1319: <input type="hidden" name="pres_value"  value="" />
                   1320: <input type="hidden" name="pres_type"   value="" />
                   1321: <input type="hidden" name="pres_marker" value="" />
1.25      matthew  1322: ENDFORMINFO
1.375     raeburn  1323:     my (%inccourses,$roledom,$defaultcredits);
1.329     raeburn  1324:     if ($context eq 'course') {
                   1325:         $inccourses{$env{'request.course.id'}}=1;
                   1326:         $roledom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.375     raeburn  1327:         if ($showcredits) {
                   1328:             $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1329:         }
1.329     raeburn  1330:     } elsif ($context eq 'author') {
                   1331:         $roledom = $env{'request.role.domain'};
                   1332:     } elsif ($context eq 'domain') {
                   1333:         foreach my $key (keys(%env)) {
                   1334:             $roledom = $env{'request.role.domain'};
                   1335:             if ($key=~/^user\.priv\.cm\.\/($roledom)\/($match_username)/) {
                   1336:                 $inccourses{$1.'_'.$2}=1;
                   1337:             }
                   1338:         }
                   1339:     } else {
                   1340:         foreach my $key (keys(%env)) {
                   1341: 	    if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
                   1342: 	        $inccourses{$1.'_'.$2}=1;
                   1343:             }
1.2       www      1344:         }
1.24      matthew  1345:     }
1.389     bisitz   1346:     my $title = '';
1.216     raeburn  1347:     if ($newuser) {
1.414     raeburn  1348:         my ($portfolioform,$domroleform,$adhocroleform);
1.267     raeburn  1349:         if ((&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) ||
                   1350:             (&Apache::lonnet::allowed('mut',$env{'request.role.domain'}))) {
                   1351:             # Current user has quota or user tools modification privileges
1.378     raeburn  1352:             $portfolioform = '<br />'.&user_quotas($ccuname,$ccdomain);
1.134     raeburn  1353:         }
1.383     raeburn  1354:         if ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) &&
                   1355:             ($ccdomain eq $env{'request.role.domain'})) {
1.362     raeburn  1356:             $domroleform = '<br />'.&domainrole_req($ccuname,$ccdomain);
                   1357:         }
1.414     raeburn  1358:         if (&Apache::lonnet::allowed('cdh',$env{'request.role.domain'})) {
                   1359:             $adhocroleform = &domadhocroles($ccuname,$ccdomain);
                   1360:             if ($adhocroleform) {
                   1361:                 $adhocroleform = '<br />'.$adhocroleform;
                   1362:             }
                   1363:         }
1.227     raeburn  1364:         &initialize_authen_forms($ccdomain,$formname);
1.188     raeburn  1365:         my %lt=&Apache::lonlocal::texthash(
                   1366:                 'lg'             => 'Login Data',
1.190     raeburn  1367:                 'hs'             => "Home Server",
1.188     raeburn  1368:         );
1.185     raeburn  1369: 	$r->print(<<ENDTITLE);
1.110     albertel 1370: $start_page
1.160     raeburn  1371: $response
1.25      matthew  1372: $forminfo
1.31      matthew  1373: <script type="text/javascript" language="Javascript">
1.301     bisitz   1374: // <![CDATA[
1.20      harris41 1375: $loginscript
1.301     bisitz   1376: // ]]>
1.31      matthew  1377: </script>
1.20      harris41 1378: <input type='hidden' name='makeuser' value='1' />
1.185     raeburn  1379: ENDTITLE
1.213     raeburn  1380:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1381:             if ($crstype eq 'Community') {
1.389     bisitz   1382:                 $title = &mt('Create New User [_1] in domain [_2] as a member',
                   1383:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1384:             } else {
1.389     bisitz   1385:                 $title = &mt('Create New User [_1] in domain [_2] as a student',
                   1386:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1387:             }
1.389     bisitz   1388:         } else {
                   1389:                 $title = &mt('Create New User [_1] in domain [_2]',
                   1390:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.213     raeburn  1391:         }
1.389     bisitz   1392:         $r->print('<h2>'.$title.'</h2>'."\n");
                   1393:         $r->print('<div class="LC_left_float">');
1.393     raeburn  1394:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1395:                                          $inst_results{$ccuname.':'.$ccdomain}));
                   1396:         # Option to disable student/employee ID conflict checking not offerred for new users.
1.187     raeburn  1397:         my ($home_server_pick,$numlib) = 
                   1398:             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
                   1399:                                                       'default','hide');
                   1400:         if ($numlib > 1) {
                   1401:             $r->print("
1.185     raeburn  1402: <br />
1.187     raeburn  1403: $lt{'hs'}: $home_server_pick
                   1404: <br />");
                   1405:         } else {
                   1406:             $r->print($home_server_pick);
                   1407:         }
1.304     raeburn  1408:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.362     raeburn  1409:             $r->print('<br /><h3>'.
                   1410:                       &mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.304     raeburn  1411:                       &Apache::loncommon::start_data_table().
                   1412:                       &build_tools_display($ccuname,$ccdomain,
                   1413:                                            'requestcourses').
                   1414:                       &Apache::loncommon::end_data_table());
                   1415:         }
1.188     raeburn  1416:         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                   1417:                   $lt{'lg'}.'</h3>');
1.185     raeburn  1418:         my ($fixedauth,$varauth,$authmsg); 
1.193     raeburn  1419:         if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
                   1420:             my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
                   1421:             my ($rules,$ruleorder) = 
                   1422:                 &Apache::lonnet::inst_userrules($ccdomain,'username');
1.185     raeburn  1423:             if (ref($rules) eq 'HASH') {
1.193     raeburn  1424:                 if (ref($rules->{$matchedrule}) eq 'HASH') {
                   1425:                     my $authtype = $rules->{$matchedrule}{'authtype'};
1.185     raeburn  1426:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
1.190     raeburn  1427:                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.275     raeburn  1428:                     } else { 
1.193     raeburn  1429:                         my $authparm = $rules->{$matchedrule}{'authparm'};
1.273     raeburn  1430:                         $authmsg = $rules->{$matchedrule}{'authmsg'};
1.185     raeburn  1431:                         if ($authtype =~ /^krb(4|5)$/) {
                   1432:                             my $ver = $1;
                   1433:                             if ($authparm ne '') {
                   1434:                                 $fixedauth = <<"KERB"; 
                   1435: <input type="hidden" name="login" value="krb" />
                   1436: <input type="hidden" name="krbver" value="$ver" />
                   1437: <input type="hidden" name="krbarg" value="$authparm" />
                   1438: KERB
                   1439:                             }
                   1440:                         } else {
                   1441:                             $fixedauth = 
                   1442: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
1.193     raeburn  1443:                             if ($rules->{$matchedrule}{'authparmfixed'}) {
1.185     raeburn  1444:                                 $fixedauth .=    
                   1445: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
                   1446:                             } else {
1.273     raeburn  1447:                                 if ($authtype eq 'int') {
                   1448:                                     $varauth = '<br />'.
1.301     bisitz   1449: &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  1450:                                 } elsif ($authtype eq 'loc') {
                   1451:                                     $varauth = '<br />'.
                   1452: &mt('[_1] Local Authentication with argument [_2]','','<input type="text" name="'.$authtype.'arg" value="" />')."\n";
                   1453:                                 } else {
                   1454:                                     $varauth =
1.185     raeburn  1455: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
1.273     raeburn  1456:                                 }
1.185     raeburn  1457:                             }
                   1458:                         }
                   1459:                     }
                   1460:                 } else {
1.190     raeburn  1461:                     $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.185     raeburn  1462:                 }
                   1463:             }
                   1464:             if ($authmsg) {
                   1465:                 $r->print(<<ENDAUTH);
                   1466: $fixedauth
                   1467: $authmsg
                   1468: $varauth
                   1469: ENDAUTH
                   1470:             }
                   1471:         } else {
1.190     raeburn  1472:             $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc)); 
1.187     raeburn  1473:         }
1.414     raeburn  1474:         $r->print($portfolioform.$domroleform.$adhocroleform);
1.215     raeburn  1475:         if ($env{'form.action'} eq 'singlestudent') {
                   1476:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1477:                                             $permission,$crstype,$ccuname,
                   1478:                                             $ccdomain,$showcredits));
1.215     raeburn  1479:         }
                   1480:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.216     raeburn  1481:     } else { # user already exists
1.389     bisitz   1482: 	$r->print($start_page.$forminfo);
1.213     raeburn  1483:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1484:             if ($crstype eq 'Community') {
1.389     bisitz   1485:                 $title = &mt('Enroll one member: [_1] in domain [_2]',
                   1486:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1487:             } else {
1.389     bisitz   1488:                 $title = &mt('Enroll one student: [_1] in domain [_2]',
                   1489:                                  '"'.$ccuname.'"','"'.$ccdomain.'"');
1.318     raeburn  1490:             }
1.213     raeburn  1491:         } else {
1.418   ! raeburn  1492:             if ($permission->{'cusr'}) {
        !          1493:                 $title = &mt('Modify existing user: [_1] in domain [_2]',
        !          1494:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
        !          1495:             } else {
        !          1496:                 $title = &mt('Existing user: [_1] in domain [_2]',
1.389     bisitz   1497:                              '"'.$ccuname.'"','"'.$ccdomain.'"');
1.418   ! raeburn  1498:             }
1.213     raeburn  1499:         }
1.389     bisitz   1500:         $r->print('<h2>'.$title.'</h2>'."\n");
                   1501:         $r->print('<div class="LC_left_float">');
1.393     raeburn  1502:         $r->print(&personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1503:                                          $inst_results{$ccuname.':'.$ccdomain}));
1.418   ! raeburn  1504:         if ((&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) || 
        !          1505:             (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) {
1.362     raeburn  1506:             $r->print('<br /><h3>'.&mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.300     raeburn  1507:                       &Apache::loncommon::start_data_table());
1.314     raeburn  1508:             if ($env{'request.role.domain'} eq $ccdomain) {
1.300     raeburn  1509:                 $r->print(&build_tools_display($ccuname,$ccdomain,'requestcourses'));
                   1510:             } else {
                   1511:                 $r->print(&coursereq_externaluser($ccuname,$ccdomain,
                   1512:                                                   $env{'request.role.domain'}));
                   1513:             }
                   1514:             $r->print(&Apache::loncommon::end_data_table());
1.275     raeburn  1515:         }
1.199     raeburn  1516:         $r->print('</div>');
1.414     raeburn  1517:         my @order = ('auth','quota','tools','requestauthor','adhocroles');
1.362     raeburn  1518:         my %user_text;
                   1519:         my ($isadv,$isauthor) = 
1.418   ! raeburn  1520:             &Apache::lonnet::is_advanced_user($ccdomain,$ccuname);
1.362     raeburn  1521:         if ((!$isauthor) && 
1.418   ! raeburn  1522:             ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) ||
        !          1523:              (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) &&
        !          1524:             ($env{'request.role.domain'} eq $ccdomain)) {
1.362     raeburn  1525:             $user_text{'requestauthor'} = &domainrole_req($ccuname,$ccdomain);
                   1526:         }
1.418   ! raeburn  1527:         if ((&Apache::lonnet::allowed('cdh',$env{'request.role.domain'})) || 
        !          1528:             (&Apache::lonnet::allowed('udp',$env{'request.role.domain'}))) {
1.414     raeburn  1529:             $user_text{'adhocroles'} = &domadhocroles($ccuname,$ccdomain);
                   1530:         }
1.362     raeburn  1531:         $user_text{'auth'} =  &user_authentication($ccuname,$ccdomain,$formname);
1.267     raeburn  1532:         if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
1.418   ! raeburn  1533:             (&Apache::lonnet::allowed('mut',$ccdomain)) ||
        !          1534:             (&Apache::lonnet::allowed('udp',$ccdomain))) {
1.188     raeburn  1535:             # Current user has quota modification privileges
1.378     raeburn  1536:             $user_text{'quota'} = &user_quotas($ccuname,$ccdomain);
1.267     raeburn  1537:         }
                   1538:         if (!&Apache::lonnet::allowed('mpq',$ccdomain)) {
                   1539:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
                   1540:                 my %lt=&Apache::lonlocal::texthash(
1.385     bisitz   1541:                     'dska'  => "Disk quotas for user's portfolio and Authoring Space",
                   1542:                     'youd'  => "You do not have privileges to modify the portfolio and/or Authoring Space quotas for this user.",
1.267     raeburn  1543:                     'ichr'  => "If a change is required, contact a domain coordinator for the domain",
                   1544:                 );
1.362     raeburn  1545:                 $user_text{'quota'} = <<ENDNOPORTPRIV;
1.188     raeburn  1546: <h3>$lt{'dska'}</h3>
                   1547: $lt{'youd'} $lt{'ichr'}: $ccdomain
                   1548: ENDNOPORTPRIV
1.267     raeburn  1549:             }
                   1550:         }
                   1551:         if (!&Apache::lonnet::allowed('mut',$ccdomain)) {
                   1552:             if (&Apache::lonnet::allowed('mut',$env{'request.role.domain'})) {
                   1553:                 my %lt=&Apache::lonlocal::texthash(
                   1554:                     'utav'  => "User Tools Availability",
1.361     raeburn  1555:                     'yodo'  => "You do not have privileges to modify Portfolio, Blog, WebDAV, or Personal Information Page settings for this user.",
1.267     raeburn  1556:                     'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1557:                 );
1.362     raeburn  1558:                 $user_text{'tools'} = <<ENDNOTOOLSPRIV;
1.267     raeburn  1559: <h3>$lt{'utav'}</h3>
                   1560: $lt{'yodo'} $lt{'ifch'}: $ccdomain
                   1561: ENDNOTOOLSPRIV
                   1562:             }
1.188     raeburn  1563:         }
1.362     raeburn  1564:         my $gotdiv = 0; 
                   1565:         foreach my $item (@order) {
                   1566:             if ($user_text{$item} ne '') {
                   1567:                 unless ($gotdiv) {
                   1568:                     $r->print('<div class="LC_left_float">');
                   1569:                     $gotdiv = 1;
                   1570:                 }
                   1571:                 $r->print('<br />'.$user_text{$item});
                   1572:             }
                   1573:         }
                   1574:         if ($env{'form.action'} eq 'singlestudent') {
                   1575:             unless ($gotdiv) {
                   1576:                 $r->print('<div class="LC_left_float">');
1.213     raeburn  1577:             }
1.375     raeburn  1578:             my $credits;
                   1579:             if ($showcredits) {
                   1580:                 $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1581:                 if ($credits eq '') {
                   1582:                     $credits = $defaultcredits;
                   1583:                 }
                   1584:             }
1.374     raeburn  1585:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1586:                                             $permission,$crstype,$ccuname,
                   1587:                                             $ccdomain,$showcredits));
1.374     raeburn  1588:         }
1.362     raeburn  1589:         if ($gotdiv) {
                   1590:             $r->print('</div><div class="LC_clear_float_footer"></div>');
1.188     raeburn  1591:         }
1.418   ! raeburn  1592:         my $statuses;
        !          1593:         if (($context eq 'domain') && (&Apache::lonnet::allowed('udp',$ccdomain)) &&
        !          1594:             (!&Apache::lonnet::allowed('mau',$ccdomain))) {
        !          1595:             $statuses = ['active'];
        !          1596:         } elsif (($context eq 'course') && ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
        !          1597:                  ($env{'request.course.sec'} &&
        !          1598:                   &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'})))) {
        !          1599:             $statuses = ['active'];   
        !          1600:         }
1.217     raeburn  1601:         if ($env{'form.action'} ne 'singlestudent') {
1.329     raeburn  1602:             &display_existing_roles($r,$ccuname,$ccdomain,\%inccourses,$context,
1.418   ! raeburn  1603:                                     $roledom,$crstype,$showcredits,$statuses);
1.217     raeburn  1604:         }
1.25      matthew  1605:     } ## End of new user/old user logic
1.218     raeburn  1606:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1607:         my $btntxt;
                   1608:         if ($crstype eq 'Community') {
                   1609:             $btntxt = &mt('Enroll Member');
                   1610:         } else {
                   1611:             $btntxt = &mt('Enroll Student');
                   1612:         }
                   1613:         $r->print('<br /><input type="button" value="'.$btntxt.'" onclick="setSections(this.form)" />'."\n");
1.418   ! raeburn  1614:     } elsif ($permission->{'cusr'}) {
1.393     raeburn  1615:         $r->print('<div class="LC_left_float">'.
                   1616:                   '<fieldset><legend>'.&mt('Add Roles').'</legend>');
1.218     raeburn  1617:         my $addrolesdisplay = 0;
                   1618:         if ($context eq 'domain' || $context eq 'author') {
                   1619:             $addrolesdisplay = &new_coauthor_roles($r,$ccuname,$ccdomain);
                   1620:         }
                   1621:         if ($context eq 'domain') {
1.357     raeburn  1622:             my $add_domainroles = &new_domain_roles($r,$ccdomain);
1.218     raeburn  1623:             if (!$addrolesdisplay) {
                   1624:                 $addrolesdisplay = $add_domainroles;
1.2       www      1625:             }
1.375     raeburn  1626:             $r->print(&course_level_dc($env{'request.role.domain'},$showcredits));
1.393     raeburn  1627:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1628:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setCourse()" />'."\n");
1.218     raeburn  1629:         } elsif ($context eq 'author') {
                   1630:             if ($addrolesdisplay) {
1.393     raeburn  1631:                 $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1632:                           '<br /><input type="button" value="'.&mt('Save').'"');
1.218     raeburn  1633:                 if ($newuser) {
1.301     bisitz   1634:                     $r->print(' onclick="auth_check()" \>'."\n");
1.218     raeburn  1635:                 } else {
1.301     bisitz   1636:                     $r->print('onclick="this.form.submit()" \>'."\n");
1.218     raeburn  1637:                 }
1.188     raeburn  1638:             } else {
1.393     raeburn  1639:                 $r->print('</fieldset></div>'.
                   1640:                           '<div class="LC_clear_float_footer"></div>'.
                   1641:                           '<br /><a href="javascript:backPage(document.cu)">'.
1.218     raeburn  1642:                           &mt('Back to previous page').'</a>');
1.188     raeburn  1643:             }
                   1644:         } else {
1.375     raeburn  1645:             $r->print(&course_level_table(\%inccourses,$showcredits,$defaultcredits));
1.393     raeburn  1646:             $r->print('</fieldset></div><div class="LC_clear_float_footer"></div>'.
                   1647:                       '<br /><input type="button" value="'.&mt('Save').'" onclick="setSections(this.form)" />'."\n");
1.188     raeburn  1648:         }
1.88      raeburn  1649:     }
1.188     raeburn  1650:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
1.179     raeburn  1651:     $r->print('<input type="hidden" name="currstate" value="" />');
1.393     raeburn  1652:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" /></form><br /><br />');
1.218     raeburn  1653:     return;
1.2       www      1654: }
1.1       www      1655: 
1.213     raeburn  1656: sub singleuser_breadcrumb {
1.318     raeburn  1657:     my ($crstype) = @_;
1.213     raeburn  1658:     my %breadcrumb_text;
                   1659:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1660:         if ($crstype eq 'Community') {
                   1661:             $breadcrumb_text{'search'} = 'Enroll a member';
                   1662:         } else {
                   1663:             $breadcrumb_text{'search'} = 'Enroll a student';
                   1664:         }
1.213     raeburn  1665:         $breadcrumb_text{'userpicked'} = 'Select a user',
                   1666:         $breadcrumb_text{'modify'} = 'Set section/dates',
1.416     raeburn  1667:     } elsif ($env{'form.action'} eq 'accesslogs') {
                   1668:         $breadcrumb_text{'search'} = 'View access logs for a user';
                   1669:         $breadcrumb_text{'userpicked'} = 'Select a user',
                   1670:         $breadcrumb_text{'activity'} = 'Activity',
1.213     raeburn  1671:     } else {
1.229     raeburn  1672:         $breadcrumb_text{'search'} = 'Create/modify a user';
1.213     raeburn  1673:         $breadcrumb_text{'userpicked'} = 'Select a user',
                   1674:         $breadcrumb_text{'modify'} = 'Set user role',
                   1675:     }
                   1676:     return %breadcrumb_text;
                   1677: }
                   1678: 
                   1679: sub date_sections_select {
1.375     raeburn  1680:     my ($context,$newuser,$formname,$permission,$crstype,$ccuname,$ccdomain,
                   1681:         $showcredits) = @_;
                   1682:     my $credits;
                   1683:     if ($showcredits) {
                   1684:         my $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1685:         $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1686:         if ($credits eq '') {
                   1687:             $credits = $defaultcredits;
                   1688:         }
                   1689:     }
1.213     raeburn  1690:     my $cid = $env{'request.course.id'};
                   1691:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity($cid);
                   1692:     my $date_table = '<h3>'.&mt('Starting and Ending Dates').'</h3>'."\n".
                   1693:         &Apache::lonuserutils::date_setting_table(undef,undef,$context,
                   1694:                                                   undef,$formname,$permission);
                   1695:     my $rowtitle = 'Section';
1.375     raeburn  1696:     my $secbox = '<h3>'.&mt('Section and Credits').'</h3>'."\n".
1.213     raeburn  1697:         &Apache::lonuserutils::section_picker($cdom,$cnum,'st',$rowtitle,
1.375     raeburn  1698:                                               $permission,$context,'',$crstype,
                   1699:                                               $showcredits,$credits);
1.213     raeburn  1700:     my $output = $date_table.$secbox;
                   1701:     return $output;
                   1702: }
                   1703: 
1.216     raeburn  1704: sub validation_javascript {
1.375     raeburn  1705:     my ($context,$ccdomain,$pjump_def,$crstype,$groupslist,$newuser,$formname,
1.216     raeburn  1706:         $loaditem) = @_;
                   1707:     my $dc_setcourse_code = '';
                   1708:     my $nondc_setsection_code = '';
                   1709:     if ($context eq 'domain') {
                   1710:         my $dcdom = $env{'request.role.domain'};
                   1711:         $loaditem->{'onload'} = "document.cu.coursedesc.value='';";
1.227     raeburn  1712:         $dc_setcourse_code = 
                   1713:             &Apache::lonuserutils::dc_setcourse_js('cu','singleuser',$context);
1.216     raeburn  1714:     } else {
1.227     raeburn  1715:         my $checkauth; 
                   1716:         if (($newuser) || (&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1717:             $checkauth = 1;
                   1718:         }
                   1719:         if ($context eq 'course') {
                   1720:             $nondc_setsection_code =
                   1721:                 &Apache::lonuserutils::setsections_javascript($formname,$groupslist,
1.375     raeburn  1722:                                                               undef,$checkauth,
                   1723:                                                               $crstype);
1.227     raeburn  1724:         }
                   1725:         if ($checkauth) {
                   1726:             $nondc_setsection_code .= 
                   1727:                 &Apache::lonuserutils::verify_authen($formname,$context);
                   1728:         }
1.216     raeburn  1729:     }
                   1730:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                   1731:                                    $nondc_setsection_code,$groupslist);
                   1732:     my ($jsback,$elements) = &crumb_utilities();
                   1733:     $js .= "\n".
1.301     bisitz   1734:            '<script type="text/javascript">'."\n".
                   1735:            '// <![CDATA['."\n".
                   1736:            $jsback."\n".
                   1737:            '// ]]>'."\n".
                   1738:            '</script>'."\n";
1.216     raeburn  1739:     return $js;
                   1740: }
                   1741: 
1.217     raeburn  1742: sub display_existing_roles {
1.375     raeburn  1743:     my ($r,$ccuname,$ccdomain,$inccourses,$context,$roledom,$crstype,
1.418   ! raeburn  1744:         $showcredits,$statuses) = @_;
1.329     raeburn  1745:     my $now=time;
1.418   ! raeburn  1746:     my $showall = 1;
        !          1747:     my ($showexpired,$showactive);
        !          1748:     if ((ref($statuses) eq 'ARRAY') && (@{$statuses} > 0)) {
        !          1749:         $showall = 0;
        !          1750:         if (grep(/^expired$/,@{$statuses})) {
        !          1751:             $showexpired = 1;
        !          1752:         }
        !          1753:         if (grep(/^active$/,@{$statuses})) {
        !          1754:             $showactive = 1;
        !          1755:         }
        !          1756:         if ($showexpired && $showactive) {
        !          1757:             $showall = 1;
        !          1758:         }
        !          1759:     }
1.329     raeburn  1760:     my %lt=&Apache::lonlocal::texthash(
1.217     raeburn  1761:                     'rer'  => "Existing Roles",
                   1762:                     'rev'  => "Revoke",
                   1763:                     'del'  => "Delete",
                   1764:                     'ren'  => "Re-Enable",
                   1765:                     'rol'  => "Role",
                   1766:                     'ext'  => "Extent",
1.375     raeburn  1767:                     'crd'  => "Credits",
1.217     raeburn  1768:                     'sta'  => "Start",
                   1769:                     'end'  => "End",
                   1770:                                        );
1.329     raeburn  1771:     my (%rolesdump,%roletext,%sortrole,%roleclass,%rolepriv);
                   1772:     if ($context eq 'course' || $context eq 'author') {
                   1773:         my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
                   1774:         my %roleshash = 
                   1775:             &Apache::lonnet::get_my_roles($ccuname,$ccdomain,'userroles',
                   1776:                               ['active','previous','future'],\@roles,$roledom,1);
                   1777:         foreach my $key (keys(%roleshash)) {
                   1778:             my ($start,$end) = split(':',$roleshash{$key});
                   1779:             next if ($start eq '-1' || $end eq '-1');
                   1780:             my ($rnum,$rdom,$role,$sec) = split(':',$key);
                   1781:             if ($context eq 'course') {
                   1782:                 next unless (($rnum eq $env{'course.'.$env{'request.course.id'}.'.num'})
                   1783:                              && ($rdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}));
                   1784:             } elsif ($context eq 'author') {
                   1785:                 next unless (($rnum eq $env{'user.name'}) && ($rdom eq $env{'request.role.domain'}));
                   1786:             }
                   1787:             my ($newkey,$newvalue,$newrole);
                   1788:             $newkey = '/'.$rdom.'/'.$rnum;
                   1789:             if ($sec ne '') {
                   1790:                 $newkey .= '/'.$sec;
                   1791:             }
                   1792:             $newvalue = $role;
                   1793:             if ($role =~ /^cr/) {
                   1794:                 $newrole = 'cr';
                   1795:             } else {
                   1796:                 $newrole = $role;
                   1797:             }
                   1798:             $newkey .= '_'.$newrole;
                   1799:             if ($start ne '' && $end ne '') {
                   1800:                 $newvalue .= '_'.$end.'_'.$start;
1.335     raeburn  1801:             } elsif ($end ne '') {
                   1802:                 $newvalue .= '_'.$end;
1.329     raeburn  1803:             }
                   1804:             $rolesdump{$newkey} = $newvalue;
                   1805:         }
                   1806:     } else {
1.360     raeburn  1807:         %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
1.329     raeburn  1808:     }
                   1809:     # Build up table of user roles to allow revocation and re-enabling of roles.
                   1810:     my ($tmp) = keys(%rolesdump);
                   1811:     return if ($tmp =~ /^(con_lost|error)/i);
                   1812:     foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
                   1813:                                 my $b1=join('_',(split('_',$b))[1,0]);
                   1814:                                 return $a1 cmp $b1;
                   1815:                             } keys(%rolesdump)) {
                   1816:         next if ($area =~ /^rolesdef/);
                   1817:         my $envkey=$area;
                   1818:         my $role = $rolesdump{$area};
                   1819:         my $thisrole=$area;
                   1820:         $area =~ s/\_\w\w$//;
                   1821:         my ($role_code,$role_end_time,$role_start_time) =
                   1822:             split(/_/,$role);
1.418   ! raeburn  1823:         my $active=1;
        !          1824:         $active=0 if (($role_end_time) && ($now>$role_end_time));
        !          1825:         if ($active) {
        !          1826:             next unless($showall || $showactive);
        !          1827:         } else {
        !          1828:             next unless($showall || $showexpired); 
        !          1829:         }
1.217     raeburn  1830: # Is this a custom role? Get role owner and title.
1.329     raeburn  1831:         my ($croleudom,$croleuname,$croletitle)=
                   1832:             ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
                   1833:         my $allowed=0;
                   1834:         my $delallowed=0;
                   1835:         my $sortkey=$role_code;
                   1836:         my $class='Unknown';
1.375     raeburn  1837:         my $credits='';
1.418   ! raeburn  1838:         my $csec;
1.329     raeburn  1839:         if ($area =~ m{^/($match_domain)/($match_courseid)} ) {
                   1840:             $class='Course';
                   1841:             my ($coursedom,$coursedir) = ($1,$2);
                   1842:             my $cid = $1.'_'.$2;
                   1843:             # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
                   1844:             my %coursedata=
                   1845:                 &Apache::lonnet::coursedescription($cid);
                   1846:             if ($coursedir =~ /^$match_community$/) {
                   1847:                 $class='Community';
                   1848:             }
                   1849:             $sortkey.="\0$coursedom";
                   1850:             my $carea;
                   1851:             if (defined($coursedata{'description'})) {
                   1852:                 $carea=$coursedata{'description'}.
                   1853:                     '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
                   1854:     &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$coursedir,$coursedom);
                   1855:                 $sortkey.="\0".$coursedata{'description'};
                   1856:             } else {
                   1857:                 if ($class eq 'Community') {
                   1858:                     $carea=&mt('Unavailable community').': '.$area;
                   1859:                     $sortkey.="\0".&mt('Unavailable community').': '.$area;
1.217     raeburn  1860:                 } else {
                   1861:                     $carea=&mt('Unavailable course').': '.$area;
                   1862:                     $sortkey.="\0".&mt('Unavailable course').': '.$area;
                   1863:                 }
1.329     raeburn  1864:             }
                   1865:             $sortkey.="\0$coursedir";
                   1866:             $inccourses->{$cid}=1;
1.375     raeburn  1867:             if (($showcredits) && ($class eq 'Course') && ($role_code eq 'st')) {
                   1868:                 my $defaultcredits = $coursedata{'internal.defaultcredits'};
                   1869:                 $credits =
                   1870:                     &get_user_credits($ccuname,$ccdomain,$defaultcredits,
                   1871:                                       $coursedom,$coursedir);
                   1872:                 if ($credits eq '') {
                   1873:                     $credits = $defaultcredits;
                   1874:                 }
                   1875:             }
1.329     raeburn  1876:             if ((&Apache::lonnet::allowed('c'.$role_code,$coursedom.'/'.$coursedir)) ||
                   1877:                 (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1878:                 $allowed=1;
                   1879:             }
                   1880:             unless ($allowed) {
1.365     raeburn  1881:                 my $isowner = &Apache::lonuserutils::is_courseowner($cid,$coursedata{'internal.courseowner'});
1.329     raeburn  1882:                 if ($isowner) {
                   1883:                     if (($role_code eq 'co') && ($class eq 'Community')) {
                   1884:                         $allowed = 1;
                   1885:                     } elsif (($role_code eq 'cc') && ($class eq 'Course')) {
                   1886:                         $allowed = 1;
                   1887:                     }
1.217     raeburn  1888:                 }
1.329     raeburn  1889:             } 
                   1890:             if ((&Apache::lonnet::allowed('dro',$coursedom)) ||
                   1891:                 (&Apache::lonnet::allowed('dro',$ccdomain))) {
                   1892:                 $delallowed=1;
                   1893:             }
1.217     raeburn  1894: # - custom role. Needs more info, too
1.329     raeburn  1895:             if ($croletitle) {
                   1896:                 if (&Apache::lonnet::allowed('ccr',$coursedom.'/'.$coursedir)) {
                   1897:                     $allowed=1;
                   1898:                     $thisrole.='.'.$role_code;
1.217     raeburn  1899:                 }
1.329     raeburn  1900:             }
1.418   ! raeburn  1901:             if ($area=~m{^/($match_domain/$match_courseid/(\w+))}) {
        !          1902:                 
        !          1903:                 $csec = $2;
        !          1904:                 $carea.='<br />'.&mt('Section: [_1]',$csec);
        !          1905:                 $sortkey.="\0$csec";
1.329     raeburn  1906:                 if (!$allowed) {
1.418   ! raeburn  1907:                     if ($env{'request.course.sec'} eq $csec) {
        !          1908:                         if (&Apache::lonnet::allowed('c'.$role_code,$1)) {
1.329     raeburn  1909:                             $allowed = 1;
1.217     raeburn  1910:                         }
                   1911:                     }
                   1912:                 }
1.329     raeburn  1913:             }
                   1914:             $area=$carea;
                   1915:         } else {
                   1916:             $sortkey.="\0".$area;
                   1917:             # Determine if current user is able to revoke privileges
                   1918:             if ($area=~m{^/($match_domain)/}) {
                   1919:                 if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
                   1920:                    (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1921:                    $allowed=1;
1.217     raeburn  1922:                 }
1.329     raeburn  1923:                 if (((&Apache::lonnet::allowed('dro',$1))  ||
                   1924:                     (&Apache::lonnet::allowed('dro',$ccdomain))) &&
                   1925:                     ($role_code ne 'dc')) {
                   1926:                     $delallowed=1;
1.217     raeburn  1927:                 }
1.329     raeburn  1928:             } else {
                   1929:                 if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
1.217     raeburn  1930:                     $allowed=1;
                   1931:                 }
                   1932:             }
1.363     raeburn  1933:             if ($role_code eq 'ca' || $role_code eq 'au' || $role_code eq 'aa') {
1.377     raeburn  1934:                 $class='Authoring Space';
1.329     raeburn  1935:             } elsif ($role_code eq 'su') {
                   1936:                 $class='System';
1.217     raeburn  1937:             } else {
1.329     raeburn  1938:                 $class='Domain';
1.217     raeburn  1939:             }
1.329     raeburn  1940:         }
                   1941:         if (($role_code eq 'ca') || ($role_code eq 'aa')) {
                   1942:             $area=~m{/($match_domain)/($match_username)};
                   1943:             if (&Apache::lonuserutils::authorpriv($2,$1)) {
                   1944:                 $allowed=1;
1.217     raeburn  1945:             } else {
1.329     raeburn  1946:                 $allowed=0;
1.217     raeburn  1947:             }
1.329     raeburn  1948:         }
                   1949:         my $row = '';
1.418   ! raeburn  1950:         if ($showall) {
        !          1951:             $row.= '<td>';
        !          1952:             if (($active) && ($allowed)) {
        !          1953:                 $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
        !          1954:             } else {
        !          1955:                 if ($active) {
        !          1956:                     $row.='&nbsp;';
        !          1957:                 } else {
        !          1958:                     $row.=&mt('expired or revoked');
        !          1959:                 }
        !          1960:             }
        !          1961:             $row.='</td><td>';
        !          1962:             if ($allowed && !$active) {
        !          1963:                 $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
        !          1964:             } else {
        !          1965:                 $row.='&nbsp;';
        !          1966:             }
        !          1967:             $row.='</td><td>';
        !          1968:             if ($delallowed) {
        !          1969:                 $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
1.217     raeburn  1970:             } else {
1.418   ! raeburn  1971:                 $row.='&nbsp;';
1.217     raeburn  1972:             }
1.418   ! raeburn  1973:             $row.= '</td>'; 
1.329     raeburn  1974:         }
                   1975:         my $plaintext='';
                   1976:         if (!$croletitle) {
1.375     raeburn  1977:             $plaintext=&Apache::lonnet::plaintext($role_code,$class);
                   1978:             if (($showcredits) && ($credits ne '')) {
                   1979:                 $plaintext .= '<br/ ><span class="LC_nobreak">'.
                   1980:                               '<span class="LC_fontsize_small">'.
                   1981:                               &mt('Credits: [_1]',$credits).
                   1982:                               '</span></span>';
                   1983:             }
1.329     raeburn  1984:         } else {
                   1985:             $plaintext=
1.395     bisitz   1986:                 &mt('Custom role [_1][_2]defined by [_3]',
1.346     bisitz   1987:                         '"'.$croletitle.'"',
                   1988:                         '<br />',
                   1989:                         $croleuname.':'.$croleudom);
1.329     raeburn  1990:         }
1.418   ! raeburn  1991:         $row.= '<td>'.$plaintext.'</td>'.
        !          1992:                '<td>'.$area.'</td>'.
        !          1993:                '<td>'.($role_start_time?&Apache::lonlocal::locallocaltime($role_start_time)
        !          1994:                                             : '&nbsp;' ).'</td>'.
        !          1995:                '<td>'.($role_end_time  ?&Apache::lonlocal::locallocaltime($role_end_time)
        !          1996:                                             : '&nbsp;' ).'</td>';
1.329     raeburn  1997:         $sortrole{$sortkey}=$envkey;
                   1998:         $roletext{$envkey}=$row;
                   1999:         $roleclass{$envkey}=$class;
1.418   ! raeburn  2000:         if ($allowed) {
        !          2001:             $rolepriv{$envkey}='edit';
        !          2002:         } else {
        !          2003:             if ($context eq 'domain') {
        !          2004:                 if (&Apache::lonnet::allowed('vur',$ccdomain)) {
        !          2005:                     $rolepriv{$envkey}='view';
        !          2006:                 }
        !          2007:             } elsif ($context eq 'course') {
        !          2008:                 if ((&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) ||
        !          2009:                     ($env{'request.course.sec'} && ($env{'request.course.sec'} eq $csec) &&
        !          2010:                      &Apache::lonnet::allowed('vcl',$env{'request.course.id'}.'/'.$env{'request.course.sec'}))) {
        !          2011:                     $rolepriv{$envkey}='view';
        !          2012:                 }
        !          2013:             }
        !          2014:         }
1.329     raeburn  2015:     } # end of foreach        (table building loop)
                   2016: 
                   2017:     my $rolesdisplay = 0;
                   2018:     my %output = ();
1.377     raeburn  2019:     foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  2020:         $output{$type} = '';
                   2021:         foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                   2022:             if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) {
                   2023:                  $output{$type}.=
                   2024:                       &Apache::loncommon::start_data_table_row().
                   2025:                       $roletext{$sortrole{$which}}.
                   2026:                       &Apache::loncommon::end_data_table_row();
1.217     raeburn  2027:             }
1.329     raeburn  2028:         }
                   2029:         unless($output{$type} eq '') {
                   2030:             $output{$type} = '<tr class="LC_info_row">'.
                   2031:                       "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
                   2032:                       $output{$type};
                   2033:             $rolesdisplay = 1;
                   2034:         }
                   2035:     }
                   2036:     if ($rolesdisplay == 1) {
                   2037:         my $contextrole='';
                   2038:         if ($env{'request.course.id'}) {
                   2039:             if (&Apache::loncommon::course_type() eq 'Community') {
                   2040:                 $contextrole = &mt('Existing Roles in this Community');
1.290     bisitz   2041:             } else {
1.329     raeburn  2042:                 $contextrole = &mt('Existing Roles in this Course');
1.290     bisitz   2043:             }
1.329     raeburn  2044:         } elsif ($env{'request.role'} =~ /^au\./) {
1.377     raeburn  2045:             $contextrole = &mt('Existing Co-Author Roles in your Authoring Space');
1.329     raeburn  2046:         } else {
1.418   ! raeburn  2047:             if ($showall) {
        !          2048:                 $contextrole = &mt('Existing Roles in this Domain');
        !          2049:             } elsif ($showactive) {
        !          2050:                 $contextrole = &mt('Unexpired Roles in this Domain');
        !          2051:             } elsif ($showexpired) {
        !          2052:                 $contextrole = &mt('Expired or Revoked Roles in this Domain');
        !          2053:             }
1.329     raeburn  2054:         }
1.393     raeburn  2055:         $r->print('<div class="LC_left_float">'.
1.375     raeburn  2056: '<fieldset><legend>'.$contextrole.'</legend>'.
1.217     raeburn  2057: &Apache::loncommon::start_data_table("LC_createuser").
1.418   ! raeburn  2058: &Apache::loncommon::start_data_table_header_row());
        !          2059:         if ($showall) {
        !          2060:             $r->print(
        !          2061: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}
        !          2062:             );
        !          2063:         } elsif ($showexpired) {
        !          2064:             $r->print('<th>'.$lt{'rev'}.'</th>');
        !          2065:         }
        !          2066:         $r->print(
1.217     raeburn  2067: '</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.
                   2068: '</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
                   2069: &Apache::loncommon::end_data_table_header_row());
1.377     raeburn  2070:         foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  2071:             if ($output{$type}) {
                   2072:                 $r->print($output{$type}."\n");
1.217     raeburn  2073:             }
                   2074:         }
1.375     raeburn  2075:         $r->print(&Apache::loncommon::end_data_table().
                   2076:                   '</fieldset></div>');
1.329     raeburn  2077:     }
1.217     raeburn  2078:     return;
                   2079: }
                   2080: 
1.218     raeburn  2081: sub new_coauthor_roles {
                   2082:     my ($r,$ccuname,$ccdomain) = @_;
                   2083:     my $addrolesdisplay = 0;
                   2084:     #
                   2085:     # Co-Author
                   2086:     #
                   2087:     if (&Apache::lonuserutils::authorpriv($env{'user.name'},
                   2088:                                           $env{'request.role.domain'}) &&
                   2089:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
                   2090:         # No sense in assigning co-author role to yourself
                   2091:         $addrolesdisplay = 1;
                   2092:         my $cuname=$env{'user.name'};
                   2093:         my $cudom=$env{'request.role.domain'};
                   2094:         my %lt=&Apache::lonlocal::texthash(
1.377     raeburn  2095:                     'cs'   => "Authoring Space",
1.218     raeburn  2096:                     'act'  => "Activate",
                   2097:                     'rol'  => "Role",
                   2098:                     'ext'  => "Extent",
                   2099:                     'sta'  => "Start",
                   2100:                     'end'  => "End",
                   2101:                     'cau'  => "Co-Author",
                   2102:                     'caa'  => "Assistant Co-Author",
                   2103:                     'ssd'  => "Set Start Date",
                   2104:                     'sed'  => "Set End Date"
                   2105:                                        );
                   2106:         $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n".
                   2107:                   &Apache::loncommon::start_data_table()."\n".
                   2108:                   &Apache::loncommon::start_data_table_header_row()."\n".
                   2109:                   '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
                   2110:                   '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
                   2111:                   '<th>'.$lt{'end'}.'</th>'."\n".
                   2112:                   &Apache::loncommon::end_data_table_header_row()."\n".
                   2113:                   &Apache::loncommon::start_data_table_row().'
                   2114:            <td>
1.291     bisitz   2115:             <input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_ca" />
1.218     raeburn  2116:            </td>
                   2117:            <td>'.$lt{'cau'}.'</td>
                   2118:            <td>'.$cudom.'_'.$cuname.'</td>
                   2119:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
                   2120:              <a href=
                   2121: "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>
                   2122: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
                   2123: <a href=
                   2124: "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".
                   2125:               &Apache::loncommon::end_data_table_row()."\n".
                   2126:               &Apache::loncommon::start_data_table_row()."\n".
1.291     bisitz   2127: '<td><input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
1.218     raeburn  2128: <td>'.$lt{'caa'}.'</td>
                   2129: <td>'.$cudom.'_'.$cuname.'</td>
                   2130: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
                   2131: <a href=
                   2132: "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>
                   2133: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
                   2134: <a href=
                   2135: "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".
                   2136:              &Apache::loncommon::end_data_table_row()."\n".
                   2137:              &Apache::loncommon::end_data_table());
                   2138:     } elsif ($env{'request.role'} =~ /^au\./) {
                   2139:         if (!(&Apache::lonuserutils::authorpriv($env{'user.name'},
                   2140:                                                 $env{'request.role.domain'}))) {
                   2141:             $r->print('<span class="LC_error">'.
                   2142:                       &mt('You do not have privileges to assign co-author roles.').
                   2143:                       '</span>');
                   2144:         } elsif (($env{'user.name'} eq $ccuname) &&
                   2145:              ($env{'user.domain'} eq $ccdomain)) {
1.377     raeburn  2146:             $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  2147:         }
                   2148:     }
                   2149:     return $addrolesdisplay;;
                   2150: }
                   2151: 
                   2152: sub new_domain_roles {
1.357     raeburn  2153:     my ($r,$ccdomain) = @_;
1.218     raeburn  2154:     my $addrolesdisplay = 0;
                   2155:     #
                   2156:     # Domain level
                   2157:     #
                   2158:     my $num_domain_level = 0;
                   2159:     my $domaintext =
                   2160:     '<h4>'.&mt('Domain Level').'</h4>'.
                   2161:     &Apache::loncommon::start_data_table().
                   2162:     &Apache::loncommon::start_data_table_header_row().
                   2163:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
                   2164:     &mt('Extent').'</th>'.
                   2165:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
                   2166:     &Apache::loncommon::end_data_table_header_row();
1.312     raeburn  2167:     my @allroles = &Apache::lonuserutils::roles_by_context('domain');
1.218     raeburn  2168:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
1.312     raeburn  2169:         foreach my $role (@allroles) {
                   2170:             next if ($role eq 'ad');
1.357     raeburn  2171:             next if (($role eq 'au') && ($ccdomain ne $thisdomain));
1.218     raeburn  2172:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
                   2173:                my $plrole=&Apache::lonnet::plaintext($role);
                   2174:                my %lt=&Apache::lonlocal::texthash(
                   2175:                     'ssd'  => "Set Start Date",
                   2176:                     'sed'  => "Set End Date"
                   2177:                                        );
                   2178:                $num_domain_level ++;
                   2179:                $domaintext .=
                   2180: &Apache::loncommon::start_data_table_row().
1.291     bisitz   2181: '<td><input type="checkbox" name="act_'.$thisdomain.'_'.$role.'" /></td>
1.218     raeburn  2182: <td>'.$plrole.'</td>
                   2183: <td>'.$thisdomain.'</td>
                   2184: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
                   2185: <a href=
                   2186: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
                   2187: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
                   2188: <a href=
                   2189: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
                   2190: &Apache::loncommon::end_data_table_row();
                   2191:             }
                   2192:         }
                   2193:     }
                   2194:     $domaintext.= &Apache::loncommon::end_data_table();
                   2195:     if ($num_domain_level > 0) {
                   2196:         $r->print($domaintext);
                   2197:         $addrolesdisplay = 1;
                   2198:     }
                   2199:     return $addrolesdisplay;
                   2200: }
                   2201: 
1.188     raeburn  2202: sub user_authentication {
1.227     raeburn  2203:     my ($ccuname,$ccdomain,$formname) = @_;
1.188     raeburn  2204:     my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
1.227     raeburn  2205:     my $outcome;
1.418   ! raeburn  2206:     my %lt=&Apache::lonlocal::texthash(
        !          2207:                    'err'   => "ERROR",
        !          2208:                    'uuas'  => "This user has an unrecognized authentication scheme",
        !          2209:                    'adcs'  => "Please alert a domain coordinator of this situation",
        !          2210:                    'sldb'  => "Please specify login data below",
        !          2211:                    'ld'    => "Login Data"
        !          2212:     );
1.188     raeburn  2213:     # Check for a bad authentication type
                   2214:     if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth):/) {
                   2215:         # bad authentication scheme
                   2216:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
1.227     raeburn  2217:             &initialize_authen_forms($ccdomain,$formname);
                   2218: 
1.190     raeburn  2219:             my $choices = &Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc);
1.188     raeburn  2220:             $outcome = <<ENDBADAUTH;
                   2221: <script type="text/javascript" language="Javascript">
1.301     bisitz   2222: // <![CDATA[
1.188     raeburn  2223: $loginscript
1.301     bisitz   2224: // ]]>
1.188     raeburn  2225: </script>
                   2226: <span class="LC_error">$lt{'err'}:
                   2227: $lt{'uuas'} ($currentauth). $lt{'sldb'}.</span>
                   2228: <h3>$lt{'ld'}</h3>
                   2229: $choices
                   2230: ENDBADAUTH
                   2231:         } else {
                   2232:             # This user is not allowed to modify the user's
                   2233:             # authentication scheme, so just notify them of the problem
                   2234:             $outcome = <<ENDBADAUTH;
                   2235: <span class="LC_error"> $lt{'err'}: 
                   2236: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
                   2237: </span>
                   2238: ENDBADAUTH
                   2239:         }
                   2240:     } else { # Authentication type is valid
1.418   ! raeburn  2241:         
1.227     raeburn  2242:         &initialize_authen_forms($ccdomain,$formname,$currentauth,'modifyuser');
1.205     raeburn  2243:         my ($authformcurrent,$can_modify,@authform_others) =
1.188     raeburn  2244:             &modify_login_block($ccdomain,$currentauth);
                   2245:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2246:             # Current user has login modification privileges
                   2247:             $outcome =
                   2248:                        '<script type="text/javascript" language="Javascript">'."\n".
1.301     bisitz   2249:                        '// <![CDATA['."\n".
1.188     raeburn  2250:                        $loginscript."\n".
1.301     bisitz   2251:                        '// ]]>'."\n".
1.188     raeburn  2252:                        '</script>'."\n".
                   2253:                        '<h3>'.$lt{'ld'}.'</h3>'.
                   2254:                        &Apache::loncommon::start_data_table().
1.205     raeburn  2255:                        &Apache::loncommon::start_data_table_row().
1.188     raeburn  2256:                        '<td>'.$authformnop;
1.418   ! raeburn  2257:             if (($can_modify) && (&Apache::lonnet::allowed('mau',$ccdomain))) {
1.188     raeburn  2258:                 $outcome .= '</td>'."\n".
                   2259:                             &Apache::loncommon::end_data_table_row().
                   2260:                             &Apache::loncommon::start_data_table_row().
                   2261:                             '<td>'.$authformcurrent.'</td>'.
                   2262:                             &Apache::loncommon::end_data_table_row()."\n";
                   2263:             } else {
1.200     raeburn  2264:                 $outcome .= '&nbsp;('.$authformcurrent.')</td>'.
                   2265:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  2266:             }
1.418   ! raeburn  2267:             if (&Apache::lonnet::allowed('mau',$ccdomain)) {
        !          2268:                 foreach my $item (@authform_others) { 
        !          2269:                     $outcome .= &Apache::loncommon::start_data_table_row().
        !          2270:                                 '<td>'.$item.'</td>'.
        !          2271:                                 &Apache::loncommon::end_data_table_row()."\n";
        !          2272:                 }
1.188     raeburn  2273:             }
1.205     raeburn  2274:             $outcome .= &Apache::loncommon::end_data_table();
1.188     raeburn  2275:         } else {
1.418   ! raeburn  2276:             if (&Apache::lonnet::allowed('udp',$ccdomain)) {
        !          2277:                 # Current user has rights to view domain preferences for user's domain
        !          2278:                 my $result;
        !          2279:                 if ($currentauth =~ /^krb(4|5):([^:]*)$/) {
        !          2280:                     my ($krbver,$krbrealm) = ($1,$2);
        !          2281:                     if ($krbrealm eq '') {
        !          2282:                         $result = &mt('Currently Kerberos authenticated, Version [_1].',$krbver);
        !          2283:                     } else {
        !          2284:                         $result = &mt('Currently Kerberos authenticated with domain [_1] Version [_2].',
        !          2285:                                       $krbver,$krbrealm);
        !          2286:                     }
        !          2287:                 } elsif ($currentauth =~ /^internal:/) {
        !          2288:                     $result = &mt('Currently internally authenticated.');
        !          2289:                 } elsif ($currentauth =~ /^localauth:/) {
        !          2290:                     $result = &mt('Currently using local (institutional) authentication.');
        !          2291:                 } elsif ($currentauth =~ /^unix:/) {
        !          2292:                     $result = &mt('Currently Filesystem Authenticated.');
        !          2293:                 }
        !          2294:                 $outcome = '<h3>'.$lt{'ld'}.'</h3>'.
        !          2295:                            &Apache::loncommon::start_data_table().
        !          2296:                            &Apache::loncommon::start_data_table_row().
        !          2297:                            '<td>'.$result.'</td>'.
        !          2298:                            &Apache::loncommon::end_data_table_row()."\n".
        !          2299:                            &Apache::loncommon::end_data_table();
        !          2300:             } elsif (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
1.188     raeburn  2301:                 my %lt=&Apache::lonlocal::texthash(
                   2302:                            'ccld'  => "Change Current Login Data",
                   2303:                            'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
                   2304:                            'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   2305:                 );
                   2306:                 $outcome .= <<ENDNOPRIV;
                   2307: <h3>$lt{'ccld'}</h3>
                   2308: $lt{'yodo'} $lt{'ifch'}: $ccdomain
1.235     raeburn  2309: <input type="hidden" name="login" value="nochange" />
1.188     raeburn  2310: ENDNOPRIV
                   2311:             }
                   2312:         }
                   2313:     }  ## End of "check for bad authentication type" logic
                   2314:     return $outcome;
                   2315: }
                   2316: 
1.187     raeburn  2317: sub modify_login_block {
                   2318:     my ($dom,$currentauth) = @_;
                   2319:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2320:     my ($authnum,%can_assign) =
                   2321:         &Apache::loncommon::get_assignable_auth($dom);
1.205     raeburn  2322:     my ($authformcurrent,@authform_others,$show_override_msg);
1.187     raeburn  2323:     if ($currentauth=~/^krb(4|5):/) {
                   2324:         $authformcurrent=$authformkrb;
                   2325:         if ($can_assign{'int'}) {
1.205     raeburn  2326:             push(@authform_others,$authformint);
1.187     raeburn  2327:         }
                   2328:         if ($can_assign{'loc'}) {
1.205     raeburn  2329:             push(@authform_others,$authformloc);
1.187     raeburn  2330:         }
                   2331:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   2332:             $show_override_msg = 1;
                   2333:         }
                   2334:     } elsif ($currentauth=~/^internal:/) {
                   2335:         $authformcurrent=$authformint;
                   2336:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2337:             push(@authform_others,$authformkrb);
1.187     raeburn  2338:         }
                   2339:         if ($can_assign{'loc'}) {
1.205     raeburn  2340:             push(@authform_others,$authformloc);
1.187     raeburn  2341:         }
                   2342:         if ($can_assign{'int'}) {
                   2343:             $show_override_msg = 1;
                   2344:         }
                   2345:     } elsif ($currentauth=~/^unix:/) {
                   2346:         $authformcurrent=$authformfsys;
                   2347:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2348:             push(@authform_others,$authformkrb);
1.187     raeburn  2349:         }
                   2350:         if ($can_assign{'int'}) {
1.205     raeburn  2351:             push(@authform_others,$authformint);
1.187     raeburn  2352:         }
                   2353:         if ($can_assign{'loc'}) {
1.205     raeburn  2354:             push(@authform_others,$authformloc);
1.187     raeburn  2355:         }
                   2356:         if ($can_assign{'fsys'}) {
                   2357:             $show_override_msg = 1;
                   2358:         }
                   2359:     } elsif ($currentauth=~/^localauth:/) {
                   2360:         $authformcurrent=$authformloc;
                   2361:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2362:             push(@authform_others,$authformkrb);
1.187     raeburn  2363:         }
                   2364:         if ($can_assign{'int'}) {
1.205     raeburn  2365:             push(@authform_others,$authformint);
1.187     raeburn  2366:         }
                   2367:         if ($can_assign{'loc'}) {
                   2368:             $show_override_msg = 1;
                   2369:         }
                   2370:     }
                   2371:     if ($show_override_msg) {
1.205     raeburn  2372:         $authformcurrent = '<table><tr><td colspan="3">'.$authformcurrent.
                   2373:                            '</td></tr>'."\n".
                   2374:                            '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
                   2375:                            '<td><b>'.&mt('Currently in use').'</b></td>'.
                   2376:                            '<td align="right"><span class="LC_cusr_emph">'.
1.187     raeburn  2377:                             &mt('will override current values').
1.205     raeburn  2378:                             '</span></td></tr></table>';
1.187     raeburn  2379:     }
1.205     raeburn  2380:     return ($authformcurrent,$show_override_msg,@authform_others); 
1.187     raeburn  2381: }
                   2382: 
1.188     raeburn  2383: sub personal_data_display {
1.391     raeburn  2384:     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$rolesarray,
1.410     raeburn  2385:         $now,$captchaform,$emailusername,$usertype) = @_;
1.388     bisitz   2386:     my ($output,%userenv,%canmodify,%canmodify_status);
1.219     raeburn  2387:     my @userinfo = ('firstname','middlename','lastname','generation',
                   2388:                     'permanentemail','id');
1.252     raeburn  2389:     my $rowcount = 0;
                   2390:     my $editable = 0;
1.391     raeburn  2391:     my %textboxsize = (
                   2392:                        firstname      => '15',
                   2393:                        middlename     => '15',
                   2394:                        lastname       => '15',
                   2395:                        generation     => '5',
                   2396:                        permanentemail => '25',
                   2397:                        id             => '15',
                   2398:                       );
                   2399: 
                   2400:     my %lt=&Apache::lonlocal::texthash(
                   2401:                 'pd'             => "Personal Data",
                   2402:                 'firstname'      => "First Name",
                   2403:                 'middlename'     => "Middle Name",
                   2404:                 'lastname'       => "Last Name",
                   2405:                 'generation'     => "Generation",
                   2406:                 'permanentemail' => "Permanent e-mail address",
                   2407:                 'id'             => "Student/Employee ID",
                   2408:                 'lg'             => "Login Data",
                   2409:                 'inststatus'     => "Affiliation",
                   2410:                 'email'          => 'E-mail address',
                   2411:                 'valid'          => 'Validation',
                   2412:     );
                   2413: 
                   2414:     %canmodify_status =
1.286     raeburn  2415:         &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
                   2416:                                                    ['inststatus'],$rolesarray);
1.253     raeburn  2417:     if (!$newuser) {
1.188     raeburn  2418:         # Get the users information
                   2419:         %userenv = &Apache::lonnet::get('environment',
                   2420:                    ['firstname','middlename','lastname','generation',
1.286     raeburn  2421:                     'permanentemail','id','inststatus'],$ccdomain,$ccuname);
1.219     raeburn  2422:         %canmodify =
                   2423:             &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
1.252     raeburn  2424:                                                        \@userinfo,$rolesarray);
1.257     raeburn  2425:     } elsif ($context eq 'selfcreate') {
1.391     raeburn  2426:         if ($newuser eq 'email') {
1.396     raeburn  2427:             if (ref($emailusername) eq 'HASH') {
                   2428:                 if (ref($emailusername->{$usertype}) eq 'HASH') {
                   2429:                     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
                   2430:                     @userinfo = ();          
                   2431:                     if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
                   2432:                         foreach my $field (@{$infofields}) { 
                   2433:                             if ($emailusername->{$usertype}->{$field}) {
                   2434:                                 push(@userinfo,$field);
                   2435:                                 $canmodify{$field} = 1;
                   2436:                                 unless ($textboxsize{$field}) {
                   2437:                                     $textboxsize{$field} = 25;
                   2438:                                 }
                   2439:                                 unless ($lt{$field}) {
                   2440:                                     $lt{$field} = $infotitles->{$field};
                   2441:                                 }
                   2442:                                 if ($emailusername->{$usertype}->{$field} eq 'required') {
                   2443:                                     $lt{$field} .= '<b>*</b>';
                   2444:                                 }
1.391     raeburn  2445:                             }
                   2446:                         }
                   2447:                     }
                   2448:                 }
                   2449:             }
                   2450:         } else {
                   2451:             %canmodify = &selfcreate_canmodify($context,$ccdomain,\@userinfo,
                   2452:                                                $inst_results,$rolesarray);
                   2453:         }
1.188     raeburn  2454:     }
1.391     raeburn  2455: 
1.188     raeburn  2456:     my $genhelp=&Apache::loncommon::help_open_topic('Generation');
                   2457:     $output = '<h3>'.$lt{'pd'}.'</h3>'.
                   2458:               &Apache::lonhtmlcommon::start_pick_box();
1.391     raeburn  2459:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
1.396     raeburn  2460:         $output .= &Apache::lonhtmlcommon::row_title($lt{'email'}.'<b>*</b>',undef,
1.391     raeburn  2461:                                                      'LC_oddrow_value')."\n".
1.394     raeburn  2462:                    '<input type="text" name="uname" size="25" value="" autocomplete="off" />';
1.391     raeburn  2463:         $rowcount ++;
                   2464:         $output .= &Apache::lonhtmlcommon::row_closure(1);
1.408     raeburn  2465:         my $upassone = '<input type="password" name="upass'.$now.'" size="20" autocomplete="off" />';
                   2466:         my $upasstwo = '<input type="password" name="upasscheck'.$now.'" size="20" autocomplete="off" />';
1.396     raeburn  2467:         $output .= &Apache::lonhtmlcommon::row_title(&mt('Password').'<b>*</b>',
1.391     raeburn  2468:                                                     'LC_pick_box_title',
                   2469:                                                     'LC_oddrow_value')."\n".
                   2470:                    $upassone."\n".
                   2471:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
1.396     raeburn  2472:                    &Apache::lonhtmlcommon::row_title(&mt('Confirm password').'<b>*</b>',
1.391     raeburn  2473:                                                      'LC_pick_box_title',
                   2474:                                                      'LC_oddrow_value')."\n".
                   2475:                    $upasstwo.
                   2476:                    &Apache::lonhtmlcommon::row_closure()."\n";
                   2477:     }
1.188     raeburn  2478:     foreach my $item (@userinfo) {
                   2479:         my $rowtitle = $lt{$item};
1.252     raeburn  2480:         my $hiderow = 0;
1.188     raeburn  2481:         if ($item eq 'generation') {
                   2482:             $rowtitle = $genhelp.$rowtitle;
                   2483:         }
1.252     raeburn  2484:         my $row = &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
1.188     raeburn  2485:         if ($newuser) {
1.210     raeburn  2486:             if (ref($inst_results) eq 'HASH') {
                   2487:                 if ($inst_results->{$item} ne '') {
1.252     raeburn  2488:                     $row .= '<input type="hidden" name="c'.$item.'" value="'.$inst_results->{$item}.'" />'.$inst_results->{$item};
1.210     raeburn  2489:                 } else {
1.252     raeburn  2490:                     if ($context eq 'selfcreate') {
1.391     raeburn  2491:                         if ($canmodify{$item}) {
1.394     raeburn  2492:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.252     raeburn  2493:                             $editable ++;
                   2494:                         } else {
                   2495:                             $hiderow = 1;
                   2496:                         }
1.253     raeburn  2497:                     } else {
                   2498:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2499:                     }
1.210     raeburn  2500:                 }
1.188     raeburn  2501:             } else {
1.252     raeburn  2502:                 if ($context eq 'selfcreate') {
1.401     raeburn  2503:                     if ($canmodify{$item}) {
                   2504:                         if ($newuser eq 'email') {
                   2505:                             $row .= '<input type="text" name="'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.287     raeburn  2506:                         } else {
1.401     raeburn  2507:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" autocomplete="off" />';
1.287     raeburn  2508:                         }
1.401     raeburn  2509:                         $editable ++;
                   2510:                     } else {
                   2511:                         $hiderow = 1;
1.252     raeburn  2512:                     }
1.253     raeburn  2513:                 } else {
                   2514:                     $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2515:                 }
1.188     raeburn  2516:             }
                   2517:         } else {
1.219     raeburn  2518:             if ($canmodify{$item}) {
1.252     raeburn  2519:                 $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
1.393     raeburn  2520:                 if (($item eq 'id') && (!$newuser)) {
                   2521:                     $row .= '<br />'.&Apache::lonuserutils::forceid_change($context);
                   2522:                 }
1.188     raeburn  2523:             } else {
1.252     raeburn  2524:                 $row .= $userenv{$item};
1.188     raeburn  2525:             }
                   2526:         }
1.252     raeburn  2527:         $row .= &Apache::lonhtmlcommon::row_closure(1);
                   2528:         if (!$hiderow) {
                   2529:             $output .= $row;
                   2530:             $rowcount ++;
                   2531:         }
1.188     raeburn  2532:     }
1.286     raeburn  2533:     if (($canmodify_status{'inststatus'}) || ($context ne 'selfcreate')) {
                   2534:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($ccdomain);
                   2535:         if (ref($types) eq 'ARRAY') {
                   2536:             if (@{$types} > 0) {
                   2537:                 my ($hiderow,$shown);
                   2538:                 if ($canmodify_status{'inststatus'}) {
                   2539:                     $shown = &pick_inst_statuses($userenv{'inststatus'},$usertypes,$types);
                   2540:                 } else {
                   2541:                     if ($userenv{'inststatus'} eq '') {
                   2542:                         $hiderow = 1;
1.334     raeburn  2543:                     } else {
                   2544:                         my @showitems;
                   2545:                         foreach my $item ( map { &unescape($_); } split(':',$userenv{'inststatus'})) {
                   2546:                             if (exists($usertypes->{$item})) {
                   2547:                                 push(@showitems,$usertypes->{$item});
                   2548:                             } else {
                   2549:                                 push(@showitems,$item);
                   2550:                             }
                   2551:                         }
                   2552:                         if (@showitems) {
                   2553:                             $shown = join(', ',@showitems);
                   2554:                         } else {
                   2555:                             $hiderow = 1;
                   2556:                         }
1.286     raeburn  2557:                     }
                   2558:                 }
                   2559:                 if (!$hiderow) {
1.389     bisitz   2560:                     my $row = &Apache::lonhtmlcommon::row_title(&mt('Affiliations'),undef,'LC_oddrow_value')."\n".
1.286     raeburn  2561:                               $shown.&Apache::lonhtmlcommon::row_closure(1); 
                   2562:                     if ($context eq 'selfcreate') {
                   2563:                         $rowcount ++;
                   2564:                     }
                   2565:                     $output .= $row;
                   2566:                 }
                   2567:             }
                   2568:         }
                   2569:     }
1.391     raeburn  2570:     if (($context eq 'selfcreate') && ($newuser eq 'email')) {
                   2571:         if ($captchaform) {
1.410     raeburn  2572:             $output .= &Apache::lonhtmlcommon::row_title($lt{'valid'}.'*',
                   2573:                                                          'LC_pick_box_title')."\n".
                   2574:                        $captchaform."\n".'<br /><br />'.
1.391     raeburn  2575:                        &Apache::lonhtmlcommon::row_closure(1); 
                   2576:             $rowcount ++;
                   2577:         }
                   2578:         my $submit_text = &mt('Create account');
                   2579:         $output .= &Apache::lonhtmlcommon::row_title()."\n".
                   2580:                    '<br /><input type="submit" name="createaccount" value="'.
                   2581:                    $submit_text.'" />'.
1.396     raeburn  2582:                    '<input type="hidden" name="type" value="'.$usertype.'" />'.
1.391     raeburn  2583:                    &Apache::lonhtmlcommon::row_closure(1);
                   2584:     }
1.188     raeburn  2585:     $output .= &Apache::lonhtmlcommon::end_pick_box();
1.206     raeburn  2586:     if (wantarray) {
1.252     raeburn  2587:         if ($context eq 'selfcreate') {
                   2588:             return($output,$rowcount,$editable);
                   2589:         } else {
1.388     bisitz   2590:             return $output;
1.252     raeburn  2591:         }
1.206     raeburn  2592:     } else {
                   2593:         return $output;
                   2594:     }
1.188     raeburn  2595: }
                   2596: 
1.286     raeburn  2597: sub pick_inst_statuses {
                   2598:     my ($curr,$usertypes,$types) = @_;
                   2599:     my ($output,$rem,@currtypes);
                   2600:     if ($curr ne '') {
                   2601:         @currtypes = map { &unescape($_); } split(/:/,$curr);
                   2602:     }
                   2603:     my $numinrow = 2;
                   2604:     if (ref($types) eq 'ARRAY') {
                   2605:         $output = '<table>';
                   2606:         my $lastcolspan; 
                   2607:         for (my $i=0; $i<@{$types}; $i++) {
                   2608:             if (defined($usertypes->{$types->[$i]})) {
                   2609:                 my $rem = $i%($numinrow);
                   2610:                 if ($rem == 0) {
                   2611:                     if ($i<@{$types}-1) {
                   2612:                         if ($i > 0) { 
                   2613:                             $output .= '</tr>';
                   2614:                         }
                   2615:                         $output .= '<tr>';
                   2616:                     }
                   2617:                 } elsif ($i==@{$types}-1) {
                   2618:                     my $colsleft = $numinrow - $rem;
                   2619:                     if ($colsleft > 1) {
                   2620:                         $lastcolspan = ' colspan="'.$colsleft.'"';
                   2621:                     }
                   2622:                 }
                   2623:                 my $check = ' ';
                   2624:                 if (grep(/^\Q$types->[$i]\E$/,@currtypes)) {
                   2625:                     $check = ' checked="checked" ';
                   2626:                 }
                   2627:                 $output .= '<td class="LC_left_item"'.$lastcolspan.'>'.
                   2628:                            '<span class="LC_nobreak"><label>'.
                   2629:                            '<input type="checkbox" name="inststatus" '.
                   2630:                            'value="'.$types->[$i].'"'.$check.'/>'.
                   2631:                            $usertypes->{$types->[$i]}.'</label></span></td>';
                   2632:             }
                   2633:         }
                   2634:         $output .= '</tr></table>';
                   2635:     }
                   2636:     return $output;
                   2637: }
                   2638: 
1.257     raeburn  2639: sub selfcreate_canmodify {
                   2640:     my ($context,$dom,$userinfo,$inst_results,$rolesarray) = @_;
                   2641:     if (ref($inst_results) eq 'HASH') {
                   2642:         my @inststatuses = &get_inststatuses($inst_results);
                   2643:         if (@inststatuses == 0) {
                   2644:             @inststatuses = ('default');
                   2645:         }
                   2646:         $rolesarray = \@inststatuses;
                   2647:     }
                   2648:     my %canmodify =
                   2649:         &Apache::lonuserutils::can_modify_userinfo($context,$dom,$userinfo,
                   2650:                                                    $rolesarray);
                   2651:     return %canmodify;
                   2652: }
                   2653: 
1.252     raeburn  2654: sub get_inststatuses {
                   2655:     my ($insthashref) = @_;
                   2656:     my @inststatuses = ();
                   2657:     if (ref($insthashref) eq 'HASH') {
                   2658:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
                   2659:             @inststatuses = @{$insthashref->{'inststatus'}};
                   2660:         }
                   2661:     }
                   2662:     return @inststatuses;
                   2663: }
                   2664: 
1.4       www      2665: # ================================================================= Phase Three
1.42      matthew  2666: sub update_user_data {
1.375     raeburn  2667:     my ($r,$context,$crstype,$brcrum,$showcredits) = @_; 
1.101     albertel 2668:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                   2669:                                           $env{'form.ccdomain'});
1.27      matthew  2670:     # Error messages
1.188     raeburn  2671:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
1.193     raeburn  2672:     my $end       = '</span><br /><br />';
                   2673:     my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
1.188     raeburn  2674:                     "'$env{'form.prevphase'}','modify')".'" />'.
1.219     raeburn  2675:                     &mt('Return to previous page').'</a>'.
                   2676:                     &Apache::loncommon::end_page();
                   2677:     my $now = time;
1.40      www      2678:     my $title;
1.101     albertel 2679:     if (exists($env{'form.makeuser'})) {
1.40      www      2680: 	$title='Set Privileges for New User';
                   2681:     } else {
                   2682:         $title='Modify User Privileges';
                   2683:     }
1.213     raeburn  2684:     my $newuser = 0;
1.160     raeburn  2685:     my ($jsback,$elements) = &crumb_utilities();
                   2686:     my $jscript = '<script type="text/javascript">'."\n".
1.301     bisitz   2687:                   '// <![CDATA['."\n".
                   2688:                   $jsback."\n".
                   2689:                   '// ]]>'."\n".
                   2690:                   '</script>'."\n";
1.318     raeburn  2691:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.351     raeburn  2692:     push (@{$brcrum},
                   2693:              {href => "javascript:backPage(document.userupdate)",
                   2694:               text => $breadcrumb_text{'search'},
                   2695:               faq  => 282,
                   2696:               bug  => 'Instructor Interface',}
                   2697:              );
                   2698:     if ($env{'form.prevphase'} eq 'userpicked') {
                   2699:         push(@{$brcrum},
                   2700:                {href => "javascript:backPage(document.userupdate,'get_user_info','select')",
                   2701:                 text => $breadcrumb_text{'userpicked'},
                   2702:                 faq  => 282,
                   2703:                 bug  => 'Instructor Interface',});
1.233     raeburn  2704:     }
1.224     raeburn  2705:     my $helpitem = 'Course_Change_Privileges';
                   2706:     if ($env{'form.action'} eq 'singlestudent') {
                   2707:         $helpitem = 'Course_Add_Student';
                   2708:     }
1.351     raeburn  2709:     push(@{$brcrum}, 
                   2710:             {href => "javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
                   2711:              text => $breadcrumb_text{'modify'},
                   2712:              faq  => 282,
                   2713:              bug  => 'Instructor Interface',},
                   2714:             {href => "/adm/createuser",
                   2715:              text => "Result",
                   2716:              faq  => 282,
                   2717:              bug  => 'Instructor Interface',
                   2718:              help => $helpitem});
                   2719:     my $args = {bread_crumbs          => $brcrum,
                   2720:                 bread_crumbs_component => 'User Management'};
                   2721:     if ($env{'form.popup'}) {
                   2722:         $args->{'no_nav_bar'} = 1;
                   2723:     }
                   2724:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
1.188     raeburn  2725:     $r->print(&update_result_form($uhome));
1.27      matthew  2726:     # Check Inputs
1.101     albertel 2727:     if (! $env{'form.ccuname'} ) {
1.193     raeburn  2728: 	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
1.27      matthew  2729: 	return;
                   2730:     }
1.138     albertel 2731:     if (  $env{'form.ccuname'} ne 
                   2732: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
1.281     bisitz   2733: 	$r->print($error.&mt('Invalid login name.').'  '.
                   2734: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid.').
1.193     raeburn  2735: 		  $end.$rtnlink);
1.27      matthew  2736: 	return;
                   2737:     }
1.101     albertel 2738:     if (! $env{'form.ccdomain'}       ) {
1.193     raeburn  2739: 	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
1.27      matthew  2740: 	return;
                   2741:     }
1.138     albertel 2742:     if (  $env{'form.ccdomain'} ne
                   2743: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
1.281     bisitz   2744: 	$r->print($error.&mt('Invalid domain name.').'  '.
                   2745: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid.').
1.193     raeburn  2746: 		  $end.$rtnlink);
1.27      matthew  2747: 	return;
                   2748:     }
1.219     raeburn  2749:     if ($uhome eq 'no_host') {
                   2750:         $newuser = 1;
                   2751:     }
1.101     albertel 2752:     if (! exists($env{'form.makeuser'})) {
1.29      matthew  2753:         # Modifying an existing user, so check the validity of the name
                   2754:         if ($uhome eq 'no_host') {
1.389     bisitz   2755:             $r->print(
                   2756:                 $error
                   2757:                .'<p class="LC_error">'
                   2758:                .&mt('Unable to determine home server for [_1] in domain [_2].',
                   2759:                         '"'.$env{'form.ccuname'}.'"','"'.$env{'form.ccdomain'}.'"')
                   2760:                .'</p>');
1.29      matthew  2761:             return;
                   2762:         }
                   2763:     }
1.27      matthew  2764:     # Determine authentication method and password for the user being modified
                   2765:     my $amode='';
                   2766:     my $genpwd='';
1.101     albertel 2767:     if ($env{'form.login'} eq 'krb') {
1.41      albertel 2768: 	$amode='krb';
1.101     albertel 2769: 	$amode.=$env{'form.krbver'};
                   2770: 	$genpwd=$env{'form.krbarg'};
                   2771:     } elsif ($env{'form.login'} eq 'int') {
1.27      matthew  2772: 	$amode='internal';
1.101     albertel 2773: 	$genpwd=$env{'form.intarg'};
                   2774:     } elsif ($env{'form.login'} eq 'fsys') {
1.27      matthew  2775: 	$amode='unix';
1.101     albertel 2776: 	$genpwd=$env{'form.fsysarg'};
                   2777:     } elsif ($env{'form.login'} eq 'loc') {
1.27      matthew  2778: 	$amode='localauth';
1.101     albertel 2779: 	$genpwd=$env{'form.locarg'};
1.27      matthew  2780: 	$genpwd=" " if (!$genpwd);
1.101     albertel 2781:     } elsif (($env{'form.login'} eq 'nochange') ||
                   2782:              ($env{'form.login'} eq ''        )) { 
1.34      matthew  2783:         # There is no need to tell the user we did not change what they
                   2784:         # did not ask us to change.
1.35      matthew  2785:         # If they are creating a new user but have not specified login
                   2786:         # information this will be caught below.
1.30      matthew  2787:     } else {
1.367     golterma 2788:             $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);
                   2789:             return;
1.27      matthew  2790:     }
1.164     albertel 2791: 
1.188     raeburn  2792:     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
1.367     golterma 2793:                         $env{'form.ccuname'}.' ('.&Apache::loncommon::plainname($env{'form.ccuname'},
                   2794:                         $env{'form.ccdomain'}).')', $env{'form.ccdomain'}).'</h3>');
                   2795:     my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,2);
1.344     bisitz   2796: 
1.193     raeburn  2797:     my (%alerts,%rulematch,%inst_results,%curr_rules);
1.334     raeburn  2798:     my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
1.361     raeburn  2799:     my @usertools = ('aboutme','blog','webdav','portfolio');
1.411     raeburn  2800:     my @requestcourses = ('official','unofficial','community','textbook','placement');
1.362     raeburn  2801:     my @requestauthor = ('requestauthor');
1.286     raeburn  2802:     my ($othertitle,$usertypes,$types) = 
                   2803:         &Apache::loncommon::sorted_inst_types($env{'form.ccdomain'});
1.334     raeburn  2804:     my %canmodify_status =
                   2805:         &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},
                   2806:                                                    ['inststatus']);
1.101     albertel 2807:     if ($env{'form.makeuser'}) {
1.164     albertel 2808: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
1.27      matthew  2809:         # Check for the authentication mode and password
                   2810:         if (! $amode || ! $genpwd) {
1.193     raeburn  2811: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.27      matthew  2812: 	    return;
1.18      albertel 2813: 	}
1.29      matthew  2814:         # Determine desired host
1.101     albertel 2815:         my $desiredhost = $env{'form.hserver'};
1.29      matthew  2816:         if (lc($desiredhost) eq 'default') {
                   2817:             $desiredhost = undef;
                   2818:         } else {
1.147     albertel 2819:             my %home_servers = 
                   2820: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
1.29      matthew  2821:             if (! exists($home_servers{$desiredhost})) {
1.193     raeburn  2822:                 $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
                   2823:                 return;
                   2824:             }
                   2825:         }
                   2826:         # Check ID format
                   2827:         my %checkhash;
                   2828:         my %checks = ('id' => 1);
                   2829:         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
1.219     raeburn  2830:             'newuser' => $newuser, 
1.196     raeburn  2831:             'id' => $env{'form.cid'},
1.193     raeburn  2832:         );
1.196     raeburn  2833:         if ($env{'form.cid'} ne '') {
                   2834:             &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   2835:                                           \%rulematch,\%inst_results,\%curr_rules);
                   2836:             if (ref($alerts{'id'}) eq 'HASH') {
                   2837:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
                   2838:                     my $domdesc =
                   2839:                         &Apache::lonnet::domain($env{'form.ccdomain'},'description');
                   2840:                     if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
                   2841:                         my $userchkmsg;
                   2842:                         if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
                   2843:                             $userchkmsg  = 
                   2844:                                 &Apache::loncommon::instrule_disallow_msg('id',
                   2845:                                                                     $domdesc,1).
                   2846:                                 &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
                   2847:                                     $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
                   2848:                         }
                   2849:                         $r->print($error.&mt('Invalid ID format').$end.
                   2850:                                   $userchkmsg.$rtnlink);
                   2851:                         return;
                   2852:                     }
                   2853:                 }
1.29      matthew  2854:             }
                   2855:         }
1.367     golterma 2856:         &Apache::lonhtmlcommon::Increment_PrgWin($r, \%prog_state);
1.27      matthew  2857: 	# Call modifyuser
                   2858: 	my $result = &Apache::lonnet::modifyuser
1.193     raeburn  2859: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
1.188     raeburn  2860:              $amode,$genpwd,$env{'form.cfirstname'},
                   2861:              $env{'form.cmiddlename'},$env{'form.clastname'},
                   2862:              $env{'form.cgeneration'},undef,$desiredhost,
                   2863:              $env{'form.cpermanentemail'});
1.77      www      2864: 	$r->print(&mt('Generating user').': '.$result);
1.219     raeburn  2865:         $uhome = &Apache::lonnet::homeserver($env{'form.ccuname'},
1.101     albertel 2866:                                                $env{'form.ccdomain'});
1.334     raeburn  2867:         my (%changeHash,%newcustom,%changed,%changedinfo);
1.267     raeburn  2868:         if ($uhome ne 'no_host') {
1.334     raeburn  2869:             if ($context eq 'domain') {
1.378     raeburn  2870:                 foreach my $name ('portfolio','author') {
                   2871:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   2872:                         if ($env{'form.'.$name.'quota'} eq '') {
                   2873:                             $newcustom{$name.'quota'} = 0;
                   2874:                         } else {
                   2875:                             $newcustom{$name.'quota'} = $env{'form.'.$name.'quota'};
                   2876:                             $newcustom{$name.'quota'} =~ s/[^\d\.]//g;
                   2877:                         }
                   2878:                         if (&quota_admin($newcustom{$name.'quota'},\%changeHash,$name)) {
                   2879:                             $changed{$name.'quota'} = 1;
                   2880:                         }
1.334     raeburn  2881:                     }
                   2882:                 }
                   2883:                 foreach my $item (@usertools) {
                   2884:                     if ($env{'form.custom'.$item} == 1) {
                   2885:                         $newcustom{$item} = $env{'form.tools_'.$item};
                   2886:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2887:                                                      \%changeHash,'tools');
                   2888:                     }
1.267     raeburn  2889:                 }
1.334     raeburn  2890:                 foreach my $item (@requestcourses) {
1.341     raeburn  2891:                     if ($env{'form.custom'.$item} == 1) {
                   2892:                         $newcustom{$item} = $env{'form.crsreq_'.$item};
                   2893:                         if ($env{'form.crsreq_'.$item} eq 'autolimit') {
                   2894:                             $newcustom{$item} .= '=';
1.383     raeburn  2895:                             $env{'form.crsreq_'.$item.'_limit'} =~ s/\D+//g;
                   2896:                             if ($env{'form.crsreq_'.$item.'_limit'}) {
1.341     raeburn  2897:                                 $newcustom{$item} .= $env{'form.crsreq_'.$item.'_limit'};
                   2898:                             }
1.334     raeburn  2899:                         }
1.341     raeburn  2900:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2901:                                                       \%changeHash,'requestcourses');
1.334     raeburn  2902:                     }
1.275     raeburn  2903:                 }
1.362     raeburn  2904:                 if ($env{'form.customrequestauthor'} == 1) {
                   2905:                     $newcustom{'requestauthor'} = $env{'form.requestauthor'};
                   2906:                     $changed{'requestauthor'} = &tool_admin('requestauthor',
                   2907:                                                     $newcustom{'requestauthor'},
                   2908:                                                     \%changeHash,'requestauthor');
                   2909:                 }
1.414     raeburn  2910:                 if (&Apache::lonnet::allowed('cdh',$env{'request.role.domain'})) {
                   2911:                     my @adds = &Apache::loncommon::get_env_multiple('form.adhocroleadd');
                   2912:                     if (&adhocrole_changes(\%changeHash)) {
                   2913:                         $changed{'adhocroles.'.$env{'request.role.domain'}} = $changeHash{'adhocroles.'.$env{'request.role.domain'}};
1.417     raeburn  2914:                     }
1.414     raeburn  2915:                 }
1.275     raeburn  2916:             }
1.334     raeburn  2917:             if ($canmodify_status{'inststatus'}) {
                   2918:                 if (exists($env{'form.inststatus'})) {
                   2919:                     my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2920:                     if (@inststatuses > 0) {
                   2921:                         $changeHash{'inststatus'} = join(',',@inststatuses);
                   2922:                         $changed{'inststatus'} = $changeHash{'inststatus'};
1.306     raeburn  2923:                     }
                   2924:                 }
1.232     raeburn  2925:             }
1.334     raeburn  2926:             if (keys(%changed)) {
                   2927:                 foreach my $item (@userinfo) {
                   2928:                     $changeHash{$item}  = $env{'form.c'.$item};
1.286     raeburn  2929:                 }
1.267     raeburn  2930:                 my $chgresult =
                   2931:                      &Apache::lonnet::put('environment',\%changeHash,
                   2932:                                           $env{'form.ccdomain'},$env{'form.ccuname'});
                   2933:             } 
1.232     raeburn  2934:         }
1.219     raeburn  2935:         $r->print('<br />'.&mt('Home server').': '.$uhome.' '.
                   2936:                   &Apache::lonnet::hostname($uhome));
1.101     albertel 2937:     } elsif (($env{'form.login'} ne 'nochange') &&
                   2938:              ($env{'form.login'} ne ''        )) {
1.27      matthew  2939: 	# Modify user privileges
                   2940:         if (! $amode || ! $genpwd) {
1.193     raeburn  2941: 	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
1.27      matthew  2942: 	    return;
1.20      harris41 2943: 	}
1.395     bisitz   2944: 	# Only allow authentication modification if the person has authority
1.101     albertel 2945: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.20      harris41 2946: 	    $r->print('Modifying authentication: '.
1.31      matthew  2947:                       &Apache::lonnet::modifyuserauth(
1.101     albertel 2948: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
1.21      harris41 2949:                        $amode,$genpwd));
1.102     albertel 2950:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
1.101     albertel 2951: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
1.4       www      2952: 	} else {
1.27      matthew  2953: 	    # Okay, this is a non-fatal error.
1.395     bisitz   2954: 	    $r->print($error.&mt('You do not have the authority to modify this users authentication information.').$end);    
1.27      matthew  2955: 	}
1.28      matthew  2956:     }
1.344     bisitz   2957:     $r->rflush(); # Finish display of header before time consuming actions start
1.367     golterma 2958:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state);
1.28      matthew  2959:     ##
1.375     raeburn  2960:     my (@userroles,%userupdate,$cnum,$cdom,$defaultcredits,%namechanged);
1.213     raeburn  2961:     if ($context eq 'course') {
1.375     raeburn  2962:         ($cnum,$cdom) =
                   2963:             &Apache::lonuserutils::get_course_identity();
1.318     raeburn  2964:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
1.375     raeburn  2965:         if ($showcredits) {
                   2966:            $defaultcredits = &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   2967:         }
1.213     raeburn  2968:     }
1.101     albertel 2969:     if (! $env{'form.makeuser'} ) {
1.28      matthew  2970:         # Check for need to change
                   2971:         my %userenv = &Apache::lonnet::get
1.134     raeburn  2972:             ('environment',['firstname','middlename','lastname','generation',
1.378     raeburn  2973:              'id','permanentemail','portfolioquota','authorquota','inststatus',
                   2974:              'tools.aboutme','tools.blog','tools.webdav','tools.portfolio',
1.361     raeburn  2975:              'requestcourses.official','requestcourses.unofficial',
1.384     raeburn  2976:              'requestcourses.community','requestcourses.textbook',
                   2977:              'reqcrsotherdom.official','reqcrsotherdom.unofficial',
                   2978:              'reqcrsotherdom.community','reqcrsotherdom.textbook',
1.414     raeburn  2979:              'reqcrsotherdom.placement','requestauthor',
                   2980:              'adhocroles.'.$env{'request.role.domain'}],
1.160     raeburn  2981:               $env{'form.ccdomain'},$env{'form.ccuname'});
1.28      matthew  2982:         my ($tmp) = keys(%userenv);
                   2983:         if ($tmp =~ /^(con_lost|error)/i) { 
                   2984:             %userenv = ();
                   2985:         }
1.206     raeburn  2986:         my $no_forceid_alert;
                   2987:         # Check to see if user information can be changed
                   2988:         my %domconfig =
                   2989:             &Apache::lonnet::get_dom('configuration',['usermodification'],
                   2990:                                      $env{'form.ccdomain'});
1.213     raeburn  2991:         my @statuses = ('active','future');
                   2992:         my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@statuses,undef,$env{'request.role.domain'});
                   2993:         my ($auname,$audom);
1.220     raeburn  2994:         if ($context eq 'author') {
1.206     raeburn  2995:             $auname = $env{'user.name'};
                   2996:             $audom = $env{'user.domain'};     
                   2997:         }
                   2998:         foreach my $item (keys(%roles)) {
1.220     raeburn  2999:             my ($rolenum,$roledom,$role) = split(/:/,$item,-1);
1.206     raeburn  3000:             if ($context eq 'course') {
                   3001:                 if ($cnum ne '' && $cdom ne '') {
                   3002:                     if ($rolenum eq $cnum && $roledom eq $cdom) {
                   3003:                         if (!grep(/^\Q$role\E$/,@userroles)) {
                   3004:                             push(@userroles,$role);
                   3005:                         }
                   3006:                     }
                   3007:                 }
                   3008:             } elsif ($context eq 'author') {
                   3009:                 if ($rolenum eq $auname && $roledom eq $audom) {
                   3010:                     if (!grep(/^\Q$role\E$/,@userroles)) { 
                   3011:                         push(@userroles,$role);
                   3012:                     }
                   3013:                 }
                   3014:             }
                   3015:         }
1.220     raeburn  3016:         if ($env{'form.action'} eq 'singlestudent') {
                   3017:             if (!grep(/^st$/,@userroles)) {
                   3018:                 push(@userroles,'st');
                   3019:             }
                   3020:         } else {
                   3021:             # Check for course or co-author roles being activated or re-enabled
                   3022:             if ($context eq 'author' || $context eq 'course') {
                   3023:                 foreach my $key (keys(%env)) {
                   3024:                     if ($context eq 'author') {
                   3025:                         if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
                   3026:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3027:                                 push(@userroles,$1);
                   3028:                             }
                   3029:                         } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
                   3030:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3031:                                 push(@userroles,$1);
                   3032:                             }
1.206     raeburn  3033:                         }
1.220     raeburn  3034:                     } elsif ($context eq 'course') {
                   3035:                         if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
                   3036:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3037:                                 push(@userroles,$1);
                   3038:                             }
                   3039:                         } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
                   3040:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   3041:                                 push(@userroles,$1);
                   3042:                             }
1.206     raeburn  3043:                         }
                   3044:                     }
                   3045:                 }
                   3046:             }
                   3047:         }
                   3048:         #Check to see if we can change personal data for the user 
                   3049:         my (@mod_disallowed,@longroles);
                   3050:         foreach my $role (@userroles) {
                   3051:             if ($role eq 'cr') {
                   3052:                 push(@longroles,'Custom');
                   3053:             } else {
1.318     raeburn  3054:                 push(@longroles,&Apache::lonnet::plaintext($role,$crstype)); 
1.206     raeburn  3055:             }
                   3056:         }
1.219     raeburn  3057:         my %canmodify = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},\@userinfo,\@userroles);
                   3058:         foreach my $item (@userinfo) {
1.28      matthew  3059:             # Strip leading and trailing whitespace
1.203     raeburn  3060:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
1.219     raeburn  3061:             if (!$canmodify{$item}) {
1.207     raeburn  3062:                 if (defined($env{'form.c'.$item})) {
                   3063:                     if ($env{'form.c'.$item} ne $userenv{$item}) {
                   3064:                         push(@mod_disallowed,$item);
                   3065:                     }
1.206     raeburn  3066:                 }
                   3067:                 $env{'form.c'.$item} = $userenv{$item};
                   3068:             }
1.28      matthew  3069:         }
1.259     bisitz   3070:         # Check to see if we can change the Student/Employee ID
1.196     raeburn  3071:         my $forceid = $env{'form.forceid'};
                   3072:         my $recurseid = $env{'form.recurseid'};
                   3073:         my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
1.203     raeburn  3074:         my %uidhash = &Apache::lonnet::idrget($env{'form.ccdomain'},
                   3075:                                             $env{'form.ccuname'});
                   3076:         if (($uidhash{$env{'form.ccuname'}}) && 
                   3077:             ($uidhash{$env{'form.ccuname'}}!~/error\:/) && 
                   3078:             (!$forceid)) {
                   3079:             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
                   3080:                 $env{'form.cid'} = $userenv{'id'};
1.293     bisitz   3081:                 $no_forceid_alert = &mt('New student/employee ID does not match existing ID for this user.')
1.259     bisitz   3082:                                    .'<br />'
                   3083:                                    .&mt("Change is not permitted without checking the 'Force ID change' checkbox on the previous page.")
                   3084:                                    .'<br />'."\n";
1.203     raeburn  3085:             }
                   3086:         }
                   3087:         if ($env{'form.cid'} ne $userenv{'id'}) {
1.196     raeburn  3088:             my $checkhash;
                   3089:             my $checks = { 'id' => 1 };
                   3090:             $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
                   3091:                    { 'newuser' => $newuser,
                   3092:                      'id'  => $env{'form.cid'}, 
                   3093:                    };
                   3094:             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   3095:                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
                   3096:             if (ref($alerts{'id'}) eq 'HASH') {
                   3097:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
1.203     raeburn  3098:                    $env{'form.cid'} = $userenv{'id'};
1.196     raeburn  3099:                 }
                   3100:             }
                   3101:         }
1.378     raeburn  3102:         my (%quotachanged,%oldquota,%newquota,%olddefquota,%newdefquota, 
                   3103:             $oldinststatus,$newinststatus,%oldisdefault,%newisdefault,%oldsettings,
1.339     raeburn  3104:             %oldsettingstext,%newsettings,%newsettingstext,@disporder,
1.378     raeburn  3105:             %oldsettingstatus,%newsettingstatus);
1.334     raeburn  3106:         @disporder = ('inststatus');
                   3107:         if ($env{'request.role.domain'} eq $env{'form.ccdomain'}) {
1.362     raeburn  3108:             push(@disporder,'requestcourses','requestauthor');
1.334     raeburn  3109:         } else {
                   3110:             push(@disporder,'reqcrsotherdom');
                   3111:         }
                   3112:         push(@disporder,('quota','tools'));
1.338     raeburn  3113:         $oldinststatus = $userenv{'inststatus'};
1.378     raeburn  3114:         foreach my $name ('portfolio','author') {
                   3115:             ($olddefquota{$name},$oldsettingstatus{$name}) = 
                   3116:                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$oldinststatus,$name);
                   3117:             ($newdefquota{$name},$newsettingstatus{$name}) = ($olddefquota{$name},$oldsettingstatus{$name});
                   3118:         }
1.414     raeburn  3119:         push(@disporder,'adhocroles');
1.334     raeburn  3120:         my %canshow;
1.220     raeburn  3121:         if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
1.334     raeburn  3122:             $canshow{'quota'} = 1;
1.220     raeburn  3123:         }
1.267     raeburn  3124:         if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
1.334     raeburn  3125:             $canshow{'tools'} = 1;
1.267     raeburn  3126:         }
1.275     raeburn  3127:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
1.334     raeburn  3128:             $canshow{'requestcourses'} = 1;
1.300     raeburn  3129:         } elsif (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.334     raeburn  3130:             $canshow{'reqcrsotherdom'} = 1;
1.275     raeburn  3131:         }
1.286     raeburn  3132:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.334     raeburn  3133:             $canshow{'inststatus'} = 1;
1.286     raeburn  3134:         }
1.362     raeburn  3135:         if (&Apache::lonnet::allowed('cau',$env{'form.ccdomain'})) {
                   3136:             $canshow{'requestauthor'} = 1;
                   3137:         }
1.414     raeburn  3138:         if (&Apache::lonnet::allowed('cdh',$env{'request.role.domain'})) {
                   3139:             $canshow{'adhocroles'} = 1;
                   3140:         }
1.267     raeburn  3141:         my (%changeHash,%changed);
1.286     raeburn  3142:         if ($oldinststatus eq '') {
1.334     raeburn  3143:             $oldsettings{'inststatus'} = $othertitle; 
1.286     raeburn  3144:         } else {
                   3145:             if (ref($usertypes) eq 'HASH') {
1.334     raeburn  3146:                 $oldsettings{'inststatus'} = join(', ',map{ $usertypes->{ &unescape($_) }; } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  3147:             } else {
1.334     raeburn  3148:                 $oldsettings{'inststatus'} = join(', ',map{ &unescape($_); } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  3149:             }
                   3150:         }
                   3151:         $changeHash{'inststatus'} = $userenv{'inststatus'};
1.334     raeburn  3152:         if ($canmodify_status{'inststatus'}) {
                   3153:             $canshow{'inststatus'} = 1;
1.286     raeburn  3154:             if (exists($env{'form.inststatus'})) {
                   3155:                 my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   3156:                 if (@inststatuses > 0) {
                   3157:                     $newinststatus = join(':',map { &escape($_); } @inststatuses);
                   3158:                     $changeHash{'inststatus'} = $newinststatus;
                   3159:                     if ($newinststatus ne $oldinststatus) {
                   3160:                         $changed{'inststatus'} = $newinststatus;
1.378     raeburn  3161:                         foreach my $name ('portfolio','author') {
                   3162:                             ($newdefquota{$name},$newsettingstatus{$name}) =
                   3163:                                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   3164:                         }
1.286     raeburn  3165:                     }
                   3166:                     if (ref($usertypes) eq 'HASH') {
1.334     raeburn  3167:                         $newsettings{'inststatus'} = join(', ',map{ $usertypes->{$_}; } (@inststatuses)); 
1.286     raeburn  3168:                     } else {
1.337     raeburn  3169:                         $newsettings{'inststatus'} = join(', ',@inststatuses);
1.286     raeburn  3170:                     }
1.334     raeburn  3171:                 }
                   3172:             } else {
                   3173:                 $newinststatus = '';
                   3174:                 $changeHash{'inststatus'} = $newinststatus;
                   3175:                 $newsettings{'inststatus'} = $othertitle;
                   3176:                 if ($newinststatus ne $oldinststatus) {
                   3177:                     $changed{'inststatus'} = $changeHash{'inststatus'};
1.378     raeburn  3178:                     foreach my $name ('portfolio','author') {
                   3179:                         ($newdefquota{$name},$newsettingstatus{$name}) =
                   3180:                             &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   3181:                     }
1.286     raeburn  3182:                 }
                   3183:             }
1.334     raeburn  3184:         } elsif ($context ne 'selfcreate') {
                   3185:             $canshow{'inststatus'} = 1;
1.337     raeburn  3186:             $newsettings{'inststatus'} = $oldsettings{'inststatus'};
1.286     raeburn  3187:         }
1.378     raeburn  3188:         foreach my $name ('portfolio','author') {
                   3189:             $changeHash{$name.'quota'} = $userenv{$name.'quota'};
                   3190:         }
1.334     raeburn  3191:         if ($context eq 'domain') {
1.378     raeburn  3192:             foreach my $name ('portfolio','author') {
                   3193:                 if ($userenv{$name.'quota'} ne '') {
                   3194:                     $oldquota{$name} = $userenv{$name.'quota'};
                   3195:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   3196:                         if ($env{'form.'.$name.'quota'} eq '') {
                   3197:                             $newquota{$name} = 0;
                   3198:                         } else {
                   3199:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   3200:                             $newquota{$name} =~ s/[^\d\.]//g;
                   3201:                         }
                   3202:                         if ($newquota{$name} != $oldquota{$name}) {
                   3203:                             if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   3204:                                 $changed{$name.'quota'} = 1;
                   3205:                             }
                   3206:                         }
1.334     raeburn  3207:                     } else {
1.378     raeburn  3208:                         if (&quota_admin('',\%changeHash,$name)) {
                   3209:                             $changed{$name.'quota'} = 1;
                   3210:                             $newquota{$name} = $newdefquota{$name};
                   3211:                             $newisdefault{$name} = 1;
                   3212:                         }
1.334     raeburn  3213:                     }
1.149     raeburn  3214:                 } else {
1.378     raeburn  3215:                     $oldisdefault{$name} = 1;
                   3216:                     $oldquota{$name} = $olddefquota{$name};
                   3217:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   3218:                         if ($env{'form.'.$name.'quota'} eq '') {
                   3219:                             $newquota{$name} = 0;
                   3220:                         } else {
                   3221:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   3222:                             $newquota{$name} =~ s/[^\d\.]//g;
                   3223:                         }
                   3224:                         if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   3225:                             $changed{$name.'quota'} = 1;
                   3226:                         }
1.334     raeburn  3227:                     } else {
1.378     raeburn  3228:                         $newquota{$name} = $newdefquota{$name};
                   3229:                         $newisdefault{$name} = 1;
1.334     raeburn  3230:                     }
1.378     raeburn  3231:                 }
                   3232:                 if ($oldisdefault{$name}) {
                   3233:                     $oldsettingstext{'quota'}{$name} = &get_defaultquota_text($oldsettingstatus{$name});
1.383     raeburn  3234:                 }  else {
                   3235:                     $oldsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$oldquota{$name});
1.378     raeburn  3236:                 }
                   3237:                 if ($newisdefault{$name}) {
                   3238:                     $newsettingstext{'quota'}{$name} = &get_defaultquota_text($newsettingstatus{$name});
1.383     raeburn  3239:                 } else {
                   3240:                     $newsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$newquota{$name});
1.134     raeburn  3241:                 }
                   3242:             }
1.334     raeburn  3243:             &tool_changes('tools',\@usertools,\%oldsettings,\%oldsettingstext,\%userenv,
                   3244:                           \%changeHash,\%changed,\%newsettings,\%newsettingstext);
                   3245:             if ($env{'form.ccdomain'} eq $env{'request.role.domain'}) {
                   3246:                 &tool_changes('requestcourses',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   3247:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.384     raeburn  3248:                 &tool_changes('requestauthor',\@requestauthor,\%oldsettings,\%oldsettingstext,
                   3249:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  3250:             } else {
1.334     raeburn  3251:                 &tool_changes('reqcrsotherdom',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   3252:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  3253:             }
1.414     raeburn  3254:             if ($userenv{'adhocroles.'.$env{'request.role.domain'}}) {
                   3255:                 $changeHash{'adhocroles.'.$env{'request.role.domain'}} = $userenv{'adhocroles.'.$env{'request.role.domain'}};
                   3256:             }
                   3257:             if (&adhocrole_changes(\%changeHash,\%userenv)) {
                   3258:                 $changed{'adhocroles'} = 1;
                   3259:                 $oldsettings{'adhocroles'} = $userenv{'adhocroles.'.$env{'request.role.domain'}};
1.417     raeburn  3260:                 $newsettings{'adhocroles'} = $changeHash{'adhocroles.'.$env{'request.role.domain'}};
1.414     raeburn  3261:             }
1.149     raeburn  3262:         }
1.334     raeburn  3263:         foreach my $item (@userinfo) {
                   3264:             if ($env{'form.c'.$item} ne $userenv{$item}) {
                   3265:                 $namechanged{$item} = 1;
                   3266:             }
1.204     raeburn  3267:         }
1.378     raeburn  3268:         foreach my $name ('portfolio','author') {
1.390     bisitz   3269:             $oldsettings{'quota'}{$name} = &mt('[_1] MB',$oldquota{$name});
                   3270:             $newsettings{'quota'}{$name} = &mt('[_1] MB',$newquota{$name});
1.378     raeburn  3271:         }
1.334     raeburn  3272:         if ((keys(%namechanged) > 0) || (keys(%changed) > 0)) {
1.267     raeburn  3273:             my ($chgresult,$namechgresult);
                   3274:             if (keys(%changed) > 0) {
                   3275:                 $chgresult = 
1.204     raeburn  3276:                     &Apache::lonnet::put('environment',\%changeHash,
                   3277:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
1.267     raeburn  3278:                 if ($chgresult eq 'ok') {
                   3279:                     if (($env{'user.name'} eq $env{'form.ccuname'}) &&
                   3280:                         ($env{'user.domain'} eq $env{'form.ccdomain'})) {
1.270     raeburn  3281:                         my %newenvhash;
                   3282:                         foreach my $key (keys(%changed)) {
1.411     raeburn  3283:                             if (($key eq 'official') || ($key eq 'unofficial') ||
                   3284:                                 ($key eq 'community') || ($key eq 'textbook') ||
                   3285:                                 ($key eq 'placement')) {
1.279     raeburn  3286:                                 $newenvhash{'environment.requestcourses.'.$key} =
                   3287:                                     $changeHash{'requestcourses.'.$key};
1.362     raeburn  3288:                                 if ($changeHash{'requestcourses.'.$key}) {
1.332     raeburn  3289:                                     $newenvhash{'environment.canrequest.'.$key} = 1;
1.279     raeburn  3290:                                 } else {
                   3291:                                     $newenvhash{'environment.canrequest.'.$key} =
                   3292:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3293:                                             $key,'reload','requestcourses');
                   3294:                                 }
1.362     raeburn  3295:                             } elsif ($key eq 'requestauthor') {
                   3296:                                 $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3297:                                 if ($changeHash{$key}) {
                   3298:                                     $newenvhash{'environment.canrequest.author'} = 1;
                   3299:                                 } else {
                   3300:                                     $newenvhash{'environment.canrequest.author'} =
                   3301:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3302:                                             $key,'reload','requestauthor');
                   3303:                                 }
1.414     raeburn  3304:                             } elsif ($key eq 'adhocroles') {
                   3305:                                 $newenvhash{'adhocroles.'.$env{'request.role.domain'}} =
                   3306:                                     $changeHash{'adhocroles.'.$env{'request.role.domain'}};
1.275     raeburn  3307:                             } elsif ($key ne 'quota') {
1.270     raeburn  3308:                                 $newenvhash{'environment.tools.'.$key} = 
                   3309:                                     $changeHash{'tools.'.$key};
1.279     raeburn  3310:                                 if ($changeHash{'tools.'.$key} ne '') {
                   3311:                                     $newenvhash{'environment.availabletools.'.$key} =
                   3312:                                         $changeHash{'tools.'.$key};
                   3313:                                 } else {
                   3314:                                     $newenvhash{'environment.availabletools.'.$key} =
1.367     golterma 3315:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3316:           $key,'reload','tools');
1.279     raeburn  3317:                                 }
1.270     raeburn  3318:                             }
                   3319:                         }
1.271     raeburn  3320:                         if (keys(%newenvhash)) {
                   3321:                             &Apache::lonnet::appenv(\%newenvhash);
                   3322:                         }
1.267     raeburn  3323:                     }
                   3324:                 }
1.204     raeburn  3325:             }
1.334     raeburn  3326:             if (keys(%namechanged) > 0) {
1.337     raeburn  3327:                 foreach my $field (@userinfo) {
                   3328:                     $changeHash{$field}  = $env{'form.c'.$field};
                   3329:                 }
                   3330: # Make the change
1.204     raeburn  3331:                 $namechgresult =
                   3332:                     &Apache::lonnet::modifyuser($env{'form.ccdomain'},
                   3333:                         $env{'form.ccuname'},$changeHash{'id'},undef,undef,
                   3334:                         $changeHash{'firstname'},$changeHash{'middlename'},
                   3335:                         $changeHash{'lastname'},$changeHash{'generation'},
1.337     raeburn  3336:                         $changeHash{'id'},undef,$changeHash{'permanentemail'},undef,\@userinfo);
1.220     raeburn  3337:                 %userupdate = (
                   3338:                                lastname   => $env{'form.clastname'},
                   3339:                                middlename => $env{'form.cmiddlename'},
                   3340:                                firstname  => $env{'form.cfirstname'},
                   3341:                                generation => $env{'form.cgeneration'},
                   3342:                                id         => $env{'form.cid'},
                   3343:                              );
1.204     raeburn  3344:             }
1.334     raeburn  3345:             if (((keys(%namechanged) > 0) && $namechgresult eq 'ok') || 
1.267     raeburn  3346:                 ((keys(%changed) > 0) && $chgresult eq 'ok')) {
1.28      matthew  3347:             # Tell the user we changed the name
1.334     raeburn  3348:                 &display_userinfo($r,1,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3349:                                   \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,
1.334     raeburn  3350:                                   \%oldsettings, \%oldsettingstext,\%newsettings,
                   3351:                                   \%newsettingstext);
1.203     raeburn  3352:                 if ($env{'form.cid'} ne $userenv{'id'}) {
                   3353:                     &Apache::lonnet::idput($env{'form.ccdomain'},
1.407     raeburn  3354:                          {$env{'form.ccuname'} => $env{'form.cid'}},$uhome,'ids');
1.203     raeburn  3355:                     if (($recurseid) &&
                   3356:                         (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
                   3357:                         my $idresult = 
                   3358:                             &Apache::lonuserutils::propagate_id_change(
                   3359:                                 $env{'form.ccuname'},$env{'form.ccdomain'},
                   3360:                                 \%userupdate);
                   3361:                         $r->print('<br />'.$idresult.'<br />');
                   3362:                     }
1.196     raeburn  3363:                 }
1.149     raeburn  3364:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
                   3365:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
                   3366:                     my %newenvhash;
                   3367:                     foreach my $key (keys(%changeHash)) {
                   3368:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3369:                     }
1.238     raeburn  3370:                     &Apache::lonnet::appenv(\%newenvhash);
1.149     raeburn  3371:                 }
1.28      matthew  3372:             } else { # error occurred
1.389     bisitz   3373:                 $r->print(
                   3374:                     '<p class="LC_error">'
                   3375:                    .&mt('Unable to successfully change environment for [_1] in domain [_2].',
                   3376:                             '"'.$env{'form.ccuname'}.'"',
                   3377:                             '"'.$env{'form.ccdomain'}.'"')
                   3378:                    .'</p>');
1.28      matthew  3379:             }
1.334     raeburn  3380:         } else { # End of if ($env ... ) logic
1.275     raeburn  3381:             # They did not want to change the users name, quota, tool availability,
                   3382:             # or ability to request creation of courses, 
1.267     raeburn  3383:             # but we can still tell them what the name and quota and availabilities are  
1.334     raeburn  3384:             &display_userinfo($r,undef,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3385:                               \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,\%oldsettings,
1.334     raeburn  3386:                               \%oldsettingstext,\%newsettings,\%newsettingstext);
1.28      matthew  3387:         }
1.206     raeburn  3388:         if (@mod_disallowed) {
                   3389:             my ($rolestr,$contextname);
                   3390:             if (@longroles > 0) {
                   3391:                 $rolestr = join(', ',@longroles);
                   3392:             } else {
                   3393:                 $rolestr = &mt('No roles');
                   3394:             }
                   3395:             if ($context eq 'course') {
1.399     bisitz   3396:                 $contextname = 'course';
1.206     raeburn  3397:             } elsif ($context eq 'author') {
1.399     bisitz   3398:                 $contextname = 'co-author';
1.206     raeburn  3399:             }
                   3400:             $r->print(&mt('The following fields were not updated: ').'<ul>');
                   3401:             my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   3402:             foreach my $field (@mod_disallowed) {
                   3403:                 $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
                   3404:             }
1.207     raeburn  3405:             $r->print('</ul>');
                   3406:             if (@mod_disallowed == 1) {
1.399     bisitz   3407:                 $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  3408:             } else {
1.399     bisitz   3409:                 $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  3410:             }
1.292     bisitz   3411:             my $helplink = 'javascript:helpMenu('."'display'".')';
                   3412:             $r->print('<span class="LC_cusr_emph">'.$rolestr.'</span><br />'
                   3413:                      .&mt('Please contact your [_1]helpdesk[_2] for more information.'
                   3414:                          ,'<a href="'.$helplink.'">','</a>')
                   3415:                       .'<br />');
1.206     raeburn  3416:         }
1.259     bisitz   3417:         $r->print('<span class="LC_warning">'
                   3418:                   .$no_forceid_alert
                   3419:                   .&Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules)
                   3420:                   .'</span>');
1.4       www      3421:     }
1.367     golterma 3422:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.220     raeburn  3423:     if ($env{'form.action'} eq 'singlestudent') {
1.375     raeburn  3424:         &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context,
                   3425:                                $crstype,$showcredits,$defaultcredits);
1.386     bisitz   3426:         my $linktext = ($crstype eq 'Community' ?
                   3427:             &mt('Enroll Another Member') : &mt('Enroll Another Student'));
                   3428:         $r->print(
                   3429:             &Apache::lonhtmlcommon::actionbox([
                   3430:                 '<a href="javascript:backPage(document.userupdate)">'
                   3431:                .($crstype eq 'Community' ? 
                   3432:                     &mt('Enroll Another Member') : &mt('Enroll Another Student'))
                   3433:                .'</a>']));
1.220     raeburn  3434:     } else {
1.375     raeburn  3435:         my @rolechanges = &update_roles($r,$context,$showcredits);
1.334     raeburn  3436:         if (keys(%namechanged) > 0) {
1.220     raeburn  3437:             if ($context eq 'course') {
                   3438:                 if (@userroles > 0) {
1.225     raeburn  3439:                     if ((@rolechanges == 0) || 
                   3440:                         (!(grep(/^st$/,@rolechanges)))) {
                   3441:                         if (grep(/^st$/,@userroles)) {
                   3442:                             my $classlistupdated =
                   3443:                                 &Apache::lonuserutils::update_classlist($cdom,
1.220     raeburn  3444:                                               $cnum,$env{'form.ccdomain'},
                   3445:                                        $env{'form.ccuname'},\%userupdate);
1.225     raeburn  3446:                         }
1.220     raeburn  3447:                     }
                   3448:                 }
                   3449:             }
                   3450:         }
1.226     raeburn  3451:         my $userinfo = &Apache::loncommon::plainname($env{'form.ccuname'},
1.233     raeburn  3452:                                                      $env{'form.ccdomain'});
                   3453:         if ($env{'form.popup'}) {
                   3454:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   3455:         } else {
1.367     golterma 3456:             $r->print('<br />'.&Apache::lonhtmlcommon::actionbox(['<a href="javascript:backPage(document.userupdate,'."'$env{'form.prevphase'}','modify'".')">'
                   3457:                      .&mt('Modify this user: [_1]','<span class="LC_cusr_emph">'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.' ('.$userinfo.')</span>').'</a>',
                   3458:                      '<a href="javascript:backPage(document.userupdate)">'.&mt('Create/Modify Another User').'</a>']));
1.233     raeburn  3459:         }
1.220     raeburn  3460:     }
                   3461: }
                   3462: 
1.334     raeburn  3463: sub display_userinfo {
1.362     raeburn  3464:     my ($r,$changed,$order,$canshow,$requestcourses,$usertools,$requestauthor,
                   3465:         $userenv,$changedhash,$namechangedhash,$oldsetting,$oldsettingtext,
1.334     raeburn  3466:         $newsetting,$newsettingtext) = @_;
                   3467:     return unless (ref($order) eq 'ARRAY' &&
                   3468:                    ref($canshow) eq 'HASH' && 
                   3469:                    ref($requestcourses) eq 'ARRAY' && 
1.362     raeburn  3470:                    ref($requestauthor) eq 'ARRAY' &&
1.334     raeburn  3471:                    ref($usertools) eq 'ARRAY' && 
                   3472:                    ref($userenv) eq 'HASH' &&
                   3473:                    ref($changedhash) eq 'HASH' &&
                   3474:                    ref($oldsetting) eq 'HASH' &&
                   3475:                    ref($oldsettingtext) eq 'HASH' &&
                   3476:                    ref($newsetting) eq 'HASH' &&
                   3477:                    ref($newsettingtext) eq 'HASH');
                   3478:     my %lt=&Apache::lonlocal::texthash(
1.372     raeburn  3479:          'ui'             => 'User Information',
1.334     raeburn  3480:          'uic'            => 'User Information Changed',
                   3481:          'firstname'      => 'First Name',
                   3482:          'middlename'     => 'Middle Name',
                   3483:          'lastname'       => 'Last Name',
                   3484:          'generation'     => 'Generation',
                   3485:          'id'             => 'Student/Employee ID',
                   3486:          'permanentemail' => 'Permanent e-mail address',
1.378     raeburn  3487:          'portfolioquota' => 'Disk space allocated to portfolio files',
1.385     bisitz   3488:          'authorquota'    => 'Disk space allocated to Authoring Space',
1.334     raeburn  3489:          'blog'           => 'Blog Availability',
1.361     raeburn  3490:          'webdav'         => 'WebDAV Availability',
1.334     raeburn  3491:          'aboutme'        => 'Personal Information Page Availability',
                   3492:          'portfolio'      => 'Portfolio Availability',
                   3493:          'official'       => 'Can Request Official Courses',
                   3494:          'unofficial'     => 'Can Request Unofficial Courses',
                   3495:          'community'      => 'Can Request Communities',
1.384     raeburn  3496:          'textbook'       => 'Can Request Textbook Courses',
1.411     raeburn  3497:          'placement'      => 'Can Request Placement Tests',
1.362     raeburn  3498:          'requestauthor'  => 'Can Request Author Role',
1.414     raeburn  3499:          'adhocroles'     => 'Ad Hoc Roles Selectable via Helpdesk Role',
1.334     raeburn  3500:          'inststatus'     => "Affiliation",
                   3501:          'prvs'           => 'Previous Value:',
                   3502:          'chto'           => 'Changed To:'
                   3503:     );
                   3504:     if ($changed) {
1.372     raeburn  3505:         $r->print('<h3>'.$lt{'uic'}.'</h3>'.
1.367     golterma 3506:                 &Apache::loncommon::start_data_table().
                   3507:                 &Apache::loncommon::start_data_table_header_row());
1.334     raeburn  3508:         $r->print("<th>&nbsp;</th>\n");
1.367     golterma 3509:         $r->print('<th><b>'.$lt{'prvs'}.'</b></th>');
                   3510:         $r->print('<th><span class="LC_nobreak"><b>'.$lt{'chto'}.'</b></span></th>');
                   3511:         $r->print(&Apache::loncommon::end_data_table_header_row());
                   3512:         my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
                   3513: 
1.334     raeburn  3514:         foreach my $item (@userinfo) {
                   3515:             my $value = $env{'form.c'.$item};
1.367     golterma 3516:             #show changes only:
1.383     raeburn  3517:             unless ($value eq $userenv->{$item}){
1.367     golterma 3518:                 $r->print(&Apache::loncommon::start_data_table_row());
                   3519:                 $r->print("<td>$lt{$item}</td>\n");
1.383     raeburn  3520:                 $r->print("<td>".$userenv->{$item}."</td>\n");
1.367     golterma 3521:                 $r->print("<td>$value </td>\n");
                   3522:                 $r->print(&Apache::loncommon::end_data_table_row());
1.334     raeburn  3523:             }
                   3524:         }
                   3525:         foreach my $entry (@{$order}) {
1.383     raeburn  3526:             if ($canshow->{$entry}) {
                   3527:                 if (($entry eq 'requestcourses') || ($entry eq 'reqcrsotherdom') || ($entry eq 'requestauthor')) {
                   3528:                     my @items;
                   3529:                     if ($entry eq 'requestauthor') {
                   3530:                         @items = ($entry);
                   3531:                     } else {
                   3532:                         @items = @{$requestcourses};
1.384     raeburn  3533:                     }
1.383     raeburn  3534:                     foreach my $item (@items) {
                   3535:                         if (($newsetting->{$item} ne $oldsetting->{$item}) || 
                   3536:                             ($newsettingtext->{$item} ne $oldsettingtext->{$item})) {
                   3537:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");  
                   3538:                             $r->print("<td>$lt{$item}</td>\n");
                   3539:                             $r->print("<td>".$oldsetting->{$item});
                   3540:                             if ($oldsettingtext->{$item}) {
                   3541:                                 if ($oldsetting->{$item}) {
                   3542:                                     $r->print(' -- ');
                   3543:                                 }
                   3544:                                 $r->print($oldsettingtext->{$item});
                   3545:                             }
                   3546:                             $r->print("</td>\n");
                   3547:                             $r->print("<td>".$newsetting->{$item});
                   3548:                             if ($newsettingtext->{$item}) {
                   3549:                                 if ($newsetting->{$item}) {
                   3550:                                     $r->print(' -- ');
                   3551:                                 }
                   3552:                                 $r->print($newsettingtext->{$item});
                   3553:                             }
                   3554:                             $r->print("</td>\n");
                   3555:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3556:                         }
                   3557:                     }
                   3558:                 } elsif ($entry eq 'tools') {
                   3559:                     foreach my $item (@{$usertools}) {
1.383     raeburn  3560:                         if ($newsetting->{$item} ne $oldsetting->{$item}) {
                   3561:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3562:                             $r->print("<td>$lt{$item}</td>\n");
                   3563:                             $r->print("<td>".$oldsetting->{$item}.' '.$oldsettingtext->{$item}."</td>\n");
                   3564:                             $r->print("<td>".$newsetting->{$item}.' '.$newsettingtext->{$item}."</td>\n");
                   3565:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3566:                         }
                   3567:                     }
1.378     raeburn  3568:                 } elsif ($entry eq 'quota') {
                   3569:                     if ((ref($oldsetting->{$entry}) eq 'HASH') && (ref($oldsettingtext->{$entry}) eq 'HASH') &&
                   3570:                         (ref($newsetting->{$entry}) eq 'HASH') && (ref($newsettingtext->{$entry}) eq 'HASH')) {
                   3571:                         foreach my $name ('portfolio','author') {
1.383     raeburn  3572:                             if ($newsetting->{$entry}->{$name} ne $oldsetting->{$entry}->{$name}) {
                   3573:                                 $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3574:                                 $r->print("<td>$lt{$name.$entry}</td>\n");
                   3575:                                 $r->print("<td>".$oldsettingtext->{$entry}->{$name}."</td>\n");
                   3576:                                 $r->print("<td>".$newsettingtext->{$entry}->{$name}."</td>\n");
                   3577:                                 $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.378     raeburn  3578:                             }
                   3579:                         }
                   3580:                     }
1.334     raeburn  3581:                 } else {
1.383     raeburn  3582:                     if ($newsetting->{$entry} ne $oldsetting->{$entry}) {
                   3583:                         $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3584:                         $r->print("<td>$lt{$entry}</td>\n");
                   3585:                         $r->print("<td>".$oldsetting->{$entry}.' '.$oldsettingtext->{$entry}."</td>\n");
                   3586:                         $r->print("<td>".$newsetting->{$entry}.' '.$newsettingtext->{$entry}."</td>\n");
                   3587:                         $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3588:                     }
                   3589:                 }
                   3590:             }
                   3591:         }
1.367     golterma 3592:         $r->print(&Apache::loncommon::end_data_table().'<br />');
1.372     raeburn  3593:     } else {
                   3594:         $r->print('<h3>'.$lt{'ui'}.'</h3>'.
                   3595:                   '<p>'.&mt('No changes made to user information').'</p>');
1.334     raeburn  3596:     }
                   3597:     return;
                   3598: }
                   3599: 
1.275     raeburn  3600: sub tool_changes {
                   3601:     my ($context,$usertools,$oldaccess,$oldaccesstext,$userenv,$changeHash,
                   3602:         $changed,$newaccess,$newaccesstext) = @_;
                   3603:     if (!((ref($usertools) eq 'ARRAY') && (ref($oldaccess) eq 'HASH') &&
                   3604:           (ref($oldaccesstext) eq 'HASH') && (ref($userenv) eq 'HASH') &&
                   3605:           (ref($changeHash) eq 'HASH') && (ref($changed) eq 'HASH') &&
                   3606:           (ref($newaccess) eq 'HASH') && (ref($newaccesstext) eq 'HASH'))) {
                   3607:         return;
                   3608:     }
1.383     raeburn  3609:     my %reqdisplay = &requestchange_display();
1.300     raeburn  3610:     if ($context eq 'reqcrsotherdom') {
1.309     raeburn  3611:         my @options = ('approval','validate','autolimit');
1.306     raeburn  3612:         my $optregex = join('|',@options);
1.300     raeburn  3613:         my $cdom = $env{'request.role.domain'};
                   3614:         foreach my $tool (@{$usertools}) {
1.383     raeburn  3615:             $oldaccesstext->{$tool} = &mt("availability set to 'off'");
1.314     raeburn  3616:             $newaccesstext->{$tool} = $oldaccesstext->{$tool};
1.300     raeburn  3617:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
1.383     raeburn  3618:             my ($newop,$limit);
1.314     raeburn  3619:             if ($env{'form.'.$context.'_'.$tool}) {
                   3620:                 $newop = $env{'form.'.$context.'_'.$tool};
                   3621:                 if ($newop eq 'autolimit') {
1.383     raeburn  3622:                     $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
1.314     raeburn  3623:                     $limit =~ s/\D+//g;
                   3624:                     $newop .= '='.$limit;
                   3625:                 }
                   3626:             }
1.300     raeburn  3627:             if ($userenv->{$context.'.'.$tool} eq '') {
1.314     raeburn  3628:                 if ($newop) {
                   3629:                     $changed->{$tool}=&tool_admin($tool,$cdom.':'.$newop,
1.300     raeburn  3630:                                                   $changeHash,$context);
                   3631:                     if ($changed->{$tool}) {
1.383     raeburn  3632:                         if ($newop =~ /^autolimit/) {
                   3633:                             if ($limit) {
                   3634:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3635:                             } else {
                   3636:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3637:                             }
                   3638:                         } else {
                   3639:                             $newaccesstext->{$tool} = $reqdisplay{$newop};
                   3640:                         }
1.300     raeburn  3641:                     } else {
                   3642:                         $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3643:                     }
                   3644:                 }
                   3645:             } else {
                   3646:                 my @curr = split(',',$userenv->{$context.'.'.$tool});
                   3647:                 my @new;
                   3648:                 my $changedoms;
1.314     raeburn  3649:                 foreach my $req (@curr) {
                   3650:                     if ($req =~ /^\Q$cdom\E\:($optregex\=?\d*)$/) {
                   3651:                         my $oldop = $1;
1.383     raeburn  3652:                         if ($oldop =~ /^autolimit=(\d*)/) {
                   3653:                             my $limit = $1;
                   3654:                             if ($limit) {
                   3655:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3656:                             } else {
                   3657:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3658:                             }
                   3659:                         } else {
                   3660:                             $oldaccesstext->{$tool} = $reqdisplay{$oldop};
                   3661:                         }
1.314     raeburn  3662:                         if ($oldop ne $newop) {
                   3663:                             $changedoms = 1;
                   3664:                             foreach my $item (@curr) {
                   3665:                                 my ($reqdom,$option) = split(':',$item);
                   3666:                                 unless ($reqdom eq $cdom) {
                   3667:                                     push(@new,$item);
                   3668:                                 }
                   3669:                             }
                   3670:                             if ($newop) {
                   3671:                                 push(@new,$cdom.':'.$newop);
1.300     raeburn  3672:                             }
1.314     raeburn  3673:                             @new = sort(@new);
1.300     raeburn  3674:                         }
1.314     raeburn  3675:                         last;
1.300     raeburn  3676:                     }
1.314     raeburn  3677:                 }
                   3678:                 if ((!$changedoms) && ($newop)) {
1.300     raeburn  3679:                     $changedoms = 1;
1.306     raeburn  3680:                     @new = sort(@curr,$cdom.':'.$newop);
1.300     raeburn  3681:                 }
                   3682:                 if ($changedoms) {
1.314     raeburn  3683:                     my $newdomstr;
1.300     raeburn  3684:                     if (@new) {
                   3685:                         $newdomstr = join(',',@new);
                   3686:                     }
                   3687:                     $changed->{$tool}=&tool_admin($tool,$newdomstr,$changeHash,
                   3688:                                                   $context);
                   3689:                     if ($changed->{$tool}) {
                   3690:                         if ($env{'form.'.$context.'_'.$tool}) {
1.306     raeburn  3691:                             if ($env{'form.'.$context.'_'.$tool} eq 'autolimit') {
1.314     raeburn  3692:                                 my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3693:                                 $limit =~ s/\D+//g;
                   3694:                                 if ($limit) {
1.383     raeburn  3695:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
1.314     raeburn  3696:                                 } else {
1.383     raeburn  3697:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
1.306     raeburn  3698:                                 }
1.314     raeburn  3699:                             } else {
1.306     raeburn  3700:                                 $newaccesstext->{$tool} = $reqdisplay{$env{'form.'.$context.'_'.$tool}};
                   3701:                             }
1.300     raeburn  3702:                         } else {
1.383     raeburn  3703:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
1.300     raeburn  3704:                         }
                   3705:                     }
                   3706:                 }
                   3707:             }
                   3708:         }
                   3709:         return;
                   3710:     }
1.275     raeburn  3711:     foreach my $tool (@{$usertools}) {
1.383     raeburn  3712:         my ($newval,$limit,$envkey);
1.362     raeburn  3713:         $envkey = $context.'.'.$tool;
1.306     raeburn  3714:         if ($context eq 'requestcourses') {
                   3715:             $newval = $env{'form.crsreq_'.$tool};
                   3716:             if ($newval eq 'autolimit') {
1.383     raeburn  3717:                 $limit = $env{'form.crsreq_'.$tool.'_limit'};
                   3718:                 $limit =~ s/\D+//g;
                   3719:                 $newval .= '='.$limit;
1.306     raeburn  3720:             }
1.362     raeburn  3721:         } elsif ($context eq 'requestauthor') {
                   3722:             $newval = $env{'form.'.$context};
                   3723:             $envkey = $context;
1.314     raeburn  3724:         } else {
1.306     raeburn  3725:             $newval = $env{'form.'.$context.'_'.$tool};
                   3726:         }
1.362     raeburn  3727:         if ($userenv->{$envkey} ne '') {
1.275     raeburn  3728:             $oldaccess->{$tool} = &mt('custom');
1.383     raeburn  3729:             if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3730:                 if ($userenv->{$envkey} =~ /^autolimit=(\d*)$/) {
                   3731:                     my $currlimit = $1;
                   3732:                     if ($currlimit eq '') {
                   3733:                         $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3734:                     } else {
                   3735:                         $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$currlimit);
                   3736:                     }
                   3737:                 } elsif ($userenv->{$envkey}) {
                   3738:                     $oldaccesstext->{$tool} = $reqdisplay{$userenv->{$envkey}};
                   3739:                 } else {
                   3740:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3741:                 }
1.275     raeburn  3742:             } else {
1.383     raeburn  3743:                 if ($userenv->{$envkey}) {
                   3744:                     $oldaccesstext->{$tool} = &mt("availability set to 'on'");
                   3745:                 } else {
                   3746:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3747:                 }
1.275     raeburn  3748:             }
1.362     raeburn  3749:             $changeHash->{$envkey} = $userenv->{$envkey};
1.275     raeburn  3750:             if ($env{'form.custom'.$tool} == 1) {
1.362     raeburn  3751:                 if ($newval ne $userenv->{$envkey}) {
1.306     raeburn  3752:                     $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3753:                                                     $context);
1.275     raeburn  3754:                     if ($changed->{$tool}) {
                   3755:                         $newaccess->{$tool} = &mt('custom');
1.383     raeburn  3756:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3757:                             if ($newval =~ /^autolimit/) {
                   3758:                                 if ($limit) {
                   3759:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3760:                                 } else {
                   3761:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3762:                                 }
                   3763:                             } elsif ($newval) {
                   3764:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3765:                             } else {
                   3766:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3767:                             }
1.275     raeburn  3768:                         } else {
1.383     raeburn  3769:                             if ($newval) {
                   3770:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3771:                             } else {
                   3772:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3773:                             }
1.275     raeburn  3774:                         }
                   3775:                     } else {
                   3776:                         $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  3777:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3778:                             if ($newval =~ /^autolimit/) {
                   3779:                                 if ($limit) {
                   3780:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3781:                                 } else {
                   3782:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3783:                                 }
                   3784:                             } elsif ($newval) {
                   3785:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3786:                             } else {
                   3787:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3788:                             }
1.275     raeburn  3789:                         } else {
1.383     raeburn  3790:                             if ($userenv->{$context.'.'.$tool}) {
                   3791:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3792:                             } else {
                   3793:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3794:                             }
1.275     raeburn  3795:                         }
                   3796:                     }
                   3797:                 } else {
                   3798:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3799:                     $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3800:                 }
                   3801:             } else {
                   3802:                 $changed->{$tool} = &tool_admin($tool,'',$changeHash,$context);
                   3803:                 if ($changed->{$tool}) {
                   3804:                     $newaccess->{$tool} = &mt('default');
                   3805:                 } else {
                   3806:                     $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  3807:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3808:                         if ($newval =~ /^autolimit/) {
                   3809:                             if ($limit) {
                   3810:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3811:                             } else {
                   3812:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3813:                             }
                   3814:                         } elsif ($newval) {
                   3815:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3816:                         } else {
                   3817:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3818:                         }
1.275     raeburn  3819:                     } else {
1.383     raeburn  3820:                         if ($userenv->{$context.'.'.$tool}) {
                   3821:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3822:                         } else {
                   3823:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3824:                         }
1.275     raeburn  3825:                     }
                   3826:                 }
                   3827:             }
                   3828:         } else {
                   3829:             $oldaccess->{$tool} = &mt('default');
                   3830:             if ($env{'form.custom'.$tool} == 1) {
1.306     raeburn  3831:                 $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3832:                                                 $context);
1.275     raeburn  3833:                 if ($changed->{$tool}) {
                   3834:                     $newaccess->{$tool} = &mt('custom');
1.383     raeburn  3835:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3836:                         if ($newval =~ /^autolimit/) {
                   3837:                             if ($limit) {
                   3838:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3839:                             } else {
                   3840:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3841:                             }
                   3842:                         } elsif ($newval) {
                   3843:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3844:                         } else {
                   3845:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3846:                         }
1.275     raeburn  3847:                     } else {
1.383     raeburn  3848:                         if ($newval) {
                   3849:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3850:                         } else {
                   3851:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3852:                         }
1.275     raeburn  3853:                     }
                   3854:                 } else {
                   3855:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3856:                 }
                   3857:             } else {
                   3858:                 $newaccess->{$tool} = $oldaccess->{$tool};
                   3859:             }
                   3860:         }
                   3861:     }
                   3862:     return;
                   3863: }
                   3864: 
1.414     raeburn  3865: sub adhocrole_changes {
                   3866:     my ($changehashref,$userenv) = @_;
                   3867:     my @adds = &Apache::loncommon::get_env_multiple('form.adhocroleadd');
                   3868:     my @dels = &Apache::loncommon::get_env_multiple('form.adhocroledel');
                   3869:     my (@saved,@added,@alladhoc,$changed);
                   3870:     my $adhoc_key = 'adhocroles.'.$env{'request.role.domain'};
                   3871:     if (!$env{'form.makeuser'}) {
                   3872:         if (ref($userenv) eq 'HASH') {
                   3873:             my @current;
                   3874:             if ($userenv->{$adhoc_key}) {
                   3875:                 @current = split(/,/,$userenv->{$adhoc_key});
                   3876:                 if (@dels) {
                   3877:                     foreach my $curr (@current) {
1.417     raeburn  3878:                         next if ($curr eq '');
1.414     raeburn  3879:                         unless (grep(/\Q$curr\E$/,@dels)) {
                   3880:                             push(@saved,$curr);
                   3881:                         }
                   3882:                     }
                   3883:                     $changed = 1;
                   3884:                 } else {
                   3885:                     @saved = @current;
                   3886:                 }
                   3887:             }
                   3888:         }
                   3889:     }
                   3890:     if (@adds) {
                   3891:         my $confname = &Apache::lonnet::get_domainconfiguser($env{'request.role.domain'});
                   3892:         my %existing=&Apache::lonnet::dump('roles',$env{'request.role.domain'},
                   3893:                                            $confname,'rolesdef_');
                   3894:         foreach my $poss (@adds) {
                   3895:             if (exists($existing{'rolesdef_'.$poss})) {
                   3896:                 push(@added,$poss);
                   3897:                 $changed = 1;
                   3898:             }
                   3899:         }
                   3900:     }
                   3901:     if (@added) {
                   3902:         if (@saved) {
                   3903:             foreach my $add (@added) {
                   3904:                 unless (grep(/^\Q$add\E$/,@saved)) {
                   3905:                     push(@alladhoc,$add);
                   3906:                 }
                   3907:             }
                   3908:         } else {
                   3909:             push(@alladhoc,@added);
                   3910:         }
                   3911:     }
                   3912:     if (@saved) {
                   3913:         push(@alladhoc,@saved);
                   3914:     }
                   3915:     if (@alladhoc) {
1.417     raeburn  3916:         my $adhocstr = join(',',sort(@alladhoc));
1.414     raeburn  3917:         $changehashref->{$adhoc_key} = $adhocstr;
                   3918:     } elsif (@dels) {
                   3919:         &Apache::lonnet::del('environment',[$adhoc_key],$env{'form.ccdomain'},$env{'form.ccuname'});
                   3920:         delete($changehashref->{$adhoc_key});
                   3921:         if (($env{'form.ccdomain'} eq $env{'user.domain'}) &&
                   3922:             ($env{'form.ccuname'} eq $env{'user.name'})) {
                   3923:             &Apache::lonnet::delenv($adhoc_key);
                   3924:         }
                   3925:     }
                   3926:     return $changed;
                   3927: }
                   3928: 
1.220     raeburn  3929: sub update_roles {
1.375     raeburn  3930:     my ($r,$context,$showcredits) = @_;
1.4       www      3931:     my $now=time;
1.225     raeburn  3932:     my @rolechanges;
1.220     raeburn  3933:     my %disallowed;
1.73      sakharuk 3934:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
1.404     raeburn  3935:     foreach my $key (keys(%env)) {
1.135     raeburn  3936: 	next if (! $env{$key});
1.190     raeburn  3937:         next if ($key eq 'form.action');
1.27      matthew  3938: 	# Revoke roles
1.135     raeburn  3939: 	if ($key=~/^form\.rev/) {
                   3940: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
1.64      www      3941: # Revoke standard role
1.170     albertel 3942: 		my ($scope,$role) = ($1,$2);
                   3943: 		my $result =
                   3944: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
                   3945: 						$env{'form.ccuname'},
1.239     raeburn  3946: 						$scope,$role,'','',$context);
1.367     golterma 3947:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3948:                             &mt('Revoking [_1] in [_2]',
                   3949:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  3950:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   3951:                                 $result ne "ok").'<br />');
                   3952:                 if ($result ne "ok") {
                   3953:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3954:                 }
1.170     albertel 3955: 		if ($role eq 'st') {
1.202     raeburn  3956: 		    my $result = 
1.198     raeburn  3957:                         &Apache::lonuserutils::classlist_drop($scope,
                   3958:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3959: 			    $now);
1.367     golterma 3960:                     $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.53      www      3961: 		}
1.225     raeburn  3962:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3963:                     push(@rolechanges,$role);
                   3964:                 }
1.196     raeburn  3965: 	    }
1.195     raeburn  3966: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}s) {
1.64      www      3967: # Revoke custom role
1.369     bisitz   3968:                 my $result = &Apache::lonnet::revokecustomrole(
                   3969:                     $env{'form.ccdomain'},$env{'form.ccuname'},$1,$2,$3,$4,'','',$context);
1.367     golterma 3970:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3971:                             &mt('Revoking custom role [_1] by [_2] in [_3]',
1.372     raeburn  3972:                                 $4,$3.':'.$2,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3973:                             $result ne 'ok').'<br />');
                   3974:                 if ($result ne "ok") {
                   3975:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3976:                 }
1.225     raeburn  3977:                 if (!grep(/^cr$/,@rolechanges)) {
                   3978:                     push(@rolechanges,'cr');
                   3979:                 }
1.64      www      3980: 	    }
1.135     raeburn  3981: 	} elsif ($key=~/^form\.del/) {
                   3982: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
1.116     raeburn  3983: # Delete standard role
1.170     albertel 3984: 		my ($scope,$role) = ($1,$2);
                   3985: 		my $result =
                   3986: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
                   3987: 						$env{'form.ccuname'},
1.239     raeburn  3988: 						$scope,$role,$now,0,1,'',
                   3989:                                                 $context);
1.367     golterma 3990:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   3991:                             &mt('Deleting [_1] in [_2]',
1.369     bisitz   3992:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  3993:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   3994:                             $result ne 'ok').'<br />');
                   3995:                 if ($result ne "ok") {
                   3996:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3997:                 }
1.367     golterma 3998: 
1.170     albertel 3999: 		if ($role eq 'st') {
1.202     raeburn  4000: 		    my $result = 
1.198     raeburn  4001:                         &Apache::lonuserutils::classlist_drop($scope,
                   4002:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  4003: 			    $now);
1.369     bisitz   4004: 		    $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.81      albertel 4005: 		}
1.225     raeburn  4006:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   4007:                     push(@rolechanges,$role);
                   4008:                 }
1.116     raeburn  4009:             }
1.139     albertel 4010: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  4011:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   4012: # Delete custom role
1.369     bisitz   4013:                 my $result =
                   4014:                     &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
                   4015:                         $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
                   4016:                         0,1,$context);
                   4017:                 $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Deleting custom role [_1] by [_2] in [_3]',
1.372     raeburn  4018:                       $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   4019:                       $result ne "ok").'<br />');
                   4020:                 if ($result ne "ok") {
                   4021:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4022:                 }
1.367     golterma 4023: 
1.225     raeburn  4024:                 if (!grep(/^cr$/,@rolechanges)) {
                   4025:                     push(@rolechanges,'cr');
                   4026:                 }
1.116     raeburn  4027:             }
1.135     raeburn  4028: 	} elsif ($key=~/^form\.ren/) {
1.101     albertel 4029:             my $udom = $env{'form.ccdomain'};
                   4030:             my $uname = $env{'form.ccuname'};
1.116     raeburn  4031: # Re-enable standard role
1.135     raeburn  4032: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
1.89      raeburn  4033:                 my $url = $1;
                   4034:                 my $role = $2;
                   4035:                 my $logmsg;
                   4036:                 my $output;
                   4037:                 if ($role eq 'st') {
1.141     albertel 4038:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
1.374     raeburn  4039:                         my ($cdom,$cnum,$csec) = ($1,$2,$3);
1.375     raeburn  4040:                         my $credits;
                   4041:                         if ($showcredits) {
                   4042:                             my $defaultcredits = 
                   4043:                                 &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   4044:                             $credits = &get_user_credits($defaultcredits,$cdom,$cnum);
                   4045:                         }
                   4046:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$cdom,$cnum,$csec,$context,$credits);
1.220     raeburn  4047:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course') || ($result eq 'refused')) {
1.223     raeburn  4048:                             if ($result eq 'refused' && $logmsg) {
                   4049:                                 $output = $logmsg;
                   4050:                             } else { 
1.369     bisitz   4051:                                 $output = &mt('Error: [_1]',$result)."\n";
1.223     raeburn  4052:                             }
1.89      raeburn  4053:                         } else {
1.372     raeburn  4054:                             $output = &Apache::lonhtmlcommon::confirm_success(&mt('Assigning [_1] in [_2] starting [_3]',
                   4055:                                         &Apache::lonnet::plaintext($role),
                   4056:                                         &Apache::loncommon::show_role_extent($url,$context,'st'),
                   4057:                                         &Apache::lonlocal::locallocaltime($now))).'<br />'.$logmsg.'<br />';
1.89      raeburn  4058:                         }
                   4059:                     }
                   4060:                 } else {
1.101     albertel 4061: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
1.239     raeburn  4062:                                $env{'form.ccuname'},$url,$role,0,$now,'','',
                   4063:                                $context);
1.367     golterma 4064:                         $output = &Apache::lonhtmlcommon::confirm_success(&mt('Re-enabling [_1] in [_2]',
1.372     raeburn  4065:                                         &Apache::lonnet::plaintext($role),
                   4066:                                         &Apache::loncommon::show_role_extent($url,$context,$role)),$result ne "ok").'<br />';
1.369     bisitz   4067:                     if ($result ne "ok") {
                   4068:                         $output .= &mt('Error: [_1]',$result).'<br />';
                   4069:                     }
                   4070:                 }
1.89      raeburn  4071:                 $r->print($output);
1.225     raeburn  4072:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   4073:                     push(@rolechanges,$role);
                   4074:                 }
1.113     raeburn  4075: 	    }
1.116     raeburn  4076: # Re-enable custom role
1.139     albertel 4077: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  4078:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   4079:                 my $result = &Apache::lonnet::assigncustomrole(
                   4080:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
1.240     raeburn  4081:                                $url,$rdom,$rnam,$rolename,0,$now,undef,$context);
1.369     bisitz   4082:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   4083:                     &mt('Re-enabling custom role [_1] by [_2] in [_3]',
1.372     raeburn  4084:                         $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   4085:                     $result ne "ok").'<br />');
                   4086:                 if ($result ne "ok") {
                   4087:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   4088:                 }
1.225     raeburn  4089:                 if (!grep(/^cr$/,@rolechanges)) {
                   4090:                     push(@rolechanges,'cr');
                   4091:                 }
1.116     raeburn  4092:             }
1.135     raeburn  4093: 	} elsif ($key=~/^form\.act/) {
1.101     albertel 4094:             my $udom = $env{'form.ccdomain'};
                   4095:             my $uname = $env{'form.ccuname'};
1.141     albertel 4096: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
1.65      www      4097:                 # Activate a custom role
1.83      albertel 4098: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
                   4099: 		my $url='/'.$one.'/'.$two;
                   4100: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
1.65      www      4101: 
1.101     albertel 4102:                 my $start = ( $env{'form.start_'.$full} ?
                   4103:                               $env{'form.start_'.$full} :
1.88      raeburn  4104:                               $now );
1.101     albertel 4105:                 my $end   = ( $env{'form.end_'.$full} ?
                   4106:                               $env{'form.end_'.$full} :
1.88      raeburn  4107:                               0 );
                   4108:                                                                                      
                   4109:                 # split multiple sections
                   4110:                 my %sections = ();
1.101     albertel 4111:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
1.88      raeburn  4112:                 if ($num_sections == 0) {
1.240     raeburn  4113:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end,$context));
1.88      raeburn  4114:                 } else {
1.114     albertel 4115: 		    my %curr_groups =
1.117     raeburn  4116: 			&Apache::longroup::coursegroups($one,$two);
1.404     raeburn  4117:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.113     raeburn  4118:                         if (($sec eq 'none') || ($sec eq 'all') || 
                   4119:                             exists($curr_groups{$sec})) {
                   4120:                             $disallowed{$sec} = $url;
                   4121:                             next;
                   4122:                         }
                   4123:                         my $securl = $url.'/'.$sec;
1.240     raeburn  4124: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end,$context));
1.88      raeburn  4125:                     }
                   4126:                 }
1.225     raeburn  4127:                 if (!grep(/^cr$/,@rolechanges)) {
                   4128:                     push(@rolechanges,'cr');
                   4129:                 }
1.142     raeburn  4130: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
1.27      matthew  4131: 		# Activate roles for sections with 3 id numbers
                   4132: 		# set start, end times, and the url for the class
1.83      albertel 4133: 		my ($one,$two,$three)=($1,$2,$3);
1.101     albertel 4134: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
                   4135: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
1.27      matthew  4136: 			      $now );
1.101     albertel 4137: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
                   4138: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  4139: 			      0 );
1.83      albertel 4140: 		my $url='/'.$one.'/'.$two;
1.88      raeburn  4141:                 my $type = 'three';
                   4142:                 # split multiple sections
                   4143:                 my %sections = ();
1.101     albertel 4144:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
1.375     raeburn  4145:                 my $credits;
                   4146:                 if ($three eq 'st') {
                   4147:                     if ($showcredits) { 
                   4148:                         my $defaultcredits = 
                   4149:                             &Apache::lonuserutils::get_defaultcredits($one,$two);
                   4150:                         $credits = $env{'form.credits_'.$one.'_'.$two.'_'.$three};
                   4151:                         $credits =~ s/[^\d\.]//g;
                   4152:                         if ($credits eq $defaultcredits) {
                   4153:                             undef($credits);
                   4154:                         }
                   4155:                     }
                   4156:                 }
1.88      raeburn  4157:                 if ($num_sections == 0) {
1.375     raeburn  4158:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  4159:                 } else {
1.114     albertel 4160:                     my %curr_groups = 
1.117     raeburn  4161: 			&Apache::longroup::coursegroups($one,$two);
1.88      raeburn  4162:                     my $emptysec = 0;
1.404     raeburn  4163:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.88      raeburn  4164:                         $sec =~ s/\W//g;
1.113     raeburn  4165:                         if ($sec ne '') {
                   4166:                             if (($sec eq 'none') || ($sec eq 'all') || 
                   4167:                                 exists($curr_groups{$sec})) {
                   4168:                                 $disallowed{$sec} = $url;
                   4169:                                 next;
                   4170:                             }
1.88      raeburn  4171:                             my $securl = $url.'/'.$sec;
1.375     raeburn  4172:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec,$context,$credits));
1.88      raeburn  4173:                         } else {
                   4174:                             $emptysec = 1;
                   4175:                         }
                   4176:                     }
                   4177:                     if ($emptysec) {
1.375     raeburn  4178:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  4179:                     }
1.225     raeburn  4180:                 }
                   4181:                 if (!grep(/^\Q$three\E$/,@rolechanges)) {
                   4182:                     push(@rolechanges,$three);
                   4183:                 }
1.135     raeburn  4184: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
1.27      matthew  4185: 		# Activate roles for sections with two id numbers
                   4186: 		# set start, end times, and the url for the class
1.101     albertel 4187: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
                   4188: 			      $env{'form.start_'.$1.'_'.$2} : 
1.27      matthew  4189: 			      $now );
1.101     albertel 4190: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
                   4191: 			      $env{'form.end_'.$1.'_'.$2} :
1.27      matthew  4192: 			      0 );
1.225     raeburn  4193:                 my $one = $1;
                   4194:                 my $two = $2;
                   4195: 		my $url='/'.$one.'/';
1.88      raeburn  4196:                 # split multiple sections
                   4197:                 my %sections = ();
1.225     raeburn  4198:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two},\%sections,$two);
1.88      raeburn  4199:                 if ($num_sections == 0) {
1.240     raeburn  4200:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  4201:                 } else {
                   4202:                     my $emptysec = 0;
1.404     raeburn  4203:                     foreach my $sec (sort {$a cmp $b} keys(%sections)) {
1.88      raeburn  4204:                         if ($sec ne '') {
                   4205:                             my $securl = $url.'/'.$sec;
1.240     raeburn  4206:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$two,$start,$end,$one,undef,$sec,$context));
1.88      raeburn  4207:                         } else {
                   4208:                             $emptysec = 1;
                   4209:                         }
                   4210:                     }
                   4211:                     if ($emptysec) {
1.240     raeburn  4212:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  4213:                     }
                   4214:                 }
1.225     raeburn  4215:                 if (!grep(/^\Q$two\E$/,@rolechanges)) {
                   4216:                     push(@rolechanges,$two);
                   4217:                 }
1.64      www      4218: 	    } else {
1.190     raeburn  4219: 		$r->print('<p><span class="LC_error">'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></span></p><br />');
1.64      www      4220:             }
1.113     raeburn  4221:             foreach my $key (sort(keys(%disallowed))) {
1.274     bisitz   4222:                 $r->print('<p class="LC_warning">');
1.113     raeburn  4223:                 if (($key eq 'none') || ($key eq 'all')) {  
1.274     bisitz   4224:                     $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  4225:                 } else {
1.274     bisitz   4226:                     $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  4227:                 }
1.274     bisitz   4228:                 $r->print('</p><p>'
                   4229:                          .&mt('Please [_1]go back[_2] and choose a different section name.'
                   4230:                              ,'<a href="javascript:history.go(-1)'
                   4231:                              ,'</a>')
                   4232:                          .'</p><br />'
                   4233:                 );
1.113     raeburn  4234:             }
                   4235: 	}
1.101     albertel 4236:     } # End of foreach (keys(%env))
1.75      www      4237: # Flush the course logs so reverse user roles immediately updated
1.349     raeburn  4238:     $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.225     raeburn  4239:     if (@rolechanges == 0) {
1.372     raeburn  4240:         $r->print('<p>'.&mt('No roles to modify').'</p>');
1.193     raeburn  4241:     }
1.225     raeburn  4242:     return @rolechanges;
1.220     raeburn  4243: }
                   4244: 
1.375     raeburn  4245: sub get_user_credits {
                   4246:     my ($uname,$udom,$defaultcredits,$cdom,$cnum) = @_;
                   4247:     if ($cdom eq '' || $cnum eq '') {
                   4248:         return unless ($env{'request.course.id'});
                   4249:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4250:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4251:     }
                   4252:     my $credits;
                   4253:     my %currhash =
                   4254:         &Apache::lonnet::get('classlist',[$uname.':'.$udom],$cdom,$cnum);
                   4255:     if (keys(%currhash) > 0) {
                   4256:         my @items = split(/:/,$currhash{$uname.':'.$udom});
                   4257:         my $crdidx = &Apache::loncoursedata::CL_CREDITS() - 3;
                   4258:         $credits = $items[$crdidx];
                   4259:         $credits =~ s/[^\d\.]//g;
                   4260:     }
                   4261:     if ($credits eq $defaultcredits) {
                   4262:         undef($credits);
                   4263:     }
                   4264:     return $credits;
                   4265: }
                   4266: 
1.220     raeburn  4267: sub enroll_single_student {
1.375     raeburn  4268:     my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype,
                   4269:         $showcredits,$defaultcredits) = @_;
1.318     raeburn  4270:     $r->print('<h3>');
                   4271:     if ($crstype eq 'Community') {
                   4272:         $r->print(&mt('Enrolling Member'));
                   4273:     } else {
                   4274:         $r->print(&mt('Enrolling Student'));
                   4275:     }
                   4276:     $r->print('</h3>');
1.220     raeburn  4277: 
                   4278:     # Remove non alphanumeric values from section
                   4279:     $env{'form.sections'}=~s/\W//g;
                   4280: 
1.375     raeburn  4281:     my $credits;
                   4282:     if (($showcredits) && ($env{'form.credits'} ne '')) {
                   4283:         $credits = $env{'form.credits'};
                   4284:         $credits =~ s/[^\d\.]//g;
                   4285:         if ($credits ne '') {
                   4286:             if ($credits eq $defaultcredits) {
                   4287:                 undef($credits);
                   4288:             }
                   4289:         }
                   4290:     }
                   4291: 
1.220     raeburn  4292:     # Clean out any old student roles the user has in this class.
                   4293:     &Apache::lonuserutils::modifystudent($env{'form.ccdomain'},
                   4294:          $env{'form.ccuname'},$env{'request.course.id'},undef,$uhome);
                   4295:     my ($startdate,$enddate) = &Apache::lonuserutils::get_dates_from_form();
                   4296:     my $enroll_result =
                   4297:         &Apache::lonnet::modify_student_enrollment($env{'form.ccdomain'},
                   4298:             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
                   4299:             $env{'form.cmiddlename'},$env{'form.clastname'},
                   4300:             $env{'form.generation'},$env{'form.sections'},$enddate,
1.375     raeburn  4301:             $startdate,'manual',undef,$env{'request.course.id'},'',$context,
                   4302:             $credits);
1.220     raeburn  4303:     if ($enroll_result =~ /^ok/) {
1.381     bisitz   4304:         $r->print(&mt('[_1] enrolled','<b>'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.'</b>'));
1.220     raeburn  4305:         if ($env{'form.sections'} ne '') {
                   4306:             $r->print(' '.&mt('in section [_1]',$env{'form.sections'}));
                   4307:         }
                   4308:         my ($showstart,$showend);
                   4309:         if ($startdate <= $now) {
                   4310:             $showstart = &mt('Access starts immediately');
                   4311:         } else {
                   4312:             $showstart = &mt('Access starts: ').&Apache::lonlocal::locallocaltime($startdate);
                   4313:         }
                   4314:         if ($enddate == 0) {
                   4315:             $showend = &mt('ends: no ending date');
                   4316:         } else {
                   4317:             $showend = &mt('ends: ').&Apache::lonlocal::locallocaltime($enddate);
                   4318:         }
                   4319:         $r->print('.<br />'.$showstart.'; '.$showend);
                   4320:         if ($startdate <= $now && !$newuser) {
1.386     bisitz   4321:             $r->print('<p class="LC_info">');
1.318     raeburn  4322:             if ($crstype eq 'Community') {
1.392     raeburn  4323:                 $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  4324:             } else {
1.392     raeburn  4325:                 $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  4326:            }
                   4327:            $r->print('</p>');
1.220     raeburn  4328:         }
                   4329:     } else {
                   4330:         $r->print(&mt('unable to enroll').": ".$enroll_result);
                   4331:     }
                   4332:     return;
1.188     raeburn  4333: }
                   4334: 
1.204     raeburn  4335: sub get_defaultquota_text {
                   4336:     my ($settingstatus) = @_;
                   4337:     my $defquotatext; 
                   4338:     if ($settingstatus eq '') {
1.383     raeburn  4339:         $defquotatext = &mt('default');
1.204     raeburn  4340:     } else {
                   4341:         my ($usertypes,$order) =
                   4342:             &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
                   4343:         if ($usertypes->{$settingstatus} eq '') {
1.383     raeburn  4344:             $defquotatext = &mt('default');
1.204     raeburn  4345:         } else {
1.383     raeburn  4346:             $defquotatext = &mt('default for [_1]',$usertypes->{$settingstatus});
1.204     raeburn  4347:         }
                   4348:     }
                   4349:     return $defquotatext;
                   4350: }
                   4351: 
1.188     raeburn  4352: sub update_result_form {
                   4353:     my ($uhome) = @_;
                   4354:     my $outcome = 
1.367     golterma 4355:     '<form name="userupdate" method="post" action="">'."\n";
1.160     raeburn  4356:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
1.188     raeburn  4357:         $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  4358:     }
1.207     raeburn  4359:     if ($env{'form.origname'} ne '') {
                   4360:         $outcome .= '<input type="hidden" name="origname" value="'.$env{'form.origname'}.'" />'."\n";
                   4361:     }
1.160     raeburn  4362:     foreach my $item ('sortby','seluname','seludom') {
                   4363:         if (exists($env{'form.'.$item})) {
1.188     raeburn  4364:             $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  4365:         }
                   4366:     }
1.188     raeburn  4367:     if ($uhome eq 'no_host') {
                   4368:         $outcome .= '<input type="hidden" name="forcenewuser" value="1" />'."\n";
                   4369:     }
                   4370:     $outcome .= '<input type="hidden" name="phase" value="" />'."\n".
1.383     raeburn  4371:                 '<input type="hidden" name="currstate" value="" />'."\n".
                   4372:                 '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1.188     raeburn  4373:                 '</form>';
                   4374:     return $outcome;
1.4       www      4375: }
                   4376: 
1.149     raeburn  4377: sub quota_admin {
1.378     raeburn  4378:     my ($setquota,$changeHash,$name) = @_;
1.149     raeburn  4379:     my $quotachanged;
                   4380:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   4381:         # Current user has quota modification privileges
1.267     raeburn  4382:         if (ref($changeHash) eq 'HASH') {
                   4383:             $quotachanged = 1;
1.378     raeburn  4384:             $changeHash->{$name.'quota'} = $setquota;
1.267     raeburn  4385:         }
1.149     raeburn  4386:     }
                   4387:     return $quotachanged;
                   4388: }
                   4389: 
1.267     raeburn  4390: sub tool_admin {
1.275     raeburn  4391:     my ($tool,$settool,$changeHash,$context) = @_;
                   4392:     my $canchange = 0; 
1.279     raeburn  4393:     if ($context eq 'requestcourses') {
1.275     raeburn  4394:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
                   4395:             $canchange = 1;
                   4396:         }
1.300     raeburn  4397:     } elsif ($context eq 'reqcrsotherdom') {
                   4398:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   4399:             $canchange = 1;
                   4400:         }
1.362     raeburn  4401:     } elsif ($context eq 'requestauthor') {
                   4402:         if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
                   4403:             $canchange = 1;
                   4404:         }
1.275     raeburn  4405:     } elsif (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   4406:         # Current user has quota modification privileges
                   4407:         $canchange = 1;
                   4408:     }
1.267     raeburn  4409:     my $toolchanged;
1.275     raeburn  4410:     if ($canchange) {
1.267     raeburn  4411:         if (ref($changeHash) eq 'HASH') {
                   4412:             $toolchanged = 1;
1.362     raeburn  4413:             if ($tool eq 'requestauthor') {
                   4414:                 $changeHash->{$context} = $settool;
                   4415:             } else {
                   4416:                 $changeHash->{$context.'.'.$tool} = $settool;
                   4417:             }
1.267     raeburn  4418:         }
                   4419:     }
                   4420:     return $toolchanged;
                   4421: }
                   4422: 
1.88      raeburn  4423: sub build_roles {
1.89      raeburn  4424:     my ($sectionstr,$sections,$role) = @_;
1.88      raeburn  4425:     my $num_sections = 0;
                   4426:     if ($sectionstr=~ /,/) {
                   4427:         my @secnums = split/,/,$sectionstr;
1.89      raeburn  4428:         if ($role eq 'st') {
                   4429:             $secnums[0] =~ s/\W//g;
                   4430:             $$sections{$secnums[0]} = 1;
                   4431:             $num_sections = 1;
                   4432:         } else {
                   4433:             foreach my $sec (@secnums) {
                   4434:                 $sec =~ ~s/\W//g;
1.150     banghart 4435:                 if (!($sec eq "")) {
1.89      raeburn  4436:                     if (exists($$sections{$sec})) {
                   4437:                         $$sections{$sec} ++;
                   4438:                     } else {
                   4439:                         $$sections{$sec} = 1;
                   4440:                         $num_sections ++;
                   4441:                     }
1.88      raeburn  4442:                 }
                   4443:             }
                   4444:         }
                   4445:     } else {
                   4446:         $sectionstr=~s/\W//g;
                   4447:         unless ($sectionstr eq '') {
                   4448:             $$sections{$sectionstr} = 1;
                   4449:             $num_sections ++;
                   4450:         }
                   4451:     }
1.129     albertel 4452: 
1.88      raeburn  4453:     return $num_sections;
                   4454: }
                   4455: 
1.58      www      4456: # ========================================================== Custom Role Editor
                   4457: 
                   4458: sub custom_role_editor {
1.414     raeburn  4459:     my ($r,$brcrum,$prefix) = @_;
1.324     raeburn  4460:     my $action = $env{'form.customroleaction'};
                   4461:     my $rolename; 
                   4462:     if ($action eq 'new') {
                   4463:         $rolename=$env{'form.newrolename'};
                   4464:     } else {
                   4465:         $rolename=$env{'form.rolename'};
1.59      www      4466:     }
                   4467: 
1.324     raeburn  4468:     my ($crstype,$context);
                   4469:     if ($env{'request.course.id'}) {
                   4470:         $crstype = &Apache::loncommon::course_type();
                   4471:         $context = 'course';
                   4472:     } else {
                   4473:         $context = 'domain';
1.414     raeburn  4474:         $crstype = 'course';
1.324     raeburn  4475:     }
1.351     raeburn  4476: 
                   4477:     $rolename=~s/[^A-Za-z0-9]//gs;
                   4478:     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
                   4479: 	&print_username_entry_form($r,undef,undef,undef,undef,$crstype,$brcrum);
                   4480:         return;
                   4481:     }
                   4482: 
1.414     raeburn  4483:     my $formname = 'form1';
                   4484:     my %privs=();
                   4485:     my $body_top = '<h2>';
                   4486: # ------------------------------------------------------- Does this role exist?
1.59      www      4487:     my ($rdummy,$roledef)=
                   4488: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   4489:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.414     raeburn  4490:         $body_top .= &mt('Existing Role').' "';
1.61      www      4491: # ------------------------------------------------- Get current role privileges
1.414     raeburn  4492:         ($privs{'system'},$privs{'domain'},$privs{'course'})=split(/\_/,$roledef);
                   4493:         if ($privs{'system'} =~ /bre\&S/) {
                   4494:             if ($context eq 'domain') {
1.417     raeburn  4495:                 $crstype = 'Course';
1.414     raeburn  4496:             } elsif ($crstype eq 'Community') {
                   4497:                 $privs{'system'} =~ s/bre\&S//;
                   4498:             }
                   4499:         } elsif ($context eq 'domain') {
                   4500:             $crstype = 'Course';
1.324     raeburn  4501:         }
1.59      www      4502:     } else {
1.414     raeburn  4503:         $body_top .= &mt('New Role').' "';
                   4504:         $roledef='';
1.59      www      4505:     }
1.153     banghart 4506:     $body_top .= $rolename.'"</h2>';
1.414     raeburn  4507: 
                   4508: # ------------------------------------------------------- What can be assigned?
                   4509:     my %full=();
1.417     raeburn  4510:     my %levels=(
1.414     raeburn  4511:                  course => {},
                   4512:                  domain => {},
                   4513:                  system => {},
                   4514:                );
                   4515:     my %levelscurrent=(
                   4516:                         course => {},
                   4517:                         domain => {},
                   4518:                         system => {},
                   4519:                       );
                   4520:     &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
1.160     raeburn  4521:     my ($jsback,$elements) = &crumb_utilities();
1.414     raeburn  4522:     my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
1.417     raeburn  4523:     my $head_script =
1.414     raeburn  4524:         &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,
                   4525:                                                   \%full,\@templateroles,$jsback);
1.351     raeburn  4526:     push (@{$brcrum},
1.414     raeburn  4527:               {href => "javascript:backPage(document.$formname,'pickrole','')",
1.351     raeburn  4528:                text => "Pick custom role",
                   4529:                faq  => 282,bug=>'Instructor Interface',},
1.414     raeburn  4530:               {href => "javascript:backPage(document.$formname,'','')",
1.351     raeburn  4531:                text => "Edit custom role",
                   4532:                faq  => 282,
                   4533:                bug  => 'Instructor Interface',
                   4534:                help => 'Course_Editing_Custom_Roles'}
                   4535:               );
                   4536:     my $args = { bread_crumbs          => $brcrum,
                   4537:                  bread_crumbs_component => 'User Management'};
                   4538:  
                   4539:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',
                   4540:                                              $head_script,$args).
                   4541:               $body_top);
1.414     raeburn  4542:     $r->print('<form name="'.$formname.'" method="post" action="">'."\n".
                   4543:               &Apache::lonuserutils::custom_role_header($context,$crstype,
                   4544:                                                         \@templateroles,$prefix));
1.264     bisitz   4545: 
1.61      www      4546:     $r->print(<<ENDCCF);
                   4547: <input type="hidden" name="phase" value="set_custom_roles" />
                   4548: <input type="hidden" name="rolename" value="$rolename" />
                   4549: ENDCCF
1.414     raeburn  4550:     $r->print(&Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
                   4551:                                                        \%levelscurrent,$prefix));
1.135     raeburn  4552:     $r->print(&Apache::loncommon::end_data_table().
1.190     raeburn  4553:    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
1.160     raeburn  4554:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
1.417     raeburn  4555:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".
1.160     raeburn  4556:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
1.351     raeburn  4557:    '<input type="submit" value="'.&mt('Save').'" /></form>');
1.61      www      4558: }
1.414     raeburn  4559: 
1.61      www      4560: # ---------------------------------------------------------- Call to definerole
                   4561: sub set_custom_role {
1.414     raeburn  4562:     my ($r,$context,$brcrum,$prefix) = @_;
1.101     albertel 4563:     my $rolename=$env{'form.rolename'};
1.63      www      4564:     $rolename=~s/[^A-Za-z0-9]//gs;
1.150     banghart 4565:     if (!$rolename) {
1.414     raeburn  4566: 	&custom_role_editor($r,$brcrum,$prefix);
1.61      www      4567:         return;
                   4568:     }
1.160     raeburn  4569:     my ($jsback,$elements) = &crumb_utilities();
1.301     bisitz   4570:     my $jscript = '<script type="text/javascript">'
                   4571:                  .'// <![CDATA['."\n"
                   4572:                  .$jsback."\n"
                   4573:                  .'// ]]>'."\n"
                   4574:                  .'</script>'."\n";
1.352     raeburn  4575:     push(@{$brcrum},
                   4576:         {href => "javascript:backPage(document.customresult,'pickrole','')",
                   4577:          text => "Pick custom role",
                   4578:          faq  => 282,
                   4579:          bug  => 'Instructor Interface',},
                   4580:         {href => "javascript:backPage(document.customresult,'selected_custom_edit','')",
                   4581:          text => "Edit custom role",
                   4582:          faq  => 282,
                   4583:          bug  => 'Instructor Interface',},
                   4584:         {href => "javascript:backPage(document.customresult,'set_custom_roles','')",
                   4585:          text => "Result",
                   4586:          faq  => 282,
                   4587:          bug  => 'Instructor Interface',
                   4588:          help => 'Course_Editing_Custom_Roles'},
                   4589:         );
                   4590:     my $args = { bread_crumbs           => $brcrum,
1.414     raeburn  4591:                  bread_crumbs_component => 'User Management'};
1.351     raeburn  4592:     $r->print(&Apache::loncommon::start_page('Save Custom Role',$jscript,$args));
1.160     raeburn  4593: 
1.393     raeburn  4594:     my $newrole;
1.61      www      4595:     my ($rdummy,$roledef)=
1.110     albertel 4596: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   4597: 
1.61      www      4598: # ------------------------------------------------------- Does this role exist?
1.188     raeburn  4599:     $r->print('<h3>');
1.61      www      4600:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 4601: 	$r->print(&mt('Existing Role').' "');
1.61      www      4602:     } else {
1.73      sakharuk 4603: 	$r->print(&mt('New Role').' "');
1.61      www      4604: 	$roledef='';
1.393     raeburn  4605:         $newrole = 1;
1.61      www      4606:     }
1.188     raeburn  4607:     $r->print($rolename.'"</h3>');
1.414     raeburn  4608: # ------------------------------------------------- Assign role and show result
1.61      www      4609: 
1.387     bisitz   4610:     my $errmsg;
1.414     raeburn  4611:     my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$prefix);
                   4612:     # Assign role and return result
                   4613:     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
                   4614:                                              $newprivs{'c'});
1.387     bisitz   4615:     if ($result ne 'ok') {
                   4616:         $errmsg = ': '.$result;
                   4617:     }
                   4618:     my $message =
                   4619:         &Apache::lonhtmlcommon::confirm_success(
                   4620:             &mt('Defining Role').$errmsg, ($result eq 'ok' ? 0 : 1));
1.101     albertel 4621:     if ($env{'request.course.id'}) {
                   4622:         my $url='/'.$env{'request.course.id'};
1.63      www      4623:         $url=~s/\_/\//g;
1.387     bisitz   4624:         $result =
                   4625:             &Apache::lonnet::assigncustomrole(
                   4626:                 $env{'user.domain'},$env{'user.name'},
                   4627:                 $url,
                   4628:                 $env{'user.domain'},$env{'user.name'},
                   4629:                 $rolename,undef,undef,undef,$context);
                   4630:         if ($result ne 'ok') {
                   4631:             $errmsg = ': '.$result;
                   4632:         }
                   4633:         $message .=
                   4634:             '<br />'
                   4635:            .&Apache::lonhtmlcommon::confirm_success(
                   4636:                 &mt('Assigning Role to Self').$errmsg, ($result eq 'ok' ? 0 : 1));
1.63      www      4637:     }
1.380     bisitz   4638:     $r->print(
1.387     bisitz   4639:         &Apache::loncommon::confirmwrapper($message)
                   4640:        .'<br />'
                   4641:        .&Apache::lonhtmlcommon::actionbox([
                   4642:             '<a href="javascript:backPage(document.customresult,'."'pickrole'".')">'
                   4643:            .&mt('Create or edit another custom role')
                   4644:            .'</a>'])
1.380     bisitz   4645:        .'<form name="customresult" method="post" action="">'
1.387     bisitz   4646:        .&Apache::lonhtmlcommon::echo_form_input([])
                   4647:        .'</form>'
1.380     bisitz   4648:     );
1.58      www      4649: }
                   4650: 
1.2       www      4651: # ================================================================ Main Handler
                   4652: sub handler {
                   4653:     my $r = shift;
                   4654:     if ($r->header_only) {
1.68      www      4655:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      4656:        $r->send_http_header;
                   4657:        return OK;
                   4658:     }
1.318     raeburn  4659:     my ($context,$crstype);
1.190     raeburn  4660:     if ($env{'request.course.id'}) {
                   4661:         $context = 'course';
1.318     raeburn  4662:         $crstype = &Apache::loncommon::course_type();
1.190     raeburn  4663:     } elsif ($env{'request.role'} =~ /^au\./) {
1.206     raeburn  4664:         $context = 'author';
1.190     raeburn  4665:     } else {
                   4666:         $context = 'domain';
                   4667:     }
1.375     raeburn  4668: 
1.190     raeburn  4669:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.233     raeburn  4670:         ['action','state','callingform','roletype','showrole','bulkaction','popup','phase',
1.391     raeburn  4671:          'username','domain','srchterm','srchdomain','srchin','srchby','srchtype','queue']);
1.190     raeburn  4672:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.351     raeburn  4673:     my $args;
                   4674:     my $brcrum = [];
                   4675:     my $bread_crumbs_component = 'User Management';
1.391     raeburn  4676:     if (($env{'form.action'} ne 'dateselect') && ($env{'form.action'} ne 'displayuserreq')) {
1.351     raeburn  4677:         $brcrum = [{href=>"/adm/createuser",
                   4678:                     text=>"User Management",
                   4679:                     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'}
                   4680:                   ];
1.202     raeburn  4681:     }
1.289     droeschl 4682:     #SD Following files not added to help, because the corresponding .tex-files seem to
                   4683:     #be missing: Course_Approve_Selfenroll,Course_User_Logs,
1.209     raeburn  4684:     my ($permission,$allowed) = 
1.318     raeburn  4685:         &Apache::lonuserutils::get_permission($context,$crstype);
1.190     raeburn  4686:     if (!$allowed) {
1.358     raeburn  4687:         if ($context eq 'course') {
                   4688:             $r->internal_redirect('/adm/viewclasslist');
                   4689:             return OK;
                   4690:         }
1.190     raeburn  4691:         $env{'user.error.msg'}=
                   4692:             "/adm/createuser:cst:0:0:Cannot create/modify user data ".
                   4693:                                  "or view user status.";
                   4694:         return HTTP_NOT_ACCEPTABLE;
                   4695:     }
                   4696: 
                   4697:     &Apache::loncommon::content_type($r,'text/html');
                   4698:     $r->send_http_header;
                   4699: 
1.375     raeburn  4700:     my $showcredits;
                   4701:     if ((($context eq 'course') && ($crstype eq 'Course')) || 
                   4702:          ($context eq 'domain')) {
                   4703:         my %domdefaults = 
                   4704:             &Apache::lonnet::get_domain_defaults($env{'request.role.domain'});
                   4705:         if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'}) {
                   4706:             $showcredits = 1;
                   4707:         }
                   4708:     }
                   4709: 
1.190     raeburn  4710:     # Main switch on form.action and form.state, as appropriate
                   4711:     if (! exists($env{'form.action'})) {
1.351     raeburn  4712:         $args = {bread_crumbs => $brcrum,
                   4713:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4714:         $r->print(&header(undef,$args));
1.318     raeburn  4715:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4716:     } elsif ($env{'form.action'} eq 'upload' && $permission->{'cusr'}) {
1.351     raeburn  4717:         push(@{$brcrum},
                   4718:               { href => '/adm/createuser?action=upload&state=',
                   4719:                 text => 'Upload Users List',
                   4720:                 help => 'Course_Create_Class_List',
                   4721:               });
                   4722:         $bread_crumbs_component = 'Upload Users List';
                   4723:         $args = {bread_crumbs           => $brcrum,
                   4724:                  bread_crumbs_component => $bread_crumbs_component};
                   4725:         $r->print(&header(undef,$args));
1.190     raeburn  4726:         $r->print('<form name="studentform" method="post" '.
                   4727:                   'enctype="multipart/form-data" '.
                   4728:                   ' action="/adm/createuser">'."\n");
                   4729:         if (! exists($env{'form.state'})) {
                   4730:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4731:         } elsif ($env{'form.state'} eq 'got_file') {
1.375     raeburn  4732:             &Apache::lonuserutils::print_upload_manager_form($r,$context,$permission,
                   4733:                                                              $crstype,$showcredits);
1.190     raeburn  4734:         } elsif ($env{'form.state'} eq 'enrolling') {
                   4735:             if ($env{'form.datatoken'}) {
1.375     raeburn  4736:                 &Apache::lonuserutils::upfile_drop_add($r,$context,$permission,
                   4737:                                                        $showcredits);
1.190     raeburn  4738:             }
                   4739:         } else {
                   4740:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4741:         }
1.416     raeburn  4742:     } elsif (((($env{'form.action'} eq 'singleuser') || ($env{'form.action'}
                   4743:               eq 'singlestudent')) && ($permission->{'cusr'})) ||
1.418   ! raeburn  4744:              (($env{'form.action'} eq 'singleuser') && ($permission->{'view'})) ||
1.416     raeburn  4745:              (($env{'form.action'} eq 'accesslogs') && ($permission->{'activity'}))) {
1.190     raeburn  4746:         my $phase = $env{'form.phase'};
                   4747:         my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
1.192     albertel 4748: 	&Apache::loncreateuser::restore_prev_selections();
                   4749: 	my $srch;
                   4750: 	foreach my $item (@search) {
                   4751: 	    $srch->{$item} = $env{'form.'.$item};
                   4752: 	}
1.207     raeburn  4753:         if (($phase eq 'get_user_info') || ($phase eq 'userpicked') ||
1.416     raeburn  4754:             ($phase eq 'createnewuser') || ($phase eq 'activity')) {
1.207     raeburn  4755:             if ($env{'form.phase'} eq 'createnewuser') {
                   4756:                 my $response;
                   4757:                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
1.366     bisitz   4758:                     my $response =
                   4759:                         '<span class="LC_warning">'
                   4760:                        .&mt('You must specify a valid username. Only the following are allowed:'
                   4761:                            .' letters numbers - . @')
                   4762:                        .'</span>';
1.221     raeburn  4763:                     $env{'form.phase'} = '';
1.375     raeburn  4764:                     &print_username_entry_form($r,$context,$response,$srch,undef,
                   4765:                                                $crstype,$brcrum,$showcredits);
1.207     raeburn  4766:                 } else {
                   4767:                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
                   4768:                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
                   4769:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4770:                                                   $srch,$response,$context,
1.375     raeburn  4771:                                                   $permission,$crstype,$brcrum,
                   4772:                                                   $showcredits);
1.207     raeburn  4773:                 }
                   4774:             } elsif ($env{'form.phase'} eq 'get_user_info') {
1.190     raeburn  4775:                 my ($currstate,$response,$forcenewuser,$results) = 
1.221     raeburn  4776:                     &user_search_result($context,$srch);
1.190     raeburn  4777:                 if ($env{'form.currstate'} eq 'modify') {
                   4778:                     $currstate = $env{'form.currstate'};
                   4779:                 }
                   4780:                 if ($currstate eq 'select') {
                   4781:                     &print_user_selection_page($r,$response,$srch,$results,
1.351     raeburn  4782:                                                \@search,$context,undef,$crstype,
                   4783:                                                $brcrum);
1.416     raeburn  4784:                 } elsif (($currstate eq 'modify') || ($env{'form.action'} eq 'accesslogs')) {
                   4785:                     my ($ccuname,$ccdomain,$uhome);
1.190     raeburn  4786:                     if (($srch->{'srchby'} eq 'uname') && 
                   4787:                         ($srch->{'srchtype'} eq 'exact')) {
                   4788:                         $ccuname = $srch->{'srchterm'};
                   4789:                         $ccdomain= $srch->{'srchdomain'};
                   4790:                     } else {
                   4791:                         my @matchedunames = keys(%{$results});
                   4792:                         ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
                   4793:                     }
                   4794:                     $ccuname =&LONCAPA::clean_username($ccuname);
                   4795:                     $ccdomain=&LONCAPA::clean_domain($ccdomain);
1.416     raeburn  4796:                     if ($env{'form.action'} eq 'accesslogs') {
                   4797:                         my $uhome;
                   4798:                         if (($ccuname ne '') && ($ccdomain ne '')) {
                   4799:                            $uhome = &Apache::lonnet::homeserver($ccuname,$ccdomain);
                   4800:                         }
                   4801:                         if (($uhome eq '') || ($uhome eq 'no_host')) {
                   4802:                             $env{'form.phase'} = '';
                   4803:                             undef($forcenewuser);
                   4804:                             #if ($response) {
                   4805:                             #    unless ($response =~ m{\Q<br /><br />\E$}) {
                   4806:                             #        $response .= '<br /><br />';
                   4807:                             #    }
                   4808:                             #}
                   4809:                             &print_username_entry_form($r,$context,$response,$srch,
                   4810:                                                        $forcenewuser,$crstype,$brcrum);
                   4811:                         } else {
                   4812:                             &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
                   4813:                         }
                   4814:                     } else {
                   4815:                         if ($env{'form.forcenewuser'}) {
                   4816:                             $response = '';
                   4817:                         }
                   4818:                         &print_user_modification_page($r,$ccuname,$ccdomain,
                   4819:                                                       $srch,$response,$context,
                   4820:                                                       $permission,$crstype,$brcrum);
1.190     raeburn  4821:                     }
                   4822:                 } elsif ($currstate eq 'query') {
1.351     raeburn  4823:                     &print_user_query_page($r,'createuser',$brcrum);
1.190     raeburn  4824:                 } else {
1.229     raeburn  4825:                     $env{'form.phase'} = '';
1.207     raeburn  4826:                     &print_username_entry_form($r,$context,$response,$srch,
1.351     raeburn  4827:                                                $forcenewuser,$crstype,$brcrum);
1.190     raeburn  4828:                 }
                   4829:             } elsif ($env{'form.phase'} eq 'userpicked') {
                   4830:                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
                   4831:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
1.416     raeburn  4832:                 if ($env{'form.action'} eq 'accesslogs') {
                   4833:                     &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
                   4834:                 } else {
                   4835:                     &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
                   4836:                                                   $context,$permission,$crstype,
                   4837:                                                   $brcrum);
                   4838:                 }
                   4839:             } elsif ($env{'form.action'} eq 'accesslogs') {
                   4840:                 my $ccuname = &LONCAPA::clean_username($env{'form.accessuname'});
                   4841:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.accessudom'});
                   4842:                 &print_useraccesslogs_display($r,$ccuname,$ccdomain,$permission,$brcrum);
1.190     raeburn  4843:             }
                   4844:         } elsif ($env{'form.phase'} eq 'update_user_data') {
1.375     raeburn  4845:             &update_user_data($r,$context,$crstype,$brcrum,$showcredits);
1.190     raeburn  4846:         } else {
1.351     raeburn  4847:             &print_username_entry_form($r,$context,undef,$srch,undef,$crstype,
                   4848:                                        $brcrum);
1.190     raeburn  4849:         }
                   4850:     } elsif ($env{'form.action'} eq 'custom' && $permission->{'custom'}) {
1.414     raeburn  4851:         my $prefix;
1.190     raeburn  4852:         if ($env{'form.phase'} eq 'set_custom_roles') {
1.414     raeburn  4853:             &set_custom_role($r,$context,$brcrum,$prefix);
1.190     raeburn  4854:         } else {
1.414     raeburn  4855:             &custom_role_editor($r,$brcrum,$prefix);
1.190     raeburn  4856:         }
1.362     raeburn  4857:     } elsif (($env{'form.action'} eq 'processauthorreq') &&
                   4858:              ($permission->{'cusr'}) && 
                   4859:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   4860:         push(@{$brcrum},
                   4861:                  {href => '/adm/createuser?action=processauthorreq',
1.385     bisitz   4862:                   text => 'Authoring Space requests',
1.362     raeburn  4863:                   help => 'Domain_Role_Approvals'});
                   4864:         $bread_crumbs_component = 'Authoring requests';
                   4865:         if ($env{'form.state'} eq 'done') {
                   4866:             push(@{$brcrum},
                   4867:                      {href => '/adm/createuser?action=authorreqqueue',
                   4868:                       text => 'Result',
                   4869:                       help => 'Domain_Role_Approvals'});
                   4870:             $bread_crumbs_component = 'Authoring request result';
                   4871:         }
                   4872:         $args = { bread_crumbs           => $brcrum,
                   4873:                   bread_crumbs_component => $bread_crumbs_component};
1.391     raeburn  4874:         my $js = &usernamerequest_javascript();
                   4875:         $r->print(&header(&add_script($js),$args));
1.362     raeburn  4876:         if (!exists($env{'form.state'})) {
                   4877:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestauthor',
                   4878:                                                                             $env{'request.role.domain'}));
                   4879:         } elsif ($env{'form.state'} eq 'done') {
                   4880:             $r->print('<h3>'.&mt('Authoring request processing').'</h3>'."\n");
                   4881:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestauthor',
                   4882:                                                                          $env{'request.role.domain'}));
                   4883:         }
1.391     raeburn  4884:     } elsif (($env{'form.action'} eq 'processusernamereq') &&
                   4885:              ($permission->{'cusr'}) &&
                   4886:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   4887:         push(@{$brcrum},
                   4888:                  {href => '/adm/createuser?action=processusernamereq',
                   4889:                   text => 'LON-CAPA account requests',
                   4890:                   help => 'Domain_Username_Approvals'});
                   4891:         $bread_crumbs_component = 'Account requests';
                   4892:         if ($env{'form.state'} eq 'done') {
                   4893:             push(@{$brcrum},
                   4894:                      {href => '/adm/createuser?action=usernamereqqueue',
                   4895:                       text => 'Result',
                   4896:                       help => 'Domain_Username_Approvals'});
                   4897:             $bread_crumbs_component = 'LON-CAPA account request result';
                   4898:         }
                   4899:         $args = { bread_crumbs           => $brcrum,
                   4900:                   bread_crumbs_component => $bread_crumbs_component};
                   4901:         my $js = &usernamerequest_javascript();
                   4902:         $r->print(&header(&add_script($js),$args));
                   4903:         if (!exists($env{'form.state'})) {
                   4904:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestusername',
                   4905:                                                                             $env{'request.role.domain'}));
                   4906:         } elsif ($env{'form.state'} eq 'done') {
                   4907:             $r->print('<h3>'.&mt('LON-CAPA account request processing').'</h3>'."\n");
                   4908:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestusername',
                   4909:                                                                          $env{'request.role.domain'}));
                   4910:         }
                   4911:     } elsif (($env{'form.action'} eq 'displayuserreq') &&
                   4912:              ($permission->{'cusr'})) {
                   4913:         my $dom = $env{'form.domain'};
                   4914:         my $uname = $env{'form.username'};
                   4915:         my $warning;
                   4916:         if (($dom =~ /^$match_domain$/) && (&Apache::lonnet::domain($dom) ne '')) {
                   4917:             if (($dom eq $env{'request.role.domain'}) && (&Apache::lonnet::allowed('ccc',$dom))) {
                   4918:                 if (($uname =~ /^$match_username$/) && ($env{'form.queue'} eq 'approval')) {
                   4919:                     my $uhome = &Apache::lonnet::homeserver($uname,$dom);
                   4920:                     if ($uhome eq 'no_host') {
                   4921:                         my $queue = $env{'form.queue'};
                   4922:                         my $reqkey = &escape($uname).'_'.$queue; 
                   4923:                         my $namespace = 'usernamequeue';
                   4924:                         my $domconfig = &Apache::lonnet::get_domainconfiguser($dom);
                   4925:                         my %queued =
                   4926:                             &Apache::lonnet::get($namespace,[$reqkey],$dom,$domconfig);
                   4927:                         unless ($queued{$reqkey}) {
                   4928:                             $warning = &mt('No information was found for this LON-CAPA account request.');
                   4929:                         }
                   4930:                     } else {
                   4931:                         $warning = &mt('A LON-CAPA account already exists for the requested username and domain.');
                   4932:                     }
                   4933:                 } else {
                   4934:                     $warning = &mt('LON-CAPA account request status check is for an invalid username.');
                   4935:                 }
                   4936:             } else {
                   4937:                 $warning = &mt('You do not have rights to view LON-CAPA account requests in the domain specified.');
                   4938:             }
                   4939:         } else {
                   4940:             $warning = &mt('LON-CAPA account request status check is for an invalid domain.');
                   4941:         }
                   4942:         my $args = { only_body => 1 };
                   4943:         $r->print(&header(undef,$args).
                   4944:                   '<h3>'.&mt('LON-CAPA Account Request Details').'</h3>');
                   4945:         if ($warning ne '') {
                   4946:             $r->print('<div class="LC_warning">'.$warning.'</div>');
                   4947:         } else {
                   4948:             my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
                   4949:             my $domconfiguser = &Apache::lonnet::get_domainconfiguser($dom);
                   4950:             my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   4951:             if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   4952:                 if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
                   4953:                     if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}) eq 'HASH') {
                   4954:                         my %info =
                   4955:                             &Apache::lonnet::get('nohist_requestedusernames',[$uname],$dom,$domconfiguser);
                   4956:                         if (ref($info{$uname}) eq 'HASH') {
1.396     raeburn  4957:                             my $usertype = $info{$uname}{'inststatus'};
                   4958:                             unless ($usertype) {
                   4959:                                 $usertype = 'default';
                   4960:                             }
                   4961:                             if (ref($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}) eq 'HASH') {
                   4962:                                 if ((ref($infofields) eq 'ARRAY') && (ref($infotitles) eq 'HASH')) {
                   4963:                                     $r->print('<div>'.&Apache::lonhtmlcommon::start_pick_box());
                   4964:                                     my ($num,$count,$showstatus);
                   4965:                                     $count = scalar(keys(%{$domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}}));
                   4966:                                     unless ($usertype eq 'default') {
                   4967:                                         my ($othertitle,$usertypes,$types) = 
                   4968:                                             &Apache::loncommon::sorted_inst_types($dom);
                   4969:                                         if (ref($usertypes) eq 'HASH') {
                   4970:                                             if ($usertypes->{$usertype}) {
                   4971:                                                 $showstatus = $usertypes->{$usertype};
                   4972:                                                 $count ++;
                   4973:                                             }
                   4974:                                         }
                   4975:                                     }
                   4976:                                     foreach my $field (@{$infofields}) {
                   4977:                                         next unless ($domconfig{'usercreation'}{'cancreate'}{'emailusername'}{$usertype}{$field});
                   4978:                                         next unless ($infotitles->{$field});
                   4979:                                         $r->print(&Apache::lonhtmlcommon::row_title($infotitles->{$field}).
                   4980:                                                   $info{$uname}{$field});
                   4981:                                         $num ++;
                   4982:                                         if ($count == $num) {
                   4983:                                             $r->print(&Apache::lonhtmlcommon::row_closure(1));
                   4984:                                         } else {
                   4985:                                             $r->print(&Apache::lonhtmlcommon::row_closure());
                   4986:                                         }
                   4987:                                     }
                   4988:                                     if ($showstatus) {
                   4989:                                         $r->print(&Apache::lonhtmlcommon::row_title(&mt('Status type (self-reported)')).
                   4990:                                                   $showstatus.
                   4991:                                                   &Apache::lonhtmlcommon::row_closure(1));
1.391     raeburn  4992:                                     }
1.396     raeburn  4993:                                     $r->print(&Apache::lonhtmlcommon::end_pick_box().'</div>');
1.391     raeburn  4994:                                 }
                   4995:                             }
                   4996:                         }
                   4997:                     }
                   4998:                 }
                   4999:             }
                   5000:             $r->print(&close_popup_form());
                   5001:         }
1.207     raeburn  5002:     } elsif (($env{'form.action'} eq 'listusers') && 
                   5003:              ($permission->{'view'} || $permission->{'cusr'})) {
1.202     raeburn  5004:         if ($env{'form.phase'} eq 'bulkchange') {
1.351     raeburn  5005:             push(@{$brcrum},
                   5006:                     {href => '/adm/createuser?action=listusers',
                   5007:                      text => "List Users"},
                   5008:                     {href => "/adm/createuser",
                   5009:                      text => "Result",
                   5010:                      help => 'Course_View_Class_List'});
                   5011:             $bread_crumbs_component = 'Update Users';
                   5012:             $args = {bread_crumbs           => $brcrum,
                   5013:                      bread_crumbs_component => $bread_crumbs_component};
                   5014:             $r->print(&header(undef,$args));
1.202     raeburn  5015:             my $setting = $env{'form.roletype'};
                   5016:             my $choice = $env{'form.bulkaction'};
                   5017:             if ($permission->{'cusr'}) {
1.336     raeburn  5018:                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice,$crstype);
1.221     raeburn  5019:             } else {
                   5020:                 $r->print(&mt('You are not authorized to make bulk changes to user roles'));
1.223     raeburn  5021:                 $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
1.202     raeburn  5022:             }
                   5023:         } else {
1.351     raeburn  5024:             push(@{$brcrum},
                   5025:                     {href => '/adm/createuser?action=listusers',
                   5026:                      text => "List Users",
                   5027:                      help => 'Course_View_Class_List'});
                   5028:             $bread_crumbs_component = 'List Users';
                   5029:             $args = {bread_crumbs           => $brcrum,
                   5030:                      bread_crumbs_component => $bread_crumbs_component};
1.202     raeburn  5031:             my ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles);
                   5032:             my $formname = 'studentform';
1.364     raeburn  5033:             my $hidecall = "hide_searching();";
1.321     raeburn  5034:             if (($context eq 'domain') && (($env{'form.roletype'} eq 'course') ||
                   5035:                 ($env{'form.roletype'} eq 'community'))) {
                   5036:                 if ($env{'form.roletype'} eq 'course') {
                   5037:                     ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles) = 
                   5038:                         &Apache::lonuserutils::courses_selector($env{'request.role.domain'},
                   5039:                                                                 $formname);
                   5040:                 } elsif ($env{'form.roletype'} eq 'community') {
                   5041:                     $cb_jscript = 
                   5042:                         &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
                   5043:                     my %elements = (
                   5044:                                       coursepick => 'radio',
                   5045:                                       coursetotal => 'text',
                   5046:                                       courselist => 'text',
                   5047:                                    );
                   5048:                     $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   5049:                 }
1.364     raeburn  5050:                 $jscript .= &verify_user_display($context)."\n".
                   5051:                             &Apache::loncommon::check_uncheck_jscript();
1.202     raeburn  5052:                 my $js = &add_script($jscript).$cb_jscript;
                   5053:                 my $loadcode = 
                   5054:                     &Apache::lonuserutils::course_selector_loadcode($formname);
                   5055:                 if ($loadcode ne '') {
1.364     raeburn  5056:                     $args->{add_entries} = {onload => "$loadcode;$hidecall"};
                   5057:                 } else {
                   5058:                     $args->{add_entries} = {onload => $hidecall};
1.202     raeburn  5059:                 }
1.351     raeburn  5060:                 $r->print(&header($js,$args));
1.191     raeburn  5061:             } else {
1.364     raeburn  5062:                 $args->{add_entries} = {onload => $hidecall};
                   5063:                 $jscript = &verify_user_display($context).
                   5064:                            &Apache::loncommon::check_uncheck_jscript(); 
                   5065:                 $r->print(&header(&add_script($jscript),$args));
1.191     raeburn  5066:             }
1.202     raeburn  5067:             &Apache::lonuserutils::print_userlist($r,undef,$permission,$context,
1.375     raeburn  5068:                          $formname,$totcodes,$codetitles,$idlist,$idlist_titles,
                   5069:                          $showcredits);
1.191     raeburn  5070:         }
1.213     raeburn  5071:     } elsif ($env{'form.action'} eq 'drop' && $permission->{'cusr'}) {
1.318     raeburn  5072:         my $brtext;
                   5073:         if ($crstype eq 'Community') {
                   5074:             $brtext = 'Drop Members';
                   5075:         } else {
                   5076:             $brtext = 'Drop Students';
                   5077:         }
1.351     raeburn  5078:         push(@{$brcrum},
                   5079:                 {href => '/adm/createuser?action=drop',
                   5080:                  text => $brtext,
                   5081:                  help => 'Course_Drop_Student'});
                   5082:         if ($env{'form.state'} eq 'done') {
                   5083:             push(@{$brcrum},
                   5084:                      {href=>'/adm/createuser?action=drop',
                   5085:                       text=>"Result"});
                   5086:         }
                   5087:         $bread_crumbs_component = $brtext;
                   5088:         $args = {bread_crumbs           => $brcrum,
                   5089:                  bread_crumbs_component => $bread_crumbs_component}; 
                   5090:         $r->print(&header(undef,$args));
1.213     raeburn  5091:         if (!exists($env{'form.state'})) {
1.318     raeburn  5092:             &Apache::lonuserutils::print_drop_menu($r,$context,$permission,$crstype);
1.213     raeburn  5093:         } elsif ($env{'form.state'} eq 'done') {
                   5094:             &Apache::lonuserutils::update_user_list($r,$context,undef,
                   5095:                                                     $env{'form.action'});
                   5096:         }
1.202     raeburn  5097:     } elsif ($env{'form.action'} eq 'dateselect') {
                   5098:         if ($permission->{'cusr'}) {
1.351     raeburn  5099:             $r->print(&header(undef,{'no_nav_bar' => 1}).
1.375     raeburn  5100:                       &Apache::lonuserutils::date_section_selector($context,$permission,
                   5101:                                                                    $crstype,$showcredits));
1.202     raeburn  5102:         } else {
1.351     raeburn  5103:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5104:                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>'); 
1.202     raeburn  5105:         }
1.237     raeburn  5106:     } elsif ($env{'form.action'} eq 'selfenroll') {
1.398     raeburn  5107:         if ($permission->{selfenrolladmin}) {
                   5108:             my $cid = $env{'request.course.id'};
                   5109:             my $cdom = $env{'course.'.$cid.'.domain'};
                   5110:             my $cnum = $env{'course.'.$cid.'.num'};
                   5111:             my %currsettings = (
                   5112:                 selfenroll_types              => $env{'course.'.$cid.'.internal.selfenroll_types'},
                   5113:                 selfenroll_registered         => $env{'course.'.$cid.'.internal.selfenroll_registered'},
                   5114:                 selfenroll_section            => $env{'course.'.$cid.'.internal.selfenroll_section'},
                   5115:                 selfenroll_notifylist         => $env{'course.'.$cid.'.internal.selfenroll_notifylist'},
                   5116:                 selfenroll_approval           => $env{'course.'.$cid.'.internal.selfenroll_approval'},
                   5117:                 selfenroll_limit              => $env{'course.'.$cid.'.internal.selfenroll_limit'},
                   5118:                 selfenroll_cap                => $env{'course.'.$cid.'.internal.selfenroll_cap'},
                   5119:                 selfenroll_start_date         => $env{'course.'.$cid.'.internal.selfenroll_start_date'},
                   5120:                 selfenroll_end_date           => $env{'course.'.$cid.'.internal.selfenroll_end_date'},
                   5121:                 selfenroll_start_access       => $env{'course.'.$cid.'.internal.selfenroll_start_access'},
                   5122:                 selfenroll_end_access         => $env{'course.'.$cid.'.internal.selfenroll_end_access'},
                   5123:                 default_enrollment_start_date => $env{'course.'.$cid.'.default_enrollment_start_date'},
                   5124:                 default_enrollment_end_date   => $env{'course.'.$cid.'.default_enrollment_end_date'},
1.400     raeburn  5125:                 uniquecode                    => $env{'course.'.$cid.'.internal.uniquecode'},
1.398     raeburn  5126:             );
                   5127:             push(@{$brcrum},
                   5128:                     {href => '/adm/createuser?action=selfenroll',
                   5129:                      text => "Configure Self-enrollment",
                   5130:                      help => 'Course_Self_Enrollment'});
                   5131:             if (!exists($env{'form.state'})) {
                   5132:                 $args = { bread_crumbs           => $brcrum,
                   5133:                           bread_crumbs_component => 'Configure Self-enrollment'};
                   5134:                 $r->print(&header(undef,$args));
                   5135:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
                   5136:                 &print_selfenroll_menu($r,'course',$cid,$cdom,$cnum,\%currsettings);
                   5137:             } elsif ($env{'form.state'} eq 'done') {
                   5138:                 push (@{$brcrum},
                   5139:                           {href=>'/adm/createuser?action=selfenroll',
                   5140:                            text=>"Result"});
                   5141:                 $args = { bread_crumbs           => $brcrum,
                   5142:                           bread_crumbs_component => 'Self-enrollment result'};
                   5143:                 $r->print(&header(undef,$args));
                   5144:                 $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
1.400     raeburn  5145:                 &update_selfenroll_config($r,$cid,$cdom,$cnum,$context,$crstype,\%currsettings);
1.398     raeburn  5146:             }
                   5147:         } else {
                   5148:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   5149:                      '<span class="LC_error">'.&mt('You do not have permission to configure self-enrollment').'</span>');
1.237     raeburn  5150:         }
1.277     raeburn  5151:     } elsif ($env{'form.action'} eq 'selfenrollqueue') {
1.418   ! raeburn  5152:         if ($permission->{selfenrolladmin}) {
1.351     raeburn  5153:             push(@{$brcrum},
                   5154:                      {href => '/adm/createuser?action=selfenrollqueue',
1.418   ! raeburn  5155:                       text => 'Enrollment requests',
1.351     raeburn  5156:                       help => 'Course_Self_Enrollment'});
1.418   ! raeburn  5157:             $bread_crumbs_component = 'Enrollment requests';
        !          5158:             if ($env{'form.state'} eq 'done') {
        !          5159:                 push(@{$brcrum},
        !          5160:                          {href => '/adm/createuser?action=selfenrollqueue',
        !          5161:                           text => 'Result',
        !          5162:                           help => 'Course_Self_Enrollment'});
        !          5163:                 $bread_crumbs_component = 'Enrollment result';
        !          5164:             }
        !          5165:             $args = { bread_crumbs           => $brcrum,
        !          5166:                       bread_crumbs_component => $bread_crumbs_component};
        !          5167:             $r->print(&header(undef,$args));
        !          5168:             my $cid = $env{'request.course.id'};
        !          5169:             my $cdom = $env{'course.'.$cid.'.domain'};
        !          5170:             my $cnum = $env{'course.'.$cid.'.num'};
        !          5171:             my $coursedesc = $env{'course.'.$cid.'.description'};
        !          5172:             if (!exists($env{'form.state'})) {
        !          5173:                 $r->print('<h3>'.&mt('Pending enrollment requests').'</h3>'."\n");
        !          5174:                 $r->print(&Apache::loncoursequeueadmin::display_queued_requests($context,
        !          5175:                                                                                 $cdom,$cnum));
        !          5176:             } elsif ($env{'form.state'} eq 'done') {
        !          5177:                 $r->print('<h3>'.&mt('Enrollment request processing').'</h3>'."\n");
        !          5178:                 $r->print(&Apache::loncoursequeueadmin::update_request_queue($context,
        !          5179:                                $cdom,$cnum,$coursedesc));
        !          5180:             }
        !          5181:         } else {
        !          5182:             $r->print(&header(undef,{'no_nav_bar' => 1}).
        !          5183:                      '<span class="LC_error">'.&mt('You do not have permission to manage self-enrollment').'</span>');
1.351     raeburn  5184:         }
1.418   ! raeburn  5185:     } elsif ($env{'form.action'} eq 'changelogs') {
        !          5186:         if ($permission->{cusr} || $permission->{view}) {
        !          5187:             &print_userchangelogs_display($r,$context,$permission,$brcrum);
        !          5188:         } else {
        !          5189:             $r->print(&header(undef,{'no_nav_bar' => 1}).
        !          5190:                      '<span class="LC_error">'.&mt('You do not have permission to view change logs').'</span>');
1.277     raeburn  5191:         }
1.190     raeburn  5192:     } else {
1.351     raeburn  5193:         $bread_crumbs_component = 'User Management';
                   5194:         $args = { bread_crumbs           => $brcrum,
                   5195:                   bread_crumbs_component => $bread_crumbs_component};
                   5196:         $r->print(&header(undef,$args));
1.318     raeburn  5197:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  5198:     }
1.351     raeburn  5199:     $r->print(&Apache::loncommon::end_page());
1.190     raeburn  5200:     return OK;
                   5201: }
                   5202: 
                   5203: sub header {
1.351     raeburn  5204:     my ($jscript,$args) = @_;
1.190     raeburn  5205:     my $start_page;
1.351     raeburn  5206:     if (ref($args) eq 'HASH') {
                   5207:         $start_page=&Apache::loncommon::start_page('User Management',$jscript,$args);
1.190     raeburn  5208:     } else {
1.351     raeburn  5209:         $start_page=&Apache::loncommon::start_page('User Management',$jscript);
1.190     raeburn  5210:     }
                   5211:     return $start_page;
                   5212: }
1.2       www      5213: 
1.191     raeburn  5214: sub add_script {
                   5215:     my ($js) = @_;
1.301     bisitz   5216:     return '<script type="text/javascript">'."\n"
                   5217:           .'// <![CDATA['."\n"
                   5218:           .$js."\n"
                   5219:           .'// ]]>'."\n"
                   5220:           .'</script>'."\n";
1.191     raeburn  5221: }
                   5222: 
1.391     raeburn  5223: sub usernamerequest_javascript {
                   5224:     my $js = <<ENDJS;
                   5225: 
                   5226: function openusernamereqdisplay(dom,uname,queue) {
                   5227:     var url = '/adm/createuser?action=displayuserreq';
                   5228:     url += '&domain='+dom+'&username='+uname+'&queue='+queue;
                   5229:     var title = 'Account_Request_Browser';
                   5230:     var options = 'scrollbars=1,resizable=1,menubar=0';
                   5231:     options += ',width=700,height=600';
                   5232:     var stdeditbrowser = open(url,title,options,'1');
                   5233:     stdeditbrowser.focus();
                   5234:     return;
                   5235: }
                   5236:  
                   5237: ENDJS
                   5238: }
                   5239: 
                   5240: sub close_popup_form {
                   5241:     my $close= &mt('Close Window');
                   5242:     return << "END";
                   5243: <p><form name="displayreq" action="" method="post">
                   5244: <input type="button" name="closeme" value="$close" onclick="javascript:self.close();" />
                   5245: </form></p>
                   5246: END
                   5247: }
                   5248: 
1.202     raeburn  5249: sub verify_user_display {
1.364     raeburn  5250:     my ($context) = @_;
1.374     raeburn  5251:     my %lt = &Apache::lonlocal::texthash (
                   5252:         course    => 'course(s): description, section(s), status',
                   5253:         community => 'community(s): description, section(s), status',
                   5254:         author    => 'author',
                   5255:     );
1.364     raeburn  5256:     my $photos;
                   5257:     if (($context eq 'course') && $env{'request.course.id'}) {
                   5258:         $photos = $env{'course.'.$env{'request.course.id'}.'.internal.showphoto'};
                   5259:     }
1.202     raeburn  5260:     my $output = <<"END";
                   5261: 
1.364     raeburn  5262: function hide_searching() {
                   5263:     if (document.getElementById('searching')) {
                   5264:         document.getElementById('searching').style.display = 'none';
                   5265:     }
                   5266:     return;
                   5267: }
                   5268: 
1.202     raeburn  5269: function display_update() {
                   5270:     document.studentform.action.value = 'listusers';
                   5271:     document.studentform.phase.value = 'display';
                   5272:     document.studentform.submit();
                   5273: }
                   5274: 
1.364     raeburn  5275: function updateCols(caller) {
                   5276:     var context = '$context';
                   5277:     var photos = '$photos';
                   5278:     if (caller == 'Status') {
1.374     raeburn  5279:         if ((context == 'domain') && 
                   5280:             ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   5281:              (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community'))) {
1.364     raeburn  5282:             document.getElementById('showcolstatus').checked = false;
                   5283:             document.getElementById('showcolstatus').disabled = 'disabled';
                   5284:             document.getElementById('showcolstart').checked = false;
                   5285:             document.getElementById('showcolend').checked = false;
1.374     raeburn  5286:         } else {
                   5287:             if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   5288:                 document.getElementById('showcolstatus').checked = true;
                   5289:                 document.getElementById('showcolstatus').disabled = '';
                   5290:                 document.getElementById('showcolstart').checked = true;
                   5291:                 document.getElementById('showcolend').checked = true;
                   5292:             } else {
                   5293:                 document.getElementById('showcolstatus').checked = false;
                   5294:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   5295:                 document.getElementById('showcolstart').checked = false;
                   5296:                 document.getElementById('showcolend').checked = false;
                   5297:             }
1.364     raeburn  5298:         }
                   5299:     }
                   5300:     if (caller == 'output') {
                   5301:         if (photos == 1) {
                   5302:             if (document.getElementById('showcolphoto')) {
                   5303:                 var photoitem = document.getElementById('showcolphoto');
                   5304:                 if (document.studentform.output.options[document.studentform.output.selectedIndex].value == 'html') {
                   5305:                     photoitem.checked = true;
                   5306:                     photoitem.disabled = '';
                   5307:                 } else {
                   5308:                     photoitem.checked = false;
                   5309:                     photoitem.disabled = 'disabled';
                   5310:                 }
                   5311:             }
                   5312:         }
                   5313:     }
                   5314:     if (caller == 'showrole') {
1.371     raeburn  5315:         if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any') ||
                   5316:             (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'cr')) {
1.364     raeburn  5317:             document.getElementById('showcolrole').checked = true;
                   5318:             document.getElementById('showcolrole').disabled = '';
                   5319:         } else {
                   5320:             document.getElementById('showcolrole').checked = false;
                   5321:             document.getElementById('showcolrole').disabled = 'disabled';
                   5322:         }
1.374     raeburn  5323:         if (context == 'domain') {
1.382     raeburn  5324:             var quotausageshow = 0;
1.374     raeburn  5325:             if ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   5326:                 (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community')) {
                   5327:                 document.getElementById('showcolstatus').checked = false;
                   5328:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   5329:                 document.getElementById('showcolstart').checked = false;
                   5330:                 document.getElementById('showcolend').checked = false;
                   5331:             } else {
                   5332:                 if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   5333:                     document.getElementById('showcolstatus').checked = true;
                   5334:                     document.getElementById('showcolstatus').disabled = '';
                   5335:                     document.getElementById('showcolstart').checked = true;
                   5336:                     document.getElementById('showcolend').checked = true;
                   5337:                 }
                   5338:             }
                   5339:             if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'domain') {
                   5340:                 document.getElementById('showcolextent').disabled = 'disabled';
                   5341:                 document.getElementById('showcolextent').checked = 'false';
                   5342:                 document.getElementById('showextent').style.display='none';
                   5343:                 document.getElementById('showcoltextextent').innerHTML = '';
1.382     raeburn  5344:                 if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'au') ||
                   5345:                     (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any')) {
                   5346:                     if (document.getElementById('showcolauthorusage')) {
                   5347:                         document.getElementById('showcolauthorusage').disabled = '';
                   5348:                     }
                   5349:                     if (document.getElementById('showcolauthorquota')) {
                   5350:                         document.getElementById('showcolauthorquota').disabled = '';
                   5351:                     }
                   5352:                     quotausageshow = 1;
                   5353:                 }
1.374     raeburn  5354:             } else {
                   5355:                 document.getElementById('showextent').style.display='block';
                   5356:                 document.getElementById('showextent').style.textAlign='left';
                   5357:                 document.getElementById('showextent').style.textFace='normal';
                   5358:                 if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'author') {
                   5359:                     document.getElementById('showcolextent').disabled = '';
                   5360:                     document.getElementById('showcolextent').checked = 'true';
                   5361:                     document.getElementById('showcoltextextent').innerHTML="$lt{'author'}";
                   5362:                 } else {
                   5363:                     document.getElementById('showcolextent').disabled = '';
                   5364:                     document.getElementById('showcolextent').checked = 'true';
                   5365:                     if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community') {
                   5366:                         document.getElementById('showcoltextextent').innerHTML="$lt{'community'}";
                   5367:                     } else {
                   5368:                         document.getElementById('showcoltextextent').innerHTML="$lt{'course'}";
                   5369:                     }
                   5370:                 }
                   5371:             }
1.382     raeburn  5372:             if (quotausageshow == 0)  {
                   5373:                 if (document.getElementById('showcolauthorusage')) {
                   5374:                     document.getElementById('showcolauthorusage').checked = false;
                   5375:                     document.getElementById('showcolauthorusage').disabled = 'disabled';
                   5376:                 }
                   5377:                 if (document.getElementById('showcolauthorquota')) {
                   5378:                     document.getElementById('showcolauthorquota').checked = false;
                   5379:                     document.getElementById('showcolauthorquota').disabled = 'disabled';
                   5380:                 }
                   5381:             }
1.374     raeburn  5382:         }
1.364     raeburn  5383:     }
                   5384:     return;
                   5385: }
                   5386: 
1.202     raeburn  5387: END
                   5388:     return $output;
                   5389: 
                   5390: }
                   5391: 
1.190     raeburn  5392: ###############################################################
                   5393: ###############################################################
                   5394: #  Menu Phase One
                   5395: sub print_main_menu {
1.318     raeburn  5396:     my ($permission,$context,$crstype) = @_;
                   5397:     my $linkcontext = $context;
                   5398:     my $stuterm = lc(&Apache::lonnet::plaintext('st',$crstype));
                   5399:     if (($context eq 'course') && ($crstype eq 'Community')) {
                   5400:         $linkcontext = lc($crstype);
                   5401:         $stuterm = 'Members';
                   5402:     }
1.208     raeburn  5403:     my %links = (
1.298     droeschl 5404:                 domain => {
                   5405:                             upload     => 'Upload a File of Users',
                   5406:                             singleuser => 'Add/Modify a User',
                   5407:                             listusers  => 'Manage Users',
                   5408:                             },
                   5409:                 author => {
                   5410:                             upload     => 'Upload a File of Co-authors',
                   5411:                             singleuser => 'Add/Modify a Co-author',
                   5412:                             listusers  => 'Manage Co-authors',
                   5413:                             },
                   5414:                 course => {
                   5415:                             upload     => 'Upload a File of Course Users',
                   5416:                             singleuser => 'Add/Modify a Course User',
1.354     www      5417:                             listusers  => 'List and Modify Multiple Course Users',
1.298     droeschl 5418:                             },
1.318     raeburn  5419:                 community => {
                   5420:                             upload     => 'Upload a File of Community Users',
                   5421:                             singleuser => 'Add/Modify a Community User',
1.354     www      5422:                             listusers  => 'List and Modify Multiple Community Users',
1.318     raeburn  5423:                            },
                   5424:                 );
                   5425:      my %linktitles = (
                   5426:                 domain => {
                   5427:                             singleuser => 'Add a user to the domain, and/or a course or community in the domain.',
                   5428:                             listusers  => 'Show and manage users in this domain.',
                   5429:                             },
                   5430:                 author => {
                   5431:                             singleuser => 'Add a user with a co- or assistant author role.',
                   5432:                             listusers  => 'Show and manage co- or assistant authors.',
                   5433:                             },
                   5434:                 course => {
                   5435:                             singleuser => 'Add a user with a certain role to this course.',
                   5436:                             listusers  => 'Show and manage users in this course.',
                   5437:                             },
                   5438:                 community => {
                   5439:                             singleuser => 'Add a user with a certain role to this community.',
                   5440:                             listusers  => 'Show and manage users in this community.',
                   5441:                            },
1.298     droeschl 5442:                 );
1.418   ! raeburn  5443:   if ($linkcontext eq 'domain') {
        !          5444:       unless ($permission->{'cusr'}) {
        !          5445:           $links{'domain'}{'singleuser'} = 'View a User'; 
        !          5446:           $linktitles{'domain'}{'singleuser'} = 'View information about a user in the domain';
        !          5447:           
        !          5448:       }
        !          5449:   } elsif ($linkcontext eq 'course') {
        !          5450:       unless ($permission->{'cusr'}) {
        !          5451:           $links{'course'}{'singleuser'} = 'View a Course User';
        !          5452:           $linktitles{'course'}{'singleuser'} = 'View information about a user in this course';
        !          5453:           $links{'course'}{'listusers'} = 'List Course Users';
        !          5454:           $linktitles{'course'}{'listusers'} = 'Show information about users in this course';
        !          5455:       }
        !          5456:   } elsif ($linkcontext eq 'community') {
        !          5457:       unless ($permission->{'cusr'}) {
        !          5458:           $links{'community'}{'singleuser'} = 'View a Community User';
        !          5459:           $linktitles{'community'}{'singleuser'} = 'View information about a user in this community';
        !          5460:           $links{'community'}{'listusers'} = 'List Community Users';
        !          5461:           $linktitles{'community'}{'listusers'} = 'Show information about users in this community';
        !          5462:       }
        !          5463:   }
1.298     droeschl 5464:   my @menu = ( {categorytitle => 'Single Users', 
                   5465:          items =>
                   5466:          [
                   5467:             {
1.318     raeburn  5468:              linktext => $links{$linkcontext}{'singleuser'},
1.298     droeschl 5469:              icon => 'edit-redo.png',
                   5470:              #help => 'Course_Change_Privileges',
                   5471:              url => '/adm/createuser?action=singleuser',
1.418   ! raeburn  5472:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  5473:              linktitle => $linktitles{$linkcontext}{'singleuser'},
1.298     droeschl 5474:             },
                   5475:          ]},
                   5476: 
                   5477:          {categorytitle => 'Multiple Users',
                   5478:          items => 
                   5479:          [
                   5480:             {
1.318     raeburn  5481:              linktext => $links{$linkcontext}{'upload'},
1.340     wenzelju 5482:              icon => 'uplusr.png',
1.298     droeschl 5483:              #help => 'Course_Create_Class_List',
                   5484:              url => '/adm/createuser?action=upload',
                   5485:              permission => $permission->{'cusr'},
                   5486:              linktitle => 'Upload a CSV or a text file containing users.',
                   5487:             },
                   5488:             {
1.318     raeburn  5489:              linktext => $links{$linkcontext}{'listusers'},
1.340     wenzelju 5490:              icon => 'mngcu.png',
1.298     droeschl 5491:              #help => 'Course_View_Class_List',
                   5492:              url => '/adm/createuser?action=listusers',
                   5493:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  5494:              linktitle => $linktitles{$linkcontext}{'listusers'}, 
1.298     droeschl 5495:             },
                   5496: 
                   5497:          ]},
                   5498: 
                   5499:          {categorytitle => 'Administration',
                   5500:          items => [ ]},
                   5501:        );
1.415     raeburn  5502: 
1.265     mielkec  5503:     if ($context eq 'domain'){
1.416     raeburn  5504:         push(@{  $menu[0]->{items} }, # Single Users
                   5505:             {
                   5506:              linktext => 'User Access Log',
1.417     raeburn  5507:              icon => 'document-properties.png',
1.416     raeburn  5508:              #help => 'User_Access_Logs',
                   5509:              url => '/adm/createuser?action=accesslogs',
                   5510:              permission => $permission->{'activity'},
                   5511:              linktitle => 'View user access log.',
                   5512:             }
                   5513:         );
1.298     droeschl 5514:         
                   5515:         push(@{ $menu[2]->{items} }, #Category: Administration
                   5516:             {
                   5517:              linktext => 'Custom Roles',
                   5518:              icon => 'emblem-photos.png',
                   5519:              #help => 'Course_Editing_Custom_Roles',
                   5520:              url => '/adm/createuser?action=custom',
                   5521:              permission => $permission->{'custom'},
                   5522:              linktitle => 'Configure a custom role.',
                   5523:             },
1.362     raeburn  5524:             {
                   5525:              linktext => 'Authoring Space Requests',
                   5526:              icon => 'selfenrl-queue.png',
                   5527:              #help => 'Domain_Role_Approvals',
                   5528:              url => '/adm/createuser?action=processauthorreq',
                   5529:              permission => $permission->{'cusr'},
                   5530:              linktitle => 'Approve or reject author role requests',
                   5531:             },
1.363     raeburn  5532:             {
1.391     raeburn  5533:              linktext => 'LON-CAPA Account Requests',
                   5534:              icon => 'list-add.png',
                   5535:              #help => 'Domain_Username_Approvals',
                   5536:              url => '/adm/createuser?action=processusernamereq',
                   5537:              permission => $permission->{'cusr'},
                   5538:              linktitle => 'Approve or reject LON-CAPA account requests',
                   5539:             },
                   5540:             {
1.363     raeburn  5541:              linktext => 'Change Log',
                   5542:              icon => 'document-properties.png',
                   5543:              #help => 'Course_User_Logs',
                   5544:              url => '/adm/createuser?action=changelogs',
1.418   ! raeburn  5545:              permission => ($permission->{'cusr'} || $permission->{'view'}),
1.363     raeburn  5546:              linktitle => 'View change log.',
                   5547:             },
1.298     droeschl 5548:         );
                   5549:         
1.265     mielkec  5550:     }elsif ($context eq 'course'){
1.298     droeschl 5551:         my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
1.318     raeburn  5552: 
                   5553:         my %linktext = (
                   5554:                          'Course'    => {
                   5555:                                           single => 'Add/Modify a Student', 
                   5556:                                           drop   => 'Drop Students',
                   5557:                                           groups => 'Course Groups',
                   5558:                                         },
                   5559:                          'Community' => {
                   5560:                                           single => 'Add/Modify a Member', 
                   5561:                                           drop   => 'Drop Members',
                   5562:                                           groups => 'Community Groups',
                   5563:                                         },
                   5564:                        );
1.411     raeburn  5565:         $linktext{'Placement'} = $linktext{'Course'};
1.318     raeburn  5566: 
                   5567:         my %linktitle = (
                   5568:             'Course' => {
                   5569:                   single => 'Add a user with the role of student to this course',
                   5570:                   drop   => 'Remove a student from this course.',
                   5571:                   groups => 'Manage course groups',
                   5572:                         },
                   5573:             'Community' => {
                   5574:                   single => 'Add a user with the role of member to this community',
                   5575:                   drop   => 'Remove a member from this community.',
                   5576:                   groups => 'Manage community groups',
                   5577:                            },
                   5578:         );
                   5579: 
1.411     raeburn  5580:         $linktitle{'Placement'} = $linktitle{'Course'};
                   5581: 
1.298     droeschl 5582:         push(@{ $menu[0]->{items} }, #Category: Single Users
                   5583:             {   
1.318     raeburn  5584:              linktext => $linktext{$crstype}{'single'},
1.298     droeschl 5585:              #help => 'Course_Add_Student',
                   5586:              icon => 'list-add.png',
                   5587:              url => '/adm/createuser?action=singlestudent',
                   5588:              permission => $permission->{'cusr'},
1.318     raeburn  5589:              linktitle => $linktitle{$crstype}{'single'},
1.298     droeschl 5590:             },
                   5591:         );
                   5592:         
                   5593:         push(@{ $menu[1]->{items} }, #Category: Multiple Users 
                   5594:             {
1.318     raeburn  5595:              linktext => $linktext{$crstype}{'drop'},
1.298     droeschl 5596:              icon => 'edit-undo.png',
                   5597:              #help => 'Course_Drop_Student',
                   5598:              url => '/adm/createuser?action=drop',
                   5599:              permission => $permission->{'cusr'},
1.318     raeburn  5600:              linktitle => $linktitle{$crstype}{'drop'},
1.298     droeschl 5601:             },
                   5602:         );
                   5603:         push(@{ $menu[2]->{items} }, #Category: Administration
                   5604:             {    
                   5605:              linktext => 'Custom Roles',
                   5606:              icon => 'emblem-photos.png',
                   5607:              #help => 'Course_Editing_Custom_Roles',
                   5608:              url => '/adm/createuser?action=custom',
                   5609:              permission => $permission->{'custom'},
                   5610:              linktitle => 'Configure a custom role.',
                   5611:             },
                   5612:             {
1.318     raeburn  5613:              linktext => $linktext{$crstype}{'groups'},
1.333     wenzelju 5614:              icon => 'grps.png',
1.298     droeschl 5615:              #help => 'Course_Manage_Group',
                   5616:              url => '/adm/coursegroups?refpage=cusr',
                   5617:              permission => $permission->{'grp_manage'},
1.318     raeburn  5618:              linktitle => $linktitle{$crstype}{'groups'},
1.298     droeschl 5619:             },
                   5620:             {
1.328     wenzelju 5621:              linktext => 'Change Log',
1.298     droeschl 5622:              icon => 'document-properties.png',
                   5623:              #help => 'Course_User_Logs',
                   5624:              url => '/adm/createuser?action=changelogs',
1.418   ! raeburn  5625:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.298     droeschl 5626:              linktitle => 'View change log.',
                   5627:             },
                   5628:         );
1.277     raeburn  5629:         if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'}) {
1.298     droeschl 5630:             push(@{ $menu[2]->{items} },
1.398     raeburn  5631:                     {
1.298     droeschl 5632:                      linktext => 'Enrollment Requests',
                   5633:                      icon => 'selfenrl-queue.png',
                   5634:                      #help => 'Course_Approve_Selfenroll',
                   5635:                      url => '/adm/createuser?action=selfenrollqueue',
1.398     raeburn  5636:                      permission => $permission->{'selfenrolladmin'},
1.298     droeschl 5637:                      linktitle =>'Approve or reject enrollment requests.',
                   5638:                     },
                   5639:             );
1.277     raeburn  5640:         }
1.298     droeschl 5641:         
1.265     mielkec  5642:         if (!exists($permission->{'cusr_section'})){
1.320     raeburn  5643:             if ($crstype ne 'Community') {
                   5644:                 push(@{ $menu[2]->{items} },
                   5645:                     {
                   5646:                      linktext => 'Automated Enrollment',
                   5647:                      icon => 'roles.png',
                   5648:                      #help => 'Course_Automated_Enrollment',
                   5649:                      permission => (&Apache::lonnet::auto_run($cnum,$cdom)
1.418   ! raeburn  5650:                                          && (($permission->{'cusr'}) ||
        !          5651:                                              ($permission->{'view'}))),
1.320     raeburn  5652:                      url  => '/adm/populate',
                   5653:                      linktitle => 'Automated enrollment manager.',
                   5654:                     }
                   5655:                 );
                   5656:             }
                   5657:             push(@{ $menu[2]->{items} }, 
1.298     droeschl 5658:                 {
                   5659:                  linktext => 'User Self-Enrollment',
1.342     wenzelju 5660:                  icon => 'self_enroll.png',
1.298     droeschl 5661:                  #help => 'Course_Self_Enrollment',
                   5662:                  url => '/adm/createuser?action=selfenroll',
1.398     raeburn  5663:                  permission => $permission->{'selfenrolladmin'},
1.317     bisitz   5664:                  linktitle => 'Configure user self-enrollment.',
1.298     droeschl 5665:                 },
                   5666:             );
                   5667:         }
1.363     raeburn  5668:     } elsif ($context eq 'author') {
1.370     raeburn  5669:         push(@{ $menu[2]->{items} }, #Category: Administration
1.363     raeburn  5670:             {
                   5671:              linktext => 'Change Log',
                   5672:              icon => 'document-properties.png',
                   5673:              #help => 'Course_User_Logs',
                   5674:              url => '/adm/createuser?action=changelogs',
                   5675:              permission => $permission->{'cusr'},
                   5676:              linktitle => 'View change log.',
                   5677:             },
1.370     raeburn  5678:         );
1.363     raeburn  5679:     }
                   5680:     return Apache::lonhtmlcommon::generate_menu(@menu);
1.250     raeburn  5681: #               { text => 'View Log-in History',
                   5682: #                 help => 'Course_User_Logins',
                   5683: #                 action => 'logins',
                   5684: #                 permission => $permission->{'cusr'},
                   5685: #               });
1.190     raeburn  5686: }
                   5687: 
1.189     albertel 5688: sub restore_prev_selections {
                   5689:     my %saveable_parameters = ('srchby'   => 'scalar',
                   5690: 			       'srchin'   => 'scalar',
                   5691: 			       'srchtype' => 'scalar',
                   5692: 			       );
                   5693:     &Apache::loncommon::store_settings('user','user_picker',
                   5694: 				       \%saveable_parameters);
                   5695:     &Apache::loncommon::restore_settings('user','user_picker',
                   5696: 					 \%saveable_parameters);
                   5697: }
                   5698: 
1.237     raeburn  5699: sub print_selfenroll_menu {
1.418   ! raeburn  5700:     my ($r,$context,$cid,$cdom,$cnum,$currsettings,$additional,$readonly) = @_;
1.322     raeburn  5701:     my $crstype = &Apache::loncommon::course_type();
1.398     raeburn  5702:     my $formname = 'selfenroll';
1.237     raeburn  5703:     my $nolink = 1;
1.398     raeburn  5704:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
1.237     raeburn  5705:     my $groupslist = &Apache::lonuserutils::get_groupslist();
                   5706:     my $setsec_js = 
                   5707:         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
1.249     raeburn  5708:     my %alerts = &Apache::lonlocal::texthash(
                   5709:         acto => 'Activation of self-enrollment was selected for the following domain(s)',
                   5710:         butn => 'but no user types have been checked.',
                   5711:         wilf => "Please uncheck 'activate' or check at least one type.",
                   5712:     );
1.418   ! raeburn  5713:     my $disabled;
        !          5714:     if ($readonly) {
        !          5715:        $disabled = ' disabled="disabled"';
        !          5716:     }
1.405     damieng  5717:     &js_escape(\%alerts);
1.249     raeburn  5718:     my $selfenroll_js = <<"ENDSCRIPT";
                   5719: function update_types(caller,num) {
                   5720:     var delidx = getIndexByName('selfenroll_delete');
                   5721:     var actidx = getIndexByName('selfenroll_activate');
                   5722:     if (caller == 'selfenroll_all') {
                   5723:         var selall;
                   5724:         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5725:             if (document.$formname.selfenroll_all[i].checked) {
                   5726:                 selall = document.$formname.selfenroll_all[i].value;
                   5727:             }
                   5728:         }
                   5729:         if (selall == 1) {
                   5730:             if (delidx != -1) {
                   5731:                 if (document.$formname.selfenroll_delete.length) {
                   5732:                     for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5733:                         document.$formname.selfenroll_delete[j].checked = true;
                   5734:                     }
                   5735:                 } else {
                   5736:                     document.$formname.elements[delidx].checked = true;
                   5737:                 }
                   5738:             }
                   5739:             if (actidx != -1) {
                   5740:                 if (document.$formname.selfenroll_activate.length) {
                   5741:                     for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5742:                         document.$formname.selfenroll_activate[j].checked = false;
                   5743:                     }
                   5744:                 } else {
                   5745:                     document.$formname.elements[actidx].checked = false;
                   5746:                 }
                   5747:             }
                   5748:             document.$formname.selfenroll_newdom.selectedIndex = 0; 
                   5749:         }
                   5750:     }
                   5751:     if (caller == 'selfenroll_activate') {
                   5752:         if (document.$formname.selfenroll_activate.length) {
                   5753:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5754:                 if (document.$formname.selfenroll_activate[j].value == num) {
                   5755:                     if (document.$formname.selfenroll_activate[j].checked) {
                   5756:                         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5757:                             if (document.$formname.selfenroll_all[i].value == '1') {
                   5758:                                 document.$formname.selfenroll_all[i].checked = false;
                   5759:                             }
                   5760:                             if (document.$formname.selfenroll_all[i].value == '0') {
                   5761:                                 document.$formname.selfenroll_all[i].checked = true;
                   5762:                             }
                   5763:                         }
                   5764:                     }
                   5765:                 }
                   5766:             }
                   5767:         } else {
                   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:     if (caller == 'selfenroll_delete') {
                   5779:         if (document.$formname.selfenroll_delete.length) {
                   5780:             for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5781:                 if (document.$formname.selfenroll_delete[j].value == num) {
                   5782:                     if (document.$formname.selfenroll_delete[j].checked) {
                   5783:                         var delindex = getIndexByName('selfenroll_types_'+num);
                   5784:                         if (delindex != -1) { 
                   5785:                             if (document.$formname.elements[delindex].length) {
                   5786:                                 for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5787:                                     document.$formname.elements[delindex][k].checked = false;
                   5788:                                 }
                   5789:                             } else {
                   5790:                                 document.$formname.elements[delindex].checked = false;
                   5791:                             }
                   5792:                         }
                   5793:                     }
                   5794:                 }
                   5795:             }
                   5796:         } else {
                   5797:             if (document.$formname.selfenroll_delete.checked) {
                   5798:                 var delindex = getIndexByName('selfenroll_types_'+num);
                   5799:                 if (delindex != -1) {
                   5800:                     if (document.$formname.elements[delindex].length) {
                   5801:                         for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5802:                             document.$formname.elements[delindex][k].checked = false;
                   5803:                         }
                   5804:                     } else {
                   5805:                         document.$formname.elements[delindex].checked = false;
                   5806:                     }
                   5807:                 }
                   5808:             }
                   5809:         }
                   5810:     }
                   5811:     return;
                   5812: }
                   5813: 
                   5814: function validate_types(form) {
                   5815:     var needaction = new Array();
                   5816:     var countfail = 0;
                   5817:     var actidx = getIndexByName('selfenroll_activate');
                   5818:     if (actidx != -1) {
                   5819:         if (document.$formname.selfenroll_activate.length) {
                   5820:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5821:                 var num = document.$formname.selfenroll_activate[j].value;
                   5822:                 if (document.$formname.selfenroll_activate[j].checked) {
                   5823:                     countfail = check_types(num,countfail,needaction)
                   5824:                 }
                   5825:             }
                   5826:         } else {
                   5827:             if (document.$formname.selfenroll_activate.checked) {
1.398     raeburn  5828:                 var num = document.$formname.selfenroll_activate.value;
1.249     raeburn  5829:                 countfail = check_types(num,countfail,needaction)
                   5830:             }
                   5831:         }
                   5832:     }
                   5833:     if (countfail > 0) {
                   5834:         var msg = "$alerts{'acto'}\\n";
                   5835:         var loopend = needaction.length -1;
                   5836:         if (loopend > 0) {
                   5837:             for (var m=0; m<loopend; m++) {
                   5838:                 msg += needaction[m]+", ";
                   5839:             }
                   5840:         }
                   5841:         msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
                   5842:         alert(msg);
                   5843:         return; 
                   5844:     }
                   5845:     setSections(form);
                   5846: }
                   5847: 
                   5848: function check_types(num,countfail,needaction) {
                   5849:     var typeidx = getIndexByName('selfenroll_types_'+num);
                   5850:     var count = 0;
                   5851:     if (typeidx != -1) {
                   5852:         if (document.$formname.elements[typeidx].length) {
                   5853:             for (var k=0; k<document.$formname.elements[typeidx].length; k++) {
                   5854:                 if (document.$formname.elements[typeidx][k].checked) {
                   5855:                     count ++;
                   5856:                 }
                   5857:             }
                   5858:         } else {
                   5859:             if (document.$formname.elements[typeidx].checked) {
                   5860:                 count ++;
                   5861:             }
                   5862:         }
                   5863:         if (count == 0) {
                   5864:             var domidx = getIndexByName('selfenroll_dom_'+num);
                   5865:             if (domidx != -1) {
                   5866:                 var domname = document.$formname.elements[domidx].value;
                   5867:                 needaction[countfail] = domname;
                   5868:                 countfail ++;
                   5869:             }
                   5870:         }
                   5871:     }
                   5872:     return countfail;
                   5873: }
                   5874: 
1.398     raeburn  5875: function toggleNotify() {
                   5876:     var selfenrollApproval = 0;
                   5877:     if (document.$formname.selfenroll_approval.length) {
                   5878:         for (var i=0; i<document.$formname.selfenroll_approval.length; i++) {
                   5879:             if (document.$formname.selfenroll_approval[i].checked) {
                   5880:                 selfenrollApproval = document.$formname.selfenroll_approval[i].value;
                   5881:                 break;        
                   5882:             }
                   5883:         }
                   5884:     }
                   5885:     if (document.getElementById('notified')) {
                   5886:         if (selfenrollApproval == 0) {
                   5887:             document.getElementById('notified').style.display='none';
                   5888:         } else {
                   5889:             document.getElementById('notified').style.display='block';
                   5890:         }
                   5891:     }
                   5892:     return;
                   5893: }
                   5894: 
1.249     raeburn  5895: function getIndexByName(item) {
                   5896:     for (var i=0;i<document.$formname.elements.length;i++) {
                   5897:         if (document.$formname.elements[i].name == item) {
                   5898:             return i;
                   5899:         }
                   5900:     }
                   5901:     return -1;
                   5902: }
                   5903: ENDSCRIPT
1.256     raeburn  5904: 
1.237     raeburn  5905:     my $output = '<script type="text/javascript">'."\n".
1.301     bisitz   5906:                  '// <![CDATA['."\n".
1.249     raeburn  5907:                  $setsec_js."\n".$selfenroll_js."\n".
1.301     bisitz   5908:                  '// ]]>'."\n".
1.237     raeburn  5909:                  '</script>'."\n".
1.256     raeburn  5910:                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n";
1.400     raeburn  5911:  
                   5912:     my $visactions = &cat_visibility();
                   5913:     my ($cathash,%cattype);
                   5914:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   5915:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   5916:         $cathash = $domconfig{'coursecategories'}{'cats'};
                   5917:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
                   5918:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
1.406     raeburn  5919:         if ($cattype{'auth'} eq '') {
                   5920:             $cattype{'auth'} = 'std';
                   5921:         }
                   5922:         if ($cattype{'unauth'} eq '') {
                   5923:             $cattype{'unauth'} = 'std';
                   5924:         }
1.400     raeburn  5925:     } else {
                   5926:         $cathash = {};
                   5927:         $cattype{'auth'} = 'std';
                   5928:         $cattype{'unauth'} = 'std';
                   5929:     }
                   5930:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
                   5931:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   5932:                   '<br />'.
                   5933:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   5934:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
                   5935:                   '</ul>');
                   5936:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
                   5937:         if ($currsettings->{'uniquecode'}) {
                   5938:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
                   5939:         } else {
                   5940:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   5941:                   '<br />'.
                   5942:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   5943:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
                   5944:                   '</ul><br />');
                   5945:         }
                   5946:     } else {
                   5947:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
                   5948:         if (ref($visactions) eq 'HASH') {
                   5949:             if ($visible) {
                   5950:                 $output .= '<p class="LC_info">'.$visactions->{'vis'}.'</p>';
                   5951:            } else {
                   5952:                 $output .= '<p class="LC_warning">'.$visactions->{'miss'}.'</p>'
                   5953:                           .$visactions->{'yous'}.
                   5954:                            '<p>'.$visactions->{'gen'}.'<br />'.$visactions->{'coca'};
                   5955:                 if (ref($vismsgs) eq 'ARRAY') {
                   5956:                     $output .= '<br />'.$visactions->{'make'}.'<ul>';
                   5957:                     foreach my $item (@{$vismsgs}) {
                   5958:                         $output .= '<li>'.$visactions->{$item}.'</li>';
                   5959:                     }
                   5960:                     $output .= '</ul>';
1.256     raeburn  5961:                 }
1.400     raeburn  5962:                 $output .= '</p>';
1.256     raeburn  5963:             }
                   5964:         }
                   5965:     }
1.398     raeburn  5966:     my $actionhref = '/adm/createuser';
                   5967:     if ($context eq 'domain') {
                   5968:         $actionhref = '/adm/modifycourse';
                   5969:     }
1.400     raeburn  5970: 
                   5971:     my %noedit;
                   5972:     unless ($context eq 'domain') {
                   5973:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
                   5974:     }
1.398     raeburn  5975:     $output .= '<form name="'.$formname.'" method="post" action="'.$actionhref.'">'."\n".
1.256     raeburn  5976:                &Apache::lonhtmlcommon::start_pick_box();
1.237     raeburn  5977:     if (ref($row) eq 'ARRAY') {
                   5978:         foreach my $item (@{$row}) {
                   5979:             my $title = $item; 
                   5980:             if (ref($lt) eq 'HASH') {
                   5981:                 $title = $lt->{$item};
                   5982:             }
1.297     bisitz   5983:             $output .= &Apache::lonhtmlcommon::row_title($title);
1.237     raeburn  5984:             if ($item eq 'types') {
1.398     raeburn  5985:                 my $curr_types;
                   5986:                 if (ref($currsettings) eq 'HASH') {
                   5987:                     $curr_types = $currsettings->{'selfenroll_types'};
                   5988:                 }
1.400     raeburn  5989:                 if ($noedit{$item}) {
                   5990:                     if ($curr_types eq '*') {
                   5991:                         $output .= &mt('Any user in any domain');   
                   5992:                     } else {
                   5993:                         my @entries = split(/;/,$curr_types);
                   5994:                         if (@entries > 0) {
                   5995:                             $output .= '<ul>'; 
                   5996:                             foreach my $entry (@entries) {
                   5997:                                 my ($currdom,$typestr) = split(/:/,$entry);
                   5998:                                 next if ($typestr eq '');
                   5999:                                 my $domdesc = &Apache::lonnet::domain($currdom);
                   6000:                                 my @currinsttypes = split(',',$typestr);
                   6001:                                 my ($othertitle,$usertypes,$types) = 
                   6002:                                     &Apache::loncommon::sorted_inst_types($currdom);
                   6003:                                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   6004:                                     $usertypes->{'any'} = &mt('any user'); 
                   6005:                                     if (keys(%{$usertypes}) > 0) {
                   6006:                                         $usertypes->{'other'} = &mt('other users');
                   6007:                                     }
                   6008:                                     my @longinsttypes = map { $usertypes->{$_}; } @currinsttypes;
                   6009:                                     $output .= '<li>'.$domdesc.':'.join(', ',@longinsttypes).'</li>';
                   6010:                                  }
                   6011:                             }
                   6012:                             $output .= '</ul>';
                   6013:                         } else {
                   6014:                             $output .= &mt('None');
                   6015:                         }
                   6016:                     }
                   6017:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6018:                     next;
                   6019:                 }
1.241     raeburn  6020:                 my $showdomdesc = 1;
                   6021:                 my $includeempty = 1;
                   6022:                 my $num = 0;
                   6023:                 $output .= &Apache::loncommon::start_data_table().
                   6024:                            &Apache::loncommon::start_data_table_row()
                   6025:                            .'<td colspan="2"><span class="LC_nobreak"><label>'
                   6026:                            .&mt('Any user in any domain:')
                   6027:                            .'&nbsp;<input type="radio" name="selfenroll_all" value="1" ';
                   6028:                 if ($curr_types eq '*') {
                   6029:                     $output .= ' checked="checked" '; 
                   6030:                 }
1.249     raeburn  6031:                 $output .= 'onchange="javascript:update_types('.
1.418   ! raeburn  6032:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('Yes').'</label>'.
1.249     raeburn  6033:                            '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
1.241     raeburn  6034:                 if ($curr_types ne '*') {
                   6035:                     $output .= ' checked="checked" ';
                   6036:                 }
1.249     raeburn  6037:                 $output .= ' onchange="javascript:update_types('.
1.418   ! raeburn  6038:                            "'selfenroll_all'".');"'.$disabled.' />'.&mt('No').'</label></td>'.
1.249     raeburn  6039:                            &Apache::loncommon::end_data_table_row().
                   6040:                            &Apache::loncommon::end_data_table().
                   6041:                            &mt('Or').'<br />'.
                   6042:                            &Apache::loncommon::start_data_table();
1.241     raeburn  6043:                 my %currdoms;
1.249     raeburn  6044:                 if ($curr_types eq '') {
1.241     raeburn  6045:                     $output .= &new_selfenroll_dom_row($cdom,'0');
                   6046:                 } elsif ($curr_types ne '*') {
                   6047:                     my @entries = split(/;/,$curr_types);
                   6048:                     if (@entries > 0) {
                   6049:                         foreach my $entry (@entries) {
                   6050:                             my ($currdom,$typestr) = split(/:/,$entry);
                   6051:                             $currdoms{$currdom} = 1;
                   6052:                             my $domdesc = &Apache::lonnet::domain($currdom);
1.249     raeburn  6053:                             my @currinsttypes = split(',',$typestr);
1.241     raeburn  6054:                             $output .= &Apache::loncommon::start_data_table_row()
                   6055:                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
                   6056:                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
                   6057:                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
                   6058:                                        .'" value="'.$currdom.'" /></span><br />'
                   6059:                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
1.418   ! raeburn  6060:                                        .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');"'.$disabled.' />'
1.241     raeburn  6061:                                        .&mt('Delete').'</label></span></td>';
1.249     raeburn  6062:                             $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
1.418   ! raeburn  6063:                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes,$readonly).'</td>'
1.241     raeburn  6064:                                        .&Apache::loncommon::end_data_table_row();
                   6065:                             $num ++;
                   6066:                         }
                   6067:                     }
                   6068:                 }
1.249     raeburn  6069:                 my $add_domtitle = &mt('Users in additional domain:');
1.241     raeburn  6070:                 if ($curr_types eq '*') { 
1.249     raeburn  6071:                     $add_domtitle = &mt('Users in specific domain:');
1.241     raeburn  6072:                 } elsif ($curr_types eq '') {
1.249     raeburn  6073:                     $add_domtitle = &mt('Users in other domain:');
1.241     raeburn  6074:                 }
                   6075:                 $output .= &Apache::loncommon::start_data_table_row()
                   6076:                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
                   6077:                            .&Apache::loncommon::select_dom_form('','selfenroll_newdom',
1.418   ! raeburn  6078:                                                                 $includeempty,$showdomdesc,'','','',$readonly)
1.241     raeburn  6079:                            .'<input type="hidden" name="selfenroll_types_total" value="'.$num.'" />'
                   6080:                            .'</td>'.&Apache::loncommon::end_data_table_row()
                   6081:                            .&Apache::loncommon::end_data_table();
1.237     raeburn  6082:             } elsif ($item eq 'registered') {
                   6083:                 my ($regon,$regoff);
1.398     raeburn  6084:                 my $registered;
                   6085:                 if (ref($currsettings) eq 'HASH') {
                   6086:                     $registered = $currsettings->{'selfenroll_registered'};
                   6087:                 }
1.400     raeburn  6088:                 if ($noedit{$item}) {
                   6089:                     if ($registered) {
                   6090:                         $output .= &mt('Must be registered in course');
                   6091:                     } else {
                   6092:                         $output .= &mt('No requirement');
                   6093:                     }
                   6094:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6095:                     next;
                   6096:                 }
1.398     raeburn  6097:                 if ($registered) {
1.237     raeburn  6098:                     $regon = ' checked="checked" ';
                   6099:                     $regoff = ' ';
                   6100:                 } else {
                   6101:                     $regon = ' ';
                   6102:                     $regoff = ' checked="checked" ';
                   6103:                 }
                   6104:                 $output .= '<label>'.
1.418   ! raeburn  6105:                            '<input type="radio" name="selfenroll_registered" value="1"'.$regon.$disabled.'/>'.
1.244     bisitz   6106:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
1.418   ! raeburn  6107:                            '<input type="radio" name="selfenroll_registered" value="0"'.$regoff.$disabled.'/>'.
1.244     bisitz   6108:                            &mt('No').'</label>';
1.237     raeburn  6109:             } elsif ($item eq 'enroll_dates') {
1.398     raeburn  6110:                 my ($starttime,$endtime);
                   6111:                 if (ref($currsettings) eq 'HASH') {
                   6112:                     $starttime = $currsettings->{'selfenroll_start_date'};
                   6113:                     $endtime = $currsettings->{'selfenroll_end_date'};
                   6114:                     if ($starttime eq '') {
                   6115:                         $starttime = $currsettings->{'default_enrollment_start_date'};
                   6116:                     }
                   6117:                     if ($endtime eq '') {
                   6118:                         $endtime = $currsettings->{'default_enrollment_end_date'};
                   6119:                     }
1.237     raeburn  6120:                 }
1.400     raeburn  6121:                 if ($noedit{$item}) {
                   6122:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
                   6123:                                                           &Apache::lonlocal::locallocaltime($endtime));
                   6124:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6125:                     next;
                   6126:                 }
1.237     raeburn  6127:                 my $startform =
                   6128:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
1.418   ! raeburn  6129:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6130:                 my $endform =
                   6131:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
1.418   ! raeburn  6132:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6133:                 $output .= &selfenroll_date_forms($startform,$endform);
                   6134:             } elsif ($item eq 'access_dates') {
1.398     raeburn  6135:                 my ($starttime,$endtime);
                   6136:                 if (ref($currsettings) eq 'HASH') {
                   6137:                     $starttime = $currsettings->{'selfenroll_start_access'};
                   6138:                     $endtime = $currsettings->{'selfenroll_end_access'};
                   6139:                     if ($starttime eq '') {
                   6140:                         $starttime = $currsettings->{'default_enrollment_start_date'};
                   6141:                     }
                   6142:                     if ($endtime eq '') {
                   6143:                         $endtime = $currsettings->{'default_enrollment_end_date'};
                   6144:                     }
1.237     raeburn  6145:                 }
1.400     raeburn  6146:                 if ($noedit{$item}) {
                   6147:                     $output .= &mt('From: [_1], to: [_2]',&Apache::lonlocal::locallocaltime($starttime),
                   6148:                                                           &Apache::lonlocal::locallocaltime($endtime));
                   6149:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6150:                     next;
                   6151:                 }
1.237     raeburn  6152:                 my $startform =
                   6153:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
1.418   ! raeburn  6154:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6155:                 my $endform =
                   6156:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
1.418   ! raeburn  6157:                                       $disabled,undef,undef,undef,undef,undef,undef,$nolink);
1.237     raeburn  6158:                 $output .= &selfenroll_date_forms($startform,$endform);
                   6159:             } elsif ($item eq 'section') {
1.398     raeburn  6160:                 my $currsec;
                   6161:                 if (ref($currsettings) eq 'HASH') {
                   6162:                     $currsec = $currsettings->{'selfenroll_section'};
                   6163:                 }
1.237     raeburn  6164:                 my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   6165:                 my $newsecval;
                   6166:                 if ($currsec ne 'none' && $currsec ne '') {
                   6167:                     if (!defined($sections_count{$currsec})) {
                   6168:                         $newsecval = $currsec;
                   6169:                     }
                   6170:                 }
1.400     raeburn  6171:                 if ($noedit{$item}) {
                   6172:                     if ($currsec ne '') {
                   6173:                         $output .= $currsec;
                   6174:                     } else {
                   6175:                         $output .= &mt('No specific section');
                   6176:                     }
                   6177:                     $output .= '<br />'.&mt('(Set by Domain Coordinator)');
                   6178:                     next;
                   6179:                 }
1.237     raeburn  6180:                 my $sections_select = 
1.418   ! raeburn  6181:                     &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec,$disabled);
1.237     raeburn  6182:                 $output .= '<table class="LC_createuser">'."\n".
                   6183:                            '<tr class="LC_section_row">'."\n".
                   6184:                            '<td align="center">'.&mt('Existing sections')."\n".
                   6185:                            '<br />'.$sections_select.'</td><td align="center">'.
                   6186:                            &mt('New section').'<br />'."\n".
1.418   ! raeburn  6187:                            '<input type="text" name="newsec" size="15" value="'.$newsecval.'"'.$disabled.' />'."\n".
1.237     raeburn  6188:                            '<input type="hidden" name="sections" value="" />'."\n".
                   6189:                            '</td></tr></table>'."\n";
1.276     raeburn  6190:             } elsif ($item eq 'approval') {
1.398     raeburn  6191:                 my ($currnotified,$currapproval,%appchecked);
                   6192:                 my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
                   6193:                 if (ref($currsettings) eq 'HASH') { 
                   6194:                     $currnotified = $currsettings->{'selfenroll_notifylist'};
                   6195:                     $currapproval = $currsettings->{'selfenroll_approval'};
                   6196:                 }
                   6197:                 if ($currapproval !~ /^[012]$/) {
                   6198:                     $currapproval = 0;
                   6199:                 }
1.400     raeburn  6200:                 if ($noedit{$item}) {
                   6201:                     $output .=  $selfdescs{'approval'}{$currapproval}.
                   6202:                                 '<br />'.&mt('(Set by Domain Coordinator)');
                   6203:                     next;
                   6204:                 }
1.398     raeburn  6205:                 $appchecked{$currapproval} = ' checked="checked"';
                   6206:                 for my $i (0..2) {
                   6207:                     $output .= '<label>'.
                   6208:                                '<input type="radio" name="selfenroll_approval" value="'.$i.'"'.
1.418   ! raeburn  6209:                                $appchecked{$i}.' onclick="toggleNotify();"'.$disabled.' />'.
        !          6210:                                $selfdescs{'approval'}{$i}.'</label>'.('&nbsp;'x2);
1.276     raeburn  6211:                 }
                   6212:                 my %advhash = &Apache::lonnet::get_course_adv_roles($cid,1);
                   6213:                 my (@ccs,%notified);
1.322     raeburn  6214:                 my $ccrole = 'cc';
                   6215:                 if ($crstype eq 'Community') {
                   6216:                     $ccrole = 'co';
                   6217:                 }
                   6218:                 if ($advhash{$ccrole}) {
                   6219:                     @ccs = split(/,/,$advhash{$ccrole});
1.276     raeburn  6220:                 }
                   6221:                 if ($currnotified) {
                   6222:                     foreach my $current (split(/,/,$currnotified)) {
                   6223:                         $notified{$current} = 1;
                   6224:                         if (!grep(/^\Q$current\E$/,@ccs)) {
                   6225:                             push(@ccs,$current);
                   6226:                         }
                   6227:                     }
                   6228:                 }
                   6229:                 if (@ccs) {
1.398     raeburn  6230:                     my $style;
                   6231:                     unless ($currapproval) {
                   6232:                         $style = ' style="display: none;"'; 
                   6233:                     }
                   6234:                     $output .= '<br /><div id="notified"'.$style.'>'.
                   6235:                                &mt('Personnel to be notified when an enrollment request needs approval, or has been approved:').'&nbsp;'.
                   6236:                                &Apache::loncommon::start_data_table().
1.276     raeburn  6237:                                &Apache::loncommon::start_data_table_row();
                   6238:                     my $count = 0;
                   6239:                     my $numcols = 4;
                   6240:                     foreach my $cc (sort(@ccs)) {
                   6241:                         my $notifyon;
                   6242:                         my ($ccuname,$ccudom) = split(/:/,$cc);
                   6243:                         if ($notified{$cc}) {
                   6244:                             $notifyon = ' checked="checked" ';
                   6245:                         }
                   6246:                         if ($count && !$count%$numcols) {
                   6247:                             $output .= &Apache::loncommon::end_data_table_row().
                   6248:                                        &Apache::loncommon::start_data_table_row()
                   6249:                         }
                   6250:                         $output .= '<td><span class="LC_nobreak"><label>'.
1.418   ! raeburn  6251:                                    '<input type="checkbox" name="selfenroll_notify"'.$notifyon.' value="'.$cc.'"'.$disabled.' />'.
1.276     raeburn  6252:                                    &Apache::loncommon::plainname($ccuname,$ccudom).
                   6253:                                    '</label></span></td>';
1.343     raeburn  6254:                         $count ++;
1.276     raeburn  6255:                     }
                   6256:                     my $rem = $count%$numcols;
                   6257:                     if ($rem) {
                   6258:                         my $emptycols = $numcols - $rem;
                   6259:                         for (my $i=0; $i<$emptycols; $i++) { 
                   6260:                             $output .= '<td>&nbsp;</td>';
                   6261:                         }
                   6262:                     }
                   6263:                     $output .= &Apache::loncommon::end_data_table_row().
1.398     raeburn  6264:                                &Apache::loncommon::end_data_table().
                   6265:                                '</div>';
1.276     raeburn  6266:                 }
                   6267:             } elsif ($item eq 'limit') {
1.398     raeburn  6268:                 my ($crslimit,$selflimit,$nolimit,$currlim,$currcap);
                   6269:                 if (ref($currsettings) eq 'HASH') {
                   6270:                     $currlim = $currsettings->{'selfenroll_limit'};
                   6271:                     $currcap = $currsettings->{'selfenroll_cap'};
                   6272:                 }
1.400     raeburn  6273:                 if ($noedit{$item}) {
                   6274:                     if (($currlim eq 'allstudents') || ($currlim eq 'selfenrolled')) {
                   6275:                         if ($currlim eq 'allstudents') {
                   6276:                             $output .= &mt('Limit by total students');
                   6277:                         } elsif ($currlim eq 'selfenrolled') {
                   6278:                             $output .= &mt('Limit by total self-enrolled students');
                   6279:                         }
                   6280:                         $output .= ' '.&mt('Maximum: [_1]',$currcap).
                   6281:                                    '<br />'.&mt('(Set by Domain Coordinator)');
                   6282:                     } else {
                   6283:                         $output .= &mt('No limit').'<br />'.&mt('(Set by Domain Coordinator)');
                   6284:                     }
                   6285:                     next;
                   6286:                 }
1.276     raeburn  6287:                 if ($currlim eq 'allstudents') {
                   6288:                     $crslimit = ' checked="checked" ';
                   6289:                     $selflimit = ' ';
                   6290:                     $nolimit = ' ';
                   6291:                 } elsif ($currlim eq 'selfenrolled') {
                   6292:                     $crslimit = ' ';
                   6293:                     $selflimit = ' checked="checked" ';
                   6294:                     $nolimit = ' '; 
                   6295:                 } else {
                   6296:                     $crslimit = ' ';
                   6297:                     $selflimit = ' ';
1.398     raeburn  6298:                     $nolimit = ' checked="checked" ';
1.276     raeburn  6299:                 }
                   6300:                 $output .= '<table><tr><td><label>'.
1.418   ! raeburn  6301:                            '<input type="radio" name="selfenroll_limit" value="none"'.$nolimit.$disabled.'/>'.
1.276     raeburn  6302:                            &mt('No limit').'</label></td><td><label>'.
1.418   ! raeburn  6303:                            '<input type="radio" name="selfenroll_limit" value="allstudents"'.$crslimit.$disabled.'/>'.
1.276     raeburn  6304:                            &mt('Limit by total students').'</label></td><td><label>'.
1.418   ! raeburn  6305:                            '<input type="radio" name="selfenroll_limit" value="selfenrolled"'.$selflimit.$disabled.'/>'.
1.276     raeburn  6306:                            &mt('Limit by total self-enrolled students').
                   6307:                            '</td></tr><tr>'.
                   6308:                            '<td>&nbsp;</td><td colspan="2"><span class="LC_nobreak">'.
                   6309:                            ('&nbsp;'x3).&mt('Maximum number allowed: ').
1.418   ! raeburn  6310:                            '<input type="text" name="selfenroll_cap" size = "5" value="'.$currcap.'"'.$disabled.' /></td></tr></table>';
1.237     raeburn  6311:             }
                   6312:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   6313:         }
                   6314:     }
1.418   ! raeburn  6315:     $output .= &Apache::lonhtmlcommon::end_pick_box().'<br />';
        !          6316:     unless ($readonly) {
        !          6317:         $output .= '<input type="button" name="selfenrollconf" value="'
        !          6318:                    .&mt('Save').'" onclick="validate_types(this.form);" />';
        !          6319:     }
        !          6320:     $output .= '<input type="hidden" name="action" value="selfenroll" />'
        !          6321:               .'<input type="hidden" name="state" value="done" />'."\n"
        !          6322:               .$additional.'</form>';
1.237     raeburn  6323:     $r->print($output);
                   6324:     return;
                   6325: }
                   6326: 
1.400     raeburn  6327: sub get_noedit_fields {
                   6328:     my ($cdom,$cnum,$crstype,$row) = @_;
                   6329:     my %noedit;
                   6330:     if (ref($row) eq 'ARRAY') {
                   6331:         my %settings = &Apache::lonnet::get('environment',['internal.coursecode','internal.textbook',
                   6332:                                                            'internal.selfenrollmgrdc',
                   6333:                                                            'internal.selfenrollmgrcc'],$cdom,$cnum);
                   6334:         my $type = &Apache::lonuserutils::get_extended_type($cdom,$cnum,$crstype,\%settings);
                   6335:         my (%specific_managebydc,%specific_managebycc,%default_managebydc);
                   6336:         map { $specific_managebydc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrdc'}));
                   6337:         map { $specific_managebycc{$_} = 1; } (split(/,/,$settings{'internal.selfenrollmgrcc'}));
                   6338:         my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
                   6339:         map { $default_managebydc{$_} = 1; } (split(/,/,$domdefaults{$type.'selfenrolladmdc'}));
                   6340: 
                   6341:         foreach my $item (@{$row}) {
                   6342:             next if ($specific_managebycc{$item});
                   6343:             if (($specific_managebydc{$item}) || ($default_managebydc{$item})) {
                   6344:                 $noedit{$item} = 1;
                   6345:             }
                   6346:         }
                   6347:     }
                   6348:     return %noedit;
                   6349: } 
                   6350: 
                   6351: sub visible_in_stdcat {
                   6352:     my ($cdom,$cnum,$domconf) = @_;
                   6353:     my ($cathash,%settable,@vismsgs,$cansetvis,$visible);
                   6354:     unless (ref($domconf) eq 'HASH') {
                   6355:         return ($visible,$cansetvis,\@vismsgs);
                   6356:     }
                   6357:     if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6358:         if ($domconf->{'coursecategories'}{'togglecats'} eq 'crs') {
1.256     raeburn  6359:             $settable{'togglecats'} = 1;
                   6360:         }
1.400     raeburn  6361:         if ($domconf->{'coursecategories'}{'categorize'} eq 'crs') {
1.256     raeburn  6362:             $settable{'categorize'} = 1;
                   6363:         }
1.400     raeburn  6364:         $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6365:     }
1.260     raeburn  6366:     if ($settable{'togglecats'} && $settable{'categorize'}) {
1.256     raeburn  6367:         $cansetvis = &mt('You are able to both assign a course category and choose to exclude this course from the catalog.');   
                   6368:     } elsif ($settable{'togglecats'}) {
                   6369:         $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  6370:     } elsif ($settable{'categorize'}) {
1.256     raeburn  6371:         $cansetvis = &mt('You may assign a course category, but only a Domain Coordinator may choose to exclude this course from the catalog.');  
                   6372:     } else {
                   6373:         $cansetvis = &mt('Only a Domain Coordinator may assign a course category or choose to exclude this course from the catalog.'); 
                   6374:     }
                   6375:      
                   6376:     my %currsettings =
                   6377:         &Apache::lonnet::get('environment',['hidefromcat','categories','internal.coursecode'],
                   6378:                              $cdom,$cnum);
1.400     raeburn  6379:     $visible = 0;
1.256     raeburn  6380:     if ($currsettings{'internal.coursecode'} ne '') {
1.400     raeburn  6381:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6382:             $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6383:             if (ref($cathash) eq 'HASH') {
                   6384:                 if ($cathash->{'instcode::0'} eq '') {
                   6385:                     push(@vismsgs,'dc_addinst'); 
                   6386:                 } else {
                   6387:                     $visible = 1;
                   6388:                 }
                   6389:             } else {
                   6390:                 $visible = 1;
                   6391:             }
                   6392:         } else {
                   6393:             $visible = 1;
                   6394:         }
                   6395:     } else {
                   6396:         if (ref($cathash) eq 'HASH') {
                   6397:             if ($cathash->{'instcode::0'} ne '') {
                   6398:                 push(@vismsgs,'dc_instcode');
                   6399:             }
                   6400:         } else {
                   6401:             push(@vismsgs,'dc_instcode');
                   6402:         }
                   6403:     }
                   6404:     if ($currsettings{'categories'} ne '') {
                   6405:         my $cathash;
1.400     raeburn  6406:         if (ref($domconf->{'coursecategories'}) eq 'HASH') {
                   6407:             $cathash = $domconf->{'coursecategories'}{'cats'};
1.256     raeburn  6408:             if (ref($cathash) eq 'HASH') {
                   6409:                 if (keys(%{$cathash}) == 0) {
                   6410:                     push(@vismsgs,'dc_catalog');
                   6411:                 } elsif ((keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} ne '')) {
                   6412:                     push(@vismsgs,'dc_categories');
                   6413:                 } else {
                   6414:                     my @currcategories = split('&',$currsettings{'categories'});
                   6415:                     my $matched = 0;
                   6416:                     foreach my $cat (@currcategories) {
                   6417:                         if ($cathash->{$cat} ne '') {
                   6418:                             $visible = 1;
                   6419:                             $matched = 1;
                   6420:                             last;
                   6421:                         }
                   6422:                     }
                   6423:                     if (!$matched) {
1.260     raeburn  6424:                         if ($settable{'categorize'}) { 
1.256     raeburn  6425:                             push(@vismsgs,'chgcat');
                   6426:                         } else {
                   6427:                             push(@vismsgs,'dc_chgcat');
                   6428:                         }
                   6429:                     }
                   6430:                 }
                   6431:             }
                   6432:         }
                   6433:     } else {
                   6434:         if (ref($cathash) eq 'HASH') {
                   6435:             if ((keys(%{$cathash}) > 1) || 
                   6436:                 (keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} eq '')) {
1.260     raeburn  6437:                 if ($settable{'categorize'}) {
1.256     raeburn  6438:                     push(@vismsgs,'addcat');
                   6439:                 } else {
                   6440:                     push(@vismsgs,'dc_addcat');
                   6441:                 }
                   6442:             }
                   6443:         }
                   6444:     }
                   6445:     if ($currsettings{'hidefromcat'} eq 'yes') {
                   6446:         $visible = 0;
                   6447:         if ($settable{'togglecats'}) {
                   6448:             unshift(@vismsgs,'unhide');
                   6449:         } else {
                   6450:             unshift(@vismsgs,'dc_unhide')
                   6451:         }
                   6452:     }
1.400     raeburn  6453:     return ($visible,$cansetvis,\@vismsgs);
                   6454: }
                   6455: 
                   6456: sub cat_visibility {
                   6457:     my %visactions = &Apache::lonlocal::texthash(
                   6458:                    vis => 'This course/community currently appears in the Course/Community Catalog for this domain.',
                   6459:                    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.',
                   6460:                    miss => 'This course/community does not currently appear in the Course/Community Catalog for this domain.',
                   6461:                    none => 'Display of a course catalog is disabled for this domain.',
                   6462:                    yous => 'You should remedy this if you plan to allow self-enrollment, otherwise students will have difficulty finding this course.',
                   6463:                    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.',
                   6464:                    make => 'Make any changes to self-enrollment settings below, click "Save", then take action to include the course in the Catalog:',
                   6465:                    take => 'Take the following action to ensure the course appears in the Catalog:',
                   6466:                    dc_chgconf => 'Ask a domain coordinator to change the Catalog type for this domain.',
                   6467:                    dc_setcode => 'Ask a domain coordinator to assign a six character code to the course',
                   6468:                    dc_unhide  => 'Ask a domain coordinator to change the "Exclude from course catalog" setting.',
                   6469:                    dc_addinst => 'Ask a domain coordinator to enable display the catalog of "Official courses (with institutional codes)".',
                   6470:                    dc_instcode => 'Ask a domain coordinator to assign an institutional code (if this is an official course).',
                   6471:                    dc_catalog  => 'Ask a domain coordinator to enable or create at least one course category in the domain.',
                   6472:                    dc_categories => 'Ask a domain coordinator to create a hierarchy of categories and sub categories for courses in the domain.',
                   6473:                    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',
                   6474:                    dc_addcat => 'Ask a domain coordinator to assign a category to the course.',
                   6475:     );
                   6476:     $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>"');
                   6477:     $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>"');
                   6478:     $visactions{'addcat'} = &mt('Use [_1]Categorize course[_2] to assign a category to the course.','"<a href="/adm/courseprefs?phase=display&actions=courseinfo">','</a>"');
                   6479:     return \%visactions;
1.256     raeburn  6480: }
                   6481: 
1.241     raeburn  6482: sub new_selfenroll_dom_row {
                   6483:     my ($newdom,$num) = @_;
                   6484:     my $domdesc = &Apache::lonnet::domain($newdom);
                   6485:     my $output;
                   6486:     if ($domdesc ne '') {
                   6487:         $output .= &Apache::loncommon::start_data_table_row()
                   6488:                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
                   6489:                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
1.249     raeburn  6490:                    .'" value="'.$newdom.'" /></span><br />'
                   6491:                    .'<span class="LC_nobreak"><label><input type="checkbox" '
                   6492:                    .'name="selfenroll_activate" value="'.$num.'" '
                   6493:                    .'onchange="javascript:update_types('
                   6494:                    ."'selfenroll_activate','$num'".');" />'
                   6495:                    .&mt('Activate').'</label></span></td>';
1.241     raeburn  6496:         my @currinsttypes;
                   6497:         $output .= '<td>'.&mt('User types:').'<br />'
                   6498:                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
                   6499:                    .&Apache::loncommon::end_data_table_row();
                   6500:     }
                   6501:     return $output;
                   6502: }
                   6503: 
                   6504: sub selfenroll_inst_types {
1.418   ! raeburn  6505:     my ($num,$currdom,$currinsttypes,$readonly) = @_;
1.241     raeburn  6506:     my $output;
                   6507:     my $numinrow = 4;
                   6508:     my $count = 0;
                   6509:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($currdom);
1.247     raeburn  6510:     my $othervalue = 'any';
1.418   ! raeburn  6511:     my $disabled;
        !          6512:     if ($readonly) {
        !          6513:         $disabled = ' disabled="disabled"';
        !          6514:     }
1.241     raeburn  6515:     if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
1.251     raeburn  6516:         if (keys(%{$usertypes}) > 0) {
1.247     raeburn  6517:             $othervalue = 'other';
                   6518:         }
1.241     raeburn  6519:         $output .= '<table><tr>';
                   6520:         foreach my $type (@{$types}) {
                   6521:             if (($count > 0) && ($count%$numinrow == 0)) {
                   6522:                 $output .= '</tr><tr>';
                   6523:             }
                   6524:             if (defined($usertypes->{$type})) {
1.257     raeburn  6525:                 my $esc_type = &escape($type);
1.241     raeburn  6526:                 $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.
1.257     raeburn  6527:                            $esc_type.'" ';
1.241     raeburn  6528:                 if (ref($currinsttypes) eq 'ARRAY') {
                   6529:                     if (@{$currinsttypes} > 0) {
1.249     raeburn  6530:                         if (grep(/^any$/,@{$currinsttypes})) {
                   6531:                             $output .= 'checked="checked"';
1.257     raeburn  6532:                         } elsif (grep(/^\Q$esc_type\E$/,@{$currinsttypes})) {
1.241     raeburn  6533:                             $output .= 'checked="checked"';
                   6534:                         }
1.249     raeburn  6535:                     } else {
                   6536:                         $output .= 'checked="checked"';
1.241     raeburn  6537:                     }
                   6538:                 }
1.418   ! raeburn  6539:                 $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$usertypes->{$type}.'</label></span></td>';
1.241     raeburn  6540:             }
                   6541:             $count ++;
                   6542:         }
                   6543:         if (($count > 0) && ($count%$numinrow == 0)) {
                   6544:             $output .= '</tr><tr>';
                   6545:         }
1.249     raeburn  6546:         $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
1.241     raeburn  6547:         if (ref($currinsttypes) eq 'ARRAY') {
                   6548:             if (@{$currinsttypes} > 0) {
1.249     raeburn  6549:                 if (grep(/^any$/,@{$currinsttypes})) { 
                   6550:                     $output .= ' checked="checked"';
                   6551:                 } elsif ($othervalue eq 'other') {
                   6552:                     if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
                   6553:                         $output .= ' checked="checked"';
                   6554:                     }
1.241     raeburn  6555:                 }
1.249     raeburn  6556:             } else {
                   6557:                 $output .= ' checked="checked"';
1.241     raeburn  6558:             }
1.249     raeburn  6559:         } else {
                   6560:             $output .= ' checked="checked"';
1.241     raeburn  6561:         }
1.418   ! raeburn  6562:         $output .= ' name="selfenroll_types_'.$num.'"'.$disabled.' />'.$othertitle.'</label></span></td></tr></table>';
1.241     raeburn  6563:     }
                   6564:     return $output;
                   6565: }
                   6566: 
1.237     raeburn  6567: sub selfenroll_date_forms {
                   6568:     my ($startform,$endform) = @_;
                   6569:     my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
1.244     bisitz   6570:                   &Apache::lonhtmlcommon::row_title(&mt('Start date'),
1.237     raeburn  6571:                                                     'LC_oddrow_value')."\n".
                   6572:                   $startform."\n".
                   6573:                   &Apache::lonhtmlcommon::row_closure(1).
1.244     bisitz   6574:                   &Apache::lonhtmlcommon::row_title(&mt('End date'),
1.237     raeburn  6575:                                                    'LC_oddrow_value')."\n".
                   6576:                   $endform."\n".
                   6577:                   &Apache::lonhtmlcommon::row_closure(1).
                   6578:                   &Apache::lonhtmlcommon::end_pick_box();
                   6579:     return $output;
                   6580: }
                   6581: 
1.239     raeburn  6582: sub print_userchangelogs_display {
1.415     raeburn  6583:     my ($r,$context,$permission,$brcrum) = @_;
1.363     raeburn  6584:     my $formname = 'rolelog';
1.418   ! raeburn  6585:     my ($username,$domain,$crstype,$viewablesec,%roleslog);
1.363     raeburn  6586:     if ($context eq 'domain') {
                   6587:         $domain = $env{'request.role.domain'};
                   6588:         %roleslog=&Apache::lonnet::dump_dom('nohist_rolelog',$domain);
                   6589:     } else {
                   6590:         if ($context eq 'course') { 
                   6591:             $domain = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   6592:             $username = $env{'course.'.$env{'request.course.id'}.'.num'};
                   6593:             $crstype = &Apache::loncommon::course_type();
1.418   ! raeburn  6594:             $viewablesec = &Apache::lonuserutils::viewable_section($permission);
1.363     raeburn  6595:             my %saveable_parameters = ('show' => 'scalar',);
                   6596:             &Apache::loncommon::store_course_settings('roles_log',
                   6597:                                                       \%saveable_parameters);
                   6598:             &Apache::loncommon::restore_course_settings('roles_log',
                   6599:                                                         \%saveable_parameters);
                   6600:         } elsif ($context eq 'author') {
                   6601:             $domain = $env{'user.domain'}; 
                   6602:             if ($env{'request.role'} =~ m{^au\./\Q$domain\E/$}) {
                   6603:                 $username = $env{'user.name'};
                   6604:             } else {
                   6605:                 undef($domain);
                   6606:             }
                   6607:         }
                   6608:         if ($domain ne '' && $username ne '') { 
                   6609:             %roleslog=&Apache::lonnet::dump('nohist_rolelog',$domain,$username);
                   6610:         }
                   6611:     }
1.239     raeburn  6612:     if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
                   6613: 
1.415     raeburn  6614:     my $helpitem;
                   6615:     if ($context eq 'course') {
                   6616:         $helpitem = 'Course_User_Logs';
                   6617:     }
                   6618:     push (@{$brcrum},
                   6619:              {href => '/adm/createuser?action=changelogs',
                   6620:               text => 'User Management Logs',
                   6621:               help => $helpitem});
                   6622:     my $bread_crumbs_component = 'User Changes';
                   6623:     my $args = { bread_crumbs           => $brcrum,
                   6624:                  bread_crumbs_component => $bread_crumbs_component};
                   6625: 
                   6626:     # Create navigation javascript
                   6627:     my $jsnav = &userlogdisplay_js($formname);
                   6628: 
                   6629:     my $jscript = (<<ENDSCRIPT);
                   6630: <script type="text/javascript">
                   6631: // <![CDATA[
                   6632: $jsnav
                   6633: // ]]>
                   6634: </script>
                   6635: ENDSCRIPT
                   6636: 
                   6637:     # print page header
                   6638:     $r->print(&header($jscript,$args));
                   6639: 
1.239     raeburn  6640:     # set defaults
                   6641:     my $now = time();
                   6642:     my $defstart = $now - (7*24*3600); #7 days ago 
                   6643:     my %defaults = (
                   6644:                      page               => '1',
                   6645:                      show               => '10',
                   6646:                      role               => 'any',
                   6647:                      chgcontext         => 'any',
                   6648:                      rolelog_start_date => $defstart,
                   6649:                      rolelog_end_date   => $now,
                   6650:                    );
                   6651:     my $more_records = 0;
                   6652: 
                   6653:     # set current
                   6654:     my %curr;
                   6655:     foreach my $item ('show','page','role','chgcontext') {
                   6656:         $curr{$item} = $env{'form.'.$item};
                   6657:     }
                   6658:     my ($startdate,$enddate) = 
                   6659:         &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
                   6660:     $curr{'rolelog_start_date'} = $startdate;
                   6661:     $curr{'rolelog_end_date'} = $enddate;
                   6662:     foreach my $key (keys(%defaults)) {
                   6663:         if ($curr{$key} eq '') {
                   6664:             $curr{$key} = $defaults{$key};
                   6665:         }
                   6666:     }
1.248     raeburn  6667:     my (%whodunit,%changed,$version);
                   6668:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
1.239     raeburn  6669:     my ($minshown,$maxshown);
1.255     raeburn  6670:     $minshown = 1;
1.239     raeburn  6671:     my $count = 0;
1.415     raeburn  6672:     if ($curr{'show'} =~ /\D/) {
                   6673:         $curr{'page'} = 1;
                   6674:     } else {
1.239     raeburn  6675:         $maxshown = $curr{'page'} * $curr{'show'};
                   6676:         if ($curr{'page'} > 1) {
                   6677:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   6678:         }
                   6679:     }
1.301     bisitz   6680: 
1.327     raeburn  6681:     # Form Header
                   6682:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
1.363     raeburn  6683:               &role_display_filter($context,$formname,$domain,$username,\%curr,
                   6684:                                    $version,$crstype));
1.327     raeburn  6685: 
                   6686:     my $showntableheader = 0;
                   6687: 
                   6688:     # Table Header
                   6689:     my $tableheader = 
                   6690:         &Apache::loncommon::start_data_table_header_row()
                   6691:        .'<th>&nbsp;</th>'
                   6692:        .'<th>'.&mt('When').'</th>'
                   6693:        .'<th>'.&mt('Who made the change').'</th>'
                   6694:        .'<th>'.&mt('Changed User').'</th>'
1.363     raeburn  6695:        .'<th>'.&mt('Role').'</th>';
                   6696: 
                   6697:     if ($context eq 'course') {
                   6698:         $tableheader .= '<th>'.&mt('Section').'</th>';
                   6699:     }
                   6700:     $tableheader .=
                   6701:         '<th>'.&mt('Context').'</th>'
1.327     raeburn  6702:        .'<th>'.&mt('Start').'</th>'
                   6703:        .'<th>'.&mt('End').'</th>'
                   6704:        .&Apache::loncommon::end_data_table_header_row();
                   6705: 
                   6706:     # Display user change log data
1.239     raeburn  6707:     foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
                   6708:         next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
                   6709:                  ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
1.415     raeburn  6710:         if ($curr{'show'} !~ /\D/) {
1.239     raeburn  6711:             if ($count >= $curr{'page'} * $curr{'show'}) {
                   6712:                 $more_records = 1;
                   6713:                 last;
                   6714:             }
                   6715:         }
                   6716:         if ($curr{'role'} ne 'any') {
                   6717:             next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
                   6718:         }
                   6719:         if ($curr{'chgcontext'} ne 'any') {
                   6720:             if ($curr{'chgcontext'} eq 'selfenroll') {
                   6721:                 next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
                   6722:             } else {
                   6723:                 next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
                   6724:             }
                   6725:         }
1.418   ! raeburn  6726:         if (($context eq 'course') && ($viewablesec ne '')) {
        !          6727:             next if ($roleslog{$id}{'logentry'}{'section'} ne $viewablesec); 
        !          6728:         }
1.239     raeburn  6729:         $count ++;
                   6730:         next if ($count < $minshown);
1.327     raeburn  6731:         unless ($showntableheader) {
1.415     raeburn  6732:             $r->print(&Apache::loncommon::start_data_table()
1.327     raeburn  6733:                      .$tableheader);
                   6734:             $r->rflush();
                   6735:             $showntableheader = 1;
                   6736:         }
1.239     raeburn  6737:         if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
                   6738:             $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
                   6739:                 &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
                   6740:         }
                   6741:         if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
                   6742:             $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
                   6743:                 &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
                   6744:         }
                   6745:         my $sec = $roleslog{$id}{'logentry'}{'section'};
                   6746:         if ($sec eq '') {
                   6747:             $sec = &mt('None');
                   6748:         }
                   6749:         my ($rolestart,$roleend);
                   6750:         if ($roleslog{$id}{'delflag'}) {
                   6751:             $rolestart = &mt('deleted');
                   6752:             $roleend = &mt('deleted');
                   6753:         } else {
                   6754:             $rolestart = $roleslog{$id}{'logentry'}{'start'};
                   6755:             $roleend = $roleslog{$id}{'logentry'}{'end'};
                   6756:             if ($rolestart eq '' || $rolestart == 0) {
                   6757:                 $rolestart = &mt('No start date'); 
                   6758:             } else {
                   6759:                 $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
                   6760:             }
                   6761:             if ($roleend eq '' || $roleend == 0) { 
                   6762:                 $roleend = &mt('No end date');
                   6763:             } else {
                   6764:                 $roleend = &Apache::lonlocal::locallocaltime($roleend);
                   6765:             }
                   6766:         }
                   6767:         my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
                   6768:         if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
                   6769:             $chgcontext = 'selfenroll';
                   6770:         }
1.363     raeburn  6771:         my %lt = &rolechg_contexts($context,$crstype);
1.239     raeburn  6772:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
                   6773:             $chgcontext = $lt{$chgcontext};
                   6774:         }
1.327     raeburn  6775:         $r->print(
1.301     bisitz   6776:             &Apache::loncommon::start_data_table_row()
                   6777:            .'<td>'.$count.'</td>'
                   6778:            .'<td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td>'
                   6779:            .'<td>'.$whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.'</td>'
                   6780:            .'<td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td>'
1.363     raeburn  6781:            .'<td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'},$crstype).'</td>');
                   6782:         if ($context eq 'course') { 
                   6783:             $r->print('<td>'.$sec.'</td>');
                   6784:         }
                   6785:         $r->print(
                   6786:             '<td>'.$chgcontext.'</td>'
1.301     bisitz   6787:            .'<td>'.$rolestart.'</td>'
                   6788:            .'<td>'.$roleend.'</td>'
1.327     raeburn  6789:            .&Apache::loncommon::end_data_table_row()."\n");
1.301     bisitz   6790:     }
                   6791: 
1.327     raeburn  6792:     if ($showntableheader) { # Table footer, if content displayed above
1.415     raeburn  6793:         $r->print(&Apache::loncommon::end_data_table().
                   6794:                   &userlogdisplay_navlinks(\%curr,$more_records));
1.327     raeburn  6795:     } else { # No content displayed above
1.301     bisitz   6796:         $r->print('<p class="LC_info">'
                   6797:                  .&mt('There are no records to display.')
                   6798:                  .'</p>'
                   6799:         );
1.239     raeburn  6800:     }
1.301     bisitz   6801: 
1.327     raeburn  6802:     # Form Footer
                   6803:     $r->print( 
                   6804:         '<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   6805:        .'<input type="hidden" name="action" value="changelogs" />'
                   6806:        .'</form>');
                   6807:     return;
                   6808: }
1.301     bisitz   6809: 
1.416     raeburn  6810: sub print_useraccesslogs_display {
                   6811:     my ($r,$uname,$udom,$permission,$brcrum) = @_;
                   6812:     my $formname = 'accesslog';
                   6813:     my $form = 'document.accesslog';
                   6814: 
                   6815: # set breadcrumbs
                   6816:     my %breadcrumb_text = &singleuser_breadcrumb();
                   6817:     push (@{$brcrum},
                   6818:         {href => "javascript:backPage($form)",
                   6819:          text => $breadcrumb_text{'search'}});
                   6820:     my (@prevphases,$prevphasestr);
                   6821:     if ($env{'form.prevphases'}) {
                   6822:         @prevphases = split(/,/,$env{'form.prevphases'});
                   6823:         $prevphasestr = $env{'form.prevphases'};
                   6824:     }
                   6825:     if (($env{'form.phase'} eq 'userpicked') || (grep(/^userpicked$/,@prevphases))) {
                   6826:         push(@{$brcrum},
                   6827:               {href => "javascript:backPage($form,'get_user_info','select')",
                   6828:                text => $breadcrumb_text{'userpicked'}});
                   6829:         if ($env{'form.phase'} eq 'userpicked') {
                   6830:             $prevphasestr = 'userpicked';
                   6831:         }
                   6832:     }
                   6833:     push(@{$brcrum},
                   6834:              {href => '/adm/createuser?action=accesslogs',
                   6835:               text => 'User access logs',
                   6836:               help => 'User_Access_Logs'});
                   6837:     my $bread_crumbs_component = 'User Access Logs';
                   6838:     my $args = { bread_crumbs           => $brcrum,
                   6839:                  bread_crumbs_component => 'User Management'};
                   6840: 
1.417     raeburn  6841: # set javascript
1.416     raeburn  6842:     my ($jsback,$elements) = &crumb_utilities();
                   6843:     my $jsnav = &userlogdisplay_js($formname);
                   6844: 
                   6845:     my $jscript = (<<ENDSCRIPT);
                   6846: <script type="text/javascript">
                   6847: // <![CDATA[
                   6848: 
                   6849: $jsback
                   6850: $jsnav
                   6851: 
                   6852: // ]]>
                   6853: </script>
                   6854: 
                   6855: ENDSCRIPT
                   6856: 
1.417     raeburn  6857: # print page header
1.416     raeburn  6858:     $r->print(&header($jscript,$args));
                   6859: 
                   6860: # early out unless log data can be displayed.
                   6861:     unless ($permission->{'activity'}) {
                   6862:         $r->print('<p class="LC_warning">'
                   6863:                  .&mt('You do not have rights to display user access logs.')
                   6864:                  .'</p>'
                   6865:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   6866:         return;
                   6867:     }
                   6868: 
                   6869:     unless ($udom eq $env{'request.role.domain'}) {
                   6870:         $r->print('<p class="LC_warning">'
                   6871:                  .&mt("User's domain must match role's domain")
                   6872:                  .'</p>'
                   6873:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
1.417     raeburn  6874:         return;
1.416     raeburn  6875:     }
                   6876: 
                   6877:     if (($uname eq '') || ($udom eq '')) {
                   6878:         $r->print('<p class="LC_warning">'
                   6879:                  .&mt('Invalid username or domain')
                   6880:                  .'</p>'
                   6881:                  .&earlyout_accesslog_form($formname,$prevphasestr,$udom));
                   6882:         return;
                   6883:     }
                   6884: 
                   6885: # set defaults
                   6886:     my $now = time();
                   6887:     my $defstart = $now - (7*24*3600);
                   6888:     my %defaults = (
                   6889:                      page                 => '1',
                   6890:                      show                 => '10',
                   6891:                      activity             => 'any',
                   6892:                      accesslog_start_date => $defstart,
                   6893:                      accesslog_end_date   => $now,
                   6894:                    );
                   6895:     my $more_records = 0;
                   6896: 
                   6897: # set current
                   6898:     my %curr;
                   6899:     foreach my $item ('show','page','activity') {
                   6900:         $curr{$item} = $env{'form.'.$item};
                   6901:     }
                   6902:     my ($startdate,$enddate) =
                   6903:         &Apache::lonuserutils::get_dates_from_form('accesslog_start_date','accesslog_end_date');
                   6904:     $curr{'accesslog_start_date'} = $startdate;
                   6905:     $curr{'accesslog_end_date'} = $enddate;
                   6906:     foreach my $key (keys(%defaults)) {
                   6907:         if ($curr{$key} eq '') {
                   6908:             $curr{$key} = $defaults{$key};
                   6909:         }
                   6910:     }
                   6911:     my ($minshown,$maxshown);
                   6912:     $minshown = 1;
                   6913:     my $count = 0;
                   6914:     if ($curr{'show'} =~ /\D/) {
                   6915:         $curr{'page'} = 1;
                   6916:     } else {
                   6917:         $maxshown = $curr{'page'} * $curr{'show'};
                   6918:         if ($curr{'page'} > 1) {
                   6919:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   6920:         }
                   6921:     }
                   6922: 
                   6923: # form header
                   6924:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
                   6925:               &activity_display_filter($formname,\%curr));
                   6926: 
                   6927:     my $showntableheader = 0;
                   6928:     my ($nav_script,$nav_links);
                   6929: 
                   6930: # table header
                   6931:     my $tableheader =
                   6932:         &Apache::loncommon::start_data_table_header_row()
                   6933:        .'<th>&nbsp;</th>'
                   6934:        .'<th>'.&mt('When').'</th>'
                   6935:        .'<th>'.&mt('HostID').'</th>'
                   6936:        .'<th>'.&mt('Event').'</th>'
                   6937:        .'<th>'.&mt('Other data').'</th>'
                   6938:        .&Apache::loncommon::end_data_table_header_row();
                   6939: 
                   6940:     my %filters=(
                   6941:         start  => $curr{'accesslog_start_date'},
                   6942:         end    => $curr{'accesslog_end_date'},
                   6943:         action => $curr{'activity'},
                   6944:     );
                   6945: 
                   6946:     my $reply = &Apache::lonnet::userlog_query($uname,$udom,%filters);
                   6947:     unless ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   6948:         my (%courses,%missing);
                   6949:         my @results = split(/\&/,$reply);
                   6950:         foreach my $item (reverse(@results)) {
                   6951:             my ($timestamp,$host,$event) = split(/:/,$item);
                   6952:             next unless ($event =~ /^(Log|Role)/);
                   6953:             if ($curr{'show'} !~ /\D/) {
                   6954:                 if ($count >= $curr{'page'} * $curr{'show'}) {
                   6955:                     $more_records = 1;
                   6956:                     last;
                   6957:                 }
                   6958:             }
                   6959:             $count ++;
                   6960:             next if ($count < $minshown);
                   6961:             unless ($showntableheader) {
                   6962:                 $r->print($nav_script
                   6963:                          .&Apache::loncommon::start_data_table()
                   6964:                          .$tableheader);
                   6965:                 $r->rflush();
                   6966:                 $showntableheader = 1;
                   6967:             }
1.418   ! raeburn  6968:             my ($shown,$extra);
1.416     raeburn  6969:             my ($event,$data) = split(/\s+/,&unescape($event));
                   6970:             if ($event eq 'Role') {
                   6971:                 my ($rolecode,$extent) = split(/\./,$data,2);
                   6972:                 next if ($extent eq '');
                   6973:                 my ($crstype,$desc,$info);
1.418   ! raeburn  6974:                 if ($extent =~ m{^/($match_domain)/($match_courseid)(?:/(\w+)|)$}) {
        !          6975:                     my ($cdom,$cnum,$sec) = ($1,$2,$3);
1.416     raeburn  6976:                     my $cid = $cdom.'_'.$cnum;
                   6977:                     if (exists($courses{$cid})) {
                   6978:                         $crstype = $courses{$cid}{'type'};
                   6979:                         $desc = $courses{$cid}{'description'};
                   6980:                     } elsif ($missing{$cid}) {
                   6981:                         $crstype = 'Course';
                   6982:                         $desc = 'Course/Community';
                   6983:                     } else {
                   6984:                         my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   6985:                         if (ref($crsinfo{$cdom.'_'.$cnum}) eq 'HASH') {
                   6986:                             $courses{$cid} = $crsinfo{$cid};
                   6987:                             $crstype = $crsinfo{$cid}{'type'};
                   6988:                             $desc = $crsinfo{$cid}{'description'};
                   6989:                         } else {
                   6990:                             $missing{$cid} = 1;
                   6991:                         }
                   6992:                     }
                   6993:                     $extra = &mt($crstype).': <a href="/public/'.$cdom.'/'.$cnum.'/syllabus">'.$desc.'</a>';
1.418   ! raeburn  6994:                     if ($sec ne '') {
        !          6995:                        $extra .= ' ('.&mt('Section: [_1]',$sec).')';
        !          6996:                     }
1.416     raeburn  6997:                 } elsif ($extent =~ m{^/($match_domain)/($match_username|$)}) {
                   6998:                     my ($dom,$name) = ($1,$2);
                   6999:                     if ($rolecode eq 'au') {
                   7000:                         $extra = '';
                   7001:                     } elsif ($rolecode =~ /^(ca|aa)$/) {
1.417     raeburn  7002:                         $extra = &mt('Authoring Space: [_1]',$name.':'.$dom);
1.416     raeburn  7003:                     } elsif ($rolecode =~ /^(li|dg|dh|dc|sc)$/) {
                   7004:                         $extra = &mt('Domain: [_1]',$dom);
                   7005:                     }
                   7006:                 }
                   7007:                 my $rolename;
                   7008:                 if ($rolecode =~ m{^cr/($match_domain)/($match_username)/(\w+)}) {
                   7009:                     my $role = $3;
1.417     raeburn  7010:                     my $owner = "($2:$1)";
1.416     raeburn  7011:                     if ($2 eq $1.'-domainconfig') {
                   7012:                         $owner = '(ad hoc)';
1.417     raeburn  7013:                     }
1.416     raeburn  7014:                     $rolename = &mt('Custom role: [_1]',$role.' '.$owner);
                   7015:                 } else {
                   7016:                     $rolename = &Apache::lonnet::plaintext($rolecode,$crstype);
                   7017:                 }
                   7018:                 $shown = &mt('Role selection: [_1]',$rolename);
                   7019:             } else {
                   7020:                 $shown = &mt($event);
                   7021:                 if ($data ne '') {
                   7022:                    $extra = &mt('Client IP address: [_1]',$data);
                   7023:                 }
                   7024:             }
                   7025:             $r->print(
                   7026:             &Apache::loncommon::start_data_table_row()
                   7027:            .'<td>'.$count.'</td>'
                   7028:            .'<td>'.&Apache::lonlocal::locallocaltime($timestamp).'</td>'
                   7029:            .'<td>'.$host.'</td>'
                   7030:            .'<td>'.$shown.'</td>'
                   7031:            .'<td>'.$extra.'</td>'
                   7032:            .&Apache::loncommon::end_data_table_row()."\n");
                   7033:         }
                   7034:     }
                   7035: 
                   7036:     if ($showntableheader) { # Table footer, if content displayed above
                   7037:         $r->print(&Apache::loncommon::end_data_table().
                   7038:                   &userlogdisplay_navlinks(\%curr,$more_records));
                   7039:     } else { # No content displayed above
                   7040:         $r->print('<p class="LC_info">'
                   7041:                  .&mt('There are no records to display.')
                   7042:                  .'</p>');
                   7043:     }
                   7044: 
                   7045:     # Form Footer
                   7046:     $r->print(
                   7047:         '<input type="hidden" name="currstate" value="" />'
                   7048:        .'<input type="hidden" name="accessuname" value="'.$uname.'" />'
                   7049:        .'<input type="hidden" name="accessudom" value="'.$udom.'" />'
                   7050:        .'<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   7051:        .'<input type="hidden" name="prevphases" value="'.$prevphasestr.'" />'
                   7052:        .'<input type="hidden" name="phase" value="activity" />'
                   7053:        .'<input type="hidden" name="action" value="accesslogs" />'
                   7054:        .'<input type="hidden" name="srchdomain" value="'.$udom.'" />'
                   7055:        .'<input type="hidden" name="srchby" value="'.$env{'form.srchby'}.'" />'
                   7056:        .'<input type="hidden" name="srchtype" value="'.$env{'form.srchtype'}.'" />'
                   7057:        .'<input type="hidden" name="srchterm" value="'.&HTML::Entities::encode($env{'form.srchterm'},'<>"&').'" />'
                   7058:        .'<input type="hidden" name="srchin" value="'.$env{'form.srchin'}.'" />'
                   7059:        .'</form>');
                   7060:     return;
                   7061: }
                   7062: 
                   7063: sub earlyout_accesslog_form {
                   7064:     my ($formname,$prevphasestr,$udom) = @_;
                   7065:     my $srchterm = &HTML::Entities::encode($env{'form.srchterm'},'<>"&');
                   7066:    return <<"END";
                   7067: <form action="/adm/createuser" method="post" name="$formname">
                   7068: <input type="hidden" name="currstate" value="" />
                   7069: <input type="hidden" name="prevphases" value="$prevphasestr" />
                   7070: <input type="hidden" name="phase" value="activity" />
                   7071: <input type="hidden" name="action" value="accesslogs" />
                   7072: <input type="hidden" name="srchdomain" value="$udom" />
                   7073: <input type="hidden" name="srchby" value="$env{'form.srchby'}" />
                   7074: <input type="hidden" name="srchtype" value="$env{'form.srchtype'}" />
                   7075: <input type="hidden" name="srchterm" value="$srchterm" />
                   7076: <input type="hidden" name="srchin" value="$env{'form.srchin'}" />
                   7077: </form>
                   7078: END
                   7079: }
                   7080: 
                   7081: sub activity_display_filter {
                   7082:     my ($formname,$curr) = @_;
                   7083:     my $nolink = 1;
                   7084:     my $output = '<table><tr><td valign="top">'.
                   7085:                  '<span class="LC_nobreak"><b>'.&mt('Actions/page:').'</b></span><br />'.
                   7086:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   7087:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   7088:                  '</td><td>&nbsp;&nbsp;</td>';
                   7089:     my $startform =
                   7090:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_start_date',
                   7091:                                             $curr->{'accesslog_start_date'},undef,
                   7092:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   7093:     my $endform =
                   7094:         &Apache::lonhtmlcommon::date_setter($formname,'accesslog_end_date',
                   7095:                                             $curr->{'accesslog_end_date'},undef,
                   7096:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   7097:     my %lt = &Apache::lonlocal::texthash (
                   7098:                                           activity => 'Activity',
                   7099:                                           Role     => 'Role selection',
                   7100:                                           log      => 'Log-in or Logout',
                   7101:     );
                   7102:     $output .= '<td valign="top"><b>'.&mt('Window during which actions occurred:').'</b><br />'.
                   7103:                '<table><tr><td>'.&mt('After:').
                   7104:                '</td><td>'.$startform.'</td></tr>'.
                   7105:                '<tr><td>'.&mt('Before:').'</td>'.
                   7106:                '<td>'.$endform.'</td></tr></table>'.
                   7107:                '</td>'.
                   7108:                '<td>&nbsp;&nbsp;</td>'.
                   7109:                '<td valign="top"><b>'.&mt('Activities').'</b><br />'.
                   7110:                '<select name="activity"><option value="any"';
                   7111:     if ($curr->{'activity'} eq 'any') {
                   7112:         $output .= ' selected="selected"';
                   7113:     }
                   7114:     $output .= '>'.&mt('Any').'</option>'."\n";
                   7115:     foreach my $activity ('Role','log') {
                   7116:         my $selstr = '';
                   7117:         if ($activity eq $curr->{'activity'}) {
                   7118:             $selstr = ' selected="selected"';
                   7119:         }
                   7120:         $output .= '<option value="'.$activity.'"'.$selstr.'>'.$lt{$activity}.'</option>';
                   7121:     }
                   7122:     $output .= '</select></td>'.
                   7123:                '</tr></table>';
                   7124:     # Update Display button
                   7125:     $output .= '<p>'
                   7126:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   7127:               .'</p>';
                   7128:     return $output;
                   7129: }
                   7130: 
1.415     raeburn  7131: sub userlogdisplay_js {
                   7132:     my ($formname) = @_;
                   7133:     return <<"ENDSCRIPT";
                   7134: 
1.239     raeburn  7135: function chgPage(caller) {
                   7136:     if (caller == 'previous') {
                   7137:         document.$formname.page.value --;
                   7138:     }
                   7139:     if (caller == 'next') {
                   7140:         document.$formname.page.value ++;
                   7141:     }
1.327     raeburn  7142:     document.$formname.submit();
1.239     raeburn  7143:     return;
                   7144: }
                   7145: ENDSCRIPT
1.415     raeburn  7146: }
                   7147: 
                   7148: sub userlogdisplay_navlinks {
                   7149:     my ($curr,$more_records) = @_;
                   7150:     return unless(ref($curr) eq 'HASH');
                   7151:     # Navigation Buttons
                   7152:     my $nav_links = '<p>';
                   7153:     if (($curr->{'page'} > 1) || ($more_records)) {
                   7154:         if (($curr->{'page'} > 1) && ($curr->{'show'} !~ /\D/)) {
                   7155:             $nav_links .= '<input type="button"'
                   7156:                          .' onclick="javascript:chgPage('."'previous'".');"'
                   7157:                          .' value="'.&mt('Previous [_1] changes',$curr->{'show'})
                   7158:                          .'" /> ';
                   7159:         }
                   7160:         if ($more_records) {
                   7161:             $nav_links .= '<input type="button"'
                   7162:                          .' onclick="javascript:chgPage('."'next'".');"'
                   7163:                          .' value="'.&mt('Next [_1] changes',$curr->{'show'})
                   7164:                          .'" />';
1.301     bisitz   7165:         }
                   7166:     }
1.415     raeburn  7167:     $nav_links .= '</p>';
                   7168:     return $nav_links;
1.239     raeburn  7169: }
                   7170: 
                   7171: sub role_display_filter {
1.363     raeburn  7172:     my ($context,$formname,$cdom,$cnum,$curr,$version,$crstype) = @_;
                   7173:     my $lctype;
                   7174:     if ($context eq 'course') {
                   7175:         $lctype = lc($crstype);
                   7176:     }
1.239     raeburn  7177:     my $nolink = 1;
                   7178:     my $output = '<table><tr><td valign="top">'.
1.301     bisitz   7179:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b></span><br />'.
1.239     raeburn  7180:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   7181:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   7182:                  '</td><td>&nbsp;&nbsp;</td>';
                   7183:     my $startform =
                   7184:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
                   7185:                                             $curr->{'rolelog_start_date'},undef,
                   7186:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   7187:     my $endform =
                   7188:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
                   7189:                                             $curr->{'rolelog_end_date'},undef,
                   7190:                                             undef,undef,undef,undef,undef,undef,$nolink);
1.363     raeburn  7191:     my %lt = &rolechg_contexts($context,$crstype);
1.301     bisitz   7192:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br />'.
                   7193:                '<table><tr><td>'.&mt('After:').
                   7194:                '</td><td>'.$startform.'</td></tr>'.
                   7195:                '<tr><td>'.&mt('Before:').'</td>'.
                   7196:                '<td>'.$endform.'</td></tr></table>'.
                   7197:                '</td>'.
                   7198:                '<td>&nbsp;&nbsp;</td>'.
1.239     raeburn  7199:                '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
                   7200:                '<select name="role"><option value="any"';
                   7201:     if ($curr->{'role'} eq 'any') {
                   7202:         $output .= ' selected="selected"';
                   7203:     }
                   7204:     $output .=  '>'.&mt('Any').'</option>'."\n";
1.363     raeburn  7205:     my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
1.239     raeburn  7206:     foreach my $role (@roles) {
                   7207:         my $plrole;
                   7208:         if ($role eq 'cr') {
                   7209:             $plrole = &mt('Custom Role');
                   7210:         } else {
1.318     raeburn  7211:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.239     raeburn  7212:         }
                   7213:         my $selstr = '';
                   7214:         if ($role eq $curr->{'role'}) {
                   7215:             $selstr = ' selected="selected"';
                   7216:         }
                   7217:         $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
                   7218:     }
1.301     bisitz   7219:     $output .= '</select></td>'.
                   7220:                '<td>&nbsp;&nbsp;</td>'.
                   7221:                '<td valign="top"><b>'.
1.239     raeburn  7222:                &mt('Context:').'</b><br /><select name="chgcontext">';
1.363     raeburn  7223:     my @posscontexts;
                   7224:     if ($context eq 'course') {
1.376     raeburn  7225:         @posscontexts = ('any','automated','updatenow','createcourse','course','domain','selfenroll','requestcourses');
1.363     raeburn  7226:     } elsif ($context eq 'domain') {
                   7227:         @posscontexts = ('any','domain','requestauthor','domconfig','server');
                   7228:     } else {
                   7229:         @posscontexts = ('any','author','domain');
                   7230:     } 
                   7231:     foreach my $chgtype (@posscontexts) {
1.239     raeburn  7232:         my $selstr = '';
                   7233:         if ($curr->{'chgcontext'} eq $chgtype) {
1.301     bisitz   7234:             $selstr = ' selected="selected"';
1.239     raeburn  7235:         }
1.363     raeburn  7236:         if ($context eq 'course') {
1.376     raeburn  7237:             if (($chgtype eq 'automated') || ($chgtype eq 'updatenow')) {
1.363     raeburn  7238:                 next if (!&Apache::lonnet::auto_run($cnum,$cdom));
                   7239:             }
1.239     raeburn  7240:         }
                   7241:         $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
1.248     raeburn  7242:     }
1.303     bisitz   7243:     $output .= '</select></td>'
                   7244:               .'</tr></table>';
                   7245: 
                   7246:     # Update Display button
                   7247:     $output .= '<p>'
                   7248:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   7249:               .'</p>';
                   7250: 
                   7251:     # Server version info
1.363     raeburn  7252:     my $needsrev = '2.11.0';
                   7253:     if ($context eq 'course') {
                   7254:         $needsrev = '2.7.0';
                   7255:     }
                   7256:     
1.303     bisitz   7257:     $output .= '<p class="LC_info">'
                   7258:               .&mt('Only changes made from servers running LON-CAPA [_1] or later are displayed.'
1.363     raeburn  7259:                   ,$needsrev);
1.248     raeburn  7260:     if ($version) {
1.303     bisitz   7261:         $output .= ' '.&mt('This LON-CAPA server is version [_1]',$version);
                   7262:     }
                   7263:     $output .= '</p><hr />';
1.239     raeburn  7264:     return $output;
                   7265: }
                   7266: 
                   7267: sub rolechg_contexts {
1.363     raeburn  7268:     my ($context,$crstype) = @_;
                   7269:     my %lt;
                   7270:     if ($context eq 'course') {
                   7271:         %lt = &Apache::lonlocal::texthash (
1.239     raeburn  7272:                                              any          => 'Any',
1.376     raeburn  7273:                                              automated    => 'Automated Enrollment',
1.239     raeburn  7274:                                              updatenow    => 'Roster Update',
                   7275:                                              createcourse => 'Course Creation',
                   7276:                                              course       => 'User Management in course',
                   7277:                                              domain       => 'User Management in domain',
1.313     raeburn  7278:                                              selfenroll   => 'Self-enrolled',
1.318     raeburn  7279:                                              requestcourses => 'Course Request',
1.239     raeburn  7280:                                          );
1.363     raeburn  7281:         if ($crstype eq 'Community') {
                   7282:             $lt{'createcourse'} = &mt('Community Creation');
                   7283:             $lt{'course'} = &mt('User Management in community');
                   7284:             $lt{'requestcourses'} = &mt('Community Request');
                   7285:         }
                   7286:     } elsif ($context eq 'domain') {
                   7287:         %lt = &Apache::lonlocal::texthash (
                   7288:                                              any           => 'Any',
                   7289:                                              domain        => 'User Management in domain',
                   7290:                                              requestauthor => 'Authoring Request',
                   7291:                                              server        => 'Command line script (DC role)',
                   7292:                                              domconfig     => 'Self-enrolled',
                   7293:                                          );
                   7294:     } else {
                   7295:         %lt = &Apache::lonlocal::texthash (
                   7296:                                              any    => 'Any',
                   7297:                                              domain => 'User Management in domain',
                   7298:                                              author => 'User Management by author',
                   7299:                                          );
                   7300:     } 
1.239     raeburn  7301:     return %lt;
                   7302: }
                   7303: 
1.27      matthew  7304: #-------------------------------------------------- functions for &phase_two
1.160     raeburn  7305: sub user_search_result {
1.221     raeburn  7306:     my ($context,$srch) = @_;
1.160     raeburn  7307:     my %allhomes;
                   7308:     my %inst_matches;
                   7309:     my %srch_results;
1.181     raeburn  7310:     my ($response,$currstate,$forcenewuser,$dirsrchres);
1.183     raeburn  7311:     $srch->{'srchterm'} =~ s/\s+/ /g;
1.176     raeburn  7312:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
1.160     raeburn  7313:         $response = &mt('Invalid search.');
                   7314:     }
                   7315:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
                   7316:         $response = &mt('Invalid search.');
                   7317:     }
1.177     raeburn  7318:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
1.160     raeburn  7319:         $response = &mt('Invalid search.');
                   7320:     }
                   7321:     if ($srch->{'srchterm'} eq '') {
                   7322:         $response = &mt('You must enter a search term.');
                   7323:     }
1.183     raeburn  7324:     if ($srch->{'srchterm'} =~ /^\s+$/) {
                   7325:         $response = &mt('Your search term must contain more than just spaces.');
                   7326:     }
1.160     raeburn  7327:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
                   7328:         if (($srch->{'srchdomain'} eq '') || 
1.163     albertel 7329: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
1.160     raeburn  7330:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
                   7331:         }
                   7332:     }
                   7333:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
                   7334:         ($srch->{'srchin'} eq 'alc')) {
1.176     raeburn  7335:         if ($srch->{'srchby'} eq 'uname') {
1.243     raeburn  7336:             my $unamecheck = $srch->{'srchterm'};
                   7337:             if ($srch->{'srchtype'} eq 'contains') {
                   7338:                 if ($unamecheck !~ /^\w/) {
                   7339:                     $unamecheck = 'a'.$unamecheck; 
                   7340:                 }
                   7341:             }
                   7342:             if ($unamecheck !~ /^$match_username$/) {
1.176     raeburn  7343:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
                   7344:             }
1.160     raeburn  7345:         }
                   7346:     }
1.180     raeburn  7347:     if ($response ne '') {
1.413     raeburn  7348:         $response = '<span class="LC_warning">'.$response.'</span><br />';
1.180     raeburn  7349:     }
1.160     raeburn  7350:     if ($srch->{'srchin'} eq 'instd') {
1.412     raeburn  7351:         my $instd_chk = &instdirectorysrch_check($srch);
1.160     raeburn  7352:         if ($instd_chk ne 'ok') {
1.412     raeburn  7353:             my $domd_chk = &domdirectorysrch_check($srch);
1.413     raeburn  7354:             $response .= '<span class="LC_warning">'.$instd_chk.'</span><br />';
1.412     raeburn  7355:             if ($domd_chk eq 'ok') {
1.413     raeburn  7356:                 $response .= &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.');
1.412     raeburn  7357:             }
1.415     raeburn  7358:             $response .= '<br />';
1.412     raeburn  7359:         }
                   7360:     } else {
1.417     raeburn  7361:         unless (($context eq 'requestcrs') && ($srch->{'srchtype'} eq 'exact')) {
1.412     raeburn  7362:             my $domd_chk = &domdirectorysrch_check($srch);
                   7363:             if ($domd_chk ne 'ok') {
                   7364:                 my $instd_chk = &instdirectorysrch_check($srch);
1.413     raeburn  7365:                 $response .= '<span class="LC_warning">'.$domd_chk.'</span><br />';
1.412     raeburn  7366:                 if ($instd_chk eq 'ok') {
1.413     raeburn  7367:                     $response .= &mt('You may want to search in the institutional directory instead of the LON-CAPA domain.');
1.412     raeburn  7368:                 }
1.415     raeburn  7369:                 $response .= '<br />';
1.412     raeburn  7370:             }
1.160     raeburn  7371:         }
                   7372:     }
                   7373:     if ($response ne '') {
1.180     raeburn  7374:         return ($currstate,$response);
1.160     raeburn  7375:     }
                   7376:     if ($srch->{'srchby'} eq 'uname') {
                   7377:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
                   7378:             if ($env{'form.forcenew'}) {
                   7379:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
                   7380:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   7381:                     if ($uhome eq 'no_host') {
                   7382:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.180     raeburn  7383:                         my $showdom = &display_domain_info($env{'request.role.domain'});
                   7384:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
1.160     raeburn  7385:                     } else {
1.179     raeburn  7386:                         $currstate = 'modify';
1.160     raeburn  7387:                     }
                   7388:                 } else {
1.179     raeburn  7389:                     $currstate = 'modify';
1.160     raeburn  7390:                 }
                   7391:             } else {
                   7392:                 if ($srch->{'srchin'} eq 'dom') {
1.162     raeburn  7393:                     if ($srch->{'srchtype'} eq 'exact') {
                   7394:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   7395:                         if ($uhome eq 'no_host') {
1.179     raeburn  7396:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  7397:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  7398:                         } else {
1.179     raeburn  7399:                             $currstate = 'modify';
1.416     raeburn  7400:                             if ($env{'form.action'} eq 'accesslogs') {
                   7401:                                 $currstate = 'activity';
                   7402:                             }
1.310     raeburn  7403:                             my $uname = $srch->{'srchterm'};
                   7404:                             my $udom = $srch->{'srchdomain'};
                   7405:                             $srch_results{$uname.':'.$udom} =
                   7406:                                 { &Apache::lonnet::get('environment',
                   7407:                                                        ['firstname',
                   7408:                                                         'lastname',
                   7409:                                                         'permanentemail'],
                   7410:                                                          $udom,$uname)
                   7411:                                 };
1.162     raeburn  7412:                         }
                   7413:                     } else {
                   7414:                         %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  7415:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  7416:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  7417:                     }
                   7418:                 } else {
1.167     albertel 7419:                     my $courseusers = &get_courseusers();
1.162     raeburn  7420:                     if ($srch->{'srchtype'} eq 'exact') {
1.167     albertel 7421:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
1.179     raeburn  7422:                             $currstate = 'modify';
1.162     raeburn  7423:                         } else {
1.179     raeburn  7424:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  7425:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  7426:                         }
1.160     raeburn  7427:                     } else {
1.167     albertel 7428:                         foreach my $user (keys(%$courseusers)) {
1.162     raeburn  7429:                             my ($cuname,$cudomain) = split(/:/,$user);
                   7430:                             if ($cudomain eq $srch->{'srchdomain'}) {
1.177     raeburn  7431:                                 my $matched = 0;
                   7432:                                 if ($srch->{'srchtype'} eq 'begins') {
                   7433:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
                   7434:                                         $matched = 1;
                   7435:                                     }
                   7436:                                 } else {
                   7437:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
                   7438:                                         $matched = 1;
                   7439:                                     }
                   7440:                                 }
                   7441:                                 if ($matched) {
1.167     albertel 7442:                                     $srch_results{$user} = 
                   7443: 					{&Apache::lonnet::get('environment',
                   7444: 							     ['firstname',
                   7445: 							      'lastname',
1.194     albertel 7446: 							      'permanentemail'],
                   7447: 							      $cudomain,$cuname)};
1.162     raeburn  7448:                                 }
                   7449:                             }
                   7450:                         }
1.179     raeburn  7451:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  7452:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  7453:                     }
                   7454:                 }
                   7455:             }
                   7456:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  7457:             $currstate = 'query';
1.160     raeburn  7458:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  7459:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
                   7460:             if ($dirsrchres eq 'ok') {
                   7461:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7462:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  7463:             } else {
                   7464:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   7465:                 $response = '<span class="LC_warning">'.
                   7466:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   7467:                     '</span><br />'.
                   7468:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
1.415     raeburn  7469:                     '<br />'; 
1.181     raeburn  7470:             }
1.160     raeburn  7471:         }
                   7472:     } else {
                   7473:         if ($srch->{'srchin'} eq 'dom') {
                   7474:             %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  7475:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7476:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  7477:         } elsif ($srch->{'srchin'} eq 'crs') {
1.167     albertel 7478:             my $courseusers = &get_courseusers(); 
                   7479:             foreach my $user (keys(%$courseusers)) {
1.160     raeburn  7480:                 my ($uname,$udom) = split(/:/,$user);
                   7481:                 my %names = &Apache::loncommon::getnames($uname,$udom);
                   7482:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
                   7483:                 if ($srch->{'srchby'} eq 'lastname') {
                   7484:                     if ((($srch->{'srchtype'} eq 'exact') && 
                   7485:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
1.177     raeburn  7486:                         (($srch->{'srchtype'} eq 'begins') &&
                   7487:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
1.160     raeburn  7488:                         (($srch->{'srchtype'} eq 'contains') &&
                   7489:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
                   7490:                         $srch_results{$user} = {firstname => $names{'firstname'},
                   7491:                                             lastname => $names{'lastname'},
                   7492:                                             permanentemail => $emails{'permanentemail'},
                   7493:                                            };
                   7494:                     }
                   7495:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
                   7496:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
1.177     raeburn  7497:                     $srchlast =~ s/\s+$//;
                   7498:                     $srchfirst =~ s/^\s+//;
1.160     raeburn  7499:                     if ($srch->{'srchtype'} eq 'exact') {
                   7500:                         if (($names{'lastname'} eq $srchlast) &&
                   7501:                             ($names{'firstname'} eq $srchfirst)) {
                   7502:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   7503:                                                 lastname => $names{'lastname'},
                   7504:                                                 permanentemail => $emails{'permanentemail'},
                   7505: 
                   7506:                                            };
                   7507:                         }
1.177     raeburn  7508:                     } elsif ($srch->{'srchtype'} eq 'begins') {
                   7509:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
                   7510:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
                   7511:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   7512:                                                 lastname => $names{'lastname'},
                   7513:                                                 permanentemail => $emails{'permanentemail'},
                   7514:                                                };
                   7515:                         }
                   7516:                     } else {
1.160     raeburn  7517:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
                   7518:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
                   7519:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   7520:                                                 lastname => $names{'lastname'},
                   7521:                                                 permanentemail => $emails{'permanentemail'},
                   7522:                                                };
                   7523:                         }
                   7524:                     }
                   7525:                 }
                   7526:             }
1.179     raeburn  7527:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7528:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  7529:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  7530:             $currstate = 'query';
1.160     raeburn  7531:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  7532:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
                   7533:             if ($dirsrchres eq 'ok') {
                   7534:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  7535:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  7536:             } else {
1.412     raeburn  7537:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   7538:                 $response = '<span class="LC_warning">'.
1.181     raeburn  7539:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   7540:                     '</span><br />'.
                   7541:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
1.415     raeburn  7542:                     '<br />';
1.181     raeburn  7543:             }
1.160     raeburn  7544:         }
                   7545:     }
1.179     raeburn  7546:     return ($currstate,$response,$forcenewuser,\%srch_results);
1.160     raeburn  7547: }
                   7548: 
1.412     raeburn  7549: sub domdirectorysrch_check {
                   7550:     my ($srch) = @_;
                   7551:     my $response;
                   7552:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   7553:                                              ['directorysrch'],$srch->{'srchdomain'});
                   7554:     my $showdom = &display_domain_info($srch->{'srchdomain'});
                   7555:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   7556:         if ($dom_inst_srch{'directorysrch'}{'lcavailable'} eq '0') {
                   7557:             return &mt('LON-CAPA directory search is not available in domain: [_1]',$showdom);
                   7558:         }
                   7559:         if ($dom_inst_srch{'directorysrch'}{'lclocalonly'}) {
                   7560:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
                   7561:                 return &mt('LON-CAPA directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom);
                   7562:             }
                   7563:         }
                   7564:     }
                   7565:     return 'ok';
                   7566: }
                   7567: 
                   7568: sub instdirectorysrch_check {
1.160     raeburn  7569:     my ($srch) = @_;
                   7570:     my $can_search = 0;
                   7571:     my $response;
                   7572:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   7573:                                              ['directorysrch'],$srch->{'srchdomain'});
1.180     raeburn  7574:     my $showdom = &display_domain_info($srch->{'srchdomain'});
1.160     raeburn  7575:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   7576:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
1.180     raeburn  7577:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
1.160     raeburn  7578:         }
                   7579:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
                   7580:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
1.180     raeburn  7581:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
1.160     raeburn  7582:             }
                   7583:             my @usertypes = split(/:/,$env{'environment.inststatus'});
                   7584:             if (!@usertypes) {
                   7585:                 push(@usertypes,'default');
                   7586:             }
                   7587:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
                   7588:                 foreach my $type (@usertypes) {
                   7589:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
                   7590:                         $can_search = 1;
                   7591:                         last;
                   7592:                     }
                   7593:                 }
                   7594:             }
                   7595:             if (!$can_search) {
                   7596:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
                   7597:                 my @longtypes; 
                   7598:                 foreach my $item (@usertypes) {
1.229     raeburn  7599:                     if (defined($insttypes->{$item})) { 
                   7600:                         push (@longtypes,$insttypes->{$item});
                   7601:                     } elsif ($item eq 'default') {
                   7602:                         push (@longtypes,&mt('other')); 
                   7603:                     }
1.160     raeburn  7604:                 }
                   7605:                 my $insttype_str = join(', ',@longtypes); 
1.180     raeburn  7606:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
1.229     raeburn  7607:             }
1.160     raeburn  7608:         } else {
                   7609:             $can_search = 1;
                   7610:         }
                   7611:     } else {
1.180     raeburn  7612:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
1.160     raeburn  7613:     }
                   7614:     my %longtext = &Apache::lonlocal::texthash (
1.167     albertel 7615:                        uname     => 'username',
1.160     raeburn  7616:                        lastfirst => 'last name, first name',
1.167     albertel 7617:                        lastname  => 'last name',
1.172     raeburn  7618:                        contains  => 'contains',
1.178     raeburn  7619:                        exact     => 'as exact match to',
                   7620:                        begins    => 'begins with',
1.160     raeburn  7621:                    );
                   7622:     if ($can_search) {
                   7623:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
                   7624:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
1.180     raeburn  7625:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
1.160     raeburn  7626:             }
                   7627:         } else {
1.180     raeburn  7628:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
1.160     raeburn  7629:         }
                   7630:     }
                   7631:     if ($can_search) {
1.178     raeburn  7632:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
                   7633:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
                   7634:                 return 'ok';
                   7635:             } else {
1.180     raeburn  7636:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  7637:             }
                   7638:         } else {
                   7639:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
                   7640:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
                   7641:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
                   7642:                 return 'ok';
                   7643:             } else {
1.180     raeburn  7644:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  7645:             }
1.160     raeburn  7646:         }
                   7647:     }
                   7648: }
                   7649: 
                   7650: sub get_courseusers {
                   7651:     my %advhash;
1.167     albertel 7652:     my $classlist = &Apache::loncoursedata::get_classlist();
1.160     raeburn  7653:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                   7654:     foreach my $role (sort(keys(%coursepersonnel))) {
                   7655:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
1.167     albertel 7656: 	    if (!exists($classlist->{$user})) {
                   7657: 		$classlist->{$user} = [];
                   7658: 	    }
1.160     raeburn  7659:         }
                   7660:     }
1.167     albertel 7661:     return $classlist;
1.160     raeburn  7662: }
                   7663: 
                   7664: sub build_search_response {
1.221     raeburn  7665:     my ($context,$srch,%srch_results) = @_;
1.179     raeburn  7666:     my ($currstate,$response,$forcenewuser);
1.160     raeburn  7667:     my %names = (
1.330     bisitz   7668:           'uname'     => 'username',
                   7669:           'lastname'  => 'last name',
1.160     raeburn  7670:           'lastfirst' => 'last name, first name',
1.330     bisitz   7671:           'crs'       => 'this course',
                   7672:           'dom'       => 'LON-CAPA domain',
                   7673:           'instd'     => 'the institutional directory for domain',
1.160     raeburn  7674:     );
                   7675: 
                   7676:     my %single = (
1.180     raeburn  7677:                    begins   => 'A match',
1.160     raeburn  7678:                    contains => 'A match',
1.180     raeburn  7679:                    exact    => 'An exact match',
1.160     raeburn  7680:                  );
                   7681:     my %nomatch = (
1.180     raeburn  7682:                    begins   => 'No match',
1.160     raeburn  7683:                    contains => 'No match',
1.180     raeburn  7684:                    exact    => 'No exact match',
1.160     raeburn  7685:                   );
                   7686:     if (keys(%srch_results) > 1) {
1.179     raeburn  7687:         $currstate = 'select';
1.160     raeburn  7688:     } else {
                   7689:         if (keys(%srch_results) == 1) {
1.416     raeburn  7690:             if ($env{'form.action'} eq 'accesslogs') {
                   7691:                 $currstate = 'activity';
                   7692:             } else {
                   7693:                 $currstate = 'modify';
                   7694:             }
1.180     raeburn  7695:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
                   7696:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   7697:                 $response .= ': '.&display_domain_info($srch->{'srchdomain'});
1.180     raeburn  7698:             }
1.330     bisitz   7699:         } else { # Search has nothing found. Prepare message to user.
                   7700:             $response = '<span class="LC_warning">';
1.180     raeburn  7701:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   7702:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}: [_2]",
                   7703:                                  '<b>'.$srch->{'srchterm'}.'</b>',
                   7704:                                  &display_domain_info($srch->{'srchdomain'}));
                   7705:             } else {
                   7706:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}.",
                   7707:                                  '<b>'.$srch->{'srchterm'}.'</b>');
1.180     raeburn  7708:             }
                   7709:             $response .= '</span>';
1.330     bisitz   7710: 
1.160     raeburn  7711:             if ($srch->{'srchin'} ne 'alc') {
                   7712:                 $forcenewuser = 1;
                   7713:                 my $cansrchinst = 0; 
                   7714:                 if ($srch->{'srchdomain'}) {
                   7715:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
                   7716:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
                   7717:                         if ($domconfig{'directorysrch'}{'available'}) {
                   7718:                             $cansrchinst = 1;
                   7719:                         } 
                   7720:                     }
                   7721:                 }
1.180     raeburn  7722:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
                   7723:                      ($srch->{'srchby'} eq 'lastname')) &&
                   7724:                     ($srch->{'srchin'} eq 'dom')) {
                   7725:                     if ($cansrchinst) {
                   7726:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
1.160     raeburn  7727:                     }
                   7728:                 }
1.180     raeburn  7729:                 if ($srch->{'srchin'} eq 'crs') {
                   7730:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
                   7731:                 }
                   7732:             }
1.305     raeburn  7733:             my $createdom = $env{'request.role.domain'};
                   7734:             if ($context eq 'requestcrs') {
                   7735:                 if ($env{'form.coursedom'} ne '') {
                   7736:                     $createdom = $env{'form.coursedom'};
                   7737:                 }
                   7738:             }
1.416     raeburn  7739:             unless (($env{'form.action'} eq 'accesslogs') || (($srch->{'srchby'} eq 'uname') && ($srch->{'srchin'} eq 'dom') &&
                   7740:                     ($srch->{'srchtype'} eq 'exact') && ($srch->{'srchdomain'} eq $createdom))) {
1.221     raeburn  7741:                 my $cancreate =
1.305     raeburn  7742:                     &Apache::lonuserutils::can_create_user($createdom,$context);
                   7743:                 my $targetdom = '<span class="LC_cusr_emph">'.$createdom.'</span>';
1.221     raeburn  7744:                 if ($cancreate) {
1.305     raeburn  7745:                     my $showdom = &display_domain_info($createdom); 
1.266     bisitz   7746:                     $response .= '<br /><br />'
                   7747:                                 .'<b>'.&mt('To add a new user:').'</b>'
1.305     raeburn  7748:                                 .'<br />';
                   7749:                     if ($context eq 'requestcrs') {
                   7750:                         $response .= &mt("(You can only define new users in the new course's domain - [_1])",$targetdom);
                   7751:                     } else {
                   7752:                         $response .= &mt("(You can only create new users in your current role's domain - [_1])",$targetdom);
                   7753:                     }
                   7754:                     $response .='<ul><li>'
1.266     bisitz   7755:                                 .&mt("Set 'Domain/institution to search' to: [_1]",'<span class="LC_cusr_emph">'.$showdom.'</span>')
                   7756:                                 .'</li><li>'
                   7757:                                 .&mt("Set 'Search criteria' to: [_1]username is ..... in selected LON-CAPA domain[_2]",'<span class="LC_cusr_emph">','</span>')
                   7758:                                 .'</li><li>'
                   7759:                                 .&mt('Provide the proposed username')
                   7760:                                 .'</li><li>'
                   7761:                                 .&mt("Click 'Search'")
                   7762:                                 .'</li></ul><br />';
1.221     raeburn  7763:                 } else {
                   7764:                     my $helplink = ' href="javascript:helpMenu('."'display'".')"';
1.305     raeburn  7765:                     $response .= '<br /><br />';
                   7766:                     if ($context eq 'requestcrs') {
1.314     raeburn  7767:                         $response .= &mt("You are not authorized to define new users in the new course's domain - [_1].",$targetdom);
1.305     raeburn  7768:                     } else {
                   7769:                         $response .= &mt("You are not authorized to create new users in your current role's domain - [_1].",$targetdom);
                   7770:                     }
                   7771:                     $response .= '<br />'
                   7772:                                  .&mt('Please contact the [_1]helpdesk[_2] if you need to create a new user.'
1.266     bisitz   7773:                                     ,' <a'.$helplink.'>'
                   7774:                                     ,'</a>')
1.415     raeburn  7775:                                  .'<br />';
1.221     raeburn  7776:                 }
1.160     raeburn  7777:             }
                   7778:         }
                   7779:     }
1.179     raeburn  7780:     return ($currstate,$response,$forcenewuser);
1.160     raeburn  7781: }
                   7782: 
1.180     raeburn  7783: sub display_domain_info {
                   7784:     my ($dom) = @_;
                   7785:     my $output = $dom;
                   7786:     if ($dom ne '') { 
                   7787:         my $domdesc = &Apache::lonnet::domain($dom,'description');
                   7788:         if ($domdesc ne '') {
                   7789:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
                   7790:         }
                   7791:     }
                   7792:     return $output;
                   7793: }
                   7794: 
1.160     raeburn  7795: sub crumb_utilities {
                   7796:     my %elements = (
                   7797:        crtuser => {
                   7798:            srchterm => 'text',
1.172     raeburn  7799:            srchin => 'selectbox',
1.160     raeburn  7800:            srchby => 'selectbox',
                   7801:            srchtype => 'selectbox',
                   7802:            srchdomain => 'selectbox',
                   7803:        },
1.207     raeburn  7804:        crtusername => {
                   7805:            srchterm => 'text',
                   7806:            srchdomain => 'selectbox',
                   7807:        },
1.160     raeburn  7808:        docustom => {
                   7809:            rolename => 'selectbox',
                   7810:            newrolename => 'textbox',
                   7811:        },
1.179     raeburn  7812:        studentform => {
                   7813:            srchterm => 'text',
                   7814:            srchin => 'selectbox',
                   7815:            srchby => 'selectbox',
                   7816:            srchtype => 'selectbox',
                   7817:            srchdomain => 'selectbox',
                   7818:        },
1.160     raeburn  7819:     );
                   7820: 
                   7821:     my $jsback .= qq|
                   7822: function backPage(formname,prevphase,prevstate) {
1.211     raeburn  7823:     if (typeof prevphase == 'undefined') {
                   7824:         formname.phase.value = '';
                   7825:     }
                   7826:     else {  
                   7827:         formname.phase.value = prevphase;
                   7828:     }
                   7829:     if (typeof prevstate == 'undefined') {
                   7830:         formname.currstate.value = '';
                   7831:     }
                   7832:     else {
                   7833:         formname.currstate.value = prevstate;
                   7834:     }
1.160     raeburn  7835:     formname.submit();
                   7836: }
                   7837: |;
                   7838:     return ($jsback,\%elements);
                   7839: }
                   7840: 
1.26      matthew  7841: sub course_level_table {
1.375     raeburn  7842:     my ($inccourses,$showcredits,$defaultcredits) = @_;
                   7843:     return unless (ref($inccourses) eq 'HASH');
1.26      matthew  7844:     my $table = '';
1.62      www      7845: # Custom Roles?
                   7846: 
1.190     raeburn  7847:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.89      raeburn  7848:     my %lt=&Apache::lonlocal::texthash(
                   7849:             'exs'  => "Existing sections",
                   7850:             'new'  => "Define new section",
                   7851:             'ssd'  => "Set Start Date",
                   7852:             'sed'  => "Set End Date",
1.131     raeburn  7853:             'crl'  => "Course Level",
1.89      raeburn  7854:             'act'  => "Activate",
                   7855:             'rol'  => "Role",
                   7856:             'ext'  => "Extent",
1.113     raeburn  7857:             'grs'  => "Section",
1.375     raeburn  7858:             'crd'  => "Credits",
1.89      raeburn  7859:             'sta'  => "Start",
                   7860:             'end'  => "End"
                   7861:     );
1.62      www      7862: 
1.375     raeburn  7863:     foreach my $protectedcourse (sort(keys(%{$inccourses}))) {
1.135     raeburn  7864: 	my $thiscourse=$protectedcourse;
1.26      matthew  7865: 	$thiscourse=~s:_:/:g;
                   7866: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
1.365     raeburn  7867:         my $isowner = &Apache::lonuserutils::is_courseowner($protectedcourse,$coursedata{'internal.courseowner'});
1.26      matthew  7868: 	my $area=$coursedata{'description'};
1.321     raeburn  7869:         my $crstype=$coursedata{'type'};
1.135     raeburn  7870: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
1.89      raeburn  7871: 	my ($domain,$cnum)=split(/\//,$thiscourse);
1.115     albertel 7872:         my %sections_count;
1.101     albertel 7873:         if (defined($env{'request.course.id'})) {
                   7874:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
1.115     albertel 7875:                 %sections_count = 
                   7876: 		    &Apache::loncommon::get_sections($domain,$cnum);
1.92      raeburn  7877:             }
                   7878:         }
1.321     raeburn  7879:         my @roles = &Apache::lonuserutils::roles_by_context('course','',$crstype);
1.213     raeburn  7880: 	foreach my $role (@roles) {
1.321     raeburn  7881:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.329     raeburn  7882: 	    if ((&Apache::lonnet::allowed('c'.$role,$thiscourse)) ||
                   7883:                 ((($role eq 'cc') || ($role eq 'co')) && ($isowner))) {
1.221     raeburn  7884:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  7885:                                             $plrole,\%sections_count,\%lt,
1.402     raeburn  7886:                                             $showcredits,$defaultcredits,$crstype);
1.221     raeburn  7887:             } elsif ($env{'request.course.sec'} ne '') {
                   7888:                 if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
                   7889:                                              $env{'request.course.sec'})) {
                   7890:                     $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  7891:                                                 $plrole,\%sections_count,\%lt,
1.402     raeburn  7892:                                                 $showcredits,$defaultcredits,$crstype);
1.26      matthew  7893:                 }
                   7894:             }
                   7895:         }
1.221     raeburn  7896:         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
1.324     raeburn  7897:             foreach my $cust (sort(keys(%customroles))) {
                   7898:                 next if ($crstype eq 'Community' && $customroles{$cust} =~ /bre\&S/);
1.221     raeburn  7899:                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
                   7900:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.402     raeburn  7901:                                             $cust,\%sections_count,\%lt,
                   7902:                                             $showcredits,$defaultcredits,$crstype);
1.221     raeburn  7903:             }
1.62      www      7904: 	}
1.26      matthew  7905:     }
                   7906:     return '' if ($table eq ''); # return nothing if there is nothing 
                   7907:                                  # in the table
1.188     raeburn  7908:     my $result;
                   7909:     if (!$env{'request.course.id'}) {
                   7910:         $result = '<h4>'.$lt{'crl'}.'</h4>'."\n";
                   7911:     }
                   7912:     $result .= 
1.136     raeburn  7913: &Apache::loncommon::start_data_table().
                   7914: &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  7915: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
1.402     raeburn  7916: '<th>'.$lt{'ext'}.'</th><th>'."\n";
                   7917:     if ($showcredits) {
                   7918:         $result .= $lt{'crd'}.'</th>';
                   7919:     }
                   7920:     $result .=
1.375     raeburn  7921: '<th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th>'."\n".
                   7922: '<th>'.$lt{'end'}.'</th>'.
1.136     raeburn  7923: &Apache::loncommon::end_data_table_header_row().
                   7924: $table.
                   7925: &Apache::loncommon::end_data_table();
1.26      matthew  7926:     return $result;
                   7927: }
1.88      raeburn  7928: 
1.221     raeburn  7929: sub course_level_row {
1.375     raeburn  7930:     my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,
1.402     raeburn  7931:         $lt,$showcredits,$defaultcredits,$crstype) = @_;
1.375     raeburn  7932:     my $creditem;
1.222     raeburn  7933:     my $row = &Apache::loncommon::start_data_table_row().
                   7934:               ' <td><input type="checkbox" name="act_'.
                   7935:               $protectedcourse.'_'.$role.'" /></td>'."\n".
                   7936:               ' <td>'.$plrole.'</td>'."\n".
                   7937:               ' <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
1.402     raeburn  7938:     if (($showcredits) && ($role eq 'st') && ($crstype eq 'Course')) {
1.375     raeburn  7939:         $row .= 
                   7940:             '<td><input type="text" name="credits_'.$protectedcourse.'_'.
                   7941:             $role.'" size="3" value="'.$defaultcredits.'" /></td>';
                   7942:     } else {
                   7943:         $row .= '<td>&nbsp;</td>';
                   7944:     }
1.322     raeburn  7945:     if (($role eq 'cc') || ($role eq 'co')) {
1.222     raeburn  7946:         $row .= '<td>&nbsp;</td>';
1.221     raeburn  7947:     } elsif ($env{'request.course.sec'} ne '') {
1.222     raeburn  7948:         $row .= ' <td><input type="hidden" value="'.
                   7949:                 $env{'request.course.sec'}.'" '.
                   7950:                 'name="sec_'.$protectedcourse.'_'.$role.'" />'.
                   7951:                 $env{'request.course.sec'}.'</td>';
1.221     raeburn  7952:     } else {
                   7953:         if (ref($sections_count) eq 'HASH') {
                   7954:             my $currsec = 
                   7955:                 &Apache::lonuserutils::course_sections($sections_count,
                   7956:                                                        $protectedcourse.'_'.$role);
1.222     raeburn  7957:             $row .= '<td><table class="LC_createuser">'."\n".
                   7958:                     '<tr class="LC_section_row">'."\n".
                   7959:                     ' <td valign="top">'.$lt->{'exs'}.'<br />'.
                   7960:                        $currsec.'</td>'."\n".
                   7961:                      ' <td>&nbsp;&nbsp;</td>'."\n".
                   7962:                      ' <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
1.221     raeburn  7963:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
                   7964:                      '" value="" />'.
                   7965:                      '<input type="hidden" '.
                   7966:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
1.222     raeburn  7967:                      '</tr></table></td>'."\n";
1.221     raeburn  7968:         } else {
1.222     raeburn  7969:             $row .= '<td><input type="text" size="10" '.
1.375     raeburn  7970:                     'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n";
1.221     raeburn  7971:         }
                   7972:     }
1.222     raeburn  7973:     $row .= <<ENDTIMEENTRY;
                   7974: <td><input type="hidden" name="start_$protectedcourse\_$role" value="" />
1.221     raeburn  7975: <a href=
                   7976: "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  7977: <td><input type="hidden" name="end_$protectedcourse\_$role" value="" />
1.221     raeburn  7978: <a href=
                   7979: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
                   7980: ENDTIMEENTRY
1.222     raeburn  7981:     $row .= &Apache::loncommon::end_data_table_row();
                   7982:     return $row;
1.221     raeburn  7983: }
                   7984: 
1.88      raeburn  7985: sub course_level_dc {
1.375     raeburn  7986:     my ($dcdom,$showcredits) = @_;
1.190     raeburn  7987:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.213     raeburn  7988:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.88      raeburn  7989:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
                   7990:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
1.133     raeburn  7991:                       '<input type="hidden" name="dccourse" value="" />';
1.355     www      7992:     my $courseform=&Apache::loncommon::selectcourse_link
1.356     raeburn  7993:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Select','crstype');
1.375     raeburn  7994:     my $credit_elem;
                   7995:     if ($showcredits) {
                   7996:         $credit_elem = 'credits';
                   7997:     }
                   7998:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu','role','Course/Community Browser',$credit_elem);
1.88      raeburn  7999:     my %lt=&Apache::lonlocal::texthash(
                   8000:                     'rol'  => "Role",
1.113     raeburn  8001:                     'grs'  => "Section",
1.88      raeburn  8002:                     'exs'  => "Existing sections",
                   8003:                     'new'  => "Define new section", 
                   8004:                     'sta'  => "Start",
                   8005:                     'end'  => "End",
                   8006:                     'ssd'  => "Set Start Date",
1.355     www      8007:                     'sed'  => "Set End Date",
1.375     raeburn  8008:                     'scc'  => "Course/Community",
                   8009:                     'crd'  => "Credits",
1.88      raeburn  8010:                   );
1.323     raeburn  8011:     my $header = '<h4>'.&mt('Course/Community Level').'</h4>'.
1.136     raeburn  8012:                  &Apache::loncommon::start_data_table().
                   8013:                  &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  8014:                  '<th>'.$lt{'scc'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
1.397     bisitz   8015:                  '<th>'.$lt{'grs'}.'</th>'."\n";
                   8016:     $header .=   '<th>'.$lt{'crd'}.'</th>'."\n" if ($showcredits);
                   8017:     $header .=   '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'."\n".
1.136     raeburn  8018:                  &Apache::loncommon::end_data_table_header_row();
1.143     raeburn  8019:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
1.356     raeburn  8020:                      '<td><br /><span class="LC_nobreak"><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc','','','','crstype'".')" />'.
                   8021:                      $courseform.('&nbsp;' x4).'</span></td>'."\n".
1.389     bisitz   8022:                      '<td valign="top"><br /><select name="role">'."\n";
1.213     raeburn  8023:     foreach my $role (@roles) {
1.135     raeburn  8024:         my $plrole=&Apache::lonnet::plaintext($role);
1.389     bisitz   8025:         $otheritems .= '  <option value="'.$role.'">'.$plrole.'</option>';
1.88      raeburn  8026:     }
1.404     raeburn  8027:     if ( keys(%customroles) > 0) {
                   8028:         foreach my $cust (sort(keys(%customroles))) {
1.101     albertel 8029:             my $custrole='cr_cr_'.$env{'user.domain'}.
1.135     raeburn  8030:                     '_'.$env{'user.name'}.'_'.$cust;
1.389     bisitz   8031:             $otheritems .= '  <option value="'.$custrole.'">'.$cust.'</option>';
1.88      raeburn  8032:         }
                   8033:     }
                   8034:     $otheritems .= '</select></td><td>'.
                   8035:                      '<table border="0" cellspacing="0" cellpadding="0">'.
                   8036:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
1.389     bisitz   8037:                      ' <option value="">&lt;--'.&mt('Pick course first').'</option></select></td>'.
1.88      raeburn  8038:                      '<td>&nbsp;&nbsp;</td>'.
                   8039:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
1.113     raeburn  8040:                      '<input type="text" name="newsec" value="" />'.
1.237     raeburn  8041:                      '<input type="hidden" name="section" value="" />'.
1.323     raeburn  8042:                      '<input type="hidden" name="groups" value="" />'.
                   8043:                      '<input type="hidden" name="crstype" value="" /></td>'.
1.375     raeburn  8044:                      '</tr></table></td>'."\n";
                   8045:     if ($showcredits) {
                   8046:         $otheritems .= '<td><br />'."\n".
1.397     bisitz   8047:                        '<input type="text" size="3" name="credits" value="" /></td>'."\n";
1.375     raeburn  8048:     }
1.88      raeburn  8049:     $otheritems .= <<ENDTIMEENTRY;
1.323     raeburn  8050: <td><br /><input type="hidden" name="start" value='' />
1.88      raeburn  8051: <a href=
                   8052: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.323     raeburn  8053: <td><br /><input type="hidden" name="end" value='' />
1.88      raeburn  8054: <a href=
                   8055: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
                   8056: ENDTIMEENTRY
1.136     raeburn  8057:     $otheritems .= &Apache::loncommon::end_data_table_row().
                   8058:                    &Apache::loncommon::end_data_table()."\n";
1.88      raeburn  8059:     return $cb_jscript.$header.$hiddenitems.$otheritems;
                   8060: }
                   8061: 
1.237     raeburn  8062: sub update_selfenroll_config {
1.400     raeburn  8063:     my ($r,$cid,$cdom,$cnum,$context,$crstype,$currsettings) = @_;
1.398     raeburn  8064:     return unless (ref($currsettings) eq 'HASH');
                   8065:     my ($row,$lt) = &Apache::lonuserutils::get_selfenroll_titles();
                   8066:     my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
1.237     raeburn  8067:     my (%changes,%warning);
1.241     raeburn  8068:     my $curr_types;
1.400     raeburn  8069:     my %noedit;
                   8070:     unless ($context eq 'domain') {
                   8071:         %noedit = &get_noedit_fields($cdom,$cnum,$crstype,$row);
                   8072:     }
1.237     raeburn  8073:     if (ref($row) eq 'ARRAY') {
                   8074:         foreach my $item (@{$row}) {
1.400     raeburn  8075:             next if ($noedit{$item});
1.237     raeburn  8076:             if ($item eq 'enroll_dates') {
                   8077:                 my (%currenrolldate,%newenrolldate);
                   8078:                 foreach my $type ('start','end') {
1.398     raeburn  8079:                     $currenrolldate{$type} = $currsettings->{'selfenroll_'.$type.'_date'};
1.237     raeburn  8080:                     $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
                   8081:                     if ($newenrolldate{$type} ne $currenrolldate{$type}) {
                   8082:                         $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
                   8083:                     }
                   8084:                 }
                   8085:             } elsif ($item eq 'access_dates') {
                   8086:                 my (%currdate,%newdate);
                   8087:                 foreach my $type ('start','end') {
1.398     raeburn  8088:                     $currdate{$type} = $currsettings->{'selfenroll_'.$type.'_access'};
1.237     raeburn  8089:                     $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
                   8090:                     if ($newdate{$type} ne $currdate{$type}) {
                   8091:                         $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
                   8092:                     }
                   8093:                 }
1.241     raeburn  8094:             } elsif ($item eq 'types') {
1.398     raeburn  8095:                 $curr_types = $currsettings->{'selfenroll_'.$item};
1.241     raeburn  8096:                 if ($env{'form.selfenroll_all'}) {
                   8097:                     if ($curr_types ne '*') {
                   8098:                         $changes{'internal.selfenroll_types'} = '*';
                   8099:                     } else {
                   8100:                         next;
                   8101:                     }
                   8102:                 } else {
1.249     raeburn  8103:                     my %currdoms;
1.241     raeburn  8104:                     my @entries = split(/;/,$curr_types);
                   8105:                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
1.249     raeburn  8106:                     my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
1.241     raeburn  8107:                     my $newnum = 0;
1.249     raeburn  8108:                     my @latesttypes;
                   8109:                     foreach my $num (@activations) {
                   8110:                         my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
                   8111:                         if (@types > 0) {
1.241     raeburn  8112:                             @types = sort(@types);
                   8113:                             my $typestr = join(',',@types);
1.249     raeburn  8114:                             my $typedom = $env{'form.selfenroll_dom_'.$num};
                   8115:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   8116:                             $currdoms{$typedom} = 1;
1.241     raeburn  8117:                             $newnum ++;
                   8118:                         }
                   8119:                     }
1.338     raeburn  8120:                     for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {
                   8121:                         if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
1.249     raeburn  8122:                             my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
                   8123:                             if (@types > 0) {
                   8124:                                 @types = sort(@types);
                   8125:                                 my $typestr = join(',',@types);
                   8126:                                 my $typedom = $env{'form.selfenroll_dom_'.$j};
                   8127:                                 $latesttypes[$newnum] = $typedom.':'.$typestr;
                   8128:                                 $currdoms{$typedom} = 1;
                   8129:                                 $newnum ++;
                   8130:                             }
                   8131:                         }
                   8132:                     }
                   8133:                     if ($env{'form.selfenroll_newdom'} ne '') {
                   8134:                         my $typedom = $env{'form.selfenroll_newdom'};
                   8135:                         if ((!defined($currdoms{$typedom})) && 
                   8136:                             (&Apache::lonnet::domain($typedom) ne '')) {
                   8137:                             my $typestr;
                   8138:                             my ($othertitle,$usertypes,$types) = 
                   8139:                                 &Apache::loncommon::sorted_inst_types($typedom);
                   8140:                             my $othervalue = 'any';
                   8141:                             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   8142:                                 if (@{$types} > 0) {
1.257     raeburn  8143:                                     my @esc_types = map { &escape($_); } @{$types};
1.249     raeburn  8144:                                     $othervalue = 'other';
1.258     raeburn  8145:                                     $typestr = join(',',(@esc_types,$othervalue));
1.249     raeburn  8146:                                 }
                   8147:                                 $typestr = $othervalue;
                   8148:                             } else {
                   8149:                                 $typestr = $othervalue;
                   8150:                             } 
                   8151:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   8152:                             $newnum ++ ;
                   8153:                         }
                   8154:                     }
1.241     raeburn  8155:                     my $selfenroll_types = join(';',@latesttypes);
                   8156:                     if ($selfenroll_types ne $curr_types) {
                   8157:                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
                   8158:                     }
                   8159:                 }
1.276     raeburn  8160:             } elsif ($item eq 'limit') {
                   8161:                 my $newlimit = $env{'form.selfenroll_limit'};
                   8162:                 my $newcap = $env{'form.selfenroll_cap'};
                   8163:                 $newcap =~s/\s+//g;
1.398     raeburn  8164:                 my $currlimit =  $currsettings->{'selfenroll_limit'};
1.276     raeburn  8165:                 $currlimit = 'none' if ($currlimit eq '');
1.398     raeburn  8166:                 my $currcap = $currsettings->{'selfenroll_cap'};
1.276     raeburn  8167:                 if ($newlimit ne $currlimit) {
                   8168:                     if ($newlimit ne 'none') {
                   8169:                         if ($newcap =~ /^\d+$/) {
                   8170:                             if ($newcap ne $currcap) {
                   8171:                                 $changes{'internal.selfenroll_cap'} = $newcap;
                   8172:                             }
                   8173:                             $changes{'internal.selfenroll_limit'} = $newlimit;
                   8174:                         } else {
1.398     raeburn  8175:                             $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
                   8176:                                 &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.'); 
1.276     raeburn  8177:                         }
                   8178:                     } elsif ($currcap ne '') {
                   8179:                         $changes{'internal.selfenroll_cap'} = '';
                   8180:                         $changes{'internal.selfenroll_limit'} = $newlimit; 
                   8181:                     }
                   8182:                 } elsif ($currlimit ne 'none') {
                   8183:                     if ($newcap =~ /^\d+$/) {
                   8184:                         if ($newcap ne $currcap) {
                   8185:                             $changes{'internal.selfenroll_cap'} = $newcap;
                   8186:                         }
                   8187:                     } else {
1.398     raeburn  8188:                         $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.
                   8189:                             &mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.');
1.276     raeburn  8190:                     }
                   8191:                 }
                   8192:             } elsif ($item eq 'approval') {
                   8193:                 my (@currnotified,@newnotified);
1.398     raeburn  8194:                 my $currapproval = $currsettings->{'selfenroll_approval'};
                   8195:                 my $currnotifylist = $currsettings->{'selfenroll_notifylist'};
1.276     raeburn  8196:                 if ($currnotifylist ne '') {
                   8197:                     @currnotified = split(/,/,$currnotifylist);
                   8198:                     @currnotified = sort(@currnotified);
                   8199:                 }
                   8200:                 my $newapproval = $env{'form.selfenroll_approval'};
                   8201:                 @newnotified = &Apache::loncommon::get_env_multiple('form.selfenroll_notify');
                   8202:                 @newnotified = sort(@newnotified);
                   8203:                 if ($newapproval ne $currapproval) {
                   8204:                     $changes{'internal.selfenroll_approval'} = $newapproval;
                   8205:                     if (!$newapproval) {
                   8206:                         if ($currnotifylist ne '') {
                   8207:                             $changes{'internal.selfenroll_notifylist'} = '';
                   8208:                         }
                   8209:                     } else {
                   8210:                         my @differences =  
1.295     raeburn  8211:                             &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  8212:                         if (@differences > 0) {
                   8213:                             if (@newnotified > 0) {
                   8214:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   8215:                             } else {
                   8216:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   8217:                             }
                   8218:                         }
                   8219:                     }
                   8220:                 } else {
1.295     raeburn  8221:                     my @differences = &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  8222:                     if (@differences > 0) {
                   8223:                         if (@newnotified > 0) {
                   8224:                             $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   8225:                         } else {
                   8226:                             $changes{'internal.selfenroll_notifylist'} = '';
                   8227:                         }
                   8228:                     }
                   8229:                 }
1.237     raeburn  8230:             } else {
1.398     raeburn  8231:                 my $curr_val = $currsettings->{'selfenroll_'.$item};
1.237     raeburn  8232:                 my $newval = $env{'form.selfenroll_'.$item};
                   8233:                 if ($item eq 'section') {
                   8234:                     $newval = $env{'form.sections'};
1.241     raeburn  8235:                     if (defined($curr_groups{$newval})) {
1.237     raeburn  8236:                         $newval = $curr_val;
1.398     raeburn  8237:                         $warning{$item} = &mt('Section for self-enrolled users unchanged as the proposed section is a group').'<br />'.
                   8238:                                           &mt('Group names and section names must be distinct');
1.237     raeburn  8239:                     } elsif ($newval eq 'all') {
                   8240:                         $newval = $curr_val;
1.274     bisitz   8241:                         $warning{$item} = &mt('Section for self-enrolled users unchanged, as "all" is a reserved section name.');
1.237     raeburn  8242:                     }
                   8243:                     if ($newval eq '') {
                   8244:                         $newval = 'none';
                   8245:                     }
                   8246:                 }
                   8247:                 if ($newval ne $curr_val) {
                   8248:                     $changes{'internal.selfenroll_'.$item} = $newval;
                   8249:                 }
1.241     raeburn  8250:             }
1.237     raeburn  8251:         }
                   8252:         if (keys(%warning) > 0) {
                   8253:             foreach my $item (@{$row}) {
                   8254:                 if (exists($warning{$item})) {
                   8255:                     $r->print($warning{$item}.'<br />');
                   8256:                 }
                   8257:             } 
                   8258:         }
                   8259:         if (keys(%changes) > 0) {
                   8260:             my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
                   8261:             if ($putresult eq 'ok') {
                   8262:                 if ((exists($changes{'internal.selfenroll_types'})) ||
                   8263:                     (exists($changes{'internal.selfenroll_start_date'}))  ||
                   8264:                     (exists($changes{'internal.selfenroll_end_date'}))) {
                   8265:                     my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
                   8266:                                                                 $cnum,undef,undef,'Course');
                   8267:                     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
1.398     raeburn  8268:                     if (ref($crsinfo{$cid}) eq 'HASH') {
1.237     raeburn  8269:                         foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
                   8270:                             if (exists($changes{'internal.'.$item})) {
1.398     raeburn  8271:                                 $crsinfo{$cid}{$item} = $changes{'internal.'.$item};
1.237     raeburn  8272:                             }
                   8273:                         }
                   8274:                         my $crsputresult =
                   8275:                             &Apache::lonnet::courseidput($cdom,\%crsinfo,
                   8276:                                                          $chome,'notime');
                   8277:                     }
                   8278:                 }
                   8279:                 $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
                   8280:                 foreach my $item (@{$row}) {
                   8281:                     my $title = $item;
                   8282:                     if (ref($lt) eq 'HASH') {
                   8283:                         $title = $lt->{$item};
                   8284:                     }
                   8285:                     if ($item eq 'enroll_dates') {
                   8286:                         foreach my $type ('start','end') {
                   8287:                             if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
                   8288:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
1.244     bisitz   8289:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  8290:                                           $title,$type,$newdate).'</li>');
                   8291:                             }
                   8292:                         }
                   8293:                     } elsif ($item eq 'access_dates') {
                   8294:                         foreach my $type ('start','end') {
                   8295:                             if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
                   8296:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
1.244     bisitz   8297:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  8298:                                           $title,$type,$newdate).'</li>');
                   8299:                             }
                   8300:                         }
1.276     raeburn  8301:                     } elsif ($item eq 'limit') {
                   8302:                         if ((exists($changes{'internal.selfenroll_limit'})) ||
                   8303:                             (exists($changes{'internal.selfenroll_cap'}))) {
                   8304:                             my ($newval,$newcap);
                   8305:                             if ($changes{'internal.selfenroll_cap'} ne '') {
                   8306:                                 $newcap = $changes{'internal.selfenroll_cap'}
                   8307:                             } else {
1.398     raeburn  8308:                                 $newcap = $currsettings->{'selfenroll_cap'};
1.276     raeburn  8309:                             }
                   8310:                             if ($changes{'internal.selfenroll_limit'} eq 'none') {
                   8311:                                 $newval = &mt('No limit');
                   8312:                             } elsif ($changes{'internal.selfenroll_limit'} eq 
                   8313:                                      'allstudents') {
                   8314:                                 $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   8315:                             } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
                   8316:                                 $newval = &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
                   8317:                             } else {
1.398     raeburn  8318:                                 my $currlimit =  $currsettings->{'selfenroll_limit'};
1.276     raeburn  8319:                                 if ($currlimit eq 'allstudents') {
                   8320:                                     $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   8321:                                 } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
1.308     raeburn  8322:                                     $newval =  &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
1.276     raeburn  8323:                                 }
                   8324:                             }
                   8325:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
                   8326:                         }
                   8327:                     } elsif ($item eq 'approval') {
                   8328:                         if ((exists($changes{'internal.selfenroll_approval'})) ||
                   8329:                             (exists($changes{'internal.selfenroll_notifylist'}))) {
1.398     raeburn  8330:                             my %selfdescs = &Apache::lonuserutils::selfenroll_default_descs();
1.276     raeburn  8331:                             my ($newval,$newnotify);
                   8332:                             if (exists($changes{'internal.selfenroll_notifylist'})) {
                   8333:                                 $newnotify = $changes{'internal.selfenroll_notifylist'};
                   8334:                             } else {   
1.398     raeburn  8335:                                 $newnotify = $currsettings->{'selfenroll_notifylist'};
1.276     raeburn  8336:                             }
1.398     raeburn  8337:                             if (exists($changes{'internal.selfenroll_approval'})) {
                   8338:                                 if ($changes{'internal.selfenroll_approval'} !~ /^[012]$/) {
                   8339:                                     $changes{'internal.selfenroll_approval'} = '0';
                   8340:                                 }
                   8341:                                 $newval = $selfdescs{'approval'}{$changes{'internal.selfenroll_approval'}};
1.276     raeburn  8342:                             } else {
1.398     raeburn  8343:                                 my $currapproval = $currsettings->{'selfenroll_approval'}; 
                   8344:                                 if ($currapproval !~ /^[012]$/) {
                   8345:                                     $currapproval = 0;
1.276     raeburn  8346:                                 }
1.398     raeburn  8347:                                 $newval = $selfdescs{'approval'}{$currapproval};
1.276     raeburn  8348:                             }
                   8349:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval));
                   8350:                             if ($newnotify) {
1.277     raeburn  8351:                                 $r->print('<br />'.&mt('The following will be notified when an enrollment request needs approval, or has been approved: [_1].',$newnotify));
1.276     raeburn  8352:                             } else {
1.277     raeburn  8353:                                 $r->print('<br />'.&mt('No notifications sent when an enrollment request needs approval, or has been approved.'));
1.276     raeburn  8354:                             }
                   8355:                             $r->print('</li>'."\n");
                   8356:                         }
1.237     raeburn  8357:                     } else {
                   8358:                         if (exists($changes{'internal.selfenroll_'.$item})) {
1.241     raeburn  8359:                             my $newval = $changes{'internal.selfenroll_'.$item};
                   8360:                             if ($item eq 'types') {
                   8361:                                 if ($newval eq '') {
                   8362:                                     $newval = &mt('None');
                   8363:                                 } elsif ($newval eq '*') {
                   8364:                                     $newval = &mt('Any user in any domain');
                   8365:                                 }
1.245     raeburn  8366:                             } elsif ($item eq 'registered') {
                   8367:                                 if ($newval eq '1') {
                   8368:                                     $newval = &mt('Yes');
                   8369:                                 } elsif ($newval eq '0') {
                   8370:                                     $newval = &mt('No');
                   8371:                                 }
1.241     raeburn  8372:                             }
1.244     bisitz   8373:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
1.237     raeburn  8374:                         }
                   8375:                     }
                   8376:                 }
                   8377:                 $r->print('</ul>');
1.398     raeburn  8378:                 if ($env{'course.'.$cid.'.description'} ne '') {
                   8379:                     my %newenvhash;
                   8380:                     foreach my $key (keys(%changes)) {
                   8381:                         $newenvhash{'course.'.$cid.'.'.$key} = $changes{$key};
                   8382:                     }
                   8383:                     &Apache::lonnet::appenv(\%newenvhash);
1.237     raeburn  8384:                 }
                   8385:             } else {
1.398     raeburn  8386:                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.
                   8387:                           &mt('The error was: [_1].',$putresult));
1.237     raeburn  8388:             }
                   8389:         } else {
1.249     raeburn  8390:             $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.237     raeburn  8391:         }
                   8392:     } else {
1.249     raeburn  8393:         $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.241     raeburn  8394:     }
1.400     raeburn  8395:     my $visactions = &cat_visibility();
                   8396:     my ($cathash,%cattype);
                   8397:     my %domconfig = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   8398:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   8399:         $cathash = $domconfig{'coursecategories'}{'cats'};
                   8400:         $cattype{'auth'} = $domconfig{'coursecategories'}{'auth'};
                   8401:         $cattype{'unauth'} = $domconfig{'coursecategories'}{'unauth'};
                   8402:     } else {
                   8403:         $cathash = {};
                   8404:         $cattype{'auth'} = 'std';
                   8405:         $cattype{'unauth'} = 'std';
                   8406:     }
                   8407:     if (($cattype{'auth'} eq 'none') && ($cattype{'unauth'} eq 'none')) {
                   8408:         $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   8409:                   '<br />'.
                   8410:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   8411:                   '<li>'.$visactions->{'dc_chgconf'}.'</li>'.
                   8412:                   '</ul>');
                   8413:     } elsif (($cattype{'auth'} !~ /^(std|domonly)$/) && ($cattype{'unauth'} !~ /^(std|domonly)$/)) {
                   8414:         if ($currsettings->{'uniquecode'}) {
                   8415:             $r->print('<span class="LC_info">'.$visactions->{'vis'}.'</span>');
                   8416:         } else {
1.366     bisitz   8417:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
1.400     raeburn  8418:                   '<br />'.
                   8419:                   '<br />'.$visactions->{'take'}.'<ul>'.
                   8420:                   '<li>'.$visactions->{'dc_setcode'}.'</li>'.
                   8421:                   '</ul><br />');
                   8422:         }
                   8423:     } else {
                   8424:         my ($visible,$cansetvis,$vismsgs) = &visible_in_stdcat($cdom,$cnum,\%domconfig);
                   8425:         if (ref($visactions) eq 'HASH') {
                   8426:             if (!$visible) {
                   8427:                 $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
                   8428:                           '<br />');
                   8429:                 if (ref($vismsgs) eq 'ARRAY') {
                   8430:                     $r->print('<br />'.$visactions->{'take'}.'<ul>');
                   8431:                     foreach my $item (@{$vismsgs}) {
                   8432:                         $r->print('<li>'.$visactions->{$item}.'</li>');
                   8433:                     }
                   8434:                     $r->print('</ul>');
1.256     raeburn  8435:                 }
1.400     raeburn  8436:                 $r->print($cansetvis);
1.256     raeburn  8437:             }
                   8438:         }
                   8439:     } 
1.237     raeburn  8440:     return;
                   8441: }
                   8442: 
1.27      matthew  8443: #---------------------------------------------- end functions for &phase_two
1.29      matthew  8444: 
                   8445: #--------------------------------- functions for &phase_two and &phase_three
                   8446: 
                   8447: #--------------------------end of functions for &phase_two and &phase_three
1.372     raeburn  8448: 
1.1       www      8449: 1;
                   8450: __END__
1.2       www      8451: 
                   8452: 

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