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

1.20      harris41    1: # The LearningOnline Network with CAPA
1.1       www         2: # Create a user
                      3: #
1.378   ! raeburn     4: # $Id: loncreateuser.pm,v 1.377 2013/06/04 23:12:08 raeburn Exp $
1.22      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.20      harris41   28: ###
                     29: 
1.1       www        30: package Apache::loncreateuser;
1.66      bowersj2   31: 
                     32: =pod
                     33: 
                     34: =head1 NAME
                     35: 
1.263     jms        36: Apache::loncreateuser.pm
1.66      bowersj2   37: 
                     38: =head1 SYNOPSIS
                     39: 
1.263     jms        40:     Handler to create users and custom roles
                     41: 
                     42:     Provides an Apache handler for creating users,
1.66      bowersj2   43:     editing their login parameters, roles, and removing roles, and
                     44:     also creating and assigning custom roles.
                     45: 
                     46: =head1 OVERVIEW
                     47: 
                     48: =head2 Custom Roles
                     49: 
                     50: In LON-CAPA, roles are actually collections of privileges. "Teaching
                     51: Assistant", "Course Coordinator", and other such roles are really just
                     52: collection of privileges that are useful in many circumstances.
                     53: 
1.324     raeburn    54: Custom roles can be defined by a Domain Coordinator, Course Coordinator
                     55: or Community Coordinator via the Manage User functionality.
                     56: The custom role editor screen will show all privileges which can be
                     57: assigned to users. For a complete list of privileges, please see 
                     58: C</home/httpd/lonTabs/rolesplain.tab>.
1.66      bowersj2   59: 
1.324     raeburn    60: Custom role definitions are stored in the C<roles.db> file of the creator
                     61: of the role.
1.66      bowersj2   62: 
                     63: =cut
1.1       www        64: 
                     65: use strict;
                     66: use Apache::Constants qw(:common :http);
                     67: use Apache::lonnet;
1.54      bowersj2   68: use Apache::loncommon;
1.68      www        69: use Apache::lonlocal;
1.117     raeburn    70: use Apache::longroup;
1.190     raeburn    71: use Apache::lonuserutils;
1.307     raeburn    72: use Apache::loncoursequeueadmin;
1.139     albertel   73: use LONCAPA qw(:DEFAULT :match);
1.1       www        74: 
1.20      harris41   75: my $loginscript; # piece of javascript used in two separate instances
                     76: my $authformnop;
                     77: my $authformkrb;
                     78: my $authformint;
                     79: my $authformfsys;
                     80: my $authformloc;
                     81: 
1.94      matthew    82: sub initialize_authen_forms {
1.227     raeburn    83:     my ($dom,$formname,$curr_authtype,$mode) = @_;
                     84:     my ($krbdef,$krbdefdom) = &Apache::loncommon::get_kerberos_defaults($dom);
                     85:     my %param = ( formname => $formname,
1.187     raeburn    86:                   kerb_def_dom => $krbdefdom,
1.227     raeburn    87:                   kerb_def_auth => $krbdef,
1.187     raeburn    88:                   domain => $dom,
                     89:                 );
1.188     raeburn    90:     my %abv_auth = &auth_abbrev();
1.227     raeburn    91:     if ($curr_authtype =~ /^(krb4|krb5|internal|localauth|unix):(.*)$/) {
1.188     raeburn    92:         my $long_auth = $1;
1.227     raeburn    93:         my $curr_autharg = $2;
1.188     raeburn    94:         my %abv_auth = &auth_abbrev();
                     95:         $param{'curr_authtype'} = $abv_auth{$long_auth};
                     96:         if ($long_auth =~ /^krb(4|5)$/) {
                     97:             $param{'curr_kerb_ver'} = $1;
1.227     raeburn    98:             $param{'curr_autharg'} = $curr_autharg;
1.188     raeburn    99:         }
1.205     raeburn   100:         if ($mode eq 'modifyuser') {
                    101:             $param{'mode'} = $mode;
                    102:         }
1.187     raeburn   103:     }
1.227     raeburn   104:     $loginscript  = &Apache::loncommon::authform_header(%param);
                    105:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
1.31      matthew   106:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
                    107:     $authformint  = &Apache::loncommon::authform_internal(%param);
                    108:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
                    109:     $authformloc  = &Apache::loncommon::authform_local(%param);
1.20      harris41  110: }
                    111: 
1.188     raeburn   112: sub auth_abbrev {
                    113:     my %abv_auth = (
1.368     raeburn   114:                      krb5      => 'krb',
                    115:                      krb4      => 'krb',
                    116:                      internal  => 'int',
                    117:                      localauth => 'loc',
                    118:                      unix      => 'fsys',
1.188     raeburn   119:                    );
                    120:     return %abv_auth;
                    121: }
1.43      www       122: 
1.134     raeburn   123: # ====================================================
                    124: 
1.378   ! raeburn   125: sub user_quotas {
1.134     raeburn   126:     my ($ccuname,$ccdomain) = @_;
                    127:     my %lt = &Apache::lonlocal::texthash(
1.267     raeburn   128:                    'usrt'      => "User Tools",
                    129:                    '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",
        !           171:                     author    => "Disk space allocated to user's authoring space (if role assigned)",
        !           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]'
        !           192:                               .' Mb.',$defquota);
        !           193:             } else {
        !           194:                 $defaultinfo = &mt("For this user, the default quota would be [_1]".
        !           195:                                    " Mb, as determined by the user's institutional".
        !           196:                                    " affiliation ([_2]).",$defquota,$longinsttype);
        !           197:             }
        !           198:         } else {
        !           199:             if ($longinsttype eq '') {
        !           200:                 $defaultinfo = &mt('For this user, the default quota is [_1]'
        !           201:                               .' Mb.',$defquota);
        !           202:             } else {
        !           203:                 $defaultinfo = &mt("For this user, the default quota of [_1]".
        !           204:                                    " Mb, is determined by the user's institutional".
        !           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'}.': '.
        !           215:                        $currquota.'&nbsp;Mb.&nbsp;&nbsp;'.
        !           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" '.
        !           222:                        'value="0" '.$custom_off.' onchange="javascript:quota_changes('."'custom','$name'".')"'.
        !           223:                        ' />'.$lt{'defa'}.'&nbsp;('.$defquota.' Mb).</label>&nbsp;'.
        !           224:                        '&nbsp;<label><input type="radio" name="custom_'.$name.'quota" id="custom_'.$name.'quota_on" '.
        !           225:                        'value="1" '.$custom_on.'  onchange="javascript:quota_changes('."'custom'".')";'.
        !           226:                        ' />'.$lt{'cust'}.':</label>&nbsp;'.
        !           227:                        '<input type="text" name="'.$name.'quota" size ="5" value="'.
        !           228:                        $showquota.'" onfocus="javascript:quota_changes('."'quota','$name'".')"'.
        !           229:                        ' />&nbsp;Mb</span></td>'."\n".
        !           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.361     raeburn   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.362     raeburn   254:                    'requestauthor'  => 'Can request author space',
1.275     raeburn   255:     );
1.279     raeburn   256:     if ($context eq 'requestcourses') {
1.275     raeburn   257:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.299     raeburn   258:                       'requestcourses.official','requestcourses.unofficial',
                    259:                       'requestcourses.community');
                    260:         @usertools = ('official','unofficial','community');
1.309     raeburn   261:         @options =('norequest','approval','autolimit','validate');
1.306     raeburn   262:         %validations = &Apache::lonnet::auto_courserequest_checks($ccdomain);
                    263:         %reqtitles = &courserequest_titles();
                    264:         %reqdisplay = &courserequest_display();
                    265:         $colspan = ' colspan="2"';
1.332     raeburn   266:         %domconfig =
                    267:             &Apache::lonnet::get_dom('configuration',['requestcourses'],$ccdomain);
                    268:         $isadv = &Apache::lonnet::is_advanced_user($ccuname,$ccdomain);
1.362     raeburn   269:     } elsif ($context eq 'requestauthor') {
                    270:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    271:                                                     'requestauthor');
                    272:         @usertools = ('requestauthor');
                    273:         @options =('norequest','approval','automatic');
                    274:         %reqtitles = &requestauthor_titles();
                    275:         %reqdisplay = &requestauthor_display();
                    276:         $colspan = ' colspan="2"';
                    277:         %domconfig =
                    278:             &Apache::lonnet::get_dom('configuration',['requestauthor'],$ccdomain);
1.275     raeburn   279:     } else {
                    280:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.361     raeburn   281:                           'tools.aboutme','tools.portfolio','tools.blog',
                    282:                           'tools.webdav');
                    283:         @usertools = ('aboutme','blog','webdav','portfolio');
1.275     raeburn   284:     }
                    285:     foreach my $item (@usertools) {
1.306     raeburn   286:         my ($custom_access,$curr_access,$cust_on,$cust_off,$tool_on,$tool_off,
                    287:             $currdisp,$custdisp,$custradio);
1.275     raeburn   288:         $cust_off = 'checked="checked" ';
                    289:         $tool_on = 'checked="checked" ';
                    290:         $curr_access =  
                    291:             &Apache::lonnet::usertools_access($ccuname,$ccdomain,$item,undef,
                    292:                                               $context);
1.362     raeburn   293:         if ($context eq 'requestauthor') {
                    294:             if ($userenv{$context} ne '') {
                    295:                 $cust_on = ' checked="checked" ';
                    296:                 $cust_off = '';
                    297:             }  
                    298:         } elsif ($userenv{$context.'.'.$item} ne '') {
1.306     raeburn   299:             $cust_on = ' checked="checked" ';
                    300:             $cust_off = '';
                    301:         }
                    302:         if ($context eq 'requestcourses') {
                    303:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   304:                 $custom_access = &mt('Currently from default setting.');
1.306     raeburn   305:             } else {
                    306:                 $custom_access = &mt('Currently from custom setting.');
1.275     raeburn   307:             }
1.362     raeburn   308:         } elsif ($context eq 'requestauthor') {
                    309:             if ($userenv{$context} eq '') {
                    310:                 $custom_access = &mt('Currently from default setting.');
                    311:             } else {
                    312:                 $custom_access = &mt('Currently from custom setting.');
                    313:             }
1.275     raeburn   314:         } else {
1.306     raeburn   315:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   316:                 $custom_access =
1.306     raeburn   317:                     &mt('Availability determined currently from default setting.');
                    318:                 if (!$curr_access) {
                    319:                     $tool_off = 'checked="checked" ';
                    320:                     $tool_on = '';
                    321:                 }
                    322:             } else {
1.314     raeburn   323:                 $custom_access =
1.306     raeburn   324:                     &mt('Availability determined currently from custom setting.');
                    325:                 if ($userenv{$context.'.'.$item} == 0) {
                    326:                     $tool_off = 'checked="checked" ';
                    327:                     $tool_on = '';
                    328:                 }
1.275     raeburn   329:             }
                    330:         }
                    331:         $output .= '  <tr class="LC_info_row">'."\n".
1.306     raeburn   332:                    '   <td'.$colspan.'>'.$lt{$item}.'</td>'."\n".
1.275     raeburn   333:                    '  </tr>'."\n".
1.306     raeburn   334:                    &Apache::loncommon::start_data_table_row()."\n";
1.362     raeburn   335:         if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.306     raeburn   336:             my ($curroption,$currlimit);
1.362     raeburn   337:             my $envkey = $context.'.'.$item;
                    338:             if ($context eq 'requestauthor') {
                    339:                 $envkey = $context;
                    340:             }
                    341:             if ($userenv{$envkey} ne '') {
                    342:                 $curroption = $userenv{$envkey};
1.332     raeburn   343:             } else {
                    344:                 my (@inststatuses);
1.362     raeburn   345:                 if ($context eq 'requestcourses') {
                    346:                     $curroption =
                    347:                         &Apache::loncoursequeueadmin::get_processtype('course',$ccuname,$ccdomain,
                    348:                                                                       $isadv,$ccdomain,$item,
                    349:                                                                       \@inststatuses,\%domconfig);
                    350:                 } else {
                    351:                      $curroption = 
                    352:                          &Apache::loncoursequeueadmin::get_processtype('requestauthor',$ccuname,$ccdomain,
                    353:                                                                        $isadv,$ccdomain,undef,
                    354:                                                                        \@inststatuses,\%domconfig);
                    355:                 }
1.332     raeburn   356:             }
1.306     raeburn   357:             if (!$curroption) {
                    358:                 $curroption = 'norequest';
                    359:             }
                    360:             if ($curroption =~ /^autolimit=(\d*)$/) {
                    361:                 $currlimit = $1;
1.314     raeburn   362:                 if ($currlimit eq '') {
                    363:                     $currdisp = &mt('Yes, automatic creation');
                    364:                 } else {
                    365:                     $currdisp = &mt('Yes, up to [quant,_1,request]/user',$currlimit);
                    366:                 }
1.306     raeburn   367:             } else {
                    368:                 $currdisp = $reqdisplay{$curroption};
                    369:             }
                    370:             $custdisp = '<table>';
                    371:             foreach my $option (@options) {
                    372:                 my $val = $option;
                    373:                 if ($option eq 'norequest') {
                    374:                     $val = 0;
                    375:                 }
                    376:                 if ($option eq 'validate') {
                    377:                     my $canvalidate = 0;
                    378:                     if (ref($validations{$item}) eq 'HASH') {
                    379:                         if ($validations{$item}{'_custom_'}) {
                    380:                             $canvalidate = 1;
                    381:                         }
                    382:                     }
                    383:                     next if (!$canvalidate);
                    384:                 }
                    385:                 my $checked = '';
                    386:                 if ($option eq $curroption) {
                    387:                     $checked = ' checked="checked"';
                    388:                 } elsif ($option eq 'autolimit') {
                    389:                     if ($curroption =~ /^autolimit/) {
                    390:                         $checked = ' checked="checked"';
                    391:                     }
                    392:                 }
1.362     raeburn   393:                 my $name = 'crsreq_'.$item;
                    394:                 if ($context eq 'requestauthor') {
                    395:                     $name = $item;
                    396:                 }
1.306     raeburn   397:                 $custdisp .= '<tr><td><span class="LC_nobreak"><label>'.
1.362     raeburn   398:                              '<input type="radio" name="'.$name.'" '.
                    399:                              'value="'.$val.'"'.$checked.' />'.
1.306     raeburn   400:                              $reqtitles{$option}.'</label>&nbsp;';
                    401:                 if ($option eq 'autolimit') {
1.362     raeburn   402:                     $custdisp .= '<input type="text" name="'.$name.
                    403:                                  '_limit" size="1" '.
1.314     raeburn   404:                                  'value="'.$currlimit.'" /></span><br />'.
                    405:                                  $reqtitles{'unlimited'};
1.362     raeburn   406:                 } else {
                    407:                     $custdisp .= '</span>';
                    408:                 }
                    409:                 $custdisp .= '</td></tr>';
1.306     raeburn   410:             }
                    411:             $custdisp .= '</table>';
                    412:             $custradio = '</span></td><td>'.&mt('Custom setting').'<br />'.$custdisp;
                    413:         } else {
                    414:             $currdisp = ($curr_access?&mt('Yes'):&mt('No'));
1.362     raeburn   415:             my $name = $context.'_'.$item;
                    416:             if ($context eq 'requestauthor') {
                    417:                 $name = $context;
                    418:             }
1.306     raeburn   419:             $custdisp = '<span class="LC_nobreak"><label>'.
1.362     raeburn   420:                         '<input type="radio" name="'.$name.'"'.
1.361     raeburn   421:                         ' value="1" '.$tool_on.'/>'.&mt('On').'</label>&nbsp;<label>'.
1.362     raeburn   422:                         '<input type="radio" name="'.$name.'" value="0" '.
1.306     raeburn   423:                         $tool_off.'/>'.&mt('Off').'</label></span>';
                    424:             $custradio = ('&nbsp;'x2).'--'.$lt{'cusa'}.':&nbsp;'.$custdisp.
                    425:                           '</span>';
                    426:         }
                    427:         $output .= '  <td'.$colspan.'>'.$custom_access.('&nbsp;'x4).
                    428:                    $lt{'avai'}.': '.$currdisp.'</td>'."\n".
1.275     raeburn   429:                    &Apache::loncommon::end_data_table_row()."\n".
                    430:                    &Apache::loncommon::start_data_table_row()."\n".
1.306     raeburn   431:                    '  <td style="vertical-align:top;"><span class="LC_nobreak">'.
                    432:                    $lt{'chse'}.': <label>'.
1.275     raeburn   433:                    '<input type="radio" name="custom'.$item.'" value="0" '.
1.306     raeburn   434:                    $cust_off.'/>'.$lt{'usde'}.'</label>'.('&nbsp;' x3).
                    435:                    '<label><input type="radio" name="custom'.$item.'" value="1" '.
                    436:                    $cust_on.'/>'.$lt{'uscu'}.'</label>'.$custradio.'</td>'.
1.275     raeburn   437:                    &Apache::loncommon::end_data_table_row()."\n";
                    438:     }
                    439:     return $output;
                    440: }
                    441: 
1.300     raeburn   442: sub coursereq_externaluser {
                    443:     my ($ccuname,$ccdomain,$cdom) = @_;
1.306     raeburn   444:     my (@usertools,@options,%validations,%userenv,$output);
1.300     raeburn   445:     my %lt = &Apache::lonlocal::texthash (
                    446:                    'official'   => 'Can request creation of official courses',
                    447:                    'unofficial' => 'Can request creation of unofficial courses',
                    448:                    'community'  => 'Can request creation of communities',
                    449:     );
                    450: 
                    451:     %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    452:                       'reqcrsotherdom.official','reqcrsotherdom.unofficial',
                    453:                       'reqcrsotherdom.community');
                    454:     @usertools = ('official','unofficial','community');
1.309     raeburn   455:     @options = ('approval','validate','autolimit');
1.306     raeburn   456:     %validations = &Apache::lonnet::auto_courserequest_checks($cdom);
                    457:     my $optregex = join('|',@options);
                    458:     my %reqtitles = &courserequest_titles();
1.300     raeburn   459:     foreach my $item (@usertools) {
1.306     raeburn   460:         my ($curroption,$currlimit,$tooloff);
1.300     raeburn   461:         if ($userenv{'reqcrsotherdom.'.$item} ne '') {
                    462:             my @curr = split(',',$userenv{'reqcrsotherdom.'.$item});
1.314     raeburn   463:             foreach my $req (@curr) {
                    464:                 if ($req =~ /^\Q$cdom\E\:($optregex)=?(\d*)$/) {
                    465:                     $curroption = $1;
                    466:                     $currlimit = $2;
                    467:                     last;
1.306     raeburn   468:                 }
                    469:             }
1.314     raeburn   470:             if (!$curroption) {
                    471:                 $curroption = 'norequest';
                    472:                 $tooloff = ' checked="checked"';
                    473:             }
1.306     raeburn   474:         } else {
                    475:             $curroption = 'norequest';
                    476:             $tooloff = ' checked="checked"';
                    477:         }
                    478:         $output.= &Apache::loncommon::start_data_table_row()."\n".
1.314     raeburn   479:                   '  <td><span class="LC_nobreak">'.$lt{$item}.': </span></td><td>'.
                    480:                   '<table><tr><td valign="top">'."\n".
1.306     raeburn   481:                   '<label><input type="radio" name="reqcrsotherdom_'.$item.
1.314     raeburn   482:                   '" value=""'.$tooloff.' />'.$reqtitles{'norequest'}.
                    483:                   '</label></td>';
1.306     raeburn   484:         foreach my $option (@options) {
                    485:             if ($option eq 'validate') {
                    486:                 my $canvalidate = 0;
                    487:                 if (ref($validations{$item}) eq 'HASH') {
                    488:                     if ($validations{$item}{'_external_'}) {
                    489:                         $canvalidate = 1;
                    490:                     }
                    491:                 }
                    492:                 next if (!$canvalidate);
                    493:             }
                    494:             my $checked = '';
                    495:             if ($option eq $curroption) {
                    496:                 $checked = ' checked="checked"';
                    497:             }
1.314     raeburn   498:             $output .= '<td valign="top"><span class="LC_nobreak"><label>'.
1.306     raeburn   499:                        '<input type="radio" name="reqcrsotherdom_'.$item.
                    500:                        '" value="'.$option.'"'.$checked.' />'.
1.314     raeburn   501:                        $reqtitles{$option}.'</label>';
1.306     raeburn   502:             if ($option eq 'autolimit') {
1.314     raeburn   503:                 $output .= '&nbsp;<input type="text" name="reqcrsotherdom_'.
1.306     raeburn   504:                            $item.'_limit" size="1" '.
1.314     raeburn   505:                            'value="'.$currlimit.'" /></span>'.
                    506:                            '<br />'.$reqtitles{'unlimited'};
                    507:             } else {
                    508:                 $output .= '</span>';
1.300     raeburn   509:             }
1.314     raeburn   510:             $output .= '</td>';
1.300     raeburn   511:         }
1.314     raeburn   512:         $output .= '</td></tr></table></td>'."\n".
1.300     raeburn   513:                    &Apache::loncommon::end_data_table_row()."\n";
                    514:     }
                    515:     return $output;
                    516: }
                    517: 
1.362     raeburn   518: sub domainrole_req {
                    519:     my ($ccuname,$ccdomain) = @_;
                    520:     return '<br /><h3>'.
                    521:            &mt('User Can Request Assignment of Domain Roles?').
                    522:            '</h3>'."\n".
                    523:            &Apache::loncommon::start_data_table().
                    524:            &build_tools_display($ccuname,$ccdomain,
                    525:                                 'requestauthor').
                    526:            &Apache::loncommon::end_data_table();
                    527: }
                    528: 
1.306     raeburn   529: sub courserequest_titles {
                    530:     my %titles = &Apache::lonlocal::texthash (
                    531:                                    official   => 'Official',
                    532:                                    unofficial => 'Unofficial',
                    533:                                    community  => 'Communities',
                    534:                                    norequest  => 'Not allowed',
1.309     raeburn   535:                                    approval   => 'Approval by Dom. Coord.',
1.306     raeburn   536:                                    validate   => 'With validation',
                    537:                                    autolimit  => 'Numerical limit',
1.314     raeburn   538:                                    unlimited  => '(blank for unlimited)',
1.306     raeburn   539:                  );
                    540:     return %titles;
                    541: }
                    542: 
                    543: sub courserequest_display {
                    544:     my %titles = &Apache::lonlocal::texthash (
1.309     raeburn   545:                                    approval   => 'Yes, need approval',
1.306     raeburn   546:                                    validate   => 'Yes, with validation',
                    547:                                    norequest  => 'No',
                    548:    );
                    549:    return %titles;
                    550: }
                    551: 
1.362     raeburn   552: sub requestauthor_titles {
                    553:     my %titles = &Apache::lonlocal::texthash (
                    554:                                    norequest  => 'Not allowed',
                    555:                                    approval   => 'Approval by Dom. Coord.',
                    556:                                    automatic  => 'Automatic approval',
                    557:                  );
                    558:     return %titles;
                    559: 
                    560: }
                    561: 
                    562: sub requestauthor_display {
                    563:     my %titles = &Apache::lonlocal::texthash (
                    564:                                    approval   => 'Yes, need approval',
                    565:                                    automatic  => 'Yes, automatic approval',
                    566:                                    norequest  => 'No',
                    567:    );
                    568:    return %titles;
                    569: }
                    570: 
                    571: sub curr_requestauthor {
                    572:     my ($uname,$udom,$isadv,$inststatuses,$domconfig) = @_;
                    573:     return unless ((ref($inststatuses) eq 'ARRAY') && (ref($domconfig) eq 'HASH'));
                    574:     if ($uname eq '' || $udom eq '') {
                    575:         $uname = $env{'user.name'};
                    576:         $udom = $env{'user.domain'};
                    577:         $isadv = $env{'user.adv'};
                    578:     }
                    579:     my (%userenv,%settings,$val);
                    580:     my @options = ('automatic','approval');
                    581:     %userenv =
                    582:         &Apache::lonnet::userenvironment($udom,$uname,'requestauthor','inststatus');
                    583:     if ($userenv{'requestauthor'}) {
                    584:         $val = $userenv{'requestauthor'};
                    585:         @{$inststatuses} = ('_custom_');
                    586:     } else {
                    587:         my %alltasks;
                    588:         if (ref($domconfig->{'requestauthor'}) eq 'HASH') {
                    589:             %settings = %{$domconfig->{'requestauthor'}};
                    590:             if (($isadv) && ($settings{'_LC_adv'} ne '')) {
                    591:                 $val = $settings{'_LC_adv'};
                    592:                 @{$inststatuses} = ('_LC_adv_');
                    593:             } else {
                    594:                 if ($userenv{'inststatus'} ne '') {
                    595:                     @{$inststatuses} = split(',',$userenv{'inststatus'});
                    596:                 } else {
                    597:                     @{$inststatuses} = ('default');
                    598:                 }
                    599:                 foreach my $status (@{$inststatuses}) {
                    600:                     if (exists($settings{$status})) {
                    601:                         my $value = $settings{$status};
                    602:                         next unless ($value);
                    603:                         unless (exists($alltasks{$value})) {
                    604:                             if (ref($alltasks{$value}) eq 'ARRAY') {
                    605:                                 unless(grep(/^\Q$status\E$/,@{$alltasks{$value}})) {
                    606:                                     push(@{$alltasks{$value}},$status);
                    607:                                 }
                    608:                             } else {
                    609:                                 @{$alltasks{$value}} = ($status);
                    610:                             }
                    611:                         }
                    612:                     }
                    613:                 }
                    614:                 foreach my $option (@options) {
                    615:                     if ($alltasks{$option}) {
                    616:                         $val = $option;
                    617:                         last;
                    618:                     }
                    619:                 }
                    620:             }
                    621:         }
                    622:     }
                    623:     return $val;
                    624: }
                    625: 
1.2       www       626: # =================================================================== Phase one
1.1       www       627: 
1.42      matthew   628: sub print_username_entry_form {
1.351     raeburn   629:     my ($r,$context,$response,$srch,$forcenewuser,$crstype,$brcrum) = @_;
1.101     albertel  630:     my $defdom=$env{'request.role.domain'};
1.160     raeburn   631:     my $formtoset = 'crtuser';
                    632:     if (exists($env{'form.startrolename'})) {
                    633:         $formtoset = 'docustom';
                    634:         $env{'form.rolename'} = $env{'form.startrolename'};
1.207     raeburn   635:     } elsif ($env{'form.origform'} eq 'crtusername') {
                    636:         $formtoset =  $env{'form.origform'};
1.160     raeburn   637:     }
                    638: 
                    639:     my ($jsback,$elements) = &crumb_utilities();
                    640: 
                    641:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
1.165     albertel  642:         '<script type="text/javascript">'."\n".
1.301     bisitz    643:         '// <![CDATA['."\n".
                    644:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset})."\n".
                    645:         '// ]]>'."\n".
1.162     raeburn   646:         '</script>'."\n";
1.160     raeburn   647: 
1.324     raeburn   648:     my %existingroles=&Apache::lonuserutils::my_custom_roles($crstype);
                    649:     if (($env{'form.action'} eq 'custom') && (keys(%existingroles) > 0)
                    650:         && (&Apache::lonnet::allowed('mcr','/'))) {
                    651:         $jscript .= &customrole_javascript();
                    652:     }
1.224     raeburn   653:     my $helpitem = 'Course_Change_Privileges';
                    654:     if ($env{'form.action'} eq 'custom') {
                    655:         $helpitem = 'Course_Editing_Custom_Roles';
                    656:     } elsif ($env{'form.action'} eq 'singlestudent') {
                    657:         $helpitem = 'Course_Add_Student';
                    658:     }
1.351     raeburn   659:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
                    660:     if ($env{'form.action'} eq 'custom') {
                    661:         push(@{$brcrum},
                    662:                  {href=>"javascript:backPage(document.crtuser)",       
                    663:                   text=>"Pick custom role",
                    664:                   help => $helpitem,}
                    665:                  );
                    666:     } else {
                    667:         push (@{$brcrum},
                    668:                   {href => "javascript:backPage(document.crtuser)",
                    669:                    text => $breadcrumb_text{'search'},
                    670:                    help => $helpitem,
                    671:                    faq  => 282,
                    672:                    bug  => 'Instructor Interface',}
                    673:                   );
                    674:     }
                    675:     my %loaditems = (
                    676:                 'onload' => "javascript:setFormElements(document.$formtoset)",
                    677:                     );
                    678:     my $args = {bread_crumbs           => $brcrum,
                    679:                 bread_crumbs_component => 'User Management',
                    680:                 add_entries            => \%loaditems,};
                    681:     $r->print(&Apache::loncommon::start_page('User Management',$jscript,$args));
                    682: 
1.71      sakharuk  683:     my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   684:                     'srst' => 'Search for a user and enroll as a student',
1.318     raeburn   685:                     'srme' => 'Search for a user and enroll as a member',
1.229     raeburn   686:                     'srad' => 'Search for a user and modify/add user information or roles',
1.71      sakharuk  687: 		    'usr'  => "Username",
                    688:                     'dom'  => "Domain",
1.324     raeburn   689:                     'ecrp' => "Define or Edit Custom Role",
                    690:                     'nr'   => "role name",
1.282     schafran  691:                     'cre'  => "Next",
1.71      sakharuk  692: 				       );
1.351     raeburn   693: 
1.214     raeburn   694:     if ($env{'form.action'} eq 'custom') {
1.190     raeburn   695:         if (&Apache::lonnet::allowed('mcr','/')) {
1.324     raeburn   696:             my $newroletext = &mt('Define new custom role:');
                    697:             $r->print('<form action="/adm/createuser" method="post" name="docustom">'.
                    698:                       '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                    699:                       '<input type="hidden" name="phase" value="selected_custom_edit" />'.
                    700:                       '<h3>'.$lt{'ecrp'}.'</h3>'.
                    701:                       &Apache::loncommon::start_data_table().
                    702:                       &Apache::loncommon::start_data_table_row().
                    703:                       '<td>');
                    704:             if (keys(%existingroles) > 0) {
                    705:                 $r->print('<br /><label><input type="radio" name="customroleaction" value="new" checked="checked" onclick="setCustomFields();" /><b>'.$newroletext.'</b></label>');
                    706:             } else {
                    707:                 $r->print('<br /><input type="hidden" name="customroleaction" value="new" /><b>'.$newroletext.'</b>');
                    708:             }
                    709:             $r->print('</td><td align="center">'.$lt{'nr'}.'<br /><input type="text" size="15" name="newrolename" onfocus="setCustomAction('."'new'".');" /></td>'.
                    710:                       &Apache::loncommon::end_data_table_row());
                    711:             if (keys(%existingroles) > 0) {
                    712:                 $r->print(&Apache::loncommon::start_data_table_row().'<td><br />'.
                    713:                           '<label><input type="radio" name="customroleaction" value="edit" onclick="setCustomFields();"/><b>'.
                    714:                           &mt('View/Modify existing role:').'</b></label></td>'.
                    715:                           '<td align="center"><br />'.
                    716:                           '<select name="rolename" onchange="setCustomAction('."'edit'".');">'.
1.326     raeburn   717:                           '<option value="" selected="selected">'.
1.324     raeburn   718:                           &mt('Select'));
                    719:                 foreach my $role (sort(keys(%existingroles))) {
1.326     raeburn   720:                     $r->print('<option value="'.$role.'">'.$role.'</option>');
1.324     raeburn   721:                 }
                    722:                 $r->print('</select>'.
                    723:                           '</td>'.
                    724:                           &Apache::loncommon::end_data_table_row());
                    725:             }
                    726:             $r->print(&Apache::loncommon::end_data_table().'<p>'.
                    727:                       '<input name="customeditor" type="submit" value="'.
                    728:                       $lt{'cre'}.'" /></p>'.
                    729:                       '</form>');
1.190     raeburn   730:         }
1.213     raeburn   731:     } else {
1.229     raeburn   732:         my $actiontext = $lt{'srad'};
1.213     raeburn   733:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   734:             if ($crstype eq 'Community') {
                    735:                 $actiontext = $lt{'srme'};
                    736:             } else {
                    737:                 $actiontext = $lt{'srst'};
                    738:             }
1.213     raeburn   739:         }
1.324     raeburn   740:         $r->print("<h3>$actiontext</h3>");
1.213     raeburn   741:         if ($env{'form.origform'} ne 'crtusername') {
                    742:             $r->print("\n".$response);
                    743:         }
1.318     raeburn   744:         $r->print(&entry_form($defdom,$srch,$forcenewuser,$context,$response,$crstype));
1.107     www       745:     }
1.110     albertel  746: }
                    747: 
1.324     raeburn   748: sub customrole_javascript {
                    749:     my $js = <<"END";
                    750: <script type="text/javascript">
                    751: // <![CDATA[
                    752: 
                    753: function setCustomFields() {
                    754:     if (document.docustom.customroleaction.length > 0) {
                    755:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    756:             if (document.docustom.customroleaction[i].checked) {
                    757:                 if (document.docustom.customroleaction[i].value == 'new') {
                    758:                     document.docustom.rolename.selectedIndex = 0;
                    759:                 } else {
                    760:                     document.docustom.newrolename.value = '';
                    761:                 }
                    762:             }
                    763:         }
                    764:     }
                    765:     return;
                    766: }
                    767: 
                    768: function setCustomAction(caller) {
                    769:     if (document.docustom.customroleaction.length > 0) {
                    770:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    771:             if (document.docustom.customroleaction[i].value == caller) {
                    772:                 document.docustom.customroleaction[i].checked = true;
                    773:             }
                    774:         }
                    775:     }
                    776:     setCustomFields();
                    777:     return;
                    778: }
                    779: 
                    780: // ]]>
                    781: </script>
                    782: END
                    783:     return $js;
                    784: }
                    785: 
1.160     raeburn   786: sub entry_form {
1.318     raeburn   787:     my ($dom,$srch,$forcenewuser,$context,$responsemsg,$crstype) = @_;
1.229     raeburn   788:     my ($usertype,$inexact);
1.214     raeburn   789:     if (ref($srch) eq 'HASH') {
                    790:         if (($srch->{'srchin'} eq 'dom') &&
                    791:             ($srch->{'srchby'} eq 'uname') &&
                    792:             ($srch->{'srchtype'} eq 'exact') &&
                    793:             ($srch->{'srchdomain'} ne '') &&
                    794:             ($srch->{'srchterm'} ne '')) {
1.353     raeburn   795:             my (%curr_rules,%got_rules);
1.214     raeburn   796:             my ($rules,$ruleorder) =
                    797:                 &Apache::lonnet::inst_userrules($srch->{'srchdomain'},'username');
1.353     raeburn   798:             $usertype = &Apache::lonuserutils::check_usertype($srch->{'srchdomain'},$srch->{'srchterm'},$rules,\%curr_rules,\%got_rules);
1.229     raeburn   799:         } else {
                    800:             $inexact = 1;
1.214     raeburn   801:         }
1.207     raeburn   802:     }
1.214     raeburn   803:     my $cancreate =
                    804:         &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
1.160     raeburn   805:     my $userpicker = 
1.179     raeburn   806:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
1.214     raeburn   807:                                        'document.crtuser',$cancreate,$usertype);
1.160     raeburn   808:     my $srchbutton = &mt('Search');
1.229     raeburn   809:     if ($env{'form.action'} eq 'singlestudent') {
                    810:         $srchbutton = &mt('Search and Enroll');
                    811:     } elsif ($cancreate && $responsemsg ne '' && $inexact) {
                    812:         $srchbutton = &mt('Search or Add New User');
                    813:     }
1.207     raeburn   814:     my $output = <<"ENDBLOCK";
1.160     raeburn   815: <form action="/adm/createuser" method="post" name="crtuser">
1.190     raeburn   816: <input type="hidden" name="action" value="$env{'form.action'}" />
1.160     raeburn   817: <input type="hidden" name="phase" value="get_user_info" />
                    818: $userpicker
1.179     raeburn   819: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
1.160     raeburn   820: </form>
1.207     raeburn   821: ENDBLOCK
1.229     raeburn   822:     if ($env{'form.phase'} eq '') {
1.207     raeburn   823:         my $defdom=$env{'request.role.domain'};
                    824:         my $domform = &Apache::loncommon::select_dom_form($defdom,'srchdomain');
                    825:         my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   826:                   'enro' => 'Enroll one student',
1.318     raeburn   827:                   'enrm' => 'Enroll one member',
1.229     raeburn   828:                   'admo' => 'Add/modify a single user',
                    829:                   'crea' => 'create new user if required',
                    830:                   'uskn' => "username is known",
1.207     raeburn   831:                   'crnu' => 'Create a new user',
                    832:                   'usr'  => 'Username',
                    833:                   'dom'  => 'in domain',
1.229     raeburn   834:                   'enrl' => 'Enroll',
                    835:                   'cram'  => 'Create/Modify user',
1.207     raeburn   836:         );
1.229     raeburn   837:         my $sellink=&Apache::loncommon::selectstudent_link('crtusername','srchterm','srchdomain');
                    838:         my ($title,$buttontext,$showresponse);
1.318     raeburn   839:         if ($env{'form.action'} eq 'singlestudent') {
                    840:             if ($crstype eq 'Community') {
                    841:                 $title = $lt{'enrm'};
                    842:             } else {
                    843:                 $title = $lt{'enro'};
                    844:             }
1.229     raeburn   845:             $buttontext = $lt{'enrl'};
                    846:         } else {
                    847:             $title = $lt{'admo'};
                    848:             $buttontext = $lt{'cram'};
                    849:         }
                    850:         if ($cancreate) {
                    851:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'crea'}.')</span>';
                    852:         } else {
                    853:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'uskn'}.')</span>';
                    854:         }
                    855:         if ($env{'form.origform'} eq 'crtusername') {
                    856:             $showresponse = $responsemsg;
                    857:         }
1.207     raeburn   858:         $output .= <<"ENDDOCUMENT";
1.229     raeburn   859: <br />
1.207     raeburn   860: <form action="/adm/createuser" method="post" name="crtusername">
                    861: <input type="hidden" name="action" value="$env{'form.action'}" />
                    862: <input type="hidden" name="phase" value="createnewuser" />
                    863: <input type="hidden" name="srchtype" value="exact" />
1.233     raeburn   864: <input type="hidden" name="srchby" value="uname" />
1.207     raeburn   865: <input type="hidden" name="srchin" value="dom" />
                    866: <input type="hidden" name="forcenewuser" value="1" />
                    867: <input type="hidden" name="origform" value="crtusername" />
1.229     raeburn   868: <h3>$title</h3>
                    869: $showresponse
1.207     raeburn   870: <table>
                    871:  <tr>
                    872:   <td>$lt{'usr'}:</td>
                    873:   <td><input type="text" size="15" name="srchterm" /></td>
                    874:   <td>&nbsp;$lt{'dom'}:</td><td>$domform</td>
1.229     raeburn   875:   <td>&nbsp;$sellink&nbsp;</td>
                    876:   <td>&nbsp;<input name="userrole" type="submit" value="$buttontext" /></td>
1.207     raeburn   877:  </tr>
                    878: </table>
                    879: </form>
1.160     raeburn   880: ENDDOCUMENT
1.207     raeburn   881:     }
1.160     raeburn   882:     return $output;
                    883: }
1.110     albertel  884: 
                    885: sub user_modification_js {
1.113     raeburn   886:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
                    887:     
1.110     albertel  888:     return <<END;
                    889: <script type="text/javascript" language="Javascript">
1.301     bisitz    890: // <![CDATA[
1.314     raeburn   891: 
1.110     albertel  892:     $pjump_def
                    893:     $dc_setcourse_code
                    894: 
                    895:     function dateset() {
                    896:         eval("document.cu."+document.cu.pres_marker.value+
                    897:             ".value=document.cu.pres_value.value");
1.359     www       898:         modalWindow.close();
1.110     albertel  899:     }
                    900: 
1.113     raeburn   901:     $nondc_setsection_code
1.301     bisitz    902: // ]]>
1.110     albertel  903: </script>
                    904: END
1.2       www       905: }
                    906: 
                    907: # =================================================================== Phase two
1.160     raeburn   908: sub print_user_selection_page {
1.351     raeburn   909:     my ($r,$response,$srch,$srch_results,$srcharray,$context,$opener_elements,$crstype,$brcrum) = @_;
1.160     raeburn   910:     my @fields = ('username','domain','lastname','firstname','permanentemail');
                    911:     my $sortby = $env{'form.sortby'};
                    912: 
                    913:     if (!grep(/^\Q$sortby\E$/,@fields)) {
                    914:         $sortby = 'lastname';
                    915:     }
                    916: 
                    917:     my ($jsback,$elements) = &crumb_utilities();
                    918: 
                    919:     my $jscript = (<<ENDSCRIPT);
                    920: <script type="text/javascript">
1.301     bisitz    921: // <![CDATA[
1.160     raeburn   922: function pickuser(uname,udom) {
                    923:     document.usersrchform.seluname.value=uname;
                    924:     document.usersrchform.seludom.value=udom;
                    925:     document.usersrchform.phase.value="userpicked";
                    926:     document.usersrchform.submit();
                    927: }
                    928: 
                    929: $jsback
1.301     bisitz    930: // ]]>
1.160     raeburn   931: </script>
                    932: ENDSCRIPT
                    933: 
                    934:     my %lt=&Apache::lonlocal::texthash(
1.179     raeburn   935:                                        'usrch'          => "User Search to add/modify roles",
                    936:                                        'stusrch'        => "User Search to enroll student",
1.318     raeburn   937:                                        'memsrch'        => "User Search to enroll member",
1.179     raeburn   938:                                        'usel'           => "Select a user to add/modify roles",
1.318     raeburn   939:                                        'stusel'         => "Select a user to enroll as a student",
                    940:                                        'memsel'         => "Select a user to enroll as a member",
1.160     raeburn   941:                                        'username'       => "username",
                    942:                                        'domain'         => "domain",
                    943:                                        'lastname'       => "last name",
                    944:                                        'firstname'      => "first name",
                    945:                                        'permanentemail' => "permanent e-mail",
                    946:                                       );
1.302     raeburn   947:     if ($context eq 'requestcrs') {
                    948:         $r->print('<div>');
                    949:     } else {
1.318     raeburn   950:         my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.351     raeburn   951:         my $helpitem;
                    952:         if ($env{'form.action'} eq 'singleuser') {
                    953:             $helpitem = 'Course_Change_Privileges';
                    954:         } elsif ($env{'form.action'} eq 'singlestudent') {
                    955:             $helpitem = 'Course_Add_Student';
                    956:         }
                    957:         push (@{$brcrum},
                    958:                   {href => "javascript:backPage(document.usersrchform,'','')",
                    959:                    text => $breadcrumb_text{'search'},
                    960:                    faq  => 282,
                    961:                    bug  => 'Instructor Interface',},
                    962:                   {href => "javascript:backPage(document.usersrchform,'get_user_info','select')",
                    963:                    text => $breadcrumb_text{'userpicked'},
                    964:                    faq  => 282,
                    965:                    bug  => 'Instructor Interface',
                    966:                    help => $helpitem}
                    967:                   );
                    968:         $r->print(&Apache::loncommon::start_page('User Management',$jscript,{bread_crumbs => $brcrum}));
1.302     raeburn   969:         if ($env{'form.action'} eq 'singleuser') {
                    970:             $r->print("<b>$lt{'usrch'}</b><br />");
1.318     raeburn   971:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
1.302     raeburn   972:             $r->print('<h3>'.$lt{'usel'}.'</h3>');
                    973:         } elsif ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   974:             $r->print($jscript."<b>");
                    975:             if ($crstype eq 'Community') {
                    976:                 $r->print($lt{'memsrch'});
                    977:             } else {
                    978:                 $r->print($lt{'stusrch'});
                    979:             }
                    980:             $r->print("</b><br />");
                    981:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
                    982:             $r->print('</form><h3>');
                    983:             if ($crstype eq 'Community') {
                    984:                 $r->print($lt{'memsel'});
                    985:             } else {
                    986:                 $r->print($lt{'stusel'});
                    987:             }
                    988:             $r->print('</h3>');
1.302     raeburn   989:         }
1.179     raeburn   990:     }
1.160     raeburn   991:     $r->print('<form name="usersrchform" method="post">'.
                    992:               &Apache::loncommon::start_data_table()."\n".
                    993:               &Apache::loncommon::start_data_table_header_row()."\n".
                    994:               ' <th> </th>'."\n");
                    995:     foreach my $field (@fields) {
                    996:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
                    997:                   "'".$field."'".';document.usersrchform.submit();">'.
                    998:                   $lt{$field}.'</a></th>'."\n");
                    999:     }
                   1000:     $r->print(&Apache::loncommon::end_data_table_header_row());
                   1001: 
                   1002:     my @sorted_users = sort {
1.167     albertel 1003:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
1.160     raeburn  1004:             ||
1.167     albertel 1005:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
1.160     raeburn  1006:             ||
                   1007:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
1.167     albertel 1008: 	    ||
                   1009: 	lc($a) cmp lc($b)
1.160     raeburn  1010:         } (keys(%$srch_results));
                   1011: 
                   1012:     foreach my $user (@sorted_users) {
                   1013:         my ($uname,$udom) = split(/:/,$user);
1.302     raeburn  1014:         my $onclick;
                   1015:         if ($context eq 'requestcrs') {
1.314     raeburn  1016:             $onclick =
1.302     raeburn  1017:                 'onclick="javascript:gochoose('."'$uname','$udom',".
                   1018:                                                "'$srch_results->{$user}->{firstname}',".
                   1019:                                                "'$srch_results->{$user}->{lastname}',".
                   1020:                                                "'$srch_results->{$user}->{permanentemail}'".');"';
                   1021:         } else {
1.314     raeburn  1022:             $onclick =
1.302     raeburn  1023:                 ' onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".');"';
                   1024:         }
1.160     raeburn  1025:         $r->print(&Apache::loncommon::start_data_table_row().
1.302     raeburn  1026:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" '.
                   1027:                   $onclick.' /></td>'.
1.160     raeburn  1028:                   '<td><tt>'.$uname.'</tt></td>'.
                   1029:                   '<td><tt>'.$udom.'</tt></td>');
                   1030:         foreach my $field ('lastname','firstname','permanentemail') {
                   1031:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
                   1032:         }
                   1033:         $r->print(&Apache::loncommon::end_data_table_row());
                   1034:     }
                   1035:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
1.179     raeburn  1036:     if (ref($srcharray) eq 'ARRAY') {
                   1037:         foreach my $item (@{$srcharray}) {
                   1038:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
                   1039:         }
                   1040:     }
1.160     raeburn  1041:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
                   1042:               ' <input type="hidden" name="seluname" value="" />'."\n".
                   1043:               ' <input type="hidden" name="seludom" value="" />'."\n".
1.179     raeburn  1044:               ' <input type="hidden" name="currstate" value="select" />'."\n".
1.190     raeburn  1045:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
1.214     raeburn  1046:               ' <input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
1.302     raeburn  1047:     if ($context eq 'requestcrs') {
                   1048:         $r->print($opener_elements.'</form></div>');
                   1049:     } else {
1.351     raeburn  1050:         $r->print($response.'</form>');
1.302     raeburn  1051:     }
1.160     raeburn  1052: }
                   1053: 
                   1054: sub print_user_query_page {
1.351     raeburn  1055:     my ($r,$caller,$brcrum) = @_;
1.160     raeburn  1056: # FIXME - this is for a network-wide name search (similar to catalog search)
                   1057: # To use frames with similar behavior to catalog/portfolio search.
                   1058: # To be implemented. 
                   1059:     return;
                   1060: }
                   1061: 
1.42      matthew  1062: sub print_user_modification_page {
1.375     raeburn  1063:     my ($r,$ccuname,$ccdomain,$srch,$response,$context,$permission,$crstype,
                   1064:         $brcrum,$showcredits) = @_;
1.185     raeburn  1065:     if (($ccuname eq '') || ($ccdomain eq '')) {
1.215     raeburn  1066:         my $usermsg = &mt('No username and/or domain provided.');
                   1067:         $env{'form.phase'} = '';
1.351     raeburn  1068: 	&print_username_entry_form($r,$context,$usermsg,'','',$crstype,$brcrum);
1.58      www      1069:         return;
                   1070:     }
1.213     raeburn  1071:     my ($form,$formname);
                   1072:     if ($env{'form.action'} eq 'singlestudent') {
                   1073:         $form = 'document.enrollstudent';
                   1074:         $formname = 'enrollstudent';
                   1075:     } else {
                   1076:         $form = 'document.cu';
                   1077:         $formname = 'cu';
                   1078:     }
1.188     raeburn  1079:     my %abv_auth = &auth_abbrev();
1.227     raeburn  1080:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
1.185     raeburn  1081:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
                   1082:     if ($uhome eq 'no_host') {
1.215     raeburn  1083:         my $usertype;
                   1084:         my ($rules,$ruleorder) =
                   1085:             &Apache::lonnet::inst_userrules($ccdomain,'username');
                   1086:             $usertype =
1.353     raeburn  1087:                 &Apache::lonuserutils::check_usertype($ccdomain,$ccuname,$rules,
1.362     raeburn  1088:                                                       \%curr_rules,\%got_rules);
1.215     raeburn  1089:         my $cancreate =
                   1090:             &Apache::lonuserutils::can_create_user($ccdomain,$context,
                   1091:                                                    $usertype);
                   1092:         if (!$cancreate) {
1.292     bisitz   1093:             my $helplink = 'javascript:helpMenu('."'display'".')';
1.215     raeburn  1094:             my %usertypetext = (
                   1095:                 official   => 'institutional',
                   1096:                 unofficial => 'non-institutional',
                   1097:             );
                   1098:             my $response;
                   1099:             if ($env{'form.origform'} eq 'crtusername') {
1.362     raeburn  1100:                 $response = '<span class="LC_warning">'.
                   1101:                             &mt('No match found for the username [_1] in LON-CAPA domain: [_2]',
                   1102:                                 '<b>'.$ccuname.'</b>',$ccdomain).
1.215     raeburn  1103:                             '</span><br />';
                   1104:             }
1.292     bisitz   1105:             $response .= '<p class="LC_warning">'
                   1106:                         .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
                   1107:                         .' '
                   1108:                         .&mt('Please contact the [_1]helpdesk[_2] for assistance.'
                   1109:                             ,'<a href="'.$helplink.'">','</a>')
                   1110:                         .'</p><br />';
1.215     raeburn  1111:             $env{'form.phase'} = '';
1.351     raeburn  1112:             &print_username_entry_form($r,$context,$response,undef,undef,$crstype,$brcrum);
1.215     raeburn  1113:             return;
                   1114:         }
1.188     raeburn  1115:         $newuser = 1;
1.193     raeburn  1116:         my $checkhash;
                   1117:         my $checks = { 'username' => 1 };
1.196     raeburn  1118:         $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
1.193     raeburn  1119:         &Apache::loncommon::user_rule_check($checkhash,$checks,
1.196     raeburn  1120:             \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
                   1121:         if (ref($alerts{'username'}) eq 'HASH') {
                   1122:             if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
                   1123:                 my $domdesc =
1.193     raeburn  1124:                     &Apache::lonnet::domain($ccdomain,'description');
1.196     raeburn  1125:                 if ($alerts{'username'}{$ccdomain}{$ccuname}) {
                   1126:                     my $userchkmsg;
                   1127:                     if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
                   1128:                         $userchkmsg = 
                   1129:                             &Apache::loncommon::instrule_disallow_msg('username',
1.193     raeburn  1130:                                                                  $domdesc,1).
                   1131:                         &Apache::loncommon::user_rule_formats($ccdomain,
                   1132:                             $domdesc,$curr_rules{$ccdomain}{'username'},
                   1133:                             'username');
1.196     raeburn  1134:                     }
1.215     raeburn  1135:                     $env{'form.phase'} = '';
1.351     raeburn  1136:                     &print_username_entry_form($r,$context,$userchkmsg,undef,undef,$crstype,$brcrum);
1.196     raeburn  1137:                     return;
1.215     raeburn  1138:                 }
1.193     raeburn  1139:             }
1.185     raeburn  1140:         }
1.187     raeburn  1141:     } else {
1.188     raeburn  1142:         $newuser = 0;
1.185     raeburn  1143:     }
1.160     raeburn  1144:     if ($response) {
1.215     raeburn  1145:         $response = '<br />'.$response;
1.160     raeburn  1146:     }
1.149     raeburn  1147: 
1.52      matthew  1148:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.88      raeburn  1149:     my $dc_setcourse_code = '';
1.119     raeburn  1150:     my $nondc_setsection_code = '';                                        
1.112     albertel 1151:     my %loaditem;
1.114     albertel 1152: 
1.216     raeburn  1153:     my $groupslist = &Apache::lonuserutils::get_groupslist();
1.88      raeburn  1154: 
1.375     raeburn  1155:     my $js = &validation_javascript($context,$ccdomain,$pjump_def,$crstype,
1.216     raeburn  1156:                                $groupslist,$newuser,$formname,\%loaditem);
1.318     raeburn  1157:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.224     raeburn  1158:     my $helpitem = 'Course_Change_Privileges';
                   1159:     if ($env{'form.action'} eq 'singlestudent') {
                   1160:         $helpitem = 'Course_Add_Student';
                   1161:     }
1.351     raeburn  1162:     push (@{$brcrum},
                   1163:         {href => "javascript:backPage($form)",
                   1164:          text => $breadcrumb_text{'search'},
                   1165:          faq  => 282,
                   1166:          bug  => 'Instructor Interface',});
                   1167:     if ($env{'form.phase'} eq 'userpicked') {
                   1168:        push(@{$brcrum},
                   1169:               {href => "javascript:backPage($form,'get_user_info','select')",
                   1170:                text => $breadcrumb_text{'userpicked'},
                   1171:                faq  => 282,
                   1172:                bug  => 'Instructor Interface',});
                   1173:     }
                   1174:     push(@{$brcrum},
                   1175:             {href => "javascript:backPage($form,'$env{'form.phase'}','modify')",
                   1176:              text => $breadcrumb_text{'modify'},
                   1177:              faq  => 282,
                   1178:              bug  => 'Instructor Interface',
                   1179:              help => $helpitem});
                   1180:     my $args = {'add_entries'           => \%loaditem,
                   1181:                 'bread_crumbs'          => $brcrum,
                   1182:                 'bread_crumbs_component' => 'User Management'};
                   1183:     if ($env{'form.popup'}) {
                   1184:         $args->{'no_nav_bar'} = 1;
                   1185:     }
                   1186:     my $start_page =
                   1187:         &Apache::loncommon::start_page('User Management',$js,$args);
1.3       www      1188: 
1.25      matthew  1189:     my $forminfo =<<"ENDFORMINFO";
1.216     raeburn  1190: <form action="/adm/createuser" method="post" name="$formname">
1.190     raeburn  1191: <input type="hidden" name="phase" value="update_user_data" />
1.188     raeburn  1192: <input type="hidden" name="ccuname" value="$ccuname" />
                   1193: <input type="hidden" name="ccdomain" value="$ccdomain" />
1.157     albertel 1194: <input type="hidden" name="pres_value"  value="" />
                   1195: <input type="hidden" name="pres_type"   value="" />
                   1196: <input type="hidden" name="pres_marker" value="" />
1.25      matthew  1197: ENDFORMINFO
1.375     raeburn  1198:     my (%inccourses,$roledom,$defaultcredits);
1.329     raeburn  1199:     if ($context eq 'course') {
                   1200:         $inccourses{$env{'request.course.id'}}=1;
                   1201:         $roledom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.375     raeburn  1202:         if ($showcredits) {
                   1203:             $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1204:         }
1.329     raeburn  1205:     } elsif ($context eq 'author') {
                   1206:         $roledom = $env{'request.role.domain'};
                   1207:     } elsif ($context eq 'domain') {
                   1208:         foreach my $key (keys(%env)) {
                   1209:             $roledom = $env{'request.role.domain'};
                   1210:             if ($key=~/^user\.priv\.cm\.\/($roledom)\/($match_username)/) {
                   1211:                 $inccourses{$1.'_'.$2}=1;
                   1212:             }
                   1213:         }
                   1214:     } else {
                   1215:         foreach my $key (keys(%env)) {
                   1216: 	    if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
                   1217: 	        $inccourses{$1.'_'.$2}=1;
                   1218:             }
1.2       www      1219:         }
1.24      matthew  1220:     }
1.216     raeburn  1221:     if ($newuser) {
1.362     raeburn  1222:         my ($portfolioform,$domroleform);
1.267     raeburn  1223:         if ((&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) ||
                   1224:             (&Apache::lonnet::allowed('mut',$env{'request.role.domain'}))) {
                   1225:             # Current user has quota or user tools modification privileges
1.378   ! raeburn  1226:             $portfolioform = '<br />'.&user_quotas($ccuname,$ccdomain);
1.134     raeburn  1227:         }
1.362     raeburn  1228:         if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
                   1229:             $domroleform = '<br />'.&domainrole_req($ccuname,$ccdomain);
                   1230:         }
1.227     raeburn  1231:         &initialize_authen_forms($ccdomain,$formname);
1.188     raeburn  1232:         my %lt=&Apache::lonlocal::texthash(
                   1233:                 'cnu'            => 'Create New User',
1.213     raeburn  1234:                 'ast'            => 'as a student',
1.318     raeburn  1235:                 'ame'            => 'as a member',
1.188     raeburn  1236:                 'ind'            => 'in domain',
                   1237:                 'lg'             => 'Login Data',
1.190     raeburn  1238:                 'hs'             => "Home Server",
1.188     raeburn  1239:         );
1.185     raeburn  1240: 	$r->print(<<ENDTITLE);
1.110     albertel 1241: $start_page
1.160     raeburn  1242: $response
1.25      matthew  1243: $forminfo
1.31      matthew  1244: <script type="text/javascript" language="Javascript">
1.301     bisitz   1245: // <![CDATA[
1.20      harris41 1246: $loginscript
1.301     bisitz   1247: // ]]>
1.31      matthew  1248: </script>
1.20      harris41 1249: <input type='hidden' name='makeuser' value='1' />
1.216     raeburn  1250: <h2>$lt{'cnu'} "$ccuname" $lt{'ind'} $ccdomain
1.185     raeburn  1251: ENDTITLE
1.213     raeburn  1252:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1253:             if ($crstype eq 'Community') {
                   1254:                 $r->print(' ('.$lt{'ame'}.')');
                   1255:             } else {
                   1256:                 $r->print(' ('.$lt{'ast'}.')');
                   1257:             }
1.213     raeburn  1258:         }
                   1259:         $r->print('</h2>'."\n".'<div class="LC_left_float">');
1.206     raeburn  1260:         my $personal_table = 
1.210     raeburn  1261:             &personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1262:                                    $inst_results{$ccuname.':'.$ccdomain});
1.206     raeburn  1263:         $r->print($personal_table);
1.187     raeburn  1264:         my ($home_server_pick,$numlib) = 
                   1265:             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
                   1266:                                                       'default','hide');
                   1267:         if ($numlib > 1) {
                   1268:             $r->print("
1.185     raeburn  1269: <br />
1.187     raeburn  1270: $lt{'hs'}: $home_server_pick
                   1271: <br />");
                   1272:         } else {
                   1273:             $r->print($home_server_pick);
                   1274:         }
1.304     raeburn  1275:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.362     raeburn  1276:             $r->print('<br /><h3>'.
                   1277:                       &mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.304     raeburn  1278:                       &Apache::loncommon::start_data_table().
                   1279:                       &build_tools_display($ccuname,$ccdomain,
                   1280:                                            'requestcourses').
                   1281:                       &Apache::loncommon::end_data_table());
                   1282:         }
1.188     raeburn  1283:         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                   1284:                   $lt{'lg'}.'</h3>');
1.185     raeburn  1285:         my ($fixedauth,$varauth,$authmsg); 
1.193     raeburn  1286:         if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
                   1287:             my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
                   1288:             my ($rules,$ruleorder) = 
                   1289:                 &Apache::lonnet::inst_userrules($ccdomain,'username');
1.185     raeburn  1290:             if (ref($rules) eq 'HASH') {
1.193     raeburn  1291:                 if (ref($rules->{$matchedrule}) eq 'HASH') {
                   1292:                     my $authtype = $rules->{$matchedrule}{'authtype'};
1.185     raeburn  1293:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
1.190     raeburn  1294:                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.275     raeburn  1295:                     } else { 
1.193     raeburn  1296:                         my $authparm = $rules->{$matchedrule}{'authparm'};
1.273     raeburn  1297:                         $authmsg = $rules->{$matchedrule}{'authmsg'};
1.185     raeburn  1298:                         if ($authtype =~ /^krb(4|5)$/) {
                   1299:                             my $ver = $1;
                   1300:                             if ($authparm ne '') {
                   1301:                                 $fixedauth = <<"KERB"; 
                   1302: <input type="hidden" name="login" value="krb" />
                   1303: <input type="hidden" name="krbver" value="$ver" />
                   1304: <input type="hidden" name="krbarg" value="$authparm" />
                   1305: KERB
                   1306:                             }
                   1307:                         } else {
                   1308:                             $fixedauth = 
                   1309: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
1.193     raeburn  1310:                             if ($rules->{$matchedrule}{'authparmfixed'}) {
1.185     raeburn  1311:                                 $fixedauth .=    
                   1312: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
                   1313:                             } else {
1.273     raeburn  1314:                                 if ($authtype eq 'int') {
                   1315:                                     $varauth = '<br />'.
1.301     bisitz   1316: &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  1317:                                 } elsif ($authtype eq 'loc') {
                   1318:                                     $varauth = '<br />'.
                   1319: &mt('[_1] Local Authentication with argument [_2]','','<input type="text" name="'.$authtype.'arg" value="" />')."\n";
                   1320:                                 } else {
                   1321:                                     $varauth =
1.185     raeburn  1322: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
1.273     raeburn  1323:                                 }
1.185     raeburn  1324:                             }
                   1325:                         }
                   1326:                     }
                   1327:                 } else {
1.190     raeburn  1328:                     $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.185     raeburn  1329:                 }
                   1330:             }
                   1331:             if ($authmsg) {
                   1332:                 $r->print(<<ENDAUTH);
                   1333: $fixedauth
                   1334: $authmsg
                   1335: $varauth
                   1336: ENDAUTH
                   1337:             }
                   1338:         } else {
1.190     raeburn  1339:             $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc)); 
1.187     raeburn  1340:         }
1.362     raeburn  1341:         $r->print($portfolioform.$domroleform);
1.215     raeburn  1342:         if ($env{'form.action'} eq 'singlestudent') {
                   1343:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1344:                                             $permission,$crstype,$ccuname,
                   1345:                                             $ccdomain,$showcredits));
1.215     raeburn  1346:         }
                   1347:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.216     raeburn  1348:     } else { # user already exists
1.79      albertel 1349: 	my %lt=&Apache::lonlocal::texthash(
1.191     raeburn  1350:                     'cup'  => "Modify existing user: ",
1.213     raeburn  1351:                     'ens'  => "Enroll one student: ",
1.318     raeburn  1352:                     'enm'  => "Enroll one member: ",
1.72      sakharuk 1353:                     'id'   => "in domain",
                   1354: 				       );
1.26      matthew  1355: 	$r->print(<<ENDCHANGEUSER);
1.110     albertel 1356: $start_page
1.25      matthew  1357: $forminfo
1.213     raeburn  1358: <h2>
1.26      matthew  1359: ENDCHANGEUSER
1.213     raeburn  1360:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1361:             if ($crstype eq 'Community') {
                   1362:                 $r->print($lt{'enm'});
                   1363:             } else {
                   1364:                 $r->print($lt{'ens'});
                   1365:             }
1.213     raeburn  1366:         } else {
                   1367:             $r->print($lt{'cup'});
                   1368:         }
                   1369:         $r->print(' "'.$ccuname.'" '.$lt{'id'}.' "'.$ccdomain.'"</h2>'.
                   1370:                   "\n".'<div class="LC_left_float">');
1.206     raeburn  1371:         my ($personal_table,$showforceid) = 
1.210     raeburn  1372:             &personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1373:                                    $inst_results{$ccuname.':'.$ccdomain});
1.206     raeburn  1374:         $r->print($personal_table);
                   1375:         if ($showforceid) {
1.362     raeburn  1376:             $r->print('<table>'.&Apache::lonuserutils::forceid_change($context).'</table>');
1.199     raeburn  1377:         }
1.275     raeburn  1378:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.362     raeburn  1379:             $r->print('<br /><h3>'.&mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.300     raeburn  1380:                       &Apache::loncommon::start_data_table());
1.314     raeburn  1381:             if ($env{'request.role.domain'} eq $ccdomain) {
1.300     raeburn  1382:                 $r->print(&build_tools_display($ccuname,$ccdomain,'requestcourses'));
                   1383:             } else {
                   1384:                 $r->print(&coursereq_externaluser($ccuname,$ccdomain,
                   1385:                                                   $env{'request.role.domain'}));
                   1386:             }
                   1387:             $r->print(&Apache::loncommon::end_data_table());
1.275     raeburn  1388:         }
1.199     raeburn  1389:         $r->print('</div>');
1.362     raeburn  1390:         my @order = ('auth','quota','tools','requestauthor');
                   1391:         my %user_text;
                   1392:         my ($isadv,$isauthor) = 
                   1393:             &Apache::lonnet::is_advanced_user($ccuname,$ccdomain);
                   1394:         if ((!$isauthor) && 
                   1395:             (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   1396:             $user_text{'requestauthor'} = &domainrole_req($ccuname,$ccdomain);
                   1397:         }
                   1398:         $user_text{'auth'} =  &user_authentication($ccuname,$ccdomain,$formname);
1.267     raeburn  1399:         if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
                   1400:             (&Apache::lonnet::allowed('mut',$ccdomain))) {
1.188     raeburn  1401:             # Current user has quota modification privileges
1.378   ! raeburn  1402:             $user_text{'quota'} = &user_quotas($ccuname,$ccdomain);
1.267     raeburn  1403:         }
                   1404:         if (!&Apache::lonnet::allowed('mpq',$ccdomain)) {
                   1405:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
                   1406:                 my %lt=&Apache::lonlocal::texthash(
1.378   ! raeburn  1407:                     'dska'  => "Disk quotas for user's portfolio and authoring space",
        !          1408:                     'youd'  => "You do not have privileges to modify the portfolio and/or authoring space quotas for this user.",
1.267     raeburn  1409:                     'ichr'  => "If a change is required, contact a domain coordinator for the domain",
                   1410:                 );
1.362     raeburn  1411:                 $user_text{'quota'} = <<ENDNOPORTPRIV;
1.188     raeburn  1412: <h3>$lt{'dska'}</h3>
                   1413: $lt{'youd'} $lt{'ichr'}: $ccdomain
                   1414: ENDNOPORTPRIV
1.267     raeburn  1415:             }
                   1416:         }
                   1417:         if (!&Apache::lonnet::allowed('mut',$ccdomain)) {
                   1418:             if (&Apache::lonnet::allowed('mut',$env{'request.role.domain'})) {
                   1419:                 my %lt=&Apache::lonlocal::texthash(
                   1420:                     'utav'  => "User Tools Availability",
1.361     raeburn  1421:                     'yodo'  => "You do not have privileges to modify Portfolio, Blog, WebDAV, or Personal Information Page settings for this user.",
1.267     raeburn  1422:                     'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1423:                 );
1.362     raeburn  1424:                 $user_text{'tools'} = <<ENDNOTOOLSPRIV;
1.267     raeburn  1425: <h3>$lt{'utav'}</h3>
                   1426: $lt{'yodo'} $lt{'ifch'}: $ccdomain
                   1427: ENDNOTOOLSPRIV
                   1428:             }
1.188     raeburn  1429:         }
1.362     raeburn  1430:         my $gotdiv = 0; 
                   1431:         foreach my $item (@order) {
                   1432:             if ($user_text{$item} ne '') {
                   1433:                 unless ($gotdiv) {
                   1434:                     $r->print('<div class="LC_left_float">');
                   1435:                     $gotdiv = 1;
                   1436:                 }
                   1437:                 $r->print('<br />'.$user_text{$item});
                   1438:             }
                   1439:         }
                   1440:         if ($env{'form.action'} eq 'singlestudent') {
                   1441:             unless ($gotdiv) {
                   1442:                 $r->print('<div class="LC_left_float">');
1.213     raeburn  1443:             }
1.375     raeburn  1444:             my $credits;
                   1445:             if ($showcredits) {
                   1446:                 $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1447:                 if ($credits eq '') {
                   1448:                     $credits = $defaultcredits;
                   1449:                 }
                   1450:             }
1.374     raeburn  1451:             $r->print(&date_sections_select($context,$newuser,$formname,
1.375     raeburn  1452:                                             $permission,$crstype,$ccuname,
                   1453:                                             $ccdomain,$showcredits));
1.374     raeburn  1454:         }
1.362     raeburn  1455:         if ($gotdiv) {
                   1456:             $r->print('</div><div class="LC_clear_float_footer"></div>');
1.188     raeburn  1457:         }
1.217     raeburn  1458:         if ($env{'form.action'} ne 'singlestudent') {
1.329     raeburn  1459:             &display_existing_roles($r,$ccuname,$ccdomain,\%inccourses,$context,
                   1460:                                     $roledom,$crstype);
1.217     raeburn  1461:         }
1.25      matthew  1462:     } ## End of new user/old user logic
1.218     raeburn  1463:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1464:         my $btntxt;
                   1465:         if ($crstype eq 'Community') {
                   1466:             $btntxt = &mt('Enroll Member');
                   1467:         } else {
                   1468:             $btntxt = &mt('Enroll Student');
                   1469:         }
                   1470:         $r->print('<br /><input type="button" value="'.$btntxt.'" onclick="setSections(this.form)" />'."\n");
1.218     raeburn  1471:     } else {
1.375     raeburn  1472:         $r->print('<fieldset><legend>'.&mt('Add Roles').'</legend>');
1.218     raeburn  1473:         my $addrolesdisplay = 0;
                   1474:         if ($context eq 'domain' || $context eq 'author') {
                   1475:             $addrolesdisplay = &new_coauthor_roles($r,$ccuname,$ccdomain);
                   1476:         }
                   1477:         if ($context eq 'domain') {
1.357     raeburn  1478:             my $add_domainroles = &new_domain_roles($r,$ccdomain);
1.218     raeburn  1479:             if (!$addrolesdisplay) {
                   1480:                 $addrolesdisplay = $add_domainroles;
1.2       www      1481:             }
1.375     raeburn  1482:             $r->print(&course_level_dc($env{'request.role.domain'},$showcredits));
                   1483:             $r->print('</fieldset><br /><input type="button" value="'.&mt('Save').'" onclick="setCourse()" />'."\n");
1.218     raeburn  1484:         } elsif ($context eq 'author') {
                   1485:             if ($addrolesdisplay) {
1.375     raeburn  1486:                 $r->print('</fieldset><br /><input type="button" value="'.&mt('Save').'"');
1.218     raeburn  1487:                 if ($newuser) {
1.301     bisitz   1488:                     $r->print(' onclick="auth_check()" \>'."\n");
1.218     raeburn  1489:                 } else {
1.301     bisitz   1490:                     $r->print('onclick="this.form.submit()" \>'."\n");
1.218     raeburn  1491:                 }
1.188     raeburn  1492:             } else {
1.375     raeburn  1493:                 $r->print('</fieldset><br /><a href="javascript:backPage(document.cu)">'.
1.218     raeburn  1494:                           &mt('Back to previous page').'</a>');
1.188     raeburn  1495:             }
                   1496:         } else {
1.375     raeburn  1497:             $r->print(&course_level_table(\%inccourses,$showcredits,$defaultcredits));
                   1498:             $r->print('</fieldset><br /><input type="button" value="'.&mt('Save').'" onclick="setSections(this.form)" />'."\n");
1.188     raeburn  1499:         }
1.88      raeburn  1500:     }
1.188     raeburn  1501:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
1.179     raeburn  1502:     $r->print('<input type="hidden" name="currstate" value="" />');
1.352     raeburn  1503:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" /></form>');
1.218     raeburn  1504:     return;
1.2       www      1505: }
1.1       www      1506: 
1.213     raeburn  1507: sub singleuser_breadcrumb {
1.318     raeburn  1508:     my ($crstype) = @_;
1.213     raeburn  1509:     my %breadcrumb_text;
                   1510:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1511:         if ($crstype eq 'Community') {
                   1512:             $breadcrumb_text{'search'} = 'Enroll a member';
                   1513:         } else {
                   1514:             $breadcrumb_text{'search'} = 'Enroll a student';
                   1515:         }
1.213     raeburn  1516:         $breadcrumb_text{'userpicked'} = 'Select a user',
                   1517:         $breadcrumb_text{'modify'} = 'Set section/dates',
                   1518:     } else {
1.229     raeburn  1519:         $breadcrumb_text{'search'} = 'Create/modify a user';
1.213     raeburn  1520:         $breadcrumb_text{'userpicked'} = 'Select a user',
                   1521:         $breadcrumb_text{'modify'} = 'Set user role',
                   1522:     }
                   1523:     return %breadcrumb_text;
                   1524: }
                   1525: 
                   1526: sub date_sections_select {
1.375     raeburn  1527:     my ($context,$newuser,$formname,$permission,$crstype,$ccuname,$ccdomain,
                   1528:         $showcredits) = @_;
                   1529:     my $credits;
                   1530:     if ($showcredits) {
                   1531:         my $defaultcredits = &Apache::lonuserutils::get_defaultcredits();
                   1532:         $credits = &get_user_credits($ccuname,$ccdomain,$defaultcredits);
                   1533:         if ($credits eq '') {
                   1534:             $credits = $defaultcredits;
                   1535:         }
                   1536:     }
1.213     raeburn  1537:     my $cid = $env{'request.course.id'};
                   1538:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity($cid);
                   1539:     my $date_table = '<h3>'.&mt('Starting and Ending Dates').'</h3>'."\n".
                   1540:         &Apache::lonuserutils::date_setting_table(undef,undef,$context,
                   1541:                                                   undef,$formname,$permission);
                   1542:     my $rowtitle = 'Section';
1.375     raeburn  1543:     my $secbox = '<h3>'.&mt('Section and Credits').'</h3>'."\n".
1.213     raeburn  1544:         &Apache::lonuserutils::section_picker($cdom,$cnum,'st',$rowtitle,
1.375     raeburn  1545:                                               $permission,$context,'',$crstype,
                   1546:                                               $showcredits,$credits);
1.213     raeburn  1547:     my $output = $date_table.$secbox;
                   1548:     return $output;
                   1549: }
                   1550: 
1.216     raeburn  1551: sub validation_javascript {
1.375     raeburn  1552:     my ($context,$ccdomain,$pjump_def,$crstype,$groupslist,$newuser,$formname,
1.216     raeburn  1553:         $loaditem) = @_;
                   1554:     my $dc_setcourse_code = '';
                   1555:     my $nondc_setsection_code = '';
                   1556:     if ($context eq 'domain') {
                   1557:         my $dcdom = $env{'request.role.domain'};
                   1558:         $loaditem->{'onload'} = "document.cu.coursedesc.value='';";
1.227     raeburn  1559:         $dc_setcourse_code = 
                   1560:             &Apache::lonuserutils::dc_setcourse_js('cu','singleuser',$context);
1.216     raeburn  1561:     } else {
1.227     raeburn  1562:         my $checkauth; 
                   1563:         if (($newuser) || (&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1564:             $checkauth = 1;
                   1565:         }
                   1566:         if ($context eq 'course') {
                   1567:             $nondc_setsection_code =
                   1568:                 &Apache::lonuserutils::setsections_javascript($formname,$groupslist,
1.375     raeburn  1569:                                                               undef,$checkauth,
                   1570:                                                               $crstype);
1.227     raeburn  1571:         }
                   1572:         if ($checkauth) {
                   1573:             $nondc_setsection_code .= 
                   1574:                 &Apache::lonuserutils::verify_authen($formname,$context);
                   1575:         }
1.216     raeburn  1576:     }
                   1577:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                   1578:                                    $nondc_setsection_code,$groupslist);
                   1579:     my ($jsback,$elements) = &crumb_utilities();
                   1580:     $js .= "\n".
1.301     bisitz   1581:            '<script type="text/javascript">'."\n".
                   1582:            '// <![CDATA['."\n".
                   1583:            $jsback."\n".
                   1584:            '// ]]>'."\n".
                   1585:            '</script>'."\n";
1.216     raeburn  1586:     return $js;
                   1587: }
                   1588: 
1.217     raeburn  1589: sub display_existing_roles {
1.375     raeburn  1590:     my ($r,$ccuname,$ccdomain,$inccourses,$context,$roledom,$crstype,
                   1591:         $showcredits) = @_;
1.329     raeburn  1592:     my $now=time;
                   1593:     my %lt=&Apache::lonlocal::texthash(
1.217     raeburn  1594:                     'rer'  => "Existing Roles",
                   1595:                     'rev'  => "Revoke",
                   1596:                     'del'  => "Delete",
                   1597:                     'ren'  => "Re-Enable",
                   1598:                     'rol'  => "Role",
                   1599:                     'ext'  => "Extent",
1.375     raeburn  1600:                     'crd'  => "Credits",
1.217     raeburn  1601:                     'sta'  => "Start",
                   1602:                     'end'  => "End",
                   1603:                                        );
1.329     raeburn  1604:     my (%rolesdump,%roletext,%sortrole,%roleclass,%rolepriv);
                   1605:     if ($context eq 'course' || $context eq 'author') {
                   1606:         my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
                   1607:         my %roleshash = 
                   1608:             &Apache::lonnet::get_my_roles($ccuname,$ccdomain,'userroles',
                   1609:                               ['active','previous','future'],\@roles,$roledom,1);
                   1610:         foreach my $key (keys(%roleshash)) {
                   1611:             my ($start,$end) = split(':',$roleshash{$key});
                   1612:             next if ($start eq '-1' || $end eq '-1');
                   1613:             my ($rnum,$rdom,$role,$sec) = split(':',$key);
                   1614:             if ($context eq 'course') {
                   1615:                 next unless (($rnum eq $env{'course.'.$env{'request.course.id'}.'.num'})
                   1616:                              && ($rdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}));
                   1617:             } elsif ($context eq 'author') {
                   1618:                 next unless (($rnum eq $env{'user.name'}) && ($rdom eq $env{'request.role.domain'}));
                   1619:             }
                   1620:             my ($newkey,$newvalue,$newrole);
                   1621:             $newkey = '/'.$rdom.'/'.$rnum;
                   1622:             if ($sec ne '') {
                   1623:                 $newkey .= '/'.$sec;
                   1624:             }
                   1625:             $newvalue = $role;
                   1626:             if ($role =~ /^cr/) {
                   1627:                 $newrole = 'cr';
                   1628:             } else {
                   1629:                 $newrole = $role;
                   1630:             }
                   1631:             $newkey .= '_'.$newrole;
                   1632:             if ($start ne '' && $end ne '') {
                   1633:                 $newvalue .= '_'.$end.'_'.$start;
1.335     raeburn  1634:             } elsif ($end ne '') {
                   1635:                 $newvalue .= '_'.$end;
1.329     raeburn  1636:             }
                   1637:             $rolesdump{$newkey} = $newvalue;
                   1638:         }
                   1639:     } else {
1.360     raeburn  1640:         %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
1.329     raeburn  1641:     }
                   1642:     # Build up table of user roles to allow revocation and re-enabling of roles.
                   1643:     my ($tmp) = keys(%rolesdump);
                   1644:     return if ($tmp =~ /^(con_lost|error)/i);
                   1645:     foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
                   1646:                                 my $b1=join('_',(split('_',$b))[1,0]);
                   1647:                                 return $a1 cmp $b1;
                   1648:                             } keys(%rolesdump)) {
                   1649:         next if ($area =~ /^rolesdef/);
                   1650:         my $envkey=$area;
                   1651:         my $role = $rolesdump{$area};
                   1652:         my $thisrole=$area;
                   1653:         $area =~ s/\_\w\w$//;
                   1654:         my ($role_code,$role_end_time,$role_start_time) =
                   1655:             split(/_/,$role);
1.217     raeburn  1656: # Is this a custom role? Get role owner and title.
1.329     raeburn  1657:         my ($croleudom,$croleuname,$croletitle)=
                   1658:             ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
                   1659:         my $allowed=0;
                   1660:         my $delallowed=0;
                   1661:         my $sortkey=$role_code;
                   1662:         my $class='Unknown';
1.375     raeburn  1663:         my $credits='';
1.329     raeburn  1664:         if ($area =~ m{^/($match_domain)/($match_courseid)} ) {
                   1665:             $class='Course';
                   1666:             my ($coursedom,$coursedir) = ($1,$2);
                   1667:             my $cid = $1.'_'.$2;
                   1668:             # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
                   1669:             my %coursedata=
                   1670:                 &Apache::lonnet::coursedescription($cid);
                   1671:             if ($coursedir =~ /^$match_community$/) {
                   1672:                 $class='Community';
                   1673:             }
                   1674:             $sortkey.="\0$coursedom";
                   1675:             my $carea;
                   1676:             if (defined($coursedata{'description'})) {
                   1677:                 $carea=$coursedata{'description'}.
                   1678:                     '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
                   1679:     &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$coursedir,$coursedom);
                   1680:                 $sortkey.="\0".$coursedata{'description'};
                   1681:             } else {
                   1682:                 if ($class eq 'Community') {
                   1683:                     $carea=&mt('Unavailable community').': '.$area;
                   1684:                     $sortkey.="\0".&mt('Unavailable community').': '.$area;
1.217     raeburn  1685:                 } else {
                   1686:                     $carea=&mt('Unavailable course').': '.$area;
                   1687:                     $sortkey.="\0".&mt('Unavailable course').': '.$area;
                   1688:                 }
1.329     raeburn  1689:             }
                   1690:             $sortkey.="\0$coursedir";
                   1691:             $inccourses->{$cid}=1;
1.375     raeburn  1692:             if (($showcredits) && ($class eq 'Course') && ($role_code eq 'st')) {
                   1693:                 my $defaultcredits = $coursedata{'internal.defaultcredits'};
                   1694:                 $credits =
                   1695:                     &get_user_credits($ccuname,$ccdomain,$defaultcredits,
                   1696:                                       $coursedom,$coursedir);
                   1697:                 if ($credits eq '') {
                   1698:                     $credits = $defaultcredits;
                   1699:                 }
                   1700:             }
1.329     raeburn  1701:             if ((&Apache::lonnet::allowed('c'.$role_code,$coursedom.'/'.$coursedir)) ||
                   1702:                 (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1703:                 $allowed=1;
                   1704:             }
                   1705:             unless ($allowed) {
1.365     raeburn  1706:                 my $isowner = &Apache::lonuserutils::is_courseowner($cid,$coursedata{'internal.courseowner'});
1.329     raeburn  1707:                 if ($isowner) {
                   1708:                     if (($role_code eq 'co') && ($class eq 'Community')) {
                   1709:                         $allowed = 1;
                   1710:                     } elsif (($role_code eq 'cc') && ($class eq 'Course')) {
                   1711:                         $allowed = 1;
                   1712:                     }
1.217     raeburn  1713:                 }
1.329     raeburn  1714:             } 
                   1715:             if ((&Apache::lonnet::allowed('dro',$coursedom)) ||
                   1716:                 (&Apache::lonnet::allowed('dro',$ccdomain))) {
                   1717:                 $delallowed=1;
                   1718:             }
1.217     raeburn  1719: # - custom role. Needs more info, too
1.329     raeburn  1720:             if ($croletitle) {
                   1721:                 if (&Apache::lonnet::allowed('ccr',$coursedom.'/'.$coursedir)) {
                   1722:                     $allowed=1;
                   1723:                     $thisrole.='.'.$role_code;
1.217     raeburn  1724:                 }
1.329     raeburn  1725:             }
                   1726:             if ($area=~m{^/($match_domain)/($match_courseid)/(\w+)}) {
1.373     bisitz   1727:                 $carea.='<br />'.&mt('Section: [_1]',$3);
1.329     raeburn  1728:                 $sortkey.="\0$3";
                   1729:                 if (!$allowed) {
                   1730:                     if ($env{'request.course.sec'} eq $3) {
                   1731:                         if (&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2.'/'.$3)) {
                   1732:                             $allowed = 1;
1.217     raeburn  1733:                         }
                   1734:                     }
                   1735:                 }
1.329     raeburn  1736:             }
                   1737:             $area=$carea;
                   1738:         } else {
                   1739:             $sortkey.="\0".$area;
                   1740:             # Determine if current user is able to revoke privileges
                   1741:             if ($area=~m{^/($match_domain)/}) {
                   1742:                 if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
                   1743:                    (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1744:                    $allowed=1;
1.217     raeburn  1745:                 }
1.329     raeburn  1746:                 if (((&Apache::lonnet::allowed('dro',$1))  ||
                   1747:                     (&Apache::lonnet::allowed('dro',$ccdomain))) &&
                   1748:                     ($role_code ne 'dc')) {
                   1749:                     $delallowed=1;
1.217     raeburn  1750:                 }
1.329     raeburn  1751:             } else {
                   1752:                 if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
1.217     raeburn  1753:                     $allowed=1;
                   1754:                 }
                   1755:             }
1.363     raeburn  1756:             if ($role_code eq 'ca' || $role_code eq 'au' || $role_code eq 'aa') {
1.377     raeburn  1757:                 $class='Authoring Space';
1.329     raeburn  1758:             } elsif ($role_code eq 'su') {
                   1759:                 $class='System';
1.217     raeburn  1760:             } else {
1.329     raeburn  1761:                 $class='Domain';
1.217     raeburn  1762:             }
1.329     raeburn  1763:         }
                   1764:         if (($role_code eq 'ca') || ($role_code eq 'aa')) {
                   1765:             $area=~m{/($match_domain)/($match_username)};
                   1766:             if (&Apache::lonuserutils::authorpriv($2,$1)) {
                   1767:                 $allowed=1;
1.217     raeburn  1768:             } else {
1.329     raeburn  1769:                 $allowed=0;
1.217     raeburn  1770:             }
1.329     raeburn  1771:         }
                   1772:         my $row = '';
                   1773:         $row.= '<td>';
                   1774:         my $active=1;
                   1775:         $active=0 if (($role_end_time) && ($now>$role_end_time));
                   1776:         if (($active) && ($allowed)) {
                   1777:             $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
                   1778:         } else {
                   1779:             if ($active) {
                   1780:                $row.='&nbsp;';
1.217     raeburn  1781:             } else {
1.329     raeburn  1782:                $row.=&mt('expired or revoked');
1.217     raeburn  1783:             }
1.329     raeburn  1784:         }
                   1785:         $row.='</td><td>';
                   1786:         if ($allowed && !$active) {
                   1787:             $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
                   1788:         } else {
                   1789:             $row.='&nbsp;';
                   1790:         }
                   1791:         $row.='</td><td>';
                   1792:         if ($delallowed) {
                   1793:             $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
                   1794:         } else {
                   1795:             $row.='&nbsp;';
                   1796:         }
                   1797:         my $plaintext='';
                   1798:         if (!$croletitle) {
1.375     raeburn  1799:             $plaintext=&Apache::lonnet::plaintext($role_code,$class);
                   1800:             if (($showcredits) && ($credits ne '')) {
                   1801:                 $plaintext .= '<br/ ><span class="LC_nobreak">'.
                   1802:                               '<span class="LC_fontsize_small">'.
                   1803:                               &mt('Credits: [_1]',$credits).
                   1804:                               '</span></span>';
                   1805:             }
1.329     raeburn  1806:         } else {
                   1807:             $plaintext=
1.346     bisitz   1808:                 &mt('Customrole [_1][_2]defined by [_3]',
                   1809:                         '"'.$croletitle.'"',
                   1810:                         '<br />',
                   1811:                         $croleuname.':'.$croleudom);
1.329     raeburn  1812:         }
                   1813:         $row.= '</td><td>'.$plaintext.
                   1814:                '</td><td>'.$area.
                   1815:                '</td><td>'.($role_start_time?&Apache::lonlocal::locallocaltime($role_start_time)
                   1816:                                             : '&nbsp;' ).
                   1817:                '</td><td>'.($role_end_time  ?&Apache::lonlocal::locallocaltime($role_end_time)
                   1818:                                             : '&nbsp;' )
                   1819:                ."</td>";
                   1820:         $sortrole{$sortkey}=$envkey;
                   1821:         $roletext{$envkey}=$row;
                   1822:         $roleclass{$envkey}=$class;
                   1823:         $rolepriv{$envkey}=$allowed;
                   1824:     } # end of foreach        (table building loop)
                   1825: 
                   1826:     my $rolesdisplay = 0;
                   1827:     my %output = ();
1.377     raeburn  1828:     foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  1829:         $output{$type} = '';
                   1830:         foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                   1831:             if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) {
                   1832:                  $output{$type}.=
                   1833:                       &Apache::loncommon::start_data_table_row().
                   1834:                       $roletext{$sortrole{$which}}.
                   1835:                       &Apache::loncommon::end_data_table_row();
1.217     raeburn  1836:             }
1.329     raeburn  1837:         }
                   1838:         unless($output{$type} eq '') {
                   1839:             $output{$type} = '<tr class="LC_info_row">'.
                   1840:                       "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
                   1841:                       $output{$type};
                   1842:             $rolesdisplay = 1;
                   1843:         }
                   1844:     }
                   1845:     if ($rolesdisplay == 1) {
                   1846:         my $contextrole='';
                   1847:         if ($env{'request.course.id'}) {
                   1848:             if (&Apache::loncommon::course_type() eq 'Community') {
                   1849:                 $contextrole = &mt('Existing Roles in this Community');
1.290     bisitz   1850:             } else {
1.329     raeburn  1851:                 $contextrole = &mt('Existing Roles in this Course');
1.290     bisitz   1852:             }
1.329     raeburn  1853:         } elsif ($env{'request.role'} =~ /^au\./) {
1.377     raeburn  1854:             $contextrole = &mt('Existing Co-Author Roles in your Authoring Space');
1.329     raeburn  1855:         } else {
                   1856:             $contextrole = &mt('Existing Roles in this Domain');
                   1857:         }
1.375     raeburn  1858:         $r->print('<div>'.
                   1859: '<fieldset><legend>'.$contextrole.'</legend>'.
1.217     raeburn  1860: &Apache::loncommon::start_data_table("LC_createuser").
                   1861: &Apache::loncommon::start_data_table_header_row().
                   1862: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}.
                   1863: '</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.
                   1864: '</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
                   1865: &Apache::loncommon::end_data_table_header_row());
1.377     raeburn  1866:         foreach my $type ('Authoring Space','Course','Community','Domain','System','Unknown') {
1.329     raeburn  1867:             if ($output{$type}) {
                   1868:                 $r->print($output{$type}."\n");
1.217     raeburn  1869:             }
                   1870:         }
1.375     raeburn  1871:         $r->print(&Apache::loncommon::end_data_table().
                   1872:                   '</fieldset></div>');
1.329     raeburn  1873:     }
1.217     raeburn  1874:     return;
                   1875: }
                   1876: 
1.218     raeburn  1877: sub new_coauthor_roles {
                   1878:     my ($r,$ccuname,$ccdomain) = @_;
                   1879:     my $addrolesdisplay = 0;
                   1880:     #
                   1881:     # Co-Author
                   1882:     #
                   1883:     if (&Apache::lonuserutils::authorpriv($env{'user.name'},
                   1884:                                           $env{'request.role.domain'}) &&
                   1885:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
                   1886:         # No sense in assigning co-author role to yourself
                   1887:         $addrolesdisplay = 1;
                   1888:         my $cuname=$env{'user.name'};
                   1889:         my $cudom=$env{'request.role.domain'};
                   1890:         my %lt=&Apache::lonlocal::texthash(
1.377     raeburn  1891:                     'cs'   => "Authoring Space",
1.218     raeburn  1892:                     'act'  => "Activate",
                   1893:                     'rol'  => "Role",
                   1894:                     'ext'  => "Extent",
                   1895:                     'sta'  => "Start",
                   1896:                     'end'  => "End",
                   1897:                     'cau'  => "Co-Author",
                   1898:                     'caa'  => "Assistant Co-Author",
                   1899:                     'ssd'  => "Set Start Date",
                   1900:                     'sed'  => "Set End Date"
                   1901:                                        );
                   1902:         $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n".
                   1903:                   &Apache::loncommon::start_data_table()."\n".
                   1904:                   &Apache::loncommon::start_data_table_header_row()."\n".
                   1905:                   '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
                   1906:                   '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
                   1907:                   '<th>'.$lt{'end'}.'</th>'."\n".
                   1908:                   &Apache::loncommon::end_data_table_header_row()."\n".
                   1909:                   &Apache::loncommon::start_data_table_row().'
                   1910:            <td>
1.291     bisitz   1911:             <input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_ca" />
1.218     raeburn  1912:            </td>
                   1913:            <td>'.$lt{'cau'}.'</td>
                   1914:            <td>'.$cudom.'_'.$cuname.'</td>
                   1915:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
                   1916:              <a href=
                   1917: "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>
                   1918: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
                   1919: <a href=
                   1920: "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".
                   1921:               &Apache::loncommon::end_data_table_row()."\n".
                   1922:               &Apache::loncommon::start_data_table_row()."\n".
1.291     bisitz   1923: '<td><input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
1.218     raeburn  1924: <td>'.$lt{'caa'}.'</td>
                   1925: <td>'.$cudom.'_'.$cuname.'</td>
                   1926: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
                   1927: <a href=
                   1928: "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>
                   1929: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
                   1930: <a href=
                   1931: "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".
                   1932:              &Apache::loncommon::end_data_table_row()."\n".
                   1933:              &Apache::loncommon::end_data_table());
                   1934:     } elsif ($env{'request.role'} =~ /^au\./) {
                   1935:         if (!(&Apache::lonuserutils::authorpriv($env{'user.name'},
                   1936:                                                 $env{'request.role.domain'}))) {
                   1937:             $r->print('<span class="LC_error">'.
                   1938:                       &mt('You do not have privileges to assign co-author roles.').
                   1939:                       '</span>');
                   1940:         } elsif (($env{'user.name'} eq $ccuname) &&
                   1941:              ($env{'user.domain'} eq $ccdomain)) {
1.377     raeburn  1942:             $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  1943:         }
                   1944:     }
                   1945:     return $addrolesdisplay;;
                   1946: }
                   1947: 
                   1948: sub new_domain_roles {
1.357     raeburn  1949:     my ($r,$ccdomain) = @_;
1.218     raeburn  1950:     my $addrolesdisplay = 0;
                   1951:     #
                   1952:     # Domain level
                   1953:     #
                   1954:     my $num_domain_level = 0;
                   1955:     my $domaintext =
                   1956:     '<h4>'.&mt('Domain Level').'</h4>'.
                   1957:     &Apache::loncommon::start_data_table().
                   1958:     &Apache::loncommon::start_data_table_header_row().
                   1959:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
                   1960:     &mt('Extent').'</th>'.
                   1961:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
                   1962:     &Apache::loncommon::end_data_table_header_row();
1.312     raeburn  1963:     my @allroles = &Apache::lonuserutils::roles_by_context('domain');
1.218     raeburn  1964:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
1.312     raeburn  1965:         foreach my $role (@allroles) {
                   1966:             next if ($role eq 'ad');
1.357     raeburn  1967:             next if (($role eq 'au') && ($ccdomain ne $thisdomain));
1.218     raeburn  1968:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
                   1969:                my $plrole=&Apache::lonnet::plaintext($role);
                   1970:                my %lt=&Apache::lonlocal::texthash(
                   1971:                     'ssd'  => "Set Start Date",
                   1972:                     'sed'  => "Set End Date"
                   1973:                                        );
                   1974:                $num_domain_level ++;
                   1975:                $domaintext .=
                   1976: &Apache::loncommon::start_data_table_row().
1.291     bisitz   1977: '<td><input type="checkbox" name="act_'.$thisdomain.'_'.$role.'" /></td>
1.218     raeburn  1978: <td>'.$plrole.'</td>
                   1979: <td>'.$thisdomain.'</td>
                   1980: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
                   1981: <a href=
                   1982: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
                   1983: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
                   1984: <a href=
                   1985: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
                   1986: &Apache::loncommon::end_data_table_row();
                   1987:             }
                   1988:         }
                   1989:     }
                   1990:     $domaintext.= &Apache::loncommon::end_data_table();
                   1991:     if ($num_domain_level > 0) {
                   1992:         $r->print($domaintext);
                   1993:         $addrolesdisplay = 1;
                   1994:     }
                   1995:     return $addrolesdisplay;
                   1996: }
                   1997: 
1.188     raeburn  1998: sub user_authentication {
1.227     raeburn  1999:     my ($ccuname,$ccdomain,$formname) = @_;
1.188     raeburn  2000:     my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
1.227     raeburn  2001:     my $outcome;
1.188     raeburn  2002:     # Check for a bad authentication type
                   2003:     if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth):/) {
                   2004:         # bad authentication scheme
                   2005:         my %lt=&Apache::lonlocal::texthash(
                   2006:                        'err'   => "ERROR",
                   2007:                        'uuas'  => "This user has an unrecognized authentication scheme",
                   2008:                        'adcs'  => "Please alert a domain coordinator of this situation",
                   2009:                        'sldb'  => "Please specify login data below",
                   2010:                        'ld'    => "Login Data"
                   2011:         );
                   2012:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
1.227     raeburn  2013:             &initialize_authen_forms($ccdomain,$formname);
                   2014: 
1.190     raeburn  2015:             my $choices = &Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc);
1.188     raeburn  2016:             $outcome = <<ENDBADAUTH;
                   2017: <script type="text/javascript" language="Javascript">
1.301     bisitz   2018: // <![CDATA[
1.188     raeburn  2019: $loginscript
1.301     bisitz   2020: // ]]>
1.188     raeburn  2021: </script>
                   2022: <span class="LC_error">$lt{'err'}:
                   2023: $lt{'uuas'} ($currentauth). $lt{'sldb'}.</span>
                   2024: <h3>$lt{'ld'}</h3>
                   2025: $choices
                   2026: ENDBADAUTH
                   2027:         } else {
                   2028:             # This user is not allowed to modify the user's
                   2029:             # authentication scheme, so just notify them of the problem
                   2030:             $outcome = <<ENDBADAUTH;
                   2031: <span class="LC_error"> $lt{'err'}: 
                   2032: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
                   2033: </span>
                   2034: ENDBADAUTH
                   2035:         }
                   2036:     } else { # Authentication type is valid
1.227     raeburn  2037:         &initialize_authen_forms($ccdomain,$formname,$currentauth,'modifyuser');
1.205     raeburn  2038:         my ($authformcurrent,$can_modify,@authform_others) =
1.188     raeburn  2039:             &modify_login_block($ccdomain,$currentauth);
                   2040:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   2041:             # Current user has login modification privileges
                   2042:             my %lt=&Apache::lonlocal::texthash (
                   2043:                            'ld'    => "Login Data",
                   2044:                            'ccld'  => "Change Current Login Data",
                   2045:                            'enld'  => "Enter New Login Data"
                   2046:                                                );
                   2047:             $outcome =
                   2048:                        '<script type="text/javascript" language="Javascript">'."\n".
1.301     bisitz   2049:                        '// <![CDATA['."\n".
1.188     raeburn  2050:                        $loginscript."\n".
1.301     bisitz   2051:                        '// ]]>'."\n".
1.188     raeburn  2052:                        '</script>'."\n".
                   2053:                        '<h3>'.$lt{'ld'}.'</h3>'.
                   2054:                        &Apache::loncommon::start_data_table().
1.205     raeburn  2055:                        &Apache::loncommon::start_data_table_row().
1.188     raeburn  2056:                        '<td>'.$authformnop;
                   2057:             if ($can_modify) {
                   2058:                 $outcome .= '</td>'."\n".
                   2059:                             &Apache::loncommon::end_data_table_row().
                   2060:                             &Apache::loncommon::start_data_table_row().
                   2061:                             '<td>'.$authformcurrent.'</td>'.
                   2062:                             &Apache::loncommon::end_data_table_row()."\n";
                   2063:             } else {
1.200     raeburn  2064:                 $outcome .= '&nbsp;('.$authformcurrent.')</td>'.
                   2065:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  2066:             }
1.205     raeburn  2067:             foreach my $item (@authform_others) { 
                   2068:                 $outcome .= &Apache::loncommon::start_data_table_row().
                   2069:                             '<td>'.$item.'</td>'.
                   2070:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  2071:             }
1.205     raeburn  2072:             $outcome .= &Apache::loncommon::end_data_table();
1.188     raeburn  2073:         } else {
                   2074:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
                   2075:                 my %lt=&Apache::lonlocal::texthash(
                   2076:                            'ccld'  => "Change Current Login Data",
                   2077:                            'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
                   2078:                            'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   2079:                 );
                   2080:                 $outcome .= <<ENDNOPRIV;
                   2081: <h3>$lt{'ccld'}</h3>
                   2082: $lt{'yodo'} $lt{'ifch'}: $ccdomain
1.235     raeburn  2083: <input type="hidden" name="login" value="nochange" />
1.188     raeburn  2084: ENDNOPRIV
                   2085:             }
                   2086:         }
                   2087:     }  ## End of "check for bad authentication type" logic
                   2088:     return $outcome;
                   2089: }
                   2090: 
1.187     raeburn  2091: sub modify_login_block {
                   2092:     my ($dom,$currentauth) = @_;
                   2093:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   2094:     my ($authnum,%can_assign) =
                   2095:         &Apache::loncommon::get_assignable_auth($dom);
1.205     raeburn  2096:     my ($authformcurrent,@authform_others,$show_override_msg);
1.187     raeburn  2097:     if ($currentauth=~/^krb(4|5):/) {
                   2098:         $authformcurrent=$authformkrb;
                   2099:         if ($can_assign{'int'}) {
1.205     raeburn  2100:             push(@authform_others,$authformint);
1.187     raeburn  2101:         }
                   2102:         if ($can_assign{'loc'}) {
1.205     raeburn  2103:             push(@authform_others,$authformloc);
1.187     raeburn  2104:         }
                   2105:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   2106:             $show_override_msg = 1;
                   2107:         }
                   2108:     } elsif ($currentauth=~/^internal:/) {
                   2109:         $authformcurrent=$authformint;
                   2110:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2111:             push(@authform_others,$authformkrb);
1.187     raeburn  2112:         }
                   2113:         if ($can_assign{'loc'}) {
1.205     raeburn  2114:             push(@authform_others,$authformloc);
1.187     raeburn  2115:         }
                   2116:         if ($can_assign{'int'}) {
                   2117:             $show_override_msg = 1;
                   2118:         }
                   2119:     } elsif ($currentauth=~/^unix:/) {
                   2120:         $authformcurrent=$authformfsys;
                   2121:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  2122:             push(@authform_others,$authformkrb);
1.187     raeburn  2123:         }
                   2124:         if ($can_assign{'int'}) {
1.205     raeburn  2125:             push(@authform_others,$authformint);
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{'fsys'}) {
                   2131:             $show_override_msg = 1;
                   2132:         }
                   2133:     } elsif ($currentauth=~/^localauth:/) {
                   2134:         $authformcurrent=$authformloc;
                   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'}) {
                   2142:             $show_override_msg = 1;
                   2143:         }
                   2144:     }
                   2145:     if ($show_override_msg) {
1.205     raeburn  2146:         $authformcurrent = '<table><tr><td colspan="3">'.$authformcurrent.
                   2147:                            '</td></tr>'."\n".
                   2148:                            '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
                   2149:                            '<td><b>'.&mt('Currently in use').'</b></td>'.
                   2150:                            '<td align="right"><span class="LC_cusr_emph">'.
1.187     raeburn  2151:                             &mt('will override current values').
1.205     raeburn  2152:                             '</span></td></tr></table>';
1.187     raeburn  2153:     }
1.205     raeburn  2154:     return ($authformcurrent,$show_override_msg,@authform_others); 
1.187     raeburn  2155: }
                   2156: 
1.188     raeburn  2157: sub personal_data_display {
1.252     raeburn  2158:     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$rolesarray) = @_;
1.286     raeburn  2159:     my ($output,$showforceid,%userenv,%canmodify,%canmodify_status);
1.219     raeburn  2160:     my @userinfo = ('firstname','middlename','lastname','generation',
                   2161:                     'permanentemail','id');
1.252     raeburn  2162:     my $rowcount = 0;
                   2163:     my $editable = 0;
1.286     raeburn  2164:     %canmodify_status = 
                   2165:         &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
                   2166:                                                    ['inststatus'],$rolesarray);
1.253     raeburn  2167:     if (!$newuser) {
1.188     raeburn  2168:         # Get the users information
                   2169:         %userenv = &Apache::lonnet::get('environment',
                   2170:                    ['firstname','middlename','lastname','generation',
1.286     raeburn  2171:                     'permanentemail','id','inststatus'],$ccdomain,$ccuname);
1.219     raeburn  2172:         %canmodify =
                   2173:             &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
1.252     raeburn  2174:                                                        \@userinfo,$rolesarray);
1.257     raeburn  2175:     } elsif ($context eq 'selfcreate') {
                   2176:         %canmodify = &selfcreate_canmodify($context,$ccdomain,\@userinfo,
                   2177:                                            $inst_results,$rolesarray);
1.188     raeburn  2178:     }
                   2179:     my %lt=&Apache::lonlocal::texthash(
                   2180:                 'pd'             => "Personal Data",
                   2181:                 'firstname'      => "First Name",
                   2182:                 'middlename'     => "Middle Name",
                   2183:                 'lastname'       => "Last Name",
                   2184:                 'generation'     => "Generation",
                   2185:                 'permanentemail' => "Permanent e-mail address",
1.259     bisitz   2186:                 'id'             => "Student/Employee ID",
1.286     raeburn  2187:                 'lg'             => "Login Data",
                   2188:                 'inststatus'     => "Affiliation",
1.188     raeburn  2189:     );
                   2190:     my %textboxsize = (
                   2191:                        firstname      => '15',
                   2192:                        middlename     => '15',
                   2193:                        lastname       => '15',
                   2194:                        generation     => '5',
                   2195:                        permanentemail => '25',
                   2196:                        id             => '15',
                   2197:                       );
                   2198:     my $genhelp=&Apache::loncommon::help_open_topic('Generation');
                   2199:     $output = '<h3>'.$lt{'pd'}.'</h3>'.
                   2200:               &Apache::lonhtmlcommon::start_pick_box();
                   2201:     foreach my $item (@userinfo) {
                   2202:         my $rowtitle = $lt{$item};
1.252     raeburn  2203:         my $hiderow = 0;
1.188     raeburn  2204:         if ($item eq 'generation') {
                   2205:             $rowtitle = $genhelp.$rowtitle;
                   2206:         }
1.252     raeburn  2207:         my $row = &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
1.188     raeburn  2208:         if ($newuser) {
1.210     raeburn  2209:             if (ref($inst_results) eq 'HASH') {
                   2210:                 if ($inst_results->{$item} ne '') {
1.252     raeburn  2211:                     $row .= '<input type="hidden" name="c'.$item.'" value="'.$inst_results->{$item}.'" />'.$inst_results->{$item};
1.210     raeburn  2212:                 } else {
1.252     raeburn  2213:                     if ($context eq 'selfcreate') {
                   2214:                         if ($canmodify{$item}) { 
                   2215:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                   2216:                             $editable ++;
                   2217:                         } else {
                   2218:                             $hiderow = 1;
                   2219:                         }
1.253     raeburn  2220:                     } else {
                   2221:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2222:                     }
1.210     raeburn  2223:                 }
1.188     raeburn  2224:             } else {
1.252     raeburn  2225:                 if ($context eq 'selfcreate') {
1.287     raeburn  2226:                     if (($item eq 'permanentemail') && ($newuser eq 'email')) {
                   2227:                         $row .= $ccuname;
1.252     raeburn  2228:                     } else {
1.287     raeburn  2229:                         if ($canmodify{$item}) {
                   2230:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                   2231:                             $editable ++;
                   2232:                         } else {
                   2233:                             $hiderow = 1;
                   2234:                         }
1.252     raeburn  2235:                     }
1.253     raeburn  2236:                 } else {
                   2237:                     $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2238:                 }
1.188     raeburn  2239:             }
                   2240:         } else {
1.219     raeburn  2241:             if ($canmodify{$item}) {
1.252     raeburn  2242:                 $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
1.188     raeburn  2243:             } else {
1.252     raeburn  2244:                 $row .= $userenv{$item};
1.188     raeburn  2245:             }
1.206     raeburn  2246:             if ($item eq 'id') {
1.219     raeburn  2247:                 $showforceid = $canmodify{$item};
                   2248:             }
1.188     raeburn  2249:         }
1.252     raeburn  2250:         $row .= &Apache::lonhtmlcommon::row_closure(1);
                   2251:         if (!$hiderow) {
                   2252:             $output .= $row;
                   2253:             $rowcount ++;
                   2254:         }
1.188     raeburn  2255:     }
1.286     raeburn  2256:     if (($canmodify_status{'inststatus'}) || ($context ne 'selfcreate')) {
                   2257:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($ccdomain);
                   2258:         if (ref($types) eq 'ARRAY') {
                   2259:             if (@{$types} > 0) {
                   2260:                 my ($hiderow,$shown);
                   2261:                 if ($canmodify_status{'inststatus'}) {
                   2262:                     $shown = &pick_inst_statuses($userenv{'inststatus'},$usertypes,$types);
                   2263:                 } else {
                   2264:                     if ($userenv{'inststatus'} eq '') {
                   2265:                         $hiderow = 1;
1.334     raeburn  2266:                     } else {
                   2267:                         my @showitems;
                   2268:                         foreach my $item ( map { &unescape($_); } split(':',$userenv{'inststatus'})) {
                   2269:                             if (exists($usertypes->{$item})) {
                   2270:                                 push(@showitems,$usertypes->{$item});
                   2271:                             } else {
                   2272:                                 push(@showitems,$item);
                   2273:                             }
                   2274:                         }
                   2275:                         if (@showitems) {
                   2276:                             $shown = join(', ',@showitems);
                   2277:                         } else {
                   2278:                             $hiderow = 1;
                   2279:                         }
1.286     raeburn  2280:                     }
                   2281:                 }
                   2282:                 if (!$hiderow) {
                   2283:                     my $row = &Apache::lonhtmlcommon::row_title(&mt('Affliations'),undef,'LC_oddrow_value')."\n".
                   2284:                               $shown.&Apache::lonhtmlcommon::row_closure(1); 
                   2285:                     if ($context eq 'selfcreate') {
                   2286:                         $rowcount ++;
                   2287:                     }
                   2288:                     $output .= $row;
                   2289:                 }
                   2290:             }
                   2291:         }
                   2292:     }
1.188     raeburn  2293:     $output .= &Apache::lonhtmlcommon::end_pick_box();
1.206     raeburn  2294:     if (wantarray) {
1.252     raeburn  2295:         if ($context eq 'selfcreate') {
                   2296:             return($output,$rowcount,$editable);
                   2297:         } else {
                   2298:             return ($output,$showforceid);
                   2299:         }
1.206     raeburn  2300:     } else {
                   2301:         return $output;
                   2302:     }
1.188     raeburn  2303: }
                   2304: 
1.286     raeburn  2305: sub pick_inst_statuses {
                   2306:     my ($curr,$usertypes,$types) = @_;
                   2307:     my ($output,$rem,@currtypes);
                   2308:     if ($curr ne '') {
                   2309:         @currtypes = map { &unescape($_); } split(/:/,$curr);
                   2310:     }
                   2311:     my $numinrow = 2;
                   2312:     if (ref($types) eq 'ARRAY') {
                   2313:         $output = '<table>';
                   2314:         my $lastcolspan; 
                   2315:         for (my $i=0; $i<@{$types}; $i++) {
                   2316:             if (defined($usertypes->{$types->[$i]})) {
                   2317:                 my $rem = $i%($numinrow);
                   2318:                 if ($rem == 0) {
                   2319:                     if ($i<@{$types}-1) {
                   2320:                         if ($i > 0) { 
                   2321:                             $output .= '</tr>';
                   2322:                         }
                   2323:                         $output .= '<tr>';
                   2324:                     }
                   2325:                 } elsif ($i==@{$types}-1) {
                   2326:                     my $colsleft = $numinrow - $rem;
                   2327:                     if ($colsleft > 1) {
                   2328:                         $lastcolspan = ' colspan="'.$colsleft.'"';
                   2329:                     }
                   2330:                 }
                   2331:                 my $check = ' ';
                   2332:                 if (grep(/^\Q$types->[$i]\E$/,@currtypes)) {
                   2333:                     $check = ' checked="checked" ';
                   2334:                 }
                   2335:                 $output .= '<td class="LC_left_item"'.$lastcolspan.'>'.
                   2336:                            '<span class="LC_nobreak"><label>'.
                   2337:                            '<input type="checkbox" name="inststatus" '.
                   2338:                            'value="'.$types->[$i].'"'.$check.'/>'.
                   2339:                            $usertypes->{$types->[$i]}.'</label></span></td>';
                   2340:             }
                   2341:         }
                   2342:         $output .= '</tr></table>';
                   2343:     }
                   2344:     return $output;
                   2345: }
                   2346: 
1.257     raeburn  2347: sub selfcreate_canmodify {
                   2348:     my ($context,$dom,$userinfo,$inst_results,$rolesarray) = @_;
                   2349:     if (ref($inst_results) eq 'HASH') {
                   2350:         my @inststatuses = &get_inststatuses($inst_results);
                   2351:         if (@inststatuses == 0) {
                   2352:             @inststatuses = ('default');
                   2353:         }
                   2354:         $rolesarray = \@inststatuses;
                   2355:     }
                   2356:     my %canmodify =
                   2357:         &Apache::lonuserutils::can_modify_userinfo($context,$dom,$userinfo,
                   2358:                                                    $rolesarray);
                   2359:     return %canmodify;
                   2360: }
                   2361: 
1.252     raeburn  2362: sub get_inststatuses {
                   2363:     my ($insthashref) = @_;
                   2364:     my @inststatuses = ();
                   2365:     if (ref($insthashref) eq 'HASH') {
                   2366:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
                   2367:             @inststatuses = @{$insthashref->{'inststatus'}};
                   2368:         }
                   2369:     }
                   2370:     return @inststatuses;
                   2371: }
                   2372: 
1.4       www      2373: # ================================================================= Phase Three
1.42      matthew  2374: sub update_user_data {
1.375     raeburn  2375:     my ($r,$context,$crstype,$brcrum,$showcredits) = @_; 
1.101     albertel 2376:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                   2377:                                           $env{'form.ccdomain'});
1.27      matthew  2378:     # Error messages
1.188     raeburn  2379:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
1.193     raeburn  2380:     my $end       = '</span><br /><br />';
                   2381:     my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
1.188     raeburn  2382:                     "'$env{'form.prevphase'}','modify')".'" />'.
1.219     raeburn  2383:                     &mt('Return to previous page').'</a>'.
                   2384:                     &Apache::loncommon::end_page();
                   2385:     my $now = time;
1.40      www      2386:     my $title;
1.101     albertel 2387:     if (exists($env{'form.makeuser'})) {
1.40      www      2388: 	$title='Set Privileges for New User';
                   2389:     } else {
                   2390:         $title='Modify User Privileges';
                   2391:     }
1.213     raeburn  2392:     my $newuser = 0;
1.160     raeburn  2393:     my ($jsback,$elements) = &crumb_utilities();
                   2394:     my $jscript = '<script type="text/javascript">'."\n".
1.301     bisitz   2395:                   '// <![CDATA['."\n".
                   2396:                   $jsback."\n".
                   2397:                   '// ]]>'."\n".
                   2398:                   '</script>'."\n";
1.318     raeburn  2399:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.351     raeburn  2400:     push (@{$brcrum},
                   2401:              {href => "javascript:backPage(document.userupdate)",
                   2402:               text => $breadcrumb_text{'search'},
                   2403:               faq  => 282,
                   2404:               bug  => 'Instructor Interface',}
                   2405:              );
                   2406:     if ($env{'form.prevphase'} eq 'userpicked') {
                   2407:         push(@{$brcrum},
                   2408:                {href => "javascript:backPage(document.userupdate,'get_user_info','select')",
                   2409:                 text => $breadcrumb_text{'userpicked'},
                   2410:                 faq  => 282,
                   2411:                 bug  => 'Instructor Interface',});
1.233     raeburn  2412:     }
1.224     raeburn  2413:     my $helpitem = 'Course_Change_Privileges';
                   2414:     if ($env{'form.action'} eq 'singlestudent') {
                   2415:         $helpitem = 'Course_Add_Student';
                   2416:     }
1.351     raeburn  2417:     push(@{$brcrum}, 
                   2418:             {href => "javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
                   2419:              text => $breadcrumb_text{'modify'},
                   2420:              faq  => 282,
                   2421:              bug  => 'Instructor Interface',},
                   2422:             {href => "/adm/createuser",
                   2423:              text => "Result",
                   2424:              faq  => 282,
                   2425:              bug  => 'Instructor Interface',
                   2426:              help => $helpitem});
                   2427:     my $args = {bread_crumbs          => $brcrum,
                   2428:                 bread_crumbs_component => 'User Management'};
                   2429:     if ($env{'form.popup'}) {
                   2430:         $args->{'no_nav_bar'} = 1;
                   2431:     }
                   2432:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
1.188     raeburn  2433:     $r->print(&update_result_form($uhome));
1.27      matthew  2434:     # Check Inputs
1.101     albertel 2435:     if (! $env{'form.ccuname'} ) {
1.193     raeburn  2436: 	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
1.27      matthew  2437: 	return;
                   2438:     }
1.138     albertel 2439:     if (  $env{'form.ccuname'} ne 
                   2440: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
1.281     bisitz   2441: 	$r->print($error.&mt('Invalid login name.').'  '.
                   2442: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid.').
1.193     raeburn  2443: 		  $end.$rtnlink);
1.27      matthew  2444: 	return;
                   2445:     }
1.101     albertel 2446:     if (! $env{'form.ccdomain'}       ) {
1.193     raeburn  2447: 	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
1.27      matthew  2448: 	return;
                   2449:     }
1.138     albertel 2450:     if (  $env{'form.ccdomain'} ne
                   2451: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
1.281     bisitz   2452: 	$r->print($error.&mt('Invalid domain name.').'  '.
                   2453: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid.').
1.193     raeburn  2454: 		  $end.$rtnlink);
1.27      matthew  2455: 	return;
                   2456:     }
1.219     raeburn  2457:     if ($uhome eq 'no_host') {
                   2458:         $newuser = 1;
                   2459:     }
1.101     albertel 2460:     if (! exists($env{'form.makeuser'})) {
1.29      matthew  2461:         # Modifying an existing user, so check the validity of the name
                   2462:         if ($uhome eq 'no_host') {
1.73      sakharuk 2463:             $r->print($error.&mt('Unable to determine home server for ').
1.101     albertel 2464:                       $env{'form.ccuname'}.&mt(' in domain ').
                   2465:                       $env{'form.ccdomain'}.'.');
1.29      matthew  2466:             return;
                   2467:         }
                   2468:     }
1.27      matthew  2469:     # Determine authentication method and password for the user being modified
                   2470:     my $amode='';
                   2471:     my $genpwd='';
1.101     albertel 2472:     if ($env{'form.login'} eq 'krb') {
1.41      albertel 2473: 	$amode='krb';
1.101     albertel 2474: 	$amode.=$env{'form.krbver'};
                   2475: 	$genpwd=$env{'form.krbarg'};
                   2476:     } elsif ($env{'form.login'} eq 'int') {
1.27      matthew  2477: 	$amode='internal';
1.101     albertel 2478: 	$genpwd=$env{'form.intarg'};
                   2479:     } elsif ($env{'form.login'} eq 'fsys') {
1.27      matthew  2480: 	$amode='unix';
1.101     albertel 2481: 	$genpwd=$env{'form.fsysarg'};
                   2482:     } elsif ($env{'form.login'} eq 'loc') {
1.27      matthew  2483: 	$amode='localauth';
1.101     albertel 2484: 	$genpwd=$env{'form.locarg'};
1.27      matthew  2485: 	$genpwd=" " if (!$genpwd);
1.101     albertel 2486:     } elsif (($env{'form.login'} eq 'nochange') ||
                   2487:              ($env{'form.login'} eq ''        )) { 
1.34      matthew  2488:         # There is no need to tell the user we did not change what they
                   2489:         # did not ask us to change.
1.35      matthew  2490:         # If they are creating a new user but have not specified login
                   2491:         # information this will be caught below.
1.30      matthew  2492:     } else {
1.367     golterma 2493:             $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);
                   2494:             return;
1.27      matthew  2495:     }
1.164     albertel 2496: 
1.188     raeburn  2497:     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
1.367     golterma 2498:                         $env{'form.ccuname'}.' ('.&Apache::loncommon::plainname($env{'form.ccuname'},
                   2499:                         $env{'form.ccdomain'}).')', $env{'form.ccdomain'}).'</h3>');
                   2500:     my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,2);
1.344     bisitz   2501: 
1.193     raeburn  2502:     my (%alerts,%rulematch,%inst_results,%curr_rules);
1.334     raeburn  2503:     my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
1.361     raeburn  2504:     my @usertools = ('aboutme','blog','webdav','portfolio');
1.299     raeburn  2505:     my @requestcourses = ('official','unofficial','community');
1.362     raeburn  2506:     my @requestauthor = ('requestauthor');
1.286     raeburn  2507:     my ($othertitle,$usertypes,$types) = 
                   2508:         &Apache::loncommon::sorted_inst_types($env{'form.ccdomain'});
1.334     raeburn  2509:     my %canmodify_status =
                   2510:         &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},
                   2511:                                                    ['inststatus']);
1.101     albertel 2512:     if ($env{'form.makeuser'}) {
1.164     albertel 2513: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
1.27      matthew  2514:         # Check for the authentication mode and password
                   2515:         if (! $amode || ! $genpwd) {
1.193     raeburn  2516: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.27      matthew  2517: 	    return;
1.18      albertel 2518: 	}
1.29      matthew  2519:         # Determine desired host
1.101     albertel 2520:         my $desiredhost = $env{'form.hserver'};
1.29      matthew  2521:         if (lc($desiredhost) eq 'default') {
                   2522:             $desiredhost = undef;
                   2523:         } else {
1.147     albertel 2524:             my %home_servers = 
                   2525: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
1.29      matthew  2526:             if (! exists($home_servers{$desiredhost})) {
1.193     raeburn  2527:                 $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
                   2528:                 return;
                   2529:             }
                   2530:         }
                   2531:         # Check ID format
                   2532:         my %checkhash;
                   2533:         my %checks = ('id' => 1);
                   2534:         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
1.219     raeburn  2535:             'newuser' => $newuser, 
1.196     raeburn  2536:             'id' => $env{'form.cid'},
1.193     raeburn  2537:         );
1.196     raeburn  2538:         if ($env{'form.cid'} ne '') {
                   2539:             &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   2540:                                           \%rulematch,\%inst_results,\%curr_rules);
                   2541:             if (ref($alerts{'id'}) eq 'HASH') {
                   2542:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
                   2543:                     my $domdesc =
                   2544:                         &Apache::lonnet::domain($env{'form.ccdomain'},'description');
                   2545:                     if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
                   2546:                         my $userchkmsg;
                   2547:                         if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
                   2548:                             $userchkmsg  = 
                   2549:                                 &Apache::loncommon::instrule_disallow_msg('id',
                   2550:                                                                     $domdesc,1).
                   2551:                                 &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
                   2552:                                     $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
                   2553:                         }
                   2554:                         $r->print($error.&mt('Invalid ID format').$end.
                   2555:                                   $userchkmsg.$rtnlink);
                   2556:                         return;
                   2557:                     }
                   2558:                 }
1.29      matthew  2559:             }
                   2560:         }
1.367     golterma 2561:         &Apache::lonhtmlcommon::Increment_PrgWin($r, \%prog_state);
1.27      matthew  2562: 	# Call modifyuser
                   2563: 	my $result = &Apache::lonnet::modifyuser
1.193     raeburn  2564: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
1.188     raeburn  2565:              $amode,$genpwd,$env{'form.cfirstname'},
                   2566:              $env{'form.cmiddlename'},$env{'form.clastname'},
                   2567:              $env{'form.cgeneration'},undef,$desiredhost,
                   2568:              $env{'form.cpermanentemail'});
1.77      www      2569: 	$r->print(&mt('Generating user').': '.$result);
1.219     raeburn  2570:         $uhome = &Apache::lonnet::homeserver($env{'form.ccuname'},
1.101     albertel 2571:                                                $env{'form.ccdomain'});
1.334     raeburn  2572:         my (%changeHash,%newcustom,%changed,%changedinfo);
1.267     raeburn  2573:         if ($uhome ne 'no_host') {
1.334     raeburn  2574:             if ($context eq 'domain') {
1.378   ! raeburn  2575:                 foreach my $name ('portfolio','author') {
        !          2576:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
        !          2577:                         if ($env{'form.'.$name.'quota'} eq '') {
        !          2578:                             $newcustom{$name.'quota'} = 0;
        !          2579:                         } else {
        !          2580:                             $newcustom{$name.'quota'} = $env{'form.'.$name.'quota'};
        !          2581:                             $newcustom{$name.'quota'} =~ s/[^\d\.]//g;
        !          2582:                         }
        !          2583:                         if (&quota_admin($newcustom{$name.'quota'},\%changeHash,$name)) {
        !          2584:                             $changed{$name.'quota'} = 1;
        !          2585:                         }
1.334     raeburn  2586:                     }
                   2587:                 }
                   2588:                 foreach my $item (@usertools) {
                   2589:                     if ($env{'form.custom'.$item} == 1) {
                   2590:                         $newcustom{$item} = $env{'form.tools_'.$item};
                   2591:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2592:                                                      \%changeHash,'tools');
                   2593:                     }
1.267     raeburn  2594:                 }
1.334     raeburn  2595:                 foreach my $item (@requestcourses) {
1.341     raeburn  2596:                     if ($env{'form.custom'.$item} == 1) {
                   2597:                         $newcustom{$item} = $env{'form.crsreq_'.$item};
                   2598:                         if ($env{'form.crsreq_'.$item} eq 'autolimit') {
                   2599:                             $newcustom{$item} .= '=';
                   2600:                             unless ($env{'form.crsreq_'.$item.'_limit'} =~ /\D/) {
                   2601:                                 $newcustom{$item} .= $env{'form.crsreq_'.$item.'_limit'};
                   2602:                             }
1.334     raeburn  2603:                         }
1.341     raeburn  2604:                         $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2605:                                                       \%changeHash,'requestcourses');
1.334     raeburn  2606:                     }
1.275     raeburn  2607:                 }
1.362     raeburn  2608:                 if ($env{'form.customrequestauthor'} == 1) {
                   2609:                     $newcustom{'requestauthor'} = $env{'form.requestauthor'};
                   2610:                     $changed{'requestauthor'} = &tool_admin('requestauthor',
                   2611:                                                     $newcustom{'requestauthor'},
                   2612:                                                     \%changeHash,'requestauthor');
                   2613:                 }
1.275     raeburn  2614:             }
1.334     raeburn  2615:             if ($canmodify_status{'inststatus'}) {
                   2616:                 if (exists($env{'form.inststatus'})) {
                   2617:                     my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2618:                     if (@inststatuses > 0) {
                   2619:                         $changeHash{'inststatus'} = join(',',@inststatuses);
                   2620:                         $changed{'inststatus'} = $changeHash{'inststatus'};
1.306     raeburn  2621:                     }
                   2622:                 }
1.232     raeburn  2623:             }
1.334     raeburn  2624:             if (keys(%changed)) {
                   2625:                 foreach my $item (@userinfo) {
                   2626:                     $changeHash{$item}  = $env{'form.c'.$item};
1.286     raeburn  2627:                 }
1.267     raeburn  2628:                 my $chgresult =
                   2629:                      &Apache::lonnet::put('environment',\%changeHash,
                   2630:                                           $env{'form.ccdomain'},$env{'form.ccuname'});
                   2631:             } 
1.232     raeburn  2632:         }
1.219     raeburn  2633:         $r->print('<br />'.&mt('Home server').': '.$uhome.' '.
                   2634:                   &Apache::lonnet::hostname($uhome));
1.101     albertel 2635:     } elsif (($env{'form.login'} ne 'nochange') &&
                   2636:              ($env{'form.login'} ne ''        )) {
1.27      matthew  2637: 	# Modify user privileges
                   2638:         if (! $amode || ! $genpwd) {
1.193     raeburn  2639: 	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
1.27      matthew  2640: 	    return;
1.20      harris41 2641: 	}
1.27      matthew  2642: 	# Only allow authentification modification if the person has authority
1.101     albertel 2643: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.20      harris41 2644: 	    $r->print('Modifying authentication: '.
1.31      matthew  2645:                       &Apache::lonnet::modifyuserauth(
1.101     albertel 2646: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
1.21      harris41 2647:                        $amode,$genpwd));
1.102     albertel 2648:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
1.101     albertel 2649: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
1.4       www      2650: 	} else {
1.27      matthew  2651: 	    # Okay, this is a non-fatal error.
1.193     raeburn  2652: 	    $r->print($error.&mt('You do not have the authority to modify this users authentification information').'.'.$end);    
1.27      matthew  2653: 	}
1.28      matthew  2654:     }
1.344     bisitz   2655:     $r->rflush(); # Finish display of header before time consuming actions start
1.367     golterma 2656:     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state);
1.28      matthew  2657:     ##
1.375     raeburn  2658:     my (@userroles,%userupdate,$cnum,$cdom,$defaultcredits,%namechanged);
1.213     raeburn  2659:     if ($context eq 'course') {
1.375     raeburn  2660:         ($cnum,$cdom) =
                   2661:             &Apache::lonuserutils::get_course_identity();
1.318     raeburn  2662:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
1.375     raeburn  2663:         if ($showcredits) {
                   2664:            $defaultcredits = &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   2665:         }
1.213     raeburn  2666:     }
1.101     albertel 2667:     if (! $env{'form.makeuser'} ) {
1.28      matthew  2668:         # Check for need to change
                   2669:         my %userenv = &Apache::lonnet::get
1.134     raeburn  2670:             ('environment',['firstname','middlename','lastname','generation',
1.378   ! raeburn  2671:              'id','permanentemail','portfolioquota','authorquota','inststatus',
        !          2672:              'tools.aboutme','tools.blog','tools.webdav','tools.portfolio',
1.361     raeburn  2673:              'requestcourses.official','requestcourses.unofficial',
                   2674:              'requestcourses.community','reqcrsotherdom.official',
1.362     raeburn  2675:              'reqcrsotherdom.unofficial','reqcrsotherdom.community',
                   2676:              'requestauthor'],
1.160     raeburn  2677:               $env{'form.ccdomain'},$env{'form.ccuname'});
1.28      matthew  2678:         my ($tmp) = keys(%userenv);
                   2679:         if ($tmp =~ /^(con_lost|error)/i) { 
                   2680:             %userenv = ();
                   2681:         }
1.206     raeburn  2682:         my $no_forceid_alert;
                   2683:         # Check to see if user information can be changed
                   2684:         my %domconfig =
                   2685:             &Apache::lonnet::get_dom('configuration',['usermodification'],
                   2686:                                      $env{'form.ccdomain'});
1.213     raeburn  2687:         my @statuses = ('active','future');
                   2688:         my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@statuses,undef,$env{'request.role.domain'});
                   2689:         my ($auname,$audom);
1.220     raeburn  2690:         if ($context eq 'author') {
1.206     raeburn  2691:             $auname = $env{'user.name'};
                   2692:             $audom = $env{'user.domain'};     
                   2693:         }
                   2694:         foreach my $item (keys(%roles)) {
1.220     raeburn  2695:             my ($rolenum,$roledom,$role) = split(/:/,$item,-1);
1.206     raeburn  2696:             if ($context eq 'course') {
                   2697:                 if ($cnum ne '' && $cdom ne '') {
                   2698:                     if ($rolenum eq $cnum && $roledom eq $cdom) {
                   2699:                         if (!grep(/^\Q$role\E$/,@userroles)) {
                   2700:                             push(@userroles,$role);
                   2701:                         }
                   2702:                     }
                   2703:                 }
                   2704:             } elsif ($context eq 'author') {
                   2705:                 if ($rolenum eq $auname && $roledom eq $audom) {
                   2706:                     if (!grep(/^\Q$role\E$/,@userroles)) { 
                   2707:                         push(@userroles,$role);
                   2708:                     }
                   2709:                 }
                   2710:             }
                   2711:         }
1.220     raeburn  2712:         if ($env{'form.action'} eq 'singlestudent') {
                   2713:             if (!grep(/^st$/,@userroles)) {
                   2714:                 push(@userroles,'st');
                   2715:             }
                   2716:         } else {
                   2717:             # Check for course or co-author roles being activated or re-enabled
                   2718:             if ($context eq 'author' || $context eq 'course') {
                   2719:                 foreach my $key (keys(%env)) {
                   2720:                     if ($context eq 'author') {
                   2721:                         if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
                   2722:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2723:                                 push(@userroles,$1);
                   2724:                             }
                   2725:                         } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
                   2726:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2727:                                 push(@userroles,$1);
                   2728:                             }
1.206     raeburn  2729:                         }
1.220     raeburn  2730:                     } elsif ($context eq 'course') {
                   2731:                         if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
                   2732:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2733:                                 push(@userroles,$1);
                   2734:                             }
                   2735:                         } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
                   2736:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2737:                                 push(@userroles,$1);
                   2738:                             }
1.206     raeburn  2739:                         }
                   2740:                     }
                   2741:                 }
                   2742:             }
                   2743:         }
                   2744:         #Check to see if we can change personal data for the user 
                   2745:         my (@mod_disallowed,@longroles);
                   2746:         foreach my $role (@userroles) {
                   2747:             if ($role eq 'cr') {
                   2748:                 push(@longroles,'Custom');
                   2749:             } else {
1.318     raeburn  2750:                 push(@longroles,&Apache::lonnet::plaintext($role,$crstype)); 
1.206     raeburn  2751:             }
                   2752:         }
1.219     raeburn  2753:         my %canmodify = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},\@userinfo,\@userroles);
                   2754:         foreach my $item (@userinfo) {
1.28      matthew  2755:             # Strip leading and trailing whitespace
1.203     raeburn  2756:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
1.219     raeburn  2757:             if (!$canmodify{$item}) {
1.207     raeburn  2758:                 if (defined($env{'form.c'.$item})) {
                   2759:                     if ($env{'form.c'.$item} ne $userenv{$item}) {
                   2760:                         push(@mod_disallowed,$item);
                   2761:                     }
1.206     raeburn  2762:                 }
                   2763:                 $env{'form.c'.$item} = $userenv{$item};
                   2764:             }
1.28      matthew  2765:         }
1.259     bisitz   2766:         # Check to see if we can change the Student/Employee ID
1.196     raeburn  2767:         my $forceid = $env{'form.forceid'};
                   2768:         my $recurseid = $env{'form.recurseid'};
                   2769:         my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
1.203     raeburn  2770:         my %uidhash = &Apache::lonnet::idrget($env{'form.ccdomain'},
                   2771:                                             $env{'form.ccuname'});
                   2772:         if (($uidhash{$env{'form.ccuname'}}) && 
                   2773:             ($uidhash{$env{'form.ccuname'}}!~/error\:/) && 
                   2774:             (!$forceid)) {
                   2775:             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
                   2776:                 $env{'form.cid'} = $userenv{'id'};
1.293     bisitz   2777:                 $no_forceid_alert = &mt('New student/employee ID does not match existing ID for this user.')
1.259     bisitz   2778:                                    .'<br />'
                   2779:                                    .&mt("Change is not permitted without checking the 'Force ID change' checkbox on the previous page.")
                   2780:                                    .'<br />'."\n";
1.203     raeburn  2781:             }
                   2782:         }
                   2783:         if ($env{'form.cid'} ne $userenv{'id'}) {
1.196     raeburn  2784:             my $checkhash;
                   2785:             my $checks = { 'id' => 1 };
                   2786:             $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
                   2787:                    { 'newuser' => $newuser,
                   2788:                      'id'  => $env{'form.cid'}, 
                   2789:                    };
                   2790:             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   2791:                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
                   2792:             if (ref($alerts{'id'}) eq 'HASH') {
                   2793:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
1.203     raeburn  2794:                    $env{'form.cid'} = $userenv{'id'};
1.196     raeburn  2795:                 }
                   2796:             }
                   2797:         }
1.378   ! raeburn  2798:         my (%quotachanged,%oldquota,%newquota,%olddefquota,%newdefquota, 
        !          2799:             $oldinststatus,$newinststatus,%oldisdefault,%newisdefault,%oldsettings,
1.339     raeburn  2800:             %oldsettingstext,%newsettings,%newsettingstext,@disporder,
1.378   ! raeburn  2801:             %oldsettingstatus,%newsettingstatus);
1.334     raeburn  2802:         @disporder = ('inststatus');
                   2803:         if ($env{'request.role.domain'} eq $env{'form.ccdomain'}) {
1.362     raeburn  2804:             push(@disporder,'requestcourses','requestauthor');
1.334     raeburn  2805:         } else {
                   2806:             push(@disporder,'reqcrsotherdom');
                   2807:         }
                   2808:         push(@disporder,('quota','tools'));
1.338     raeburn  2809:         $oldinststatus = $userenv{'inststatus'};
1.378   ! raeburn  2810:         foreach my $name ('portfolio','author') {
        !          2811:             ($olddefquota{$name},$oldsettingstatus{$name}) = 
        !          2812:                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$oldinststatus,$name);
        !          2813:             ($newdefquota{$name},$newsettingstatus{$name}) = ($olddefquota{$name},$oldsettingstatus{$name});
        !          2814:         }
1.334     raeburn  2815:         my %canshow;
1.220     raeburn  2816:         if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
1.334     raeburn  2817:             $canshow{'quota'} = 1;
1.220     raeburn  2818:         }
1.267     raeburn  2819:         if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
1.334     raeburn  2820:             $canshow{'tools'} = 1;
1.267     raeburn  2821:         }
1.275     raeburn  2822:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
1.334     raeburn  2823:             $canshow{'requestcourses'} = 1;
1.300     raeburn  2824:         } elsif (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.334     raeburn  2825:             $canshow{'reqcrsotherdom'} = 1;
1.275     raeburn  2826:         }
1.286     raeburn  2827:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.334     raeburn  2828:             $canshow{'inststatus'} = 1;
1.286     raeburn  2829:         }
1.362     raeburn  2830:         if (&Apache::lonnet::allowed('cau',$env{'form.ccdomain'})) {
                   2831:             $canshow{'requestauthor'} = 1;
                   2832:         }
1.267     raeburn  2833:         my (%changeHash,%changed);
1.286     raeburn  2834:         if ($oldinststatus eq '') {
1.334     raeburn  2835:             $oldsettings{'inststatus'} = $othertitle; 
1.286     raeburn  2836:         } else {
                   2837:             if (ref($usertypes) eq 'HASH') {
1.334     raeburn  2838:                 $oldsettings{'inststatus'} = join(', ',map{ $usertypes->{ &unescape($_) }; } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  2839:             } else {
1.334     raeburn  2840:                 $oldsettings{'inststatus'} = join(', ',map{ &unescape($_); } (split(/:/,$userenv{'inststatus'})));
1.286     raeburn  2841:             }
                   2842:         }
                   2843:         $changeHash{'inststatus'} = $userenv{'inststatus'};
1.334     raeburn  2844:         if ($canmodify_status{'inststatus'}) {
                   2845:             $canshow{'inststatus'} = 1;
1.286     raeburn  2846:             if (exists($env{'form.inststatus'})) {
                   2847:                 my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2848:                 if (@inststatuses > 0) {
                   2849:                     $newinststatus = join(':',map { &escape($_); } @inststatuses);
                   2850:                     $changeHash{'inststatus'} = $newinststatus;
                   2851:                     if ($newinststatus ne $oldinststatus) {
                   2852:                         $changed{'inststatus'} = $newinststatus;
1.378   ! raeburn  2853:                         foreach my $name ('portfolio','author') {
        !          2854:                             ($newdefquota{$name},$newsettingstatus{$name}) =
        !          2855:                                 &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
        !          2856:                         }
1.286     raeburn  2857:                     }
                   2858:                     if (ref($usertypes) eq 'HASH') {
1.334     raeburn  2859:                         $newsettings{'inststatus'} = join(', ',map{ $usertypes->{$_}; } (@inststatuses)); 
1.286     raeburn  2860:                     } else {
1.337     raeburn  2861:                         $newsettings{'inststatus'} = join(', ',@inststatuses);
1.286     raeburn  2862:                     }
1.334     raeburn  2863:                 }
                   2864:             } else {
                   2865:                 $newinststatus = '';
                   2866:                 $changeHash{'inststatus'} = $newinststatus;
                   2867:                 $newsettings{'inststatus'} = $othertitle;
                   2868:                 if ($newinststatus ne $oldinststatus) {
                   2869:                     $changed{'inststatus'} = $changeHash{'inststatus'};
1.378   ! raeburn  2870:                     foreach my $name ('portfolio','author') {
        !          2871:                         ($newdefquota{$name},$newsettingstatus{$name}) =
        !          2872:                             &Apache::loncommon::default_quota($env{'form.ccdomain'},$newinststatus,$name);
        !          2873:                     }
1.286     raeburn  2874:                 }
                   2875:             }
1.334     raeburn  2876:         } elsif ($context ne 'selfcreate') {
                   2877:             $canshow{'inststatus'} = 1;
1.337     raeburn  2878:             $newsettings{'inststatus'} = $oldsettings{'inststatus'};
1.286     raeburn  2879:         }
1.378   ! raeburn  2880:         foreach my $name ('portfolio','author') {
        !          2881:             $changeHash{$name.'quota'} = $userenv{$name.'quota'};
        !          2882:         }
1.334     raeburn  2883:         if ($context eq 'domain') {
1.378   ! raeburn  2884:             foreach my $name ('portfolio','author') {
        !          2885:                 if ($userenv{$name.'quota'} ne '') {
        !          2886:                     $oldquota{$name} = $userenv{$name.'quota'};
        !          2887:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
        !          2888:                         if ($env{'form.'.$name.'quota'} eq '') {
        !          2889:                             $newquota{$name} = 0;
        !          2890:                         } else {
        !          2891:                             $newquota{$name} = $env{'form.'.$name.'quota'};
        !          2892:                             $newquota{$name} =~ s/[^\d\.]//g;
        !          2893:                         }
        !          2894:                         if ($newquota{$name} != $oldquota{$name}) {
        !          2895:                             if (&quota_admin($newquota{$name},\%changeHash,$name)) {
        !          2896:                                 $changed{$name.'quota'} = 1;
        !          2897:                             }
        !          2898:                         }
1.334     raeburn  2899:                     } else {
1.378   ! raeburn  2900:                         if (&quota_admin('',\%changeHash,$name)) {
        !          2901:                             $changed{$name.'quota'} = 1;
        !          2902:                             $newquota{$name} = $newdefquota{$name};
        !          2903:                             $newisdefault{$name} = 1;
        !          2904:                         }
1.334     raeburn  2905:                     }
1.149     raeburn  2906:                 } else {
1.378   ! raeburn  2907:                     $oldisdefault{$name} = 1;
        !          2908:                     $oldquota{$name} = $olddefquota{$name};
        !          2909:                     if ($env{'form.custom_'.$name.'quota'} == 1) {
        !          2910:                         if ($env{'form.'.$name.'quota'} eq '') {
        !          2911:                             $newquota{$name} = 0;
        !          2912:                         } else {
        !          2913:                             $newquota{$name} = $env{'form.'.$name.'quota'};
        !          2914:                             $newquota{$name} =~ s/[^\d\.]//g;
        !          2915:                         }
        !          2916:                         if (&quota_admin($newquota{$name},\%changeHash,$name)) {
        !          2917:                             $changed{$name.'quota'} = 1;
        !          2918:                         }
1.334     raeburn  2919:                     } else {
1.378   ! raeburn  2920:                         $newquota{$name} = $newdefquota{$name};
        !          2921:                         $newisdefault{$name} = 1;
1.334     raeburn  2922:                     }
1.378   ! raeburn  2923:                 }
        !          2924:                 if ($oldisdefault{$name}) {
        !          2925:                     $oldsettingstext{'quota'}{$name} = &get_defaultquota_text($oldsettingstatus{$name});
        !          2926:                 }
        !          2927:                 if ($newisdefault{$name}) {
        !          2928:                     $newsettingstext{'quota'}{$name} = &get_defaultquota_text($newsettingstatus{$name});
1.134     raeburn  2929:                 }
                   2930:             }
1.334     raeburn  2931:             &tool_changes('tools',\@usertools,\%oldsettings,\%oldsettingstext,\%userenv,
                   2932:                           \%changeHash,\%changed,\%newsettings,\%newsettingstext);
                   2933:             if ($env{'form.ccdomain'} eq $env{'request.role.domain'}) {
                   2934:                 &tool_changes('requestcourses',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   2935:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.362     raeburn  2936:                 &tool_changes('requestauthor',\@requestauthor,\%oldsettings,\%oldsettingstext,\%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  2937:             } else {
1.334     raeburn  2938:                 &tool_changes('reqcrsotherdom',\@requestcourses,\%oldsettings,\%oldsettingstext,
                   2939:                               \%userenv,\%changeHash,\%changed,\%newsettings,\%newsettingstext);
1.149     raeburn  2940:             }
                   2941:         }
1.334     raeburn  2942:         foreach my $item (@userinfo) {
                   2943:             if ($env{'form.c'.$item} ne $userenv{$item}) {
                   2944:                 $namechanged{$item} = 1;
                   2945:             }
1.204     raeburn  2946:         }
1.378   ! raeburn  2947:         foreach my $name ('portfolio','author') {
        !          2948:             $oldsettings{'quota'}{$name} = $oldquota{$name}.' Mb';
        !          2949:             $newsettings{'quota'}{$name} = $newquota{$name}.' Mb';
        !          2950:         }
1.334     raeburn  2951:         if ((keys(%namechanged) > 0) || (keys(%changed) > 0)) {
1.267     raeburn  2952:             my ($chgresult,$namechgresult);
                   2953:             if (keys(%changed) > 0) {
                   2954:                 $chgresult = 
1.204     raeburn  2955:                     &Apache::lonnet::put('environment',\%changeHash,
                   2956:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
1.267     raeburn  2957:                 if ($chgresult eq 'ok') {
                   2958:                     if (($env{'user.name'} eq $env{'form.ccuname'}) &&
                   2959:                         ($env{'user.domain'} eq $env{'form.ccdomain'})) {
1.270     raeburn  2960:                         my %newenvhash;
                   2961:                         foreach my $key (keys(%changed)) {
1.299     raeburn  2962:                             if (($key eq 'official') || ($key eq 'unofficial')
                   2963:                                 || ($key eq 'community')) {
1.279     raeburn  2964:                                 $newenvhash{'environment.requestcourses.'.$key} =
                   2965:                                     $changeHash{'requestcourses.'.$key};
1.362     raeburn  2966:                                 if ($changeHash{'requestcourses.'.$key}) {
1.332     raeburn  2967:                                     $newenvhash{'environment.canrequest.'.$key} = 1;
1.279     raeburn  2968:                                 } else {
                   2969:                                     $newenvhash{'environment.canrequest.'.$key} =
                   2970:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   2971:                                             $key,'reload','requestcourses');
                   2972:                                 }
1.362     raeburn  2973:                             } elsif ($key eq 'requestauthor') {
                   2974:                                 $newenvhash{'environment.'.$key} = $changeHash{$key};
                   2975:                                 if ($changeHash{$key}) {
                   2976:                                     $newenvhash{'environment.canrequest.author'} = 1;
                   2977:                                 } else {
                   2978:                                     $newenvhash{'environment.canrequest.author'} =
                   2979:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   2980:                                             $key,'reload','requestauthor');
                   2981:                                 }
1.275     raeburn  2982:                             } elsif ($key ne 'quota') {
1.270     raeburn  2983:                                 $newenvhash{'environment.tools.'.$key} = 
                   2984:                                     $changeHash{'tools.'.$key};
1.279     raeburn  2985:                                 if ($changeHash{'tools.'.$key} ne '') {
                   2986:                                     $newenvhash{'environment.availabletools.'.$key} =
                   2987:                                         $changeHash{'tools.'.$key};
                   2988:                                 } else {
                   2989:                                     $newenvhash{'environment.availabletools.'.$key} =
1.367     golterma 2990:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   2991:           $key,'reload','tools');
1.279     raeburn  2992:                                 }
1.270     raeburn  2993:                             }
                   2994:                         }
1.271     raeburn  2995:                         if (keys(%newenvhash)) {
                   2996:                             &Apache::lonnet::appenv(\%newenvhash);
                   2997:                         }
1.267     raeburn  2998:                     }
                   2999:                 }
1.204     raeburn  3000:             }
1.334     raeburn  3001:             if (keys(%namechanged) > 0) {
1.337     raeburn  3002:                 foreach my $field (@userinfo) {
                   3003:                     $changeHash{$field}  = $env{'form.c'.$field};
                   3004:                 }
                   3005: # Make the change
1.204     raeburn  3006:                 $namechgresult =
                   3007:                     &Apache::lonnet::modifyuser($env{'form.ccdomain'},
                   3008:                         $env{'form.ccuname'},$changeHash{'id'},undef,undef,
                   3009:                         $changeHash{'firstname'},$changeHash{'middlename'},
                   3010:                         $changeHash{'lastname'},$changeHash{'generation'},
1.337     raeburn  3011:                         $changeHash{'id'},undef,$changeHash{'permanentemail'},undef,\@userinfo);
1.220     raeburn  3012:                 %userupdate = (
                   3013:                                lastname   => $env{'form.clastname'},
                   3014:                                middlename => $env{'form.cmiddlename'},
                   3015:                                firstname  => $env{'form.cfirstname'},
                   3016:                                generation => $env{'form.cgeneration'},
                   3017:                                id         => $env{'form.cid'},
                   3018:                              );
1.204     raeburn  3019:             }
1.334     raeburn  3020:             if (((keys(%namechanged) > 0) && $namechgresult eq 'ok') || 
1.267     raeburn  3021:                 ((keys(%changed) > 0) && $chgresult eq 'ok')) {
1.28      matthew  3022:             # Tell the user we changed the name
1.334     raeburn  3023:                 &display_userinfo($r,1,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3024:                                   \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,
1.334     raeburn  3025:                                   \%oldsettings, \%oldsettingstext,\%newsettings,
                   3026:                                   \%newsettingstext);
1.203     raeburn  3027:                 if ($env{'form.cid'} ne $userenv{'id'}) {
                   3028:                     &Apache::lonnet::idput($env{'form.ccdomain'},
                   3029:                          ($env{'form.ccuname'} => $env{'form.cid'}));
                   3030:                     if (($recurseid) &&
                   3031:                         (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
                   3032:                         my $idresult = 
                   3033:                             &Apache::lonuserutils::propagate_id_change(
                   3034:                                 $env{'form.ccuname'},$env{'form.ccdomain'},
                   3035:                                 \%userupdate);
                   3036:                         $r->print('<br />'.$idresult.'<br />');
                   3037:                     }
1.196     raeburn  3038:                 }
1.149     raeburn  3039:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
                   3040:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
                   3041:                     my %newenvhash;
                   3042:                     foreach my $key (keys(%changeHash)) {
                   3043:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
                   3044:                     }
1.238     raeburn  3045:                     &Apache::lonnet::appenv(\%newenvhash);
1.149     raeburn  3046:                 }
1.28      matthew  3047:             } else { # error occurred
1.188     raeburn  3048:                 $r->print('<span class="LC_error">'.&mt('Unable to successfully change environment for').' '.
                   3049:                       $env{'form.ccuname'}.' '.&mt('in domain').' '.
1.206     raeburn  3050:                       $env{'form.ccdomain'}.'</span><br />');
1.28      matthew  3051:             }
1.334     raeburn  3052:         } else { # End of if ($env ... ) logic
1.275     raeburn  3053:             # They did not want to change the users name, quota, tool availability,
                   3054:             # or ability to request creation of courses, 
1.267     raeburn  3055:             # but we can still tell them what the name and quota and availabilities are  
1.334     raeburn  3056:             &display_userinfo($r,undef,\@disporder,\%canshow,\@requestcourses,
1.362     raeburn  3057:                               \@usertools,\@requestauthor,\%userenv,\%changed,\%namechanged,\%oldsettings,
1.334     raeburn  3058:                               \%oldsettingstext,\%newsettings,\%newsettingstext);
1.28      matthew  3059:         }
1.206     raeburn  3060:         if (@mod_disallowed) {
                   3061:             my ($rolestr,$contextname);
                   3062:             if (@longroles > 0) {
                   3063:                 $rolestr = join(', ',@longroles);
                   3064:             } else {
                   3065:                 $rolestr = &mt('No roles');
                   3066:             }
                   3067:             if ($context eq 'course') {
                   3068:                 $contextname = &mt('course');
                   3069:             } elsif ($context eq 'author') {
                   3070:                 $contextname = &mt('co-author');
                   3071:             }
                   3072:             $r->print(&mt('The following fields were not updated: ').'<ul>');
                   3073:             my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   3074:             foreach my $field (@mod_disallowed) {
                   3075:                 $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
                   3076:             }
1.207     raeburn  3077:             $r->print('</ul>');
                   3078:             if (@mod_disallowed == 1) {
                   3079:                 $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));
                   3080:             } else {
                   3081:                 $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));
                   3082:             }
1.292     bisitz   3083:             my $helplink = 'javascript:helpMenu('."'display'".')';
                   3084:             $r->print('<span class="LC_cusr_emph">'.$rolestr.'</span><br />'
                   3085:                      .&mt('Please contact your [_1]helpdesk[_2] for more information.'
                   3086:                          ,'<a href="'.$helplink.'">','</a>')
                   3087:                       .'<br />');
1.206     raeburn  3088:         }
1.259     bisitz   3089:         $r->print('<span class="LC_warning">'
                   3090:                   .$no_forceid_alert
                   3091:                   .&Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules)
                   3092:                   .'</span>');
1.4       www      3093:     }
1.367     golterma 3094:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.220     raeburn  3095:     if ($env{'form.action'} eq 'singlestudent') {
1.375     raeburn  3096:         &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context,
                   3097:                                $crstype,$showcredits,$defaultcredits);
1.318     raeburn  3098:         $r->print('<p><a href="javascript:backPage(document.userupdate)">');
                   3099:         if ($crstype eq 'Community') {
                   3100:             $r->print(&mt('Enroll Another Member'));
                   3101:         } else {
                   3102:             $r->print(&mt('Enroll Another Student'));
                   3103:         }
                   3104:         $r->print('</a></p>');
1.220     raeburn  3105:     } else {
1.375     raeburn  3106:         my @rolechanges = &update_roles($r,$context,$showcredits);
1.334     raeburn  3107:         if (keys(%namechanged) > 0) {
1.220     raeburn  3108:             if ($context eq 'course') {
                   3109:                 if (@userroles > 0) {
1.225     raeburn  3110:                     if ((@rolechanges == 0) || 
                   3111:                         (!(grep(/^st$/,@rolechanges)))) {
                   3112:                         if (grep(/^st$/,@userroles)) {
                   3113:                             my $classlistupdated =
                   3114:                                 &Apache::lonuserutils::update_classlist($cdom,
1.220     raeburn  3115:                                               $cnum,$env{'form.ccdomain'},
                   3116:                                        $env{'form.ccuname'},\%userupdate);
1.225     raeburn  3117:                         }
1.220     raeburn  3118:                     }
                   3119:                 }
                   3120:             }
                   3121:         }
1.226     raeburn  3122:         my $userinfo = &Apache::loncommon::plainname($env{'form.ccuname'},
1.233     raeburn  3123:                                                      $env{'form.ccdomain'});
                   3124:         if ($env{'form.popup'}) {
                   3125:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   3126:         } else {
1.367     golterma 3127:             $r->print('<br />'.&Apache::lonhtmlcommon::actionbox(['<a href="javascript:backPage(document.userupdate,'."'$env{'form.prevphase'}','modify'".')">'
                   3128:                      .&mt('Modify this user: [_1]','<span class="LC_cusr_emph">'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.' ('.$userinfo.')</span>').'</a>',
                   3129:                      '<a href="javascript:backPage(document.userupdate)">'.&mt('Create/Modify Another User').'</a>']));
1.233     raeburn  3130:         }
1.220     raeburn  3131:     }
                   3132: }
                   3133: 
1.334     raeburn  3134: sub display_userinfo {
1.362     raeburn  3135:     my ($r,$changed,$order,$canshow,$requestcourses,$usertools,$requestauthor,
                   3136:         $userenv,$changedhash,$namechangedhash,$oldsetting,$oldsettingtext,
1.334     raeburn  3137:         $newsetting,$newsettingtext) = @_;
                   3138:     return unless (ref($order) eq 'ARRAY' &&
                   3139:                    ref($canshow) eq 'HASH' && 
                   3140:                    ref($requestcourses) eq 'ARRAY' && 
1.362     raeburn  3141:                    ref($requestauthor) eq 'ARRAY' &&
1.334     raeburn  3142:                    ref($usertools) eq 'ARRAY' && 
                   3143:                    ref($userenv) eq 'HASH' &&
                   3144:                    ref($changedhash) eq 'HASH' &&
                   3145:                    ref($oldsetting) eq 'HASH' &&
                   3146:                    ref($oldsettingtext) eq 'HASH' &&
                   3147:                    ref($newsetting) eq 'HASH' &&
                   3148:                    ref($newsettingtext) eq 'HASH');
                   3149:     my %lt=&Apache::lonlocal::texthash(
1.372     raeburn  3150:          'ui'             => 'User Information',
1.334     raeburn  3151:          'uic'            => 'User Information Changed',
                   3152:          'firstname'      => 'First Name',
                   3153:          'middlename'     => 'Middle Name',
                   3154:          'lastname'       => 'Last Name',
                   3155:          'generation'     => 'Generation',
                   3156:          'id'             => 'Student/Employee ID',
                   3157:          'permanentemail' => 'Permanent e-mail address',
1.378   ! raeburn  3158:          'portfolioquota' => 'Disk space allocated to portfolio files',
        !          3159:          'authorquota'    => 'Disk space allocated to authoring space',
1.334     raeburn  3160:          'blog'           => 'Blog Availability',
1.361     raeburn  3161:          'webdav'         => 'WebDAV Availability',
1.334     raeburn  3162:          'aboutme'        => 'Personal Information Page Availability',
                   3163:          'portfolio'      => 'Portfolio Availability',
                   3164:          'official'       => 'Can Request Official Courses',
                   3165:          'unofficial'     => 'Can Request Unofficial Courses',
                   3166:          'community'      => 'Can Request Communities',
1.362     raeburn  3167:          'requestauthor'  => 'Can Request Author Role',
1.334     raeburn  3168:          'inststatus'     => "Affiliation",
                   3169:          'prvs'           => 'Previous Value:',
                   3170:          'chto'           => 'Changed To:'
                   3171:     );
                   3172:     if ($changed) {
1.372     raeburn  3173:         $r->print('<h3>'.$lt{'uic'}.'</h3>'.
1.367     golterma 3174:                 &Apache::loncommon::start_data_table().
                   3175:                 &Apache::loncommon::start_data_table_header_row());
1.334     raeburn  3176:         $r->print("<th>&nbsp;</th>\n");
1.367     golterma 3177:         $r->print('<th><b>'.$lt{'prvs'}.'</b></th>');
                   3178:         $r->print('<th><span class="LC_nobreak"><b>'.$lt{'chto'}.'</b></span></th>');
                   3179:         $r->print(&Apache::loncommon::end_data_table_header_row());
                   3180:         my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
                   3181:         
                   3182: 
1.334     raeburn  3183:         foreach my $item (@userinfo) {
                   3184:             my $value = $env{'form.c'.$item};
1.367     golterma 3185:             #show changes only:
                   3186:             unless($value eq $userenv->{$item}){
                   3187:                 $r->print(&Apache::loncommon::start_data_table_row());
                   3188: 
                   3189:                 $r->print("<td>$lt{$item}</td>\n");
                   3190:                 $r->print('<td>'.$userenv->{$item}.' </td>');
                   3191:                 $r->print("<td>$value </td>\n");
                   3192: 
                   3193:                 $r->print(&Apache::loncommon::end_data_table_row());
1.334     raeburn  3194:             }
                   3195:         }
                   3196:         foreach my $entry (@{$order}) {
1.367     golterma 3197:             if ($canshow->{$entry} && ($newsetting->{$entry} ne $newsetting->{$entry})) {
                   3198:                 $r->print(&Apache::loncommon::start_data_table_row());
1.334     raeburn  3199:                 if (($entry eq 'requestcourses') || ($entry eq 'reqcrsotherdom')) {
                   3200:                     foreach my $item (@{$requestcourses}) {
1.367     golterma 3201:                         $r->print("<td>$lt{$item}</td>\n");
                   3202:                         $r->print("<td>$oldsetting->{$item} $oldsettingtext->{$item}</td>\n");
1.334     raeburn  3203:                         my $value = $newsetting->{$item}.' '.$newsettingtext->{$item};
                   3204:                         if ($changedhash->{$item}) {
                   3205:                             $value = '<span class="LC_cusr_emph">'.$value.'</span>';
                   3206:                         }
                   3207:                         $r->print("<td>$value </td>\n");
                   3208:                     }
                   3209:                 } elsif ($entry eq 'tools') {
                   3210:                     foreach my $item (@{$usertools}) {
1.367     golterma 3211:                         $r->print("<td>$lt{$item}</td>\n");
                   3212:                         $r->print("<td>$oldsetting->{$item} $oldsettingtext->{$item}</td>\n");
1.334     raeburn  3213:                         my $value = $newsetting->{$item}.' '.$newsettingtext->{$item};
                   3214:                         if ($changedhash->{$item}) {
                   3215:                             $value = '<span class="LC_cusr_emph">'.$value.'</span>';
                   3216:                         }
                   3217:                         $r->print("<td>$value </td>\n");
                   3218:                     }
1.378   ! raeburn  3219:                 } elsif ($entry eq 'quota') {
        !          3220:                     if ((ref($oldsetting->{$entry}) eq 'HASH') && (ref($oldsettingtext->{$entry}) eq 'HASH') &&
        !          3221:                         (ref($newsetting->{$entry}) eq 'HASH') && (ref($newsettingtext->{$entry}) eq 'HASH')) {
        !          3222:                         foreach my $name ('portfolio','author') {
        !          3223:                             $r->print("<td>$lt{$name.$entry}</td>\n");
        !          3224:                             $r->print("<td>$oldsetting->{$entry}->{$name} $oldsettingtext->{$entry}->{$name} </td>\n");
        !          3225:                             my $value = $newsetting->{$entry}->{$name}.' '.$newsettingtext->{$entry}->{$name};
        !          3226:                             if ($changedhash->{$entry}) {
        !          3227:                                 $value = '<span class="LC_cusr_emph">'.$value.'</span>';
        !          3228:                             }
        !          3229:                              $r->print("<td>$value </td>\n");
        !          3230:                         }
        !          3231:                     }
1.334     raeburn  3232:                 } else {
1.367     golterma 3233:                     $r->print("<td>$lt{$entry}</td>\n");
                   3234:                     $r->print("<td>$oldsetting->{$entry} $oldsettingtext->{$entry} </td>\n");
1.334     raeburn  3235:                     my $value = $newsetting->{$entry}.' '.$newsettingtext->{$entry};
                   3236:                     if ($changedhash->{$entry}) {
                   3237:                         $value = '<span class="LC_cusr_emph">'.$value.'</span>';
                   3238:                     }
                   3239:                     $r->print("<td>$value </td>\n");
                   3240:                 }
1.367     golterma 3241:                 $r->print(&Apache::loncommon::end_data_table_row());
1.334     raeburn  3242:             }
                   3243:         }
1.367     golterma 3244:         $r->print(&Apache::loncommon::end_data_table().'<br />');
1.372     raeburn  3245:     } else {
                   3246:         $r->print('<h3>'.$lt{'ui'}.'</h3>'.
                   3247:                   '<p>'.&mt('No changes made to user information').'</p>');
1.334     raeburn  3248:     }
                   3249:     return;
                   3250: }
                   3251: 
1.275     raeburn  3252: sub tool_changes {
                   3253:     my ($context,$usertools,$oldaccess,$oldaccesstext,$userenv,$changeHash,
                   3254:         $changed,$newaccess,$newaccesstext) = @_;
                   3255:     if (!((ref($usertools) eq 'ARRAY') && (ref($oldaccess) eq 'HASH') &&
                   3256:           (ref($oldaccesstext) eq 'HASH') && (ref($userenv) eq 'HASH') &&
                   3257:           (ref($changeHash) eq 'HASH') && (ref($changed) eq 'HASH') &&
                   3258:           (ref($newaccess) eq 'HASH') && (ref($newaccesstext) eq 'HASH'))) {
                   3259:         return;
                   3260:     }
1.300     raeburn  3261:     if ($context eq 'reqcrsotherdom') {
1.309     raeburn  3262:         my @options = ('approval','validate','autolimit');
1.306     raeburn  3263:         my $optregex = join('|',@options);
                   3264:         my %reqdisplay = &courserequest_display();
1.300     raeburn  3265:         my $cdom = $env{'request.role.domain'};
                   3266:         foreach my $tool (@{$usertools}) {
1.314     raeburn  3267:             $oldaccesstext->{$tool} = &mt('No');
                   3268:             $newaccesstext->{$tool} = $oldaccesstext->{$tool};
1.300     raeburn  3269:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
1.314     raeburn  3270:             my $newop;
                   3271:             if ($env{'form.'.$context.'_'.$tool}) {
                   3272:                 $newop = $env{'form.'.$context.'_'.$tool};
                   3273:                 if ($newop eq 'autolimit') {
                   3274:                     my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3275:                     $limit =~ s/\D+//g;
                   3276:                     $newop .= '='.$limit;
                   3277:                 }
                   3278:             }
1.300     raeburn  3279:             if ($userenv->{$context.'.'.$tool} eq '') {
1.314     raeburn  3280:                 if ($newop) {
                   3281:                     $changed->{$tool}=&tool_admin($tool,$cdom.':'.$newop,
1.300     raeburn  3282:                                                   $changeHash,$context);
                   3283:                     if ($changed->{$tool}) {
1.314     raeburn  3284:                         $newaccesstext->{$tool} = &mt('Yes');
1.300     raeburn  3285:                     } else {
                   3286:                         $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3287:                     }
                   3288:                 }
                   3289:             } else {
                   3290:                 my @curr = split(',',$userenv->{$context.'.'.$tool});
                   3291:                 my @new;
                   3292:                 my $changedoms;
1.314     raeburn  3293:                 foreach my $req (@curr) {
                   3294:                     if ($req =~ /^\Q$cdom\E\:($optregex\=?\d*)$/) {
                   3295:                         $oldaccesstext->{$tool} = &mt('Yes');
                   3296:                         my $oldop = $1;
                   3297:                         if ($oldop ne $newop) {
                   3298:                             $changedoms = 1;
                   3299:                             foreach my $item (@curr) {
                   3300:                                 my ($reqdom,$option) = split(':',$item);
                   3301:                                 unless ($reqdom eq $cdom) {
                   3302:                                     push(@new,$item);
                   3303:                                 }
                   3304:                             }
                   3305:                             if ($newop) {
                   3306:                                 push(@new,$cdom.':'.$newop);
1.300     raeburn  3307:                             }
1.314     raeburn  3308:                             @new = sort(@new);
1.300     raeburn  3309:                         }
1.314     raeburn  3310:                         last;
1.300     raeburn  3311:                     }
1.314     raeburn  3312:                 }
                   3313:                 if ((!$changedoms) && ($newop)) {
1.300     raeburn  3314:                     $changedoms = 1;
1.306     raeburn  3315:                     @new = sort(@curr,$cdom.':'.$newop);
1.300     raeburn  3316:                 }
                   3317:                 if ($changedoms) {
1.314     raeburn  3318:                     my $newdomstr;
1.300     raeburn  3319:                     if (@new) {
                   3320:                         $newdomstr = join(',',@new);
                   3321:                     }
                   3322:                     $changed->{$tool}=&tool_admin($tool,$newdomstr,$changeHash,
                   3323:                                                   $context);
                   3324:                     if ($changed->{$tool}) {
                   3325:                         if ($env{'form.'.$context.'_'.$tool}) {
1.306     raeburn  3326:                             if ($env{'form.'.$context.'_'.$tool} eq 'autolimit') {
1.314     raeburn  3327:                                 my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3328:                                 $limit =~ s/\D+//g;
                   3329:                                 if ($limit) {
                   3330:                                     $newaccesstext->{$tool} = &mt('Yes, up to limit of [quant,_1,request] per user.',$limit);
                   3331:                                 } else {
1.306     raeburn  3332:                                     $newaccesstext->{$tool} = &mt('Yes, processed automatically');
                   3333:                                 }
1.314     raeburn  3334:                             } else {
1.306     raeburn  3335:                                 $newaccesstext->{$tool} = $reqdisplay{$env{'form.'.$context.'_'.$tool}};
                   3336:                             }
1.300     raeburn  3337:                         } else {
1.306     raeburn  3338:                             $newaccesstext->{$tool} = &mt('No');
1.300     raeburn  3339:                         }
                   3340:                     }
                   3341:                 }
                   3342:             }
                   3343:         }
                   3344:         return;
                   3345:     }
1.275     raeburn  3346:     foreach my $tool (@{$usertools}) {
1.362     raeburn  3347:         my ($newval,$envkey);
                   3348:         $envkey = $context.'.'.$tool;
1.306     raeburn  3349:         if ($context eq 'requestcourses') {
                   3350:             $newval = $env{'form.crsreq_'.$tool};
                   3351:             if ($newval eq 'autolimit') {
                   3352:                 $newval .= '='.$env{'form.crsreq_'.$tool.'_limit'};
                   3353:             }
1.362     raeburn  3354:         } elsif ($context eq 'requestauthor') {
                   3355:             $newval = $env{'form.'.$context};
                   3356:             $envkey = $context;
1.314     raeburn  3357:         } else {
1.306     raeburn  3358:             $newval = $env{'form.'.$context.'_'.$tool};
                   3359:         }
1.362     raeburn  3360:         if ($userenv->{$envkey} ne '') {
1.275     raeburn  3361:             $oldaccess->{$tool} = &mt('custom');
1.362     raeburn  3362:             if ($userenv->{$envkey}) {
1.275     raeburn  3363:                 $oldaccesstext->{$tool} = &mt("availability set to 'on'");
                   3364:             } else {
                   3365:                 $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3366:             }
1.362     raeburn  3367:             $changeHash->{$envkey} = $userenv->{$envkey};
1.275     raeburn  3368:             if ($env{'form.custom'.$tool} == 1) {
1.362     raeburn  3369:                 if ($newval ne $userenv->{$envkey}) {
1.306     raeburn  3370:                     $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3371:                                                     $context);
1.275     raeburn  3372:                     if ($changed->{$tool}) {
                   3373:                         $newaccess->{$tool} = &mt('custom');
1.306     raeburn  3374:                         if ($newval) {
1.275     raeburn  3375:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3376:                         } else {
                   3377:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3378:                         }
                   3379:                     } else {
                   3380:                         $newaccess->{$tool} = $oldaccess->{$tool};
                   3381:                         if ($userenv->{$context.'.'.$tool}) {
                   3382:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3383:                         } else {
                   3384:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3385:                         }
                   3386:                     }
                   3387:                 } else {
                   3388:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3389:                     $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3390:                 }
                   3391:             } else {
                   3392:                 $changed->{$tool} = &tool_admin($tool,'',$changeHash,$context);
                   3393:                 if ($changed->{$tool}) {
                   3394:                     $newaccess->{$tool} = &mt('default');
                   3395:                 } else {
                   3396:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3397:                     if ($userenv->{$context.'.'.$tool}) {
1.300     raeburn  3398:                         $newaccesstext->{$tool} = &mt("availability set to 'on'");
1.275     raeburn  3399:                     } else {
1.300     raeburn  3400:                         $newaccesstext->{$tool} = &mt("availability set to 'off'");
1.275     raeburn  3401:                     }
                   3402:                 }
                   3403:             }
                   3404:         } else {
                   3405:             $oldaccess->{$tool} = &mt('default');
                   3406:             if ($env{'form.custom'.$tool} == 1) {
1.306     raeburn  3407:                 $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3408:                                                 $context);
1.275     raeburn  3409:                 if ($changed->{$tool}) {
                   3410:                     $newaccess->{$tool} = &mt('custom');
1.306     raeburn  3411:                     if ($newval) {
1.275     raeburn  3412:                         $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3413:                     } else {
                   3414:                         $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3415:                     }
                   3416:                 } else {
                   3417:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3418:                 }
                   3419:             } else {
                   3420:                 $newaccess->{$tool} = $oldaccess->{$tool};
                   3421:             }
                   3422:         }
                   3423:     }
                   3424:     return;
                   3425: }
                   3426: 
1.220     raeburn  3427: sub update_roles {
1.375     raeburn  3428:     my ($r,$context,$showcredits) = @_;
1.4       www      3429:     my $now=time;
1.225     raeburn  3430:     my @rolechanges;
1.220     raeburn  3431:     my %disallowed;
1.73      sakharuk 3432:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
1.135     raeburn  3433:     foreach my $key (keys (%env)) {
                   3434: 	next if (! $env{$key});
1.190     raeburn  3435:         next if ($key eq 'form.action');
1.27      matthew  3436: 	# Revoke roles
1.135     raeburn  3437: 	if ($key=~/^form\.rev/) {
                   3438: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
1.64      www      3439: # Revoke standard role
1.170     albertel 3440: 		my ($scope,$role) = ($1,$2);
                   3441: 		my $result =
                   3442: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
                   3443: 						$env{'form.ccuname'},
1.239     raeburn  3444: 						$scope,$role,'','',$context);
1.367     golterma 3445:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3446:                             &mt('Revoking [_1] in [_2]',
                   3447:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  3448:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   3449:                                 $result ne "ok").'<br />');
                   3450:                 if ($result ne "ok") {
                   3451:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3452:                 }
1.170     albertel 3453: 		if ($role eq 'st') {
1.202     raeburn  3454: 		    my $result = 
1.198     raeburn  3455:                         &Apache::lonuserutils::classlist_drop($scope,
                   3456:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3457: 			    $now);
1.367     golterma 3458:                     $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.53      www      3459: 		}
1.225     raeburn  3460:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3461:                     push(@rolechanges,$role);
                   3462:                 }
1.196     raeburn  3463: 	    }
1.195     raeburn  3464: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}s) {
1.64      www      3465: # Revoke custom role
1.369     bisitz   3466:                 my $result = &Apache::lonnet::revokecustomrole(
                   3467:                     $env{'form.ccdomain'},$env{'form.ccuname'},$1,$2,$3,$4,'','',$context);
1.367     golterma 3468:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
1.369     bisitz   3469:                             &mt('Revoking custom role [_1] by [_2] in [_3]',
1.372     raeburn  3470:                                 $4,$3.':'.$2,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3471:                             $result ne 'ok').'<br />');
                   3472:                 if ($result ne "ok") {
                   3473:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3474:                 }
1.225     raeburn  3475:                 if (!grep(/^cr$/,@rolechanges)) {
                   3476:                     push(@rolechanges,'cr');
                   3477:                 }
1.64      www      3478: 	    }
1.135     raeburn  3479: 	} elsif ($key=~/^form\.del/) {
                   3480: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
1.116     raeburn  3481: # Delete standard role
1.170     albertel 3482: 		my ($scope,$role) = ($1,$2);
                   3483: 		my $result =
                   3484: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
                   3485: 						$env{'form.ccuname'},
1.239     raeburn  3486: 						$scope,$role,$now,0,1,'',
                   3487:                                                 $context);
1.367     golterma 3488:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   3489:                             &mt('Deleting [_1] in [_2]',
1.369     bisitz   3490:                                 &Apache::lonnet::plaintext($role),
1.372     raeburn  3491:                                 &Apache::loncommon::show_role_extent($scope,$context,$role)),
1.369     bisitz   3492:                             $result ne 'ok').'<br />');
                   3493:                 if ($result ne "ok") {
                   3494:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3495:                 }
1.367     golterma 3496: 
1.170     albertel 3497: 		if ($role eq 'st') {
1.202     raeburn  3498: 		    my $result = 
1.198     raeburn  3499:                         &Apache::lonuserutils::classlist_drop($scope,
                   3500:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3501: 			    $now);
1.369     bisitz   3502: 		    $r->print(&Apache::lonhtmlcommon::confirm_success($result));
1.81      albertel 3503: 		}
1.225     raeburn  3504:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3505:                     push(@rolechanges,$role);
                   3506:                 }
1.116     raeburn  3507:             }
1.139     albertel 3508: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3509:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3510: # Delete custom role
1.369     bisitz   3511:                 my $result =
                   3512:                     &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
                   3513:                         $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
                   3514:                         0,1,$context);
                   3515:                 $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Deleting custom role [_1] by [_2] in [_3]',
1.372     raeburn  3516:                       $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3517:                       $result ne "ok").'<br />');
                   3518:                 if ($result ne "ok") {
                   3519:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3520:                 }
1.367     golterma 3521: 
1.225     raeburn  3522:                 if (!grep(/^cr$/,@rolechanges)) {
                   3523:                     push(@rolechanges,'cr');
                   3524:                 }
1.116     raeburn  3525:             }
1.135     raeburn  3526: 	} elsif ($key=~/^form\.ren/) {
1.101     albertel 3527:             my $udom = $env{'form.ccdomain'};
                   3528:             my $uname = $env{'form.ccuname'};
1.116     raeburn  3529: # Re-enable standard role
1.135     raeburn  3530: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
1.89      raeburn  3531:                 my $url = $1;
                   3532:                 my $role = $2;
                   3533:                 my $logmsg;
                   3534:                 my $output;
                   3535:                 if ($role eq 'st') {
1.141     albertel 3536:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
1.374     raeburn  3537:                         my ($cdom,$cnum,$csec) = ($1,$2,$3);
1.375     raeburn  3538:                         my $credits;
                   3539:                         if ($showcredits) {
                   3540:                             my $defaultcredits = 
                   3541:                                 &Apache::lonuserutils::get_defaultcredits($cdom,$cnum);
                   3542:                             $credits = &get_user_credits($defaultcredits,$cdom,$cnum);
                   3543:                         }
                   3544:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$cdom,$cnum,$csec,$context,$credits);
1.220     raeburn  3545:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course') || ($result eq 'refused')) {
1.223     raeburn  3546:                             if ($result eq 'refused' && $logmsg) {
                   3547:                                 $output = $logmsg;
                   3548:                             } else { 
1.369     bisitz   3549:                                 $output = &mt('Error: [_1]',$result)."\n";
1.223     raeburn  3550:                             }
1.89      raeburn  3551:                         } else {
1.372     raeburn  3552:                             $output = &Apache::lonhtmlcommon::confirm_success(&mt('Assigning [_1] in [_2] starting [_3]',
                   3553:                                         &Apache::lonnet::plaintext($role),
                   3554:                                         &Apache::loncommon::show_role_extent($url,$context,'st'),
                   3555:                                         &Apache::lonlocal::locallocaltime($now))).'<br />'.$logmsg.'<br />';
1.89      raeburn  3556:                         }
                   3557:                     }
                   3558:                 } else {
1.101     albertel 3559: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
1.239     raeburn  3560:                                $env{'form.ccuname'},$url,$role,0,$now,'','',
                   3561:                                $context);
1.367     golterma 3562:                         $output = &Apache::lonhtmlcommon::confirm_success(&mt('Re-enabling [_1] in [_2]',
1.372     raeburn  3563:                                         &Apache::lonnet::plaintext($role),
                   3564:                                         &Apache::loncommon::show_role_extent($url,$context,$role)),$result ne "ok").'<br />';
1.369     bisitz   3565:                     if ($result ne "ok") {
                   3566:                         $output .= &mt('Error: [_1]',$result).'<br />';
                   3567:                     }
                   3568:                 }
1.89      raeburn  3569:                 $r->print($output);
1.225     raeburn  3570:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3571:                     push(@rolechanges,$role);
                   3572:                 }
1.113     raeburn  3573: 	    }
1.116     raeburn  3574: # Re-enable custom role
1.139     albertel 3575: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3576:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3577:                 my $result = &Apache::lonnet::assigncustomrole(
                   3578:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
1.240     raeburn  3579:                                $url,$rdom,$rnam,$rolename,0,$now,undef,$context);
1.369     bisitz   3580:                 $r->print(&Apache::lonhtmlcommon::confirm_success(
                   3581:                     &mt('Re-enabling custom role [_1] by [_2] in [_3]',
1.372     raeburn  3582:                         $rolename,$rnam.':'.$rdom,&Apache::loncommon::show_role_extent($1,$context,'cr')),
1.369     bisitz   3583:                     $result ne "ok").'<br />');
                   3584:                 if ($result ne "ok") {
                   3585:                     $r->print(&mt('Error: [_1]',$result).'<br />');
                   3586:                 }
1.225     raeburn  3587:                 if (!grep(/^cr$/,@rolechanges)) {
                   3588:                     push(@rolechanges,'cr');
                   3589:                 }
1.116     raeburn  3590:             }
1.135     raeburn  3591: 	} elsif ($key=~/^form\.act/) {
1.101     albertel 3592:             my $udom = $env{'form.ccdomain'};
                   3593:             my $uname = $env{'form.ccuname'};
1.141     albertel 3594: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
1.65      www      3595:                 # Activate a custom role
1.83      albertel 3596: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
                   3597: 		my $url='/'.$one.'/'.$two;
                   3598: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
1.65      www      3599: 
1.101     albertel 3600:                 my $start = ( $env{'form.start_'.$full} ?
                   3601:                               $env{'form.start_'.$full} :
1.88      raeburn  3602:                               $now );
1.101     albertel 3603:                 my $end   = ( $env{'form.end_'.$full} ?
                   3604:                               $env{'form.end_'.$full} :
1.88      raeburn  3605:                               0 );
                   3606:                                                                                      
                   3607:                 # split multiple sections
                   3608:                 my %sections = ();
1.101     albertel 3609:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
1.88      raeburn  3610:                 if ($num_sections == 0) {
1.240     raeburn  3611:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3612:                 } else {
1.114     albertel 3613: 		    my %curr_groups =
1.117     raeburn  3614: 			&Apache::longroup::coursegroups($one,$two);
1.113     raeburn  3615:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3616:                         if (($sec eq 'none') || ($sec eq 'all') || 
                   3617:                             exists($curr_groups{$sec})) {
                   3618:                             $disallowed{$sec} = $url;
                   3619:                             next;
                   3620:                         }
                   3621:                         my $securl = $url.'/'.$sec;
1.240     raeburn  3622: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3623:                     }
                   3624:                 }
1.225     raeburn  3625:                 if (!grep(/^cr$/,@rolechanges)) {
                   3626:                     push(@rolechanges,'cr');
                   3627:                 }
1.142     raeburn  3628: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
1.27      matthew  3629: 		# Activate roles for sections with 3 id numbers
                   3630: 		# set start, end times, and the url for the class
1.83      albertel 3631: 		my ($one,$two,$three)=($1,$2,$3);
1.101     albertel 3632: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
                   3633: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
1.27      matthew  3634: 			      $now );
1.101     albertel 3635: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
                   3636: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  3637: 			      0 );
1.83      albertel 3638: 		my $url='/'.$one.'/'.$two;
1.88      raeburn  3639:                 my $type = 'three';
                   3640:                 # split multiple sections
                   3641:                 my %sections = ();
1.101     albertel 3642:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
1.375     raeburn  3643:                 my $credits;
                   3644:                 if ($three eq 'st') {
                   3645:                     if ($showcredits) { 
                   3646:                         my $defaultcredits = 
                   3647:                             &Apache::lonuserutils::get_defaultcredits($one,$two);
                   3648:                         $credits = $env{'form.credits_'.$one.'_'.$two.'_'.$three};
                   3649:                         $credits =~ s/[^\d\.]//g;
                   3650:                         if ($credits eq $defaultcredits) {
                   3651:                             undef($credits);
                   3652:                         }
                   3653:                     }
                   3654:                 }
1.88      raeburn  3655:                 if ($num_sections == 0) {
1.375     raeburn  3656:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  3657:                 } else {
1.114     albertel 3658:                     my %curr_groups = 
1.117     raeburn  3659: 			&Apache::longroup::coursegroups($one,$two);
1.88      raeburn  3660:                     my $emptysec = 0;
                   3661:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3662:                         $sec =~ s/\W//g;
1.113     raeburn  3663:                         if ($sec ne '') {
                   3664:                             if (($sec eq 'none') || ($sec eq 'all') || 
                   3665:                                 exists($curr_groups{$sec})) {
                   3666:                                 $disallowed{$sec} = $url;
                   3667:                                 next;
                   3668:                             }
1.88      raeburn  3669:                             my $securl = $url.'/'.$sec;
1.375     raeburn  3670:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec,$context,$credits));
1.88      raeburn  3671:                         } else {
                   3672:                             $emptysec = 1;
                   3673:                         }
                   3674:                     }
                   3675:                     if ($emptysec) {
1.375     raeburn  3676:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context,$credits));
1.88      raeburn  3677:                     }
1.225     raeburn  3678:                 }
                   3679:                 if (!grep(/^\Q$three\E$/,@rolechanges)) {
                   3680:                     push(@rolechanges,$three);
                   3681:                 }
1.135     raeburn  3682: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
1.27      matthew  3683: 		# Activate roles for sections with two id numbers
                   3684: 		# set start, end times, and the url for the class
1.101     albertel 3685: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
                   3686: 			      $env{'form.start_'.$1.'_'.$2} : 
1.27      matthew  3687: 			      $now );
1.101     albertel 3688: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
                   3689: 			      $env{'form.end_'.$1.'_'.$2} :
1.27      matthew  3690: 			      0 );
1.225     raeburn  3691:                 my $one = $1;
                   3692:                 my $two = $2;
                   3693: 		my $url='/'.$one.'/';
1.88      raeburn  3694:                 # split multiple sections
                   3695:                 my %sections = ();
1.225     raeburn  3696:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two},\%sections,$two);
1.88      raeburn  3697:                 if ($num_sections == 0) {
1.240     raeburn  3698:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  3699:                 } else {
                   3700:                     my $emptysec = 0;
                   3701:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3702:                         if ($sec ne '') {
                   3703:                             my $securl = $url.'/'.$sec;
1.240     raeburn  3704:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$two,$start,$end,$one,undef,$sec,$context));
1.88      raeburn  3705:                         } else {
                   3706:                             $emptysec = 1;
                   3707:                         }
                   3708:                     }
                   3709:                     if ($emptysec) {
1.240     raeburn  3710:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  3711:                     }
                   3712:                 }
1.225     raeburn  3713:                 if (!grep(/^\Q$two\E$/,@rolechanges)) {
                   3714:                     push(@rolechanges,$two);
                   3715:                 }
1.64      www      3716: 	    } else {
1.190     raeburn  3717: 		$r->print('<p><span class="LC_error">'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></span></p><br />');
1.64      www      3718:             }
1.113     raeburn  3719:             foreach my $key (sort(keys(%disallowed))) {
1.274     bisitz   3720:                 $r->print('<p class="LC_warning">');
1.113     raeburn  3721:                 if (($key eq 'none') || ($key eq 'all')) {  
1.274     bisitz   3722:                     $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  3723:                 } else {
1.274     bisitz   3724:                     $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  3725:                 }
1.274     bisitz   3726:                 $r->print('</p><p>'
                   3727:                          .&mt('Please [_1]go back[_2] and choose a different section name.'
                   3728:                              ,'<a href="javascript:history.go(-1)'
                   3729:                              ,'</a>')
                   3730:                          .'</p><br />'
                   3731:                 );
1.113     raeburn  3732:             }
                   3733: 	}
1.101     albertel 3734:     } # End of foreach (keys(%env))
1.75      www      3735: # Flush the course logs so reverse user roles immediately updated
1.349     raeburn  3736:     $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.225     raeburn  3737:     if (@rolechanges == 0) {
1.372     raeburn  3738:         $r->print('<p>'.&mt('No roles to modify').'</p>');
1.193     raeburn  3739:     }
1.225     raeburn  3740:     return @rolechanges;
1.220     raeburn  3741: }
                   3742: 
1.375     raeburn  3743: sub get_user_credits {
                   3744:     my ($uname,$udom,$defaultcredits,$cdom,$cnum) = @_;
                   3745:     if ($cdom eq '' || $cnum eq '') {
                   3746:         return unless ($env{'request.course.id'});
                   3747:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   3748:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   3749:     }
                   3750:     my $credits;
                   3751:     my %currhash =
                   3752:         &Apache::lonnet::get('classlist',[$uname.':'.$udom],$cdom,$cnum);
                   3753:     if (keys(%currhash) > 0) {
                   3754:         my @items = split(/:/,$currhash{$uname.':'.$udom});
                   3755:         my $crdidx = &Apache::loncoursedata::CL_CREDITS() - 3;
                   3756:         $credits = $items[$crdidx];
                   3757:         $credits =~ s/[^\d\.]//g;
                   3758:     }
                   3759:     if ($credits eq $defaultcredits) {
                   3760:         undef($credits);
                   3761:     }
                   3762:     return $credits;
                   3763: }
                   3764: 
1.220     raeburn  3765: sub enroll_single_student {
1.375     raeburn  3766:     my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype,
                   3767:         $showcredits,$defaultcredits) = @_;
1.318     raeburn  3768:     $r->print('<h3>');
                   3769:     if ($crstype eq 'Community') {
                   3770:         $r->print(&mt('Enrolling Member'));
                   3771:     } else {
                   3772:         $r->print(&mt('Enrolling Student'));
                   3773:     }
                   3774:     $r->print('</h3>');
1.220     raeburn  3775: 
                   3776:     # Remove non alphanumeric values from section
                   3777:     $env{'form.sections'}=~s/\W//g;
                   3778: 
1.375     raeburn  3779:     my $credits;
                   3780:     if (($showcredits) && ($env{'form.credits'} ne '')) {
                   3781:         $credits = $env{'form.credits'};
                   3782:         $credits =~ s/[^\d\.]//g;
                   3783:         if ($credits ne '') {
                   3784:             if ($credits eq $defaultcredits) {
                   3785:                 undef($credits);
                   3786:             }
                   3787:         }
                   3788:     }
                   3789: 
1.220     raeburn  3790:     # Clean out any old student roles the user has in this class.
                   3791:     &Apache::lonuserutils::modifystudent($env{'form.ccdomain'},
                   3792:          $env{'form.ccuname'},$env{'request.course.id'},undef,$uhome);
                   3793:     my ($startdate,$enddate) = &Apache::lonuserutils::get_dates_from_form();
                   3794:     my $enroll_result =
                   3795:         &Apache::lonnet::modify_student_enrollment($env{'form.ccdomain'},
                   3796:             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
                   3797:             $env{'form.cmiddlename'},$env{'form.clastname'},
                   3798:             $env{'form.generation'},$env{'form.sections'},$enddate,
1.375     raeburn  3799:             $startdate,'manual',undef,$env{'request.course.id'},'',$context,
                   3800:             $credits);
1.220     raeburn  3801:     if ($enroll_result =~ /^ok/) {
                   3802:         $r->print(&mt('<b>[_1]</b> enrolled',$env{'form.ccuname'}.':'.$env{'form.ccdomain'}));
                   3803:         if ($env{'form.sections'} ne '') {
                   3804:             $r->print(' '.&mt('in section [_1]',$env{'form.sections'}));
                   3805:         }
                   3806:         my ($showstart,$showend);
                   3807:         if ($startdate <= $now) {
                   3808:             $showstart = &mt('Access starts immediately');
                   3809:         } else {
                   3810:             $showstart = &mt('Access starts: ').&Apache::lonlocal::locallocaltime($startdate);
                   3811:         }
                   3812:         if ($enddate == 0) {
                   3813:             $showend = &mt('ends: no ending date');
                   3814:         } else {
                   3815:             $showend = &mt('ends: ').&Apache::lonlocal::locallocaltime($enddate);
                   3816:         }
                   3817:         $r->print('.<br />'.$showstart.'; '.$showend);
                   3818:         if ($startdate <= $now && !$newuser) {
1.318     raeburn  3819:             $r->print('<p> ');
                   3820:             if ($crstype eq 'Community') {
                   3821:                 $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.'));
                   3822:             } else {
                   3823:                 $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.'));
                   3824:            }
                   3825:            $r->print('</p>');
1.220     raeburn  3826:         }
                   3827:     } else {
                   3828:         $r->print(&mt('unable to enroll').": ".$enroll_result);
                   3829:     }
                   3830:     return;
1.188     raeburn  3831: }
                   3832: 
1.204     raeburn  3833: sub get_defaultquota_text {
                   3834:     my ($settingstatus) = @_;
                   3835:     my $defquotatext; 
                   3836:     if ($settingstatus eq '') {
                   3837:         $defquotatext = &mt('(default)');
                   3838:     } else {
                   3839:         my ($usertypes,$order) =
                   3840:             &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
                   3841:         if ($usertypes->{$settingstatus} eq '') {
                   3842:             $defquotatext = &mt('(default)');
                   3843:         } else {
                   3844:             $defquotatext = &mt('(default for [_1])',$usertypes->{$settingstatus});
                   3845:         }
                   3846:     }
                   3847:     return $defquotatext;
                   3848: }
                   3849: 
1.188     raeburn  3850: sub update_result_form {
                   3851:     my ($uhome) = @_;
                   3852:     my $outcome = 
1.367     golterma 3853:     '<form name="userupdate" method="post" action="">'."\n";
1.160     raeburn  3854:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
1.188     raeburn  3855:         $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  3856:     }
1.207     raeburn  3857:     if ($env{'form.origname'} ne '') {
                   3858:         $outcome .= '<input type="hidden" name="origname" value="'.$env{'form.origname'}.'" />'."\n";
                   3859:     }
1.160     raeburn  3860:     foreach my $item ('sortby','seluname','seludom') {
                   3861:         if (exists($env{'form.'.$item})) {
1.188     raeburn  3862:             $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  3863:         }
                   3864:     }
1.188     raeburn  3865:     if ($uhome eq 'no_host') {
                   3866:         $outcome .= '<input type="hidden" name="forcenewuser" value="1" />'."\n";
                   3867:     }
                   3868:     $outcome .= '<input type="hidden" name="phase" value="" />'."\n".
                   3869:                 '<input type ="hidden" name="currstate" value="" />'."\n".
1.220     raeburn  3870:                 '<input type ="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1.188     raeburn  3871:                 '</form>';
                   3872:     return $outcome;
1.4       www      3873: }
                   3874: 
1.149     raeburn  3875: sub quota_admin {
1.378   ! raeburn  3876:     my ($setquota,$changeHash,$name) = @_;
1.149     raeburn  3877:     my $quotachanged;
                   3878:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   3879:         # Current user has quota modification privileges
1.267     raeburn  3880:         if (ref($changeHash) eq 'HASH') {
                   3881:             $quotachanged = 1;
1.378   ! raeburn  3882:             $changeHash->{$name.'quota'} = $setquota;
1.267     raeburn  3883:         }
1.149     raeburn  3884:     }
                   3885:     return $quotachanged;
                   3886: }
                   3887: 
1.267     raeburn  3888: sub tool_admin {
1.275     raeburn  3889:     my ($tool,$settool,$changeHash,$context) = @_;
                   3890:     my $canchange = 0; 
1.279     raeburn  3891:     if ($context eq 'requestcourses') {
1.275     raeburn  3892:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
                   3893:             $canchange = 1;
                   3894:         }
1.300     raeburn  3895:     } elsif ($context eq 'reqcrsotherdom') {
                   3896:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   3897:             $canchange = 1;
                   3898:         }
1.362     raeburn  3899:     } elsif ($context eq 'requestauthor') {
                   3900:         if (&Apache::lonnet::allowed('cau',$env{'request.role.domain'})) {
                   3901:             $canchange = 1;
                   3902:         }
1.275     raeburn  3903:     } elsif (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   3904:         # Current user has quota modification privileges
                   3905:         $canchange = 1;
                   3906:     }
1.267     raeburn  3907:     my $toolchanged;
1.275     raeburn  3908:     if ($canchange) {
1.267     raeburn  3909:         if (ref($changeHash) eq 'HASH') {
                   3910:             $toolchanged = 1;
1.362     raeburn  3911:             if ($tool eq 'requestauthor') {
                   3912:                 $changeHash->{$context} = $settool;
                   3913:             } else {
                   3914:                 $changeHash->{$context.'.'.$tool} = $settool;
                   3915:             }
1.267     raeburn  3916:         }
                   3917:     }
                   3918:     return $toolchanged;
                   3919: }
                   3920: 
1.88      raeburn  3921: sub build_roles {
1.89      raeburn  3922:     my ($sectionstr,$sections,$role) = @_;
1.88      raeburn  3923:     my $num_sections = 0;
                   3924:     if ($sectionstr=~ /,/) {
                   3925:         my @secnums = split/,/,$sectionstr;
1.89      raeburn  3926:         if ($role eq 'st') {
                   3927:             $secnums[0] =~ s/\W//g;
                   3928:             $$sections{$secnums[0]} = 1;
                   3929:             $num_sections = 1;
                   3930:         } else {
                   3931:             foreach my $sec (@secnums) {
                   3932:                 $sec =~ ~s/\W//g;
1.150     banghart 3933:                 if (!($sec eq "")) {
1.89      raeburn  3934:                     if (exists($$sections{$sec})) {
                   3935:                         $$sections{$sec} ++;
                   3936:                     } else {
                   3937:                         $$sections{$sec} = 1;
                   3938:                         $num_sections ++;
                   3939:                     }
1.88      raeburn  3940:                 }
                   3941:             }
                   3942:         }
                   3943:     } else {
                   3944:         $sectionstr=~s/\W//g;
                   3945:         unless ($sectionstr eq '') {
                   3946:             $$sections{$sectionstr} = 1;
                   3947:             $num_sections ++;
                   3948:         }
                   3949:     }
1.129     albertel 3950: 
1.88      raeburn  3951:     return $num_sections;
                   3952: }
                   3953: 
1.58      www      3954: # ========================================================== Custom Role Editor
                   3955: 
                   3956: sub custom_role_editor {
1.351     raeburn  3957:     my ($r,$brcrum) = @_;
1.324     raeburn  3958:     my $action = $env{'form.customroleaction'};
                   3959:     my $rolename; 
                   3960:     if ($action eq 'new') {
                   3961:         $rolename=$env{'form.newrolename'};
                   3962:     } else {
                   3963:         $rolename=$env{'form.rolename'};
1.59      www      3964:     }
                   3965: 
1.324     raeburn  3966:     my ($crstype,$context);
                   3967:     if ($env{'request.course.id'}) {
                   3968:         $crstype = &Apache::loncommon::course_type();
                   3969:         $context = 'course';
                   3970:     } else {
                   3971:         $context = 'domain';
                   3972:         $crstype = $env{'form.templatecrstype'};
                   3973:     }
1.351     raeburn  3974: 
                   3975:     $rolename=~s/[^A-Za-z0-9]//gs;
                   3976:     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
                   3977: 	&print_username_entry_form($r,undef,undef,undef,undef,$crstype,$brcrum);
                   3978:         return;
                   3979:     }
                   3980: 
1.153     banghart 3981: # ------------------------------------------------------- What can be assigned?
                   3982:     my %full=();
                   3983:     my %courselevel=();
                   3984:     my %courselevelcurrent=();
1.61      www      3985:     my $syspriv='';
                   3986:     my $dompriv='';
                   3987:     my $coursepriv='';
1.153     banghart 3988:     my $body_top;
1.59      www      3989:     my ($rdummy,$roledef)=
                   3990: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
1.60      www      3991: # ------------------------------------------------------- Does this role exist?
1.153     banghart 3992:     $body_top .= '<h2>';
1.59      www      3993:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.153     banghart 3994: 	$body_top .= &mt('Existing Role').' "';
1.61      www      3995: # ------------------------------------------------- Get current role privileges
                   3996: 	($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
1.324     raeburn  3997:         if ($crstype eq 'Community') {
                   3998:             $syspriv =~ s/bre\&S//;   
                   3999:         }
1.59      www      4000:     } else {
1.153     banghart 4001: 	$body_top .= &mt('New Role').' "';
1.59      www      4002: 	$roledef='';
                   4003:     }
1.153     banghart 4004:     $body_top .= $rolename.'"</h2>';
1.135     raeburn  4005:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   4006: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4007:         if (!$restrict) { $restrict='F'; }
1.60      www      4008:         $courselevel{$priv}=$restrict;
1.61      www      4009:         if ($coursepriv=~/\:$priv/) {
                   4010: 	    $courselevelcurrent{$priv}=1;
                   4011: 	}
1.60      www      4012: 	$full{$priv}=1;
                   4013:     }
                   4014:     my %domainlevel=();
1.61      www      4015:     my %domainlevelcurrent=();
1.135     raeburn  4016:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   4017: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4018:         if (!$restrict) { $restrict='F'; }
1.60      www      4019:         $domainlevel{$priv}=$restrict;
1.61      www      4020:         if ($dompriv=~/\:$priv/) {
                   4021: 	    $domainlevelcurrent{$priv}=1;
                   4022: 	}
1.60      www      4023: 	$full{$priv}=1;
                   4024:     }
1.61      www      4025:     my %systemlevel=();
                   4026:     my %systemlevelcurrent=();
1.135     raeburn  4027:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   4028: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4029:         if (!$restrict) { $restrict='F'; }
1.61      www      4030:         $systemlevel{$priv}=$restrict;
                   4031:         if ($syspriv=~/\:$priv/) {
                   4032: 	    $systemlevelcurrent{$priv}=1;
                   4033: 	}
                   4034: 	$full{$priv}=1;
                   4035:     }
1.160     raeburn  4036:     my ($jsback,$elements) = &crumb_utilities();
1.154     banghart 4037:     my $button_code = "\n";
1.153     banghart 4038:     my $head_script = "\n";
1.301     bisitz   4039:     $head_script .= '<script type="text/javascript">'."\n"
                   4040:                    .'// <![CDATA['."\n";
1.324     raeburn  4041:     my @template_roles = ("in","ta","ep");
                   4042:     if ($context eq 'domain') {
                   4043:         push(@template_roles,"ad");
1.318     raeburn  4044:     }
1.324     raeburn  4045:     push(@template_roles,"st");
1.318     raeburn  4046:     if ($crstype eq 'Community') {
                   4047:         unshift(@template_roles,'co');
                   4048:     } else {
                   4049:         unshift(@template_roles,'cc');
                   4050:     }
1.154     banghart 4051:     foreach my $role (@template_roles) {
1.324     raeburn  4052:         $head_script .= &make_script_template($role,$crstype);
1.318     raeburn  4053:         $button_code .= &make_button_code($role,$crstype).' ';
1.154     banghart 4054:     }
1.324     raeburn  4055:     my $context_code;
                   4056:     if ($context eq 'domain') {
                   4057:         my $checkedCommunity = '';
                   4058:         my $checkedCourse = ' checked="checked"';
                   4059:         if ($env{'form.templatecrstype'} eq 'Community') {
                   4060:             $checkedCommunity = $checkedCourse;
                   4061:             $checkedCourse = '';
                   4062:         }
                   4063:         $context_code = '<label>'.
                   4064:                         '<input type="radio" name="templatecrstype" value="Course"'.$checkedCourse.' onclick="this.form.submit();">'.
                   4065:                         &mt('Course').
                   4066:                         '</label>'.('&nbsp;' x2).
                   4067:                         '<label>'.
                   4068:                         '<input type="radio" name="templatecrstype" value="Community"'.$checkedCommunity.' onclick="this.form.submit();">'.
                   4069:                         &mt('Community').
                   4070:                         '</label>'.
                   4071:                         '</fieldset>'.
                   4072:                         '<input type="hidden" name="customroleaction" value="'.
                   4073:                         $action.'" />';
                   4074:         if ($env{'form.customroleaction'} eq 'new') {
                   4075:             $context_code .= '<input type="hidden" name="newrolename" value="'.
                   4076:                              $rolename.'" />';
                   4077:         } else {
                   4078:             $context_code .= '<input type="hidden" name="rolename" value="'.
                   4079:                              $rolename.'" />';
                   4080:         }
                   4081:         $context_code .= '<input type="hidden" name="action" value="custom" />'.
                   4082:                          '<input type="hidden" name="phase" value="selected_custom_edit" />';
                   4083:     }
                   4084: 
1.301     bisitz   4085:     $head_script .= "\n".$jsback."\n"
                   4086:                    .'// ]]>'."\n"
                   4087:                    .'</script>'."\n";
1.351     raeburn  4088:     push (@{$brcrum},
                   4089:               {href => "javascript:backPage(document.form1,'pickrole','')",
                   4090:                text => "Pick custom role",
                   4091:                faq  => 282,bug=>'Instructor Interface',},
                   4092:               {href => "javascript:backPage(document.form1,'','')",
                   4093:                text => "Edit custom role",
                   4094:                faq  => 282,
                   4095:                bug  => 'Instructor Interface',
                   4096:                help => 'Course_Editing_Custom_Roles'}
                   4097:               );
                   4098:     my $args = { bread_crumbs          => $brcrum,
                   4099:                  bread_crumbs_component => 'User Management'};
                   4100:  
                   4101:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',
                   4102:                                              $head_script,$args).
                   4103:               $body_top);
1.73      sakharuk 4104:     my %lt=&Apache::lonlocal::texthash(
                   4105: 		    'prv'  => "Privilege",
1.131     raeburn  4106: 		    'crl'  => "Course Level",
1.73      sakharuk 4107:                     'dml'  => "Domain Level",
1.150     banghart 4108:                     'ssl'  => "System Level");
1.264     bisitz   4109: 
1.324     raeburn  4110:     $r->print('<div class="LC_left_float">'
1.264     bisitz   4111:              .'<form action=""><fieldset>'
                   4112:              .'<legend>'.&mt('Select a Template').'</legend>'
                   4113:              .$button_code
1.324     raeburn  4114:              .'</fieldset></form></div>');
                   4115:     if ($context_code) {
                   4116:         $r->print('<div class="LC_left_float">'
                   4117:                  .'<form action="/adm/createuser" method="post"><fieldset>'
                   4118:                  .'<legend>'.&mt('Context').'</legend>'
                   4119:                  .$context_code
                   4120:                  .'</form>'
                   4121:                  .'</div>'
                   4122:         );
                   4123:     }
                   4124:     $r->print('<br clear="all" />');
1.264     bisitz   4125: 
1.61      www      4126:     $r->print(<<ENDCCF);
1.160     raeburn  4127: <form name="form1" method="post">
1.61      www      4128: <input type="hidden" name="phase" value="set_custom_roles" />
                   4129: <input type="hidden" name="rolename" value="$rolename" />
                   4130: ENDCCF
1.135     raeburn  4131:     $r->print(&Apache::loncommon::start_data_table().
                   4132:               &Apache::loncommon::start_data_table_header_row(). 
                   4133: '<th>'.$lt{'prv'}.'</th><th>'.$lt{'crl'}.'</th><th>'.$lt{'dml'}.
                   4134: '</th><th>'.$lt{'ssl'}.'</th>'.
                   4135:               &Apache::loncommon::end_data_table_header_row());
1.324     raeburn  4136:     foreach my $priv (sort(keys(%full))) {
1.318     raeburn  4137:         my $privtext = &Apache::lonnet::plaintext($priv,$crstype);
1.135     raeburn  4138:         $r->print(&Apache::loncommon::start_data_table_row().
                   4139: 	          '<td>'.$privtext.'</td><td>'.
1.288     bisitz   4140:     ($courselevel{$priv}?'<input type="checkbox" name="'.$priv.'_c"'.
                   4141:     ($courselevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;').
1.61      www      4142:     '</td><td>'.
1.288     bisitz   4143:     ($domainlevel{$priv}?'<input type="checkbox" name="'.$priv.'_d"'.
                   4144:     ($domainlevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;').
1.324     raeburn  4145:     '</td><td>');
                   4146:         if ($priv eq 'bre' && $crstype eq 'Community') {
                   4147:             $r->print('&nbsp;');  
                   4148:         } else {
                   4149:             $r->print($systemlevel{$priv}?'<input type="checkbox" name="'.$priv.'_s"'.
                   4150:                       ($systemlevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;');
                   4151:         }
                   4152:         $r->print('</td>'.
                   4153:                   &Apache::loncommon::end_data_table_row());
1.60      www      4154:     }
1.135     raeburn  4155:     $r->print(&Apache::loncommon::end_data_table().
1.190     raeburn  4156:    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
1.160     raeburn  4157:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
1.179     raeburn  4158:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".   
1.160     raeburn  4159:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
1.351     raeburn  4160:    '<input type="submit" value="'.&mt('Save').'" /></form>');
1.61      www      4161: }
1.153     banghart 4162: # --------------------------------------------------------
                   4163: sub make_script_template {
1.324     raeburn  4164:     my ($role,$crstype) = @_;
1.153     banghart 4165:     my %full_c=();
                   4166:     my %full_d=();
                   4167:     my %full_s=();
                   4168:     my $return_script;
                   4169:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   4170:         my ($priv,$restrict)=split(/\&/,$item);
                   4171:         $full_c{$priv}=1;
                   4172:     }
                   4173:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   4174:         my ($priv,$restrict)=split(/\&/,$item);
                   4175:         $full_d{$priv}=1;
                   4176:     }
1.154     banghart 4177:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
1.324     raeburn  4178:         next if (($crstype eq 'Community') && ($item eq 'bre&S'));
1.153     banghart 4179:         my ($priv,$restrict)=split(/\&/,$item);
                   4180:         $full_s{$priv}=1;
                   4181:     }
                   4182:     $return_script .= 'function set_'.$role.'() {'."\n";
                   4183:     my @temp = split(/:/,$Apache::lonnet::pr{$role.':c'});
                   4184:     my %role_c;
1.155     banghart 4185:     foreach my $priv (@temp) {
1.153     banghart 4186:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   4187:         $role_c{$priv_item} = 1;
                   4188:     }
1.269     raeburn  4189:     my %role_d;
                   4190:     @temp = split(/:/,$Apache::lonnet::pr{$role.':d'});
                   4191:     foreach my $priv(@temp) {
                   4192:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   4193:         $role_d{$priv_item} = 1;
                   4194:     }
                   4195:     my %role_s;
                   4196:     @temp = split(/:/,$Apache::lonnet::pr{$role.':s'});
                   4197:     foreach my $priv(@temp) {
                   4198:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   4199:         $role_s{$priv_item} = 1;
                   4200:     }
1.153     banghart 4201:     foreach my $priv_item (keys(%full_c)) {
                   4202:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.269     raeburn  4203:         if ((exists($role_c{$priv})) || (exists($role_d{$priv})) || 
                   4204:             (exists($role_s{$priv}))) {
1.153     banghart 4205:             $return_script .= "document.form1.$priv"."_c.checked = true;\n";
                   4206:         } else {
                   4207:             $return_script .= "document.form1.$priv"."_c.checked = false;\n";
                   4208:         }
                   4209:     }
1.154     banghart 4210:     foreach my $priv_item (keys(%full_d)) {
                   4211:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.269     raeburn  4212:         if ((exists($role_d{$priv})) || (exists($role_s{$priv}))) {
1.154     banghart 4213:             $return_script .= "document.form1.$priv"."_d.checked = true;\n";
                   4214:         } else {
                   4215:             $return_script .= "document.form1.$priv"."_d.checked = false;\n";
                   4216:         }
                   4217:     }
                   4218:     foreach my $priv_item (keys(%full_s)) {
1.153     banghart 4219:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.154     banghart 4220:         if (exists($role_s{$priv})) {
                   4221:             $return_script .= "document.form1.$priv"."_s.checked = true;\n";
                   4222:         } else {
                   4223:             $return_script .= "document.form1.$priv"."_s.checked = false;\n";
                   4224:         }
1.153     banghart 4225:     }
                   4226:     $return_script .= '}'."\n";
1.154     banghart 4227:     return ($return_script);
                   4228: }
                   4229: # ----------------------------------------------------------
                   4230: sub make_button_code {
1.318     raeburn  4231:     my ($role,$crstype) = @_;
                   4232:     my $label = &Apache::lonnet::plaintext($role,$crstype);
1.301     bisitz   4233:     my $button_code = '<input type="button" onclick="set_'.$role.'()" value="'.$label.'" />';
1.154     banghart 4234:     return ($button_code);
1.153     banghart 4235: }
1.61      www      4236: # ---------------------------------------------------------- Call to definerole
                   4237: sub set_custom_role {
1.351     raeburn  4238:     my ($r,$context,$brcrum) = @_;
1.101     albertel 4239:     my $rolename=$env{'form.rolename'};
1.63      www      4240:     $rolename=~s/[^A-Za-z0-9]//gs;
1.150     banghart 4241:     if (!$rolename) {
1.351     raeburn  4242: 	&custom_role_editor($r,$brcrum);
1.61      www      4243:         return;
                   4244:     }
1.160     raeburn  4245:     my ($jsback,$elements) = &crumb_utilities();
1.301     bisitz   4246:     my $jscript = '<script type="text/javascript">'
                   4247:                  .'// <![CDATA['."\n"
                   4248:                  .$jsback."\n"
                   4249:                  .'// ]]>'."\n"
                   4250:                  .'</script>'."\n";
1.352     raeburn  4251:     push(@{$brcrum},
                   4252:         {href => "javascript:backPage(document.customresult,'pickrole','')",
                   4253:          text => "Pick custom role",
                   4254:          faq  => 282,
                   4255:          bug  => 'Instructor Interface',},
                   4256:         {href => "javascript:backPage(document.customresult,'selected_custom_edit','')",
                   4257:          text => "Edit custom role",
                   4258:          faq  => 282,
                   4259:          bug  => 'Instructor Interface',},
                   4260:         {href => "javascript:backPage(document.customresult,'set_custom_roles','')",
                   4261:          text => "Result",
                   4262:          faq  => 282,
                   4263:          bug  => 'Instructor Interface',
                   4264:          help => 'Course_Editing_Custom_Roles'},
                   4265:         );
                   4266:     my $args = { bread_crumbs           => $brcrum,
1.351     raeburn  4267:                  bread_crumbs_component => 'User Management'}; 
                   4268:     $r->print(&Apache::loncommon::start_page('Save Custom Role',$jscript,$args));
1.160     raeburn  4269: 
1.61      www      4270:     my ($rdummy,$roledef)=
1.110     albertel 4271: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   4272: 
1.61      www      4273: # ------------------------------------------------------- Does this role exist?
1.188     raeburn  4274:     $r->print('<h3>');
1.61      www      4275:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 4276: 	$r->print(&mt('Existing Role').' "');
1.61      www      4277:     } else {
1.73      sakharuk 4278: 	$r->print(&mt('New Role').' "');
1.61      www      4279: 	$roledef='';
                   4280:     }
1.188     raeburn  4281:     $r->print($rolename.'"</h3>');
1.61      www      4282: # ------------------------------------------------------- What can be assigned?
                   4283:     my $sysrole='';
                   4284:     my $domrole='';
                   4285:     my $courole='';
                   4286: 
1.135     raeburn  4287:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   4288: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4289:         if (!$restrict) { $restrict=''; }
                   4290:         if ($env{'form.'.$priv.'_c'}) {
1.135     raeburn  4291: 	    $courole.=':'.$item;
1.61      www      4292: 	}
                   4293:     }
                   4294: 
1.135     raeburn  4295:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   4296: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4297:         if (!$restrict) { $restrict=''; }
                   4298:         if ($env{'form.'.$priv.'_d'}) {
1.135     raeburn  4299: 	    $domrole.=':'.$item;
1.61      www      4300: 	}
                   4301:     }
                   4302: 
1.135     raeburn  4303:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   4304: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4305:         if (!$restrict) { $restrict=''; }
                   4306:         if ($env{'form.'.$priv.'_s'}) {
1.135     raeburn  4307: 	    $sysrole.=':'.$item;
1.61      www      4308: 	}
                   4309:     }
1.63      www      4310:     $r->print('<br />Defining Role: '.
1.61      www      4311: 	   &Apache::lonnet::definerole($rolename,$sysrole,$domrole,$courole));
1.101     albertel 4312:     if ($env{'request.course.id'}) {
                   4313:         my $url='/'.$env{'request.course.id'};
1.63      www      4314:         $url=~s/\_/\//g;
1.73      sakharuk 4315: 	$r->print('<br />'.&mt('Assigning Role to Self').': '.
1.101     albertel 4316: 	      &Apache::lonnet::assigncustomrole($env{'user.domain'},
                   4317: 						$env{'user.name'},
1.63      www      4318: 						$url,
1.101     albertel 4319: 						$env{'user.domain'},
                   4320: 						$env{'user.name'},
1.240     raeburn  4321: 						$rolename,undef,undef,undef,$context));
1.63      www      4322:     }
1.190     raeburn  4323:     $r->print('<p><a href="javascript:backPage(document.customresult,'."'pickrole'".')">'.&mt('Create or edit another custom role').'</a></p><form name="customresult" method="post">');
1.160     raeburn  4324:     $r->print(&Apache::lonhtmlcommon::echo_form_input([]).'</form>');
1.58      www      4325: }
                   4326: 
1.2       www      4327: # ================================================================ Main Handler
                   4328: sub handler {
                   4329:     my $r = shift;
                   4330:     if ($r->header_only) {
1.68      www      4331:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      4332:        $r->send_http_header;
                   4333:        return OK;
                   4334:     }
1.318     raeburn  4335:     my ($context,$crstype);
1.190     raeburn  4336:     if ($env{'request.course.id'}) {
                   4337:         $context = 'course';
1.318     raeburn  4338:         $crstype = &Apache::loncommon::course_type();
1.190     raeburn  4339:     } elsif ($env{'request.role'} =~ /^au\./) {
1.206     raeburn  4340:         $context = 'author';
1.190     raeburn  4341:     } else {
                   4342:         $context = 'domain';
                   4343:     }
1.375     raeburn  4344: 
1.190     raeburn  4345:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.233     raeburn  4346:         ['action','state','callingform','roletype','showrole','bulkaction','popup','phase',
                   4347:          'username','domain','srchterm','srchdomain','srchin','srchby','srchtype']);
1.190     raeburn  4348:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.351     raeburn  4349:     my $args;
                   4350:     my $brcrum = [];
                   4351:     my $bread_crumbs_component = 'User Management';
1.202     raeburn  4352:     if ($env{'form.action'} ne 'dateselect') {
1.351     raeburn  4353:         $brcrum = [{href=>"/adm/createuser",
                   4354:                     text=>"User Management",
                   4355:                     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'}
                   4356:                   ];
1.202     raeburn  4357:     }
1.289     droeschl 4358:     #SD Following files not added to help, because the corresponding .tex-files seem to
                   4359:     #be missing: Course_Approve_Selfenroll,Course_User_Logs,
1.209     raeburn  4360:     my ($permission,$allowed) = 
1.318     raeburn  4361:         &Apache::lonuserutils::get_permission($context,$crstype);
1.190     raeburn  4362:     if (!$allowed) {
1.358     raeburn  4363:         if ($context eq 'course') {
                   4364:             $r->internal_redirect('/adm/viewclasslist');
                   4365:             return OK;
                   4366:         }
1.190     raeburn  4367:         $env{'user.error.msg'}=
                   4368:             "/adm/createuser:cst:0:0:Cannot create/modify user data ".
                   4369:                                  "or view user status.";
                   4370:         return HTTP_NOT_ACCEPTABLE;
                   4371:     }
                   4372: 
                   4373:     &Apache::loncommon::content_type($r,'text/html');
                   4374:     $r->send_http_header;
                   4375: 
1.375     raeburn  4376:     my $showcredits;
                   4377:     if ((($context eq 'course') && ($crstype eq 'Course')) || 
                   4378:          ($context eq 'domain')) {
                   4379:         my %domdefaults = 
                   4380:             &Apache::lonnet::get_domain_defaults($env{'request.role.domain'});
                   4381:         if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'}) {
                   4382:             $showcredits = 1;
                   4383:         }
                   4384:     }
                   4385: 
1.190     raeburn  4386:     # Main switch on form.action and form.state, as appropriate
                   4387:     if (! exists($env{'form.action'})) {
1.351     raeburn  4388:         $args = {bread_crumbs => $brcrum,
                   4389:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4390:         $r->print(&header(undef,$args));
1.318     raeburn  4391:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4392:     } elsif ($env{'form.action'} eq 'upload' && $permission->{'cusr'}) {
1.351     raeburn  4393:         push(@{$brcrum},
                   4394:               { href => '/adm/createuser?action=upload&state=',
                   4395:                 text => 'Upload Users List',
                   4396:                 help => 'Course_Create_Class_List',
                   4397:               });
                   4398:         $bread_crumbs_component = 'Upload Users List';
                   4399:         $args = {bread_crumbs           => $brcrum,
                   4400:                  bread_crumbs_component => $bread_crumbs_component};
                   4401:         $r->print(&header(undef,$args));
1.190     raeburn  4402:         $r->print('<form name="studentform" method="post" '.
                   4403:                   'enctype="multipart/form-data" '.
                   4404:                   ' action="/adm/createuser">'."\n");
                   4405:         if (! exists($env{'form.state'})) {
                   4406:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4407:         } elsif ($env{'form.state'} eq 'got_file') {
1.375     raeburn  4408:             &Apache::lonuserutils::print_upload_manager_form($r,$context,$permission,
                   4409:                                                              $crstype,$showcredits);
1.190     raeburn  4410:         } elsif ($env{'form.state'} eq 'enrolling') {
                   4411:             if ($env{'form.datatoken'}) {
1.375     raeburn  4412:                 &Apache::lonuserutils::upfile_drop_add($r,$context,$permission,
                   4413:                                                        $showcredits);
1.190     raeburn  4414:             }
                   4415:         } else {
                   4416:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4417:         }
1.213     raeburn  4418:     } elsif ((($env{'form.action'} eq 'singleuser') || ($env{'form.action'}
                   4419:              eq 'singlestudent')) && ($permission->{'cusr'})) {
1.190     raeburn  4420:         my $phase = $env{'form.phase'};
                   4421:         my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
1.192     albertel 4422: 	&Apache::loncreateuser::restore_prev_selections();
                   4423: 	my $srch;
                   4424: 	foreach my $item (@search) {
                   4425: 	    $srch->{$item} = $env{'form.'.$item};
                   4426: 	}
1.207     raeburn  4427:         if (($phase eq 'get_user_info') || ($phase eq 'userpicked') ||
                   4428:             ($phase eq 'createnewuser')) {
                   4429:             if ($env{'form.phase'} eq 'createnewuser') {
                   4430:                 my $response;
                   4431:                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
1.366     bisitz   4432:                     my $response =
                   4433:                         '<span class="LC_warning">'
                   4434:                        .&mt('You must specify a valid username. Only the following are allowed:'
                   4435:                            .' letters numbers - . @')
                   4436:                        .'</span>';
1.221     raeburn  4437:                     $env{'form.phase'} = '';
1.375     raeburn  4438:                     &print_username_entry_form($r,$context,$response,$srch,undef,
                   4439:                                                $crstype,$brcrum,$showcredits);
1.207     raeburn  4440:                 } else {
                   4441:                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
                   4442:                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
                   4443:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4444:                                                   $srch,$response,$context,
1.375     raeburn  4445:                                                   $permission,$crstype,$brcrum,
                   4446:                                                   $showcredits);
1.207     raeburn  4447:                 }
                   4448:             } elsif ($env{'form.phase'} eq 'get_user_info') {
1.190     raeburn  4449:                 my ($currstate,$response,$forcenewuser,$results) = 
1.221     raeburn  4450:                     &user_search_result($context,$srch);
1.190     raeburn  4451:                 if ($env{'form.currstate'} eq 'modify') {
                   4452:                     $currstate = $env{'form.currstate'};
                   4453:                 }
                   4454:                 if ($currstate eq 'select') {
                   4455:                     &print_user_selection_page($r,$response,$srch,$results,
1.351     raeburn  4456:                                                \@search,$context,undef,$crstype,
                   4457:                                                $brcrum);
1.190     raeburn  4458:                 } elsif ($currstate eq 'modify') {
                   4459:                     my ($ccuname,$ccdomain);
                   4460:                     if (($srch->{'srchby'} eq 'uname') && 
                   4461:                         ($srch->{'srchtype'} eq 'exact')) {
                   4462:                         $ccuname = $srch->{'srchterm'};
                   4463:                         $ccdomain= $srch->{'srchdomain'};
                   4464:                     } else {
                   4465:                         my @matchedunames = keys(%{$results});
                   4466:                         ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
                   4467:                     }
                   4468:                     $ccuname =&LONCAPA::clean_username($ccuname);
                   4469:                     $ccdomain=&LONCAPA::clean_domain($ccdomain);
                   4470:                     if ($env{'form.forcenewuser'}) {
                   4471:                         $response = '';
                   4472:                     }
                   4473:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4474:                                                   $srch,$response,$context,
1.351     raeburn  4475:                                                   $permission,$crstype,$brcrum);
1.190     raeburn  4476:                 } elsif ($currstate eq 'query') {
1.351     raeburn  4477:                     &print_user_query_page($r,'createuser',$brcrum);
1.190     raeburn  4478:                 } else {
1.229     raeburn  4479:                     $env{'form.phase'} = '';
1.207     raeburn  4480:                     &print_username_entry_form($r,$context,$response,$srch,
1.351     raeburn  4481:                                                $forcenewuser,$crstype,$brcrum);
1.190     raeburn  4482:                 }
                   4483:             } elsif ($env{'form.phase'} eq 'userpicked') {
                   4484:                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
                   4485:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
1.196     raeburn  4486:                 &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
1.351     raeburn  4487:                                               $context,$permission,$crstype,
                   4488:                                               $brcrum);
1.190     raeburn  4489:             }
                   4490:         } elsif ($env{'form.phase'} eq 'update_user_data') {
1.375     raeburn  4491:             &update_user_data($r,$context,$crstype,$brcrum,$showcredits);
1.190     raeburn  4492:         } else {
1.351     raeburn  4493:             &print_username_entry_form($r,$context,undef,$srch,undef,$crstype,
                   4494:                                        $brcrum);
1.190     raeburn  4495:         }
                   4496:     } elsif ($env{'form.action'} eq 'custom' && $permission->{'custom'}) {
                   4497:         if ($env{'form.phase'} eq 'set_custom_roles') {
1.351     raeburn  4498:             &set_custom_role($r,$context,$brcrum);
1.190     raeburn  4499:         } else {
1.351     raeburn  4500:             &custom_role_editor($r,$brcrum);
1.190     raeburn  4501:         }
1.362     raeburn  4502:     } elsif (($env{'form.action'} eq 'processauthorreq') &&
                   4503:              ($permission->{'cusr'}) && 
                   4504:              (&Apache::lonnet::allowed('cau',$env{'request.role.domain'}))) {
                   4505:         push(@{$brcrum},
                   4506:                  {href => '/adm/createuser?action=processauthorreq',
                   4507:                   text => 'Authoring space requests',
                   4508:                   help => 'Domain_Role_Approvals'});
                   4509:         $bread_crumbs_component = 'Authoring requests';
                   4510:         if ($env{'form.state'} eq 'done') {
                   4511:             push(@{$brcrum},
                   4512:                      {href => '/adm/createuser?action=authorreqqueue',
                   4513:                       text => 'Result',
                   4514:                       help => 'Domain_Role_Approvals'});
                   4515:             $bread_crumbs_component = 'Authoring request result';
                   4516:         }
                   4517:         $args = { bread_crumbs           => $brcrum,
                   4518:                   bread_crumbs_component => $bread_crumbs_component};
                   4519:         $r->print(&header(undef,$args));
                   4520:         if (!exists($env{'form.state'})) {
                   4521:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests('requestauthor',
                   4522:                                                                             $env{'request.role.domain'}));
                   4523:         } elsif ($env{'form.state'} eq 'done') {
                   4524:             $r->print('<h3>'.&mt('Authoring request processing').'</h3>'."\n");
                   4525:             $r->print(&Apache::loncoursequeueadmin::update_request_queue('requestauthor',
                   4526:                                                                          $env{'request.role.domain'}));
                   4527:         }
1.207     raeburn  4528:     } elsif (($env{'form.action'} eq 'listusers') && 
                   4529:              ($permission->{'view'} || $permission->{'cusr'})) {
1.202     raeburn  4530:         if ($env{'form.phase'} eq 'bulkchange') {
1.351     raeburn  4531:             push(@{$brcrum},
                   4532:                     {href => '/adm/createuser?action=listusers',
                   4533:                      text => "List Users"},
                   4534:                     {href => "/adm/createuser",
                   4535:                      text => "Result",
                   4536:                      help => 'Course_View_Class_List'});
                   4537:             $bread_crumbs_component = 'Update Users';
                   4538:             $args = {bread_crumbs           => $brcrum,
                   4539:                      bread_crumbs_component => $bread_crumbs_component};
                   4540:             $r->print(&header(undef,$args));
1.202     raeburn  4541:             my $setting = $env{'form.roletype'};
                   4542:             my $choice = $env{'form.bulkaction'};
                   4543:             if ($permission->{'cusr'}) {
1.336     raeburn  4544:                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice,$crstype);
1.221     raeburn  4545:             } else {
                   4546:                 $r->print(&mt('You are not authorized to make bulk changes to user roles'));
1.223     raeburn  4547:                 $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
1.202     raeburn  4548:             }
                   4549:         } else {
1.351     raeburn  4550:             push(@{$brcrum},
                   4551:                     {href => '/adm/createuser?action=listusers',
                   4552:                      text => "List Users",
                   4553:                      help => 'Course_View_Class_List'});
                   4554:             $bread_crumbs_component = 'List Users';
                   4555:             $args = {bread_crumbs           => $brcrum,
                   4556:                      bread_crumbs_component => $bread_crumbs_component};
1.202     raeburn  4557:             my ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles);
                   4558:             my $formname = 'studentform';
1.364     raeburn  4559:             my $hidecall = "hide_searching();";
1.321     raeburn  4560:             if (($context eq 'domain') && (($env{'form.roletype'} eq 'course') ||
                   4561:                 ($env{'form.roletype'} eq 'community'))) {
                   4562:                 if ($env{'form.roletype'} eq 'course') {
                   4563:                     ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles) = 
                   4564:                         &Apache::lonuserutils::courses_selector($env{'request.role.domain'},
                   4565:                                                                 $formname);
                   4566:                 } elsif ($env{'form.roletype'} eq 'community') {
                   4567:                     $cb_jscript = 
                   4568:                         &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
                   4569:                     my %elements = (
                   4570:                                       coursepick => 'radio',
                   4571:                                       coursetotal => 'text',
                   4572:                                       courselist => 'text',
                   4573:                                    );
                   4574:                     $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   4575:                 }
1.364     raeburn  4576:                 $jscript .= &verify_user_display($context)."\n".
                   4577:                             &Apache::loncommon::check_uncheck_jscript();
1.202     raeburn  4578:                 my $js = &add_script($jscript).$cb_jscript;
                   4579:                 my $loadcode = 
                   4580:                     &Apache::lonuserutils::course_selector_loadcode($formname);
                   4581:                 if ($loadcode ne '') {
1.364     raeburn  4582:                     $args->{add_entries} = {onload => "$loadcode;$hidecall"};
                   4583:                 } else {
                   4584:                     $args->{add_entries} = {onload => $hidecall};
1.202     raeburn  4585:                 }
1.351     raeburn  4586:                 $r->print(&header($js,$args));
1.191     raeburn  4587:             } else {
1.364     raeburn  4588:                 $args->{add_entries} = {onload => $hidecall};
                   4589:                 $jscript = &verify_user_display($context).
                   4590:                            &Apache::loncommon::check_uncheck_jscript(); 
                   4591:                 $r->print(&header(&add_script($jscript),$args));
1.191     raeburn  4592:             }
1.202     raeburn  4593:             &Apache::lonuserutils::print_userlist($r,undef,$permission,$context,
1.375     raeburn  4594:                          $formname,$totcodes,$codetitles,$idlist,$idlist_titles,
                   4595:                          $showcredits);
1.191     raeburn  4596:         }
1.213     raeburn  4597:     } elsif ($env{'form.action'} eq 'drop' && $permission->{'cusr'}) {
1.318     raeburn  4598:         my $brtext;
                   4599:         if ($crstype eq 'Community') {
                   4600:             $brtext = 'Drop Members';
                   4601:         } else {
                   4602:             $brtext = 'Drop Students';
                   4603:         }
1.351     raeburn  4604:         push(@{$brcrum},
                   4605:                 {href => '/adm/createuser?action=drop',
                   4606:                  text => $brtext,
                   4607:                  help => 'Course_Drop_Student'});
                   4608:         if ($env{'form.state'} eq 'done') {
                   4609:             push(@{$brcrum},
                   4610:                      {href=>'/adm/createuser?action=drop',
                   4611:                       text=>"Result"});
                   4612:         }
                   4613:         $bread_crumbs_component = $brtext;
                   4614:         $args = {bread_crumbs           => $brcrum,
                   4615:                  bread_crumbs_component => $bread_crumbs_component}; 
                   4616:         $r->print(&header(undef,$args));
1.213     raeburn  4617:         if (!exists($env{'form.state'})) {
1.318     raeburn  4618:             &Apache::lonuserutils::print_drop_menu($r,$context,$permission,$crstype);
1.213     raeburn  4619:         } elsif ($env{'form.state'} eq 'done') {
                   4620:             &Apache::lonuserutils::update_user_list($r,$context,undef,
                   4621:                                                     $env{'form.action'});
                   4622:         }
1.202     raeburn  4623:     } elsif ($env{'form.action'} eq 'dateselect') {
                   4624:         if ($permission->{'cusr'}) {
1.351     raeburn  4625:             $r->print(&header(undef,{'no_nav_bar' => 1}).
1.375     raeburn  4626:                       &Apache::lonuserutils::date_section_selector($context,$permission,
                   4627:                                                                    $crstype,$showcredits));
1.202     raeburn  4628:         } else {
1.351     raeburn  4629:             $r->print(&header(undef,{'no_nav_bar' => 1}).
                   4630:                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>'); 
1.202     raeburn  4631:         }
1.237     raeburn  4632:     } elsif ($env{'form.action'} eq 'selfenroll') {
1.351     raeburn  4633:         push(@{$brcrum},
                   4634:                 {href => '/adm/createuser?action=selfenroll',
                   4635:                  text => "Configure Self-enrollment",
                   4636:                  help => 'Course_Self_Enrollment'});
1.237     raeburn  4637:         if (!exists($env{'form.state'})) {
1.351     raeburn  4638:             $args = { bread_crumbs           => $brcrum,
                   4639:                       bread_crumbs_component => 'Configure Self-enrollment'};
                   4640:             $r->print(&header(undef,$args));
1.241     raeburn  4641:             $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
1.237     raeburn  4642:             &print_selfenroll_menu($r,$context,$permission);
                   4643:         } elsif ($env{'form.state'} eq 'done') {
1.351     raeburn  4644:             push (@{$brcrum},
                   4645:                       {href=>'/adm/createuser?action=selfenroll',
                   4646:                        text=>"Result"});
                   4647:             $args = { bread_crumbs           => $brcrum,
                   4648:                       bread_crumbs_component => 'Self-enrollment result'};
                   4649:             $r->print(&header(undef,$args));
1.241     raeburn  4650:             $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
                   4651:             &update_selfenroll_config($r,$context,$permission);
1.237     raeburn  4652:         }
1.277     raeburn  4653:     } elsif ($env{'form.action'} eq 'selfenrollqueue') {
1.351     raeburn  4654:         push(@{$brcrum},
                   4655:                  {href => '/adm/createuser?action=selfenrollqueue',
                   4656:                   text => 'Enrollment requests',
                   4657:                   help => 'Course_Self_Enrollment'});
                   4658:         $bread_crumbs_component = 'Enrollment requests';
                   4659:         if ($env{'form.state'} eq 'done') {
                   4660:             push(@{$brcrum},
                   4661:                      {href => '/adm/createuser?action=selfenrollqueue',
                   4662:                       text => 'Result',
                   4663:                       help => 'Course_Self_Enrollment'});
                   4664:             $bread_crumbs_component = 'Enrollment result';
                   4665:         }
                   4666:         $args = { bread_crumbs           => $brcrum,
                   4667:                   bread_crumbs_component => $bread_crumbs_component};
                   4668:         $r->print(&header(undef,$args));
1.277     raeburn  4669:         my $cid = $env{'request.course.id'};
                   4670:         my $cdom = $env{'course.'.$cid.'.domain'};
                   4671:         my $cnum = $env{'course.'.$cid.'.num'};
1.307     raeburn  4672:         my $coursedesc = $env{'course.'.$cid.'.description'};
1.277     raeburn  4673:         if (!exists($env{'form.state'})) {
                   4674:             $r->print('<h3>'.&mt('Pending enrollment requests').'</h3>'."\n");
1.307     raeburn  4675:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests($context,
                   4676:                                                                        $cdom,$cnum));
1.277     raeburn  4677:         } elsif ($env{'form.state'} eq 'done') {
                   4678:             $r->print('<h3>'.&mt('Enrollment request processing').'</h3>'."\n");
1.307     raeburn  4679:             $r->print(&Apache::loncoursequeueadmin::update_request_queue($context,
                   4680:                           $cdom,$cnum,$coursedesc));
1.277     raeburn  4681:         }
1.239     raeburn  4682:     } elsif ($env{'form.action'} eq 'changelogs') {
1.363     raeburn  4683:         my $helpitem;
                   4684:         if ($context eq 'course') {
                   4685:             $helpitem = 'Course_User_Logs';
                   4686:         }
1.351     raeburn  4687:         push (@{$brcrum},
                   4688:                  {href => '/adm/createuser?action=changelogs',
                   4689:                   text => 'User Management Logs',
1.363     raeburn  4690:                   help => $helpitem});
1.351     raeburn  4691:         $bread_crumbs_component = 'User Changes';
                   4692:         $args = { bread_crumbs           => $brcrum,
                   4693:                   bread_crumbs_component => $bread_crumbs_component};
                   4694:         $r->print(&header(undef,$args));
                   4695:         &print_userchangelogs_display($r,$context,$permission);
1.190     raeburn  4696:     } else {
1.351     raeburn  4697:         $bread_crumbs_component = 'User Management';
                   4698:         $args = { bread_crumbs           => $brcrum,
                   4699:                   bread_crumbs_component => $bread_crumbs_component};
                   4700:         $r->print(&header(undef,$args));
1.318     raeburn  4701:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4702:     }
1.351     raeburn  4703:     $r->print(&Apache::loncommon::end_page());
1.190     raeburn  4704:     return OK;
                   4705: }
                   4706: 
                   4707: sub header {
1.351     raeburn  4708:     my ($jscript,$args) = @_;
1.190     raeburn  4709:     my $start_page;
1.351     raeburn  4710:     if (ref($args) eq 'HASH') {
                   4711:         $start_page=&Apache::loncommon::start_page('User Management',$jscript,$args);
1.190     raeburn  4712:     } else {
1.351     raeburn  4713:         $start_page=&Apache::loncommon::start_page('User Management',$jscript);
1.190     raeburn  4714:     }
                   4715:     return $start_page;
                   4716: }
1.2       www      4717: 
1.191     raeburn  4718: sub add_script {
                   4719:     my ($js) = @_;
1.301     bisitz   4720:     return '<script type="text/javascript">'."\n"
                   4721:           .'// <![CDATA['."\n"
                   4722:           .$js."\n"
                   4723:           .'// ]]>'."\n"
                   4724:           .'</script>'."\n";
1.191     raeburn  4725: }
                   4726: 
1.202     raeburn  4727: sub verify_user_display {
1.364     raeburn  4728:     my ($context) = @_;
1.374     raeburn  4729:     my %lt = &Apache::lonlocal::texthash (
                   4730:         course    => 'course(s): description, section(s), status',
                   4731:         community => 'community(s): description, section(s), status',
                   4732:         author    => 'author',
                   4733:     );
1.364     raeburn  4734:     my $photos;
                   4735:     if (($context eq 'course') && $env{'request.course.id'}) {
                   4736:         $photos = $env{'course.'.$env{'request.course.id'}.'.internal.showphoto'};
                   4737:     }
1.202     raeburn  4738:     my $output = <<"END";
                   4739: 
1.364     raeburn  4740: function hide_searching() {
                   4741:     if (document.getElementById('searching')) {
                   4742:         document.getElementById('searching').style.display = 'none';
                   4743:     }
                   4744:     return;
                   4745: }
                   4746: 
1.202     raeburn  4747: function display_update() {
                   4748:     document.studentform.action.value = 'listusers';
                   4749:     document.studentform.phase.value = 'display';
                   4750:     document.studentform.submit();
                   4751: }
                   4752: 
1.364     raeburn  4753: function updateCols(caller) {
                   4754:     var context = '$context';
                   4755:     var photos = '$photos';
                   4756:     if (caller == 'Status') {
1.374     raeburn  4757:         if ((context == 'domain') && 
                   4758:             ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   4759:              (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community'))) {
1.364     raeburn  4760:             document.getElementById('showcolstatus').checked = false;
                   4761:             document.getElementById('showcolstatus').disabled = 'disabled';
                   4762:             document.getElementById('showcolstart').checked = false;
                   4763:             document.getElementById('showcolend').checked = false;
1.374     raeburn  4764:         } else {
                   4765:             if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   4766:                 document.getElementById('showcolstatus').checked = true;
                   4767:                 document.getElementById('showcolstatus').disabled = '';
                   4768:                 document.getElementById('showcolstart').checked = true;
                   4769:                 document.getElementById('showcolend').checked = true;
                   4770:             } else {
                   4771:                 document.getElementById('showcolstatus').checked = false;
                   4772:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   4773:                 document.getElementById('showcolstart').checked = false;
                   4774:                 document.getElementById('showcolend').checked = false;
                   4775:             }
1.364     raeburn  4776:         }
                   4777:     }
                   4778:     if (caller == 'output') {
                   4779:         if (photos == 1) {
                   4780:             if (document.getElementById('showcolphoto')) {
                   4781:                 var photoitem = document.getElementById('showcolphoto');
                   4782:                 if (document.studentform.output.options[document.studentform.output.selectedIndex].value == 'html') {
                   4783:                     photoitem.checked = true;
                   4784:                     photoitem.disabled = '';
                   4785:                 } else {
                   4786:                     photoitem.checked = false;
                   4787:                     photoitem.disabled = 'disabled';
                   4788:                 }
                   4789:             }
                   4790:         }
                   4791:     }
                   4792:     if (caller == 'showrole') {
1.371     raeburn  4793:         if ((document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'Any') ||
                   4794:             (document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value == 'cr')) {
1.364     raeburn  4795:             document.getElementById('showcolrole').checked = true;
                   4796:             document.getElementById('showcolrole').disabled = '';
                   4797:         } else {
                   4798:             document.getElementById('showcolrole').checked = false;
                   4799:             document.getElementById('showcolrole').disabled = 'disabled';
                   4800:         }
1.374     raeburn  4801:         if (context == 'domain') {
                   4802:             if ((document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'course') ||
                   4803:                 (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community')) {
                   4804:                 document.getElementById('showcolstatus').checked = false;
                   4805:                 document.getElementById('showcolstatus').disabled = 'disabled';
                   4806:                 document.getElementById('showcolstart').checked = false;
                   4807:                 document.getElementById('showcolend').checked = false;
                   4808:             } else {
                   4809:                 if (document.studentform.Status.options[document.studentform.Status.selectedIndex].value == 'Any') {
                   4810:                     document.getElementById('showcolstatus').checked = true;
                   4811:                     document.getElementById('showcolstatus').disabled = '';
                   4812:                     document.getElementById('showcolstart').checked = true;
                   4813:                     document.getElementById('showcolend').checked = true;
                   4814:                 }
                   4815:             }
                   4816:             if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'domain') {
                   4817:                 document.getElementById('showcolextent').disabled = 'disabled';
                   4818:                 document.getElementById('showcolextent').checked = 'false';
                   4819:                 document.getElementById('showextent').style.display='none';
                   4820:                 document.getElementById('showcoltextextent').innerHTML = '';
                   4821:             } else {
                   4822:                 document.getElementById('showextent').style.display='block';
                   4823:                 document.getElementById('showextent').style.textAlign='left';
                   4824:                 document.getElementById('showextent').style.textFace='normal';
                   4825:                 if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'author') {
                   4826:                     document.getElementById('showcolextent').disabled = '';
                   4827:                     document.getElementById('showcolextent').checked = 'true';
                   4828:                     document.getElementById('showcoltextextent').innerHTML="$lt{'author'}";
                   4829:                 } else {
                   4830:                     document.getElementById('showcolextent').disabled = '';
                   4831:                     document.getElementById('showcolextent').checked = 'true';
                   4832:                     if (document.studentform.roletype.options[document.studentform.roletype.selectedIndex].value == 'community') {
                   4833:                         document.getElementById('showcoltextextent').innerHTML="$lt{'community'}";
                   4834:                     } else {
                   4835:                         document.getElementById('showcoltextextent').innerHTML="$lt{'course'}";
                   4836:                     }
                   4837:                 }
                   4838:             }
                   4839:         }
1.364     raeburn  4840:     }
                   4841:     return;
                   4842: }
                   4843: 
1.202     raeburn  4844: END
                   4845:     return $output;
                   4846: 
                   4847: }
                   4848: 
1.190     raeburn  4849: ###############################################################
                   4850: ###############################################################
                   4851: #  Menu Phase One
                   4852: sub print_main_menu {
1.318     raeburn  4853:     my ($permission,$context,$crstype) = @_;
                   4854:     my $linkcontext = $context;
                   4855:     my $stuterm = lc(&Apache::lonnet::plaintext('st',$crstype));
                   4856:     if (($context eq 'course') && ($crstype eq 'Community')) {
                   4857:         $linkcontext = lc($crstype);
                   4858:         $stuterm = 'Members';
                   4859:     }
1.208     raeburn  4860:     my %links = (
1.298     droeschl 4861:                 domain => {
                   4862:                             upload     => 'Upload a File of Users',
                   4863:                             singleuser => 'Add/Modify a User',
                   4864:                             listusers  => 'Manage Users',
                   4865:                             },
                   4866:                 author => {
                   4867:                             upload     => 'Upload a File of Co-authors',
                   4868:                             singleuser => 'Add/Modify a Co-author',
                   4869:                             listusers  => 'Manage Co-authors',
                   4870:                             },
                   4871:                 course => {
                   4872:                             upload     => 'Upload a File of Course Users',
                   4873:                             singleuser => 'Add/Modify a Course User',
1.354     www      4874:                             listusers  => 'List and Modify Multiple Course Users',
1.298     droeschl 4875:                             },
1.318     raeburn  4876:                 community => {
                   4877:                             upload     => 'Upload a File of Community Users',
                   4878:                             singleuser => 'Add/Modify a Community User',
1.354     www      4879:                             listusers  => 'List and Modify Multiple Community Users',
1.318     raeburn  4880:                            },
                   4881:                 );
                   4882:      my %linktitles = (
                   4883:                 domain => {
                   4884:                             singleuser => 'Add a user to the domain, and/or a course or community in the domain.',
                   4885:                             listusers  => 'Show and manage users in this domain.',
                   4886:                             },
                   4887:                 author => {
                   4888:                             singleuser => 'Add a user with a co- or assistant author role.',
                   4889:                             listusers  => 'Show and manage co- or assistant authors.',
                   4890:                             },
                   4891:                 course => {
                   4892:                             singleuser => 'Add a user with a certain role to this course.',
                   4893:                             listusers  => 'Show and manage users in this course.',
                   4894:                             },
                   4895:                 community => {
                   4896:                             singleuser => 'Add a user with a certain role to this community.',
                   4897:                             listusers  => 'Show and manage users in this community.',
                   4898:                            },
1.298     droeschl 4899:                 );
                   4900:   my @menu = ( {categorytitle => 'Single Users', 
                   4901:          items =>
                   4902:          [
                   4903:             {
1.318     raeburn  4904:              linktext => $links{$linkcontext}{'singleuser'},
1.298     droeschl 4905:              icon => 'edit-redo.png',
                   4906:              #help => 'Course_Change_Privileges',
                   4907:              url => '/adm/createuser?action=singleuser',
                   4908:              permission => $permission->{'cusr'},
1.318     raeburn  4909:              linktitle => $linktitles{$linkcontext}{'singleuser'},
1.298     droeschl 4910:             },
                   4911:          ]},
                   4912: 
                   4913:          {categorytitle => 'Multiple Users',
                   4914:          items => 
                   4915:          [
                   4916:             {
1.318     raeburn  4917:              linktext => $links{$linkcontext}{'upload'},
1.340     wenzelju 4918:              icon => 'uplusr.png',
1.298     droeschl 4919:              #help => 'Course_Create_Class_List',
                   4920:              url => '/adm/createuser?action=upload',
                   4921:              permission => $permission->{'cusr'},
                   4922:              linktitle => 'Upload a CSV or a text file containing users.',
                   4923:             },
                   4924:             {
1.318     raeburn  4925:              linktext => $links{$linkcontext}{'listusers'},
1.340     wenzelju 4926:              icon => 'mngcu.png',
1.298     droeschl 4927:              #help => 'Course_View_Class_List',
                   4928:              url => '/adm/createuser?action=listusers',
                   4929:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  4930:              linktitle => $linktitles{$linkcontext}{'listusers'}, 
1.298     droeschl 4931:             },
                   4932: 
                   4933:          ]},
                   4934: 
                   4935:          {categorytitle => 'Administration',
                   4936:          items => [ ]},
                   4937:        );
                   4938:             
1.265     mielkec  4939:     if ($context eq 'domain'){
1.298     droeschl 4940:         
                   4941:         push(@{ $menu[2]->{items} }, #Category: Administration
                   4942:             {
                   4943:              linktext => 'Custom Roles',
                   4944:              icon => 'emblem-photos.png',
                   4945:              #help => 'Course_Editing_Custom_Roles',
                   4946:              url => '/adm/createuser?action=custom',
                   4947:              permission => $permission->{'custom'},
                   4948:              linktitle => 'Configure a custom role.',
                   4949:             },
1.362     raeburn  4950:             {
                   4951:              linktext => 'Authoring Space Requests',
                   4952:              icon => 'selfenrl-queue.png',
                   4953:              #help => 'Domain_Role_Approvals',
                   4954:              url => '/adm/createuser?action=processauthorreq',
                   4955:              permission => $permission->{'cusr'},
                   4956:              linktitle => 'Approve or reject author role requests',
                   4957:             },
1.363     raeburn  4958:             {
                   4959:              linktext => 'Change Log',
                   4960:              icon => 'document-properties.png',
                   4961:              #help => 'Course_User_Logs',
                   4962:              url => '/adm/createuser?action=changelogs',
                   4963:              permission => $permission->{'cusr'},
                   4964:              linktitle => 'View change log.',
                   4965:             },
1.298     droeschl 4966:         );
                   4967:         
1.265     mielkec  4968:     }elsif ($context eq 'course'){
1.298     droeschl 4969:         my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
1.318     raeburn  4970: 
                   4971:         my %linktext = (
                   4972:                          'Course'    => {
                   4973:                                           single => 'Add/Modify a Student', 
                   4974:                                           drop   => 'Drop Students',
                   4975:                                           groups => 'Course Groups',
                   4976:                                         },
                   4977:                          'Community' => {
                   4978:                                           single => 'Add/Modify a Member', 
                   4979:                                           drop   => 'Drop Members',
                   4980:                                           groups => 'Community Groups',
                   4981:                                         },
                   4982:                        );
                   4983: 
                   4984:         my %linktitle = (
                   4985:             'Course' => {
                   4986:                   single => 'Add a user with the role of student to this course',
                   4987:                   drop   => 'Remove a student from this course.',
                   4988:                   groups => 'Manage course groups',
                   4989:                         },
                   4990:             'Community' => {
                   4991:                   single => 'Add a user with the role of member to this community',
                   4992:                   drop   => 'Remove a member from this community.',
                   4993:                   groups => 'Manage community groups',
                   4994:                            },
                   4995:         );
                   4996: 
1.298     droeschl 4997:         push(@{ $menu[0]->{items} }, #Category: Single Users
                   4998:             {   
1.318     raeburn  4999:              linktext => $linktext{$crstype}{'single'},
1.298     droeschl 5000:              #help => 'Course_Add_Student',
                   5001:              icon => 'list-add.png',
                   5002:              url => '/adm/createuser?action=singlestudent',
                   5003:              permission => $permission->{'cusr'},
1.318     raeburn  5004:              linktitle => $linktitle{$crstype}{'single'},
1.298     droeschl 5005:             },
                   5006:         );
                   5007:         
                   5008:         push(@{ $menu[1]->{items} }, #Category: Multiple Users 
                   5009:             {
1.318     raeburn  5010:              linktext => $linktext{$crstype}{'drop'},
1.298     droeschl 5011:              icon => 'edit-undo.png',
                   5012:              #help => 'Course_Drop_Student',
                   5013:              url => '/adm/createuser?action=drop',
                   5014:              permission => $permission->{'cusr'},
1.318     raeburn  5015:              linktitle => $linktitle{$crstype}{'drop'},
1.298     droeschl 5016:             },
                   5017:         );
                   5018:         push(@{ $menu[2]->{items} }, #Category: Administration
                   5019:             {    
                   5020:              linktext => 'Custom Roles',
                   5021:              icon => 'emblem-photos.png',
                   5022:              #help => 'Course_Editing_Custom_Roles',
                   5023:              url => '/adm/createuser?action=custom',
                   5024:              permission => $permission->{'custom'},
                   5025:              linktitle => 'Configure a custom role.',
                   5026:             },
                   5027:             {
1.318     raeburn  5028:              linktext => $linktext{$crstype}{'groups'},
1.333     wenzelju 5029:              icon => 'grps.png',
1.298     droeschl 5030:              #help => 'Course_Manage_Group',
                   5031:              url => '/adm/coursegroups?refpage=cusr',
                   5032:              permission => $permission->{'grp_manage'},
1.318     raeburn  5033:              linktitle => $linktitle{$crstype}{'groups'},
1.298     droeschl 5034:             },
                   5035:             {
1.328     wenzelju 5036:              linktext => 'Change Log',
1.298     droeschl 5037:              icon => 'document-properties.png',
                   5038:              #help => 'Course_User_Logs',
                   5039:              url => '/adm/createuser?action=changelogs',
                   5040:              permission => $permission->{'cusr'},
                   5041:              linktitle => 'View change log.',
                   5042:             },
                   5043:         );
1.277     raeburn  5044:         if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'}) {
1.298     droeschl 5045:             push(@{ $menu[2]->{items} },
                   5046:                     {   
                   5047:                      linktext => 'Enrollment Requests',
                   5048:                      icon => 'selfenrl-queue.png',
                   5049:                      #help => 'Course_Approve_Selfenroll',
                   5050:                      url => '/adm/createuser?action=selfenrollqueue',
                   5051:                      permission => $permission->{'cusr'},
                   5052:                      linktitle =>'Approve or reject enrollment requests.',
                   5053:                     },
                   5054:             );
1.277     raeburn  5055:         }
1.298     droeschl 5056:         
1.265     mielkec  5057:         if (!exists($permission->{'cusr_section'})){
1.320     raeburn  5058:             if ($crstype ne 'Community') {
                   5059:                 push(@{ $menu[2]->{items} },
                   5060:                     {
                   5061:                      linktext => 'Automated Enrollment',
                   5062:                      icon => 'roles.png',
                   5063:                      #help => 'Course_Automated_Enrollment',
                   5064:                      permission => (&Apache::lonnet::auto_run($cnum,$cdom)
                   5065:                                          && $permission->{'cusr'}),
                   5066:                      url  => '/adm/populate',
                   5067:                      linktitle => 'Automated enrollment manager.',
                   5068:                     }
                   5069:                 );
                   5070:             }
                   5071:             push(@{ $menu[2]->{items} }, 
1.298     droeschl 5072:                 {
                   5073:                  linktext => 'User Self-Enrollment',
1.342     wenzelju 5074:                  icon => 'self_enroll.png',
1.298     droeschl 5075:                  #help => 'Course_Self_Enrollment',
                   5076:                  url => '/adm/createuser?action=selfenroll',
                   5077:                  permission => $permission->{'cusr'},
1.317     bisitz   5078:                  linktitle => 'Configure user self-enrollment.',
1.298     droeschl 5079:                 },
                   5080:             );
                   5081:         }
1.363     raeburn  5082:     } elsif ($context eq 'author') {
1.370     raeburn  5083:         push(@{ $menu[2]->{items} }, #Category: Administration
1.363     raeburn  5084:             {
                   5085:              linktext => 'Change Log',
                   5086:              icon => 'document-properties.png',
                   5087:              #help => 'Course_User_Logs',
                   5088:              url => '/adm/createuser?action=changelogs',
                   5089:              permission => $permission->{'cusr'},
                   5090:              linktitle => 'View change log.',
                   5091:             },
1.370     raeburn  5092:         );
1.363     raeburn  5093:     }
                   5094:     return Apache::lonhtmlcommon::generate_menu(@menu);
1.250     raeburn  5095: #               { text => 'View Log-in History',
                   5096: #                 help => 'Course_User_Logins',
                   5097: #                 action => 'logins',
                   5098: #                 permission => $permission->{'cusr'},
                   5099: #               });
1.190     raeburn  5100: }
                   5101: 
1.189     albertel 5102: sub restore_prev_selections {
                   5103:     my %saveable_parameters = ('srchby'   => 'scalar',
                   5104: 			       'srchin'   => 'scalar',
                   5105: 			       'srchtype' => 'scalar',
                   5106: 			       );
                   5107:     &Apache::loncommon::store_settings('user','user_picker',
                   5108: 				       \%saveable_parameters);
                   5109:     &Apache::loncommon::restore_settings('user','user_picker',
                   5110: 					 \%saveable_parameters);
                   5111: }
                   5112: 
1.237     raeburn  5113: sub print_selfenroll_menu {
                   5114:     my ($r,$context,$permission) = @_;
1.322     raeburn  5115:     my $crstype = &Apache::loncommon::course_type();
1.237     raeburn  5116:     my $formname = 'enrollstudent';
                   5117:     my $nolink = 1;
                   5118:     my ($row,$lt) = &get_selfenroll_titles();
                   5119:     my $groupslist = &Apache::lonuserutils::get_groupslist();
                   5120:     my $setsec_js = 
                   5121:         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
1.249     raeburn  5122:     my %alerts = &Apache::lonlocal::texthash(
                   5123:         acto => 'Activation of self-enrollment was selected for the following domain(s)',
                   5124:         butn => 'but no user types have been checked.',
                   5125:         wilf => "Please uncheck 'activate' or check at least one type.",
                   5126:     );
                   5127:     my $selfenroll_js = <<"ENDSCRIPT";
                   5128: function update_types(caller,num) {
                   5129:     var delidx = getIndexByName('selfenroll_delete');
                   5130:     var actidx = getIndexByName('selfenroll_activate');
                   5131:     if (caller == 'selfenroll_all') {
                   5132:         var selall;
                   5133:         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5134:             if (document.$formname.selfenroll_all[i].checked) {
                   5135:                 selall = document.$formname.selfenroll_all[i].value;
                   5136:             }
                   5137:         }
                   5138:         if (selall == 1) {
                   5139:             if (delidx != -1) {
                   5140:                 if (document.$formname.selfenroll_delete.length) {
                   5141:                     for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5142:                         document.$formname.selfenroll_delete[j].checked = true;
                   5143:                     }
                   5144:                 } else {
                   5145:                     document.$formname.elements[delidx].checked = true;
                   5146:                 }
                   5147:             }
                   5148:             if (actidx != -1) {
                   5149:                 if (document.$formname.selfenroll_activate.length) {
                   5150:                     for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5151:                         document.$formname.selfenroll_activate[j].checked = false;
                   5152:                     }
                   5153:                 } else {
                   5154:                     document.$formname.elements[actidx].checked = false;
                   5155:                 }
                   5156:             }
                   5157:             document.$formname.selfenroll_newdom.selectedIndex = 0; 
                   5158:         }
                   5159:     }
                   5160:     if (caller == 'selfenroll_activate') {
                   5161:         if (document.$formname.selfenroll_activate.length) {
                   5162:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5163:                 if (document.$formname.selfenroll_activate[j].value == num) {
                   5164:                     if (document.$formname.selfenroll_activate[j].checked) {
                   5165:                         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5166:                             if (document.$formname.selfenroll_all[i].value == '1') {
                   5167:                                 document.$formname.selfenroll_all[i].checked = false;
                   5168:                             }
                   5169:                             if (document.$formname.selfenroll_all[i].value == '0') {
                   5170:                                 document.$formname.selfenroll_all[i].checked = true;
                   5171:                             }
                   5172:                         }
                   5173:                     }
                   5174:                 }
                   5175:             }
                   5176:         } else {
                   5177:             for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   5178:                 if (document.$formname.selfenroll_all[i].value == '1') {
                   5179:                     document.$formname.selfenroll_all[i].checked = false;
                   5180:                 }
                   5181:                 if (document.$formname.selfenroll_all[i].value == '0') {
                   5182:                     document.$formname.selfenroll_all[i].checked = true;
                   5183:                 }
                   5184:             }
                   5185:         }
                   5186:     }
                   5187:     if (caller == 'selfenroll_delete') {
                   5188:         if (document.$formname.selfenroll_delete.length) {
                   5189:             for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   5190:                 if (document.$formname.selfenroll_delete[j].value == num) {
                   5191:                     if (document.$formname.selfenroll_delete[j].checked) {
                   5192:                         var delindex = getIndexByName('selfenroll_types_'+num);
                   5193:                         if (delindex != -1) { 
                   5194:                             if (document.$formname.elements[delindex].length) {
                   5195:                                 for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5196:                                     document.$formname.elements[delindex][k].checked = false;
                   5197:                                 }
                   5198:                             } else {
                   5199:                                 document.$formname.elements[delindex].checked = false;
                   5200:                             }
                   5201:                         }
                   5202:                     }
                   5203:                 }
                   5204:             }
                   5205:         } else {
                   5206:             if (document.$formname.selfenroll_delete.checked) {
                   5207:                 var delindex = getIndexByName('selfenroll_types_'+num);
                   5208:                 if (delindex != -1) {
                   5209:                     if (document.$formname.elements[delindex].length) {
                   5210:                         for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   5211:                             document.$formname.elements[delindex][k].checked = false;
                   5212:                         }
                   5213:                     } else {
                   5214:                         document.$formname.elements[delindex].checked = false;
                   5215:                     }
                   5216:                 }
                   5217:             }
                   5218:         }
                   5219:     }
                   5220:     return;
                   5221: }
                   5222: 
                   5223: function validate_types(form) {
                   5224:     var needaction = new Array();
                   5225:     var countfail = 0;
                   5226:     var actidx = getIndexByName('selfenroll_activate');
                   5227:     if (actidx != -1) {
                   5228:         if (document.$formname.selfenroll_activate.length) {
                   5229:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   5230:                 var num = document.$formname.selfenroll_activate[j].value;
                   5231:                 if (document.$formname.selfenroll_activate[j].checked) {
                   5232:                     countfail = check_types(num,countfail,needaction)
                   5233:                 }
                   5234:             }
                   5235:         } else {
                   5236:             if (document.$formname.selfenroll_activate.checked) {
                   5237:                 var num = document.enrollstudent.selfenroll_activate.value;
                   5238:                 countfail = check_types(num,countfail,needaction)
                   5239:             }
                   5240:         }
                   5241:     }
                   5242:     if (countfail > 0) {
                   5243:         var msg = "$alerts{'acto'}\\n";
                   5244:         var loopend = needaction.length -1;
                   5245:         if (loopend > 0) {
                   5246:             for (var m=0; m<loopend; m++) {
                   5247:                 msg += needaction[m]+", ";
                   5248:             }
                   5249:         }
                   5250:         msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
                   5251:         alert(msg);
                   5252:         return; 
                   5253:     }
                   5254:     setSections(form);
                   5255: }
                   5256: 
                   5257: function check_types(num,countfail,needaction) {
                   5258:     var typeidx = getIndexByName('selfenroll_types_'+num);
                   5259:     var count = 0;
                   5260:     if (typeidx != -1) {
                   5261:         if (document.$formname.elements[typeidx].length) {
                   5262:             for (var k=0; k<document.$formname.elements[typeidx].length; k++) {
                   5263:                 if (document.$formname.elements[typeidx][k].checked) {
                   5264:                     count ++;
                   5265:                 }
                   5266:             }
                   5267:         } else {
                   5268:             if (document.$formname.elements[typeidx].checked) {
                   5269:                 count ++;
                   5270:             }
                   5271:         }
                   5272:         if (count == 0) {
                   5273:             var domidx = getIndexByName('selfenroll_dom_'+num);
                   5274:             if (domidx != -1) {
                   5275:                 var domname = document.$formname.elements[domidx].value;
                   5276:                 needaction[countfail] = domname;
                   5277:                 countfail ++;
                   5278:             }
                   5279:         }
                   5280:     }
                   5281:     return countfail;
                   5282: }
                   5283: 
                   5284: function getIndexByName(item) {
                   5285:     for (var i=0;i<document.$formname.elements.length;i++) {
                   5286:         if (document.$formname.elements[i].name == item) {
                   5287:             return i;
                   5288:         }
                   5289:     }
                   5290:     return -1;
                   5291: }
                   5292: ENDSCRIPT
1.256     raeburn  5293:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5294:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   5295: 
1.237     raeburn  5296:     my $output = '<script type="text/javascript">'."\n".
1.301     bisitz   5297:                  '// <![CDATA['."\n".
1.249     raeburn  5298:                  $setsec_js."\n".$selfenroll_js."\n".
1.301     bisitz   5299:                  '// ]]>'."\n".
1.237     raeburn  5300:                  '</script>'."\n".
1.256     raeburn  5301:                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n";
                   5302:     my ($visible,$cansetvis,$vismsgs,$visactions) = &visible_in_cat($cdom,$cnum);
                   5303:     if (ref($visactions) eq 'HASH') {
                   5304:         if ($visible) {
1.283     bisitz   5305:             $output .= '<p class="LC_info">'.$visactions->{'vis'}.'</p>';
1.256     raeburn  5306:         } else {
1.283     bisitz   5307:             $output .= '<p class="LC_warning">'.$visactions->{'miss'}.'</p>'
                   5308:                       .$visactions->{'yous'}.
1.256     raeburn  5309:                        '<p>'.$visactions->{'gen'}.'<br />'.$visactions->{'coca'};
                   5310:             if (ref($vismsgs) eq 'ARRAY') {
                   5311:                 $output .= '<br />'.$visactions->{'make'}.'<ul>';
                   5312:                 foreach my $item (@{$vismsgs}) {
                   5313:                     $output .= '<li>'.$visactions->{$item}.'</li>';
                   5314:                 }
                   5315:                 $output .= '</ul>';
                   5316:             }
                   5317:             $output .= '</p>';
                   5318:         }
                   5319:     }
                   5320:     $output .= '<form name="'.$formname.'" method="post" action="/adm/createuser">'."\n".
                   5321:                &Apache::lonhtmlcommon::start_pick_box();
1.237     raeburn  5322:     if (ref($row) eq 'ARRAY') {
                   5323:         foreach my $item (@{$row}) {
                   5324:             my $title = $item; 
                   5325:             if (ref($lt) eq 'HASH') {
                   5326:                 $title = $lt->{$item};
                   5327:             }
1.297     bisitz   5328:             $output .= &Apache::lonhtmlcommon::row_title($title);
1.237     raeburn  5329:             if ($item eq 'types') {
                   5330:                 my $curr_types = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_types'};
1.241     raeburn  5331:                 my $showdomdesc = 1;
                   5332:                 my $includeempty = 1;
                   5333:                 my $num = 0;
                   5334:                 $output .= &Apache::loncommon::start_data_table().
                   5335:                            &Apache::loncommon::start_data_table_row()
                   5336:                            .'<td colspan="2"><span class="LC_nobreak"><label>'
                   5337:                            .&mt('Any user in any domain:')
                   5338:                            .'&nbsp;<input type="radio" name="selfenroll_all" value="1" ';
                   5339:                 if ($curr_types eq '*') {
                   5340:                     $output .= ' checked="checked" '; 
                   5341:                 }
1.249     raeburn  5342:                 $output .= 'onchange="javascript:update_types('.
                   5343:                            "'selfenroll_all'".');" />'.&mt('Yes').'</label>'.
                   5344:                            '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
1.241     raeburn  5345:                 if ($curr_types ne '*') {
                   5346:                     $output .= ' checked="checked" ';
                   5347:                 }
1.249     raeburn  5348:                 $output .= ' onchange="javascript:update_types('.
                   5349:                            "'selfenroll_all'".');"/>'.&mt('No').'</label></td>'.
                   5350:                            &Apache::loncommon::end_data_table_row().
                   5351:                            &Apache::loncommon::end_data_table().
                   5352:                            &mt('Or').'<br />'.
                   5353:                            &Apache::loncommon::start_data_table();
1.241     raeburn  5354:                 my %currdoms;
1.249     raeburn  5355:                 if ($curr_types eq '') {
1.241     raeburn  5356:                     $output .= &new_selfenroll_dom_row($cdom,'0');
                   5357:                 } elsif ($curr_types ne '*') {
                   5358:                     my @entries = split(/;/,$curr_types);
                   5359:                     if (@entries > 0) {
                   5360:                         foreach my $entry (@entries) {
                   5361:                             my ($currdom,$typestr) = split(/:/,$entry);
                   5362:                             $currdoms{$currdom} = 1;
                   5363:                             my $domdesc = &Apache::lonnet::domain($currdom);
1.249     raeburn  5364:                             my @currinsttypes = split(',',$typestr);
1.241     raeburn  5365:                             $output .= &Apache::loncommon::start_data_table_row()
                   5366:                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
                   5367:                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
                   5368:                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
                   5369:                                        .'" value="'.$currdom.'" /></span><br />'
                   5370:                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
1.249     raeburn  5371:                                        .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');" />'
1.241     raeburn  5372:                                        .&mt('Delete').'</label></span></td>';
1.249     raeburn  5373:                             $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
1.241     raeburn  5374:                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes).'</td>'
                   5375:                                        .&Apache::loncommon::end_data_table_row();
                   5376:                             $num ++;
                   5377:                         }
                   5378:                     }
                   5379:                 }
1.249     raeburn  5380:                 my $add_domtitle = &mt('Users in additional domain:');
1.241     raeburn  5381:                 if ($curr_types eq '*') { 
1.249     raeburn  5382:                     $add_domtitle = &mt('Users in specific domain:');
1.241     raeburn  5383:                 } elsif ($curr_types eq '') {
1.249     raeburn  5384:                     $add_domtitle = &mt('Users in other domain:');
1.241     raeburn  5385:                 }
                   5386:                 $output .= &Apache::loncommon::start_data_table_row()
                   5387:                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
                   5388:                            .&Apache::loncommon::select_dom_form('','selfenroll_newdom',
                   5389:                                                                 $includeempty,$showdomdesc)
                   5390:                            .'<input type="hidden" name="selfenroll_types_total" value="'.$num.'" />'
                   5391:                            .'</td>'.&Apache::loncommon::end_data_table_row()
                   5392:                            .&Apache::loncommon::end_data_table();
1.237     raeburn  5393:             } elsif ($item eq 'registered') {
                   5394:                 my ($regon,$regoff);
                   5395:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_registered'}) {
                   5396:                     $regon = ' checked="checked" ';
                   5397:                     $regoff = ' ';
                   5398:                 } else {
                   5399:                     $regon = ' ';
                   5400:                     $regoff = ' checked="checked" ';
                   5401:                 }
                   5402:                 $output .= '<label>'.
1.245     raeburn  5403:                            '<input type="radio" name="selfenroll_registered" value="1"'.$regon.'/>'.
1.244     bisitz   5404:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
1.245     raeburn  5405:                            '<input type="radio" name="selfenroll_registered" value="0"'.$regoff.'/>'.
1.244     bisitz   5406:                            &mt('No').'</label>';
1.237     raeburn  5407:             } elsif ($item eq 'enroll_dates') {
                   5408:                 my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_date'};
                   5409:                 my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_date'};
                   5410:                 if ($starttime eq '') {
                   5411:                     $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   5412:                 }
                   5413:                 if ($endtime eq '') {
                   5414:                     $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   5415:                 }
                   5416:                 my $startform =
                   5417:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
                   5418:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5419:                 my $endform =
                   5420:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
                   5421:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5422:                 $output .= &selfenroll_date_forms($startform,$endform);
                   5423:             } elsif ($item eq 'access_dates') {
                   5424:                 my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_access'};
                   5425:                 my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_access'};
                   5426:                 if ($starttime eq '') {
                   5427:                     $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   5428:                 }
                   5429:                 if ($endtime eq '') {
                   5430:                     $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   5431:                 }
                   5432:                 my $startform =
                   5433:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
                   5434:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5435:                 my $endform =
                   5436:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
                   5437:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5438:                 $output .= &selfenroll_date_forms($startform,$endform);
                   5439:             } elsif ($item eq 'section') {
                   5440:                 my $currsec = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_section'}; 
                   5441:                 my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   5442:                 my $newsecval;
                   5443:                 if ($currsec ne 'none' && $currsec ne '') {
                   5444:                     if (!defined($sections_count{$currsec})) {
                   5445:                         $newsecval = $currsec;
                   5446:                     }
                   5447:                 }
                   5448:                 my $sections_select = 
                   5449:                     &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec);
                   5450:                 $output .= '<table class="LC_createuser">'."\n".
                   5451:                            '<tr class="LC_section_row">'."\n".
                   5452:                            '<td align="center">'.&mt('Existing sections')."\n".
                   5453:                            '<br />'.$sections_select.'</td><td align="center">'.
                   5454:                            &mt('New section').'<br />'."\n".
                   5455:                            '<input type="text" name="newsec" size="15" value="'.$newsecval.'" />'."\n".
                   5456:                            '<input type="hidden" name="sections" value="" />'."\n".
                   5457:                            '<input type="hidden" name="state" value="done" />'."\n".
                   5458:                            '</td></tr></table>'."\n";
1.276     raeburn  5459:             } elsif ($item eq 'approval') {
                   5460:                 my ($appon,$appoff);
                   5461:                 my $cid = $env{'request.course.id'};
                   5462:                 my $currnotified = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
                   5463:                 if ($env{'course.'.$cid.'.internal.selfenroll_approval'}) {
                   5464:                     $appon = ' checked="checked" ';
                   5465:                     $appoff = ' ';
                   5466:                 } else {
                   5467:                     $appon = ' ';
                   5468:                     $appoff = ' checked="checked" ';
                   5469:                 }
                   5470:                 $output .= '<label>'.
                   5471:                            '<input type="radio" name="selfenroll_approval" value="1"'.$appon.'/>'.
                   5472:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
                   5473:                            '<input type="radio" name="selfenroll_approval" value="0"'.$appoff.'/>'.
                   5474:                            &mt('No').'</label>';
                   5475:                 my %advhash = &Apache::lonnet::get_course_adv_roles($cid,1);
                   5476:                 my (@ccs,%notified);
1.322     raeburn  5477:                 my $ccrole = 'cc';
                   5478:                 if ($crstype eq 'Community') {
                   5479:                     $ccrole = 'co';
                   5480:                 }
                   5481:                 if ($advhash{$ccrole}) {
                   5482:                     @ccs = split(/,/,$advhash{$ccrole});
1.276     raeburn  5483:                 }
                   5484:                 if ($currnotified) {
                   5485:                     foreach my $current (split(/,/,$currnotified)) {
                   5486:                         $notified{$current} = 1;
                   5487:                         if (!grep(/^\Q$current\E$/,@ccs)) {
                   5488:                             push(@ccs,$current);
                   5489:                         }
                   5490:                     }
                   5491:                 }
                   5492:                 if (@ccs) {
1.277     raeburn  5493:                     $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  5494:                                &Apache::loncommon::start_data_table_row();
                   5495:                     my $count = 0;
                   5496:                     my $numcols = 4;
                   5497:                     foreach my $cc (sort(@ccs)) {
                   5498:                         my $notifyon;
                   5499:                         my ($ccuname,$ccudom) = split(/:/,$cc);
                   5500:                         if ($notified{$cc}) {
                   5501:                             $notifyon = ' checked="checked" ';
                   5502:                         }
                   5503:                         if ($count && !$count%$numcols) {
                   5504:                             $output .= &Apache::loncommon::end_data_table_row().
                   5505:                                        &Apache::loncommon::start_data_table_row()
                   5506:                         }
                   5507:                         $output .= '<td><span class="LC_nobreak"><label>'.
                   5508:                                    '<input type="checkbox" name="selfenroll_notify"'.$notifyon.' value="'.$cc.'" />'.
                   5509:                                    &Apache::loncommon::plainname($ccuname,$ccudom).
                   5510:                                    '</label></span></td>';
1.343     raeburn  5511:                         $count ++;
1.276     raeburn  5512:                     }
                   5513:                     my $rem = $count%$numcols;
                   5514:                     if ($rem) {
                   5515:                         my $emptycols = $numcols - $rem;
                   5516:                         for (my $i=0; $i<$emptycols; $i++) { 
                   5517:                             $output .= '<td>&nbsp;</td>';
                   5518:                         }
                   5519:                     }
                   5520:                     $output .= &Apache::loncommon::end_data_table_row().
                   5521:                                &Apache::loncommon::end_data_table();
                   5522:                 }
                   5523:             } elsif ($item eq 'limit') {
                   5524:                 my ($crslimit,$selflimit,$nolimit);
                   5525:                 my $cid = $env{'request.course.id'};
                   5526:                 my $currlim = $env{'course.'.$cid.'.internal.selfenroll_limit'};
                   5527:                 my $currcap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
1.343     raeburn  5528:                 $nolimit = ' checked="checked" ';
1.276     raeburn  5529:                 if ($currlim eq 'allstudents') {
                   5530:                     $crslimit = ' checked="checked" ';
                   5531:                     $selflimit = ' ';
                   5532:                     $nolimit = ' ';
                   5533:                 } elsif ($currlim eq 'selfenrolled') {
                   5534:                     $crslimit = ' ';
                   5535:                     $selflimit = ' checked="checked" ';
                   5536:                     $nolimit = ' '; 
                   5537:                 } else {
                   5538:                     $crslimit = ' ';
                   5539:                     $selflimit = ' ';
                   5540:                 }
                   5541:                 $output .= '<table><tr><td><label>'.
1.278     raeburn  5542:                            '<input type="radio" name="selfenroll_limit" value="none"'.$nolimit.'/>'.
1.276     raeburn  5543:                            &mt('No limit').'</label></td><td><label>'.
                   5544:                            '<input type="radio" name="selfenroll_limit" value="allstudents"'.$crslimit.'/>'.
                   5545:                            &mt('Limit by total students').'</label></td><td><label>'.
                   5546:                            '<input type="radio" name="selfenroll_limit" value="selfenrolled"'.$selflimit.'/>'.
                   5547:                            &mt('Limit by total self-enrolled students').
                   5548:                            '</td></tr><tr>'.
                   5549:                            '<td>&nbsp;</td><td colspan="2"><span class="LC_nobreak">'.
                   5550:                            ('&nbsp;'x3).&mt('Maximum number allowed: ').
                   5551:                            '<input type="text" name="selfenroll_cap" size = "5" value="'.$currcap.'" /></td></tr></table>';
1.237     raeburn  5552:             }
                   5553:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   5554:         }
                   5555:     }
                   5556:     $output .= &Apache::lonhtmlcommon::end_pick_box().
1.241     raeburn  5557:                '<br /><input type="button" name="selfenrollconf" value="'
1.282     schafran 5558:                .&mt('Save').'" onclick="validate_types(this.form);" />'
1.241     raeburn  5559:                .'<input type="hidden" name="action" value="selfenroll" /></form>';
1.237     raeburn  5560:     $r->print($output);
                   5561:     return;
                   5562: }
                   5563: 
1.256     raeburn  5564: sub visible_in_cat {
                   5565:     my ($cdom,$cnum) = @_;
                   5566:     my %domconf = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   5567:     my ($cathash,%settable,@vismsgs,$cansetvis);
                   5568:     my %visactions = &Apache::lonlocal::texthash(
1.316     bisitz   5569:                    vis => 'Your course/community currently appears in the Course/Community Catalog for this domain.',
1.256     raeburn  5570:                    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   5571:                    miss => 'Your course/community does not currently appear in the Course/Community Catalog for this domain.',
1.256     raeburn  5572:                    yous => 'You should remedy this if you plan to allow self-enrollment, otherwise students will have difficulty finding your course.',
                   5573:                    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 5574:                    make => 'Make any changes to self-enrollment settings below, click "Save", then take action to include the course in the Catalog:',
1.256     raeburn  5575:                    take => 'Take the following action to ensure the course appears in the Catalog:',
                   5576:                    dc_unhide  => 'Ask a domain coordinator to change the "Exclude from course catalog" setting.',
                   5577:                    dc_addinst => 'Ask a domain coordinator to enable display the catalog of "Official courses (with institutional codes)".',
                   5578:                    dc_instcode => 'Ask a domain coordinator to assign an institutional code (if this is an official course).',
                   5579:                    dc_catalog  => 'Ask a domain coordinator to enable or create at least one course category in the domain.',
                   5580:                    dc_categories => 'Ask a domain coordinator to create a hierarchy of categories and sub categories for courses in the domain.',
                   5581:                    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',
                   5582:                    dc_addcat => 'Ask a domain coordinator to assign a category to the course.',
                   5583:     );
1.347     raeburn  5584:     $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>"');
                   5585:     $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>"');
                   5586:     $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  5587:     if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5588:         if ($domconf{'coursecategories'}{'togglecats'} eq 'crs') {
                   5589:             $settable{'togglecats'} = 1;
                   5590:         }
                   5591:         if ($domconf{'coursecategories'}{'categorize'} eq 'crs') {
                   5592:             $settable{'categorize'} = 1;
                   5593:         }
                   5594:         $cathash = $domconf{'coursecategories'}{'cats'};
                   5595:     }
1.260     raeburn  5596:     if ($settable{'togglecats'} && $settable{'categorize'}) {
1.256     raeburn  5597:         $cansetvis = &mt('You are able to both assign a course category and choose to exclude this course from the catalog.');   
                   5598:     } elsif ($settable{'togglecats'}) {
                   5599:         $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  5600:     } elsif ($settable{'categorize'}) {
1.256     raeburn  5601:         $cansetvis = &mt('You may assign a course category, but only a Domain Coordinator may choose to exclude this course from the catalog.');  
                   5602:     } else {
                   5603:         $cansetvis = &mt('Only a Domain Coordinator may assign a course category or choose to exclude this course from the catalog.'); 
                   5604:     }
                   5605:      
                   5606:     my %currsettings =
                   5607:         &Apache::lonnet::get('environment',['hidefromcat','categories','internal.coursecode'],
                   5608:                              $cdom,$cnum);
                   5609:     my $visible = 0;
                   5610:     if ($currsettings{'internal.coursecode'} ne '') {
                   5611:         if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5612:             $cathash = $domconf{'coursecategories'}{'cats'};
                   5613:             if (ref($cathash) eq 'HASH') {
                   5614:                 if ($cathash->{'instcode::0'} eq '') {
                   5615:                     push(@vismsgs,'dc_addinst'); 
                   5616:                 } else {
                   5617:                     $visible = 1;
                   5618:                 }
                   5619:             } else {
                   5620:                 $visible = 1;
                   5621:             }
                   5622:         } else {
                   5623:             $visible = 1;
                   5624:         }
                   5625:     } else {
                   5626:         if (ref($cathash) eq 'HASH') {
                   5627:             if ($cathash->{'instcode::0'} ne '') {
                   5628:                 push(@vismsgs,'dc_instcode');
                   5629:             }
                   5630:         } else {
                   5631:             push(@vismsgs,'dc_instcode');
                   5632:         }
                   5633:     }
                   5634:     if ($currsettings{'categories'} ne '') {
                   5635:         my $cathash;
                   5636:         if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5637:             $cathash = $domconf{'coursecategories'}{'cats'};
                   5638:             if (ref($cathash) eq 'HASH') {
                   5639:                 if (keys(%{$cathash}) == 0) {
                   5640:                     push(@vismsgs,'dc_catalog');
                   5641:                 } elsif ((keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} ne '')) {
                   5642:                     push(@vismsgs,'dc_categories');
                   5643:                 } else {
                   5644:                     my @currcategories = split('&',$currsettings{'categories'});
                   5645:                     my $matched = 0;
                   5646:                     foreach my $cat (@currcategories) {
                   5647:                         if ($cathash->{$cat} ne '') {
                   5648:                             $visible = 1;
                   5649:                             $matched = 1;
                   5650:                             last;
                   5651:                         }
                   5652:                     }
                   5653:                     if (!$matched) {
1.260     raeburn  5654:                         if ($settable{'categorize'}) { 
1.256     raeburn  5655:                             push(@vismsgs,'chgcat');
                   5656:                         } else {
                   5657:                             push(@vismsgs,'dc_chgcat');
                   5658:                         }
                   5659:                     }
                   5660:                 }
                   5661:             }
                   5662:         }
                   5663:     } else {
                   5664:         if (ref($cathash) eq 'HASH') {
                   5665:             if ((keys(%{$cathash}) > 1) || 
                   5666:                 (keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} eq '')) {
1.260     raeburn  5667:                 if ($settable{'categorize'}) {
1.256     raeburn  5668:                     push(@vismsgs,'addcat');
                   5669:                 } else {
                   5670:                     push(@vismsgs,'dc_addcat');
                   5671:                 }
                   5672:             }
                   5673:         }
                   5674:     }
                   5675:     if ($currsettings{'hidefromcat'} eq 'yes') {
                   5676:         $visible = 0;
                   5677:         if ($settable{'togglecats'}) {
                   5678:             unshift(@vismsgs,'unhide');
                   5679:         } else {
                   5680:             unshift(@vismsgs,'dc_unhide')
                   5681:         }
                   5682:     }
                   5683:     return ($visible,$cansetvis,\@vismsgs,\%visactions);
                   5684: }
                   5685: 
1.241     raeburn  5686: sub new_selfenroll_dom_row {
                   5687:     my ($newdom,$num) = @_;
                   5688:     my $domdesc = &Apache::lonnet::domain($newdom);
                   5689:     my $output;
                   5690:     if ($domdesc ne '') {
                   5691:         $output .= &Apache::loncommon::start_data_table_row()
                   5692:                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
                   5693:                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
1.249     raeburn  5694:                    .'" value="'.$newdom.'" /></span><br />'
                   5695:                    .'<span class="LC_nobreak"><label><input type="checkbox" '
                   5696:                    .'name="selfenroll_activate" value="'.$num.'" '
                   5697:                    .'onchange="javascript:update_types('
                   5698:                    ."'selfenroll_activate','$num'".');" />'
                   5699:                    .&mt('Activate').'</label></span></td>';
1.241     raeburn  5700:         my @currinsttypes;
                   5701:         $output .= '<td>'.&mt('User types:').'<br />'
                   5702:                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
                   5703:                    .&Apache::loncommon::end_data_table_row();
                   5704:     }
                   5705:     return $output;
                   5706: }
                   5707: 
                   5708: sub selfenroll_inst_types {
                   5709:     my ($num,$currdom,$currinsttypes) = @_;
                   5710:     my $output;
                   5711:     my $numinrow = 4;
                   5712:     my $count = 0;
                   5713:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($currdom);
1.247     raeburn  5714:     my $othervalue = 'any';
1.241     raeburn  5715:     if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
1.251     raeburn  5716:         if (keys(%{$usertypes}) > 0) {
1.247     raeburn  5717:             $othervalue = 'other';
                   5718:         }
1.241     raeburn  5719:         $output .= '<table><tr>';
                   5720:         foreach my $type (@{$types}) {
                   5721:             if (($count > 0) && ($count%$numinrow == 0)) {
                   5722:                 $output .= '</tr><tr>';
                   5723:             }
                   5724:             if (defined($usertypes->{$type})) {
1.257     raeburn  5725:                 my $esc_type = &escape($type);
1.241     raeburn  5726:                 $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.
1.257     raeburn  5727:                            $esc_type.'" ';
1.241     raeburn  5728:                 if (ref($currinsttypes) eq 'ARRAY') {
                   5729:                     if (@{$currinsttypes} > 0) {
1.249     raeburn  5730:                         if (grep(/^any$/,@{$currinsttypes})) {
                   5731:                             $output .= 'checked="checked"';
1.257     raeburn  5732:                         } elsif (grep(/^\Q$esc_type\E$/,@{$currinsttypes})) {
1.241     raeburn  5733:                             $output .= 'checked="checked"';
                   5734:                         }
1.249     raeburn  5735:                     } else {
                   5736:                         $output .= 'checked="checked"';
1.241     raeburn  5737:                     }
                   5738:                 }
                   5739:                 $output .= ' name="selfenroll_types_'.$num.'" />'.$usertypes->{$type}.'</label></span></td>';
                   5740:             }
                   5741:             $count ++;
                   5742:         }
                   5743:         if (($count > 0) && ($count%$numinrow == 0)) {
                   5744:             $output .= '</tr><tr>';
                   5745:         }
1.249     raeburn  5746:         $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
1.241     raeburn  5747:         if (ref($currinsttypes) eq 'ARRAY') {
                   5748:             if (@{$currinsttypes} > 0) {
1.249     raeburn  5749:                 if (grep(/^any$/,@{$currinsttypes})) { 
                   5750:                     $output .= ' checked="checked"';
                   5751:                 } elsif ($othervalue eq 'other') {
                   5752:                     if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
                   5753:                         $output .= ' checked="checked"';
                   5754:                     }
1.241     raeburn  5755:                 }
1.249     raeburn  5756:             } else {
                   5757:                 $output .= ' checked="checked"';
1.241     raeburn  5758:             }
1.249     raeburn  5759:         } else {
                   5760:             $output .= ' checked="checked"';
1.241     raeburn  5761:         }
                   5762:         $output .= ' name="selfenroll_types_'.$num.'" />'.$othertitle.'</label></span></td></tr></table>';
                   5763:     }
                   5764:     return $output;
                   5765: }
                   5766: 
1.237     raeburn  5767: sub selfenroll_date_forms {
                   5768:     my ($startform,$endform) = @_;
                   5769:     my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
1.244     bisitz   5770:                   &Apache::lonhtmlcommon::row_title(&mt('Start date'),
1.237     raeburn  5771:                                                     'LC_oddrow_value')."\n".
                   5772:                   $startform."\n".
                   5773:                   &Apache::lonhtmlcommon::row_closure(1).
1.244     bisitz   5774:                   &Apache::lonhtmlcommon::row_title(&mt('End date'),
1.237     raeburn  5775:                                                    'LC_oddrow_value')."\n".
                   5776:                   $endform."\n".
                   5777:                   &Apache::lonhtmlcommon::row_closure(1).
                   5778:                   &Apache::lonhtmlcommon::end_pick_box();
                   5779:     return $output;
                   5780: }
                   5781: 
1.239     raeburn  5782: sub print_userchangelogs_display {
                   5783:     my ($r,$context,$permission) = @_;
1.363     raeburn  5784:     my $formname = 'rolelog';
                   5785:     my ($username,$domain,$crstype,%roleslog);
                   5786:     if ($context eq 'domain') {
                   5787:         $domain = $env{'request.role.domain'};
                   5788:         %roleslog=&Apache::lonnet::dump_dom('nohist_rolelog',$domain);
                   5789:     } else {
                   5790:         if ($context eq 'course') { 
                   5791:             $domain = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5792:             $username = $env{'course.'.$env{'request.course.id'}.'.num'};
                   5793:             $crstype = &Apache::loncommon::course_type();
                   5794:             my %saveable_parameters = ('show' => 'scalar',);
                   5795:             &Apache::loncommon::store_course_settings('roles_log',
                   5796:                                                       \%saveable_parameters);
                   5797:             &Apache::loncommon::restore_course_settings('roles_log',
                   5798:                                                         \%saveable_parameters);
                   5799:         } elsif ($context eq 'author') {
                   5800:             $domain = $env{'user.domain'}; 
                   5801:             if ($env{'request.role'} =~ m{^au\./\Q$domain\E/$}) {
                   5802:                 $username = $env{'user.name'};
                   5803:             } else {
                   5804:                 undef($domain);
                   5805:             }
                   5806:         }
                   5807:         if ($domain ne '' && $username ne '') { 
                   5808:             %roleslog=&Apache::lonnet::dump('nohist_rolelog',$domain,$username);
                   5809:         }
                   5810:     }
1.239     raeburn  5811:     if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
                   5812: 
                   5813:     # set defaults
                   5814:     my $now = time();
                   5815:     my $defstart = $now - (7*24*3600); #7 days ago 
                   5816:     my %defaults = (
                   5817:                      page               => '1',
                   5818:                      show               => '10',
                   5819:                      role               => 'any',
                   5820:                      chgcontext         => 'any',
                   5821:                      rolelog_start_date => $defstart,
                   5822:                      rolelog_end_date   => $now,
                   5823:                    );
                   5824:     my $more_records = 0;
                   5825: 
                   5826:     # set current
                   5827:     my %curr;
                   5828:     foreach my $item ('show','page','role','chgcontext') {
                   5829:         $curr{$item} = $env{'form.'.$item};
                   5830:     }
                   5831:     my ($startdate,$enddate) = 
                   5832:         &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
                   5833:     $curr{'rolelog_start_date'} = $startdate;
                   5834:     $curr{'rolelog_end_date'} = $enddate;
                   5835:     foreach my $key (keys(%defaults)) {
                   5836:         if ($curr{$key} eq '') {
                   5837:             $curr{$key} = $defaults{$key};
                   5838:         }
                   5839:     }
1.248     raeburn  5840:     my (%whodunit,%changed,$version);
                   5841:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
1.239     raeburn  5842:     my ($minshown,$maxshown);
1.255     raeburn  5843:     $minshown = 1;
1.239     raeburn  5844:     my $count = 0;
                   5845:     if ($curr{'show'} ne &mt('all')) { 
                   5846:         $maxshown = $curr{'page'} * $curr{'show'};
                   5847:         if ($curr{'page'} > 1) {
                   5848:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   5849:         }
                   5850:     }
1.301     bisitz   5851: 
1.327     raeburn  5852:     # Form Header
                   5853:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
1.363     raeburn  5854:               &role_display_filter($context,$formname,$domain,$username,\%curr,
                   5855:                                    $version,$crstype));
1.327     raeburn  5856: 
                   5857:     # Create navigation
                   5858:     my ($nav_script,$nav_links) = &userlogdisplay_nav($formname,\%curr,$more_records);
                   5859:     my $showntableheader = 0;
                   5860: 
                   5861:     # Table Header
                   5862:     my $tableheader = 
                   5863:         &Apache::loncommon::start_data_table_header_row()
                   5864:        .'<th>&nbsp;</th>'
                   5865:        .'<th>'.&mt('When').'</th>'
                   5866:        .'<th>'.&mt('Who made the change').'</th>'
                   5867:        .'<th>'.&mt('Changed User').'</th>'
1.363     raeburn  5868:        .'<th>'.&mt('Role').'</th>';
                   5869: 
                   5870:     if ($context eq 'course') {
                   5871:         $tableheader .= '<th>'.&mt('Section').'</th>';
                   5872:     }
                   5873:     $tableheader .=
                   5874:         '<th>'.&mt('Context').'</th>'
1.327     raeburn  5875:        .'<th>'.&mt('Start').'</th>'
                   5876:        .'<th>'.&mt('End').'</th>'
                   5877:        .&Apache::loncommon::end_data_table_header_row();
                   5878: 
                   5879:     # Display user change log data
1.239     raeburn  5880:     foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
                   5881:         next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
                   5882:                  ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
                   5883:         if ($curr{'show'} ne &mt('all')) {
                   5884:             if ($count >= $curr{'page'} * $curr{'show'}) {
                   5885:                 $more_records = 1;
                   5886:                 last;
                   5887:             }
                   5888:         }
                   5889:         if ($curr{'role'} ne 'any') {
                   5890:             next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
                   5891:         }
                   5892:         if ($curr{'chgcontext'} ne 'any') {
                   5893:             if ($curr{'chgcontext'} eq 'selfenroll') {
                   5894:                 next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
                   5895:             } else {
                   5896:                 next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
                   5897:             }
                   5898:         }
                   5899:         $count ++;
                   5900:         next if ($count < $minshown);
1.327     raeburn  5901:         unless ($showntableheader) {
                   5902:             $r->print($nav_script
                   5903:                      .$nav_links
                   5904:                      .&Apache::loncommon::start_data_table()
                   5905:                      .$tableheader);
                   5906:             $r->rflush();
                   5907:             $showntableheader = 1;
                   5908:         }
1.239     raeburn  5909:         if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
                   5910:             $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
                   5911:                 &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
                   5912:         }
                   5913:         if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
                   5914:             $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
                   5915:                 &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
                   5916:         }
                   5917:         my $sec = $roleslog{$id}{'logentry'}{'section'};
                   5918:         if ($sec eq '') {
                   5919:             $sec = &mt('None');
                   5920:         }
                   5921:         my ($rolestart,$roleend);
                   5922:         if ($roleslog{$id}{'delflag'}) {
                   5923:             $rolestart = &mt('deleted');
                   5924:             $roleend = &mt('deleted');
                   5925:         } else {
                   5926:             $rolestart = $roleslog{$id}{'logentry'}{'start'};
                   5927:             $roleend = $roleslog{$id}{'logentry'}{'end'};
                   5928:             if ($rolestart eq '' || $rolestart == 0) {
                   5929:                 $rolestart = &mt('No start date'); 
                   5930:             } else {
                   5931:                 $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
                   5932:             }
                   5933:             if ($roleend eq '' || $roleend == 0) { 
                   5934:                 $roleend = &mt('No end date');
                   5935:             } else {
                   5936:                 $roleend = &Apache::lonlocal::locallocaltime($roleend);
                   5937:             }
                   5938:         }
                   5939:         my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
                   5940:         if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
                   5941:             $chgcontext = 'selfenroll';
                   5942:         }
1.363     raeburn  5943:         my %lt = &rolechg_contexts($context,$crstype);
1.239     raeburn  5944:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
                   5945:             $chgcontext = $lt{$chgcontext};
                   5946:         }
1.327     raeburn  5947:         $r->print(
1.301     bisitz   5948:             &Apache::loncommon::start_data_table_row()
                   5949:            .'<td>'.$count.'</td>'
                   5950:            .'<td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td>'
                   5951:            .'<td>'.$whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.'</td>'
                   5952:            .'<td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td>'
1.363     raeburn  5953:            .'<td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'},$crstype).'</td>');
                   5954:         if ($context eq 'course') { 
                   5955:             $r->print('<td>'.$sec.'</td>');
                   5956:         }
                   5957:         $r->print(
                   5958:             '<td>'.$chgcontext.'</td>'
1.301     bisitz   5959:            .'<td>'.$rolestart.'</td>'
                   5960:            .'<td>'.$roleend.'</td>'
1.327     raeburn  5961:            .&Apache::loncommon::end_data_table_row()."\n");
1.301     bisitz   5962:     }
                   5963: 
1.327     raeburn  5964:     if ($showntableheader) { # Table footer, if content displayed above
                   5965:         $r->print(&Apache::loncommon::end_data_table()
                   5966:                  .$nav_links);
                   5967:     } else { # No content displayed above
1.301     bisitz   5968:         $r->print('<p class="LC_info">'
                   5969:                  .&mt('There are no records to display.')
                   5970:                  .'</p>'
                   5971:         );
1.239     raeburn  5972:     }
1.301     bisitz   5973: 
1.327     raeburn  5974:     # Form Footer
                   5975:     $r->print( 
                   5976:         '<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   5977:        .'<input type="hidden" name="action" value="changelogs" />'
                   5978:        .'</form>');
                   5979:     return;
                   5980: }
1.301     bisitz   5981: 
1.327     raeburn  5982: sub userlogdisplay_nav {
                   5983:     my ($formname,$curr,$more_records) = @_;
                   5984:     my ($nav_script,$nav_links);
                   5985:     if (ref($curr) eq 'HASH') {
                   5986:         # Create Navigation:
                   5987:         # Navigation Script
                   5988:         $nav_script = <<"ENDSCRIPT";
1.239     raeburn  5989: <script type="text/javascript">
1.301     bisitz   5990: // <![CDATA[
1.239     raeburn  5991: function chgPage(caller) {
                   5992:     if (caller == 'previous') {
                   5993:         document.$formname.page.value --;
                   5994:     }
                   5995:     if (caller == 'next') {
                   5996:         document.$formname.page.value ++;
                   5997:     }
1.327     raeburn  5998:     document.$formname.submit();
1.239     raeburn  5999:     return;
                   6000: }
1.301     bisitz   6001: // ]]>
1.239     raeburn  6002: </script>
                   6003: ENDSCRIPT
1.327     raeburn  6004:         # Navigation Buttons
                   6005:         $nav_links = '<p>';
                   6006:         if (($curr->{'page'} > 1) || ($more_records)) {
                   6007:             if ($curr->{'page'} > 1) {
                   6008:                 $nav_links .= '<input type="button"'
                   6009:                              .' onclick="javascript:chgPage('."'previous'".');"'
                   6010:                              .' value="'.&mt('Previous [_1] changes',$curr->{'show'})
                   6011:                              .'" /> ';
                   6012:             }
                   6013:             if ($more_records) {
                   6014:                 $nav_links .= '<input type="button"'
                   6015:                              .' onclick="javascript:chgPage('."'next'".');"'
                   6016:                              .' value="'.&mt('Next [_1] changes',$curr->{'show'})
                   6017:                              .'" />';
                   6018:             }
1.301     bisitz   6019:         }
1.327     raeburn  6020:         $nav_links .= '</p>';
1.301     bisitz   6021:     }
1.327     raeburn  6022:     return ($nav_script,$nav_links);
1.239     raeburn  6023: }
                   6024: 
                   6025: sub role_display_filter {
1.363     raeburn  6026:     my ($context,$formname,$cdom,$cnum,$curr,$version,$crstype) = @_;
                   6027:     my $lctype;
                   6028:     if ($context eq 'course') {
                   6029:         $lctype = lc($crstype);
                   6030:     }
1.239     raeburn  6031:     my $nolink = 1;
                   6032:     my $output = '<table><tr><td valign="top">'.
1.301     bisitz   6033:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b></span><br />'.
1.239     raeburn  6034:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   6035:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   6036:                  '</td><td>&nbsp;&nbsp;</td>';
                   6037:     my $startform =
                   6038:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
                   6039:                                             $curr->{'rolelog_start_date'},undef,
                   6040:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   6041:     my $endform =
                   6042:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
                   6043:                                             $curr->{'rolelog_end_date'},undef,
                   6044:                                             undef,undef,undef,undef,undef,undef,$nolink);
1.363     raeburn  6045:     my %lt = &rolechg_contexts($context,$crstype);
1.301     bisitz   6046:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br />'.
                   6047:                '<table><tr><td>'.&mt('After:').
                   6048:                '</td><td>'.$startform.'</td></tr>'.
                   6049:                '<tr><td>'.&mt('Before:').'</td>'.
                   6050:                '<td>'.$endform.'</td></tr></table>'.
                   6051:                '</td>'.
                   6052:                '<td>&nbsp;&nbsp;</td>'.
1.239     raeburn  6053:                '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
                   6054:                '<select name="role"><option value="any"';
                   6055:     if ($curr->{'role'} eq 'any') {
                   6056:         $output .= ' selected="selected"';
                   6057:     }
                   6058:     $output .=  '>'.&mt('Any').'</option>'."\n";
1.363     raeburn  6059:     my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
1.239     raeburn  6060:     foreach my $role (@roles) {
                   6061:         my $plrole;
                   6062:         if ($role eq 'cr') {
                   6063:             $plrole = &mt('Custom Role');
                   6064:         } else {
1.318     raeburn  6065:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.239     raeburn  6066:         }
                   6067:         my $selstr = '';
                   6068:         if ($role eq $curr->{'role'}) {
                   6069:             $selstr = ' selected="selected"';
                   6070:         }
                   6071:         $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
                   6072:     }
1.301     bisitz   6073:     $output .= '</select></td>'.
                   6074:                '<td>&nbsp;&nbsp;</td>'.
                   6075:                '<td valign="top"><b>'.
1.239     raeburn  6076:                &mt('Context:').'</b><br /><select name="chgcontext">';
1.363     raeburn  6077:     my @posscontexts;
                   6078:     if ($context eq 'course') {
1.376     raeburn  6079:         @posscontexts = ('any','automated','updatenow','createcourse','course','domain','selfenroll','requestcourses');
1.363     raeburn  6080:     } elsif ($context eq 'domain') {
                   6081:         @posscontexts = ('any','domain','requestauthor','domconfig','server');
                   6082:     } else {
                   6083:         @posscontexts = ('any','author','domain');
                   6084:     } 
                   6085:     foreach my $chgtype (@posscontexts) {
1.239     raeburn  6086:         my $selstr = '';
                   6087:         if ($curr->{'chgcontext'} eq $chgtype) {
1.301     bisitz   6088:             $selstr = ' selected="selected"';
1.239     raeburn  6089:         }
1.363     raeburn  6090:         if ($context eq 'course') {
1.376     raeburn  6091:             if (($chgtype eq 'automated') || ($chgtype eq 'updatenow')) {
1.363     raeburn  6092:                 next if (!&Apache::lonnet::auto_run($cnum,$cdom));
                   6093:             }
1.239     raeburn  6094:         }
                   6095:         $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
1.248     raeburn  6096:     }
1.303     bisitz   6097:     $output .= '</select></td>'
                   6098:               .'</tr></table>';
                   6099: 
                   6100:     # Update Display button
                   6101:     $output .= '<p>'
                   6102:               .'<input type="submit" value="'.&mt('Update Display').'" />'
                   6103:               .'</p>';
                   6104: 
                   6105:     # Server version info
1.363     raeburn  6106:     my $needsrev = '2.11.0';
                   6107:     if ($context eq 'course') {
                   6108:         $needsrev = '2.7.0';
                   6109:     }
                   6110:     
1.303     bisitz   6111:     $output .= '<p class="LC_info">'
                   6112:               .&mt('Only changes made from servers running LON-CAPA [_1] or later are displayed.'
1.363     raeburn  6113:                   ,$needsrev);
1.248     raeburn  6114:     if ($version) {
1.303     bisitz   6115:         $output .= ' '.&mt('This LON-CAPA server is version [_1]',$version);
                   6116:     }
                   6117:     $output .= '</p><hr />';
1.239     raeburn  6118:     return $output;
                   6119: }
                   6120: 
                   6121: sub rolechg_contexts {
1.363     raeburn  6122:     my ($context,$crstype) = @_;
                   6123:     my %lt;
                   6124:     if ($context eq 'course') {
                   6125:         %lt = &Apache::lonlocal::texthash (
1.239     raeburn  6126:                                              any          => 'Any',
1.376     raeburn  6127:                                              automated    => 'Automated Enrollment',
1.239     raeburn  6128:                                              updatenow    => 'Roster Update',
                   6129:                                              createcourse => 'Course Creation',
                   6130:                                              course       => 'User Management in course',
                   6131:                                              domain       => 'User Management in domain',
1.313     raeburn  6132:                                              selfenroll   => 'Self-enrolled',
1.318     raeburn  6133:                                              requestcourses => 'Course Request',
1.239     raeburn  6134:                                          );
1.363     raeburn  6135:         if ($crstype eq 'Community') {
                   6136:             $lt{'createcourse'} = &mt('Community Creation');
                   6137:             $lt{'course'} = &mt('User Management in community');
                   6138:             $lt{'requestcourses'} = &mt('Community Request');
                   6139:         }
                   6140:     } elsif ($context eq 'domain') {
                   6141:         %lt = &Apache::lonlocal::texthash (
                   6142:                                              any           => 'Any',
                   6143:                                              domain        => 'User Management in domain',
                   6144:                                              requestauthor => 'Authoring Request',
                   6145:                                              server        => 'Command line script (DC role)',
                   6146:                                              domconfig     => 'Self-enrolled',
                   6147:                                          );
                   6148:     } else {
                   6149:         %lt = &Apache::lonlocal::texthash (
                   6150:                                              any    => 'Any',
                   6151:                                              domain => 'User Management in domain',
                   6152:                                              author => 'User Management by author',
                   6153:                                          );
                   6154:     } 
1.239     raeburn  6155:     return %lt;
                   6156: }
                   6157: 
1.27      matthew  6158: #-------------------------------------------------- functions for &phase_two
1.160     raeburn  6159: sub user_search_result {
1.221     raeburn  6160:     my ($context,$srch) = @_;
1.160     raeburn  6161:     my %allhomes;
                   6162:     my %inst_matches;
                   6163:     my %srch_results;
1.181     raeburn  6164:     my ($response,$currstate,$forcenewuser,$dirsrchres);
1.183     raeburn  6165:     $srch->{'srchterm'} =~ s/\s+/ /g;
1.176     raeburn  6166:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
1.160     raeburn  6167:         $response = &mt('Invalid search.');
                   6168:     }
                   6169:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
                   6170:         $response = &mt('Invalid search.');
                   6171:     }
1.177     raeburn  6172:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
1.160     raeburn  6173:         $response = &mt('Invalid search.');
                   6174:     }
                   6175:     if ($srch->{'srchterm'} eq '') {
                   6176:         $response = &mt('You must enter a search term.');
                   6177:     }
1.183     raeburn  6178:     if ($srch->{'srchterm'} =~ /^\s+$/) {
                   6179:         $response = &mt('Your search term must contain more than just spaces.');
                   6180:     }
1.160     raeburn  6181:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
                   6182:         if (($srch->{'srchdomain'} eq '') || 
1.163     albertel 6183: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
1.160     raeburn  6184:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
                   6185:         }
                   6186:     }
                   6187:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
                   6188:         ($srch->{'srchin'} eq 'alc')) {
1.176     raeburn  6189:         if ($srch->{'srchby'} eq 'uname') {
1.243     raeburn  6190:             my $unamecheck = $srch->{'srchterm'};
                   6191:             if ($srch->{'srchtype'} eq 'contains') {
                   6192:                 if ($unamecheck !~ /^\w/) {
                   6193:                     $unamecheck = 'a'.$unamecheck; 
                   6194:                 }
                   6195:             }
                   6196:             if ($unamecheck !~ /^$match_username$/) {
1.176     raeburn  6197:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
                   6198:             }
1.160     raeburn  6199:         }
                   6200:     }
1.180     raeburn  6201:     if ($response ne '') {
                   6202:         $response = '<span class="LC_warning">'.$response.'</span>';
                   6203:     }
1.160     raeburn  6204:     if ($srch->{'srchin'} eq 'instd') {
                   6205:         my $instd_chk = &directorysrch_check($srch);
                   6206:         if ($instd_chk ne 'ok') {
1.180     raeburn  6207:             $response = '<span class="LC_warning">'.$instd_chk.'</span>'.
                   6208:                         '<br />'.&mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').'<br /><br />';
1.160     raeburn  6209:         }
                   6210:     }
                   6211:     if ($response ne '') {
1.180     raeburn  6212:         return ($currstate,$response);
1.160     raeburn  6213:     }
                   6214:     if ($srch->{'srchby'} eq 'uname') {
                   6215:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
                   6216:             if ($env{'form.forcenew'}) {
                   6217:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
                   6218:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   6219:                     if ($uhome eq 'no_host') {
                   6220:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.180     raeburn  6221:                         my $showdom = &display_domain_info($env{'request.role.domain'});
                   6222:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
1.160     raeburn  6223:                     } else {
1.179     raeburn  6224:                         $currstate = 'modify';
1.160     raeburn  6225:                     }
                   6226:                 } else {
1.179     raeburn  6227:                     $currstate = 'modify';
1.160     raeburn  6228:                 }
                   6229:             } else {
                   6230:                 if ($srch->{'srchin'} eq 'dom') {
1.162     raeburn  6231:                     if ($srch->{'srchtype'} eq 'exact') {
                   6232:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   6233:                         if ($uhome eq 'no_host') {
1.179     raeburn  6234:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  6235:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  6236:                         } else {
1.179     raeburn  6237:                             $currstate = 'modify';
1.310     raeburn  6238:                             my $uname = $srch->{'srchterm'};
                   6239:                             my $udom = $srch->{'srchdomain'};
                   6240:                             $srch_results{$uname.':'.$udom} =
                   6241:                                 { &Apache::lonnet::get('environment',
                   6242:                                                        ['firstname',
                   6243:                                                         'lastname',
                   6244:                                                         'permanentemail'],
                   6245:                                                          $udom,$uname)
                   6246:                                 };
1.162     raeburn  6247:                         }
                   6248:                     } else {
                   6249:                         %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  6250:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  6251:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  6252:                     }
                   6253:                 } else {
1.167     albertel 6254:                     my $courseusers = &get_courseusers();
1.162     raeburn  6255:                     if ($srch->{'srchtype'} eq 'exact') {
1.167     albertel 6256:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
1.179     raeburn  6257:                             $currstate = 'modify';
1.162     raeburn  6258:                         } else {
1.179     raeburn  6259:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  6260:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  6261:                         }
1.160     raeburn  6262:                     } else {
1.167     albertel 6263:                         foreach my $user (keys(%$courseusers)) {
1.162     raeburn  6264:                             my ($cuname,$cudomain) = split(/:/,$user);
                   6265:                             if ($cudomain eq $srch->{'srchdomain'}) {
1.177     raeburn  6266:                                 my $matched = 0;
                   6267:                                 if ($srch->{'srchtype'} eq 'begins') {
                   6268:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
                   6269:                                         $matched = 1;
                   6270:                                     }
                   6271:                                 } else {
                   6272:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
                   6273:                                         $matched = 1;
                   6274:                                     }
                   6275:                                 }
                   6276:                                 if ($matched) {
1.167     albertel 6277:                                     $srch_results{$user} = 
                   6278: 					{&Apache::lonnet::get('environment',
                   6279: 							     ['firstname',
                   6280: 							      'lastname',
1.194     albertel 6281: 							      'permanentemail'],
                   6282: 							      $cudomain,$cuname)};
1.162     raeburn  6283:                                 }
                   6284:                             }
                   6285:                         }
1.179     raeburn  6286:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  6287:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  6288:                     }
                   6289:                 }
                   6290:             }
                   6291:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  6292:             $currstate = 'query';
1.160     raeburn  6293:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  6294:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
                   6295:             if ($dirsrchres eq 'ok') {
                   6296:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  6297:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  6298:             } else {
                   6299:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   6300:                 $response = '<span class="LC_warning">'.
                   6301:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   6302:                     '</span><br />'.
                   6303:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
                   6304:                     '<br /><br />'; 
                   6305:             }
1.160     raeburn  6306:         }
                   6307:     } else {
                   6308:         if ($srch->{'srchin'} eq 'dom') {
                   6309:             %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  6310:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  6311:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  6312:         } elsif ($srch->{'srchin'} eq 'crs') {
1.167     albertel 6313:             my $courseusers = &get_courseusers(); 
                   6314:             foreach my $user (keys(%$courseusers)) {
1.160     raeburn  6315:                 my ($uname,$udom) = split(/:/,$user);
                   6316:                 my %names = &Apache::loncommon::getnames($uname,$udom);
                   6317:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
                   6318:                 if ($srch->{'srchby'} eq 'lastname') {
                   6319:                     if ((($srch->{'srchtype'} eq 'exact') && 
                   6320:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
1.177     raeburn  6321:                         (($srch->{'srchtype'} eq 'begins') &&
                   6322:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
1.160     raeburn  6323:                         (($srch->{'srchtype'} eq 'contains') &&
                   6324:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
                   6325:                         $srch_results{$user} = {firstname => $names{'firstname'},
                   6326:                                             lastname => $names{'lastname'},
                   6327:                                             permanentemail => $emails{'permanentemail'},
                   6328:                                            };
                   6329:                     }
                   6330:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
                   6331:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
1.177     raeburn  6332:                     $srchlast =~ s/\s+$//;
                   6333:                     $srchfirst =~ s/^\s+//;
1.160     raeburn  6334:                     if ($srch->{'srchtype'} eq 'exact') {
                   6335:                         if (($names{'lastname'} eq $srchlast) &&
                   6336:                             ($names{'firstname'} eq $srchfirst)) {
                   6337:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   6338:                                                 lastname => $names{'lastname'},
                   6339:                                                 permanentemail => $emails{'permanentemail'},
                   6340: 
                   6341:                                            };
                   6342:                         }
1.177     raeburn  6343:                     } elsif ($srch->{'srchtype'} eq 'begins') {
                   6344:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
                   6345:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
                   6346:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   6347:                                                 lastname => $names{'lastname'},
                   6348:                                                 permanentemail => $emails{'permanentemail'},
                   6349:                                                };
                   6350:                         }
                   6351:                     } else {
1.160     raeburn  6352:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
                   6353:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
                   6354:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   6355:                                                 lastname => $names{'lastname'},
                   6356:                                                 permanentemail => $emails{'permanentemail'},
                   6357:                                                };
                   6358:                         }
                   6359:                     }
                   6360:                 }
                   6361:             }
1.179     raeburn  6362:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  6363:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  6364:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  6365:             $currstate = 'query';
1.160     raeburn  6366:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  6367:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
                   6368:             if ($dirsrchres eq 'ok') {
                   6369:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  6370:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  6371:             } else {
                   6372:                 my $showdom = &display_domain_info($srch->{'srchdomain'});                $response = '<span class="LC_warning">'.
                   6373:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   6374:                     '</span><br />'.
                   6375:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
                   6376:                     '<br /><br />';
                   6377:             }
1.160     raeburn  6378:         }
                   6379:     }
1.179     raeburn  6380:     return ($currstate,$response,$forcenewuser,\%srch_results);
1.160     raeburn  6381: }
                   6382: 
                   6383: sub directorysrch_check {
                   6384:     my ($srch) = @_;
                   6385:     my $can_search = 0;
                   6386:     my $response;
                   6387:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   6388:                                              ['directorysrch'],$srch->{'srchdomain'});
1.180     raeburn  6389:     my $showdom = &display_domain_info($srch->{'srchdomain'});
1.160     raeburn  6390:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   6391:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
1.180     raeburn  6392:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
1.160     raeburn  6393:         }
                   6394:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
                   6395:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
1.180     raeburn  6396:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
1.160     raeburn  6397:             }
                   6398:             my @usertypes = split(/:/,$env{'environment.inststatus'});
                   6399:             if (!@usertypes) {
                   6400:                 push(@usertypes,'default');
                   6401:             }
                   6402:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
                   6403:                 foreach my $type (@usertypes) {
                   6404:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
                   6405:                         $can_search = 1;
                   6406:                         last;
                   6407:                     }
                   6408:                 }
                   6409:             }
                   6410:             if (!$can_search) {
                   6411:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
                   6412:                 my @longtypes; 
                   6413:                 foreach my $item (@usertypes) {
1.229     raeburn  6414:                     if (defined($insttypes->{$item})) { 
                   6415:                         push (@longtypes,$insttypes->{$item});
                   6416:                     } elsif ($item eq 'default') {
                   6417:                         push (@longtypes,&mt('other')); 
                   6418:                     }
1.160     raeburn  6419:                 }
                   6420:                 my $insttype_str = join(', ',@longtypes); 
1.180     raeburn  6421:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
1.229     raeburn  6422:             }
1.160     raeburn  6423:         } else {
                   6424:             $can_search = 1;
                   6425:         }
                   6426:     } else {
1.180     raeburn  6427:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
1.160     raeburn  6428:     }
                   6429:     my %longtext = &Apache::lonlocal::texthash (
1.167     albertel 6430:                        uname     => 'username',
1.160     raeburn  6431:                        lastfirst => 'last name, first name',
1.167     albertel 6432:                        lastname  => 'last name',
1.172     raeburn  6433:                        contains  => 'contains',
1.178     raeburn  6434:                        exact     => 'as exact match to',
                   6435:                        begins    => 'begins with',
1.160     raeburn  6436:                    );
                   6437:     if ($can_search) {
                   6438:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
                   6439:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
1.180     raeburn  6440:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
1.160     raeburn  6441:             }
                   6442:         } else {
1.180     raeburn  6443:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
1.160     raeburn  6444:         }
                   6445:     }
                   6446:     if ($can_search) {
1.178     raeburn  6447:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
                   6448:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
                   6449:                 return 'ok';
                   6450:             } else {
1.180     raeburn  6451:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  6452:             }
                   6453:         } else {
                   6454:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
                   6455:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
                   6456:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
                   6457:                 return 'ok';
                   6458:             } else {
1.180     raeburn  6459:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  6460:             }
1.160     raeburn  6461:         }
                   6462:     }
                   6463: }
                   6464: 
                   6465: sub get_courseusers {
                   6466:     my %advhash;
1.167     albertel 6467:     my $classlist = &Apache::loncoursedata::get_classlist();
1.160     raeburn  6468:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                   6469:     foreach my $role (sort(keys(%coursepersonnel))) {
                   6470:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
1.167     albertel 6471: 	    if (!exists($classlist->{$user})) {
                   6472: 		$classlist->{$user} = [];
                   6473: 	    }
1.160     raeburn  6474:         }
                   6475:     }
1.167     albertel 6476:     return $classlist;
1.160     raeburn  6477: }
                   6478: 
                   6479: sub build_search_response {
1.221     raeburn  6480:     my ($context,$srch,%srch_results) = @_;
1.179     raeburn  6481:     my ($currstate,$response,$forcenewuser);
1.160     raeburn  6482:     my %names = (
1.330     bisitz   6483:           'uname'     => 'username',
                   6484:           'lastname'  => 'last name',
1.160     raeburn  6485:           'lastfirst' => 'last name, first name',
1.330     bisitz   6486:           'crs'       => 'this course',
                   6487:           'dom'       => 'LON-CAPA domain',
                   6488:           'instd'     => 'the institutional directory for domain',
1.160     raeburn  6489:     );
                   6490: 
                   6491:     my %single = (
1.180     raeburn  6492:                    begins   => 'A match',
1.160     raeburn  6493:                    contains => 'A match',
1.180     raeburn  6494:                    exact    => 'An exact match',
1.160     raeburn  6495:                  );
                   6496:     my %nomatch = (
1.180     raeburn  6497:                    begins   => 'No match',
1.160     raeburn  6498:                    contains => 'No match',
1.180     raeburn  6499:                    exact    => 'No exact match',
1.160     raeburn  6500:                   );
                   6501:     if (keys(%srch_results) > 1) {
1.179     raeburn  6502:         $currstate = 'select';
1.160     raeburn  6503:     } else {
                   6504:         if (keys(%srch_results) == 1) {
1.179     raeburn  6505:             $currstate = 'modify';
1.180     raeburn  6506:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
                   6507:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   6508:                 $response .= ': '.&display_domain_info($srch->{'srchdomain'});
1.180     raeburn  6509:             }
1.330     bisitz   6510:         } else { # Search has nothing found. Prepare message to user.
                   6511:             $response = '<span class="LC_warning">';
1.180     raeburn  6512:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
1.330     bisitz   6513:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}: [_2]",
                   6514:                                  '<b>'.$srch->{'srchterm'}.'</b>',
                   6515:                                  &display_domain_info($srch->{'srchdomain'}));
                   6516:             } else {
                   6517:                 $response .= &mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} [_1] in $names{$srch->{'srchin'}}.",
                   6518:                                  '<b>'.$srch->{'srchterm'}.'</b>');
1.180     raeburn  6519:             }
                   6520:             $response .= '</span>';
1.330     bisitz   6521: 
1.160     raeburn  6522:             if ($srch->{'srchin'} ne 'alc') {
                   6523:                 $forcenewuser = 1;
                   6524:                 my $cansrchinst = 0; 
                   6525:                 if ($srch->{'srchdomain'}) {
                   6526:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
                   6527:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
                   6528:                         if ($domconfig{'directorysrch'}{'available'}) {
                   6529:                             $cansrchinst = 1;
                   6530:                         } 
                   6531:                     }
                   6532:                 }
1.180     raeburn  6533:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
                   6534:                      ($srch->{'srchby'} eq 'lastname')) &&
                   6535:                     ($srch->{'srchin'} eq 'dom')) {
                   6536:                     if ($cansrchinst) {
                   6537:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
1.160     raeburn  6538:                     }
                   6539:                 }
1.180     raeburn  6540:                 if ($srch->{'srchin'} eq 'crs') {
                   6541:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
                   6542:                 }
                   6543:             }
1.305     raeburn  6544:             my $createdom = $env{'request.role.domain'};
                   6545:             if ($context eq 'requestcrs') {
                   6546:                 if ($env{'form.coursedom'} ne '') {
                   6547:                     $createdom = $env{'form.coursedom'};
                   6548:                 }
                   6549:             }
                   6550:             if (!($srch->{'srchby'} eq 'uname' && $srch->{'srchin'} eq 'dom' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchdomain'} eq $createdom)) {
1.221     raeburn  6551:                 my $cancreate =
1.305     raeburn  6552:                     &Apache::lonuserutils::can_create_user($createdom,$context);
                   6553:                 my $targetdom = '<span class="LC_cusr_emph">'.$createdom.'</span>';
1.221     raeburn  6554:                 if ($cancreate) {
1.305     raeburn  6555:                     my $showdom = &display_domain_info($createdom); 
1.266     bisitz   6556:                     $response .= '<br /><br />'
                   6557:                                 .'<b>'.&mt('To add a new user:').'</b>'
1.305     raeburn  6558:                                 .'<br />';
                   6559:                     if ($context eq 'requestcrs') {
                   6560:                         $response .= &mt("(You can only define new users in the new course's domain - [_1])",$targetdom);
                   6561:                     } else {
                   6562:                         $response .= &mt("(You can only create new users in your current role's domain - [_1])",$targetdom);
                   6563:                     }
                   6564:                     $response .='<ul><li>'
1.266     bisitz   6565:                                 .&mt("Set 'Domain/institution to search' to: [_1]",'<span class="LC_cusr_emph">'.$showdom.'</span>')
                   6566:                                 .'</li><li>'
                   6567:                                 .&mt("Set 'Search criteria' to: [_1]username is ..... in selected LON-CAPA domain[_2]",'<span class="LC_cusr_emph">','</span>')
                   6568:                                 .'</li><li>'
                   6569:                                 .&mt('Provide the proposed username')
                   6570:                                 .'</li><li>'
                   6571:                                 .&mt("Click 'Search'")
                   6572:                                 .'</li></ul><br />';
1.221     raeburn  6573:                 } else {
                   6574:                     my $helplink = ' href="javascript:helpMenu('."'display'".')"';
1.305     raeburn  6575:                     $response .= '<br /><br />';
                   6576:                     if ($context eq 'requestcrs') {
1.314     raeburn  6577:                         $response .= &mt("You are not authorized to define new users in the new course's domain - [_1].",$targetdom);
1.305     raeburn  6578:                     } else {
                   6579:                         $response .= &mt("You are not authorized to create new users in your current role's domain - [_1].",$targetdom);
                   6580:                     }
                   6581:                     $response .= '<br />'
                   6582:                                  .&mt('Please contact the [_1]helpdesk[_2] if you need to create a new user.'
1.266     bisitz   6583:                                     ,' <a'.$helplink.'>'
                   6584:                                     ,'</a>')
1.305     raeburn  6585:                                  .'<br /><br />';
1.221     raeburn  6586:                 }
1.160     raeburn  6587:             }
                   6588:         }
                   6589:     }
1.179     raeburn  6590:     return ($currstate,$response,$forcenewuser);
1.160     raeburn  6591: }
                   6592: 
1.180     raeburn  6593: sub display_domain_info {
                   6594:     my ($dom) = @_;
                   6595:     my $output = $dom;
                   6596:     if ($dom ne '') { 
                   6597:         my $domdesc = &Apache::lonnet::domain($dom,'description');
                   6598:         if ($domdesc ne '') {
                   6599:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
                   6600:         }
                   6601:     }
                   6602:     return $output;
                   6603: }
                   6604: 
1.160     raeburn  6605: sub crumb_utilities {
                   6606:     my %elements = (
                   6607:        crtuser => {
                   6608:            srchterm => 'text',
1.172     raeburn  6609:            srchin => 'selectbox',
1.160     raeburn  6610:            srchby => 'selectbox',
                   6611:            srchtype => 'selectbox',
                   6612:            srchdomain => 'selectbox',
                   6613:        },
1.207     raeburn  6614:        crtusername => {
                   6615:            srchterm => 'text',
                   6616:            srchdomain => 'selectbox',
                   6617:        },
1.160     raeburn  6618:        docustom => {
                   6619:            rolename => 'selectbox',
                   6620:            newrolename => 'textbox',
                   6621:        },
1.179     raeburn  6622:        studentform => {
                   6623:            srchterm => 'text',
                   6624:            srchin => 'selectbox',
                   6625:            srchby => 'selectbox',
                   6626:            srchtype => 'selectbox',
                   6627:            srchdomain => 'selectbox',
                   6628:        },
1.160     raeburn  6629:     );
                   6630: 
                   6631:     my $jsback .= qq|
                   6632: function backPage(formname,prevphase,prevstate) {
1.211     raeburn  6633:     if (typeof prevphase == 'undefined') {
                   6634:         formname.phase.value = '';
                   6635:     }
                   6636:     else {  
                   6637:         formname.phase.value = prevphase;
                   6638:     }
                   6639:     if (typeof prevstate == 'undefined') {
                   6640:         formname.currstate.value = '';
                   6641:     }
                   6642:     else {
                   6643:         formname.currstate.value = prevstate;
                   6644:     }
1.160     raeburn  6645:     formname.submit();
                   6646: }
                   6647: |;
                   6648:     return ($jsback,\%elements);
                   6649: }
                   6650: 
1.26      matthew  6651: sub course_level_table {
1.375     raeburn  6652:     my ($inccourses,$showcredits,$defaultcredits) = @_;
                   6653:     return unless (ref($inccourses) eq 'HASH');
1.26      matthew  6654:     my $table = '';
1.62      www      6655: # Custom Roles?
                   6656: 
1.190     raeburn  6657:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.89      raeburn  6658:     my %lt=&Apache::lonlocal::texthash(
                   6659:             'exs'  => "Existing sections",
                   6660:             'new'  => "Define new section",
                   6661:             'ssd'  => "Set Start Date",
                   6662:             'sed'  => "Set End Date",
1.131     raeburn  6663:             'crl'  => "Course Level",
1.89      raeburn  6664:             'act'  => "Activate",
                   6665:             'rol'  => "Role",
                   6666:             'ext'  => "Extent",
1.113     raeburn  6667:             'grs'  => "Section",
1.375     raeburn  6668:             'crd'  => "Credits",
1.89      raeburn  6669:             'sta'  => "Start",
                   6670:             'end'  => "End"
                   6671:     );
1.62      www      6672: 
1.375     raeburn  6673:     foreach my $protectedcourse (sort(keys(%{$inccourses}))) {
1.135     raeburn  6674: 	my $thiscourse=$protectedcourse;
1.26      matthew  6675: 	$thiscourse=~s:_:/:g;
                   6676: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
1.365     raeburn  6677:         my $isowner = &Apache::lonuserutils::is_courseowner($protectedcourse,$coursedata{'internal.courseowner'});
1.26      matthew  6678: 	my $area=$coursedata{'description'};
1.321     raeburn  6679:         my $crstype=$coursedata{'type'};
1.135     raeburn  6680: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
1.89      raeburn  6681: 	my ($domain,$cnum)=split(/\//,$thiscourse);
1.115     albertel 6682:         my %sections_count;
1.101     albertel 6683:         if (defined($env{'request.course.id'})) {
                   6684:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
1.115     albertel 6685:                 %sections_count = 
                   6686: 		    &Apache::loncommon::get_sections($domain,$cnum);
1.92      raeburn  6687:             }
                   6688:         }
1.321     raeburn  6689:         my @roles = &Apache::lonuserutils::roles_by_context('course','',$crstype);
1.213     raeburn  6690: 	foreach my $role (@roles) {
1.321     raeburn  6691:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.329     raeburn  6692: 	    if ((&Apache::lonnet::allowed('c'.$role,$thiscourse)) ||
                   6693:                 ((($role eq 'cc') || ($role eq 'co')) && ($isowner))) {
1.221     raeburn  6694:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  6695:                                             $plrole,\%sections_count,\%lt,
                   6696:                                             $defaultcredits,$crstype);
1.221     raeburn  6697:             } elsif ($env{'request.course.sec'} ne '') {
                   6698:                 if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
                   6699:                                              $env{'request.course.sec'})) {
                   6700:                     $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.375     raeburn  6701:                                                 $plrole,\%sections_count,\%lt,
                   6702:                                                 $defaultcredits,$crstype);
1.26      matthew  6703:                 }
                   6704:             }
                   6705:         }
1.221     raeburn  6706:         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
1.324     raeburn  6707:             foreach my $cust (sort(keys(%customroles))) {
                   6708:                 next if ($crstype eq 'Community' && $customroles{$cust} =~ /bre\&S/);
1.221     raeburn  6709:                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
                   6710:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
                   6711:                                             $cust,\%sections_count,\%lt);
                   6712:             }
1.62      www      6713: 	}
1.26      matthew  6714:     }
                   6715:     return '' if ($table eq ''); # return nothing if there is nothing 
                   6716:                                  # in the table
1.188     raeburn  6717:     my $result;
                   6718:     if (!$env{'request.course.id'}) {
                   6719:         $result = '<h4>'.$lt{'crl'}.'</h4>'."\n";
                   6720:     }
                   6721:     $result .= 
1.136     raeburn  6722: &Apache::loncommon::start_data_table().
                   6723: &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  6724: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
                   6725: '<th>'.$lt{'ext'}.'</th><th>'.$lt{'crd'}.'</th>'."\n".
                   6726: '<th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th>'."\n".
                   6727: '<th>'.$lt{'end'}.'</th>'.
1.136     raeburn  6728: &Apache::loncommon::end_data_table_header_row().
                   6729: $table.
                   6730: &Apache::loncommon::end_data_table();
1.26      matthew  6731:     return $result;
                   6732: }
1.88      raeburn  6733: 
1.221     raeburn  6734: sub course_level_row {
1.375     raeburn  6735:     my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,
                   6736:         $lt,$defaultcredits,$crstype) = @_;
                   6737:     my $creditem;
1.222     raeburn  6738:     my $row = &Apache::loncommon::start_data_table_row().
                   6739:               ' <td><input type="checkbox" name="act_'.
                   6740:               $protectedcourse.'_'.$role.'" /></td>'."\n".
                   6741:               ' <td>'.$plrole.'</td>'."\n".
                   6742:               ' <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
1.375     raeburn  6743:     if (($role eq 'st') && ($crstype eq 'Course')) {
                   6744:         $row .= 
                   6745:             '<td><input type="text" name="credits_'.$protectedcourse.'_'.
                   6746:             $role.'" size="3" value="'.$defaultcredits.'" /></td>';
                   6747:     } else {
                   6748:         $row .= '<td>&nbsp;</td>';
                   6749:     }
1.322     raeburn  6750:     if (($role eq 'cc') || ($role eq 'co')) {
1.222     raeburn  6751:         $row .= '<td>&nbsp;</td>';
1.221     raeburn  6752:     } elsif ($env{'request.course.sec'} ne '') {
1.222     raeburn  6753:         $row .= ' <td><input type="hidden" value="'.
                   6754:                 $env{'request.course.sec'}.'" '.
                   6755:                 'name="sec_'.$protectedcourse.'_'.$role.'" />'.
                   6756:                 $env{'request.course.sec'}.'</td>';
1.221     raeburn  6757:     } else {
                   6758:         if (ref($sections_count) eq 'HASH') {
                   6759:             my $currsec = 
                   6760:                 &Apache::lonuserutils::course_sections($sections_count,
                   6761:                                                        $protectedcourse.'_'.$role);
1.222     raeburn  6762:             $row .= '<td><table class="LC_createuser">'."\n".
                   6763:                     '<tr class="LC_section_row">'."\n".
                   6764:                     ' <td valign="top">'.$lt->{'exs'}.'<br />'.
                   6765:                        $currsec.'</td>'."\n".
                   6766:                      ' <td>&nbsp;&nbsp;</td>'."\n".
                   6767:                      ' <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
1.221     raeburn  6768:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
                   6769:                      '" value="" />'.
                   6770:                      '<input type="hidden" '.
                   6771:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
1.222     raeburn  6772:                      '</tr></table></td>'."\n";
1.221     raeburn  6773:         } else {
1.222     raeburn  6774:             $row .= '<td><input type="text" size="10" '.
1.375     raeburn  6775:                     'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n";
1.221     raeburn  6776:         }
                   6777:     }
1.222     raeburn  6778:     $row .= <<ENDTIMEENTRY;
                   6779: <td><input type="hidden" name="start_$protectedcourse\_$role" value="" />
1.221     raeburn  6780: <a href=
                   6781: "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  6782: <td><input type="hidden" name="end_$protectedcourse\_$role" value="" />
1.221     raeburn  6783: <a href=
                   6784: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
                   6785: ENDTIMEENTRY
1.222     raeburn  6786:     $row .= &Apache::loncommon::end_data_table_row();
                   6787:     return $row;
1.221     raeburn  6788: }
                   6789: 
1.88      raeburn  6790: sub course_level_dc {
1.375     raeburn  6791:     my ($dcdom,$showcredits) = @_;
1.190     raeburn  6792:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.213     raeburn  6793:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.88      raeburn  6794:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
                   6795:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
1.133     raeburn  6796:                       '<input type="hidden" name="dccourse" value="" />';
1.355     www      6797:     my $courseform=&Apache::loncommon::selectcourse_link
1.356     raeburn  6798:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Select','crstype');
1.375     raeburn  6799:     my $credit_elem;
                   6800:     if ($showcredits) {
                   6801:         $credit_elem = 'credits';
                   6802:     }
                   6803:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu','role','Course/Community Browser',$credit_elem);
1.88      raeburn  6804:     my %lt=&Apache::lonlocal::texthash(
                   6805:                     'rol'  => "Role",
1.113     raeburn  6806:                     'grs'  => "Section",
1.88      raeburn  6807:                     'exs'  => "Existing sections",
                   6808:                     'new'  => "Define new section", 
                   6809:                     'sta'  => "Start",
                   6810:                     'end'  => "End",
                   6811:                     'ssd'  => "Set Start Date",
1.355     www      6812:                     'sed'  => "Set End Date",
1.375     raeburn  6813:                     'scc'  => "Course/Community",
                   6814:                     'crd'  => "Credits",
1.88      raeburn  6815:                   );
1.323     raeburn  6816:     my $header = '<h4>'.&mt('Course/Community Level').'</h4>'.
1.136     raeburn  6817:                  &Apache::loncommon::start_data_table().
                   6818:                  &Apache::loncommon::start_data_table_header_row().
1.375     raeburn  6819:                  '<th>'.$lt{'scc'}.'</th><th>'.$lt{'rol'}.'</th>'."\n".
                   6820:                  '<th>'.$lt{'grs'}.'</th><th>'.$lt{'crd'}.'</th>'."\n".
                   6821:                  '<th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'."\n".
1.136     raeburn  6822:                  &Apache::loncommon::end_data_table_header_row();
1.143     raeburn  6823:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
1.356     raeburn  6824:                      '<td><br /><span class="LC_nobreak"><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc','','','','crstype'".')" />'.
                   6825:                      $courseform.('&nbsp;' x4).'</span></td>'."\n".
1.323     raeburn  6826:                      '<td valign><br /><select name="role">'."\n";
1.213     raeburn  6827:     foreach my $role (@roles) {
1.135     raeburn  6828:         my $plrole=&Apache::lonnet::plaintext($role);
                   6829:         $otheritems .= '  <option value="'.$role.'">'.$plrole;
1.88      raeburn  6830:     }
                   6831:     if ( keys %customroles > 0) {
1.135     raeburn  6832:         foreach my $cust (sort keys %customroles) {
1.101     albertel 6833:             my $custrole='cr_cr_'.$env{'user.domain'}.
1.135     raeburn  6834:                     '_'.$env{'user.name'}.'_'.$cust;
                   6835:             $otheritems .= '  <option value="'.$custrole.'">'.$cust;
1.88      raeburn  6836:         }
                   6837:     }
                   6838:     $otheritems .= '</select></td><td>'.
                   6839:                      '<table border="0" cellspacing="0" cellpadding="0">'.
                   6840:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
                   6841:                      ' <option value=""><--'.&mt('Pick course first').'</select></td>'.
                   6842:                      '<td>&nbsp;&nbsp;</td>'.
                   6843:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
1.113     raeburn  6844:                      '<input type="text" name="newsec" value="" />'.
1.237     raeburn  6845:                      '<input type="hidden" name="section" value="" />'.
1.323     raeburn  6846:                      '<input type="hidden" name="groups" value="" />'.
                   6847:                      '<input type="hidden" name="crstype" value="" /></td>'.
1.375     raeburn  6848:                      '</tr></table></td>'."\n";
                   6849:     if ($showcredits) {
                   6850:         $otheritems .= '<td><br />'."\n".
                   6851:                        '<input type="text" size="3" name="credits" value="" />'."\n";
                   6852:     }
1.88      raeburn  6853:     $otheritems .= <<ENDTIMEENTRY;
1.323     raeburn  6854: <td><br /><input type="hidden" name="start" value='' />
1.88      raeburn  6855: <a href=
                   6856: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.323     raeburn  6857: <td><br /><input type="hidden" name="end" value='' />
1.88      raeburn  6858: <a href=
                   6859: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
                   6860: ENDTIMEENTRY
1.136     raeburn  6861:     $otheritems .= &Apache::loncommon::end_data_table_row().
                   6862:                    &Apache::loncommon::end_data_table()."\n";
1.88      raeburn  6863:     return $cb_jscript.$header.$hiddenitems.$otheritems;
                   6864: }
                   6865: 
1.237     raeburn  6866: sub update_selfenroll_config {
1.241     raeburn  6867:     my ($r,$context,$permission) = @_;
1.237     raeburn  6868:     my ($row,$lt) = &get_selfenroll_titles();
1.241     raeburn  6869:     my %curr_groups = &Apache::longroup::coursegroups();
1.237     raeburn  6870:     my (%changes,%warning);
                   6871:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   6872:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.241     raeburn  6873:     my $curr_types;
1.237     raeburn  6874:     if (ref($row) eq 'ARRAY') {
                   6875:         foreach my $item (@{$row}) {
                   6876:             if ($item eq 'enroll_dates') {
                   6877:                 my (%currenrolldate,%newenrolldate);
                   6878:                 foreach my $type ('start','end') {
                   6879:                     $currenrolldate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_date'};
                   6880:                     $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
                   6881:                     if ($newenrolldate{$type} ne $currenrolldate{$type}) {
                   6882:                         $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
                   6883:                     }
                   6884:                 }
                   6885:             } elsif ($item eq 'access_dates') {
                   6886:                 my (%currdate,%newdate);
                   6887:                 foreach my $type ('start','end') {
                   6888:                     $currdate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_access'};
                   6889:                     $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
                   6890:                     if ($newdate{$type} ne $currdate{$type}) {
                   6891:                         $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
                   6892:                     }
                   6893:                 }
1.241     raeburn  6894:             } elsif ($item eq 'types') {
                   6895:                 $curr_types =
                   6896:                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$item};
                   6897:                 if ($env{'form.selfenroll_all'}) {
                   6898:                     if ($curr_types ne '*') {
                   6899:                         $changes{'internal.selfenroll_types'} = '*';
                   6900:                     } else {
                   6901:                         next;
                   6902:                     }
                   6903:                 } else {
1.249     raeburn  6904:                     my %currdoms;
1.241     raeburn  6905:                     my @entries = split(/;/,$curr_types);
                   6906:                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
1.249     raeburn  6907:                     my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
1.241     raeburn  6908:                     my $newnum = 0;
1.249     raeburn  6909:                     my @latesttypes;
                   6910:                     foreach my $num (@activations) {
                   6911:                         my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
                   6912:                         if (@types > 0) {
1.241     raeburn  6913:                             @types = sort(@types);
                   6914:                             my $typestr = join(',',@types);
1.249     raeburn  6915:                             my $typedom = $env{'form.selfenroll_dom_'.$num};
                   6916:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   6917:                             $currdoms{$typedom} = 1;
1.241     raeburn  6918:                             $newnum ++;
                   6919:                         }
                   6920:                     }
1.338     raeburn  6921:                     for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {
                   6922:                         if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
1.249     raeburn  6923:                             my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
                   6924:                             if (@types > 0) {
                   6925:                                 @types = sort(@types);
                   6926:                                 my $typestr = join(',',@types);
                   6927:                                 my $typedom = $env{'form.selfenroll_dom_'.$j};
                   6928:                                 $latesttypes[$newnum] = $typedom.':'.$typestr;
                   6929:                                 $currdoms{$typedom} = 1;
                   6930:                                 $newnum ++;
                   6931:                             }
                   6932:                         }
                   6933:                     }
                   6934:                     if ($env{'form.selfenroll_newdom'} ne '') {
                   6935:                         my $typedom = $env{'form.selfenroll_newdom'};
                   6936:                         if ((!defined($currdoms{$typedom})) && 
                   6937:                             (&Apache::lonnet::domain($typedom) ne '')) {
                   6938:                             my $typestr;
                   6939:                             my ($othertitle,$usertypes,$types) = 
                   6940:                                 &Apache::loncommon::sorted_inst_types($typedom);
                   6941:                             my $othervalue = 'any';
                   6942:                             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   6943:                                 if (@{$types} > 0) {
1.257     raeburn  6944:                                     my @esc_types = map { &escape($_); } @{$types};
1.249     raeburn  6945:                                     $othervalue = 'other';
1.258     raeburn  6946:                                     $typestr = join(',',(@esc_types,$othervalue));
1.249     raeburn  6947:                                 }
                   6948:                                 $typestr = $othervalue;
                   6949:                             } else {
                   6950:                                 $typestr = $othervalue;
                   6951:                             } 
                   6952:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   6953:                             $newnum ++ ;
                   6954:                         }
                   6955:                     }
1.241     raeburn  6956:                     my $selfenroll_types = join(';',@latesttypes);
                   6957:                     if ($selfenroll_types ne $curr_types) {
                   6958:                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
                   6959:                     }
                   6960:                 }
1.276     raeburn  6961:             } elsif ($item eq 'limit') {
                   6962:                 my $newlimit = $env{'form.selfenroll_limit'};
                   6963:                 my $newcap = $env{'form.selfenroll_cap'};
                   6964:                 $newcap =~s/\s+//g;
                   6965:                 my $currlimit =  $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_limit'};
                   6966:                 $currlimit = 'none' if ($currlimit eq '');
                   6967:                 my $currcap = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_cap'};
                   6968:                 if ($newlimit ne $currlimit) {
                   6969:                     if ($newlimit ne 'none') {
                   6970:                         if ($newcap =~ /^\d+$/) {
                   6971:                             if ($newcap ne $currcap) {
                   6972:                                 $changes{'internal.selfenroll_cap'} = $newcap;
                   6973:                             }
                   6974:                             $changes{'internal.selfenroll_limit'} = $newlimit;
                   6975:                         } else {
                   6976:                             $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.'); 
                   6977:                         }
                   6978:                     } elsif ($currcap ne '') {
                   6979:                         $changes{'internal.selfenroll_cap'} = '';
                   6980:                         $changes{'internal.selfenroll_limit'} = $newlimit; 
                   6981:                     }
                   6982:                 } elsif ($currlimit ne 'none') {
                   6983:                     if ($newcap =~ /^\d+$/) {
                   6984:                         if ($newcap ne $currcap) {
                   6985:                             $changes{'internal.selfenroll_cap'} = $newcap;
                   6986:                         }
                   6987:                     } else {
                   6988:                         $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.');
                   6989:                     }
                   6990:                 }
                   6991:             } elsif ($item eq 'approval') {
                   6992:                 my (@currnotified,@newnotified);
                   6993:                 my $currapproval = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'};
                   6994:                 my $currnotifylist = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_notifylist'};
                   6995:                 if ($currnotifylist ne '') {
                   6996:                     @currnotified = split(/,/,$currnotifylist);
                   6997:                     @currnotified = sort(@currnotified);
                   6998:                 }
                   6999:                 my $newapproval = $env{'form.selfenroll_approval'};
                   7000:                 @newnotified = &Apache::loncommon::get_env_multiple('form.selfenroll_notify');
                   7001:                 @newnotified = sort(@newnotified);
                   7002:                 if ($newapproval ne $currapproval) {
                   7003:                     $changes{'internal.selfenroll_approval'} = $newapproval;
                   7004:                     if (!$newapproval) {
                   7005:                         if ($currnotifylist ne '') {
                   7006:                             $changes{'internal.selfenroll_notifylist'} = '';
                   7007:                         }
                   7008:                     } else {
                   7009:                         my @differences =  
1.295     raeburn  7010:                             &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  7011:                         if (@differences > 0) {
                   7012:                             if (@newnotified > 0) {
                   7013:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   7014:                             } else {
                   7015:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   7016:                             }
                   7017:                         }
                   7018:                     }
                   7019:                 } else {
1.295     raeburn  7020:                     my @differences = &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  7021:                     if (@differences > 0) {
                   7022:                         if (@newnotified > 0) {
                   7023:                             $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   7024:                         } else {
                   7025:                             $changes{'internal.selfenroll_notifylist'} = '';
                   7026:                         }
                   7027:                     }
                   7028:                 }
1.237     raeburn  7029:             } else {
                   7030:                 my $curr_val = 
                   7031:                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$item};
                   7032:                 my $newval = $env{'form.selfenroll_'.$item};
                   7033:                 if ($item eq 'section') {
                   7034:                     $newval = $env{'form.sections'};
1.241     raeburn  7035:                     if (defined($curr_groups{$newval})) {
1.237     raeburn  7036:                         $newval = $curr_val;
                   7037:                         $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');
                   7038:                     } elsif ($newval eq 'all') {
                   7039:                         $newval = $curr_val;
1.274     bisitz   7040:                         $warning{$item} = &mt('Section for self-enrolled users unchanged, as "all" is a reserved section name.');
1.237     raeburn  7041:                     }
                   7042:                     if ($newval eq '') {
                   7043:                         $newval = 'none';
                   7044:                     }
                   7045:                 }
                   7046:                 if ($newval ne $curr_val) {
                   7047:                     $changes{'internal.selfenroll_'.$item} = $newval;
                   7048:                 }
1.241     raeburn  7049:             }
1.237     raeburn  7050:         }
                   7051:         if (keys(%warning) > 0) {
                   7052:             foreach my $item (@{$row}) {
                   7053:                 if (exists($warning{$item})) {
                   7054:                     $r->print($warning{$item}.'<br />');
                   7055:                 }
                   7056:             } 
                   7057:         }
                   7058:         if (keys(%changes) > 0) {
                   7059:             my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
                   7060:             if ($putresult eq 'ok') {
                   7061:                 if ((exists($changes{'internal.selfenroll_types'})) ||
                   7062:                     (exists($changes{'internal.selfenroll_start_date'}))  ||
                   7063:                     (exists($changes{'internal.selfenroll_end_date'}))) {
                   7064:                     my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
                   7065:                                                                 $cnum,undef,undef,'Course');
                   7066:                     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
                   7067:                     if (ref($crsinfo{$env{'request.course.id'}}) eq 'HASH') {
                   7068:                         foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
                   7069:                             if (exists($changes{'internal.'.$item})) {
                   7070:                                 $crsinfo{$env{'request.course.id'}}{$item} = 
                   7071:                                     $changes{'internal.'.$item};
                   7072:                             }
                   7073:                         }
                   7074:                         my $crsputresult =
                   7075:                             &Apache::lonnet::courseidput($cdom,\%crsinfo,
                   7076:                                                          $chome,'notime');
                   7077:                     }
                   7078:                 }
                   7079:                 $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
                   7080:                 foreach my $item (@{$row}) {
                   7081:                     my $title = $item;
                   7082:                     if (ref($lt) eq 'HASH') {
                   7083:                         $title = $lt->{$item};
                   7084:                     }
                   7085:                     if ($item eq 'enroll_dates') {
                   7086:                         foreach my $type ('start','end') {
                   7087:                             if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
                   7088:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
1.244     bisitz   7089:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  7090:                                           $title,$type,$newdate).'</li>');
                   7091:                             }
                   7092:                         }
                   7093:                     } elsif ($item eq 'access_dates') {
                   7094:                         foreach my $type ('start','end') {
                   7095:                             if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
                   7096:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
1.244     bisitz   7097:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  7098:                                           $title,$type,$newdate).'</li>');
                   7099:                             }
                   7100:                         }
1.276     raeburn  7101:                     } elsif ($item eq 'limit') {
                   7102:                         if ((exists($changes{'internal.selfenroll_limit'})) ||
                   7103:                             (exists($changes{'internal.selfenroll_cap'}))) {
                   7104:                             my ($newval,$newcap);
                   7105:                             if ($changes{'internal.selfenroll_cap'} ne '') {
                   7106:                                 $newcap = $changes{'internal.selfenroll_cap'}
                   7107:                             } else {
                   7108:                                 $newcap = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_cap'};
                   7109:                             }
                   7110:                             if ($changes{'internal.selfenroll_limit'} eq 'none') {
                   7111:                                 $newval = &mt('No limit');
                   7112:                             } elsif ($changes{'internal.selfenroll_limit'} eq 
                   7113:                                      'allstudents') {
                   7114:                                 $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   7115:                             } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
                   7116:                                 $newval = &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
                   7117:                             } else {
                   7118:                                 my $currlimit =  $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_limit'};
                   7119:                                 if ($currlimit eq 'allstudents') {
                   7120:                                     $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   7121:                                 } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
1.308     raeburn  7122:                                     $newval =  &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
1.276     raeburn  7123:                                 }
                   7124:                             }
                   7125:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
                   7126:                         }
                   7127:                     } elsif ($item eq 'approval') {
                   7128:                         if ((exists($changes{'internal.selfenroll_approval'})) ||
                   7129:                             (exists($changes{'internal.selfenroll_notifylist'}))) {
                   7130:                             my ($newval,$newnotify);
                   7131:                             if (exists($changes{'internal.selfenroll_notifylist'})) {
                   7132:                                 $newnotify = $changes{'internal.selfenroll_notifylist'};
                   7133:                             } else {   
                   7134:                                 $newnotify = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_notifylist'};
                   7135:                             }
                   7136:                             if ($changes{'internal.selfenroll_approval'}) {
                   7137:                                 $newval = &mt('Yes');
                   7138:                             } elsif ($changes{'internal.selfenroll_approval'} eq '0') {
                   7139:                                 $newval = &mt('No');
                   7140:                             } else {
                   7141:                                 my $currapproval = 
                   7142:                                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'};
                   7143:                                 if ($currapproval) {
                   7144:                                     $newval = &mt('Yes');
                   7145:                                 } else {
                   7146:                                     $newval = &mt('No');
                   7147:                                 }
                   7148:                             }
                   7149:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval));
                   7150:                             if ($newnotify) {
1.277     raeburn  7151:                                 $r->print('<br />'.&mt('The following will be notified when an enrollment request needs approval, or has been approved: [_1].',$newnotify));
1.276     raeburn  7152:                             } else {
1.277     raeburn  7153:                                 $r->print('<br />'.&mt('No notifications sent when an enrollment request needs approval, or has been approved.'));
1.276     raeburn  7154:                             }
                   7155:                             $r->print('</li>'."\n");
                   7156:                         }
1.237     raeburn  7157:                     } else {
                   7158:                         if (exists($changes{'internal.selfenroll_'.$item})) {
1.241     raeburn  7159:                             my $newval = $changes{'internal.selfenroll_'.$item};
                   7160:                             if ($item eq 'types') {
                   7161:                                 if ($newval eq '') {
                   7162:                                     $newval = &mt('None');
                   7163:                                 } elsif ($newval eq '*') {
                   7164:                                     $newval = &mt('Any user in any domain');
                   7165:                                 }
1.245     raeburn  7166:                             } elsif ($item eq 'registered') {
                   7167:                                 if ($newval eq '1') {
                   7168:                                     $newval = &mt('Yes');
                   7169:                                 } elsif ($newval eq '0') {
                   7170:                                     $newval = &mt('No');
                   7171:                                 }
1.241     raeburn  7172:                             }
1.244     bisitz   7173:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
1.237     raeburn  7174:                         }
                   7175:                     }
                   7176:                 }
                   7177:                 $r->print('</ul>');
                   7178:                 my %newenvhash;
                   7179:                 foreach my $key (keys(%changes)) {
                   7180:                     $newenvhash{'course.'.$env{'request.course.id'}.'.'.$key} = $changes{$key};
                   7181:                 }
1.238     raeburn  7182:                 &Apache::lonnet::appenv(\%newenvhash);
1.237     raeburn  7183:             } else {
                   7184:                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.&mt('The error was: [_1].',$putresult));
                   7185:             }
                   7186:         } else {
1.249     raeburn  7187:             $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.237     raeburn  7188:         }
                   7189:     } else {
1.249     raeburn  7190:         $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.241     raeburn  7191:     }
1.256     raeburn  7192:     my ($visible,$cansetvis,$vismsgs,$visactions) = &visible_in_cat($cdom,$cnum);
                   7193:     if (ref($visactions) eq 'HASH') {
                   7194:         if (!$visible) {
1.366     bisitz   7195:             $r->print('<br /><span class="LC_warning">'.$visactions->{'miss'}.'</span><br />'.$visactions->{'yous'}.
1.256     raeburn  7196:                       '<br />');
                   7197:             if (ref($vismsgs) eq 'ARRAY') {
                   7198:                 $r->print('<br />'.$visactions->{'take'}.'<ul>');
                   7199:                 foreach my $item (@{$vismsgs}) {
                   7200:                     $r->print('<li>'.$visactions->{$item}.'</li>');
                   7201:                 }
                   7202:                 $r->print('</ul>');
                   7203:             }
                   7204:             $r->print($cansetvis);
                   7205:         }
                   7206:     } 
1.237     raeburn  7207:     return;
                   7208: }
                   7209: 
                   7210: sub get_selfenroll_titles {
1.276     raeburn  7211:     my @row = ('types','registered','enroll_dates','access_dates','section',
                   7212:                'approval','limit');
1.237     raeburn  7213:     my %lt = &Apache::lonlocal::texthash (
                   7214:                 types        => 'Users allowed to self-enroll in this course',
1.245     raeburn  7215:                 registered   => 'Restrict self-enrollment to students officially registered for the course',
1.237     raeburn  7216:                 enroll_dates => 'Dates self-enrollment available',
1.256     raeburn  7217:                 access_dates => 'Course access dates assigned to self-enrolling users',
                   7218:                 section      => 'Section assigned to self-enrolling users',
1.276     raeburn  7219:                 approval     => 'Self-enrollment requests need approval?',
                   7220:                 limit        => 'Enrollment limit',
1.237     raeburn  7221:              );
                   7222:     return (\@row,\%lt);
                   7223: }
                   7224: 
1.27      matthew  7225: #---------------------------------------------- end functions for &phase_two
1.29      matthew  7226: 
                   7227: #--------------------------------- functions for &phase_two and &phase_three
                   7228: 
                   7229: #--------------------------end of functions for &phase_two and &phase_three
1.372     raeburn  7230: 
1.1       www      7231: 1;
                   7232: __END__
1.2       www      7233: 
                   7234: 

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