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

1.20      harris41    1: # The LearningOnline Network with CAPA
1.1       www         2: # Create a user
                      3: #
1.386   ! bisitz      4: # $Id: loncreateuser.pm,v 1.385 2014/01/08 17:18:11 bisitz 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:                    'cuqu'      => "Current quota",
                    130:                    'cust'      => "Custom quota",
                    131:                    'defa'      => "Default",
                    132:                    'chqu'      => "Change quota",
1.134     raeburn   133:     );
1.378     raeburn   134:    
1.149     raeburn   135:     my $quota_javascript = <<"END_SCRIPT";
                    136: <script type="text/javascript">
1.301     bisitz    137: // <![CDATA[
1.378     raeburn   138: function quota_changes(caller,context) {
                    139:     var customoff = document.getElementById('custom_'+context+'quota_off');
                    140:     var customon = document.getElementById('custom_'+context+'quota_on');
                    141:     var number = document.getElementById(context+'quota');
1.149     raeburn   142:     if (caller == "custom") {
1.378     raeburn   143:         if (customoff) {
                    144:             if (customoff.checked) {
                    145:                 number.value = "";
                    146:             }
1.149     raeburn   147:         }
                    148:     }
                    149:     if (caller == "quota") {
1.378     raeburn   150:         if (customon) {
                    151:             customon.checked = true;
                    152:         }
1.149     raeburn   153:     }
1.378     raeburn   154:     return;
1.149     raeburn   155: }
1.301     bisitz    156: // ]]>
1.149     raeburn   157: </script>
                    158: END_SCRIPT
1.378     raeburn   159:     my $longinsttype;
                    160:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
1.267     raeburn   161:     my $output = $quota_javascript."\n".
                    162:                  '<h3>'.$lt{'usrt'}.'</h3>'."\n".
                    163:                  &Apache::loncommon::start_data_table();
                    164: 
                    165:     if (&Apache::lonnet::allowed('mut',$ccdomain)) {
1.275     raeburn   166:         $output .= &build_tools_display($ccuname,$ccdomain,'tools');
1.267     raeburn   167:     }
1.378     raeburn   168: 
                    169:     my %titles = &Apache::lonlocal::texthash (
                    170:                     portfolio => "Disk space allocated to user's portfolio files",
1.385     bisitz    171:                     author    => "Disk space allocated to user's Authoring Space (if role assigned)",
1.378     raeburn   172:                  );
                    173:     foreach my $name ('portfolio','author') {
                    174:         my ($currquota,$quotatype,$inststatus,$defquota) =
                    175:             &Apache::loncommon::get_user_quota($ccuname,$ccdomain,$name);
                    176:         if ($longinsttype eq '') { 
                    177:             if ($inststatus ne '') {
                    178:                 if ($usertypes->{$inststatus} ne '') {
                    179:                     $longinsttype = $usertypes->{$inststatus};
                    180:                 }
                    181:             }
                    182:         }
                    183:         my ($showquota,$custom_on,$custom_off,$defaultinfo);
                    184:         $custom_on = ' ';
                    185:         $custom_off = ' checked="checked" ';
                    186:         if ($quotatype eq 'custom') {
                    187:             $custom_on = $custom_off;
                    188:             $custom_off = ' ';
                    189:             $showquota = $currquota;
                    190:             if ($longinsttype eq '') {
                    191:                 $defaultinfo = &mt('For this user, the default quota would be [_1]'
1.383     raeburn   192:                               .' MB.',$defquota);
1.378     raeburn   193:             } else {
                    194:                 $defaultinfo = &mt("For this user, the default quota would be [_1]".
1.383     raeburn   195:                                    " MB, as determined by the user's institutional".
1.378     raeburn   196:                                    " affiliation ([_2]).",$defquota,$longinsttype);
                    197:             }
                    198:         } else {
                    199:             if ($longinsttype eq '') {
                    200:                 $defaultinfo = &mt('For this user, the default quota is [_1]'
1.383     raeburn   201:                               .' MB.',$defquota);
1.378     raeburn   202:             } else {
                    203:                 $defaultinfo = &mt("For this user, the default quota of [_1]".
1.383     raeburn   204:                                    " MB, is determined by the user's institutional".
1.378     raeburn   205:                                    " affiliation ([_2]).",$defquota,$longinsttype);
                    206:             }
                    207:         }
                    208: 
                    209:         if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
                    210:             $output .= '<tr class="LC_info_row">'."\n".
                    211:                        '    <td>'.$titles{$name}.'</td>'."\n".
                    212:                        '  </tr>'."\n".
                    213:                        &Apache::loncommon::start_data_table_row()."\n".
                    214:                        '  <td>'.$lt{'cuqu'}.': '.
1.383     raeburn   215:                        $currquota.'&nbsp;MB.&nbsp;&nbsp;'.
1.378     raeburn   216:                        $defaultinfo.'</td>'."\n".
                    217:                        &Apache::loncommon::end_data_table_row()."\n".
                    218:                        &Apache::loncommon::start_data_table_row()."\n".
                    219:                        '  <td><span class="LC_nobreak">'.$lt{'chqu'}.
                    220:                        ': <label>'.
                    221:                        '<input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_off" '.
1.379     raeburn   222:                        'value="0" '.$custom_off.' onchange="javascript:quota_changes('."'custom','$name'".');"'.
1.383     raeburn   223:                        ' />'.$lt{'defa'}.'&nbsp;('.$defquota.' MB).</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.383     raeburn   229:                        ' />&nbsp;MB</span></td>'."\n".
1.378     raeburn   230:                        &Apache::loncommon::end_data_table_row()."\n";
                    231:         }
                    232:     }
1.267     raeburn   233:     $output .= &Apache::loncommon::end_data_table();
1.134     raeburn   234:     return $output;
                    235: }
                    236: 
1.275     raeburn   237: sub build_tools_display {
                    238:     my ($ccuname,$ccdomain,$context) = @_;
1.306     raeburn   239:     my (@usertools,%userenv,$output,@options,%validations,%reqtitles,%reqdisplay,
1.332     raeburn   240:         $colspan,$isadv,%domconfig);
1.275     raeburn   241:     my %lt = &Apache::lonlocal::texthash (
                    242:                    'blog'       => "Personal User Blog",
                    243:                    'aboutme'    => "Personal Information Page",
1.385     bisitz    244:                    'webdav'     => "WebDAV access to Authoring Spaces (if SSL and author/co-author)",
1.275     raeburn   245:                    'portfolio'  => "Personal User Portfolio",
                    246:                    'avai'       => "Available",
                    247:                    'cusa'       => "availability",
                    248:                    'chse'       => "Change setting",
                    249:                    'usde'       => "Use default",
                    250:                    'uscu'       => "Use custom",
                    251:                    'official'   => 'Can request creation of official courses',
1.299     raeburn   252:                    'unofficial' => 'Can request creation of unofficial courses',
                    253:                    'community'  => 'Can request creation of communities',
1.384     raeburn   254:                    'textbook'   => 'Can request creation of textbook courses',
1.362     raeburn   255:                    'requestauthor'  => 'Can request author space',
1.275     raeburn   256:     );
1.279     raeburn   257:     if ($context eq 'requestcourses') {
1.275     raeburn   258:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.299     raeburn   259:                       'requestcourses.official','requestcourses.unofficial',
1.384     raeburn   260:                       'requestcourses.community','requestcourses.textbook');
                    261:         @usertools = ('official','unofficial','community','textbook');
1.309     raeburn   262:         @options =('norequest','approval','autolimit','validate');
1.306     raeburn   263:         %validations = &Apache::lonnet::auto_courserequest_checks($ccdomain);
                    264:         %reqtitles = &courserequest_titles();
                    265:         %reqdisplay = &courserequest_display();
                    266:         $colspan = ' colspan="2"';
1.332     raeburn   267:         %domconfig =
                    268:             &Apache::lonnet::get_dom('configuration',['requestcourses'],$ccdomain);
                    269:         $isadv = &Apache::lonnet::is_advanced_user($ccuname,$ccdomain);
1.362     raeburn   270:     } elsif ($context eq 'requestauthor') {
                    271:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    272:                                                     'requestauthor');
                    273:         @usertools = ('requestauthor');
                    274:         @options =('norequest','approval','automatic');
                    275:         %reqtitles = &requestauthor_titles();
                    276:         %reqdisplay = &requestauthor_display();
                    277:         $colspan = ' colspan="2"';
                    278:         %domconfig =
                    279:             &Apache::lonnet::get_dom('configuration',['requestauthor'],$ccdomain);
1.275     raeburn   280:     } else {
                    281:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.361     raeburn   282:                           'tools.aboutme','tools.portfolio','tools.blog',
                    283:                           'tools.webdav');
                    284:         @usertools = ('aboutme','blog','webdav','portfolio');
1.275     raeburn   285:     }
                    286:     foreach my $item (@usertools) {
1.306     raeburn   287:         my ($custom_access,$curr_access,$cust_on,$cust_off,$tool_on,$tool_off,
                    288:             $currdisp,$custdisp,$custradio);
1.275     raeburn   289:         $cust_off = 'checked="checked" ';
                    290:         $tool_on = 'checked="checked" ';
                    291:         $curr_access =  
                    292:             &Apache::lonnet::usertools_access($ccuname,$ccdomain,$item,undef,
                    293:                                               $context);
1.362     raeburn   294:         if ($context eq 'requestauthor') {
                    295:             if ($userenv{$context} ne '') {
                    296:                 $cust_on = ' checked="checked" ';
                    297:                 $cust_off = '';
                    298:             }  
                    299:         } elsif ($userenv{$context.'.'.$item} ne '') {
1.306     raeburn   300:             $cust_on = ' checked="checked" ';
                    301:             $cust_off = '';
                    302:         }
                    303:         if ($context eq 'requestcourses') {
                    304:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   305:                 $custom_access = &mt('Currently from default setting.');
1.306     raeburn   306:             } else {
                    307:                 $custom_access = &mt('Currently from custom setting.');
1.275     raeburn   308:             }
1.362     raeburn   309:         } elsif ($context eq 'requestauthor') {
                    310:             if ($userenv{$context} eq '') {
                    311:                 $custom_access = &mt('Currently from default setting.');
                    312:             } else {
                    313:                 $custom_access = &mt('Currently from custom setting.');
                    314:             }
1.275     raeburn   315:         } else {
1.306     raeburn   316:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   317:                 $custom_access =
1.306     raeburn   318:                     &mt('Availability determined currently from default setting.');
                    319:                 if (!$curr_access) {
                    320:                     $tool_off = 'checked="checked" ';
                    321:                     $tool_on = '';
                    322:                 }
                    323:             } else {
1.314     raeburn   324:                 $custom_access =
1.306     raeburn   325:                     &mt('Availability determined currently from custom setting.');
                    326:                 if ($userenv{$context.'.'.$item} == 0) {
                    327:                     $tool_off = 'checked="checked" ';
                    328:                     $tool_on = '';
                    329:                 }
1.275     raeburn   330:             }
                    331:         }
                    332:         $output .= '  <tr class="LC_info_row">'."\n".
1.306     raeburn   333:                    '   <td'.$colspan.'>'.$lt{$item}.'</td>'."\n".
1.275     raeburn   334:                    '  </tr>'."\n".
1.306     raeburn   335:                    &Apache::loncommon::start_data_table_row()."\n";
1.362     raeburn   336:         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.306     raeburn   337:             my ($curroption,$currlimit);
1.362     raeburn   338:             my $envkey = $context.'.'.$item;
                    339:             if ($context eq 'requestauthor') {
                    340:                 $envkey = $context;
                    341:             }
                    342:             if ($userenv{$envkey} ne '') {
                    343:                 $curroption = $userenv{$envkey};
1.332     raeburn   344:             } else {
                    345:                 my (@inststatuses);
1.362     raeburn   346:                 if ($context eq 'requestcourses') {
                    347:                     $curroption =
                    348:                         &Apache::loncoursequeueadmin::get_processtype('course',$ccuname,$ccdomain,
                    349:                                                                       $isadv,$ccdomain,$item,
                    350:                                                                       \@inststatuses,\%domconfig);
                    351:                 } else {
                    352:                      $curroption = 
                    353:                          &Apache::loncoursequeueadmin::get_processtype('requestauthor',$ccuname,$ccdomain,
                    354:                                                                        $isadv,$ccdomain,undef,
                    355:                                                                        \@inststatuses,\%domconfig);
                    356:                 }
1.332     raeburn   357:             }
1.306     raeburn   358:             if (!$curroption) {
                    359:                 $curroption = 'norequest';
                    360:             }
                    361:             if ($curroption =~ /^autolimit=(\d*)$/) {
                    362:                 $currlimit = $1;
1.314     raeburn   363:                 if ($currlimit eq '') {
                    364:                     $currdisp = &mt('Yes, automatic creation');
                    365:                 } else {
                    366:                     $currdisp = &mt('Yes, up to [quant,_1,request]/user',$currlimit);
                    367:                 }
1.306     raeburn   368:             } else {
                    369:                 $currdisp = $reqdisplay{$curroption};
                    370:             }
                    371:             $custdisp = '<table>';
                    372:             foreach my $option (@options) {
                    373:                 my $val = $option;
                    374:                 if ($option eq 'norequest') {
                    375:                     $val = 0;
                    376:                 }
                    377:                 if ($option eq 'validate') {
                    378:                     my $canvalidate = 0;
                    379:                     if (ref($validations{$item}) eq 'HASH') {
                    380:                         if ($validations{$item}{'_custom_'}) {
                    381:                             $canvalidate = 1;
                    382:                         }
                    383:                     }
                    384:                     next if (!$canvalidate);
                    385:                 }
                    386:                 my $checked = '';
                    387:                 if ($option eq $curroption) {
                    388:                     $checked = ' checked="checked"';
                    389:                 } elsif ($option eq 'autolimit') {
                    390:                     if ($curroption =~ /^autolimit/) {
                    391:                         $checked = ' checked="checked"';
                    392:                     }
                    393:                 }
1.362     raeburn   394:                 my $name = 'crsreq_'.$item;
                    395:                 if ($context eq 'requestauthor') {
                    396:                     $name = $item;
                    397:                 }
1.306     raeburn   398:                 $custdisp .= '<tr><td><span class="LC_nobreak"><label>'.
1.362     raeburn   399:                              '<input type="radio" name="'.$name.'" '.
                    400:                              'value="'.$val.'"'.$checked.' />'.
1.306     raeburn   401:                              $reqtitles{$option}.'</label>&nbsp;';
                    402:                 if ($option eq 'autolimit') {
1.362     raeburn   403:                     $custdisp .= '<input type="text" name="'.$name.
                    404:                                  '_limit" size="1" '.
1.314     raeburn   405:                                  'value="'.$currlimit.'" /></span><br />'.
                    406:                                  $reqtitles{'unlimited'};
1.362     raeburn   407:                 } else {
                    408:                     $custdisp .= '</span>';
                    409:                 }
                    410:                 $custdisp .= '</td></tr>';
1.306     raeburn   411:             }
                    412:             $custdisp .= '</table>';
                    413:             $custradio = '</span></td><td>'.&mt('Custom setting').'<br />'.$custdisp;
                    414:         } else {
                    415:             $currdisp = ($curr_access?&mt('Yes'):&mt('No'));
1.362     raeburn   416:             my $name = $context.'_'.$item;
                    417:             if ($context eq 'requestauthor') {
                    418:                 $name = $context;
                    419:             }
1.306     raeburn   420:             $custdisp = '<span class="LC_nobreak"><label>'.
1.362     raeburn   421:                         '<input type="radio" name="'.$name.'"'.
1.361     raeburn   422:                         ' value="1" '.$tool_on.'/>'.&mt('On').'</label>&nbsp;<label>'.
1.362     raeburn   423:                         '<input type="radio" name="'.$name.'" value="0" '.
1.306     raeburn   424:                         $tool_off.'/>'.&mt('Off').'</label></span>';
                    425:             $custradio = ('&nbsp;'x2).'--'.$lt{'cusa'}.':&nbsp;'.$custdisp.
                    426:                           '</span>';
                    427:         }
                    428:         $output .= '  <td'.$colspan.'>'.$custom_access.('&nbsp;'x4).
                    429:                    $lt{'avai'}.': '.$currdisp.'</td>'."\n".
1.275     raeburn   430:                    &Apache::loncommon::end_data_table_row()."\n".
                    431:                    &Apache::loncommon::start_data_table_row()."\n".
1.306     raeburn   432:                    '  <td style="vertical-align:top;"><span class="LC_nobreak">'.
                    433:                    $lt{'chse'}.': <label>'.
1.275     raeburn   434:                    '<input type="radio" name="custom'.$item.'" value="0" '.
1.306     raeburn   435:                    $cust_off.'/>'.$lt{'usde'}.'</label>'.('&nbsp;' x3).
                    436:                    '<label><input type="radio" name="custom'.$item.'" value="1" '.
                    437:                    $cust_on.'/>'.$lt{'uscu'}.'</label>'.$custradio.'</td>'.
1.275     raeburn   438:                    &Apache::loncommon::end_data_table_row()."\n";
                    439:     }
                    440:     return $output;
                    441: }
                    442: 
1.300     raeburn   443: sub coursereq_externaluser {
                    444:     my ($ccuname,$ccdomain,$cdom) = @_;
1.306     raeburn   445:     my (@usertools,@options,%validations,%userenv,$output);
1.300     raeburn   446:     my %lt = &Apache::lonlocal::texthash (
                    447:                    'official'   => 'Can request creation of official courses',
                    448:                    'unofficial' => 'Can request creation of unofficial courses',
                    449:                    'community'  => 'Can request creation of communities',
1.384     raeburn   450:                    'textbook'   => 'Can request creation of textbook courses',
1.300     raeburn   451:     );
                    452: 
                    453:     %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    454:                       'reqcrsotherdom.official','reqcrsotherdom.unofficial',
1.384     raeburn   455:                       'reqcrsotherdom.community','reqcrsotherdom.textbook');
                    456:     @usertools = ('official','unofficial','community','textbook');
1.309     raeburn   457:     @options = ('approval','validate','autolimit');
1.306     raeburn   458:     %validations = &Apache::lonnet::auto_courserequest_checks($cdom);
                    459:     my $optregex = join('|',@options);
                    460:     my %reqtitles = &courserequest_titles();
1.300     raeburn   461:     foreach my $item (@usertools) {
1.306     raeburn   462:         my ($curroption,$currlimit,$tooloff);
1.300     raeburn   463:         if ($userenv{'reqcrsotherdom.'.$item} ne '') {
                    464:             my @curr = split(',',$userenv{'reqcrsotherdom.'.$item});
1.314     raeburn   465:             foreach my $req (@curr) {
                    466:                 if ($req =~ /^\Q$cdom\E\:($optregex)=?(\d*)$/) {
                    467:                     $curroption = $1;
                    468:                     $currlimit = $2;
                    469:                     last;
1.306     raeburn   470:                 }
                    471:             }
1.314     raeburn   472:             if (!$curroption) {
                    473:                 $curroption = 'norequest';
                    474:                 $tooloff = ' checked="checked"';
                    475:             }
1.306     raeburn   476:         } else {
                    477:             $curroption = 'norequest';
                    478:             $tooloff = ' checked="checked"';
                    479:         }
                    480:         $output.= &Apache::loncommon::start_data_table_row()."\n".
1.314     raeburn   481:                   '  <td><span class="LC_nobreak">'.$lt{$item}.': </span></td><td>'.
                    482:                   '<table><tr><td valign="top">'."\n".
1.306     raeburn   483:                   '<label><input type="radio" name="reqcrsotherdom_'.$item.
1.314     raeburn   484:                   '" value=""'.$tooloff.' />'.$reqtitles{'norequest'}.
                    485:                   '</label></td>';
1.306     raeburn   486:         foreach my $option (@options) {
                    487:             if ($option eq 'validate') {
                    488:                 my $canvalidate = 0;
                    489:                 if (ref($validations{$item}) eq 'HASH') {
                    490:                     if ($validations{$item}{'_external_'}) {
                    491:                         $canvalidate = 1;
                    492:                     }
                    493:                 }
                    494:                 next if (!$canvalidate);
                    495:             }
                    496:             my $checked = '';
                    497:             if ($option eq $curroption) {
                    498:                 $checked = ' checked="checked"';
                    499:             }
1.314     raeburn   500:             $output .= '<td valign="top"><span class="LC_nobreak"><label>'.
1.306     raeburn   501:                        '<input type="radio" name="reqcrsotherdom_'.$item.
                    502:                        '" value="'.$option.'"'.$checked.' />'.
1.314     raeburn   503:                        $reqtitles{$option}.'</label>';
1.306     raeburn   504:             if ($option eq 'autolimit') {
1.314     raeburn   505:                 $output .= '&nbsp;<input type="text" name="reqcrsotherdom_'.
1.306     raeburn   506:                            $item.'_limit" size="1" '.
1.314     raeburn   507:                            'value="'.$currlimit.'" /></span>'.
                    508:                            '<br />'.$reqtitles{'unlimited'};
                    509:             } else {
                    510:                 $output .= '</span>';
1.300     raeburn   511:             }
1.314     raeburn   512:             $output .= '</td>';
1.300     raeburn   513:         }
1.314     raeburn   514:         $output .= '</td></tr></table></td>'."\n".
1.300     raeburn   515:                    &Apache::loncommon::end_data_table_row()."\n";
                    516:     }
                    517:     return $output;
                    518: }
                    519: 
1.362     raeburn   520: sub domainrole_req {
                    521:     my ($ccuname,$ccdomain) = @_;
                    522:     return '<br /><h3>'.
                    523:            &mt('User Can Request Assignment of Domain Roles?').
                    524:            '</h3>'."\n".
                    525:            &Apache::loncommon::start_data_table().
                    526:            &build_tools_display($ccuname,$ccdomain,
                    527:                                 'requestauthor').
                    528:            &Apache::loncommon::end_data_table();
                    529: }
                    530: 
1.306     raeburn   531: sub courserequest_titles {
                    532:     my %titles = &Apache::lonlocal::texthash (
                    533:                                    official   => 'Official',
                    534:                                    unofficial => 'Unofficial',
                    535:                                    community  => 'Communities',
1.384     raeburn   536:                                    textbook   => 'Textbook',
1.306     raeburn   537:                                    norequest  => 'Not allowed',
1.309     raeburn   538:                                    approval   => 'Approval by Dom. Coord.',
1.306     raeburn   539:                                    validate   => 'With validation',
                    540:                                    autolimit  => 'Numerical limit',
1.314     raeburn   541:                                    unlimited  => '(blank for unlimited)',
1.306     raeburn   542:                  );
                    543:     return %titles;
                    544: }
                    545: 
                    546: sub courserequest_display {
                    547:     my %titles = &Apache::lonlocal::texthash (
1.309     raeburn   548:                                    approval   => 'Yes, need approval',
1.306     raeburn   549:                                    validate   => 'Yes, with validation',
                    550:                                    norequest  => 'No',
                    551:    );
                    552:    return %titles;
                    553: }
                    554: 
1.362     raeburn   555: sub requestauthor_titles {
                    556:     my %titles = &Apache::lonlocal::texthash (
                    557:                                    norequest  => 'Not allowed',
                    558:                                    approval   => 'Approval by Dom. Coord.',
                    559:                                    automatic  => 'Automatic approval',
                    560:                  );
                    561:     return %titles;
                    562: 
                    563: }
                    564: 
                    565: sub requestauthor_display {
                    566:     my %titles = &Apache::lonlocal::texthash (
                    567:                                    approval   => 'Yes, need approval',
                    568:                                    automatic  => 'Yes, automatic approval',
                    569:                                    norequest  => 'No',
                    570:    );
                    571:    return %titles;
                    572: }
                    573: 
1.383     raeburn   574: sub requestchange_display {
                    575:     my %titles = &Apache::lonlocal::texthash (
                    576:                                    approval   => "availability set to 'on' (approval required)", 
                    577:                                    automatic  => "availability set to 'on' (automatic approval)",
                    578:                                    norequest  => "availability set to 'off'",
                    579:    );
                    580:    return %titles;
                    581: }
                    582: 
1.362     raeburn   583: sub curr_requestauthor {
                    584:     my ($uname,$udom,$isadv,$inststatuses,$domconfig) = @_;
                    585:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
                    586:     if ($uname eq '' || $udom eq '') {
                    587:         $uname = $env{'user.name'};
                    588:         $udom = $env{'user.domain'};
                    589:         $isadv = $env{'user.adv'};
                    590:     }
                    591:     my (%userenv,%settings,$val);
                    592:     my @options = ('automatic','approval');
                    593:     %userenv =
                    594:         &Apache::lonnet::userenvironment($udom,$uname,'requestauthor','inststatus');
                    595:     if ($userenv{'requestauthor'}) {
                    596:         $val = $userenv{'requestauthor'};
                    597:         @{$inststatuses} = ('_custom_');
                    598:     } else {
                    599:         my %alltasks;
                    600:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') {
                    601:             %settings = %{$domconfig->{'requestauthor'}};
                    602:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
                    603:                 $val = $settings{'_LC_adv'};
                    604:                 @{$inststatuses} = ('_LC_adv_');
                    605:             } else {
                    606:                 if ($userenv{'inststatus'} ne '') {
                    607:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
                    608:                 } else {
                    609:                     @{$inststatuses} = ('default');
                    610:                 }
                    611:                 foreach my $status (@{$inststatuses}) {
                    612:                     if (exists($settings{$status})) {
                    613:                         my $value = $settings{$status};
                    614:                         next unless ($value);
                    615:                         unless (exists($alltasks{$value})) {
                    616:                             if (ref($alltasks{$value}) eq 'ARRAY') {
                    617:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
                    618:                                     push(@{$alltasks{$value}},$status);
                    619:                                 }
                    620:                             } else {
                    621:                                 @{$alltasks{$value}} = ($status);
                    622:                             }
                    623:                         }
                    624:                     }
                    625:                 }
                    626:                 foreach my $option (@options) {
                    627:                     if ($alltasks{$option}) {
                    628:                         $val = $option;
                    629:                         last;
                    630:                     }
                    631:                 }
                    632:             }
                    633:         }
                    634:     }
                    635:     return $val;
                    636: }
                    637: 
1.2       www       638: # =================================================================== Phase one
1.1       www       639: 
1.42      matthew   640: sub print_username_entry_form {
1.351     raeburn   641:     my ($r,$context,$response,$srch,$forcenewuser,$crstype,$brcrum) = @_;
1.101     albertel  642:     my $defdom=$env{'request.role.domain'};
1.160     raeburn   643:     my $formtoset = 'crtuser';
                    644:     if (exists($env{'form.startrolename'})) {
                    645:         $formtoset = 'docustom';
                    646:         $env{'form.rolename'} = $env{'form.startrolename'};
1.207     raeburn   647:     } elsif ($env{'form.origform'} eq 'crtusername') {
                    648:         $formtoset =  $env{'form.origform'};
1.160     raeburn   649:     }
                    650: 
                    651:     my ($jsback,$elements) = &crumb_utilities();
                    652: 
                    653:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
1.165     albertel  654:         '<script type="text/javascript">'."\n".
1.301     bisitz    655:         '// <![CDATA['."\n".
                    656:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset})."\n".
                    657:         '// ]]>'."\n".
1.162     raeburn   658:         '</script>'."\n";
1.160     raeburn   659: 
1.324     raeburn   660:     my %existingroles=&Apache::lonuserutils::my_custom_roles($crstype);
                    661:     if (($env{'form.action'} eq 'custom') && (keys(%existingroles) > 0)
                    662:         && (&Apache::lonnet::allowed('mcr','/'))) {
                    663:         $jscript .= &customrole_javascript();
                    664:     }
1.224     raeburn   665:     my $helpitem = 'Course_Change_Privileges';
                    666:     if ($env{'form.action'} eq 'custom') {
                    667:         $helpitem = 'Course_Editing_Custom_Roles';
                    668:     } elsif ($env{'form.action'} eq 'singlestudent') {
                    669:         $helpitem = 'Course_Add_Student';
                    670:     }
1.351     raeburn   671:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
                    672:     if ($env{'form.action'} eq 'custom') {
                    673:         push(@{$brcrum},
                    674:                  {href=>"javascript:backPage(document.crtuser)",       
                    675:                   text=>"Pick custom role",
                    676:                   help => $helpitem,}
                    677:                  );
                    678:     } else {
                    679:         push (@{$brcrum},
                    680:                   {href => "javascript:backPage(document.crtuser)",
                    681:                    text => $breadcrumb_text{'search'},
                    682:                    help => $helpitem,
                    683:                    faq  => 282,
                    684:                    bug  => 'Instructor Interface',}
                    685:                   );
                    686:     }
                    687:     my %loaditems = (
                    688:                 'onload' => "javascript:setFormElements(document.$formtoset)",
                    689:                     );
                    690:     my $args = {bread_crumbs           => $brcrum,
                    691:                 bread_crumbs_component => 'User Management',
                    692:                 add_entries            => \%loaditems,};
                    693:     $r->print(&Apache::loncommon::start_page('User Management',$jscript,$args));
                    694: 
1.71      sakharuk  695:     my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   696:                     'srst' => 'Search for a user and enroll as a student',
1.318     raeburn   697:                     'srme' => 'Search for a user and enroll as a member',
1.229     raeburn   698:                     'srad' => 'Search for a user and modify/add user information or roles',
1.71      sakharuk  699: 		    'usr'  => "Username",
                    700:                     'dom'  => "Domain",
1.324     raeburn   701:                     'ecrp' => "Define or Edit Custom Role",
                    702:                     'nr'   => "role name",
1.282     schafran  703:                     'cre'  => "Next",
1.71      sakharuk  704: 				       );
1.351     raeburn   705: 
1.214     raeburn   706:     if ($env{'form.action'} eq 'custom') {
1.190     raeburn   707:         if (&Apache::lonnet::allowed('mcr','/')) {
1.324     raeburn   708:             my $newroletext = &mt('Define new custom role:');
                    709:             $r->print('<form action="/adm/createuser" method="post" name="docustom">'.
                    710:                       '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                    711:                       '<input type="hidden" name="phase" value="selected_custom_edit" />'.
                    712:                       '<h3>'.$lt{'ecrp'}.'</h3>'.
                    713:                       &Apache::loncommon::start_data_table().
                    714:                       &Apache::loncommon::start_data_table_row().
                    715:                       '<td>');
                    716:             if (keys(%existingroles) > 0) {
                    717:                 $r->print('<br /><label><input type="radio" name="customroleaction" value="new" checked="checked" onclick="setCustomFields();" /><b>'.$newroletext.'</b></label>');
                    718:             } else {
                    719:                 $r->print('<br /><input type="hidden" name="customroleaction" value="new" /><b>'.$newroletext.'</b>');
                    720:             }
                    721:             $r->print('</td><td align="center">'.$lt{'nr'}.'<br /><input type="text" size="15" name="newrolename" onfocus="setCustomAction('."'new'".');" /></td>'.
                    722:                       &Apache::loncommon::end_data_table_row());
                    723:             if (keys(%existingroles) > 0) {
                    724:                 $r->print(&Apache::loncommon::start_data_table_row().'<td><br />'.
                    725:                           '<label><input type="radio" name="customroleaction" value="edit" onclick="setCustomFields();"/><b>'.
                    726:                           &mt('View/Modify existing role:').'</b></label></td>'.
                    727:                           '<td align="center"><br />'.
                    728:                           '<select name="rolename" onchange="setCustomAction('."'edit'".');">'.
1.326     raeburn   729:                           '<option value="" selected="selected">'.
1.324     raeburn   730:                           &mt('Select'));
                    731:                 foreach my $role (sort(keys(%existingroles))) {
1.326     raeburn   732:                     $r->print('<option value="'.$role.'">'.$role.'</option>');
1.324     raeburn   733:                 }
                    734:                 $r->print('</select>'.
                    735:                           '</td>'.
                    736:                           &Apache::loncommon::end_data_table_row());
                    737:             }
                    738:             $r->print(&Apache::loncommon::end_data_table().'<p>'.
                    739:                       '<input name="customeditor" type="submit" value="'.
                    740:                       $lt{'cre'}.'" /></p>'.
                    741:                       '</form>');
1.190     raeburn   742:         }
1.213     raeburn   743:     } else {
1.229     raeburn   744:         my $actiontext = $lt{'srad'};
1.213     raeburn   745:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   746:             if ($crstype eq 'Community') {
                    747:                 $actiontext = $lt{'srme'};
                    748:             } else {
                    749:                 $actiontext = $lt{'srst'};
                    750:             }
1.213     raeburn   751:         }
1.324     raeburn   752:         $r->print("<h3>$actiontext</h3>");
1.213     raeburn   753:         if ($env{'form.origform'} ne 'crtusername') {
                    754:             $r->print("\n".$response);
                    755:         }
1.318     raeburn   756:         $r->print(&entry_form($defdom,$srch,$forcenewuser,$context,$response,$crstype));
1.107     www       757:     }
1.110     albertel  758: }
                    759: 
1.324     raeburn   760: sub customrole_javascript {
                    761:     my $js = <<"END";
                    762: <script type="text/javascript">
                    763: // <![CDATA[
                    764: 
                    765: function setCustomFields() {
                    766:     if (document.docustom.customroleaction.length > 0) {
                    767:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    768:             if (document.docustom.customroleaction[i].checked) {
                    769:                 if (document.docustom.customroleaction[i].value == 'new') {
                    770:                     document.docustom.rolename.selectedIndex = 0;
                    771:                 } else {
                    772:                     document.docustom.newrolename.value = '';
                    773:                 }
                    774:             }
                    775:         }
                    776:     }
                    777:     return;
                    778: }
                    779: 
                    780: function setCustomAction(caller) {
                    781:     if (document.docustom.customroleaction.length > 0) {
                    782:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    783:             if (document.docustom.customroleaction[i].value == caller) {
                    784:                 document.docustom.customroleaction[i].checked = true;
                    785:             }
                    786:         }
                    787:     }
                    788:     setCustomFields();
                    789:     return;
                    790: }
                    791: 
                    792: // ]]>
                    793: </script>
                    794: END
                    795:     return $js;
                    796: }
                    797: 
1.160     raeburn   798: sub entry_form {
1.318     raeburn   799:     my ($dom,$srch,$forcenewuser,$context,$responsemsg,$crstype) = @_;
1.229     raeburn   800:     my ($usertype,$inexact);
1.214     raeburn   801:     if (ref($srch) eq 'HASH') {
                    802:         if (($srch->{'srchin'} eq 'dom') &&
                    803:             ($srch->{'srchby'} eq 'uname') &&
                    804:             ($srch->{'srchtype'} eq 'exact') &&
                    805:             ($srch->{'srchdomain'} ne '') &&
                    806:             ($srch->{'srchterm'} ne '')) {
1.353     raeburn   807:             my (%curr_rules,%got_rules);
1.214     raeburn   808:             my ($rules,$ruleorder) =
                    809:                 &Apache::lonnet::inst_userrules($srch->{'srchdomain'},'username');
1.353     raeburn   810:             $usertype = &Apache::lonuserutils::check_usertype($srch->{'srchdomain'},$srch->{'srchterm'},$rules,\%curr_rules,\%got_rules);
1.229     raeburn   811:         } else {
                    812:             $inexact = 1;
1.214     raeburn   813:         }
1.207     raeburn   814:     }
1.214     raeburn   815:     my $cancreate =
                    816:         &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
1.160     raeburn   817:     my $userpicker = 
1.179     raeburn   818:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
1.214     raeburn   819:                                        'document.crtuser',$cancreate,$usertype);
1.160     raeburn   820:     my $srchbutton = &mt('Search');
1.229     raeburn   821:     if ($env{'form.action'} eq 'singlestudent') {
                    822:         $srchbutton = &mt('Search and Enroll');
                    823:     } elsif ($cancreate && $responsemsg ne '' && $inexact) {
                    824:         $srchbutton = &mt('Search or Add New User');
                    825:     }
1.207     raeburn   826:     my $output = <<"ENDBLOCK";
1.160     raeburn   827: <form action="/adm/createuser" method="post" name="crtuser">
1.190     raeburn   828: <input type="hidden" name="action" value="$env{'form.action'}" />
1.160     raeburn   829: <input type="hidden" name="phase" value="get_user_info" />
                    830: $userpicker
1.179     raeburn   831: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
1.160     raeburn   832: </form>
1.207     raeburn   833: ENDBLOCK
1.229     raeburn   834:     if ($env{'form.phase'} eq '') {
1.207     raeburn   835:         my $defdom=$env{'request.role.domain'};
                    836:         my $domform = &Apache::loncommon::select_dom_form($defdom,'srchdomain');
                    837:         my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   838:                   'enro' => 'Enroll one student',
1.318     raeburn   839:                   'enrm' => 'Enroll one member',
1.229     raeburn   840:                   'admo' => 'Add/modify a single user',
                    841:                   'crea' => 'create new user if required',
                    842:                   'uskn' => "username is known",
1.207     raeburn   843:                   'crnu' => 'Create a new user',
                    844:                   'usr'  => 'Username',
                    845:                   'dom'  => 'in domain',
1.229     raeburn   846:                   'enrl' => 'Enroll',
                    847:                   'cram'  => 'Create/Modify user',
1.207     raeburn   848:         );
1.229     raeburn   849:         my $sellink=&Apache::loncommon::selectstudent_link('crtusername','srchterm','srchdomain');
                    850:         my ($title,$buttontext,$showresponse);
1.318     raeburn   851:         if ($env{'form.action'} eq 'singlestudent') {
                    852:             if ($crstype eq 'Community') {
                    853:                 $title = $lt{'enrm'};
                    854:             } else {
                    855:                 $title = $lt{'enro'};
                    856:             }
1.229     raeburn   857:             $buttontext = $lt{'enrl'};
                    858:         } else {
                    859:             $title = $lt{'admo'};
                    860:             $buttontext = $lt{'cram'};
                    861:         }
                    862:         if ($cancreate) {
                    863:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'crea'}.')</span>';
                    864:         } else {
                    865:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'uskn'}.')</span>';
                    866:         }
                    867:         if ($env{'form.origform'} eq 'crtusername') {
                    868:             $showresponse = $responsemsg;
                    869:         }
1.207     raeburn   870:         $output .= <<"ENDDOCUMENT";
1.229     raeburn   871: <br />
1.207     raeburn   872: <form action="/adm/createuser" method="post" name="crtusername">
                    873: <input type="hidden" name="action" value="$env{'form.action'}" />
                    874: <input type="hidden" name="phase" value="createnewuser" />
                    875: <input type="hidden" name="srchtype" value="exact" />
1.233     raeburn   876: <input type="hidden" name="srchby" value="uname" />
1.207     raeburn   877: <input type="hidden" name="srchin" value="dom" />
                    878: <input type="hidden" name="forcenewuser" value="1" />
                    879: <input type="hidden" name="origform" value="crtusername" />
1.229     raeburn   880: <h3>$title</h3>
                    881: $showresponse
1.207     raeburn   882: <table>
                    883:  <tr>
                    884:   <td>$lt{'usr'}:</td>
                    885:   <td><input type="text" size="15" name="srchterm" /></td>
                    886:   <td>&nbsp;$lt{'dom'}:</td><td>$domform</td>
1.229     raeburn   887:   <td>&nbsp;$sellink&nbsp;</td>
                    888:   <td>&nbsp;<input name="userrole" type="submit" value="$buttontext" /></td>
1.207     raeburn   889:  </tr>
                    890: </table>
                    891: </form>
1.160     raeburn   892: ENDDOCUMENT
1.207     raeburn   893:     }
1.160     raeburn   894:     return $output;
                    895: }
1.110     albertel  896: 
                    897: sub user_modification_js {
1.113     raeburn   898:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
                    899:     
1.110     albertel  900:     return <<END;
                    901: <script type="text/javascript" language="Javascript">
1.301     bisitz    902: // <![CDATA[
1.314     raeburn   903: 
1.110     albertel  904:     $pjump_def
                    905:     $dc_setcourse_code
                    906: 
                    907:     function dateset() {
                    908:         eval("document.cu."+document.cu.pres_marker.value+
                    909:             ".value=document.cu.pres_value.value");
1.359     www       910:         modalWindow.close();
1.110     albertel  911:     }
                    912: 
1.113     raeburn   913:     $nondc_setsection_code
1.301     bisitz    914: // ]]>
1.110     albertel  915: </script>
                    916: END
1.2       www       917: }
                    918: 
                    919: # =================================================================== Phase two
1.160     raeburn   920: sub print_user_selection_page {
1.351     raeburn   921:     my ($r,$response,$srch,$srch_results,$srcharray,$context,$opener_elements,$crstype,$brcrum) = @_;
1.160     raeburn   922:     my @fields = ('username','domain','lastname','firstname','permanentemail');
                    923:     my $sortby = $env{'form.sortby'};
                    924: 
                    925:     if (!grep(/^\Q$sortby\E$/,@fields)) {
                    926:         $sortby = 'lastname';
                    927:     }
                    928: 
                    929:     my ($jsback,$elements) = &crumb_utilities();
                    930: 
                    931:     my $jscript = (<<ENDSCRIPT);
                    932: <script type="text/javascript">
1.301     bisitz    933: // <![CDATA[
1.160     raeburn   934: function pickuser(uname,udom) {
                    935:     document.usersrchform.seluname.value=uname;
                    936:     document.usersrchform.seludom.value=udom;
                    937:     document.usersrchform.phase.value="userpicked";
                    938:     document.usersrchform.submit();
                    939: }
                    940: 
                    941: $jsback
1.301     bisitz    942: // ]]>
1.160     raeburn   943: </script>
                    944: ENDSCRIPT
                    945: 
                    946:     my %lt=&Apache::lonlocal::texthash(
1.179     raeburn   947:                                        'usrch'          => "User Search to add/modify roles",
                    948:                                        'stusrch'        => "User Search to enroll student",
1.318     raeburn   949:                                        'memsrch'        => "User Search to enroll member",
1.179     raeburn   950:                                        'usel'           => "Select a user to add/modify roles",
1.318     raeburn   951:                                        'stusel'         => "Select a user to enroll as a student",
                    952:                                        'memsel'         => "Select a user to enroll as a member",
1.160     raeburn   953:                                        'username'       => "username",
                    954:                                        'domain'         => "domain",
                    955:                                        'lastname'       => "last name",
                    956:                                        'firstname'      => "first name",
                    957:                                        'permanentemail' => "permanent e-mail",
                    958:                                       );
1.302     raeburn   959:     if ($context eq 'requestcrs') {
                    960:         $r->print('<div>');
                    961:     } else {
1.318     raeburn   962:         my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.351     raeburn   963:         my $helpitem;
                    964:         if ($env{'form.action'} eq 'singleuser') {
                    965:             $helpitem = 'Course_Change_Privileges';
                    966:         } elsif ($env{'form.action'} eq 'singlestudent') {
                    967:             $helpitem = 'Course_Add_Student';
                    968:         }
                    969:         push (@{$brcrum},
                    970:                   {href => "javascript:backPage(document.usersrchform,'','')",
                    971:                    text => $breadcrumb_text{'search'},
                    972:                    faq  => 282,
                    973:                    bug  => 'Instructor Interface',},
                    974:                   {href => "javascript:backPage(document.usersrchform,'get_user_info','select')",
                    975:                    text => $breadcrumb_text{'userpicked'},
                    976:                    faq  => 282,
                    977:                    bug  => 'Instructor Interface',
                    978:                    help => $helpitem}
                    979:                   );
                    980:         $r->print(&Apache::loncommon::start_page('User Management',$jscript,{bread_crumbs => $brcrum}));
1.302     raeburn   981:         if ($env{'form.action'} eq 'singleuser') {
                    982:             $r->print("<b>$lt{'usrch'}</b><br />");
1.318     raeburn   983:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
1.302     raeburn   984:             $r->print('<h3>'.$lt{'usel'}.'</h3>');
                    985:         } elsif ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   986:             $r->print($jscript."<b>");
                    987:             if ($crstype eq 'Community') {
                    988:                 $r->print($lt{'memsrch'});
                    989:             } else {
                    990:                 $r->print($lt{'stusrch'});
                    991:             }
                    992:             $r->print("</b><br />");
                    993:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
                    994:             $r->print('</form><h3>');
                    995:             if ($crstype eq 'Community') {
                    996:                 $r->print($lt{'memsel'});
                    997:             } else {
                    998:                 $r->print($lt{'stusel'});
                    999:             }
                   1000:             $r->print('</h3>');
1.302     raeburn  1001:         }
1.179     raeburn  1002:     }
1.380     bisitz   1003:     $r->print('<form name="usersrchform" method="post" action="">'.
1.160     raeburn  1004:               &Apache::loncommon::start_data_table()."\n".
                   1005:               &Apache::loncommon::start_data_table_header_row()."\n".
                   1006:               ' <th> </th>'."\n");
                   1007:     foreach my $field (@fields) {
                   1008:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
                   1009:                   "'".$field."'".';document.usersrchform.submit();">'.
                   1010:                   $lt{$field}.'</a></th>'."\n");
                   1011:     }
                   1012:     $r->print(&Apache::loncommon::end_data_table_header_row());
                   1013: 
                   1014:     my @sorted_users = sort {
1.167     albertel 1015:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
1.160     raeburn  1016:             ||
1.167     albertel 1017:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
1.160     raeburn  1018:             ||
                   1019:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
1.167     albertel 1020: 	    ||
                   1021: 	lc($a) cmp lc($b)
1.160     raeburn  1022:         } (keys(%$srch_results));
                   1023: 
                   1024:     foreach my $user (@sorted_users) {
                   1025:         my ($uname,$udom) = split(/:/,$user);
1.302     raeburn  1026:         my $onclick;
                   1027:         if ($context eq 'requestcrs') {
1.314     raeburn  1028:             $onclick =
1.302     raeburn  1029:                 'onclick="javascript:gochoose('."'$uname','$udom',".
                   1030:                                                "'$srch_results->{$user}->{firstname}',".
                   1031:                                                "'$srch_results->{$user}->{lastname}',".
                   1032:                                                "'$srch_results->{$user}->{permanentemail}'".');"';
                   1033:         } else {
1.314     raeburn  1034:             $onclick =
1.302     raeburn  1035:                 ' onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".');"';
                   1036:         }
1.160     raeburn  1037:         $r->print(&Apache::loncommon::start_data_table_row().
1.302     raeburn  1038:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" '.
                   1039:                   $onclick.' /></td>'.
1.160     raeburn  1040:                   '<td><tt>'.$uname.'</tt></td>'.
                   1041:                   '<td><tt>'.$udom.'</tt></td>');
                   1042:         foreach my $field ('lastname','firstname','permanentemail') {
                   1043:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
                   1044:         }
                   1045:         $r->print(&Apache::loncommon::end_data_table_row());
                   1046:     }
                   1047:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
1.179     raeburn  1048:     if (ref($srcharray) eq 'ARRAY') {
                   1049:         foreach my $item (@{$srcharray}) {
                   1050:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
                   1051:         }
                   1052:     }
1.160     raeburn  1053:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
                   1054:               ' <input type="hidden" name="seluname" value="" />'."\n".
                   1055:               ' <input type="hidden" name="seludom" value="" />'."\n".
1.179     raeburn  1056:               ' <input type="hidden" name="currstate" value="select" />'."\n".
1.190     raeburn  1057:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
1.214     raeburn  1058:               ' <input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
1.302     raeburn  1059:     if ($context eq 'requestcrs') {
                   1060:         $r->print($opener_elements.'</form></div>');
                   1061:     } else {
1.351     raeburn  1062:         $r->print($response.'</form>');
1.302     raeburn  1063:     }
1.160     raeburn  1064: }
                   1065: 
                   1066: sub print_user_query_page {
1.351     raeburn  1067:     my ($r,$caller,$brcrum) = @_;
1.160     raeburn  1068: # FIXME - this is for a network-wide name search (similar to catalog search)
                   1069: # To use frames with similar behavior to catalog/portfolio search.
                   1070: # To be implemented. 
                   1071:     return;
                   1072: }
                   1073: 
1.42      matthew  1074: sub print_user_modification_page {
1.375     raeburn  1075:     my ($r,$ccuname,$ccdomain,$srch,$response,$context,$permission,$crstype,
                   1076:         $brcrum,$showcredits) = @_;
1.185     raeburn  1077:     if (($ccuname eq '') || ($ccdomain eq '')) {
1.215     raeburn  1078:         my $usermsg = &mt('No username and/or domain provided.');
                   1079:         $env{'form.phase'} = '';
1.351     raeburn  1080: 	&print_username_entry_form($r,$context,$usermsg,'','',$crstype,$brcrum);
1.58      www      1081:         return;
                   1082:     }
1.213     raeburn  1083:     my ($form,$formname);
                   1084:     if ($env{'form.action'} eq 'singlestudent') {
                   1085:         $form = 'document.enrollstudent';
                   1086:         $formname = 'enrollstudent';
                   1087:     } else {
                   1088:         $form = 'document.cu';
                   1089:         $formname = 'cu';
                   1090:     }
1.188     raeburn  1091:     my %abv_auth = &auth_abbrev();
1.227     raeburn  1092:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
1.185     raeburn  1093:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
                   1094:     if ($uhome eq 'no_host') {
1.215     raeburn  1095:         my $usertype;
                   1096:         my ($rules,$ruleorder) =
                   1097:             &Apache::lonnet::inst_userrules($ccdomain,'username');
                   1098:             $usertype =
1.353     raeburn  1099:                 &Apache::lonuserutils::check_usertype($ccdomain,$ccuname,$rules,
1.362     raeburn  1100:                                                       \%curr_rules,\%got_rules);
1.215     raeburn  1101:         my $cancreate =
                   1102:             &Apache::lonuserutils::can_create_user($ccdomain,$context,
                   1103:                                                    $usertype);
                   1104:         if (!$cancreate) {
1.292     bisitz   1105:             my $helplink = 'javascript:helpMenu('."'display'".')';
1.215     raeburn  1106:             my %usertypetext = (
                   1107:                 official   => 'institutional',
                   1108:                 unofficial => 'non-institutional',
                   1109:             );
                   1110:             my $response;
                   1111:             if ($env{'form.origform'} eq 'crtusername') {
1.362     raeburn  1112:                 $response = '<span class="LC_warning">'.
                   1113:                             &mt('No match found for the username [_1] in LON-CAPA domain: [_2]',
                   1114:                                 '<b>'.$ccuname.'</b>',$ccdomain).
1.215     raeburn  1115:                             '</span><br />';
                   1116:             }
1.292     bisitz   1117:             $response .= '<p class="LC_warning">'
                   1118:                         .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
                   1119:                         .' '
                   1120:                         .&mt('Please contact the [_1]helpdesk[_2] for assistance.'
                   1121:                             ,'<a href="'.$helplink.'">','</a>')
                   1122:                         .'</p><br />';
1.215     raeburn  1123:             $env{'form.phase'} = '';
1.351     raeburn  1124:             &print_username_entry_form($r,$context,$response,undef,undef,$crstype,$brcrum);
1.215     raeburn  1125:             return;
                   1126:         }
1.188     raeburn  1127:         $newuser = 1;
1.193     raeburn  1128:         my $checkhash;
                   1129:         my $checks = { 'username' => 1 };
1.196     raeburn  1130:         $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
1.193     raeburn  1131:         &Apache::loncommon::user_rule_check($checkhash,$checks,
1.196     raeburn  1132:             \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
                   1133:         if (ref($alerts{'username'}) eq 'HASH') {
                   1134:             if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
                   1135:                 my $domdesc =
1.193     raeburn  1136:                     &Apache::lonnet::domain($ccdomain,'description');
1.196     raeburn  1137:                 if ($alerts{'username'}{$ccdomain}{$ccuname}) {
                   1138:                     my $userchkmsg;
                   1139:                     if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
                   1140:                         $userchkmsg = 
                   1141:                             &Apache::loncommon::instrule_disallow_msg('username',
1.193     raeburn  1142:                                                                  $domdesc,1).
                   1143:                         &Apache::loncommon::user_rule_formats($ccdomain,
                   1144:                             $domdesc,$curr_rules{$ccdomain}{'username'},
                   1145:                             'username');
1.196     raeburn  1146:                     }
1.215     raeburn  1147:                     $env{'form.phase'} = '';
1.351     raeburn  1148:                     &print_username_entry_form($r,$context,$userchkmsg,undef,undef,$crstype,$brcrum);
1.196     raeburn  1149:                     return;
1.215     raeburn  1150:                 }
1.193     raeburn  1151:             }
1.185     raeburn  1152:         }
1.187     raeburn  1153:     } else {
1.188     raeburn  1154:         $newuser = 0;
1.185     raeburn  1155:     }
1.160     raeburn  1156:     if ($response) {
1.215     raeburn  1157:         $response = '<br />'.$response;
1.160     raeburn  1158:     }
1.149     raeburn  1159: 
1.52      matthew  1160:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.88      raeburn  1161:     my $dc_setcourse_code = '';
1.119     raeburn  1162:     my $nondc_setsection_code = '';                                        
1.112     albertel 1163:     my %loaditem;
1.114     albertel 1164: 
1.216     raeburn  1165:     my $groupslist = &Apache::lonuserutils::get_groupslist();
1.88      raeburn  1166: 
1.375     raeburn  1167:     my $js = &validation_javascript($context,$ccdomain,$pjump_def,$crstype,
1.216     raeburn  1168:                                $groupslist,$newuser,$formname,\%loaditem);
1.318     raeburn  1169:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.224     raeburn  1170:     my $helpitem = 'Course_Change_Privileges';
                   1171:     if ($env{'form.action'} eq 'singlestudent') {
                   1172:         $helpitem = 'Course_Add_Student';
                   1173:     }
1.351     raeburn  1174:     push (@{$brcrum},
                   1175:         {href => "javascript:backPage($form)",
                   1176:          text => $breadcrumb_text{'search'},
                   1177:          faq  => 282,
                   1178:          bug  => 'Instructor Interface',});
                   1179:     if ($env{'form.phase'} eq 'userpicked') {
                   1180:        push(@{$brcrum},
                   1181:               {href => "javascript:backPage($form,'get_user_info','select')",
                   1182:                text => $breadcrumb_text{'userpicked'},
                   1183:                faq  => 282,
                   1184:                bug  => 'Instructor Interface',});
                   1185:     }
                   1186:     push(@{$brcrum},
                   1187:             {href => "javascript:backPage($form,'$env{'form.phase'}','modify')",
                   1188:              text => $breadcrumb_text{'modify'},
                   1189:              faq  => 282,
                   1190:              bug  => 'Instructor Interface',
                   1191:              help => $helpitem});
                   1192:     my $args = {'add_entries'           => \%loaditem,
                   1193:                 'bread_crumbs'          => $brcrum,
                   1194:                 'bread_crumbs_component' => 'User Management'};
                   1195:     if ($env{'form.popup'}) {
                   1196:         $args->{'no_nav_bar'} = 1;
                   1197:     }
                   1198:     my $start_page =
                   1199:         &Apache::loncommon::start_page('User Management',$js,$args);
1.3       www      1200: 
1.25      matthew  1201:     my $forminfo =<<"ENDFORMINFO";
1.216     raeburn  1202: <form action="/adm/createuser" method="post" name="$formname">
1.190     raeburn  1203: <input type="hidden" name="phase" value="update_user_data" />
1.188     raeburn  1204: <input type="hidden" name="ccuname" value="$ccuname" />
                   1205: <input type="hidden" name="ccdomain" value="$ccdomain" />
1.157     albertel 1206: <input type="hidden" name="pres_value"  value="" />
                   1207: <input type="hidden" name="pres_type"   value="" />
                   1208: <input type="hidden" name="pres_marker" value="" />
1.25      matthew  1209: ENDFORMINFO
1.375     raeburn  1210:     my (%inccourses,$roledom,$defaultcredits);
1.329     raeburn  1211:     if ($context eq 'course') {
                   1212:         $inccourses{$env{'request.course.id'}}=1;
                   1213:         $roledom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.375     raeburn  1214:         if ($showcredits) {
                   1215:             $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1216:         }
1.329     raeburn  1217:     } elsif ($context eq 'author') {
                   1218:         $roledom = $env{'request.role.domain'};
                   1219:     } elsif ($context eq 'domain') {
                   1220:         foreach my $key (keys(%env)) {
                   1221:             $roledom = $env{'request.role.domain'};
                   1222:             if ($key=~/^user\.priv\.cm\.\/($roledom)\/($match_username)/) {
                   1223:                 $inccourses{$1.'_'.$2}=1;
                   1224:             }
                   1225:         }
                   1226:     } else {
                   1227:         foreach my $key (keys(%env)) {
                   1228: 	    if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
                   1229: 	        $inccourses{$1.'_'.$2}=1;
                   1230:             }
1.2       www      1231:         }
1.24      matthew  1232:     }
1.216     raeburn  1233:     if ($newuser) {
1.362     raeburn  1234:         my ($portfolioform,$domroleform);
1.267     raeburn  1235:         if ((&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) ||
                   1236:             (&Apache::lonnet::allowed('mut',$env{'request.role.domain'}))) {
                   1237:             # Current user has quota or user tools modification privileges
1.378     raeburn  1238:             $portfolioform = '<br />'.&user_quotas($ccuname,$ccdomain);
1.134     raeburn  1239:         }
1.383     raeburn  1240:         if ((&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) &&
                   1241:             ($ccdomain eq $env{'request.role.domain'})) {
1.362     raeburn  1242:             $domroleform = '<br />'.&domainrole_req($ccuname,$ccdomain);
                   1243:         }
1.227     raeburn  1244:         &initialize_authen_forms($ccdomain,$formname);
1.188     raeburn  1245:         my %lt=&Apache::lonlocal::texthash(
                   1246:                 'cnu'            => 'Create New User',
1.213     raeburn  1247:                 'ast'            => 'as a student',
1.318     raeburn  1248:                 'ame'            => 'as a member',
1.188     raeburn  1249:                 'ind'            => 'in domain',
                   1250:                 'lg'             => 'Login Data',
1.190     raeburn  1251:                 'hs'             => "Home Server",
1.188     raeburn  1252:         );
1.185     raeburn  1253: 	$r->print(<<ENDTITLE);
1.110     albertel 1254: $start_page
1.160     raeburn  1255: $response
1.25      matthew  1256: $forminfo
1.31      matthew  1257: <script type="text/javascript" language="Javascript">
1.301     bisitz   1258: // <![CDATA[
1.20      harris41 1259: $loginscript
1.301     bisitz   1260: // ]]>
1.31      matthew  1261: </script>
1.20      harris41 1262: <input type='hidden' name='makeuser' value='1' />
1.216     raeburn  1263: <h2>$lt{'cnu'} "$ccuname" $lt{'ind'} $ccdomain
1.185     raeburn  1264: ENDTITLE
1.213     raeburn  1265:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1266:             if ($crstype eq 'Community') {
                   1267:                 $r->print(' ('.$lt{'ame'}.')');
                   1268:             } else {
                   1269:                 $r->print(' ('.$lt{'ast'}.')');
                   1270:             }
1.213     raeburn  1271:         }
                   1272:         $r->print('</h2>'."\n".'<div class="LC_left_float">');
1.206     raeburn  1273:         my $personal_table = 
1.210     raeburn  1274:             &personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1275:                                    $inst_results{$ccuname.':'.$ccdomain});
1.206     raeburn  1276:         $r->print($personal_table);
1.187     raeburn  1277:         my ($home_server_pick,$numlib) = 
                   1278:             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
                   1279:                                                       'default','hide');
                   1280:         if ($numlib > 1) {
                   1281:             $r->print("
1.185     raeburn  1282: <br />
1.187     raeburn  1283: $lt{'hs'}: $home_server_pick
                   1284: <br />");
                   1285:         } else {
                   1286:             $r->print($home_server_pick);
                   1287:         }
1.304     raeburn  1288:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.362     raeburn  1289:             $r->print('<br /><h3>'.
                   1290:                       &mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.304     raeburn  1291:                       &Apache::loncommon::start_data_table().
                   1292:                       &build_tools_display($ccuname,$ccdomain,
                   1293:                                            'requestcourses').
                   1294:                       &Apache::loncommon::end_data_table());
                   1295:         }
1.188     raeburn  1296:         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                   1297:                   $lt{'lg'}.'</h3>');
1.185     raeburn  1298:         my ($fixedauth,$varauth,$authmsg); 
1.193     raeburn  1299:         if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
                   1300:             my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
                   1301:             my ($rules,$ruleorder) = 
                   1302:                 &Apache::lonnet::inst_userrules($ccdomain,'username');
1.185     raeburn  1303:             if (ref($rules) eq 'HASH') {
1.193     raeburn  1304:                 if (ref($rules->{$matchedrule}) eq 'HASH') {
                   1305:                     my $authtype = $rules->{$matchedrule}{'authtype'};
1.185     raeburn  1306:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
1.190     raeburn  1307:                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.275     raeburn  1308:                     } else { 
1.193     raeburn  1309:                         my $authparm = $rules->{$matchedrule}{'authparm'};
1.273     raeburn  1310:                         $authmsg = $rules->{$matchedrule}{'authmsg'};
1.185     raeburn  1311:                         if ($authtype =~ /^krb(4|5)$/) {
                   1312:                             my $ver = $1;
                   1313:                             if ($authparm ne '') {
                   1314:                                 $fixedauth = <<"KERB"; 
                   1315: <input type="hidden" name="login" value="krb" />
                   1316: <input type="hidden" name="krbver" value="$ver" />
                   1317: <input type="hidden" name="krbarg" value="$authparm" />
                   1318: KERB
                   1319:                             }
                   1320:                         } else {
                   1321:                             $fixedauth = 
                   1322: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
1.193     raeburn  1323:                             if ($rules->{$matchedrule}{'authparmfixed'}) {
1.185     raeburn  1324:                                 $fixedauth .=    
                   1325: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
                   1326:                             } else {
1.273     raeburn  1327:                                 if ($authtype eq 'int') {
                   1328:                                     $varauth = '<br />'.
1.301     bisitz   1329: &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  1330:                                 } elsif ($authtype eq 'loc') {
                   1331:                                     $varauth = '<br />'.
                   1332: &mt('[_1] Local Authentication with argument [_2]','','<input type="text" name="'.$authtype.'arg" value="" />')."\n";
                   1333:                                 } else {
                   1334:                                     $varauth =
1.185     raeburn  1335: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
1.273     raeburn  1336:                                 }
1.185     raeburn  1337:                             }
                   1338:                         }
                   1339:                     }
                   1340:                 } else {
1.190     raeburn  1341:                     $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.185     raeburn  1342:                 }
                   1343:             }
                   1344:             if ($authmsg) {
                   1345:                 $r->print(<<ENDAUTH);
                   1346: $fixedauth
                   1347: $authmsg
                   1348: $varauth
                   1349: ENDAUTH
                   1350:             }
                   1351:         } else {
1.190     raeburn  1352:             $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc)); 
1.187     raeburn  1353:         }
1.362     raeburn  1354:         $r->print($portfolioform.$domroleform);
1.215     raeburn  1355:         if ($env{'form.action'} eq 'singlestudent') {
                   1356:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1357:                                             $permission,$crstype,$ccuname,
                   1358:                                             $ccdomain,$showcredits));
1.215     raeburn  1359:         }
                   1360:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.216     raeburn  1361:     } else { # user already exists
1.79      albertel 1362: 	my %lt=&Apache::lonlocal::texthash(
1.191     raeburn  1363:                     'cup'  => "Modify existing user: ",
1.213     raeburn  1364:                     'ens'  => "Enroll one student: ",
1.318     raeburn  1365:                     'enm'  => "Enroll one member: ",
1.72      sakharuk 1366:                     'id'   => "in domain",
                   1367: 				       );
1.26      matthew  1368: 	$r->print(<<ENDCHANGEUSER);
1.110     albertel 1369: $start_page
1.25      matthew  1370: $forminfo
1.213     raeburn  1371: <h2>
1.26      matthew  1372: ENDCHANGEUSER
1.213     raeburn  1373:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1374:             if ($crstype eq 'Community') {
                   1375:                 $r->print($lt{'enm'});
                   1376:             } else {
                   1377:                 $r->print($lt{'ens'});
                   1378:             }
1.213     raeburn  1379:         } else {
                   1380:             $r->print($lt{'cup'});
                   1381:         }
                   1382:         $r->print(' "'.$ccuname.'" '.$lt{'id'}.' "'.$ccdomain.'"</h2>'.
                   1383:                   "\n".'<div class="LC_left_float">');
1.206     raeburn  1384:         my ($personal_table,$showforceid) = 
1.210     raeburn  1385:             &personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1386:                                    $inst_results{$ccuname.':'.$ccdomain});
1.206     raeburn  1387:         $r->print($personal_table);
                   1388:         if ($showforceid) {
1.362     raeburn  1389:             $r->print('<table>'.&Apache::lonuserutils::forceid_change($context).'</table>');
1.199     raeburn  1390:         }
1.275     raeburn  1391:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.362     raeburn  1392:             $r->print('<br /><h3>'.&mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.300     raeburn  1393:                       &Apache::loncommon::start_data_table());
1.314     raeburn  1394:             if ($env{'request.role.domain'} eq $ccdomain) {
1.300     raeburn  1395:                 $r->print(&build_tools_display($ccuname,$ccdomain,'requestcourses'));
                   1396:             } else {
                   1397:                 $r->print(&coursereq_externaluser($ccuname,$ccdomain,
                   1398:                                                   $env{'request.role.domain'}));
                   1399:             }
                   1400:             $r->print(&Apache::loncommon::end_data_table());
1.275     raeburn  1401:         }
1.199     raeburn  1402:         $r->print('</div>');
1.362     raeburn  1403:         my @order = ('auth','quota','tools','requestauthor');
                   1404:         my %user_text;
                   1405:         my ($isadv,$isauthor) = 
                   1406:             &Apache::lonnet::is_advanced_user($ccuname,$ccdomain);
                   1407:         if ((!$isauthor) && 
1.383     raeburn  1408:             (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))
                   1409:             && ($env{'request.role.domain'} eq $ccdomain)) {
1.362     raeburn  1410:             $user_text{'requestauthor'} = &domainrole_req($ccuname,$ccdomain);
                   1411:         }
                   1412:         $user_text{'auth'} =  &user_authentication($ccuname,$ccdomain,$formname);
1.267     raeburn  1413:         if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
                   1414:             (&Apache::lonnet::allowed('mut',$ccdomain))) {
1.188     raeburn  1415:             # Current user has quota modification privileges
1.378     raeburn  1416:             $user_text{'quota'} = &user_quotas($ccuname,$ccdomain);
1.267     raeburn  1417:         }
                   1418:         if (!&Apache::lonnet::allowed('mpq',$ccdomain)) {
                   1419:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
                   1420:                 my %lt=&Apache::lonlocal::texthash(
1.385     bisitz   1421:                     'dska'  => "Disk quotas for user's portfolio and Authoring Space",
                   1422:                     'youd'  => "You do not have privileges to modify the portfolio and/or Authoring Space quotas for this user.",
1.267     raeburn  1423:                     'ichr'  => "If a change is required, contact a domain coordinator for the domain",
                   1424:                 );
1.362     raeburn  1425:                 $user_text{'quota'} = <<ENDNOPORTPRIV;
1.188     raeburn  1426: <h3>$lt{'dska'}</h3>
                   1427: $lt{'youd'} $lt{'ichr'}: $ccdomain
                   1428: ENDNOPORTPRIV
1.267     raeburn  1429:             }
                   1430:         }
                   1431:         if (!&Apache::lonnet::allowed('mut',$ccdomain)) {
                   1432:             if (&Apache::lonnet::allowed('mut',$env{'request.role.domain'})) {
                   1433:                 my %lt=&Apache::lonlocal::texthash(
                   1434:                     'utav'  => "User Tools Availability",
1.361     raeburn  1435:                     'yodo'  => "You do not have privileges to modify Portfolio, Blog, WebDAV, or Personal Information Page settings for this user.",
1.267     raeburn  1436:                     'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1437:                 );
1.362     raeburn  1438:                 $user_text{'tools'} = <<ENDNOTOOLSPRIV;
1.267     raeburn  1439: <h3>$lt{'utav'}</h3>
                   1440: $lt{'yodo'} $lt{'ifch'}: $ccdomain
                   1441: ENDNOTOOLSPRIV
                   1442:             }
1.188     raeburn  1443:         }
1.362     raeburn  1444:         my $gotdiv = 0; 
                   1445:         foreach my $item (@order) {
                   1446:             if ($user_text{$item} ne '') {
                   1447:                 unless ($gotdiv) {
                   1448:                     $r->print('<div class="LC_left_float">');
                   1449:                     $gotdiv = 1;
                   1450:                 }
                   1451:                 $r->print('<br />'.$user_text{$item});
                   1452:             }
                   1453:         }
                   1454:         if ($env{'form.action'} eq 'singlestudent') {
                   1455:             unless ($gotdiv) {
                   1456:                 $r->print('<div class="LC_left_float">');
1.213     raeburn  1457:             }
1.375     raeburn  1458:             my $credits;
                   1459:             if ($showcredits) {
                   1460:                 $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1461:                 if ($credits eq '') {
                   1462:                     $credits = $defaultcredits;
                   1463:                 }
                   1464:             }
1.374     raeburn  1465:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1466:                                             $permission,$crstype,$ccuname,
                   1467:                                             $ccdomain,$showcredits));
1.374     raeburn  1468:         }
1.362     raeburn  1469:         if ($gotdiv) {
                   1470:             $r->print('</div><div class="LC_clear_float_footer"></div>');
1.188     raeburn  1471:         }
1.217     raeburn  1472:         if ($env{'form.action'} ne 'singlestudent') {
1.329     raeburn  1473:             &display_existing_roles($r,$ccuname,$ccdomain,\%inccourses,$context,
                   1474:                                     $roledom,$crstype);
1.217     raeburn  1475:         }
1.25      matthew  1476:     } ## End of new user/old user logic
1.218     raeburn  1477:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1478:         my $btntxt;
                   1479:         if ($crstype eq 'Community') {
                   1480:             $btntxt = &mt('Enroll Member');
                   1481:         } else {
                   1482:             $btntxt = &mt('Enroll Student');
                   1483:         }
                   1484:         $r->print('<br /><input type="button" value="'.$btntxt.'" onclick="setSections(this.form)" />'."\n");
1.218     raeburn  1485:     } else {
1.375     raeburn  1486:         $r->print('<fieldset><legend>'.&mt('Add Roles').'</legend>');
1.218     raeburn  1487:         my $addrolesdisplay = 0;
                   1488:         if ($context eq 'domain' || $context eq 'author') {
                   1489:             $addrolesdisplay = &new_coauthor_roles($r,$ccuname,$ccdomain);
                   1490:         }
                   1491:         if ($context eq 'domain') {
1.357     raeburn  1492:             my $add_domainroles = &new_domain_roles($r,$ccdomain);
1.218     raeburn  1493:             if (!$addrolesdisplay) {
                   1494:                 $addrolesdisplay = $add_domainroles;
1.2       www      1495:             }
1.375     raeburn  1496:             $r->print(&course_level_dc($env{'request.role.domain'},$showcredits));
                   1497:             $r->print('</fieldset><br /><input type="button" value="'.&mt('Save').'" onclick="setCourse()" />'."\n");
1.218     raeburn  1498:         } elsif ($context eq 'author') {
                   1499:             if ($addrolesdisplay) {
1.375     raeburn  1500:                 $r->print('</fieldset><br /><input type="button" value="'.&mt('Save').'"');
1.218     raeburn  1501:                 if ($newuser) {
1.301     bisitz   1502:                     $r->print(' onclick="auth_check()" \>'."\n");
1.218     raeburn  1503:                 } else {
1.301     bisitz   1504:                     $r->print('onclick="this.form.submit()" \>'."\n");
1.218     raeburn  1505:                 }
1.188     raeburn  1506:             } else {
1.375     raeburn  1507:                 $r->print('</fieldset><br /><a href="javascript:backPage(document.cu)">'.
1.218     raeburn  1508:                           &mt('Back to previous page').'</a>');
1.188     raeburn  1509:             }
                   1510:         } else {
1.375     raeburn  1511:             $r->print(&course_level_table(\%inccourses,$showcredits,$defaultcredits));
                   1512:             $r->print('</fieldset><br /><input type="button" value="'.&mt('Save').'" onclick="setSections(this.form)" />'."\n");
1.188     raeburn  1513:         }
1.88      raeburn  1514:     }
1.188     raeburn  1515:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
1.179     raeburn  1516:     $r->print('<input type="hidden" name="currstate" value="" />');
1.352     raeburn  1517:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" /></form>');
1.218     raeburn  1518:     return;
1.2       www      1519: }
1.1       www      1520: 
1.213     raeburn  1521: sub singleuser_breadcrumb {
1.318     raeburn  1522:     my ($crstype) = @_;
1.213     raeburn  1523:     my %breadcrumb_text;
                   1524:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1525:         if ($crstype eq 'Community') {
                   1526:             $breadcrumb_text{'search'} = 'Enroll a member';
                   1527:         } else {
                   1528:             $breadcrumb_text{'search'} = 'Enroll a student';
                   1529:         }
1.213     raeburn  1530:         $breadcrumb_text{'userpicked'} = 'Select a user',
                   1531:         $breadcrumb_text{'modify'} = 'Set section/dates',
                   1532:     } else {
1.229     raeburn  1533:         $breadcrumb_text{'search'} = 'Create/modify a user';
1.213     raeburn  1534:         $breadcrumb_text{'userpicked'} = 'Select a user',
                   1535:         $breadcrumb_text{'modify'} = 'Set user role',
                   1536:     }
                   1537:     return %breadcrumb_text;
                   1538: }
                   1539: 
                   1540: sub date_sections_select {
1.375     raeburn  1541:     my ($context,$newuser,$formname,$permission,$crstype,$ccuname,$ccdomain,
                   1542:         $showcredits) = @_;
                   1543:     my $credits;
                   1544:     if ($showcredits) {
                   1545:         my $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1546:         $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1547:         if ($credits eq '') {
                   1548:             $credits = $defaultcredits;
                   1549:         }
                   1550:     }
1.213     raeburn  1551:     my $cid = $env{'request.course.id'};
                   1552:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity($cid);
                   1553:     my $date_table = '<h3>'.&mt('Starting and Ending Dates').'</h3>'."\n".
                   1554:         &Apache::lonuserutils::date_setting_table(undef,undef,$context,
                   1555:                                                   undef,$formname,$permission);
                   1556:     my $rowtitle = 'Section';
1.375     raeburn  1557:     my $secbox = '<h3>'.&mt('Section and Credits').'</h3>'."\n".
1.213     raeburn  1558:         &Apache::lonuserutils::section_picker($cdom,$cnum,'st',$rowtitle,
1.375     raeburn  1559:                                               $permission,$context,'',$crstype,
                   1560:                                               $showcredits,$credits);
1.213     raeburn  1561:     my $output = $date_table.$secbox;
                   1562:     return $output;
                   1563: }
                   1564: 
1.216     raeburn  1565: sub validation_javascript {
1.375     raeburn  1566:     my ($context,$ccdomain,$pjump_def,$crstype,$groupslist,$newuser,$formname,
1.216     raeburn  1567:         $loaditem) = @_;
                   1568:     my $dc_setcourse_code = '';
                   1569:     my $nondc_setsection_code = '';
                   1570:     if ($context eq 'domain') {
                   1571:         my $dcdom = $env{'request.role.domain'};
                   1572:         $loaditem->{'onload'} = "document.cu.coursedesc.value='';";
1.227     raeburn  1573:         $dc_setcourse_code = 
                   1574:             &Apache::lonuserutils::dc_setcourse_js('cu','singleuser',$context);
1.216     raeburn  1575:     } else {
1.227     raeburn  1576:         my $checkauth; 
                   1577:         if (($newuser) || (&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1578:             $checkauth = 1;
                   1579:         }
                   1580:         if ($context eq 'course') {
                   1581:             $nondc_setsection_code =
                   1582:                 &Apache::lonuserutils::setsections_javascript($formname,$groupslist,
1.375     raeburn  1583:                                                               undef,$checkauth,
                   1584:                                                               $crstype);
1.227     raeburn  1585:         }
                   1586:         if ($checkauth) {
                   1587:             $nondc_setsection_code .= 
                   1588:                 &Apache::lonuserutils::verify_authen($formname,$context);
                   1589:         }
1.216     raeburn  1590:     }
                   1591:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                   1592:                                    $nondc_setsection_code,$groupslist);
                   1593:     my ($jsback,$elements) = &crumb_utilities();
                   1594:     $js .= "\n".
1.301     bisitz   1595:            '<script type="text/javascript">'."\n".
                   1596:            '// <![CDATA['."\n".
                   1597:            $jsback."\n".
                   1598:            '// ]]>'."\n".
                   1599:            '</script>'."\n";
1.216     raeburn  1600:     return $js;
                   1601: }
                   1602: 
1.217     raeburn  1603: sub display_existing_roles {
1.375     raeburn  1604:     my ($r,$ccuname,$ccdomain,$inccourses,$context,$roledom,$crstype,
                   1605:         $showcredits) = @_;
1.329     raeburn  1606:     my $now=time;
                   1607:     my %lt=&Apache::lonlocal::texthash(
1.217     raeburn  1608:                     'rer'  => "Existing Roles",
                   1609:                     'rev'  => "Revoke",
                   1610:                     'del'  => "Delete",
                   1611:                     'ren'  => "Re-Enable",
                   1612:                     'rol'  => "Role",
                   1613:                     'ext'  => "Extent",
1.375     raeburn  1614:                     'crd'  => "Credits",
1.217     raeburn  1615:                     'sta'  => "Start",
                   1616:                     'end'  => "End",
                   1617:                                        );
1.329     raeburn  1618:     my (%rolesdump,%roletext,%sortrole,%roleclass,%rolepriv);
                   1619:     if ($context eq 'course' || $context eq 'author') {
                   1620:         my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
                   1621:         my %roleshash = 
                   1622:             &Apache::lonnet::get_my_roles($ccuname,$ccdomain,'userroles',
                   1623:                               ['active','previous','future'],\@roles,$roledom,1);
                   1624:         foreach my $key (keys(%roleshash)) {
                   1625:             my ($start,$end) = split(':',$roleshash{$key});
                   1626:             next if ($start eq '-1' || $end eq '-1');
                   1627:             my ($rnum,$rdom,$role,$sec) = split(':',$key);
                   1628:             if ($context eq 'course') {
                   1629:                 next unless (($rnum eq $env{'course.'.$env{'request.course.id'}.'.num'})
                   1630:                              && ($rdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}));
                   1631:             } elsif ($context eq 'author') {
                   1632:                 next unless (($rnum eq $env{'user.name'}) && ($rdom eq $env{'request.role.domain'}));
                   1633:             }
                   1634:             my ($newkey,$newvalue,$newrole);
                   1635:             $newkey = '/'.$rdom.'/'.$rnum;
                   1636:             if ($sec ne '') {
                   1637:                 $newkey .= '/'.$sec;
                   1638:             }
                   1639:             $newvalue = $role;
                   1640:             if ($role =~ /^cr/) {
                   1641:                 $newrole = 'cr';
                   1642:             } else {
                   1643:                 $newrole = $role;
                   1644:             }
                   1645:             $newkey .= '_'.$newrole;
                   1646:             if ($start ne '' && $end ne '') {
                   1647:                 $newvalue .= '_'.$end.'_'.$start;
1.335     raeburn  1648:             } elsif ($end ne '') {
                   1649:                 $newvalue .= '_'.$end;
1.329     raeburn  1650:             }
                   1651:             $rolesdump{$newkey} = $newvalue;
                   1652:         }
                   1653:     } else {
1.360     raeburn  1654:         %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
1.329     raeburn  1655:     }
                   1656:     # Build up table of user roles to allow revocation and re-enabling of roles.
                   1657:     my ($tmp) = keys(%rolesdump);
                   1658:     return if ($tmp =~ /^(con_lost|error)/i);
                   1659:     foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
                   1660:                                 my $b1=join('_',(split('_',$b))[1,0]);
                   1661:                                 return $a1 cmp $b1;
                   1662:                             } keys(%rolesdump)) {
                   1663:         next if ($area =~ /^rolesdef/);
                   1664:         my $envkey=$area;
                   1665:         my $role = $rolesdump{$area};
                   1666:         my $thisrole=$area;
                   1667:         $area =~ s/\_\w\w$//;
                   1668:         my ($role_code,$role_end_time,$role_start_time) =
                   1669:             split(/_/,$role);
1.217     raeburn  1670: # Is this a custom role? Get role owner and title.
1.329     raeburn  1671:         my ($croleudom,$croleuname,$croletitle)=
                   1672:             ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
                   1673:         my $allowed=0;
                   1674:         my $delallowed=0;
                   1675:         my $sortkey=$role_code;
                   1676:         my $class='Unknown';
1.375     raeburn  1677:         my $credits='';
1.329     raeburn  1678:         if ($area =~ m{^/($match_domain)/($match_courseid)} ) {
                   1679:             $class='Course';
                   1680:             my ($coursedom,$coursedir) = ($1,$2);
                   1681:             my $cid = $1.'_'.$2;
                   1682:             # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
                   1683:             my %coursedata=
                   1684:                 &Apache::lonnet::coursedescription($cid);
                   1685:             if ($coursedir =~ /^$match_community$/) {
                   1686:                 $class='Community';
                   1687:             }
                   1688:             $sortkey.="\0$coursedom";
                   1689:             my $carea;
                   1690:             if (defined($coursedata{'description'})) {
                   1691:                 $carea=$coursedata{'description'}.
                   1692:                     '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
                   1693:     &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$coursedir,$coursedom);
                   1694:                 $sortkey.="\0".$coursedata{'description'};
                   1695:             } else {
                   1696:                 if ($class eq 'Community') {
                   1697:                     $carea=&mt('Unavailable community').': '.$area;
                   1698:                     $sortkey.="\0".&mt('Unavailable community').': '.$area;
1.217     raeburn  1699:                 } else {
                   1700:                     $carea=&mt('Unavailable course').': '.$area;
                   1701:                     $sortkey.="\0".&mt('Unavailable course').': '.$area;
                   1702:                 }
1.329     raeburn  1703:             }
                   1704:             $sortkey.="\0$coursedir";
                   1705:             $inccourses->{$cid}=1;
1.375     raeburn  1706:             if (($showcredits) && ($class eq 'Course') && ($role_code eq 'st')) {
                   1707:                 my $defaultcredits = $coursedata{'internal.defaultcredits'};
                   1708:                 $credits =
                   1709:                     &get_user_credits($ccuname,$ccdomain,$defaultcredits,
                   1710:                                       $coursedom,$coursedir);
                   1711:                 if ($credits eq '') {
                   1712:                     $credits = $defaultcredits;
                   1713:                 }
                   1714:             }
1.329     raeburn  1715:             if ((&Apache::lonnet::allowed('c'.$role_code,$coursedom.'/'.$coursedir)) ||
                   1716:                 (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1717:                 $allowed=1;
                   1718:             }
                   1719:             unless ($allowed) {
1.365     raeburn  1720:                 my $isowner = &Apache::lonuserutils::is_courseowner($cid,$coursedata{'internal.courseowner'});
1.329     raeburn  1721:                 if ($isowner) {
                   1722:                     if (($role_code eq 'co') && ($class eq 'Community')) {
                   1723:                         $allowed = 1;
                   1724:                     } elsif (($role_code eq 'cc') && ($class eq 'Course')) {
                   1725:                         $allowed = 1;
                   1726:                     }
1.217     raeburn  1727:                 }
1.329     raeburn  1728:             } 
                   1729:             if ((&Apache::lonnet::allowed('dro',$coursedom)) ||
                   1730:                 (&Apache::lonnet::allowed('dro',$ccdomain))) {
                   1731:                 $delallowed=1;
                   1732:             }
1.217     raeburn  1733: # - custom role. Needs more info, too
1.329     raeburn  1734:             if ($croletitle) {
                   1735:                 if (&Apache::lonnet::allowed('ccr',$coursedom.'/'.$coursedir)) {
                   1736:                     $allowed=1;
                   1737:                     $thisrole.='.'.$role_code;
1.217     raeburn  1738:                 }
1.329     raeburn  1739:             }
                   1740:             if ($area=~m{^/($match_domain)/($match_courseid)/(\w+)}) {
1.373     bisitz   1741:                 $carea.='<br />'.&mt('Section: [_1]',$3);
1.329     raeburn  1742:                 $sortkey.="\0$3";
                   1743:                 if (!$allowed) {
                   1744:                     if ($env{'request.course.sec'} eq $3) {
                   1745:                         if (&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2.'/'.$3)) {
                   1746:                             $allowed = 1;
1.217     raeburn  1747:                         }
                   1748:                     }
                   1749:                 }
1.329     raeburn  1750:             }
                   1751:             $area=$carea;
                   1752:         } else {
                   1753:             $sortkey.="\0".$area;
                   1754:             # Determine if current user is able to revoke privileges
                   1755:             if ($area=~m{^/($match_domain)/}) {
                   1756:                 if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
                   1757:                    (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1758:                    $allowed=1;
1.217     raeburn  1759:                 }
1.329     raeburn  1760:                 if (((&Apache::lonnet::allowed('dro',$1))  ||
                   1761:                     (&Apache::lonnet::allowed('dro',$ccdomain))) &&
                   1762:                     ($role_code ne 'dc')) {
                   1763:                     $delallowed=1;
1.217     raeburn  1764:                 }
1.329     raeburn  1765:             } else {
                   1766:                 if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
1.217     raeburn  1767:                     $allowed=1;
                   1768:                 }
                   1769:             }
1.363     raeburn  1770:             if ($role_code eq 'ca' || $role_code eq 'au' || $role_code eq 'aa') {
1.377     raeburn  1771:                 $class='Authoring Space';
1.329     raeburn  1772:             } elsif ($role_code eq 'su') {
                   1773:                 $class='System';
1.217     raeburn  1774:             } else {
1.329     raeburn  1775:                 $class='Domain';
1.217     raeburn  1776:             }
1.329     raeburn  1777:         }
                   1778:         if (($role_code eq 'ca') || ($role_code eq 'aa')) {
                   1779:             $area=~m{/($match_domain)/($match_username)};
                   1780:             if (&Apache::lonuserutils::authorpriv($2,$1)) {
                   1781:                 $allowed=1;
1.217     raeburn  1782:             } else {
1.329     raeburn  1783:                 $allowed=0;
1.217     raeburn  1784:             }
1.329     raeburn  1785:         }
                   1786:         my $row = '';
                   1787:         $row.= '<td>';
                   1788:         my $active=1;
                   1789:         $active=0 if (($role_end_time) && ($now>$role_end_time));
                   1790:         if (($active) && ($allowed)) {
                   1791:             $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
                   1792:         } else {
                   1793:             if ($active) {
                   1794:                $row.='&nbsp;';
1.217     raeburn  1795:             } else {
1.329     raeburn  1796:                $row.=&mt('expired or revoked');
1.217     raeburn  1797:             }
1.329     raeburn  1798:         }
                   1799:         $row.='</td><td>';
                   1800:         if ($allowed && !$active) {
                   1801:             $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
                   1802:         } else {
                   1803:             $row.='&nbsp;';
                   1804:         }
                   1805:         $row.='</td><td>';
                   1806:         if ($delallowed) {
                   1807:             $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
                   1808:         } else {
                   1809:             $row.='&nbsp;';
                   1810:         }
                   1811:         my $plaintext='';
                   1812:         if (!$croletitle) {
1.375     raeburn  1813:             $plaintext=&Apache::lonnet::plaintext($role_code,$class);
                   1814:             if (($showcredits) && ($credits ne '')) {
                   1815:                 $plaintext .= '<br/ ><span class="LC_nobreak">'.
                   1816:                               '<span class="LC_fontsize_small">'.
                   1817:                               &mt('Credits: [_1]',$credits).
                   1818:                               '</span></span>';
                   1819:             }
1.329     raeburn  1820:         } else {
                   1821:             $plaintext=
1.346     bisitz   1822:                 &mt('Customrole [_1][_2]defined by [_3]',
                   1823:                         '"'.$croletitle.'"',
                   1824:                         '<br />',
                   1825:                         $croleuname.':'.$croleudom);
1.329     raeburn  1826:         }
                   1827:         $row.= '</td><td>'.$plaintext.
                   1828:                '</td><td>'.$area.
                   1829:                '</td><td>'.($role_start_time?&Apache::lonlocal::locallocaltime($role_start_time)
                   1830:                                             : '&nbsp;' ).
                   1831:                '</td><td>'.($role_end_time  ?&Apache::lonlocal::locallocaltime($role_end_time)
                   1832:                                             : '&nbsp;' )
                   1833:                ."</td>";
                   1834:         $sortrole{$sortkey}=$envkey;
                   1835:         $roletext{$envkey}=$row;
                   1836:         $roleclass{$envkey}=$class;
                   1837:         $rolepriv{$envkey}=$allowed;
                   1838:     } # end of foreach        (table building loop)
                   1839: 
                   1840:     my $rolesdisplay = 0;
                   1841:     my %output = ();
1.377     raeburn  1842:     foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  1843:         $output{$type} = '';
                   1844:         foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                   1845:             if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) {
                   1846:                  $output{$type}.=
                   1847:                       &Apache::loncommon::start_data_table_row().
                   1848:                       $roletext{$sortrole{$which}}.
                   1849:                       &Apache::loncommon::end_data_table_row();
1.217     raeburn  1850:             }
1.329     raeburn  1851:         }
                   1852:         unless($output{$type} eq '') {
                   1853:             $output{$type} = '<tr class="LC_info_row">'.
                   1854:                       "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
                   1855:                       $output{$type};
                   1856:             $rolesdisplay = 1;
                   1857:         }
                   1858:     }
                   1859:     if ($rolesdisplay == 1) {
                   1860:         my $contextrole='';
                   1861:         if ($env{'request.course.id'}) {
                   1862:             if (&Apache::loncommon::course_type() eq 'Community') {
                   1863:                 $contextrole = &mt('Existing Roles in this Community');
1.290     bisitz   1864:             } else {
1.329     raeburn  1865:                 $contextrole = &mt('Existing Roles in this Course');
1.290     bisitz   1866:             }
1.329     raeburn  1867:         } elsif ($env{'request.role'} =~ /^au\./) {
1.377     raeburn  1868:             $contextrole = &mt('Existing Co-Author Roles in your Authoring Space');
1.329     raeburn  1869:         } else {
                   1870:             $contextrole = &mt('Existing Roles in this Domain');
                   1871:         }
1.375     raeburn  1872:         $r->print('<div>'.
                   1873: '<fieldset><legend>'.$contextrole.'</legend>'.
1.217     raeburn  1874: &Apache::loncommon::start_data_table("LC_createuser").
                   1875: &Apache::loncommon::start_data_table_header_row().
                   1876: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}.
                   1877: '</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.
                   1878: '</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
                   1879: &Apache::loncommon::end_data_table_header_row());
1.377     raeburn  1880:         foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  1881:             if ($output{$type}) {
                   1882:                 $r->print($output{$type}."\n");
1.217     raeburn  1883:             }
                   1884:         }
1.375     raeburn  1885:         $r->print(&Apache::loncommon::end_data_table().
                   1886:                   '</fieldset></div>');
1.329     raeburn  1887:     }
1.217     raeburn  1888:     return;
                   1889: }
                   1890: 
1.218     raeburn  1891: sub new_coauthor_roles {
                   1892:     my ($r,$ccuname,$ccdomain) = @_;
                   1893:     my $addrolesdisplay = 0;
                   1894:     #
                   1895:     # Co-Author
                   1896:     #
                   1897:     if (&Apache::lonuserutils::authorpriv($env{'user.name'},
                   1898:                                           $env{'request.role.domain'}) &&
                   1899:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
                   1900:         # No sense in assigning co-author role to yourself
                   1901:         $addrolesdisplay = 1;
                   1902:         my $cuname=$env{'user.name'};
                   1903:         my $cudom=$env{'request.role.domain'};
                   1904:         my %lt=&Apache::lonlocal::texthash(
1.377     raeburn  1905:                     'cs'   => "Authoring Space",
1.218     raeburn  1906:                     'act'  => "Activate",
                   1907:                     'rol'  => "Role",
                   1908:                     'ext'  => "Extent",
                   1909:                     'sta'  => "Start",
                   1910:                     'end'  => "End",
                   1911:                     'cau'  => "Co-Author",
                   1912:                     'caa'  => "Assistant Co-Author",
                   1913:                     'ssd'  => "Set Start Date",
                   1914:                     'sed'  => "Set End Date"
                   1915:                                        );
                   1916:         $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n".
                   1917:                   &Apache::loncommon::start_data_table()."\n".
                   1918:                   &Apache::loncommon::start_data_table_header_row()."\n".
                   1919:                   '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
                   1920:                   '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
                   1921:                   '<th>'.$lt{'end'}.'</th>'."\n".
                   1922:                   &Apache::loncommon::end_data_table_header_row()."\n".
                   1923:                   &Apache::loncommon::start_data_table_row().'
                   1924:            <td>
1.291     bisitz   1925:             <input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_ca" />
1.218     raeburn  1926:            </td>
                   1927:            <td>'.$lt{'cau'}.'</td>
                   1928:            <td>'.$cudom.'_'.$cuname.'</td>
                   1929:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
                   1930:              <a href=
                   1931: "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>
                   1932: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
                   1933: <a href=
                   1934: "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".
                   1935:               &Apache::loncommon::end_data_table_row()."\n".
                   1936:               &Apache::loncommon::start_data_table_row()."\n".
1.291     bisitz   1937: '<td><input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
1.218     raeburn  1938: <td>'.$lt{'caa'}.'</td>
                   1939: <td>'.$cudom.'_'.$cuname.'</td>
                   1940: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
                   1941: <a href=
                   1942: "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>
                   1943: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
                   1944: <a href=
                   1945: "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".
                   1946:              &Apache::loncommon::end_data_table_row()."\n".
                   1947:              &Apache::loncommon::end_data_table());
                   1948:     } elsif ($env{'request.role'} =~ /^au\./) {
                   1949:         if (!(&Apache::lonuserutils::authorpriv($env{'user.name'},
                   1950:                                                 $env{'request.role.domain'}))) {
                   1951:             $r->print('<span class="LC_error">'.
                   1952:                       &mt('You do not have privileges to assign co-author roles.').
                   1953:                       '</span>');
                   1954:         } elsif (($env{'user.name'} eq $ccuname) &&
                   1955:              ($env{'user.domain'} eq $ccdomain)) {
1.377     raeburn  1956:             $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  1957:         }
                   1958:     }
                   1959:     return $addrolesdisplay;;
                   1960: }
                   1961: 
                   1962: sub new_domain_roles {
1.357     raeburn  1963:     my ($r,$ccdomain) = @_;
1.218     raeburn  1964:     my $addrolesdisplay = 0;
                   1965:     #
                   1966:     # Domain level
                   1967:     #
                   1968:     my $num_domain_level = 0;
                   1969:     my $domaintext =
                   1970:     '<h4>'.&mt('Domain Level').'</h4>'.
                   1971:     &Apache::loncommon::start_data_table().
                   1972:     &Apache::loncommon::start_data_table_header_row().
                   1973:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
                   1974:     &mt('Extent').'</th>'.
                   1975:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
                   1976:     &Apache::loncommon::end_data_table_header_row();
1.312     raeburn  1977:     my @allroles = &Apache::lonuserutils::roles_by_context('domain');
1.218     raeburn  1978:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
1.312     raeburn  1979:         foreach my $role (@allroles) {
                   1980:             next if ($role eq 'ad');
1.357     raeburn  1981:             next if (($role eq 'au') && ($ccdomain ne $thisdomain));
1.218     raeburn  1982:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
                   1983:                my $plrole=&Apache::lonnet::plaintext($role);
                   1984:                my %lt=&Apache::lonlocal::texthash(
                   1985:                     'ssd'  => "Set Start Date",
                   1986:                     'sed'  => "Set End Date"
                   1987:                                        );
                   1988:                $num_domain_level ++;
                   1989:                $domaintext .=
                   1990: &Apache::loncommon::start_data_table_row().
1.291     bisitz   1991: '<td><input type="checkbox" name="act_'.$thisdomain.'_'.$role.'" /></td>
1.218     raeburn  1992: <td>'.$plrole.'</td>
                   1993: <td>'.$thisdomain.'</td>
                   1994: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
                   1995: <a href=
                   1996: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
                   1997: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
                   1998: <a href=
                   1999: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
                   2000: &Apache::loncommon::end_data_table_row();
                   2001:             }
                   2002:         }
                   2003:     }
                   2004:     $domaintext.= &Apache::loncommon::end_data_table();
                   2005:     if ($num_domain_level > 0) {
                   2006:         $r->print($domaintext);
                   2007:         $addrolesdisplay = 1;
                   2008:     }
                   2009:     return $addrolesdisplay;
                   2010: }
                   2011: 
1.188     raeburn  2012: sub user_authentication {
1.227     raeburn  2013:     my ($ccuname,$ccdomain,$formname) = @_;
1.188     raeburn  2014:     my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
1.227     raeburn  2015:     my $outcome;
1.188     raeburn  2016:     # Check for a bad authentication type
                   2017:     if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth):/) {
                   2018:         # bad authentication scheme
                   2019:         my %lt=&Apache::lonlocal::texthash(
                   2020:                        'err'   => "ERROR",
                   2021:                        'uuas'  => "This user has an unrecognized authentication scheme",
                   2022:                        'adcs'  => "Please alert a domain coordinator of this situation",
                   2023:                        'sldb'  => "Please specify login data below",
                   2024:                        'ld'    => "Login Data"
                   2025:         );
                   2026:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
1.227     raeburn  2027:             &initialize_authen_forms($ccdomain,$formname);
                   2028: 
1.190     raeburn  2029:             my $choices = &Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc);
1.188     raeburn  2030:             $outcome = <<ENDBADAUTH;
                   2031: <script type="text/javascript" language="Javascript">
1.301     bisitz   2032: // <![CDATA[
1.188     raeburn  2033: $loginscript
1.301     bisitz   2034: // ]]>
1.188     raeburn  2035: </script>
                   2036: <span class="LC_error">$lt{'err'}:
                   2037: $lt{'uuas'} ($currentauth). $lt{'sldb'}.</span>
                   2038: <h3>$lt{'ld'}</h3>
                   2039: $choices
                   2040: ENDBADAUTH
                   2041:         } else {
                   2042:             # This user is not allowed to modify the user's
                   2043:             # authentication scheme, so just notify them of the problem
                   2044:             $outcome = <<ENDBADAUTH;
                   2045: <span class="LC_error"> $lt{'err'}: 
                   2046: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
                   2047: </span>
                   2048: ENDBADAUTH
                   2049:         }
                   2050:     } else { # Authentication type is valid
1.227     raeburn  2051:         &initialize_authen_forms($ccdomain,$formname,$currentauth,'modifyuser');
1.205     raeburn  2052:         my ($authformcurrent,$can_modify,@authform_others) =
1.188     raeburn  2053:             &modify_login_block($ccdomain,$currentauth);
                   2054:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2055:             # Current user has login modification privileges
                   2056:             my %lt=&Apache::lonlocal::texthash (
                   2057:                            'ld'    => "Login Data",
                   2058:                            'ccld'  => "Change Current Login Data",
                   2059:                            'enld'  => "Enter New Login Data"
                   2060:                                                );
                   2061:             $outcome =
                   2062:                        '<script type="text/javascript" language="Javascript">'."\n".
1.301     bisitz   2063:                        '// <![CDATA['."\n".
1.188     raeburn  2064:                        $loginscript."\n".
1.301     bisitz   2065:                        '// ]]>'."\n".
1.188     raeburn  2066:                        '</script>'."\n".
                   2067:                        '<h3>'.$lt{'ld'}.'</h3>'.
                   2068:                        &Apache::loncommon::start_data_table().
1.205     raeburn  2069:                        &Apache::loncommon::start_data_table_row().
1.188     raeburn  2070:                        '<td>'.$authformnop;
                   2071:             if ($can_modify) {
                   2072:                 $outcome .= '</td>'."\n".
                   2073:                             &Apache::loncommon::end_data_table_row().
                   2074:                             &Apache::loncommon::start_data_table_row().
                   2075:                             '<td>'.$authformcurrent.'</td>'.
                   2076:                             &Apache::loncommon::end_data_table_row()."\n";
                   2077:             } else {
1.200     raeburn  2078:                 $outcome .= '&nbsp;('.$authformcurrent.')</td>'.
                   2079:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  2080:             }
1.205     raeburn  2081:             foreach my $item (@authform_others) { 
                   2082:                 $outcome .= &Apache::loncommon::start_data_table_row().
                   2083:                             '<td>'.$item.'</td>'.
                   2084:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  2085:             }
1.205     raeburn  2086:             $outcome .= &Apache::loncommon::end_data_table();
1.188     raeburn  2087:         } else {
                   2088:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
                   2089:                 my %lt=&Apache::lonlocal::texthash(
                   2090:                            'ccld'  => "Change Current Login Data",
                   2091:                            'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
                   2092:                            'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   2093:                 );
                   2094:                 $outcome .= <<ENDNOPRIV;
                   2095: <h3>$lt{'ccld'}</h3>
                   2096: $lt{'yodo'} $lt{'ifch'}: $ccdomain
1.235     raeburn  2097: <input type="hidden" name="login" value="nochange" />
1.188     raeburn  2098: ENDNOPRIV
                   2099:             }
                   2100:         }
                   2101:     }  ## End of "check for bad authentication type" logic
                   2102:     return $outcome;
                   2103: }
                   2104: 
1.187     raeburn  2105: sub modify_login_block {
                   2106:     my ($dom,$currentauth) = @_;
                   2107:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2108:     my ($authnum,%can_assign) =
                   2109:         &Apache::loncommon::get_assignable_auth($dom);
1.205     raeburn  2110:     my ($authformcurrent,@authform_others,$show_override_msg);
1.187     raeburn  2111:     if ($currentauth=~/^krb(4|5):/) {
                   2112:         $authformcurrent=$authformkrb;
                   2113:         if ($can_assign{'int'}) {
1.205     raeburn  2114:             push(@authform_others,$authformint);
1.187     raeburn  2115:         }
                   2116:         if ($can_assign{'loc'}) {
1.205     raeburn  2117:             push(@authform_others,$authformloc);
1.187     raeburn  2118:         }
                   2119:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   2120:             $show_override_msg = 1;
                   2121:         }
                   2122:     } elsif ($currentauth=~/^internal:/) {
                   2123:         $authformcurrent=$authformint;
                   2124:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2125:             push(@authform_others,$authformkrb);
1.187     raeburn  2126:         }
                   2127:         if ($can_assign{'loc'}) {
1.205     raeburn  2128:             push(@authform_others,$authformloc);
1.187     raeburn  2129:         }
                   2130:         if ($can_assign{'int'}) {
                   2131:             $show_override_msg = 1;
                   2132:         }
                   2133:     } elsif ($currentauth=~/^unix:/) {
                   2134:         $authformcurrent=$authformfsys;
                   2135:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2136:             push(@authform_others,$authformkrb);
1.187     raeburn  2137:         }
                   2138:         if ($can_assign{'int'}) {
1.205     raeburn  2139:             push(@authform_others,$authformint);
1.187     raeburn  2140:         }
                   2141:         if ($can_assign{'loc'}) {
1.205     raeburn  2142:             push(@authform_others,$authformloc);
1.187     raeburn  2143:         }
                   2144:         if ($can_assign{'fsys'}) {
                   2145:             $show_override_msg = 1;
                   2146:         }
                   2147:     } elsif ($currentauth=~/^localauth:/) {
                   2148:         $authformcurrent=$authformloc;
                   2149:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2150:             push(@authform_others,$authformkrb);
1.187     raeburn  2151:         }
                   2152:         if ($can_assign{'int'}) {
1.205     raeburn  2153:             push(@authform_others,$authformint);
1.187     raeburn  2154:         }
                   2155:         if ($can_assign{'loc'}) {
                   2156:             $show_override_msg = 1;
                   2157:         }
                   2158:     }
                   2159:     if ($show_override_msg) {
1.205     raeburn  2160:         $authformcurrent = '<table><tr><td colspan="3">'.$authformcurrent.
                   2161:                            '</td></tr>'."\n".
                   2162:                            '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
                   2163:                            '<td><b>'.&mt('Currently in use').'</b></td>'.
                   2164:                            '<td align="right"><span class="LC_cusr_emph">'.
1.187     raeburn  2165:                             &mt('will override current values').
1.205     raeburn  2166:                             '</span></td></tr></table>';
1.187     raeburn  2167:     }
1.205     raeburn  2168:     return ($authformcurrent,$show_override_msg,@authform_others); 
1.187     raeburn  2169: }
                   2170: 
1.188     raeburn  2171: sub personal_data_display {
1.252     raeburn  2172:     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$rolesarray) = @_;
1.286     raeburn  2173:     my ($output,$showforceid,%userenv,%canmodify,%canmodify_status);
1.219     raeburn  2174:     my @userinfo = ('firstname','middlename','lastname','generation',
                   2175:                     'permanentemail','id');
1.252     raeburn  2176:     my $rowcount = 0;
                   2177:     my $editable = 0;
1.286     raeburn  2178:     %canmodify_status = 
                   2179:         &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
                   2180:                                                    ['inststatus'],$rolesarray);
1.253     raeburn  2181:     if (!$newuser) {
1.188     raeburn  2182:         # Get the users information
                   2183:         %userenv = &Apache::lonnet::get('environment',
                   2184:                    ['firstname','middlename','lastname','generation',
1.286     raeburn  2185:                     'permanentemail','id','inststatus'],$ccdomain,$ccuname);
1.219     raeburn  2186:         %canmodify =
                   2187:             &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
1.252     raeburn  2188:                                                        \@userinfo,$rolesarray);
1.257     raeburn  2189:     } elsif ($context eq 'selfcreate') {
                   2190:         %canmodify = &selfcreate_canmodify($context,$ccdomain,\@userinfo,
                   2191:                                            $inst_results,$rolesarray);
1.188     raeburn  2192:     }
                   2193:     my %lt=&Apache::lonlocal::texthash(
                   2194:                 'pd'             => "Personal Data",
                   2195:                 'firstname'      => "First Name",
                   2196:                 'middlename'     => "Middle Name",
                   2197:                 'lastname'       => "Last Name",
                   2198:                 'generation'     => "Generation",
                   2199:                 'permanentemail' => "Permanent e-mail address",
1.259     bisitz   2200:                 'id'             => "Student/Employee ID",
1.286     raeburn  2201:                 'lg'             => "Login Data",
                   2202:                 'inststatus'     => "Affiliation",
1.188     raeburn  2203:     );
                   2204:     my %textboxsize = (
                   2205:                        firstname      => '15',
                   2206:                        middlename     => '15',
                   2207:                        lastname       => '15',
                   2208:                        generation     => '5',
                   2209:                        permanentemail => '25',
                   2210:                        id             => '15',
                   2211:                       );
                   2212:     my $genhelp=&Apache::loncommon::help_open_topic('Generation');
                   2213:     $output = '<h3>'.$lt{'pd'}.'</h3>'.
                   2214:               &Apache::lonhtmlcommon::start_pick_box();
                   2215:     foreach my $item (@userinfo) {
                   2216:         my $rowtitle = $lt{$item};
1.252     raeburn  2217:         my $hiderow = 0;
1.188     raeburn  2218:         if ($item eq 'generation') {
                   2219:             $rowtitle = $genhelp.$rowtitle;
                   2220:         }
1.252     raeburn  2221:         my $row = &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
1.188     raeburn  2222:         if ($newuser) {
1.210     raeburn  2223:             if (ref($inst_results) eq 'HASH') {
                   2224:                 if ($inst_results->{$item} ne '') {
1.252     raeburn  2225:                     $row .= '<input type="hidden" name="c'.$item.'" value="'.$inst_results->{$item}.'" />'.$inst_results->{$item};
1.210     raeburn  2226:                 } else {
1.252     raeburn  2227:                     if ($context eq 'selfcreate') {
                   2228:                         if ($canmodify{$item}) { 
                   2229:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                   2230:                             $editable ++;
                   2231:                         } else {
                   2232:                             $hiderow = 1;
                   2233:                         }
1.253     raeburn  2234:                     } else {
                   2235:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2236:                     }
1.210     raeburn  2237:                 }
1.188     raeburn  2238:             } else {
1.252     raeburn  2239:                 if ($context eq 'selfcreate') {
1.287     raeburn  2240:                     if (($item eq 'permanentemail') && ($newuser eq 'email')) {
                   2241:                         $row .= $ccuname;
1.252     raeburn  2242:                     } else {
1.287     raeburn  2243:                         if ($canmodify{$item}) {
                   2244:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                   2245:                             $editable ++;
                   2246:                         } else {
                   2247:                             $hiderow = 1;
                   2248:                         }
1.252     raeburn  2249:                     }
1.253     raeburn  2250:                 } else {
                   2251:                     $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2252:                 }
1.188     raeburn  2253:             }
                   2254:         } else {
1.219     raeburn  2255:             if ($canmodify{$item}) {
1.252     raeburn  2256:                 $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
1.188     raeburn  2257:             } else {
1.252     raeburn  2258:                 $row .= $userenv{$item};
1.188     raeburn  2259:             }
1.206     raeburn  2260:             if ($item eq 'id') {
1.219     raeburn  2261:                 $showforceid = $canmodify{$item};
                   2262:             }
1.188     raeburn  2263:         }
1.252     raeburn  2264:         $row .= &Apache::lonhtmlcommon::row_closure(1);
                   2265:         if (!$hiderow) {
                   2266:             $output .= $row;
                   2267:             $rowcount ++;
                   2268:         }
1.188     raeburn  2269:     }
1.286     raeburn  2270:     if (($canmodify_status{'inststatus'}) || ($context ne 'selfcreate')) {
                   2271:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($ccdomain);
                   2272:         if (ref($types) eq 'ARRAY') {
                   2273:             if (@{$types} > 0) {
                   2274:                 my ($hiderow,$shown);
                   2275:                 if ($canmodify_status{'inststatus'}) {
                   2276:                     $shown = &pick_inst_statuses($userenv{'inststatus'},$usertypes,$types);
                   2277:                 } else {
                   2278:                     if ($userenv{'inststatus'} eq '') {
                   2279:                         $hiderow = 1;
1.334     raeburn  2280:                     } else {
                   2281:                         my @showitems;
                   2282:                         foreach my $item ( map { &unescape($_); } split(':',$userenv{'inststatus'})) {
                   2283:                             if (exists($usertypes->{$item})) {
                   2284:                                 push(@showitems,$usertypes->{$item});
                   2285:                             } else {
                   2286:                                 push(@showitems,$item);
                   2287:                             }
                   2288:                         }
                   2289:                         if (@showitems) {
                   2290:                             $shown = join(', ',@showitems);
                   2291:                         } else {
                   2292:                             $hiderow = 1;
                   2293:                         }
1.286     raeburn  2294:                     }
                   2295:                 }
                   2296:                 if (!$hiderow) {
                   2297:                     my $row = &Apache::lonhtmlcommon::row_title(&mt('Affliations'),undef,'LC_oddrow_value')."\n".
                   2298:                               $shown.&Apache::lonhtmlcommon::row_closure(1); 
                   2299:                     if ($context eq 'selfcreate') {
                   2300:                         $rowcount ++;
                   2301:                     }
                   2302:                     $output .= $row;
                   2303:                 }
                   2304:             }
                   2305:         }
                   2306:     }
1.188     raeburn  2307:     $output .= &Apache::lonhtmlcommon::end_pick_box();
1.206     raeburn  2308:     if (wantarray) {
1.252     raeburn  2309:         if ($context eq 'selfcreate') {
                   2310:             return($output,$rowcount,$editable);
                   2311:         } else {
                   2312:             return ($output,$showforceid);
                   2313:         }
1.206     raeburn  2314:     } else {
                   2315:         return $output;
                   2316:     }
1.188     raeburn  2317: }
                   2318: 
1.286     raeburn  2319: sub pick_inst_statuses {
                   2320:     my ($curr,$usertypes,$types) = @_;
                   2321:     my ($output,$rem,@currtypes);
                   2322:     if ($curr ne '') {
                   2323:         @currtypes = map { &unescape($_); } split(/:/,$curr);
                   2324:     }
                   2325:     my $numinrow = 2;
                   2326:     if (ref($types) eq 'ARRAY') {
                   2327:         $output = '<table>';
                   2328:         my $lastcolspan; 
                   2329:         for (my $i=0; $i<@{$types}; $i++) {
                   2330:             if (defined($usertypes->{$types->[$i]})) {
                   2331:                 my $rem = $i%($numinrow);
                   2332:                 if ($rem == 0) {
                   2333:                     if ($i<@{$types}-1) {
                   2334:                         if ($i > 0) { 
                   2335:                             $output .= '</tr>';
                   2336:                         }
                   2337:                         $output .= '<tr>';
                   2338:                     }
                   2339:                 } elsif ($i==@{$types}-1) {
                   2340:                     my $colsleft = $numinrow - $rem;
                   2341:                     if ($colsleft > 1) {
                   2342:                         $lastcolspan = ' colspan="'.$colsleft.'"';
                   2343:                     }
                   2344:                 }
                   2345:                 my $check = ' ';
                   2346:                 if (grep(/^\Q$types->[$i]\E$/,@currtypes)) {
                   2347:                     $check = ' checked="checked" ';
                   2348:                 }
                   2349:                 $output .= '<td class="LC_left_item"'.$lastcolspan.'>'.
                   2350:                            '<span class="LC_nobreak"><label>'.
                   2351:                            '<input type="checkbox" name="inststatus" '.
                   2352:                            'value="'.$types->[$i].'"'.$check.'/>'.
                   2353:                            $usertypes->{$types->[$i]}.'</label></span></td>';
                   2354:             }
                   2355:         }
                   2356:         $output .= '</tr></table>';
                   2357:     }
                   2358:     return $output;
                   2359: }
                   2360: 
1.257     raeburn  2361: sub selfcreate_canmodify {
                   2362:     my ($context,$dom,$userinfo,$inst_results,$rolesarray) = @_;
                   2363:     if (ref($inst_results) eq 'HASH') {
                   2364:         my @inststatuses = &get_inststatuses($inst_results);
                   2365:         if (@inststatuses == 0) {
                   2366:             @inststatuses = ('default');
                   2367:         }
                   2368:         $rolesarray = \@inststatuses;
                   2369:     }
                   2370:     my %canmodify =
                   2371:         &Apache::lonuserutils::can_modify_userinfo($context,$dom,$userinfo,
                   2372:                                                    $rolesarray);
                   2373:     return %canmodify;
                   2374: }
                   2375: 
1.252     raeburn  2376: sub get_inststatuses {
                   2377:     my ($insthashref) = @_;
                   2378:     my @inststatuses = ();
                   2379:     if (ref($insthashref) eq 'HASH') {
                   2380:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
                   2381:             @inststatuses = @{$insthashref->{'inststatus'}};
                   2382:         }
                   2383:     }
                   2384:     return @inststatuses;
                   2385: }
                   2386: 
1.4       www      2387: # ================================================================= Phase Three
1.42      matthew  2388: sub update_user_data {
1.375     raeburn  2389:     my ($r,$context,$crstype,$brcrum,$showcredits) = @_; 
1.101     albertel 2390:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                   2391:                                           $env{'form.ccdomain'});
1.27      matthew  2392:     # Error messages
1.188     raeburn  2393:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
1.193     raeburn  2394:     my $end       = '</span><br /><br />';
                   2395:     my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
1.188     raeburn  2396:                     "'$env{'form.prevphase'}','modify')".'" />'.
1.219     raeburn  2397:                     &mt('Return to previous page').'</a>'.
                   2398:                     &Apache::loncommon::end_page();
                   2399:     my $now = time;
1.40      www      2400:     my $title;
1.101     albertel 2401:     if (exists($env{'form.makeuser'})) {
1.40      www      2402: 	$title='Set Privileges for New User';
                   2403:     } else {
                   2404:         $title='Modify User Privileges';
                   2405:     }
1.213     raeburn  2406:     my $newuser = 0;
1.160     raeburn  2407:     my ($jsback,$elements) = &crumb_utilities();
                   2408:     my $jscript = '<script type="text/javascript">'."\n".
1.301     bisitz   2409:                   '// <![CDATA['."\n".
                   2410:                   $jsback."\n".
                   2411:                   '// ]]>'."\n".
                   2412:                   '</script>'."\n";
1.318     raeburn  2413:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.351     raeburn  2414:     push (@{$brcrum},
                   2415:              {href => "javascript:backPage(document.userupdate)",
                   2416:               text => $breadcrumb_text{'search'},
                   2417:               faq  => 282,
                   2418:               bug  => 'Instructor Interface',}
                   2419:              );
                   2420:     if ($env{'form.prevphase'} eq 'userpicked') {
                   2421:         push(@{$brcrum},
                   2422:                {href => "javascript:backPage(document.userupdate,'get_user_info','select')",
                   2423:                 text => $breadcrumb_text{'userpicked'},
                   2424:                 faq  => 282,
                   2425:                 bug  => 'Instructor Interface',});
1.233     raeburn  2426:     }
1.224     raeburn  2427:     my $helpitem = 'Course_Change_Privileges';
                   2428:     if ($env{'form.action'} eq 'singlestudent') {
                   2429:         $helpitem = 'Course_Add_Student';
                   2430:     }
1.351     raeburn  2431:     push(@{$brcrum}, 
                   2432:             {href => "javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
                   2433:              text => $breadcrumb_text{'modify'},
                   2434:              faq  => 282,
                   2435:              bug  => 'Instructor Interface',},
                   2436:             {href => "/adm/createuser",
                   2437:              text => "Result",
                   2438:              faq  => 282,
                   2439:              bug  => 'Instructor Interface',
                   2440:              help => $helpitem});
                   2441:     my $args = {bread_crumbs          => $brcrum,
                   2442:                 bread_crumbs_component => 'User Management'};
                   2443:     if ($env{'form.popup'}) {
                   2444:         $args->{'no_nav_bar'} = 1;
                   2445:     }
                   2446:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
1.188     raeburn  2447:     $r->print(&update_result_form($uhome));
1.27      matthew  2448:     # Check Inputs
1.101     albertel 2449:     if (! $env{'form.ccuname'} ) {
1.193     raeburn  2450: 	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
1.27      matthew  2451: 	return;
                   2452:     }
1.138     albertel 2453:     if (  $env{'form.ccuname'} ne 
                   2454: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
1.281     bisitz   2455: 	$r->print($error.&mt('Invalid login name.').'  '.
                   2456: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid.').
1.193     raeburn  2457: 		  $end.$rtnlink);
1.27      matthew  2458: 	return;
                   2459:     }
1.101     albertel 2460:     if (! $env{'form.ccdomain'}       ) {
1.193     raeburn  2461: 	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
1.27      matthew  2462: 	return;
                   2463:     }
1.138     albertel 2464:     if (  $env{'form.ccdomain'} ne
                   2465: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
1.281     bisitz   2466: 	$r->print($error.&mt('Invalid domain name.').'  '.
                   2467: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid.').
1.193     raeburn  2468: 		  $end.$rtnlink);
1.27      matthew  2469: 	return;
                   2470:     }
1.219     raeburn  2471:     if ($uhome eq 'no_host') {
                   2472:         $newuser = 1;
                   2473:     }
1.101     albertel 2474:     if (! exists($env{'form.makeuser'})) {
1.29      matthew  2475:         # Modifying an existing user, so check the validity of the name
                   2476:         if ($uhome eq 'no_host') {
1.73      sakharuk 2477:             $r->print($error.&mt('Unable to determine home server for ').
1.101     albertel 2478:                       $env{'form.ccuname'}.&mt(' in domain ').
                   2479:                       $env{'form.ccdomain'}.'.');
1.29      matthew  2480:             return;
                   2481:         }
                   2482:     }
1.27      matthew  2483:     # Determine authentication method and password for the user being modified
                   2484:     my $amode='';
                   2485:     my $genpwd='';
1.101     albertel 2486:     if ($env{'form.login'} eq 'krb') {
1.41      albertel 2487: 	$amode='krb';
1.101     albertel 2488: 	$amode.=$env{'form.krbver'};
                   2489: 	$genpwd=$env{'form.krbarg'};
                   2490:     } elsif ($env{'form.login'} eq 'int') {
1.27      matthew  2491: 	$amode='internal';
1.101     albertel 2492: 	$genpwd=$env{'form.intarg'};
                   2493:     } elsif ($env{'form.login'} eq 'fsys') {
1.27      matthew  2494: 	$amode='unix';
1.101     albertel 2495: 	$genpwd=$env{'form.fsysarg'};
                   2496:     } elsif ($env{'form.login'} eq 'loc') {
1.27      matthew  2497: 	$amode='localauth';
1.101     albertel 2498: 	$genpwd=$env{'form.locarg'};
1.27      matthew  2499: 	$genpwd=" " if (!$genpwd);
1.101     albertel 2500:     } elsif (($env{'form.login'} eq 'nochange') ||
                   2501:              ($env{'form.login'} eq ''        )) { 
1.34      matthew  2502:         # There is no need to tell the user we did not change what they
                   2503:         # did not ask us to change.
1.35      matthew  2504:         # If they are creating a new user but have not specified login
                   2505:         # information this will be caught below.
1.30      matthew  2506:     } else {
1.367     golterma 2507:             $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);
                   2508:             return;
1.27      matthew  2509:     }
1.164     albertel 2510: 
1.188     raeburn  2511:     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
1.367     golterma 2512:                         $env{'form.ccuname'}.' ('.&Apache::loncommon::plainname($env{'form.ccuname'},
                   2513:                         $env{'form.ccdomain'}).')', $env{'form.ccdomain'}).'</h3>');
                   2514:     my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,2);
1.344     bisitz   2515: 
1.193     raeburn  2516:     my (%alerts,%rulematch,%inst_results,%curr_rules);
1.334     raeburn  2517:     my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
1.361     raeburn  2518:     my @usertools = ('aboutme','blog','webdav','portfolio');
1.384     raeburn  2519:     my @requestcourses = ('official','unofficial','community','textbook');
1.362     raeburn  2520:     my @requestauthor = ('requestauthor');
1.286     raeburn  2521:     my ($othertitle,$usertypes,$types) = 
                   2522:         &Apache::loncommon::sorted_inst_types($env{'form.ccdomain'});
1.334     raeburn  2523:     my %canmodify_status =
                   2524:         &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},
                   2525:                                                    ['inststatus']);
1.101     albertel 2526:     if ($env{'form.makeuser'}) {
1.164     albertel 2527: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
1.27      matthew  2528:         # Check for the authentication mode and password
                   2529:         if (! $amode || ! $genpwd) {
1.193     raeburn  2530: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.27      matthew  2531: 	    return;
1.18      albertel 2532: 	}
1.29      matthew  2533:         # Determine desired host
1.101     albertel 2534:         my $desiredhost = $env{'form.hserver'};
1.29      matthew  2535:         if (lc($desiredhost) eq 'default') {
                   2536:             $desiredhost = undef;
                   2537:         } else {
1.147     albertel 2538:             my %home_servers = 
                   2539: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
1.29      matthew  2540:             if (! exists($home_servers{$desiredhost})) {
1.193     raeburn  2541:                 $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
                   2542:                 return;
                   2543:             }
                   2544:         }
                   2545:         # Check ID format
                   2546:         my %checkhash;
                   2547:         my %checks = ('id' => 1);
                   2548:         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
1.219     raeburn  2549:             'newuser' => $newuser, 
1.196     raeburn  2550:             'id' => $env{'form.cid'},
1.193     raeburn  2551:         );
1.196     raeburn  2552:         if ($env{'form.cid'} ne '') {
                   2553:             &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   2554:                                           \%rulematch,\%inst_results,\%curr_rules);
                   2555:             if (ref($alerts{'id'}) eq 'HASH') {
                   2556:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
                   2557:                     my $domdesc =
                   2558:                         &Apache::lonnet::domain($env{'form.ccdomain'},'description');
                   2559:                     if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
                   2560:                         my $userchkmsg;
                   2561:                         if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
                   2562:                             $userchkmsg  = 
                   2563:                                 &Apache::loncommon::instrule_disallow_msg('id',
                   2564:                                                                     $domdesc,1).
                   2565:                                 &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
                   2566:                                     $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
                   2567:                         }
                   2568:                         $r->print($error.&mt('Invalid ID format').$end.
                   2569:                                   $userchkmsg.$rtnlink);
                   2570:                         return;
                   2571:                     }
                   2572:                 }
1.29      matthew  2573:             }
                   2574:         }
1.367     golterma 2575:         &Apache::lonhtmlcommon::Increment_PrgWin($r, \%prog_state);
1.27      matthew  2576: 	# Call modifyuser
                   2577: 	my $result = &Apache::lonnet::modifyuser
1.193     raeburn  2578: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
1.188     raeburn  2579:              $amode,$genpwd,$env{'form.cfirstname'},
                   2580:              $env{'form.cmiddlename'},$env{'form.clastname'},
                   2581:              $env{'form.cgeneration'},undef,$desiredhost,
                   2582:              $env{'form.cpermanentemail'});
1.77      www      2583: 	$r->print(&mt('Generating user').': '.$result);
1.219     raeburn  2584:         $uhome = &Apache::lonnet::homeserver($env{'form.ccuname'},
1.101     albertel 2585:                                                $env{'form.ccdomain'});
1.334     raeburn  2586:         my (%changeHash,%newcustom,%changed,%changedinfo);
1.267     raeburn  2587:         if ($uhome ne 'no_host') {
1.334     raeburn  2588:             if ($context eq 'domain') {
1.378     raeburn  2589:                 foreach my $name ('portfolio','author') {
                   2590:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   2591:                         if ($env{'form.'.$name.'quota'} eq '') {
                   2592:                             $newcustom{$name.'quota'} = 0;
                   2593:                         } else {
                   2594:                             $newcustom{$name.'quota'} = $env{'form.'.$name.'quota'};
                   2595:                             $newcustom{$name.'quota'} =~ s/[^\d\.]//g;
                   2596:                         }
                   2597:                         if (&quota_admin($newcustom{$name.'quota'},\%changeHash,$name)) {
                   2598:                             $changed{$name.'quota'} = 1;
                   2599:                         }
1.334     raeburn  2600:                     }
                   2601:                 }
                   2602:                 foreach my $item (@usertools) {
                   2603:                     if ($env{'form.custom'.$item} == 1) {
                   2604:                         $newcustom{$item} = $env{'form.tools_'.$item};
                   2605:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2606:                                                      \%changeHash,'tools');
                   2607:                     }
1.267     raeburn  2608:                 }
1.334     raeburn  2609:                 foreach my $item (@requestcourses) {
1.341     raeburn  2610:                     if ($env{'form.custom'.$item} == 1) {
                   2611:                         $newcustom{$item} = $env{'form.crsreq_'.$item};
                   2612:                         if ($env{'form.crsreq_'.$item} eq 'autolimit') {
                   2613:                             $newcustom{$item} .= '=';
1.383     raeburn  2614:                             $env{'form.crsreq_'.$item.'_limit'} =~ s/\D+//g;
                   2615:                             if ($env{'form.crsreq_'.$item.'_limit'}) {
1.341     raeburn  2616:                                 $newcustom{$item} .= $env{'form.crsreq_'.$item.'_limit'};
                   2617:                             }
1.334     raeburn  2618:                         }
1.341     raeburn  2619:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2620:                                                       \%changeHash,'requestcourses');
1.334     raeburn  2621:                     }
1.275     raeburn  2622:                 }
1.362     raeburn  2623:                 if ($env{'form.customrequestauthor'} == 1) {
                   2624:                     $newcustom{'requestauthor'} = $env{'form.requestauthor'};
                   2625:                     $changed{'requestauthor'} = &tool_admin('requestauthor',
                   2626:                                                     $newcustom{'requestauthor'},
                   2627:                                                     \%changeHash,'requestauthor');
                   2628:                 }
1.275     raeburn  2629:             }
1.334     raeburn  2630:             if ($canmodify_status{'inststatus'}) {
                   2631:                 if (exists($env{'form.inststatus'})) {
                   2632:                     my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2633:                     if (@inststatuses > 0) {
                   2634:                         $changeHash{'inststatus'} = join(',',@inststatuses);
                   2635:                         $changed{'inststatus'} = $changeHash{'inststatus'};
1.306     raeburn  2636:                     }
                   2637:                 }
1.232     raeburn  2638:             }
1.334     raeburn  2639:             if (keys(%changed)) {
                   2640:                 foreach my $item (@userinfo) {
                   2641:                     $changeHash{$item}  = $env{'form.c'.$item};
1.286     raeburn  2642:                 }
1.267     raeburn  2643:                 my $chgresult =
                   2644:                      &Apache::lonnet::put('environment',\%changeHash,
                   2645:                                           $env{'form.ccdomain'},$env{'form.ccuname'});
                   2646:             } 
1.232     raeburn  2647:         }
1.219     raeburn  2648:         $r->print('<br />'.&mt('Home server').': '.$uhome.' '.
                   2649:                   &Apache::lonnet::hostname($uhome));
1.101     albertel 2650:     } elsif (($env{'form.login'} ne 'nochange') &&
                   2651:              ($env{'form.login'} ne ''        )) {
1.27      matthew  2652: 	# Modify user privileges
                   2653:         if (! $amode || ! $genpwd) {
1.193     raeburn  2654: 	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
1.27      matthew  2655: 	    return;
1.20      harris41 2656: 	}
1.27      matthew  2657: 	# Only allow authentification modification if the person has authority
1.101     albertel 2658: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.20      harris41 2659: 	    $r->print('Modifying authentication: '.
1.31      matthew  2660:                       &Apache::lonnet::modifyuserauth(
1.101     albertel 2661: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
1.21      harris41 2662:                        $amode,$genpwd));
1.102     albertel 2663:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
1.101     albertel 2664: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
1.4       www      2665: 	} else {
1.27      matthew  2666: 	    # Okay, this is a non-fatal error.
1.193     raeburn  2667: 	    $r->print($error.&mt('You do not have the authority to modify this users authentification information').'.'.$end);    
1.27      matthew  2668: 	}
1.28      matthew  2669:     }
1.344     bisitz   2670:     $r->rflush(); # Finish display of header before time consuming actions start
1.367     golterma 2671:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state);
1.28      matthew  2672:     ##
1.375     raeburn  2673:     my (@userroles,%userupdate,$cnum,$cdom,$defaultcredits,%namechanged);
1.213     raeburn  2674:     if ($context eq 'course') {
1.375     raeburn  2675:         ($cnum,$cdom) =
                   2676:             &Apache::lonuserutils::get_course_identity();
1.318     raeburn  2677:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
1.375     raeburn  2678:         if ($showcredits) {
                   2679:            $defaultcredits = &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   2680:         }
1.213     raeburn  2681:     }
1.101     albertel 2682:     if (! $env{'form.makeuser'} ) {
1.28      matthew  2683:         # Check for need to change
                   2684:         my %userenv = &Apache::lonnet::get
1.134     raeburn  2685:             ('environment',['firstname','middlename','lastname','generation',
1.378     raeburn  2686:              'id','permanentemail','portfolioquota','authorquota','inststatus',
                   2687:              'tools.aboutme','tools.blog','tools.webdav','tools.portfolio',
1.361     raeburn  2688:              'requestcourses.official','requestcourses.unofficial',
1.384     raeburn  2689:              'requestcourses.community','requestcourses.textbook',
                   2690:              'reqcrsotherdom.official','reqcrsotherdom.unofficial',
                   2691:              'reqcrsotherdom.community','reqcrsotherdom.textbook',
1.362     raeburn  2692:              'requestauthor'],
1.160     raeburn  2693:               $env{'form.ccdomain'},$env{'form.ccuname'});
1.28      matthew  2694:         my ($tmp) = keys(%userenv);
                   2695:         if ($tmp =~ /^(con_lost|error)/i) { 
                   2696:             %userenv = ();
                   2697:         }
1.206     raeburn  2698:         my $no_forceid_alert;
                   2699:         # Check to see if user information can be changed
                   2700:         my %domconfig =
                   2701:             &Apache::lonnet::get_dom('configuration',['usermodification'],
                   2702:                                      $env{'form.ccdomain'});
1.213     raeburn  2703:         my @statuses = ('active','future');
                   2704:         my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@statuses,undef,$env{'request.role.domain'});
                   2705:         my ($auname,$audom);
1.220     raeburn  2706:         if ($context eq 'author') {
1.206     raeburn  2707:             $auname = $env{'user.name'};
                   2708:             $audom = $env{'user.domain'};     
                   2709:         }
                   2710:         foreach my $item (keys(%roles)) {
1.220     raeburn  2711:             my ($rolenum,$roledom,$role) = split(/:/,$item,-1);
1.206     raeburn  2712:             if ($context eq 'course') {
                   2713:                 if ($cnum ne '' && $cdom ne '') {
                   2714:                     if ($rolenum eq $cnum && $roledom eq $cdom) {
                   2715:                         if (!grep(/^\Q$role\E$/,@userroles)) {
                   2716:                             push(@userroles,$role);
                   2717:                         }
                   2718:                     }
                   2719:                 }
                   2720:             } elsif ($context eq 'author') {
                   2721:                 if ($rolenum eq $auname && $roledom eq $audom) {
                   2722:                     if (!grep(/^\Q$role\E$/,@userroles)) { 
                   2723:                         push(@userroles,$role);
                   2724:                     }
                   2725:                 }
                   2726:             }
                   2727:         }
1.220     raeburn  2728:         if ($env{'form.action'} eq 'singlestudent') {
                   2729:             if (!grep(/^st$/,@userroles)) {
                   2730:                 push(@userroles,'st');
                   2731:             }
                   2732:         } else {
                   2733:             # Check for course or co-author roles being activated or re-enabled
                   2734:             if ($context eq 'author' || $context eq 'course') {
                   2735:                 foreach my $key (keys(%env)) {
                   2736:                     if ($context eq 'author') {
                   2737:                         if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
                   2738:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2739:                                 push(@userroles,$1);
                   2740:                             }
                   2741:                         } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
                   2742:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2743:                                 push(@userroles,$1);
                   2744:                             }
1.206     raeburn  2745:                         }
1.220     raeburn  2746:                     } elsif ($context eq 'course') {
                   2747:                         if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
                   2748:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2749:                                 push(@userroles,$1);
                   2750:                             }
                   2751:                         } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
                   2752:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2753:                                 push(@userroles,$1);
                   2754:                             }
1.206     raeburn  2755:                         }
                   2756:                     }
                   2757:                 }
                   2758:             }
                   2759:         }
                   2760:         #Check to see if we can change personal data for the user 
                   2761:         my (@mod_disallowed,@longroles);
                   2762:         foreach my $role (@userroles) {
                   2763:             if ($role eq 'cr') {
                   2764:                 push(@longroles,'Custom');
                   2765:             } else {
1.318     raeburn  2766:                 push(@longroles,&Apache::lonnet::plaintext($role,$crstype)); 
1.206     raeburn  2767:             }
                   2768:         }
1.219     raeburn  2769:         my %canmodify = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},\@userinfo,\@userroles);
                   2770:         foreach my $item (@userinfo) {
1.28      matthew  2771:             # Strip leading and trailing whitespace
1.203     raeburn  2772:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
1.219     raeburn  2773:             if (!$canmodify{$item}) {
1.207     raeburn  2774:                 if (defined($env{'form.c'.$item})) {
                   2775:                     if ($env{'form.c'.$item} ne $userenv{$item}) {
                   2776:                         push(@mod_disallowed,$item);
                   2777:                     }
1.206     raeburn  2778:                 }
                   2779:                 $env{'form.c'.$item} = $userenv{$item};
                   2780:             }
1.28      matthew  2781:         }
1.259     bisitz   2782:         # Check to see if we can change the Student/Employee ID
1.196     raeburn  2783:         my $forceid = $env{'form.forceid'};
                   2784:         my $recurseid = $env{'form.recurseid'};
                   2785:         my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
1.203     raeburn  2786:         my %uidhash = &Apache::lonnet::idrget($env{'form.ccdomain'},
                   2787:                                             $env{'form.ccuname'});
                   2788:         if (($uidhash{$env{'form.ccuname'}}) && 
                   2789:             ($uidhash{$env{'form.ccuname'}}!~/error\:/) && 
                   2790:             (!$forceid)) {
                   2791:             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
                   2792:                 $env{'form.cid'} = $userenv{'id'};
1.293     bisitz   2793:                 $no_forceid_alert = &mt('New student/employee ID does not match existing ID for this user.')
1.259     bisitz   2794:                                    .'<br />'
                   2795:                                    .&mt("Change is not permitted without checking the 'Force ID change' checkbox on the previous page.")
                   2796:                                    .'<br />'."\n";
1.203     raeburn  2797:             }
                   2798:         }
                   2799:         if ($env{'form.cid'} ne $userenv{'id'}) {
1.196     raeburn  2800:             my $checkhash;
                   2801:             my $checks = { 'id' => 1 };
                   2802:             $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
                   2803:                    { 'newuser' => $newuser,
                   2804:                      'id'  => $env{'form.cid'}, 
                   2805:                    };
                   2806:             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   2807:                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
                   2808:             if (ref($alerts{'id'}) eq 'HASH') {
                   2809:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
1.203     raeburn  2810:                    $env{'form.cid'} = $userenv{'id'};
1.196     raeburn  2811:                 }
                   2812:             }
                   2813:         }
1.378     raeburn  2814:         my (%quotachanged,%oldquota,%newquota,%olddefquota,%newdefquota, 
                   2815:             $oldinststatus,$newinststatus,%oldisdefault,%newisdefault,%oldsettings,
1.339     raeburn  2816:             %oldsettingstext,%newsettings,%newsettingstext,@disporder,
1.378     raeburn  2817:             %oldsettingstatus,%newsettingstatus);
1.334     raeburn  2818:         @disporder = ('inststatus');
                   2819:         if ($env{'request.role.domain'} eq $env{'form.ccdomain'}) {
1.362     raeburn  2820:             push(@disporder,'requestcourses','requestauthor');
1.334     raeburn  2821:         } else {
                   2822:             push(@disporder,'reqcrsotherdom');
                   2823:         }
                   2824:         push(@disporder,('quota','tools'));
1.338     raeburn  2825:         $oldinststatus = $userenv{'inststatus'};
1.378     raeburn  2826:         foreach my $name ('portfolio','author') {
                   2827:             ($olddefquota{$name},$oldsettingstatus{$name}) = 
                   2828:                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$oldinststatus,$name);
                   2829:             ($newdefquota{$name},$newsettingstatus{$name}) = ($olddefquota{$name},$oldsettingstatus{$name});
                   2830:         }
1.334     raeburn  2831:         my %canshow;
1.220     raeburn  2832:         if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
1.334     raeburn  2833:             $canshow{'quota'} = 1;
1.220     raeburn  2834:         }
1.267     raeburn  2835:         if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
1.334     raeburn  2836:             $canshow{'tools'} = 1;
1.267     raeburn  2837:         }
1.275     raeburn  2838:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
1.334     raeburn  2839:             $canshow{'requestcourses'} = 1;
1.300     raeburn  2840:         } elsif (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.334     raeburn  2841:             $canshow{'reqcrsotherdom'} = 1;
1.275     raeburn  2842:         }
1.286     raeburn  2843:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.334     raeburn  2844:             $canshow{'inststatus'} = 1;
1.286     raeburn  2845:         }
1.362     raeburn  2846:         if (&Apache::lonnet::allowed('cau',$env{'form.ccdomain'})) {
                   2847:             $canshow{'requestauthor'} = 1;
                   2848:         }
1.267     raeburn  2849:         my (%changeHash,%changed);
1.286     raeburn  2850:         if ($oldinststatus eq '') {
1.334     raeburn  2851:             $oldsettings{'inststatus'} = $othertitle; 
1.286     raeburn  2852:         } else {
                   2853:             if (ref($usertypes) eq 'HASH') {
1.334     raeburn  2854:                 $oldsettings{'inststatus'} = join(', ',map{ $usertypes->{ &unescape($_) }; } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  2855:             } else {
1.334     raeburn  2856:                 $oldsettings{'inststatus'} = join(', ',map{ &unescape($_); } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  2857:             }
                   2858:         }
                   2859:         $changeHash{'inststatus'} = $userenv{'inststatus'};
1.334     raeburn  2860:         if ($canmodify_status{'inststatus'}) {
                   2861:             $canshow{'inststatus'} = 1;
1.286     raeburn  2862:             if (exists($env{'form.inststatus'})) {
                   2863:                 my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2864:                 if (@inststatuses > 0) {
                   2865:                     $newinststatus = join(':',map { &escape($_); } @inststatuses);
                   2866:                     $changeHash{'inststatus'} = $newinststatus;
                   2867:                     if ($newinststatus ne $oldinststatus) {
                   2868:                         $changed{'inststatus'} = $newinststatus;
1.378     raeburn  2869:                         foreach my $name ('portfolio','author') {
                   2870:                             ($newdefquota{$name},$newsettingstatus{$name}) =
                   2871:                                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   2872:                         }
1.286     raeburn  2873:                     }
                   2874:                     if (ref($usertypes) eq 'HASH') {
1.334     raeburn  2875:                         $newsettings{'inststatus'} = join(', ',map{ $usertypes->{$_}; } (@inststatuses)); 
1.286     raeburn  2876:                     } else {
1.337     raeburn  2877:                         $newsettings{'inststatus'} = join(', ',@inststatuses);
1.286     raeburn  2878:                     }
1.334     raeburn  2879:                 }
                   2880:             } else {
                   2881:                 $newinststatus = '';
                   2882:                 $changeHash{'inststatus'} = $newinststatus;
                   2883:                 $newsettings{'inststatus'} = $othertitle;
                   2884:                 if ($newinststatus ne $oldinststatus) {
                   2885:                     $changed{'inststatus'} = $changeHash{'inststatus'};
1.378     raeburn  2886:                     foreach my $name ('portfolio','author') {
                   2887:                         ($newdefquota{$name},$newsettingstatus{$name}) =
                   2888:                             &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
                   2889:                     }
1.286     raeburn  2890:                 }
                   2891:             }
1.334     raeburn  2892:         } elsif ($context ne 'selfcreate') {
                   2893:             $canshow{'inststatus'} = 1;
1.337     raeburn  2894:             $newsettings{'inststatus'} = $oldsettings{'inststatus'};
1.286     raeburn  2895:         }
1.378     raeburn  2896:         foreach my $name ('portfolio','author') {
                   2897:             $changeHash{$name.'quota'} = $userenv{$name.'quota'};
                   2898:         }
1.334     raeburn  2899:         if ($context eq 'domain') {
1.378     raeburn  2900:             foreach my $name ('portfolio','author') {
                   2901:                 if ($userenv{$name.'quota'} ne '') {
                   2902:                     $oldquota{$name} = $userenv{$name.'quota'};
                   2903:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   2904:                         if ($env{'form.'.$name.'quota'} eq '') {
                   2905:                             $newquota{$name} = 0;
                   2906:                         } else {
                   2907:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   2908:                             $newquota{$name} =~ s/[^\d\.]//g;
                   2909:                         }
                   2910:                         if ($newquota{$name} != $oldquota{$name}) {
                   2911:                             if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   2912:                                 $changed{$name.'quota'} = 1;
                   2913:                             }
                   2914:                         }
1.334     raeburn  2915:                     } else {
1.378     raeburn  2916:                         if (&quota_admin('',\%changeHash,$name)) {
                   2917:                             $changed{$name.'quota'} = 1;
                   2918:                             $newquota{$name} = $newdefquota{$name};
                   2919:                             $newisdefault{$name} = 1;
                   2920:                         }
1.334     raeburn  2921:                     }
1.149     raeburn  2922:                 } else {
1.378     raeburn  2923:                     $oldisdefault{$name} = 1;
                   2924:                     $oldquota{$name} = $olddefquota{$name};
                   2925:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
                   2926:                         if ($env{'form.'.$name.'quota'} eq '') {
                   2927:                             $newquota{$name} = 0;
                   2928:                         } else {
                   2929:                             $newquota{$name} = $env{'form.'.$name.'quota'};
                   2930:                             $newquota{$name} =~ s/[^\d\.]//g;
                   2931:                         }
                   2932:                         if (&quota_admin($newquota{$name},\%changeHash,$name)) {
                   2933:                             $changed{$name.'quota'} = 1;
                   2934:                         }
1.334     raeburn  2935:                     } else {
1.378     raeburn  2936:                         $newquota{$name} = $newdefquota{$name};
                   2937:                         $newisdefault{$name} = 1;
1.334     raeburn  2938:                     }
1.378     raeburn  2939:                 }
                   2940:                 if ($oldisdefault{$name}) {
                   2941:                     $oldsettingstext{'quota'}{$name} = &get_defaultquota_text($oldsettingstatus{$name});
1.383     raeburn  2942:                 }  else {
                   2943:                     $oldsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$oldquota{$name});
1.378     raeburn  2944:                 }
                   2945:                 if ($newisdefault{$name}) {
                   2946:                     $newsettingstext{'quota'}{$name} = &get_defaultquota_text($newsettingstatus{$name});
1.383     raeburn  2947:                 } else {
                   2948:                     $newsettingstext{'quota'}{$name} = &mt('custom quota: [_1] MB',$newquota{$name});
1.134     raeburn  2949:                 }
                   2950:             }
1.334     raeburn  2951:             &tool_changes('tools',\@usertools,\%oldsettings,\%oldsettingstext,\%userenv,
                   2952:                           \%changeHash,\%changed,\%newsettings,\%newsettingstext);
                   2953:             if ($env{'form.ccdomain'} eq $env{'request.role.domain'}) {
                   2954:                 &tool_changes('requestcourses',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   2955:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.384     raeburn  2956:                 &tool_changes('requestauthor',\@requestauthor,\%oldsettings,\%oldsettingstext,
                   2957:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  2958:             } else {
1.334     raeburn  2959:                 &tool_changes('reqcrsotherdom',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   2960:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  2961:             }
                   2962:         }
1.334     raeburn  2963:         foreach my $item (@userinfo) {
                   2964:             if ($env{'form.c'.$item} ne $userenv{$item}) {
                   2965:                 $namechanged{$item} = 1;
                   2966:             }
1.204     raeburn  2967:         }
1.378     raeburn  2968:         foreach my $name ('portfolio','author') {
1.383     raeburn  2969:             $oldsettings{'quota'}{$name} = $oldquota{$name}.' MB';
                   2970:             $newsettings{'quota'}{$name} = $newquota{$name}.' MB';
1.378     raeburn  2971:         }
1.334     raeburn  2972:         if ((keys(%namechanged) > 0) || (keys(%changed) > 0)) {
1.267     raeburn  2973:             my ($chgresult,$namechgresult);
                   2974:             if (keys(%changed) > 0) {
                   2975:                 $chgresult = 
1.204     raeburn  2976:                     &Apache::lonnet::put('environment',\%changeHash,
                   2977:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
1.267     raeburn  2978:                 if ($chgresult eq 'ok') {
                   2979:                     if (($env{'user.name'} eq $env{'form.ccuname'}) &&
                   2980:                         ($env{'user.domain'} eq $env{'form.ccdomain'})) {
1.270     raeburn  2981:                         my %newenvhash;
                   2982:                         foreach my $key (keys(%changed)) {
1.299     raeburn  2983:                             if (($key eq 'official') || ($key eq 'unofficial')
                   2984:                                 || ($key eq 'community')) {
1.279     raeburn  2985:                                 $newenvhash{'environment.requestcourses.'.$key} =
                   2986:                                     $changeHash{'requestcourses.'.$key};
1.362     raeburn  2987:                                 if ($changeHash{'requestcourses.'.$key}) {
1.332     raeburn  2988:                                     $newenvhash{'environment.canrequest.'.$key} = 1;
1.279     raeburn  2989:                                 } else {
                   2990:                                     $newenvhash{'environment.canrequest.'.$key} =
                   2991:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   2992:                                             $key,'reload','requestcourses');
                   2993:                                 }
1.362     raeburn  2994:                             } elsif ($key eq 'requestauthor') {
                   2995:                                 $newenvhash{'environment.'.$key} = $changeHash{$key};
                   2996:                                 if ($changeHash{$key}) {
                   2997:                                     $newenvhash{'environment.canrequest.author'} = 1;
                   2998:                                 } else {
                   2999:                                     $newenvhash{'environment.canrequest.author'} =
                   3000:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3001:                                             $key,'reload','requestauthor');
                   3002:                                 }
1.275     raeburn  3003:                             } elsif ($key ne 'quota') {
1.270     raeburn  3004:                                 $newenvhash{'environment.tools.'.$key} = 
                   3005:                                     $changeHash{'tools.'.$key};
1.279     raeburn  3006:                                 if ($changeHash{'tools.'.$key} ne '') {
                   3007:                                     $newenvhash{'environment.availabletools.'.$key} =
                   3008:                                         $changeHash{'tools.'.$key};
                   3009:                                 } else {
                   3010:                                     $newenvhash{'environment.availabletools.'.$key} =
1.367     golterma 3011:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   3012:           $key,'reload','tools');
1.279     raeburn  3013:                                 }
1.270     raeburn  3014:                             }
                   3015:                         }
1.271     raeburn  3016:                         if (keys(%newenvhash)) {
                   3017:                             &Apache::lonnet::appenv(\%newenvhash);
                   3018:                         }
1.267     raeburn  3019:                     }
                   3020:                 }
1.204     raeburn  3021:             }
1.334     raeburn  3022:             if (keys(%namechanged) > 0) {
1.337     raeburn  3023:                 foreach my $field (@userinfo) {
                   3024:                     $changeHash{$field}  = $env{'form.c'.$field};
                   3025:                 }
                   3026: # Make the change
1.204     raeburn  3027:                 $namechgresult =
                   3028:                     &Apache::lonnet::modifyuser($env{'form.ccdomain'},
                   3029:                         $env{'form.ccuname'},$changeHash{'id'},undef,undef,
                   3030:                         $changeHash{'firstname'},$changeHash{'middlename'},
                   3031:                         $changeHash{'lastname'},$changeHash{'generation'},
1.337     raeburn  3032:                         $changeHash{'id'},undef,$changeHash{'permanentemail'},undef,\@userinfo);
1.220     raeburn  3033:                 %userupdate = (
                   3034:                                lastname   => $env{'form.clastname'},
                   3035:                                middlename => $env{'form.cmiddlename'},
                   3036:                                firstname  => $env{'form.cfirstname'},
                   3037:                                generation => $env{'form.cgeneration'},
                   3038:                                id         => $env{'form.cid'},
                   3039:                              );
1.204     raeburn  3040:             }
1.334     raeburn  3041:             if (((keys(%namechanged) > 0) && $namechgresult eq 'ok') || 
1.267     raeburn  3042:                 ((keys(%changed) > 0) && $chgresult eq 'ok')) {
1.28      matthew  3043:             # Tell the user we changed the name
1.334     raeburn  3044:                 &display_userinfo($r,1,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3045:                                   \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,
1.334     raeburn  3046:                                   \%oldsettings, \%oldsettingstext,\%newsettings,
                   3047:                                   \%newsettingstext);
1.203     raeburn  3048:                 if ($env{'form.cid'} ne $userenv{'id'}) {
                   3049:                     &Apache::lonnet::idput($env{'form.ccdomain'},
                   3050:                          ($env{'form.ccuname'} => $env{'form.cid'}));
                   3051:                     if (($recurseid) &&
                   3052:                         (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
                   3053:                         my $idresult = 
                   3054:                             &Apache::lonuserutils::propagate_id_change(
                   3055:                                 $env{'form.ccuname'},$env{'form.ccdomain'},
                   3056:                                 \%userupdate);
                   3057:                         $r->print('<br />'.$idresult.'<br />');
                   3058:                     }
1.196     raeburn  3059:                 }
1.149     raeburn  3060:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
                   3061:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
                   3062:                     my %newenvhash;
                   3063:                     foreach my $key (keys(%changeHash)) {
                   3064:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3065:                     }
1.238     raeburn  3066:                     &Apache::lonnet::appenv(\%newenvhash);
1.149     raeburn  3067:                 }
1.28      matthew  3068:             } else { # error occurred
1.188     raeburn  3069:                 $r->print('<span class="LC_error">'.&mt('Unable to successfully change environment for').' '.
                   3070:                       $env{'form.ccuname'}.' '.&mt('in domain').' '.
1.206     raeburn  3071:                       $env{'form.ccdomain'}.'</span><br />');
1.28      matthew  3072:             }
1.334     raeburn  3073:         } else { # End of if ($env ... ) logic
1.275     raeburn  3074:             # They did not want to change the users name, quota, tool availability,
                   3075:             # or ability to request creation of courses, 
1.267     raeburn  3076:             # but we can still tell them what the name and quota and availabilities are  
1.334     raeburn  3077:             &display_userinfo($r,undef,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3078:                               \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,\%oldsettings,
1.334     raeburn  3079:                               \%oldsettingstext,\%newsettings,\%newsettingstext);
1.28      matthew  3080:         }
1.206     raeburn  3081:         if (@mod_disallowed) {
                   3082:             my ($rolestr,$contextname);
                   3083:             if (@longroles > 0) {
                   3084:                 $rolestr = join(', ',@longroles);
                   3085:             } else {
                   3086:                 $rolestr = &mt('No roles');
                   3087:             }
                   3088:             if ($context eq 'course') {
                   3089:                 $contextname = &mt('course');
                   3090:             } elsif ($context eq 'author') {
                   3091:                 $contextname = &mt('co-author');
                   3092:             }
                   3093:             $r->print(&mt('The following fields were not updated: ').'<ul>');
                   3094:             my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   3095:             foreach my $field (@mod_disallowed) {
                   3096:                 $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
                   3097:             }
1.207     raeburn  3098:             $r->print('</ul>');
                   3099:             if (@mod_disallowed == 1) {
                   3100:                 $r->print(&mt("You do not have the authority to change this field given the user's current set of active/future [_1] roles:",$contextname));
                   3101:             } else {
                   3102:                 $r->print(&mt("You do not have the authority to change these fields given the user's current set of active/future [_1] roles:",$contextname));
                   3103:             }
1.292     bisitz   3104:             my $helplink = 'javascript:helpMenu('."'display'".')';
                   3105:             $r->print('<span class="LC_cusr_emph">'.$rolestr.'</span><br />'
                   3106:                      .&mt('Please contact your [_1]helpdesk[_2] for more information.'
                   3107:                          ,'<a href="'.$helplink.'">','</a>')
                   3108:                       .'<br />');
1.206     raeburn  3109:         }
1.259     bisitz   3110:         $r->print('<span class="LC_warning">'
                   3111:                   .$no_forceid_alert
                   3112:                   .&Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules)
                   3113:                   .'</span>');
1.4       www      3114:     }
1.367     golterma 3115:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.220     raeburn  3116:     if ($env{'form.action'} eq 'singlestudent') {
1.375     raeburn  3117:         &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context,
                   3118:                                $crstype,$showcredits,$defaultcredits);
1.386   ! bisitz   3119:         my $linktext = ($crstype eq 'Community' ?
        !          3120:             &mt('Enroll Another Member') : &mt('Enroll Another Student'));
        !          3121:         $r->print(
        !          3122:             &Apache::lonhtmlcommon::actionbox([
        !          3123:                 '<a href="javascript:backPage(document.userupdate)">'
        !          3124:                .($crstype eq 'Community' ? 
        !          3125:                     &mt('Enroll Another Member') : &mt('Enroll Another Student'))
        !          3126:                .'</a>']));
1.220     raeburn  3127:     } else {
1.375     raeburn  3128:         my @rolechanges = &update_roles($r,$context,$showcredits);
1.334     raeburn  3129:         if (keys(%namechanged) > 0) {
1.220     raeburn  3130:             if ($context eq 'course') {
                   3131:                 if (@userroles > 0) {
1.225     raeburn  3132:                     if ((@rolechanges == 0) || 
                   3133:                         (!(grep(/^st$/,@rolechanges)))) {
                   3134:                         if (grep(/^st$/,@userroles)) {
                   3135:                             my $classlistupdated =
                   3136:                                 &Apache::lonuserutils::update_classlist($cdom,
1.220     raeburn  3137:                                               $cnum,$env{'form.ccdomain'},
                   3138:                                        $env{'form.ccuname'},\%userupdate);
1.225     raeburn  3139:                         }
1.220     raeburn  3140:                     }
                   3141:                 }
                   3142:             }
                   3143:         }
1.226     raeburn  3144:         my $userinfo = &Apache::loncommon::plainname($env{'form.ccuname'},
1.233     raeburn  3145:                                                      $env{'form.ccdomain'});
                   3146:         if ($env{'form.popup'}) {
                   3147:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   3148:         } else {
1.367     golterma 3149:             $r->print('<br />'.&Apache::lonhtmlcommon::actionbox(['<a href="javascript:backPage(document.userupdate,'."'$env{'form.prevphase'}','modify'".')">'
                   3150:                      .&mt('Modify this user: [_1]','<span class="LC_cusr_emph">'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.' ('.$userinfo.')</span>').'</a>',
                   3151:                      '<a href="javascript:backPage(document.userupdate)">'.&mt('Create/Modify Another User').'</a>']));
1.233     raeburn  3152:         }
1.220     raeburn  3153:     }
                   3154: }
                   3155: 
1.334     raeburn  3156: sub display_userinfo {
1.362     raeburn  3157:     my ($r,$changed,$order,$canshow,$requestcourses,$usertools,$requestauthor,
                   3158:         $userenv,$changedhash,$namechangedhash,$oldsetting,$oldsettingtext,
1.334     raeburn  3159:         $newsetting,$newsettingtext) = @_;
                   3160:     return unless (ref($order) eq 'ARRAY' &&
                   3161:                    ref($canshow) eq 'HASH' && 
                   3162:                    ref($requestcourses) eq 'ARRAY' && 
1.362     raeburn  3163:                    ref($requestauthor) eq 'ARRAY' &&
1.334     raeburn  3164:                    ref($usertools) eq 'ARRAY' && 
                   3165:                    ref($userenv) eq 'HASH' &&
                   3166:                    ref($changedhash) eq 'HASH' &&
                   3167:                    ref($oldsetting) eq 'HASH' &&
                   3168:                    ref($oldsettingtext) eq 'HASH' &&
                   3169:                    ref($newsetting) eq 'HASH' &&
                   3170:                    ref($newsettingtext) eq 'HASH');
                   3171:     my %lt=&Apache::lonlocal::texthash(
1.372     raeburn  3172:          'ui'             => 'User Information',
1.334     raeburn  3173:          'uic'            => 'User Information Changed',
                   3174:          'firstname'      => 'First Name',
                   3175:          'middlename'     => 'Middle Name',
                   3176:          'lastname'       => 'Last Name',
                   3177:          'generation'     => 'Generation',
                   3178:          'id'             => 'Student/Employee ID',
                   3179:          'permanentemail' => 'Permanent e-mail address',
1.378     raeburn  3180:          'portfolioquota' => 'Disk space allocated to portfolio files',
1.385     bisitz   3181:          'authorquota'    => 'Disk space allocated to Authoring Space',
1.334     raeburn  3182:          'blog'           => 'Blog Availability',
1.361     raeburn  3183:          'webdav'         => 'WebDAV Availability',
1.334     raeburn  3184:          'aboutme'        => 'Personal Information Page Availability',
                   3185:          'portfolio'      => 'Portfolio Availability',
                   3186:          'official'       => 'Can Request Official Courses',
                   3187:          'unofficial'     => 'Can Request Unofficial Courses',
                   3188:          'community'      => 'Can Request Communities',
1.384     raeburn  3189:          'textbook'       => 'Can Request Textbook Courses',
1.362     raeburn  3190:          'requestauthor'  => 'Can Request Author Role',
1.334     raeburn  3191:          'inststatus'     => "Affiliation",
                   3192:          'prvs'           => 'Previous Value:',
                   3193:          'chto'           => 'Changed To:'
                   3194:     );
                   3195:     if ($changed) {
1.372     raeburn  3196:         $r->print('<h3>'.$lt{'uic'}.'</h3>'.
1.367     golterma 3197:                 &Apache::loncommon::start_data_table().
                   3198:                 &Apache::loncommon::start_data_table_header_row());
1.334     raeburn  3199:         $r->print("<th>&nbsp;</th>\n");
1.367     golterma 3200:         $r->print('<th><b>'.$lt{'prvs'}.'</b></th>');
                   3201:         $r->print('<th><span class="LC_nobreak"><b>'.$lt{'chto'}.'</b></span></th>');
                   3202:         $r->print(&Apache::loncommon::end_data_table_header_row());
                   3203:         my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
                   3204: 
1.334     raeburn  3205:         foreach my $item (@userinfo) {
                   3206:             my $value = $env{'form.c'.$item};
1.367     golterma 3207:             #show changes only:
1.383     raeburn  3208:             unless ($value eq $userenv->{$item}){
1.367     golterma 3209:                 $r->print(&Apache::loncommon::start_data_table_row());
                   3210:                 $r->print("<td>$lt{$item}</td>\n");
1.383     raeburn  3211:                 $r->print("<td>".$userenv->{$item}."</td>\n");
1.367     golterma 3212:                 $r->print("<td>$value </td>\n");
                   3213:                 $r->print(&Apache::loncommon::end_data_table_row());
1.334     raeburn  3214:             }
                   3215:         }
                   3216:         foreach my $entry (@{$order}) {
1.383     raeburn  3217:             if ($canshow->{$entry}) {
                   3218:                 if (($entry eq 'requestcourses') || ($entry eq 'reqcrsotherdom') || ($entry eq 'requestauthor')) {
                   3219:                     my @items;
                   3220:                     if ($entry eq 'requestauthor') {
                   3221:                         @items = ($entry);
                   3222:                     } else {
                   3223:                         @items = @{$requestcourses};
1.384     raeburn  3224:                     }
1.383     raeburn  3225:                     foreach my $item (@items) {
                   3226:                         if (($newsetting->{$item} ne $oldsetting->{$item}) || 
                   3227:                             ($newsettingtext->{$item} ne $oldsettingtext->{$item})) {
                   3228:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");  
                   3229:                             $r->print("<td>$lt{$item}</td>\n");
                   3230:                             $r->print("<td>".$oldsetting->{$item});
                   3231:                             if ($oldsettingtext->{$item}) {
                   3232:                                 if ($oldsetting->{$item}) {
                   3233:                                     $r->print(' -- ');
                   3234:                                 }
                   3235:                                 $r->print($oldsettingtext->{$item});
                   3236:                             }
                   3237:                             $r->print("</td>\n");
                   3238:                             $r->print("<td>".$newsetting->{$item});
                   3239:                             if ($newsettingtext->{$item}) {
                   3240:                                 if ($newsetting->{$item}) {
                   3241:                                     $r->print(' -- ');
                   3242:                                 }
                   3243:                                 $r->print($newsettingtext->{$item});
                   3244:                             }
                   3245:                             $r->print("</td>\n");
                   3246:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3247:                         }
                   3248:                     }
                   3249:                 } elsif ($entry eq 'tools') {
                   3250:                     foreach my $item (@{$usertools}) {
1.383     raeburn  3251:                         if ($newsetting->{$item} ne $oldsetting->{$item}) {
                   3252:                             $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3253:                             $r->print("<td>$lt{$item}</td>\n");
                   3254:                             $r->print("<td>".$oldsetting->{$item}.' '.$oldsettingtext->{$item}."</td>\n");
                   3255:                             $r->print("<td>".$newsetting->{$item}.' '.$newsettingtext->{$item}."</td>\n");
                   3256:                             $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3257:                         }
                   3258:                     }
1.378     raeburn  3259:                 } elsif ($entry eq 'quota') {
                   3260:                     if ((ref($oldsetting->{$entry}) eq 'HASH') && (ref($oldsettingtext->{$entry}) eq 'HASH') &&
                   3261:                         (ref($newsetting->{$entry}) eq 'HASH') && (ref($newsettingtext->{$entry}) eq 'HASH')) {
                   3262:                         foreach my $name ('portfolio','author') {
1.383     raeburn  3263:                             if ($newsetting->{$entry}->{$name} ne $oldsetting->{$entry}->{$name}) {
                   3264:                                 $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3265:                                 $r->print("<td>$lt{$name.$entry}</td>\n");
                   3266:                                 $r->print("<td>".$oldsettingtext->{$entry}->{$name}."</td>\n");
                   3267:                                 $r->print("<td>".$newsettingtext->{$entry}->{$name}."</td>\n");
                   3268:                                 $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.378     raeburn  3269:                             }
                   3270:                         }
                   3271:                     }
1.334     raeburn  3272:                 } else {
1.383     raeburn  3273:                     if ($newsetting->{$entry} ne $oldsetting->{$entry}) {
                   3274:                         $r->print(&Apache::loncommon::start_data_table_row()."\n");
                   3275:                         $r->print("<td>$lt{$entry}</td>\n");
                   3276:                         $r->print("<td>".$oldsetting->{$entry}.' '.$oldsettingtext->{$entry}."</td>\n");
                   3277:                         $r->print("<td>".$newsetting->{$entry}.' '.$newsettingtext->{$entry}."</td>\n");
                   3278:                         $r->print(&Apache::loncommon::end_data_table_row()."\n");
1.334     raeburn  3279:                     }
                   3280:                 }
                   3281:             }
                   3282:         }
1.367     golterma 3283:         $r->print(&Apache::loncommon::end_data_table().'<br />');
1.372     raeburn  3284:     } else {
                   3285:         $r->print('<h3>'.$lt{'ui'}.'</h3>'.
                   3286:                   '<p>'.&mt('No changes made to user information').'</p>');
1.334     raeburn  3287:     }
                   3288:     return;
                   3289: }
                   3290: 
1.275     raeburn  3291: sub tool_changes {
                   3292:     my ($context,$usertools,$oldaccess,$oldaccesstext,$userenv,$changeHash,
                   3293:         $changed,$newaccess,$newaccesstext) = @_;
                   3294:     if (!((ref($usertools) eq 'ARRAY') && (ref($oldaccess) eq 'HASH') &&
                   3295:           (ref($oldaccesstext) eq 'HASH') && (ref($userenv) eq 'HASH') &&
                   3296:           (ref($changeHash) eq 'HASH') && (ref($changed) eq 'HASH') &&
                   3297:           (ref($newaccess) eq 'HASH') && (ref($newaccesstext) eq 'HASH'))) {
                   3298:         return;
                   3299:     }
1.383     raeburn  3300:     my %reqdisplay = &requestchange_display();
1.300     raeburn  3301:     if ($context eq 'reqcrsotherdom') {
1.309     raeburn  3302:         my @options = ('approval','validate','autolimit');
1.306     raeburn  3303:         my $optregex = join('|',@options);
1.300     raeburn  3304:         my $cdom = $env{'request.role.domain'};
                   3305:         foreach my $tool (@{$usertools}) {
1.383     raeburn  3306:             $oldaccesstext->{$tool} = &mt("availability set to 'off'");
1.314     raeburn  3307:             $newaccesstext->{$tool} = $oldaccesstext->{$tool};
1.300     raeburn  3308:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
1.383     raeburn  3309:             my ($newop,$limit);
1.314     raeburn  3310:             if ($env{'form.'.$context.'_'.$tool}) {
                   3311:                 $newop = $env{'form.'.$context.'_'.$tool};
                   3312:                 if ($newop eq 'autolimit') {
1.383     raeburn  3313:                     $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
1.314     raeburn  3314:                     $limit =~ s/\D+//g;
                   3315:                     $newop .= '='.$limit;
                   3316:                 }
                   3317:             }
1.300     raeburn  3318:             if ($userenv->{$context.'.'.$tool} eq '') {
1.314     raeburn  3319:                 if ($newop) {
                   3320:                     $changed->{$tool}=&tool_admin($tool,$cdom.':'.$newop,
1.300     raeburn  3321:                                                   $changeHash,$context);
                   3322:                     if ($changed->{$tool}) {
1.383     raeburn  3323:                         if ($newop =~ /^autolimit/) {
                   3324:                             if ($limit) {
                   3325:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3326:                             } else {
                   3327:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3328:                             }
                   3329:                         } else {
                   3330:                             $newaccesstext->{$tool} = $reqdisplay{$newop};
                   3331:                         }
1.300     raeburn  3332:                     } else {
                   3333:                         $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3334:                     }
                   3335:                 }
                   3336:             } else {
                   3337:                 my @curr = split(',',$userenv->{$context.'.'.$tool});
                   3338:                 my @new;
                   3339:                 my $changedoms;
1.314     raeburn  3340:                 foreach my $req (@curr) {
                   3341:                     if ($req =~ /^\Q$cdom\E\:($optregex\=?\d*)$/) {
                   3342:                         my $oldop = $1;
1.383     raeburn  3343:                         if ($oldop =~ /^autolimit=(\d*)/) {
                   3344:                             my $limit = $1;
                   3345:                             if ($limit) {
                   3346:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3347:                             } else {
                   3348:                                 $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3349:                             }
                   3350:                         } else {
                   3351:                             $oldaccesstext->{$tool} = $reqdisplay{$oldop};
                   3352:                         }
1.314     raeburn  3353:                         if ($oldop ne $newop) {
                   3354:                             $changedoms = 1;
                   3355:                             foreach my $item (@curr) {
                   3356:                                 my ($reqdom,$option) = split(':',$item);
                   3357:                                 unless ($reqdom eq $cdom) {
                   3358:                                     push(@new,$item);
                   3359:                                 }
                   3360:                             }
                   3361:                             if ($newop) {
                   3362:                                 push(@new,$cdom.':'.$newop);
1.300     raeburn  3363:                             }
1.314     raeburn  3364:                             @new = sort(@new);
1.300     raeburn  3365:                         }
1.314     raeburn  3366:                         last;
1.300     raeburn  3367:                     }
1.314     raeburn  3368:                 }
                   3369:                 if ((!$changedoms) && ($newop)) {
1.300     raeburn  3370:                     $changedoms = 1;
1.306     raeburn  3371:                     @new = sort(@curr,$cdom.':'.$newop);
1.300     raeburn  3372:                 }
                   3373:                 if ($changedoms) {
1.314     raeburn  3374:                     my $newdomstr;
1.300     raeburn  3375:                     if (@new) {
                   3376:                         $newdomstr = join(',',@new);
                   3377:                     }
                   3378:                     $changed->{$tool}=&tool_admin($tool,$newdomstr,$changeHash,
                   3379:                                                   $context);
                   3380:                     if ($changed->{$tool}) {
                   3381:                         if ($env{'form.'.$context.'_'.$tool}) {
1.306     raeburn  3382:                             if ($env{'form.'.$context.'_'.$tool} eq 'autolimit') {
1.314     raeburn  3383:                                 my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3384:                                 $limit =~ s/\D+//g;
                   3385:                                 if ($limit) {
1.383     raeburn  3386:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
1.314     raeburn  3387:                                 } else {
1.383     raeburn  3388:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
1.306     raeburn  3389:                                 }
1.314     raeburn  3390:                             } else {
1.306     raeburn  3391:                                 $newaccesstext->{$tool} = $reqdisplay{$env{'form.'.$context.'_'.$tool}};
                   3392:                             }
1.300     raeburn  3393:                         } else {
1.383     raeburn  3394:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
1.300     raeburn  3395:                         }
                   3396:                     }
                   3397:                 }
                   3398:             }
                   3399:         }
                   3400:         return;
                   3401:     }
1.275     raeburn  3402:     foreach my $tool (@{$usertools}) {
1.383     raeburn  3403:         my ($newval,$limit,$envkey);
1.362     raeburn  3404:         $envkey = $context.'.'.$tool;
1.306     raeburn  3405:         if ($context eq 'requestcourses') {
                   3406:             $newval = $env{'form.crsreq_'.$tool};
                   3407:             if ($newval eq 'autolimit') {
1.383     raeburn  3408:                 $limit = $env{'form.crsreq_'.$tool.'_limit'};
                   3409:                 $limit =~ s/\D+//g;
                   3410:                 $newval .= '='.$limit;
1.306     raeburn  3411:             }
1.362     raeburn  3412:         } elsif ($context eq 'requestauthor') {
                   3413:             $newval = $env{'form.'.$context};
                   3414:             $envkey = $context;
1.314     raeburn  3415:         } else {
1.306     raeburn  3416:             $newval = $env{'form.'.$context.'_'.$tool};
                   3417:         }
1.362     raeburn  3418:         if ($userenv->{$envkey} ne '') {
1.275     raeburn  3419:             $oldaccess->{$tool} = &mt('custom');
1.383     raeburn  3420:             if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3421:                 if ($userenv->{$envkey} =~ /^autolimit=(\d*)$/) {
                   3422:                     my $currlimit = $1;
                   3423:                     if ($currlimit eq '') {
                   3424:                         $oldaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3425:                     } else {
                   3426:                         $oldaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$currlimit);
                   3427:                     }
                   3428:                 } elsif ($userenv->{$envkey}) {
                   3429:                     $oldaccesstext->{$tool} = $reqdisplay{$userenv->{$envkey}};
                   3430:                 } else {
                   3431:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3432:                 }
1.275     raeburn  3433:             } else {
1.383     raeburn  3434:                 if ($userenv->{$envkey}) {
                   3435:                     $oldaccesstext->{$tool} = &mt("availability set to 'on'");
                   3436:                 } else {
                   3437:                     $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3438:                 }
1.275     raeburn  3439:             }
1.362     raeburn  3440:             $changeHash->{$envkey} = $userenv->{$envkey};
1.275     raeburn  3441:             if ($env{'form.custom'.$tool} == 1) {
1.362     raeburn  3442:                 if ($newval ne $userenv->{$envkey}) {
1.306     raeburn  3443:                     $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3444:                                                     $context);
1.275     raeburn  3445:                     if ($changed->{$tool}) {
                   3446:                         $newaccess->{$tool} = &mt('custom');
1.383     raeburn  3447:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3448:                             if ($newval =~ /^autolimit/) {
                   3449:                                 if ($limit) {
                   3450:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3451:                                 } else {
                   3452:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3453:                                 }
                   3454:                             } elsif ($newval) {
                   3455:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3456:                             } else {
                   3457:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3458:                             }
1.275     raeburn  3459:                         } else {
1.383     raeburn  3460:                             if ($newval) {
                   3461:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3462:                             } else {
                   3463:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3464:                             }
1.275     raeburn  3465:                         }
                   3466:                     } else {
                   3467:                         $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  3468:                         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3469:                             if ($newval =~ /^autolimit/) {
                   3470:                                 if ($limit) {
                   3471:                                     $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3472:                                 } else {
                   3473:                                     $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3474:                                 }
                   3475:                             } elsif ($newval) {
                   3476:                                 $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3477:                             } else {
                   3478:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3479:                             }
1.275     raeburn  3480:                         } else {
1.383     raeburn  3481:                             if ($userenv->{$context.'.'.$tool}) {
                   3482:                                 $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3483:                             } else {
                   3484:                                 $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3485:                             }
1.275     raeburn  3486:                         }
                   3487:                     }
                   3488:                 } else {
                   3489:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3490:                     $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3491:                 }
                   3492:             } else {
                   3493:                 $changed->{$tool} = &tool_admin($tool,'',$changeHash,$context);
                   3494:                 if ($changed->{$tool}) {
                   3495:                     $newaccess->{$tool} = &mt('default');
                   3496:                 } else {
                   3497:                     $newaccess->{$tool} = $oldaccess->{$tool};
1.383     raeburn  3498:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3499:                         if ($newval =~ /^autolimit/) {
                   3500:                             if ($limit) {
                   3501:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3502:                             } else {
                   3503:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3504:                             }
                   3505:                         } elsif ($newval) {
                   3506:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3507:                         } else {
                   3508:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3509:                         }
1.275     raeburn  3510:                     } else {
1.383     raeburn  3511:                         if ($userenv->{$context.'.'.$tool}) {
                   3512:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3513:                         } else {
                   3514:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3515:                         }
1.275     raeburn  3516:                     }
                   3517:                 }
                   3518:             }
                   3519:         } else {
                   3520:             $oldaccess->{$tool} = &mt('default');
                   3521:             if ($env{'form.custom'.$tool} == 1) {
1.306     raeburn  3522:                 $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3523:                                                 $context);
1.275     raeburn  3524:                 if ($changed->{$tool}) {
                   3525:                     $newaccess->{$tool} = &mt('custom');
1.383     raeburn  3526:                     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
                   3527:                         if ($newval =~ /^autolimit/) {
                   3528:                             if ($limit) {
                   3529:                                 $newaccesstext->{$tool} = &mt('available with automatic approval, up to limit of [quant,_1,request] per user',$limit);
                   3530:                             } else {
                   3531:                                 $newaccesstext->{$tool} = &mt('available with automatic approval (unlimited)');
                   3532:                             }
                   3533:                         } elsif ($newval) {
                   3534:                             $newaccesstext->{$tool} = $reqdisplay{$newval};
                   3535:                         } else {
                   3536:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3537:                         }
1.275     raeburn  3538:                     } else {
1.383     raeburn  3539:                         if ($newval) {
                   3540:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3541:                         } else {
                   3542:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3543:                         }
1.275     raeburn  3544:                     }
                   3545:                 } else {
                   3546:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3547:                 }
                   3548:             } else {
                   3549:                 $newaccess->{$tool} = $oldaccess->{$tool};
                   3550:             }
                   3551:         }
                   3552:     }
                   3553:     return;
                   3554: }
                   3555: 
1.220     raeburn  3556: sub update_roles {
1.375     raeburn  3557:     my ($r,$context,$showcredits) = @_;
1.4       www      3558:     my $now=time;
1.225     raeburn  3559:     my @rolechanges;
1.220     raeburn  3560:     my %disallowed;
1.73      sakharuk 3561:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
1.135     raeburn  3562:     foreach my $key (keys (%env)) {
                   3563: 	next if (! $env{$key});
1.190     raeburn  3564:         next if ($key eq 'form.action');
1.27      matthew  3565: 	# Revoke roles
1.135     raeburn  3566: 	if ($key=~/^form\.rev/) {
                   3567: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
1.64      www      3568: # Revoke standard role
1.170     albertel 3569: 		my ($scope,$role) = ($1,$2);
                   3570: 		my $result =
                   3571: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
                   3572: 						$env{'form.ccuname'},
1.239     raeburn  3573: 						$scope,$role,'','',$context);
1.367     golterma 3574:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3575:                             &mt('Revoking [_1] in [_2]',
                   3576:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  3577:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   3578:                                 $result ne "ok").'<br />');
                   3579:                 if ($result ne "ok") {
                   3580:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3581:                 }
1.170     albertel 3582: 		if ($role eq 'st') {
1.202     raeburn  3583: 		    my $result = 
1.198     raeburn  3584:                         &Apache::lonuserutils::classlist_drop($scope,
                   3585:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3586: 			    $now);
1.367     golterma 3587:                     $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.53      www      3588: 		}
1.225     raeburn  3589:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3590:                     push(@rolechanges,$role);
                   3591:                 }
1.196     raeburn  3592: 	    }
1.195     raeburn  3593: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}s) {
1.64      www      3594: # Revoke custom role
1.369     bisitz   3595:                 my $result = &Apache::lonnet::revokecustomrole(
                   3596:                     $env{'form.ccdomain'},$env{'form.ccuname'},$1,$2,$3,$4,'','',$context);
1.367     golterma 3597:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3598:                             &mt('Revoking custom role [_1] by [_2] in [_3]',
1.372     raeburn  3599:                                 $4,$3.':'.$2,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3600:                             $result ne 'ok').'<br />');
                   3601:                 if ($result ne "ok") {
                   3602:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3603:                 }
1.225     raeburn  3604:                 if (!grep(/^cr$/,@rolechanges)) {
                   3605:                     push(@rolechanges,'cr');
                   3606:                 }
1.64      www      3607: 	    }
1.135     raeburn  3608: 	} elsif ($key=~/^form\.del/) {
                   3609: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
1.116     raeburn  3610: # Delete standard role
1.170     albertel 3611: 		my ($scope,$role) = ($1,$2);
                   3612: 		my $result =
                   3613: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
                   3614: 						$env{'form.ccuname'},
1.239     raeburn  3615: 						$scope,$role,$now,0,1,'',
                   3616:                                                 $context);
1.367     golterma 3617:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   3618:                             &mt('Deleting [_1] in [_2]',
1.369     bisitz   3619:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  3620:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   3621:                             $result ne 'ok').'<br />');
                   3622:                 if ($result ne "ok") {
                   3623:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3624:                 }
1.367     golterma 3625: 
1.170     albertel 3626: 		if ($role eq 'st') {
1.202     raeburn  3627: 		    my $result = 
1.198     raeburn  3628:                         &Apache::lonuserutils::classlist_drop($scope,
                   3629:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3630: 			    $now);
1.369     bisitz   3631: 		    $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.81      albertel 3632: 		}
1.225     raeburn  3633:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3634:                     push(@rolechanges,$role);
                   3635:                 }
1.116     raeburn  3636:             }
1.139     albertel 3637: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3638:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3639: # Delete custom role
1.369     bisitz   3640:                 my $result =
                   3641:                     &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
                   3642:                         $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
                   3643:                         0,1,$context);
                   3644:                 $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Deleting custom role [_1] by [_2] in [_3]',
1.372     raeburn  3645:                       $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3646:                       $result ne "ok").'<br />');
                   3647:                 if ($result ne "ok") {
                   3648:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3649:                 }
1.367     golterma 3650: 
1.225     raeburn  3651:                 if (!grep(/^cr$/,@rolechanges)) {
                   3652:                     push(@rolechanges,'cr');
                   3653:                 }
1.116     raeburn  3654:             }
1.135     raeburn  3655: 	} elsif ($key=~/^form\.ren/) {
1.101     albertel 3656:             my $udom = $env{'form.ccdomain'};
                   3657:             my $uname = $env{'form.ccuname'};
1.116     raeburn  3658: # Re-enable standard role
1.135     raeburn  3659: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
1.89      raeburn  3660:                 my $url = $1;
                   3661:                 my $role = $2;
                   3662:                 my $logmsg;
                   3663:                 my $output;
                   3664:                 if ($role eq 'st') {
1.141     albertel 3665:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
1.374     raeburn  3666:                         my ($cdom,$cnum,$csec) = ($1,$2,$3);
1.375     raeburn  3667:                         my $credits;
                   3668:                         if ($showcredits) {
                   3669:                             my $defaultcredits = 
                   3670:                                 &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   3671:                             $credits = &get_user_credits($defaultcredits,$cdom,$cnum);
                   3672:                         }
                   3673:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$cdom,$cnum,$csec,$context,$credits);
1.220     raeburn  3674:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course') || ($result eq 'refused')) {
1.223     raeburn  3675:                             if ($result eq 'refused' && $logmsg) {
                   3676:                                 $output = $logmsg;
                   3677:                             } else { 
1.369     bisitz   3678:                                 $output = &mt('Error: [_1]',$result)."\n";
1.223     raeburn  3679:                             }
1.89      raeburn  3680:                         } else {
1.372     raeburn  3681:                             $output = &Apache::lonhtmlcommon::confirm_success(&mt('Assigning [_1] in [_2] starting [_3]',
                   3682:                                         &Apache::lonnet::plaintext($role),
                   3683:                                         &Apache::loncommon::show_role_extent($url,$context,'st'),
                   3684:                                         &Apache::lonlocal::locallocaltime($now))).'<br />'.$logmsg.'<br />';
1.89      raeburn  3685:                         }
                   3686:                     }
                   3687:                 } else {
1.101     albertel 3688: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
1.239     raeburn  3689:                                $env{'form.ccuname'},$url,$role,0,$now,'','',
                   3690:                                $context);
1.367     golterma 3691:                         $output = &Apache::lonhtmlcommon::confirm_success(&mt('Re-enabling [_1] in [_2]',
1.372     raeburn  3692:                                         &Apache::lonnet::plaintext($role),
                   3693:                                         &Apache::loncommon::show_role_extent($url,$context,$role)),$result ne "ok").'<br />';
1.369     bisitz   3694:                     if ($result ne "ok") {
                   3695:                         $output .= &mt('Error: [_1]',$result).'<br />';
                   3696:                     }
                   3697:                 }
1.89      raeburn  3698:                 $r->print($output);
1.225     raeburn  3699:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3700:                     push(@rolechanges,$role);
                   3701:                 }
1.113     raeburn  3702: 	    }
1.116     raeburn  3703: # Re-enable custom role
1.139     albertel 3704: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3705:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3706:                 my $result = &Apache::lonnet::assigncustomrole(
                   3707:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
1.240     raeburn  3708:                                $url,$rdom,$rnam,$rolename,0,$now,undef,$context);
1.369     bisitz   3709:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   3710:                     &mt('Re-enabling custom role [_1] by [_2] in [_3]',
1.372     raeburn  3711:                         $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3712:                     $result ne "ok").'<br />');
                   3713:                 if ($result ne "ok") {
                   3714:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3715:                 }
1.225     raeburn  3716:                 if (!grep(/^cr$/,@rolechanges)) {
                   3717:                     push(@rolechanges,'cr');
                   3718:                 }
1.116     raeburn  3719:             }
1.135     raeburn  3720: 	} elsif ($key=~/^form\.act/) {
1.101     albertel 3721:             my $udom = $env{'form.ccdomain'};
                   3722:             my $uname = $env{'form.ccuname'};
1.141     albertel 3723: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
1.65      www      3724:                 # Activate a custom role
1.83      albertel 3725: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
                   3726: 		my $url='/'.$one.'/'.$two;
                   3727: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
1.65      www      3728: 
1.101     albertel 3729:                 my $start = ( $env{'form.start_'.$full} ?
                   3730:                               $env{'form.start_'.$full} :
1.88      raeburn  3731:                               $now );
1.101     albertel 3732:                 my $end   = ( $env{'form.end_'.$full} ?
                   3733:                               $env{'form.end_'.$full} :
1.88      raeburn  3734:                               0 );
                   3735:                                                                                      
                   3736:                 # split multiple sections
                   3737:                 my %sections = ();
1.101     albertel 3738:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
1.88      raeburn  3739:                 if ($num_sections == 0) {
1.240     raeburn  3740:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3741:                 } else {
1.114     albertel 3742: 		    my %curr_groups =
1.117     raeburn  3743: 			&Apache::longroup::coursegroups($one,$two);
1.113     raeburn  3744:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3745:                         if (($sec eq 'none') || ($sec eq 'all') || 
                   3746:                             exists($curr_groups{$sec})) {
                   3747:                             $disallowed{$sec} = $url;
                   3748:                             next;
                   3749:                         }
                   3750:                         my $securl = $url.'/'.$sec;
1.240     raeburn  3751: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3752:                     }
                   3753:                 }
1.225     raeburn  3754:                 if (!grep(/^cr$/,@rolechanges)) {
                   3755:                     push(@rolechanges,'cr');
                   3756:                 }
1.142     raeburn  3757: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
1.27      matthew  3758: 		# Activate roles for sections with 3 id numbers
                   3759: 		# set start, end times, and the url for the class
1.83      albertel 3760: 		my ($one,$two,$three)=($1,$2,$3);
1.101     albertel 3761: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
                   3762: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
1.27      matthew  3763: 			      $now );
1.101     albertel 3764: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
                   3765: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  3766: 			      0 );
1.83      albertel 3767: 		my $url='/'.$one.'/'.$two;
1.88      raeburn  3768:                 my $type = 'three';
                   3769:                 # split multiple sections
                   3770:                 my %sections = ();
1.101     albertel 3771:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
1.375     raeburn  3772:                 my $credits;
                   3773:                 if ($three eq 'st') {
                   3774:                     if ($showcredits) { 
                   3775:                         my $defaultcredits = 
                   3776:                             &Apache::lonuserutils::get_defaultcredits($one,$two);
                   3777:                         $credits = $env{'form.credits_'.$one.'_'.$two.'_'.$three};
                   3778:                         $credits =~ s/[^\d\.]//g;
                   3779:                         if ($credits eq $defaultcredits) {
                   3780:                             undef($credits);
                   3781:                         }
                   3782:                     }
                   3783:                 }
1.88      raeburn  3784:                 if ($num_sections == 0) {
1.375     raeburn  3785:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  3786:                 } else {
1.114     albertel 3787:                     my %curr_groups = 
1.117     raeburn  3788: 			&Apache::longroup::coursegroups($one,$two);
1.88      raeburn  3789:                     my $emptysec = 0;
                   3790:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3791:                         $sec =~ s/\W//g;
1.113     raeburn  3792:                         if ($sec ne '') {
                   3793:                             if (($sec eq 'none') || ($sec eq 'all') || 
                   3794:                                 exists($curr_groups{$sec})) {
                   3795:                                 $disallowed{$sec} = $url;
                   3796:                                 next;
                   3797:                             }
1.88      raeburn  3798:                             my $securl = $url.'/'.$sec;
1.375     raeburn  3799:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec,$context,$credits));
1.88      raeburn  3800:                         } else {
                   3801:                             $emptysec = 1;
                   3802:                         }
                   3803:                     }
                   3804:                     if ($emptysec) {
1.375     raeburn  3805:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  3806:                     }
1.225     raeburn  3807:                 }
                   3808:                 if (!grep(/^\Q$three\E$/,@rolechanges)) {
                   3809:                     push(@rolechanges,$three);
                   3810:                 }
1.135     raeburn  3811: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
1.27      matthew  3812: 		# Activate roles for sections with two id numbers
                   3813: 		# set start, end times, and the url for the class
1.101     albertel 3814: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
                   3815: 			      $env{'form.start_'.$1.'_'.$2} : 
1.27      matthew  3816: 			      $now );
1.101     albertel 3817: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
                   3818: 			      $env{'form.end_'.$1.'_'.$2} :
1.27      matthew  3819: 			      0 );
1.225     raeburn  3820:                 my $one = $1;
                   3821:                 my $two = $2;
                   3822: 		my $url='/'.$one.'/';
1.88      raeburn  3823:                 # split multiple sections
                   3824:                 my %sections = ();
1.225     raeburn  3825:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two},\%sections,$two);
1.88      raeburn  3826:                 if ($num_sections == 0) {
1.240     raeburn  3827:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  3828:                 } else {
                   3829:                     my $emptysec = 0;
                   3830:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3831:                         if ($sec ne '') {
                   3832:                             my $securl = $url.'/'.$sec;
1.240     raeburn  3833:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$two,$start,$end,$one,undef,$sec,$context));
1.88      raeburn  3834:                         } else {
                   3835:                             $emptysec = 1;
                   3836:                         }
                   3837:                     }
                   3838:                     if ($emptysec) {
1.240     raeburn  3839:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  3840:                     }
                   3841:                 }
1.225     raeburn  3842:                 if (!grep(/^\Q$two\E$/,@rolechanges)) {
                   3843:                     push(@rolechanges,$two);
                   3844:                 }
1.64      www      3845: 	    } else {
1.190     raeburn  3846: 		$r->print('<p><span class="LC_error">'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></span></p><br />');
1.64      www      3847:             }
1.113     raeburn  3848:             foreach my $key (sort(keys(%disallowed))) {
1.274     bisitz   3849:                 $r->print('<p class="LC_warning">');
1.113     raeburn  3850:                 if (($key eq 'none') || ($key eq 'all')) {  
1.274     bisitz   3851:                     $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  3852:                 } else {
1.274     bisitz   3853:                     $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  3854:                 }
1.274     bisitz   3855:                 $r->print('</p><p>'
                   3856:                          .&mt('Please [_1]go back[_2] and choose a different section name.'
                   3857:                              ,'<a href="javascript:history.go(-1)'
                   3858:                              ,'</a>')
                   3859:                          .'</p><br />'
                   3860:                 );
1.113     raeburn  3861:             }
                   3862: 	}
1.101     albertel 3863:     } # End of foreach (keys(%env))
1.75      www      3864: # Flush the course logs so reverse user roles immediately updated
1.349     raeburn  3865:     $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.225     raeburn  3866:     if (@rolechanges == 0) {
1.372     raeburn  3867:         $r->print('<p>'.&mt('No roles to modify').'</p>');
1.193     raeburn  3868:     }
1.225     raeburn  3869:     return @rolechanges;
1.220     raeburn  3870: }
                   3871: 
1.375     raeburn  3872: sub get_user_credits {
                   3873:     my ($uname,$udom,$defaultcredits,$cdom,$cnum) = @_;
                   3874:     if ($cdom eq '' || $cnum eq '') {
                   3875:         return unless ($env{'request.course.id'});
                   3876:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   3877:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   3878:     }
                   3879:     my $credits;
                   3880:     my %currhash =
                   3881:         &Apache::lonnet::get('classlist',[$uname.':'.$udom],$cdom,$cnum);
                   3882:     if (keys(%currhash) > 0) {
                   3883:         my @items = split(/:/,$currhash{$uname.':'.$udom});
                   3884:         my $crdidx = &Apache::loncoursedata::CL_CREDITS() - 3;
                   3885:         $credits = $items[$crdidx];
                   3886:         $credits =~ s/[^\d\.]//g;
                   3887:     }
                   3888:     if ($credits eq $defaultcredits) {
                   3889:         undef($credits);
                   3890:     }
                   3891:     return $credits;
                   3892: }
                   3893: 
1.220     raeburn  3894: sub enroll_single_student {
1.375     raeburn  3895:     my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype,
                   3896:         $showcredits,$defaultcredits) = @_;
1.318     raeburn  3897:     $r->print('<h3>');
                   3898:     if ($crstype eq 'Community') {
                   3899:         $r->print(&mt('Enrolling Member'));
                   3900:     } else {
                   3901:         $r->print(&mt('Enrolling Student'));
                   3902:     }
                   3903:     $r->print('</h3>');
1.220     raeburn  3904: 
                   3905:     # Remove non alphanumeric values from section
                   3906:     $env{'form.sections'}=~s/\W//g;
                   3907: 
1.375     raeburn  3908:     my $credits;
                   3909:     if (($showcredits) && ($env{'form.credits'} ne '')) {
                   3910:         $credits = $env{'form.credits'};
                   3911:         $credits =~ s/[^\d\.]//g;
                   3912:         if ($credits ne '') {
                   3913:             if ($credits eq $defaultcredits) {
                   3914:                 undef($credits);
                   3915:             }
                   3916:         }
                   3917:     }
                   3918: 
1.220     raeburn  3919:     # Clean out any old student roles the user has in this class.
                   3920:     &Apache::lonuserutils::modifystudent($env{'form.ccdomain'},
                   3921:          $env{'form.ccuname'},$env{'request.course.id'},undef,$uhome);
                   3922:     my ($startdate,$enddate) = &Apache::lonuserutils::get_dates_from_form();
                   3923:     my $enroll_result =
                   3924:         &Apache::lonnet::modify_student_enrollment($env{'form.ccdomain'},
                   3925:             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
                   3926:             $env{'form.cmiddlename'},$env{'form.clastname'},
                   3927:             $env{'form.generation'},$env{'form.sections'},$enddate,
1.375     raeburn  3928:             $startdate,'manual',undef,$env{'request.course.id'},'',$context,
                   3929:             $credits);
1.220     raeburn  3930:     if ($enroll_result =~ /^ok/) {
1.381     bisitz   3931:         $r->print(&mt('[_1] enrolled','<b>'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.'</b>'));
1.220     raeburn  3932:         if ($env{'form.sections'} ne '') {
                   3933:             $r->print(' '.&mt('in section [_1]',$env{'form.sections'}));
                   3934:         }
                   3935:         my ($showstart,$showend);
                   3936:         if ($startdate <= $now) {
                   3937:             $showstart = &mt('Access starts immediately');
                   3938:         } else {
                   3939:             $showstart = &mt('Access starts: ').&Apache::lonlocal::locallocaltime($startdate);
                   3940:         }
                   3941:         if ($enddate == 0) {
                   3942:             $showend = &mt('ends: no ending date');
                   3943:         } else {
                   3944:             $showend = &mt('ends: ').&Apache::lonlocal::locallocaltime($enddate);
                   3945:         }
                   3946:         $r->print('.<br />'.$showstart.'; '.$showend);
                   3947:         if ($startdate <= $now && !$newuser) {
1.386   ! bisitz   3948:             $r->print('<p class="LC_info">');
1.318     raeburn  3949:             if ($crstype eq 'Community') {
                   3950:                 $r->print(&mt('If the member is currently logged-in to LON-CAPA, the new role will be available when the member next logs in.'));
                   3951:             } else {
                   3952:                 $r->print(&mt('If the student is currently logged-in to LON-CAPA, the new role will be available when the student next logs in.'));
                   3953:            }
                   3954:            $r->print('</p>');
1.220     raeburn  3955:         }
                   3956:     } else {
                   3957:         $r->print(&mt('unable to enroll').": ".$enroll_result);
                   3958:     }
                   3959:     return;
1.188     raeburn  3960: }
                   3961: 
1.204     raeburn  3962: sub get_defaultquota_text {
                   3963:     my ($settingstatus) = @_;
                   3964:     my $defquotatext; 
                   3965:     if ($settingstatus eq '') {
1.383     raeburn  3966:         $defquotatext = &mt('default');
1.204     raeburn  3967:     } else {
                   3968:         my ($usertypes,$order) =
                   3969:             &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
                   3970:         if ($usertypes->{$settingstatus} eq '') {
1.383     raeburn  3971:             $defquotatext = &mt('default');
1.204     raeburn  3972:         } else {
1.383     raeburn  3973:             $defquotatext = &mt('default for [_1]',$usertypes->{$settingstatus});
1.204     raeburn  3974:         }
                   3975:     }
                   3976:     return $defquotatext;
                   3977: }
                   3978: 
1.188     raeburn  3979: sub update_result_form {
                   3980:     my ($uhome) = @_;
                   3981:     my $outcome = 
1.367     golterma 3982:     '<form name="userupdate" method="post" action="">'."\n";
1.160     raeburn  3983:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
1.188     raeburn  3984:         $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  3985:     }
1.207     raeburn  3986:     if ($env{'form.origname'} ne '') {
                   3987:         $outcome .= '<input type="hidden" name="origname" value="'.$env{'form.origname'}.'" />'."\n";
                   3988:     }
1.160     raeburn  3989:     foreach my $item ('sortby','seluname','seludom') {
                   3990:         if (exists($env{'form.'.$item})) {
1.188     raeburn  3991:             $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  3992:         }
                   3993:     }
1.188     raeburn  3994:     if ($uhome eq 'no_host') {
                   3995:         $outcome .= '<input type="hidden" name="forcenewuser" value="1" />'."\n";
                   3996:     }
                   3997:     $outcome .= '<input type="hidden" name="phase" value="" />'."\n".
1.383     raeburn  3998:                 '<input type="hidden" name="currstate" value="" />'."\n".
                   3999:                 '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1.188     raeburn  4000:                 '</form>';
                   4001:     return $outcome;
1.4       www      4002: }
                   4003: 
1.149     raeburn  4004: sub quota_admin {
1.378     raeburn  4005:     my ($setquota,$changeHash,$name) = @_;
1.149     raeburn  4006:     my $quotachanged;
                   4007:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   4008:         # Current user has quota modification privileges
1.267     raeburn  4009:         if (ref($changeHash) eq 'HASH') {
                   4010:             $quotachanged = 1;
1.378     raeburn  4011:             $changeHash->{$name.'quota'} = $setquota;
1.267     raeburn  4012:         }
1.149     raeburn  4013:     }
                   4014:     return $quotachanged;
                   4015: }
                   4016: 
1.267     raeburn  4017: sub tool_admin {
1.275     raeburn  4018:     my ($tool,$settool,$changeHash,$context) = @_;
                   4019:     my $canchange = 0; 
1.279     raeburn  4020:     if ($context eq 'requestcourses') {
1.275     raeburn  4021:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
                   4022:             $canchange = 1;
                   4023:         }
1.300     raeburn  4024:     } elsif ($context eq 'reqcrsotherdom') {
                   4025:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   4026:             $canchange = 1;
                   4027:         }
1.362     raeburn  4028:     } elsif ($context eq 'requestauthor') {
                   4029:         if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
                   4030:             $canchange = 1;
                   4031:         }
1.275     raeburn  4032:     } elsif (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   4033:         # Current user has quota modification privileges
                   4034:         $canchange = 1;
                   4035:     }
1.267     raeburn  4036:     my $toolchanged;
1.275     raeburn  4037:     if ($canchange) {
1.267     raeburn  4038:         if (ref($changeHash) eq 'HASH') {
                   4039:             $toolchanged = 1;
1.362     raeburn  4040:             if ($tool eq 'requestauthor') {
                   4041:                 $changeHash->{$context} = $settool;
                   4042:             } else {
                   4043:                 $changeHash->{$context.'.'.$tool} = $settool;
                   4044:             }
1.267     raeburn  4045:         }
                   4046:     }
                   4047:     return $toolchanged;
                   4048: }
                   4049: 
1.88      raeburn  4050: sub build_roles {
1.89      raeburn  4051:     my ($sectionstr,$sections,$role) = @_;
1.88      raeburn  4052:     my $num_sections = 0;
                   4053:     if ($sectionstr=~ /,/) {
                   4054:         my @secnums = split/,/,$sectionstr;
1.89      raeburn  4055:         if ($role eq 'st') {
                   4056:             $secnums[0] =~ s/\W//g;
                   4057:             $$sections{$secnums[0]} = 1;
                   4058:             $num_sections = 1;
                   4059:         } else {
                   4060:             foreach my $sec (@secnums) {
                   4061:                 $sec =~ ~s/\W//g;
1.150     banghart 4062:                 if (!($sec eq "")) {
1.89      raeburn  4063:                     if (exists($$sections{$sec})) {
                   4064:                         $$sections{$sec} ++;
                   4065:                     } else {
                   4066:                         $$sections{$sec} = 1;
                   4067:                         $num_sections ++;
                   4068:                     }
1.88      raeburn  4069:                 }
                   4070:             }
                   4071:         }
                   4072:     } else {
                   4073:         $sectionstr=~s/\W//g;
                   4074:         unless ($sectionstr eq '') {
                   4075:             $$sections{$sectionstr} = 1;
                   4076:             $num_sections ++;
                   4077:         }
                   4078:     }
1.129     albertel 4079: 
1.88      raeburn  4080:     return $num_sections;
                   4081: }
                   4082: 
1.58      www      4083: # ========================================================== Custom Role Editor
                   4084: 
                   4085: sub custom_role_editor {
1.351     raeburn  4086:     my ($r,$brcrum) = @_;
1.324     raeburn  4087:     my $action = $env{'form.customroleaction'};
                   4088:     my $rolename; 
                   4089:     if ($action eq 'new') {
                   4090:         $rolename=$env{'form.newrolename'};
                   4091:     } else {
                   4092:         $rolename=$env{'form.rolename'};
1.59      www      4093:     }
                   4094: 
1.324     raeburn  4095:     my ($crstype,$context);
                   4096:     if ($env{'request.course.id'}) {
                   4097:         $crstype = &Apache::loncommon::course_type();
                   4098:         $context = 'course';
                   4099:     } else {
                   4100:         $context = 'domain';
                   4101:         $crstype = $env{'form.templatecrstype'};
                   4102:     }
1.351     raeburn  4103: 
                   4104:     $rolename=~s/[^A-Za-z0-9]//gs;
                   4105:     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
                   4106: 	&print_username_entry_form($r,undef,undef,undef,undef,$crstype,$brcrum);
                   4107:         return;
                   4108:     }
                   4109: 
1.153     banghart 4110: # ------------------------------------------------------- What can be assigned?
                   4111:     my %full=();
                   4112:     my %courselevel=();
                   4113:     my %courselevelcurrent=();
1.61      www      4114:     my $syspriv='';
                   4115:     my $dompriv='';
                   4116:     my $coursepriv='';
1.153     banghart 4117:     my $body_top;
1.59      www      4118:     my ($rdummy,$roledef)=
                   4119: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
1.60      www      4120: # ------------------------------------------------------- Does this role exist?
1.153     banghart 4121:     $body_top .= '<h2>';
1.59      www      4122:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.153     banghart 4123: 	$body_top .= &mt('Existing Role').' "';
1.61      www      4124: # ------------------------------------------------- Get current role privileges
                   4125: 	($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
1.324     raeburn  4126:         if ($crstype eq 'Community') {
                   4127:             $syspriv =~ s/bre\&S//;   
                   4128:         }
1.59      www      4129:     } else {
1.153     banghart 4130: 	$body_top .= &mt('New Role').' "';
1.59      www      4131: 	$roledef='';
                   4132:     }
1.153     banghart 4133:     $body_top .= $rolename.'"</h2>';
1.135     raeburn  4134:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   4135: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4136:         if (!$restrict) { $restrict='F'; }
1.60      www      4137:         $courselevel{$priv}=$restrict;
1.61      www      4138:         if ($coursepriv=~/\:$priv/) {
                   4139: 	    $courselevelcurrent{$priv}=1;
                   4140: 	}
1.60      www      4141: 	$full{$priv}=1;
                   4142:     }
                   4143:     my %domainlevel=();
1.61      www      4144:     my %domainlevelcurrent=();
1.135     raeburn  4145:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   4146: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4147:         if (!$restrict) { $restrict='F'; }
1.60      www      4148:         $domainlevel{$priv}=$restrict;
1.61      www      4149:         if ($dompriv=~/\:$priv/) {
                   4150: 	    $domainlevelcurrent{$priv}=1;
                   4151: 	}
1.60      www      4152: 	$full{$priv}=1;
                   4153:     }
1.61      www      4154:     my %systemlevel=();
                   4155:     my %systemlevelcurrent=();
1.135     raeburn  4156:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   4157: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4158:         if (!$restrict) { $restrict='F'; }
1.61      www      4159:         $systemlevel{$priv}=$restrict;
                   4160:         if ($syspriv=~/\:$priv/) {
                   4161: 	    $systemlevelcurrent{$priv}=1;
                   4162: 	}
                   4163: 	$full{$priv}=1;
                   4164:     }
1.160     raeburn  4165:     my ($jsback,$elements) = &crumb_utilities();
1.154     banghart 4166:     my $button_code = "\n";
1.153     banghart 4167:     my $head_script = "\n";
1.301     bisitz   4168:     $head_script .= '<script type="text/javascript">'."\n"
                   4169:                    .'// <![CDATA['."\n";
1.324     raeburn  4170:     my @template_roles = ("in","ta","ep");
                   4171:     if ($context eq 'domain') {
                   4172:         push(@template_roles,"ad");
1.318     raeburn  4173:     }
1.324     raeburn  4174:     push(@template_roles,"st");
1.318     raeburn  4175:     if ($crstype eq 'Community') {
                   4176:         unshift(@template_roles,'co');
                   4177:     } else {
                   4178:         unshift(@template_roles,'cc');
                   4179:     }
1.154     banghart 4180:     foreach my $role (@template_roles) {
1.324     raeburn  4181:         $head_script .= &make_script_template($role,$crstype);
1.318     raeburn  4182:         $button_code .= &make_button_code($role,$crstype).' ';
1.154     banghart 4183:     }
1.324     raeburn  4184:     my $context_code;
                   4185:     if ($context eq 'domain') {
                   4186:         my $checkedCommunity = '';
                   4187:         my $checkedCourse = ' checked="checked"';
                   4188:         if ($env{'form.templatecrstype'} eq 'Community') {
                   4189:             $checkedCommunity = $checkedCourse;
                   4190:             $checkedCourse = '';
                   4191:         }
                   4192:         $context_code = '<label>'.
                   4193:                         '<input type="radio" name="templatecrstype" value="Course"'.$checkedCourse.' onclick="this.form.submit();">'.
                   4194:                         &mt('Course').
                   4195:                         '</label>'.('&nbsp;' x2).
                   4196:                         '<label>'.
                   4197:                         '<input type="radio" name="templatecrstype" value="Community"'.$checkedCommunity.' onclick="this.form.submit();">'.
                   4198:                         &mt('Community').
                   4199:                         '</label>'.
                   4200:                         '</fieldset>'.
                   4201:                         '<input type="hidden" name="customroleaction" value="'.
                   4202:                         $action.'" />';
                   4203:         if ($env{'form.customroleaction'} eq 'new') {
                   4204:             $context_code .= '<input type="hidden" name="newrolename" value="'.
                   4205:                              $rolename.'" />';
                   4206:         } else {
                   4207:             $context_code .= '<input type="hidden" name="rolename" value="'.
                   4208:                              $rolename.'" />';
                   4209:         }
                   4210:         $context_code .= '<input type="hidden" name="action" value="custom" />'.
                   4211:                          '<input type="hidden" name="phase" value="selected_custom_edit" />';
                   4212:     }
                   4213: 
1.301     bisitz   4214:     $head_script .= "\n".$jsback."\n"
                   4215:                    .'// ]]>'."\n"
                   4216:                    .'</script>'."\n";
1.351     raeburn  4217:     push (@{$brcrum},
                   4218:               {href => "javascript:backPage(document.form1,'pickrole','')",
                   4219:                text => "Pick custom role",
                   4220:                faq  => 282,bug=>'Instructor Interface',},
                   4221:               {href => "javascript:backPage(document.form1,'','')",
                   4222:                text => "Edit custom role",
                   4223:                faq  => 282,
                   4224:                bug  => 'Instructor Interface',
                   4225:                help => 'Course_Editing_Custom_Roles'}
                   4226:               );
                   4227:     my $args = { bread_crumbs          => $brcrum,
                   4228:                  bread_crumbs_component => 'User Management'};
                   4229:  
                   4230:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',
                   4231:                                              $head_script,$args).
                   4232:               $body_top);
1.73      sakharuk 4233:     my %lt=&Apache::lonlocal::texthash(
                   4234: 		    'prv'  => "Privilege",
1.131     raeburn  4235: 		    'crl'  => "Course Level",
1.73      sakharuk 4236:                     'dml'  => "Domain Level",
1.150     banghart 4237:                     'ssl'  => "System Level");
1.264     bisitz   4238: 
1.324     raeburn  4239:     $r->print('<div class="LC_left_float">'
1.264     bisitz   4240:              .'<form action=""><fieldset>'
                   4241:              .'<legend>'.&mt('Select a Template').'</legend>'
                   4242:              .$button_code
1.324     raeburn  4243:              .'</fieldset></form></div>');
                   4244:     if ($context_code) {
                   4245:         $r->print('<div class="LC_left_float">'
                   4246:                  .'<form action="/adm/createuser" method="post"><fieldset>'
                   4247:                  .'<legend>'.&mt('Context').'</legend>'
                   4248:                  .$context_code
                   4249:                  .'</form>'
                   4250:                  .'</div>'
                   4251:         );
                   4252:     }
                   4253:     $r->print('<br clear="all" />');
1.264     bisitz   4254: 
1.61      www      4255:     $r->print(<<ENDCCF);
1.380     bisitz   4256: <form name="form1" method="post" action="">
1.61      www      4257: <input type="hidden" name="phase" value="set_custom_roles" />
                   4258: <input type="hidden" name="rolename" value="$rolename" />
                   4259: ENDCCF
1.135     raeburn  4260:     $r->print(&Apache::loncommon::start_data_table().
                   4261:               &Apache::loncommon::start_data_table_header_row(). 
                   4262: '<th>'.$lt{'prv'}.'</th><th>'.$lt{'crl'}.'</th><th>'.$lt{'dml'}.
                   4263: '</th><th>'.$lt{'ssl'}.'</th>'.
                   4264:               &Apache::loncommon::end_data_table_header_row());
1.324     raeburn  4265:     foreach my $priv (sort(keys(%full))) {
1.318     raeburn  4266:         my $privtext = &Apache::lonnet::plaintext($priv,$crstype);
1.135     raeburn  4267:         $r->print(&Apache::loncommon::start_data_table_row().
                   4268: 	          '<td>'.$privtext.'</td><td>'.
1.288     bisitz   4269:     ($courselevel{$priv}?'<input type="checkbox" name="'.$priv.'_c"'.
                   4270:     ($courselevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;').
1.61      www      4271:     '</td><td>'.
1.288     bisitz   4272:     ($domainlevel{$priv}?'<input type="checkbox" name="'.$priv.'_d"'.
                   4273:     ($domainlevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;').
1.324     raeburn  4274:     '</td><td>');
                   4275:         if ($priv eq 'bre' && $crstype eq 'Community') {
                   4276:             $r->print('&nbsp;');  
                   4277:         } else {
                   4278:             $r->print($systemlevel{$priv}?'<input type="checkbox" name="'.$priv.'_s"'.
                   4279:                       ($systemlevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;');
                   4280:         }
                   4281:         $r->print('</td>'.
                   4282:                   &Apache::loncommon::end_data_table_row());
1.60      www      4283:     }
1.135     raeburn  4284:     $r->print(&Apache::loncommon::end_data_table().
1.190     raeburn  4285:    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
1.160     raeburn  4286:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
1.179     raeburn  4287:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".   
1.160     raeburn  4288:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
1.351     raeburn  4289:    '<input type="submit" value="'.&mt('Save').'" /></form>');
1.61      www      4290: }
1.153     banghart 4291: # --------------------------------------------------------
                   4292: sub make_script_template {
1.324     raeburn  4293:     my ($role,$crstype) = @_;
1.153     banghart 4294:     my %full_c=();
                   4295:     my %full_d=();
                   4296:     my %full_s=();
                   4297:     my $return_script;
                   4298:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   4299:         my ($priv,$restrict)=split(/\&/,$item);
                   4300:         $full_c{$priv}=1;
                   4301:     }
                   4302:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   4303:         my ($priv,$restrict)=split(/\&/,$item);
                   4304:         $full_d{$priv}=1;
                   4305:     }
1.154     banghart 4306:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
1.324     raeburn  4307:         next if (($crstype eq 'Community') && ($item eq 'bre&S'));
1.153     banghart 4308:         my ($priv,$restrict)=split(/\&/,$item);
                   4309:         $full_s{$priv}=1;
                   4310:     }
                   4311:     $return_script .= 'function set_'.$role.'() {'."\n";
                   4312:     my @temp = split(/:/,$Apache::lonnet::pr{$role.':c'});
                   4313:     my %role_c;
1.155     banghart 4314:     foreach my $priv (@temp) {
1.153     banghart 4315:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   4316:         $role_c{$priv_item} = 1;
                   4317:     }
1.269     raeburn  4318:     my %role_d;
                   4319:     @temp = split(/:/,$Apache::lonnet::pr{$role.':d'});
                   4320:     foreach my $priv(@temp) {
                   4321:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   4322:         $role_d{$priv_item} = 1;
                   4323:     }
                   4324:     my %role_s;
                   4325:     @temp = split(/:/,$Apache::lonnet::pr{$role.':s'});
                   4326:     foreach my $priv(@temp) {
                   4327:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   4328:         $role_s{$priv_item} = 1;
                   4329:     }
1.153     banghart 4330:     foreach my $priv_item (keys(%full_c)) {
                   4331:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.269     raeburn  4332:         if ((exists($role_c{$priv})) || (exists($role_d{$priv})) || 
                   4333:             (exists($role_s{$priv}))) {
1.153     banghart 4334:             $return_script .= "document.form1.$priv"."_c.checked = true;\n";
                   4335:         } else {
                   4336:             $return_script .= "document.form1.$priv"."_c.checked = false;\n";
                   4337:         }
                   4338:     }
1.154     banghart 4339:     foreach my $priv_item (keys(%full_d)) {
                   4340:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.269     raeburn  4341:         if ((exists($role_d{$priv})) || (exists($role_s{$priv}))) {
1.154     banghart 4342:             $return_script .= "document.form1.$priv"."_d.checked = true;\n";
                   4343:         } else {
                   4344:             $return_script .= "document.form1.$priv"."_d.checked = false;\n";
                   4345:         }
                   4346:     }
                   4347:     foreach my $priv_item (keys(%full_s)) {
1.153     banghart 4348:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.154     banghart 4349:         if (exists($role_s{$priv})) {
                   4350:             $return_script .= "document.form1.$priv"."_s.checked = true;\n";
                   4351:         } else {
                   4352:             $return_script .= "document.form1.$priv"."_s.checked = false;\n";
                   4353:         }
1.153     banghart 4354:     }
                   4355:     $return_script .= '}'."\n";
1.154     banghart 4356:     return ($return_script);
                   4357: }
                   4358: # ----------------------------------------------------------
                   4359: sub make_button_code {
1.318     raeburn  4360:     my ($role,$crstype) = @_;
                   4361:     my $label = &Apache::lonnet::plaintext($role,$crstype);
1.301     bisitz   4362:     my $button_code = '<input type="button" onclick="set_'.$role.'()" value="'.$label.'" />';
1.154     banghart 4363:     return ($button_code);
1.153     banghart 4364: }
1.61      www      4365: # ---------------------------------------------------------- Call to definerole
                   4366: sub set_custom_role {
1.351     raeburn  4367:     my ($r,$context,$brcrum) = @_;
1.101     albertel 4368:     my $rolename=$env{'form.rolename'};
1.63      www      4369:     $rolename=~s/[^A-Za-z0-9]//gs;
1.150     banghart 4370:     if (!$rolename) {
1.351     raeburn  4371: 	&custom_role_editor($r,$brcrum);
1.61      www      4372:         return;
                   4373:     }
1.160     raeburn  4374:     my ($jsback,$elements) = &crumb_utilities();
1.301     bisitz   4375:     my $jscript = '<script type="text/javascript">'
                   4376:                  .'// <![CDATA['."\n"
                   4377:                  .$jsback."\n"
                   4378:                  .'// ]]>'."\n"
                   4379:                  .'</script>'."\n";
1.352     raeburn  4380:     push(@{$brcrum},
                   4381:         {href => "javascript:backPage(document.customresult,'pickrole','')",
                   4382:          text => "Pick custom role",
                   4383:          faq  => 282,
                   4384:          bug  => 'Instructor Interface',},
                   4385:         {href => "javascript:backPage(document.customresult,'selected_custom_edit','')",
                   4386:          text => "Edit custom role",
                   4387:          faq  => 282,
                   4388:          bug  => 'Instructor Interface',},
                   4389:         {href => "javascript:backPage(document.customresult,'set_custom_roles','')",
                   4390:          text => "Result",
                   4391:          faq  => 282,
                   4392:          bug  => 'Instructor Interface',
                   4393:          help => 'Course_Editing_Custom_Roles'},
                   4394:         );
                   4395:     my $args = { bread_crumbs           => $brcrum,
1.351     raeburn  4396:                  bread_crumbs_component => 'User Management'}; 
                   4397:     $r->print(&Apache::loncommon::start_page('Save Custom Role',$jscript,$args));
1.160     raeburn  4398: 
1.61      www      4399:     my ($rdummy,$roledef)=
1.110     albertel 4400: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   4401: 
1.61      www      4402: # ------------------------------------------------------- Does this role exist?
1.188     raeburn  4403:     $r->print('<h3>');
1.61      www      4404:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 4405: 	$r->print(&mt('Existing Role').' "');
1.61      www      4406:     } else {
1.73      sakharuk 4407: 	$r->print(&mt('New Role').' "');
1.61      www      4408: 	$roledef='';
                   4409:     }
1.188     raeburn  4410:     $r->print($rolename.'"</h3>');
1.61      www      4411: # ------------------------------------------------------- What can be assigned?
                   4412:     my $sysrole='';
                   4413:     my $domrole='';
                   4414:     my $courole='';
                   4415: 
1.135     raeburn  4416:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   4417: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4418:         if (!$restrict) { $restrict=''; }
                   4419:         if ($env{'form.'.$priv.'_c'}) {
1.135     raeburn  4420: 	    $courole.=':'.$item;
1.61      www      4421: 	}
                   4422:     }
                   4423: 
1.135     raeburn  4424:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   4425: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4426:         if (!$restrict) { $restrict=''; }
                   4427:         if ($env{'form.'.$priv.'_d'}) {
1.135     raeburn  4428: 	    $domrole.=':'.$item;
1.61      www      4429: 	}
                   4430:     }
                   4431: 
1.135     raeburn  4432:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   4433: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4434:         if (!$restrict) { $restrict=''; }
                   4435:         if ($env{'form.'.$priv.'_s'}) {
1.135     raeburn  4436: 	    $sysrole.=':'.$item;
1.61      www      4437: 	}
                   4438:     }
1.63      www      4439:     $r->print('<br />Defining Role: '.
1.61      www      4440: 	   &Apache::lonnet::definerole($rolename,$sysrole,$domrole,$courole));
1.101     albertel 4441:     if ($env{'request.course.id'}) {
                   4442:         my $url='/'.$env{'request.course.id'};
1.63      www      4443:         $url=~s/\_/\//g;
1.73      sakharuk 4444: 	$r->print('<br />'.&mt('Assigning Role to Self').': '.
1.101     albertel 4445: 	      &Apache::lonnet::assigncustomrole($env{'user.domain'},
                   4446: 						$env{'user.name'},
1.63      www      4447: 						$url,
1.101     albertel 4448: 						$env{'user.domain'},
                   4449: 						$env{'user.name'},
1.240     raeburn  4450: 						$rolename,undef,undef,undef,$context));
1.63      www      4451:     }
1.380     bisitz   4452:     $r->print(
                   4453:         '<p><a href="javascript:backPage(document.customresult,'."'pickrole'".')">'
                   4454:        .&mt('Create or edit another custom role')
                   4455:        .'</a></p>'
                   4456:        .'<form name="customresult" method="post" action="">'
                   4457:        .&Apache::lonhtmlcommon::echo_form_input([]).'</form>'
                   4458:     );
1.58      www      4459: }
                   4460: 
1.2       www      4461: # ================================================================ Main Handler
                   4462: sub handler {
                   4463:     my $r = shift;
                   4464:     if ($r->header_only) {
1.68      www      4465:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      4466:        $r->send_http_header;
                   4467:        return OK;
                   4468:     }
1.318     raeburn  4469:     my ($context,$crstype);
1.190     raeburn  4470:     if ($env{'request.course.id'}) {
                   4471:         $context = 'course';
1.318     raeburn  4472:         $crstype = &Apache::loncommon::course_type();
1.190     raeburn  4473:     } elsif ($env{'request.role'} =~ /^au\./) {
1.206     raeburn  4474:         $context = 'author';
1.190     raeburn  4475:     } else {
                   4476:         $context = 'domain';
                   4477:     }
1.375     raeburn  4478: 
1.190     raeburn  4479:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.233     raeburn  4480:         ['action','state','callingform','roletype','showrole','bulkaction','popup','phase',
                   4481:          'username','domain','srchterm','srchdomain','srchin','srchby','srchtype']);
1.190     raeburn  4482:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.351     raeburn  4483:     my $args;
                   4484:     my $brcrum = [];
                   4485:     my $bread_crumbs_component = 'User Management';
1.202     raeburn  4486:     if ($env{'form.action'} ne 'dateselect') {
1.351     raeburn  4487:         $brcrum = [{href=>"/adm/createuser",
                   4488:                     text=>"User Management",
                   4489:                     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'}
                   4490:                   ];
1.202     raeburn  4491:     }
1.289     droeschl 4492:     #SD Following files not added to help, because the corresponding .tex-files seem to
                   4493:     #be missing: Course_Approve_Selfenroll,Course_User_Logs,
1.209     raeburn  4494:     my ($permission,$allowed) = 
1.318     raeburn  4495:         &Apache::lonuserutils::get_permission($context,$crstype);
1.190     raeburn  4496:     if (!$allowed) {
1.358     raeburn  4497:         if ($context eq 'course') {
                   4498:             $r->internal_redirect('/adm/viewclasslist');
                   4499:             return OK;
                   4500:         }
1.190     raeburn  4501:         $env{'user.error.msg'}=
                   4502:             "/adm/createuser:cst:0:0:Cannot create/modify user data ".
                   4503:                                  "or view user status.";
                   4504:         return HTTP_NOT_ACCEPTABLE;
                   4505:     }
                   4506: 
                   4507:     &Apache::loncommon::content_type($r,'text/html');
                   4508:     $r->send_http_header;
                   4509: 
1.375     raeburn  4510:     my $showcredits;
                   4511:     if ((($context eq 'course') && ($crstype eq 'Course')) || 
                   4512:          ($context eq 'domain')) {
                   4513:         my %domdefaults = 
                   4514:             &Apache::lonnet::get_domain_defaults($env{'request.role.domain'});
                   4515:         if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'}) {
                   4516:             $showcredits = 1;
                   4517:         }
                   4518:     }
                   4519: 
1.190     raeburn  4520:     # Main switch on form.action and form.state, as appropriate
                   4521:     if (! exists($env{'form.action'})) {
1.351     raeburn  4522:         $args = {bread_crumbs => $brcrum,
                   4523:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4524:         $r->print(&header(undef,$args));
1.318     raeburn  4525:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4526:     } elsif ($env{'form.action'} eq 'upload' && $permission->{'cusr'}) {
1.351     raeburn  4527:         push(@{$brcrum},
                   4528:               { href => '/adm/createuser?action=upload&state=',
                   4529:                 text => 'Upload Users List',
                   4530:                 help => 'Course_Create_Class_List',
                   4531:               });
                   4532:         $bread_crumbs_component = 'Upload Users List';
                   4533:         $args = {bread_crumbs           => $brcrum,
                   4534:                  bread_crumbs_component => $bread_crumbs_component};
                   4535:         $r->print(&header(undef,$args));
1.190     raeburn  4536:         $r->print('<form name="studentform" method="post" '.
                   4537:                   'enctype="multipart/form-data" '.
                   4538:                   ' action="/adm/createuser">'."\n");
                   4539:         if (! exists($env{'form.state'})) {
                   4540:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4541:         } elsif ($env{'form.state'} eq 'got_file') {
1.375     raeburn  4542:             &Apache::lonuserutils::print_upload_manager_form($r,$context,$permission,
                   4543:                                                              $crstype,$showcredits);
1.190     raeburn  4544:         } elsif ($env{'form.state'} eq 'enrolling') {
                   4545:             if ($env{'form.datatoken'}) {
1.375     raeburn  4546:                 &Apache::lonuserutils::upfile_drop_add($r,$context,$permission,
                   4547:                                                        $showcredits);
1.190     raeburn  4548:             }
                   4549:         } else {
                   4550:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4551:         }
1.213     raeburn  4552:     } elsif ((($env{'form.action'} eq 'singleuser') || ($env{'form.action'}
                   4553:              eq 'singlestudent')) && ($permission->{'cusr'})) {
1.190     raeburn  4554:         my $phase = $env{'form.phase'};
                   4555:         my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
1.192     albertel 4556: 	&Apache::loncreateuser::restore_prev_selections();
                   4557: 	my $srch;
                   4558: 	foreach my $item (@search) {
                   4559: 	    $srch->{$item} = $env{'form.'.$item};
                   4560: 	}
1.207     raeburn  4561:         if (($phase eq 'get_user_info') || ($phase eq 'userpicked') ||
                   4562:             ($phase eq 'createnewuser')) {
                   4563:             if ($env{'form.phase'} eq 'createnewuser') {
                   4564:                 my $response;
                   4565:                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
1.366     bisitz   4566:                     my $response =
                   4567:                         '<span class="LC_warning">'
                   4568:                        .&mt('You must specify a valid username. Only the following are allowed:'
                   4569:                            .' letters numbers - . @')
                   4570:                        .'</span>';
1.221     raeburn  4571:                     $env{'form.phase'} = '';
1.375     raeburn  4572:                     &print_username_entry_form($r,$context,$response,$srch,undef,
                   4573:                                                $crstype,$brcrum,$showcredits);
1.207     raeburn  4574:                 } else {
                   4575:                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
                   4576:                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
                   4577:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4578:                                                   $srch,$response,$context,
1.375     raeburn  4579:                                                   $permission,$crstype,$brcrum,
                   4580:                                                   $showcredits);
1.207     raeburn  4581:                 }
                   4582:             } elsif ($env{'form.phase'} eq 'get_user_info') {
1.190     raeburn  4583:                 my ($currstate,$response,$forcenewuser,$results) = 
1.221     raeburn  4584:                     &user_search_result($context,$srch);
1.190     raeburn  4585:                 if ($env{'form.currstate'} eq 'modify') {
                   4586:                     $currstate = $env{'form.currstate'};
                   4587:                 }
                   4588:                 if ($currstate eq 'select') {
                   4589:                     &print_user_selection_page($r,$response,$srch,$results,
1.351     raeburn  4590:                                                \@search,$context,undef,$crstype,
                   4591:                                                $brcrum);
1.190     raeburn  4592:                 } elsif ($currstate eq 'modify') {
                   4593:                     my ($ccuname,$ccdomain);
                   4594:                     if (($srch->{'srchby'} eq 'uname') && 
                   4595:                         ($srch->{'srchtype'} eq 'exact')) {
                   4596:                         $ccuname = $srch->{'srchterm'};
                   4597:                         $ccdomain= $srch->{'srchdomain'};
                   4598:                     } else {
                   4599:                         my @matchedunames = keys(%{$results});
                   4600:                         ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
                   4601:                     }
                   4602:                     $ccuname =&LONCAPA::clean_username($ccuname);
                   4603:                     $ccdomain=&LONCAPA::clean_domain($ccdomain);
                   4604:                     if ($env{'form.forcenewuser'}) {
                   4605:                         $response = '';
                   4606:                     }
                   4607:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4608:                                                   $srch,$response,$context,
1.351     raeburn  4609:                                                   $permission,$crstype,$brcrum);
1.190     raeburn  4610:                 } elsif ($currstate eq 'query') {
1.351     raeburn  4611:                     &print_user_query_page($r,'createuser',$brcrum);
1.190     raeburn  4612:                 } else {
1.229     raeburn  4613:                     $env{'form.phase'} = '';
1.207     raeburn  4614:                     &print_username_entry_form($r,$context,$response,$srch,
1.351     raeburn  4615:                                                $forcenewuser,$crstype,$brcrum);
1.190     raeburn  4616:                 }
                   4617:             } elsif ($env{'form.phase'} eq 'userpicked') {
                   4618:                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
                   4619:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
1.196     raeburn  4620:                 &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
1.351     raeburn  4621:                                               $context,$permission,$crstype,
                   4622:                                               $brcrum);
1.190     raeburn  4623:             }
                   4624:         } elsif ($env{'form.phase'} eq 'update_user_data') {
1.375     raeburn  4625:             &update_user_data($r,$context,$crstype,$brcrum,$showcredits);
1.190     raeburn  4626:         } else {
1.351     raeburn  4627:             &print_username_entry_form($r,$context,undef,$srch,undef,$crstype,
                   4628:                                        $brcrum);
1.190     raeburn  4629:         }
                   4630:     } elsif ($env{'form.action'} eq 'custom' && $permission->{'custom'}) {
                   4631:         if ($env{'form.phase'} eq 'set_custom_roles') {
1.351     raeburn  4632:             &set_custom_role($r,$context,$brcrum);
1.190     raeburn  4633:         } else {
1.351     raeburn  4634:             &custom_role_editor($r,$brcrum);
1.190     raeburn  4635:         }
1.362     raeburn  4636:     } elsif (($env{'form.action'} eq 'processauthorreq') &&
                   4637:              ($permission->{'cusr'}) && 
                   4638:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   4639:         push(@{$brcrum},
                   4640:                  {href => '/adm/createuser?action=processauthorreq',
1.385     bisitz   4641:                   text => 'Authoring Space requests',
1.362     raeburn  4642:                   help => 'Domain_Role_Approvals'});
                   4643:         $bread_crumbs_component = 'Authoring requests';
                   4644:         if ($env{'form.state'} eq 'done') {
                   4645:             push(@{$brcrum},
                   4646:                      {href => '/adm/createuser?action=authorreqqueue',
                   4647:                       text => 'Result',
                   4648:                       help => 'Domain_Role_Approvals'});
                   4649:             $bread_crumbs_component = 'Authoring request result';
                   4650:         }
                   4651:         $args = { bread_crumbs           => $brcrum,
                   4652:                   bread_crumbs_component => $bread_crumbs_component};
                   4653:         $r->print(&header(undef,$args));
                   4654:         if (!exists($env{'form.state'})) {
                   4655:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestauthor',
                   4656:                                                                             $env{'request.role.domain'}));
                   4657:         } elsif ($env{'form.state'} eq 'done') {
                   4658:             $r->print('<h3>'.&mt('Authoring request processing').'</h3>'."\n");
                   4659:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestauthor',
                   4660:                                                                          $env{'request.role.domain'}));
                   4661:         }
1.207     raeburn  4662:     } elsif (($env{'form.action'} eq 'listusers') && 
                   4663:              ($permission->{'view'} || $permission->{'cusr'})) {
1.202     raeburn  4664:         if ($env{'form.phase'} eq 'bulkchange') {
1.351     raeburn  4665:             push(@{$brcrum},
                   4666:                     {href => '/adm/createuser?action=listusers',
                   4667:                      text => "List Users"},
                   4668:                     {href => "/adm/createuser",
                   4669:                      text => "Result",
                   4670:                      help => 'Course_View_Class_List'});
                   4671:             $bread_crumbs_component = 'Update Users';
                   4672:             $args = {bread_crumbs           => $brcrum,
                   4673:                      bread_crumbs_component => $bread_crumbs_component};
                   4674:             $r->print(&header(undef,$args));
1.202     raeburn  4675:             my $setting = $env{'form.roletype'};
                   4676:             my $choice = $env{'form.bulkaction'};
                   4677:             if ($permission->{'cusr'}) {
1.336     raeburn  4678:                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice,$crstype);
1.221     raeburn  4679:             } else {
                   4680:                 $r->print(&mt('You are not authorized to make bulk changes to user roles'));
1.223     raeburn  4681:                 $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
1.202     raeburn  4682:             }
                   4683:         } else {
1.351     raeburn  4684:             push(@{$brcrum},
                   4685:                     {href => '/adm/createuser?action=listusers',
                   4686:                      text => "List Users",
                   4687:                      help => 'Course_View_Class_List'});
                   4688:             $bread_crumbs_component = 'List Users';
                   4689:             $args = {bread_crumbs           => $brcrum,
                   4690:                      bread_crumbs_component => $bread_crumbs_component};
1.202     raeburn  4691:             my ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles);
                   4692:             my $formname = 'studentform';
1.364     raeburn  4693:             my $hidecall = "hide_searching();";
1.321     raeburn  4694:             if (($context eq 'domain') && (($env{'form.roletype'} eq 'course') ||
                   4695:                 ($env{'form.roletype'} eq 'community'))) {
                   4696:                 if ($env{'form.roletype'} eq 'course') {
                   4697:                     ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles) = 
                   4698:                         &Apache::lonuserutils::courses_selector($env{'request.role.domain'},
                   4699:                                                                 $formname);
                   4700:                 } elsif ($env{'form.roletype'} eq 'community') {
                   4701:                     $cb_jscript = 
                   4702:                         &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
                   4703:                     my %elements = (
                   4704:                                       coursepick => 'radio',
                   4705:                                       coursetotal => 'text',
                   4706:                                       courselist => 'text',
                   4707:                                    );
                   4708:                     $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   4709:                 }
1.364     raeburn  4710:                 $jscript .= &verify_user_display($context)."\n".
                   4711:                             &Apache::loncommon::check_uncheck_jscript();
1.202     raeburn  4712:                 my $js = &add_script($jscript).$cb_jscript;
                   4713:                 my $loadcode = 
                   4714:                     &Apache::lonuserutils::course_selector_loadcode($formname);
                   4715:                 if ($loadcode ne '') {
1.364     raeburn  4716:                     $args->{add_entries} = {onload => "$loadcode;$hidecall"};
                   4717:                 } else {
                   4718:                     $args->{add_entries} = {onload => $hidecall};
1.202     raeburn  4719:                 }
1.351     raeburn  4720:                 $r->print(&header($js,$args));
1.191     raeburn  4721:             } else {
1.364     raeburn  4722:                 $args->{add_entries} = {onload => $hidecall};
                   4723:                 $jscript = &verify_user_display($context).
                   4724:                            &Apache::loncommon::check_uncheck_jscript(); 
                   4725:                 $r->print(&header(&add_script($jscript),$args));
1.191     raeburn  4726:             }
1.202     raeburn  4727:             &Apache::lonuserutils::print_userlist($r,undef,$permission,$context,
1.375     raeburn  4728:                          $formname,$totcodes,$codetitles,$idlist,$idlist_titles,
                   4729:                          $showcredits);
1.191     raeburn  4730:         }
1.213     raeburn  4731:     } elsif ($env{'form.action'} eq 'drop' && $permission->{'cusr'}) {
1.318     raeburn  4732:         my $brtext;
                   4733:         if ($crstype eq 'Community') {
                   4734:             $brtext = 'Drop Members';
                   4735:         } else {
                   4736:             $brtext = 'Drop Students';
                   4737:         }
1.351     raeburn  4738:         push(@{$brcrum},
                   4739:                 {href => '/adm/createuser?action=drop',
                   4740:                  text => $brtext,
                   4741:                  help => 'Course_Drop_Student'});
                   4742:         if ($env{'form.state'} eq 'done') {
                   4743:             push(@{$brcrum},
                   4744:                      {href=>'/adm/createuser?action=drop',
                   4745:                       text=>"Result"});
                   4746:         }
                   4747:         $bread_crumbs_component = $brtext;
                   4748:         $args = {bread_crumbs           => $brcrum,
                   4749:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4750:         $r->print(&header(undef,$args));
1.213     raeburn  4751:         if (!exists($env{'form.state'})) {
1.318     raeburn  4752:             &Apache::lonuserutils::print_drop_menu($r,$context,$permission,$crstype);
1.213     raeburn  4753:         } elsif ($env{'form.state'} eq 'done') {
                   4754:             &Apache::lonuserutils::update_user_list($r,$context,undef,
                   4755:                                                     $env{'form.action'});
                   4756:         }
1.202     raeburn  4757:     } elsif ($env{'form.action'} eq 'dateselect') {
                   4758:         if ($permission->{'cusr'}) {
1.351     raeburn  4759:             $r->print(&header(undef,{'no_nav_bar' => 1}).
1.375     raeburn  4760:                       &Apache::lonuserutils::date_section_selector($context,$permission,
                   4761:                                                                    $crstype,$showcredits));
1.202     raeburn  4762:         } else {
1.351     raeburn  4763:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   4764:                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>'); 
1.202     raeburn  4765:         }
1.237     raeburn  4766:     } elsif ($env{'form.action'} eq 'selfenroll') {
1.351     raeburn  4767:         push(@{$brcrum},
                   4768:                 {href => '/adm/createuser?action=selfenroll',
                   4769:                  text => "Configure Self-enrollment",
                   4770:                  help => 'Course_Self_Enrollment'});
1.237     raeburn  4771:         if (!exists($env{'form.state'})) {
1.351     raeburn  4772:             $args = { bread_crumbs           => $brcrum,
                   4773:                       bread_crumbs_component => 'Configure Self-enrollment'};
                   4774:             $r->print(&header(undef,$args));
1.241     raeburn  4775:             $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
1.237     raeburn  4776:             &print_selfenroll_menu($r,$context,$permission);
                   4777:         } elsif ($env{'form.state'} eq 'done') {
1.351     raeburn  4778:             push (@{$brcrum},
                   4779:                       {href=>'/adm/createuser?action=selfenroll',
                   4780:                        text=>"Result"});
                   4781:             $args = { bread_crumbs           => $brcrum,
                   4782:                       bread_crumbs_component => 'Self-enrollment result'};
                   4783:             $r->print(&header(undef,$args));
1.241     raeburn  4784:             $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
                   4785:             &update_selfenroll_config($r,$context,$permission);
1.237     raeburn  4786:         }
1.277     raeburn  4787:     } elsif ($env{'form.action'} eq 'selfenrollqueue') {
1.351     raeburn  4788:         push(@{$brcrum},
                   4789:                  {href => '/adm/createuser?action=selfenrollqueue',
                   4790:                   text => 'Enrollment requests',
                   4791:                   help => 'Course_Self_Enrollment'});
                   4792:         $bread_crumbs_component = 'Enrollment requests';
                   4793:         if ($env{'form.state'} eq 'done') {
                   4794:             push(@{$brcrum},
                   4795:                      {href => '/adm/createuser?action=selfenrollqueue',
                   4796:                       text => 'Result',
                   4797:                       help => 'Course_Self_Enrollment'});
                   4798:             $bread_crumbs_component = 'Enrollment result';
                   4799:         }
                   4800:         $args = { bread_crumbs           => $brcrum,
                   4801:                   bread_crumbs_component => $bread_crumbs_component};
                   4802:         $r->print(&header(undef,$args));
1.277     raeburn  4803:         my $cid = $env{'request.course.id'};
                   4804:         my $cdom = $env{'course.'.$cid.'.domain'};
                   4805:         my $cnum = $env{'course.'.$cid.'.num'};
1.307     raeburn  4806:         my $coursedesc = $env{'course.'.$cid.'.description'};
1.277     raeburn  4807:         if (!exists($env{'form.state'})) {
                   4808:             $r->print('<h3>'.&mt('Pending enrollment requests').'</h3>'."\n");
1.307     raeburn  4809:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests($context,
                   4810:                                                                        $cdom,$cnum));
1.277     raeburn  4811:         } elsif ($env{'form.state'} eq 'done') {
                   4812:             $r->print('<h3>'.&mt('Enrollment request processing').'</h3>'."\n");
1.307     raeburn  4813:             $r->print(&Apache::loncoursequeueadmin::update_request_queue($context,
                   4814:                           $cdom,$cnum,$coursedesc));
1.277     raeburn  4815:         }
1.239     raeburn  4816:     } elsif ($env{'form.action'} eq 'changelogs') {
1.363     raeburn  4817:         my $helpitem;
                   4818:         if ($context eq 'course') {
                   4819:             $helpitem = 'Course_User_Logs';
                   4820:         }
1.351     raeburn  4821:         push (@{$brcrum},
                   4822:                  {href => '/adm/createuser?action=changelogs',
                   4823:                   text => 'User Management Logs',
1.363     raeburn  4824:                   help => $helpitem});
1.351     raeburn  4825:         $bread_crumbs_component = 'User Changes';
                   4826:         $args = { bread_crumbs           => $brcrum,
                   4827:                   bread_crumbs_component => $bread_crumbs_component};
                   4828:         $r->print(&header(undef,$args));
                   4829:         &print_userchangelogs_display($r,$context,$permission);
1.190     raeburn  4830:     } else {
1.351     raeburn  4831:         $bread_crumbs_component = 'User Management';
                   4832:         $args = { bread_crumbs           => $brcrum,
                   4833:                   bread_crumbs_component => $bread_crumbs_component};
                   4834:         $r->print(&header(undef,$args));
1.318     raeburn  4835:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4836:     }
1.351     raeburn  4837:     $r->print(&Apache::loncommon::end_page());
1.190     raeburn  4838:     return OK;
                   4839: }
                   4840: 
                   4841: sub header {
1.351     raeburn  4842:     my ($jscript,$args) = @_;
1.190     raeburn  4843:     my $start_page;
1.351     raeburn  4844:     if (ref($args) eq 'HASH') {
                   4845:         $start_page=&Apache::loncommon::start_page('User Management',$jscript,$args);
1.190     raeburn  4846:     } else {
1.351     raeburn  4847:         $start_page=&Apache::loncommon::start_page('User Management',$jscript);
1.190     raeburn  4848:     }
                   4849:     return $start_page;
                   4850: }
1.2       www      4851: 
1.191     raeburn  4852: sub add_script {
                   4853:     my ($js) = @_;
1.301     bisitz   4854:     return '<script type="text/javascript">'."\n"
                   4855:           .'// <![CDATA['."\n"
                   4856:           .$js."\n"
                   4857:           .'// ]]>'."\n"
                   4858:           .'</script>'."\n";
1.191     raeburn  4859: }
                   4860: 
1.202     raeburn  4861: sub verify_user_display {
1.364     raeburn  4862:     my ($context) = @_;
1.374     raeburn  4863:     my %lt = &Apache::lonlocal::texthash (
                   4864:         course    => 'course(s): description, section(s), status',
                   4865:         community => 'community(s): description, section(s), status',
                   4866:         author    => 'author',
                   4867:     );
1.364     raeburn  4868:     my $photos;
                   4869:     if (($context eq 'course') && $env{'request.course.id'}) {
                   4870:         $photos = $env{'course.'.$env{'request.course.id'}.'.internal.showphoto'};
                   4871:     }
1.202     raeburn  4872:     my $output = <<"END";
                   4873: 
1.364     raeburn  4874: function hide_searching() {
                   4875:     if (document.getElementById('searching')) {
                   4876:         document.getElementById('searching').style.display = 'none';
                   4877:     }
                   4878:     return;
                   4879: }
                   4880: 
1.202     raeburn  4881: function display_update() {
                   4882:     document.studentform.action.value = 'listusers';
                   4883:     document.studentform.phase.value = 'display';
                   4884:     document.studentform.submit();
                   4885: }
                   4886: 
1.364     raeburn  4887: function updateCols(caller) {
                   4888:     var context = '$context';
                   4889:     var photos = '$photos';
                   4890:     if (caller == 'Status') {
1.374     raeburn  4891:         if ((context == 'domain') && 
                   4892:             ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   4893:              (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community'))) {
1.364     raeburn  4894:             document.getElementById('showcolstatus').checked = false;
                   4895:             document.getElementById('showcolstatus').disabled = 'disabled';
                   4896:             document.getElementById('showcolstart').checked = false;
                   4897:             document.getElementById('showcolend').checked = false;
1.374     raeburn  4898:         } else {
                   4899:             if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   4900:                 document.getElementById('showcolstatus').checked = true;
                   4901:                 document.getElementById('showcolstatus').disabled = '';
                   4902:                 document.getElementById('showcolstart').checked = true;
                   4903:                 document.getElementById('showcolend').checked = true;
                   4904:             } else {
                   4905:                 document.getElementById('showcolstatus').checked = false;
                   4906:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   4907:                 document.getElementById('showcolstart').checked = false;
                   4908:                 document.getElementById('showcolend').checked = false;
                   4909:             }
1.364     raeburn  4910:         }
                   4911:     }
                   4912:     if (caller == 'output') {
                   4913:         if (photos == 1) {
                   4914:             if (document.getElementById('showcolphoto')) {
                   4915:                 var photoitem = document.getElementById('showcolphoto');
                   4916:                 if (document.studentform.output.options[document.studentform.output.selectedIndex].value == 'html') {
                   4917:                     photoitem.checked = true;
                   4918:                     photoitem.disabled = '';
                   4919:                 } else {
                   4920:                     photoitem.checked = false;
                   4921:                     photoitem.disabled = 'disabled';
                   4922:                 }
                   4923:             }
                   4924:         }
                   4925:     }
                   4926:     if (caller == 'showrole') {
1.371     raeburn  4927:         if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any') ||
                   4928:             (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'cr')) {
1.364     raeburn  4929:             document.getElementById('showcolrole').checked = true;
                   4930:             document.getElementById('showcolrole').disabled = '';
                   4931:         } else {
                   4932:             document.getElementById('showcolrole').checked = false;
                   4933:             document.getElementById('showcolrole').disabled = 'disabled';
                   4934:         }
1.374     raeburn  4935:         if (context == 'domain') {
1.382     raeburn  4936:             var quotausageshow = 0;
1.374     raeburn  4937:             if ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   4938:                 (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community')) {
                   4939:                 document.getElementById('showcolstatus').checked = false;
                   4940:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   4941:                 document.getElementById('showcolstart').checked = false;
                   4942:                 document.getElementById('showcolend').checked = false;
                   4943:             } else {
                   4944:                 if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   4945:                     document.getElementById('showcolstatus').checked = true;
                   4946:                     document.getElementById('showcolstatus').disabled = '';
                   4947:                     document.getElementById('showcolstart').checked = true;
                   4948:                     document.getElementById('showcolend').checked = true;
                   4949:                 }
                   4950:             }
                   4951:             if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'domain') {
                   4952:                 document.getElementById('showcolextent').disabled = 'disabled';
                   4953:                 document.getElementById('showcolextent').checked = 'false';
                   4954:                 document.getElementById('showextent').style.display='none';
                   4955:                 document.getElementById('showcoltextextent').innerHTML = '';
1.382     raeburn  4956:                 if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'au') ||
                   4957:                     (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any')) {
                   4958:                     if (document.getElementById('showcolauthorusage')) {
                   4959:                         document.getElementById('showcolauthorusage').disabled = '';
                   4960:                     }
                   4961:                     if (document.getElementById('showcolauthorquota')) {
                   4962:                         document.getElementById('showcolauthorquota').disabled = '';
                   4963:                     }
                   4964:                     quotausageshow = 1;
                   4965:                 }
1.374     raeburn  4966:             } else {
                   4967:                 document.getElementById('showextent').style.display='block';
                   4968:                 document.getElementById('showextent').style.textAlign='left';
                   4969:                 document.getElementById('showextent').style.textFace='normal';
                   4970:                 if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'author') {
                   4971:                     document.getElementById('showcolextent').disabled = '';
                   4972:                     document.getElementById('showcolextent').checked = 'true';
                   4973:                     document.getElementById('showcoltextextent').innerHTML="$lt{'author'}";
                   4974:                 } else {
                   4975:                     document.getElementById('showcolextent').disabled = '';
                   4976:                     document.getElementById('showcolextent').checked = 'true';
                   4977:                     if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community') {
                   4978:                         document.getElementById('showcoltextextent').innerHTML="$lt{'community'}";
                   4979:                     } else {
                   4980:                         document.getElementById('showcoltextextent').innerHTML="$lt{'course'}";
                   4981:                     }
                   4982:                 }
                   4983:             }
1.382     raeburn  4984:             if (quotausageshow == 0)  {
                   4985:                 if (document.getElementById('showcolauthorusage')) {
                   4986:                     document.getElementById('showcolauthorusage').checked = false;
                   4987:                     document.getElementById('showcolauthorusage').disabled = 'disabled';
                   4988:                 }
                   4989:                 if (document.getElementById('showcolauthorquota')) {
                   4990:                     document.getElementById('showcolauthorquota').checked = false;
                   4991:                     document.getElementById('showcolauthorquota').disabled = 'disabled';
                   4992:                 }
                   4993:             }
1.374     raeburn  4994:         }
1.364     raeburn  4995:     }
                   4996:     return;
                   4997: }
                   4998: 
1.202     raeburn  4999: END
                   5000:     return $output;
                   5001: 
                   5002: }
                   5003: 
1.190     raeburn  5004: ###############################################################
                   5005: ###############################################################
                   5006: #  Menu Phase One
                   5007: sub print_main_menu {
1.318     raeburn  5008:     my ($permission,$context,$crstype) = @_;
                   5009:     my $linkcontext = $context;
                   5010:     my $stuterm = lc(&Apache::lonnet::plaintext('st',$crstype));
                   5011:     if (($context eq 'course') && ($crstype eq 'Community')) {
                   5012:         $linkcontext = lc($crstype);
                   5013:         $stuterm = 'Members';
                   5014:     }
1.208     raeburn  5015:     my %links = (
1.298     droeschl 5016:                 domain => {
                   5017:                             upload     => 'Upload a File of Users',
                   5018:                             singleuser => 'Add/Modify a User',
                   5019:                             listusers  => 'Manage Users',
                   5020:                             },
                   5021:                 author => {
                   5022:                             upload     => 'Upload a File of Co-authors',
                   5023:                             singleuser => 'Add/Modify a Co-author',
                   5024:                             listusers  => 'Manage Co-authors',
                   5025:                             },
                   5026:                 course => {
                   5027:                             upload     => 'Upload a File of Course Users',
                   5028:                             singleuser => 'Add/Modify a Course User',
1.354     www      5029:                             listusers  => 'List and Modify Multiple Course Users',
1.298     droeschl 5030:                             },
1.318     raeburn  5031:                 community => {
                   5032:                             upload     => 'Upload a File of Community Users',
                   5033:                             singleuser => 'Add/Modify a Community User',
1.354     www      5034:                             listusers  => 'List and Modify Multiple Community Users',
1.318     raeburn  5035:                            },
                   5036:                 );
                   5037:      my %linktitles = (
                   5038:                 domain => {
                   5039:                             singleuser => 'Add a user to the domain, and/or a course or community in the domain.',
                   5040:                             listusers  => 'Show and manage users in this domain.',
                   5041:                             },
                   5042:                 author => {
                   5043:                             singleuser => 'Add a user with a co- or assistant author role.',
                   5044:                             listusers  => 'Show and manage co- or assistant authors.',
                   5045:                             },
                   5046:                 course => {
                   5047:                             singleuser => 'Add a user with a certain role to this course.',
                   5048:                             listusers  => 'Show and manage users in this course.',
                   5049:                             },
                   5050:                 community => {
                   5051:                             singleuser => 'Add a user with a certain role to this community.',
                   5052:                             listusers  => 'Show and manage users in this community.',
                   5053:                            },
1.298     droeschl 5054:                 );
                   5055:   my @menu = ( {categorytitle => 'Single Users', 
                   5056:          items =>
                   5057:          [
                   5058:             {
1.318     raeburn  5059:              linktext => $links{$linkcontext}{'singleuser'},
1.298     droeschl 5060:              icon => 'edit-redo.png',
                   5061:              #help => 'Course_Change_Privileges',
                   5062:              url => '/adm/createuser?action=singleuser',
                   5063:              permission => $permission->{'cusr'},
1.318     raeburn  5064:              linktitle => $linktitles{$linkcontext}{'singleuser'},
1.298     droeschl 5065:             },
                   5066:          ]},
                   5067: 
                   5068:          {categorytitle => 'Multiple Users',
                   5069:          items => 
                   5070:          [
                   5071:             {
1.318     raeburn  5072:              linktext => $links{$linkcontext}{'upload'},
1.340     wenzelju 5073:              icon => 'uplusr.png',
1.298     droeschl 5074:              #help => 'Course_Create_Class_List',
                   5075:              url => '/adm/createuser?action=upload',
                   5076:              permission => $permission->{'cusr'},
                   5077:              linktitle => 'Upload a CSV or a text file containing users.',
                   5078:             },
                   5079:             {
1.318     raeburn  5080:              linktext => $links{$linkcontext}{'listusers'},
1.340     wenzelju 5081:              icon => 'mngcu.png',
1.298     droeschl 5082:              #help => 'Course_View_Class_List',
                   5083:              url => '/adm/createuser?action=listusers',
                   5084:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  5085:              linktitle => $linktitles{$linkcontext}{'listusers'}, 
1.298     droeschl 5086:             },
                   5087: 
                   5088:          ]},
                   5089: 
                   5090:          {categorytitle => 'Administration',
                   5091:          items => [ ]},
                   5092:        );
                   5093:             
1.265     mielkec  5094:     if ($context eq 'domain'){
1.298     droeschl 5095:         
                   5096:         push(@{ $menu[2]->{items} }, #Category: Administration
                   5097:             {
                   5098:              linktext => 'Custom Roles',
                   5099:              icon => 'emblem-photos.png',
                   5100:              #help => 'Course_Editing_Custom_Roles',
                   5101:              url => '/adm/createuser?action=custom',
                   5102:              permission => $permission->{'custom'},
                   5103:              linktitle => 'Configure a custom role.',
                   5104:             },
1.362     raeburn  5105:             {
                   5106:              linktext => 'Authoring Space Requests',
                   5107:              icon => 'selfenrl-queue.png',
                   5108:              #help => 'Domain_Role_Approvals',
                   5109:              url => '/adm/createuser?action=processauthorreq',
                   5110:              permission => $permission->{'cusr'},
                   5111:              linktitle => 'Approve or reject author role requests',
                   5112:             },
1.363     raeburn  5113:             {
                   5114:              linktext => 'Change Log',
                   5115:              icon => 'document-properties.png',
                   5116:              #help => 'Course_User_Logs',
                   5117:              url => '/adm/createuser?action=changelogs',
                   5118:              permission => $permission->{'cusr'},
                   5119:              linktitle => 'View change log.',
                   5120:             },
1.298     droeschl 5121:         );
                   5122:         
1.265     mielkec  5123:     }elsif ($context eq 'course'){
1.298     droeschl 5124:         my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
1.318     raeburn  5125: 
                   5126:         my %linktext = (
                   5127:                          'Course'    => {
                   5128:                                           single => 'Add/Modify a Student', 
                   5129:                                           drop   => 'Drop Students',
                   5130:                                           groups => 'Course Groups',
                   5131:                                         },
                   5132:                          'Community' => {
                   5133:                                           single => 'Add/Modify a Member', 
                   5134:                                           drop   => 'Drop Members',
                   5135:                                           groups => 'Community Groups',
                   5136:                                         },
                   5137:                        );
                   5138: 
                   5139:         my %linktitle = (
                   5140:             'Course' => {
                   5141:                   single => 'Add a user with the role of student to this course',
                   5142:                   drop   => 'Remove a student from this course.',
                   5143:                   groups => 'Manage course groups',
                   5144:                         },
                   5145:             'Community' => {
                   5146:                   single => 'Add a user with the role of member to this community',
                   5147:                   drop   => 'Remove a member from this community.',
                   5148:                   groups => 'Manage community groups',
                   5149:                            },
                   5150:         );
                   5151: 
1.298     droeschl 5152:         push(@{ $menu[0]->{items} }, #Category: Single Users
                   5153:             {   
1.318     raeburn  5154:              linktext => $linktext{$crstype}{'single'},
1.298     droeschl 5155:              #help => 'Course_Add_Student',
                   5156:              icon => 'list-add.png',
                   5157:              url => '/adm/createuser?action=singlestudent',
                   5158:              permission => $permission->{'cusr'},
1.318     raeburn  5159:              linktitle => $linktitle{$crstype}{'single'},
1.298     droeschl 5160:             },
                   5161:         );
                   5162:         
                   5163:         push(@{ $menu[1]->{items} }, #Category: Multiple Users 
                   5164:             {
1.318     raeburn  5165:              linktext => $linktext{$crstype}{'drop'},
1.298     droeschl 5166:              icon => 'edit-undo.png',
                   5167:              #help => 'Course_Drop_Student',
                   5168:              url => '/adm/createuser?action=drop',
                   5169:              permission => $permission->{'cusr'},
1.318     raeburn  5170:              linktitle => $linktitle{$crstype}{'drop'},
1.298     droeschl 5171:             },
                   5172:         );
                   5173:         push(@{ $menu[2]->{items} }, #Category: Administration
                   5174:             {    
                   5175:              linktext => 'Custom Roles',
                   5176:              icon => 'emblem-photos.png',
                   5177:              #help => 'Course_Editing_Custom_Roles',
                   5178:              url => '/adm/createuser?action=custom',
                   5179:              permission => $permission->{'custom'},
                   5180:              linktitle => 'Configure a custom role.',
                   5181:             },
                   5182:             {
1.318     raeburn  5183:              linktext => $linktext{$crstype}{'groups'},
1.333     wenzelju 5184:              icon => 'grps.png',
1.298     droeschl 5185:              #help => 'Course_Manage_Group',
                   5186:              url => '/adm/coursegroups?refpage=cusr',
                   5187:              permission => $permission->{'grp_manage'},
1.318     raeburn  5188:              linktitle => $linktitle{$crstype}{'groups'},
1.298     droeschl 5189:             },
                   5190:             {
1.328     wenzelju 5191:              linktext => 'Change Log',
1.298     droeschl 5192:              icon => 'document-properties.png',
                   5193:              #help => 'Course_User_Logs',
                   5194:              url => '/adm/createuser?action=changelogs',
                   5195:              permission => $permission->{'cusr'},
                   5196:              linktitle => 'View change log.',
                   5197:             },
                   5198:         );
1.277     raeburn  5199:         if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'}) {
1.298     droeschl 5200:             push(@{ $menu[2]->{items} },
                   5201:                     {   
                   5202:                      linktext => 'Enrollment Requests',
                   5203:                      icon => 'selfenrl-queue.png',
                   5204:                      #help => 'Course_Approve_Selfenroll',
                   5205:                      url => '/adm/createuser?action=selfenrollqueue',
                   5206:                      permission => $permission->{'cusr'},
                   5207:                      linktitle =>'Approve or reject enrollment requests.',
                   5208:                     },
                   5209:             );
1.277     raeburn  5210:         }
1.298     droeschl 5211:         
1.265     mielkec  5212:         if (!exists($permission->{'cusr_section'})){
1.320     raeburn  5213:             if ($crstype ne 'Community') {
                   5214:                 push(@{ $menu[2]->{items} },
                   5215:                     {
                   5216:                      linktext => 'Automated Enrollment',
                   5217:                      icon => 'roles.png',
                   5218:                      #help => 'Course_Automated_Enrollment',
                   5219:                      permission => (&Apache::lonnet::auto_run($cnum,$cdom)
                   5220:                                          && $permission->{'cusr'}),
                   5221:                      url  => '/adm/populate',
                   5222:                      linktitle => 'Automated enrollment manager.',
                   5223:                     }
                   5224:                 );
                   5225:             }
                   5226:             push(@{ $menu[2]->{items} }, 
1.298     droeschl 5227:                 {
                   5228:                  linktext => 'User Self-Enrollment',
1.342     wenzelju 5229:                  icon => 'self_enroll.png',
1.298     droeschl 5230:                  #help => 'Course_Self_Enrollment',
                   5231:                  url => '/adm/createuser?action=selfenroll',
                   5232:                  permission => $permission->{'cusr'},
1.317     bisitz   5233:                  linktitle => 'Configure user self-enrollment.',
1.298     droeschl 5234:                 },
                   5235:             );
                   5236:         }
1.363     raeburn  5237:     } elsif ($context eq 'author') {
1.370     raeburn  5238:         push(@{ $menu[2]->{items} }, #Category: Administration
1.363     raeburn  5239:             {
                   5240:              linktext => 'Change Log',
                   5241:              icon => 'document-properties.png',
                   5242:              #help => 'Course_User_Logs',
                   5243:              url => '/adm/createuser?action=changelogs',
                   5244:              permission => $permission->{'cusr'},
                   5245:              linktitle => 'View change log.',
                   5246:             },
1.370     raeburn  5247:         );
1.363     raeburn  5248:     }
                   5249:     return Apache::lonhtmlcommon::generate_menu(@menu);
1.250     raeburn  5250: #               { text => 'View Log-in History',
                   5251: #                 help => 'Course_User_Logins',
                   5252: #                 action => 'logins',
                   5253: #                 permission => $permission->{'cusr'},
                   5254: #               });
1.190     raeburn  5255: }
                   5256: 
1.189     albertel 5257: sub restore_prev_selections {
                   5258:     my %saveable_parameters = ('srchby'   => 'scalar',
                   5259: 			       'srchin'   => 'scalar',
                   5260: 			       'srchtype' => 'scalar',
                   5261: 			       );
                   5262:     &Apache::loncommon::store_settings('user','user_picker',
                   5263: 				       \%saveable_parameters);
                   5264:     &Apache::loncommon::restore_settings('user','user_picker',
                   5265: 					 \%saveable_parameters);
                   5266: }
                   5267: 
1.237     raeburn  5268: sub print_selfenroll_menu {
                   5269:     my ($r,$context,$permission) = @_;
1.322     raeburn  5270:     my $crstype = &Apache::loncommon::course_type();
1.237     raeburn  5271:     my $formname = 'enrollstudent';
                   5272:     my $nolink = 1;
                   5273:     my ($row,$lt) = &get_selfenroll_titles();
                   5274:     my $groupslist = &Apache::lonuserutils::get_groupslist();
                   5275:     my $setsec_js = 
                   5276:         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
1.249     raeburn  5277:     my %alerts = &Apache::lonlocal::texthash(
                   5278:         acto => 'Activation of self-enrollment was selected for the following domain(s)',
                   5279:         butn => 'but no user types have been checked.',
                   5280:         wilf => "Please uncheck 'activate' or check at least one type.",
                   5281:     );
                   5282:     my $selfenroll_js = <<"ENDSCRIPT";
                   5283: function update_types(caller,num) {
                   5284:     var delidx = getIndexByName('selfenroll_delete');
                   5285:     var actidx = getIndexByName('selfenroll_activate');
                   5286:     if (caller == 'selfenroll_all') {
                   5287:         var selall;
                   5288:         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5289:             if (document.$formname.selfenroll_all[i].checked) {
                   5290:                 selall = document.$formname.selfenroll_all[i].value;
                   5291:             }
                   5292:         }
                   5293:         if (selall == 1) {
                   5294:             if (delidx != -1) {
                   5295:                 if (document.$formname.selfenroll_delete.length) {
                   5296:                     for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5297:                         document.$formname.selfenroll_delete[j].checked = true;
                   5298:                     }
                   5299:                 } else {
                   5300:                     document.$formname.elements[delidx].checked = true;
                   5301:                 }
                   5302:             }
                   5303:             if (actidx != -1) {
                   5304:                 if (document.$formname.selfenroll_activate.length) {
                   5305:                     for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5306:                         document.$formname.selfenroll_activate[j].checked = false;
                   5307:                     }
                   5308:                 } else {
                   5309:                     document.$formname.elements[actidx].checked = false;
                   5310:                 }
                   5311:             }
                   5312:             document.$formname.selfenroll_newdom.selectedIndex = 0; 
                   5313:         }
                   5314:     }
                   5315:     if (caller == 'selfenroll_activate') {
                   5316:         if (document.$formname.selfenroll_activate.length) {
                   5317:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5318:                 if (document.$formname.selfenroll_activate[j].value == num) {
                   5319:                     if (document.$formname.selfenroll_activate[j].checked) {
                   5320:                         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5321:                             if (document.$formname.selfenroll_all[i].value == '1') {
                   5322:                                 document.$formname.selfenroll_all[i].checked = false;
                   5323:                             }
                   5324:                             if (document.$formname.selfenroll_all[i].value == '0') {
                   5325:                                 document.$formname.selfenroll_all[i].checked = true;
                   5326:                             }
                   5327:                         }
                   5328:                     }
                   5329:                 }
                   5330:             }
                   5331:         } else {
                   5332:             for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5333:                 if (document.$formname.selfenroll_all[i].value == '1') {
                   5334:                     document.$formname.selfenroll_all[i].checked = false;
                   5335:                 }
                   5336:                 if (document.$formname.selfenroll_all[i].value == '0') {
                   5337:                     document.$formname.selfenroll_all[i].checked = true;
                   5338:                 }
                   5339:             }
                   5340:         }
                   5341:     }
                   5342:     if (caller == 'selfenroll_delete') {
                   5343:         if (document.$formname.selfenroll_delete.length) {
                   5344:             for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5345:                 if (document.$formname.selfenroll_delete[j].value == num) {
                   5346:                     if (document.$formname.selfenroll_delete[j].checked) {
                   5347:                         var delindex = getIndexByName('selfenroll_types_'+num);
                   5348:                         if (delindex != -1) { 
                   5349:                             if (document.$formname.elements[delindex].length) {
                   5350:                                 for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5351:                                     document.$formname.elements[delindex][k].checked = false;
                   5352:                                 }
                   5353:                             } else {
                   5354:                                 document.$formname.elements[delindex].checked = false;
                   5355:                             }
                   5356:                         }
                   5357:                     }
                   5358:                 }
                   5359:             }
                   5360:         } else {
                   5361:             if (document.$formname.selfenroll_delete.checked) {
                   5362:                 var delindex = getIndexByName('selfenroll_types_'+num);
                   5363:                 if (delindex != -1) {
                   5364:                     if (document.$formname.elements[delindex].length) {
                   5365:                         for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5366:                             document.$formname.elements[delindex][k].checked = false;
                   5367:                         }
                   5368:                     } else {
                   5369:                         document.$formname.elements[delindex].checked = false;
                   5370:                     }
                   5371:                 }
                   5372:             }
                   5373:         }
                   5374:     }
                   5375:     return;
                   5376: }
                   5377: 
                   5378: function validate_types(form) {
                   5379:     var needaction = new Array();
                   5380:     var countfail = 0;
                   5381:     var actidx = getIndexByName('selfenroll_activate');
                   5382:     if (actidx != -1) {
                   5383:         if (document.$formname.selfenroll_activate.length) {
                   5384:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5385:                 var num = document.$formname.selfenroll_activate[j].value;
                   5386:                 if (document.$formname.selfenroll_activate[j].checked) {
                   5387:                     countfail = check_types(num,countfail,needaction)
                   5388:                 }
                   5389:             }
                   5390:         } else {
                   5391:             if (document.$formname.selfenroll_activate.checked) {
                   5392:                 var num = document.enrollstudent.selfenroll_activate.value;
                   5393:                 countfail = check_types(num,countfail,needaction)
                   5394:             }
                   5395:         }
                   5396:     }
                   5397:     if (countfail > 0) {
                   5398:         var msg = "$alerts{'acto'}\\n";
                   5399:         var loopend = needaction.length -1;
                   5400:         if (loopend > 0) {
                   5401:             for (var m=0; m<loopend; m++) {
                   5402:                 msg += needaction[m]+", ";
                   5403:             }
                   5404:         }
                   5405:         msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
                   5406:         alert(msg);
                   5407:         return; 
                   5408:     }
                   5409:     setSections(form);
                   5410: }
                   5411: 
                   5412: function check_types(num,countfail,needaction) {
                   5413:     var typeidx = getIndexByName('selfenroll_types_'+num);
                   5414:     var count = 0;
                   5415:     if (typeidx != -1) {
                   5416:         if (document.$formname.elements[typeidx].length) {
                   5417:             for (var k=0; k<document.$formname.elements[typeidx].length; k++) {
                   5418:                 if (document.$formname.elements[typeidx][k].checked) {
                   5419:                     count ++;
                   5420:                 }
                   5421:             }
                   5422:         } else {
                   5423:             if (document.$formname.elements[typeidx].checked) {
                   5424:                 count ++;
                   5425:             }
                   5426:         }
                   5427:         if (count == 0) {
                   5428:             var domidx = getIndexByName('selfenroll_dom_'+num);
                   5429:             if (domidx != -1) {
                   5430:                 var domname = document.$formname.elements[domidx].value;
                   5431:                 needaction[countfail] = domname;
                   5432:                 countfail ++;
                   5433:             }
                   5434:         }
                   5435:     }
                   5436:     return countfail;
                   5437: }
                   5438: 
                   5439: function getIndexByName(item) {
                   5440:     for (var i=0;i<document.$formname.elements.length;i++) {
                   5441:         if (document.$formname.elements[i].name == item) {
                   5442:             return i;
                   5443:         }
                   5444:     }
                   5445:     return -1;
                   5446: }
                   5447: ENDSCRIPT
1.256     raeburn  5448:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5449:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   5450: 
1.237     raeburn  5451:     my $output = '<script type="text/javascript">'."\n".
1.301     bisitz   5452:                  '// <![CDATA['."\n".
1.249     raeburn  5453:                  $setsec_js."\n".$selfenroll_js."\n".
1.301     bisitz   5454:                  '// ]]>'."\n".
1.237     raeburn  5455:                  '</script>'."\n".
1.256     raeburn  5456:                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n";
                   5457:     my ($visible,$cansetvis,$vismsgs,$visactions) = &visible_in_cat($cdom,$cnum);
                   5458:     if (ref($visactions) eq 'HASH') {
                   5459:         if ($visible) {
1.283     bisitz   5460:             $output .= '<p class="LC_info">'.$visactions->{'vis'}.'</p>';
1.256     raeburn  5461:         } else {
1.283     bisitz   5462:             $output .= '<p class="LC_warning">'.$visactions->{'miss'}.'</p>'
                   5463:                       .$visactions->{'yous'}.
1.256     raeburn  5464:                        '<p>'.$visactions->{'gen'}.'<br />'.$visactions->{'coca'};
                   5465:             if (ref($vismsgs) eq 'ARRAY') {
                   5466:                 $output .= '<br />'.$visactions->{'make'}.'<ul>';
                   5467:                 foreach my $item (@{$vismsgs}) {
                   5468:                     $output .= '<li>'.$visactions->{$item}.'</li>';
                   5469:                 }
                   5470:                 $output .= '</ul>';
                   5471:             }
                   5472:             $output .= '</p>';
                   5473:         }
                   5474:     }
                   5475:     $output .= '<form name="'.$formname.'" method="post" action="/adm/createuser">'."\n".
                   5476:                &Apache::lonhtmlcommon::start_pick_box();
1.237     raeburn  5477:     if (ref($row) eq 'ARRAY') {
                   5478:         foreach my $item (@{$row}) {
                   5479:             my $title = $item; 
                   5480:             if (ref($lt) eq 'HASH') {
                   5481:                 $title = $lt->{$item};
                   5482:             }
1.297     bisitz   5483:             $output .= &Apache::lonhtmlcommon::row_title($title);
1.237     raeburn  5484:             if ($item eq 'types') {
                   5485:                 my $curr_types = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_types'};
1.241     raeburn  5486:                 my $showdomdesc = 1;
                   5487:                 my $includeempty = 1;
                   5488:                 my $num = 0;
                   5489:                 $output .= &Apache::loncommon::start_data_table().
                   5490:                            &Apache::loncommon::start_data_table_row()
                   5491:                            .'<td colspan="2"><span class="LC_nobreak"><label>'
                   5492:                            .&mt('Any user in any domain:')
                   5493:                            .'&nbsp;<input type="radio" name="selfenroll_all" value="1" ';
                   5494:                 if ($curr_types eq '*') {
                   5495:                     $output .= ' checked="checked" '; 
                   5496:                 }
1.249     raeburn  5497:                 $output .= 'onchange="javascript:update_types('.
                   5498:                            "'selfenroll_all'".');" />'.&mt('Yes').'</label>'.
                   5499:                            '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
1.241     raeburn  5500:                 if ($curr_types ne '*') {
                   5501:                     $output .= ' checked="checked" ';
                   5502:                 }
1.249     raeburn  5503:                 $output .= ' onchange="javascript:update_types('.
                   5504:                            "'selfenroll_all'".');"/>'.&mt('No').'</label></td>'.
                   5505:                            &Apache::loncommon::end_data_table_row().
                   5506:                            &Apache::loncommon::end_data_table().
                   5507:                            &mt('Or').'<br />'.
                   5508:                            &Apache::loncommon::start_data_table();
1.241     raeburn  5509:                 my %currdoms;
1.249     raeburn  5510:                 if ($curr_types eq '') {
1.241     raeburn  5511:                     $output .= &new_selfenroll_dom_row($cdom,'0');
                   5512:                 } elsif ($curr_types ne '*') {
                   5513:                     my @entries = split(/;/,$curr_types);
                   5514:                     if (@entries > 0) {
                   5515:                         foreach my $entry (@entries) {
                   5516:                             my ($currdom,$typestr) = split(/:/,$entry);
                   5517:                             $currdoms{$currdom} = 1;
                   5518:                             my $domdesc = &Apache::lonnet::domain($currdom);
1.249     raeburn  5519:                             my @currinsttypes = split(',',$typestr);
1.241     raeburn  5520:                             $output .= &Apache::loncommon::start_data_table_row()
                   5521:                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
                   5522:                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
                   5523:                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
                   5524:                                        .'" value="'.$currdom.'" /></span><br />'
                   5525:                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
1.249     raeburn  5526:                                        .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');" />'
1.241     raeburn  5527:                                        .&mt('Delete').'</label></span></td>';
1.249     raeburn  5528:                             $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
1.241     raeburn  5529:                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes).'</td>'
                   5530:                                        .&Apache::loncommon::end_data_table_row();
                   5531:                             $num ++;
                   5532:                         }
                   5533:                     }
                   5534:                 }
1.249     raeburn  5535:                 my $add_domtitle = &mt('Users in additional domain:');
1.241     raeburn  5536:                 if ($curr_types eq '*') { 
1.249     raeburn  5537:                     $add_domtitle = &mt('Users in specific domain:');
1.241     raeburn  5538:                 } elsif ($curr_types eq '') {
1.249     raeburn  5539:                     $add_domtitle = &mt('Users in other domain:');
1.241     raeburn  5540:                 }
                   5541:                 $output .= &Apache::loncommon::start_data_table_row()
                   5542:                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
                   5543:                            .&Apache::loncommon::select_dom_form('','selfenroll_newdom',
                   5544:                                                                 $includeempty,$showdomdesc)
                   5545:                            .'<input type="hidden" name="selfenroll_types_total" value="'.$num.'" />'
                   5546:                            .'</td>'.&Apache::loncommon::end_data_table_row()
                   5547:                            .&Apache::loncommon::end_data_table();
1.237     raeburn  5548:             } elsif ($item eq 'registered') {
                   5549:                 my ($regon,$regoff);
                   5550:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_registered'}) {
                   5551:                     $regon = ' checked="checked" ';
                   5552:                     $regoff = ' ';
                   5553:                 } else {
                   5554:                     $regon = ' ';
                   5555:                     $regoff = ' checked="checked" ';
                   5556:                 }
                   5557:                 $output .= '<label>'.
1.245     raeburn  5558:                            '<input type="radio" name="selfenroll_registered" value="1"'.$regon.'/>'.
1.244     bisitz   5559:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
1.245     raeburn  5560:                            '<input type="radio" name="selfenroll_registered" value="0"'.$regoff.'/>'.
1.244     bisitz   5561:                            &mt('No').'</label>';
1.237     raeburn  5562:             } elsif ($item eq 'enroll_dates') {
                   5563:                 my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_date'};
                   5564:                 my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_date'};
                   5565:                 if ($starttime eq '') {
                   5566:                     $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   5567:                 }
                   5568:                 if ($endtime eq '') {
                   5569:                     $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   5570:                 }
                   5571:                 my $startform =
                   5572:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
                   5573:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5574:                 my $endform =
                   5575:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
                   5576:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5577:                 $output .= &selfenroll_date_forms($startform,$endform);
                   5578:             } elsif ($item eq 'access_dates') {
                   5579:                 my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_access'};
                   5580:                 my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_access'};
                   5581:                 if ($starttime eq '') {
                   5582:                     $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   5583:                 }
                   5584:                 if ($endtime eq '') {
                   5585:                     $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   5586:                 }
                   5587:                 my $startform =
                   5588:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
                   5589:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5590:                 my $endform =
                   5591:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
                   5592:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5593:                 $output .= &selfenroll_date_forms($startform,$endform);
                   5594:             } elsif ($item eq 'section') {
                   5595:                 my $currsec = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_section'}; 
                   5596:                 my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   5597:                 my $newsecval;
                   5598:                 if ($currsec ne 'none' && $currsec ne '') {
                   5599:                     if (!defined($sections_count{$currsec})) {
                   5600:                         $newsecval = $currsec;
                   5601:                     }
                   5602:                 }
                   5603:                 my $sections_select = 
                   5604:                     &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec);
                   5605:                 $output .= '<table class="LC_createuser">'."\n".
                   5606:                            '<tr class="LC_section_row">'."\n".
                   5607:                            '<td align="center">'.&mt('Existing sections')."\n".
                   5608:                            '<br />'.$sections_select.'</td><td align="center">'.
                   5609:                            &mt('New section').'<br />'."\n".
                   5610:                            '<input type="text" name="newsec" size="15" value="'.$newsecval.'" />'."\n".
                   5611:                            '<input type="hidden" name="sections" value="" />'."\n".
                   5612:                            '<input type="hidden" name="state" value="done" />'."\n".
                   5613:                            '</td></tr></table>'."\n";
1.276     raeburn  5614:             } elsif ($item eq 'approval') {
                   5615:                 my ($appon,$appoff);
                   5616:                 my $cid = $env{'request.course.id'};
                   5617:                 my $currnotified = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
                   5618:                 if ($env{'course.'.$cid.'.internal.selfenroll_approval'}) {
                   5619:                     $appon = ' checked="checked" ';
                   5620:                     $appoff = ' ';
                   5621:                 } else {
                   5622:                     $appon = ' ';
                   5623:                     $appoff = ' checked="checked" ';
                   5624:                 }
                   5625:                 $output .= '<label>'.
                   5626:                            '<input type="radio" name="selfenroll_approval" value="1"'.$appon.'/>'.
                   5627:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
                   5628:                            '<input type="radio" name="selfenroll_approval" value="0"'.$appoff.'/>'.
                   5629:                            &mt('No').'</label>';
                   5630:                 my %advhash = &Apache::lonnet::get_course_adv_roles($cid,1);
                   5631:                 my (@ccs,%notified);
1.322     raeburn  5632:                 my $ccrole = 'cc';
                   5633:                 if ($crstype eq 'Community') {
                   5634:                     $ccrole = 'co';
                   5635:                 }
                   5636:                 if ($advhash{$ccrole}) {
                   5637:                     @ccs = split(/,/,$advhash{$ccrole});
1.276     raeburn  5638:                 }
                   5639:                 if ($currnotified) {
                   5640:                     foreach my $current (split(/,/,$currnotified)) {
                   5641:                         $notified{$current} = 1;
                   5642:                         if (!grep(/^\Q$current\E$/,@ccs)) {
                   5643:                             push(@ccs,$current);
                   5644:                         }
                   5645:                     }
                   5646:                 }
                   5647:                 if (@ccs) {
1.277     raeburn  5648:                     $output .= '<br />'.&mt('Personnel to be notified when an enrollment request needs approval, or has been approved:').'&nbsp;'.&Apache::loncommon::start_data_table().
1.276     raeburn  5649:                                &Apache::loncommon::start_data_table_row();
                   5650:                     my $count = 0;
                   5651:                     my $numcols = 4;
                   5652:                     foreach my $cc (sort(@ccs)) {
                   5653:                         my $notifyon;
                   5654:                         my ($ccuname,$ccudom) = split(/:/,$cc);
                   5655:                         if ($notified{$cc}) {
                   5656:                             $notifyon = ' checked="checked" ';
                   5657:                         }
                   5658:                         if ($count && !$count%$numcols) {
                   5659:                             $output .= &Apache::loncommon::end_data_table_row().
                   5660:                                        &Apache::loncommon::start_data_table_row()
                   5661:                         }
                   5662:                         $output .= '<td><span class="LC_nobreak"><label>'.
                   5663:                                    '<input type="checkbox" name="selfenroll_notify"'.$notifyon.' value="'.$cc.'" />'.
                   5664:                                    &Apache::loncommon::plainname($ccuname,$ccudom).
                   5665:                                    '</label></span></td>';
1.343     raeburn  5666:                         $count ++;
1.276     raeburn  5667:                     }
                   5668:                     my $rem = $count%$numcols;
                   5669:                     if ($rem) {
                   5670:                         my $emptycols = $numcols - $rem;
                   5671:                         for (my $i=0; $i<$emptycols; $i++) { 
                   5672:                             $output .= '<td>&nbsp;</td>';
                   5673:                         }
                   5674:                     }
                   5675:                     $output .= &Apache::loncommon::end_data_table_row().
                   5676:                                &Apache::loncommon::end_data_table();
                   5677:                 }
                   5678:             } elsif ($item eq 'limit') {
                   5679:                 my ($crslimit,$selflimit,$nolimit);
                   5680:                 my $cid = $env{'request.course.id'};
                   5681:                 my $currlim = $env{'course.'.$cid.'.internal.selfenroll_limit'};
                   5682:                 my $currcap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
1.343     raeburn  5683:                 $nolimit = ' checked="checked" ';
1.276     raeburn  5684:                 if ($currlim eq 'allstudents') {
                   5685:                     $crslimit = ' checked="checked" ';
                   5686:                     $selflimit = ' ';
                   5687:                     $nolimit = ' ';
                   5688:                 } elsif ($currlim eq 'selfenrolled') {
                   5689:                     $crslimit = ' ';
                   5690:                     $selflimit = ' checked="checked" ';
                   5691:                     $nolimit = ' '; 
                   5692:                 } else {
                   5693:                     $crslimit = ' ';
                   5694:                     $selflimit = ' ';
                   5695:                 }
                   5696:                 $output .= '<table><tr><td><label>'.
1.278     raeburn  5697:                            '<input type="radio" name="selfenroll_limit" value="none"'.$nolimit.'/>'.
1.276     raeburn  5698:                            &mt('No limit').'</label></td><td><label>'.
                   5699:                            '<input type="radio" name="selfenroll_limit" value="allstudents"'.$crslimit.'/>'.
                   5700:                            &mt('Limit by total students').'</label></td><td><label>'.
                   5701:                            '<input type="radio" name="selfenroll_limit" value="selfenrolled"'.$selflimit.'/>'.
                   5702:                            &mt('Limit by total self-enrolled students').
                   5703:                            '</td></tr><tr>'.
                   5704:                            '<td>&nbsp;</td><td colspan="2"><span class="LC_nobreak">'.
                   5705:                            ('&nbsp;'x3).&mt('Maximum number allowed: ').
                   5706:                            '<input type="text" name="selfenroll_cap" size = "5" value="'.$currcap.'" /></td></tr></table>';
1.237     raeburn  5707:             }
                   5708:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   5709:         }
                   5710:     }
                   5711:     $output .= &Apache::lonhtmlcommon::end_pick_box().
1.241     raeburn  5712:                '<br /><input type="button" name="selfenrollconf" value="'
1.282     schafran 5713:                .&mt('Save').'" onclick="validate_types(this.form);" />'
1.241     raeburn  5714:                .'<input type="hidden" name="action" value="selfenroll" /></form>';
1.237     raeburn  5715:     $r->print($output);
                   5716:     return;
                   5717: }
                   5718: 
1.256     raeburn  5719: sub visible_in_cat {
                   5720:     my ($cdom,$cnum) = @_;
                   5721:     my %domconf = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   5722:     my ($cathash,%settable,@vismsgs,$cansetvis);
                   5723:     my %visactions = &Apache::lonlocal::texthash(
1.316     bisitz   5724:                    vis => 'Your course/community currently appears in the Course/Community Catalog for this domain.',
1.256     raeburn  5725:                    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.',
1.316     bisitz   5726:                    miss => 'Your course/community does not currently appear in the Course/Community Catalog for this domain.',
1.256     raeburn  5727:                    yous => 'You should remedy this if you plan to allow self-enrollment, otherwise students will have difficulty finding your course.',
                   5728:                    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.',
1.282     schafran 5729:                    make => 'Make any changes to self-enrollment settings below, click "Save", then take action to include the course in the Catalog:',
1.256     raeburn  5730:                    take => 'Take the following action to ensure the course appears in the Catalog:',
                   5731:                    dc_unhide  => 'Ask a domain coordinator to change the "Exclude from course catalog" setting.',
                   5732:                    dc_addinst => 'Ask a domain coordinator to enable display the catalog of "Official courses (with institutional codes)".',
                   5733:                    dc_instcode => 'Ask a domain coordinator to assign an institutional code (if this is an official course).',
                   5734:                    dc_catalog  => 'Ask a domain coordinator to enable or create at least one course category in the domain.',
                   5735:                    dc_categories => 'Ask a domain coordinator to create a hierarchy of categories and sub categories for courses in the domain.',
                   5736:                    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',
                   5737:                    dc_addcat => 'Ask a domain coordinator to assign a category to the course.',
                   5738:     );
1.347     raeburn  5739:     $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>"');
                   5740:     $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>"');
                   5741:     $visactions{'addcat'} = &mt('Use [_1]Categorize course[_2] to assign a category to the course.','"<a href="/adm/courseprefs?phase=display&actions=courseinfo">','</a>"');
1.256     raeburn  5742:     if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5743:         if ($domconf{'coursecategories'}{'togglecats'} eq 'crs') {
                   5744:             $settable{'togglecats'} = 1;
                   5745:         }
                   5746:         if ($domconf{'coursecategories'}{'categorize'} eq 'crs') {
                   5747:             $settable{'categorize'} = 1;
                   5748:         }
                   5749:         $cathash = $domconf{'coursecategories'}{'cats'};
                   5750:     }
1.260     raeburn  5751:     if ($settable{'togglecats'} && $settable{'categorize'}) {
1.256     raeburn  5752:         $cansetvis = &mt('You are able to both assign a course category and choose to exclude this course from the catalog.');   
                   5753:     } elsif ($settable{'togglecats'}) {
                   5754:         $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  5755:     } elsif ($settable{'categorize'}) {
1.256     raeburn  5756:         $cansetvis = &mt('You may assign a course category, but only a Domain Coordinator may choose to exclude this course from the catalog.');  
                   5757:     } else {
                   5758:         $cansetvis = &mt('Only a Domain Coordinator may assign a course category or choose to exclude this course from the catalog.'); 
                   5759:     }
                   5760:      
                   5761:     my %currsettings =
                   5762:         &Apache::lonnet::get('environment',['hidefromcat','categories','internal.coursecode'],
                   5763:                              $cdom,$cnum);
                   5764:     my $visible = 0;
                   5765:     if ($currsettings{'internal.coursecode'} ne '') {
                   5766:         if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5767:             $cathash = $domconf{'coursecategories'}{'cats'};
                   5768:             if (ref($cathash) eq 'HASH') {
                   5769:                 if ($cathash->{'instcode::0'} eq '') {
                   5770:                     push(@vismsgs,'dc_addinst'); 
                   5771:                 } else {
                   5772:                     $visible = 1;
                   5773:                 }
                   5774:             } else {
                   5775:                 $visible = 1;
                   5776:             }
                   5777:         } else {
                   5778:             $visible = 1;
                   5779:         }
                   5780:     } else {
                   5781:         if (ref($cathash) eq 'HASH') {
                   5782:             if ($cathash->{'instcode::0'} ne '') {
                   5783:                 push(@vismsgs,'dc_instcode');
                   5784:             }
                   5785:         } else {
                   5786:             push(@vismsgs,'dc_instcode');
                   5787:         }
                   5788:     }
                   5789:     if ($currsettings{'categories'} ne '') {
                   5790:         my $cathash;
                   5791:         if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5792:             $cathash = $domconf{'coursecategories'}{'cats'};
                   5793:             if (ref($cathash) eq 'HASH') {
                   5794:                 if (keys(%{$cathash}) == 0) {
                   5795:                     push(@vismsgs,'dc_catalog');
                   5796:                 } elsif ((keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} ne '')) {
                   5797:                     push(@vismsgs,'dc_categories');
                   5798:                 } else {
                   5799:                     my @currcategories = split('&',$currsettings{'categories'});
                   5800:                     my $matched = 0;
                   5801:                     foreach my $cat (@currcategories) {
                   5802:                         if ($cathash->{$cat} ne '') {
                   5803:                             $visible = 1;
                   5804:                             $matched = 1;
                   5805:                             last;
                   5806:                         }
                   5807:                     }
                   5808:                     if (!$matched) {
1.260     raeburn  5809:                         if ($settable{'categorize'}) { 
1.256     raeburn  5810:                             push(@vismsgs,'chgcat');
                   5811:                         } else {
                   5812:                             push(@vismsgs,'dc_chgcat');
                   5813:                         }
                   5814:                     }
                   5815:                 }
                   5816:             }
                   5817:         }
                   5818:     } else {
                   5819:         if (ref($cathash) eq 'HASH') {
                   5820:             if ((keys(%{$cathash}) > 1) || 
                   5821:                 (keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} eq '')) {
1.260     raeburn  5822:                 if ($settable{'categorize'}) {
1.256     raeburn  5823:                     push(@vismsgs,'addcat');
                   5824:                 } else {
                   5825:                     push(@vismsgs,'dc_addcat');
                   5826:                 }
                   5827:             }
                   5828:         }
                   5829:     }
                   5830:     if ($currsettings{'hidefromcat'} eq 'yes') {
                   5831:         $visible = 0;
                   5832:         if ($settable{'togglecats'}) {
                   5833:             unshift(@vismsgs,'unhide');
                   5834:         } else {
                   5835:             unshift(@vismsgs,'dc_unhide')
                   5836:         }
                   5837:     }
                   5838:     return ($visible,$cansetvis,\@vismsgs,\%visactions);
                   5839: }
                   5840: 
1.241     raeburn  5841: sub new_selfenroll_dom_row {
                   5842:     my ($newdom,$num) = @_;
                   5843:     my $domdesc = &Apache::lonnet::domain($newdom);
                   5844:     my $output;
                   5845:     if ($domdesc ne '') {
                   5846:         $output .= &Apache::loncommon::start_data_table_row()
                   5847:                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
                   5848:                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
1.249     raeburn  5849:                    .'" value="'.$newdom.'" /></span><br />'
                   5850:                    .'<span class="LC_nobreak"><label><input type="checkbox" '
                   5851:                    .'name="selfenroll_activate" value="'.$num.'" '
                   5852:                    .'onchange="javascript:update_types('
                   5853:                    ."'selfenroll_activate','$num'".');" />'
                   5854:                    .&mt('Activate').'</label></span></td>';
1.241     raeburn  5855:         my @currinsttypes;
                   5856:         $output .= '<td>'.&mt('User types:').'<br />'
                   5857:                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
                   5858:                    .&Apache::loncommon::end_data_table_row();
                   5859:     }
                   5860:     return $output;
                   5861: }
                   5862: 
                   5863: sub selfenroll_inst_types {
                   5864:     my ($num,$currdom,$currinsttypes) = @_;
                   5865:     my $output;
                   5866:     my $numinrow = 4;
                   5867:     my $count = 0;
                   5868:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($currdom);
1.247     raeburn  5869:     my $othervalue = 'any';
1.241     raeburn  5870:     if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
1.251     raeburn  5871:         if (keys(%{$usertypes}) > 0) {
1.247     raeburn  5872:             $othervalue = 'other';
                   5873:         }
1.241     raeburn  5874:         $output .= '<table><tr>';
                   5875:         foreach my $type (@{$types}) {
                   5876:             if (($count > 0) && ($count%$numinrow == 0)) {
                   5877:                 $output .= '</tr><tr>';
                   5878:             }
                   5879:             if (defined($usertypes->{$type})) {
1.257     raeburn  5880:                 my $esc_type = &escape($type);
1.241     raeburn  5881:                 $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.
1.257     raeburn  5882:                            $esc_type.'" ';
1.241     raeburn  5883:                 if (ref($currinsttypes) eq 'ARRAY') {
                   5884:                     if (@{$currinsttypes} > 0) {
1.249     raeburn  5885:                         if (grep(/^any$/,@{$currinsttypes})) {
                   5886:                             $output .= 'checked="checked"';
1.257     raeburn  5887:                         } elsif (grep(/^\Q$esc_type\E$/,@{$currinsttypes})) {
1.241     raeburn  5888:                             $output .= 'checked="checked"';
                   5889:                         }
1.249     raeburn  5890:                     } else {
                   5891:                         $output .= 'checked="checked"';
1.241     raeburn  5892:                     }
                   5893:                 }
                   5894:                 $output .= ' name="selfenroll_types_'.$num.'" />'.$usertypes->{$type}.'</label></span></td>';
                   5895:             }
                   5896:             $count ++;
                   5897:         }
                   5898:         if (($count > 0) && ($count%$numinrow == 0)) {
                   5899:             $output .= '</tr><tr>';
                   5900:         }
1.249     raeburn  5901:         $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
1.241     raeburn  5902:         if (ref($currinsttypes) eq 'ARRAY') {
                   5903:             if (@{$currinsttypes} > 0) {
1.249     raeburn  5904:                 if (grep(/^any$/,@{$currinsttypes})) { 
                   5905:                     $output .= ' checked="checked"';
                   5906:                 } elsif ($othervalue eq 'other') {
                   5907:                     if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
                   5908:                         $output .= ' checked="checked"';
                   5909:                     }
1.241     raeburn  5910:                 }
1.249     raeburn  5911:             } else {
                   5912:                 $output .= ' checked="checked"';
1.241     raeburn  5913:             }
1.249     raeburn  5914:         } else {
                   5915:             $output .= ' checked="checked"';
1.241     raeburn  5916:         }
                   5917:         $output .= ' name="selfenroll_types_'.$num.'" />'.$othertitle.'</label></span></td></tr></table>';
                   5918:     }
                   5919:     return $output;
                   5920: }
                   5921: 
1.237     raeburn  5922: sub selfenroll_date_forms {
                   5923:     my ($startform,$endform) = @_;
                   5924:     my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
1.244     bisitz   5925:                   &Apache::lonhtmlcommon::row_title(&mt('Start date'),
1.237     raeburn  5926:                                                     'LC_oddrow_value')."\n".
                   5927:                   $startform."\n".
                   5928:                   &Apache::lonhtmlcommon::row_closure(1).
1.244     bisitz   5929:                   &Apache::lonhtmlcommon::row_title(&mt('End date'),
1.237     raeburn  5930:                                                    'LC_oddrow_value')."\n".
                   5931:                   $endform."\n".
                   5932:                   &Apache::lonhtmlcommon::row_closure(1).
                   5933:                   &Apache::lonhtmlcommon::end_pick_box();
                   5934:     return $output;
                   5935: }
                   5936: 
1.239     raeburn  5937: sub print_userchangelogs_display {
                   5938:     my ($r,$context,$permission) = @_;
1.363     raeburn  5939:     my $formname = 'rolelog';
                   5940:     my ($username,$domain,$crstype,%roleslog);
                   5941:     if ($context eq 'domain') {
                   5942:         $domain = $env{'request.role.domain'};
                   5943:         %roleslog=&Apache::lonnet::dump_dom('nohist_rolelog',$domain);
                   5944:     } else {
                   5945:         if ($context eq 'course') { 
                   5946:             $domain = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5947:             $username = $env{'course.'.$env{'request.course.id'}.'.num'};
                   5948:             $crstype = &Apache::loncommon::course_type();
                   5949:             my %saveable_parameters = ('show' => 'scalar',);
                   5950:             &Apache::loncommon::store_course_settings('roles_log',
                   5951:                                                       \%saveable_parameters);
                   5952:             &Apache::loncommon::restore_course_settings('roles_log',
                   5953:                                                         \%saveable_parameters);
                   5954:         } elsif ($context eq 'author') {
                   5955:             $domain = $env{'user.domain'}; 
                   5956:             if ($env{'request.role'} =~ m{^au\./\Q$domain\E/$}) {
                   5957:                 $username = $env{'user.name'};
                   5958:             } else {
                   5959:                 undef($domain);
                   5960:             }
                   5961:         }
                   5962:         if ($domain ne '' && $username ne '') { 
                   5963:             %roleslog=&Apache::lonnet::dump('nohist_rolelog',$domain,$username);
                   5964:         }
                   5965:     }
1.239     raeburn  5966:     if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
                   5967: 
                   5968:     # set defaults
                   5969:     my $now = time();
                   5970:     my $defstart = $now - (7*24*3600); #7 days ago 
                   5971:     my %defaults = (
                   5972:                      page               => '1',
                   5973:                      show               => '10',
                   5974:                      role               => 'any',
                   5975:                      chgcontext         => 'any',
                   5976:                      rolelog_start_date => $defstart,
                   5977:                      rolelog_end_date   => $now,
                   5978:                    );
                   5979:     my $more_records = 0;
                   5980: 
                   5981:     # set current
                   5982:     my %curr;
                   5983:     foreach my $item ('show','page','role','chgcontext') {
                   5984:         $curr{$item} = $env{'form.'.$item};
                   5985:     }
                   5986:     my ($startdate,$enddate) = 
                   5987:         &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
                   5988:     $curr{'rolelog_start_date'} = $startdate;
                   5989:     $curr{'rolelog_end_date'} = $enddate;
                   5990:     foreach my $key (keys(%defaults)) {
                   5991:         if ($curr{$key} eq '') {
                   5992:             $curr{$key} = $defaults{$key};
                   5993:         }
                   5994:     }
1.248     raeburn  5995:     my (%whodunit,%changed,$version);
                   5996:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
1.239     raeburn  5997:     my ($minshown,$maxshown);
1.255     raeburn  5998:     $minshown = 1;
1.239     raeburn  5999:     my $count = 0;
                   6000:     if ($curr{'show'} ne &mt('all')) { 
                   6001:         $maxshown = $curr{'page'} * $curr{'show'};
                   6002:         if ($curr{'page'} > 1) {
                   6003:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   6004:         }
                   6005:     }
1.301     bisitz   6006: 
1.327     raeburn  6007:     # Form Header
                   6008:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
1.363     raeburn  6009:               &role_display_filter($context,$formname,$domain,$username,\%curr,
                   6010:                                    $version,$crstype));
1.327     raeburn  6011: 
                   6012:     # Create navigation
                   6013:     my ($nav_script,$nav_links) = &userlogdisplay_nav($formname,\%curr,$more_records);
                   6014:     my $showntableheader = 0;
                   6015: 
                   6016:     # Table Header
                   6017:     my $tableheader = 
                   6018:         &Apache::loncommon::start_data_table_header_row()
                   6019:        .'<th>&nbsp;</th>'
                   6020:        .'<th>'.&mt('When').'</th>'
                   6021:        .'<th>'.&mt('Who made the change').'</th>'
                   6022:        .'<th>'.&mt('Changed User').'</th>'
1.363     raeburn  6023:        .'<th>'.&mt('Role').'</th>';
                   6024: 
                   6025:     if ($context eq 'course') {
                   6026:         $tableheader .= '<th>'.&mt('Section').'</th>';
                   6027:     }
                   6028:     $tableheader .=
                   6029:         '<th>'.&mt('Context').'</th>'
1.327     raeburn  6030:        .'<th>'.&mt('Start').'</th>'
                   6031:        .'<th>'.&mt('End').'</th>'
                   6032:        .&Apache::loncommon::end_data_table_header_row();
                   6033: 
                   6034:     # Display user change log data
1.239     raeburn  6035:     foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
                   6036:         next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
                   6037:                  ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
                   6038:         if ($curr{'show'} ne &mt('all')) {
                   6039:             if ($count >= $curr{'page'} * $curr{'show'}) {
                   6040:                 $more_records = 1;
                   6041:                 last;
                   6042:             }
                   6043:         }
                   6044:         if ($curr{'role'} ne 'any') {
                   6045:             next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
                   6046:         }
                   6047:         if ($curr{'chgcontext'} ne 'any') {
                   6048:             if ($curr{'chgcontext'} eq 'selfenroll') {
                   6049:                 next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
                   6050:             } else {
                   6051:                 next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
                   6052:             }
                   6053:         }
                   6054:         $count ++;
                   6055:         next if ($count < $minshown);
1.327     raeburn  6056:         unless ($showntableheader) {
                   6057:             $r->print($nav_script
                   6058:                      .$nav_links
                   6059:                      .&Apache::loncommon::start_data_table()
                   6060:                      .$tableheader);
                   6061:             $r->rflush();
                   6062:             $showntableheader = 1;
                   6063:         }
1.239     raeburn  6064:         if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
                   6065:             $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
                   6066:                 &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
                   6067:         }
                   6068:         if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
                   6069:             $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
                   6070:                 &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
                   6071:         }
                   6072:         my $sec = $roleslog{$id}{'logentry'}{'section'};
                   6073:         if ($sec eq '') {
                   6074:             $sec = &mt('None');
                   6075:         }
                   6076:         my ($rolestart,$roleend);
                   6077:         if ($roleslog{$id}{'delflag'}) {
                   6078:             $rolestart = &mt('deleted');
                   6079:             $roleend = &mt('deleted');
                   6080:         } else {
                   6081:             $rolestart = $roleslog{$id}{'logentry'}{'start'};
                   6082:             $roleend = $roleslog{$id}{'logentry'}{'end'};
                   6083:             if ($rolestart eq '' || $rolestart == 0) {
                   6084:                 $rolestart = &mt('No start date'); 
                   6085:             } else {
                   6086:                 $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
                   6087:             }
                   6088:             if ($roleend eq '' || $roleend == 0) { 
                   6089:                 $roleend = &mt('No end date');
                   6090:             } else {
                   6091:                 $roleend = &Apache::lonlocal::locallocaltime($roleend);
                   6092:             }
                   6093:         }
                   6094:         my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
                   6095:         if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
                   6096:             $chgcontext = 'selfenroll';
                   6097:         }
1.363     raeburn  6098:         my %lt = &rolechg_contexts($context,$crstype);
1.239     raeburn  6099:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
                   6100:             $chgcontext = $lt{$chgcontext};
                   6101:         }
1.327     raeburn  6102:         $r->print(
1.301     bisitz   6103:             &Apache::loncommon::start_data_table_row()
                   6104:            .'<td>'.$count.'</td>'
                   6105:            .'<td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td>'
                   6106:            .'<td>'.$whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.'</td>'
                   6107:            .'<td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td>'
1.363     raeburn  6108:            .'<td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'},$crstype).'</td>');
                   6109:         if ($context eq 'course') { 
                   6110:             $r->print('<td>'.$sec.'</td>');
                   6111:         }
                   6112:         $r->print(
                   6113:             '<td>'.$chgcontext.'</td>'
1.301     bisitz   6114:            .'<td>'.$rolestart.'</td>'
                   6115:            .'<td>'.$roleend.'</td>'
1.327     raeburn  6116:            .&Apache::loncommon::end_data_table_row()."\n");
1.301     bisitz   6117:     }
                   6118: 
1.327     raeburn  6119:     if ($showntableheader) { # Table footer, if content displayed above
                   6120:         $r->print(&Apache::loncommon::end_data_table()
                   6121:                  .$nav_links);
                   6122:     } else { # No content displayed above
1.301     bisitz   6123:         $r->print('<p class="LC_info">'
                   6124:                  .&mt('There are no records to display.')
                   6125:                  .'</p>'
                   6126:         );
1.239     raeburn  6127:     }
1.301     bisitz   6128: 
1.327     raeburn  6129:     # Form Footer
                   6130:     $r->print( 
                   6131:         '<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   6132:        .'<input type="hidden" name="action" value="changelogs" />'
                   6133:        .'</form>');
                   6134:     return;
                   6135: }
1.301     bisitz   6136: 
1.327     raeburn  6137: sub userlogdisplay_nav {
                   6138:     my ($formname,$curr,$more_records) = @_;
                   6139:     my ($nav_script,$nav_links);
                   6140:     if (ref($curr) eq 'HASH') {
                   6141:         # Create Navigation:
                   6142:         # Navigation Script
                   6143:         $nav_script = <<"ENDSCRIPT";
1.239     raeburn  6144: <script type="text/javascript">
1.301     bisitz   6145: // <![CDATA[
1.239     raeburn  6146: function chgPage(caller) {
                   6147:     if (caller == 'previous') {
                   6148:         document.$formname.page.value --;
                   6149:     }
                   6150:     if (caller == 'next') {
                   6151:         document.$formname.page.value ++;
                   6152:     }
1.327     raeburn  6153:     document.$formname.submit();
1.239     raeburn  6154:     return;
                   6155: }
1.301     bisitz   6156: // ]]>
1.239     raeburn  6157: </script>
                   6158: ENDSCRIPT
1.327     raeburn  6159:         # Navigation Buttons
                   6160:         $nav_links = '<p>';
                   6161:         if (($curr->{'page'} > 1) || ($more_records)) {
                   6162:             if ($curr->{'page'} > 1) {
                   6163:                 $nav_links .= '<input type="button"'
                   6164:                              .' onclick="javascript:chgPage('."'previous'".');"'
                   6165:                              .' value="'.&mt('Previous [_1] changes',$curr->{'show'})
                   6166:                              .'" /> ';
                   6167:             }
                   6168:             if ($more_records) {
                   6169:                 $nav_links .= '<input type="button"'
                   6170:                              .' onclick="javascript:chgPage('."'next'".');"'
                   6171:                              .' value="'.&mt('Next [_1] changes',$curr->{'show'})
                   6172:                              .'" />';
                   6173:             }
1.301     bisitz   6174:         }
1.327     raeburn  6175:         $nav_links .= '</p>';
1.301     bisitz   6176:     }
1.327     raeburn  6177:     return ($nav_script,$nav_links);
1.239     raeburn  6178: }
                   6179: 
                   6180: sub role_display_filter {
1.363     raeburn  6181:     my ($context,$formname,$cdom,$cnum,$curr,$version,$crstype) = @_;
                   6182:     my $lctype;
                   6183:     if ($context eq 'course') {
                   6184:         $lctype = lc($crstype);
                   6185:     }
1.239     raeburn  6186:     my $nolink = 1;
                   6187:     my $output = '<table><tr><td valign="top">'.
1.301     bisitz   6188:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b></span><br />'.
1.239     raeburn  6189:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   6190:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   6191:                  '</td><td>&nbsp;&nbsp;</td>';
                   6192:     my $startform =
                   6193:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
                   6194:                                             $curr->{'rolelog_start_date'},undef,
                   6195:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   6196:     my $endform =
                   6197:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
                   6198:                                             $curr->{'rolelog_end_date'},undef,
                   6199:                                             undef,undef,undef,undef,undef,undef,$nolink);
1.363     raeburn  6200:     my %lt = &rolechg_contexts($context,$crstype);
1.301     bisitz   6201:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br />'.
                   6202:                '<table><tr><td>'.&mt('After:').
                   6203:                '</td><td>'.$startform.'</td></tr>'.
                   6204:                '<tr><td>'.&mt('Before:').'</td>'.
                   6205:                '<td>'.$endform.'</td></tr></table>'.
                   6206:                '</td>'.
                   6207:                '<td>&nbsp;&nbsp;</td>'.
1.239     raeburn  6208:                '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
                   6209:                '<select name="role"><option value="any"';
                   6210:     if ($curr->{'role'} eq 'any') {
                   6211:         $output .= ' selected="selected"';
                   6212:     }
                   6213:     $output .=  '>'.&mt('Any').'</option>'."\n";
1.363     raeburn  6214:     my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
1.239     raeburn  6215:     foreach my $role (@roles) {
                   6216:         my $plrole;
                   6217:         if ($role eq 'cr') {
                   6218:             $plrole = &mt('Custom Role');
                   6219:         } else {
1.318     raeburn  6220:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.239     raeburn  6221:         }
                   6222:         my $selstr = '';
                   6223:         if ($role eq $curr->{'role'}) {
                   6224:             $selstr = ' selected="selected"';
                   6225:         }
                   6226:         $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
                   6227:     }
1.301     bisitz   6228:     $output .= '</select></td>'.
                   6229:                '<td>&nbsp;&nbsp;</td>'.
                   6230:                '<td valign="top"><b>'.
1.239     raeburn  6231:                &mt('Context:').'</b><br /><select name="chgcontext">';
1.363     raeburn  6232:     my @posscontexts;
                   6233:     if ($context eq 'course') {
1.376     raeburn  6234:         @posscontexts = ('any','automated','updatenow','createcourse','course','domain','selfenroll','requestcourses');
1.363     raeburn  6235:     } elsif ($context eq 'domain') {
                   6236:         @posscontexts = ('any','domain','requestauthor','domconfig','server');
                   6237:     } else {
                   6238:         @posscontexts = ('any','author','domain');
                   6239:     } 
                   6240:     foreach my $chgtype (@posscontexts) {
1.239     raeburn  6241:         my $selstr = '';
                   6242:         if ($curr->{'chgcontext'} eq $chgtype) {
1.301     bisitz   6243:             $selstr = ' selected="selected"';
1.239     raeburn  6244:         }
1.363     raeburn  6245:         if ($context eq 'course') {
1.376     raeburn  6246:             if (($chgtype eq 'automated') || ($chgtype eq 'updatenow')) {
1.363     raeburn  6247:                 next if (!&Apache::lonnet::auto_run($cnum,$cdom));
                   6248:             }
1.239     raeburn  6249:         }
                   6250:         $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
1.248     raeburn  6251:     }
1.303     bisitz   6252:     $output .= '</select></td>'
                   6253:               .'</tr></table>';
                   6254: 
                   6255:     # Update Display button
                   6256:     $output .= '<p>'
                   6257:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   6258:               .'</p>';
                   6259: 
                   6260:     # Server version info
1.363     raeburn  6261:     my $needsrev = '2.11.0';
                   6262:     if ($context eq 'course') {
                   6263:         $needsrev = '2.7.0';
                   6264:     }
                   6265:     
1.303     bisitz   6266:     $output .= '<p class="LC_info">'
                   6267:               .&mt('Only changes made from servers running LON-CAPA [_1] or later are displayed.'
1.363     raeburn  6268:                   ,$needsrev);
1.248     raeburn  6269:     if ($version) {
1.303     bisitz   6270:         $output .= ' '.&mt('This LON-CAPA server is version [_1]',$version);
                   6271:     }
                   6272:     $output .= '</p><hr />';
1.239     raeburn  6273:     return $output;
                   6274: }
                   6275: 
                   6276: sub rolechg_contexts {
1.363     raeburn  6277:     my ($context,$crstype) = @_;
                   6278:     my %lt;
                   6279:     if ($context eq 'course') {
                   6280:         %lt = &Apache::lonlocal::texthash (
1.239     raeburn  6281:                                              any          => 'Any',
1.376     raeburn  6282:                                              automated    => 'Automated Enrollment',
1.239     raeburn  6283:                                              updatenow    => 'Roster Update',
                   6284:                                              createcourse => 'Course Creation',
                   6285:                                              course       => 'User Management in course',
                   6286:                                              domain       => 'User Management in domain',
1.313     raeburn  6287:                                              selfenroll   => 'Self-enrolled',
1.318     raeburn  6288:                                              requestcourses => 'Course Request',
1.239     raeburn  6289:                                          );
1.363     raeburn  6290:         if ($crstype eq 'Community') {
                   6291:             $lt{'createcourse'} = &mt('Community Creation');
                   6292:             $lt{'course'} = &mt('User Management in community');
                   6293:             $lt{'requestcourses'} = &mt('Community Request');
                   6294:         }
                   6295:     } elsif ($context eq 'domain') {
                   6296:         %lt = &Apache::lonlocal::texthash (
                   6297:                                              any           => 'Any',
                   6298:                                              domain        => 'User Management in domain',
                   6299:                                              requestauthor => 'Authoring Request',
                   6300:                                              server        => 'Command line script (DC role)',
                   6301:                                              domconfig     => 'Self-enrolled',
                   6302:                                          );
                   6303:     } else {
                   6304:         %lt = &Apache::lonlocal::texthash (
                   6305:                                              any    => 'Any',
                   6306:                                              domain => 'User Management in domain',
                   6307:                                              author => 'User Management by author',
                   6308:                                          );
                   6309:     } 
1.239     raeburn  6310:     return %lt;
                   6311: }
                   6312: 
1.27      matthew  6313: #-------------------------------------------------- functions for &phase_two
1.160     raeburn  6314: sub user_search_result {
1.221     raeburn  6315:     my ($context,$srch) = @_;
1.160     raeburn  6316:     my %allhomes;
                   6317:     my %inst_matches;
                   6318:     my %srch_results;
1.181     raeburn  6319:     my ($response,$currstate,$forcenewuser,$dirsrchres);
1.183     raeburn  6320:     $srch->{'srchterm'} =~ s/\s+/ /g;
1.176     raeburn  6321:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
1.160     raeburn  6322:         $response = &mt('Invalid search.');
                   6323:     }
                   6324:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
                   6325:         $response = &mt('Invalid search.');
                   6326:     }
1.177     raeburn  6327:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
1.160     raeburn  6328:         $response = &mt('Invalid search.');
                   6329:     }
                   6330:     if ($srch->{'srchterm'} eq '') {
                   6331:         $response = &mt('You must enter a search term.');
                   6332:     }
1.183     raeburn  6333:     if ($srch->{'srchterm'} =~ /^\s+$/) {
                   6334:         $response = &mt('Your search term must contain more than just spaces.');
                   6335:     }
1.160     raeburn  6336:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
                   6337:         if (($srch->{'srchdomain'} eq '') || 
1.163     albertel 6338: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
1.160     raeburn  6339:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
                   6340:         }
                   6341:     }
                   6342:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
                   6343:         ($srch->{'srchin'} eq 'alc')) {
1.176     raeburn  6344:         if ($srch->{'srchby'} eq 'uname') {
1.243     raeburn  6345:             my $unamecheck = $srch->{'srchterm'};
                   6346:             if ($srch->{'srchtype'} eq 'contains') {
                   6347:                 if ($unamecheck !~ /^\w/) {
                   6348:                     $unamecheck = 'a'.$unamecheck; 
                   6349:                 }
                   6350:             }
                   6351:             if ($unamecheck !~ /^$match_username$/) {
1.176     raeburn  6352:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
                   6353:             }
1.160     raeburn  6354:         }
                   6355:     }
1.180     raeburn  6356:     if ($response ne '') {
                   6357:         $response = '<span class="LC_warning">'.$response.'</span>';
                   6358:     }
1.160     raeburn  6359:     if ($srch->{'srchin'} eq 'instd') {
                   6360:         my $instd_chk = &directorysrch_check($srch);
                   6361:         if ($instd_chk ne 'ok') {
1.180     raeburn  6362:             $response = '<span class="LC_warning">'.$instd_chk.'</span>'.
                   6363:                         '<br />'.&mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').'<br /><br />';
1.160     raeburn  6364:         }
                   6365:     }
                   6366:     if ($response ne '') {
1.180     raeburn  6367:         return ($currstate,$response);
1.160     raeburn  6368:     }
                   6369:     if ($srch->{'srchby'} eq 'uname') {
                   6370:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
                   6371:             if ($env{'form.forcenew'}) {
                   6372:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
                   6373:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   6374:                     if ($uhome eq 'no_host') {
                   6375:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.180     raeburn  6376:                         my $showdom = &display_domain_info($env{'request.role.domain'});
                   6377:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
1.160     raeburn  6378:                     } else {
1.179     raeburn  6379:                         $currstate = 'modify';
1.160     raeburn  6380:                     }
                   6381:                 } else {
1.179     raeburn  6382:                     $currstate = 'modify';
1.160     raeburn  6383:                 }
                   6384:             } else {
                   6385:                 if ($srch->{'srchin'} eq 'dom') {
1.162     raeburn  6386:                     if ($srch->{'srchtype'} eq 'exact') {
                   6387:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   6388:                         if ($uhome eq 'no_host') {
1.179     raeburn  6389:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  6390:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  6391:                         } else {
1.179     raeburn  6392:                             $currstate = 'modify';
1.310     raeburn  6393:                             my $uname = $srch->{'srchterm'};
                   6394:                             my $udom = $srch->{'srchdomain'};
                   6395:                             $srch_results{$uname.':'.$udom} =
                   6396:                                 { &Apache::lonnet::get('environment',
                   6397:                                                        ['firstname',
                   6398:                                                         'lastname',
                   6399:                                                         'permanentemail'],
                   6400:                                                          $udom,$uname)
                   6401:                                 };
1.162     raeburn  6402:                         }
                   6403:                     } else {
                   6404:                         %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  6405:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  6406:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  6407:                     }
                   6408:                 } else {
1.167     albertel 6409:                     my $courseusers = &get_courseusers();
1.162     raeburn  6410:                     if ($srch->{'srchtype'} eq 'exact') {
1.167     albertel 6411:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
1.179     raeburn  6412:                             $currstate = 'modify';
1.162     raeburn  6413:                         } else {
1.179     raeburn  6414:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  6415:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  6416:                         }
1.160     raeburn  6417:                     } else {
1.167     albertel 6418:                         foreach my $user (keys(%$courseusers)) {
1.162     raeburn  6419:                             my ($cuname,$cudomain) = split(/:/,$user);
                   6420:                             if ($cudomain eq $srch->{'srchdomain'}) {
1.177     raeburn  6421:                                 my $matched = 0;
                   6422:                                 if ($srch->{'srchtype'} eq 'begins') {
                   6423:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
                   6424:                                         $matched = 1;
                   6425:                                     }
                   6426:                                 } else {
                   6427:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
                   6428:                                         $matched = 1;
                   6429:                                     }
                   6430:                                 }
                   6431:                                 if ($matched) {
1.167     albertel 6432:                                     $srch_results{$user} = 
                   6433: 					{&Apache::lonnet::get('environment',
                   6434: 							     ['firstname',
                   6435: 							      'lastname',
1.194     albertel 6436: 							      'permanentemail'],
                   6437: 							      $cudomain,$cuname)};
1.162     raeburn  6438:                                 }
                   6439:                             }
                   6440:                         }
1.179     raeburn  6441:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  6442:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  6443:                     }
                   6444:                 }
                   6445:             }
                   6446:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  6447:             $currstate = 'query';
1.160     raeburn  6448:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  6449:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
                   6450:             if ($dirsrchres eq 'ok') {
                   6451:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  6452:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  6453:             } else {
                   6454:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   6455:                 $response = '<span class="LC_warning">'.
                   6456:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   6457:                     '</span><br />'.
                   6458:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
                   6459:                     '<br /><br />'; 
                   6460:             }
1.160     raeburn  6461:         }
                   6462:     } else {
                   6463:         if ($srch->{'srchin'} eq 'dom') {
                   6464:             %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  6465:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  6466:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  6467:         } elsif ($srch->{'srchin'} eq 'crs') {
1.167     albertel 6468:             my $courseusers = &get_courseusers(); 
                   6469:             foreach my $user (keys(%$courseusers)) {
1.160     raeburn  6470:                 my ($uname,$udom) = split(/:/,$user);
                   6471:                 my %names = &Apache::loncommon::getnames($uname,$udom);
                   6472:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
                   6473:                 if ($srch->{'srchby'} eq 'lastname') {
                   6474:                     if ((($srch->{'srchtype'} eq 'exact') && 
                   6475:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
1.177     raeburn  6476:                         (($srch->{'srchtype'} eq 'begins') &&
                   6477:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
1.160     raeburn  6478:                         (($srch->{'srchtype'} eq 'contains') &&
                   6479:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
                   6480:                         $srch_results{$user} = {firstname => $names{'firstname'},
                   6481:                                             lastname => $names{'lastname'},
                   6482:                                             permanentemail => $emails{'permanentemail'},
                   6483:                                            };
                   6484:                     }
                   6485:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
                   6486:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
1.177     raeburn  6487:                     $srchlast =~ s/\s+$//;
                   6488:                     $srchfirst =~ s/^\s+//;
1.160     raeburn  6489:                     if ($srch->{'srchtype'} eq 'exact') {
                   6490:                         if (($names{'lastname'} eq $srchlast) &&
                   6491:                             ($names{'firstname'} eq $srchfirst)) {
                   6492:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   6493:                                                 lastname => $names{'lastname'},
                   6494:                                                 permanentemail => $emails{'permanentemail'},
                   6495: 
                   6496:                                            };
                   6497:                         }
1.177     raeburn  6498:                     } elsif ($srch->{'srchtype'} eq 'begins') {
                   6499:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
                   6500:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
                   6501:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   6502:                                                 lastname => $names{'lastname'},
                   6503:                                                 permanentemail => $emails{'permanentemail'},
                   6504:                                                };
                   6505:                         }
                   6506:                     } else {
1.160     raeburn  6507:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
                   6508:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
                   6509:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   6510:                                                 lastname => $names{'lastname'},
                   6511:                                                 permanentemail => $emails{'permanentemail'},
                   6512:                                                };
                   6513:                         }
                   6514:                     }
                   6515:                 }
                   6516:             }
1.179     raeburn  6517:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  6518:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  6519:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  6520:             $currstate = 'query';
1.160     raeburn  6521:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  6522:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
                   6523:             if ($dirsrchres eq 'ok') {
                   6524:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  6525:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  6526:             } else {
                   6527:                 my $showdom = &display_domain_info($srch->{'srchdomain'});                $response = '<span class="LC_warning">'.
                   6528:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   6529:                     '</span><br />'.
                   6530:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
                   6531:                     '<br /><br />';
                   6532:             }
1.160     raeburn  6533:         }
                   6534:     }
1.179     raeburn  6535:     return ($currstate,$response,$forcenewuser,\%srch_results);
1.160     raeburn  6536: }
                   6537: 
                   6538: sub directorysrch_check {
                   6539:     my ($srch) = @_;
                   6540:     my $can_search = 0;
                   6541:     my $response;
                   6542:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   6543:                                              ['directorysrch'],$srch->{'srchdomain'});
1.180     raeburn  6544:     my $showdom = &display_domain_info($srch->{'srchdomain'});
1.160     raeburn  6545:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   6546:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
1.180     raeburn  6547:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
1.160     raeburn  6548:         }
                   6549:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
                   6550:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
1.180     raeburn  6551:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
1.160     raeburn  6552:             }
                   6553:             my @usertypes = split(/:/,$env{'environment.inststatus'});
                   6554:             if (!@usertypes) {
                   6555:                 push(@usertypes,'default');
                   6556:             }
                   6557:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
                   6558:                 foreach my $type (@usertypes) {
                   6559:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
                   6560:                         $can_search = 1;
                   6561:                         last;
                   6562:                     }
                   6563:                 }
                   6564:             }
                   6565:             if (!$can_search) {
                   6566:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
                   6567:                 my @longtypes; 
                   6568:                 foreach my $item (@usertypes) {
1.229     raeburn  6569:                     if (defined($insttypes->{$item})) { 
                   6570:                         push (@longtypes,$insttypes->{$item});
                   6571:                     } elsif ($item eq 'default') {
                   6572:                         push (@longtypes,&mt('other')); 
                   6573:                     }
1.160     raeburn  6574:                 }
                   6575:                 my $insttype_str = join(', ',@longtypes); 
1.180     raeburn  6576:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
1.229     raeburn  6577:             }
1.160     raeburn  6578:         } else {
                   6579:             $can_search = 1;
                   6580:         }
                   6581:     } else {
1.180     raeburn  6582:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
1.160     raeburn  6583:     }
                   6584:     my %longtext = &Apache::lonlocal::texthash (
1.167     albertel 6585:                        uname     => 'username',
1.160     raeburn  6586:                        lastfirst => 'last name, first name',
1.167     albertel 6587:                        lastname  => 'last name',
1.172     raeburn  6588:                        contains  => 'contains',
1.178     raeburn  6589:                        exact     => 'as exact match to',
                   6590:                        begins    => 'begins with',
1.160     raeburn  6591:                    );
                   6592:     if ($can_search) {
                   6593:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
                   6594:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
1.180     raeburn  6595:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
1.160     raeburn  6596:             }
                   6597:         } else {
1.180     raeburn  6598:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
1.160     raeburn  6599:         }
                   6600:     }
                   6601:     if ($can_search) {
1.178     raeburn  6602:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
                   6603:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
                   6604:                 return 'ok';
                   6605:             } else {
1.180     raeburn  6606:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  6607:             }
                   6608:         } else {
                   6609:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
                   6610:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
                   6611:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
                   6612:                 return 'ok';
                   6613:             } else {
1.180     raeburn  6614:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  6615:             }
1.160     raeburn  6616:         }
                   6617:     }
                   6618: }
                   6619: 
                   6620: sub get_courseusers {
                   6621:     my %advhash;
1.167     albertel 6622:     my $classlist = &Apache::loncoursedata::get_classlist();
1.160     raeburn  6623:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                   6624:     foreach my $role (sort(keys(%coursepersonnel))) {
                   6625:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
1.167     albertel 6626: 	    if (!exists($classlist->{$user})) {
                   6627: 		$classlist->{$user} = [];
                   6628: 	    }
1.160     raeburn  6629:         }
                   6630:     }
1.167     albertel 6631:     return $classlist;
1.160     raeburn  6632: }
                   6633: 
                   6634: sub build_search_response {
1.221     raeburn  6635:     my ($context,$srch,%srch_results) = @_;
1.179     raeburn  6636:     my ($currstate,$response,$forcenewuser);
1.160     raeburn  6637:     my %names = (
1.330     bisitz   6638:           'uname'     => 'username',
                   6639:           'lastname'  => 'last name',
1.160     raeburn  6640:           'lastfirst' => 'last name, first name',
1.330     bisitz   6641:           'crs'       => 'this course',
                   6642:           'dom'       => 'LON-CAPA domain',
                   6643:           'instd'     => 'the institutional directory for domain',
1.160     raeburn  6644:     );
                   6645: 
                   6646:     my %single = (
1.180     raeburn  6647:                    begins   => 'A match',
1.160     raeburn  6648:                    contains => 'A match',
1.180     raeburn  6649:                    exact    => 'An exact match',
1.160     raeburn  6650:                  );
                   6651:     my %nomatch = (
1.180     raeburn  6652:                    begins   => 'No match',
1.160     raeburn  6653:                    contains => 'No match',
1.180     raeburn  6654:                    exact    => 'No exact match',
1.160     raeburn  6655:                   );
                   6656:     if (keys(%srch_results) > 1) {
1.179     raeburn  6657:         $currstate = 'select';
1.160     raeburn  6658:     } else {
                   6659:         if (keys(%srch_results) == 1) {
1.179     raeburn  6660:             $currstate = 'modify';
1.180     raeburn  6661:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
                   6662:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   6663:                 $response .= ': '.&display_domain_info($srch->{'srchdomain'});
1.180     raeburn  6664:             }
1.330     bisitz   6665:         } else { # Search has nothing found. Prepare message to user.
                   6666:             $response = '<span class="LC_warning">';
1.180     raeburn  6667:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   6668:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}: [_2]",
                   6669:                                  '<b>'.$srch->{'srchterm'}.'</b>',
                   6670:                                  &display_domain_info($srch->{'srchdomain'}));
                   6671:             } else {
                   6672:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}.",
                   6673:                                  '<b>'.$srch->{'srchterm'}.'</b>');
1.180     raeburn  6674:             }
                   6675:             $response .= '</span>';
1.330     bisitz   6676: 
1.160     raeburn  6677:             if ($srch->{'srchin'} ne 'alc') {
                   6678:                 $forcenewuser = 1;
                   6679:                 my $cansrchinst = 0; 
                   6680:                 if ($srch->{'srchdomain'}) {
                   6681:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
                   6682:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
                   6683:                         if ($domconfig{'directorysrch'}{'available'}) {
                   6684:                             $cansrchinst = 1;
                   6685:                         } 
                   6686:                     }
                   6687:                 }
1.180     raeburn  6688:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
                   6689:                      ($srch->{'srchby'} eq 'lastname')) &&
                   6690:                     ($srch->{'srchin'} eq 'dom')) {
                   6691:                     if ($cansrchinst) {
                   6692:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
1.160     raeburn  6693:                     }
                   6694:                 }
1.180     raeburn  6695:                 if ($srch->{'srchin'} eq 'crs') {
                   6696:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
                   6697:                 }
                   6698:             }
1.305     raeburn  6699:             my $createdom = $env{'request.role.domain'};
                   6700:             if ($context eq 'requestcrs') {
                   6701:                 if ($env{'form.coursedom'} ne '') {
                   6702:                     $createdom = $env{'form.coursedom'};
                   6703:                 }
                   6704:             }
                   6705:             if (!($srch->{'srchby'} eq 'uname' && $srch->{'srchin'} eq 'dom' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchdomain'} eq $createdom)) {
1.221     raeburn  6706:                 my $cancreate =
1.305     raeburn  6707:                     &Apache::lonuserutils::can_create_user($createdom,$context);
                   6708:                 my $targetdom = '<span class="LC_cusr_emph">'.$createdom.'</span>';
1.221     raeburn  6709:                 if ($cancreate) {
1.305     raeburn  6710:                     my $showdom = &display_domain_info($createdom); 
1.266     bisitz   6711:                     $response .= '<br /><br />'
                   6712:                                 .'<b>'.&mt('To add a new user:').'</b>'
1.305     raeburn  6713:                                 .'<br />';
                   6714:                     if ($context eq 'requestcrs') {
                   6715:                         $response .= &mt("(You can only define new users in the new course's domain - [_1])",$targetdom);
                   6716:                     } else {
                   6717:                         $response .= &mt("(You can only create new users in your current role's domain - [_1])",$targetdom);
                   6718:                     }
                   6719:                     $response .='<ul><li>'
1.266     bisitz   6720:                                 .&mt("Set 'Domain/institution to search' to: [_1]",'<span class="LC_cusr_emph">'.$showdom.'</span>')
                   6721:                                 .'</li><li>'
                   6722:                                 .&mt("Set 'Search criteria' to: [_1]username is ..... in selected LON-CAPA domain[_2]",'<span class="LC_cusr_emph">','</span>')
                   6723:                                 .'</li><li>'
                   6724:                                 .&mt('Provide the proposed username')
                   6725:                                 .'</li><li>'
                   6726:                                 .&mt("Click 'Search'")
                   6727:                                 .'</li></ul><br />';
1.221     raeburn  6728:                 } else {
                   6729:                     my $helplink = ' href="javascript:helpMenu('."'display'".')"';
1.305     raeburn  6730:                     $response .= '<br /><br />';
                   6731:                     if ($context eq 'requestcrs') {
1.314     raeburn  6732:                         $response .= &mt("You are not authorized to define new users in the new course's domain - [_1].",$targetdom);
1.305     raeburn  6733:                     } else {
                   6734:                         $response .= &mt("You are not authorized to create new users in your current role's domain - [_1].",$targetdom);
                   6735:                     }
                   6736:                     $response .= '<br />'
                   6737:                                  .&mt('Please contact the [_1]helpdesk[_2] if you need to create a new user.'
1.266     bisitz   6738:                                     ,' <a'.$helplink.'>'
                   6739:                                     ,'</a>')
1.305     raeburn  6740:                                  .'<br /><br />';
1.221     raeburn  6741:                 }
1.160     raeburn  6742:             }
                   6743:         }
                   6744:     }
1.179     raeburn  6745:     return ($currstate,$response,$forcenewuser);
1.160     raeburn  6746: }
                   6747: 
1.180     raeburn  6748: sub display_domain_info {
                   6749:     my ($dom) = @_;
                   6750:     my $output = $dom;
                   6751:     if ($dom ne '') { 
                   6752:         my $domdesc = &Apache::lonnet::domain($dom,'description');
                   6753:         if ($domdesc ne '') {
                   6754:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
                   6755:         }
                   6756:     }
                   6757:     return $output;
                   6758: }
                   6759: 
1.160     raeburn  6760: sub crumb_utilities {
                   6761:     my %elements = (
                   6762:        crtuser => {
                   6763:            srchterm => 'text',
1.172     raeburn  6764:            srchin => 'selectbox',
1.160     raeburn  6765:            srchby => 'selectbox',
                   6766:            srchtype => 'selectbox',
                   6767:            srchdomain => 'selectbox',
                   6768:        },
1.207     raeburn  6769:        crtusername => {
                   6770:            srchterm => 'text',
                   6771:            srchdomain => 'selectbox',
                   6772:        },
1.160     raeburn  6773:        docustom => {
                   6774:            rolename => 'selectbox',
                   6775:            newrolename => 'textbox',
                   6776:        },
1.179     raeburn  6777:        studentform => {
                   6778:            srchterm => 'text',
                   6779:            srchin => 'selectbox',
                   6780:            srchby => 'selectbox',
                   6781:            srchtype => 'selectbox',
                   6782:            srchdomain => 'selectbox',
                   6783:        },
1.160     raeburn  6784:     );
                   6785: 
                   6786:     my $jsback .= qq|
                   6787: function backPage(formname,prevphase,prevstate) {
1.211     raeburn  6788:     if (typeof prevphase == 'undefined') {
                   6789:         formname.phase.value = '';
                   6790:     }
                   6791:     else {  
                   6792:         formname.phase.value = prevphase;
                   6793:     }
                   6794:     if (typeof prevstate == 'undefined') {
                   6795:         formname.currstate.value = '';
                   6796:     }
                   6797:     else {
                   6798:         formname.currstate.value = prevstate;
                   6799:     }
1.160     raeburn  6800:     formname.submit();
                   6801: }
                   6802: |;
                   6803:     return ($jsback,\%elements);
                   6804: }
                   6805: 
1.26      matthew  6806: sub course_level_table {
1.375     raeburn  6807:     my ($inccourses,$showcredits,$defaultcredits) = @_;
                   6808:     return unless (ref($inccourses) eq 'HASH');
1.26      matthew  6809:     my $table = '';
1.62      www      6810: # Custom Roles?
                   6811: 
1.190     raeburn  6812:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.89      raeburn  6813:     my %lt=&Apache::lonlocal::texthash(
                   6814:             'exs'  => "Existing sections",
                   6815:             'new'  => "Define new section",
                   6816:             'ssd'  => "Set Start Date",
                   6817:             'sed'  => "Set End Date",
1.131     raeburn  6818:             'crl'  => "Course Level",
1.89      raeburn  6819:             'act'  => "Activate",
                   6820:             'rol'  => "Role",
                   6821:             'ext'  => "Extent",
1.113     raeburn  6822:             'grs'  => "Section",
1.375     raeburn  6823:             'crd'  => "Credits",
1.89      raeburn  6824:             'sta'  => "Start",
                   6825:             'end'  => "End"
                   6826:     );
1.62      www      6827: 
1.375     raeburn  6828:     foreach my $protectedcourse (sort(keys(%{$inccourses}))) {
1.135     raeburn  6829: 	my $thiscourse=$protectedcourse;
1.26      matthew  6830: 	$thiscourse=~s:_:/:g;
                   6831: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
1.365     raeburn  6832:         my $isowner = &Apache::lonuserutils::is_courseowner($protectedcourse,$coursedata{'internal.courseowner'});
1.26      matthew  6833: 	my $area=$coursedata{'description'};
1.321     raeburn  6834:         my $crstype=$coursedata{'type'};
1.135     raeburn  6835: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
1.89      raeburn  6836: 	my ($domain,$cnum)=split(/\//,$thiscourse);
1.115     albertel 6837:         my %sections_count;
1.101     albertel 6838:         if (defined($env{'request.course.id'})) {
                   6839:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
1.115     albertel 6840:                 %sections_count = 
                   6841: 		    &Apache::loncommon::get_sections($domain,$cnum);
1.92      raeburn  6842:             }
                   6843:         }
1.321     raeburn  6844:         my @roles = &Apache::lonuserutils::roles_by_context('course','',$crstype);
1.213     raeburn  6845: 	foreach my $role (@roles) {
1.321     raeburn  6846:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.329     raeburn  6847: 	    if ((&Apache::lonnet::allowed('c'.$role,$thiscourse)) ||
                   6848:                 ((($role eq 'cc') || ($role eq 'co')) && ($isowner))) {
1.221     raeburn  6849:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  6850:                                             $plrole,\%sections_count,\%lt,
                   6851:                                             $defaultcredits,$crstype);
1.221     raeburn  6852:             } elsif ($env{'request.course.sec'} ne '') {
                   6853:                 if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
                   6854:                                              $env{'request.course.sec'})) {
                   6855:                     $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  6856:                                                 $plrole,\%sections_count,\%lt,
                   6857:                                                 $defaultcredits,$crstype);
1.26      matthew  6858:                 }
                   6859:             }
                   6860:         }
1.221     raeburn  6861:         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
1.324     raeburn  6862:             foreach my $cust (sort(keys(%customroles))) {
                   6863:                 next if ($crstype eq 'Community' && $customroles{$cust} =~ /bre\&S/);
1.221     raeburn  6864:                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
                   6865:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
                   6866:                                             $cust,\%sections_count,\%lt);
                   6867:             }
1.62      www      6868: 	}
1.26      matthew  6869:     }
                   6870:     return '' if ($table eq ''); # return nothing if there is nothing 
                   6871:                                  # in the table
1.188     raeburn  6872:     my $result;
                   6873:     if (!$env{'request.course.id'}) {
                   6874:         $result = '<h4>'.$lt{'crl'}.'</h4>'."\n";
                   6875:     }
                   6876:     $result .= 
1.136     raeburn  6877: &Apache::loncommon::start_data_table().
                   6878: &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  6879: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
                   6880: '<th>'.$lt{'ext'}.'</th><th>'.$lt{'crd'}.'</th>'."\n".
                   6881: '<th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th>'."\n".
                   6882: '<th>'.$lt{'end'}.'</th>'.
1.136     raeburn  6883: &Apache::loncommon::end_data_table_header_row().
                   6884: $table.
                   6885: &Apache::loncommon::end_data_table();
1.26      matthew  6886:     return $result;
                   6887: }
1.88      raeburn  6888: 
1.221     raeburn  6889: sub course_level_row {
1.375     raeburn  6890:     my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,
                   6891:         $lt,$defaultcredits,$crstype) = @_;
                   6892:     my $creditem;
1.222     raeburn  6893:     my $row = &Apache::loncommon::start_data_table_row().
                   6894:               ' <td><input type="checkbox" name="act_'.
                   6895:               $protectedcourse.'_'.$role.'" /></td>'."\n".
                   6896:               ' <td>'.$plrole.'</td>'."\n".
                   6897:               ' <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
1.375     raeburn  6898:     if (($role eq 'st') && ($crstype eq 'Course')) {
                   6899:         $row .= 
                   6900:             '<td><input type="text" name="credits_'.$protectedcourse.'_'.
                   6901:             $role.'" size="3" value="'.$defaultcredits.'" /></td>';
                   6902:     } else {
                   6903:         $row .= '<td>&nbsp;</td>';
                   6904:     }
1.322     raeburn  6905:     if (($role eq 'cc') || ($role eq 'co')) {
1.222     raeburn  6906:         $row .= '<td>&nbsp;</td>';
1.221     raeburn  6907:     } elsif ($env{'request.course.sec'} ne '') {
1.222     raeburn  6908:         $row .= ' <td><input type="hidden" value="'.
                   6909:                 $env{'request.course.sec'}.'" '.
                   6910:                 'name="sec_'.$protectedcourse.'_'.$role.'" />'.
                   6911:                 $env{'request.course.sec'}.'</td>';
1.221     raeburn  6912:     } else {
                   6913:         if (ref($sections_count) eq 'HASH') {
                   6914:             my $currsec = 
                   6915:                 &Apache::lonuserutils::course_sections($sections_count,
                   6916:                                                        $protectedcourse.'_'.$role);
1.222     raeburn  6917:             $row .= '<td><table class="LC_createuser">'."\n".
                   6918:                     '<tr class="LC_section_row">'."\n".
                   6919:                     ' <td valign="top">'.$lt->{'exs'}.'<br />'.
                   6920:                        $currsec.'</td>'."\n".
                   6921:                      ' <td>&nbsp;&nbsp;</td>'."\n".
                   6922:                      ' <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
1.221     raeburn  6923:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
                   6924:                      '" value="" />'.
                   6925:                      '<input type="hidden" '.
                   6926:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
1.222     raeburn  6927:                      '</tr></table></td>'."\n";
1.221     raeburn  6928:         } else {
1.222     raeburn  6929:             $row .= '<td><input type="text" size="10" '.
1.375     raeburn  6930:                     'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n";
1.221     raeburn  6931:         }
                   6932:     }
1.222     raeburn  6933:     $row .= <<ENDTIMEENTRY;
                   6934: <td><input type="hidden" name="start_$protectedcourse\_$role" value="" />
1.221     raeburn  6935: <a href=
                   6936: "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  6937: <td><input type="hidden" name="end_$protectedcourse\_$role" value="" />
1.221     raeburn  6938: <a href=
                   6939: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
                   6940: ENDTIMEENTRY
1.222     raeburn  6941:     $row .= &Apache::loncommon::end_data_table_row();
                   6942:     return $row;
1.221     raeburn  6943: }
                   6944: 
1.88      raeburn  6945: sub course_level_dc {
1.375     raeburn  6946:     my ($dcdom,$showcredits) = @_;
1.190     raeburn  6947:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.213     raeburn  6948:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.88      raeburn  6949:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
                   6950:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
1.133     raeburn  6951:                       '<input type="hidden" name="dccourse" value="" />';
1.355     www      6952:     my $courseform=&Apache::loncommon::selectcourse_link
1.356     raeburn  6953:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Select','crstype');
1.375     raeburn  6954:     my $credit_elem;
                   6955:     if ($showcredits) {
                   6956:         $credit_elem = 'credits';
                   6957:     }
                   6958:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu','role','Course/Community Browser',$credit_elem);
1.88      raeburn  6959:     my %lt=&Apache::lonlocal::texthash(
                   6960:                     'rol'  => "Role",
1.113     raeburn  6961:                     'grs'  => "Section",
1.88      raeburn  6962:                     'exs'  => "Existing sections",
                   6963:                     'new'  => "Define new section", 
                   6964:                     'sta'  => "Start",
                   6965:                     'end'  => "End",
                   6966:                     'ssd'  => "Set Start Date",
1.355     www      6967:                     'sed'  => "Set End Date",
1.375     raeburn  6968:                     'scc'  => "Course/Community",
                   6969:                     'crd'  => "Credits",
1.88      raeburn  6970:                   );
1.323     raeburn  6971:     my $header = '<h4>'.&mt('Course/Community Level').'</h4>'.
1.136     raeburn  6972:                  &Apache::loncommon::start_data_table().
                   6973:                  &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  6974:                  '<th>'.$lt{'scc'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
                   6975:                  '<th>'.$lt{'grs'}.'</th><th>'.$lt{'crd'}.'</th>'."\n".
                   6976:                  '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'."\n".
1.136     raeburn  6977:                  &Apache::loncommon::end_data_table_header_row();
1.143     raeburn  6978:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
1.356     raeburn  6979:                      '<td><br /><span class="LC_nobreak"><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc','','','','crstype'".')" />'.
                   6980:                      $courseform.('&nbsp;' x4).'</span></td>'."\n".
1.323     raeburn  6981:                      '<td valign><br /><select name="role">'."\n";
1.213     raeburn  6982:     foreach my $role (@roles) {
1.135     raeburn  6983:         my $plrole=&Apache::lonnet::plaintext($role);
                   6984:         $otheritems .= '  <option value="'.$role.'">'.$plrole;
1.88      raeburn  6985:     }
                   6986:     if ( keys %customroles > 0) {
1.135     raeburn  6987:         foreach my $cust (sort keys %customroles) {
1.101     albertel 6988:             my $custrole='cr_cr_'.$env{'user.domain'}.
1.135     raeburn  6989:                     '_'.$env{'user.name'}.'_'.$cust;
                   6990:             $otheritems .= '  <option value="'.$custrole.'">'.$cust;
1.88      raeburn  6991:         }
                   6992:     }
                   6993:     $otheritems .= '</select></td><td>'.
                   6994:                      '<table border="0" cellspacing="0" cellpadding="0">'.
                   6995:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
                   6996:                      ' <option value=""><--'.&mt('Pick course first').'</select></td>'.
                   6997:                      '<td>&nbsp;&nbsp;</td>'.
                   6998:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
1.113     raeburn  6999:                      '<input type="text" name="newsec" value="" />'.
1.237     raeburn  7000:                      '<input type="hidden" name="section" value="" />'.
1.323     raeburn  7001:                      '<input type="hidden" name="groups" value="" />'.
                   7002:                      '<input type="hidden" name="crstype" value="" /></td>'.
1.375     raeburn  7003:                      '</tr></table></td>'."\n";
                   7004:     if ($showcredits) {
                   7005:         $otheritems .= '<td><br />'."\n".
                   7006:                        '<input type="text" size="3" name="credits" value="" />'."\n";
                   7007:     }
1.88      raeburn  7008:     $otheritems .= <<ENDTIMEENTRY;
1.323     raeburn  7009: <td><br /><input type="hidden" name="start" value='' />
1.88      raeburn  7010: <a href=
                   7011: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.323     raeburn  7012: <td><br /><input type="hidden" name="end" value='' />
1.88      raeburn  7013: <a href=
                   7014: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
                   7015: ENDTIMEENTRY
1.136     raeburn  7016:     $otheritems .= &Apache::loncommon::end_data_table_row().
                   7017:                    &Apache::loncommon::end_data_table()."\n";
1.88      raeburn  7018:     return $cb_jscript.$header.$hiddenitems.$otheritems;
                   7019: }
                   7020: 
1.237     raeburn  7021: sub update_selfenroll_config {
1.241     raeburn  7022:     my ($r,$context,$permission) = @_;
1.237     raeburn  7023:     my ($row,$lt) = &get_selfenroll_titles();
1.241     raeburn  7024:     my %curr_groups = &Apache::longroup::coursegroups();
1.237     raeburn  7025:     my (%changes,%warning);
                   7026:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7027:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.241     raeburn  7028:     my $curr_types;
1.237     raeburn  7029:     if (ref($row) eq 'ARRAY') {
                   7030:         foreach my $item (@{$row}) {
                   7031:             if ($item eq 'enroll_dates') {
                   7032:                 my (%currenrolldate,%newenrolldate);
                   7033:                 foreach my $type ('start','end') {
                   7034:                     $currenrolldate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_date'};
                   7035:                     $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
                   7036:                     if ($newenrolldate{$type} ne $currenrolldate{$type}) {
                   7037:                         $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
                   7038:                     }
                   7039:                 }
                   7040:             } elsif ($item eq 'access_dates') {
                   7041:                 my (%currdate,%newdate);
                   7042:                 foreach my $type ('start','end') {
                   7043:                     $currdate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_access'};
                   7044:                     $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
                   7045:                     if ($newdate{$type} ne $currdate{$type}) {
                   7046:                         $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
                   7047:                     }
                   7048:                 }
1.241     raeburn  7049:             } elsif ($item eq 'types') {
                   7050:                 $curr_types =
                   7051:                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$item};
                   7052:                 if ($env{'form.selfenroll_all'}) {
                   7053:                     if ($curr_types ne '*') {
                   7054:                         $changes{'internal.selfenroll_types'} = '*';
                   7055:                     } else {
                   7056:                         next;
                   7057:                     }
                   7058:                 } else {
1.249     raeburn  7059:                     my %currdoms;
1.241     raeburn  7060:                     my @entries = split(/;/,$curr_types);
                   7061:                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
1.249     raeburn  7062:                     my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
1.241     raeburn  7063:                     my $newnum = 0;
1.249     raeburn  7064:                     my @latesttypes;
                   7065:                     foreach my $num (@activations) {
                   7066:                         my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
                   7067:                         if (@types > 0) {
1.241     raeburn  7068:                             @types = sort(@types);
                   7069:                             my $typestr = join(',',@types);
1.249     raeburn  7070:                             my $typedom = $env{'form.selfenroll_dom_'.$num};
                   7071:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   7072:                             $currdoms{$typedom} = 1;
1.241     raeburn  7073:                             $newnum ++;
                   7074:                         }
                   7075:                     }
1.338     raeburn  7076:                     for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {
                   7077:                         if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
1.249     raeburn  7078:                             my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
                   7079:                             if (@types > 0) {
                   7080:                                 @types = sort(@types);
                   7081:                                 my $typestr = join(',',@types);
                   7082:                                 my $typedom = $env{'form.selfenroll_dom_'.$j};
                   7083:                                 $latesttypes[$newnum] = $typedom.':'.$typestr;
                   7084:                                 $currdoms{$typedom} = 1;
                   7085:                                 $newnum ++;
                   7086:                             }
                   7087:                         }
                   7088:                     }
                   7089:                     if ($env{'form.selfenroll_newdom'} ne '') {
                   7090:                         my $typedom = $env{'form.selfenroll_newdom'};
                   7091:                         if ((!defined($currdoms{$typedom})) && 
                   7092:                             (&Apache::lonnet::domain($typedom) ne '')) {
                   7093:                             my $typestr;
                   7094:                             my ($othertitle,$usertypes,$types) = 
                   7095:                                 &Apache::loncommon::sorted_inst_types($typedom);
                   7096:                             my $othervalue = 'any';
                   7097:                             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   7098:                                 if (@{$types} > 0) {
1.257     raeburn  7099:                                     my @esc_types = map { &escape($_); } @{$types};
1.249     raeburn  7100:                                     $othervalue = 'other';
1.258     raeburn  7101:                                     $typestr = join(',',(@esc_types,$othervalue));
1.249     raeburn  7102:                                 }
                   7103:                                 $typestr = $othervalue;
                   7104:                             } else {
                   7105:                                 $typestr = $othervalue;
                   7106:                             } 
                   7107:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   7108:                             $newnum ++ ;
                   7109:                         }
                   7110:                     }
1.241     raeburn  7111:                     my $selfenroll_types = join(';',@latesttypes);
                   7112:                     if ($selfenroll_types ne $curr_types) {
                   7113:                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
                   7114:                     }
                   7115:                 }
1.276     raeburn  7116:             } elsif ($item eq 'limit') {
                   7117:                 my $newlimit = $env{'form.selfenroll_limit'};
                   7118:                 my $newcap = $env{'form.selfenroll_cap'};
                   7119:                 $newcap =~s/\s+//g;
                   7120:                 my $currlimit =  $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_limit'};
                   7121:                 $currlimit = 'none' if ($currlimit eq '');
                   7122:                 my $currcap = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_cap'};
                   7123:                 if ($newlimit ne $currlimit) {
                   7124:                     if ($newlimit ne 'none') {
                   7125:                         if ($newcap =~ /^\d+$/) {
                   7126:                             if ($newcap ne $currcap) {
                   7127:                                 $changes{'internal.selfenroll_cap'} = $newcap;
                   7128:                             }
                   7129:                             $changes{'internal.selfenroll_limit'} = $newlimit;
                   7130:                         } else {
                   7131:                             $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.&mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.'); 
                   7132:                         }
                   7133:                     } elsif ($currcap ne '') {
                   7134:                         $changes{'internal.selfenroll_cap'} = '';
                   7135:                         $changes{'internal.selfenroll_limit'} = $newlimit; 
                   7136:                     }
                   7137:                 } elsif ($currlimit ne 'none') {
                   7138:                     if ($newcap =~ /^\d+$/) {
                   7139:                         if ($newcap ne $currcap) {
                   7140:                             $changes{'internal.selfenroll_cap'} = $newcap;
                   7141:                         }
                   7142:                     } else {
                   7143:                         $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.&mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.');
                   7144:                     }
                   7145:                 }
                   7146:             } elsif ($item eq 'approval') {
                   7147:                 my (@currnotified,@newnotified);
                   7148:                 my $currapproval = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'};
                   7149:                 my $currnotifylist = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_notifylist'};
                   7150:                 if ($currnotifylist ne '') {
                   7151:                     @currnotified = split(/,/,$currnotifylist);
                   7152:                     @currnotified = sort(@currnotified);
                   7153:                 }
                   7154:                 my $newapproval = $env{'form.selfenroll_approval'};
                   7155:                 @newnotified = &Apache::loncommon::get_env_multiple('form.selfenroll_notify');
                   7156:                 @newnotified = sort(@newnotified);
                   7157:                 if ($newapproval ne $currapproval) {
                   7158:                     $changes{'internal.selfenroll_approval'} = $newapproval;
                   7159:                     if (!$newapproval) {
                   7160:                         if ($currnotifylist ne '') {
                   7161:                             $changes{'internal.selfenroll_notifylist'} = '';
                   7162:                         }
                   7163:                     } else {
                   7164:                         my @differences =  
1.295     raeburn  7165:                             &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  7166:                         if (@differences > 0) {
                   7167:                             if (@newnotified > 0) {
                   7168:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   7169:                             } else {
                   7170:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   7171:                             }
                   7172:                         }
                   7173:                     }
                   7174:                 } else {
1.295     raeburn  7175:                     my @differences = &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  7176:                     if (@differences > 0) {
                   7177:                         if (@newnotified > 0) {
                   7178:                             $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   7179:                         } else {
                   7180:                             $changes{'internal.selfenroll_notifylist'} = '';
                   7181:                         }
                   7182:                     }
                   7183:                 }
1.237     raeburn  7184:             } else {
                   7185:                 my $curr_val = 
                   7186:                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$item};
                   7187:                 my $newval = $env{'form.selfenroll_'.$item};
                   7188:                 if ($item eq 'section') {
                   7189:                     $newval = $env{'form.sections'};
1.241     raeburn  7190:                     if (defined($curr_groups{$newval})) {
1.237     raeburn  7191:                         $newval = $curr_val;
                   7192:                         $warning{$item} = &mt('Section for self-enrolled users unchanged as the proposed section is a group').'<br />'.&mt('Group names and section names must be distinct');
                   7193:                     } elsif ($newval eq 'all') {
                   7194:                         $newval = $curr_val;
1.274     bisitz   7195:                         $warning{$item} = &mt('Section for self-enrolled users unchanged, as "all" is a reserved section name.');
1.237     raeburn  7196:                     }
                   7197:                     if ($newval eq '') {
                   7198:                         $newval = 'none';
                   7199:                     }
                   7200:                 }
                   7201:                 if ($newval ne $curr_val) {
                   7202:                     $changes{'internal.selfenroll_'.$item} = $newval;
                   7203:                 }
1.241     raeburn  7204:             }
1.237     raeburn  7205:         }
                   7206:         if (keys(%warning) > 0) {
                   7207:             foreach my $item (@{$row}) {
                   7208:                 if (exists($warning{$item})) {
                   7209:                     $r->print($warning{$item}.'<br />');
                   7210:                 }
                   7211:             } 
                   7212:         }
                   7213:         if (keys(%changes) > 0) {
                   7214:             my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
                   7215:             if ($putresult eq 'ok') {
                   7216:                 if ((exists($changes{'internal.selfenroll_types'})) ||
                   7217:                     (exists($changes{'internal.selfenroll_start_date'}))  ||
                   7218:                     (exists($changes{'internal.selfenroll_end_date'}))) {
                   7219:                     my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
                   7220:                                                                 $cnum,undef,undef,'Course');
                   7221:                     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
                   7222:                     if (ref($crsinfo{$env{'request.course.id'}}) eq 'HASH') {
                   7223:                         foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
                   7224:                             if (exists($changes{'internal.'.$item})) {
                   7225:                                 $crsinfo{$env{'request.course.id'}}{$item} = 
                   7226:                                     $changes{'internal.'.$item};
                   7227:                             }
                   7228:                         }
                   7229:                         my $crsputresult =
                   7230:                             &Apache::lonnet::courseidput($cdom,\%crsinfo,
                   7231:                                                          $chome,'notime');
                   7232:                     }
                   7233:                 }
                   7234:                 $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
                   7235:                 foreach my $item (@{$row}) {
                   7236:                     my $title = $item;
                   7237:                     if (ref($lt) eq 'HASH') {
                   7238:                         $title = $lt->{$item};
                   7239:                     }
                   7240:                     if ($item eq 'enroll_dates') {
                   7241:                         foreach my $type ('start','end') {
                   7242:                             if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
                   7243:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
1.244     bisitz   7244:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  7245:                                           $title,$type,$newdate).'</li>');
                   7246:                             }
                   7247:                         }
                   7248:                     } elsif ($item eq 'access_dates') {
                   7249:                         foreach my $type ('start','end') {
                   7250:                             if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
                   7251:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
1.244     bisitz   7252:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  7253:                                           $title,$type,$newdate).'</li>');
                   7254:                             }
                   7255:                         }
1.276     raeburn  7256:                     } elsif ($item eq 'limit') {
                   7257:                         if ((exists($changes{'internal.selfenroll_limit'})) ||
                   7258:                             (exists($changes{'internal.selfenroll_cap'}))) {
                   7259:                             my ($newval,$newcap);
                   7260:                             if ($changes{'internal.selfenroll_cap'} ne '') {
                   7261:                                 $newcap = $changes{'internal.selfenroll_cap'}
                   7262:                             } else {
                   7263:                                 $newcap = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_cap'};
                   7264:                             }
                   7265:                             if ($changes{'internal.selfenroll_limit'} eq 'none') {
                   7266:                                 $newval = &mt('No limit');
                   7267:                             } elsif ($changes{'internal.selfenroll_limit'} eq 
                   7268:                                      'allstudents') {
                   7269:                                 $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   7270:                             } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
                   7271:                                 $newval = &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
                   7272:                             } else {
                   7273:                                 my $currlimit =  $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_limit'};
                   7274:                                 if ($currlimit eq 'allstudents') {
                   7275:                                     $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   7276:                                 } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
1.308     raeburn  7277:                                     $newval =  &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
1.276     raeburn  7278:                                 }
                   7279:                             }
                   7280:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
                   7281:                         }
                   7282:                     } elsif ($item eq 'approval') {
                   7283:                         if ((exists($changes{'internal.selfenroll_approval'})) ||
                   7284:                             (exists($changes{'internal.selfenroll_notifylist'}))) {
                   7285:                             my ($newval,$newnotify);
                   7286:                             if (exists($changes{'internal.selfenroll_notifylist'})) {
                   7287:                                 $newnotify = $changes{'internal.selfenroll_notifylist'};
                   7288:                             } else {   
                   7289:                                 $newnotify = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_notifylist'};
                   7290:                             }
                   7291:                             if ($changes{'internal.selfenroll_approval'}) {
                   7292:                                 $newval = &mt('Yes');
                   7293:                             } elsif ($changes{'internal.selfenroll_approval'} eq '0') {
                   7294:                                 $newval = &mt('No');
                   7295:                             } else {
                   7296:                                 my $currapproval = 
                   7297:                                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'};
                   7298:                                 if ($currapproval) {
                   7299:                                     $newval = &mt('Yes');
                   7300:                                 } else {
                   7301:                                     $newval = &mt('No');
                   7302:                                 }
                   7303:                             }
                   7304:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval));
                   7305:                             if ($newnotify) {
1.277     raeburn  7306:                                 $r->print('<br />'.&mt('The following will be notified when an enrollment request needs approval, or has been approved: [_1].',$newnotify));
1.276     raeburn  7307:                             } else {
1.277     raeburn  7308:                                 $r->print('<br />'.&mt('No notifications sent when an enrollment request needs approval, or has been approved.'));
1.276     raeburn  7309:                             }
                   7310:                             $r->print('</li>'."\n");
                   7311:                         }
1.237     raeburn  7312:                     } else {
                   7313:                         if (exists($changes{'internal.selfenroll_'.$item})) {
1.241     raeburn  7314:                             my $newval = $changes{'internal.selfenroll_'.$item};
                   7315:                             if ($item eq 'types') {
                   7316:                                 if ($newval eq '') {
                   7317:                                     $newval = &mt('None');
                   7318:                                 } elsif ($newval eq '*') {
                   7319:                                     $newval = &mt('Any user in any domain');
                   7320:                                 }
1.245     raeburn  7321:                             } elsif ($item eq 'registered') {
                   7322:                                 if ($newval eq '1') {
                   7323:                                     $newval = &mt('Yes');
                   7324:                                 } elsif ($newval eq '0') {
                   7325:                                     $newval = &mt('No');
                   7326:                                 }
1.241     raeburn  7327:                             }
1.244     bisitz   7328:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
1.237     raeburn  7329:                         }
                   7330:                     }
                   7331:                 }
                   7332:                 $r->print('</ul>');
                   7333:                 my %newenvhash;
                   7334:                 foreach my $key (keys(%changes)) {
                   7335:                     $newenvhash{'course.'.$env{'request.course.id'}.'.'.$key} = $changes{$key};
                   7336:                 }
1.238     raeburn  7337:                 &Apache::lonnet::appenv(\%newenvhash);
1.237     raeburn  7338:             } else {
                   7339:                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.&mt('The error was: [_1].',$putresult));
                   7340:             }
                   7341:         } else {
1.249     raeburn  7342:             $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.237     raeburn  7343:         }
                   7344:     } else {
1.249     raeburn  7345:         $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.241     raeburn  7346:     }
1.256     raeburn  7347:     my ($visible,$cansetvis,$vismsgs,$visactions) = &visible_in_cat($cdom,$cnum);
                   7348:     if (ref($visactions) eq 'HASH') {
                   7349:         if (!$visible) {
1.366     bisitz   7350:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
1.256     raeburn  7351:                       '<br />');
                   7352:             if (ref($vismsgs) eq 'ARRAY') {
                   7353:                 $r->print('<br />'.$visactions->{'take'}.'<ul>');
                   7354:                 foreach my $item (@{$vismsgs}) {
                   7355:                     $r->print('<li>'.$visactions->{$item}.'</li>');
                   7356:                 }
                   7357:                 $r->print('</ul>');
                   7358:             }
                   7359:             $r->print($cansetvis);
                   7360:         }
                   7361:     } 
1.237     raeburn  7362:     return;
                   7363: }
                   7364: 
                   7365: sub get_selfenroll_titles {
1.276     raeburn  7366:     my @row = ('types','registered','enroll_dates','access_dates','section',
                   7367:                'approval','limit');
1.237     raeburn  7368:     my %lt = &Apache::lonlocal::texthash (
                   7369:                 types        => 'Users allowed to self-enroll in this course',
1.245     raeburn  7370:                 registered   => 'Restrict self-enrollment to students officially registered for the course',
1.237     raeburn  7371:                 enroll_dates => 'Dates self-enrollment available',
1.256     raeburn  7372:                 access_dates => 'Course access dates assigned to self-enrolling users',
                   7373:                 section      => 'Section assigned to self-enrolling users',
1.276     raeburn  7374:                 approval     => 'Self-enrollment requests need approval?',
                   7375:                 limit        => 'Enrollment limit',
1.237     raeburn  7376:              );
                   7377:     return (\@row,\%lt);
                   7378: }
                   7379: 
1.27      matthew  7380: #---------------------------------------------- end functions for &phase_two
1.29      matthew  7381: 
                   7382: #--------------------------------- functions for &phase_two and &phase_three
                   7383: 
                   7384: #--------------------------end of functions for &phase_two and &phase_three
1.372     raeburn  7385: 
1.1       www      7386: 1;
                   7387: __END__
1.2       www      7388: 
                   7389: 

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